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);
214 EXPORT_SYMBOL_GPL(regulator_lock);
217 * regulator_unlock - unlock a single regulator
218 * @rdev: regulator_source
220 * This function unlocks the mutex when the
221 * reference counter reaches 0.
223 void regulator_unlock(struct regulator_dev *rdev)
225 mutex_lock(®ulator_nesting_mutex);
227 if (--rdev->ref_cnt == 0) {
228 rdev->mutex_owner = NULL;
229 ww_mutex_unlock(&rdev->mutex);
232 WARN_ON_ONCE(rdev->ref_cnt < 0);
234 mutex_unlock(®ulator_nesting_mutex);
236 EXPORT_SYMBOL_GPL(regulator_unlock);
238 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
240 struct regulator_dev *c_rdev;
243 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
244 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
246 if (rdev->supply->rdev == c_rdev)
253 static void regulator_unlock_recursive(struct regulator_dev *rdev,
254 unsigned int n_coupled)
256 struct regulator_dev *c_rdev;
259 for (i = n_coupled; i > 0; i--) {
260 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
265 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
266 regulator_unlock_recursive(
267 c_rdev->supply->rdev,
268 c_rdev->coupling_desc.n_coupled);
270 regulator_unlock(c_rdev);
274 static int regulator_lock_recursive(struct regulator_dev *rdev,
275 struct regulator_dev **new_contended_rdev,
276 struct regulator_dev **old_contended_rdev,
277 struct ww_acquire_ctx *ww_ctx)
279 struct regulator_dev *c_rdev;
282 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
283 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
288 if (c_rdev != *old_contended_rdev) {
289 err = regulator_lock_nested(c_rdev, ww_ctx);
291 if (err == -EDEADLK) {
292 *new_contended_rdev = c_rdev;
296 /* shouldn't happen */
297 WARN_ON_ONCE(err != -EALREADY);
300 *old_contended_rdev = NULL;
303 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
304 err = regulator_lock_recursive(c_rdev->supply->rdev,
309 regulator_unlock(c_rdev);
318 regulator_unlock_recursive(rdev, i);
324 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
326 * @rdev: regulator source
327 * @ww_ctx: w/w mutex acquire context
329 * Unlock all regulators related with rdev by coupling or suppling.
331 static void regulator_unlock_dependent(struct regulator_dev *rdev,
332 struct ww_acquire_ctx *ww_ctx)
334 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
335 ww_acquire_fini(ww_ctx);
339 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
340 * @rdev: regulator source
341 * @ww_ctx: w/w mutex acquire context
343 * This function as a wrapper on regulator_lock_recursive(), which locks
344 * all regulators related with rdev by coupling or suppling.
346 static void regulator_lock_dependent(struct regulator_dev *rdev,
347 struct ww_acquire_ctx *ww_ctx)
349 struct regulator_dev *new_contended_rdev = NULL;
350 struct regulator_dev *old_contended_rdev = NULL;
353 mutex_lock(®ulator_list_mutex);
355 ww_acquire_init(ww_ctx, ®ulator_ww_class);
358 if (new_contended_rdev) {
359 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
360 old_contended_rdev = new_contended_rdev;
361 old_contended_rdev->ref_cnt++;
364 err = regulator_lock_recursive(rdev,
369 if (old_contended_rdev)
370 regulator_unlock(old_contended_rdev);
372 } while (err == -EDEADLK);
374 ww_acquire_done(ww_ctx);
376 mutex_unlock(®ulator_list_mutex);
380 * of_get_child_regulator - get a child regulator device node
381 * based on supply name
382 * @parent: Parent device node
383 * @prop_name: Combination regulator supply name and "-supply"
385 * Traverse all child nodes.
386 * Extract the child regulator device node corresponding to the supply name.
387 * returns the device node corresponding to the regulator if found, else
390 static struct device_node *of_get_child_regulator(struct device_node *parent,
391 const char *prop_name)
393 struct device_node *regnode = NULL;
394 struct device_node *child = NULL;
396 for_each_child_of_node(parent, child) {
397 regnode = of_parse_phandle(child, prop_name, 0);
400 regnode = of_get_child_regulator(child, prop_name);
411 * of_get_regulator - get a regulator device node based on supply name
412 * @dev: Device pointer for the consumer (of regulator) device
413 * @supply: regulator supply name
415 * Extract the regulator device node corresponding to the supply name.
416 * returns the device node corresponding to the regulator if found, else
419 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
421 struct device_node *regnode = NULL;
422 char prop_name[32]; /* 32 is max size of property name */
424 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
426 snprintf(prop_name, 32, "%s-supply", supply);
427 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
430 regnode = of_get_child_regulator(dev->of_node, prop_name);
434 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
435 prop_name, dev->of_node);
441 /* Platform voltage constraint check */
442 static int regulator_check_voltage(struct regulator_dev *rdev,
443 int *min_uV, int *max_uV)
445 BUG_ON(*min_uV > *max_uV);
447 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
448 rdev_err(rdev, "voltage operation not allowed\n");
452 if (*max_uV > rdev->constraints->max_uV)
453 *max_uV = rdev->constraints->max_uV;
454 if (*min_uV < rdev->constraints->min_uV)
455 *min_uV = rdev->constraints->min_uV;
457 if (*min_uV > *max_uV) {
458 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
466 /* return 0 if the state is valid */
467 static int regulator_check_states(suspend_state_t state)
469 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
472 /* Make sure we select a voltage that suits the needs of all
473 * regulator consumers
475 static int regulator_check_consumers(struct regulator_dev *rdev,
476 int *min_uV, int *max_uV,
477 suspend_state_t state)
479 struct regulator *regulator;
480 struct regulator_voltage *voltage;
482 list_for_each_entry(regulator, &rdev->consumer_list, list) {
483 voltage = ®ulator->voltage[state];
485 * Assume consumers that didn't say anything are OK
486 * with anything in the constraint range.
488 if (!voltage->min_uV && !voltage->max_uV)
491 if (*max_uV > voltage->max_uV)
492 *max_uV = voltage->max_uV;
493 if (*min_uV < voltage->min_uV)
494 *min_uV = voltage->min_uV;
497 if (*min_uV > *max_uV) {
498 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
506 /* current constraint check */
507 static int regulator_check_current_limit(struct regulator_dev *rdev,
508 int *min_uA, int *max_uA)
510 BUG_ON(*min_uA > *max_uA);
512 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
513 rdev_err(rdev, "current operation not allowed\n");
517 if (*max_uA > rdev->constraints->max_uA)
518 *max_uA = rdev->constraints->max_uA;
519 if (*min_uA < rdev->constraints->min_uA)
520 *min_uA = rdev->constraints->min_uA;
522 if (*min_uA > *max_uA) {
523 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
531 /* operating mode constraint check */
532 static int regulator_mode_constrain(struct regulator_dev *rdev,
536 case REGULATOR_MODE_FAST:
537 case REGULATOR_MODE_NORMAL:
538 case REGULATOR_MODE_IDLE:
539 case REGULATOR_MODE_STANDBY:
542 rdev_err(rdev, "invalid mode %x specified\n", *mode);
546 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
547 rdev_err(rdev, "mode operation not allowed\n");
551 /* The modes are bitmasks, the most power hungry modes having
552 * the lowest values. If the requested mode isn't supported
553 * try higher modes. */
555 if (rdev->constraints->valid_modes_mask & *mode)
563 static inline struct regulator_state *
564 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
566 if (rdev->constraints == NULL)
570 case PM_SUSPEND_STANDBY:
571 return &rdev->constraints->state_standby;
573 return &rdev->constraints->state_mem;
575 return &rdev->constraints->state_disk;
581 static ssize_t regulator_uV_show(struct device *dev,
582 struct device_attribute *attr, char *buf)
584 struct regulator_dev *rdev = dev_get_drvdata(dev);
587 regulator_lock(rdev);
588 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
589 regulator_unlock(rdev);
593 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
595 static ssize_t regulator_uA_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
598 struct regulator_dev *rdev = dev_get_drvdata(dev);
600 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
602 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
604 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
607 struct regulator_dev *rdev = dev_get_drvdata(dev);
609 return sprintf(buf, "%s\n", rdev_get_name(rdev));
611 static DEVICE_ATTR_RO(name);
613 static const char *regulator_opmode_to_str(int mode)
616 case REGULATOR_MODE_FAST:
618 case REGULATOR_MODE_NORMAL:
620 case REGULATOR_MODE_IDLE:
622 case REGULATOR_MODE_STANDBY:
628 static ssize_t regulator_print_opmode(char *buf, int mode)
630 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
633 static ssize_t regulator_opmode_show(struct device *dev,
634 struct device_attribute *attr, char *buf)
636 struct regulator_dev *rdev = dev_get_drvdata(dev);
638 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
640 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
642 static ssize_t regulator_print_state(char *buf, int state)
645 return sprintf(buf, "enabled\n");
647 return sprintf(buf, "disabled\n");
649 return sprintf(buf, "unknown\n");
652 static ssize_t regulator_state_show(struct device *dev,
653 struct device_attribute *attr, char *buf)
655 struct regulator_dev *rdev = dev_get_drvdata(dev);
658 regulator_lock(rdev);
659 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
660 regulator_unlock(rdev);
664 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
666 static ssize_t regulator_status_show(struct device *dev,
667 struct device_attribute *attr, char *buf)
669 struct regulator_dev *rdev = dev_get_drvdata(dev);
673 status = rdev->desc->ops->get_status(rdev);
678 case REGULATOR_STATUS_OFF:
681 case REGULATOR_STATUS_ON:
684 case REGULATOR_STATUS_ERROR:
687 case REGULATOR_STATUS_FAST:
690 case REGULATOR_STATUS_NORMAL:
693 case REGULATOR_STATUS_IDLE:
696 case REGULATOR_STATUS_STANDBY:
699 case REGULATOR_STATUS_BYPASS:
702 case REGULATOR_STATUS_UNDEFINED:
709 return sprintf(buf, "%s\n", label);
711 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
713 static ssize_t regulator_min_uA_show(struct device *dev,
714 struct device_attribute *attr, char *buf)
716 struct regulator_dev *rdev = dev_get_drvdata(dev);
718 if (!rdev->constraints)
719 return sprintf(buf, "constraint not defined\n");
721 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
723 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
725 static ssize_t regulator_max_uA_show(struct device *dev,
726 struct device_attribute *attr, char *buf)
728 struct regulator_dev *rdev = dev_get_drvdata(dev);
730 if (!rdev->constraints)
731 return sprintf(buf, "constraint not defined\n");
733 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
735 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
737 static ssize_t regulator_min_uV_show(struct device *dev,
738 struct device_attribute *attr, char *buf)
740 struct regulator_dev *rdev = dev_get_drvdata(dev);
742 if (!rdev->constraints)
743 return sprintf(buf, "constraint not defined\n");
745 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
747 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
749 static ssize_t regulator_max_uV_show(struct device *dev,
750 struct device_attribute *attr, char *buf)
752 struct regulator_dev *rdev = dev_get_drvdata(dev);
754 if (!rdev->constraints)
755 return sprintf(buf, "constraint not defined\n");
757 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
759 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
761 static ssize_t regulator_total_uA_show(struct device *dev,
762 struct device_attribute *attr, char *buf)
764 struct regulator_dev *rdev = dev_get_drvdata(dev);
765 struct regulator *regulator;
768 regulator_lock(rdev);
769 list_for_each_entry(regulator, &rdev->consumer_list, list) {
770 if (regulator->enable_count)
771 uA += regulator->uA_load;
773 regulator_unlock(rdev);
774 return sprintf(buf, "%d\n", uA);
776 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
778 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
781 struct regulator_dev *rdev = dev_get_drvdata(dev);
782 return sprintf(buf, "%d\n", rdev->use_count);
784 static DEVICE_ATTR_RO(num_users);
786 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
789 struct regulator_dev *rdev = dev_get_drvdata(dev);
791 switch (rdev->desc->type) {
792 case REGULATOR_VOLTAGE:
793 return sprintf(buf, "voltage\n");
794 case REGULATOR_CURRENT:
795 return sprintf(buf, "current\n");
797 return sprintf(buf, "unknown\n");
799 static DEVICE_ATTR_RO(type);
801 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
802 struct device_attribute *attr, char *buf)
804 struct regulator_dev *rdev = dev_get_drvdata(dev);
806 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
808 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
809 regulator_suspend_mem_uV_show, NULL);
811 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
812 struct device_attribute *attr, char *buf)
814 struct regulator_dev *rdev = dev_get_drvdata(dev);
816 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
818 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
819 regulator_suspend_disk_uV_show, NULL);
821 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
822 struct device_attribute *attr, char *buf)
824 struct regulator_dev *rdev = dev_get_drvdata(dev);
826 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
828 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
829 regulator_suspend_standby_uV_show, NULL);
831 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
832 struct device_attribute *attr, char *buf)
834 struct regulator_dev *rdev = dev_get_drvdata(dev);
836 return regulator_print_opmode(buf,
837 rdev->constraints->state_mem.mode);
839 static DEVICE_ATTR(suspend_mem_mode, 0444,
840 regulator_suspend_mem_mode_show, NULL);
842 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
843 struct device_attribute *attr, char *buf)
845 struct regulator_dev *rdev = dev_get_drvdata(dev);
847 return regulator_print_opmode(buf,
848 rdev->constraints->state_disk.mode);
850 static DEVICE_ATTR(suspend_disk_mode, 0444,
851 regulator_suspend_disk_mode_show, NULL);
853 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
854 struct device_attribute *attr, char *buf)
856 struct regulator_dev *rdev = dev_get_drvdata(dev);
858 return regulator_print_opmode(buf,
859 rdev->constraints->state_standby.mode);
861 static DEVICE_ATTR(suspend_standby_mode, 0444,
862 regulator_suspend_standby_mode_show, NULL);
864 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
865 struct device_attribute *attr, char *buf)
867 struct regulator_dev *rdev = dev_get_drvdata(dev);
869 return regulator_print_state(buf,
870 rdev->constraints->state_mem.enabled);
872 static DEVICE_ATTR(suspend_mem_state, 0444,
873 regulator_suspend_mem_state_show, NULL);
875 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
876 struct device_attribute *attr, char *buf)
878 struct regulator_dev *rdev = dev_get_drvdata(dev);
880 return regulator_print_state(buf,
881 rdev->constraints->state_disk.enabled);
883 static DEVICE_ATTR(suspend_disk_state, 0444,
884 regulator_suspend_disk_state_show, NULL);
886 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
887 struct device_attribute *attr, char *buf)
889 struct regulator_dev *rdev = dev_get_drvdata(dev);
891 return regulator_print_state(buf,
892 rdev->constraints->state_standby.enabled);
894 static DEVICE_ATTR(suspend_standby_state, 0444,
895 regulator_suspend_standby_state_show, NULL);
897 static ssize_t regulator_bypass_show(struct device *dev,
898 struct device_attribute *attr, char *buf)
900 struct regulator_dev *rdev = dev_get_drvdata(dev);
905 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
914 return sprintf(buf, "%s\n", report);
916 static DEVICE_ATTR(bypass, 0444,
917 regulator_bypass_show, NULL);
919 /* Calculate the new optimum regulator operating mode based on the new total
920 * consumer load. All locks held by caller */
921 static int drms_uA_update(struct regulator_dev *rdev)
923 struct regulator *sibling;
924 int current_uA = 0, output_uV, input_uV, err;
927 lockdep_assert_held_once(&rdev->mutex.base);
930 * first check to see if we can set modes at all, otherwise just
931 * tell the consumer everything is OK.
933 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
936 if (!rdev->desc->ops->get_optimum_mode &&
937 !rdev->desc->ops->set_load)
940 if (!rdev->desc->ops->set_mode &&
941 !rdev->desc->ops->set_load)
944 /* calc total requested load */
945 list_for_each_entry(sibling, &rdev->consumer_list, list) {
946 if (sibling->enable_count)
947 current_uA += sibling->uA_load;
950 current_uA += rdev->constraints->system_load;
952 if (rdev->desc->ops->set_load) {
953 /* set the optimum mode for our new total regulator load */
954 err = rdev->desc->ops->set_load(rdev, current_uA);
956 rdev_err(rdev, "failed to set load %d\n", current_uA);
958 /* get output voltage */
959 output_uV = _regulator_get_voltage(rdev);
960 if (output_uV <= 0) {
961 rdev_err(rdev, "invalid output voltage found\n");
965 /* get input voltage */
968 input_uV = regulator_get_voltage(rdev->supply);
970 input_uV = rdev->constraints->input_uV;
972 rdev_err(rdev, "invalid input voltage found\n");
976 /* now get the optimum mode for our new total regulator load */
977 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
978 output_uV, current_uA);
980 /* check the new mode is allowed */
981 err = regulator_mode_constrain(rdev, &mode);
983 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
984 current_uA, input_uV, output_uV);
988 err = rdev->desc->ops->set_mode(rdev, mode);
990 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
996 static int suspend_set_state(struct regulator_dev *rdev,
997 suspend_state_t state)
1000 struct regulator_state *rstate;
1002 rstate = regulator_get_suspend_state(rdev, state);
1006 /* If we have no suspend mode configration don't set anything;
1007 * only warn if the driver implements set_suspend_voltage or
1008 * set_suspend_mode callback.
1010 if (rstate->enabled != ENABLE_IN_SUSPEND &&
1011 rstate->enabled != DISABLE_IN_SUSPEND) {
1012 if (rdev->desc->ops->set_suspend_voltage ||
1013 rdev->desc->ops->set_suspend_mode)
1014 rdev_warn(rdev, "No configuration\n");
1018 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1019 rdev->desc->ops->set_suspend_enable)
1020 ret = rdev->desc->ops->set_suspend_enable(rdev);
1021 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1022 rdev->desc->ops->set_suspend_disable)
1023 ret = rdev->desc->ops->set_suspend_disable(rdev);
1024 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1028 rdev_err(rdev, "failed to enabled/disable\n");
1032 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1033 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1035 rdev_err(rdev, "failed to set voltage\n");
1040 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1041 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1043 rdev_err(rdev, "failed to set mode\n");
1051 static void print_constraints(struct regulator_dev *rdev)
1053 struct regulation_constraints *constraints = rdev->constraints;
1055 size_t len = sizeof(buf) - 1;
1059 if (constraints->min_uV && constraints->max_uV) {
1060 if (constraints->min_uV == constraints->max_uV)
1061 count += scnprintf(buf + count, len - count, "%d mV ",
1062 constraints->min_uV / 1000);
1064 count += scnprintf(buf + count, len - count,
1066 constraints->min_uV / 1000,
1067 constraints->max_uV / 1000);
1070 if (!constraints->min_uV ||
1071 constraints->min_uV != constraints->max_uV) {
1072 ret = _regulator_get_voltage(rdev);
1074 count += scnprintf(buf + count, len - count,
1075 "at %d mV ", ret / 1000);
1078 if (constraints->uV_offset)
1079 count += scnprintf(buf + count, len - count, "%dmV offset ",
1080 constraints->uV_offset / 1000);
1082 if (constraints->min_uA && constraints->max_uA) {
1083 if (constraints->min_uA == constraints->max_uA)
1084 count += scnprintf(buf + count, len - count, "%d mA ",
1085 constraints->min_uA / 1000);
1087 count += scnprintf(buf + count, len - count,
1089 constraints->min_uA / 1000,
1090 constraints->max_uA / 1000);
1093 if (!constraints->min_uA ||
1094 constraints->min_uA != constraints->max_uA) {
1095 ret = _regulator_get_current_limit(rdev);
1097 count += scnprintf(buf + count, len - count,
1098 "at %d mA ", ret / 1000);
1101 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1102 count += scnprintf(buf + count, len - count, "fast ");
1103 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1104 count += scnprintf(buf + count, len - count, "normal ");
1105 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1106 count += scnprintf(buf + count, len - count, "idle ");
1107 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1108 count += scnprintf(buf + count, len - count, "standby");
1111 scnprintf(buf, len, "no parameters");
1113 rdev_dbg(rdev, "%s\n", buf);
1115 if ((constraints->min_uV != constraints->max_uV) &&
1116 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1118 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1121 static int machine_constraints_voltage(struct regulator_dev *rdev,
1122 struct regulation_constraints *constraints)
1124 const struct regulator_ops *ops = rdev->desc->ops;
1127 /* do we need to apply the constraint voltage */
1128 if (rdev->constraints->apply_uV &&
1129 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1130 int target_min, target_max;
1131 int current_uV = _regulator_get_voltage(rdev);
1133 if (current_uV == -ENOTRECOVERABLE) {
1134 /* This regulator can't be read and must be initted */
1135 rdev_info(rdev, "Setting %d-%duV\n",
1136 rdev->constraints->min_uV,
1137 rdev->constraints->max_uV);
1138 _regulator_do_set_voltage(rdev,
1139 rdev->constraints->min_uV,
1140 rdev->constraints->max_uV);
1141 current_uV = _regulator_get_voltage(rdev);
1144 if (current_uV < 0) {
1146 "failed to get the current voltage(%d)\n",
1152 * If we're below the minimum voltage move up to the
1153 * minimum voltage, if we're above the maximum voltage
1154 * then move down to the maximum.
1156 target_min = current_uV;
1157 target_max = current_uV;
1159 if (current_uV < rdev->constraints->min_uV) {
1160 target_min = rdev->constraints->min_uV;
1161 target_max = rdev->constraints->min_uV;
1164 if (current_uV > rdev->constraints->max_uV) {
1165 target_min = rdev->constraints->max_uV;
1166 target_max = rdev->constraints->max_uV;
1169 if (target_min != current_uV || target_max != current_uV) {
1170 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1171 current_uV, target_min, target_max);
1172 ret = _regulator_do_set_voltage(
1173 rdev, target_min, target_max);
1176 "failed to apply %d-%duV constraint(%d)\n",
1177 target_min, target_max, ret);
1183 /* constrain machine-level voltage specs to fit
1184 * the actual range supported by this regulator.
1186 if (ops->list_voltage && rdev->desc->n_voltages) {
1187 int count = rdev->desc->n_voltages;
1189 int min_uV = INT_MAX;
1190 int max_uV = INT_MIN;
1191 int cmin = constraints->min_uV;
1192 int cmax = constraints->max_uV;
1194 /* it's safe to autoconfigure fixed-voltage supplies
1195 and the constraints are used by list_voltage. */
1196 if (count == 1 && !cmin) {
1199 constraints->min_uV = cmin;
1200 constraints->max_uV = cmax;
1203 /* voltage constraints are optional */
1204 if ((cmin == 0) && (cmax == 0))
1207 /* else require explicit machine-level constraints */
1208 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1209 rdev_err(rdev, "invalid voltage constraints\n");
1213 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1214 for (i = 0; i < count; i++) {
1217 value = ops->list_voltage(rdev, i);
1221 /* maybe adjust [min_uV..max_uV] */
1222 if (value >= cmin && value < min_uV)
1224 if (value <= cmax && value > max_uV)
1228 /* final: [min_uV..max_uV] valid iff constraints valid */
1229 if (max_uV < min_uV) {
1231 "unsupportable voltage constraints %u-%uuV\n",
1236 /* use regulator's subset of machine constraints */
1237 if (constraints->min_uV < min_uV) {
1238 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1239 constraints->min_uV, min_uV);
1240 constraints->min_uV = min_uV;
1242 if (constraints->max_uV > max_uV) {
1243 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1244 constraints->max_uV, max_uV);
1245 constraints->max_uV = max_uV;
1252 static int machine_constraints_current(struct regulator_dev *rdev,
1253 struct regulation_constraints *constraints)
1255 const struct regulator_ops *ops = rdev->desc->ops;
1258 if (!constraints->min_uA && !constraints->max_uA)
1261 if (constraints->min_uA > constraints->max_uA) {
1262 rdev_err(rdev, "Invalid current constraints\n");
1266 if (!ops->set_current_limit || !ops->get_current_limit) {
1267 rdev_warn(rdev, "Operation of current configuration missing\n");
1271 /* Set regulator current in constraints range */
1272 ret = ops->set_current_limit(rdev, constraints->min_uA,
1273 constraints->max_uA);
1275 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1282 static int _regulator_do_enable(struct regulator_dev *rdev);
1285 * set_machine_constraints - sets regulator constraints
1286 * @rdev: regulator source
1287 * @constraints: constraints to apply
1289 * Allows platform initialisation code to define and constrain
1290 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1291 * Constraints *must* be set by platform code in order for some
1292 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1295 static int set_machine_constraints(struct regulator_dev *rdev,
1296 const struct regulation_constraints *constraints)
1299 const struct regulator_ops *ops = rdev->desc->ops;
1302 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1305 rdev->constraints = kzalloc(sizeof(*constraints),
1307 if (!rdev->constraints)
1310 ret = machine_constraints_voltage(rdev, rdev->constraints);
1314 ret = machine_constraints_current(rdev, rdev->constraints);
1318 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1319 ret = ops->set_input_current_limit(rdev,
1320 rdev->constraints->ilim_uA);
1322 rdev_err(rdev, "failed to set input limit\n");
1327 /* do we need to setup our suspend state */
1328 if (rdev->constraints->initial_state) {
1329 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1331 rdev_err(rdev, "failed to set suspend state\n");
1336 if (rdev->constraints->initial_mode) {
1337 if (!ops->set_mode) {
1338 rdev_err(rdev, "no set_mode operation\n");
1342 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1344 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1347 } else if (rdev->constraints->system_load) {
1349 * We'll only apply the initial system load if an
1350 * initial mode wasn't specified.
1352 drms_uA_update(rdev);
1355 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1356 && ops->set_ramp_delay) {
1357 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1359 rdev_err(rdev, "failed to set ramp_delay\n");
1364 if (rdev->constraints->pull_down && ops->set_pull_down) {
1365 ret = ops->set_pull_down(rdev);
1367 rdev_err(rdev, "failed to set pull down\n");
1372 if (rdev->constraints->soft_start && ops->set_soft_start) {
1373 ret = ops->set_soft_start(rdev);
1375 rdev_err(rdev, "failed to set soft start\n");
1380 if (rdev->constraints->over_current_protection
1381 && ops->set_over_current_protection) {
1382 ret = ops->set_over_current_protection(rdev);
1384 rdev_err(rdev, "failed to set over current protection\n");
1389 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1390 bool ad_state = (rdev->constraints->active_discharge ==
1391 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1393 ret = ops->set_active_discharge(rdev, ad_state);
1395 rdev_err(rdev, "failed to set active discharge\n");
1400 /* If the constraints say the regulator should be on at this point
1401 * and we have control then make sure it is enabled.
1403 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1405 ret = regulator_enable(rdev->supply);
1407 _regulator_put(rdev->supply);
1408 rdev->supply = NULL;
1413 ret = _regulator_do_enable(rdev);
1414 if (ret < 0 && ret != -EINVAL) {
1415 rdev_err(rdev, "failed to enable\n");
1421 print_constraints(rdev);
1426 * set_supply - set regulator supply regulator
1427 * @rdev: regulator name
1428 * @supply_rdev: supply regulator name
1430 * Called by platform initialisation code to set the supply regulator for this
1431 * regulator. This ensures that a regulators supply will also be enabled by the
1432 * core if it's child is enabled.
1434 static int set_supply(struct regulator_dev *rdev,
1435 struct regulator_dev *supply_rdev)
1439 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1441 if (!try_module_get(supply_rdev->owner))
1444 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1445 if (rdev->supply == NULL) {
1449 supply_rdev->open_count++;
1455 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1456 * @rdev: regulator source
1457 * @consumer_dev_name: dev_name() string for device supply applies to
1458 * @supply: symbolic name for supply
1460 * Allows platform initialisation code to map physical regulator
1461 * sources to symbolic names for supplies for use by devices. Devices
1462 * should use these symbolic names to request regulators, avoiding the
1463 * need to provide board-specific regulator names as platform data.
1465 static int set_consumer_device_supply(struct regulator_dev *rdev,
1466 const char *consumer_dev_name,
1469 struct regulator_map *node;
1475 if (consumer_dev_name != NULL)
1480 list_for_each_entry(node, ®ulator_map_list, list) {
1481 if (node->dev_name && consumer_dev_name) {
1482 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1484 } else if (node->dev_name || consumer_dev_name) {
1488 if (strcmp(node->supply, supply) != 0)
1491 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1493 dev_name(&node->regulator->dev),
1494 node->regulator->desc->name,
1496 dev_name(&rdev->dev), rdev_get_name(rdev));
1500 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1504 node->regulator = rdev;
1505 node->supply = supply;
1508 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1509 if (node->dev_name == NULL) {
1515 list_add(&node->list, ®ulator_map_list);
1519 static void unset_regulator_supplies(struct regulator_dev *rdev)
1521 struct regulator_map *node, *n;
1523 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1524 if (rdev == node->regulator) {
1525 list_del(&node->list);
1526 kfree(node->dev_name);
1532 #ifdef CONFIG_DEBUG_FS
1533 static ssize_t constraint_flags_read_file(struct file *file,
1534 char __user *user_buf,
1535 size_t count, loff_t *ppos)
1537 const struct regulator *regulator = file->private_data;
1538 const struct regulation_constraints *c = regulator->rdev->constraints;
1545 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1549 ret = snprintf(buf, PAGE_SIZE,
1553 "ramp_disable: %u\n"
1556 "over_current_protection: %u\n",
1563 c->over_current_protection);
1565 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1573 static const struct file_operations constraint_flags_fops = {
1574 #ifdef CONFIG_DEBUG_FS
1575 .open = simple_open,
1576 .read = constraint_flags_read_file,
1577 .llseek = default_llseek,
1581 #define REG_STR_SIZE 64
1583 static struct regulator *create_regulator(struct regulator_dev *rdev,
1585 const char *supply_name)
1587 struct regulator *regulator;
1588 char buf[REG_STR_SIZE];
1591 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1592 if (regulator == NULL)
1595 regulator_lock(rdev);
1596 regulator->rdev = rdev;
1597 list_add(®ulator->list, &rdev->consumer_list);
1600 regulator->dev = dev;
1602 /* Add a link to the device sysfs entry */
1603 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1604 dev->kobj.name, supply_name);
1605 if (size >= REG_STR_SIZE)
1608 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1609 if (regulator->supply_name == NULL)
1612 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1615 rdev_dbg(rdev, "could not add device link %s err %d\n",
1616 dev->kobj.name, err);
1620 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1621 if (regulator->supply_name == NULL)
1625 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1627 if (!regulator->debugfs) {
1628 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1630 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1631 ®ulator->uA_load);
1632 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1633 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1634 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1635 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1636 debugfs_create_file("constraint_flags", 0444,
1637 regulator->debugfs, regulator,
1638 &constraint_flags_fops);
1642 * Check now if the regulator is an always on regulator - if
1643 * it is then we don't need to do nearly so much work for
1644 * enable/disable calls.
1646 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1647 _regulator_is_enabled(rdev))
1648 regulator->always_on = true;
1650 regulator_unlock(rdev);
1653 list_del(®ulator->list);
1655 regulator_unlock(rdev);
1659 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1661 if (rdev->constraints && rdev->constraints->enable_time)
1662 return rdev->constraints->enable_time;
1663 if (!rdev->desc->ops->enable_time)
1664 return rdev->desc->enable_time;
1665 return rdev->desc->ops->enable_time(rdev);
1668 static struct regulator_supply_alias *regulator_find_supply_alias(
1669 struct device *dev, const char *supply)
1671 struct regulator_supply_alias *map;
1673 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1674 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1680 static void regulator_supply_alias(struct device **dev, const char **supply)
1682 struct regulator_supply_alias *map;
1684 map = regulator_find_supply_alias(*dev, *supply);
1686 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1687 *supply, map->alias_supply,
1688 dev_name(map->alias_dev));
1689 *dev = map->alias_dev;
1690 *supply = map->alias_supply;
1694 static int regulator_match(struct device *dev, const void *data)
1696 struct regulator_dev *r = dev_to_rdev(dev);
1698 return strcmp(rdev_get_name(r), data) == 0;
1701 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1705 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1707 return dev ? dev_to_rdev(dev) : NULL;
1711 * regulator_dev_lookup - lookup a regulator device.
1712 * @dev: device for regulator "consumer".
1713 * @supply: Supply name or regulator ID.
1715 * If successful, returns a struct regulator_dev that corresponds to the name
1716 * @supply and with the embedded struct device refcount incremented by one.
1717 * The refcount must be dropped by calling put_device().
1718 * On failure one of the following ERR-PTR-encoded values is returned:
1719 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1722 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1725 struct regulator_dev *r = NULL;
1726 struct device_node *node;
1727 struct regulator_map *map;
1728 const char *devname = NULL;
1730 regulator_supply_alias(&dev, &supply);
1732 /* first do a dt based lookup */
1733 if (dev && dev->of_node) {
1734 node = of_get_regulator(dev, supply);
1736 r = of_find_regulator_by_node(node);
1741 * We have a node, but there is no device.
1742 * assume it has not registered yet.
1744 return ERR_PTR(-EPROBE_DEFER);
1748 /* if not found, try doing it non-dt way */
1750 devname = dev_name(dev);
1752 mutex_lock(®ulator_list_mutex);
1753 list_for_each_entry(map, ®ulator_map_list, list) {
1754 /* If the mapping has a device set up it must match */
1755 if (map->dev_name &&
1756 (!devname || strcmp(map->dev_name, devname)))
1759 if (strcmp(map->supply, supply) == 0 &&
1760 get_device(&map->regulator->dev)) {
1765 mutex_unlock(®ulator_list_mutex);
1770 r = regulator_lookup_by_name(supply);
1774 return ERR_PTR(-ENODEV);
1777 static int regulator_resolve_supply(struct regulator_dev *rdev)
1779 struct regulator_dev *r;
1780 struct device *dev = rdev->dev.parent;
1783 /* No supply to resovle? */
1784 if (!rdev->supply_name)
1787 /* Supply already resolved? */
1791 r = regulator_dev_lookup(dev, rdev->supply_name);
1795 /* Did the lookup explicitly defer for us? */
1796 if (ret == -EPROBE_DEFER)
1799 if (have_full_constraints()) {
1800 r = dummy_regulator_rdev;
1801 get_device(&r->dev);
1803 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1804 rdev->supply_name, rdev->desc->name);
1805 return -EPROBE_DEFER;
1810 * If the supply's parent device is not the same as the
1811 * regulator's parent device, then ensure the parent device
1812 * is bound before we resolve the supply, in case the parent
1813 * device get probe deferred and unregisters the supply.
1815 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1816 if (!device_is_bound(r->dev.parent)) {
1817 put_device(&r->dev);
1818 return -EPROBE_DEFER;
1822 /* Recursively resolve the supply of the supply */
1823 ret = regulator_resolve_supply(r);
1825 put_device(&r->dev);
1829 ret = set_supply(rdev, r);
1831 put_device(&r->dev);
1836 * In set_machine_constraints() we may have turned this regulator on
1837 * but we couldn't propagate to the supply if it hadn't been resolved
1840 if (rdev->use_count) {
1841 ret = regulator_enable(rdev->supply);
1843 _regulator_put(rdev->supply);
1844 rdev->supply = NULL;
1852 /* Internal regulator request function */
1853 struct regulator *_regulator_get(struct device *dev, const char *id,
1854 enum regulator_get_type get_type)
1856 struct regulator_dev *rdev;
1857 struct regulator *regulator;
1858 const char *devname = dev ? dev_name(dev) : "deviceless";
1861 if (get_type >= MAX_GET_TYPE) {
1862 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1863 return ERR_PTR(-EINVAL);
1867 pr_err("get() with no identifier\n");
1868 return ERR_PTR(-EINVAL);
1871 rdev = regulator_dev_lookup(dev, id);
1873 ret = PTR_ERR(rdev);
1876 * If regulator_dev_lookup() fails with error other
1877 * than -ENODEV our job here is done, we simply return it.
1880 return ERR_PTR(ret);
1882 if (!have_full_constraints()) {
1884 "incomplete constraints, dummy supplies not allowed\n");
1885 return ERR_PTR(-ENODEV);
1891 * Assume that a regulator is physically present and
1892 * enabled, even if it isn't hooked up, and just
1896 "%s supply %s not found, using dummy regulator\n",
1898 rdev = dummy_regulator_rdev;
1899 get_device(&rdev->dev);
1904 "dummy supplies not allowed for exclusive requests\n");
1908 return ERR_PTR(-ENODEV);
1912 if (rdev->exclusive) {
1913 regulator = ERR_PTR(-EPERM);
1914 put_device(&rdev->dev);
1918 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1919 regulator = ERR_PTR(-EBUSY);
1920 put_device(&rdev->dev);
1924 mutex_lock(®ulator_list_mutex);
1925 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1926 mutex_unlock(®ulator_list_mutex);
1929 regulator = ERR_PTR(-EPROBE_DEFER);
1930 put_device(&rdev->dev);
1934 ret = regulator_resolve_supply(rdev);
1936 regulator = ERR_PTR(ret);
1937 put_device(&rdev->dev);
1941 if (!try_module_get(rdev->owner)) {
1942 regulator = ERR_PTR(-EPROBE_DEFER);
1943 put_device(&rdev->dev);
1947 regulator = create_regulator(rdev, dev, id);
1948 if (regulator == NULL) {
1949 regulator = ERR_PTR(-ENOMEM);
1950 put_device(&rdev->dev);
1951 module_put(rdev->owner);
1956 if (get_type == EXCLUSIVE_GET) {
1957 rdev->exclusive = 1;
1959 ret = _regulator_is_enabled(rdev);
1961 rdev->use_count = 1;
1963 rdev->use_count = 0;
1966 device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1972 * regulator_get - lookup and obtain a reference to a regulator.
1973 * @dev: device for regulator "consumer"
1974 * @id: Supply name or regulator ID.
1976 * Returns a struct regulator corresponding to the regulator producer,
1977 * or IS_ERR() condition containing errno.
1979 * Use of supply names configured via regulator_set_device_supply() is
1980 * strongly encouraged. It is recommended that the supply name used
1981 * should match the name used for the supply and/or the relevant
1982 * device pins in the datasheet.
1984 struct regulator *regulator_get(struct device *dev, const char *id)
1986 return _regulator_get(dev, id, NORMAL_GET);
1988 EXPORT_SYMBOL_GPL(regulator_get);
1991 * regulator_get_exclusive - obtain exclusive access to a regulator.
1992 * @dev: device for regulator "consumer"
1993 * @id: Supply name or regulator ID.
1995 * Returns a struct regulator corresponding to the regulator producer,
1996 * or IS_ERR() condition containing errno. Other consumers will be
1997 * unable to obtain this regulator while this reference is held and the
1998 * use count for the regulator will be initialised to reflect the current
1999 * state of the regulator.
2001 * This is intended for use by consumers which cannot tolerate shared
2002 * use of the regulator such as those which need to force the
2003 * regulator off for correct operation of the hardware they are
2006 * Use of supply names configured via regulator_set_device_supply() is
2007 * strongly encouraged. It is recommended that the supply name used
2008 * should match the name used for the supply and/or the relevant
2009 * device pins in the datasheet.
2011 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2013 return _regulator_get(dev, id, EXCLUSIVE_GET);
2015 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2018 * regulator_get_optional - obtain optional access to a regulator.
2019 * @dev: device for regulator "consumer"
2020 * @id: Supply name or regulator ID.
2022 * Returns a struct regulator corresponding to the regulator producer,
2023 * or IS_ERR() condition containing errno.
2025 * This is intended for use by consumers for devices which can have
2026 * some supplies unconnected in normal use, such as some MMC devices.
2027 * It can allow the regulator core to provide stub supplies for other
2028 * supplies requested using normal regulator_get() calls without
2029 * disrupting the operation of drivers that can handle absent
2032 * Use of supply names configured via regulator_set_device_supply() is
2033 * strongly encouraged. It is recommended that the supply name used
2034 * should match the name used for the supply and/or the relevant
2035 * device pins in the datasheet.
2037 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2039 return _regulator_get(dev, id, OPTIONAL_GET);
2041 EXPORT_SYMBOL_GPL(regulator_get_optional);
2043 /* regulator_list_mutex lock held by regulator_put() */
2044 static void _regulator_put(struct regulator *regulator)
2046 struct regulator_dev *rdev;
2048 if (IS_ERR_OR_NULL(regulator))
2051 lockdep_assert_held_once(®ulator_list_mutex);
2053 /* Docs say you must disable before calling regulator_put() */
2054 WARN_ON(regulator->enable_count);
2056 rdev = regulator->rdev;
2058 debugfs_remove_recursive(regulator->debugfs);
2060 if (regulator->dev) {
2062 struct regulator *r;
2064 list_for_each_entry(r, &rdev->consumer_list, list)
2065 if (r->dev == regulator->dev)
2069 device_link_remove(regulator->dev, &rdev->dev);
2071 /* remove any sysfs entries */
2072 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2075 regulator_lock(rdev);
2076 list_del(®ulator->list);
2079 rdev->exclusive = 0;
2080 put_device(&rdev->dev);
2081 regulator_unlock(rdev);
2083 kfree_const(regulator->supply_name);
2086 module_put(rdev->owner);
2090 * regulator_put - "free" the regulator source
2091 * @regulator: regulator source
2093 * Note: drivers must ensure that all regulator_enable calls made on this
2094 * regulator source are balanced by regulator_disable calls prior to calling
2097 void regulator_put(struct regulator *regulator)
2099 mutex_lock(®ulator_list_mutex);
2100 _regulator_put(regulator);
2101 mutex_unlock(®ulator_list_mutex);
2103 EXPORT_SYMBOL_GPL(regulator_put);
2106 * regulator_register_supply_alias - Provide device alias for supply lookup
2108 * @dev: device that will be given as the regulator "consumer"
2109 * @id: Supply name or regulator ID
2110 * @alias_dev: device that should be used to lookup the supply
2111 * @alias_id: Supply name or regulator ID that should be used to lookup the
2114 * All lookups for id on dev will instead be conducted for alias_id on
2117 int regulator_register_supply_alias(struct device *dev, const char *id,
2118 struct device *alias_dev,
2119 const char *alias_id)
2121 struct regulator_supply_alias *map;
2123 map = regulator_find_supply_alias(dev, id);
2127 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2132 map->src_supply = id;
2133 map->alias_dev = alias_dev;
2134 map->alias_supply = alias_id;
2136 list_add(&map->list, ®ulator_supply_alias_list);
2138 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2139 id, dev_name(dev), alias_id, dev_name(alias_dev));
2143 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2146 * regulator_unregister_supply_alias - Remove device alias
2148 * @dev: device that will be given as the regulator "consumer"
2149 * @id: Supply name or regulator ID
2151 * Remove a lookup alias if one exists for id on dev.
2153 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2155 struct regulator_supply_alias *map;
2157 map = regulator_find_supply_alias(dev, id);
2159 list_del(&map->list);
2163 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2166 * regulator_bulk_register_supply_alias - register multiple aliases
2168 * @dev: device that will be given as the regulator "consumer"
2169 * @id: List of supply names or regulator IDs
2170 * @alias_dev: device that should be used to lookup the supply
2171 * @alias_id: List of supply names or regulator IDs that should be used to
2173 * @num_id: Number of aliases to register
2175 * @return 0 on success, an errno on failure.
2177 * This helper function allows drivers to register several supply
2178 * aliases in one operation. If any of the aliases cannot be
2179 * registered any aliases that were registered will be removed
2180 * before returning to the caller.
2182 int regulator_bulk_register_supply_alias(struct device *dev,
2183 const char *const *id,
2184 struct device *alias_dev,
2185 const char *const *alias_id,
2191 for (i = 0; i < num_id; ++i) {
2192 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2202 "Failed to create supply alias %s,%s -> %s,%s\n",
2203 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2206 regulator_unregister_supply_alias(dev, id[i]);
2210 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2213 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2215 * @dev: device that will be given as the regulator "consumer"
2216 * @id: List of supply names or regulator IDs
2217 * @num_id: Number of aliases to unregister
2219 * This helper function allows drivers to unregister several supply
2220 * aliases in one operation.
2222 void regulator_bulk_unregister_supply_alias(struct device *dev,
2223 const char *const *id,
2228 for (i = 0; i < num_id; ++i)
2229 regulator_unregister_supply_alias(dev, id[i]);
2231 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2234 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2235 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2236 const struct regulator_config *config)
2238 struct regulator_enable_gpio *pin;
2239 struct gpio_desc *gpiod;
2242 if (config->ena_gpiod)
2243 gpiod = config->ena_gpiod;
2245 gpiod = gpio_to_desc(config->ena_gpio);
2247 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2248 if (pin->gpiod == gpiod) {
2249 rdev_dbg(rdev, "GPIO %d is already used\n",
2251 goto update_ena_gpio_to_rdev;
2255 if (!config->ena_gpiod) {
2256 ret = gpio_request_one(config->ena_gpio,
2257 GPIOF_DIR_OUT | config->ena_gpio_flags,
2258 rdev_get_name(rdev));
2263 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2265 if (!config->ena_gpiod)
2266 gpio_free(config->ena_gpio);
2271 pin->ena_gpio_invert = config->ena_gpio_invert;
2272 list_add(&pin->list, ®ulator_ena_gpio_list);
2274 update_ena_gpio_to_rdev:
2275 pin->request_count++;
2276 rdev->ena_pin = pin;
2280 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2282 struct regulator_enable_gpio *pin, *n;
2287 /* Free the GPIO only in case of no use */
2288 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2289 if (pin->gpiod == rdev->ena_pin->gpiod) {
2290 if (pin->request_count <= 1) {
2291 pin->request_count = 0;
2292 gpiod_put(pin->gpiod);
2293 list_del(&pin->list);
2295 rdev->ena_pin = NULL;
2298 pin->request_count--;
2305 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2306 * @rdev: regulator_dev structure
2307 * @enable: enable GPIO at initial use?
2309 * GPIO is enabled in case of initial use. (enable_count is 0)
2310 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2312 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2314 struct regulator_enable_gpio *pin = rdev->ena_pin;
2320 /* Enable GPIO at initial use */
2321 if (pin->enable_count == 0)
2322 gpiod_set_value_cansleep(pin->gpiod,
2323 !pin->ena_gpio_invert);
2325 pin->enable_count++;
2327 if (pin->enable_count > 1) {
2328 pin->enable_count--;
2332 /* Disable GPIO if not used */
2333 if (pin->enable_count <= 1) {
2334 gpiod_set_value_cansleep(pin->gpiod,
2335 pin->ena_gpio_invert);
2336 pin->enable_count = 0;
2344 * _regulator_enable_delay - a delay helper function
2345 * @delay: time to delay in microseconds
2347 * Delay for the requested amount of time as per the guidelines in:
2349 * Documentation/timers/timers-howto.txt
2351 * The assumption here is that regulators will never be enabled in
2352 * atomic context and therefore sleeping functions can be used.
2354 static void _regulator_enable_delay(unsigned int delay)
2356 unsigned int ms = delay / 1000;
2357 unsigned int us = delay % 1000;
2361 * For small enough values, handle super-millisecond
2362 * delays in the usleep_range() call below.
2371 * Give the scheduler some room to coalesce with any other
2372 * wakeup sources. For delays shorter than 10 us, don't even
2373 * bother setting up high-resolution timers and just busy-
2377 usleep_range(us, us + 100);
2382 static int _regulator_do_enable(struct regulator_dev *rdev)
2386 /* Query before enabling in case configuration dependent. */
2387 ret = _regulator_get_enable_time(rdev);
2391 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2395 trace_regulator_enable(rdev_get_name(rdev));
2397 if (rdev->desc->off_on_delay) {
2398 /* if needed, keep a distance of off_on_delay from last time
2399 * this regulator was disabled.
2401 unsigned long start_jiffy = jiffies;
2402 unsigned long intended, max_delay, remaining;
2404 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2405 intended = rdev->last_off_jiffy + max_delay;
2407 if (time_before(start_jiffy, intended)) {
2408 /* calc remaining jiffies to deal with one-time
2410 * in case of multiple timer wrapping, either it can be
2411 * detected by out-of-range remaining, or it cannot be
2412 * detected and we gets a panelty of
2413 * _regulator_enable_delay().
2415 remaining = intended - start_jiffy;
2416 if (remaining <= max_delay)
2417 _regulator_enable_delay(
2418 jiffies_to_usecs(remaining));
2422 if (rdev->ena_pin) {
2423 if (!rdev->ena_gpio_state) {
2424 ret = regulator_ena_gpio_ctrl(rdev, true);
2427 rdev->ena_gpio_state = 1;
2429 } else if (rdev->desc->ops->enable) {
2430 ret = rdev->desc->ops->enable(rdev);
2437 /* Allow the regulator to ramp; it would be useful to extend
2438 * this for bulk operations so that the regulators can ramp
2440 trace_regulator_enable_delay(rdev_get_name(rdev));
2442 _regulator_enable_delay(delay);
2444 trace_regulator_enable_complete(rdev_get_name(rdev));
2450 * _regulator_handle_consumer_enable - handle that a consumer enabled
2451 * @regulator: regulator source
2453 * Some things on a regulator consumer (like the contribution towards total
2454 * load on the regulator) only have an effect when the consumer wants the
2455 * regulator enabled. Explained in example with two consumers of the same
2457 * consumer A: set_load(100); => total load = 0
2458 * consumer A: regulator_enable(); => total load = 100
2459 * consumer B: set_load(1000); => total load = 100
2460 * consumer B: regulator_enable(); => total load = 1100
2461 * consumer A: regulator_disable(); => total_load = 1000
2463 * This function (together with _regulator_handle_consumer_disable) is
2464 * responsible for keeping track of the refcount for a given regulator consumer
2465 * and applying / unapplying these things.
2467 * Returns 0 upon no error; -error upon error.
2469 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2471 struct regulator_dev *rdev = regulator->rdev;
2473 lockdep_assert_held_once(&rdev->mutex.base);
2475 regulator->enable_count++;
2476 if (regulator->uA_load && regulator->enable_count == 1)
2477 return drms_uA_update(rdev);
2483 * _regulator_handle_consumer_disable - handle that a consumer disabled
2484 * @regulator: regulator source
2486 * The opposite of _regulator_handle_consumer_enable().
2488 * Returns 0 upon no error; -error upon error.
2490 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2492 struct regulator_dev *rdev = regulator->rdev;
2494 lockdep_assert_held_once(&rdev->mutex.base);
2496 if (!regulator->enable_count) {
2497 rdev_err(rdev, "Underflow of regulator enable count\n");
2501 regulator->enable_count--;
2502 if (regulator->uA_load && regulator->enable_count == 0)
2503 return drms_uA_update(rdev);
2508 /* locks held by regulator_enable() */
2509 static int _regulator_enable(struct regulator *regulator)
2511 struct regulator_dev *rdev = regulator->rdev;
2514 lockdep_assert_held_once(&rdev->mutex.base);
2516 if (rdev->use_count == 0 && rdev->supply) {
2517 ret = _regulator_enable(rdev->supply);
2522 /* balance only if there are regulators coupled */
2523 if (rdev->coupling_desc.n_coupled > 1) {
2524 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2526 goto err_disable_supply;
2529 ret = _regulator_handle_consumer_enable(regulator);
2531 goto err_disable_supply;
2533 if (rdev->use_count == 0) {
2534 /* The regulator may on if it's not switchable or left on */
2535 ret = _regulator_is_enabled(rdev);
2536 if (ret == -EINVAL || ret == 0) {
2537 if (!regulator_ops_is_valid(rdev,
2538 REGULATOR_CHANGE_STATUS)) {
2540 goto err_consumer_disable;
2543 ret = _regulator_do_enable(rdev);
2545 goto err_consumer_disable;
2547 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2549 } else if (ret < 0) {
2550 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2551 goto err_consumer_disable;
2553 /* Fallthrough on positive return values - already enabled */
2560 err_consumer_disable:
2561 _regulator_handle_consumer_disable(regulator);
2564 if (rdev->use_count == 0 && rdev->supply)
2565 _regulator_disable(rdev->supply);
2571 * regulator_enable - enable regulator output
2572 * @regulator: regulator source
2574 * Request that the regulator be enabled with the regulator output at
2575 * the predefined voltage or current value. Calls to regulator_enable()
2576 * must be balanced with calls to regulator_disable().
2578 * NOTE: the output value can be set by other drivers, boot loader or may be
2579 * hardwired in the regulator.
2581 int regulator_enable(struct regulator *regulator)
2583 struct regulator_dev *rdev = regulator->rdev;
2584 struct ww_acquire_ctx ww_ctx;
2587 regulator_lock_dependent(rdev, &ww_ctx);
2588 ret = _regulator_enable(regulator);
2589 regulator_unlock_dependent(rdev, &ww_ctx);
2593 EXPORT_SYMBOL_GPL(regulator_enable);
2595 static int _regulator_do_disable(struct regulator_dev *rdev)
2599 trace_regulator_disable(rdev_get_name(rdev));
2601 if (rdev->ena_pin) {
2602 if (rdev->ena_gpio_state) {
2603 ret = regulator_ena_gpio_ctrl(rdev, false);
2606 rdev->ena_gpio_state = 0;
2609 } else if (rdev->desc->ops->disable) {
2610 ret = rdev->desc->ops->disable(rdev);
2615 /* cares about last_off_jiffy only if off_on_delay is required by
2618 if (rdev->desc->off_on_delay)
2619 rdev->last_off_jiffy = jiffies;
2621 trace_regulator_disable_complete(rdev_get_name(rdev));
2626 /* locks held by regulator_disable() */
2627 static int _regulator_disable(struct regulator *regulator)
2629 struct regulator_dev *rdev = regulator->rdev;
2632 lockdep_assert_held_once(&rdev->mutex.base);
2634 if (WARN(rdev->use_count <= 0,
2635 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2638 /* are we the last user and permitted to disable ? */
2639 if (rdev->use_count == 1 &&
2640 (rdev->constraints && !rdev->constraints->always_on)) {
2642 /* we are last user */
2643 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2644 ret = _notifier_call_chain(rdev,
2645 REGULATOR_EVENT_PRE_DISABLE,
2647 if (ret & NOTIFY_STOP_MASK)
2650 ret = _regulator_do_disable(rdev);
2652 rdev_err(rdev, "failed to disable\n");
2653 _notifier_call_chain(rdev,
2654 REGULATOR_EVENT_ABORT_DISABLE,
2658 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2662 rdev->use_count = 0;
2663 } else if (rdev->use_count > 1) {
2668 ret = _regulator_handle_consumer_disable(regulator);
2670 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2671 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2673 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2674 ret = _regulator_disable(rdev->supply);
2680 * regulator_disable - disable regulator output
2681 * @regulator: regulator source
2683 * Disable the regulator output voltage or current. Calls to
2684 * regulator_enable() must be balanced with calls to
2685 * regulator_disable().
2687 * NOTE: this will only disable the regulator output if no other consumer
2688 * devices have it enabled, the regulator device supports disabling and
2689 * machine constraints permit this operation.
2691 int regulator_disable(struct regulator *regulator)
2693 struct regulator_dev *rdev = regulator->rdev;
2694 struct ww_acquire_ctx ww_ctx;
2697 regulator_lock_dependent(rdev, &ww_ctx);
2698 ret = _regulator_disable(regulator);
2699 regulator_unlock_dependent(rdev, &ww_ctx);
2703 EXPORT_SYMBOL_GPL(regulator_disable);
2705 /* locks held by regulator_force_disable() */
2706 static int _regulator_force_disable(struct regulator_dev *rdev)
2710 lockdep_assert_held_once(&rdev->mutex.base);
2712 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2713 REGULATOR_EVENT_PRE_DISABLE, NULL);
2714 if (ret & NOTIFY_STOP_MASK)
2717 ret = _regulator_do_disable(rdev);
2719 rdev_err(rdev, "failed to force disable\n");
2720 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2721 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2725 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2726 REGULATOR_EVENT_DISABLE, NULL);
2732 * regulator_force_disable - force disable regulator output
2733 * @regulator: regulator source
2735 * Forcibly disable the regulator output voltage or current.
2736 * NOTE: this *will* disable the regulator output even if other consumer
2737 * devices have it enabled. This should be used for situations when device
2738 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2740 int regulator_force_disable(struct regulator *regulator)
2742 struct regulator_dev *rdev = regulator->rdev;
2743 struct ww_acquire_ctx ww_ctx;
2746 regulator_lock_dependent(rdev, &ww_ctx);
2748 ret = _regulator_force_disable(regulator->rdev);
2750 if (rdev->coupling_desc.n_coupled > 1)
2751 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2753 if (regulator->uA_load) {
2754 regulator->uA_load = 0;
2755 ret = drms_uA_update(rdev);
2758 if (rdev->use_count != 0 && rdev->supply)
2759 _regulator_disable(rdev->supply);
2761 regulator_unlock_dependent(rdev, &ww_ctx);
2765 EXPORT_SYMBOL_GPL(regulator_force_disable);
2767 static void regulator_disable_work(struct work_struct *work)
2769 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2771 struct ww_acquire_ctx ww_ctx;
2773 struct regulator *regulator;
2774 int total_count = 0;
2776 regulator_lock_dependent(rdev, &ww_ctx);
2779 * Workqueue functions queue the new work instance while the previous
2780 * work instance is being processed. Cancel the queued work instance
2781 * as the work instance under processing does the job of the queued
2784 cancel_delayed_work(&rdev->disable_work);
2786 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2787 count = regulator->deferred_disables;
2792 total_count += count;
2793 regulator->deferred_disables = 0;
2795 for (i = 0; i < count; i++) {
2796 ret = _regulator_disable(regulator);
2798 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2801 WARN_ON(!total_count);
2803 if (rdev->coupling_desc.n_coupled > 1)
2804 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2806 regulator_unlock_dependent(rdev, &ww_ctx);
2810 * regulator_disable_deferred - disable regulator output with delay
2811 * @regulator: regulator source
2812 * @ms: miliseconds until the regulator is disabled
2814 * Execute regulator_disable() on the regulator after a delay. This
2815 * is intended for use with devices that require some time to quiesce.
2817 * NOTE: this will only disable the regulator output if no other consumer
2818 * devices have it enabled, the regulator device supports disabling and
2819 * machine constraints permit this operation.
2821 int regulator_disable_deferred(struct regulator *regulator, int ms)
2823 struct regulator_dev *rdev = regulator->rdev;
2826 return regulator_disable(regulator);
2828 regulator_lock(rdev);
2829 regulator->deferred_disables++;
2830 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2831 msecs_to_jiffies(ms));
2832 regulator_unlock(rdev);
2836 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2838 static int _regulator_is_enabled(struct regulator_dev *rdev)
2840 /* A GPIO control always takes precedence */
2842 return rdev->ena_gpio_state;
2844 /* If we don't know then assume that the regulator is always on */
2845 if (!rdev->desc->ops->is_enabled)
2848 return rdev->desc->ops->is_enabled(rdev);
2851 static int _regulator_list_voltage(struct regulator_dev *rdev,
2852 unsigned selector, int lock)
2854 const struct regulator_ops *ops = rdev->desc->ops;
2857 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2858 return rdev->desc->fixed_uV;
2860 if (ops->list_voltage) {
2861 if (selector >= rdev->desc->n_voltages)
2864 regulator_lock(rdev);
2865 ret = ops->list_voltage(rdev, selector);
2867 regulator_unlock(rdev);
2868 } else if (rdev->is_switch && rdev->supply) {
2869 ret = _regulator_list_voltage(rdev->supply->rdev,
2876 if (ret < rdev->constraints->min_uV)
2878 else if (ret > rdev->constraints->max_uV)
2886 * regulator_is_enabled - is the regulator output enabled
2887 * @regulator: regulator source
2889 * Returns positive if the regulator driver backing the source/client
2890 * has requested that the device be enabled, zero if it hasn't, else a
2891 * negative errno code.
2893 * Note that the device backing this regulator handle can have multiple
2894 * users, so it might be enabled even if regulator_enable() was never
2895 * called for this particular source.
2897 int regulator_is_enabled(struct regulator *regulator)
2901 if (regulator->always_on)
2904 regulator_lock(regulator->rdev);
2905 ret = _regulator_is_enabled(regulator->rdev);
2906 regulator_unlock(regulator->rdev);
2910 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2913 * regulator_count_voltages - count regulator_list_voltage() selectors
2914 * @regulator: regulator source
2916 * Returns number of selectors, or negative errno. Selectors are
2917 * numbered starting at zero, and typically correspond to bitfields
2918 * in hardware registers.
2920 int regulator_count_voltages(struct regulator *regulator)
2922 struct regulator_dev *rdev = regulator->rdev;
2924 if (rdev->desc->n_voltages)
2925 return rdev->desc->n_voltages;
2927 if (!rdev->is_switch || !rdev->supply)
2930 return regulator_count_voltages(rdev->supply);
2932 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2935 * regulator_list_voltage - enumerate supported voltages
2936 * @regulator: regulator source
2937 * @selector: identify voltage to list
2938 * Context: can sleep
2940 * Returns a voltage that can be passed to @regulator_set_voltage(),
2941 * zero if this selector code can't be used on this system, or a
2944 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2946 return _regulator_list_voltage(regulator->rdev, selector, 1);
2948 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2951 * regulator_get_regmap - get the regulator's register map
2952 * @regulator: regulator source
2954 * Returns the register map for the given regulator, or an ERR_PTR value
2955 * if the regulator doesn't use regmap.
2957 struct regmap *regulator_get_regmap(struct regulator *regulator)
2959 struct regmap *map = regulator->rdev->regmap;
2961 return map ? map : ERR_PTR(-EOPNOTSUPP);
2965 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2966 * @regulator: regulator source
2967 * @vsel_reg: voltage selector register, output parameter
2968 * @vsel_mask: mask for voltage selector bitfield, output parameter
2970 * Returns the hardware register offset and bitmask used for setting the
2971 * regulator voltage. This might be useful when configuring voltage-scaling
2972 * hardware or firmware that can make I2C requests behind the kernel's back,
2975 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2976 * and 0 is returned, otherwise a negative errno is returned.
2978 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2980 unsigned *vsel_mask)
2982 struct regulator_dev *rdev = regulator->rdev;
2983 const struct regulator_ops *ops = rdev->desc->ops;
2985 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2988 *vsel_reg = rdev->desc->vsel_reg;
2989 *vsel_mask = rdev->desc->vsel_mask;
2993 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2996 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2997 * @regulator: regulator source
2998 * @selector: identify voltage to list
3000 * Converts the selector to a hardware-specific voltage selector that can be
3001 * directly written to the regulator registers. The address of the voltage
3002 * register can be determined by calling @regulator_get_hardware_vsel_register.
3004 * On error a negative errno is returned.
3006 int regulator_list_hardware_vsel(struct regulator *regulator,
3009 struct regulator_dev *rdev = regulator->rdev;
3010 const struct regulator_ops *ops = rdev->desc->ops;
3012 if (selector >= rdev->desc->n_voltages)
3014 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
3019 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
3022 * regulator_get_linear_step - return the voltage step size between VSEL values
3023 * @regulator: regulator source
3025 * Returns the voltage step size between VSEL values for linear
3026 * regulators, or return 0 if the regulator isn't a linear regulator.
3028 unsigned int regulator_get_linear_step(struct regulator *regulator)
3030 struct regulator_dev *rdev = regulator->rdev;
3032 return rdev->desc->uV_step;
3034 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3037 * regulator_is_supported_voltage - check if a voltage range can be supported
3039 * @regulator: Regulator to check.
3040 * @min_uV: Minimum required voltage in uV.
3041 * @max_uV: Maximum required voltage in uV.
3043 * Returns a boolean or a negative error code.
3045 int regulator_is_supported_voltage(struct regulator *regulator,
3046 int min_uV, int max_uV)
3048 struct regulator_dev *rdev = regulator->rdev;
3049 int i, voltages, ret;
3051 /* If we can't change voltage check the current voltage */
3052 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3053 ret = regulator_get_voltage(regulator);
3055 return min_uV <= ret && ret <= max_uV;
3060 /* Any voltage within constrains range is fine? */
3061 if (rdev->desc->continuous_voltage_range)
3062 return min_uV >= rdev->constraints->min_uV &&
3063 max_uV <= rdev->constraints->max_uV;
3065 ret = regulator_count_voltages(regulator);
3070 for (i = 0; i < voltages; i++) {
3071 ret = regulator_list_voltage(regulator, i);
3073 if (ret >= min_uV && ret <= max_uV)
3079 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3081 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3084 const struct regulator_desc *desc = rdev->desc;
3086 if (desc->ops->map_voltage)
3087 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3089 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3090 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3092 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3093 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3095 if (desc->ops->list_voltage ==
3096 regulator_list_voltage_pickable_linear_range)
3097 return regulator_map_voltage_pickable_linear_range(rdev,
3100 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3103 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3104 int min_uV, int max_uV,
3107 struct pre_voltage_change_data data;
3110 data.old_uV = _regulator_get_voltage(rdev);
3111 data.min_uV = min_uV;
3112 data.max_uV = max_uV;
3113 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3115 if (ret & NOTIFY_STOP_MASK)
3118 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3122 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3123 (void *)data.old_uV);
3128 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3129 int uV, unsigned selector)
3131 struct pre_voltage_change_data data;
3134 data.old_uV = _regulator_get_voltage(rdev);
3137 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3139 if (ret & NOTIFY_STOP_MASK)
3142 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3146 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3147 (void *)data.old_uV);
3152 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3153 int old_uV, int new_uV)
3155 unsigned int ramp_delay = 0;
3157 if (rdev->constraints->ramp_delay)
3158 ramp_delay = rdev->constraints->ramp_delay;
3159 else if (rdev->desc->ramp_delay)
3160 ramp_delay = rdev->desc->ramp_delay;
3161 else if (rdev->constraints->settling_time)
3162 return rdev->constraints->settling_time;
3163 else if (rdev->constraints->settling_time_up &&
3165 return rdev->constraints->settling_time_up;
3166 else if (rdev->constraints->settling_time_down &&
3168 return rdev->constraints->settling_time_down;
3170 if (ramp_delay == 0) {
3171 rdev_dbg(rdev, "ramp_delay not set\n");
3175 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3178 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3179 int min_uV, int max_uV)
3184 unsigned int selector;
3185 int old_selector = -1;
3186 const struct regulator_ops *ops = rdev->desc->ops;
3187 int old_uV = _regulator_get_voltage(rdev);
3189 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3191 min_uV += rdev->constraints->uV_offset;
3192 max_uV += rdev->constraints->uV_offset;
3195 * If we can't obtain the old selector there is not enough
3196 * info to call set_voltage_time_sel().
3198 if (_regulator_is_enabled(rdev) &&
3199 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3200 old_selector = ops->get_voltage_sel(rdev);
3201 if (old_selector < 0)
3202 return old_selector;
3205 if (ops->set_voltage) {
3206 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3210 if (ops->list_voltage)
3211 best_val = ops->list_voltage(rdev,
3214 best_val = _regulator_get_voltage(rdev);
3217 } else if (ops->set_voltage_sel) {
3218 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3220 best_val = ops->list_voltage(rdev, ret);
3221 if (min_uV <= best_val && max_uV >= best_val) {
3223 if (old_selector == selector)
3226 ret = _regulator_call_set_voltage_sel(
3227 rdev, best_val, selector);
3239 if (ops->set_voltage_time_sel) {
3241 * Call set_voltage_time_sel if successfully obtained
3244 if (old_selector >= 0 && old_selector != selector)
3245 delay = ops->set_voltage_time_sel(rdev, old_selector,
3248 if (old_uV != best_val) {
3249 if (ops->set_voltage_time)
3250 delay = ops->set_voltage_time(rdev, old_uV,
3253 delay = _regulator_set_voltage_time(rdev,
3260 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3264 /* Insert any necessary delays */
3265 if (delay >= 1000) {
3266 mdelay(delay / 1000);
3267 udelay(delay % 1000);
3272 if (best_val >= 0) {
3273 unsigned long data = best_val;
3275 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3280 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3285 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3286 int min_uV, int max_uV, suspend_state_t state)
3288 struct regulator_state *rstate;
3291 rstate = regulator_get_suspend_state(rdev, state);
3295 if (min_uV < rstate->min_uV)
3296 min_uV = rstate->min_uV;
3297 if (max_uV > rstate->max_uV)
3298 max_uV = rstate->max_uV;
3300 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3304 uV = rdev->desc->ops->list_voltage(rdev, sel);
3305 if (uV >= min_uV && uV <= max_uV)
3311 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3312 int min_uV, int max_uV,
3313 suspend_state_t state)
3315 struct regulator_dev *rdev = regulator->rdev;
3316 struct regulator_voltage *voltage = ®ulator->voltage[state];
3318 int old_min_uV, old_max_uV;
3321 /* If we're setting the same range as last time the change
3322 * should be a noop (some cpufreq implementations use the same
3323 * voltage for multiple frequencies, for example).
3325 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3328 /* If we're trying to set a range that overlaps the current voltage,
3329 * return successfully even though the regulator does not support
3330 * changing the voltage.
3332 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3333 current_uV = _regulator_get_voltage(rdev);
3334 if (min_uV <= current_uV && current_uV <= max_uV) {
3335 voltage->min_uV = min_uV;
3336 voltage->max_uV = max_uV;
3342 if (!rdev->desc->ops->set_voltage &&
3343 !rdev->desc->ops->set_voltage_sel) {
3348 /* constraints check */
3349 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3353 /* restore original values in case of error */
3354 old_min_uV = voltage->min_uV;
3355 old_max_uV = voltage->max_uV;
3356 voltage->min_uV = min_uV;
3357 voltage->max_uV = max_uV;
3359 /* for not coupled regulators this will just set the voltage */
3360 ret = regulator_balance_voltage(rdev, state);
3367 voltage->min_uV = old_min_uV;
3368 voltage->max_uV = old_max_uV;
3373 static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3374 int max_uV, suspend_state_t state)
3376 int best_supply_uV = 0;
3377 int supply_change_uV = 0;
3381 regulator_ops_is_valid(rdev->supply->rdev,
3382 REGULATOR_CHANGE_VOLTAGE) &&
3383 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3384 rdev->desc->ops->get_voltage_sel))) {
3385 int current_supply_uV;
3388 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3394 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3395 if (best_supply_uV < 0) {
3396 ret = best_supply_uV;
3400 best_supply_uV += rdev->desc->min_dropout_uV;
3402 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3403 if (current_supply_uV < 0) {
3404 ret = current_supply_uV;
3408 supply_change_uV = best_supply_uV - current_supply_uV;
3411 if (supply_change_uV > 0) {
3412 ret = regulator_set_voltage_unlocked(rdev->supply,
3413 best_supply_uV, INT_MAX, state);
3415 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3421 if (state == PM_SUSPEND_ON)
3422 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3424 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3429 if (supply_change_uV < 0) {
3430 ret = regulator_set_voltage_unlocked(rdev->supply,
3431 best_supply_uV, INT_MAX, state);
3433 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3435 /* No need to fail here */
3443 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3444 int *current_uV, int *min_uV)
3446 struct regulation_constraints *constraints = rdev->constraints;
3448 /* Limit voltage change only if necessary */
3449 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3452 if (*current_uV < 0) {
3453 *current_uV = _regulator_get_voltage(rdev);
3455 if (*current_uV < 0)
3459 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3462 /* Clamp target voltage within the given step */
3463 if (*current_uV < *min_uV)
3464 *min_uV = min(*current_uV + constraints->max_uV_step,
3467 *min_uV = max(*current_uV - constraints->max_uV_step,
3473 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3475 int *min_uV, int *max_uV,
3476 suspend_state_t state,
3479 struct coupling_desc *c_desc = &rdev->coupling_desc;
3480 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3481 struct regulation_constraints *constraints = rdev->constraints;
3482 int max_spread = constraints->max_spread;
3483 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3484 int max_current_uV = 0, min_current_uV = INT_MAX;
3485 int highest_min_uV = 0, target_uV, possible_uV;
3492 * If there are no coupled regulators, simply set the voltage
3493 * demanded by consumers.
3495 if (n_coupled == 1) {
3497 * If consumers don't provide any demands, set voltage
3500 desired_min_uV = constraints->min_uV;
3501 desired_max_uV = constraints->max_uV;
3503 ret = regulator_check_consumers(rdev,
3505 &desired_max_uV, state);
3509 possible_uV = desired_min_uV;
3515 /* Find highest min desired voltage */
3516 for (i = 0; i < n_coupled; i++) {
3518 int tmp_max = INT_MAX;
3520 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3522 ret = regulator_check_consumers(c_rdevs[i],
3528 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3532 highest_min_uV = max(highest_min_uV, tmp_min);
3535 desired_min_uV = tmp_min;
3536 desired_max_uV = tmp_max;
3541 * Let target_uV be equal to the desired one if possible.
3542 * If not, set it to minimum voltage, allowed by other coupled
3545 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3548 * Find min and max voltages, which currently aren't violating
3551 for (i = 1; i < n_coupled; i++) {
3554 if (!_regulator_is_enabled(c_rdevs[i]))
3557 tmp_act = _regulator_get_voltage(c_rdevs[i]);
3561 min_current_uV = min(tmp_act, min_current_uV);
3562 max_current_uV = max(tmp_act, max_current_uV);
3565 /* There aren't any other regulators enabled */
3566 if (max_current_uV == 0) {
3567 possible_uV = target_uV;
3570 * Correct target voltage, so as it currently isn't
3571 * violating max_spread
3573 possible_uV = max(target_uV, max_current_uV - max_spread);
3574 possible_uV = min(possible_uV, min_current_uV + max_spread);
3577 if (possible_uV > desired_max_uV)
3580 done = (possible_uV == target_uV);
3581 desired_min_uV = possible_uV;
3584 /* Apply max_uV_step constraint if necessary */
3585 if (state == PM_SUSPEND_ON) {
3586 ret = regulator_limit_voltage_step(rdev, current_uV,
3595 /* Set current_uV if wasn't done earlier in the code and if necessary */
3596 if (n_coupled > 1 && *current_uV == -1) {
3598 if (_regulator_is_enabled(rdev)) {
3599 ret = _regulator_get_voltage(rdev);
3605 *current_uV = desired_min_uV;
3609 *min_uV = desired_min_uV;
3610 *max_uV = desired_max_uV;
3615 static int regulator_balance_voltage(struct regulator_dev *rdev,
3616 suspend_state_t state)
3618 struct regulator_dev **c_rdevs;
3619 struct regulator_dev *best_rdev;
3620 struct coupling_desc *c_desc = &rdev->coupling_desc;
3621 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3622 bool best_c_rdev_done, c_rdev_done[MAX_COUPLED];
3623 unsigned int delta, best_delta;
3625 c_rdevs = c_desc->coupled_rdevs;
3626 n_coupled = c_desc->n_coupled;
3629 * If system is in a state other than PM_SUSPEND_ON, don't check
3630 * other coupled regulators.
3632 if (state != PM_SUSPEND_ON)
3635 if (c_desc->n_resolved < n_coupled) {
3636 rdev_err(rdev, "Not all coupled regulators registered\n");
3640 for (i = 0; i < n_coupled; i++)
3641 c_rdev_done[i] = false;
3644 * Find the best possible voltage change on each loop. Leave the loop
3645 * if there isn't any possible change.
3648 best_c_rdev_done = false;
3656 * Find highest difference between optimal voltage
3657 * and current voltage.
3659 for (i = 0; i < n_coupled; i++) {
3661 * optimal_uV is the best voltage that can be set for
3662 * i-th regulator at the moment without violating
3663 * max_spread constraint in order to balance
3664 * the coupled voltages.
3666 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3671 ret = regulator_get_optimal_voltage(c_rdevs[i],
3679 delta = abs(optimal_uV - current_uV);
3681 if (delta && best_delta <= delta) {
3682 best_c_rdev_done = ret;
3684 best_rdev = c_rdevs[i];
3685 best_min_uV = optimal_uV;
3686 best_max_uV = optimal_max_uV;
3691 /* Nothing to change, return successfully */
3697 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3698 best_max_uV, state);
3703 c_rdev_done[best_c_rdev] = best_c_rdev_done;
3705 } while (n_coupled > 1);
3712 * regulator_set_voltage - set regulator output voltage
3713 * @regulator: regulator source
3714 * @min_uV: Minimum required voltage in uV
3715 * @max_uV: Maximum acceptable voltage in uV
3717 * Sets a voltage regulator to the desired output voltage. This can be set
3718 * during any regulator state. IOW, regulator can be disabled or enabled.
3720 * If the regulator is enabled then the voltage will change to the new value
3721 * immediately otherwise if the regulator is disabled the regulator will
3722 * output at the new voltage when enabled.
3724 * NOTE: If the regulator is shared between several devices then the lowest
3725 * request voltage that meets the system constraints will be used.
3726 * Regulator system constraints must be set for this regulator before
3727 * calling this function otherwise this call will fail.
3729 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3731 struct ww_acquire_ctx ww_ctx;
3734 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3736 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3739 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3743 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3745 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3746 suspend_state_t state, bool en)
3748 struct regulator_state *rstate;
3750 rstate = regulator_get_suspend_state(rdev, state);
3754 if (!rstate->changeable)
3757 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3762 int regulator_suspend_enable(struct regulator_dev *rdev,
3763 suspend_state_t state)
3765 return regulator_suspend_toggle(rdev, state, true);
3767 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3769 int regulator_suspend_disable(struct regulator_dev *rdev,
3770 suspend_state_t state)
3772 struct regulator *regulator;
3773 struct regulator_voltage *voltage;
3776 * if any consumer wants this regulator device keeping on in
3777 * suspend states, don't set it as disabled.
3779 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3780 voltage = ®ulator->voltage[state];
3781 if (voltage->min_uV || voltage->max_uV)
3785 return regulator_suspend_toggle(rdev, state, false);
3787 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3789 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3790 int min_uV, int max_uV,
3791 suspend_state_t state)
3793 struct regulator_dev *rdev = regulator->rdev;
3794 struct regulator_state *rstate;
3796 rstate = regulator_get_suspend_state(rdev, state);
3800 if (rstate->min_uV == rstate->max_uV) {
3801 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3805 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3808 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3809 int max_uV, suspend_state_t state)
3811 struct ww_acquire_ctx ww_ctx;
3814 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3815 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3818 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3820 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3823 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3827 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3830 * regulator_set_voltage_time - get raise/fall time
3831 * @regulator: regulator source
3832 * @old_uV: starting voltage in microvolts
3833 * @new_uV: target voltage in microvolts
3835 * Provided with the starting and ending voltage, this function attempts to
3836 * calculate the time in microseconds required to rise or fall to this new
3839 int regulator_set_voltage_time(struct regulator *regulator,
3840 int old_uV, int new_uV)
3842 struct regulator_dev *rdev = regulator->rdev;
3843 const struct regulator_ops *ops = rdev->desc->ops;
3849 if (ops->set_voltage_time)
3850 return ops->set_voltage_time(rdev, old_uV, new_uV);
3851 else if (!ops->set_voltage_time_sel)
3852 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3854 /* Currently requires operations to do this */
3855 if (!ops->list_voltage || !rdev->desc->n_voltages)
3858 for (i = 0; i < rdev->desc->n_voltages; i++) {
3859 /* We only look for exact voltage matches here */
3860 voltage = regulator_list_voltage(regulator, i);
3865 if (voltage == old_uV)
3867 if (voltage == new_uV)
3871 if (old_sel < 0 || new_sel < 0)
3874 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3876 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3879 * regulator_set_voltage_time_sel - get raise/fall time
3880 * @rdev: regulator source device
3881 * @old_selector: selector for starting voltage
3882 * @new_selector: selector for target voltage
3884 * Provided with the starting and target voltage selectors, this function
3885 * returns time in microseconds required to rise or fall to this new voltage
3887 * Drivers providing ramp_delay in regulation_constraints can use this as their
3888 * set_voltage_time_sel() operation.
3890 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3891 unsigned int old_selector,
3892 unsigned int new_selector)
3894 int old_volt, new_volt;
3897 if (!rdev->desc->ops->list_voltage)
3900 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3901 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3903 if (rdev->desc->ops->set_voltage_time)
3904 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3907 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3909 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3912 * regulator_sync_voltage - re-apply last regulator output voltage
3913 * @regulator: regulator source
3915 * Re-apply the last configured voltage. This is intended to be used
3916 * where some external control source the consumer is cooperating with
3917 * has caused the configured voltage to change.
3919 int regulator_sync_voltage(struct regulator *regulator)
3921 struct regulator_dev *rdev = regulator->rdev;
3922 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
3923 int ret, min_uV, max_uV;
3925 regulator_lock(rdev);
3927 if (!rdev->desc->ops->set_voltage &&
3928 !rdev->desc->ops->set_voltage_sel) {
3933 /* This is only going to work if we've had a voltage configured. */
3934 if (!voltage->min_uV && !voltage->max_uV) {
3939 min_uV = voltage->min_uV;
3940 max_uV = voltage->max_uV;
3942 /* This should be a paranoia check... */
3943 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3947 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3951 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3954 regulator_unlock(rdev);
3957 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3959 static int _regulator_get_voltage(struct regulator_dev *rdev)
3964 if (rdev->desc->ops->get_bypass) {
3965 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3969 /* if bypassed the regulator must have a supply */
3970 if (!rdev->supply) {
3972 "bypassed regulator has no supply!\n");
3973 return -EPROBE_DEFER;
3976 return _regulator_get_voltage(rdev->supply->rdev);
3980 if (rdev->desc->ops->get_voltage_sel) {
3981 sel = rdev->desc->ops->get_voltage_sel(rdev);
3984 ret = rdev->desc->ops->list_voltage(rdev, sel);
3985 } else if (rdev->desc->ops->get_voltage) {
3986 ret = rdev->desc->ops->get_voltage(rdev);
3987 } else if (rdev->desc->ops->list_voltage) {
3988 ret = rdev->desc->ops->list_voltage(rdev, 0);
3989 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3990 ret = rdev->desc->fixed_uV;
3991 } else if (rdev->supply) {
3992 ret = _regulator_get_voltage(rdev->supply->rdev);
3999 return ret - rdev->constraints->uV_offset;
4003 * regulator_get_voltage - get regulator output voltage
4004 * @regulator: regulator source
4006 * This returns the current regulator voltage in uV.
4008 * NOTE: If the regulator is disabled it will return the voltage value. This
4009 * function should not be used to determine regulator state.
4011 int regulator_get_voltage(struct regulator *regulator)
4013 struct ww_acquire_ctx ww_ctx;
4016 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4017 ret = _regulator_get_voltage(regulator->rdev);
4018 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4022 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4025 * regulator_set_current_limit - set regulator output current limit
4026 * @regulator: regulator source
4027 * @min_uA: Minimum supported current in uA
4028 * @max_uA: Maximum supported current in uA
4030 * Sets current sink to the desired output current. This can be set during
4031 * any regulator state. IOW, regulator can be disabled or enabled.
4033 * If the regulator is enabled then the current will change to the new value
4034 * immediately otherwise if the regulator is disabled the regulator will
4035 * output at the new current when enabled.
4037 * NOTE: Regulator system constraints must be set for this regulator before
4038 * calling this function otherwise this call will fail.
4040 int regulator_set_current_limit(struct regulator *regulator,
4041 int min_uA, int max_uA)
4043 struct regulator_dev *rdev = regulator->rdev;
4046 regulator_lock(rdev);
4049 if (!rdev->desc->ops->set_current_limit) {
4054 /* constraints check */
4055 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4059 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4061 regulator_unlock(rdev);
4064 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4066 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4069 if (!rdev->desc->ops->get_current_limit)
4072 return rdev->desc->ops->get_current_limit(rdev);
4075 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4079 regulator_lock(rdev);
4080 ret = _regulator_get_current_limit_unlocked(rdev);
4081 regulator_unlock(rdev);
4087 * regulator_get_current_limit - get regulator output current
4088 * @regulator: regulator source
4090 * This returns the current supplied by the specified current sink in uA.
4092 * NOTE: If the regulator is disabled it will return the current value. This
4093 * function should not be used to determine regulator state.
4095 int regulator_get_current_limit(struct regulator *regulator)
4097 return _regulator_get_current_limit(regulator->rdev);
4099 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4102 * regulator_set_mode - set regulator operating mode
4103 * @regulator: regulator source
4104 * @mode: operating mode - one of the REGULATOR_MODE constants
4106 * Set regulator operating mode to increase regulator efficiency or improve
4107 * regulation performance.
4109 * NOTE: Regulator system constraints must be set for this regulator before
4110 * calling this function otherwise this call will fail.
4112 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4114 struct regulator_dev *rdev = regulator->rdev;
4116 int regulator_curr_mode;
4118 regulator_lock(rdev);
4121 if (!rdev->desc->ops->set_mode) {
4126 /* return if the same mode is requested */
4127 if (rdev->desc->ops->get_mode) {
4128 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4129 if (regulator_curr_mode == mode) {
4135 /* constraints check */
4136 ret = regulator_mode_constrain(rdev, &mode);
4140 ret = rdev->desc->ops->set_mode(rdev, mode);
4142 regulator_unlock(rdev);
4145 EXPORT_SYMBOL_GPL(regulator_set_mode);
4147 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4150 if (!rdev->desc->ops->get_mode)
4153 return rdev->desc->ops->get_mode(rdev);
4156 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4160 regulator_lock(rdev);
4161 ret = _regulator_get_mode_unlocked(rdev);
4162 regulator_unlock(rdev);
4168 * regulator_get_mode - get regulator operating mode
4169 * @regulator: regulator source
4171 * Get the current regulator operating mode.
4173 unsigned int regulator_get_mode(struct regulator *regulator)
4175 return _regulator_get_mode(regulator->rdev);
4177 EXPORT_SYMBOL_GPL(regulator_get_mode);
4179 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4180 unsigned int *flags)
4184 regulator_lock(rdev);
4187 if (!rdev->desc->ops->get_error_flags) {
4192 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4194 regulator_unlock(rdev);
4199 * regulator_get_error_flags - get regulator error information
4200 * @regulator: regulator source
4201 * @flags: pointer to store error flags
4203 * Get the current regulator error information.
4205 int regulator_get_error_flags(struct regulator *regulator,
4206 unsigned int *flags)
4208 return _regulator_get_error_flags(regulator->rdev, flags);
4210 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4213 * regulator_set_load - set regulator load
4214 * @regulator: regulator source
4215 * @uA_load: load current
4217 * Notifies the regulator core of a new device load. This is then used by
4218 * DRMS (if enabled by constraints) to set the most efficient regulator
4219 * operating mode for the new regulator loading.
4221 * Consumer devices notify their supply regulator of the maximum power
4222 * they will require (can be taken from device datasheet in the power
4223 * consumption tables) when they change operational status and hence power
4224 * state. Examples of operational state changes that can affect power
4225 * consumption are :-
4227 * o Device is opened / closed.
4228 * o Device I/O is about to begin or has just finished.
4229 * o Device is idling in between work.
4231 * This information is also exported via sysfs to userspace.
4233 * DRMS will sum the total requested load on the regulator and change
4234 * to the most efficient operating mode if platform constraints allow.
4236 * NOTE: when a regulator consumer requests to have a regulator
4237 * disabled then any load that consumer requested no longer counts
4238 * toward the total requested load. If the regulator is re-enabled
4239 * then the previously requested load will start counting again.
4241 * If a regulator is an always-on regulator then an individual consumer's
4242 * load will still be removed if that consumer is fully disabled.
4244 * On error a negative errno is returned.
4246 int regulator_set_load(struct regulator *regulator, int uA_load)
4248 struct regulator_dev *rdev = regulator->rdev;
4252 regulator_lock(rdev);
4253 old_uA_load = regulator->uA_load;
4254 regulator->uA_load = uA_load;
4255 if (regulator->enable_count && old_uA_load != uA_load) {
4256 ret = drms_uA_update(rdev);
4258 regulator->uA_load = old_uA_load;
4260 regulator_unlock(rdev);
4264 EXPORT_SYMBOL_GPL(regulator_set_load);
4267 * regulator_allow_bypass - allow the regulator to go into bypass mode
4269 * @regulator: Regulator to configure
4270 * @enable: enable or disable bypass mode
4272 * Allow the regulator to go into bypass mode if all other consumers
4273 * for the regulator also enable bypass mode and the machine
4274 * constraints allow this. Bypass mode means that the regulator is
4275 * simply passing the input directly to the output with no regulation.
4277 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4279 struct regulator_dev *rdev = regulator->rdev;
4282 if (!rdev->desc->ops->set_bypass)
4285 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4288 regulator_lock(rdev);
4290 if (enable && !regulator->bypass) {
4291 rdev->bypass_count++;
4293 if (rdev->bypass_count == rdev->open_count) {
4294 ret = rdev->desc->ops->set_bypass(rdev, enable);
4296 rdev->bypass_count--;
4299 } else if (!enable && regulator->bypass) {
4300 rdev->bypass_count--;
4302 if (rdev->bypass_count != rdev->open_count) {
4303 ret = rdev->desc->ops->set_bypass(rdev, enable);
4305 rdev->bypass_count++;
4310 regulator->bypass = enable;
4312 regulator_unlock(rdev);
4316 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4319 * regulator_register_notifier - register regulator event notifier
4320 * @regulator: regulator source
4321 * @nb: notifier block
4323 * Register notifier block to receive regulator events.
4325 int regulator_register_notifier(struct regulator *regulator,
4326 struct notifier_block *nb)
4328 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4331 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4334 * regulator_unregister_notifier - unregister regulator event notifier
4335 * @regulator: regulator source
4336 * @nb: notifier block
4338 * Unregister regulator event notifier block.
4340 int regulator_unregister_notifier(struct regulator *regulator,
4341 struct notifier_block *nb)
4343 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4346 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4348 /* notify regulator consumers and downstream regulator consumers.
4349 * Note mutex must be held by caller.
4351 static int _notifier_call_chain(struct regulator_dev *rdev,
4352 unsigned long event, void *data)
4354 /* call rdev chain first */
4355 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4359 * regulator_bulk_get - get multiple regulator consumers
4361 * @dev: Device to supply
4362 * @num_consumers: Number of consumers to register
4363 * @consumers: Configuration of consumers; clients are stored here.
4365 * @return 0 on success, an errno on failure.
4367 * This helper function allows drivers to get several regulator
4368 * consumers in one operation. If any of the regulators cannot be
4369 * acquired then any regulators that were allocated will be freed
4370 * before returning to the caller.
4372 int regulator_bulk_get(struct device *dev, int num_consumers,
4373 struct regulator_bulk_data *consumers)
4378 for (i = 0; i < num_consumers; i++)
4379 consumers[i].consumer = NULL;
4381 for (i = 0; i < num_consumers; i++) {
4382 consumers[i].consumer = regulator_get(dev,
4383 consumers[i].supply);
4384 if (IS_ERR(consumers[i].consumer)) {
4385 ret = PTR_ERR(consumers[i].consumer);
4386 dev_err(dev, "Failed to get supply '%s': %d\n",
4387 consumers[i].supply, ret);
4388 consumers[i].consumer = NULL;
4397 regulator_put(consumers[i].consumer);
4401 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4403 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4405 struct regulator_bulk_data *bulk = data;
4407 bulk->ret = regulator_enable(bulk->consumer);
4411 * regulator_bulk_enable - enable multiple regulator consumers
4413 * @num_consumers: Number of consumers
4414 * @consumers: Consumer data; clients are stored here.
4415 * @return 0 on success, an errno on failure
4417 * This convenience API allows consumers to enable multiple regulator
4418 * clients in a single API call. If any consumers cannot be enabled
4419 * then any others that were enabled will be disabled again prior to
4422 int regulator_bulk_enable(int num_consumers,
4423 struct regulator_bulk_data *consumers)
4425 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4429 for (i = 0; i < num_consumers; i++) {
4430 async_schedule_domain(regulator_bulk_enable_async,
4431 &consumers[i], &async_domain);
4434 async_synchronize_full_domain(&async_domain);
4436 /* If any consumer failed we need to unwind any that succeeded */
4437 for (i = 0; i < num_consumers; i++) {
4438 if (consumers[i].ret != 0) {
4439 ret = consumers[i].ret;
4447 for (i = 0; i < num_consumers; i++) {
4448 if (consumers[i].ret < 0)
4449 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4452 regulator_disable(consumers[i].consumer);
4457 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4460 * regulator_bulk_disable - disable multiple regulator consumers
4462 * @num_consumers: Number of consumers
4463 * @consumers: Consumer data; clients are stored here.
4464 * @return 0 on success, an errno on failure
4466 * This convenience API allows consumers to disable multiple regulator
4467 * clients in a single API call. If any consumers cannot be disabled
4468 * then any others that were disabled will be enabled again prior to
4471 int regulator_bulk_disable(int num_consumers,
4472 struct regulator_bulk_data *consumers)
4477 for (i = num_consumers - 1; i >= 0; --i) {
4478 ret = regulator_disable(consumers[i].consumer);
4486 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4487 for (++i; i < num_consumers; ++i) {
4488 r = regulator_enable(consumers[i].consumer);
4490 pr_err("Failed to re-enable %s: %d\n",
4491 consumers[i].supply, r);
4496 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4499 * regulator_bulk_force_disable - force disable multiple regulator consumers
4501 * @num_consumers: Number of consumers
4502 * @consumers: Consumer data; clients are stored here.
4503 * @return 0 on success, an errno on failure
4505 * This convenience API allows consumers to forcibly disable multiple regulator
4506 * clients in a single API call.
4507 * NOTE: This should be used for situations when device damage will
4508 * likely occur if the regulators are not disabled (e.g. over temp).
4509 * Although regulator_force_disable function call for some consumers can
4510 * return error numbers, the function is called for all consumers.
4512 int regulator_bulk_force_disable(int num_consumers,
4513 struct regulator_bulk_data *consumers)
4518 for (i = 0; i < num_consumers; i++) {
4520 regulator_force_disable(consumers[i].consumer);
4522 /* Store first error for reporting */
4523 if (consumers[i].ret && !ret)
4524 ret = consumers[i].ret;
4529 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4532 * regulator_bulk_free - free multiple regulator consumers
4534 * @num_consumers: Number of consumers
4535 * @consumers: Consumer data; clients are stored here.
4537 * This convenience API allows consumers to free multiple regulator
4538 * clients in a single API call.
4540 void regulator_bulk_free(int num_consumers,
4541 struct regulator_bulk_data *consumers)
4545 for (i = 0; i < num_consumers; i++) {
4546 regulator_put(consumers[i].consumer);
4547 consumers[i].consumer = NULL;
4550 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4553 * regulator_notifier_call_chain - call regulator event notifier
4554 * @rdev: regulator source
4555 * @event: notifier block
4556 * @data: callback-specific data.
4558 * Called by regulator drivers to notify clients a regulator event has
4559 * occurred. We also notify regulator clients downstream.
4560 * Note lock must be held by caller.
4562 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4563 unsigned long event, void *data)
4565 lockdep_assert_held_once(&rdev->mutex.base);
4567 _notifier_call_chain(rdev, event, data);
4571 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4574 * regulator_mode_to_status - convert a regulator mode into a status
4576 * @mode: Mode to convert
4578 * Convert a regulator mode into a status.
4580 int regulator_mode_to_status(unsigned int mode)
4583 case REGULATOR_MODE_FAST:
4584 return REGULATOR_STATUS_FAST;
4585 case REGULATOR_MODE_NORMAL:
4586 return REGULATOR_STATUS_NORMAL;
4587 case REGULATOR_MODE_IDLE:
4588 return REGULATOR_STATUS_IDLE;
4589 case REGULATOR_MODE_STANDBY:
4590 return REGULATOR_STATUS_STANDBY;
4592 return REGULATOR_STATUS_UNDEFINED;
4595 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4597 static struct attribute *regulator_dev_attrs[] = {
4598 &dev_attr_name.attr,
4599 &dev_attr_num_users.attr,
4600 &dev_attr_type.attr,
4601 &dev_attr_microvolts.attr,
4602 &dev_attr_microamps.attr,
4603 &dev_attr_opmode.attr,
4604 &dev_attr_state.attr,
4605 &dev_attr_status.attr,
4606 &dev_attr_bypass.attr,
4607 &dev_attr_requested_microamps.attr,
4608 &dev_attr_min_microvolts.attr,
4609 &dev_attr_max_microvolts.attr,
4610 &dev_attr_min_microamps.attr,
4611 &dev_attr_max_microamps.attr,
4612 &dev_attr_suspend_standby_state.attr,
4613 &dev_attr_suspend_mem_state.attr,
4614 &dev_attr_suspend_disk_state.attr,
4615 &dev_attr_suspend_standby_microvolts.attr,
4616 &dev_attr_suspend_mem_microvolts.attr,
4617 &dev_attr_suspend_disk_microvolts.attr,
4618 &dev_attr_suspend_standby_mode.attr,
4619 &dev_attr_suspend_mem_mode.attr,
4620 &dev_attr_suspend_disk_mode.attr,
4625 * To avoid cluttering sysfs (and memory) with useless state, only
4626 * create attributes that can be meaningfully displayed.
4628 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4629 struct attribute *attr, int idx)
4631 struct device *dev = kobj_to_dev(kobj);
4632 struct regulator_dev *rdev = dev_to_rdev(dev);
4633 const struct regulator_ops *ops = rdev->desc->ops;
4634 umode_t mode = attr->mode;
4636 /* these three are always present */
4637 if (attr == &dev_attr_name.attr ||
4638 attr == &dev_attr_num_users.attr ||
4639 attr == &dev_attr_type.attr)
4642 /* some attributes need specific methods to be displayed */
4643 if (attr == &dev_attr_microvolts.attr) {
4644 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4645 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4646 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4647 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4652 if (attr == &dev_attr_microamps.attr)
4653 return ops->get_current_limit ? mode : 0;
4655 if (attr == &dev_attr_opmode.attr)
4656 return ops->get_mode ? mode : 0;
4658 if (attr == &dev_attr_state.attr)
4659 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4661 if (attr == &dev_attr_status.attr)
4662 return ops->get_status ? mode : 0;
4664 if (attr == &dev_attr_bypass.attr)
4665 return ops->get_bypass ? mode : 0;
4667 /* constraints need specific supporting methods */
4668 if (attr == &dev_attr_min_microvolts.attr ||
4669 attr == &dev_attr_max_microvolts.attr)
4670 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4672 if (attr == &dev_attr_min_microamps.attr ||
4673 attr == &dev_attr_max_microamps.attr)
4674 return ops->set_current_limit ? mode : 0;
4676 if (attr == &dev_attr_suspend_standby_state.attr ||
4677 attr == &dev_attr_suspend_mem_state.attr ||
4678 attr == &dev_attr_suspend_disk_state.attr)
4681 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4682 attr == &dev_attr_suspend_mem_microvolts.attr ||
4683 attr == &dev_attr_suspend_disk_microvolts.attr)
4684 return ops->set_suspend_voltage ? mode : 0;
4686 if (attr == &dev_attr_suspend_standby_mode.attr ||
4687 attr == &dev_attr_suspend_mem_mode.attr ||
4688 attr == &dev_attr_suspend_disk_mode.attr)
4689 return ops->set_suspend_mode ? mode : 0;
4694 static const struct attribute_group regulator_dev_group = {
4695 .attrs = regulator_dev_attrs,
4696 .is_visible = regulator_attr_is_visible,
4699 static const struct attribute_group *regulator_dev_groups[] = {
4700 ®ulator_dev_group,
4704 static void regulator_dev_release(struct device *dev)
4706 struct regulator_dev *rdev = dev_get_drvdata(dev);
4708 kfree(rdev->constraints);
4709 of_node_put(rdev->dev.of_node);
4713 static void rdev_init_debugfs(struct regulator_dev *rdev)
4715 struct device *parent = rdev->dev.parent;
4716 const char *rname = rdev_get_name(rdev);
4717 char name[NAME_MAX];
4719 /* Avoid duplicate debugfs directory names */
4720 if (parent && rname == rdev->desc->name) {
4721 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4726 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4727 if (!rdev->debugfs) {
4728 rdev_warn(rdev, "Failed to create debugfs directory\n");
4732 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4734 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4736 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4737 &rdev->bypass_count);
4740 static int regulator_register_resolve_supply(struct device *dev, void *data)
4742 struct regulator_dev *rdev = dev_to_rdev(dev);
4744 if (regulator_resolve_supply(rdev))
4745 rdev_dbg(rdev, "unable to resolve supply\n");
4750 static void regulator_resolve_coupling(struct regulator_dev *rdev)
4752 struct coupling_desc *c_desc = &rdev->coupling_desc;
4753 int n_coupled = c_desc->n_coupled;
4754 struct regulator_dev *c_rdev;
4757 for (i = 1; i < n_coupled; i++) {
4758 /* already resolved */
4759 if (c_desc->coupled_rdevs[i])
4762 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4767 regulator_lock(c_rdev);
4769 c_desc->coupled_rdevs[i] = c_rdev;
4770 c_desc->n_resolved++;
4772 regulator_unlock(c_rdev);
4774 regulator_resolve_coupling(c_rdev);
4778 static void regulator_remove_coupling(struct regulator_dev *rdev)
4780 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4781 struct regulator_dev *__c_rdev, *c_rdev;
4782 unsigned int __n_coupled, n_coupled;
4785 n_coupled = c_desc->n_coupled;
4787 for (i = 1; i < n_coupled; i++) {
4788 c_rdev = c_desc->coupled_rdevs[i];
4793 regulator_lock(c_rdev);
4795 __c_desc = &c_rdev->coupling_desc;
4796 __n_coupled = __c_desc->n_coupled;
4798 for (k = 1; k < __n_coupled; k++) {
4799 __c_rdev = __c_desc->coupled_rdevs[k];
4801 if (__c_rdev == rdev) {
4802 __c_desc->coupled_rdevs[k] = NULL;
4803 __c_desc->n_resolved--;
4808 regulator_unlock(c_rdev);
4810 c_desc->coupled_rdevs[i] = NULL;
4811 c_desc->n_resolved--;
4815 static int regulator_init_coupling(struct regulator_dev *rdev)
4819 if (!IS_ENABLED(CONFIG_OF))
4822 n_phandles = of_get_n_coupled(rdev);
4824 if (n_phandles + 1 > MAX_COUPLED) {
4825 rdev_err(rdev, "too many regulators coupled\n");
4830 * Every regulator should always have coupling descriptor filled with
4831 * at least pointer to itself.
4833 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4834 rdev->coupling_desc.n_coupled = n_phandles + 1;
4835 rdev->coupling_desc.n_resolved++;
4837 /* regulator isn't coupled */
4838 if (n_phandles == 0)
4841 /* regulator, which can't change its voltage, can't be coupled */
4842 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
4843 rdev_err(rdev, "voltage operation not allowed\n");
4847 if (rdev->constraints->max_spread <= 0) {
4848 rdev_err(rdev, "wrong max_spread value\n");
4852 if (!of_check_coupling_data(rdev))
4859 * regulator_register - register regulator
4860 * @regulator_desc: regulator to register
4861 * @cfg: runtime configuration for regulator
4863 * Called by regulator drivers to register a regulator.
4864 * Returns a valid pointer to struct regulator_dev on success
4865 * or an ERR_PTR() on error.
4867 struct regulator_dev *
4868 regulator_register(const struct regulator_desc *regulator_desc,
4869 const struct regulator_config *cfg)
4871 const struct regulation_constraints *constraints = NULL;
4872 const struct regulator_init_data *init_data;
4873 struct regulator_config *config = NULL;
4874 static atomic_t regulator_no = ATOMIC_INIT(-1);
4875 struct regulator_dev *rdev;
4876 bool dangling_cfg_gpiod = false;
4877 bool dangling_of_gpiod = false;
4882 return ERR_PTR(-EINVAL);
4884 dangling_cfg_gpiod = true;
4885 if (regulator_desc == NULL) {
4893 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
4898 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4899 regulator_desc->type != REGULATOR_CURRENT) {
4904 /* Only one of each should be implemented */
4905 WARN_ON(regulator_desc->ops->get_voltage &&
4906 regulator_desc->ops->get_voltage_sel);
4907 WARN_ON(regulator_desc->ops->set_voltage &&
4908 regulator_desc->ops->set_voltage_sel);
4910 /* If we're using selectors we must implement list_voltage. */
4911 if (regulator_desc->ops->get_voltage_sel &&
4912 !regulator_desc->ops->list_voltage) {
4916 if (regulator_desc->ops->set_voltage_sel &&
4917 !regulator_desc->ops->list_voltage) {
4922 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4929 * Duplicate the config so the driver could override it after
4930 * parsing init data.
4932 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4933 if (config == NULL) {
4939 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4940 &rdev->dev.of_node);
4942 * We need to keep track of any GPIO descriptor coming from the
4943 * device tree until we have handled it over to the core. If the
4944 * config that was passed in to this function DOES NOT contain
4945 * a descriptor, and the config after this call DOES contain
4946 * a descriptor, we definately got one from parsing the device
4949 if (!cfg->ena_gpiod && config->ena_gpiod)
4950 dangling_of_gpiod = true;
4952 init_data = config->init_data;
4953 rdev->dev.of_node = of_node_get(config->of_node);
4956 ww_mutex_init(&rdev->mutex, ®ulator_ww_class);
4957 rdev->reg_data = config->driver_data;
4958 rdev->owner = regulator_desc->owner;
4959 rdev->desc = regulator_desc;
4961 rdev->regmap = config->regmap;
4962 else if (dev_get_regmap(dev, NULL))
4963 rdev->regmap = dev_get_regmap(dev, NULL);
4964 else if (dev->parent)
4965 rdev->regmap = dev_get_regmap(dev->parent, NULL);
4966 INIT_LIST_HEAD(&rdev->consumer_list);
4967 INIT_LIST_HEAD(&rdev->list);
4968 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4969 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4971 /* preform any regulator specific init */
4972 if (init_data && init_data->regulator_init) {
4973 ret = init_data->regulator_init(rdev->reg_data);
4978 if (config->ena_gpiod ||
4979 ((config->ena_gpio || config->ena_gpio_initialized) &&
4980 gpio_is_valid(config->ena_gpio))) {
4981 mutex_lock(®ulator_list_mutex);
4982 ret = regulator_ena_gpio_request(rdev, config);
4983 mutex_unlock(®ulator_list_mutex);
4985 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4986 config->ena_gpio, ret);
4989 /* The regulator core took over the GPIO descriptor */
4990 dangling_cfg_gpiod = false;
4991 dangling_of_gpiod = false;
4994 /* register with sysfs */
4995 rdev->dev.class = ®ulator_class;
4996 rdev->dev.parent = dev;
4997 dev_set_name(&rdev->dev, "regulator.%lu",
4998 (unsigned long) atomic_inc_return(®ulator_no));
5000 /* set regulator constraints */
5002 constraints = &init_data->constraints;
5004 if (init_data && init_data->supply_regulator)
5005 rdev->supply_name = init_data->supply_regulator;
5006 else if (regulator_desc->supply_name)
5007 rdev->supply_name = regulator_desc->supply_name;
5010 * Attempt to resolve the regulator supply, if specified,
5011 * but don't return an error if we fail because we will try
5012 * to resolve it again later as more regulators are added.
5014 if (regulator_resolve_supply(rdev))
5015 rdev_dbg(rdev, "unable to resolve supply\n");
5017 ret = set_machine_constraints(rdev, constraints);
5021 ret = regulator_init_coupling(rdev);
5025 /* add consumers devices */
5027 mutex_lock(®ulator_list_mutex);
5028 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5029 ret = set_consumer_device_supply(rdev,
5030 init_data->consumer_supplies[i].dev_name,
5031 init_data->consumer_supplies[i].supply);
5033 mutex_unlock(®ulator_list_mutex);
5034 dev_err(dev, "Failed to set supply %s\n",
5035 init_data->consumer_supplies[i].supply);
5036 goto unset_supplies;
5039 mutex_unlock(®ulator_list_mutex);
5042 if (!rdev->desc->ops->get_voltage &&
5043 !rdev->desc->ops->list_voltage &&
5044 !rdev->desc->fixed_uV)
5045 rdev->is_switch = true;
5047 dev_set_drvdata(&rdev->dev, rdev);
5048 ret = device_register(&rdev->dev);
5050 put_device(&rdev->dev);
5051 goto unset_supplies;
5054 rdev_init_debugfs(rdev);
5056 /* try to resolve regulators coupling since a new one was registered */
5057 mutex_lock(®ulator_list_mutex);
5058 regulator_resolve_coupling(rdev);
5059 mutex_unlock(®ulator_list_mutex);
5061 /* try to resolve regulators supply since a new one was registered */
5062 class_for_each_device(®ulator_class, NULL, NULL,
5063 regulator_register_resolve_supply);
5068 mutex_lock(®ulator_list_mutex);
5069 unset_regulator_supplies(rdev);
5070 mutex_unlock(®ulator_list_mutex);
5072 kfree(rdev->constraints);
5073 mutex_lock(®ulator_list_mutex);
5074 regulator_ena_gpio_free(rdev);
5075 mutex_unlock(®ulator_list_mutex);
5077 if (dangling_of_gpiod)
5078 gpiod_put(config->ena_gpiod);
5082 if (dangling_cfg_gpiod)
5083 gpiod_put(cfg->ena_gpiod);
5084 return ERR_PTR(ret);
5086 EXPORT_SYMBOL_GPL(regulator_register);
5089 * regulator_unregister - unregister regulator
5090 * @rdev: regulator to unregister
5092 * Called by regulator drivers to unregister a regulator.
5094 void regulator_unregister(struct regulator_dev *rdev)
5100 while (rdev->use_count--)
5101 regulator_disable(rdev->supply);
5102 regulator_put(rdev->supply);
5105 mutex_lock(®ulator_list_mutex);
5107 debugfs_remove_recursive(rdev->debugfs);
5108 flush_work(&rdev->disable_work.work);
5109 WARN_ON(rdev->open_count);
5110 regulator_remove_coupling(rdev);
5111 unset_regulator_supplies(rdev);
5112 list_del(&rdev->list);
5113 regulator_ena_gpio_free(rdev);
5114 device_unregister(&rdev->dev);
5116 mutex_unlock(®ulator_list_mutex);
5118 EXPORT_SYMBOL_GPL(regulator_unregister);
5120 #ifdef CONFIG_SUSPEND
5122 * regulator_suspend - prepare regulators for system wide suspend
5123 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5125 * Configure each regulator with it's suspend operating parameters for state.
5127 static int regulator_suspend(struct device *dev)
5129 struct regulator_dev *rdev = dev_to_rdev(dev);
5130 suspend_state_t state = pm_suspend_target_state;
5133 regulator_lock(rdev);
5134 ret = suspend_set_state(rdev, state);
5135 regulator_unlock(rdev);
5140 static int regulator_resume(struct device *dev)
5142 suspend_state_t state = pm_suspend_target_state;
5143 struct regulator_dev *rdev = dev_to_rdev(dev);
5144 struct regulator_state *rstate;
5147 rstate = regulator_get_suspend_state(rdev, state);
5151 regulator_lock(rdev);
5153 if (rdev->desc->ops->resume &&
5154 (rstate->enabled == ENABLE_IN_SUSPEND ||
5155 rstate->enabled == DISABLE_IN_SUSPEND))
5156 ret = rdev->desc->ops->resume(rdev);
5158 regulator_unlock(rdev);
5162 #else /* !CONFIG_SUSPEND */
5164 #define regulator_suspend NULL
5165 #define regulator_resume NULL
5167 #endif /* !CONFIG_SUSPEND */
5170 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5171 .suspend = regulator_suspend,
5172 .resume = regulator_resume,
5176 struct class regulator_class = {
5177 .name = "regulator",
5178 .dev_release = regulator_dev_release,
5179 .dev_groups = regulator_dev_groups,
5181 .pm = ®ulator_pm_ops,
5185 * regulator_has_full_constraints - the system has fully specified constraints
5187 * Calling this function will cause the regulator API to disable all
5188 * regulators which have a zero use count and don't have an always_on
5189 * constraint in a late_initcall.
5191 * The intention is that this will become the default behaviour in a
5192 * future kernel release so users are encouraged to use this facility
5195 void regulator_has_full_constraints(void)
5197 has_full_constraints = 1;
5199 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5202 * rdev_get_drvdata - get rdev regulator driver data
5205 * Get rdev regulator driver private data. This call can be used in the
5206 * regulator driver context.
5208 void *rdev_get_drvdata(struct regulator_dev *rdev)
5210 return rdev->reg_data;
5212 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5215 * regulator_get_drvdata - get regulator driver data
5216 * @regulator: regulator
5218 * Get regulator driver private data. This call can be used in the consumer
5219 * driver context when non API regulator specific functions need to be called.
5221 void *regulator_get_drvdata(struct regulator *regulator)
5223 return regulator->rdev->reg_data;
5225 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5228 * regulator_set_drvdata - set regulator driver data
5229 * @regulator: regulator
5232 void regulator_set_drvdata(struct regulator *regulator, void *data)
5234 regulator->rdev->reg_data = data;
5236 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5239 * regulator_get_id - get regulator ID
5242 int rdev_get_id(struct regulator_dev *rdev)
5244 return rdev->desc->id;
5246 EXPORT_SYMBOL_GPL(rdev_get_id);
5248 struct device *rdev_get_dev(struct regulator_dev *rdev)
5252 EXPORT_SYMBOL_GPL(rdev_get_dev);
5254 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5256 return reg_init_data->driver_data;
5258 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5260 #ifdef CONFIG_DEBUG_FS
5261 static int supply_map_show(struct seq_file *sf, void *data)
5263 struct regulator_map *map;
5265 list_for_each_entry(map, ®ulator_map_list, list) {
5266 seq_printf(sf, "%s -> %s.%s\n",
5267 rdev_get_name(map->regulator), map->dev_name,
5273 DEFINE_SHOW_ATTRIBUTE(supply_map);
5275 struct summary_data {
5277 struct regulator_dev *parent;
5281 static void regulator_summary_show_subtree(struct seq_file *s,
5282 struct regulator_dev *rdev,
5285 static int regulator_summary_show_children(struct device *dev, void *data)
5287 struct regulator_dev *rdev = dev_to_rdev(dev);
5288 struct summary_data *summary_data = data;
5290 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5291 regulator_summary_show_subtree(summary_data->s, rdev,
5292 summary_data->level + 1);
5297 static void regulator_summary_show_subtree(struct seq_file *s,
5298 struct regulator_dev *rdev,
5301 struct regulation_constraints *c;
5302 struct regulator *consumer;
5303 struct summary_data summary_data;
5304 unsigned int opmode;
5309 opmode = _regulator_get_mode_unlocked(rdev);
5310 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5312 30 - level * 3, rdev_get_name(rdev),
5313 rdev->use_count, rdev->open_count, rdev->bypass_count,
5314 regulator_opmode_to_str(opmode));
5316 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
5317 seq_printf(s, "%5dmA ",
5318 _regulator_get_current_limit_unlocked(rdev) / 1000);
5320 c = rdev->constraints;
5322 switch (rdev->desc->type) {
5323 case REGULATOR_VOLTAGE:
5324 seq_printf(s, "%5dmV %5dmV ",
5325 c->min_uV / 1000, c->max_uV / 1000);
5327 case REGULATOR_CURRENT:
5328 seq_printf(s, "%5dmA %5dmA ",
5329 c->min_uA / 1000, c->max_uA / 1000);
5336 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5337 if (consumer->dev && consumer->dev->class == ®ulator_class)
5340 seq_printf(s, "%*s%-*s ",
5341 (level + 1) * 3 + 1, "",
5342 30 - (level + 1) * 3,
5343 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5345 switch (rdev->desc->type) {
5346 case REGULATOR_VOLTAGE:
5347 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5348 consumer->enable_count,
5349 consumer->uA_load / 1000,
5350 consumer->uA_load && !consumer->enable_count ?
5352 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5353 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5355 case REGULATOR_CURRENT:
5363 summary_data.level = level;
5364 summary_data.parent = rdev;
5366 class_for_each_device(®ulator_class, NULL, &summary_data,
5367 regulator_summary_show_children);
5370 struct summary_lock_data {
5371 struct ww_acquire_ctx *ww_ctx;
5372 struct regulator_dev **new_contended_rdev;
5373 struct regulator_dev **old_contended_rdev;
5376 static int regulator_summary_lock_one(struct device *dev, void *data)
5378 struct regulator_dev *rdev = dev_to_rdev(dev);
5379 struct summary_lock_data *lock_data = data;
5382 if (rdev != *lock_data->old_contended_rdev) {
5383 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5385 if (ret == -EDEADLK)
5386 *lock_data->new_contended_rdev = rdev;
5390 *lock_data->old_contended_rdev = NULL;
5396 static int regulator_summary_unlock_one(struct device *dev, void *data)
5398 struct regulator_dev *rdev = dev_to_rdev(dev);
5399 struct summary_lock_data *lock_data = data;
5402 if (rdev == *lock_data->new_contended_rdev)
5406 regulator_unlock(rdev);
5411 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5412 struct regulator_dev **new_contended_rdev,
5413 struct regulator_dev **old_contended_rdev)
5415 struct summary_lock_data lock_data;
5418 lock_data.ww_ctx = ww_ctx;
5419 lock_data.new_contended_rdev = new_contended_rdev;
5420 lock_data.old_contended_rdev = old_contended_rdev;
5422 ret = class_for_each_device(®ulator_class, NULL, &lock_data,
5423 regulator_summary_lock_one);
5425 class_for_each_device(®ulator_class, NULL, &lock_data,
5426 regulator_summary_unlock_one);
5431 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5433 struct regulator_dev *new_contended_rdev = NULL;
5434 struct regulator_dev *old_contended_rdev = NULL;
5437 mutex_lock(®ulator_list_mutex);
5439 ww_acquire_init(ww_ctx, ®ulator_ww_class);
5442 if (new_contended_rdev) {
5443 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5444 old_contended_rdev = new_contended_rdev;
5445 old_contended_rdev->ref_cnt++;
5448 err = regulator_summary_lock_all(ww_ctx,
5449 &new_contended_rdev,
5450 &old_contended_rdev);
5452 if (old_contended_rdev)
5453 regulator_unlock(old_contended_rdev);
5455 } while (err == -EDEADLK);
5457 ww_acquire_done(ww_ctx);
5460 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5462 class_for_each_device(®ulator_class, NULL, NULL,
5463 regulator_summary_unlock_one);
5464 ww_acquire_fini(ww_ctx);
5466 mutex_unlock(®ulator_list_mutex);
5469 static int regulator_summary_show_roots(struct device *dev, void *data)
5471 struct regulator_dev *rdev = dev_to_rdev(dev);
5472 struct seq_file *s = data;
5475 regulator_summary_show_subtree(s, rdev, 0);
5480 static int regulator_summary_show(struct seq_file *s, void *data)
5482 struct ww_acquire_ctx ww_ctx;
5484 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5485 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5487 regulator_summary_lock(&ww_ctx);
5489 class_for_each_device(®ulator_class, NULL, s,
5490 regulator_summary_show_roots);
5492 regulator_summary_unlock(&ww_ctx);
5496 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5497 #endif /* CONFIG_DEBUG_FS */
5499 static int __init regulator_init(void)
5503 ret = class_register(®ulator_class);
5505 debugfs_root = debugfs_create_dir("regulator", NULL);
5507 pr_warn("regulator: Failed to create debugfs directory\n");
5509 #ifdef CONFIG_DEBUG_FS
5510 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5513 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5514 NULL, ®ulator_summary_fops);
5516 regulator_dummy_init();
5521 /* init early to allow our consumers to complete system booting */
5522 core_initcall(regulator_init);
5524 static int __init regulator_late_cleanup(struct device *dev, void *data)
5526 struct regulator_dev *rdev = dev_to_rdev(dev);
5527 const struct regulator_ops *ops = rdev->desc->ops;
5528 struct regulation_constraints *c = rdev->constraints;
5531 if (c && c->always_on)
5534 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5537 regulator_lock(rdev);
5539 if (rdev->use_count)
5542 /* If we can't read the status assume it's on. */
5543 if (ops->is_enabled)
5544 enabled = ops->is_enabled(rdev);
5551 if (have_full_constraints()) {
5552 /* We log since this may kill the system if it goes
5554 rdev_info(rdev, "disabling\n");
5555 ret = _regulator_do_disable(rdev);
5557 rdev_err(rdev, "couldn't disable: %d\n", ret);
5559 /* The intention is that in future we will
5560 * assume that full constraints are provided
5561 * so warn even if we aren't going to do
5564 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5568 regulator_unlock(rdev);
5573 static int __init regulator_init_complete(void)
5576 * Since DT doesn't provide an idiomatic mechanism for
5577 * enabling full constraints and since it's much more natural
5578 * with DT to provide them just assume that a DT enabled
5579 * system has full constraints.
5581 if (of_have_populated_dt())
5582 has_full_constraints = true;
5585 * Regulators may had failed to resolve their input supplies
5586 * when were registered, either because the input supply was
5587 * not registered yet or because its parent device was not
5588 * bound yet. So attempt to resolve the input supplies for
5589 * pending regulators before trying to disable unused ones.
5591 class_for_each_device(®ulator_class, NULL, NULL,
5592 regulator_register_resolve_supply);
5594 /* If we have a full configuration then disable any regulators
5595 * we have permission to change the status for and which are
5596 * not in use or always_on. This is effectively the default
5597 * for DT and ACPI as they have full constraints.
5599 class_for_each_device(®ulator_class, NULL, NULL,
5600 regulator_late_cleanup);
5604 late_initcall_sync(regulator_init_complete);