2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/gpio/consumer.h>
29 #include <linux/regmap.h>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/module.h>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/regulator.h>
42 #define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50 #define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
53 static DEFINE_WW_CLASS(regulator_ww_class);
54 static DEFINE_MUTEX(regulator_nesting_mutex);
55 static DEFINE_MUTEX(regulator_list_mutex);
56 static LIST_HEAD(regulator_map_list);
57 static LIST_HEAD(regulator_ena_gpio_list);
58 static LIST_HEAD(regulator_supply_alias_list);
59 static bool has_full_constraints;
61 static struct dentry *debugfs_root;
64 * struct regulator_map
66 * Used to provide symbolic supply names to devices.
68 struct regulator_map {
69 struct list_head list;
70 const char *dev_name; /* The dev_name() for the consumer */
72 struct regulator_dev *regulator;
76 * struct regulator_enable_gpio
78 * Management for shared enable GPIO pin
80 struct regulator_enable_gpio {
81 struct list_head list;
82 struct gpio_desc *gpiod;
83 u32 enable_count; /* a number of enabled shared GPIO */
84 u32 request_count; /* a number of requested shared GPIO */
85 unsigned int ena_gpio_invert:1;
89 * struct regulator_supply_alias
91 * Used to map lookups for a supply onto an alternative device.
93 struct regulator_supply_alias {
94 struct list_head list;
95 struct device *src_dev;
96 const char *src_supply;
97 struct device *alias_dev;
98 const char *alias_supply;
101 static int _regulator_is_enabled(struct regulator_dev *rdev);
102 static int _regulator_disable(struct regulator *regulator);
103 static int _regulator_get_voltage(struct regulator_dev *rdev);
104 static int _regulator_get_current_limit(struct regulator_dev *rdev);
105 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
106 static int _notifier_call_chain(struct regulator_dev *rdev,
107 unsigned long event, void *data);
108 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
109 int min_uV, int max_uV);
110 static int regulator_balance_voltage(struct regulator_dev *rdev,
111 suspend_state_t state);
112 static int regulator_set_voltage_rdev(struct regulator_dev *rdev,
113 int min_uV, int max_uV,
114 suspend_state_t state);
115 static struct regulator *create_regulator(struct regulator_dev *rdev,
117 const char *supply_name);
118 static void _regulator_put(struct regulator *regulator);
120 static const char *rdev_get_name(struct regulator_dev *rdev)
122 if (rdev->constraints && rdev->constraints->name)
123 return rdev->constraints->name;
124 else if (rdev->desc->name)
125 return rdev->desc->name;
130 static bool have_full_constraints(void)
132 return has_full_constraints || of_have_populated_dt();
135 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
137 if (!rdev->constraints) {
138 rdev_err(rdev, "no constraints\n");
142 if (rdev->constraints->valid_ops_mask & ops)
148 static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
150 if (rdev && rdev->supply)
151 return rdev->supply->rdev;
157 * regulator_lock_nested - lock a single regulator
158 * @rdev: regulator source
159 * @ww_ctx: w/w mutex acquire context
161 * This function can be called many times by one task on
162 * a single regulator and its mutex will be locked only
163 * once. If a task, which is calling this function is other
164 * than the one, which initially locked the mutex, it will
167 static inline int regulator_lock_nested(struct regulator_dev *rdev,
168 struct ww_acquire_ctx *ww_ctx)
173 mutex_lock(®ulator_nesting_mutex);
175 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
176 if (rdev->mutex_owner == current)
182 mutex_unlock(®ulator_nesting_mutex);
183 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
184 mutex_lock(®ulator_nesting_mutex);
190 if (lock && ret != -EDEADLK) {
192 rdev->mutex_owner = current;
195 mutex_unlock(®ulator_nesting_mutex);
201 * regulator_lock - lock a single regulator
202 * @rdev: regulator source
204 * This function can be called many times by one task on
205 * a single regulator and its mutex will be locked only
206 * once. If a task, which is calling this function is other
207 * than the one, which initially locked the mutex, it will
210 void regulator_lock(struct regulator_dev *rdev)
212 regulator_lock_nested(rdev, NULL);
216 * regulator_unlock - unlock a single regulator
217 * @rdev: regulator_source
219 * This function unlocks the mutex when the
220 * reference counter reaches 0.
222 void regulator_unlock(struct regulator_dev *rdev)
224 mutex_lock(®ulator_nesting_mutex);
226 if (--rdev->ref_cnt == 0) {
227 rdev->mutex_owner = NULL;
228 ww_mutex_unlock(&rdev->mutex);
231 WARN_ON_ONCE(rdev->ref_cnt < 0);
233 mutex_unlock(®ulator_nesting_mutex);
236 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
238 struct regulator_dev *c_rdev;
241 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
242 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
244 if (rdev->supply->rdev == c_rdev)
251 static void regulator_unlock_recursive(struct regulator_dev *rdev,
252 unsigned int n_coupled)
254 struct regulator_dev *c_rdev;
257 for (i = n_coupled; i > 0; i--) {
258 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
263 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
264 regulator_unlock_recursive(
265 c_rdev->supply->rdev,
266 c_rdev->coupling_desc.n_coupled);
268 regulator_unlock(c_rdev);
272 static int regulator_lock_recursive(struct regulator_dev *rdev,
273 struct regulator_dev **new_contended_rdev,
274 struct regulator_dev **old_contended_rdev,
275 struct ww_acquire_ctx *ww_ctx)
277 struct regulator_dev *c_rdev;
280 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
281 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
286 if (c_rdev != *old_contended_rdev) {
287 err = regulator_lock_nested(c_rdev, ww_ctx);
289 if (err == -EDEADLK) {
290 *new_contended_rdev = c_rdev;
294 /* shouldn't happen */
295 WARN_ON_ONCE(err != -EALREADY);
298 *old_contended_rdev = NULL;
301 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
302 err = regulator_lock_recursive(c_rdev->supply->rdev,
307 regulator_unlock(c_rdev);
316 regulator_unlock_recursive(rdev, i);
322 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
324 * @rdev: regulator source
325 * @ww_ctx: w/w mutex acquire context
327 * Unlock all regulators related with rdev by coupling or suppling.
329 static void regulator_unlock_dependent(struct regulator_dev *rdev,
330 struct ww_acquire_ctx *ww_ctx)
332 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
333 ww_acquire_fini(ww_ctx);
337 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
338 * @rdev: regulator source
339 * @ww_ctx: w/w mutex acquire context
341 * This function as a wrapper on regulator_lock_recursive(), which locks
342 * all regulators related with rdev by coupling or suppling.
344 static void regulator_lock_dependent(struct regulator_dev *rdev,
345 struct ww_acquire_ctx *ww_ctx)
347 struct regulator_dev *new_contended_rdev = NULL;
348 struct regulator_dev *old_contended_rdev = NULL;
351 mutex_lock(®ulator_list_mutex);
353 ww_acquire_init(ww_ctx, ®ulator_ww_class);
356 if (new_contended_rdev) {
357 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
358 old_contended_rdev = new_contended_rdev;
359 old_contended_rdev->ref_cnt++;
362 err = regulator_lock_recursive(rdev,
367 if (old_contended_rdev)
368 regulator_unlock(old_contended_rdev);
370 } while (err == -EDEADLK);
372 ww_acquire_done(ww_ctx);
374 mutex_unlock(®ulator_list_mutex);
378 * of_get_child_regulator - get a child regulator device node
379 * based on supply name
380 * @parent: Parent device node
381 * @prop_name: Combination regulator supply name and "-supply"
383 * Traverse all child nodes.
384 * Extract the child regulator device node corresponding to the supply name.
385 * returns the device node corresponding to the regulator if found, else
388 static struct device_node *of_get_child_regulator(struct device_node *parent,
389 const char *prop_name)
391 struct device_node *regnode = NULL;
392 struct device_node *child = NULL;
394 for_each_child_of_node(parent, child) {
395 regnode = of_parse_phandle(child, prop_name, 0);
398 regnode = of_get_child_regulator(child, prop_name);
409 * of_get_regulator - get a regulator device node based on supply name
410 * @dev: Device pointer for the consumer (of regulator) device
411 * @supply: regulator supply name
413 * Extract the regulator device node corresponding to the supply name.
414 * returns the device node corresponding to the regulator if found, else
417 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
419 struct device_node *regnode = NULL;
420 char prop_name[32]; /* 32 is max size of property name */
422 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
424 snprintf(prop_name, 32, "%s-supply", supply);
425 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
428 regnode = of_get_child_regulator(dev->of_node, prop_name);
432 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
433 prop_name, dev->of_node);
439 /* Platform voltage constraint check */
440 static int regulator_check_voltage(struct regulator_dev *rdev,
441 int *min_uV, int *max_uV)
443 BUG_ON(*min_uV > *max_uV);
445 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
446 rdev_err(rdev, "voltage operation not allowed\n");
450 if (*max_uV > rdev->constraints->max_uV)
451 *max_uV = rdev->constraints->max_uV;
452 if (*min_uV < rdev->constraints->min_uV)
453 *min_uV = rdev->constraints->min_uV;
455 if (*min_uV > *max_uV) {
456 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
464 /* return 0 if the state is valid */
465 static int regulator_check_states(suspend_state_t state)
467 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
470 /* Make sure we select a voltage that suits the needs of all
471 * regulator consumers
473 static int regulator_check_consumers(struct regulator_dev *rdev,
474 int *min_uV, int *max_uV,
475 suspend_state_t state)
477 struct regulator *regulator;
478 struct regulator_voltage *voltage;
480 list_for_each_entry(regulator, &rdev->consumer_list, list) {
481 voltage = ®ulator->voltage[state];
483 * Assume consumers that didn't say anything are OK
484 * with anything in the constraint range.
486 if (!voltage->min_uV && !voltage->max_uV)
489 if (*max_uV > voltage->max_uV)
490 *max_uV = voltage->max_uV;
491 if (*min_uV < voltage->min_uV)
492 *min_uV = voltage->min_uV;
495 if (*min_uV > *max_uV) {
496 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
504 /* current constraint check */
505 static int regulator_check_current_limit(struct regulator_dev *rdev,
506 int *min_uA, int *max_uA)
508 BUG_ON(*min_uA > *max_uA);
510 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
511 rdev_err(rdev, "current operation not allowed\n");
515 if (*max_uA > rdev->constraints->max_uA)
516 *max_uA = rdev->constraints->max_uA;
517 if (*min_uA < rdev->constraints->min_uA)
518 *min_uA = rdev->constraints->min_uA;
520 if (*min_uA > *max_uA) {
521 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
529 /* operating mode constraint check */
530 static int regulator_mode_constrain(struct regulator_dev *rdev,
534 case REGULATOR_MODE_FAST:
535 case REGULATOR_MODE_NORMAL:
536 case REGULATOR_MODE_IDLE:
537 case REGULATOR_MODE_STANDBY:
540 rdev_err(rdev, "invalid mode %x specified\n", *mode);
544 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
545 rdev_err(rdev, "mode operation not allowed\n");
549 /* The modes are bitmasks, the most power hungry modes having
550 * the lowest values. If the requested mode isn't supported
551 * try higher modes. */
553 if (rdev->constraints->valid_modes_mask & *mode)
561 static inline struct regulator_state *
562 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
564 if (rdev->constraints == NULL)
568 case PM_SUSPEND_STANDBY:
569 return &rdev->constraints->state_standby;
571 return &rdev->constraints->state_mem;
573 return &rdev->constraints->state_disk;
579 static ssize_t regulator_uV_show(struct device *dev,
580 struct device_attribute *attr, char *buf)
582 struct regulator_dev *rdev = dev_get_drvdata(dev);
585 regulator_lock(rdev);
586 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
587 regulator_unlock(rdev);
591 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
593 static ssize_t regulator_uA_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
596 struct regulator_dev *rdev = dev_get_drvdata(dev);
598 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
600 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
602 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
605 struct regulator_dev *rdev = dev_get_drvdata(dev);
607 return sprintf(buf, "%s\n", rdev_get_name(rdev));
609 static DEVICE_ATTR_RO(name);
611 static const char *regulator_opmode_to_str(int mode)
614 case REGULATOR_MODE_FAST:
616 case REGULATOR_MODE_NORMAL:
618 case REGULATOR_MODE_IDLE:
620 case REGULATOR_MODE_STANDBY:
626 static ssize_t regulator_print_opmode(char *buf, int mode)
628 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
631 static ssize_t regulator_opmode_show(struct device *dev,
632 struct device_attribute *attr, char *buf)
634 struct regulator_dev *rdev = dev_get_drvdata(dev);
636 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
638 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
640 static ssize_t regulator_print_state(char *buf, int state)
643 return sprintf(buf, "enabled\n");
645 return sprintf(buf, "disabled\n");
647 return sprintf(buf, "unknown\n");
650 static ssize_t regulator_state_show(struct device *dev,
651 struct device_attribute *attr, char *buf)
653 struct regulator_dev *rdev = dev_get_drvdata(dev);
656 regulator_lock(rdev);
657 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
658 regulator_unlock(rdev);
662 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
664 static ssize_t regulator_status_show(struct device *dev,
665 struct device_attribute *attr, char *buf)
667 struct regulator_dev *rdev = dev_get_drvdata(dev);
671 status = rdev->desc->ops->get_status(rdev);
676 case REGULATOR_STATUS_OFF:
679 case REGULATOR_STATUS_ON:
682 case REGULATOR_STATUS_ERROR:
685 case REGULATOR_STATUS_FAST:
688 case REGULATOR_STATUS_NORMAL:
691 case REGULATOR_STATUS_IDLE:
694 case REGULATOR_STATUS_STANDBY:
697 case REGULATOR_STATUS_BYPASS:
700 case REGULATOR_STATUS_UNDEFINED:
707 return sprintf(buf, "%s\n", label);
709 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
711 static ssize_t regulator_min_uA_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
714 struct regulator_dev *rdev = dev_get_drvdata(dev);
716 if (!rdev->constraints)
717 return sprintf(buf, "constraint not defined\n");
719 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
721 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
723 static ssize_t regulator_max_uA_show(struct device *dev,
724 struct device_attribute *attr, char *buf)
726 struct regulator_dev *rdev = dev_get_drvdata(dev);
728 if (!rdev->constraints)
729 return sprintf(buf, "constraint not defined\n");
731 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
733 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
735 static ssize_t regulator_min_uV_show(struct device *dev,
736 struct device_attribute *attr, char *buf)
738 struct regulator_dev *rdev = dev_get_drvdata(dev);
740 if (!rdev->constraints)
741 return sprintf(buf, "constraint not defined\n");
743 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
745 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
747 static ssize_t regulator_max_uV_show(struct device *dev,
748 struct device_attribute *attr, char *buf)
750 struct regulator_dev *rdev = dev_get_drvdata(dev);
752 if (!rdev->constraints)
753 return sprintf(buf, "constraint not defined\n");
755 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
757 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
759 static ssize_t regulator_total_uA_show(struct device *dev,
760 struct device_attribute *attr, char *buf)
762 struct regulator_dev *rdev = dev_get_drvdata(dev);
763 struct regulator *regulator;
766 regulator_lock(rdev);
767 list_for_each_entry(regulator, &rdev->consumer_list, list) {
768 if (regulator->enable_count)
769 uA += regulator->uA_load;
771 regulator_unlock(rdev);
772 return sprintf(buf, "%d\n", uA);
774 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
776 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
779 struct regulator_dev *rdev = dev_get_drvdata(dev);
780 return sprintf(buf, "%d\n", rdev->use_count);
782 static DEVICE_ATTR_RO(num_users);
784 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
787 struct regulator_dev *rdev = dev_get_drvdata(dev);
789 switch (rdev->desc->type) {
790 case REGULATOR_VOLTAGE:
791 return sprintf(buf, "voltage\n");
792 case REGULATOR_CURRENT:
793 return sprintf(buf, "current\n");
795 return sprintf(buf, "unknown\n");
797 static DEVICE_ATTR_RO(type);
799 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
800 struct device_attribute *attr, char *buf)
802 struct regulator_dev *rdev = dev_get_drvdata(dev);
804 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
806 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
807 regulator_suspend_mem_uV_show, NULL);
809 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
810 struct device_attribute *attr, char *buf)
812 struct regulator_dev *rdev = dev_get_drvdata(dev);
814 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
816 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
817 regulator_suspend_disk_uV_show, NULL);
819 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
820 struct device_attribute *attr, char *buf)
822 struct regulator_dev *rdev = dev_get_drvdata(dev);
824 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
826 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
827 regulator_suspend_standby_uV_show, NULL);
829 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
830 struct device_attribute *attr, char *buf)
832 struct regulator_dev *rdev = dev_get_drvdata(dev);
834 return regulator_print_opmode(buf,
835 rdev->constraints->state_mem.mode);
837 static DEVICE_ATTR(suspend_mem_mode, 0444,
838 regulator_suspend_mem_mode_show, NULL);
840 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
841 struct device_attribute *attr, char *buf)
843 struct regulator_dev *rdev = dev_get_drvdata(dev);
845 return regulator_print_opmode(buf,
846 rdev->constraints->state_disk.mode);
848 static DEVICE_ATTR(suspend_disk_mode, 0444,
849 regulator_suspend_disk_mode_show, NULL);
851 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
852 struct device_attribute *attr, char *buf)
854 struct regulator_dev *rdev = dev_get_drvdata(dev);
856 return regulator_print_opmode(buf,
857 rdev->constraints->state_standby.mode);
859 static DEVICE_ATTR(suspend_standby_mode, 0444,
860 regulator_suspend_standby_mode_show, NULL);
862 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
863 struct device_attribute *attr, char *buf)
865 struct regulator_dev *rdev = dev_get_drvdata(dev);
867 return regulator_print_state(buf,
868 rdev->constraints->state_mem.enabled);
870 static DEVICE_ATTR(suspend_mem_state, 0444,
871 regulator_suspend_mem_state_show, NULL);
873 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
874 struct device_attribute *attr, char *buf)
876 struct regulator_dev *rdev = dev_get_drvdata(dev);
878 return regulator_print_state(buf,
879 rdev->constraints->state_disk.enabled);
881 static DEVICE_ATTR(suspend_disk_state, 0444,
882 regulator_suspend_disk_state_show, NULL);
884 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
885 struct device_attribute *attr, char *buf)
887 struct regulator_dev *rdev = dev_get_drvdata(dev);
889 return regulator_print_state(buf,
890 rdev->constraints->state_standby.enabled);
892 static DEVICE_ATTR(suspend_standby_state, 0444,
893 regulator_suspend_standby_state_show, NULL);
895 static ssize_t regulator_bypass_show(struct device *dev,
896 struct device_attribute *attr, char *buf)
898 struct regulator_dev *rdev = dev_get_drvdata(dev);
903 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
912 return sprintf(buf, "%s\n", report);
914 static DEVICE_ATTR(bypass, 0444,
915 regulator_bypass_show, NULL);
917 /* Calculate the new optimum regulator operating mode based on the new total
918 * consumer load. All locks held by caller */
919 static int drms_uA_update(struct regulator_dev *rdev)
921 struct regulator *sibling;
922 int current_uA = 0, output_uV, input_uV, err;
925 lockdep_assert_held_once(&rdev->mutex.base);
928 * first check to see if we can set modes at all, otherwise just
929 * tell the consumer everything is OK.
931 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
934 if (!rdev->desc->ops->get_optimum_mode &&
935 !rdev->desc->ops->set_load)
938 if (!rdev->desc->ops->set_mode &&
939 !rdev->desc->ops->set_load)
942 /* calc total requested load */
943 list_for_each_entry(sibling, &rdev->consumer_list, list) {
944 if (sibling->enable_count)
945 current_uA += sibling->uA_load;
948 current_uA += rdev->constraints->system_load;
950 if (rdev->desc->ops->set_load) {
951 /* set the optimum mode for our new total regulator load */
952 err = rdev->desc->ops->set_load(rdev, current_uA);
954 rdev_err(rdev, "failed to set load %d\n", current_uA);
956 /* get output voltage */
957 output_uV = _regulator_get_voltage(rdev);
958 if (output_uV <= 0) {
959 rdev_err(rdev, "invalid output voltage found\n");
963 /* get input voltage */
966 input_uV = regulator_get_voltage(rdev->supply);
968 input_uV = rdev->constraints->input_uV;
970 rdev_err(rdev, "invalid input voltage found\n");
974 /* now get the optimum mode for our new total regulator load */
975 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
976 output_uV, current_uA);
978 /* check the new mode is allowed */
979 err = regulator_mode_constrain(rdev, &mode);
981 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
982 current_uA, input_uV, output_uV);
986 err = rdev->desc->ops->set_mode(rdev, mode);
988 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
994 static int suspend_set_state(struct regulator_dev *rdev,
995 suspend_state_t state)
998 struct regulator_state *rstate;
1000 rstate = regulator_get_suspend_state(rdev, state);
1004 /* If we have no suspend mode configration don't set anything;
1005 * only warn if the driver implements set_suspend_voltage or
1006 * set_suspend_mode callback.
1008 if (rstate->enabled != ENABLE_IN_SUSPEND &&
1009 rstate->enabled != DISABLE_IN_SUSPEND) {
1010 if (rdev->desc->ops->set_suspend_voltage ||
1011 rdev->desc->ops->set_suspend_mode)
1012 rdev_warn(rdev, "No configuration\n");
1016 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1017 rdev->desc->ops->set_suspend_enable)
1018 ret = rdev->desc->ops->set_suspend_enable(rdev);
1019 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1020 rdev->desc->ops->set_suspend_disable)
1021 ret = rdev->desc->ops->set_suspend_disable(rdev);
1022 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1026 rdev_err(rdev, "failed to enabled/disable\n");
1030 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1031 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1033 rdev_err(rdev, "failed to set voltage\n");
1038 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1039 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1041 rdev_err(rdev, "failed to set mode\n");
1049 static void print_constraints(struct regulator_dev *rdev)
1051 struct regulation_constraints *constraints = rdev->constraints;
1053 size_t len = sizeof(buf) - 1;
1057 if (constraints->min_uV && constraints->max_uV) {
1058 if (constraints->min_uV == constraints->max_uV)
1059 count += scnprintf(buf + count, len - count, "%d mV ",
1060 constraints->min_uV / 1000);
1062 count += scnprintf(buf + count, len - count,
1064 constraints->min_uV / 1000,
1065 constraints->max_uV / 1000);
1068 if (!constraints->min_uV ||
1069 constraints->min_uV != constraints->max_uV) {
1070 ret = _regulator_get_voltage(rdev);
1072 count += scnprintf(buf + count, len - count,
1073 "at %d mV ", ret / 1000);
1076 if (constraints->uV_offset)
1077 count += scnprintf(buf + count, len - count, "%dmV offset ",
1078 constraints->uV_offset / 1000);
1080 if (constraints->min_uA && constraints->max_uA) {
1081 if (constraints->min_uA == constraints->max_uA)
1082 count += scnprintf(buf + count, len - count, "%d mA ",
1083 constraints->min_uA / 1000);
1085 count += scnprintf(buf + count, len - count,
1087 constraints->min_uA / 1000,
1088 constraints->max_uA / 1000);
1091 if (!constraints->min_uA ||
1092 constraints->min_uA != constraints->max_uA) {
1093 ret = _regulator_get_current_limit(rdev);
1095 count += scnprintf(buf + count, len - count,
1096 "at %d mA ", ret / 1000);
1099 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1100 count += scnprintf(buf + count, len - count, "fast ");
1101 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1102 count += scnprintf(buf + count, len - count, "normal ");
1103 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1104 count += scnprintf(buf + count, len - count, "idle ");
1105 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1106 count += scnprintf(buf + count, len - count, "standby");
1109 scnprintf(buf, len, "no parameters");
1111 rdev_dbg(rdev, "%s\n", buf);
1113 if ((constraints->min_uV != constraints->max_uV) &&
1114 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1116 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1119 static int machine_constraints_voltage(struct regulator_dev *rdev,
1120 struct regulation_constraints *constraints)
1122 const struct regulator_ops *ops = rdev->desc->ops;
1125 /* do we need to apply the constraint voltage */
1126 if (rdev->constraints->apply_uV &&
1127 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1128 int target_min, target_max;
1129 int current_uV = _regulator_get_voltage(rdev);
1131 if (current_uV == -ENOTRECOVERABLE) {
1132 /* This regulator can't be read and must be initted */
1133 rdev_info(rdev, "Setting %d-%duV\n",
1134 rdev->constraints->min_uV,
1135 rdev->constraints->max_uV);
1136 _regulator_do_set_voltage(rdev,
1137 rdev->constraints->min_uV,
1138 rdev->constraints->max_uV);
1139 current_uV = _regulator_get_voltage(rdev);
1142 if (current_uV < 0) {
1144 "failed to get the current voltage(%d)\n",
1150 * If we're below the minimum voltage move up to the
1151 * minimum voltage, if we're above the maximum voltage
1152 * then move down to the maximum.
1154 target_min = current_uV;
1155 target_max = current_uV;
1157 if (current_uV < rdev->constraints->min_uV) {
1158 target_min = rdev->constraints->min_uV;
1159 target_max = rdev->constraints->min_uV;
1162 if (current_uV > rdev->constraints->max_uV) {
1163 target_min = rdev->constraints->max_uV;
1164 target_max = rdev->constraints->max_uV;
1167 if (target_min != current_uV || target_max != current_uV) {
1168 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1169 current_uV, target_min, target_max);
1170 ret = _regulator_do_set_voltage(
1171 rdev, target_min, target_max);
1174 "failed to apply %d-%duV constraint(%d)\n",
1175 target_min, target_max, ret);
1181 /* constrain machine-level voltage specs to fit
1182 * the actual range supported by this regulator.
1184 if (ops->list_voltage && rdev->desc->n_voltages) {
1185 int count = rdev->desc->n_voltages;
1187 int min_uV = INT_MAX;
1188 int max_uV = INT_MIN;
1189 int cmin = constraints->min_uV;
1190 int cmax = constraints->max_uV;
1192 /* it's safe to autoconfigure fixed-voltage supplies
1193 and the constraints are used by list_voltage. */
1194 if (count == 1 && !cmin) {
1197 constraints->min_uV = cmin;
1198 constraints->max_uV = cmax;
1201 /* voltage constraints are optional */
1202 if ((cmin == 0) && (cmax == 0))
1205 /* else require explicit machine-level constraints */
1206 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1207 rdev_err(rdev, "invalid voltage constraints\n");
1211 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1212 for (i = 0; i < count; i++) {
1215 value = ops->list_voltage(rdev, i);
1219 /* maybe adjust [min_uV..max_uV] */
1220 if (value >= cmin && value < min_uV)
1222 if (value <= cmax && value > max_uV)
1226 /* final: [min_uV..max_uV] valid iff constraints valid */
1227 if (max_uV < min_uV) {
1229 "unsupportable voltage constraints %u-%uuV\n",
1234 /* use regulator's subset of machine constraints */
1235 if (constraints->min_uV < min_uV) {
1236 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1237 constraints->min_uV, min_uV);
1238 constraints->min_uV = min_uV;
1240 if (constraints->max_uV > max_uV) {
1241 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1242 constraints->max_uV, max_uV);
1243 constraints->max_uV = max_uV;
1250 static int machine_constraints_current(struct regulator_dev *rdev,
1251 struct regulation_constraints *constraints)
1253 const struct regulator_ops *ops = rdev->desc->ops;
1256 if (!constraints->min_uA && !constraints->max_uA)
1259 if (constraints->min_uA > constraints->max_uA) {
1260 rdev_err(rdev, "Invalid current constraints\n");
1264 if (!ops->set_current_limit || !ops->get_current_limit) {
1265 rdev_warn(rdev, "Operation of current configuration missing\n");
1269 /* Set regulator current in constraints range */
1270 ret = ops->set_current_limit(rdev, constraints->min_uA,
1271 constraints->max_uA);
1273 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1280 static int _regulator_do_enable(struct regulator_dev *rdev);
1283 * set_machine_constraints - sets regulator constraints
1284 * @rdev: regulator source
1285 * @constraints: constraints to apply
1287 * Allows platform initialisation code to define and constrain
1288 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1289 * Constraints *must* be set by platform code in order for some
1290 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1293 static int set_machine_constraints(struct regulator_dev *rdev,
1294 const struct regulation_constraints *constraints)
1297 const struct regulator_ops *ops = rdev->desc->ops;
1300 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1303 rdev->constraints = kzalloc(sizeof(*constraints),
1305 if (!rdev->constraints)
1308 ret = machine_constraints_voltage(rdev, rdev->constraints);
1312 ret = machine_constraints_current(rdev, rdev->constraints);
1316 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1317 ret = ops->set_input_current_limit(rdev,
1318 rdev->constraints->ilim_uA);
1320 rdev_err(rdev, "failed to set input limit\n");
1325 /* do we need to setup our suspend state */
1326 if (rdev->constraints->initial_state) {
1327 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1329 rdev_err(rdev, "failed to set suspend state\n");
1334 if (rdev->constraints->initial_mode) {
1335 if (!ops->set_mode) {
1336 rdev_err(rdev, "no set_mode operation\n");
1340 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1342 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1345 } else if (rdev->constraints->system_load) {
1347 * We'll only apply the initial system load if an
1348 * initial mode wasn't specified.
1350 drms_uA_update(rdev);
1353 /* If the constraints say the regulator should be on at this point
1354 * and we have control then make sure it is enabled.
1356 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1357 ret = _regulator_do_enable(rdev);
1358 if (ret < 0 && ret != -EINVAL) {
1359 rdev_err(rdev, "failed to enable\n");
1364 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1365 && ops->set_ramp_delay) {
1366 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1368 rdev_err(rdev, "failed to set ramp_delay\n");
1373 if (rdev->constraints->pull_down && ops->set_pull_down) {
1374 ret = ops->set_pull_down(rdev);
1376 rdev_err(rdev, "failed to set pull down\n");
1381 if (rdev->constraints->soft_start && ops->set_soft_start) {
1382 ret = ops->set_soft_start(rdev);
1384 rdev_err(rdev, "failed to set soft start\n");
1389 if (rdev->constraints->over_current_protection
1390 && ops->set_over_current_protection) {
1391 ret = ops->set_over_current_protection(rdev);
1393 rdev_err(rdev, "failed to set over current protection\n");
1398 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1399 bool ad_state = (rdev->constraints->active_discharge ==
1400 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1402 ret = ops->set_active_discharge(rdev, ad_state);
1404 rdev_err(rdev, "failed to set active discharge\n");
1409 print_constraints(rdev);
1414 * set_supply - set regulator supply regulator
1415 * @rdev: regulator name
1416 * @supply_rdev: supply regulator name
1418 * Called by platform initialisation code to set the supply regulator for this
1419 * regulator. This ensures that a regulators supply will also be enabled by the
1420 * core if it's child is enabled.
1422 static int set_supply(struct regulator_dev *rdev,
1423 struct regulator_dev *supply_rdev)
1427 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1429 if (!try_module_get(supply_rdev->owner))
1432 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1433 if (rdev->supply == NULL) {
1437 supply_rdev->open_count++;
1443 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1444 * @rdev: regulator source
1445 * @consumer_dev_name: dev_name() string for device supply applies to
1446 * @supply: symbolic name for supply
1448 * Allows platform initialisation code to map physical regulator
1449 * sources to symbolic names for supplies for use by devices. Devices
1450 * should use these symbolic names to request regulators, avoiding the
1451 * need to provide board-specific regulator names as platform data.
1453 static int set_consumer_device_supply(struct regulator_dev *rdev,
1454 const char *consumer_dev_name,
1457 struct regulator_map *node;
1463 if (consumer_dev_name != NULL)
1468 list_for_each_entry(node, ®ulator_map_list, list) {
1469 if (node->dev_name && consumer_dev_name) {
1470 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1472 } else if (node->dev_name || consumer_dev_name) {
1476 if (strcmp(node->supply, supply) != 0)
1479 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1481 dev_name(&node->regulator->dev),
1482 node->regulator->desc->name,
1484 dev_name(&rdev->dev), rdev_get_name(rdev));
1488 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1492 node->regulator = rdev;
1493 node->supply = supply;
1496 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1497 if (node->dev_name == NULL) {
1503 list_add(&node->list, ®ulator_map_list);
1507 static void unset_regulator_supplies(struct regulator_dev *rdev)
1509 struct regulator_map *node, *n;
1511 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1512 if (rdev == node->regulator) {
1513 list_del(&node->list);
1514 kfree(node->dev_name);
1520 #ifdef CONFIG_DEBUG_FS
1521 static ssize_t constraint_flags_read_file(struct file *file,
1522 char __user *user_buf,
1523 size_t count, loff_t *ppos)
1525 const struct regulator *regulator = file->private_data;
1526 const struct regulation_constraints *c = regulator->rdev->constraints;
1533 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1537 ret = snprintf(buf, PAGE_SIZE,
1541 "ramp_disable: %u\n"
1544 "over_current_protection: %u\n",
1551 c->over_current_protection);
1553 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1561 static const struct file_operations constraint_flags_fops = {
1562 #ifdef CONFIG_DEBUG_FS
1563 .open = simple_open,
1564 .read = constraint_flags_read_file,
1565 .llseek = default_llseek,
1569 #define REG_STR_SIZE 64
1571 static struct regulator *create_regulator(struct regulator_dev *rdev,
1573 const char *supply_name)
1575 struct regulator *regulator;
1576 char buf[REG_STR_SIZE];
1579 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1580 if (regulator == NULL)
1583 regulator_lock(rdev);
1584 regulator->rdev = rdev;
1585 list_add(®ulator->list, &rdev->consumer_list);
1588 regulator->dev = dev;
1590 /* Add a link to the device sysfs entry */
1591 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1592 dev->kobj.name, supply_name);
1593 if (size >= REG_STR_SIZE)
1596 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1597 if (regulator->supply_name == NULL)
1600 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1603 rdev_dbg(rdev, "could not add device link %s err %d\n",
1604 dev->kobj.name, err);
1608 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1609 if (regulator->supply_name == NULL)
1613 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1615 if (!regulator->debugfs) {
1616 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1618 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1619 ®ulator->uA_load);
1620 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1621 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1622 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1623 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1624 debugfs_create_file("constraint_flags", 0444,
1625 regulator->debugfs, regulator,
1626 &constraint_flags_fops);
1630 * Check now if the regulator is an always on regulator - if
1631 * it is then we don't need to do nearly so much work for
1632 * enable/disable calls.
1634 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1635 _regulator_is_enabled(rdev))
1636 regulator->always_on = true;
1638 regulator_unlock(rdev);
1641 list_del(®ulator->list);
1643 regulator_unlock(rdev);
1647 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1649 if (rdev->constraints && rdev->constraints->enable_time)
1650 return rdev->constraints->enable_time;
1651 if (!rdev->desc->ops->enable_time)
1652 return rdev->desc->enable_time;
1653 return rdev->desc->ops->enable_time(rdev);
1656 static struct regulator_supply_alias *regulator_find_supply_alias(
1657 struct device *dev, const char *supply)
1659 struct regulator_supply_alias *map;
1661 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1662 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1668 static void regulator_supply_alias(struct device **dev, const char **supply)
1670 struct regulator_supply_alias *map;
1672 map = regulator_find_supply_alias(*dev, *supply);
1674 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1675 *supply, map->alias_supply,
1676 dev_name(map->alias_dev));
1677 *dev = map->alias_dev;
1678 *supply = map->alias_supply;
1682 static int regulator_match(struct device *dev, const void *data)
1684 struct regulator_dev *r = dev_to_rdev(dev);
1686 return strcmp(rdev_get_name(r), data) == 0;
1689 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1693 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1695 return dev ? dev_to_rdev(dev) : NULL;
1699 * regulator_dev_lookup - lookup a regulator device.
1700 * @dev: device for regulator "consumer".
1701 * @supply: Supply name or regulator ID.
1703 * If successful, returns a struct regulator_dev that corresponds to the name
1704 * @supply and with the embedded struct device refcount incremented by one.
1705 * The refcount must be dropped by calling put_device().
1706 * On failure one of the following ERR-PTR-encoded values is returned:
1707 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1710 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1713 struct regulator_dev *r = NULL;
1714 struct device_node *node;
1715 struct regulator_map *map;
1716 const char *devname = NULL;
1718 regulator_supply_alias(&dev, &supply);
1720 /* first do a dt based lookup */
1721 if (dev && dev->of_node) {
1722 node = of_get_regulator(dev, supply);
1724 r = of_find_regulator_by_node(node);
1729 * We have a node, but there is no device.
1730 * assume it has not registered yet.
1732 return ERR_PTR(-EPROBE_DEFER);
1736 /* if not found, try doing it non-dt way */
1738 devname = dev_name(dev);
1740 mutex_lock(®ulator_list_mutex);
1741 list_for_each_entry(map, ®ulator_map_list, list) {
1742 /* If the mapping has a device set up it must match */
1743 if (map->dev_name &&
1744 (!devname || strcmp(map->dev_name, devname)))
1747 if (strcmp(map->supply, supply) == 0 &&
1748 get_device(&map->regulator->dev)) {
1753 mutex_unlock(®ulator_list_mutex);
1758 r = regulator_lookup_by_name(supply);
1762 return ERR_PTR(-ENODEV);
1765 static int regulator_resolve_supply(struct regulator_dev *rdev)
1767 struct regulator_dev *r;
1768 struct device *dev = rdev->dev.parent;
1771 /* No supply to resovle? */
1772 if (!rdev->supply_name)
1775 /* Supply already resolved? */
1779 r = regulator_dev_lookup(dev, rdev->supply_name);
1783 /* Did the lookup explicitly defer for us? */
1784 if (ret == -EPROBE_DEFER)
1787 if (have_full_constraints()) {
1788 r = dummy_regulator_rdev;
1789 get_device(&r->dev);
1791 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1792 rdev->supply_name, rdev->desc->name);
1793 return -EPROBE_DEFER;
1798 * If the supply's parent device is not the same as the
1799 * regulator's parent device, then ensure the parent device
1800 * is bound before we resolve the supply, in case the parent
1801 * device get probe deferred and unregisters the supply.
1803 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1804 if (!device_is_bound(r->dev.parent)) {
1805 put_device(&r->dev);
1806 return -EPROBE_DEFER;
1810 /* Recursively resolve the supply of the supply */
1811 ret = regulator_resolve_supply(r);
1813 put_device(&r->dev);
1817 ret = set_supply(rdev, r);
1819 put_device(&r->dev);
1823 /* Cascade always-on state to supply */
1824 if (_regulator_is_enabled(rdev)) {
1825 ret = regulator_enable(rdev->supply);
1827 _regulator_put(rdev->supply);
1828 rdev->supply = NULL;
1831 rdev->use_count = 1;
1837 /* Internal regulator request function */
1838 struct regulator *_regulator_get(struct device *dev, const char *id,
1839 enum regulator_get_type get_type)
1841 struct regulator_dev *rdev;
1842 struct regulator *regulator;
1843 const char *devname = dev ? dev_name(dev) : "deviceless";
1846 if (get_type >= MAX_GET_TYPE) {
1847 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1848 return ERR_PTR(-EINVAL);
1852 pr_err("get() with no identifier\n");
1853 return ERR_PTR(-EINVAL);
1856 rdev = regulator_dev_lookup(dev, id);
1858 ret = PTR_ERR(rdev);
1861 * If regulator_dev_lookup() fails with error other
1862 * than -ENODEV our job here is done, we simply return it.
1865 return ERR_PTR(ret);
1867 if (!have_full_constraints()) {
1869 "incomplete constraints, dummy supplies not allowed\n");
1870 return ERR_PTR(-ENODEV);
1876 * Assume that a regulator is physically present and
1877 * enabled, even if it isn't hooked up, and just
1881 "%s supply %s not found, using dummy regulator\n",
1883 rdev = dummy_regulator_rdev;
1884 get_device(&rdev->dev);
1889 "dummy supplies not allowed for exclusive requests\n");
1893 return ERR_PTR(-ENODEV);
1897 if (rdev->exclusive) {
1898 regulator = ERR_PTR(-EPERM);
1899 put_device(&rdev->dev);
1903 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1904 regulator = ERR_PTR(-EBUSY);
1905 put_device(&rdev->dev);
1909 mutex_lock(®ulator_list_mutex);
1910 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1911 mutex_unlock(®ulator_list_mutex);
1914 regulator = ERR_PTR(-EPROBE_DEFER);
1915 put_device(&rdev->dev);
1919 ret = regulator_resolve_supply(rdev);
1921 regulator = ERR_PTR(ret);
1922 put_device(&rdev->dev);
1926 if (!try_module_get(rdev->owner)) {
1927 regulator = ERR_PTR(-EPROBE_DEFER);
1928 put_device(&rdev->dev);
1932 regulator = create_regulator(rdev, dev, id);
1933 if (regulator == NULL) {
1934 regulator = ERR_PTR(-ENOMEM);
1935 put_device(&rdev->dev);
1936 module_put(rdev->owner);
1941 if (get_type == EXCLUSIVE_GET) {
1942 rdev->exclusive = 1;
1944 ret = _regulator_is_enabled(rdev);
1946 rdev->use_count = 1;
1948 rdev->use_count = 0;
1951 device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1957 * regulator_get - lookup and obtain a reference to a regulator.
1958 * @dev: device for regulator "consumer"
1959 * @id: Supply name or regulator ID.
1961 * Returns a struct regulator corresponding to the regulator producer,
1962 * or IS_ERR() condition containing errno.
1964 * Use of supply names configured via regulator_set_device_supply() is
1965 * strongly encouraged. It is recommended that the supply name used
1966 * should match the name used for the supply and/or the relevant
1967 * device pins in the datasheet.
1969 struct regulator *regulator_get(struct device *dev, const char *id)
1971 return _regulator_get(dev, id, NORMAL_GET);
1973 EXPORT_SYMBOL_GPL(regulator_get);
1976 * regulator_get_exclusive - obtain exclusive access to a regulator.
1977 * @dev: device for regulator "consumer"
1978 * @id: Supply name or regulator ID.
1980 * Returns a struct regulator corresponding to the regulator producer,
1981 * or IS_ERR() condition containing errno. Other consumers will be
1982 * unable to obtain this regulator while this reference is held and the
1983 * use count for the regulator will be initialised to reflect the current
1984 * state of the regulator.
1986 * This is intended for use by consumers which cannot tolerate shared
1987 * use of the regulator such as those which need to force the
1988 * regulator off for correct operation of the hardware they are
1991 * Use of supply names configured via regulator_set_device_supply() is
1992 * strongly encouraged. It is recommended that the supply name used
1993 * should match the name used for the supply and/or the relevant
1994 * device pins in the datasheet.
1996 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1998 return _regulator_get(dev, id, EXCLUSIVE_GET);
2000 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2003 * regulator_get_optional - obtain optional access to a regulator.
2004 * @dev: device for regulator "consumer"
2005 * @id: Supply name or regulator ID.
2007 * Returns a struct regulator corresponding to the regulator producer,
2008 * or IS_ERR() condition containing errno.
2010 * This is intended for use by consumers for devices which can have
2011 * some supplies unconnected in normal use, such as some MMC devices.
2012 * It can allow the regulator core to provide stub supplies for other
2013 * supplies requested using normal regulator_get() calls without
2014 * disrupting the operation of drivers that can handle absent
2017 * Use of supply names configured via regulator_set_device_supply() is
2018 * strongly encouraged. It is recommended that the supply name used
2019 * should match the name used for the supply and/or the relevant
2020 * device pins in the datasheet.
2022 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2024 return _regulator_get(dev, id, OPTIONAL_GET);
2026 EXPORT_SYMBOL_GPL(regulator_get_optional);
2028 /* regulator_list_mutex lock held by regulator_put() */
2029 static void _regulator_put(struct regulator *regulator)
2031 struct regulator_dev *rdev;
2033 if (IS_ERR_OR_NULL(regulator))
2036 lockdep_assert_held_once(®ulator_list_mutex);
2038 /* Docs say you must disable before calling regulator_put() */
2039 WARN_ON(regulator->enable_count);
2041 rdev = regulator->rdev;
2043 debugfs_remove_recursive(regulator->debugfs);
2045 if (regulator->dev) {
2047 struct regulator *r;
2049 list_for_each_entry(r, &rdev->consumer_list, list)
2050 if (r->dev == regulator->dev)
2054 device_link_remove(regulator->dev, &rdev->dev);
2056 /* remove any sysfs entries */
2057 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2060 regulator_lock(rdev);
2061 list_del(®ulator->list);
2064 rdev->exclusive = 0;
2065 put_device(&rdev->dev);
2066 regulator_unlock(rdev);
2068 kfree_const(regulator->supply_name);
2071 module_put(rdev->owner);
2075 * regulator_put - "free" the regulator source
2076 * @regulator: regulator source
2078 * Note: drivers must ensure that all regulator_enable calls made on this
2079 * regulator source are balanced by regulator_disable calls prior to calling
2082 void regulator_put(struct regulator *regulator)
2084 mutex_lock(®ulator_list_mutex);
2085 _regulator_put(regulator);
2086 mutex_unlock(®ulator_list_mutex);
2088 EXPORT_SYMBOL_GPL(regulator_put);
2091 * regulator_register_supply_alias - Provide device alias for supply lookup
2093 * @dev: device that will be given as the regulator "consumer"
2094 * @id: Supply name or regulator ID
2095 * @alias_dev: device that should be used to lookup the supply
2096 * @alias_id: Supply name or regulator ID that should be used to lookup the
2099 * All lookups for id on dev will instead be conducted for alias_id on
2102 int regulator_register_supply_alias(struct device *dev, const char *id,
2103 struct device *alias_dev,
2104 const char *alias_id)
2106 struct regulator_supply_alias *map;
2108 map = regulator_find_supply_alias(dev, id);
2112 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2117 map->src_supply = id;
2118 map->alias_dev = alias_dev;
2119 map->alias_supply = alias_id;
2121 list_add(&map->list, ®ulator_supply_alias_list);
2123 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2124 id, dev_name(dev), alias_id, dev_name(alias_dev));
2128 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2131 * regulator_unregister_supply_alias - Remove device alias
2133 * @dev: device that will be given as the regulator "consumer"
2134 * @id: Supply name or regulator ID
2136 * Remove a lookup alias if one exists for id on dev.
2138 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2140 struct regulator_supply_alias *map;
2142 map = regulator_find_supply_alias(dev, id);
2144 list_del(&map->list);
2148 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2151 * regulator_bulk_register_supply_alias - register multiple aliases
2153 * @dev: device that will be given as the regulator "consumer"
2154 * @id: List of supply names or regulator IDs
2155 * @alias_dev: device that should be used to lookup the supply
2156 * @alias_id: List of supply names or regulator IDs that should be used to
2158 * @num_id: Number of aliases to register
2160 * @return 0 on success, an errno on failure.
2162 * This helper function allows drivers to register several supply
2163 * aliases in one operation. If any of the aliases cannot be
2164 * registered any aliases that were registered will be removed
2165 * before returning to the caller.
2167 int regulator_bulk_register_supply_alias(struct device *dev,
2168 const char *const *id,
2169 struct device *alias_dev,
2170 const char *const *alias_id,
2176 for (i = 0; i < num_id; ++i) {
2177 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2187 "Failed to create supply alias %s,%s -> %s,%s\n",
2188 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2191 regulator_unregister_supply_alias(dev, id[i]);
2195 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2198 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2200 * @dev: device that will be given as the regulator "consumer"
2201 * @id: List of supply names or regulator IDs
2202 * @num_id: Number of aliases to unregister
2204 * This helper function allows drivers to unregister several supply
2205 * aliases in one operation.
2207 void regulator_bulk_unregister_supply_alias(struct device *dev,
2208 const char *const *id,
2213 for (i = 0; i < num_id; ++i)
2214 regulator_unregister_supply_alias(dev, id[i]);
2216 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2219 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2220 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2221 const struct regulator_config *config)
2223 struct regulator_enable_gpio *pin;
2224 struct gpio_desc *gpiod;
2227 if (config->ena_gpiod)
2228 gpiod = config->ena_gpiod;
2230 gpiod = gpio_to_desc(config->ena_gpio);
2232 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2233 if (pin->gpiod == gpiod) {
2234 rdev_dbg(rdev, "GPIO %d is already used\n",
2236 goto update_ena_gpio_to_rdev;
2240 if (!config->ena_gpiod) {
2241 ret = gpio_request_one(config->ena_gpio,
2242 GPIOF_DIR_OUT | config->ena_gpio_flags,
2243 rdev_get_name(rdev));
2248 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2250 if (!config->ena_gpiod)
2251 gpio_free(config->ena_gpio);
2256 pin->ena_gpio_invert = config->ena_gpio_invert;
2257 list_add(&pin->list, ®ulator_ena_gpio_list);
2259 update_ena_gpio_to_rdev:
2260 pin->request_count++;
2261 rdev->ena_pin = pin;
2265 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2267 struct regulator_enable_gpio *pin, *n;
2272 /* Free the GPIO only in case of no use */
2273 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2274 if (pin->gpiod == rdev->ena_pin->gpiod) {
2275 if (pin->request_count <= 1) {
2276 pin->request_count = 0;
2277 gpiod_put(pin->gpiod);
2278 list_del(&pin->list);
2280 rdev->ena_pin = NULL;
2283 pin->request_count--;
2290 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2291 * @rdev: regulator_dev structure
2292 * @enable: enable GPIO at initial use?
2294 * GPIO is enabled in case of initial use. (enable_count is 0)
2295 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2297 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2299 struct regulator_enable_gpio *pin = rdev->ena_pin;
2305 /* Enable GPIO at initial use */
2306 if (pin->enable_count == 0)
2307 gpiod_set_value_cansleep(pin->gpiod,
2308 !pin->ena_gpio_invert);
2310 pin->enable_count++;
2312 if (pin->enable_count > 1) {
2313 pin->enable_count--;
2317 /* Disable GPIO if not used */
2318 if (pin->enable_count <= 1) {
2319 gpiod_set_value_cansleep(pin->gpiod,
2320 pin->ena_gpio_invert);
2321 pin->enable_count = 0;
2329 * _regulator_enable_delay - a delay helper function
2330 * @delay: time to delay in microseconds
2332 * Delay for the requested amount of time as per the guidelines in:
2334 * Documentation/timers/timers-howto.txt
2336 * The assumption here is that regulators will never be enabled in
2337 * atomic context and therefore sleeping functions can be used.
2339 static void _regulator_enable_delay(unsigned int delay)
2341 unsigned int ms = delay / 1000;
2342 unsigned int us = delay % 1000;
2346 * For small enough values, handle super-millisecond
2347 * delays in the usleep_range() call below.
2356 * Give the scheduler some room to coalesce with any other
2357 * wakeup sources. For delays shorter than 10 us, don't even
2358 * bother setting up high-resolution timers and just busy-
2362 usleep_range(us, us + 100);
2367 static int _regulator_do_enable(struct regulator_dev *rdev)
2371 /* Query before enabling in case configuration dependent. */
2372 ret = _regulator_get_enable_time(rdev);
2376 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2380 trace_regulator_enable(rdev_get_name(rdev));
2382 if (rdev->desc->off_on_delay) {
2383 /* if needed, keep a distance of off_on_delay from last time
2384 * this regulator was disabled.
2386 unsigned long start_jiffy = jiffies;
2387 unsigned long intended, max_delay, remaining;
2389 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2390 intended = rdev->last_off_jiffy + max_delay;
2392 if (time_before(start_jiffy, intended)) {
2393 /* calc remaining jiffies to deal with one-time
2395 * in case of multiple timer wrapping, either it can be
2396 * detected by out-of-range remaining, or it cannot be
2397 * detected and we gets a panelty of
2398 * _regulator_enable_delay().
2400 remaining = intended - start_jiffy;
2401 if (remaining <= max_delay)
2402 _regulator_enable_delay(
2403 jiffies_to_usecs(remaining));
2407 if (rdev->ena_pin) {
2408 if (!rdev->ena_gpio_state) {
2409 ret = regulator_ena_gpio_ctrl(rdev, true);
2412 rdev->ena_gpio_state = 1;
2414 } else if (rdev->desc->ops->enable) {
2415 ret = rdev->desc->ops->enable(rdev);
2422 /* Allow the regulator to ramp; it would be useful to extend
2423 * this for bulk operations so that the regulators can ramp
2425 trace_regulator_enable_delay(rdev_get_name(rdev));
2427 _regulator_enable_delay(delay);
2429 trace_regulator_enable_complete(rdev_get_name(rdev));
2435 * _regulator_handle_consumer_enable - handle that a consumer enabled
2436 * @regulator: regulator source
2438 * Some things on a regulator consumer (like the contribution towards total
2439 * load on the regulator) only have an effect when the consumer wants the
2440 * regulator enabled. Explained in example with two consumers of the same
2442 * consumer A: set_load(100); => total load = 0
2443 * consumer A: regulator_enable(); => total load = 100
2444 * consumer B: set_load(1000); => total load = 100
2445 * consumer B: regulator_enable(); => total load = 1100
2446 * consumer A: regulator_disable(); => total_load = 1000
2448 * This function (together with _regulator_handle_consumer_disable) is
2449 * responsible for keeping track of the refcount for a given regulator consumer
2450 * and applying / unapplying these things.
2452 * Returns 0 upon no error; -error upon error.
2454 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2456 struct regulator_dev *rdev = regulator->rdev;
2458 lockdep_assert_held_once(&rdev->mutex.base);
2460 regulator->enable_count++;
2461 if (regulator->uA_load && regulator->enable_count == 1)
2462 return drms_uA_update(rdev);
2468 * _regulator_handle_consumer_disable - handle that a consumer disabled
2469 * @regulator: regulator source
2471 * The opposite of _regulator_handle_consumer_enable().
2473 * Returns 0 upon no error; -error upon error.
2475 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2477 struct regulator_dev *rdev = regulator->rdev;
2479 lockdep_assert_held_once(&rdev->mutex.base);
2481 if (!regulator->enable_count) {
2482 rdev_err(rdev, "Underflow of regulator enable count\n");
2486 regulator->enable_count--;
2487 if (regulator->uA_load && regulator->enable_count == 0)
2488 return drms_uA_update(rdev);
2493 /* locks held by regulator_enable() */
2494 static int _regulator_enable(struct regulator *regulator)
2496 struct regulator_dev *rdev = regulator->rdev;
2499 lockdep_assert_held_once(&rdev->mutex.base);
2501 if (rdev->use_count == 0 && rdev->supply) {
2502 ret = _regulator_enable(rdev->supply);
2507 /* balance only if there are regulators coupled */
2508 if (rdev->coupling_desc.n_coupled > 1) {
2509 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2511 goto err_disable_supply;
2514 ret = _regulator_handle_consumer_enable(regulator);
2516 goto err_disable_supply;
2518 if (rdev->use_count == 0) {
2519 /* The regulator may on if it's not switchable or left on */
2520 ret = _regulator_is_enabled(rdev);
2521 if (ret == -EINVAL || ret == 0) {
2522 if (!regulator_ops_is_valid(rdev,
2523 REGULATOR_CHANGE_STATUS)) {
2525 goto err_consumer_disable;
2528 ret = _regulator_do_enable(rdev);
2530 goto err_consumer_disable;
2532 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2534 } else if (ret < 0) {
2535 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2536 goto err_consumer_disable;
2538 /* Fallthrough on positive return values - already enabled */
2545 err_consumer_disable:
2546 _regulator_handle_consumer_disable(regulator);
2549 if (rdev->use_count == 0 && rdev->supply)
2550 _regulator_disable(rdev->supply);
2556 * regulator_enable - enable regulator output
2557 * @regulator: regulator source
2559 * Request that the regulator be enabled with the regulator output at
2560 * the predefined voltage or current value. Calls to regulator_enable()
2561 * must be balanced with calls to regulator_disable().
2563 * NOTE: the output value can be set by other drivers, boot loader or may be
2564 * hardwired in the regulator.
2566 int regulator_enable(struct regulator *regulator)
2568 struct regulator_dev *rdev = regulator->rdev;
2569 struct ww_acquire_ctx ww_ctx;
2572 regulator_lock_dependent(rdev, &ww_ctx);
2573 ret = _regulator_enable(regulator);
2574 regulator_unlock_dependent(rdev, &ww_ctx);
2578 EXPORT_SYMBOL_GPL(regulator_enable);
2580 static int _regulator_do_disable(struct regulator_dev *rdev)
2584 trace_regulator_disable(rdev_get_name(rdev));
2586 if (rdev->ena_pin) {
2587 if (rdev->ena_gpio_state) {
2588 ret = regulator_ena_gpio_ctrl(rdev, false);
2591 rdev->ena_gpio_state = 0;
2594 } else if (rdev->desc->ops->disable) {
2595 ret = rdev->desc->ops->disable(rdev);
2600 /* cares about last_off_jiffy only if off_on_delay is required by
2603 if (rdev->desc->off_on_delay)
2604 rdev->last_off_jiffy = jiffies;
2606 trace_regulator_disable_complete(rdev_get_name(rdev));
2611 /* locks held by regulator_disable() */
2612 static int _regulator_disable(struct regulator *regulator)
2614 struct regulator_dev *rdev = regulator->rdev;
2617 lockdep_assert_held_once(&rdev->mutex.base);
2619 if (WARN(rdev->use_count <= 0,
2620 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2623 /* are we the last user and permitted to disable ? */
2624 if (rdev->use_count == 1 &&
2625 (rdev->constraints && !rdev->constraints->always_on)) {
2627 /* we are last user */
2628 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2629 ret = _notifier_call_chain(rdev,
2630 REGULATOR_EVENT_PRE_DISABLE,
2632 if (ret & NOTIFY_STOP_MASK)
2635 ret = _regulator_do_disable(rdev);
2637 rdev_err(rdev, "failed to disable\n");
2638 _notifier_call_chain(rdev,
2639 REGULATOR_EVENT_ABORT_DISABLE,
2643 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2647 rdev->use_count = 0;
2648 } else if (rdev->use_count > 1) {
2653 ret = _regulator_handle_consumer_disable(regulator);
2655 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2656 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2658 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2659 ret = _regulator_disable(rdev->supply);
2665 * regulator_disable - disable regulator output
2666 * @regulator: regulator source
2668 * Disable the regulator output voltage or current. Calls to
2669 * regulator_enable() must be balanced with calls to
2670 * regulator_disable().
2672 * NOTE: this will only disable the regulator output if no other consumer
2673 * devices have it enabled, the regulator device supports disabling and
2674 * machine constraints permit this operation.
2676 int regulator_disable(struct regulator *regulator)
2678 struct regulator_dev *rdev = regulator->rdev;
2679 struct ww_acquire_ctx ww_ctx;
2682 regulator_lock_dependent(rdev, &ww_ctx);
2683 ret = _regulator_disable(regulator);
2684 regulator_unlock_dependent(rdev, &ww_ctx);
2688 EXPORT_SYMBOL_GPL(regulator_disable);
2690 /* locks held by regulator_force_disable() */
2691 static int _regulator_force_disable(struct regulator_dev *rdev)
2695 lockdep_assert_held_once(&rdev->mutex.base);
2697 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2698 REGULATOR_EVENT_PRE_DISABLE, NULL);
2699 if (ret & NOTIFY_STOP_MASK)
2702 ret = _regulator_do_disable(rdev);
2704 rdev_err(rdev, "failed to force disable\n");
2705 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2706 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2710 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2711 REGULATOR_EVENT_DISABLE, NULL);
2717 * regulator_force_disable - force disable regulator output
2718 * @regulator: regulator source
2720 * Forcibly disable the regulator output voltage or current.
2721 * NOTE: this *will* disable the regulator output even if other consumer
2722 * devices have it enabled. This should be used for situations when device
2723 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2725 int regulator_force_disable(struct regulator *regulator)
2727 struct regulator_dev *rdev = regulator->rdev;
2728 struct ww_acquire_ctx ww_ctx;
2731 regulator_lock_dependent(rdev, &ww_ctx);
2733 ret = _regulator_force_disable(regulator->rdev);
2735 if (rdev->coupling_desc.n_coupled > 1)
2736 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2738 if (regulator->uA_load) {
2739 regulator->uA_load = 0;
2740 ret = drms_uA_update(rdev);
2743 if (rdev->use_count != 0 && rdev->supply)
2744 _regulator_disable(rdev->supply);
2746 regulator_unlock_dependent(rdev, &ww_ctx);
2750 EXPORT_SYMBOL_GPL(regulator_force_disable);
2752 static void regulator_disable_work(struct work_struct *work)
2754 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2756 struct ww_acquire_ctx ww_ctx;
2758 struct regulator *regulator;
2759 int total_count = 0;
2761 regulator_lock_dependent(rdev, &ww_ctx);
2764 * Workqueue functions queue the new work instance while the previous
2765 * work instance is being processed. Cancel the queued work instance
2766 * as the work instance under processing does the job of the queued
2769 cancel_delayed_work(&rdev->disable_work);
2771 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2772 count = regulator->deferred_disables;
2777 total_count += count;
2778 regulator->deferred_disables = 0;
2780 for (i = 0; i < count; i++) {
2781 ret = _regulator_disable(regulator);
2783 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2786 WARN_ON(!total_count);
2788 if (rdev->coupling_desc.n_coupled > 1)
2789 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2791 regulator_unlock_dependent(rdev, &ww_ctx);
2794 for (i = 0; i < count; i++) {
2795 ret = regulator_disable(rdev->supply);
2798 "Supply disable failed: %d\n", ret);
2805 * regulator_disable_deferred - disable regulator output with delay
2806 * @regulator: regulator source
2807 * @ms: miliseconds until the regulator is disabled
2809 * Execute regulator_disable() on the regulator after a delay. This
2810 * is intended for use with devices that require some time to quiesce.
2812 * NOTE: this will only disable the regulator output if no other consumer
2813 * devices have it enabled, the regulator device supports disabling and
2814 * machine constraints permit this operation.
2816 int regulator_disable_deferred(struct regulator *regulator, int ms)
2818 struct regulator_dev *rdev = regulator->rdev;
2821 return regulator_disable(regulator);
2823 regulator_lock(rdev);
2824 regulator->deferred_disables++;
2825 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2826 msecs_to_jiffies(ms));
2827 regulator_unlock(rdev);
2831 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2833 static int _regulator_is_enabled(struct regulator_dev *rdev)
2835 /* A GPIO control always takes precedence */
2837 return rdev->ena_gpio_state;
2839 /* If we don't know then assume that the regulator is always on */
2840 if (!rdev->desc->ops->is_enabled)
2843 return rdev->desc->ops->is_enabled(rdev);
2846 static int _regulator_list_voltage(struct regulator_dev *rdev,
2847 unsigned selector, int lock)
2849 const struct regulator_ops *ops = rdev->desc->ops;
2852 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2853 return rdev->desc->fixed_uV;
2855 if (ops->list_voltage) {
2856 if (selector >= rdev->desc->n_voltages)
2859 regulator_lock(rdev);
2860 ret = ops->list_voltage(rdev, selector);
2862 regulator_unlock(rdev);
2863 } else if (rdev->is_switch && rdev->supply) {
2864 ret = _regulator_list_voltage(rdev->supply->rdev,
2871 if (ret < rdev->constraints->min_uV)
2873 else if (ret > rdev->constraints->max_uV)
2881 * regulator_is_enabled - is the regulator output enabled
2882 * @regulator: regulator source
2884 * Returns positive if the regulator driver backing the source/client
2885 * has requested that the device be enabled, zero if it hasn't, else a
2886 * negative errno code.
2888 * Note that the device backing this regulator handle can have multiple
2889 * users, so it might be enabled even if regulator_enable() was never
2890 * called for this particular source.
2892 int regulator_is_enabled(struct regulator *regulator)
2896 if (regulator->always_on)
2899 regulator_lock(regulator->rdev);
2900 ret = _regulator_is_enabled(regulator->rdev);
2901 regulator_unlock(regulator->rdev);
2905 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2908 * regulator_count_voltages - count regulator_list_voltage() selectors
2909 * @regulator: regulator source
2911 * Returns number of selectors, or negative errno. Selectors are
2912 * numbered starting at zero, and typically correspond to bitfields
2913 * in hardware registers.
2915 int regulator_count_voltages(struct regulator *regulator)
2917 struct regulator_dev *rdev = regulator->rdev;
2919 if (rdev->desc->n_voltages)
2920 return rdev->desc->n_voltages;
2922 if (!rdev->is_switch || !rdev->supply)
2925 return regulator_count_voltages(rdev->supply);
2927 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2930 * regulator_list_voltage - enumerate supported voltages
2931 * @regulator: regulator source
2932 * @selector: identify voltage to list
2933 * Context: can sleep
2935 * Returns a voltage that can be passed to @regulator_set_voltage(),
2936 * zero if this selector code can't be used on this system, or a
2939 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2941 return _regulator_list_voltage(regulator->rdev, selector, 1);
2943 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2946 * regulator_get_regmap - get the regulator's register map
2947 * @regulator: regulator source
2949 * Returns the register map for the given regulator, or an ERR_PTR value
2950 * if the regulator doesn't use regmap.
2952 struct regmap *regulator_get_regmap(struct regulator *regulator)
2954 struct regmap *map = regulator->rdev->regmap;
2956 return map ? map : ERR_PTR(-EOPNOTSUPP);
2960 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2961 * @regulator: regulator source
2962 * @vsel_reg: voltage selector register, output parameter
2963 * @vsel_mask: mask for voltage selector bitfield, output parameter
2965 * Returns the hardware register offset and bitmask used for setting the
2966 * regulator voltage. This might be useful when configuring voltage-scaling
2967 * hardware or firmware that can make I2C requests behind the kernel's back,
2970 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2971 * and 0 is returned, otherwise a negative errno is returned.
2973 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2975 unsigned *vsel_mask)
2977 struct regulator_dev *rdev = regulator->rdev;
2978 const struct regulator_ops *ops = rdev->desc->ops;
2980 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2983 *vsel_reg = rdev->desc->vsel_reg;
2984 *vsel_mask = rdev->desc->vsel_mask;
2988 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2991 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2992 * @regulator: regulator source
2993 * @selector: identify voltage to list
2995 * Converts the selector to a hardware-specific voltage selector that can be
2996 * directly written to the regulator registers. The address of the voltage
2997 * register can be determined by calling @regulator_get_hardware_vsel_register.
2999 * On error a negative errno is returned.
3001 int regulator_list_hardware_vsel(struct regulator *regulator,
3004 struct regulator_dev *rdev = regulator->rdev;
3005 const struct regulator_ops *ops = rdev->desc->ops;
3007 if (selector >= rdev->desc->n_voltages)
3009 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3014 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3017 * regulator_get_linear_step - return the voltage step size between VSEL values
3018 * @regulator: regulator source
3020 * Returns the voltage step size between VSEL values for linear
3021 * regulators, or return 0 if the regulator isn't a linear regulator.
3023 unsigned int regulator_get_linear_step(struct regulator *regulator)
3025 struct regulator_dev *rdev = regulator->rdev;
3027 return rdev->desc->uV_step;
3029 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3032 * regulator_is_supported_voltage - check if a voltage range can be supported
3034 * @regulator: Regulator to check.
3035 * @min_uV: Minimum required voltage in uV.
3036 * @max_uV: Maximum required voltage in uV.
3038 * Returns a boolean or a negative error code.
3040 int regulator_is_supported_voltage(struct regulator *regulator,
3041 int min_uV, int max_uV)
3043 struct regulator_dev *rdev = regulator->rdev;
3044 int i, voltages, ret;
3046 /* If we can't change voltage check the current voltage */
3047 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3048 ret = regulator_get_voltage(regulator);
3050 return min_uV <= ret && ret <= max_uV;
3055 /* Any voltage within constrains range is fine? */
3056 if (rdev->desc->continuous_voltage_range)
3057 return min_uV >= rdev->constraints->min_uV &&
3058 max_uV <= rdev->constraints->max_uV;
3060 ret = regulator_count_voltages(regulator);
3065 for (i = 0; i < voltages; i++) {
3066 ret = regulator_list_voltage(regulator, i);
3068 if (ret >= min_uV && ret <= max_uV)
3074 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3076 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3079 const struct regulator_desc *desc = rdev->desc;
3081 if (desc->ops->map_voltage)
3082 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3084 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3085 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3087 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3088 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3090 if (desc->ops->list_voltage ==
3091 regulator_list_voltage_pickable_linear_range)
3092 return regulator_map_voltage_pickable_linear_range(rdev,
3095 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3098 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3099 int min_uV, int max_uV,
3102 struct pre_voltage_change_data data;
3105 data.old_uV = _regulator_get_voltage(rdev);
3106 data.min_uV = min_uV;
3107 data.max_uV = max_uV;
3108 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3110 if (ret & NOTIFY_STOP_MASK)
3113 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3117 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3118 (void *)data.old_uV);
3123 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3124 int uV, unsigned selector)
3126 struct pre_voltage_change_data data;
3129 data.old_uV = _regulator_get_voltage(rdev);
3132 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3134 if (ret & NOTIFY_STOP_MASK)
3137 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3141 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3142 (void *)data.old_uV);
3147 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3148 int old_uV, int new_uV)
3150 unsigned int ramp_delay = 0;
3152 if (rdev->constraints->ramp_delay)
3153 ramp_delay = rdev->constraints->ramp_delay;
3154 else if (rdev->desc->ramp_delay)
3155 ramp_delay = rdev->desc->ramp_delay;
3156 else if (rdev->constraints->settling_time)
3157 return rdev->constraints->settling_time;
3158 else if (rdev->constraints->settling_time_up &&
3160 return rdev->constraints->settling_time_up;
3161 else if (rdev->constraints->settling_time_down &&
3163 return rdev->constraints->settling_time_down;
3165 if (ramp_delay == 0) {
3166 rdev_dbg(rdev, "ramp_delay not set\n");
3170 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3173 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3174 int min_uV, int max_uV)
3179 unsigned int selector;
3180 int old_selector = -1;
3181 const struct regulator_ops *ops = rdev->desc->ops;
3182 int old_uV = _regulator_get_voltage(rdev);
3184 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3186 min_uV += rdev->constraints->uV_offset;
3187 max_uV += rdev->constraints->uV_offset;
3190 * If we can't obtain the old selector there is not enough
3191 * info to call set_voltage_time_sel().
3193 if (_regulator_is_enabled(rdev) &&
3194 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3195 old_selector = ops->get_voltage_sel(rdev);
3196 if (old_selector < 0)
3197 return old_selector;
3200 if (ops->set_voltage) {
3201 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3205 if (ops->list_voltage)
3206 best_val = ops->list_voltage(rdev,
3209 best_val = _regulator_get_voltage(rdev);
3212 } else if (ops->set_voltage_sel) {
3213 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3215 best_val = ops->list_voltage(rdev, ret);
3216 if (min_uV <= best_val && max_uV >= best_val) {
3218 if (old_selector == selector)
3221 ret = _regulator_call_set_voltage_sel(
3222 rdev, best_val, selector);
3234 if (ops->set_voltage_time_sel) {
3236 * Call set_voltage_time_sel if successfully obtained
3239 if (old_selector >= 0 && old_selector != selector)
3240 delay = ops->set_voltage_time_sel(rdev, old_selector,
3243 if (old_uV != best_val) {
3244 if (ops->set_voltage_time)
3245 delay = ops->set_voltage_time(rdev, old_uV,
3248 delay = _regulator_set_voltage_time(rdev,
3255 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3259 /* Insert any necessary delays */
3260 if (delay >= 1000) {
3261 mdelay(delay / 1000);
3262 udelay(delay % 1000);
3267 if (best_val >= 0) {
3268 unsigned long data = best_val;
3270 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3275 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3280 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3281 int min_uV, int max_uV, suspend_state_t state)
3283 struct regulator_state *rstate;
3286 rstate = regulator_get_suspend_state(rdev, state);
3290 if (min_uV < rstate->min_uV)
3291 min_uV = rstate->min_uV;
3292 if (max_uV > rstate->max_uV)
3293 max_uV = rstate->max_uV;
3295 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3299 uV = rdev->desc->ops->list_voltage(rdev, sel);
3300 if (uV >= min_uV && uV <= max_uV)
3306 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3307 int min_uV, int max_uV,
3308 suspend_state_t state)
3310 struct regulator_dev *rdev = regulator->rdev;
3311 struct regulator_voltage *voltage = ®ulator->voltage[state];
3313 int old_min_uV, old_max_uV;
3316 /* If we're setting the same range as last time the change
3317 * should be a noop (some cpufreq implementations use the same
3318 * voltage for multiple frequencies, for example).
3320 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3323 /* If we're trying to set a range that overlaps the current voltage,
3324 * return successfully even though the regulator does not support
3325 * changing the voltage.
3327 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3328 current_uV = _regulator_get_voltage(rdev);
3329 if (min_uV <= current_uV && current_uV <= max_uV) {
3330 voltage->min_uV = min_uV;
3331 voltage->max_uV = max_uV;
3337 if (!rdev->desc->ops->set_voltage &&
3338 !rdev->desc->ops->set_voltage_sel) {
3343 /* constraints check */
3344 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3348 /* restore original values in case of error */
3349 old_min_uV = voltage->min_uV;
3350 old_max_uV = voltage->max_uV;
3351 voltage->min_uV = min_uV;
3352 voltage->max_uV = max_uV;
3354 /* for not coupled regulators this will just set the voltage */
3355 ret = regulator_balance_voltage(rdev, state);
3362 voltage->min_uV = old_min_uV;
3363 voltage->max_uV = old_max_uV;
3368 static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3369 int max_uV, suspend_state_t state)
3371 int best_supply_uV = 0;
3372 int supply_change_uV = 0;
3376 regulator_ops_is_valid(rdev->supply->rdev,
3377 REGULATOR_CHANGE_VOLTAGE) &&
3378 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3379 rdev->desc->ops->get_voltage_sel))) {
3380 int current_supply_uV;
3383 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3389 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3390 if (best_supply_uV < 0) {
3391 ret = best_supply_uV;
3395 best_supply_uV += rdev->desc->min_dropout_uV;
3397 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3398 if (current_supply_uV < 0) {
3399 ret = current_supply_uV;
3403 supply_change_uV = best_supply_uV - current_supply_uV;
3406 if (supply_change_uV > 0) {
3407 ret = regulator_set_voltage_unlocked(rdev->supply,
3408 best_supply_uV, INT_MAX, state);
3410 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3416 if (state == PM_SUSPEND_ON)
3417 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3419 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3424 if (supply_change_uV < 0) {
3425 ret = regulator_set_voltage_unlocked(rdev->supply,
3426 best_supply_uV, INT_MAX, state);
3428 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3430 /* No need to fail here */
3438 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3439 int *current_uV, int *min_uV)
3441 struct regulation_constraints *constraints = rdev->constraints;
3443 /* Limit voltage change only if necessary */
3444 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3447 if (*current_uV < 0) {
3448 *current_uV = _regulator_get_voltage(rdev);
3450 if (*current_uV < 0)
3454 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3457 /* Clamp target voltage within the given step */
3458 if (*current_uV < *min_uV)
3459 *min_uV = min(*current_uV + constraints->max_uV_step,
3462 *min_uV = max(*current_uV - constraints->max_uV_step,
3468 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3470 int *min_uV, int *max_uV,
3471 suspend_state_t state,
3474 struct coupling_desc *c_desc = &rdev->coupling_desc;
3475 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3476 struct regulation_constraints *constraints = rdev->constraints;
3477 int max_spread = constraints->max_spread;
3478 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3479 int max_current_uV = 0, min_current_uV = INT_MAX;
3480 int highest_min_uV = 0, target_uV, possible_uV;
3487 * If there are no coupled regulators, simply set the voltage
3488 * demanded by consumers.
3490 if (n_coupled == 1) {
3492 * If consumers don't provide any demands, set voltage
3495 desired_min_uV = constraints->min_uV;
3496 desired_max_uV = constraints->max_uV;
3498 ret = regulator_check_consumers(rdev,
3500 &desired_max_uV, state);
3504 possible_uV = desired_min_uV;
3510 /* Find highest min desired voltage */
3511 for (i = 0; i < n_coupled; i++) {
3513 int tmp_max = INT_MAX;
3515 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3517 ret = regulator_check_consumers(c_rdevs[i],
3523 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3527 highest_min_uV = max(highest_min_uV, tmp_min);
3530 desired_min_uV = tmp_min;
3531 desired_max_uV = tmp_max;
3536 * Let target_uV be equal to the desired one if possible.
3537 * If not, set it to minimum voltage, allowed by other coupled
3540 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3543 * Find min and max voltages, which currently aren't violating
3546 for (i = 1; i < n_coupled; i++) {
3549 if (!_regulator_is_enabled(c_rdevs[i]))
3552 tmp_act = _regulator_get_voltage(c_rdevs[i]);
3556 min_current_uV = min(tmp_act, min_current_uV);
3557 max_current_uV = max(tmp_act, max_current_uV);
3560 /* There aren't any other regulators enabled */
3561 if (max_current_uV == 0) {
3562 possible_uV = target_uV;
3565 * Correct target voltage, so as it currently isn't
3566 * violating max_spread
3568 possible_uV = max(target_uV, max_current_uV - max_spread);
3569 possible_uV = min(possible_uV, min_current_uV + max_spread);
3572 if (possible_uV > desired_max_uV)
3575 done = (possible_uV == target_uV);
3576 desired_min_uV = possible_uV;
3579 /* Apply max_uV_step constraint if necessary */
3580 if (state == PM_SUSPEND_ON) {
3581 ret = regulator_limit_voltage_step(rdev, current_uV,
3590 /* Set current_uV if wasn't done earlier in the code and if necessary */
3591 if (n_coupled > 1 && *current_uV == -1) {
3593 if (_regulator_is_enabled(rdev)) {
3594 ret = _regulator_get_voltage(rdev);
3600 *current_uV = desired_min_uV;
3604 *min_uV = desired_min_uV;
3605 *max_uV = desired_max_uV;
3610 static int regulator_balance_voltage(struct regulator_dev *rdev,
3611 suspend_state_t state)
3613 struct regulator_dev **c_rdevs;
3614 struct regulator_dev *best_rdev;
3615 struct coupling_desc *c_desc = &rdev->coupling_desc;
3616 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3617 bool best_c_rdev_done, c_rdev_done[MAX_COUPLED];
3618 unsigned int delta, best_delta;
3620 c_rdevs = c_desc->coupled_rdevs;
3621 n_coupled = c_desc->n_coupled;
3624 * If system is in a state other than PM_SUSPEND_ON, don't check
3625 * other coupled regulators.
3627 if (state != PM_SUSPEND_ON)
3630 if (c_desc->n_resolved < n_coupled) {
3631 rdev_err(rdev, "Not all coupled regulators registered\n");
3635 for (i = 0; i < n_coupled; i++)
3636 c_rdev_done[i] = false;
3639 * Find the best possible voltage change on each loop. Leave the loop
3640 * if there isn't any possible change.
3643 best_c_rdev_done = false;
3651 * Find highest difference between optimal voltage
3652 * and current voltage.
3654 for (i = 0; i < n_coupled; i++) {
3656 * optimal_uV is the best voltage that can be set for
3657 * i-th regulator at the moment without violating
3658 * max_spread constraint in order to balance
3659 * the coupled voltages.
3661 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3666 ret = regulator_get_optimal_voltage(c_rdevs[i],
3674 delta = abs(optimal_uV - current_uV);
3676 if (delta && best_delta <= delta) {
3677 best_c_rdev_done = ret;
3679 best_rdev = c_rdevs[i];
3680 best_min_uV = optimal_uV;
3681 best_max_uV = optimal_max_uV;
3686 /* Nothing to change, return successfully */
3692 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3693 best_max_uV, state);
3698 c_rdev_done[best_c_rdev] = best_c_rdev_done;
3700 } while (n_coupled > 1);
3707 * regulator_set_voltage - set regulator output voltage
3708 * @regulator: regulator source
3709 * @min_uV: Minimum required voltage in uV
3710 * @max_uV: Maximum acceptable voltage in uV
3712 * Sets a voltage regulator to the desired output voltage. This can be set
3713 * during any regulator state. IOW, regulator can be disabled or enabled.
3715 * If the regulator is enabled then the voltage will change to the new value
3716 * immediately otherwise if the regulator is disabled the regulator will
3717 * output at the new voltage when enabled.
3719 * NOTE: If the regulator is shared between several devices then the lowest
3720 * request voltage that meets the system constraints will be used.
3721 * Regulator system constraints must be set for this regulator before
3722 * calling this function otherwise this call will fail.
3724 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3726 struct ww_acquire_ctx ww_ctx;
3729 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3731 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3734 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3738 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3740 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3741 suspend_state_t state, bool en)
3743 struct regulator_state *rstate;
3745 rstate = regulator_get_suspend_state(rdev, state);
3749 if (!rstate->changeable)
3752 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3757 int regulator_suspend_enable(struct regulator_dev *rdev,
3758 suspend_state_t state)
3760 return regulator_suspend_toggle(rdev, state, true);
3762 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3764 int regulator_suspend_disable(struct regulator_dev *rdev,
3765 suspend_state_t state)
3767 struct regulator *regulator;
3768 struct regulator_voltage *voltage;
3771 * if any consumer wants this regulator device keeping on in
3772 * suspend states, don't set it as disabled.
3774 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3775 voltage = ®ulator->voltage[state];
3776 if (voltage->min_uV || voltage->max_uV)
3780 return regulator_suspend_toggle(rdev, state, false);
3782 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3784 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3785 int min_uV, int max_uV,
3786 suspend_state_t state)
3788 struct regulator_dev *rdev = regulator->rdev;
3789 struct regulator_state *rstate;
3791 rstate = regulator_get_suspend_state(rdev, state);
3795 if (rstate->min_uV == rstate->max_uV) {
3796 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3800 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3803 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3804 int max_uV, suspend_state_t state)
3806 struct ww_acquire_ctx ww_ctx;
3809 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3810 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3813 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3815 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3818 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3822 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3825 * regulator_set_voltage_time - get raise/fall time
3826 * @regulator: regulator source
3827 * @old_uV: starting voltage in microvolts
3828 * @new_uV: target voltage in microvolts
3830 * Provided with the starting and ending voltage, this function attempts to
3831 * calculate the time in microseconds required to rise or fall to this new
3834 int regulator_set_voltage_time(struct regulator *regulator,
3835 int old_uV, int new_uV)
3837 struct regulator_dev *rdev = regulator->rdev;
3838 const struct regulator_ops *ops = rdev->desc->ops;
3844 if (ops->set_voltage_time)
3845 return ops->set_voltage_time(rdev, old_uV, new_uV);
3846 else if (!ops->set_voltage_time_sel)
3847 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3849 /* Currently requires operations to do this */
3850 if (!ops->list_voltage || !rdev->desc->n_voltages)
3853 for (i = 0; i < rdev->desc->n_voltages; i++) {
3854 /* We only look for exact voltage matches here */
3855 voltage = regulator_list_voltage(regulator, i);
3860 if (voltage == old_uV)
3862 if (voltage == new_uV)
3866 if (old_sel < 0 || new_sel < 0)
3869 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3871 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3874 * regulator_set_voltage_time_sel - get raise/fall time
3875 * @rdev: regulator source device
3876 * @old_selector: selector for starting voltage
3877 * @new_selector: selector for target voltage
3879 * Provided with the starting and target voltage selectors, this function
3880 * returns time in microseconds required to rise or fall to this new voltage
3882 * Drivers providing ramp_delay in regulation_constraints can use this as their
3883 * set_voltage_time_sel() operation.
3885 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3886 unsigned int old_selector,
3887 unsigned int new_selector)
3889 int old_volt, new_volt;
3892 if (!rdev->desc->ops->list_voltage)
3895 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3896 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3898 if (rdev->desc->ops->set_voltage_time)
3899 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3902 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3904 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3907 * regulator_sync_voltage - re-apply last regulator output voltage
3908 * @regulator: regulator source
3910 * Re-apply the last configured voltage. This is intended to be used
3911 * where some external control source the consumer is cooperating with
3912 * has caused the configured voltage to change.
3914 int regulator_sync_voltage(struct regulator *regulator)
3916 struct regulator_dev *rdev = regulator->rdev;
3917 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
3918 int ret, min_uV, max_uV;
3920 regulator_lock(rdev);
3922 if (!rdev->desc->ops->set_voltage &&
3923 !rdev->desc->ops->set_voltage_sel) {
3928 /* This is only going to work if we've had a voltage configured. */
3929 if (!voltage->min_uV && !voltage->max_uV) {
3934 min_uV = voltage->min_uV;
3935 max_uV = voltage->max_uV;
3937 /* This should be a paranoia check... */
3938 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3942 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3946 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3949 regulator_unlock(rdev);
3952 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3954 static int _regulator_get_voltage(struct regulator_dev *rdev)
3959 if (rdev->desc->ops->get_bypass) {
3960 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3964 /* if bypassed the regulator must have a supply */
3965 if (!rdev->supply) {
3967 "bypassed regulator has no supply!\n");
3968 return -EPROBE_DEFER;
3971 return _regulator_get_voltage(rdev->supply->rdev);
3975 if (rdev->desc->ops->get_voltage_sel) {
3976 sel = rdev->desc->ops->get_voltage_sel(rdev);
3979 ret = rdev->desc->ops->list_voltage(rdev, sel);
3980 } else if (rdev->desc->ops->get_voltage) {
3981 ret = rdev->desc->ops->get_voltage(rdev);
3982 } else if (rdev->desc->ops->list_voltage) {
3983 ret = rdev->desc->ops->list_voltage(rdev, 0);
3984 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3985 ret = rdev->desc->fixed_uV;
3986 } else if (rdev->supply) {
3987 ret = _regulator_get_voltage(rdev->supply->rdev);
3994 return ret - rdev->constraints->uV_offset;
3998 * regulator_get_voltage - get regulator output voltage
3999 * @regulator: regulator source
4001 * This returns the current regulator voltage in uV.
4003 * NOTE: If the regulator is disabled it will return the voltage value. This
4004 * function should not be used to determine regulator state.
4006 int regulator_get_voltage(struct regulator *regulator)
4008 struct ww_acquire_ctx ww_ctx;
4011 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4012 ret = _regulator_get_voltage(regulator->rdev);
4013 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4017 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4020 * regulator_set_current_limit - set regulator output current limit
4021 * @regulator: regulator source
4022 * @min_uA: Minimum supported current in uA
4023 * @max_uA: Maximum supported current in uA
4025 * Sets current sink to the desired output current. This can be set during
4026 * any regulator state. IOW, regulator can be disabled or enabled.
4028 * If the regulator is enabled then the current will change to the new value
4029 * immediately otherwise if the regulator is disabled the regulator will
4030 * output at the new current when enabled.
4032 * NOTE: Regulator system constraints must be set for this regulator before
4033 * calling this function otherwise this call will fail.
4035 int regulator_set_current_limit(struct regulator *regulator,
4036 int min_uA, int max_uA)
4038 struct regulator_dev *rdev = regulator->rdev;
4041 regulator_lock(rdev);
4044 if (!rdev->desc->ops->set_current_limit) {
4049 /* constraints check */
4050 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4054 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4056 regulator_unlock(rdev);
4059 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4061 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4064 if (!rdev->desc->ops->get_current_limit)
4067 return rdev->desc->ops->get_current_limit(rdev);
4070 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4074 regulator_lock(rdev);
4075 ret = _regulator_get_current_limit_unlocked(rdev);
4076 regulator_unlock(rdev);
4082 * regulator_get_current_limit - get regulator output current
4083 * @regulator: regulator source
4085 * This returns the current supplied by the specified current sink in uA.
4087 * NOTE: If the regulator is disabled it will return the current value. This
4088 * function should not be used to determine regulator state.
4090 int regulator_get_current_limit(struct regulator *regulator)
4092 return _regulator_get_current_limit(regulator->rdev);
4094 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4097 * regulator_set_mode - set regulator operating mode
4098 * @regulator: regulator source
4099 * @mode: operating mode - one of the REGULATOR_MODE constants
4101 * Set regulator operating mode to increase regulator efficiency or improve
4102 * regulation performance.
4104 * NOTE: Regulator system constraints must be set for this regulator before
4105 * calling this function otherwise this call will fail.
4107 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4109 struct regulator_dev *rdev = regulator->rdev;
4111 int regulator_curr_mode;
4113 regulator_lock(rdev);
4116 if (!rdev->desc->ops->set_mode) {
4121 /* return if the same mode is requested */
4122 if (rdev->desc->ops->get_mode) {
4123 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4124 if (regulator_curr_mode == mode) {
4130 /* constraints check */
4131 ret = regulator_mode_constrain(rdev, &mode);
4135 ret = rdev->desc->ops->set_mode(rdev, mode);
4137 regulator_unlock(rdev);
4140 EXPORT_SYMBOL_GPL(regulator_set_mode);
4142 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4145 if (!rdev->desc->ops->get_mode)
4148 return rdev->desc->ops->get_mode(rdev);
4151 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4155 regulator_lock(rdev);
4156 ret = _regulator_get_mode_unlocked(rdev);
4157 regulator_unlock(rdev);
4163 * regulator_get_mode - get regulator operating mode
4164 * @regulator: regulator source
4166 * Get the current regulator operating mode.
4168 unsigned int regulator_get_mode(struct regulator *regulator)
4170 return _regulator_get_mode(regulator->rdev);
4172 EXPORT_SYMBOL_GPL(regulator_get_mode);
4174 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4175 unsigned int *flags)
4179 regulator_lock(rdev);
4182 if (!rdev->desc->ops->get_error_flags) {
4187 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4189 regulator_unlock(rdev);
4194 * regulator_get_error_flags - get regulator error information
4195 * @regulator: regulator source
4196 * @flags: pointer to store error flags
4198 * Get the current regulator error information.
4200 int regulator_get_error_flags(struct regulator *regulator,
4201 unsigned int *flags)
4203 return _regulator_get_error_flags(regulator->rdev, flags);
4205 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4208 * regulator_set_load - set regulator load
4209 * @regulator: regulator source
4210 * @uA_load: load current
4212 * Notifies the regulator core of a new device load. This is then used by
4213 * DRMS (if enabled by constraints) to set the most efficient regulator
4214 * operating mode for the new regulator loading.
4216 * Consumer devices notify their supply regulator of the maximum power
4217 * they will require (can be taken from device datasheet in the power
4218 * consumption tables) when they change operational status and hence power
4219 * state. Examples of operational state changes that can affect power
4220 * consumption are :-
4222 * o Device is opened / closed.
4223 * o Device I/O is about to begin or has just finished.
4224 * o Device is idling in between work.
4226 * This information is also exported via sysfs to userspace.
4228 * DRMS will sum the total requested load on the regulator and change
4229 * to the most efficient operating mode if platform constraints allow.
4231 * NOTE: when a regulator consumer requests to have a regulator
4232 * disabled then any load that consumer requested no longer counts
4233 * toward the total requested load. If the regulator is re-enabled
4234 * then the previously requested load will start counting again.
4236 * If a regulator is an always-on regulator then an individual consumer's
4237 * load will still be removed if that consumer is fully disabled.
4239 * On error a negative errno is returned.
4241 int regulator_set_load(struct regulator *regulator, int uA_load)
4243 struct regulator_dev *rdev = regulator->rdev;
4247 regulator_lock(rdev);
4248 old_uA_load = regulator->uA_load;
4249 regulator->uA_load = uA_load;
4250 if (regulator->enable_count && old_uA_load != uA_load) {
4251 ret = drms_uA_update(rdev);
4253 regulator->uA_load = old_uA_load;
4255 regulator_unlock(rdev);
4259 EXPORT_SYMBOL_GPL(regulator_set_load);
4262 * regulator_allow_bypass - allow the regulator to go into bypass mode
4264 * @regulator: Regulator to configure
4265 * @enable: enable or disable bypass mode
4267 * Allow the regulator to go into bypass mode if all other consumers
4268 * for the regulator also enable bypass mode and the machine
4269 * constraints allow this. Bypass mode means that the regulator is
4270 * simply passing the input directly to the output with no regulation.
4272 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4274 struct regulator_dev *rdev = regulator->rdev;
4277 if (!rdev->desc->ops->set_bypass)
4280 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4283 regulator_lock(rdev);
4285 if (enable && !regulator->bypass) {
4286 rdev->bypass_count++;
4288 if (rdev->bypass_count == rdev->open_count) {
4289 ret = rdev->desc->ops->set_bypass(rdev, enable);
4291 rdev->bypass_count--;
4294 } else if (!enable && regulator->bypass) {
4295 rdev->bypass_count--;
4297 if (rdev->bypass_count != rdev->open_count) {
4298 ret = rdev->desc->ops->set_bypass(rdev, enable);
4300 rdev->bypass_count++;
4305 regulator->bypass = enable;
4307 regulator_unlock(rdev);
4311 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4314 * regulator_register_notifier - register regulator event notifier
4315 * @regulator: regulator source
4316 * @nb: notifier block
4318 * Register notifier block to receive regulator events.
4320 int regulator_register_notifier(struct regulator *regulator,
4321 struct notifier_block *nb)
4323 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4326 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4329 * regulator_unregister_notifier - unregister regulator event notifier
4330 * @regulator: regulator source
4331 * @nb: notifier block
4333 * Unregister regulator event notifier block.
4335 int regulator_unregister_notifier(struct regulator *regulator,
4336 struct notifier_block *nb)
4338 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4341 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4343 /* notify regulator consumers and downstream regulator consumers.
4344 * Note mutex must be held by caller.
4346 static int _notifier_call_chain(struct regulator_dev *rdev,
4347 unsigned long event, void *data)
4349 /* call rdev chain first */
4350 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4354 * regulator_bulk_get - get multiple regulator consumers
4356 * @dev: Device to supply
4357 * @num_consumers: Number of consumers to register
4358 * @consumers: Configuration of consumers; clients are stored here.
4360 * @return 0 on success, an errno on failure.
4362 * This helper function allows drivers to get several regulator
4363 * consumers in one operation. If any of the regulators cannot be
4364 * acquired then any regulators that were allocated will be freed
4365 * before returning to the caller.
4367 int regulator_bulk_get(struct device *dev, int num_consumers,
4368 struct regulator_bulk_data *consumers)
4373 for (i = 0; i < num_consumers; i++)
4374 consumers[i].consumer = NULL;
4376 for (i = 0; i < num_consumers; i++) {
4377 consumers[i].consumer = regulator_get(dev,
4378 consumers[i].supply);
4379 if (IS_ERR(consumers[i].consumer)) {
4380 ret = PTR_ERR(consumers[i].consumer);
4381 dev_err(dev, "Failed to get supply '%s': %d\n",
4382 consumers[i].supply, ret);
4383 consumers[i].consumer = NULL;
4392 regulator_put(consumers[i].consumer);
4396 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4398 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4400 struct regulator_bulk_data *bulk = data;
4402 bulk->ret = regulator_enable(bulk->consumer);
4406 * regulator_bulk_enable - enable multiple regulator consumers
4408 * @num_consumers: Number of consumers
4409 * @consumers: Consumer data; clients are stored here.
4410 * @return 0 on success, an errno on failure
4412 * This convenience API allows consumers to enable multiple regulator
4413 * clients in a single API call. If any consumers cannot be enabled
4414 * then any others that were enabled will be disabled again prior to
4417 int regulator_bulk_enable(int num_consumers,
4418 struct regulator_bulk_data *consumers)
4420 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4424 for (i = 0; i < num_consumers; i++) {
4425 async_schedule_domain(regulator_bulk_enable_async,
4426 &consumers[i], &async_domain);
4429 async_synchronize_full_domain(&async_domain);
4431 /* If any consumer failed we need to unwind any that succeeded */
4432 for (i = 0; i < num_consumers; i++) {
4433 if (consumers[i].ret != 0) {
4434 ret = consumers[i].ret;
4442 for (i = 0; i < num_consumers; i++) {
4443 if (consumers[i].ret < 0)
4444 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4447 regulator_disable(consumers[i].consumer);
4452 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4455 * regulator_bulk_disable - disable multiple regulator consumers
4457 * @num_consumers: Number of consumers
4458 * @consumers: Consumer data; clients are stored here.
4459 * @return 0 on success, an errno on failure
4461 * This convenience API allows consumers to disable multiple regulator
4462 * clients in a single API call. If any consumers cannot be disabled
4463 * then any others that were disabled will be enabled again prior to
4466 int regulator_bulk_disable(int num_consumers,
4467 struct regulator_bulk_data *consumers)
4472 for (i = num_consumers - 1; i >= 0; --i) {
4473 ret = regulator_disable(consumers[i].consumer);
4481 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4482 for (++i; i < num_consumers; ++i) {
4483 r = regulator_enable(consumers[i].consumer);
4485 pr_err("Failed to re-enable %s: %d\n",
4486 consumers[i].supply, r);
4491 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4494 * regulator_bulk_force_disable - force disable multiple regulator consumers
4496 * @num_consumers: Number of consumers
4497 * @consumers: Consumer data; clients are stored here.
4498 * @return 0 on success, an errno on failure
4500 * This convenience API allows consumers to forcibly disable multiple regulator
4501 * clients in a single API call.
4502 * NOTE: This should be used for situations when device damage will
4503 * likely occur if the regulators are not disabled (e.g. over temp).
4504 * Although regulator_force_disable function call for some consumers can
4505 * return error numbers, the function is called for all consumers.
4507 int regulator_bulk_force_disable(int num_consumers,
4508 struct regulator_bulk_data *consumers)
4513 for (i = 0; i < num_consumers; i++) {
4515 regulator_force_disable(consumers[i].consumer);
4517 /* Store first error for reporting */
4518 if (consumers[i].ret && !ret)
4519 ret = consumers[i].ret;
4524 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4527 * regulator_bulk_free - free multiple regulator consumers
4529 * @num_consumers: Number of consumers
4530 * @consumers: Consumer data; clients are stored here.
4532 * This convenience API allows consumers to free multiple regulator
4533 * clients in a single API call.
4535 void regulator_bulk_free(int num_consumers,
4536 struct regulator_bulk_data *consumers)
4540 for (i = 0; i < num_consumers; i++) {
4541 regulator_put(consumers[i].consumer);
4542 consumers[i].consumer = NULL;
4545 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4548 * regulator_notifier_call_chain - call regulator event notifier
4549 * @rdev: regulator source
4550 * @event: notifier block
4551 * @data: callback-specific data.
4553 * Called by regulator drivers to notify clients a regulator event has
4554 * occurred. We also notify regulator clients downstream.
4555 * Note lock must be held by caller.
4557 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4558 unsigned long event, void *data)
4560 lockdep_assert_held_once(&rdev->mutex.base);
4562 _notifier_call_chain(rdev, event, data);
4566 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4569 * regulator_mode_to_status - convert a regulator mode into a status
4571 * @mode: Mode to convert
4573 * Convert a regulator mode into a status.
4575 int regulator_mode_to_status(unsigned int mode)
4578 case REGULATOR_MODE_FAST:
4579 return REGULATOR_STATUS_FAST;
4580 case REGULATOR_MODE_NORMAL:
4581 return REGULATOR_STATUS_NORMAL;
4582 case REGULATOR_MODE_IDLE:
4583 return REGULATOR_STATUS_IDLE;
4584 case REGULATOR_MODE_STANDBY:
4585 return REGULATOR_STATUS_STANDBY;
4587 return REGULATOR_STATUS_UNDEFINED;
4590 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4592 static struct attribute *regulator_dev_attrs[] = {
4593 &dev_attr_name.attr,
4594 &dev_attr_num_users.attr,
4595 &dev_attr_type.attr,
4596 &dev_attr_microvolts.attr,
4597 &dev_attr_microamps.attr,
4598 &dev_attr_opmode.attr,
4599 &dev_attr_state.attr,
4600 &dev_attr_status.attr,
4601 &dev_attr_bypass.attr,
4602 &dev_attr_requested_microamps.attr,
4603 &dev_attr_min_microvolts.attr,
4604 &dev_attr_max_microvolts.attr,
4605 &dev_attr_min_microamps.attr,
4606 &dev_attr_max_microamps.attr,
4607 &dev_attr_suspend_standby_state.attr,
4608 &dev_attr_suspend_mem_state.attr,
4609 &dev_attr_suspend_disk_state.attr,
4610 &dev_attr_suspend_standby_microvolts.attr,
4611 &dev_attr_suspend_mem_microvolts.attr,
4612 &dev_attr_suspend_disk_microvolts.attr,
4613 &dev_attr_suspend_standby_mode.attr,
4614 &dev_attr_suspend_mem_mode.attr,
4615 &dev_attr_suspend_disk_mode.attr,
4620 * To avoid cluttering sysfs (and memory) with useless state, only
4621 * create attributes that can be meaningfully displayed.
4623 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4624 struct attribute *attr, int idx)
4626 struct device *dev = kobj_to_dev(kobj);
4627 struct regulator_dev *rdev = dev_to_rdev(dev);
4628 const struct regulator_ops *ops = rdev->desc->ops;
4629 umode_t mode = attr->mode;
4631 /* these three are always present */
4632 if (attr == &dev_attr_name.attr ||
4633 attr == &dev_attr_num_users.attr ||
4634 attr == &dev_attr_type.attr)
4637 /* some attributes need specific methods to be displayed */
4638 if (attr == &dev_attr_microvolts.attr) {
4639 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4640 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4641 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4642 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4647 if (attr == &dev_attr_microamps.attr)
4648 return ops->get_current_limit ? mode : 0;
4650 if (attr == &dev_attr_opmode.attr)
4651 return ops->get_mode ? mode : 0;
4653 if (attr == &dev_attr_state.attr)
4654 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4656 if (attr == &dev_attr_status.attr)
4657 return ops->get_status ? mode : 0;
4659 if (attr == &dev_attr_bypass.attr)
4660 return ops->get_bypass ? mode : 0;
4662 /* constraints need specific supporting methods */
4663 if (attr == &dev_attr_min_microvolts.attr ||
4664 attr == &dev_attr_max_microvolts.attr)
4665 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4667 if (attr == &dev_attr_min_microamps.attr ||
4668 attr == &dev_attr_max_microamps.attr)
4669 return ops->set_current_limit ? mode : 0;
4671 if (attr == &dev_attr_suspend_standby_state.attr ||
4672 attr == &dev_attr_suspend_mem_state.attr ||
4673 attr == &dev_attr_suspend_disk_state.attr)
4676 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4677 attr == &dev_attr_suspend_mem_microvolts.attr ||
4678 attr == &dev_attr_suspend_disk_microvolts.attr)
4679 return ops->set_suspend_voltage ? mode : 0;
4681 if (attr == &dev_attr_suspend_standby_mode.attr ||
4682 attr == &dev_attr_suspend_mem_mode.attr ||
4683 attr == &dev_attr_suspend_disk_mode.attr)
4684 return ops->set_suspend_mode ? mode : 0;
4689 static const struct attribute_group regulator_dev_group = {
4690 .attrs = regulator_dev_attrs,
4691 .is_visible = regulator_attr_is_visible,
4694 static const struct attribute_group *regulator_dev_groups[] = {
4695 ®ulator_dev_group,
4699 static void regulator_dev_release(struct device *dev)
4701 struct regulator_dev *rdev = dev_get_drvdata(dev);
4703 kfree(rdev->constraints);
4704 of_node_put(rdev->dev.of_node);
4708 static void rdev_init_debugfs(struct regulator_dev *rdev)
4710 struct device *parent = rdev->dev.parent;
4711 const char *rname = rdev_get_name(rdev);
4712 char name[NAME_MAX];
4714 /* Avoid duplicate debugfs directory names */
4715 if (parent && rname == rdev->desc->name) {
4716 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4721 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4722 if (!rdev->debugfs) {
4723 rdev_warn(rdev, "Failed to create debugfs directory\n");
4727 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4729 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4731 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4732 &rdev->bypass_count);
4735 static int regulator_register_resolve_supply(struct device *dev, void *data)
4737 struct regulator_dev *rdev = dev_to_rdev(dev);
4739 if (regulator_resolve_supply(rdev))
4740 rdev_dbg(rdev, "unable to resolve supply\n");
4745 static void regulator_resolve_coupling(struct regulator_dev *rdev)
4747 struct coupling_desc *c_desc = &rdev->coupling_desc;
4748 int n_coupled = c_desc->n_coupled;
4749 struct regulator_dev *c_rdev;
4752 for (i = 1; i < n_coupled; i++) {
4753 /* already resolved */
4754 if (c_desc->coupled_rdevs[i])
4757 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4762 regulator_lock(c_rdev);
4764 c_desc->coupled_rdevs[i] = c_rdev;
4765 c_desc->n_resolved++;
4767 regulator_unlock(c_rdev);
4769 regulator_resolve_coupling(c_rdev);
4773 static void regulator_remove_coupling(struct regulator_dev *rdev)
4775 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4776 struct regulator_dev *__c_rdev, *c_rdev;
4777 unsigned int __n_coupled, n_coupled;
4780 n_coupled = c_desc->n_coupled;
4782 for (i = 1; i < n_coupled; i++) {
4783 c_rdev = c_desc->coupled_rdevs[i];
4788 regulator_lock(c_rdev);
4790 __c_desc = &c_rdev->coupling_desc;
4791 __n_coupled = __c_desc->n_coupled;
4793 for (k = 1; k < __n_coupled; k++) {
4794 __c_rdev = __c_desc->coupled_rdevs[k];
4796 if (__c_rdev == rdev) {
4797 __c_desc->coupled_rdevs[k] = NULL;
4798 __c_desc->n_resolved--;
4803 regulator_unlock(c_rdev);
4805 c_desc->coupled_rdevs[i] = NULL;
4806 c_desc->n_resolved--;
4810 static int regulator_init_coupling(struct regulator_dev *rdev)
4814 if (!IS_ENABLED(CONFIG_OF))
4817 n_phandles = of_get_n_coupled(rdev);
4819 if (n_phandles + 1 > MAX_COUPLED) {
4820 rdev_err(rdev, "too many regulators coupled\n");
4825 * Every regulator should always have coupling descriptor filled with
4826 * at least pointer to itself.
4828 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4829 rdev->coupling_desc.n_coupled = n_phandles + 1;
4830 rdev->coupling_desc.n_resolved++;
4832 /* regulator isn't coupled */
4833 if (n_phandles == 0)
4836 /* regulator, which can't change its voltage, can't be coupled */
4837 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
4838 rdev_err(rdev, "voltage operation not allowed\n");
4842 if (rdev->constraints->max_spread <= 0) {
4843 rdev_err(rdev, "wrong max_spread value\n");
4847 if (!of_check_coupling_data(rdev))
4854 * regulator_register - register regulator
4855 * @regulator_desc: regulator to register
4856 * @cfg: runtime configuration for regulator
4858 * Called by regulator drivers to register a regulator.
4859 * Returns a valid pointer to struct regulator_dev on success
4860 * or an ERR_PTR() on error.
4862 struct regulator_dev *
4863 regulator_register(const struct regulator_desc *regulator_desc,
4864 const struct regulator_config *cfg)
4866 const struct regulation_constraints *constraints = NULL;
4867 const struct regulator_init_data *init_data;
4868 struct regulator_config *config = NULL;
4869 static atomic_t regulator_no = ATOMIC_INIT(-1);
4870 struct regulator_dev *rdev;
4874 if (regulator_desc == NULL || cfg == NULL)
4875 return ERR_PTR(-EINVAL);
4880 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4881 return ERR_PTR(-EINVAL);
4883 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4884 regulator_desc->type != REGULATOR_CURRENT)
4885 return ERR_PTR(-EINVAL);
4887 /* Only one of each should be implemented */
4888 WARN_ON(regulator_desc->ops->get_voltage &&
4889 regulator_desc->ops->get_voltage_sel);
4890 WARN_ON(regulator_desc->ops->set_voltage &&
4891 regulator_desc->ops->set_voltage_sel);
4893 /* If we're using selectors we must implement list_voltage. */
4894 if (regulator_desc->ops->get_voltage_sel &&
4895 !regulator_desc->ops->list_voltage) {
4896 return ERR_PTR(-EINVAL);
4898 if (regulator_desc->ops->set_voltage_sel &&
4899 !regulator_desc->ops->list_voltage) {
4900 return ERR_PTR(-EINVAL);
4903 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4905 return ERR_PTR(-ENOMEM);
4908 * Duplicate the config so the driver could override it after
4909 * parsing init data.
4911 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4912 if (config == NULL) {
4914 return ERR_PTR(-ENOMEM);
4917 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4918 &rdev->dev.of_node);
4920 init_data = config->init_data;
4921 rdev->dev.of_node = of_node_get(config->of_node);
4924 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
4925 rdev->reg_data = config->driver_data;
4926 rdev->owner = regulator_desc->owner;
4927 rdev->desc = regulator_desc;
4929 rdev->regmap = config->regmap;
4930 else if (dev_get_regmap(dev, NULL))
4931 rdev->regmap = dev_get_regmap(dev, NULL);
4932 else if (dev->parent)
4933 rdev->regmap = dev_get_regmap(dev->parent, NULL);
4934 INIT_LIST_HEAD(&rdev->consumer_list);
4935 INIT_LIST_HEAD(&rdev->list);
4936 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4937 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4939 /* preform any regulator specific init */
4940 if (init_data && init_data->regulator_init) {
4941 ret = init_data->regulator_init(rdev->reg_data);
4946 if (config->ena_gpiod ||
4947 ((config->ena_gpio || config->ena_gpio_initialized) &&
4948 gpio_is_valid(config->ena_gpio))) {
4949 mutex_lock(®ulator_list_mutex);
4950 ret = regulator_ena_gpio_request(rdev, config);
4951 mutex_unlock(®ulator_list_mutex);
4953 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4954 config->ena_gpio, ret);
4959 /* register with sysfs */
4960 rdev->dev.class = ®ulator_class;
4961 rdev->dev.parent = dev;
4962 dev_set_name(&rdev->dev, "regulator.%lu",
4963 (unsigned long) atomic_inc_return(®ulator_no));
4965 /* set regulator constraints */
4967 constraints = &init_data->constraints;
4969 if (init_data && init_data->supply_regulator)
4970 rdev->supply_name = init_data->supply_regulator;
4971 else if (regulator_desc->supply_name)
4972 rdev->supply_name = regulator_desc->supply_name;
4975 * Attempt to resolve the regulator supply, if specified,
4976 * but don't return an error if we fail because we will try
4977 * to resolve it again later as more regulators are added.
4979 if (regulator_resolve_supply(rdev))
4980 rdev_dbg(rdev, "unable to resolve supply\n");
4982 ret = set_machine_constraints(rdev, constraints);
4986 ret = regulator_init_coupling(rdev);
4990 /* add consumers devices */
4992 mutex_lock(®ulator_list_mutex);
4993 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4994 ret = set_consumer_device_supply(rdev,
4995 init_data->consumer_supplies[i].dev_name,
4996 init_data->consumer_supplies[i].supply);
4998 mutex_unlock(®ulator_list_mutex);
4999 dev_err(dev, "Failed to set supply %s\n",
5000 init_data->consumer_supplies[i].supply);
5001 goto unset_supplies;
5004 mutex_unlock(®ulator_list_mutex);
5007 if (!rdev->desc->ops->get_voltage &&
5008 !rdev->desc->ops->list_voltage &&
5009 !rdev->desc->fixed_uV)
5010 rdev->is_switch = true;
5012 dev_set_drvdata(&rdev->dev, rdev);
5013 ret = device_register(&rdev->dev);
5015 put_device(&rdev->dev);
5016 goto unset_supplies;
5019 rdev_init_debugfs(rdev);
5021 /* try to resolve regulators coupling since a new one was registered */
5022 mutex_lock(®ulator_list_mutex);
5023 regulator_resolve_coupling(rdev);
5024 mutex_unlock(®ulator_list_mutex);
5026 /* try to resolve regulators supply since a new one was registered */
5027 class_for_each_device(®ulator_class, NULL, NULL,
5028 regulator_register_resolve_supply);
5033 mutex_lock(®ulator_list_mutex);
5034 unset_regulator_supplies(rdev);
5035 mutex_unlock(®ulator_list_mutex);
5037 kfree(rdev->constraints);
5038 mutex_lock(®ulator_list_mutex);
5039 regulator_ena_gpio_free(rdev);
5040 mutex_unlock(®ulator_list_mutex);
5044 return ERR_PTR(ret);
5046 EXPORT_SYMBOL_GPL(regulator_register);
5049 * regulator_unregister - unregister regulator
5050 * @rdev: regulator to unregister
5052 * Called by regulator drivers to unregister a regulator.
5054 void regulator_unregister(struct regulator_dev *rdev)
5060 while (rdev->use_count--)
5061 regulator_disable(rdev->supply);
5062 regulator_put(rdev->supply);
5065 mutex_lock(®ulator_list_mutex);
5067 debugfs_remove_recursive(rdev->debugfs);
5068 flush_work(&rdev->disable_work.work);
5069 WARN_ON(rdev->open_count);
5070 regulator_remove_coupling(rdev);
5071 unset_regulator_supplies(rdev);
5072 list_del(&rdev->list);
5073 regulator_ena_gpio_free(rdev);
5074 device_unregister(&rdev->dev);
5076 mutex_unlock(®ulator_list_mutex);
5078 EXPORT_SYMBOL_GPL(regulator_unregister);
5080 #ifdef CONFIG_SUSPEND
5082 * regulator_suspend - prepare regulators for system wide suspend
5083 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5085 * Configure each regulator with it's suspend operating parameters for state.
5087 static int regulator_suspend(struct device *dev)
5089 struct regulator_dev *rdev = dev_to_rdev(dev);
5090 suspend_state_t state = pm_suspend_target_state;
5093 regulator_lock(rdev);
5094 ret = suspend_set_state(rdev, state);
5095 regulator_unlock(rdev);
5100 static int regulator_resume(struct device *dev)
5102 suspend_state_t state = pm_suspend_target_state;
5103 struct regulator_dev *rdev = dev_to_rdev(dev);
5104 struct regulator_state *rstate;
5107 rstate = regulator_get_suspend_state(rdev, state);
5111 regulator_lock(rdev);
5113 if (rdev->desc->ops->resume &&
5114 (rstate->enabled == ENABLE_IN_SUSPEND ||
5115 rstate->enabled == DISABLE_IN_SUSPEND))
5116 ret = rdev->desc->ops->resume(rdev);
5118 regulator_unlock(rdev);
5122 #else /* !CONFIG_SUSPEND */
5124 #define regulator_suspend NULL
5125 #define regulator_resume NULL
5127 #endif /* !CONFIG_SUSPEND */
5130 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5131 .suspend = regulator_suspend,
5132 .resume = regulator_resume,
5136 struct class regulator_class = {
5137 .name = "regulator",
5138 .dev_release = regulator_dev_release,
5139 .dev_groups = regulator_dev_groups,
5141 .pm = ®ulator_pm_ops,
5145 * regulator_has_full_constraints - the system has fully specified constraints
5147 * Calling this function will cause the regulator API to disable all
5148 * regulators which have a zero use count and don't have an always_on
5149 * constraint in a late_initcall.
5151 * The intention is that this will become the default behaviour in a
5152 * future kernel release so users are encouraged to use this facility
5155 void regulator_has_full_constraints(void)
5157 has_full_constraints = 1;
5159 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5162 * rdev_get_drvdata - get rdev regulator driver data
5165 * Get rdev regulator driver private data. This call can be used in the
5166 * regulator driver context.
5168 void *rdev_get_drvdata(struct regulator_dev *rdev)
5170 return rdev->reg_data;
5172 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5175 * regulator_get_drvdata - get regulator driver data
5176 * @regulator: regulator
5178 * Get regulator driver private data. This call can be used in the consumer
5179 * driver context when non API regulator specific functions need to be called.
5181 void *regulator_get_drvdata(struct regulator *regulator)
5183 return regulator->rdev->reg_data;
5185 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5188 * regulator_set_drvdata - set regulator driver data
5189 * @regulator: regulator
5192 void regulator_set_drvdata(struct regulator *regulator, void *data)
5194 regulator->rdev->reg_data = data;
5196 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5199 * regulator_get_id - get regulator ID
5202 int rdev_get_id(struct regulator_dev *rdev)
5204 return rdev->desc->id;
5206 EXPORT_SYMBOL_GPL(rdev_get_id);
5208 struct device *rdev_get_dev(struct regulator_dev *rdev)
5212 EXPORT_SYMBOL_GPL(rdev_get_dev);
5214 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5216 return reg_init_data->driver_data;
5218 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5220 #ifdef CONFIG_DEBUG_FS
5221 static int supply_map_show(struct seq_file *sf, void *data)
5223 struct regulator_map *map;
5225 list_for_each_entry(map, ®ulator_map_list, list) {
5226 seq_printf(sf, "%s -> %s.%s\n",
5227 rdev_get_name(map->regulator), map->dev_name,
5234 static int supply_map_open(struct inode *inode, struct file *file)
5236 return single_open(file, supply_map_show, inode->i_private);
5240 static const struct file_operations supply_map_fops = {
5241 #ifdef CONFIG_DEBUG_FS
5242 .open = supply_map_open,
5244 .llseek = seq_lseek,
5245 .release = single_release,
5249 #ifdef CONFIG_DEBUG_FS
5250 struct summary_data {
5252 struct regulator_dev *parent;
5256 static void regulator_summary_show_subtree(struct seq_file *s,
5257 struct regulator_dev *rdev,
5260 static int regulator_summary_show_children(struct device *dev, void *data)
5262 struct regulator_dev *rdev = dev_to_rdev(dev);
5263 struct summary_data *summary_data = data;
5265 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5266 regulator_summary_show_subtree(summary_data->s, rdev,
5267 summary_data->level + 1);
5272 static void regulator_summary_show_subtree(struct seq_file *s,
5273 struct regulator_dev *rdev,
5276 struct regulation_constraints *c;
5277 struct regulator *consumer;
5278 struct summary_data summary_data;
5279 unsigned int opmode;
5284 opmode = _regulator_get_mode_unlocked(rdev);
5285 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5287 30 - level * 3, rdev_get_name(rdev),
5288 rdev->use_count, rdev->open_count, rdev->bypass_count,
5289 regulator_opmode_to_str(opmode));
5291 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
5292 seq_printf(s, "%5dmA ",
5293 _regulator_get_current_limit_unlocked(rdev) / 1000);
5295 c = rdev->constraints;
5297 switch (rdev->desc->type) {
5298 case REGULATOR_VOLTAGE:
5299 seq_printf(s, "%5dmV %5dmV ",
5300 c->min_uV / 1000, c->max_uV / 1000);
5302 case REGULATOR_CURRENT:
5303 seq_printf(s, "%5dmA %5dmA ",
5304 c->min_uA / 1000, c->max_uA / 1000);
5311 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5312 if (consumer->dev && consumer->dev->class == ®ulator_class)
5315 seq_printf(s, "%*s%-*s ",
5316 (level + 1) * 3 + 1, "",
5317 30 - (level + 1) * 3,
5318 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5320 switch (rdev->desc->type) {
5321 case REGULATOR_VOLTAGE:
5322 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5323 consumer->enable_count,
5324 consumer->uA_load / 1000,
5325 consumer->uA_load && !consumer->enable_count ?
5327 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5328 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5330 case REGULATOR_CURRENT:
5338 summary_data.level = level;
5339 summary_data.parent = rdev;
5341 class_for_each_device(®ulator_class, NULL, &summary_data,
5342 regulator_summary_show_children);
5345 struct summary_lock_data {
5346 struct ww_acquire_ctx *ww_ctx;
5347 struct regulator_dev **new_contended_rdev;
5348 struct regulator_dev **old_contended_rdev;
5351 static int regulator_summary_lock_one(struct device *dev, void *data)
5353 struct regulator_dev *rdev = dev_to_rdev(dev);
5354 struct summary_lock_data *lock_data = data;
5357 if (rdev != *lock_data->old_contended_rdev) {
5358 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5360 if (ret == -EDEADLK)
5361 *lock_data->new_contended_rdev = rdev;
5365 *lock_data->old_contended_rdev = NULL;
5371 static int regulator_summary_unlock_one(struct device *dev, void *data)
5373 struct regulator_dev *rdev = dev_to_rdev(dev);
5374 struct summary_lock_data *lock_data = data;
5377 if (rdev == *lock_data->new_contended_rdev)
5381 regulator_unlock(rdev);
5386 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5387 struct regulator_dev **new_contended_rdev,
5388 struct regulator_dev **old_contended_rdev)
5390 struct summary_lock_data lock_data;
5393 lock_data.ww_ctx = ww_ctx;
5394 lock_data.new_contended_rdev = new_contended_rdev;
5395 lock_data.old_contended_rdev = old_contended_rdev;
5397 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5398 regulator_summary_lock_one);
5400 class_for_each_device(®ulator_class, NULL, &lock_data,
5401 regulator_summary_unlock_one);
5406 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5408 struct regulator_dev *new_contended_rdev = NULL;
5409 struct regulator_dev *old_contended_rdev = NULL;
5412 mutex_lock(®ulator_list_mutex);
5414 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5417 if (new_contended_rdev) {
5418 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5419 old_contended_rdev = new_contended_rdev;
5420 old_contended_rdev->ref_cnt++;
5423 err = regulator_summary_lock_all(ww_ctx,
5424 &new_contended_rdev,
5425 &old_contended_rdev);
5427 if (old_contended_rdev)
5428 regulator_unlock(old_contended_rdev);
5430 } while (err == -EDEADLK);
5432 ww_acquire_done(ww_ctx);
5435 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5437 class_for_each_device(®ulator_class, NULL, NULL,
5438 regulator_summary_unlock_one);
5439 ww_acquire_fini(ww_ctx);
5441 mutex_unlock(®ulator_list_mutex);
5444 static int regulator_summary_show_roots(struct device *dev, void *data)
5446 struct regulator_dev *rdev = dev_to_rdev(dev);
5447 struct seq_file *s = data;
5450 regulator_summary_show_subtree(s, rdev, 0);
5455 static int regulator_summary_show(struct seq_file *s, void *data)
5457 struct ww_acquire_ctx ww_ctx;
5459 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5460 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5462 regulator_summary_lock(&ww_ctx);
5464 class_for_each_device(®ulator_class, NULL, s,
5465 regulator_summary_show_roots);
5467 regulator_summary_unlock(&ww_ctx);
5472 static int regulator_summary_open(struct inode *inode, struct file *file)
5474 return single_open(file, regulator_summary_show, inode->i_private);
5478 static const struct file_operations regulator_summary_fops = {
5479 #ifdef CONFIG_DEBUG_FS
5480 .open = regulator_summary_open,
5482 .llseek = seq_lseek,
5483 .release = single_release,
5487 static int __init regulator_init(void)
5491 ret = class_register(®ulator_class);
5493 debugfs_root = debugfs_create_dir("regulator", NULL);
5495 pr_warn("regulator: Failed to create debugfs directory\n");
5497 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5500 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5501 NULL, ®ulator_summary_fops);
5503 regulator_dummy_init();
5508 /* init early to allow our consumers to complete system booting */
5509 core_initcall(regulator_init);
5511 static int __init regulator_late_cleanup(struct device *dev, void *data)
5513 struct regulator_dev *rdev = dev_to_rdev(dev);
5514 const struct regulator_ops *ops = rdev->desc->ops;
5515 struct regulation_constraints *c = rdev->constraints;
5518 if (c && c->always_on)
5521 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5524 regulator_lock(rdev);
5526 if (rdev->use_count)
5529 /* If we can't read the status assume it's on. */
5530 if (ops->is_enabled)
5531 enabled = ops->is_enabled(rdev);
5538 if (have_full_constraints()) {
5539 /* We log since this may kill the system if it goes
5541 rdev_info(rdev, "disabling\n");
5542 ret = _regulator_do_disable(rdev);
5544 rdev_err(rdev, "couldn't disable: %d\n", ret);
5546 /* The intention is that in future we will
5547 * assume that full constraints are provided
5548 * so warn even if we aren't going to do
5551 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5555 regulator_unlock(rdev);
5560 static int __init regulator_init_complete(void)
5563 * Since DT doesn't provide an idiomatic mechanism for
5564 * enabling full constraints and since it's much more natural
5565 * with DT to provide them just assume that a DT enabled
5566 * system has full constraints.
5568 if (of_have_populated_dt())
5569 has_full_constraints = true;
5572 * Regulators may had failed to resolve their input supplies
5573 * when were registered, either because the input supply was
5574 * not registered yet or because its parent device was not
5575 * bound yet. So attempt to resolve the input supplies for
5576 * pending regulators before trying to disable unused ones.
5578 class_for_each_device(®ulator_class, NULL, NULL,
5579 regulator_register_resolve_supply);
5581 /* If we have a full configuration then disable any regulators
5582 * we have permission to change the status for and which are
5583 * not in use or always_on. This is effectively the default
5584 * for DT and ACPI as they have full constraints.
5586 class_for_each_device(®ulator_class, NULL, NULL,
5587 regulator_late_cleanup);
5591 late_initcall_sync(regulator_init_complete);