1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // core.c -- Voltage/Current Regulator framework.
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
8 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/async.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/coupler.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/module.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
36 #define rdev_crit(rdev, fmt, ...) \
37 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_err(rdev, fmt, ...) \
39 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_warn(rdev, fmt, ...) \
41 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_info(rdev, fmt, ...) \
43 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_dbg(rdev, fmt, ...) \
45 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 static DEFINE_WW_CLASS(regulator_ww_class
);
48 static DEFINE_MUTEX(regulator_nesting_mutex
);
49 static DEFINE_MUTEX(regulator_list_mutex
);
50 static LIST_HEAD(regulator_map_list
);
51 static LIST_HEAD(regulator_ena_gpio_list
);
52 static LIST_HEAD(regulator_supply_alias_list
);
53 static LIST_HEAD(regulator_coupler_list
);
54 static bool has_full_constraints
;
56 static struct dentry
*debugfs_root
;
59 * struct regulator_map
61 * Used to provide symbolic supply names to devices.
63 struct regulator_map
{
64 struct list_head list
;
65 const char *dev_name
; /* The dev_name() for the consumer */
67 struct regulator_dev
*regulator
;
71 * struct regulator_enable_gpio
73 * Management for shared enable GPIO pin
75 struct regulator_enable_gpio
{
76 struct list_head list
;
77 struct gpio_desc
*gpiod
;
78 u32 enable_count
; /* a number of enabled shared GPIO */
79 u32 request_count
; /* a number of requested shared GPIO */
83 * struct regulator_supply_alias
85 * Used to map lookups for a supply onto an alternative device.
87 struct regulator_supply_alias
{
88 struct list_head list
;
89 struct device
*src_dev
;
90 const char *src_supply
;
91 struct device
*alias_dev
;
92 const char *alias_supply
;
95 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
96 static int _regulator_disable(struct regulator
*regulator
);
97 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
98 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
99 static int _notifier_call_chain(struct regulator_dev
*rdev
,
100 unsigned long event
, void *data
);
101 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
102 int min_uV
, int max_uV
);
103 static int regulator_balance_voltage(struct regulator_dev
*rdev
,
104 suspend_state_t state
);
105 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
107 const char *supply_name
);
108 static void destroy_regulator(struct regulator
*regulator
);
109 static void _regulator_put(struct regulator
*regulator
);
111 const char *rdev_get_name(struct regulator_dev
*rdev
)
113 if (rdev
->constraints
&& rdev
->constraints
->name
)
114 return rdev
->constraints
->name
;
115 else if (rdev
->desc
->name
)
116 return rdev
->desc
->name
;
121 static bool have_full_constraints(void)
123 return has_full_constraints
|| of_have_populated_dt();
126 static bool regulator_ops_is_valid(struct regulator_dev
*rdev
, int ops
)
128 if (!rdev
->constraints
) {
129 rdev_err(rdev
, "no constraints\n");
133 if (rdev
->constraints
->valid_ops_mask
& ops
)
140 * regulator_lock_nested - lock a single regulator
141 * @rdev: regulator source
142 * @ww_ctx: w/w mutex acquire context
144 * This function can be called many times by one task on
145 * a single regulator and its mutex will be locked only
146 * once. If a task, which is calling this function is other
147 * than the one, which initially locked the mutex, it will
150 static inline int regulator_lock_nested(struct regulator_dev
*rdev
,
151 struct ww_acquire_ctx
*ww_ctx
)
156 mutex_lock(®ulator_nesting_mutex
);
158 if (ww_ctx
|| !ww_mutex_trylock(&rdev
->mutex
)) {
159 if (rdev
->mutex_owner
== current
)
165 mutex_unlock(®ulator_nesting_mutex
);
166 ret
= ww_mutex_lock(&rdev
->mutex
, ww_ctx
);
167 mutex_lock(®ulator_nesting_mutex
);
173 if (lock
&& ret
!= -EDEADLK
) {
175 rdev
->mutex_owner
= current
;
178 mutex_unlock(®ulator_nesting_mutex
);
184 * regulator_lock - lock a single regulator
185 * @rdev: regulator source
187 * This function can be called many times by one task on
188 * a single regulator and its mutex will be locked only
189 * once. If a task, which is calling this function is other
190 * than the one, which initially locked the mutex, it will
193 static void regulator_lock(struct regulator_dev
*rdev
)
195 regulator_lock_nested(rdev
, NULL
);
199 * regulator_unlock - unlock a single regulator
200 * @rdev: regulator_source
202 * This function unlocks the mutex when the
203 * reference counter reaches 0.
205 static void regulator_unlock(struct regulator_dev
*rdev
)
207 mutex_lock(®ulator_nesting_mutex
);
209 if (--rdev
->ref_cnt
== 0) {
210 rdev
->mutex_owner
= NULL
;
211 ww_mutex_unlock(&rdev
->mutex
);
214 WARN_ON_ONCE(rdev
->ref_cnt
< 0);
216 mutex_unlock(®ulator_nesting_mutex
);
219 static bool regulator_supply_is_couple(struct regulator_dev
*rdev
)
221 struct regulator_dev
*c_rdev
;
224 for (i
= 1; i
< rdev
->coupling_desc
.n_coupled
; i
++) {
225 c_rdev
= rdev
->coupling_desc
.coupled_rdevs
[i
];
227 if (rdev
->supply
->rdev
== c_rdev
)
234 static void regulator_unlock_recursive(struct regulator_dev
*rdev
,
235 unsigned int n_coupled
)
237 struct regulator_dev
*c_rdev
, *supply_rdev
;
238 int i
, supply_n_coupled
;
240 for (i
= n_coupled
; i
> 0; i
--) {
241 c_rdev
= rdev
->coupling_desc
.coupled_rdevs
[i
- 1];
246 if (c_rdev
->supply
&& !regulator_supply_is_couple(c_rdev
)) {
247 supply_rdev
= c_rdev
->supply
->rdev
;
248 supply_n_coupled
= supply_rdev
->coupling_desc
.n_coupled
;
250 regulator_unlock_recursive(supply_rdev
,
254 regulator_unlock(c_rdev
);
258 static int regulator_lock_recursive(struct regulator_dev
*rdev
,
259 struct regulator_dev
**new_contended_rdev
,
260 struct regulator_dev
**old_contended_rdev
,
261 struct ww_acquire_ctx
*ww_ctx
)
263 struct regulator_dev
*c_rdev
;
266 for (i
= 0; i
< rdev
->coupling_desc
.n_coupled
; i
++) {
267 c_rdev
= rdev
->coupling_desc
.coupled_rdevs
[i
];
272 if (c_rdev
!= *old_contended_rdev
) {
273 err
= regulator_lock_nested(c_rdev
, ww_ctx
);
275 if (err
== -EDEADLK
) {
276 *new_contended_rdev
= c_rdev
;
280 /* shouldn't happen */
281 WARN_ON_ONCE(err
!= -EALREADY
);
284 *old_contended_rdev
= NULL
;
287 if (c_rdev
->supply
&& !regulator_supply_is_couple(c_rdev
)) {
288 err
= regulator_lock_recursive(c_rdev
->supply
->rdev
,
293 regulator_unlock(c_rdev
);
302 regulator_unlock_recursive(rdev
, i
);
308 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
310 * @rdev: regulator source
311 * @ww_ctx: w/w mutex acquire context
313 * Unlock all regulators related with rdev by coupling or supplying.
315 static void regulator_unlock_dependent(struct regulator_dev
*rdev
,
316 struct ww_acquire_ctx
*ww_ctx
)
318 regulator_unlock_recursive(rdev
, rdev
->coupling_desc
.n_coupled
);
319 ww_acquire_fini(ww_ctx
);
323 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
324 * @rdev: regulator source
325 * @ww_ctx: w/w mutex acquire context
327 * This function as a wrapper on regulator_lock_recursive(), which locks
328 * all regulators related with rdev by coupling or supplying.
330 static void regulator_lock_dependent(struct regulator_dev
*rdev
,
331 struct ww_acquire_ctx
*ww_ctx
)
333 struct regulator_dev
*new_contended_rdev
= NULL
;
334 struct regulator_dev
*old_contended_rdev
= NULL
;
337 mutex_lock(®ulator_list_mutex
);
339 ww_acquire_init(ww_ctx
, ®ulator_ww_class
);
342 if (new_contended_rdev
) {
343 ww_mutex_lock_slow(&new_contended_rdev
->mutex
, ww_ctx
);
344 old_contended_rdev
= new_contended_rdev
;
345 old_contended_rdev
->ref_cnt
++;
348 err
= regulator_lock_recursive(rdev
,
353 if (old_contended_rdev
)
354 regulator_unlock(old_contended_rdev
);
356 } while (err
== -EDEADLK
);
358 ww_acquire_done(ww_ctx
);
360 mutex_unlock(®ulator_list_mutex
);
364 * of_get_child_regulator - get a child regulator device node
365 * based on supply name
366 * @parent: Parent device node
367 * @prop_name: Combination regulator supply name and "-supply"
369 * Traverse all child nodes.
370 * Extract the child regulator device node corresponding to the supply name.
371 * returns the device node corresponding to the regulator if found, else
374 static struct device_node
*of_get_child_regulator(struct device_node
*parent
,
375 const char *prop_name
)
377 struct device_node
*regnode
= NULL
;
378 struct device_node
*child
= NULL
;
380 for_each_child_of_node(parent
, child
) {
381 regnode
= of_parse_phandle(child
, prop_name
, 0);
384 regnode
= of_get_child_regulator(child
, prop_name
);
399 * of_get_regulator - get a regulator device node based on supply name
400 * @dev: Device pointer for the consumer (of regulator) device
401 * @supply: regulator supply name
403 * Extract the regulator device node corresponding to the supply name.
404 * returns the device node corresponding to the regulator if found, else
407 static struct device_node
*of_get_regulator(struct device
*dev
, const char *supply
)
409 struct device_node
*regnode
= NULL
;
410 char prop_name
[64]; /* 64 is max size of property name */
412 dev_dbg(dev
, "Looking up %s-supply from device tree\n", supply
);
414 snprintf(prop_name
, 64, "%s-supply", supply
);
415 regnode
= of_parse_phandle(dev
->of_node
, prop_name
, 0);
418 regnode
= of_get_child_regulator(dev
->of_node
, prop_name
);
422 dev_dbg(dev
, "Looking up %s property in node %pOF failed\n",
423 prop_name
, dev
->of_node
);
429 /* Platform voltage constraint check */
430 int regulator_check_voltage(struct regulator_dev
*rdev
,
431 int *min_uV
, int *max_uV
)
433 BUG_ON(*min_uV
> *max_uV
);
435 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
436 rdev_err(rdev
, "voltage operation not allowed\n");
440 if (*max_uV
> rdev
->constraints
->max_uV
)
441 *max_uV
= rdev
->constraints
->max_uV
;
442 if (*min_uV
< rdev
->constraints
->min_uV
)
443 *min_uV
= rdev
->constraints
->min_uV
;
445 if (*min_uV
> *max_uV
) {
446 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
454 /* return 0 if the state is valid */
455 static int regulator_check_states(suspend_state_t state
)
457 return (state
> PM_SUSPEND_MAX
|| state
== PM_SUSPEND_TO_IDLE
);
460 /* Make sure we select a voltage that suits the needs of all
461 * regulator consumers
463 int regulator_check_consumers(struct regulator_dev
*rdev
,
464 int *min_uV
, int *max_uV
,
465 suspend_state_t state
)
467 struct regulator
*regulator
;
468 struct regulator_voltage
*voltage
;
470 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
471 voltage
= ®ulator
->voltage
[state
];
473 * Assume consumers that didn't say anything are OK
474 * with anything in the constraint range.
476 if (!voltage
->min_uV
&& !voltage
->max_uV
)
479 if (*max_uV
> voltage
->max_uV
)
480 *max_uV
= voltage
->max_uV
;
481 if (*min_uV
< voltage
->min_uV
)
482 *min_uV
= voltage
->min_uV
;
485 if (*min_uV
> *max_uV
) {
486 rdev_err(rdev
, "Restricting voltage, %u-%uuV\n",
494 /* current constraint check */
495 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
496 int *min_uA
, int *max_uA
)
498 BUG_ON(*min_uA
> *max_uA
);
500 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_CURRENT
)) {
501 rdev_err(rdev
, "current operation not allowed\n");
505 if (*max_uA
> rdev
->constraints
->max_uA
)
506 *max_uA
= rdev
->constraints
->max_uA
;
507 if (*min_uA
< rdev
->constraints
->min_uA
)
508 *min_uA
= rdev
->constraints
->min_uA
;
510 if (*min_uA
> *max_uA
) {
511 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
519 /* operating mode constraint check */
520 static int regulator_mode_constrain(struct regulator_dev
*rdev
,
524 case REGULATOR_MODE_FAST
:
525 case REGULATOR_MODE_NORMAL
:
526 case REGULATOR_MODE_IDLE
:
527 case REGULATOR_MODE_STANDBY
:
530 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
534 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_MODE
)) {
535 rdev_err(rdev
, "mode operation not allowed\n");
539 /* The modes are bitmasks, the most power hungry modes having
540 * the lowest values. If the requested mode isn't supported
541 * try higher modes. */
543 if (rdev
->constraints
->valid_modes_mask
& *mode
)
551 static inline struct regulator_state
*
552 regulator_get_suspend_state(struct regulator_dev
*rdev
, suspend_state_t state
)
554 if (rdev
->constraints
== NULL
)
558 case PM_SUSPEND_STANDBY
:
559 return &rdev
->constraints
->state_standby
;
561 return &rdev
->constraints
->state_mem
;
563 return &rdev
->constraints
->state_disk
;
569 static const struct regulator_state
*
570 regulator_get_suspend_state_check(struct regulator_dev
*rdev
, suspend_state_t state
)
572 const struct regulator_state
*rstate
;
574 rstate
= regulator_get_suspend_state(rdev
, state
);
578 /* If we have no suspend mode configuration don't set anything;
579 * only warn if the driver implements set_suspend_voltage or
580 * set_suspend_mode callback.
582 if (rstate
->enabled
!= ENABLE_IN_SUSPEND
&&
583 rstate
->enabled
!= DISABLE_IN_SUSPEND
) {
584 if (rdev
->desc
->ops
->set_suspend_voltage
||
585 rdev
->desc
->ops
->set_suspend_mode
)
586 rdev_warn(rdev
, "No configuration\n");
593 static ssize_t
regulator_uV_show(struct device
*dev
,
594 struct device_attribute
*attr
, char *buf
)
596 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
599 regulator_lock(rdev
);
600 uV
= regulator_get_voltage_rdev(rdev
);
601 regulator_unlock(rdev
);
605 return sprintf(buf
, "%d\n", uV
);
607 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
609 static ssize_t
regulator_uA_show(struct device
*dev
,
610 struct device_attribute
*attr
, char *buf
)
612 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
614 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
616 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
618 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
621 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
623 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
625 static DEVICE_ATTR_RO(name
);
627 static const char *regulator_opmode_to_str(int mode
)
630 case REGULATOR_MODE_FAST
:
632 case REGULATOR_MODE_NORMAL
:
634 case REGULATOR_MODE_IDLE
:
636 case REGULATOR_MODE_STANDBY
:
642 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
644 return sprintf(buf
, "%s\n", regulator_opmode_to_str(mode
));
647 static ssize_t
regulator_opmode_show(struct device
*dev
,
648 struct device_attribute
*attr
, char *buf
)
650 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
652 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
654 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
656 static ssize_t
regulator_print_state(char *buf
, int state
)
659 return sprintf(buf
, "enabled\n");
661 return sprintf(buf
, "disabled\n");
663 return sprintf(buf
, "unknown\n");
666 static ssize_t
regulator_state_show(struct device
*dev
,
667 struct device_attribute
*attr
, char *buf
)
669 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
672 regulator_lock(rdev
);
673 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
674 regulator_unlock(rdev
);
678 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
680 static ssize_t
regulator_status_show(struct device
*dev
,
681 struct device_attribute
*attr
, char *buf
)
683 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
687 status
= rdev
->desc
->ops
->get_status(rdev
);
692 case REGULATOR_STATUS_OFF
:
695 case REGULATOR_STATUS_ON
:
698 case REGULATOR_STATUS_ERROR
:
701 case REGULATOR_STATUS_FAST
:
704 case REGULATOR_STATUS_NORMAL
:
707 case REGULATOR_STATUS_IDLE
:
710 case REGULATOR_STATUS_STANDBY
:
713 case REGULATOR_STATUS_BYPASS
:
716 case REGULATOR_STATUS_UNDEFINED
:
723 return sprintf(buf
, "%s\n", label
);
725 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
727 static ssize_t
regulator_min_uA_show(struct device
*dev
,
728 struct device_attribute
*attr
, char *buf
)
730 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
732 if (!rdev
->constraints
)
733 return sprintf(buf
, "constraint not defined\n");
735 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
737 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
739 static ssize_t
regulator_max_uA_show(struct device
*dev
,
740 struct device_attribute
*attr
, char *buf
)
742 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
744 if (!rdev
->constraints
)
745 return sprintf(buf
, "constraint not defined\n");
747 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
749 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
751 static ssize_t
regulator_min_uV_show(struct device
*dev
,
752 struct device_attribute
*attr
, char *buf
)
754 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
756 if (!rdev
->constraints
)
757 return sprintf(buf
, "constraint not defined\n");
759 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
761 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
763 static ssize_t
regulator_max_uV_show(struct device
*dev
,
764 struct device_attribute
*attr
, char *buf
)
766 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
768 if (!rdev
->constraints
)
769 return sprintf(buf
, "constraint not defined\n");
771 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
773 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
775 static ssize_t
regulator_total_uA_show(struct device
*dev
,
776 struct device_attribute
*attr
, char *buf
)
778 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
779 struct regulator
*regulator
;
782 regulator_lock(rdev
);
783 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
784 if (regulator
->enable_count
)
785 uA
+= regulator
->uA_load
;
787 regulator_unlock(rdev
);
788 return sprintf(buf
, "%d\n", uA
);
790 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
792 static ssize_t
num_users_show(struct device
*dev
, struct device_attribute
*attr
,
795 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
796 return sprintf(buf
, "%d\n", rdev
->use_count
);
798 static DEVICE_ATTR_RO(num_users
);
800 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
803 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
805 switch (rdev
->desc
->type
) {
806 case REGULATOR_VOLTAGE
:
807 return sprintf(buf
, "voltage\n");
808 case REGULATOR_CURRENT
:
809 return sprintf(buf
, "current\n");
811 return sprintf(buf
, "unknown\n");
813 static DEVICE_ATTR_RO(type
);
815 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
816 struct device_attribute
*attr
, char *buf
)
818 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
820 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
822 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
823 regulator_suspend_mem_uV_show
, NULL
);
825 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
826 struct device_attribute
*attr
, char *buf
)
828 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
830 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
832 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
833 regulator_suspend_disk_uV_show
, NULL
);
835 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
836 struct device_attribute
*attr
, char *buf
)
838 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
840 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
842 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
843 regulator_suspend_standby_uV_show
, NULL
);
845 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
846 struct device_attribute
*attr
, char *buf
)
848 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
850 return regulator_print_opmode(buf
,
851 rdev
->constraints
->state_mem
.mode
);
853 static DEVICE_ATTR(suspend_mem_mode
, 0444,
854 regulator_suspend_mem_mode_show
, NULL
);
856 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
857 struct device_attribute
*attr
, char *buf
)
859 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
861 return regulator_print_opmode(buf
,
862 rdev
->constraints
->state_disk
.mode
);
864 static DEVICE_ATTR(suspend_disk_mode
, 0444,
865 regulator_suspend_disk_mode_show
, NULL
);
867 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
868 struct device_attribute
*attr
, char *buf
)
870 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
872 return regulator_print_opmode(buf
,
873 rdev
->constraints
->state_standby
.mode
);
875 static DEVICE_ATTR(suspend_standby_mode
, 0444,
876 regulator_suspend_standby_mode_show
, NULL
);
878 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
879 struct device_attribute
*attr
, char *buf
)
881 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
883 return regulator_print_state(buf
,
884 rdev
->constraints
->state_mem
.enabled
);
886 static DEVICE_ATTR(suspend_mem_state
, 0444,
887 regulator_suspend_mem_state_show
, NULL
);
889 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
890 struct device_attribute
*attr
, char *buf
)
892 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
894 return regulator_print_state(buf
,
895 rdev
->constraints
->state_disk
.enabled
);
897 static DEVICE_ATTR(suspend_disk_state
, 0444,
898 regulator_suspend_disk_state_show
, NULL
);
900 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
901 struct device_attribute
*attr
, char *buf
)
903 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
905 return regulator_print_state(buf
,
906 rdev
->constraints
->state_standby
.enabled
);
908 static DEVICE_ATTR(suspend_standby_state
, 0444,
909 regulator_suspend_standby_state_show
, NULL
);
911 static ssize_t
regulator_bypass_show(struct device
*dev
,
912 struct device_attribute
*attr
, char *buf
)
914 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
919 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypass
);
928 return sprintf(buf
, "%s\n", report
);
930 static DEVICE_ATTR(bypass
, 0444,
931 regulator_bypass_show
, NULL
);
933 /* Calculate the new optimum regulator operating mode based on the new total
934 * consumer load. All locks held by caller */
935 static int drms_uA_update(struct regulator_dev
*rdev
)
937 struct regulator
*sibling
;
938 int current_uA
= 0, output_uV
, input_uV
, err
;
942 * first check to see if we can set modes at all, otherwise just
943 * tell the consumer everything is OK.
945 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_DRMS
)) {
946 rdev_dbg(rdev
, "DRMS operation not allowed\n");
950 if (!rdev
->desc
->ops
->get_optimum_mode
&&
951 !rdev
->desc
->ops
->set_load
)
954 if (!rdev
->desc
->ops
->set_mode
&&
955 !rdev
->desc
->ops
->set_load
)
958 /* calc total requested load */
959 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
) {
960 if (sibling
->enable_count
)
961 current_uA
+= sibling
->uA_load
;
964 current_uA
+= rdev
->constraints
->system_load
;
966 if (rdev
->desc
->ops
->set_load
) {
967 /* set the optimum mode for our new total regulator load */
968 err
= rdev
->desc
->ops
->set_load(rdev
, current_uA
);
970 rdev_err(rdev
, "failed to set load %d: %pe\n",
971 current_uA
, ERR_PTR(err
));
973 /* get output voltage */
974 output_uV
= regulator_get_voltage_rdev(rdev
);
975 if (output_uV
<= 0) {
976 rdev_err(rdev
, "invalid output voltage found\n");
980 /* get input voltage */
983 input_uV
= regulator_get_voltage(rdev
->supply
);
985 input_uV
= rdev
->constraints
->input_uV
;
987 rdev_err(rdev
, "invalid input voltage found\n");
991 /* now get the optimum mode for our new total regulator load */
992 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
993 output_uV
, current_uA
);
995 /* check the new mode is allowed */
996 err
= regulator_mode_constrain(rdev
, &mode
);
998 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
999 current_uA
, input_uV
, output_uV
, ERR_PTR(err
));
1003 err
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1005 rdev_err(rdev
, "failed to set optimum mode %x: %pe\n",
1006 mode
, ERR_PTR(err
));
1012 static int __suspend_set_state(struct regulator_dev
*rdev
,
1013 const struct regulator_state
*rstate
)
1017 if (rstate
->enabled
== ENABLE_IN_SUSPEND
&&
1018 rdev
->desc
->ops
->set_suspend_enable
)
1019 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
1020 else if (rstate
->enabled
== DISABLE_IN_SUSPEND
&&
1021 rdev
->desc
->ops
->set_suspend_disable
)
1022 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
1023 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1027 rdev_err(rdev
, "failed to enabled/disable: %pe\n", ERR_PTR(ret
));
1031 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
1032 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
1034 rdev_err(rdev
, "failed to set voltage: %pe\n", ERR_PTR(ret
));
1039 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
1040 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
1042 rdev_err(rdev
, "failed to set mode: %pe\n", ERR_PTR(ret
));
1050 static int suspend_set_initial_state(struct regulator_dev
*rdev
)
1052 const struct regulator_state
*rstate
;
1054 rstate
= regulator_get_suspend_state_check(rdev
,
1055 rdev
->constraints
->initial_state
);
1059 return __suspend_set_state(rdev
, rstate
);
1062 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
1063 static void print_constraints_debug(struct regulator_dev
*rdev
)
1065 struct regulation_constraints
*constraints
= rdev
->constraints
;
1067 size_t len
= sizeof(buf
) - 1;
1071 if (constraints
->min_uV
&& constraints
->max_uV
) {
1072 if (constraints
->min_uV
== constraints
->max_uV
)
1073 count
+= scnprintf(buf
+ count
, len
- count
, "%d mV ",
1074 constraints
->min_uV
/ 1000);
1076 count
+= scnprintf(buf
+ count
, len
- count
,
1078 constraints
->min_uV
/ 1000,
1079 constraints
->max_uV
/ 1000);
1082 if (!constraints
->min_uV
||
1083 constraints
->min_uV
!= constraints
->max_uV
) {
1084 ret
= regulator_get_voltage_rdev(rdev
);
1086 count
+= scnprintf(buf
+ count
, len
- count
,
1087 "at %d mV ", ret
/ 1000);
1090 if (constraints
->uV_offset
)
1091 count
+= scnprintf(buf
+ count
, len
- count
, "%dmV offset ",
1092 constraints
->uV_offset
/ 1000);
1094 if (constraints
->min_uA
&& constraints
->max_uA
) {
1095 if (constraints
->min_uA
== constraints
->max_uA
)
1096 count
+= scnprintf(buf
+ count
, len
- count
, "%d mA ",
1097 constraints
->min_uA
/ 1000);
1099 count
+= scnprintf(buf
+ count
, len
- count
,
1101 constraints
->min_uA
/ 1000,
1102 constraints
->max_uA
/ 1000);
1105 if (!constraints
->min_uA
||
1106 constraints
->min_uA
!= constraints
->max_uA
) {
1107 ret
= _regulator_get_current_limit(rdev
);
1109 count
+= scnprintf(buf
+ count
, len
- count
,
1110 "at %d mA ", ret
/ 1000);
1113 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
1114 count
+= scnprintf(buf
+ count
, len
- count
, "fast ");
1115 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
1116 count
+= scnprintf(buf
+ count
, len
- count
, "normal ");
1117 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
1118 count
+= scnprintf(buf
+ count
, len
- count
, "idle ");
1119 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
1120 count
+= scnprintf(buf
+ count
, len
- count
, "standby ");
1123 count
= scnprintf(buf
, len
, "no parameters");
1127 count
+= scnprintf(buf
+ count
, len
- count
, ", %s",
1128 _regulator_is_enabled(rdev
) ? "enabled" : "disabled");
1130 rdev_dbg(rdev
, "%s\n", buf
);
1132 #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1133 static inline void print_constraints_debug(struct regulator_dev
*rdev
) {}
1134 #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1136 static void print_constraints(struct regulator_dev
*rdev
)
1138 struct regulation_constraints
*constraints
= rdev
->constraints
;
1140 print_constraints_debug(rdev
);
1142 if ((constraints
->min_uV
!= constraints
->max_uV
) &&
1143 !regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
))
1145 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1148 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
1149 struct regulation_constraints
*constraints
)
1151 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1154 /* do we need to apply the constraint voltage */
1155 if (rdev
->constraints
->apply_uV
&&
1156 rdev
->constraints
->min_uV
&& rdev
->constraints
->max_uV
) {
1157 int target_min
, target_max
;
1158 int current_uV
= regulator_get_voltage_rdev(rdev
);
1160 if (current_uV
== -ENOTRECOVERABLE
) {
1161 /* This regulator can't be read and must be initialized */
1162 rdev_info(rdev
, "Setting %d-%duV\n",
1163 rdev
->constraints
->min_uV
,
1164 rdev
->constraints
->max_uV
);
1165 _regulator_do_set_voltage(rdev
,
1166 rdev
->constraints
->min_uV
,
1167 rdev
->constraints
->max_uV
);
1168 current_uV
= regulator_get_voltage_rdev(rdev
);
1171 if (current_uV
< 0) {
1173 "failed to get the current voltage: %pe\n",
1174 ERR_PTR(current_uV
));
1179 * If we're below the minimum voltage move up to the
1180 * minimum voltage, if we're above the maximum voltage
1181 * then move down to the maximum.
1183 target_min
= current_uV
;
1184 target_max
= current_uV
;
1186 if (current_uV
< rdev
->constraints
->min_uV
) {
1187 target_min
= rdev
->constraints
->min_uV
;
1188 target_max
= rdev
->constraints
->min_uV
;
1191 if (current_uV
> rdev
->constraints
->max_uV
) {
1192 target_min
= rdev
->constraints
->max_uV
;
1193 target_max
= rdev
->constraints
->max_uV
;
1196 if (target_min
!= current_uV
|| target_max
!= current_uV
) {
1197 rdev_info(rdev
, "Bringing %duV into %d-%duV\n",
1198 current_uV
, target_min
, target_max
);
1199 ret
= _regulator_do_set_voltage(
1200 rdev
, target_min
, target_max
);
1203 "failed to apply %d-%duV constraint: %pe\n",
1204 target_min
, target_max
, ERR_PTR(ret
));
1210 /* constrain machine-level voltage specs to fit
1211 * the actual range supported by this regulator.
1213 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
1214 int count
= rdev
->desc
->n_voltages
;
1216 int min_uV
= INT_MAX
;
1217 int max_uV
= INT_MIN
;
1218 int cmin
= constraints
->min_uV
;
1219 int cmax
= constraints
->max_uV
;
1221 /* it's safe to autoconfigure fixed-voltage supplies
1222 and the constraints are used by list_voltage. */
1223 if (count
== 1 && !cmin
) {
1226 constraints
->min_uV
= cmin
;
1227 constraints
->max_uV
= cmax
;
1230 /* voltage constraints are optional */
1231 if ((cmin
== 0) && (cmax
== 0))
1234 /* else require explicit machine-level constraints */
1235 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
1236 rdev_err(rdev
, "invalid voltage constraints\n");
1240 /* no need to loop voltages if range is continuous */
1241 if (rdev
->desc
->continuous_voltage_range
)
1244 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1245 for (i
= 0; i
< count
; i
++) {
1248 value
= ops
->list_voltage(rdev
, i
);
1252 /* maybe adjust [min_uV..max_uV] */
1253 if (value
>= cmin
&& value
< min_uV
)
1255 if (value
<= cmax
&& value
> max_uV
)
1259 /* final: [min_uV..max_uV] valid iff constraints valid */
1260 if (max_uV
< min_uV
) {
1262 "unsupportable voltage constraints %u-%uuV\n",
1267 /* use regulator's subset of machine constraints */
1268 if (constraints
->min_uV
< min_uV
) {
1269 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
1270 constraints
->min_uV
, min_uV
);
1271 constraints
->min_uV
= min_uV
;
1273 if (constraints
->max_uV
> max_uV
) {
1274 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
1275 constraints
->max_uV
, max_uV
);
1276 constraints
->max_uV
= max_uV
;
1283 static int machine_constraints_current(struct regulator_dev
*rdev
,
1284 struct regulation_constraints
*constraints
)
1286 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1289 if (!constraints
->min_uA
&& !constraints
->max_uA
)
1292 if (constraints
->min_uA
> constraints
->max_uA
) {
1293 rdev_err(rdev
, "Invalid current constraints\n");
1297 if (!ops
->set_current_limit
|| !ops
->get_current_limit
) {
1298 rdev_warn(rdev
, "Operation of current configuration missing\n");
1302 /* Set regulator current in constraints range */
1303 ret
= ops
->set_current_limit(rdev
, constraints
->min_uA
,
1304 constraints
->max_uA
);
1306 rdev_err(rdev
, "Failed to set current constraint, %d\n", ret
);
1313 static int _regulator_do_enable(struct regulator_dev
*rdev
);
1316 * set_machine_constraints - sets regulator constraints
1317 * @rdev: regulator source
1318 * @constraints: constraints to apply
1320 * Allows platform initialisation code to define and constrain
1321 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1322 * Constraints *must* be set by platform code in order for some
1323 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1326 static int set_machine_constraints(struct regulator_dev
*rdev
,
1327 const struct regulation_constraints
*constraints
)
1330 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1333 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
1336 rdev
->constraints
= kzalloc(sizeof(*constraints
),
1338 if (!rdev
->constraints
)
1341 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
1345 ret
= machine_constraints_current(rdev
, rdev
->constraints
);
1349 if (rdev
->constraints
->ilim_uA
&& ops
->set_input_current_limit
) {
1350 ret
= ops
->set_input_current_limit(rdev
,
1351 rdev
->constraints
->ilim_uA
);
1353 rdev_err(rdev
, "failed to set input limit: %pe\n", ERR_PTR(ret
));
1358 /* do we need to setup our suspend state */
1359 if (rdev
->constraints
->initial_state
) {
1360 ret
= suspend_set_initial_state(rdev
);
1362 rdev_err(rdev
, "failed to set suspend state: %pe\n", ERR_PTR(ret
));
1367 if (rdev
->constraints
->initial_mode
) {
1368 if (!ops
->set_mode
) {
1369 rdev_err(rdev
, "no set_mode operation\n");
1373 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
1375 rdev_err(rdev
, "failed to set initial mode: %pe\n", ERR_PTR(ret
));
1378 } else if (rdev
->constraints
->system_load
) {
1380 * We'll only apply the initial system load if an
1381 * initial mode wasn't specified.
1383 drms_uA_update(rdev
);
1386 if ((rdev
->constraints
->ramp_delay
|| rdev
->constraints
->ramp_disable
)
1387 && ops
->set_ramp_delay
) {
1388 ret
= ops
->set_ramp_delay(rdev
, rdev
->constraints
->ramp_delay
);
1390 rdev_err(rdev
, "failed to set ramp_delay: %pe\n", ERR_PTR(ret
));
1395 if (rdev
->constraints
->pull_down
&& ops
->set_pull_down
) {
1396 ret
= ops
->set_pull_down(rdev
);
1398 rdev_err(rdev
, "failed to set pull down: %pe\n", ERR_PTR(ret
));
1403 if (rdev
->constraints
->soft_start
&& ops
->set_soft_start
) {
1404 ret
= ops
->set_soft_start(rdev
);
1406 rdev_err(rdev
, "failed to set soft start: %pe\n", ERR_PTR(ret
));
1411 if (rdev
->constraints
->over_current_protection
1412 && ops
->set_over_current_protection
) {
1413 ret
= ops
->set_over_current_protection(rdev
);
1415 rdev_err(rdev
, "failed to set over current protection: %pe\n",
1421 if (rdev
->constraints
->active_discharge
&& ops
->set_active_discharge
) {
1422 bool ad_state
= (rdev
->constraints
->active_discharge
==
1423 REGULATOR_ACTIVE_DISCHARGE_ENABLE
) ? true : false;
1425 ret
= ops
->set_active_discharge(rdev
, ad_state
);
1427 rdev_err(rdev
, "failed to set active discharge: %pe\n", ERR_PTR(ret
));
1432 /* If the constraints say the regulator should be on at this point
1433 * and we have control then make sure it is enabled.
1435 if (rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) {
1437 ret
= regulator_enable(rdev
->supply
);
1439 _regulator_put(rdev
->supply
);
1440 rdev
->supply
= NULL
;
1445 ret
= _regulator_do_enable(rdev
);
1446 if (ret
< 0 && ret
!= -EINVAL
) {
1447 rdev_err(rdev
, "failed to enable: %pe\n", ERR_PTR(ret
));
1451 if (rdev
->constraints
->always_on
)
1455 print_constraints(rdev
);
1460 * set_supply - set regulator supply regulator
1461 * @rdev: regulator name
1462 * @supply_rdev: supply regulator name
1464 * Called by platform initialisation code to set the supply regulator for this
1465 * regulator. This ensures that a regulators supply will also be enabled by the
1466 * core if it's child is enabled.
1468 static int set_supply(struct regulator_dev
*rdev
,
1469 struct regulator_dev
*supply_rdev
)
1473 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
1475 if (!try_module_get(supply_rdev
->owner
))
1478 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
1479 if (rdev
->supply
== NULL
) {
1483 supply_rdev
->open_count
++;
1489 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1490 * @rdev: regulator source
1491 * @consumer_dev_name: dev_name() string for device supply applies to
1492 * @supply: symbolic name for supply
1494 * Allows platform initialisation code to map physical regulator
1495 * sources to symbolic names for supplies for use by devices. Devices
1496 * should use these symbolic names to request regulators, avoiding the
1497 * need to provide board-specific regulator names as platform data.
1499 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1500 const char *consumer_dev_name
,
1503 struct regulator_map
*node
, *new_node
;
1509 if (consumer_dev_name
!= NULL
)
1514 new_node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1515 if (new_node
== NULL
)
1518 new_node
->regulator
= rdev
;
1519 new_node
->supply
= supply
;
1522 new_node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1523 if (new_node
->dev_name
== NULL
) {
1529 mutex_lock(®ulator_list_mutex
);
1530 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1531 if (node
->dev_name
&& consumer_dev_name
) {
1532 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1534 } else if (node
->dev_name
|| consumer_dev_name
) {
1538 if (strcmp(node
->supply
, supply
) != 0)
1541 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1543 dev_name(&node
->regulator
->dev
),
1544 node
->regulator
->desc
->name
,
1546 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1550 list_add(&new_node
->list
, ®ulator_map_list
);
1551 mutex_unlock(®ulator_list_mutex
);
1556 mutex_unlock(®ulator_list_mutex
);
1557 kfree(new_node
->dev_name
);
1562 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1564 struct regulator_map
*node
, *n
;
1566 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1567 if (rdev
== node
->regulator
) {
1568 list_del(&node
->list
);
1569 kfree(node
->dev_name
);
1575 #ifdef CONFIG_DEBUG_FS
1576 static ssize_t
constraint_flags_read_file(struct file
*file
,
1577 char __user
*user_buf
,
1578 size_t count
, loff_t
*ppos
)
1580 const struct regulator
*regulator
= file
->private_data
;
1581 const struct regulation_constraints
*c
= regulator
->rdev
->constraints
;
1588 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1592 ret
= snprintf(buf
, PAGE_SIZE
,
1596 "ramp_disable: %u\n"
1599 "over_current_protection: %u\n",
1606 c
->over_current_protection
);
1608 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1616 static const struct file_operations constraint_flags_fops
= {
1617 #ifdef CONFIG_DEBUG_FS
1618 .open
= simple_open
,
1619 .read
= constraint_flags_read_file
,
1620 .llseek
= default_llseek
,
1624 #define REG_STR_SIZE 64
1626 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1628 const char *supply_name
)
1630 struct regulator
*regulator
;
1634 char buf
[REG_STR_SIZE
];
1637 size
= snprintf(buf
, REG_STR_SIZE
, "%s-%s",
1638 dev
->kobj
.name
, supply_name
);
1639 if (size
>= REG_STR_SIZE
)
1642 supply_name
= kstrdup(buf
, GFP_KERNEL
);
1643 if (supply_name
== NULL
)
1646 supply_name
= kstrdup_const(supply_name
, GFP_KERNEL
);
1647 if (supply_name
== NULL
)
1651 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1652 if (regulator
== NULL
) {
1657 regulator
->rdev
= rdev
;
1658 regulator
->supply_name
= supply_name
;
1660 regulator_lock(rdev
);
1661 list_add(®ulator
->list
, &rdev
->consumer_list
);
1662 regulator_unlock(rdev
);
1665 regulator
->dev
= dev
;
1667 /* Add a link to the device sysfs entry */
1668 err
= sysfs_create_link_nowarn(&rdev
->dev
.kobj
, &dev
->kobj
,
1671 rdev_dbg(rdev
, "could not add device link %s: %pe\n",
1672 dev
->kobj
.name
, ERR_PTR(err
));
1677 regulator
->debugfs
= debugfs_create_dir(supply_name
,
1679 if (!regulator
->debugfs
) {
1680 rdev_dbg(rdev
, "Failed to create debugfs directory\n");
1682 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1683 ®ulator
->uA_load
);
1684 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1685 ®ulator
->voltage
[PM_SUSPEND_ON
].min_uV
);
1686 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1687 ®ulator
->voltage
[PM_SUSPEND_ON
].max_uV
);
1688 debugfs_create_file("constraint_flags", 0444,
1689 regulator
->debugfs
, regulator
,
1690 &constraint_flags_fops
);
1694 * Check now if the regulator is an always on regulator - if
1695 * it is then we don't need to do nearly so much work for
1696 * enable/disable calls.
1698 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
) &&
1699 _regulator_is_enabled(rdev
))
1700 regulator
->always_on
= true;
1705 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1707 if (rdev
->constraints
&& rdev
->constraints
->enable_time
)
1708 return rdev
->constraints
->enable_time
;
1709 if (rdev
->desc
->ops
->enable_time
)
1710 return rdev
->desc
->ops
->enable_time(rdev
);
1711 return rdev
->desc
->enable_time
;
1714 static struct regulator_supply_alias
*regulator_find_supply_alias(
1715 struct device
*dev
, const char *supply
)
1717 struct regulator_supply_alias
*map
;
1719 list_for_each_entry(map
, ®ulator_supply_alias_list
, list
)
1720 if (map
->src_dev
== dev
&& strcmp(map
->src_supply
, supply
) == 0)
1726 static void regulator_supply_alias(struct device
**dev
, const char **supply
)
1728 struct regulator_supply_alias
*map
;
1730 map
= regulator_find_supply_alias(*dev
, *supply
);
1732 dev_dbg(*dev
, "Mapping supply %s to %s,%s\n",
1733 *supply
, map
->alias_supply
,
1734 dev_name(map
->alias_dev
));
1735 *dev
= map
->alias_dev
;
1736 *supply
= map
->alias_supply
;
1740 static int regulator_match(struct device
*dev
, const void *data
)
1742 struct regulator_dev
*r
= dev_to_rdev(dev
);
1744 return strcmp(rdev_get_name(r
), data
) == 0;
1747 static struct regulator_dev
*regulator_lookup_by_name(const char *name
)
1751 dev
= class_find_device(®ulator_class
, NULL
, name
, regulator_match
);
1753 return dev
? dev_to_rdev(dev
) : NULL
;
1757 * regulator_dev_lookup - lookup a regulator device.
1758 * @dev: device for regulator "consumer".
1759 * @supply: Supply name or regulator ID.
1761 * If successful, returns a struct regulator_dev that corresponds to the name
1762 * @supply and with the embedded struct device refcount incremented by one.
1763 * The refcount must be dropped by calling put_device().
1764 * On failure one of the following ERR-PTR-encoded values is returned:
1765 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1768 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1771 struct regulator_dev
*r
= NULL
;
1772 struct device_node
*node
;
1773 struct regulator_map
*map
;
1774 const char *devname
= NULL
;
1776 regulator_supply_alias(&dev
, &supply
);
1778 /* first do a dt based lookup */
1779 if (dev
&& dev
->of_node
) {
1780 node
= of_get_regulator(dev
, supply
);
1782 r
= of_find_regulator_by_node(node
);
1787 * We have a node, but there is no device.
1788 * assume it has not registered yet.
1790 return ERR_PTR(-EPROBE_DEFER
);
1794 /* if not found, try doing it non-dt way */
1796 devname
= dev_name(dev
);
1798 mutex_lock(®ulator_list_mutex
);
1799 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1800 /* If the mapping has a device set up it must match */
1801 if (map
->dev_name
&&
1802 (!devname
|| strcmp(map
->dev_name
, devname
)))
1805 if (strcmp(map
->supply
, supply
) == 0 &&
1806 get_device(&map
->regulator
->dev
)) {
1811 mutex_unlock(®ulator_list_mutex
);
1816 r
= regulator_lookup_by_name(supply
);
1820 return ERR_PTR(-ENODEV
);
1823 static int regulator_resolve_supply(struct regulator_dev
*rdev
)
1825 struct regulator_dev
*r
;
1826 struct device
*dev
= rdev
->dev
.parent
;
1829 /* No supply to resolve? */
1830 if (!rdev
->supply_name
)
1833 /* Supply already resolved? */
1837 r
= regulator_dev_lookup(dev
, rdev
->supply_name
);
1841 /* Did the lookup explicitly defer for us? */
1842 if (ret
== -EPROBE_DEFER
)
1845 if (have_full_constraints()) {
1846 r
= dummy_regulator_rdev
;
1847 get_device(&r
->dev
);
1849 dev_err(dev
, "Failed to resolve %s-supply for %s\n",
1850 rdev
->supply_name
, rdev
->desc
->name
);
1851 return -EPROBE_DEFER
;
1856 * If the supply's parent device is not the same as the
1857 * regulator's parent device, then ensure the parent device
1858 * is bound before we resolve the supply, in case the parent
1859 * device get probe deferred and unregisters the supply.
1861 if (r
->dev
.parent
&& r
->dev
.parent
!= rdev
->dev
.parent
) {
1862 if (!device_is_bound(r
->dev
.parent
)) {
1863 put_device(&r
->dev
);
1864 return -EPROBE_DEFER
;
1868 /* Recursively resolve the supply of the supply */
1869 ret
= regulator_resolve_supply(r
);
1871 put_device(&r
->dev
);
1875 ret
= set_supply(rdev
, r
);
1877 put_device(&r
->dev
);
1882 * In set_machine_constraints() we may have turned this regulator on
1883 * but we couldn't propagate to the supply if it hadn't been resolved
1886 if (rdev
->use_count
) {
1887 ret
= regulator_enable(rdev
->supply
);
1889 _regulator_put(rdev
->supply
);
1890 rdev
->supply
= NULL
;
1898 /* Internal regulator request function */
1899 struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1900 enum regulator_get_type get_type
)
1902 struct regulator_dev
*rdev
;
1903 struct regulator
*regulator
;
1904 struct device_link
*link
;
1907 if (get_type
>= MAX_GET_TYPE
) {
1908 dev_err(dev
, "invalid type %d in %s\n", get_type
, __func__
);
1909 return ERR_PTR(-EINVAL
);
1913 pr_err("get() with no identifier\n");
1914 return ERR_PTR(-EINVAL
);
1917 rdev
= regulator_dev_lookup(dev
, id
);
1919 ret
= PTR_ERR(rdev
);
1922 * If regulator_dev_lookup() fails with error other
1923 * than -ENODEV our job here is done, we simply return it.
1926 return ERR_PTR(ret
);
1928 if (!have_full_constraints()) {
1930 "incomplete constraints, dummy supplies not allowed\n");
1931 return ERR_PTR(-ENODEV
);
1937 * Assume that a regulator is physically present and
1938 * enabled, even if it isn't hooked up, and just
1941 dev_warn(dev
, "supply %s not found, using dummy regulator\n", id
);
1942 rdev
= dummy_regulator_rdev
;
1943 get_device(&rdev
->dev
);
1948 "dummy supplies not allowed for exclusive requests\n");
1952 return ERR_PTR(-ENODEV
);
1956 if (rdev
->exclusive
) {
1957 regulator
= ERR_PTR(-EPERM
);
1958 put_device(&rdev
->dev
);
1962 if (get_type
== EXCLUSIVE_GET
&& rdev
->open_count
) {
1963 regulator
= ERR_PTR(-EBUSY
);
1964 put_device(&rdev
->dev
);
1968 mutex_lock(®ulator_list_mutex
);
1969 ret
= (rdev
->coupling_desc
.n_resolved
!= rdev
->coupling_desc
.n_coupled
);
1970 mutex_unlock(®ulator_list_mutex
);
1973 regulator
= ERR_PTR(-EPROBE_DEFER
);
1974 put_device(&rdev
->dev
);
1978 ret
= regulator_resolve_supply(rdev
);
1980 regulator
= ERR_PTR(ret
);
1981 put_device(&rdev
->dev
);
1985 if (!try_module_get(rdev
->owner
)) {
1986 regulator
= ERR_PTR(-EPROBE_DEFER
);
1987 put_device(&rdev
->dev
);
1991 regulator
= create_regulator(rdev
, dev
, id
);
1992 if (regulator
== NULL
) {
1993 regulator
= ERR_PTR(-ENOMEM
);
1994 module_put(rdev
->owner
);
1995 put_device(&rdev
->dev
);
2000 if (get_type
== EXCLUSIVE_GET
) {
2001 rdev
->exclusive
= 1;
2003 ret
= _regulator_is_enabled(rdev
);
2005 rdev
->use_count
= 1;
2007 rdev
->use_count
= 0;
2010 link
= device_link_add(dev
, &rdev
->dev
, DL_FLAG_STATELESS
);
2011 if (!IS_ERR_OR_NULL(link
))
2012 regulator
->device_link
= true;
2018 * regulator_get - lookup and obtain a reference 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 * Use of supply names configured via regulator_set_device_supply() is
2026 * strongly encouraged. It is recommended that the supply name used
2027 * should match the name used for the supply and/or the relevant
2028 * device pins in the datasheet.
2030 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
2032 return _regulator_get(dev
, id
, NORMAL_GET
);
2034 EXPORT_SYMBOL_GPL(regulator_get
);
2037 * regulator_get_exclusive - obtain exclusive access to a regulator.
2038 * @dev: device for regulator "consumer"
2039 * @id: Supply name or regulator ID.
2041 * Returns a struct regulator corresponding to the regulator producer,
2042 * or IS_ERR() condition containing errno. Other consumers will be
2043 * unable to obtain this regulator while this reference is held and the
2044 * use count for the regulator will be initialised to reflect the current
2045 * state of the regulator.
2047 * This is intended for use by consumers which cannot tolerate shared
2048 * use of the regulator such as those which need to force the
2049 * regulator off for correct operation of the hardware they are
2052 * Use of supply names configured via regulator_set_device_supply() is
2053 * strongly encouraged. It is recommended that the supply name used
2054 * should match the name used for the supply and/or the relevant
2055 * device pins in the datasheet.
2057 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
2059 return _regulator_get(dev
, id
, EXCLUSIVE_GET
);
2061 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
2064 * regulator_get_optional - obtain optional access to a regulator.
2065 * @dev: device for regulator "consumer"
2066 * @id: Supply name or regulator ID.
2068 * Returns a struct regulator corresponding to the regulator producer,
2069 * or IS_ERR() condition containing errno.
2071 * This is intended for use by consumers for devices which can have
2072 * some supplies unconnected in normal use, such as some MMC devices.
2073 * It can allow the regulator core to provide stub supplies for other
2074 * supplies requested using normal regulator_get() calls without
2075 * disrupting the operation of drivers that can handle absent
2078 * Use of supply names configured via regulator_set_device_supply() is
2079 * strongly encouraged. It is recommended that the supply name used
2080 * should match the name used for the supply and/or the relevant
2081 * device pins in the datasheet.
2083 struct regulator
*regulator_get_optional(struct device
*dev
, const char *id
)
2085 return _regulator_get(dev
, id
, OPTIONAL_GET
);
2087 EXPORT_SYMBOL_GPL(regulator_get_optional
);
2089 static void destroy_regulator(struct regulator
*regulator
)
2091 struct regulator_dev
*rdev
= regulator
->rdev
;
2093 debugfs_remove_recursive(regulator
->debugfs
);
2095 if (regulator
->dev
) {
2096 if (regulator
->device_link
)
2097 device_link_remove(regulator
->dev
, &rdev
->dev
);
2099 /* remove any sysfs entries */
2100 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
2103 regulator_lock(rdev
);
2104 list_del(®ulator
->list
);
2107 rdev
->exclusive
= 0;
2108 regulator_unlock(rdev
);
2110 kfree_const(regulator
->supply_name
);
2114 /* regulator_list_mutex lock held by regulator_put() */
2115 static void _regulator_put(struct regulator
*regulator
)
2117 struct regulator_dev
*rdev
;
2119 if (IS_ERR_OR_NULL(regulator
))
2122 lockdep_assert_held_once(®ulator_list_mutex
);
2124 /* Docs say you must disable before calling regulator_put() */
2125 WARN_ON(regulator
->enable_count
);
2127 rdev
= regulator
->rdev
;
2129 destroy_regulator(regulator
);
2131 module_put(rdev
->owner
);
2132 put_device(&rdev
->dev
);
2136 * regulator_put - "free" the regulator source
2137 * @regulator: regulator source
2139 * Note: drivers must ensure that all regulator_enable calls made on this
2140 * regulator source are balanced by regulator_disable calls prior to calling
2143 void regulator_put(struct regulator
*regulator
)
2145 mutex_lock(®ulator_list_mutex
);
2146 _regulator_put(regulator
);
2147 mutex_unlock(®ulator_list_mutex
);
2149 EXPORT_SYMBOL_GPL(regulator_put
);
2152 * regulator_register_supply_alias - Provide device alias for supply lookup
2154 * @dev: device that will be given as the regulator "consumer"
2155 * @id: Supply name or regulator ID
2156 * @alias_dev: device that should be used to lookup the supply
2157 * @alias_id: Supply name or regulator ID that should be used to lookup the
2160 * All lookups for id on dev will instead be conducted for alias_id on
2163 int regulator_register_supply_alias(struct device
*dev
, const char *id
,
2164 struct device
*alias_dev
,
2165 const char *alias_id
)
2167 struct regulator_supply_alias
*map
;
2169 map
= regulator_find_supply_alias(dev
, id
);
2173 map
= kzalloc(sizeof(struct regulator_supply_alias
), GFP_KERNEL
);
2178 map
->src_supply
= id
;
2179 map
->alias_dev
= alias_dev
;
2180 map
->alias_supply
= alias_id
;
2182 list_add(&map
->list
, ®ulator_supply_alias_list
);
2184 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2185 id
, dev_name(dev
), alias_id
, dev_name(alias_dev
));
2189 EXPORT_SYMBOL_GPL(regulator_register_supply_alias
);
2192 * regulator_unregister_supply_alias - Remove device alias
2194 * @dev: device that will be given as the regulator "consumer"
2195 * @id: Supply name or regulator ID
2197 * Remove a lookup alias if one exists for id on dev.
2199 void regulator_unregister_supply_alias(struct device
*dev
, const char *id
)
2201 struct regulator_supply_alias
*map
;
2203 map
= regulator_find_supply_alias(dev
, id
);
2205 list_del(&map
->list
);
2209 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias
);
2212 * regulator_bulk_register_supply_alias - register multiple aliases
2214 * @dev: device that will be given as the regulator "consumer"
2215 * @id: List of supply names or regulator IDs
2216 * @alias_dev: device that should be used to lookup the supply
2217 * @alias_id: List of supply names or regulator IDs that should be used to
2219 * @num_id: Number of aliases to register
2221 * @return 0 on success, an errno on failure.
2223 * This helper function allows drivers to register several supply
2224 * aliases in one operation. If any of the aliases cannot be
2225 * registered any aliases that were registered will be removed
2226 * before returning to the caller.
2228 int regulator_bulk_register_supply_alias(struct device
*dev
,
2229 const char *const *id
,
2230 struct device
*alias_dev
,
2231 const char *const *alias_id
,
2237 for (i
= 0; i
< num_id
; ++i
) {
2238 ret
= regulator_register_supply_alias(dev
, id
[i
], alias_dev
,
2248 "Failed to create supply alias %s,%s -> %s,%s\n",
2249 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
2252 regulator_unregister_supply_alias(dev
, id
[i
]);
2256 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias
);
2259 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2261 * @dev: device that will be given as the regulator "consumer"
2262 * @id: List of supply names or regulator IDs
2263 * @num_id: Number of aliases to unregister
2265 * This helper function allows drivers to unregister several supply
2266 * aliases in one operation.
2268 void regulator_bulk_unregister_supply_alias(struct device
*dev
,
2269 const char *const *id
,
2274 for (i
= 0; i
< num_id
; ++i
)
2275 regulator_unregister_supply_alias(dev
, id
[i
]);
2277 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias
);
2280 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2281 static int regulator_ena_gpio_request(struct regulator_dev
*rdev
,
2282 const struct regulator_config
*config
)
2284 struct regulator_enable_gpio
*pin
, *new_pin
;
2285 struct gpio_desc
*gpiod
;
2287 gpiod
= config
->ena_gpiod
;
2288 new_pin
= kzalloc(sizeof(*new_pin
), GFP_KERNEL
);
2290 mutex_lock(®ulator_list_mutex
);
2292 list_for_each_entry(pin
, ®ulator_ena_gpio_list
, list
) {
2293 if (pin
->gpiod
== gpiod
) {
2294 rdev_dbg(rdev
, "GPIO is already used\n");
2295 goto update_ena_gpio_to_rdev
;
2299 if (new_pin
== NULL
) {
2300 mutex_unlock(®ulator_list_mutex
);
2308 list_add(&pin
->list
, ®ulator_ena_gpio_list
);
2310 update_ena_gpio_to_rdev
:
2311 pin
->request_count
++;
2312 rdev
->ena_pin
= pin
;
2314 mutex_unlock(®ulator_list_mutex
);
2320 static void regulator_ena_gpio_free(struct regulator_dev
*rdev
)
2322 struct regulator_enable_gpio
*pin
, *n
;
2327 /* Free the GPIO only in case of no use */
2328 list_for_each_entry_safe(pin
, n
, ®ulator_ena_gpio_list
, list
) {
2329 if (pin
!= rdev
->ena_pin
)
2332 if (--pin
->request_count
)
2335 gpiod_put(pin
->gpiod
);
2336 list_del(&pin
->list
);
2341 rdev
->ena_pin
= NULL
;
2345 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2346 * @rdev: regulator_dev structure
2347 * @enable: enable GPIO at initial use?
2349 * GPIO is enabled in case of initial use. (enable_count is 0)
2350 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2352 static int regulator_ena_gpio_ctrl(struct regulator_dev
*rdev
, bool enable
)
2354 struct regulator_enable_gpio
*pin
= rdev
->ena_pin
;
2360 /* Enable GPIO at initial use */
2361 if (pin
->enable_count
== 0)
2362 gpiod_set_value_cansleep(pin
->gpiod
, 1);
2364 pin
->enable_count
++;
2366 if (pin
->enable_count
> 1) {
2367 pin
->enable_count
--;
2371 /* Disable GPIO if not used */
2372 if (pin
->enable_count
<= 1) {
2373 gpiod_set_value_cansleep(pin
->gpiod
, 0);
2374 pin
->enable_count
= 0;
2382 * _regulator_enable_delay - a delay helper function
2383 * @delay: time to delay in microseconds
2385 * Delay for the requested amount of time as per the guidelines in:
2387 * Documentation/timers/timers-howto.rst
2389 * The assumption here is that regulators will never be enabled in
2390 * atomic context and therefore sleeping functions can be used.
2392 static void _regulator_enable_delay(unsigned int delay
)
2394 unsigned int ms
= delay
/ 1000;
2395 unsigned int us
= delay
% 1000;
2399 * For small enough values, handle super-millisecond
2400 * delays in the usleep_range() call below.
2409 * Give the scheduler some room to coalesce with any other
2410 * wakeup sources. For delays shorter than 10 us, don't even
2411 * bother setting up high-resolution timers and just busy-
2415 usleep_range(us
, us
+ 100);
2421 * _regulator_check_status_enabled
2423 * A helper function to check if the regulator status can be interpreted
2424 * as 'regulator is enabled'.
2425 * @rdev: the regulator device to check
2428 * * 1 - if status shows regulator is in enabled state
2429 * * 0 - if not enabled state
2430 * * Error Value - as received from ops->get_status()
2432 static inline int _regulator_check_status_enabled(struct regulator_dev
*rdev
)
2434 int ret
= rdev
->desc
->ops
->get_status(rdev
);
2437 rdev_info(rdev
, "get_status returned error: %d\n", ret
);
2442 case REGULATOR_STATUS_OFF
:
2443 case REGULATOR_STATUS_ERROR
:
2444 case REGULATOR_STATUS_UNDEFINED
:
2451 static int _regulator_do_enable(struct regulator_dev
*rdev
)
2455 /* Query before enabling in case configuration dependent. */
2456 ret
= _regulator_get_enable_time(rdev
);
2460 rdev_warn(rdev
, "enable_time() failed: %pe\n", ERR_PTR(ret
));
2464 trace_regulator_enable(rdev_get_name(rdev
));
2466 if (rdev
->desc
->off_on_delay
) {
2467 /* if needed, keep a distance of off_on_delay from last time
2468 * this regulator was disabled.
2470 unsigned long start_jiffy
= jiffies
;
2471 unsigned long intended
, max_delay
, remaining
;
2473 max_delay
= usecs_to_jiffies(rdev
->desc
->off_on_delay
);
2474 intended
= rdev
->last_off_jiffy
+ max_delay
;
2476 if (time_before(start_jiffy
, intended
)) {
2477 /* calc remaining jiffies to deal with one-time
2479 * in case of multiple timer wrapping, either it can be
2480 * detected by out-of-range remaining, or it cannot be
2481 * detected and we get a penalty of
2482 * _regulator_enable_delay().
2484 remaining
= intended
- start_jiffy
;
2485 if (remaining
<= max_delay
)
2486 _regulator_enable_delay(
2487 jiffies_to_usecs(remaining
));
2491 if (rdev
->ena_pin
) {
2492 if (!rdev
->ena_gpio_state
) {
2493 ret
= regulator_ena_gpio_ctrl(rdev
, true);
2496 rdev
->ena_gpio_state
= 1;
2498 } else if (rdev
->desc
->ops
->enable
) {
2499 ret
= rdev
->desc
->ops
->enable(rdev
);
2506 /* Allow the regulator to ramp; it would be useful to extend
2507 * this for bulk operations so that the regulators can ramp
2509 trace_regulator_enable_delay(rdev_get_name(rdev
));
2511 /* If poll_enabled_time is set, poll upto the delay calculated
2512 * above, delaying poll_enabled_time uS to check if the regulator
2513 * actually got enabled.
2514 * If the regulator isn't enabled after enable_delay has
2515 * expired, return -ETIMEDOUT.
2517 if (rdev
->desc
->poll_enabled_time
) {
2518 unsigned int time_remaining
= delay
;
2520 while (time_remaining
> 0) {
2521 _regulator_enable_delay(rdev
->desc
->poll_enabled_time
);
2523 if (rdev
->desc
->ops
->get_status
) {
2524 ret
= _regulator_check_status_enabled(rdev
);
2529 } else if (rdev
->desc
->ops
->is_enabled(rdev
))
2532 time_remaining
-= rdev
->desc
->poll_enabled_time
;
2535 if (time_remaining
<= 0) {
2536 rdev_err(rdev
, "Enabled check timed out\n");
2540 _regulator_enable_delay(delay
);
2543 trace_regulator_enable_complete(rdev_get_name(rdev
));
2549 * _regulator_handle_consumer_enable - handle that a consumer enabled
2550 * @regulator: regulator source
2552 * Some things on a regulator consumer (like the contribution towards total
2553 * load on the regulator) only have an effect when the consumer wants the
2554 * regulator enabled. Explained in example with two consumers of the same
2556 * consumer A: set_load(100); => total load = 0
2557 * consumer A: regulator_enable(); => total load = 100
2558 * consumer B: set_load(1000); => total load = 100
2559 * consumer B: regulator_enable(); => total load = 1100
2560 * consumer A: regulator_disable(); => total_load = 1000
2562 * This function (together with _regulator_handle_consumer_disable) is
2563 * responsible for keeping track of the refcount for a given regulator consumer
2564 * and applying / unapplying these things.
2566 * Returns 0 upon no error; -error upon error.
2568 static int _regulator_handle_consumer_enable(struct regulator
*regulator
)
2570 struct regulator_dev
*rdev
= regulator
->rdev
;
2572 lockdep_assert_held_once(&rdev
->mutex
.base
);
2574 regulator
->enable_count
++;
2575 if (regulator
->uA_load
&& regulator
->enable_count
== 1)
2576 return drms_uA_update(rdev
);
2582 * _regulator_handle_consumer_disable - handle that a consumer disabled
2583 * @regulator: regulator source
2585 * The opposite of _regulator_handle_consumer_enable().
2587 * Returns 0 upon no error; -error upon error.
2589 static int _regulator_handle_consumer_disable(struct regulator
*regulator
)
2591 struct regulator_dev
*rdev
= regulator
->rdev
;
2593 lockdep_assert_held_once(&rdev
->mutex
.base
);
2595 if (!regulator
->enable_count
) {
2596 rdev_err(rdev
, "Underflow of regulator enable count\n");
2600 regulator
->enable_count
--;
2601 if (regulator
->uA_load
&& regulator
->enable_count
== 0)
2602 return drms_uA_update(rdev
);
2607 /* locks held by regulator_enable() */
2608 static int _regulator_enable(struct regulator
*regulator
)
2610 struct regulator_dev
*rdev
= regulator
->rdev
;
2613 lockdep_assert_held_once(&rdev
->mutex
.base
);
2615 if (rdev
->use_count
== 0 && rdev
->supply
) {
2616 ret
= _regulator_enable(rdev
->supply
);
2621 /* balance only if there are regulators coupled */
2622 if (rdev
->coupling_desc
.n_coupled
> 1) {
2623 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2625 goto err_disable_supply
;
2628 ret
= _regulator_handle_consumer_enable(regulator
);
2630 goto err_disable_supply
;
2632 if (rdev
->use_count
== 0) {
2633 /* The regulator may on if it's not switchable or left on */
2634 ret
= _regulator_is_enabled(rdev
);
2635 if (ret
== -EINVAL
|| ret
== 0) {
2636 if (!regulator_ops_is_valid(rdev
,
2637 REGULATOR_CHANGE_STATUS
)) {
2639 goto err_consumer_disable
;
2642 ret
= _regulator_do_enable(rdev
);
2644 goto err_consumer_disable
;
2646 _notifier_call_chain(rdev
, REGULATOR_EVENT_ENABLE
,
2648 } else if (ret
< 0) {
2649 rdev_err(rdev
, "is_enabled() failed: %pe\n", ERR_PTR(ret
));
2650 goto err_consumer_disable
;
2652 /* Fallthrough on positive return values - already enabled */
2659 err_consumer_disable
:
2660 _regulator_handle_consumer_disable(regulator
);
2663 if (rdev
->use_count
== 0 && rdev
->supply
)
2664 _regulator_disable(rdev
->supply
);
2670 * regulator_enable - enable regulator output
2671 * @regulator: regulator source
2673 * Request that the regulator be enabled with the regulator output at
2674 * the predefined voltage or current value. Calls to regulator_enable()
2675 * must be balanced with calls to regulator_disable().
2677 * NOTE: the output value can be set by other drivers, boot loader or may be
2678 * hardwired in the regulator.
2680 int regulator_enable(struct regulator
*regulator
)
2682 struct regulator_dev
*rdev
= regulator
->rdev
;
2683 struct ww_acquire_ctx ww_ctx
;
2686 regulator_lock_dependent(rdev
, &ww_ctx
);
2687 ret
= _regulator_enable(regulator
);
2688 regulator_unlock_dependent(rdev
, &ww_ctx
);
2692 EXPORT_SYMBOL_GPL(regulator_enable
);
2694 static int _regulator_do_disable(struct regulator_dev
*rdev
)
2698 trace_regulator_disable(rdev_get_name(rdev
));
2700 if (rdev
->ena_pin
) {
2701 if (rdev
->ena_gpio_state
) {
2702 ret
= regulator_ena_gpio_ctrl(rdev
, false);
2705 rdev
->ena_gpio_state
= 0;
2708 } else if (rdev
->desc
->ops
->disable
) {
2709 ret
= rdev
->desc
->ops
->disable(rdev
);
2714 /* cares about last_off_jiffy only if off_on_delay is required by
2717 if (rdev
->desc
->off_on_delay
)
2718 rdev
->last_off_jiffy
= jiffies
;
2720 trace_regulator_disable_complete(rdev_get_name(rdev
));
2725 /* locks held by regulator_disable() */
2726 static int _regulator_disable(struct regulator
*regulator
)
2728 struct regulator_dev
*rdev
= regulator
->rdev
;
2731 lockdep_assert_held_once(&rdev
->mutex
.base
);
2733 if (WARN(rdev
->use_count
<= 0,
2734 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
2737 /* are we the last user and permitted to disable ? */
2738 if (rdev
->use_count
== 1 &&
2739 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
2741 /* we are last user */
2742 if (regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
)) {
2743 ret
= _notifier_call_chain(rdev
,
2744 REGULATOR_EVENT_PRE_DISABLE
,
2746 if (ret
& NOTIFY_STOP_MASK
)
2749 ret
= _regulator_do_disable(rdev
);
2751 rdev_err(rdev
, "failed to disable: %pe\n", ERR_PTR(ret
));
2752 _notifier_call_chain(rdev
,
2753 REGULATOR_EVENT_ABORT_DISABLE
,
2757 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
2761 rdev
->use_count
= 0;
2762 } else if (rdev
->use_count
> 1) {
2767 ret
= _regulator_handle_consumer_disable(regulator
);
2769 if (ret
== 0 && rdev
->coupling_desc
.n_coupled
> 1)
2770 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2772 if (ret
== 0 && rdev
->use_count
== 0 && rdev
->supply
)
2773 ret
= _regulator_disable(rdev
->supply
);
2779 * regulator_disable - disable regulator output
2780 * @regulator: regulator source
2782 * Disable the regulator output voltage or current. Calls to
2783 * regulator_enable() must be balanced with calls to
2784 * regulator_disable().
2786 * NOTE: this will only disable the regulator output if no other consumer
2787 * devices have it enabled, the regulator device supports disabling and
2788 * machine constraints permit this operation.
2790 int regulator_disable(struct regulator
*regulator
)
2792 struct regulator_dev
*rdev
= regulator
->rdev
;
2793 struct ww_acquire_ctx ww_ctx
;
2796 regulator_lock_dependent(rdev
, &ww_ctx
);
2797 ret
= _regulator_disable(regulator
);
2798 regulator_unlock_dependent(rdev
, &ww_ctx
);
2802 EXPORT_SYMBOL_GPL(regulator_disable
);
2804 /* locks held by regulator_force_disable() */
2805 static int _regulator_force_disable(struct regulator_dev
*rdev
)
2809 lockdep_assert_held_once(&rdev
->mutex
.base
);
2811 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2812 REGULATOR_EVENT_PRE_DISABLE
, NULL
);
2813 if (ret
& NOTIFY_STOP_MASK
)
2816 ret
= _regulator_do_disable(rdev
);
2818 rdev_err(rdev
, "failed to force disable: %pe\n", ERR_PTR(ret
));
2819 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2820 REGULATOR_EVENT_ABORT_DISABLE
, NULL
);
2824 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2825 REGULATOR_EVENT_DISABLE
, NULL
);
2831 * regulator_force_disable - force disable regulator output
2832 * @regulator: regulator source
2834 * Forcibly disable the regulator output voltage or current.
2835 * NOTE: this *will* disable the regulator output even if other consumer
2836 * devices have it enabled. This should be used for situations when device
2837 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2839 int regulator_force_disable(struct regulator
*regulator
)
2841 struct regulator_dev
*rdev
= regulator
->rdev
;
2842 struct ww_acquire_ctx ww_ctx
;
2845 regulator_lock_dependent(rdev
, &ww_ctx
);
2847 ret
= _regulator_force_disable(regulator
->rdev
);
2849 if (rdev
->coupling_desc
.n_coupled
> 1)
2850 regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2852 if (regulator
->uA_load
) {
2853 regulator
->uA_load
= 0;
2854 ret
= drms_uA_update(rdev
);
2857 if (rdev
->use_count
!= 0 && rdev
->supply
)
2858 _regulator_disable(rdev
->supply
);
2860 regulator_unlock_dependent(rdev
, &ww_ctx
);
2864 EXPORT_SYMBOL_GPL(regulator_force_disable
);
2866 static void regulator_disable_work(struct work_struct
*work
)
2868 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
2870 struct ww_acquire_ctx ww_ctx
;
2872 struct regulator
*regulator
;
2873 int total_count
= 0;
2875 regulator_lock_dependent(rdev
, &ww_ctx
);
2878 * Workqueue functions queue the new work instance while the previous
2879 * work instance is being processed. Cancel the queued work instance
2880 * as the work instance under processing does the job of the queued
2883 cancel_delayed_work(&rdev
->disable_work
);
2885 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
2886 count
= regulator
->deferred_disables
;
2891 total_count
+= count
;
2892 regulator
->deferred_disables
= 0;
2894 for (i
= 0; i
< count
; i
++) {
2895 ret
= _regulator_disable(regulator
);
2897 rdev_err(rdev
, "Deferred disable failed: %pe\n",
2901 WARN_ON(!total_count
);
2903 if (rdev
->coupling_desc
.n_coupled
> 1)
2904 regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2906 regulator_unlock_dependent(rdev
, &ww_ctx
);
2910 * regulator_disable_deferred - disable regulator output with delay
2911 * @regulator: regulator source
2912 * @ms: milliseconds until the regulator is disabled
2914 * Execute regulator_disable() on the regulator after a delay. This
2915 * is intended for use with devices that require some time to quiesce.
2917 * NOTE: this will only disable the regulator output if no other consumer
2918 * devices have it enabled, the regulator device supports disabling and
2919 * machine constraints permit this operation.
2921 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
2923 struct regulator_dev
*rdev
= regulator
->rdev
;
2926 return regulator_disable(regulator
);
2928 regulator_lock(rdev
);
2929 regulator
->deferred_disables
++;
2930 mod_delayed_work(system_power_efficient_wq
, &rdev
->disable_work
,
2931 msecs_to_jiffies(ms
));
2932 regulator_unlock(rdev
);
2936 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
2938 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
2940 /* A GPIO control always takes precedence */
2942 return rdev
->ena_gpio_state
;
2944 /* If we don't know then assume that the regulator is always on */
2945 if (!rdev
->desc
->ops
->is_enabled
)
2948 return rdev
->desc
->ops
->is_enabled(rdev
);
2951 static int _regulator_list_voltage(struct regulator_dev
*rdev
,
2952 unsigned selector
, int lock
)
2954 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2957 if (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1 && !selector
)
2958 return rdev
->desc
->fixed_uV
;
2960 if (ops
->list_voltage
) {
2961 if (selector
>= rdev
->desc
->n_voltages
)
2964 regulator_lock(rdev
);
2965 ret
= ops
->list_voltage(rdev
, selector
);
2967 regulator_unlock(rdev
);
2968 } else if (rdev
->is_switch
&& rdev
->supply
) {
2969 ret
= _regulator_list_voltage(rdev
->supply
->rdev
,
2976 if (ret
< rdev
->constraints
->min_uV
)
2978 else if (ret
> rdev
->constraints
->max_uV
)
2986 * regulator_is_enabled - is the regulator output enabled
2987 * @regulator: regulator source
2989 * Returns positive if the regulator driver backing the source/client
2990 * has requested that the device be enabled, zero if it hasn't, else a
2991 * negative errno code.
2993 * Note that the device backing this regulator handle can have multiple
2994 * users, so it might be enabled even if regulator_enable() was never
2995 * called for this particular source.
2997 int regulator_is_enabled(struct regulator
*regulator
)
3001 if (regulator
->always_on
)
3004 regulator_lock(regulator
->rdev
);
3005 ret
= _regulator_is_enabled(regulator
->rdev
);
3006 regulator_unlock(regulator
->rdev
);
3010 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
3013 * regulator_count_voltages - count regulator_list_voltage() selectors
3014 * @regulator: regulator source
3016 * Returns number of selectors, or negative errno. Selectors are
3017 * numbered starting at zero, and typically correspond to bitfields
3018 * in hardware registers.
3020 int regulator_count_voltages(struct regulator
*regulator
)
3022 struct regulator_dev
*rdev
= regulator
->rdev
;
3024 if (rdev
->desc
->n_voltages
)
3025 return rdev
->desc
->n_voltages
;
3027 if (!rdev
->is_switch
|| !rdev
->supply
)
3030 return regulator_count_voltages(rdev
->supply
);
3032 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
3035 * regulator_list_voltage - enumerate supported voltages
3036 * @regulator: regulator source
3037 * @selector: identify voltage to list
3038 * Context: can sleep
3040 * Returns a voltage that can be passed to @regulator_set_voltage(),
3041 * zero if this selector code can't be used on this system, or a
3044 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
3046 return _regulator_list_voltage(regulator
->rdev
, selector
, 1);
3048 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
3051 * regulator_get_regmap - get the regulator's register map
3052 * @regulator: regulator source
3054 * Returns the register map for the given regulator, or an ERR_PTR value
3055 * if the regulator doesn't use regmap.
3057 struct regmap
*regulator_get_regmap(struct regulator
*regulator
)
3059 struct regmap
*map
= regulator
->rdev
->regmap
;
3061 return map
? map
: ERR_PTR(-EOPNOTSUPP
);
3065 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3066 * @regulator: regulator source
3067 * @vsel_reg: voltage selector register, output parameter
3068 * @vsel_mask: mask for voltage selector bitfield, output parameter
3070 * Returns the hardware register offset and bitmask used for setting the
3071 * regulator voltage. This might be useful when configuring voltage-scaling
3072 * hardware or firmware that can make I2C requests behind the kernel's back,
3075 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3076 * and 0 is returned, otherwise a negative errno is returned.
3078 int regulator_get_hardware_vsel_register(struct regulator
*regulator
,
3080 unsigned *vsel_mask
)
3082 struct regulator_dev
*rdev
= regulator
->rdev
;
3083 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3085 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
3088 *vsel_reg
= rdev
->desc
->vsel_reg
;
3089 *vsel_mask
= rdev
->desc
->vsel_mask
;
3093 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register
);
3096 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3097 * @regulator: regulator source
3098 * @selector: identify voltage to list
3100 * Converts the selector to a hardware-specific voltage selector that can be
3101 * directly written to the regulator registers. The address of the voltage
3102 * register can be determined by calling @regulator_get_hardware_vsel_register.
3104 * On error a negative errno is returned.
3106 int regulator_list_hardware_vsel(struct regulator
*regulator
,
3109 struct regulator_dev
*rdev
= regulator
->rdev
;
3110 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3112 if (selector
>= rdev
->desc
->n_voltages
)
3114 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
3119 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel
);
3122 * regulator_get_linear_step - return the voltage step size between VSEL values
3123 * @regulator: regulator source
3125 * Returns the voltage step size between VSEL values for linear
3126 * regulators, or return 0 if the regulator isn't a linear regulator.
3128 unsigned int regulator_get_linear_step(struct regulator
*regulator
)
3130 struct regulator_dev
*rdev
= regulator
->rdev
;
3132 return rdev
->desc
->uV_step
;
3134 EXPORT_SYMBOL_GPL(regulator_get_linear_step
);
3137 * regulator_is_supported_voltage - check if a voltage range can be supported
3139 * @regulator: Regulator to check.
3140 * @min_uV: Minimum required voltage in uV.
3141 * @max_uV: Maximum required voltage in uV.
3143 * Returns a boolean.
3145 int regulator_is_supported_voltage(struct regulator
*regulator
,
3146 int min_uV
, int max_uV
)
3148 struct regulator_dev
*rdev
= regulator
->rdev
;
3149 int i
, voltages
, ret
;
3151 /* If we can't change voltage check the current voltage */
3152 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
3153 ret
= regulator_get_voltage(regulator
);
3155 return min_uV
<= ret
&& ret
<= max_uV
;
3160 /* Any voltage within constrains range is fine? */
3161 if (rdev
->desc
->continuous_voltage_range
)
3162 return min_uV
>= rdev
->constraints
->min_uV
&&
3163 max_uV
<= rdev
->constraints
->max_uV
;
3165 ret
= regulator_count_voltages(regulator
);
3170 for (i
= 0; i
< voltages
; i
++) {
3171 ret
= regulator_list_voltage(regulator
, i
);
3173 if (ret
>= min_uV
&& ret
<= max_uV
)
3179 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
3181 static int regulator_map_voltage(struct regulator_dev
*rdev
, int min_uV
,
3184 const struct regulator_desc
*desc
= rdev
->desc
;
3186 if (desc
->ops
->map_voltage
)
3187 return desc
->ops
->map_voltage(rdev
, min_uV
, max_uV
);
3189 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear
)
3190 return regulator_map_voltage_linear(rdev
, min_uV
, max_uV
);
3192 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear_range
)
3193 return regulator_map_voltage_linear_range(rdev
, min_uV
, max_uV
);
3195 if (desc
->ops
->list_voltage
==
3196 regulator_list_voltage_pickable_linear_range
)
3197 return regulator_map_voltage_pickable_linear_range(rdev
,
3200 return regulator_map_voltage_iterate(rdev
, min_uV
, max_uV
);
3203 static int _regulator_call_set_voltage(struct regulator_dev
*rdev
,
3204 int min_uV
, int max_uV
,
3207 struct pre_voltage_change_data data
;
3210 data
.old_uV
= regulator_get_voltage_rdev(rdev
);
3211 data
.min_uV
= min_uV
;
3212 data
.max_uV
= max_uV
;
3213 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
3215 if (ret
& NOTIFY_STOP_MASK
)
3218 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
, selector
);
3222 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
3223 (void *)data
.old_uV
);
3228 static int _regulator_call_set_voltage_sel(struct regulator_dev
*rdev
,
3229 int uV
, unsigned selector
)
3231 struct pre_voltage_change_data data
;
3234 data
.old_uV
= regulator_get_voltage_rdev(rdev
);
3237 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
3239 if (ret
& NOTIFY_STOP_MASK
)
3242 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
3246 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
3247 (void *)data
.old_uV
);
3252 static int _regulator_set_voltage_sel_step(struct regulator_dev
*rdev
,
3253 int uV
, int new_selector
)
3255 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3256 int diff
, old_sel
, curr_sel
, ret
;
3258 /* Stepping is only needed if the regulator is enabled. */
3259 if (!_regulator_is_enabled(rdev
))
3262 if (!ops
->get_voltage_sel
)
3265 old_sel
= ops
->get_voltage_sel(rdev
);
3269 diff
= new_selector
- old_sel
;
3271 return 0; /* No change needed. */
3275 for (curr_sel
= old_sel
+ rdev
->desc
->vsel_step
;
3276 curr_sel
< new_selector
;
3277 curr_sel
+= rdev
->desc
->vsel_step
) {
3279 * Call the callback directly instead of using
3280 * _regulator_call_set_voltage_sel() as we don't
3281 * want to notify anyone yet. Same in the branch
3284 ret
= ops
->set_voltage_sel(rdev
, curr_sel
);
3289 /* Stepping down. */
3290 for (curr_sel
= old_sel
- rdev
->desc
->vsel_step
;
3291 curr_sel
> new_selector
;
3292 curr_sel
-= rdev
->desc
->vsel_step
) {
3293 ret
= ops
->set_voltage_sel(rdev
, curr_sel
);
3300 /* The final selector will trigger the notifiers. */
3301 return _regulator_call_set_voltage_sel(rdev
, uV
, new_selector
);
3305 * At least try to return to the previous voltage if setting a new
3308 (void)ops
->set_voltage_sel(rdev
, old_sel
);
3312 static int _regulator_set_voltage_time(struct regulator_dev
*rdev
,
3313 int old_uV
, int new_uV
)
3315 unsigned int ramp_delay
= 0;
3317 if (rdev
->constraints
->ramp_delay
)
3318 ramp_delay
= rdev
->constraints
->ramp_delay
;
3319 else if (rdev
->desc
->ramp_delay
)
3320 ramp_delay
= rdev
->desc
->ramp_delay
;
3321 else if (rdev
->constraints
->settling_time
)
3322 return rdev
->constraints
->settling_time
;
3323 else if (rdev
->constraints
->settling_time_up
&&
3325 return rdev
->constraints
->settling_time_up
;
3326 else if (rdev
->constraints
->settling_time_down
&&
3328 return rdev
->constraints
->settling_time_down
;
3330 if (ramp_delay
== 0) {
3331 rdev_dbg(rdev
, "ramp_delay not set\n");
3335 return DIV_ROUND_UP(abs(new_uV
- old_uV
), ramp_delay
);
3338 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
3339 int min_uV
, int max_uV
)
3344 unsigned int selector
;
3345 int old_selector
= -1;
3346 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3347 int old_uV
= regulator_get_voltage_rdev(rdev
);
3349 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
3351 min_uV
+= rdev
->constraints
->uV_offset
;
3352 max_uV
+= rdev
->constraints
->uV_offset
;
3355 * If we can't obtain the old selector there is not enough
3356 * info to call set_voltage_time_sel().
3358 if (_regulator_is_enabled(rdev
) &&
3359 ops
->set_voltage_time_sel
&& ops
->get_voltage_sel
) {
3360 old_selector
= ops
->get_voltage_sel(rdev
);
3361 if (old_selector
< 0)
3362 return old_selector
;
3365 if (ops
->set_voltage
) {
3366 ret
= _regulator_call_set_voltage(rdev
, min_uV
, max_uV
,
3370 if (ops
->list_voltage
)
3371 best_val
= ops
->list_voltage(rdev
,
3374 best_val
= regulator_get_voltage_rdev(rdev
);
3377 } else if (ops
->set_voltage_sel
) {
3378 ret
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3380 best_val
= ops
->list_voltage(rdev
, ret
);
3381 if (min_uV
<= best_val
&& max_uV
>= best_val
) {
3383 if (old_selector
== selector
)
3385 else if (rdev
->desc
->vsel_step
)
3386 ret
= _regulator_set_voltage_sel_step(
3387 rdev
, best_val
, selector
);
3389 ret
= _regulator_call_set_voltage_sel(
3390 rdev
, best_val
, selector
);
3402 if (ops
->set_voltage_time_sel
) {
3404 * Call set_voltage_time_sel if successfully obtained
3407 if (old_selector
>= 0 && old_selector
!= selector
)
3408 delay
= ops
->set_voltage_time_sel(rdev
, old_selector
,
3411 if (old_uV
!= best_val
) {
3412 if (ops
->set_voltage_time
)
3413 delay
= ops
->set_voltage_time(rdev
, old_uV
,
3416 delay
= _regulator_set_voltage_time(rdev
,
3423 rdev_warn(rdev
, "failed to get delay: %pe\n", ERR_PTR(delay
));
3427 /* Insert any necessary delays */
3428 if (delay
>= 1000) {
3429 mdelay(delay
/ 1000);
3430 udelay(delay
% 1000);
3435 if (best_val
>= 0) {
3436 unsigned long data
= best_val
;
3438 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
3443 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), best_val
);
3448 static int _regulator_do_set_suspend_voltage(struct regulator_dev
*rdev
,
3449 int min_uV
, int max_uV
, suspend_state_t state
)
3451 struct regulator_state
*rstate
;
3454 rstate
= regulator_get_suspend_state(rdev
, state
);
3458 if (min_uV
< rstate
->min_uV
)
3459 min_uV
= rstate
->min_uV
;
3460 if (max_uV
> rstate
->max_uV
)
3461 max_uV
= rstate
->max_uV
;
3463 sel
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3467 uV
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
3468 if (uV
>= min_uV
&& uV
<= max_uV
)
3474 static int regulator_set_voltage_unlocked(struct regulator
*regulator
,
3475 int min_uV
, int max_uV
,
3476 suspend_state_t state
)
3478 struct regulator_dev
*rdev
= regulator
->rdev
;
3479 struct regulator_voltage
*voltage
= ®ulator
->voltage
[state
];
3481 int old_min_uV
, old_max_uV
;
3484 /* If we're setting the same range as last time the change
3485 * should be a noop (some cpufreq implementations use the same
3486 * voltage for multiple frequencies, for example).
3488 if (voltage
->min_uV
== min_uV
&& voltage
->max_uV
== max_uV
)
3491 /* If we're trying to set a range that overlaps the current voltage,
3492 * return successfully even though the regulator does not support
3493 * changing the voltage.
3495 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
3496 current_uV
= regulator_get_voltage_rdev(rdev
);
3497 if (min_uV
<= current_uV
&& current_uV
<= max_uV
) {
3498 voltage
->min_uV
= min_uV
;
3499 voltage
->max_uV
= max_uV
;
3505 if (!rdev
->desc
->ops
->set_voltage
&&
3506 !rdev
->desc
->ops
->set_voltage_sel
) {
3511 /* constraints check */
3512 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
3516 /* restore original values in case of error */
3517 old_min_uV
= voltage
->min_uV
;
3518 old_max_uV
= voltage
->max_uV
;
3519 voltage
->min_uV
= min_uV
;
3520 voltage
->max_uV
= max_uV
;
3522 /* for not coupled regulators this will just set the voltage */
3523 ret
= regulator_balance_voltage(rdev
, state
);
3525 voltage
->min_uV
= old_min_uV
;
3526 voltage
->max_uV
= old_max_uV
;
3533 int regulator_set_voltage_rdev(struct regulator_dev
*rdev
, int min_uV
,
3534 int max_uV
, suspend_state_t state
)
3536 int best_supply_uV
= 0;
3537 int supply_change_uV
= 0;
3541 regulator_ops_is_valid(rdev
->supply
->rdev
,
3542 REGULATOR_CHANGE_VOLTAGE
) &&
3543 (rdev
->desc
->min_dropout_uV
|| !(rdev
->desc
->ops
->get_voltage
||
3544 rdev
->desc
->ops
->get_voltage_sel
))) {
3545 int current_supply_uV
;
3548 selector
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3554 best_supply_uV
= _regulator_list_voltage(rdev
, selector
, 0);
3555 if (best_supply_uV
< 0) {
3556 ret
= best_supply_uV
;
3560 best_supply_uV
+= rdev
->desc
->min_dropout_uV
;
3562 current_supply_uV
= regulator_get_voltage_rdev(rdev
->supply
->rdev
);
3563 if (current_supply_uV
< 0) {
3564 ret
= current_supply_uV
;
3568 supply_change_uV
= best_supply_uV
- current_supply_uV
;
3571 if (supply_change_uV
> 0) {
3572 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
3573 best_supply_uV
, INT_MAX
, state
);
3575 dev_err(&rdev
->dev
, "Failed to increase supply voltage: %pe\n",
3581 if (state
== PM_SUSPEND_ON
)
3582 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
3584 ret
= _regulator_do_set_suspend_voltage(rdev
, min_uV
,
3589 if (supply_change_uV
< 0) {
3590 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
3591 best_supply_uV
, INT_MAX
, state
);
3593 dev_warn(&rdev
->dev
, "Failed to decrease supply voltage: %pe\n",
3595 /* No need to fail here */
3602 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev
);
3604 static int regulator_limit_voltage_step(struct regulator_dev
*rdev
,
3605 int *current_uV
, int *min_uV
)
3607 struct regulation_constraints
*constraints
= rdev
->constraints
;
3609 /* Limit voltage change only if necessary */
3610 if (!constraints
->max_uV_step
|| !_regulator_is_enabled(rdev
))
3613 if (*current_uV
< 0) {
3614 *current_uV
= regulator_get_voltage_rdev(rdev
);
3616 if (*current_uV
< 0)
3620 if (abs(*current_uV
- *min_uV
) <= constraints
->max_uV_step
)
3623 /* Clamp target voltage within the given step */
3624 if (*current_uV
< *min_uV
)
3625 *min_uV
= min(*current_uV
+ constraints
->max_uV_step
,
3628 *min_uV
= max(*current_uV
- constraints
->max_uV_step
,
3634 static int regulator_get_optimal_voltage(struct regulator_dev
*rdev
,
3636 int *min_uV
, int *max_uV
,
3637 suspend_state_t state
,
3640 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
3641 struct regulator_dev
**c_rdevs
= c_desc
->coupled_rdevs
;
3642 struct regulation_constraints
*constraints
= rdev
->constraints
;
3643 int desired_min_uV
= 0, desired_max_uV
= INT_MAX
;
3644 int max_current_uV
= 0, min_current_uV
= INT_MAX
;
3645 int highest_min_uV
= 0, target_uV
, possible_uV
;
3646 int i
, ret
, max_spread
;
3652 * If there are no coupled regulators, simply set the voltage
3653 * demanded by consumers.
3655 if (n_coupled
== 1) {
3657 * If consumers don't provide any demands, set voltage
3660 desired_min_uV
= constraints
->min_uV
;
3661 desired_max_uV
= constraints
->max_uV
;
3663 ret
= regulator_check_consumers(rdev
,
3665 &desired_max_uV
, state
);
3669 possible_uV
= desired_min_uV
;
3675 /* Find highest min desired voltage */
3676 for (i
= 0; i
< n_coupled
; i
++) {
3678 int tmp_max
= INT_MAX
;
3680 lockdep_assert_held_once(&c_rdevs
[i
]->mutex
.base
);
3682 ret
= regulator_check_consumers(c_rdevs
[i
],
3688 ret
= regulator_check_voltage(c_rdevs
[i
], &tmp_min
, &tmp_max
);
3692 highest_min_uV
= max(highest_min_uV
, tmp_min
);
3695 desired_min_uV
= tmp_min
;
3696 desired_max_uV
= tmp_max
;
3700 max_spread
= constraints
->max_spread
[0];
3703 * Let target_uV be equal to the desired one if possible.
3704 * If not, set it to minimum voltage, allowed by other coupled
3707 target_uV
= max(desired_min_uV
, highest_min_uV
- max_spread
);
3710 * Find min and max voltages, which currently aren't violating
3713 for (i
= 1; i
< n_coupled
; i
++) {
3716 if (!_regulator_is_enabled(c_rdevs
[i
]))
3719 tmp_act
= regulator_get_voltage_rdev(c_rdevs
[i
]);
3723 min_current_uV
= min(tmp_act
, min_current_uV
);
3724 max_current_uV
= max(tmp_act
, max_current_uV
);
3727 /* There aren't any other regulators enabled */
3728 if (max_current_uV
== 0) {
3729 possible_uV
= target_uV
;
3732 * Correct target voltage, so as it currently isn't
3733 * violating max_spread
3735 possible_uV
= max(target_uV
, max_current_uV
- max_spread
);
3736 possible_uV
= min(possible_uV
, min_current_uV
+ max_spread
);
3739 if (possible_uV
> desired_max_uV
)
3742 done
= (possible_uV
== target_uV
);
3743 desired_min_uV
= possible_uV
;
3746 /* Apply max_uV_step constraint if necessary */
3747 if (state
== PM_SUSPEND_ON
) {
3748 ret
= regulator_limit_voltage_step(rdev
, current_uV
,
3757 /* Set current_uV if wasn't done earlier in the code and if necessary */
3758 if (n_coupled
> 1 && *current_uV
== -1) {
3760 if (_regulator_is_enabled(rdev
)) {
3761 ret
= regulator_get_voltage_rdev(rdev
);
3767 *current_uV
= desired_min_uV
;
3771 *min_uV
= desired_min_uV
;
3772 *max_uV
= desired_max_uV
;
3777 int regulator_do_balance_voltage(struct regulator_dev
*rdev
,
3778 suspend_state_t state
, bool skip_coupled
)
3780 struct regulator_dev
**c_rdevs
;
3781 struct regulator_dev
*best_rdev
;
3782 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
3783 int i
, ret
, n_coupled
, best_min_uV
, best_max_uV
, best_c_rdev
;
3784 unsigned int delta
, best_delta
;
3785 unsigned long c_rdev_done
= 0;
3786 bool best_c_rdev_done
;
3788 c_rdevs
= c_desc
->coupled_rdevs
;
3789 n_coupled
= skip_coupled
? 1 : c_desc
->n_coupled
;
3792 * Find the best possible voltage change on each loop. Leave the loop
3793 * if there isn't any possible change.
3796 best_c_rdev_done
= false;
3804 * Find highest difference between optimal voltage
3805 * and current voltage.
3807 for (i
= 0; i
< n_coupled
; i
++) {
3809 * optimal_uV is the best voltage that can be set for
3810 * i-th regulator at the moment without violating
3811 * max_spread constraint in order to balance
3812 * the coupled voltages.
3814 int optimal_uV
= 0, optimal_max_uV
= 0, current_uV
= 0;
3816 if (test_bit(i
, &c_rdev_done
))
3819 ret
= regulator_get_optimal_voltage(c_rdevs
[i
],
3827 delta
= abs(optimal_uV
- current_uV
);
3829 if (delta
&& best_delta
<= delta
) {
3830 best_c_rdev_done
= ret
;
3832 best_rdev
= c_rdevs
[i
];
3833 best_min_uV
= optimal_uV
;
3834 best_max_uV
= optimal_max_uV
;
3839 /* Nothing to change, return successfully */
3845 ret
= regulator_set_voltage_rdev(best_rdev
, best_min_uV
,
3846 best_max_uV
, state
);
3851 if (best_c_rdev_done
)
3852 set_bit(best_c_rdev
, &c_rdev_done
);
3854 } while (n_coupled
> 1);
3860 static int regulator_balance_voltage(struct regulator_dev
*rdev
,
3861 suspend_state_t state
)
3863 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
3864 struct regulator_coupler
*coupler
= c_desc
->coupler
;
3865 bool skip_coupled
= false;
3868 * If system is in a state other than PM_SUSPEND_ON, don't check
3869 * other coupled regulators.
3871 if (state
!= PM_SUSPEND_ON
)
3872 skip_coupled
= true;
3874 if (c_desc
->n_resolved
< c_desc
->n_coupled
) {
3875 rdev_err(rdev
, "Not all coupled regulators registered\n");
3879 /* Invoke custom balancer for customized couplers */
3880 if (coupler
&& coupler
->balance_voltage
)
3881 return coupler
->balance_voltage(coupler
, rdev
, state
);
3883 return regulator_do_balance_voltage(rdev
, state
, skip_coupled
);
3887 * regulator_set_voltage - set regulator output voltage
3888 * @regulator: regulator source
3889 * @min_uV: Minimum required voltage in uV
3890 * @max_uV: Maximum acceptable voltage in uV
3892 * Sets a voltage regulator to the desired output voltage. This can be set
3893 * during any regulator state. IOW, regulator can be disabled or enabled.
3895 * If the regulator is enabled then the voltage will change to the new value
3896 * immediately otherwise if the regulator is disabled the regulator will
3897 * output at the new voltage when enabled.
3899 * NOTE: If the regulator is shared between several devices then the lowest
3900 * request voltage that meets the system constraints will be used.
3901 * Regulator system constraints must be set for this regulator before
3902 * calling this function otherwise this call will fail.
3904 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
3906 struct ww_acquire_ctx ww_ctx
;
3909 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
3911 ret
= regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
,
3914 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
3918 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
3920 static inline int regulator_suspend_toggle(struct regulator_dev
*rdev
,
3921 suspend_state_t state
, bool en
)
3923 struct regulator_state
*rstate
;
3925 rstate
= regulator_get_suspend_state(rdev
, state
);
3929 if (!rstate
->changeable
)
3932 rstate
->enabled
= (en
) ? ENABLE_IN_SUSPEND
: DISABLE_IN_SUSPEND
;
3937 int regulator_suspend_enable(struct regulator_dev
*rdev
,
3938 suspend_state_t state
)
3940 return regulator_suspend_toggle(rdev
, state
, true);
3942 EXPORT_SYMBOL_GPL(regulator_suspend_enable
);
3944 int regulator_suspend_disable(struct regulator_dev
*rdev
,
3945 suspend_state_t state
)
3947 struct regulator
*regulator
;
3948 struct regulator_voltage
*voltage
;
3951 * if any consumer wants this regulator device keeping on in
3952 * suspend states, don't set it as disabled.
3954 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
3955 voltage
= ®ulator
->voltage
[state
];
3956 if (voltage
->min_uV
|| voltage
->max_uV
)
3960 return regulator_suspend_toggle(rdev
, state
, false);
3962 EXPORT_SYMBOL_GPL(regulator_suspend_disable
);
3964 static int _regulator_set_suspend_voltage(struct regulator
*regulator
,
3965 int min_uV
, int max_uV
,
3966 suspend_state_t state
)
3968 struct regulator_dev
*rdev
= regulator
->rdev
;
3969 struct regulator_state
*rstate
;
3971 rstate
= regulator_get_suspend_state(rdev
, state
);
3975 if (rstate
->min_uV
== rstate
->max_uV
) {
3976 rdev_err(rdev
, "The suspend voltage can't be changed!\n");
3980 return regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
, state
);
3983 int regulator_set_suspend_voltage(struct regulator
*regulator
, int min_uV
,
3984 int max_uV
, suspend_state_t state
)
3986 struct ww_acquire_ctx ww_ctx
;
3989 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3990 if (regulator_check_states(state
) || state
== PM_SUSPEND_ON
)
3993 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
3995 ret
= _regulator_set_suspend_voltage(regulator
, min_uV
,
3998 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
4002 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage
);
4005 * regulator_set_voltage_time - get raise/fall time
4006 * @regulator: regulator source
4007 * @old_uV: starting voltage in microvolts
4008 * @new_uV: target voltage in microvolts
4010 * Provided with the starting and ending voltage, this function attempts to
4011 * calculate the time in microseconds required to rise or fall to this new
4014 int regulator_set_voltage_time(struct regulator
*regulator
,
4015 int old_uV
, int new_uV
)
4017 struct regulator_dev
*rdev
= regulator
->rdev
;
4018 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
4024 if (ops
->set_voltage_time
)
4025 return ops
->set_voltage_time(rdev
, old_uV
, new_uV
);
4026 else if (!ops
->set_voltage_time_sel
)
4027 return _regulator_set_voltage_time(rdev
, old_uV
, new_uV
);
4029 /* Currently requires operations to do this */
4030 if (!ops
->list_voltage
|| !rdev
->desc
->n_voltages
)
4033 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
4034 /* We only look for exact voltage matches here */
4035 voltage
= regulator_list_voltage(regulator
, i
);
4040 if (voltage
== old_uV
)
4042 if (voltage
== new_uV
)
4046 if (old_sel
< 0 || new_sel
< 0)
4049 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
4051 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
4054 * regulator_set_voltage_time_sel - get raise/fall time
4055 * @rdev: regulator source device
4056 * @old_selector: selector for starting voltage
4057 * @new_selector: selector for target voltage
4059 * Provided with the starting and target voltage selectors, this function
4060 * returns time in microseconds required to rise or fall to this new voltage
4062 * Drivers providing ramp_delay in regulation_constraints can use this as their
4063 * set_voltage_time_sel() operation.
4065 int regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
4066 unsigned int old_selector
,
4067 unsigned int new_selector
)
4069 int old_volt
, new_volt
;
4072 if (!rdev
->desc
->ops
->list_voltage
)
4075 old_volt
= rdev
->desc
->ops
->list_voltage(rdev
, old_selector
);
4076 new_volt
= rdev
->desc
->ops
->list_voltage(rdev
, new_selector
);
4078 if (rdev
->desc
->ops
->set_voltage_time
)
4079 return rdev
->desc
->ops
->set_voltage_time(rdev
, old_volt
,
4082 return _regulator_set_voltage_time(rdev
, old_volt
, new_volt
);
4084 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel
);
4087 * regulator_sync_voltage - re-apply last regulator output voltage
4088 * @regulator: regulator source
4090 * Re-apply the last configured voltage. This is intended to be used
4091 * where some external control source the consumer is cooperating with
4092 * has caused the configured voltage to change.
4094 int regulator_sync_voltage(struct regulator
*regulator
)
4096 struct regulator_dev
*rdev
= regulator
->rdev
;
4097 struct regulator_voltage
*voltage
= ®ulator
->voltage
[PM_SUSPEND_ON
];
4098 int ret
, min_uV
, max_uV
;
4100 regulator_lock(rdev
);
4102 if (!rdev
->desc
->ops
->set_voltage
&&
4103 !rdev
->desc
->ops
->set_voltage_sel
) {
4108 /* This is only going to work if we've had a voltage configured. */
4109 if (!voltage
->min_uV
&& !voltage
->max_uV
) {
4114 min_uV
= voltage
->min_uV
;
4115 max_uV
= voltage
->max_uV
;
4117 /* This should be a paranoia check... */
4118 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
4122 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
, 0);
4126 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
4129 regulator_unlock(rdev
);
4132 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
4134 int regulator_get_voltage_rdev(struct regulator_dev
*rdev
)
4139 if (rdev
->desc
->ops
->get_bypass
) {
4140 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypassed
);
4144 /* if bypassed the regulator must have a supply */
4145 if (!rdev
->supply
) {
4147 "bypassed regulator has no supply!\n");
4148 return -EPROBE_DEFER
;
4151 return regulator_get_voltage_rdev(rdev
->supply
->rdev
);
4155 if (rdev
->desc
->ops
->get_voltage_sel
) {
4156 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
4159 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
4160 } else if (rdev
->desc
->ops
->get_voltage
) {
4161 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
4162 } else if (rdev
->desc
->ops
->list_voltage
) {
4163 ret
= rdev
->desc
->ops
->list_voltage(rdev
, 0);
4164 } else if (rdev
->desc
->fixed_uV
&& (rdev
->desc
->n_voltages
== 1)) {
4165 ret
= rdev
->desc
->fixed_uV
;
4166 } else if (rdev
->supply
) {
4167 ret
= regulator_get_voltage_rdev(rdev
->supply
->rdev
);
4174 return ret
- rdev
->constraints
->uV_offset
;
4176 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev
);
4179 * regulator_get_voltage - get regulator output voltage
4180 * @regulator: regulator source
4182 * This returns the current regulator voltage in uV.
4184 * NOTE: If the regulator is disabled it will return the voltage value. This
4185 * function should not be used to determine regulator state.
4187 int regulator_get_voltage(struct regulator
*regulator
)
4189 struct ww_acquire_ctx ww_ctx
;
4192 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
4193 ret
= regulator_get_voltage_rdev(regulator
->rdev
);
4194 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
4198 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
4201 * regulator_set_current_limit - set regulator output current limit
4202 * @regulator: regulator source
4203 * @min_uA: Minimum supported current in uA
4204 * @max_uA: Maximum supported current in uA
4206 * Sets current sink to the desired output current. This can be set during
4207 * any regulator state. IOW, regulator can be disabled or enabled.
4209 * If the regulator is enabled then the current will change to the new value
4210 * immediately otherwise if the regulator is disabled the regulator will
4211 * output at the new current when enabled.
4213 * NOTE: Regulator system constraints must be set for this regulator before
4214 * calling this function otherwise this call will fail.
4216 int regulator_set_current_limit(struct regulator
*regulator
,
4217 int min_uA
, int max_uA
)
4219 struct regulator_dev
*rdev
= regulator
->rdev
;
4222 regulator_lock(rdev
);
4225 if (!rdev
->desc
->ops
->set_current_limit
) {
4230 /* constraints check */
4231 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
4235 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
4237 regulator_unlock(rdev
);
4240 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
4242 static int _regulator_get_current_limit_unlocked(struct regulator_dev
*rdev
)
4245 if (!rdev
->desc
->ops
->get_current_limit
)
4248 return rdev
->desc
->ops
->get_current_limit(rdev
);
4251 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
4255 regulator_lock(rdev
);
4256 ret
= _regulator_get_current_limit_unlocked(rdev
);
4257 regulator_unlock(rdev
);
4263 * regulator_get_current_limit - get regulator output current
4264 * @regulator: regulator source
4266 * This returns the current supplied by the specified current sink in uA.
4268 * NOTE: If the regulator is disabled it will return the current value. This
4269 * function should not be used to determine regulator state.
4271 int regulator_get_current_limit(struct regulator
*regulator
)
4273 return _regulator_get_current_limit(regulator
->rdev
);
4275 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
4278 * regulator_set_mode - set regulator operating mode
4279 * @regulator: regulator source
4280 * @mode: operating mode - one of the REGULATOR_MODE constants
4282 * Set regulator operating mode to increase regulator efficiency or improve
4283 * regulation performance.
4285 * NOTE: Regulator system constraints must be set for this regulator before
4286 * calling this function otherwise this call will fail.
4288 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
4290 struct regulator_dev
*rdev
= regulator
->rdev
;
4292 int regulator_curr_mode
;
4294 regulator_lock(rdev
);
4297 if (!rdev
->desc
->ops
->set_mode
) {
4302 /* return if the same mode is requested */
4303 if (rdev
->desc
->ops
->get_mode
) {
4304 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
4305 if (regulator_curr_mode
== mode
) {
4311 /* constraints check */
4312 ret
= regulator_mode_constrain(rdev
, &mode
);
4316 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
4318 regulator_unlock(rdev
);
4321 EXPORT_SYMBOL_GPL(regulator_set_mode
);
4323 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev
*rdev
)
4326 if (!rdev
->desc
->ops
->get_mode
)
4329 return rdev
->desc
->ops
->get_mode(rdev
);
4332 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
4336 regulator_lock(rdev
);
4337 ret
= _regulator_get_mode_unlocked(rdev
);
4338 regulator_unlock(rdev
);
4344 * regulator_get_mode - get regulator operating mode
4345 * @regulator: regulator source
4347 * Get the current regulator operating mode.
4349 unsigned int regulator_get_mode(struct regulator
*regulator
)
4351 return _regulator_get_mode(regulator
->rdev
);
4353 EXPORT_SYMBOL_GPL(regulator_get_mode
);
4355 static int _regulator_get_error_flags(struct regulator_dev
*rdev
,
4356 unsigned int *flags
)
4360 regulator_lock(rdev
);
4363 if (!rdev
->desc
->ops
->get_error_flags
) {
4368 ret
= rdev
->desc
->ops
->get_error_flags(rdev
, flags
);
4370 regulator_unlock(rdev
);
4375 * regulator_get_error_flags - get regulator error information
4376 * @regulator: regulator source
4377 * @flags: pointer to store error flags
4379 * Get the current regulator error information.
4381 int regulator_get_error_flags(struct regulator
*regulator
,
4382 unsigned int *flags
)
4384 return _regulator_get_error_flags(regulator
->rdev
, flags
);
4386 EXPORT_SYMBOL_GPL(regulator_get_error_flags
);
4389 * regulator_set_load - set regulator load
4390 * @regulator: regulator source
4391 * @uA_load: load current
4393 * Notifies the regulator core of a new device load. This is then used by
4394 * DRMS (if enabled by constraints) to set the most efficient regulator
4395 * operating mode for the new regulator loading.
4397 * Consumer devices notify their supply regulator of the maximum power
4398 * they will require (can be taken from device datasheet in the power
4399 * consumption tables) when they change operational status and hence power
4400 * state. Examples of operational state changes that can affect power
4401 * consumption are :-
4403 * o Device is opened / closed.
4404 * o Device I/O is about to begin or has just finished.
4405 * o Device is idling in between work.
4407 * This information is also exported via sysfs to userspace.
4409 * DRMS will sum the total requested load on the regulator and change
4410 * to the most efficient operating mode if platform constraints allow.
4412 * NOTE: when a regulator consumer requests to have a regulator
4413 * disabled then any load that consumer requested no longer counts
4414 * toward the total requested load. If the regulator is re-enabled
4415 * then the previously requested load will start counting again.
4417 * If a regulator is an always-on regulator then an individual consumer's
4418 * load will still be removed if that consumer is fully disabled.
4420 * On error a negative errno is returned.
4422 int regulator_set_load(struct regulator
*regulator
, int uA_load
)
4424 struct regulator_dev
*rdev
= regulator
->rdev
;
4428 regulator_lock(rdev
);
4429 old_uA_load
= regulator
->uA_load
;
4430 regulator
->uA_load
= uA_load
;
4431 if (regulator
->enable_count
&& old_uA_load
!= uA_load
) {
4432 ret
= drms_uA_update(rdev
);
4434 regulator
->uA_load
= old_uA_load
;
4436 regulator_unlock(rdev
);
4440 EXPORT_SYMBOL_GPL(regulator_set_load
);
4443 * regulator_allow_bypass - allow the regulator to go into bypass mode
4445 * @regulator: Regulator to configure
4446 * @enable: enable or disable bypass mode
4448 * Allow the regulator to go into bypass mode if all other consumers
4449 * for the regulator also enable bypass mode and the machine
4450 * constraints allow this. Bypass mode means that the regulator is
4451 * simply passing the input directly to the output with no regulation.
4453 int regulator_allow_bypass(struct regulator
*regulator
, bool enable
)
4455 struct regulator_dev
*rdev
= regulator
->rdev
;
4456 const char *name
= rdev_get_name(rdev
);
4459 if (!rdev
->desc
->ops
->set_bypass
)
4462 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_BYPASS
))
4465 regulator_lock(rdev
);
4467 if (enable
&& !regulator
->bypass
) {
4468 rdev
->bypass_count
++;
4470 if (rdev
->bypass_count
== rdev
->open_count
) {
4471 trace_regulator_bypass_enable(name
);
4473 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
4475 rdev
->bypass_count
--;
4477 trace_regulator_bypass_enable_complete(name
);
4480 } else if (!enable
&& regulator
->bypass
) {
4481 rdev
->bypass_count
--;
4483 if (rdev
->bypass_count
!= rdev
->open_count
) {
4484 trace_regulator_bypass_disable(name
);
4486 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
4488 rdev
->bypass_count
++;
4490 trace_regulator_bypass_disable_complete(name
);
4495 regulator
->bypass
= enable
;
4497 regulator_unlock(rdev
);
4501 EXPORT_SYMBOL_GPL(regulator_allow_bypass
);
4504 * regulator_register_notifier - register regulator event notifier
4505 * @regulator: regulator source
4506 * @nb: notifier block
4508 * Register notifier block to receive regulator events.
4510 int regulator_register_notifier(struct regulator
*regulator
,
4511 struct notifier_block
*nb
)
4513 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
4516 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
4519 * regulator_unregister_notifier - unregister regulator event notifier
4520 * @regulator: regulator source
4521 * @nb: notifier block
4523 * Unregister regulator event notifier block.
4525 int regulator_unregister_notifier(struct regulator
*regulator
,
4526 struct notifier_block
*nb
)
4528 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
4531 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
4533 /* notify regulator consumers and downstream regulator consumers.
4534 * Note mutex must be held by caller.
4536 static int _notifier_call_chain(struct regulator_dev
*rdev
,
4537 unsigned long event
, void *data
)
4539 /* call rdev chain first */
4540 return blocking_notifier_call_chain(&rdev
->notifier
, event
, data
);
4544 * regulator_bulk_get - get multiple regulator consumers
4546 * @dev: Device to supply
4547 * @num_consumers: Number of consumers to register
4548 * @consumers: Configuration of consumers; clients are stored here.
4550 * @return 0 on success, an errno on failure.
4552 * This helper function allows drivers to get several regulator
4553 * consumers in one operation. If any of the regulators cannot be
4554 * acquired then any regulators that were allocated will be freed
4555 * before returning to the caller.
4557 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
4558 struct regulator_bulk_data
*consumers
)
4563 for (i
= 0; i
< num_consumers
; i
++)
4564 consumers
[i
].consumer
= NULL
;
4566 for (i
= 0; i
< num_consumers
; i
++) {
4567 consumers
[i
].consumer
= regulator_get(dev
,
4568 consumers
[i
].supply
);
4569 if (IS_ERR(consumers
[i
].consumer
)) {
4570 ret
= PTR_ERR(consumers
[i
].consumer
);
4571 consumers
[i
].consumer
= NULL
;
4579 if (ret
!= -EPROBE_DEFER
)
4580 dev_err(dev
, "Failed to get supply '%s': %pe\n",
4581 consumers
[i
].supply
, ERR_PTR(ret
));
4583 dev_dbg(dev
, "Failed to get supply '%s', deferring\n",
4584 consumers
[i
].supply
);
4587 regulator_put(consumers
[i
].consumer
);
4591 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
4593 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
4595 struct regulator_bulk_data
*bulk
= data
;
4597 bulk
->ret
= regulator_enable(bulk
->consumer
);
4601 * regulator_bulk_enable - enable multiple regulator consumers
4603 * @num_consumers: Number of consumers
4604 * @consumers: Consumer data; clients are stored here.
4605 * @return 0 on success, an errno on failure
4607 * This convenience API allows consumers to enable multiple regulator
4608 * clients in a single API call. If any consumers cannot be enabled
4609 * then any others that were enabled will be disabled again prior to
4612 int regulator_bulk_enable(int num_consumers
,
4613 struct regulator_bulk_data
*consumers
)
4615 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
4619 for (i
= 0; i
< num_consumers
; i
++) {
4620 async_schedule_domain(regulator_bulk_enable_async
,
4621 &consumers
[i
], &async_domain
);
4624 async_synchronize_full_domain(&async_domain
);
4626 /* If any consumer failed we need to unwind any that succeeded */
4627 for (i
= 0; i
< num_consumers
; i
++) {
4628 if (consumers
[i
].ret
!= 0) {
4629 ret
= consumers
[i
].ret
;
4637 for (i
= 0; i
< num_consumers
; i
++) {
4638 if (consumers
[i
].ret
< 0)
4639 pr_err("Failed to enable %s: %pe\n", consumers
[i
].supply
,
4640 ERR_PTR(consumers
[i
].ret
));
4642 regulator_disable(consumers
[i
].consumer
);
4647 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
4650 * regulator_bulk_disable - disable multiple regulator consumers
4652 * @num_consumers: Number of consumers
4653 * @consumers: Consumer data; clients are stored here.
4654 * @return 0 on success, an errno on failure
4656 * This convenience API allows consumers to disable multiple regulator
4657 * clients in a single API call. If any consumers cannot be disabled
4658 * then any others that were disabled will be enabled again prior to
4661 int regulator_bulk_disable(int num_consumers
,
4662 struct regulator_bulk_data
*consumers
)
4667 for (i
= num_consumers
- 1; i
>= 0; --i
) {
4668 ret
= regulator_disable(consumers
[i
].consumer
);
4676 pr_err("Failed to disable %s: %pe\n", consumers
[i
].supply
, ERR_PTR(ret
));
4677 for (++i
; i
< num_consumers
; ++i
) {
4678 r
= regulator_enable(consumers
[i
].consumer
);
4680 pr_err("Failed to re-enable %s: %pe\n",
4681 consumers
[i
].supply
, ERR_PTR(r
));
4686 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
4689 * regulator_bulk_force_disable - force disable multiple regulator consumers
4691 * @num_consumers: Number of consumers
4692 * @consumers: Consumer data; clients are stored here.
4693 * @return 0 on success, an errno on failure
4695 * This convenience API allows consumers to forcibly disable multiple regulator
4696 * clients in a single API call.
4697 * NOTE: This should be used for situations when device damage will
4698 * likely occur if the regulators are not disabled (e.g. over temp).
4699 * Although regulator_force_disable function call for some consumers can
4700 * return error numbers, the function is called for all consumers.
4702 int regulator_bulk_force_disable(int num_consumers
,
4703 struct regulator_bulk_data
*consumers
)
4708 for (i
= 0; i
< num_consumers
; i
++) {
4710 regulator_force_disable(consumers
[i
].consumer
);
4712 /* Store first error for reporting */
4713 if (consumers
[i
].ret
&& !ret
)
4714 ret
= consumers
[i
].ret
;
4719 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
4722 * regulator_bulk_free - free multiple regulator consumers
4724 * @num_consumers: Number of consumers
4725 * @consumers: Consumer data; clients are stored here.
4727 * This convenience API allows consumers to free multiple regulator
4728 * clients in a single API call.
4730 void regulator_bulk_free(int num_consumers
,
4731 struct regulator_bulk_data
*consumers
)
4735 for (i
= 0; i
< num_consumers
; i
++) {
4736 regulator_put(consumers
[i
].consumer
);
4737 consumers
[i
].consumer
= NULL
;
4740 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
4743 * regulator_notifier_call_chain - call regulator event notifier
4744 * @rdev: regulator source
4745 * @event: notifier block
4746 * @data: callback-specific data.
4748 * Called by regulator drivers to notify clients a regulator event has
4751 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
4752 unsigned long event
, void *data
)
4754 _notifier_call_chain(rdev
, event
, data
);
4758 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
4761 * regulator_mode_to_status - convert a regulator mode into a status
4763 * @mode: Mode to convert
4765 * Convert a regulator mode into a status.
4767 int regulator_mode_to_status(unsigned int mode
)
4770 case REGULATOR_MODE_FAST
:
4771 return REGULATOR_STATUS_FAST
;
4772 case REGULATOR_MODE_NORMAL
:
4773 return REGULATOR_STATUS_NORMAL
;
4774 case REGULATOR_MODE_IDLE
:
4775 return REGULATOR_STATUS_IDLE
;
4776 case REGULATOR_MODE_STANDBY
:
4777 return REGULATOR_STATUS_STANDBY
;
4779 return REGULATOR_STATUS_UNDEFINED
;
4782 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
4784 static struct attribute
*regulator_dev_attrs
[] = {
4785 &dev_attr_name
.attr
,
4786 &dev_attr_num_users
.attr
,
4787 &dev_attr_type
.attr
,
4788 &dev_attr_microvolts
.attr
,
4789 &dev_attr_microamps
.attr
,
4790 &dev_attr_opmode
.attr
,
4791 &dev_attr_state
.attr
,
4792 &dev_attr_status
.attr
,
4793 &dev_attr_bypass
.attr
,
4794 &dev_attr_requested_microamps
.attr
,
4795 &dev_attr_min_microvolts
.attr
,
4796 &dev_attr_max_microvolts
.attr
,
4797 &dev_attr_min_microamps
.attr
,
4798 &dev_attr_max_microamps
.attr
,
4799 &dev_attr_suspend_standby_state
.attr
,
4800 &dev_attr_suspend_mem_state
.attr
,
4801 &dev_attr_suspend_disk_state
.attr
,
4802 &dev_attr_suspend_standby_microvolts
.attr
,
4803 &dev_attr_suspend_mem_microvolts
.attr
,
4804 &dev_attr_suspend_disk_microvolts
.attr
,
4805 &dev_attr_suspend_standby_mode
.attr
,
4806 &dev_attr_suspend_mem_mode
.attr
,
4807 &dev_attr_suspend_disk_mode
.attr
,
4812 * To avoid cluttering sysfs (and memory) with useless state, only
4813 * create attributes that can be meaningfully displayed.
4815 static umode_t
regulator_attr_is_visible(struct kobject
*kobj
,
4816 struct attribute
*attr
, int idx
)
4818 struct device
*dev
= kobj_to_dev(kobj
);
4819 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4820 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
4821 umode_t mode
= attr
->mode
;
4823 /* these three are always present */
4824 if (attr
== &dev_attr_name
.attr
||
4825 attr
== &dev_attr_num_users
.attr
||
4826 attr
== &dev_attr_type
.attr
)
4829 /* some attributes need specific methods to be displayed */
4830 if (attr
== &dev_attr_microvolts
.attr
) {
4831 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
4832 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0) ||
4833 (ops
->list_voltage
&& ops
->list_voltage(rdev
, 0) >= 0) ||
4834 (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1))
4839 if (attr
== &dev_attr_microamps
.attr
)
4840 return ops
->get_current_limit
? mode
: 0;
4842 if (attr
== &dev_attr_opmode
.attr
)
4843 return ops
->get_mode
? mode
: 0;
4845 if (attr
== &dev_attr_state
.attr
)
4846 return (rdev
->ena_pin
|| ops
->is_enabled
) ? mode
: 0;
4848 if (attr
== &dev_attr_status
.attr
)
4849 return ops
->get_status
? mode
: 0;
4851 if (attr
== &dev_attr_bypass
.attr
)
4852 return ops
->get_bypass
? mode
: 0;
4854 /* constraints need specific supporting methods */
4855 if (attr
== &dev_attr_min_microvolts
.attr
||
4856 attr
== &dev_attr_max_microvolts
.attr
)
4857 return (ops
->set_voltage
|| ops
->set_voltage_sel
) ? mode
: 0;
4859 if (attr
== &dev_attr_min_microamps
.attr
||
4860 attr
== &dev_attr_max_microamps
.attr
)
4861 return ops
->set_current_limit
? mode
: 0;
4863 if (attr
== &dev_attr_suspend_standby_state
.attr
||
4864 attr
== &dev_attr_suspend_mem_state
.attr
||
4865 attr
== &dev_attr_suspend_disk_state
.attr
)
4868 if (attr
== &dev_attr_suspend_standby_microvolts
.attr
||
4869 attr
== &dev_attr_suspend_mem_microvolts
.attr
||
4870 attr
== &dev_attr_suspend_disk_microvolts
.attr
)
4871 return ops
->set_suspend_voltage
? mode
: 0;
4873 if (attr
== &dev_attr_suspend_standby_mode
.attr
||
4874 attr
== &dev_attr_suspend_mem_mode
.attr
||
4875 attr
== &dev_attr_suspend_disk_mode
.attr
)
4876 return ops
->set_suspend_mode
? mode
: 0;
4881 static const struct attribute_group regulator_dev_group
= {
4882 .attrs
= regulator_dev_attrs
,
4883 .is_visible
= regulator_attr_is_visible
,
4886 static const struct attribute_group
*regulator_dev_groups
[] = {
4887 ®ulator_dev_group
,
4891 static void regulator_dev_release(struct device
*dev
)
4893 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
4895 kfree(rdev
->constraints
);
4896 of_node_put(rdev
->dev
.of_node
);
4900 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
4902 struct device
*parent
= rdev
->dev
.parent
;
4903 const char *rname
= rdev_get_name(rdev
);
4904 char name
[NAME_MAX
];
4906 /* Avoid duplicate debugfs directory names */
4907 if (parent
&& rname
== rdev
->desc
->name
) {
4908 snprintf(name
, sizeof(name
), "%s-%s", dev_name(parent
),
4913 rdev
->debugfs
= debugfs_create_dir(rname
, debugfs_root
);
4914 if (!rdev
->debugfs
) {
4915 rdev_warn(rdev
, "Failed to create debugfs directory\n");
4919 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
4921 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
4923 debugfs_create_u32("bypass_count", 0444, rdev
->debugfs
,
4924 &rdev
->bypass_count
);
4927 static int regulator_register_resolve_supply(struct device
*dev
, void *data
)
4929 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4931 if (regulator_resolve_supply(rdev
))
4932 rdev_dbg(rdev
, "unable to resolve supply\n");
4937 int regulator_coupler_register(struct regulator_coupler
*coupler
)
4939 mutex_lock(®ulator_list_mutex
);
4940 list_add_tail(&coupler
->list
, ®ulator_coupler_list
);
4941 mutex_unlock(®ulator_list_mutex
);
4946 static struct regulator_coupler
*
4947 regulator_find_coupler(struct regulator_dev
*rdev
)
4949 struct regulator_coupler
*coupler
;
4953 * Note that regulators are appended to the list and the generic
4954 * coupler is registered first, hence it will be attached at last
4957 list_for_each_entry_reverse(coupler
, ®ulator_coupler_list
, list
) {
4958 err
= coupler
->attach_regulator(coupler
, rdev
);
4960 if (!coupler
->balance_voltage
&&
4961 rdev
->coupling_desc
.n_coupled
> 2)
4962 goto err_unsupported
;
4968 return ERR_PTR(err
);
4976 return ERR_PTR(-EINVAL
);
4979 if (coupler
->detach_regulator
)
4980 coupler
->detach_regulator(coupler
, rdev
);
4983 "Voltage balancing for multiple regulator couples is unimplemented\n");
4985 return ERR_PTR(-EPERM
);
4988 static void regulator_resolve_coupling(struct regulator_dev
*rdev
)
4990 struct regulator_coupler
*coupler
= rdev
->coupling_desc
.coupler
;
4991 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
4992 int n_coupled
= c_desc
->n_coupled
;
4993 struct regulator_dev
*c_rdev
;
4996 for (i
= 1; i
< n_coupled
; i
++) {
4997 /* already resolved */
4998 if (c_desc
->coupled_rdevs
[i
])
5001 c_rdev
= of_parse_coupled_regulator(rdev
, i
- 1);
5006 if (c_rdev
->coupling_desc
.coupler
!= coupler
) {
5007 rdev_err(rdev
, "coupler mismatch with %s\n",
5008 rdev_get_name(c_rdev
));
5012 c_desc
->coupled_rdevs
[i
] = c_rdev
;
5013 c_desc
->n_resolved
++;
5015 regulator_resolve_coupling(c_rdev
);
5019 static void regulator_remove_coupling(struct regulator_dev
*rdev
)
5021 struct regulator_coupler
*coupler
= rdev
->coupling_desc
.coupler
;
5022 struct coupling_desc
*__c_desc
, *c_desc
= &rdev
->coupling_desc
;
5023 struct regulator_dev
*__c_rdev
, *c_rdev
;
5024 unsigned int __n_coupled
, n_coupled
;
5028 n_coupled
= c_desc
->n_coupled
;
5030 for (i
= 1; i
< n_coupled
; i
++) {
5031 c_rdev
= c_desc
->coupled_rdevs
[i
];
5036 regulator_lock(c_rdev
);
5038 __c_desc
= &c_rdev
->coupling_desc
;
5039 __n_coupled
= __c_desc
->n_coupled
;
5041 for (k
= 1; k
< __n_coupled
; k
++) {
5042 __c_rdev
= __c_desc
->coupled_rdevs
[k
];
5044 if (__c_rdev
== rdev
) {
5045 __c_desc
->coupled_rdevs
[k
] = NULL
;
5046 __c_desc
->n_resolved
--;
5051 regulator_unlock(c_rdev
);
5053 c_desc
->coupled_rdevs
[i
] = NULL
;
5054 c_desc
->n_resolved
--;
5057 if (coupler
&& coupler
->detach_regulator
) {
5058 err
= coupler
->detach_regulator(coupler
, rdev
);
5060 rdev_err(rdev
, "failed to detach from coupler: %pe\n",
5064 kfree(rdev
->coupling_desc
.coupled_rdevs
);
5065 rdev
->coupling_desc
.coupled_rdevs
= NULL
;
5068 static int regulator_init_coupling(struct regulator_dev
*rdev
)
5070 struct regulator_dev
**coupled
;
5071 int err
, n_phandles
;
5073 if (!IS_ENABLED(CONFIG_OF
))
5076 n_phandles
= of_get_n_coupled(rdev
);
5078 coupled
= kcalloc(n_phandles
+ 1, sizeof(*coupled
), GFP_KERNEL
);
5082 rdev
->coupling_desc
.coupled_rdevs
= coupled
;
5085 * Every regulator should always have coupling descriptor filled with
5086 * at least pointer to itself.
5088 rdev
->coupling_desc
.coupled_rdevs
[0] = rdev
;
5089 rdev
->coupling_desc
.n_coupled
= n_phandles
+ 1;
5090 rdev
->coupling_desc
.n_resolved
++;
5092 /* regulator isn't coupled */
5093 if (n_phandles
== 0)
5096 if (!of_check_coupling_data(rdev
))
5099 mutex_lock(®ulator_list_mutex
);
5100 rdev
->coupling_desc
.coupler
= regulator_find_coupler(rdev
);
5101 mutex_unlock(®ulator_list_mutex
);
5103 if (IS_ERR(rdev
->coupling_desc
.coupler
)) {
5104 err
= PTR_ERR(rdev
->coupling_desc
.coupler
);
5105 rdev_err(rdev
, "failed to get coupler: %pe\n", ERR_PTR(err
));
5112 static int generic_coupler_attach(struct regulator_coupler
*coupler
,
5113 struct regulator_dev
*rdev
)
5115 if (rdev
->coupling_desc
.n_coupled
> 2) {
5117 "Voltage balancing for multiple regulator couples is unimplemented\n");
5121 if (!rdev
->constraints
->always_on
) {
5123 "Coupling of a non always-on regulator is unimplemented\n");
5130 static struct regulator_coupler generic_regulator_coupler
= {
5131 .attach_regulator
= generic_coupler_attach
,
5135 * regulator_register - register regulator
5136 * @regulator_desc: regulator to register
5137 * @cfg: runtime configuration for regulator
5139 * Called by regulator drivers to register a regulator.
5140 * Returns a valid pointer to struct regulator_dev on success
5141 * or an ERR_PTR() on error.
5143 struct regulator_dev
*
5144 regulator_register(const struct regulator_desc
*regulator_desc
,
5145 const struct regulator_config
*cfg
)
5147 const struct regulation_constraints
*constraints
= NULL
;
5148 const struct regulator_init_data
*init_data
;
5149 struct regulator_config
*config
= NULL
;
5150 static atomic_t regulator_no
= ATOMIC_INIT(-1);
5151 struct regulator_dev
*rdev
;
5152 bool dangling_cfg_gpiod
= false;
5153 bool dangling_of_gpiod
= false;
5158 return ERR_PTR(-EINVAL
);
5160 dangling_cfg_gpiod
= true;
5161 if (regulator_desc
== NULL
) {
5169 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
) {
5174 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
5175 regulator_desc
->type
!= REGULATOR_CURRENT
) {
5180 /* Only one of each should be implemented */
5181 WARN_ON(regulator_desc
->ops
->get_voltage
&&
5182 regulator_desc
->ops
->get_voltage_sel
);
5183 WARN_ON(regulator_desc
->ops
->set_voltage
&&
5184 regulator_desc
->ops
->set_voltage_sel
);
5186 /* If we're using selectors we must implement list_voltage. */
5187 if (regulator_desc
->ops
->get_voltage_sel
&&
5188 !regulator_desc
->ops
->list_voltage
) {
5192 if (regulator_desc
->ops
->set_voltage_sel
&&
5193 !regulator_desc
->ops
->list_voltage
) {
5198 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
5203 device_initialize(&rdev
->dev
);
5206 * Duplicate the config so the driver could override it after
5207 * parsing init data.
5209 config
= kmemdup(cfg
, sizeof(*cfg
), GFP_KERNEL
);
5210 if (config
== NULL
) {
5215 init_data
= regulator_of_get_init_data(dev
, regulator_desc
, config
,
5216 &rdev
->dev
.of_node
);
5219 * Sometimes not all resources are probed already so we need to take
5220 * that into account. This happens most the time if the ena_gpiod comes
5221 * from a gpio extender or something else.
5223 if (PTR_ERR(init_data
) == -EPROBE_DEFER
) {
5224 ret
= -EPROBE_DEFER
;
5229 * We need to keep track of any GPIO descriptor coming from the
5230 * device tree until we have handled it over to the core. If the
5231 * config that was passed in to this function DOES NOT contain
5232 * a descriptor, and the config after this call DOES contain
5233 * a descriptor, we definitely got one from parsing the device
5236 if (!cfg
->ena_gpiod
&& config
->ena_gpiod
)
5237 dangling_of_gpiod
= true;
5239 init_data
= config
->init_data
;
5240 rdev
->dev
.of_node
= of_node_get(config
->of_node
);
5243 ww_mutex_init(&rdev
->mutex
, ®ulator_ww_class
);
5244 rdev
->reg_data
= config
->driver_data
;
5245 rdev
->owner
= regulator_desc
->owner
;
5246 rdev
->desc
= regulator_desc
;
5248 rdev
->regmap
= config
->regmap
;
5249 else if (dev_get_regmap(dev
, NULL
))
5250 rdev
->regmap
= dev_get_regmap(dev
, NULL
);
5251 else if (dev
->parent
)
5252 rdev
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
5253 INIT_LIST_HEAD(&rdev
->consumer_list
);
5254 INIT_LIST_HEAD(&rdev
->list
);
5255 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
5256 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
5258 /* preform any regulator specific init */
5259 if (init_data
&& init_data
->regulator_init
) {
5260 ret
= init_data
->regulator_init(rdev
->reg_data
);
5265 if (config
->ena_gpiod
) {
5266 ret
= regulator_ena_gpio_request(rdev
, config
);
5268 rdev_err(rdev
, "Failed to request enable GPIO: %pe\n",
5272 /* The regulator core took over the GPIO descriptor */
5273 dangling_cfg_gpiod
= false;
5274 dangling_of_gpiod
= false;
5277 /* register with sysfs */
5278 rdev
->dev
.class = ®ulator_class
;
5279 rdev
->dev
.parent
= dev
;
5280 dev_set_name(&rdev
->dev
, "regulator.%lu",
5281 (unsigned long) atomic_inc_return(®ulator_no
));
5282 dev_set_drvdata(&rdev
->dev
, rdev
);
5284 /* set regulator constraints */
5286 constraints
= &init_data
->constraints
;
5288 if (init_data
&& init_data
->supply_regulator
)
5289 rdev
->supply_name
= init_data
->supply_regulator
;
5290 else if (regulator_desc
->supply_name
)
5291 rdev
->supply_name
= regulator_desc
->supply_name
;
5293 ret
= set_machine_constraints(rdev
, constraints
);
5294 if (ret
== -EPROBE_DEFER
) {
5295 /* Regulator might be in bypass mode and so needs its supply
5296 * to set the constraints */
5297 /* FIXME: this currently triggers a chicken-and-egg problem
5298 * when creating -SUPPLY symlink in sysfs to a regulator
5299 * that is just being created */
5300 ret
= regulator_resolve_supply(rdev
);
5302 ret
= set_machine_constraints(rdev
, constraints
);
5304 rdev_dbg(rdev
, "unable to resolve supply early: %pe\n",
5310 ret
= regulator_init_coupling(rdev
);
5314 /* add consumers devices */
5316 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
5317 ret
= set_consumer_device_supply(rdev
,
5318 init_data
->consumer_supplies
[i
].dev_name
,
5319 init_data
->consumer_supplies
[i
].supply
);
5321 dev_err(dev
, "Failed to set supply %s\n",
5322 init_data
->consumer_supplies
[i
].supply
);
5323 goto unset_supplies
;
5328 if (!rdev
->desc
->ops
->get_voltage
&&
5329 !rdev
->desc
->ops
->list_voltage
&&
5330 !rdev
->desc
->fixed_uV
)
5331 rdev
->is_switch
= true;
5333 ret
= device_add(&rdev
->dev
);
5335 goto unset_supplies
;
5337 rdev_init_debugfs(rdev
);
5339 /* try to resolve regulators coupling since a new one was registered */
5340 mutex_lock(®ulator_list_mutex
);
5341 regulator_resolve_coupling(rdev
);
5342 mutex_unlock(®ulator_list_mutex
);
5344 /* try to resolve regulators supply since a new one was registered */
5345 class_for_each_device(®ulator_class
, NULL
, NULL
,
5346 regulator_register_resolve_supply
);
5351 mutex_lock(®ulator_list_mutex
);
5352 unset_regulator_supplies(rdev
);
5353 regulator_remove_coupling(rdev
);
5354 mutex_unlock(®ulator_list_mutex
);
5356 kfree(rdev
->coupling_desc
.coupled_rdevs
);
5357 mutex_lock(®ulator_list_mutex
);
5358 regulator_ena_gpio_free(rdev
);
5359 mutex_unlock(®ulator_list_mutex
);
5361 if (dangling_of_gpiod
)
5362 gpiod_put(config
->ena_gpiod
);
5364 put_device(&rdev
->dev
);
5366 if (dangling_cfg_gpiod
)
5367 gpiod_put(cfg
->ena_gpiod
);
5368 return ERR_PTR(ret
);
5370 EXPORT_SYMBOL_GPL(regulator_register
);
5373 * regulator_unregister - unregister regulator
5374 * @rdev: regulator to unregister
5376 * Called by regulator drivers to unregister a regulator.
5378 void regulator_unregister(struct regulator_dev
*rdev
)
5384 while (rdev
->use_count
--)
5385 regulator_disable(rdev
->supply
);
5386 regulator_put(rdev
->supply
);
5389 flush_work(&rdev
->disable_work
.work
);
5391 mutex_lock(®ulator_list_mutex
);
5393 debugfs_remove_recursive(rdev
->debugfs
);
5394 WARN_ON(rdev
->open_count
);
5395 regulator_remove_coupling(rdev
);
5396 unset_regulator_supplies(rdev
);
5397 list_del(&rdev
->list
);
5398 regulator_ena_gpio_free(rdev
);
5399 device_unregister(&rdev
->dev
);
5401 mutex_unlock(®ulator_list_mutex
);
5403 EXPORT_SYMBOL_GPL(regulator_unregister
);
5405 #ifdef CONFIG_SUSPEND
5407 * regulator_suspend - prepare regulators for system wide suspend
5408 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5410 * Configure each regulator with it's suspend operating parameters for state.
5412 static int regulator_suspend(struct device
*dev
)
5414 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5415 suspend_state_t state
= pm_suspend_target_state
;
5417 const struct regulator_state
*rstate
;
5419 rstate
= regulator_get_suspend_state_check(rdev
, state
);
5423 regulator_lock(rdev
);
5424 ret
= __suspend_set_state(rdev
, rstate
);
5425 regulator_unlock(rdev
);
5430 static int regulator_resume(struct device
*dev
)
5432 suspend_state_t state
= pm_suspend_target_state
;
5433 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5434 struct regulator_state
*rstate
;
5437 rstate
= regulator_get_suspend_state(rdev
, state
);
5441 /* Avoid grabbing the lock if we don't need to */
5442 if (!rdev
->desc
->ops
->resume
)
5445 regulator_lock(rdev
);
5447 if (rstate
->enabled
== ENABLE_IN_SUSPEND
||
5448 rstate
->enabled
== DISABLE_IN_SUSPEND
)
5449 ret
= rdev
->desc
->ops
->resume(rdev
);
5451 regulator_unlock(rdev
);
5455 #else /* !CONFIG_SUSPEND */
5457 #define regulator_suspend NULL
5458 #define regulator_resume NULL
5460 #endif /* !CONFIG_SUSPEND */
5463 static const struct dev_pm_ops __maybe_unused regulator_pm_ops
= {
5464 .suspend
= regulator_suspend
,
5465 .resume
= regulator_resume
,
5469 struct class regulator_class
= {
5470 .name
= "regulator",
5471 .dev_release
= regulator_dev_release
,
5472 .dev_groups
= regulator_dev_groups
,
5474 .pm
= ®ulator_pm_ops
,
5478 * regulator_has_full_constraints - the system has fully specified constraints
5480 * Calling this function will cause the regulator API to disable all
5481 * regulators which have a zero use count and don't have an always_on
5482 * constraint in a late_initcall.
5484 * The intention is that this will become the default behaviour in a
5485 * future kernel release so users are encouraged to use this facility
5488 void regulator_has_full_constraints(void)
5490 has_full_constraints
= 1;
5492 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
5495 * rdev_get_drvdata - get rdev regulator driver data
5498 * Get rdev regulator driver private data. This call can be used in the
5499 * regulator driver context.
5501 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
5503 return rdev
->reg_data
;
5505 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
5508 * regulator_get_drvdata - get regulator driver data
5509 * @regulator: regulator
5511 * Get regulator driver private data. This call can be used in the consumer
5512 * driver context when non API regulator specific functions need to be called.
5514 void *regulator_get_drvdata(struct regulator
*regulator
)
5516 return regulator
->rdev
->reg_data
;
5518 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
5521 * regulator_set_drvdata - set regulator driver data
5522 * @regulator: regulator
5525 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
5527 regulator
->rdev
->reg_data
= data
;
5529 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
5532 * regulator_get_id - get regulator ID
5535 int rdev_get_id(struct regulator_dev
*rdev
)
5537 return rdev
->desc
->id
;
5539 EXPORT_SYMBOL_GPL(rdev_get_id
);
5541 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
5545 EXPORT_SYMBOL_GPL(rdev_get_dev
);
5547 struct regmap
*rdev_get_regmap(struct regulator_dev
*rdev
)
5549 return rdev
->regmap
;
5551 EXPORT_SYMBOL_GPL(rdev_get_regmap
);
5553 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
5555 return reg_init_data
->driver_data
;
5557 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
5559 #ifdef CONFIG_DEBUG_FS
5560 static int supply_map_show(struct seq_file
*sf
, void *data
)
5562 struct regulator_map
*map
;
5564 list_for_each_entry(map
, ®ulator_map_list
, list
) {
5565 seq_printf(sf
, "%s -> %s.%s\n",
5566 rdev_get_name(map
->regulator
), map
->dev_name
,
5572 DEFINE_SHOW_ATTRIBUTE(supply_map
);
5574 struct summary_data
{
5576 struct regulator_dev
*parent
;
5580 static void regulator_summary_show_subtree(struct seq_file
*s
,
5581 struct regulator_dev
*rdev
,
5584 static int regulator_summary_show_children(struct device
*dev
, void *data
)
5586 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5587 struct summary_data
*summary_data
= data
;
5589 if (rdev
->supply
&& rdev
->supply
->rdev
== summary_data
->parent
)
5590 regulator_summary_show_subtree(summary_data
->s
, rdev
,
5591 summary_data
->level
+ 1);
5596 static void regulator_summary_show_subtree(struct seq_file
*s
,
5597 struct regulator_dev
*rdev
,
5600 struct regulation_constraints
*c
;
5601 struct regulator
*consumer
;
5602 struct summary_data summary_data
;
5603 unsigned int opmode
;
5608 opmode
= _regulator_get_mode_unlocked(rdev
);
5609 seq_printf(s
, "%*s%-*s %3d %4d %6d %7s ",
5611 30 - level
* 3, rdev_get_name(rdev
),
5612 rdev
->use_count
, rdev
->open_count
, rdev
->bypass_count
,
5613 regulator_opmode_to_str(opmode
));
5615 seq_printf(s
, "%5dmV ", regulator_get_voltage_rdev(rdev
) / 1000);
5616 seq_printf(s
, "%5dmA ",
5617 _regulator_get_current_limit_unlocked(rdev
) / 1000);
5619 c
= rdev
->constraints
;
5621 switch (rdev
->desc
->type
) {
5622 case REGULATOR_VOLTAGE
:
5623 seq_printf(s
, "%5dmV %5dmV ",
5624 c
->min_uV
/ 1000, c
->max_uV
/ 1000);
5626 case REGULATOR_CURRENT
:
5627 seq_printf(s
, "%5dmA %5dmA ",
5628 c
->min_uA
/ 1000, c
->max_uA
/ 1000);
5635 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
) {
5636 if (consumer
->dev
&& consumer
->dev
->class == ®ulator_class
)
5639 seq_printf(s
, "%*s%-*s ",
5640 (level
+ 1) * 3 + 1, "",
5641 30 - (level
+ 1) * 3,
5642 consumer
->supply_name
? consumer
->supply_name
:
5643 consumer
->dev
? dev_name(consumer
->dev
) : "deviceless");
5645 switch (rdev
->desc
->type
) {
5646 case REGULATOR_VOLTAGE
:
5647 seq_printf(s
, "%3d %33dmA%c%5dmV %5dmV",
5648 consumer
->enable_count
,
5649 consumer
->uA_load
/ 1000,
5650 consumer
->uA_load
&& !consumer
->enable_count
?
5652 consumer
->voltage
[PM_SUSPEND_ON
].min_uV
/ 1000,
5653 consumer
->voltage
[PM_SUSPEND_ON
].max_uV
/ 1000);
5655 case REGULATOR_CURRENT
:
5663 summary_data
.level
= level
;
5664 summary_data
.parent
= rdev
;
5666 class_for_each_device(®ulator_class
, NULL
, &summary_data
,
5667 regulator_summary_show_children
);
5670 struct summary_lock_data
{
5671 struct ww_acquire_ctx
*ww_ctx
;
5672 struct regulator_dev
**new_contended_rdev
;
5673 struct regulator_dev
**old_contended_rdev
;
5676 static int regulator_summary_lock_one(struct device
*dev
, void *data
)
5678 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5679 struct summary_lock_data
*lock_data
= data
;
5682 if (rdev
!= *lock_data
->old_contended_rdev
) {
5683 ret
= regulator_lock_nested(rdev
, lock_data
->ww_ctx
);
5685 if (ret
== -EDEADLK
)
5686 *lock_data
->new_contended_rdev
= rdev
;
5690 *lock_data
->old_contended_rdev
= NULL
;
5696 static int regulator_summary_unlock_one(struct device
*dev
, void *data
)
5698 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5699 struct summary_lock_data
*lock_data
= data
;
5702 if (rdev
== *lock_data
->new_contended_rdev
)
5706 regulator_unlock(rdev
);
5711 static int regulator_summary_lock_all(struct ww_acquire_ctx
*ww_ctx
,
5712 struct regulator_dev
**new_contended_rdev
,
5713 struct regulator_dev
**old_contended_rdev
)
5715 struct summary_lock_data lock_data
;
5718 lock_data
.ww_ctx
= ww_ctx
;
5719 lock_data
.new_contended_rdev
= new_contended_rdev
;
5720 lock_data
.old_contended_rdev
= old_contended_rdev
;
5722 ret
= class_for_each_device(®ulator_class
, NULL
, &lock_data
,
5723 regulator_summary_lock_one
);
5725 class_for_each_device(®ulator_class
, NULL
, &lock_data
,
5726 regulator_summary_unlock_one
);
5731 static void regulator_summary_lock(struct ww_acquire_ctx
*ww_ctx
)
5733 struct regulator_dev
*new_contended_rdev
= NULL
;
5734 struct regulator_dev
*old_contended_rdev
= NULL
;
5737 mutex_lock(®ulator_list_mutex
);
5739 ww_acquire_init(ww_ctx
, ®ulator_ww_class
);
5742 if (new_contended_rdev
) {
5743 ww_mutex_lock_slow(&new_contended_rdev
->mutex
, ww_ctx
);
5744 old_contended_rdev
= new_contended_rdev
;
5745 old_contended_rdev
->ref_cnt
++;
5748 err
= regulator_summary_lock_all(ww_ctx
,
5749 &new_contended_rdev
,
5750 &old_contended_rdev
);
5752 if (old_contended_rdev
)
5753 regulator_unlock(old_contended_rdev
);
5755 } while (err
== -EDEADLK
);
5757 ww_acquire_done(ww_ctx
);
5760 static void regulator_summary_unlock(struct ww_acquire_ctx
*ww_ctx
)
5762 class_for_each_device(®ulator_class
, NULL
, NULL
,
5763 regulator_summary_unlock_one
);
5764 ww_acquire_fini(ww_ctx
);
5766 mutex_unlock(®ulator_list_mutex
);
5769 static int regulator_summary_show_roots(struct device
*dev
, void *data
)
5771 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5772 struct seq_file
*s
= data
;
5775 regulator_summary_show_subtree(s
, rdev
, 0);
5780 static int regulator_summary_show(struct seq_file
*s
, void *data
)
5782 struct ww_acquire_ctx ww_ctx
;
5784 seq_puts(s
, " regulator use open bypass opmode voltage current min max\n");
5785 seq_puts(s
, "---------------------------------------------------------------------------------------\n");
5787 regulator_summary_lock(&ww_ctx
);
5789 class_for_each_device(®ulator_class
, NULL
, s
,
5790 regulator_summary_show_roots
);
5792 regulator_summary_unlock(&ww_ctx
);
5796 DEFINE_SHOW_ATTRIBUTE(regulator_summary
);
5797 #endif /* CONFIG_DEBUG_FS */
5799 static int __init
regulator_init(void)
5803 ret
= class_register(®ulator_class
);
5805 debugfs_root
= debugfs_create_dir("regulator", NULL
);
5807 pr_warn("regulator: Failed to create debugfs directory\n");
5809 #ifdef CONFIG_DEBUG_FS
5810 debugfs_create_file("supply_map", 0444, debugfs_root
, NULL
,
5813 debugfs_create_file("regulator_summary", 0444, debugfs_root
,
5814 NULL
, ®ulator_summary_fops
);
5816 regulator_dummy_init();
5818 regulator_coupler_register(&generic_regulator_coupler
);
5823 /* init early to allow our consumers to complete system booting */
5824 core_initcall(regulator_init
);
5826 static int regulator_late_cleanup(struct device
*dev
, void *data
)
5828 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5829 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
5830 struct regulation_constraints
*c
= rdev
->constraints
;
5833 if (c
&& c
->always_on
)
5836 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
))
5839 regulator_lock(rdev
);
5841 if (rdev
->use_count
)
5844 /* If we can't read the status assume it's on. */
5845 if (ops
->is_enabled
)
5846 enabled
= ops
->is_enabled(rdev
);
5853 if (have_full_constraints()) {
5854 /* We log since this may kill the system if it goes
5856 rdev_info(rdev
, "disabling\n");
5857 ret
= _regulator_do_disable(rdev
);
5859 rdev_err(rdev
, "couldn't disable: %pe\n", ERR_PTR(ret
));
5861 /* The intention is that in future we will
5862 * assume that full constraints are provided
5863 * so warn even if we aren't going to do
5866 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
5870 regulator_unlock(rdev
);
5875 static void regulator_init_complete_work_function(struct work_struct
*work
)
5878 * Regulators may had failed to resolve their input supplies
5879 * when were registered, either because the input supply was
5880 * not registered yet or because its parent device was not
5881 * bound yet. So attempt to resolve the input supplies for
5882 * pending regulators before trying to disable unused ones.
5884 class_for_each_device(®ulator_class
, NULL
, NULL
,
5885 regulator_register_resolve_supply
);
5887 /* If we have a full configuration then disable any regulators
5888 * we have permission to change the status for and which are
5889 * not in use or always_on. This is effectively the default
5890 * for DT and ACPI as they have full constraints.
5892 class_for_each_device(®ulator_class
, NULL
, NULL
,
5893 regulator_late_cleanup
);
5896 static DECLARE_DELAYED_WORK(regulator_init_complete_work
,
5897 regulator_init_complete_work_function
);
5899 static int __init
regulator_init_complete(void)
5902 * Since DT doesn't provide an idiomatic mechanism for
5903 * enabling full constraints and since it's much more natural
5904 * with DT to provide them just assume that a DT enabled
5905 * system has full constraints.
5907 if (of_have_populated_dt())
5908 has_full_constraints
= true;
5911 * We punt completion for an arbitrary amount of time since
5912 * systems like distros will load many drivers from userspace
5913 * so consumers might not always be ready yet, this is
5914 * particularly an issue with laptops where this might bounce
5915 * the display off then on. Ideally we'd get a notification
5916 * from userspace when this happens but we don't so just wait
5917 * a bit and hope we waited long enough. It'd be better if
5918 * we'd only do this on systems that need it, and a kernel
5919 * command line option might be useful.
5921 schedule_delayed_work(®ulator_init_complete_work
,
5922 msecs_to_jiffies(30000));
5926 late_initcall_sync(regulator_init_complete
);