1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // core.c -- Voltage/Current Regulator framework.
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
8 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/async.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/coupler.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/module.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
36 #define rdev_crit(rdev, fmt, ...) \
37 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_err(rdev, fmt, ...) \
39 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_warn(rdev, fmt, ...) \
41 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_info(rdev, fmt, ...) \
43 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_dbg(rdev, fmt, ...) \
45 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 static DEFINE_WW_CLASS(regulator_ww_class);
48 static DEFINE_MUTEX(regulator_nesting_mutex);
49 static DEFINE_MUTEX(regulator_list_mutex);
50 static LIST_HEAD(regulator_map_list);
51 static LIST_HEAD(regulator_ena_gpio_list);
52 static LIST_HEAD(regulator_supply_alias_list);
53 static LIST_HEAD(regulator_coupler_list);
54 static bool has_full_constraints;
56 static struct dentry *debugfs_root;
59 * struct regulator_map
61 * Used to provide symbolic supply names to devices.
63 struct regulator_map {
64 struct list_head list;
65 const char *dev_name; /* The dev_name() for the consumer */
67 struct regulator_dev *regulator;
71 * struct regulator_enable_gpio
73 * Management for shared enable GPIO pin
75 struct regulator_enable_gpio {
76 struct list_head list;
77 struct gpio_desc *gpiod;
78 u32 enable_count; /* a number of enabled shared GPIO */
79 u32 request_count; /* a number of requested shared GPIO */
83 * struct regulator_supply_alias
85 * Used to map lookups for a supply onto an alternative device.
87 struct regulator_supply_alias {
88 struct list_head list;
89 struct device *src_dev;
90 const char *src_supply;
91 struct device *alias_dev;
92 const char *alias_supply;
95 static int _regulator_is_enabled(struct regulator_dev *rdev);
96 static int _regulator_disable(struct regulator *regulator);
97 static int _regulator_get_current_limit(struct regulator_dev *rdev);
98 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
99 static int _notifier_call_chain(struct regulator_dev *rdev,
100 unsigned long event, void *data);
101 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
102 int min_uV, int max_uV);
103 static int regulator_balance_voltage(struct regulator_dev *rdev,
104 suspend_state_t state);
105 static struct regulator *create_regulator(struct regulator_dev *rdev,
107 const char *supply_name);
108 static void destroy_regulator(struct regulator *regulator);
109 static void _regulator_put(struct regulator *regulator);
111 const char *rdev_get_name(struct regulator_dev *rdev)
113 if (rdev->constraints && rdev->constraints->name)
114 return rdev->constraints->name;
115 else if (rdev->desc->name)
116 return rdev->desc->name;
121 static bool have_full_constraints(void)
123 return has_full_constraints || of_have_populated_dt();
126 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
128 if (!rdev->constraints) {
129 rdev_err(rdev, "no constraints\n");
133 if (rdev->constraints->valid_ops_mask & ops)
140 * regulator_lock_nested - lock a single regulator
141 * @rdev: regulator source
142 * @ww_ctx: w/w mutex acquire context
144 * This function can be called many times by one task on
145 * a single regulator and its mutex will be locked only
146 * once. If a task, which is calling this function is other
147 * than the one, which initially locked the mutex, it will
150 static inline int regulator_lock_nested(struct regulator_dev *rdev,
151 struct ww_acquire_ctx *ww_ctx)
156 mutex_lock(®ulator_nesting_mutex);
158 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
159 if (rdev->mutex_owner == current)
165 mutex_unlock(®ulator_nesting_mutex);
166 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
167 mutex_lock(®ulator_nesting_mutex);
173 if (lock && ret != -EDEADLK) {
175 rdev->mutex_owner = current;
178 mutex_unlock(®ulator_nesting_mutex);
184 * regulator_lock - lock a single regulator
185 * @rdev: regulator source
187 * This function can be called many times by one task on
188 * a single regulator and its mutex will be locked only
189 * once. If a task, which is calling this function is other
190 * than the one, which initially locked the mutex, it will
193 void regulator_lock(struct regulator_dev *rdev)
195 regulator_lock_nested(rdev, NULL);
197 EXPORT_SYMBOL_GPL(regulator_lock);
200 * regulator_unlock - unlock a single regulator
201 * @rdev: regulator_source
203 * This function unlocks the mutex when the
204 * reference counter reaches 0.
206 void regulator_unlock(struct regulator_dev *rdev)
208 mutex_lock(®ulator_nesting_mutex);
210 if (--rdev->ref_cnt == 0) {
211 rdev->mutex_owner = NULL;
212 ww_mutex_unlock(&rdev->mutex);
215 WARN_ON_ONCE(rdev->ref_cnt < 0);
217 mutex_unlock(®ulator_nesting_mutex);
219 EXPORT_SYMBOL_GPL(regulator_unlock);
221 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
223 struct regulator_dev *c_rdev;
226 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
227 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
229 if (rdev->supply->rdev == c_rdev)
236 static void regulator_unlock_recursive(struct regulator_dev *rdev,
237 unsigned int n_coupled)
239 struct regulator_dev *c_rdev;
242 for (i = n_coupled; i > 0; i--) {
243 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
248 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
249 regulator_unlock_recursive(
250 c_rdev->supply->rdev,
251 c_rdev->coupling_desc.n_coupled);
253 regulator_unlock(c_rdev);
257 static int regulator_lock_recursive(struct regulator_dev *rdev,
258 struct regulator_dev **new_contended_rdev,
259 struct regulator_dev **old_contended_rdev,
260 struct ww_acquire_ctx *ww_ctx)
262 struct regulator_dev *c_rdev;
265 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
266 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
271 if (c_rdev != *old_contended_rdev) {
272 err = regulator_lock_nested(c_rdev, ww_ctx);
274 if (err == -EDEADLK) {
275 *new_contended_rdev = c_rdev;
279 /* shouldn't happen */
280 WARN_ON_ONCE(err != -EALREADY);
283 *old_contended_rdev = NULL;
286 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
287 err = regulator_lock_recursive(c_rdev->supply->rdev,
292 regulator_unlock(c_rdev);
301 regulator_unlock_recursive(rdev, i);
307 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
309 * @rdev: regulator source
310 * @ww_ctx: w/w mutex acquire context
312 * Unlock all regulators related with rdev by coupling or supplying.
314 static void regulator_unlock_dependent(struct regulator_dev *rdev,
315 struct ww_acquire_ctx *ww_ctx)
317 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
318 ww_acquire_fini(ww_ctx);
322 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
323 * @rdev: regulator source
324 * @ww_ctx: w/w mutex acquire context
326 * This function as a wrapper on regulator_lock_recursive(), which locks
327 * all regulators related with rdev by coupling or supplying.
329 static void regulator_lock_dependent(struct regulator_dev *rdev,
330 struct ww_acquire_ctx *ww_ctx)
332 struct regulator_dev *new_contended_rdev = NULL;
333 struct regulator_dev *old_contended_rdev = NULL;
336 mutex_lock(®ulator_list_mutex);
338 ww_acquire_init(ww_ctx, ®ulator_ww_class);
341 if (new_contended_rdev) {
342 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
343 old_contended_rdev = new_contended_rdev;
344 old_contended_rdev->ref_cnt++;
347 err = regulator_lock_recursive(rdev,
352 if (old_contended_rdev)
353 regulator_unlock(old_contended_rdev);
355 } while (err == -EDEADLK);
357 ww_acquire_done(ww_ctx);
359 mutex_unlock(®ulator_list_mutex);
363 * of_get_child_regulator - get a child regulator device node
364 * based on supply name
365 * @parent: Parent device node
366 * @prop_name: Combination regulator supply name and "-supply"
368 * Traverse all child nodes.
369 * Extract the child regulator device node corresponding to the supply name.
370 * returns the device node corresponding to the regulator if found, else
373 static struct device_node *of_get_child_regulator(struct device_node *parent,
374 const char *prop_name)
376 struct device_node *regnode = NULL;
377 struct device_node *child = NULL;
379 for_each_child_of_node(parent, child) {
380 regnode = of_parse_phandle(child, prop_name, 0);
383 regnode = of_get_child_regulator(child, prop_name);
398 * of_get_regulator - get a regulator device node based on supply name
399 * @dev: Device pointer for the consumer (of regulator) device
400 * @supply: regulator supply name
402 * Extract the regulator device node corresponding to the supply name.
403 * returns the device node corresponding to the regulator if found, else
406 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
408 struct device_node *regnode = NULL;
409 char prop_name[32]; /* 32 is max size of property name */
411 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
413 snprintf(prop_name, 32, "%s-supply", supply);
414 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
417 regnode = of_get_child_regulator(dev->of_node, prop_name);
421 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
422 prop_name, dev->of_node);
428 /* Platform voltage constraint check */
429 int regulator_check_voltage(struct regulator_dev *rdev,
430 int *min_uV, int *max_uV)
432 BUG_ON(*min_uV > *max_uV);
434 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
435 rdev_err(rdev, "voltage operation not allowed\n");
439 if (*max_uV > rdev->constraints->max_uV)
440 *max_uV = rdev->constraints->max_uV;
441 if (*min_uV < rdev->constraints->min_uV)
442 *min_uV = rdev->constraints->min_uV;
444 if (*min_uV > *max_uV) {
445 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
453 /* return 0 if the state is valid */
454 static int regulator_check_states(suspend_state_t state)
456 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
459 /* Make sure we select a voltage that suits the needs of all
460 * regulator consumers
462 int regulator_check_consumers(struct regulator_dev *rdev,
463 int *min_uV, int *max_uV,
464 suspend_state_t state)
466 struct regulator *regulator;
467 struct regulator_voltage *voltage;
469 list_for_each_entry(regulator, &rdev->consumer_list, list) {
470 voltage = ®ulator->voltage[state];
472 * Assume consumers that didn't say anything are OK
473 * with anything in the constraint range.
475 if (!voltage->min_uV && !voltage->max_uV)
478 if (*max_uV > voltage->max_uV)
479 *max_uV = voltage->max_uV;
480 if (*min_uV < voltage->min_uV)
481 *min_uV = voltage->min_uV;
484 if (*min_uV > *max_uV) {
485 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
493 /* current constraint check */
494 static int regulator_check_current_limit(struct regulator_dev *rdev,
495 int *min_uA, int *max_uA)
497 BUG_ON(*min_uA > *max_uA);
499 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
500 rdev_err(rdev, "current operation not allowed\n");
504 if (*max_uA > rdev->constraints->max_uA)
505 *max_uA = rdev->constraints->max_uA;
506 if (*min_uA < rdev->constraints->min_uA)
507 *min_uA = rdev->constraints->min_uA;
509 if (*min_uA > *max_uA) {
510 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
518 /* operating mode constraint check */
519 static int regulator_mode_constrain(struct regulator_dev *rdev,
523 case REGULATOR_MODE_FAST:
524 case REGULATOR_MODE_NORMAL:
525 case REGULATOR_MODE_IDLE:
526 case REGULATOR_MODE_STANDBY:
529 rdev_err(rdev, "invalid mode %x specified\n", *mode);
533 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
534 rdev_err(rdev, "mode operation not allowed\n");
538 /* The modes are bitmasks, the most power hungry modes having
539 * the lowest values. If the requested mode isn't supported
540 * try higher modes. */
542 if (rdev->constraints->valid_modes_mask & *mode)
550 static inline struct regulator_state *
551 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
553 if (rdev->constraints == NULL)
557 case PM_SUSPEND_STANDBY:
558 return &rdev->constraints->state_standby;
560 return &rdev->constraints->state_mem;
562 return &rdev->constraints->state_disk;
568 static ssize_t regulator_uV_show(struct device *dev,
569 struct device_attribute *attr, char *buf)
571 struct regulator_dev *rdev = dev_get_drvdata(dev);
574 regulator_lock(rdev);
575 uV = regulator_get_voltage_rdev(rdev);
576 regulator_unlock(rdev);
580 return sprintf(buf, "%d\n", uV);
582 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
584 static ssize_t regulator_uA_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
587 struct regulator_dev *rdev = dev_get_drvdata(dev);
589 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
591 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
593 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
596 struct regulator_dev *rdev = dev_get_drvdata(dev);
598 return sprintf(buf, "%s\n", rdev_get_name(rdev));
600 static DEVICE_ATTR_RO(name);
602 static const char *regulator_opmode_to_str(int mode)
605 case REGULATOR_MODE_FAST:
607 case REGULATOR_MODE_NORMAL:
609 case REGULATOR_MODE_IDLE:
611 case REGULATOR_MODE_STANDBY:
617 static ssize_t regulator_print_opmode(char *buf, int mode)
619 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
622 static ssize_t regulator_opmode_show(struct device *dev,
623 struct device_attribute *attr, char *buf)
625 struct regulator_dev *rdev = dev_get_drvdata(dev);
627 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
629 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
631 static ssize_t regulator_print_state(char *buf, int state)
634 return sprintf(buf, "enabled\n");
636 return sprintf(buf, "disabled\n");
638 return sprintf(buf, "unknown\n");
641 static ssize_t regulator_state_show(struct device *dev,
642 struct device_attribute *attr, char *buf)
644 struct regulator_dev *rdev = dev_get_drvdata(dev);
647 regulator_lock(rdev);
648 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
649 regulator_unlock(rdev);
653 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
655 static ssize_t regulator_status_show(struct device *dev,
656 struct device_attribute *attr, char *buf)
658 struct regulator_dev *rdev = dev_get_drvdata(dev);
662 status = rdev->desc->ops->get_status(rdev);
667 case REGULATOR_STATUS_OFF:
670 case REGULATOR_STATUS_ON:
673 case REGULATOR_STATUS_ERROR:
676 case REGULATOR_STATUS_FAST:
679 case REGULATOR_STATUS_NORMAL:
682 case REGULATOR_STATUS_IDLE:
685 case REGULATOR_STATUS_STANDBY:
688 case REGULATOR_STATUS_BYPASS:
691 case REGULATOR_STATUS_UNDEFINED:
698 return sprintf(buf, "%s\n", label);
700 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
702 static ssize_t regulator_min_uA_show(struct device *dev,
703 struct device_attribute *attr, char *buf)
705 struct regulator_dev *rdev = dev_get_drvdata(dev);
707 if (!rdev->constraints)
708 return sprintf(buf, "constraint not defined\n");
710 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
712 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
714 static ssize_t regulator_max_uA_show(struct device *dev,
715 struct device_attribute *attr, char *buf)
717 struct regulator_dev *rdev = dev_get_drvdata(dev);
719 if (!rdev->constraints)
720 return sprintf(buf, "constraint not defined\n");
722 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
724 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
726 static ssize_t regulator_min_uV_show(struct device *dev,
727 struct device_attribute *attr, char *buf)
729 struct regulator_dev *rdev = dev_get_drvdata(dev);
731 if (!rdev->constraints)
732 return sprintf(buf, "constraint not defined\n");
734 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
736 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
738 static ssize_t regulator_max_uV_show(struct device *dev,
739 struct device_attribute *attr, char *buf)
741 struct regulator_dev *rdev = dev_get_drvdata(dev);
743 if (!rdev->constraints)
744 return sprintf(buf, "constraint not defined\n");
746 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
748 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
750 static ssize_t regulator_total_uA_show(struct device *dev,
751 struct device_attribute *attr, char *buf)
753 struct regulator_dev *rdev = dev_get_drvdata(dev);
754 struct regulator *regulator;
757 regulator_lock(rdev);
758 list_for_each_entry(regulator, &rdev->consumer_list, list) {
759 if (regulator->enable_count)
760 uA += regulator->uA_load;
762 regulator_unlock(rdev);
763 return sprintf(buf, "%d\n", uA);
765 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
767 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
770 struct regulator_dev *rdev = dev_get_drvdata(dev);
771 return sprintf(buf, "%d\n", rdev->use_count);
773 static DEVICE_ATTR_RO(num_users);
775 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
778 struct regulator_dev *rdev = dev_get_drvdata(dev);
780 switch (rdev->desc->type) {
781 case REGULATOR_VOLTAGE:
782 return sprintf(buf, "voltage\n");
783 case REGULATOR_CURRENT:
784 return sprintf(buf, "current\n");
786 return sprintf(buf, "unknown\n");
788 static DEVICE_ATTR_RO(type);
790 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
791 struct device_attribute *attr, char *buf)
793 struct regulator_dev *rdev = dev_get_drvdata(dev);
795 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
797 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
798 regulator_suspend_mem_uV_show, NULL);
800 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
801 struct device_attribute *attr, char *buf)
803 struct regulator_dev *rdev = dev_get_drvdata(dev);
805 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
807 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
808 regulator_suspend_disk_uV_show, NULL);
810 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
811 struct device_attribute *attr, char *buf)
813 struct regulator_dev *rdev = dev_get_drvdata(dev);
815 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
817 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
818 regulator_suspend_standby_uV_show, NULL);
820 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
821 struct device_attribute *attr, char *buf)
823 struct regulator_dev *rdev = dev_get_drvdata(dev);
825 return regulator_print_opmode(buf,
826 rdev->constraints->state_mem.mode);
828 static DEVICE_ATTR(suspend_mem_mode, 0444,
829 regulator_suspend_mem_mode_show, NULL);
831 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
832 struct device_attribute *attr, char *buf)
834 struct regulator_dev *rdev = dev_get_drvdata(dev);
836 return regulator_print_opmode(buf,
837 rdev->constraints->state_disk.mode);
839 static DEVICE_ATTR(suspend_disk_mode, 0444,
840 regulator_suspend_disk_mode_show, NULL);
842 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
843 struct device_attribute *attr, char *buf)
845 struct regulator_dev *rdev = dev_get_drvdata(dev);
847 return regulator_print_opmode(buf,
848 rdev->constraints->state_standby.mode);
850 static DEVICE_ATTR(suspend_standby_mode, 0444,
851 regulator_suspend_standby_mode_show, NULL);
853 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
854 struct device_attribute *attr, char *buf)
856 struct regulator_dev *rdev = dev_get_drvdata(dev);
858 return regulator_print_state(buf,
859 rdev->constraints->state_mem.enabled);
861 static DEVICE_ATTR(suspend_mem_state, 0444,
862 regulator_suspend_mem_state_show, NULL);
864 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
865 struct device_attribute *attr, char *buf)
867 struct regulator_dev *rdev = dev_get_drvdata(dev);
869 return regulator_print_state(buf,
870 rdev->constraints->state_disk.enabled);
872 static DEVICE_ATTR(suspend_disk_state, 0444,
873 regulator_suspend_disk_state_show, NULL);
875 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
876 struct device_attribute *attr, char *buf)
878 struct regulator_dev *rdev = dev_get_drvdata(dev);
880 return regulator_print_state(buf,
881 rdev->constraints->state_standby.enabled);
883 static DEVICE_ATTR(suspend_standby_state, 0444,
884 regulator_suspend_standby_state_show, NULL);
886 static ssize_t regulator_bypass_show(struct device *dev,
887 struct device_attribute *attr, char *buf)
889 struct regulator_dev *rdev = dev_get_drvdata(dev);
894 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
903 return sprintf(buf, "%s\n", report);
905 static DEVICE_ATTR(bypass, 0444,
906 regulator_bypass_show, NULL);
908 /* Calculate the new optimum regulator operating mode based on the new total
909 * consumer load. All locks held by caller */
910 static int drms_uA_update(struct regulator_dev *rdev)
912 struct regulator *sibling;
913 int current_uA = 0, output_uV, input_uV, err;
917 * first check to see if we can set modes at all, otherwise just
918 * tell the consumer everything is OK.
920 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
921 rdev_dbg(rdev, "DRMS operation not allowed\n");
925 if (!rdev->desc->ops->get_optimum_mode &&
926 !rdev->desc->ops->set_load)
929 if (!rdev->desc->ops->set_mode &&
930 !rdev->desc->ops->set_load)
933 /* calc total requested load */
934 list_for_each_entry(sibling, &rdev->consumer_list, list) {
935 if (sibling->enable_count)
936 current_uA += sibling->uA_load;
939 current_uA += rdev->constraints->system_load;
941 if (rdev->desc->ops->set_load) {
942 /* set the optimum mode for our new total regulator load */
943 err = rdev->desc->ops->set_load(rdev, current_uA);
945 rdev_err(rdev, "failed to set load %d\n", current_uA);
947 /* get output voltage */
948 output_uV = regulator_get_voltage_rdev(rdev);
949 if (output_uV <= 0) {
950 rdev_err(rdev, "invalid output voltage found\n");
954 /* get input voltage */
957 input_uV = regulator_get_voltage(rdev->supply);
959 input_uV = rdev->constraints->input_uV;
961 rdev_err(rdev, "invalid input voltage found\n");
965 /* now get the optimum mode for our new total regulator load */
966 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
967 output_uV, current_uA);
969 /* check the new mode is allowed */
970 err = regulator_mode_constrain(rdev, &mode);
972 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
973 current_uA, input_uV, output_uV);
977 err = rdev->desc->ops->set_mode(rdev, mode);
979 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
985 static int suspend_set_state(struct regulator_dev *rdev,
986 suspend_state_t state)
989 struct regulator_state *rstate;
991 rstate = regulator_get_suspend_state(rdev, state);
995 /* If we have no suspend mode configuration don't set anything;
996 * only warn if the driver implements set_suspend_voltage or
997 * set_suspend_mode callback.
999 if (rstate->enabled != ENABLE_IN_SUSPEND &&
1000 rstate->enabled != DISABLE_IN_SUSPEND) {
1001 if (rdev->desc->ops->set_suspend_voltage ||
1002 rdev->desc->ops->set_suspend_mode)
1003 rdev_warn(rdev, "No configuration\n");
1007 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1008 rdev->desc->ops->set_suspend_enable)
1009 ret = rdev->desc->ops->set_suspend_enable(rdev);
1010 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1011 rdev->desc->ops->set_suspend_disable)
1012 ret = rdev->desc->ops->set_suspend_disable(rdev);
1013 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1017 rdev_err(rdev, "failed to enabled/disable\n");
1021 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1022 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1024 rdev_err(rdev, "failed to set voltage\n");
1029 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1030 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1032 rdev_err(rdev, "failed to set mode\n");
1040 static void print_constraints(struct regulator_dev *rdev)
1042 struct regulation_constraints *constraints = rdev->constraints;
1044 size_t len = sizeof(buf) - 1;
1048 if (constraints->min_uV && constraints->max_uV) {
1049 if (constraints->min_uV == constraints->max_uV)
1050 count += scnprintf(buf + count, len - count, "%d mV ",
1051 constraints->min_uV / 1000);
1053 count += scnprintf(buf + count, len - count,
1055 constraints->min_uV / 1000,
1056 constraints->max_uV / 1000);
1059 if (!constraints->min_uV ||
1060 constraints->min_uV != constraints->max_uV) {
1061 ret = regulator_get_voltage_rdev(rdev);
1063 count += scnprintf(buf + count, len - count,
1064 "at %d mV ", ret / 1000);
1067 if (constraints->uV_offset)
1068 count += scnprintf(buf + count, len - count, "%dmV offset ",
1069 constraints->uV_offset / 1000);
1071 if (constraints->min_uA && constraints->max_uA) {
1072 if (constraints->min_uA == constraints->max_uA)
1073 count += scnprintf(buf + count, len - count, "%d mA ",
1074 constraints->min_uA / 1000);
1076 count += scnprintf(buf + count, len - count,
1078 constraints->min_uA / 1000,
1079 constraints->max_uA / 1000);
1082 if (!constraints->min_uA ||
1083 constraints->min_uA != constraints->max_uA) {
1084 ret = _regulator_get_current_limit(rdev);
1086 count += scnprintf(buf + count, len - count,
1087 "at %d mA ", ret / 1000);
1090 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1091 count += scnprintf(buf + count, len - count, "fast ");
1092 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1093 count += scnprintf(buf + count, len - count, "normal ");
1094 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1095 count += scnprintf(buf + count, len - count, "idle ");
1096 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1097 count += scnprintf(buf + count, len - count, "standby");
1100 scnprintf(buf, len, "no parameters");
1102 rdev_dbg(rdev, "%s\n", buf);
1104 if ((constraints->min_uV != constraints->max_uV) &&
1105 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1107 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1110 static int machine_constraints_voltage(struct regulator_dev *rdev,
1111 struct regulation_constraints *constraints)
1113 const struct regulator_ops *ops = rdev->desc->ops;
1116 /* do we need to apply the constraint voltage */
1117 if (rdev->constraints->apply_uV &&
1118 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1119 int target_min, target_max;
1120 int current_uV = regulator_get_voltage_rdev(rdev);
1122 if (current_uV == -ENOTRECOVERABLE) {
1123 /* This regulator can't be read and must be initialized */
1124 rdev_info(rdev, "Setting %d-%duV\n",
1125 rdev->constraints->min_uV,
1126 rdev->constraints->max_uV);
1127 _regulator_do_set_voltage(rdev,
1128 rdev->constraints->min_uV,
1129 rdev->constraints->max_uV);
1130 current_uV = regulator_get_voltage_rdev(rdev);
1133 if (current_uV < 0) {
1135 "failed to get the current voltage(%d)\n",
1141 * If we're below the minimum voltage move up to the
1142 * minimum voltage, if we're above the maximum voltage
1143 * then move down to the maximum.
1145 target_min = current_uV;
1146 target_max = current_uV;
1148 if (current_uV < rdev->constraints->min_uV) {
1149 target_min = rdev->constraints->min_uV;
1150 target_max = rdev->constraints->min_uV;
1153 if (current_uV > rdev->constraints->max_uV) {
1154 target_min = rdev->constraints->max_uV;
1155 target_max = rdev->constraints->max_uV;
1158 if (target_min != current_uV || target_max != current_uV) {
1159 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1160 current_uV, target_min, target_max);
1161 ret = _regulator_do_set_voltage(
1162 rdev, target_min, target_max);
1165 "failed to apply %d-%duV constraint(%d)\n",
1166 target_min, target_max, ret);
1172 /* constrain machine-level voltage specs to fit
1173 * the actual range supported by this regulator.
1175 if (ops->list_voltage && rdev->desc->n_voltages) {
1176 int count = rdev->desc->n_voltages;
1178 int min_uV = INT_MAX;
1179 int max_uV = INT_MIN;
1180 int cmin = constraints->min_uV;
1181 int cmax = constraints->max_uV;
1183 /* it's safe to autoconfigure fixed-voltage supplies
1184 and the constraints are used by list_voltage. */
1185 if (count == 1 && !cmin) {
1188 constraints->min_uV = cmin;
1189 constraints->max_uV = cmax;
1192 /* voltage constraints are optional */
1193 if ((cmin == 0) && (cmax == 0))
1196 /* else require explicit machine-level constraints */
1197 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1198 rdev_err(rdev, "invalid voltage constraints\n");
1202 /* no need to loop voltages if range is continuous */
1203 if (rdev->desc->continuous_voltage_range)
1206 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1207 for (i = 0; i < count; i++) {
1210 value = ops->list_voltage(rdev, i);
1214 /* maybe adjust [min_uV..max_uV] */
1215 if (value >= cmin && value < min_uV)
1217 if (value <= cmax && value > max_uV)
1221 /* final: [min_uV..max_uV] valid iff constraints valid */
1222 if (max_uV < min_uV) {
1224 "unsupportable voltage constraints %u-%uuV\n",
1229 /* use regulator's subset of machine constraints */
1230 if (constraints->min_uV < min_uV) {
1231 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1232 constraints->min_uV, min_uV);
1233 constraints->min_uV = min_uV;
1235 if (constraints->max_uV > max_uV) {
1236 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1237 constraints->max_uV, max_uV);
1238 constraints->max_uV = max_uV;
1245 static int machine_constraints_current(struct regulator_dev *rdev,
1246 struct regulation_constraints *constraints)
1248 const struct regulator_ops *ops = rdev->desc->ops;
1251 if (!constraints->min_uA && !constraints->max_uA)
1254 if (constraints->min_uA > constraints->max_uA) {
1255 rdev_err(rdev, "Invalid current constraints\n");
1259 if (!ops->set_current_limit || !ops->get_current_limit) {
1260 rdev_warn(rdev, "Operation of current configuration missing\n");
1264 /* Set regulator current in constraints range */
1265 ret = ops->set_current_limit(rdev, constraints->min_uA,
1266 constraints->max_uA);
1268 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1275 static int _regulator_do_enable(struct regulator_dev *rdev);
1278 * set_machine_constraints - sets regulator constraints
1279 * @rdev: regulator source
1280 * @constraints: constraints to apply
1282 * Allows platform initialisation code to define and constrain
1283 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1284 * Constraints *must* be set by platform code in order for some
1285 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1288 static int set_machine_constraints(struct regulator_dev *rdev,
1289 const struct regulation_constraints *constraints)
1292 const struct regulator_ops *ops = rdev->desc->ops;
1295 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1298 rdev->constraints = kzalloc(sizeof(*constraints),
1300 if (!rdev->constraints)
1303 ret = machine_constraints_voltage(rdev, rdev->constraints);
1307 ret = machine_constraints_current(rdev, rdev->constraints);
1311 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1312 ret = ops->set_input_current_limit(rdev,
1313 rdev->constraints->ilim_uA);
1315 rdev_err(rdev, "failed to set input limit\n");
1320 /* do we need to setup our suspend state */
1321 if (rdev->constraints->initial_state) {
1322 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1324 rdev_err(rdev, "failed to set suspend state\n");
1329 if (rdev->constraints->initial_mode) {
1330 if (!ops->set_mode) {
1331 rdev_err(rdev, "no set_mode operation\n");
1335 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1337 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1340 } else if (rdev->constraints->system_load) {
1342 * We'll only apply the initial system load if an
1343 * initial mode wasn't specified.
1345 drms_uA_update(rdev);
1348 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1349 && ops->set_ramp_delay) {
1350 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1352 rdev_err(rdev, "failed to set ramp_delay\n");
1357 if (rdev->constraints->pull_down && ops->set_pull_down) {
1358 ret = ops->set_pull_down(rdev);
1360 rdev_err(rdev, "failed to set pull down\n");
1365 if (rdev->constraints->soft_start && ops->set_soft_start) {
1366 ret = ops->set_soft_start(rdev);
1368 rdev_err(rdev, "failed to set soft start\n");
1373 if (rdev->constraints->over_current_protection
1374 && ops->set_over_current_protection) {
1375 ret = ops->set_over_current_protection(rdev);
1377 rdev_err(rdev, "failed to set over current protection\n");
1382 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1383 bool ad_state = (rdev->constraints->active_discharge ==
1384 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1386 ret = ops->set_active_discharge(rdev, ad_state);
1388 rdev_err(rdev, "failed to set active discharge\n");
1393 /* If the constraints say the regulator should be on at this point
1394 * and we have control then make sure it is enabled.
1396 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1398 ret = regulator_enable(rdev->supply);
1400 _regulator_put(rdev->supply);
1401 rdev->supply = NULL;
1406 ret = _regulator_do_enable(rdev);
1407 if (ret < 0 && ret != -EINVAL) {
1408 rdev_err(rdev, "failed to enable\n");
1412 if (rdev->constraints->always_on)
1416 print_constraints(rdev);
1421 * set_supply - set regulator supply regulator
1422 * @rdev: regulator name
1423 * @supply_rdev: supply regulator name
1425 * Called by platform initialisation code to set the supply regulator for this
1426 * regulator. This ensures that a regulators supply will also be enabled by the
1427 * core if it's child is enabled.
1429 static int set_supply(struct regulator_dev *rdev,
1430 struct regulator_dev *supply_rdev)
1434 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1436 if (!try_module_get(supply_rdev->owner))
1439 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1440 if (rdev->supply == NULL) {
1444 supply_rdev->open_count++;
1450 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1451 * @rdev: regulator source
1452 * @consumer_dev_name: dev_name() string for device supply applies to
1453 * @supply: symbolic name for supply
1455 * Allows platform initialisation code to map physical regulator
1456 * sources to symbolic names for supplies for use by devices. Devices
1457 * should use these symbolic names to request regulators, avoiding the
1458 * need to provide board-specific regulator names as platform data.
1460 static int set_consumer_device_supply(struct regulator_dev *rdev,
1461 const char *consumer_dev_name,
1464 struct regulator_map *node;
1470 if (consumer_dev_name != NULL)
1475 list_for_each_entry(node, ®ulator_map_list, list) {
1476 if (node->dev_name && consumer_dev_name) {
1477 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1479 } else if (node->dev_name || consumer_dev_name) {
1483 if (strcmp(node->supply, supply) != 0)
1486 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1488 dev_name(&node->regulator->dev),
1489 node->regulator->desc->name,
1491 dev_name(&rdev->dev), rdev_get_name(rdev));
1495 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1499 node->regulator = rdev;
1500 node->supply = supply;
1503 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1504 if (node->dev_name == NULL) {
1510 list_add(&node->list, ®ulator_map_list);
1514 static void unset_regulator_supplies(struct regulator_dev *rdev)
1516 struct regulator_map *node, *n;
1518 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1519 if (rdev == node->regulator) {
1520 list_del(&node->list);
1521 kfree(node->dev_name);
1527 #ifdef CONFIG_DEBUG_FS
1528 static ssize_t constraint_flags_read_file(struct file *file,
1529 char __user *user_buf,
1530 size_t count, loff_t *ppos)
1532 const struct regulator *regulator = file->private_data;
1533 const struct regulation_constraints *c = regulator->rdev->constraints;
1540 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1544 ret = snprintf(buf, PAGE_SIZE,
1548 "ramp_disable: %u\n"
1551 "over_current_protection: %u\n",
1558 c->over_current_protection);
1560 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1568 static const struct file_operations constraint_flags_fops = {
1569 #ifdef CONFIG_DEBUG_FS
1570 .open = simple_open,
1571 .read = constraint_flags_read_file,
1572 .llseek = default_llseek,
1576 #define REG_STR_SIZE 64
1578 static struct regulator *create_regulator(struct regulator_dev *rdev,
1580 const char *supply_name)
1582 struct regulator *regulator;
1583 char buf[REG_STR_SIZE];
1586 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1587 if (regulator == NULL)
1590 regulator_lock(rdev);
1591 regulator->rdev = rdev;
1592 list_add(®ulator->list, &rdev->consumer_list);
1595 regulator->dev = dev;
1597 /* Add a link to the device sysfs entry */
1598 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1599 dev->kobj.name, supply_name);
1600 if (size >= REG_STR_SIZE)
1603 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1604 if (regulator->supply_name == NULL)
1607 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1610 rdev_dbg(rdev, "could not add device link %s err %d\n",
1611 dev->kobj.name, err);
1615 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1616 if (regulator->supply_name == NULL)
1620 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1622 if (!regulator->debugfs) {
1623 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1625 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1626 ®ulator->uA_load);
1627 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1628 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1629 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1630 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1631 debugfs_create_file("constraint_flags", 0444,
1632 regulator->debugfs, regulator,
1633 &constraint_flags_fops);
1637 * Check now if the regulator is an always on regulator - if
1638 * it is then we don't need to do nearly so much work for
1639 * enable/disable calls.
1641 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1642 _regulator_is_enabled(rdev))
1643 regulator->always_on = true;
1645 regulator_unlock(rdev);
1648 list_del(®ulator->list);
1650 regulator_unlock(rdev);
1654 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1656 if (rdev->constraints && rdev->constraints->enable_time)
1657 return rdev->constraints->enable_time;
1658 if (rdev->desc->ops->enable_time)
1659 return rdev->desc->ops->enable_time(rdev);
1660 return rdev->desc->enable_time;
1663 static struct regulator_supply_alias *regulator_find_supply_alias(
1664 struct device *dev, const char *supply)
1666 struct regulator_supply_alias *map;
1668 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1669 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1675 static void regulator_supply_alias(struct device **dev, const char **supply)
1677 struct regulator_supply_alias *map;
1679 map = regulator_find_supply_alias(*dev, *supply);
1681 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1682 *supply, map->alias_supply,
1683 dev_name(map->alias_dev));
1684 *dev = map->alias_dev;
1685 *supply = map->alias_supply;
1689 static int regulator_match(struct device *dev, const void *data)
1691 struct regulator_dev *r = dev_to_rdev(dev);
1693 return strcmp(rdev_get_name(r), data) == 0;
1696 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1700 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1702 return dev ? dev_to_rdev(dev) : NULL;
1706 * regulator_dev_lookup - lookup a regulator device.
1707 * @dev: device for regulator "consumer".
1708 * @supply: Supply name or regulator ID.
1710 * If successful, returns a struct regulator_dev that corresponds to the name
1711 * @supply and with the embedded struct device refcount incremented by one.
1712 * The refcount must be dropped by calling put_device().
1713 * On failure one of the following ERR-PTR-encoded values is returned:
1714 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1717 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1720 struct regulator_dev *r = NULL;
1721 struct device_node *node;
1722 struct regulator_map *map;
1723 const char *devname = NULL;
1725 regulator_supply_alias(&dev, &supply);
1727 /* first do a dt based lookup */
1728 if (dev && dev->of_node) {
1729 node = of_get_regulator(dev, supply);
1731 r = of_find_regulator_by_node(node);
1736 * We have a node, but there is no device.
1737 * assume it has not registered yet.
1739 return ERR_PTR(-EPROBE_DEFER);
1743 /* if not found, try doing it non-dt way */
1745 devname = dev_name(dev);
1747 mutex_lock(®ulator_list_mutex);
1748 list_for_each_entry(map, ®ulator_map_list, list) {
1749 /* If the mapping has a device set up it must match */
1750 if (map->dev_name &&
1751 (!devname || strcmp(map->dev_name, devname)))
1754 if (strcmp(map->supply, supply) == 0 &&
1755 get_device(&map->regulator->dev)) {
1760 mutex_unlock(®ulator_list_mutex);
1765 r = regulator_lookup_by_name(supply);
1769 return ERR_PTR(-ENODEV);
1772 static int regulator_resolve_supply(struct regulator_dev *rdev)
1774 struct regulator_dev *r;
1775 struct device *dev = rdev->dev.parent;
1778 /* No supply to resolve? */
1779 if (!rdev->supply_name)
1782 /* Supply already resolved? */
1786 r = regulator_dev_lookup(dev, rdev->supply_name);
1790 /* Did the lookup explicitly defer for us? */
1791 if (ret == -EPROBE_DEFER)
1794 if (have_full_constraints()) {
1795 r = dummy_regulator_rdev;
1796 get_device(&r->dev);
1798 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1799 rdev->supply_name, rdev->desc->name);
1800 return -EPROBE_DEFER;
1805 * If the supply's parent device is not the same as the
1806 * regulator's parent device, then ensure the parent device
1807 * is bound before we resolve the supply, in case the parent
1808 * device get probe deferred and unregisters the supply.
1810 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1811 if (!device_is_bound(r->dev.parent)) {
1812 put_device(&r->dev);
1813 return -EPROBE_DEFER;
1817 /* Recursively resolve the supply of the supply */
1818 ret = regulator_resolve_supply(r);
1820 put_device(&r->dev);
1824 ret = set_supply(rdev, r);
1826 put_device(&r->dev);
1831 * In set_machine_constraints() we may have turned this regulator on
1832 * but we couldn't propagate to the supply if it hadn't been resolved
1835 if (rdev->use_count) {
1836 ret = regulator_enable(rdev->supply);
1838 _regulator_put(rdev->supply);
1839 rdev->supply = NULL;
1847 /* Internal regulator request function */
1848 struct regulator *_regulator_get(struct device *dev, const char *id,
1849 enum regulator_get_type get_type)
1851 struct regulator_dev *rdev;
1852 struct regulator *regulator;
1853 struct device_link *link;
1856 if (get_type >= MAX_GET_TYPE) {
1857 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1858 return ERR_PTR(-EINVAL);
1862 pr_err("get() with no identifier\n");
1863 return ERR_PTR(-EINVAL);
1866 rdev = regulator_dev_lookup(dev, id);
1868 ret = PTR_ERR(rdev);
1871 * If regulator_dev_lookup() fails with error other
1872 * than -ENODEV our job here is done, we simply return it.
1875 return ERR_PTR(ret);
1877 if (!have_full_constraints()) {
1879 "incomplete constraints, dummy supplies not allowed\n");
1880 return ERR_PTR(-ENODEV);
1886 * Assume that a regulator is physically present and
1887 * enabled, even if it isn't hooked up, and just
1890 dev_warn(dev, "supply %s not found, using dummy regulator\n", id);
1891 rdev = dummy_regulator_rdev;
1892 get_device(&rdev->dev);
1897 "dummy supplies not allowed for exclusive requests\n");
1901 return ERR_PTR(-ENODEV);
1905 if (rdev->exclusive) {
1906 regulator = ERR_PTR(-EPERM);
1907 put_device(&rdev->dev);
1911 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1912 regulator = ERR_PTR(-EBUSY);
1913 put_device(&rdev->dev);
1917 mutex_lock(®ulator_list_mutex);
1918 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1919 mutex_unlock(®ulator_list_mutex);
1922 regulator = ERR_PTR(-EPROBE_DEFER);
1923 put_device(&rdev->dev);
1927 ret = regulator_resolve_supply(rdev);
1929 regulator = ERR_PTR(ret);
1930 put_device(&rdev->dev);
1934 if (!try_module_get(rdev->owner)) {
1935 regulator = ERR_PTR(-EPROBE_DEFER);
1936 put_device(&rdev->dev);
1940 regulator = create_regulator(rdev, dev, id);
1941 if (regulator == NULL) {
1942 regulator = ERR_PTR(-ENOMEM);
1943 module_put(rdev->owner);
1944 put_device(&rdev->dev);
1949 if (get_type == EXCLUSIVE_GET) {
1950 rdev->exclusive = 1;
1952 ret = _regulator_is_enabled(rdev);
1954 rdev->use_count = 1;
1956 rdev->use_count = 0;
1959 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1960 if (!IS_ERR_OR_NULL(link))
1961 regulator->device_link = true;
1967 * regulator_get - lookup and obtain a reference to a regulator.
1968 * @dev: device for regulator "consumer"
1969 * @id: Supply name or regulator ID.
1971 * Returns a struct regulator corresponding to the regulator producer,
1972 * or IS_ERR() condition containing errno.
1974 * Use of supply names configured via regulator_set_device_supply() is
1975 * strongly encouraged. It is recommended that the supply name used
1976 * should match the name used for the supply and/or the relevant
1977 * device pins in the datasheet.
1979 struct regulator *regulator_get(struct device *dev, const char *id)
1981 return _regulator_get(dev, id, NORMAL_GET);
1983 EXPORT_SYMBOL_GPL(regulator_get);
1986 * regulator_get_exclusive - obtain exclusive access to a regulator.
1987 * @dev: device for regulator "consumer"
1988 * @id: Supply name or regulator ID.
1990 * Returns a struct regulator corresponding to the regulator producer,
1991 * or IS_ERR() condition containing errno. Other consumers will be
1992 * unable to obtain this regulator while this reference is held and the
1993 * use count for the regulator will be initialised to reflect the current
1994 * state of the regulator.
1996 * This is intended for use by consumers which cannot tolerate shared
1997 * use of the regulator such as those which need to force the
1998 * regulator off for correct operation of the hardware they are
2001 * Use of supply names configured via regulator_set_device_supply() is
2002 * strongly encouraged. It is recommended that the supply name used
2003 * should match the name used for the supply and/or the relevant
2004 * device pins in the datasheet.
2006 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2008 return _regulator_get(dev, id, EXCLUSIVE_GET);
2010 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2013 * regulator_get_optional - obtain optional access to a regulator.
2014 * @dev: device for regulator "consumer"
2015 * @id: Supply name or regulator ID.
2017 * Returns a struct regulator corresponding to the regulator producer,
2018 * or IS_ERR() condition containing errno.
2020 * This is intended for use by consumers for devices which can have
2021 * some supplies unconnected in normal use, such as some MMC devices.
2022 * It can allow the regulator core to provide stub supplies for other
2023 * supplies requested using normal regulator_get() calls without
2024 * disrupting the operation of drivers that can handle absent
2027 * Use of supply names configured via regulator_set_device_supply() is
2028 * strongly encouraged. It is recommended that the supply name used
2029 * should match the name used for the supply and/or the relevant
2030 * device pins in the datasheet.
2032 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2034 return _regulator_get(dev, id, OPTIONAL_GET);
2036 EXPORT_SYMBOL_GPL(regulator_get_optional);
2038 static void destroy_regulator(struct regulator *regulator)
2040 struct regulator_dev *rdev = regulator->rdev;
2042 debugfs_remove_recursive(regulator->debugfs);
2044 if (regulator->dev) {
2045 if (regulator->device_link)
2046 device_link_remove(regulator->dev, &rdev->dev);
2048 /* remove any sysfs entries */
2049 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2052 regulator_lock(rdev);
2053 list_del(®ulator->list);
2056 rdev->exclusive = 0;
2057 regulator_unlock(rdev);
2059 kfree_const(regulator->supply_name);
2063 /* regulator_list_mutex lock held by regulator_put() */
2064 static void _regulator_put(struct regulator *regulator)
2066 struct regulator_dev *rdev;
2068 if (IS_ERR_OR_NULL(regulator))
2071 lockdep_assert_held_once(®ulator_list_mutex);
2073 /* Docs say you must disable before calling regulator_put() */
2074 WARN_ON(regulator->enable_count);
2076 rdev = regulator->rdev;
2078 destroy_regulator(regulator);
2080 module_put(rdev->owner);
2081 put_device(&rdev->dev);
2085 * regulator_put - "free" the regulator source
2086 * @regulator: regulator source
2088 * Note: drivers must ensure that all regulator_enable calls made on this
2089 * regulator source are balanced by regulator_disable calls prior to calling
2092 void regulator_put(struct regulator *regulator)
2094 mutex_lock(®ulator_list_mutex);
2095 _regulator_put(regulator);
2096 mutex_unlock(®ulator_list_mutex);
2098 EXPORT_SYMBOL_GPL(regulator_put);
2101 * regulator_register_supply_alias - Provide device alias for supply lookup
2103 * @dev: device that will be given as the regulator "consumer"
2104 * @id: Supply name or regulator ID
2105 * @alias_dev: device that should be used to lookup the supply
2106 * @alias_id: Supply name or regulator ID that should be used to lookup the
2109 * All lookups for id on dev will instead be conducted for alias_id on
2112 int regulator_register_supply_alias(struct device *dev, const char *id,
2113 struct device *alias_dev,
2114 const char *alias_id)
2116 struct regulator_supply_alias *map;
2118 map = regulator_find_supply_alias(dev, id);
2122 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2127 map->src_supply = id;
2128 map->alias_dev = alias_dev;
2129 map->alias_supply = alias_id;
2131 list_add(&map->list, ®ulator_supply_alias_list);
2133 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2134 id, dev_name(dev), alias_id, dev_name(alias_dev));
2138 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2141 * regulator_unregister_supply_alias - Remove device alias
2143 * @dev: device that will be given as the regulator "consumer"
2144 * @id: Supply name or regulator ID
2146 * Remove a lookup alias if one exists for id on dev.
2148 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2150 struct regulator_supply_alias *map;
2152 map = regulator_find_supply_alias(dev, id);
2154 list_del(&map->list);
2158 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2161 * regulator_bulk_register_supply_alias - register multiple aliases
2163 * @dev: device that will be given as the regulator "consumer"
2164 * @id: List of supply names or regulator IDs
2165 * @alias_dev: device that should be used to lookup the supply
2166 * @alias_id: List of supply names or regulator IDs that should be used to
2168 * @num_id: Number of aliases to register
2170 * @return 0 on success, an errno on failure.
2172 * This helper function allows drivers to register several supply
2173 * aliases in one operation. If any of the aliases cannot be
2174 * registered any aliases that were registered will be removed
2175 * before returning to the caller.
2177 int regulator_bulk_register_supply_alias(struct device *dev,
2178 const char *const *id,
2179 struct device *alias_dev,
2180 const char *const *alias_id,
2186 for (i = 0; i < num_id; ++i) {
2187 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2197 "Failed to create supply alias %s,%s -> %s,%s\n",
2198 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2201 regulator_unregister_supply_alias(dev, id[i]);
2205 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2208 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2210 * @dev: device that will be given as the regulator "consumer"
2211 * @id: List of supply names or regulator IDs
2212 * @num_id: Number of aliases to unregister
2214 * This helper function allows drivers to unregister several supply
2215 * aliases in one operation.
2217 void regulator_bulk_unregister_supply_alias(struct device *dev,
2218 const char *const *id,
2223 for (i = 0; i < num_id; ++i)
2224 regulator_unregister_supply_alias(dev, id[i]);
2226 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2229 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2230 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2231 const struct regulator_config *config)
2233 struct regulator_enable_gpio *pin;
2234 struct gpio_desc *gpiod;
2236 gpiod = config->ena_gpiod;
2238 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2239 if (pin->gpiod == gpiod) {
2240 rdev_dbg(rdev, "GPIO is already used\n");
2241 goto update_ena_gpio_to_rdev;
2245 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2250 list_add(&pin->list, ®ulator_ena_gpio_list);
2252 update_ena_gpio_to_rdev:
2253 pin->request_count++;
2254 rdev->ena_pin = pin;
2258 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2260 struct regulator_enable_gpio *pin, *n;
2265 /* Free the GPIO only in case of no use */
2266 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2267 if (pin->gpiod == rdev->ena_pin->gpiod) {
2268 if (pin->request_count <= 1) {
2269 pin->request_count = 0;
2270 gpiod_put(pin->gpiod);
2271 list_del(&pin->list);
2273 rdev->ena_pin = NULL;
2276 pin->request_count--;
2283 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2284 * @rdev: regulator_dev structure
2285 * @enable: enable GPIO at initial use?
2287 * GPIO is enabled in case of initial use. (enable_count is 0)
2288 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2290 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2292 struct regulator_enable_gpio *pin = rdev->ena_pin;
2298 /* Enable GPIO at initial use */
2299 if (pin->enable_count == 0)
2300 gpiod_set_value_cansleep(pin->gpiod, 1);
2302 pin->enable_count++;
2304 if (pin->enable_count > 1) {
2305 pin->enable_count--;
2309 /* Disable GPIO if not used */
2310 if (pin->enable_count <= 1) {
2311 gpiod_set_value_cansleep(pin->gpiod, 0);
2312 pin->enable_count = 0;
2320 * _regulator_enable_delay - a delay helper function
2321 * @delay: time to delay in microseconds
2323 * Delay for the requested amount of time as per the guidelines in:
2325 * Documentation/timers/timers-howto.rst
2327 * The assumption here is that regulators will never be enabled in
2328 * atomic context and therefore sleeping functions can be used.
2330 static void _regulator_enable_delay(unsigned int delay)
2332 unsigned int ms = delay / 1000;
2333 unsigned int us = delay % 1000;
2337 * For small enough values, handle super-millisecond
2338 * delays in the usleep_range() call below.
2347 * Give the scheduler some room to coalesce with any other
2348 * wakeup sources. For delays shorter than 10 us, don't even
2349 * bother setting up high-resolution timers and just busy-
2353 usleep_range(us, us + 100);
2359 * _regulator_check_status_enabled
2361 * A helper function to check if the regulator status can be interpreted
2362 * as 'regulator is enabled'.
2363 * @rdev: the regulator device to check
2366 * * 1 - if status shows regulator is in enabled state
2367 * * 0 - if not enabled state
2368 * * Error Value - as received from ops->get_status()
2370 static inline int _regulator_check_status_enabled(struct regulator_dev *rdev)
2372 int ret = rdev->desc->ops->get_status(rdev);
2375 rdev_info(rdev, "get_status returned error: %d\n", ret);
2380 case REGULATOR_STATUS_OFF:
2381 case REGULATOR_STATUS_ERROR:
2382 case REGULATOR_STATUS_UNDEFINED:
2389 static int _regulator_do_enable(struct regulator_dev *rdev)
2393 /* Query before enabling in case configuration dependent. */
2394 ret = _regulator_get_enable_time(rdev);
2398 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2402 trace_regulator_enable(rdev_get_name(rdev));
2404 if (rdev->desc->off_on_delay) {
2405 /* if needed, keep a distance of off_on_delay from last time
2406 * this regulator was disabled.
2408 unsigned long start_jiffy = jiffies;
2409 unsigned long intended, max_delay, remaining;
2411 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2412 intended = rdev->last_off_jiffy + max_delay;
2414 if (time_before(start_jiffy, intended)) {
2415 /* calc remaining jiffies to deal with one-time
2417 * in case of multiple timer wrapping, either it can be
2418 * detected by out-of-range remaining, or it cannot be
2419 * detected and we get a penalty of
2420 * _regulator_enable_delay().
2422 remaining = intended - start_jiffy;
2423 if (remaining <= max_delay)
2424 _regulator_enable_delay(
2425 jiffies_to_usecs(remaining));
2429 if (rdev->ena_pin) {
2430 if (!rdev->ena_gpio_state) {
2431 ret = regulator_ena_gpio_ctrl(rdev, true);
2434 rdev->ena_gpio_state = 1;
2436 } else if (rdev->desc->ops->enable) {
2437 ret = rdev->desc->ops->enable(rdev);
2444 /* Allow the regulator to ramp; it would be useful to extend
2445 * this for bulk operations so that the regulators can ramp
2447 trace_regulator_enable_delay(rdev_get_name(rdev));
2449 /* If poll_enabled_time is set, poll upto the delay calculated
2450 * above, delaying poll_enabled_time uS to check if the regulator
2451 * actually got enabled.
2452 * If the regulator isn't enabled after enable_delay has
2453 * expired, return -ETIMEDOUT.
2455 if (rdev->desc->poll_enabled_time) {
2456 unsigned int time_remaining = delay;
2458 while (time_remaining > 0) {
2459 _regulator_enable_delay(rdev->desc->poll_enabled_time);
2461 if (rdev->desc->ops->get_status) {
2462 ret = _regulator_check_status_enabled(rdev);
2467 } else if (rdev->desc->ops->is_enabled(rdev))
2470 time_remaining -= rdev->desc->poll_enabled_time;
2473 if (time_remaining <= 0) {
2474 rdev_err(rdev, "Enabled check timed out\n");
2478 _regulator_enable_delay(delay);
2481 trace_regulator_enable_complete(rdev_get_name(rdev));
2487 * _regulator_handle_consumer_enable - handle that a consumer enabled
2488 * @regulator: regulator source
2490 * Some things on a regulator consumer (like the contribution towards total
2491 * load on the regulator) only have an effect when the consumer wants the
2492 * regulator enabled. Explained in example with two consumers of the same
2494 * consumer A: set_load(100); => total load = 0
2495 * consumer A: regulator_enable(); => total load = 100
2496 * consumer B: set_load(1000); => total load = 100
2497 * consumer B: regulator_enable(); => total load = 1100
2498 * consumer A: regulator_disable(); => total_load = 1000
2500 * This function (together with _regulator_handle_consumer_disable) is
2501 * responsible for keeping track of the refcount for a given regulator consumer
2502 * and applying / unapplying these things.
2504 * Returns 0 upon no error; -error upon error.
2506 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2508 struct regulator_dev *rdev = regulator->rdev;
2510 lockdep_assert_held_once(&rdev->mutex.base);
2512 regulator->enable_count++;
2513 if (regulator->uA_load && regulator->enable_count == 1)
2514 return drms_uA_update(rdev);
2520 * _regulator_handle_consumer_disable - handle that a consumer disabled
2521 * @regulator: regulator source
2523 * The opposite of _regulator_handle_consumer_enable().
2525 * Returns 0 upon no error; -error upon error.
2527 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2529 struct regulator_dev *rdev = regulator->rdev;
2531 lockdep_assert_held_once(&rdev->mutex.base);
2533 if (!regulator->enable_count) {
2534 rdev_err(rdev, "Underflow of regulator enable count\n");
2538 regulator->enable_count--;
2539 if (regulator->uA_load && regulator->enable_count == 0)
2540 return drms_uA_update(rdev);
2545 /* locks held by regulator_enable() */
2546 static int _regulator_enable(struct regulator *regulator)
2548 struct regulator_dev *rdev = regulator->rdev;
2551 lockdep_assert_held_once(&rdev->mutex.base);
2553 if (rdev->use_count == 0 && rdev->supply) {
2554 ret = _regulator_enable(rdev->supply);
2559 /* balance only if there are regulators coupled */
2560 if (rdev->coupling_desc.n_coupled > 1) {
2561 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2563 goto err_disable_supply;
2566 ret = _regulator_handle_consumer_enable(regulator);
2568 goto err_disable_supply;
2570 if (rdev->use_count == 0) {
2571 /* The regulator may on if it's not switchable or left on */
2572 ret = _regulator_is_enabled(rdev);
2573 if (ret == -EINVAL || ret == 0) {
2574 if (!regulator_ops_is_valid(rdev,
2575 REGULATOR_CHANGE_STATUS)) {
2577 goto err_consumer_disable;
2580 ret = _regulator_do_enable(rdev);
2582 goto err_consumer_disable;
2584 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2586 } else if (ret < 0) {
2587 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2588 goto err_consumer_disable;
2590 /* Fallthrough on positive return values - already enabled */
2597 err_consumer_disable:
2598 _regulator_handle_consumer_disable(regulator);
2601 if (rdev->use_count == 0 && rdev->supply)
2602 _regulator_disable(rdev->supply);
2608 * regulator_enable - enable regulator output
2609 * @regulator: regulator source
2611 * Request that the regulator be enabled with the regulator output at
2612 * the predefined voltage or current value. Calls to regulator_enable()
2613 * must be balanced with calls to regulator_disable().
2615 * NOTE: the output value can be set by other drivers, boot loader or may be
2616 * hardwired in the regulator.
2618 int regulator_enable(struct regulator *regulator)
2620 struct regulator_dev *rdev = regulator->rdev;
2621 struct ww_acquire_ctx ww_ctx;
2624 regulator_lock_dependent(rdev, &ww_ctx);
2625 ret = _regulator_enable(regulator);
2626 regulator_unlock_dependent(rdev, &ww_ctx);
2630 EXPORT_SYMBOL_GPL(regulator_enable);
2632 static int _regulator_do_disable(struct regulator_dev *rdev)
2636 trace_regulator_disable(rdev_get_name(rdev));
2638 if (rdev->ena_pin) {
2639 if (rdev->ena_gpio_state) {
2640 ret = regulator_ena_gpio_ctrl(rdev, false);
2643 rdev->ena_gpio_state = 0;
2646 } else if (rdev->desc->ops->disable) {
2647 ret = rdev->desc->ops->disable(rdev);
2652 /* cares about last_off_jiffy only if off_on_delay is required by
2655 if (rdev->desc->off_on_delay)
2656 rdev->last_off_jiffy = jiffies;
2658 trace_regulator_disable_complete(rdev_get_name(rdev));
2663 /* locks held by regulator_disable() */
2664 static int _regulator_disable(struct regulator *regulator)
2666 struct regulator_dev *rdev = regulator->rdev;
2669 lockdep_assert_held_once(&rdev->mutex.base);
2671 if (WARN(rdev->use_count <= 0,
2672 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2675 /* are we the last user and permitted to disable ? */
2676 if (rdev->use_count == 1 &&
2677 (rdev->constraints && !rdev->constraints->always_on)) {
2679 /* we are last user */
2680 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2681 ret = _notifier_call_chain(rdev,
2682 REGULATOR_EVENT_PRE_DISABLE,
2684 if (ret & NOTIFY_STOP_MASK)
2687 ret = _regulator_do_disable(rdev);
2689 rdev_err(rdev, "failed to disable\n");
2690 _notifier_call_chain(rdev,
2691 REGULATOR_EVENT_ABORT_DISABLE,
2695 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2699 rdev->use_count = 0;
2700 } else if (rdev->use_count > 1) {
2705 ret = _regulator_handle_consumer_disable(regulator);
2707 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2708 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2710 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2711 ret = _regulator_disable(rdev->supply);
2717 * regulator_disable - disable regulator output
2718 * @regulator: regulator source
2720 * Disable the regulator output voltage or current. Calls to
2721 * regulator_enable() must be balanced with calls to
2722 * regulator_disable().
2724 * NOTE: this will only disable the regulator output if no other consumer
2725 * devices have it enabled, the regulator device supports disabling and
2726 * machine constraints permit this operation.
2728 int regulator_disable(struct regulator *regulator)
2730 struct regulator_dev *rdev = regulator->rdev;
2731 struct ww_acquire_ctx ww_ctx;
2734 regulator_lock_dependent(rdev, &ww_ctx);
2735 ret = _regulator_disable(regulator);
2736 regulator_unlock_dependent(rdev, &ww_ctx);
2740 EXPORT_SYMBOL_GPL(regulator_disable);
2742 /* locks held by regulator_force_disable() */
2743 static int _regulator_force_disable(struct regulator_dev *rdev)
2747 lockdep_assert_held_once(&rdev->mutex.base);
2749 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2750 REGULATOR_EVENT_PRE_DISABLE, NULL);
2751 if (ret & NOTIFY_STOP_MASK)
2754 ret = _regulator_do_disable(rdev);
2756 rdev_err(rdev, "failed to force disable\n");
2757 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2758 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2762 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2763 REGULATOR_EVENT_DISABLE, NULL);
2769 * regulator_force_disable - force disable regulator output
2770 * @regulator: regulator source
2772 * Forcibly disable the regulator output voltage or current.
2773 * NOTE: this *will* disable the regulator output even if other consumer
2774 * devices have it enabled. This should be used for situations when device
2775 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2777 int regulator_force_disable(struct regulator *regulator)
2779 struct regulator_dev *rdev = regulator->rdev;
2780 struct ww_acquire_ctx ww_ctx;
2783 regulator_lock_dependent(rdev, &ww_ctx);
2785 ret = _regulator_force_disable(regulator->rdev);
2787 if (rdev->coupling_desc.n_coupled > 1)
2788 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2790 if (regulator->uA_load) {
2791 regulator->uA_load = 0;
2792 ret = drms_uA_update(rdev);
2795 if (rdev->use_count != 0 && rdev->supply)
2796 _regulator_disable(rdev->supply);
2798 regulator_unlock_dependent(rdev, &ww_ctx);
2802 EXPORT_SYMBOL_GPL(regulator_force_disable);
2804 static void regulator_disable_work(struct work_struct *work)
2806 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2808 struct ww_acquire_ctx ww_ctx;
2810 struct regulator *regulator;
2811 int total_count = 0;
2813 regulator_lock_dependent(rdev, &ww_ctx);
2816 * Workqueue functions queue the new work instance while the previous
2817 * work instance is being processed. Cancel the queued work instance
2818 * as the work instance under processing does the job of the queued
2821 cancel_delayed_work(&rdev->disable_work);
2823 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2824 count = regulator->deferred_disables;
2829 total_count += count;
2830 regulator->deferred_disables = 0;
2832 for (i = 0; i < count; i++) {
2833 ret = _regulator_disable(regulator);
2835 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2838 WARN_ON(!total_count);
2840 if (rdev->coupling_desc.n_coupled > 1)
2841 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2843 regulator_unlock_dependent(rdev, &ww_ctx);
2847 * regulator_disable_deferred - disable regulator output with delay
2848 * @regulator: regulator source
2849 * @ms: milliseconds until the regulator is disabled
2851 * Execute regulator_disable() on the regulator after a delay. This
2852 * is intended for use with devices that require some time to quiesce.
2854 * NOTE: this will only disable the regulator output if no other consumer
2855 * devices have it enabled, the regulator device supports disabling and
2856 * machine constraints permit this operation.
2858 int regulator_disable_deferred(struct regulator *regulator, int ms)
2860 struct regulator_dev *rdev = regulator->rdev;
2863 return regulator_disable(regulator);
2865 regulator_lock(rdev);
2866 regulator->deferred_disables++;
2867 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2868 msecs_to_jiffies(ms));
2869 regulator_unlock(rdev);
2873 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2875 static int _regulator_is_enabled(struct regulator_dev *rdev)
2877 /* A GPIO control always takes precedence */
2879 return rdev->ena_gpio_state;
2881 /* If we don't know then assume that the regulator is always on */
2882 if (!rdev->desc->ops->is_enabled)
2885 return rdev->desc->ops->is_enabled(rdev);
2888 static int _regulator_list_voltage(struct regulator_dev *rdev,
2889 unsigned selector, int lock)
2891 const struct regulator_ops *ops = rdev->desc->ops;
2894 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2895 return rdev->desc->fixed_uV;
2897 if (ops->list_voltage) {
2898 if (selector >= rdev->desc->n_voltages)
2901 regulator_lock(rdev);
2902 ret = ops->list_voltage(rdev, selector);
2904 regulator_unlock(rdev);
2905 } else if (rdev->is_switch && rdev->supply) {
2906 ret = _regulator_list_voltage(rdev->supply->rdev,
2913 if (ret < rdev->constraints->min_uV)
2915 else if (ret > rdev->constraints->max_uV)
2923 * regulator_is_enabled - is the regulator output enabled
2924 * @regulator: regulator source
2926 * Returns positive if the regulator driver backing the source/client
2927 * has requested that the device be enabled, zero if it hasn't, else a
2928 * negative errno code.
2930 * Note that the device backing this regulator handle can have multiple
2931 * users, so it might be enabled even if regulator_enable() was never
2932 * called for this particular source.
2934 int regulator_is_enabled(struct regulator *regulator)
2938 if (regulator->always_on)
2941 regulator_lock(regulator->rdev);
2942 ret = _regulator_is_enabled(regulator->rdev);
2943 regulator_unlock(regulator->rdev);
2947 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2950 * regulator_count_voltages - count regulator_list_voltage() selectors
2951 * @regulator: regulator source
2953 * Returns number of selectors, or negative errno. Selectors are
2954 * numbered starting at zero, and typically correspond to bitfields
2955 * in hardware registers.
2957 int regulator_count_voltages(struct regulator *regulator)
2959 struct regulator_dev *rdev = regulator->rdev;
2961 if (rdev->desc->n_voltages)
2962 return rdev->desc->n_voltages;
2964 if (!rdev->is_switch || !rdev->supply)
2967 return regulator_count_voltages(rdev->supply);
2969 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2972 * regulator_list_voltage - enumerate supported voltages
2973 * @regulator: regulator source
2974 * @selector: identify voltage to list
2975 * Context: can sleep
2977 * Returns a voltage that can be passed to @regulator_set_voltage(),
2978 * zero if this selector code can't be used on this system, or a
2981 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2983 return _regulator_list_voltage(regulator->rdev, selector, 1);
2985 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2988 * regulator_get_regmap - get the regulator's register map
2989 * @regulator: regulator source
2991 * Returns the register map for the given regulator, or an ERR_PTR value
2992 * if the regulator doesn't use regmap.
2994 struct regmap *regulator_get_regmap(struct regulator *regulator)
2996 struct regmap *map = regulator->rdev->regmap;
2998 return map ? map : ERR_PTR(-EOPNOTSUPP);
3002 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3003 * @regulator: regulator source
3004 * @vsel_reg: voltage selector register, output parameter
3005 * @vsel_mask: mask for voltage selector bitfield, output parameter
3007 * Returns the hardware register offset and bitmask used for setting the
3008 * regulator voltage. This might be useful when configuring voltage-scaling
3009 * hardware or firmware that can make I2C requests behind the kernel's back,
3012 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3013 * and 0 is returned, otherwise a negative errno is returned.
3015 int regulator_get_hardware_vsel_register(struct regulator *regulator,
3017 unsigned *vsel_mask)
3019 struct regulator_dev *rdev = regulator->rdev;
3020 const struct regulator_ops *ops = rdev->desc->ops;
3022 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3025 *vsel_reg = rdev->desc->vsel_reg;
3026 *vsel_mask = rdev->desc->vsel_mask;
3030 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
3033 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3034 * @regulator: regulator source
3035 * @selector: identify voltage to list
3037 * Converts the selector to a hardware-specific voltage selector that can be
3038 * directly written to the regulator registers. The address of the voltage
3039 * register can be determined by calling @regulator_get_hardware_vsel_register.
3041 * On error a negative errno is returned.
3043 int regulator_list_hardware_vsel(struct regulator *regulator,
3046 struct regulator_dev *rdev = regulator->rdev;
3047 const struct regulator_ops *ops = rdev->desc->ops;
3049 if (selector >= rdev->desc->n_voltages)
3051 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3056 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3059 * regulator_get_linear_step - return the voltage step size between VSEL values
3060 * @regulator: regulator source
3062 * Returns the voltage step size between VSEL values for linear
3063 * regulators, or return 0 if the regulator isn't a linear regulator.
3065 unsigned int regulator_get_linear_step(struct regulator *regulator)
3067 struct regulator_dev *rdev = regulator->rdev;
3069 return rdev->desc->uV_step;
3071 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3074 * regulator_is_supported_voltage - check if a voltage range can be supported
3076 * @regulator: Regulator to check.
3077 * @min_uV: Minimum required voltage in uV.
3078 * @max_uV: Maximum required voltage in uV.
3080 * Returns a boolean.
3082 int regulator_is_supported_voltage(struct regulator *regulator,
3083 int min_uV, int max_uV)
3085 struct regulator_dev *rdev = regulator->rdev;
3086 int i, voltages, ret;
3088 /* If we can't change voltage check the current voltage */
3089 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3090 ret = regulator_get_voltage(regulator);
3092 return min_uV <= ret && ret <= max_uV;
3097 /* Any voltage within constrains range is fine? */
3098 if (rdev->desc->continuous_voltage_range)
3099 return min_uV >= rdev->constraints->min_uV &&
3100 max_uV <= rdev->constraints->max_uV;
3102 ret = regulator_count_voltages(regulator);
3107 for (i = 0; i < voltages; i++) {
3108 ret = regulator_list_voltage(regulator, i);
3110 if (ret >= min_uV && ret <= max_uV)
3116 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3118 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3121 const struct regulator_desc *desc = rdev->desc;
3123 if (desc->ops->map_voltage)
3124 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3126 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3127 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3129 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3130 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3132 if (desc->ops->list_voltage ==
3133 regulator_list_voltage_pickable_linear_range)
3134 return regulator_map_voltage_pickable_linear_range(rdev,
3137 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3140 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3141 int min_uV, int max_uV,
3144 struct pre_voltage_change_data data;
3147 data.old_uV = regulator_get_voltage_rdev(rdev);
3148 data.min_uV = min_uV;
3149 data.max_uV = max_uV;
3150 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3152 if (ret & NOTIFY_STOP_MASK)
3155 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3159 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3160 (void *)data.old_uV);
3165 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3166 int uV, unsigned selector)
3168 struct pre_voltage_change_data data;
3171 data.old_uV = regulator_get_voltage_rdev(rdev);
3174 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3176 if (ret & NOTIFY_STOP_MASK)
3179 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3183 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3184 (void *)data.old_uV);
3189 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3190 int uV, int new_selector)
3192 const struct regulator_ops *ops = rdev->desc->ops;
3193 int diff, old_sel, curr_sel, ret;
3195 /* Stepping is only needed if the regulator is enabled. */
3196 if (!_regulator_is_enabled(rdev))
3199 if (!ops->get_voltage_sel)
3202 old_sel = ops->get_voltage_sel(rdev);
3206 diff = new_selector - old_sel;
3208 return 0; /* No change needed. */
3212 for (curr_sel = old_sel + rdev->desc->vsel_step;
3213 curr_sel < new_selector;
3214 curr_sel += rdev->desc->vsel_step) {
3216 * Call the callback directly instead of using
3217 * _regulator_call_set_voltage_sel() as we don't
3218 * want to notify anyone yet. Same in the branch
3221 ret = ops->set_voltage_sel(rdev, curr_sel);
3226 /* Stepping down. */
3227 for (curr_sel = old_sel - rdev->desc->vsel_step;
3228 curr_sel > new_selector;
3229 curr_sel -= rdev->desc->vsel_step) {
3230 ret = ops->set_voltage_sel(rdev, curr_sel);
3237 /* The final selector will trigger the notifiers. */
3238 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3242 * At least try to return to the previous voltage if setting a new
3245 (void)ops->set_voltage_sel(rdev, old_sel);
3249 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3250 int old_uV, int new_uV)
3252 unsigned int ramp_delay = 0;
3254 if (rdev->constraints->ramp_delay)
3255 ramp_delay = rdev->constraints->ramp_delay;
3256 else if (rdev->desc->ramp_delay)
3257 ramp_delay = rdev->desc->ramp_delay;
3258 else if (rdev->constraints->settling_time)
3259 return rdev->constraints->settling_time;
3260 else if (rdev->constraints->settling_time_up &&
3262 return rdev->constraints->settling_time_up;
3263 else if (rdev->constraints->settling_time_down &&
3265 return rdev->constraints->settling_time_down;
3267 if (ramp_delay == 0) {
3268 rdev_dbg(rdev, "ramp_delay not set\n");
3272 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3275 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3276 int min_uV, int max_uV)
3281 unsigned int selector;
3282 int old_selector = -1;
3283 const struct regulator_ops *ops = rdev->desc->ops;
3284 int old_uV = regulator_get_voltage_rdev(rdev);
3286 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3288 min_uV += rdev->constraints->uV_offset;
3289 max_uV += rdev->constraints->uV_offset;
3292 * If we can't obtain the old selector there is not enough
3293 * info to call set_voltage_time_sel().
3295 if (_regulator_is_enabled(rdev) &&
3296 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3297 old_selector = ops->get_voltage_sel(rdev);
3298 if (old_selector < 0)
3299 return old_selector;
3302 if (ops->set_voltage) {
3303 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3307 if (ops->list_voltage)
3308 best_val = ops->list_voltage(rdev,
3311 best_val = regulator_get_voltage_rdev(rdev);
3314 } else if (ops->set_voltage_sel) {
3315 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3317 best_val = ops->list_voltage(rdev, ret);
3318 if (min_uV <= best_val && max_uV >= best_val) {
3320 if (old_selector == selector)
3322 else if (rdev->desc->vsel_step)
3323 ret = _regulator_set_voltage_sel_step(
3324 rdev, best_val, selector);
3326 ret = _regulator_call_set_voltage_sel(
3327 rdev, best_val, selector);
3339 if (ops->set_voltage_time_sel) {
3341 * Call set_voltage_time_sel if successfully obtained
3344 if (old_selector >= 0 && old_selector != selector)
3345 delay = ops->set_voltage_time_sel(rdev, old_selector,
3348 if (old_uV != best_val) {
3349 if (ops->set_voltage_time)
3350 delay = ops->set_voltage_time(rdev, old_uV,
3353 delay = _regulator_set_voltage_time(rdev,
3360 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3364 /* Insert any necessary delays */
3365 if (delay >= 1000) {
3366 mdelay(delay / 1000);
3367 udelay(delay % 1000);
3372 if (best_val >= 0) {
3373 unsigned long data = best_val;
3375 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3380 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3385 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3386 int min_uV, int max_uV, suspend_state_t state)
3388 struct regulator_state *rstate;
3391 rstate = regulator_get_suspend_state(rdev, state);
3395 if (min_uV < rstate->min_uV)
3396 min_uV = rstate->min_uV;
3397 if (max_uV > rstate->max_uV)
3398 max_uV = rstate->max_uV;
3400 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3404 uV = rdev->desc->ops->list_voltage(rdev, sel);
3405 if (uV >= min_uV && uV <= max_uV)
3411 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3412 int min_uV, int max_uV,
3413 suspend_state_t state)
3415 struct regulator_dev *rdev = regulator->rdev;
3416 struct regulator_voltage *voltage = ®ulator->voltage[state];
3418 int old_min_uV, old_max_uV;
3421 /* If we're setting the same range as last time the change
3422 * should be a noop (some cpufreq implementations use the same
3423 * voltage for multiple frequencies, for example).
3425 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3428 /* If we're trying to set a range that overlaps the current voltage,
3429 * return successfully even though the regulator does not support
3430 * changing the voltage.
3432 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3433 current_uV = regulator_get_voltage_rdev(rdev);
3434 if (min_uV <= current_uV && current_uV <= max_uV) {
3435 voltage->min_uV = min_uV;
3436 voltage->max_uV = max_uV;
3442 if (!rdev->desc->ops->set_voltage &&
3443 !rdev->desc->ops->set_voltage_sel) {
3448 /* constraints check */
3449 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3453 /* restore original values in case of error */
3454 old_min_uV = voltage->min_uV;
3455 old_max_uV = voltage->max_uV;
3456 voltage->min_uV = min_uV;
3457 voltage->max_uV = max_uV;
3459 /* for not coupled regulators this will just set the voltage */
3460 ret = regulator_balance_voltage(rdev, state);
3462 voltage->min_uV = old_min_uV;
3463 voltage->max_uV = old_max_uV;
3470 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3471 int max_uV, suspend_state_t state)
3473 int best_supply_uV = 0;
3474 int supply_change_uV = 0;
3478 regulator_ops_is_valid(rdev->supply->rdev,
3479 REGULATOR_CHANGE_VOLTAGE) &&
3480 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3481 rdev->desc->ops->get_voltage_sel))) {
3482 int current_supply_uV;
3485 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3491 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3492 if (best_supply_uV < 0) {
3493 ret = best_supply_uV;
3497 best_supply_uV += rdev->desc->min_dropout_uV;
3499 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3500 if (current_supply_uV < 0) {
3501 ret = current_supply_uV;
3505 supply_change_uV = best_supply_uV - current_supply_uV;
3508 if (supply_change_uV > 0) {
3509 ret = regulator_set_voltage_unlocked(rdev->supply,
3510 best_supply_uV, INT_MAX, state);
3512 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3518 if (state == PM_SUSPEND_ON)
3519 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3521 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3526 if (supply_change_uV < 0) {
3527 ret = regulator_set_voltage_unlocked(rdev->supply,
3528 best_supply_uV, INT_MAX, state);
3530 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3532 /* No need to fail here */
3539 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev);
3541 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3542 int *current_uV, int *min_uV)
3544 struct regulation_constraints *constraints = rdev->constraints;
3546 /* Limit voltage change only if necessary */
3547 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3550 if (*current_uV < 0) {
3551 *current_uV = regulator_get_voltage_rdev(rdev);
3553 if (*current_uV < 0)
3557 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3560 /* Clamp target voltage within the given step */
3561 if (*current_uV < *min_uV)
3562 *min_uV = min(*current_uV + constraints->max_uV_step,
3565 *min_uV = max(*current_uV - constraints->max_uV_step,
3571 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3573 int *min_uV, int *max_uV,
3574 suspend_state_t state,
3577 struct coupling_desc *c_desc = &rdev->coupling_desc;
3578 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3579 struct regulation_constraints *constraints = rdev->constraints;
3580 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3581 int max_current_uV = 0, min_current_uV = INT_MAX;
3582 int highest_min_uV = 0, target_uV, possible_uV;
3583 int i, ret, max_spread;
3589 * If there are no coupled regulators, simply set the voltage
3590 * demanded by consumers.
3592 if (n_coupled == 1) {
3594 * If consumers don't provide any demands, set voltage
3597 desired_min_uV = constraints->min_uV;
3598 desired_max_uV = constraints->max_uV;
3600 ret = regulator_check_consumers(rdev,
3602 &desired_max_uV, state);
3606 possible_uV = desired_min_uV;
3612 /* Find highest min desired voltage */
3613 for (i = 0; i < n_coupled; i++) {
3615 int tmp_max = INT_MAX;
3617 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3619 ret = regulator_check_consumers(c_rdevs[i],
3625 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3629 highest_min_uV = max(highest_min_uV, tmp_min);
3632 desired_min_uV = tmp_min;
3633 desired_max_uV = tmp_max;
3637 max_spread = constraints->max_spread[0];
3640 * Let target_uV be equal to the desired one if possible.
3641 * If not, set it to minimum voltage, allowed by other coupled
3644 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3647 * Find min and max voltages, which currently aren't violating
3650 for (i = 1; i < n_coupled; i++) {
3653 if (!_regulator_is_enabled(c_rdevs[i]))
3656 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3660 min_current_uV = min(tmp_act, min_current_uV);
3661 max_current_uV = max(tmp_act, max_current_uV);
3664 /* There aren't any other regulators enabled */
3665 if (max_current_uV == 0) {
3666 possible_uV = target_uV;
3669 * Correct target voltage, so as it currently isn't
3670 * violating max_spread
3672 possible_uV = max(target_uV, max_current_uV - max_spread);
3673 possible_uV = min(possible_uV, min_current_uV + max_spread);
3676 if (possible_uV > desired_max_uV)
3679 done = (possible_uV == target_uV);
3680 desired_min_uV = possible_uV;
3683 /* Apply max_uV_step constraint if necessary */
3684 if (state == PM_SUSPEND_ON) {
3685 ret = regulator_limit_voltage_step(rdev, current_uV,
3694 /* Set current_uV if wasn't done earlier in the code and if necessary */
3695 if (n_coupled > 1 && *current_uV == -1) {
3697 if (_regulator_is_enabled(rdev)) {
3698 ret = regulator_get_voltage_rdev(rdev);
3704 *current_uV = desired_min_uV;
3708 *min_uV = desired_min_uV;
3709 *max_uV = desired_max_uV;
3714 int regulator_do_balance_voltage(struct regulator_dev *rdev,
3715 suspend_state_t state, bool skip_coupled)
3717 struct regulator_dev **c_rdevs;
3718 struct regulator_dev *best_rdev;
3719 struct coupling_desc *c_desc = &rdev->coupling_desc;
3720 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3721 unsigned int delta, best_delta;
3722 unsigned long c_rdev_done = 0;
3723 bool best_c_rdev_done;
3725 c_rdevs = c_desc->coupled_rdevs;
3726 n_coupled = skip_coupled ? 1 : c_desc->n_coupled;
3729 * Find the best possible voltage change on each loop. Leave the loop
3730 * if there isn't any possible change.
3733 best_c_rdev_done = false;
3741 * Find highest difference between optimal voltage
3742 * and current voltage.
3744 for (i = 0; i < n_coupled; i++) {
3746 * optimal_uV is the best voltage that can be set for
3747 * i-th regulator at the moment without violating
3748 * max_spread constraint in order to balance
3749 * the coupled voltages.
3751 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3753 if (test_bit(i, &c_rdev_done))
3756 ret = regulator_get_optimal_voltage(c_rdevs[i],
3764 delta = abs(optimal_uV - current_uV);
3766 if (delta && best_delta <= delta) {
3767 best_c_rdev_done = ret;
3769 best_rdev = c_rdevs[i];
3770 best_min_uV = optimal_uV;
3771 best_max_uV = optimal_max_uV;
3776 /* Nothing to change, return successfully */
3782 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3783 best_max_uV, state);
3788 if (best_c_rdev_done)
3789 set_bit(best_c_rdev, &c_rdev_done);
3791 } while (n_coupled > 1);
3797 static int regulator_balance_voltage(struct regulator_dev *rdev,
3798 suspend_state_t state)
3800 struct coupling_desc *c_desc = &rdev->coupling_desc;
3801 struct regulator_coupler *coupler = c_desc->coupler;
3802 bool skip_coupled = false;
3805 * If system is in a state other than PM_SUSPEND_ON, don't check
3806 * other coupled regulators.
3808 if (state != PM_SUSPEND_ON)
3809 skip_coupled = true;
3811 if (c_desc->n_resolved < c_desc->n_coupled) {
3812 rdev_err(rdev, "Not all coupled regulators registered\n");
3816 /* Invoke custom balancer for customized couplers */
3817 if (coupler && coupler->balance_voltage)
3818 return coupler->balance_voltage(coupler, rdev, state);
3820 return regulator_do_balance_voltage(rdev, state, skip_coupled);
3824 * regulator_set_voltage - set regulator output voltage
3825 * @regulator: regulator source
3826 * @min_uV: Minimum required voltage in uV
3827 * @max_uV: Maximum acceptable voltage in uV
3829 * Sets a voltage regulator to the desired output voltage. This can be set
3830 * during any regulator state. IOW, regulator can be disabled or enabled.
3832 * If the regulator is enabled then the voltage will change to the new value
3833 * immediately otherwise if the regulator is disabled the regulator will
3834 * output at the new voltage when enabled.
3836 * NOTE: If the regulator is shared between several devices then the lowest
3837 * request voltage that meets the system constraints will be used.
3838 * Regulator system constraints must be set for this regulator before
3839 * calling this function otherwise this call will fail.
3841 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3843 struct ww_acquire_ctx ww_ctx;
3846 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3848 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3851 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3855 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3857 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3858 suspend_state_t state, bool en)
3860 struct regulator_state *rstate;
3862 rstate = regulator_get_suspend_state(rdev, state);
3866 if (!rstate->changeable)
3869 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3874 int regulator_suspend_enable(struct regulator_dev *rdev,
3875 suspend_state_t state)
3877 return regulator_suspend_toggle(rdev, state, true);
3879 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3881 int regulator_suspend_disable(struct regulator_dev *rdev,
3882 suspend_state_t state)
3884 struct regulator *regulator;
3885 struct regulator_voltage *voltage;
3888 * if any consumer wants this regulator device keeping on in
3889 * suspend states, don't set it as disabled.
3891 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3892 voltage = ®ulator->voltage[state];
3893 if (voltage->min_uV || voltage->max_uV)
3897 return regulator_suspend_toggle(rdev, state, false);
3899 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3901 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3902 int min_uV, int max_uV,
3903 suspend_state_t state)
3905 struct regulator_dev *rdev = regulator->rdev;
3906 struct regulator_state *rstate;
3908 rstate = regulator_get_suspend_state(rdev, state);
3912 if (rstate->min_uV == rstate->max_uV) {
3913 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3917 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3920 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3921 int max_uV, suspend_state_t state)
3923 struct ww_acquire_ctx ww_ctx;
3926 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3927 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3930 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3932 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3935 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3939 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3942 * regulator_set_voltage_time - get raise/fall time
3943 * @regulator: regulator source
3944 * @old_uV: starting voltage in microvolts
3945 * @new_uV: target voltage in microvolts
3947 * Provided with the starting and ending voltage, this function attempts to
3948 * calculate the time in microseconds required to rise or fall to this new
3951 int regulator_set_voltage_time(struct regulator *regulator,
3952 int old_uV, int new_uV)
3954 struct regulator_dev *rdev = regulator->rdev;
3955 const struct regulator_ops *ops = rdev->desc->ops;
3961 if (ops->set_voltage_time)
3962 return ops->set_voltage_time(rdev, old_uV, new_uV);
3963 else if (!ops->set_voltage_time_sel)
3964 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3966 /* Currently requires operations to do this */
3967 if (!ops->list_voltage || !rdev->desc->n_voltages)
3970 for (i = 0; i < rdev->desc->n_voltages; i++) {
3971 /* We only look for exact voltage matches here */
3972 voltage = regulator_list_voltage(regulator, i);
3977 if (voltage == old_uV)
3979 if (voltage == new_uV)
3983 if (old_sel < 0 || new_sel < 0)
3986 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3988 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3991 * regulator_set_voltage_time_sel - get raise/fall time
3992 * @rdev: regulator source device
3993 * @old_selector: selector for starting voltage
3994 * @new_selector: selector for target voltage
3996 * Provided with the starting and target voltage selectors, this function
3997 * returns time in microseconds required to rise or fall to this new voltage
3999 * Drivers providing ramp_delay in regulation_constraints can use this as their
4000 * set_voltage_time_sel() operation.
4002 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
4003 unsigned int old_selector,
4004 unsigned int new_selector)
4006 int old_volt, new_volt;
4009 if (!rdev->desc->ops->list_voltage)
4012 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
4013 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
4015 if (rdev->desc->ops->set_voltage_time)
4016 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
4019 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
4021 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
4024 * regulator_sync_voltage - re-apply last regulator output voltage
4025 * @regulator: regulator source
4027 * Re-apply the last configured voltage. This is intended to be used
4028 * where some external control source the consumer is cooperating with
4029 * has caused the configured voltage to change.
4031 int regulator_sync_voltage(struct regulator *regulator)
4033 struct regulator_dev *rdev = regulator->rdev;
4034 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
4035 int ret, min_uV, max_uV;
4037 regulator_lock(rdev);
4039 if (!rdev->desc->ops->set_voltage &&
4040 !rdev->desc->ops->set_voltage_sel) {
4045 /* This is only going to work if we've had a voltage configured. */
4046 if (!voltage->min_uV && !voltage->max_uV) {
4051 min_uV = voltage->min_uV;
4052 max_uV = voltage->max_uV;
4054 /* This should be a paranoia check... */
4055 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
4059 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
4063 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
4066 regulator_unlock(rdev);
4069 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
4071 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
4076 if (rdev->desc->ops->get_bypass) {
4077 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
4081 /* if bypassed the regulator must have a supply */
4082 if (!rdev->supply) {
4084 "bypassed regulator has no supply!\n");
4085 return -EPROBE_DEFER;
4088 return regulator_get_voltage_rdev(rdev->supply->rdev);
4092 if (rdev->desc->ops->get_voltage_sel) {
4093 sel = rdev->desc->ops->get_voltage_sel(rdev);
4096 ret = rdev->desc->ops->list_voltage(rdev, sel);
4097 } else if (rdev->desc->ops->get_voltage) {
4098 ret = rdev->desc->ops->get_voltage(rdev);
4099 } else if (rdev->desc->ops->list_voltage) {
4100 ret = rdev->desc->ops->list_voltage(rdev, 0);
4101 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4102 ret = rdev->desc->fixed_uV;
4103 } else if (rdev->supply) {
4104 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4111 return ret - rdev->constraints->uV_offset;
4113 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev);
4116 * regulator_get_voltage - get regulator output voltage
4117 * @regulator: regulator source
4119 * This returns the current regulator voltage in uV.
4121 * NOTE: If the regulator is disabled it will return the voltage value. This
4122 * function should not be used to determine regulator state.
4124 int regulator_get_voltage(struct regulator *regulator)
4126 struct ww_acquire_ctx ww_ctx;
4129 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4130 ret = regulator_get_voltage_rdev(regulator->rdev);
4131 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4135 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4138 * regulator_set_current_limit - set regulator output current limit
4139 * @regulator: regulator source
4140 * @min_uA: Minimum supported current in uA
4141 * @max_uA: Maximum supported current in uA
4143 * Sets current sink to the desired output current. This can be set during
4144 * any regulator state. IOW, regulator can be disabled or enabled.
4146 * If the regulator is enabled then the current will change to the new value
4147 * immediately otherwise if the regulator is disabled the regulator will
4148 * output at the new current when enabled.
4150 * NOTE: Regulator system constraints must be set for this regulator before
4151 * calling this function otherwise this call will fail.
4153 int regulator_set_current_limit(struct regulator *regulator,
4154 int min_uA, int max_uA)
4156 struct regulator_dev *rdev = regulator->rdev;
4159 regulator_lock(rdev);
4162 if (!rdev->desc->ops->set_current_limit) {
4167 /* constraints check */
4168 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4172 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4174 regulator_unlock(rdev);
4177 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4179 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4182 if (!rdev->desc->ops->get_current_limit)
4185 return rdev->desc->ops->get_current_limit(rdev);
4188 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4192 regulator_lock(rdev);
4193 ret = _regulator_get_current_limit_unlocked(rdev);
4194 regulator_unlock(rdev);
4200 * regulator_get_current_limit - get regulator output current
4201 * @regulator: regulator source
4203 * This returns the current supplied by the specified current sink in uA.
4205 * NOTE: If the regulator is disabled it will return the current value. This
4206 * function should not be used to determine regulator state.
4208 int regulator_get_current_limit(struct regulator *regulator)
4210 return _regulator_get_current_limit(regulator->rdev);
4212 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4215 * regulator_set_mode - set regulator operating mode
4216 * @regulator: regulator source
4217 * @mode: operating mode - one of the REGULATOR_MODE constants
4219 * Set regulator operating mode to increase regulator efficiency or improve
4220 * regulation performance.
4222 * NOTE: Regulator system constraints must be set for this regulator before
4223 * calling this function otherwise this call will fail.
4225 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4227 struct regulator_dev *rdev = regulator->rdev;
4229 int regulator_curr_mode;
4231 regulator_lock(rdev);
4234 if (!rdev->desc->ops->set_mode) {
4239 /* return if the same mode is requested */
4240 if (rdev->desc->ops->get_mode) {
4241 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4242 if (regulator_curr_mode == mode) {
4248 /* constraints check */
4249 ret = regulator_mode_constrain(rdev, &mode);
4253 ret = rdev->desc->ops->set_mode(rdev, mode);
4255 regulator_unlock(rdev);
4258 EXPORT_SYMBOL_GPL(regulator_set_mode);
4260 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4263 if (!rdev->desc->ops->get_mode)
4266 return rdev->desc->ops->get_mode(rdev);
4269 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4273 regulator_lock(rdev);
4274 ret = _regulator_get_mode_unlocked(rdev);
4275 regulator_unlock(rdev);
4281 * regulator_get_mode - get regulator operating mode
4282 * @regulator: regulator source
4284 * Get the current regulator operating mode.
4286 unsigned int regulator_get_mode(struct regulator *regulator)
4288 return _regulator_get_mode(regulator->rdev);
4290 EXPORT_SYMBOL_GPL(regulator_get_mode);
4292 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4293 unsigned int *flags)
4297 regulator_lock(rdev);
4300 if (!rdev->desc->ops->get_error_flags) {
4305 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4307 regulator_unlock(rdev);
4312 * regulator_get_error_flags - get regulator error information
4313 * @regulator: regulator source
4314 * @flags: pointer to store error flags
4316 * Get the current regulator error information.
4318 int regulator_get_error_flags(struct regulator *regulator,
4319 unsigned int *flags)
4321 return _regulator_get_error_flags(regulator->rdev, flags);
4323 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4326 * regulator_set_load - set regulator load
4327 * @regulator: regulator source
4328 * @uA_load: load current
4330 * Notifies the regulator core of a new device load. This is then used by
4331 * DRMS (if enabled by constraints) to set the most efficient regulator
4332 * operating mode for the new regulator loading.
4334 * Consumer devices notify their supply regulator of the maximum power
4335 * they will require (can be taken from device datasheet in the power
4336 * consumption tables) when they change operational status and hence power
4337 * state. Examples of operational state changes that can affect power
4338 * consumption are :-
4340 * o Device is opened / closed.
4341 * o Device I/O is about to begin or has just finished.
4342 * o Device is idling in between work.
4344 * This information is also exported via sysfs to userspace.
4346 * DRMS will sum the total requested load on the regulator and change
4347 * to the most efficient operating mode if platform constraints allow.
4349 * NOTE: when a regulator consumer requests to have a regulator
4350 * disabled then any load that consumer requested no longer counts
4351 * toward the total requested load. If the regulator is re-enabled
4352 * then the previously requested load will start counting again.
4354 * If a regulator is an always-on regulator then an individual consumer's
4355 * load will still be removed if that consumer is fully disabled.
4357 * On error a negative errno is returned.
4359 int regulator_set_load(struct regulator *regulator, int uA_load)
4361 struct regulator_dev *rdev = regulator->rdev;
4365 regulator_lock(rdev);
4366 old_uA_load = regulator->uA_load;
4367 regulator->uA_load = uA_load;
4368 if (regulator->enable_count && old_uA_load != uA_load) {
4369 ret = drms_uA_update(rdev);
4371 regulator->uA_load = old_uA_load;
4373 regulator_unlock(rdev);
4377 EXPORT_SYMBOL_GPL(regulator_set_load);
4380 * regulator_allow_bypass - allow the regulator to go into bypass mode
4382 * @regulator: Regulator to configure
4383 * @enable: enable or disable bypass mode
4385 * Allow the regulator to go into bypass mode if all other consumers
4386 * for the regulator also enable bypass mode and the machine
4387 * constraints allow this. Bypass mode means that the regulator is
4388 * simply passing the input directly to the output with no regulation.
4390 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4392 struct regulator_dev *rdev = regulator->rdev;
4393 const char *name = rdev_get_name(rdev);
4396 if (!rdev->desc->ops->set_bypass)
4399 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4402 regulator_lock(rdev);
4404 if (enable && !regulator->bypass) {
4405 rdev->bypass_count++;
4407 if (rdev->bypass_count == rdev->open_count) {
4408 trace_regulator_bypass_enable(name);
4410 ret = rdev->desc->ops->set_bypass(rdev, enable);
4412 rdev->bypass_count--;
4414 trace_regulator_bypass_enable_complete(name);
4417 } else if (!enable && regulator->bypass) {
4418 rdev->bypass_count--;
4420 if (rdev->bypass_count != rdev->open_count) {
4421 trace_regulator_bypass_disable(name);
4423 ret = rdev->desc->ops->set_bypass(rdev, enable);
4425 rdev->bypass_count++;
4427 trace_regulator_bypass_disable_complete(name);
4432 regulator->bypass = enable;
4434 regulator_unlock(rdev);
4438 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4441 * regulator_register_notifier - register regulator event notifier
4442 * @regulator: regulator source
4443 * @nb: notifier block
4445 * Register notifier block to receive regulator events.
4447 int regulator_register_notifier(struct regulator *regulator,
4448 struct notifier_block *nb)
4450 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4453 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4456 * regulator_unregister_notifier - unregister regulator event notifier
4457 * @regulator: regulator source
4458 * @nb: notifier block
4460 * Unregister regulator event notifier block.
4462 int regulator_unregister_notifier(struct regulator *regulator,
4463 struct notifier_block *nb)
4465 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4468 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4470 /* notify regulator consumers and downstream regulator consumers.
4471 * Note mutex must be held by caller.
4473 static int _notifier_call_chain(struct regulator_dev *rdev,
4474 unsigned long event, void *data)
4476 /* call rdev chain first */
4477 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4481 * regulator_bulk_get - get multiple regulator consumers
4483 * @dev: Device to supply
4484 * @num_consumers: Number of consumers to register
4485 * @consumers: Configuration of consumers; clients are stored here.
4487 * @return 0 on success, an errno on failure.
4489 * This helper function allows drivers to get several regulator
4490 * consumers in one operation. If any of the regulators cannot be
4491 * acquired then any regulators that were allocated will be freed
4492 * before returning to the caller.
4494 int regulator_bulk_get(struct device *dev, int num_consumers,
4495 struct regulator_bulk_data *consumers)
4500 for (i = 0; i < num_consumers; i++)
4501 consumers[i].consumer = NULL;
4503 for (i = 0; i < num_consumers; i++) {
4504 consumers[i].consumer = regulator_get(dev,
4505 consumers[i].supply);
4506 if (IS_ERR(consumers[i].consumer)) {
4507 ret = PTR_ERR(consumers[i].consumer);
4508 consumers[i].consumer = NULL;
4516 if (ret != -EPROBE_DEFER)
4517 dev_err(dev, "Failed to get supply '%s': %d\n",
4518 consumers[i].supply, ret);
4520 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4521 consumers[i].supply);
4524 regulator_put(consumers[i].consumer);
4528 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4530 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4532 struct regulator_bulk_data *bulk = data;
4534 bulk->ret = regulator_enable(bulk->consumer);
4538 * regulator_bulk_enable - enable multiple regulator consumers
4540 * @num_consumers: Number of consumers
4541 * @consumers: Consumer data; clients are stored here.
4542 * @return 0 on success, an errno on failure
4544 * This convenience API allows consumers to enable multiple regulator
4545 * clients in a single API call. If any consumers cannot be enabled
4546 * then any others that were enabled will be disabled again prior to
4549 int regulator_bulk_enable(int num_consumers,
4550 struct regulator_bulk_data *consumers)
4552 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4556 for (i = 0; i < num_consumers; i++) {
4557 async_schedule_domain(regulator_bulk_enable_async,
4558 &consumers[i], &async_domain);
4561 async_synchronize_full_domain(&async_domain);
4563 /* If any consumer failed we need to unwind any that succeeded */
4564 for (i = 0; i < num_consumers; i++) {
4565 if (consumers[i].ret != 0) {
4566 ret = consumers[i].ret;
4574 for (i = 0; i < num_consumers; i++) {
4575 if (consumers[i].ret < 0)
4576 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4579 regulator_disable(consumers[i].consumer);
4584 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4587 * regulator_bulk_disable - disable multiple regulator consumers
4589 * @num_consumers: Number of consumers
4590 * @consumers: Consumer data; clients are stored here.
4591 * @return 0 on success, an errno on failure
4593 * This convenience API allows consumers to disable multiple regulator
4594 * clients in a single API call. If any consumers cannot be disabled
4595 * then any others that were disabled will be enabled again prior to
4598 int regulator_bulk_disable(int num_consumers,
4599 struct regulator_bulk_data *consumers)
4604 for (i = num_consumers - 1; i >= 0; --i) {
4605 ret = regulator_disable(consumers[i].consumer);
4613 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4614 for (++i; i < num_consumers; ++i) {
4615 r = regulator_enable(consumers[i].consumer);
4617 pr_err("Failed to re-enable %s: %d\n",
4618 consumers[i].supply, r);
4623 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4626 * regulator_bulk_force_disable - force disable multiple regulator consumers
4628 * @num_consumers: Number of consumers
4629 * @consumers: Consumer data; clients are stored here.
4630 * @return 0 on success, an errno on failure
4632 * This convenience API allows consumers to forcibly disable multiple regulator
4633 * clients in a single API call.
4634 * NOTE: This should be used for situations when device damage will
4635 * likely occur if the regulators are not disabled (e.g. over temp).
4636 * Although regulator_force_disable function call for some consumers can
4637 * return error numbers, the function is called for all consumers.
4639 int regulator_bulk_force_disable(int num_consumers,
4640 struct regulator_bulk_data *consumers)
4645 for (i = 0; i < num_consumers; i++) {
4647 regulator_force_disable(consumers[i].consumer);
4649 /* Store first error for reporting */
4650 if (consumers[i].ret && !ret)
4651 ret = consumers[i].ret;
4656 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4659 * regulator_bulk_free - free multiple regulator consumers
4661 * @num_consumers: Number of consumers
4662 * @consumers: Consumer data; clients are stored here.
4664 * This convenience API allows consumers to free multiple regulator
4665 * clients in a single API call.
4667 void regulator_bulk_free(int num_consumers,
4668 struct regulator_bulk_data *consumers)
4672 for (i = 0; i < num_consumers; i++) {
4673 regulator_put(consumers[i].consumer);
4674 consumers[i].consumer = NULL;
4677 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4680 * regulator_notifier_call_chain - call regulator event notifier
4681 * @rdev: regulator source
4682 * @event: notifier block
4683 * @data: callback-specific data.
4685 * Called by regulator drivers to notify clients a regulator event has
4686 * occurred. We also notify regulator clients downstream.
4687 * Note lock must be held by caller.
4689 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4690 unsigned long event, void *data)
4692 lockdep_assert_held_once(&rdev->mutex.base);
4694 _notifier_call_chain(rdev, event, data);
4698 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4701 * regulator_mode_to_status - convert a regulator mode into a status
4703 * @mode: Mode to convert
4705 * Convert a regulator mode into a status.
4707 int regulator_mode_to_status(unsigned int mode)
4710 case REGULATOR_MODE_FAST:
4711 return REGULATOR_STATUS_FAST;
4712 case REGULATOR_MODE_NORMAL:
4713 return REGULATOR_STATUS_NORMAL;
4714 case REGULATOR_MODE_IDLE:
4715 return REGULATOR_STATUS_IDLE;
4716 case REGULATOR_MODE_STANDBY:
4717 return REGULATOR_STATUS_STANDBY;
4719 return REGULATOR_STATUS_UNDEFINED;
4722 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4724 static struct attribute *regulator_dev_attrs[] = {
4725 &dev_attr_name.attr,
4726 &dev_attr_num_users.attr,
4727 &dev_attr_type.attr,
4728 &dev_attr_microvolts.attr,
4729 &dev_attr_microamps.attr,
4730 &dev_attr_opmode.attr,
4731 &dev_attr_state.attr,
4732 &dev_attr_status.attr,
4733 &dev_attr_bypass.attr,
4734 &dev_attr_requested_microamps.attr,
4735 &dev_attr_min_microvolts.attr,
4736 &dev_attr_max_microvolts.attr,
4737 &dev_attr_min_microamps.attr,
4738 &dev_attr_max_microamps.attr,
4739 &dev_attr_suspend_standby_state.attr,
4740 &dev_attr_suspend_mem_state.attr,
4741 &dev_attr_suspend_disk_state.attr,
4742 &dev_attr_suspend_standby_microvolts.attr,
4743 &dev_attr_suspend_mem_microvolts.attr,
4744 &dev_attr_suspend_disk_microvolts.attr,
4745 &dev_attr_suspend_standby_mode.attr,
4746 &dev_attr_suspend_mem_mode.attr,
4747 &dev_attr_suspend_disk_mode.attr,
4752 * To avoid cluttering sysfs (and memory) with useless state, only
4753 * create attributes that can be meaningfully displayed.
4755 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4756 struct attribute *attr, int idx)
4758 struct device *dev = kobj_to_dev(kobj);
4759 struct regulator_dev *rdev = dev_to_rdev(dev);
4760 const struct regulator_ops *ops = rdev->desc->ops;
4761 umode_t mode = attr->mode;
4763 /* these three are always present */
4764 if (attr == &dev_attr_name.attr ||
4765 attr == &dev_attr_num_users.attr ||
4766 attr == &dev_attr_type.attr)
4769 /* some attributes need specific methods to be displayed */
4770 if (attr == &dev_attr_microvolts.attr) {
4771 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4772 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4773 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4774 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4779 if (attr == &dev_attr_microamps.attr)
4780 return ops->get_current_limit ? mode : 0;
4782 if (attr == &dev_attr_opmode.attr)
4783 return ops->get_mode ? mode : 0;
4785 if (attr == &dev_attr_state.attr)
4786 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4788 if (attr == &dev_attr_status.attr)
4789 return ops->get_status ? mode : 0;
4791 if (attr == &dev_attr_bypass.attr)
4792 return ops->get_bypass ? mode : 0;
4794 /* constraints need specific supporting methods */
4795 if (attr == &dev_attr_min_microvolts.attr ||
4796 attr == &dev_attr_max_microvolts.attr)
4797 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4799 if (attr == &dev_attr_min_microamps.attr ||
4800 attr == &dev_attr_max_microamps.attr)
4801 return ops->set_current_limit ? mode : 0;
4803 if (attr == &dev_attr_suspend_standby_state.attr ||
4804 attr == &dev_attr_suspend_mem_state.attr ||
4805 attr == &dev_attr_suspend_disk_state.attr)
4808 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4809 attr == &dev_attr_suspend_mem_microvolts.attr ||
4810 attr == &dev_attr_suspend_disk_microvolts.attr)
4811 return ops->set_suspend_voltage ? mode : 0;
4813 if (attr == &dev_attr_suspend_standby_mode.attr ||
4814 attr == &dev_attr_suspend_mem_mode.attr ||
4815 attr == &dev_attr_suspend_disk_mode.attr)
4816 return ops->set_suspend_mode ? mode : 0;
4821 static const struct attribute_group regulator_dev_group = {
4822 .attrs = regulator_dev_attrs,
4823 .is_visible = regulator_attr_is_visible,
4826 static const struct attribute_group *regulator_dev_groups[] = {
4827 ®ulator_dev_group,
4831 static void regulator_dev_release(struct device *dev)
4833 struct regulator_dev *rdev = dev_get_drvdata(dev);
4835 kfree(rdev->constraints);
4836 of_node_put(rdev->dev.of_node);
4840 static void rdev_init_debugfs(struct regulator_dev *rdev)
4842 struct device *parent = rdev->dev.parent;
4843 const char *rname = rdev_get_name(rdev);
4844 char name[NAME_MAX];
4846 /* Avoid duplicate debugfs directory names */
4847 if (parent && rname == rdev->desc->name) {
4848 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4853 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4854 if (!rdev->debugfs) {
4855 rdev_warn(rdev, "Failed to create debugfs directory\n");
4859 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4861 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4863 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4864 &rdev->bypass_count);
4867 static int regulator_register_resolve_supply(struct device *dev, void *data)
4869 struct regulator_dev *rdev = dev_to_rdev(dev);
4871 if (regulator_resolve_supply(rdev))
4872 rdev_dbg(rdev, "unable to resolve supply\n");
4877 int regulator_coupler_register(struct regulator_coupler *coupler)
4879 mutex_lock(®ulator_list_mutex);
4880 list_add_tail(&coupler->list, ®ulator_coupler_list);
4881 mutex_unlock(®ulator_list_mutex);
4886 static struct regulator_coupler *
4887 regulator_find_coupler(struct regulator_dev *rdev)
4889 struct regulator_coupler *coupler;
4893 * Note that regulators are appended to the list and the generic
4894 * coupler is registered first, hence it will be attached at last
4897 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) {
4898 err = coupler->attach_regulator(coupler, rdev);
4900 if (!coupler->balance_voltage &&
4901 rdev->coupling_desc.n_coupled > 2)
4902 goto err_unsupported;
4908 return ERR_PTR(err);
4916 return ERR_PTR(-EINVAL);
4919 if (coupler->detach_regulator)
4920 coupler->detach_regulator(coupler, rdev);
4923 "Voltage balancing for multiple regulator couples is unimplemented\n");
4925 return ERR_PTR(-EPERM);
4928 static void regulator_resolve_coupling(struct regulator_dev *rdev)
4930 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4931 struct coupling_desc *c_desc = &rdev->coupling_desc;
4932 int n_coupled = c_desc->n_coupled;
4933 struct regulator_dev *c_rdev;
4936 for (i = 1; i < n_coupled; i++) {
4937 /* already resolved */
4938 if (c_desc->coupled_rdevs[i])
4941 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4946 if (c_rdev->coupling_desc.coupler != coupler) {
4947 rdev_err(rdev, "coupler mismatch with %s\n",
4948 rdev_get_name(c_rdev));
4952 regulator_lock(c_rdev);
4954 c_desc->coupled_rdevs[i] = c_rdev;
4955 c_desc->n_resolved++;
4957 regulator_unlock(c_rdev);
4959 regulator_resolve_coupling(c_rdev);
4963 static void regulator_remove_coupling(struct regulator_dev *rdev)
4965 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4966 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4967 struct regulator_dev *__c_rdev, *c_rdev;
4968 unsigned int __n_coupled, n_coupled;
4972 n_coupled = c_desc->n_coupled;
4974 for (i = 1; i < n_coupled; i++) {
4975 c_rdev = c_desc->coupled_rdevs[i];
4980 regulator_lock(c_rdev);
4982 __c_desc = &c_rdev->coupling_desc;
4983 __n_coupled = __c_desc->n_coupled;
4985 for (k = 1; k < __n_coupled; k++) {
4986 __c_rdev = __c_desc->coupled_rdevs[k];
4988 if (__c_rdev == rdev) {
4989 __c_desc->coupled_rdevs[k] = NULL;
4990 __c_desc->n_resolved--;
4995 regulator_unlock(c_rdev);
4997 c_desc->coupled_rdevs[i] = NULL;
4998 c_desc->n_resolved--;
5001 if (coupler && coupler->detach_regulator) {
5002 err = coupler->detach_regulator(coupler, rdev);
5004 rdev_err(rdev, "failed to detach from coupler: %d\n",
5008 kfree(rdev->coupling_desc.coupled_rdevs);
5009 rdev->coupling_desc.coupled_rdevs = NULL;
5012 static int regulator_init_coupling(struct regulator_dev *rdev)
5014 int err, n_phandles;
5017 if (!IS_ENABLED(CONFIG_OF))
5020 n_phandles = of_get_n_coupled(rdev);
5022 alloc_size = sizeof(*rdev) * (n_phandles + 1);
5024 rdev->coupling_desc.coupled_rdevs = kzalloc(alloc_size, GFP_KERNEL);
5025 if (!rdev->coupling_desc.coupled_rdevs)
5029 * Every regulator should always have coupling descriptor filled with
5030 * at least pointer to itself.
5032 rdev->coupling_desc.coupled_rdevs[0] = rdev;
5033 rdev->coupling_desc.n_coupled = n_phandles + 1;
5034 rdev->coupling_desc.n_resolved++;
5036 /* regulator isn't coupled */
5037 if (n_phandles == 0)
5040 if (!of_check_coupling_data(rdev))
5043 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
5044 if (IS_ERR(rdev->coupling_desc.coupler)) {
5045 err = PTR_ERR(rdev->coupling_desc.coupler);
5046 rdev_err(rdev, "failed to get coupler: %d\n", err);
5053 static int generic_coupler_attach(struct regulator_coupler *coupler,
5054 struct regulator_dev *rdev)
5056 if (rdev->coupling_desc.n_coupled > 2) {
5058 "Voltage balancing for multiple regulator couples is unimplemented\n");
5062 if (!rdev->constraints->always_on) {
5064 "Coupling of a non always-on regulator is unimplemented\n");
5071 static struct regulator_coupler generic_regulator_coupler = {
5072 .attach_regulator = generic_coupler_attach,
5076 * regulator_register - register regulator
5077 * @regulator_desc: regulator to register
5078 * @cfg: runtime configuration for regulator
5080 * Called by regulator drivers to register a regulator.
5081 * Returns a valid pointer to struct regulator_dev on success
5082 * or an ERR_PTR() on error.
5084 struct regulator_dev *
5085 regulator_register(const struct regulator_desc *regulator_desc,
5086 const struct regulator_config *cfg)
5088 const struct regulation_constraints *constraints = NULL;
5089 const struct regulator_init_data *init_data;
5090 struct regulator_config *config = NULL;
5091 static atomic_t regulator_no = ATOMIC_INIT(-1);
5092 struct regulator_dev *rdev;
5093 bool dangling_cfg_gpiod = false;
5094 bool dangling_of_gpiod = false;
5095 bool reg_device_fail = false;
5100 return ERR_PTR(-EINVAL);
5102 dangling_cfg_gpiod = true;
5103 if (regulator_desc == NULL) {
5111 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5116 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5117 regulator_desc->type != REGULATOR_CURRENT) {
5122 /* Only one of each should be implemented */
5123 WARN_ON(regulator_desc->ops->get_voltage &&
5124 regulator_desc->ops->get_voltage_sel);
5125 WARN_ON(regulator_desc->ops->set_voltage &&
5126 regulator_desc->ops->set_voltage_sel);
5128 /* If we're using selectors we must implement list_voltage. */
5129 if (regulator_desc->ops->get_voltage_sel &&
5130 !regulator_desc->ops->list_voltage) {
5134 if (regulator_desc->ops->set_voltage_sel &&
5135 !regulator_desc->ops->list_voltage) {
5140 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5147 * Duplicate the config so the driver could override it after
5148 * parsing init data.
5150 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5151 if (config == NULL) {
5157 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5158 &rdev->dev.of_node);
5161 * Sometimes not all resources are probed already so we need to take
5162 * that into account. This happens most the time if the ena_gpiod comes
5163 * from a gpio extender or something else.
5165 if (PTR_ERR(init_data) == -EPROBE_DEFER) {
5168 ret = -EPROBE_DEFER;
5173 * We need to keep track of any GPIO descriptor coming from the
5174 * device tree until we have handled it over to the core. If the
5175 * config that was passed in to this function DOES NOT contain
5176 * a descriptor, and the config after this call DOES contain
5177 * a descriptor, we definitely got one from parsing the device
5180 if (!cfg->ena_gpiod && config->ena_gpiod)
5181 dangling_of_gpiod = true;
5183 init_data = config->init_data;
5184 rdev->dev.of_node = of_node_get(config->of_node);
5187 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
5188 rdev->reg_data = config->driver_data;
5189 rdev->owner = regulator_desc->owner;
5190 rdev->desc = regulator_desc;
5192 rdev->regmap = config->regmap;
5193 else if (dev_get_regmap(dev, NULL))
5194 rdev->regmap = dev_get_regmap(dev, NULL);
5195 else if (dev->parent)
5196 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5197 INIT_LIST_HEAD(&rdev->consumer_list);
5198 INIT_LIST_HEAD(&rdev->list);
5199 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5200 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5202 /* preform any regulator specific init */
5203 if (init_data && init_data->regulator_init) {
5204 ret = init_data->regulator_init(rdev->reg_data);
5209 if (config->ena_gpiod) {
5210 mutex_lock(®ulator_list_mutex);
5211 ret = regulator_ena_gpio_request(rdev, config);
5212 mutex_unlock(®ulator_list_mutex);
5214 rdev_err(rdev, "Failed to request enable GPIO: %d\n",
5218 /* The regulator core took over the GPIO descriptor */
5219 dangling_cfg_gpiod = false;
5220 dangling_of_gpiod = false;
5223 /* register with sysfs */
5224 rdev->dev.class = ®ulator_class;
5225 rdev->dev.parent = dev;
5226 dev_set_name(&rdev->dev, "regulator.%lu",
5227 (unsigned long) atomic_inc_return(®ulator_no));
5229 /* set regulator constraints */
5231 constraints = &init_data->constraints;
5233 if (init_data && init_data->supply_regulator)
5234 rdev->supply_name = init_data->supply_regulator;
5235 else if (regulator_desc->supply_name)
5236 rdev->supply_name = regulator_desc->supply_name;
5239 * Attempt to resolve the regulator supply, if specified,
5240 * but don't return an error if we fail because we will try
5241 * to resolve it again later as more regulators are added.
5243 if (regulator_resolve_supply(rdev))
5244 rdev_dbg(rdev, "unable to resolve supply\n");
5246 ret = set_machine_constraints(rdev, constraints);
5250 mutex_lock(®ulator_list_mutex);
5251 ret = regulator_init_coupling(rdev);
5252 mutex_unlock(®ulator_list_mutex);
5256 /* add consumers devices */
5258 mutex_lock(®ulator_list_mutex);
5259 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5260 ret = set_consumer_device_supply(rdev,
5261 init_data->consumer_supplies[i].dev_name,
5262 init_data->consumer_supplies[i].supply);
5264 mutex_unlock(®ulator_list_mutex);
5265 dev_err(dev, "Failed to set supply %s\n",
5266 init_data->consumer_supplies[i].supply);
5267 goto unset_supplies;
5270 mutex_unlock(®ulator_list_mutex);
5273 if (!rdev->desc->ops->get_voltage &&
5274 !rdev->desc->ops->list_voltage &&
5275 !rdev->desc->fixed_uV)
5276 rdev->is_switch = true;
5278 dev_set_drvdata(&rdev->dev, rdev);
5279 ret = device_register(&rdev->dev);
5281 reg_device_fail = true;
5282 goto unset_supplies;
5285 rdev_init_debugfs(rdev);
5287 /* try to resolve regulators coupling since a new one was registered */
5288 mutex_lock(®ulator_list_mutex);
5289 regulator_resolve_coupling(rdev);
5290 mutex_unlock(®ulator_list_mutex);
5292 /* try to resolve regulators supply since a new one was registered */
5293 class_for_each_device(®ulator_class, NULL, NULL,
5294 regulator_register_resolve_supply);
5299 mutex_lock(®ulator_list_mutex);
5300 unset_regulator_supplies(rdev);
5301 regulator_remove_coupling(rdev);
5302 mutex_unlock(®ulator_list_mutex);
5304 kfree(rdev->coupling_desc.coupled_rdevs);
5305 kfree(rdev->constraints);
5306 mutex_lock(®ulator_list_mutex);
5307 regulator_ena_gpio_free(rdev);
5308 mutex_unlock(®ulator_list_mutex);
5310 if (dangling_of_gpiod)
5311 gpiod_put(config->ena_gpiod);
5312 if (reg_device_fail)
5313 put_device(&rdev->dev);
5318 if (dangling_cfg_gpiod)
5319 gpiod_put(cfg->ena_gpiod);
5320 return ERR_PTR(ret);
5322 EXPORT_SYMBOL_GPL(regulator_register);
5325 * regulator_unregister - unregister regulator
5326 * @rdev: regulator to unregister
5328 * Called by regulator drivers to unregister a regulator.
5330 void regulator_unregister(struct regulator_dev *rdev)
5336 while (rdev->use_count--)
5337 regulator_disable(rdev->supply);
5338 regulator_put(rdev->supply);
5341 flush_work(&rdev->disable_work.work);
5343 mutex_lock(®ulator_list_mutex);
5345 debugfs_remove_recursive(rdev->debugfs);
5346 WARN_ON(rdev->open_count);
5347 regulator_remove_coupling(rdev);
5348 unset_regulator_supplies(rdev);
5349 list_del(&rdev->list);
5350 regulator_ena_gpio_free(rdev);
5351 device_unregister(&rdev->dev);
5353 mutex_unlock(®ulator_list_mutex);
5355 EXPORT_SYMBOL_GPL(regulator_unregister);
5357 #ifdef CONFIG_SUSPEND
5359 * regulator_suspend - prepare regulators for system wide suspend
5360 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5362 * Configure each regulator with it's suspend operating parameters for state.
5364 static int regulator_suspend(struct device *dev)
5366 struct regulator_dev *rdev = dev_to_rdev(dev);
5367 suspend_state_t state = pm_suspend_target_state;
5370 regulator_lock(rdev);
5371 ret = suspend_set_state(rdev, state);
5372 regulator_unlock(rdev);
5377 static int regulator_resume(struct device *dev)
5379 suspend_state_t state = pm_suspend_target_state;
5380 struct regulator_dev *rdev = dev_to_rdev(dev);
5381 struct regulator_state *rstate;
5384 rstate = regulator_get_suspend_state(rdev, state);
5388 regulator_lock(rdev);
5390 if (rdev->desc->ops->resume &&
5391 (rstate->enabled == ENABLE_IN_SUSPEND ||
5392 rstate->enabled == DISABLE_IN_SUSPEND))
5393 ret = rdev->desc->ops->resume(rdev);
5395 regulator_unlock(rdev);
5399 #else /* !CONFIG_SUSPEND */
5401 #define regulator_suspend NULL
5402 #define regulator_resume NULL
5404 #endif /* !CONFIG_SUSPEND */
5407 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5408 .suspend = regulator_suspend,
5409 .resume = regulator_resume,
5413 struct class regulator_class = {
5414 .name = "regulator",
5415 .dev_release = regulator_dev_release,
5416 .dev_groups = regulator_dev_groups,
5418 .pm = ®ulator_pm_ops,
5422 * regulator_has_full_constraints - the system has fully specified constraints
5424 * Calling this function will cause the regulator API to disable all
5425 * regulators which have a zero use count and don't have an always_on
5426 * constraint in a late_initcall.
5428 * The intention is that this will become the default behaviour in a
5429 * future kernel release so users are encouraged to use this facility
5432 void regulator_has_full_constraints(void)
5434 has_full_constraints = 1;
5436 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5439 * rdev_get_drvdata - get rdev regulator driver data
5442 * Get rdev regulator driver private data. This call can be used in the
5443 * regulator driver context.
5445 void *rdev_get_drvdata(struct regulator_dev *rdev)
5447 return rdev->reg_data;
5449 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5452 * regulator_get_drvdata - get regulator driver data
5453 * @regulator: regulator
5455 * Get regulator driver private data. This call can be used in the consumer
5456 * driver context when non API regulator specific functions need to be called.
5458 void *regulator_get_drvdata(struct regulator *regulator)
5460 return regulator->rdev->reg_data;
5462 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5465 * regulator_set_drvdata - set regulator driver data
5466 * @regulator: regulator
5469 void regulator_set_drvdata(struct regulator *regulator, void *data)
5471 regulator->rdev->reg_data = data;
5473 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5476 * regulator_get_id - get regulator ID
5479 int rdev_get_id(struct regulator_dev *rdev)
5481 return rdev->desc->id;
5483 EXPORT_SYMBOL_GPL(rdev_get_id);
5485 struct device *rdev_get_dev(struct regulator_dev *rdev)
5489 EXPORT_SYMBOL_GPL(rdev_get_dev);
5491 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5493 return rdev->regmap;
5495 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5497 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5499 return reg_init_data->driver_data;
5501 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5503 #ifdef CONFIG_DEBUG_FS
5504 static int supply_map_show(struct seq_file *sf, void *data)
5506 struct regulator_map *map;
5508 list_for_each_entry(map, ®ulator_map_list, list) {
5509 seq_printf(sf, "%s -> %s.%s\n",
5510 rdev_get_name(map->regulator), map->dev_name,
5516 DEFINE_SHOW_ATTRIBUTE(supply_map);
5518 struct summary_data {
5520 struct regulator_dev *parent;
5524 static void regulator_summary_show_subtree(struct seq_file *s,
5525 struct regulator_dev *rdev,
5528 static int regulator_summary_show_children(struct device *dev, void *data)
5530 struct regulator_dev *rdev = dev_to_rdev(dev);
5531 struct summary_data *summary_data = data;
5533 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5534 regulator_summary_show_subtree(summary_data->s, rdev,
5535 summary_data->level + 1);
5540 static void regulator_summary_show_subtree(struct seq_file *s,
5541 struct regulator_dev *rdev,
5544 struct regulation_constraints *c;
5545 struct regulator *consumer;
5546 struct summary_data summary_data;
5547 unsigned int opmode;
5552 opmode = _regulator_get_mode_unlocked(rdev);
5553 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5555 30 - level * 3, rdev_get_name(rdev),
5556 rdev->use_count, rdev->open_count, rdev->bypass_count,
5557 regulator_opmode_to_str(opmode));
5559 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5560 seq_printf(s, "%5dmA ",
5561 _regulator_get_current_limit_unlocked(rdev) / 1000);
5563 c = rdev->constraints;
5565 switch (rdev->desc->type) {
5566 case REGULATOR_VOLTAGE:
5567 seq_printf(s, "%5dmV %5dmV ",
5568 c->min_uV / 1000, c->max_uV / 1000);
5570 case REGULATOR_CURRENT:
5571 seq_printf(s, "%5dmA %5dmA ",
5572 c->min_uA / 1000, c->max_uA / 1000);
5579 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5580 if (consumer->dev && consumer->dev->class == ®ulator_class)
5583 seq_printf(s, "%*s%-*s ",
5584 (level + 1) * 3 + 1, "",
5585 30 - (level + 1) * 3,
5586 consumer->supply_name ? consumer->supply_name :
5587 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5589 switch (rdev->desc->type) {
5590 case REGULATOR_VOLTAGE:
5591 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5592 consumer->enable_count,
5593 consumer->uA_load / 1000,
5594 consumer->uA_load && !consumer->enable_count ?
5596 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5597 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5599 case REGULATOR_CURRENT:
5607 summary_data.level = level;
5608 summary_data.parent = rdev;
5610 class_for_each_device(®ulator_class, NULL, &summary_data,
5611 regulator_summary_show_children);
5614 struct summary_lock_data {
5615 struct ww_acquire_ctx *ww_ctx;
5616 struct regulator_dev **new_contended_rdev;
5617 struct regulator_dev **old_contended_rdev;
5620 static int regulator_summary_lock_one(struct device *dev, void *data)
5622 struct regulator_dev *rdev = dev_to_rdev(dev);
5623 struct summary_lock_data *lock_data = data;
5626 if (rdev != *lock_data->old_contended_rdev) {
5627 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5629 if (ret == -EDEADLK)
5630 *lock_data->new_contended_rdev = rdev;
5634 *lock_data->old_contended_rdev = NULL;
5640 static int regulator_summary_unlock_one(struct device *dev, void *data)
5642 struct regulator_dev *rdev = dev_to_rdev(dev);
5643 struct summary_lock_data *lock_data = data;
5646 if (rdev == *lock_data->new_contended_rdev)
5650 regulator_unlock(rdev);
5655 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5656 struct regulator_dev **new_contended_rdev,
5657 struct regulator_dev **old_contended_rdev)
5659 struct summary_lock_data lock_data;
5662 lock_data.ww_ctx = ww_ctx;
5663 lock_data.new_contended_rdev = new_contended_rdev;
5664 lock_data.old_contended_rdev = old_contended_rdev;
5666 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5667 regulator_summary_lock_one);
5669 class_for_each_device(®ulator_class, NULL, &lock_data,
5670 regulator_summary_unlock_one);
5675 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5677 struct regulator_dev *new_contended_rdev = NULL;
5678 struct regulator_dev *old_contended_rdev = NULL;
5681 mutex_lock(®ulator_list_mutex);
5683 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5686 if (new_contended_rdev) {
5687 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5688 old_contended_rdev = new_contended_rdev;
5689 old_contended_rdev->ref_cnt++;
5692 err = regulator_summary_lock_all(ww_ctx,
5693 &new_contended_rdev,
5694 &old_contended_rdev);
5696 if (old_contended_rdev)
5697 regulator_unlock(old_contended_rdev);
5699 } while (err == -EDEADLK);
5701 ww_acquire_done(ww_ctx);
5704 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5706 class_for_each_device(®ulator_class, NULL, NULL,
5707 regulator_summary_unlock_one);
5708 ww_acquire_fini(ww_ctx);
5710 mutex_unlock(®ulator_list_mutex);
5713 static int regulator_summary_show_roots(struct device *dev, void *data)
5715 struct regulator_dev *rdev = dev_to_rdev(dev);
5716 struct seq_file *s = data;
5719 regulator_summary_show_subtree(s, rdev, 0);
5724 static int regulator_summary_show(struct seq_file *s, void *data)
5726 struct ww_acquire_ctx ww_ctx;
5728 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5729 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5731 regulator_summary_lock(&ww_ctx);
5733 class_for_each_device(®ulator_class, NULL, s,
5734 regulator_summary_show_roots);
5736 regulator_summary_unlock(&ww_ctx);
5740 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5741 #endif /* CONFIG_DEBUG_FS */
5743 static int __init regulator_init(void)
5747 ret = class_register(®ulator_class);
5749 debugfs_root = debugfs_create_dir("regulator", NULL);
5751 pr_warn("regulator: Failed to create debugfs directory\n");
5753 #ifdef CONFIG_DEBUG_FS
5754 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5757 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5758 NULL, ®ulator_summary_fops);
5760 regulator_dummy_init();
5762 regulator_coupler_register(&generic_regulator_coupler);
5767 /* init early to allow our consumers to complete system booting */
5768 core_initcall(regulator_init);
5770 static int regulator_late_cleanup(struct device *dev, void *data)
5772 struct regulator_dev *rdev = dev_to_rdev(dev);
5773 const struct regulator_ops *ops = rdev->desc->ops;
5774 struct regulation_constraints *c = rdev->constraints;
5777 if (c && c->always_on)
5780 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5783 regulator_lock(rdev);
5785 if (rdev->use_count)
5788 /* If we can't read the status assume it's on. */
5789 if (ops->is_enabled)
5790 enabled = ops->is_enabled(rdev);
5797 if (have_full_constraints()) {
5798 /* We log since this may kill the system if it goes
5800 rdev_info(rdev, "disabling\n");
5801 ret = _regulator_do_disable(rdev);
5803 rdev_err(rdev, "couldn't disable: %d\n", ret);
5805 /* The intention is that in future we will
5806 * assume that full constraints are provided
5807 * so warn even if we aren't going to do
5810 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5814 regulator_unlock(rdev);
5819 static void regulator_init_complete_work_function(struct work_struct *work)
5822 * Regulators may had failed to resolve their input supplies
5823 * when were registered, either because the input supply was
5824 * not registered yet or because its parent device was not
5825 * bound yet. So attempt to resolve the input supplies for
5826 * pending regulators before trying to disable unused ones.
5828 class_for_each_device(®ulator_class, NULL, NULL,
5829 regulator_register_resolve_supply);
5831 /* If we have a full configuration then disable any regulators
5832 * we have permission to change the status for and which are
5833 * not in use or always_on. This is effectively the default
5834 * for DT and ACPI as they have full constraints.
5836 class_for_each_device(®ulator_class, NULL, NULL,
5837 regulator_late_cleanup);
5840 static DECLARE_DELAYED_WORK(regulator_init_complete_work,
5841 regulator_init_complete_work_function);
5843 static int __init regulator_init_complete(void)
5846 * Since DT doesn't provide an idiomatic mechanism for
5847 * enabling full constraints and since it's much more natural
5848 * with DT to provide them just assume that a DT enabled
5849 * system has full constraints.
5851 if (of_have_populated_dt())
5852 has_full_constraints = true;
5855 * We punt completion for an arbitrary amount of time since
5856 * systems like distros will load many drivers from userspace
5857 * so consumers might not always be ready yet, this is
5858 * particularly an issue with laptops where this might bounce
5859 * the display off then on. Ideally we'd get a notification
5860 * from userspace when this happens but we don't so just wait
5861 * a bit and hope we waited long enough. It'd be better if
5862 * we'd only do this on systems that need it, and a kernel
5863 * command line option might be useful.
5865 schedule_delayed_work(®ulator_init_complete_work,
5866 msecs_to_jiffies(30000));
5870 late_initcall_sync(regulator_init_complete);