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
1319 * Allows platform initialisation code to define and constrain
1320 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1321 * Constraints *must* be set by platform code in order for some
1322 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1325 static int set_machine_constraints(struct regulator_dev
*rdev
)
1328 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1330 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
1334 ret
= machine_constraints_current(rdev
, rdev
->constraints
);
1338 if (rdev
->constraints
->ilim_uA
&& ops
->set_input_current_limit
) {
1339 ret
= ops
->set_input_current_limit(rdev
,
1340 rdev
->constraints
->ilim_uA
);
1342 rdev_err(rdev
, "failed to set input limit: %pe\n", ERR_PTR(ret
));
1347 /* do we need to setup our suspend state */
1348 if (rdev
->constraints
->initial_state
) {
1349 ret
= suspend_set_initial_state(rdev
);
1351 rdev_err(rdev
, "failed to set suspend state: %pe\n", ERR_PTR(ret
));
1356 if (rdev
->constraints
->initial_mode
) {
1357 if (!ops
->set_mode
) {
1358 rdev_err(rdev
, "no set_mode operation\n");
1362 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
1364 rdev_err(rdev
, "failed to set initial mode: %pe\n", ERR_PTR(ret
));
1367 } else if (rdev
->constraints
->system_load
) {
1369 * We'll only apply the initial system load if an
1370 * initial mode wasn't specified.
1372 drms_uA_update(rdev
);
1375 if ((rdev
->constraints
->ramp_delay
|| rdev
->constraints
->ramp_disable
)
1376 && ops
->set_ramp_delay
) {
1377 ret
= ops
->set_ramp_delay(rdev
, rdev
->constraints
->ramp_delay
);
1379 rdev_err(rdev
, "failed to set ramp_delay: %pe\n", ERR_PTR(ret
));
1384 if (rdev
->constraints
->pull_down
&& ops
->set_pull_down
) {
1385 ret
= ops
->set_pull_down(rdev
);
1387 rdev_err(rdev
, "failed to set pull down: %pe\n", ERR_PTR(ret
));
1392 if (rdev
->constraints
->soft_start
&& ops
->set_soft_start
) {
1393 ret
= ops
->set_soft_start(rdev
);
1395 rdev_err(rdev
, "failed to set soft start: %pe\n", ERR_PTR(ret
));
1400 if (rdev
->constraints
->over_current_protection
1401 && ops
->set_over_current_protection
) {
1402 ret
= ops
->set_over_current_protection(rdev
);
1404 rdev_err(rdev
, "failed to set over current protection: %pe\n",
1410 if (rdev
->constraints
->active_discharge
&& ops
->set_active_discharge
) {
1411 bool ad_state
= (rdev
->constraints
->active_discharge
==
1412 REGULATOR_ACTIVE_DISCHARGE_ENABLE
) ? true : false;
1414 ret
= ops
->set_active_discharge(rdev
, ad_state
);
1416 rdev_err(rdev
, "failed to set active discharge: %pe\n", ERR_PTR(ret
));
1421 /* If the constraints say the regulator should be on at this point
1422 * and we have control then make sure it is enabled.
1424 if (rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) {
1426 ret
= regulator_enable(rdev
->supply
);
1428 _regulator_put(rdev
->supply
);
1429 rdev
->supply
= NULL
;
1434 ret
= _regulator_do_enable(rdev
);
1435 if (ret
< 0 && ret
!= -EINVAL
) {
1436 rdev_err(rdev
, "failed to enable: %pe\n", ERR_PTR(ret
));
1440 if (rdev
->constraints
->always_on
)
1444 print_constraints(rdev
);
1449 * set_supply - set regulator supply regulator
1450 * @rdev: regulator name
1451 * @supply_rdev: supply regulator name
1453 * Called by platform initialisation code to set the supply regulator for this
1454 * regulator. This ensures that a regulators supply will also be enabled by the
1455 * core if it's child is enabled.
1457 static int set_supply(struct regulator_dev
*rdev
,
1458 struct regulator_dev
*supply_rdev
)
1462 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
1464 if (!try_module_get(supply_rdev
->owner
))
1467 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
1468 if (rdev
->supply
== NULL
) {
1472 supply_rdev
->open_count
++;
1478 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1479 * @rdev: regulator source
1480 * @consumer_dev_name: dev_name() string for device supply applies to
1481 * @supply: symbolic name for supply
1483 * Allows platform initialisation code to map physical regulator
1484 * sources to symbolic names for supplies for use by devices. Devices
1485 * should use these symbolic names to request regulators, avoiding the
1486 * need to provide board-specific regulator names as platform data.
1488 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1489 const char *consumer_dev_name
,
1492 struct regulator_map
*node
, *new_node
;
1498 if (consumer_dev_name
!= NULL
)
1503 new_node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1504 if (new_node
== NULL
)
1507 new_node
->regulator
= rdev
;
1508 new_node
->supply
= supply
;
1511 new_node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1512 if (new_node
->dev_name
== NULL
) {
1518 mutex_lock(®ulator_list_mutex
);
1519 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1520 if (node
->dev_name
&& consumer_dev_name
) {
1521 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1523 } else if (node
->dev_name
|| consumer_dev_name
) {
1527 if (strcmp(node
->supply
, supply
) != 0)
1530 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1532 dev_name(&node
->regulator
->dev
),
1533 node
->regulator
->desc
->name
,
1535 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1539 list_add(&new_node
->list
, ®ulator_map_list
);
1540 mutex_unlock(®ulator_list_mutex
);
1545 mutex_unlock(®ulator_list_mutex
);
1546 kfree(new_node
->dev_name
);
1551 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1553 struct regulator_map
*node
, *n
;
1555 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1556 if (rdev
== node
->regulator
) {
1557 list_del(&node
->list
);
1558 kfree(node
->dev_name
);
1564 #ifdef CONFIG_DEBUG_FS
1565 static ssize_t
constraint_flags_read_file(struct file
*file
,
1566 char __user
*user_buf
,
1567 size_t count
, loff_t
*ppos
)
1569 const struct regulator
*regulator
= file
->private_data
;
1570 const struct regulation_constraints
*c
= regulator
->rdev
->constraints
;
1577 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1581 ret
= snprintf(buf
, PAGE_SIZE
,
1585 "ramp_disable: %u\n"
1588 "over_current_protection: %u\n",
1595 c
->over_current_protection
);
1597 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1605 static const struct file_operations constraint_flags_fops
= {
1606 #ifdef CONFIG_DEBUG_FS
1607 .open
= simple_open
,
1608 .read
= constraint_flags_read_file
,
1609 .llseek
= default_llseek
,
1613 #define REG_STR_SIZE 64
1615 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1617 const char *supply_name
)
1619 struct regulator
*regulator
;
1623 char buf
[REG_STR_SIZE
];
1626 size
= snprintf(buf
, REG_STR_SIZE
, "%s-%s",
1627 dev
->kobj
.name
, supply_name
);
1628 if (size
>= REG_STR_SIZE
)
1631 supply_name
= kstrdup(buf
, GFP_KERNEL
);
1632 if (supply_name
== NULL
)
1635 supply_name
= kstrdup_const(supply_name
, GFP_KERNEL
);
1636 if (supply_name
== NULL
)
1640 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1641 if (regulator
== NULL
) {
1646 regulator
->rdev
= rdev
;
1647 regulator
->supply_name
= supply_name
;
1649 regulator_lock(rdev
);
1650 list_add(®ulator
->list
, &rdev
->consumer_list
);
1651 regulator_unlock(rdev
);
1654 regulator
->dev
= dev
;
1656 /* Add a link to the device sysfs entry */
1657 err
= sysfs_create_link_nowarn(&rdev
->dev
.kobj
, &dev
->kobj
,
1660 rdev_dbg(rdev
, "could not add device link %s: %pe\n",
1661 dev
->kobj
.name
, ERR_PTR(err
));
1666 regulator
->debugfs
= debugfs_create_dir(supply_name
,
1668 if (!regulator
->debugfs
) {
1669 rdev_dbg(rdev
, "Failed to create debugfs directory\n");
1671 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1672 ®ulator
->uA_load
);
1673 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1674 ®ulator
->voltage
[PM_SUSPEND_ON
].min_uV
);
1675 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1676 ®ulator
->voltage
[PM_SUSPEND_ON
].max_uV
);
1677 debugfs_create_file("constraint_flags", 0444,
1678 regulator
->debugfs
, regulator
,
1679 &constraint_flags_fops
);
1683 * Check now if the regulator is an always on regulator - if
1684 * it is then we don't need to do nearly so much work for
1685 * enable/disable calls.
1687 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
) &&
1688 _regulator_is_enabled(rdev
))
1689 regulator
->always_on
= true;
1694 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1696 if (rdev
->constraints
&& rdev
->constraints
->enable_time
)
1697 return rdev
->constraints
->enable_time
;
1698 if (rdev
->desc
->ops
->enable_time
)
1699 return rdev
->desc
->ops
->enable_time(rdev
);
1700 return rdev
->desc
->enable_time
;
1703 static struct regulator_supply_alias
*regulator_find_supply_alias(
1704 struct device
*dev
, const char *supply
)
1706 struct regulator_supply_alias
*map
;
1708 list_for_each_entry(map
, ®ulator_supply_alias_list
, list
)
1709 if (map
->src_dev
== dev
&& strcmp(map
->src_supply
, supply
) == 0)
1715 static void regulator_supply_alias(struct device
**dev
, const char **supply
)
1717 struct regulator_supply_alias
*map
;
1719 map
= regulator_find_supply_alias(*dev
, *supply
);
1721 dev_dbg(*dev
, "Mapping supply %s to %s,%s\n",
1722 *supply
, map
->alias_supply
,
1723 dev_name(map
->alias_dev
));
1724 *dev
= map
->alias_dev
;
1725 *supply
= map
->alias_supply
;
1729 static int regulator_match(struct device
*dev
, const void *data
)
1731 struct regulator_dev
*r
= dev_to_rdev(dev
);
1733 return strcmp(rdev_get_name(r
), data
) == 0;
1736 static struct regulator_dev
*regulator_lookup_by_name(const char *name
)
1740 dev
= class_find_device(®ulator_class
, NULL
, name
, regulator_match
);
1742 return dev
? dev_to_rdev(dev
) : NULL
;
1746 * regulator_dev_lookup - lookup a regulator device.
1747 * @dev: device for regulator "consumer".
1748 * @supply: Supply name or regulator ID.
1750 * If successful, returns a struct regulator_dev that corresponds to the name
1751 * @supply and with the embedded struct device refcount incremented by one.
1752 * The refcount must be dropped by calling put_device().
1753 * On failure one of the following ERR-PTR-encoded values is returned:
1754 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1757 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1760 struct regulator_dev
*r
= NULL
;
1761 struct device_node
*node
;
1762 struct regulator_map
*map
;
1763 const char *devname
= NULL
;
1765 regulator_supply_alias(&dev
, &supply
);
1767 /* first do a dt based lookup */
1768 if (dev
&& dev
->of_node
) {
1769 node
= of_get_regulator(dev
, supply
);
1771 r
= of_find_regulator_by_node(node
);
1776 * We have a node, but there is no device.
1777 * assume it has not registered yet.
1779 return ERR_PTR(-EPROBE_DEFER
);
1783 /* if not found, try doing it non-dt way */
1785 devname
= dev_name(dev
);
1787 mutex_lock(®ulator_list_mutex
);
1788 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1789 /* If the mapping has a device set up it must match */
1790 if (map
->dev_name
&&
1791 (!devname
|| strcmp(map
->dev_name
, devname
)))
1794 if (strcmp(map
->supply
, supply
) == 0 &&
1795 get_device(&map
->regulator
->dev
)) {
1800 mutex_unlock(®ulator_list_mutex
);
1805 r
= regulator_lookup_by_name(supply
);
1809 return ERR_PTR(-ENODEV
);
1812 static int regulator_resolve_supply(struct regulator_dev
*rdev
)
1814 struct regulator_dev
*r
;
1815 struct device
*dev
= rdev
->dev
.parent
;
1818 /* No supply to resolve? */
1819 if (!rdev
->supply_name
)
1822 /* Supply already resolved? */
1826 r
= regulator_dev_lookup(dev
, rdev
->supply_name
);
1830 /* Did the lookup explicitly defer for us? */
1831 if (ret
== -EPROBE_DEFER
)
1834 if (have_full_constraints()) {
1835 r
= dummy_regulator_rdev
;
1836 get_device(&r
->dev
);
1838 dev_err(dev
, "Failed to resolve %s-supply for %s\n",
1839 rdev
->supply_name
, rdev
->desc
->name
);
1840 return -EPROBE_DEFER
;
1845 dev_err(dev
, "Supply for %s (%s) resolved to itself\n",
1846 rdev
->desc
->name
, rdev
->supply_name
);
1847 if (!have_full_constraints())
1849 r
= dummy_regulator_rdev
;
1850 get_device(&r
->dev
);
1854 * If the supply's parent device is not the same as the
1855 * regulator's parent device, then ensure the parent device
1856 * is bound before we resolve the supply, in case the parent
1857 * device get probe deferred and unregisters the supply.
1859 if (r
->dev
.parent
&& r
->dev
.parent
!= rdev
->dev
.parent
) {
1860 if (!device_is_bound(r
->dev
.parent
)) {
1861 put_device(&r
->dev
);
1862 return -EPROBE_DEFER
;
1866 /* Recursively resolve the supply of the supply */
1867 ret
= regulator_resolve_supply(r
);
1869 put_device(&r
->dev
);
1873 ret
= set_supply(rdev
, r
);
1875 put_device(&r
->dev
);
1880 * In set_machine_constraints() we may have turned this regulator on
1881 * but we couldn't propagate to the supply if it hadn't been resolved
1884 if (rdev
->use_count
) {
1885 ret
= regulator_enable(rdev
->supply
);
1887 _regulator_put(rdev
->supply
);
1888 rdev
->supply
= NULL
;
1896 /* Internal regulator request function */
1897 struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1898 enum regulator_get_type get_type
)
1900 struct regulator_dev
*rdev
;
1901 struct regulator
*regulator
;
1902 struct device_link
*link
;
1905 if (get_type
>= MAX_GET_TYPE
) {
1906 dev_err(dev
, "invalid type %d in %s\n", get_type
, __func__
);
1907 return ERR_PTR(-EINVAL
);
1911 pr_err("get() with no identifier\n");
1912 return ERR_PTR(-EINVAL
);
1915 rdev
= regulator_dev_lookup(dev
, id
);
1917 ret
= PTR_ERR(rdev
);
1920 * If regulator_dev_lookup() fails with error other
1921 * than -ENODEV our job here is done, we simply return it.
1924 return ERR_PTR(ret
);
1926 if (!have_full_constraints()) {
1928 "incomplete constraints, dummy supplies not allowed\n");
1929 return ERR_PTR(-ENODEV
);
1935 * Assume that a regulator is physically present and
1936 * enabled, even if it isn't hooked up, and just
1939 dev_warn(dev
, "supply %s not found, using dummy regulator\n", id
);
1940 rdev
= dummy_regulator_rdev
;
1941 get_device(&rdev
->dev
);
1946 "dummy supplies not allowed for exclusive requests\n");
1950 return ERR_PTR(-ENODEV
);
1954 if (rdev
->exclusive
) {
1955 regulator
= ERR_PTR(-EPERM
);
1956 put_device(&rdev
->dev
);
1960 if (get_type
== EXCLUSIVE_GET
&& rdev
->open_count
) {
1961 regulator
= ERR_PTR(-EBUSY
);
1962 put_device(&rdev
->dev
);
1966 mutex_lock(®ulator_list_mutex
);
1967 ret
= (rdev
->coupling_desc
.n_resolved
!= rdev
->coupling_desc
.n_coupled
);
1968 mutex_unlock(®ulator_list_mutex
);
1971 regulator
= ERR_PTR(-EPROBE_DEFER
);
1972 put_device(&rdev
->dev
);
1976 ret
= regulator_resolve_supply(rdev
);
1978 regulator
= ERR_PTR(ret
);
1979 put_device(&rdev
->dev
);
1983 if (!try_module_get(rdev
->owner
)) {
1984 regulator
= ERR_PTR(-EPROBE_DEFER
);
1985 put_device(&rdev
->dev
);
1989 regulator
= create_regulator(rdev
, dev
, id
);
1990 if (regulator
== NULL
) {
1991 regulator
= ERR_PTR(-ENOMEM
);
1992 module_put(rdev
->owner
);
1993 put_device(&rdev
->dev
);
1998 if (get_type
== EXCLUSIVE_GET
) {
1999 rdev
->exclusive
= 1;
2001 ret
= _regulator_is_enabled(rdev
);
2003 rdev
->use_count
= 1;
2005 rdev
->use_count
= 0;
2008 link
= device_link_add(dev
, &rdev
->dev
, DL_FLAG_STATELESS
);
2009 if (!IS_ERR_OR_NULL(link
))
2010 regulator
->device_link
= true;
2016 * regulator_get - lookup and obtain a reference to a regulator.
2017 * @dev: device for regulator "consumer"
2018 * @id: Supply name or regulator ID.
2020 * Returns a struct regulator corresponding to the regulator producer,
2021 * or IS_ERR() condition containing errno.
2023 * Use of supply names configured via regulator_set_device_supply() is
2024 * strongly encouraged. It is recommended that the supply name used
2025 * should match the name used for the supply and/or the relevant
2026 * device pins in the datasheet.
2028 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
2030 return _regulator_get(dev
, id
, NORMAL_GET
);
2032 EXPORT_SYMBOL_GPL(regulator_get
);
2035 * regulator_get_exclusive - obtain exclusive access to a regulator.
2036 * @dev: device for regulator "consumer"
2037 * @id: Supply name or regulator ID.
2039 * Returns a struct regulator corresponding to the regulator producer,
2040 * or IS_ERR() condition containing errno. Other consumers will be
2041 * unable to obtain this regulator while this reference is held and the
2042 * use count for the regulator will be initialised to reflect the current
2043 * state of the regulator.
2045 * This is intended for use by consumers which cannot tolerate shared
2046 * use of the regulator such as those which need to force the
2047 * regulator off for correct operation of the hardware they are
2050 * Use of supply names configured via regulator_set_device_supply() is
2051 * strongly encouraged. It is recommended that the supply name used
2052 * should match the name used for the supply and/or the relevant
2053 * device pins in the datasheet.
2055 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
2057 return _regulator_get(dev
, id
, EXCLUSIVE_GET
);
2059 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
2062 * regulator_get_optional - obtain optional access to a regulator.
2063 * @dev: device for regulator "consumer"
2064 * @id: Supply name or regulator ID.
2066 * Returns a struct regulator corresponding to the regulator producer,
2067 * or IS_ERR() condition containing errno.
2069 * This is intended for use by consumers for devices which can have
2070 * some supplies unconnected in normal use, such as some MMC devices.
2071 * It can allow the regulator core to provide stub supplies for other
2072 * supplies requested using normal regulator_get() calls without
2073 * disrupting the operation of drivers that can handle absent
2076 * Use of supply names configured via regulator_set_device_supply() is
2077 * strongly encouraged. It is recommended that the supply name used
2078 * should match the name used for the supply and/or the relevant
2079 * device pins in the datasheet.
2081 struct regulator
*regulator_get_optional(struct device
*dev
, const char *id
)
2083 return _regulator_get(dev
, id
, OPTIONAL_GET
);
2085 EXPORT_SYMBOL_GPL(regulator_get_optional
);
2087 static void destroy_regulator(struct regulator
*regulator
)
2089 struct regulator_dev
*rdev
= regulator
->rdev
;
2091 debugfs_remove_recursive(regulator
->debugfs
);
2093 if (regulator
->dev
) {
2094 if (regulator
->device_link
)
2095 device_link_remove(regulator
->dev
, &rdev
->dev
);
2097 /* remove any sysfs entries */
2098 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
2101 regulator_lock(rdev
);
2102 list_del(®ulator
->list
);
2105 rdev
->exclusive
= 0;
2106 regulator_unlock(rdev
);
2108 kfree_const(regulator
->supply_name
);
2112 /* regulator_list_mutex lock held by regulator_put() */
2113 static void _regulator_put(struct regulator
*regulator
)
2115 struct regulator_dev
*rdev
;
2117 if (IS_ERR_OR_NULL(regulator
))
2120 lockdep_assert_held_once(®ulator_list_mutex
);
2122 /* Docs say you must disable before calling regulator_put() */
2123 WARN_ON(regulator
->enable_count
);
2125 rdev
= regulator
->rdev
;
2127 destroy_regulator(regulator
);
2129 module_put(rdev
->owner
);
2130 put_device(&rdev
->dev
);
2134 * regulator_put - "free" the regulator source
2135 * @regulator: regulator source
2137 * Note: drivers must ensure that all regulator_enable calls made on this
2138 * regulator source are balanced by regulator_disable calls prior to calling
2141 void regulator_put(struct regulator
*regulator
)
2143 mutex_lock(®ulator_list_mutex
);
2144 _regulator_put(regulator
);
2145 mutex_unlock(®ulator_list_mutex
);
2147 EXPORT_SYMBOL_GPL(regulator_put
);
2150 * regulator_register_supply_alias - Provide device alias for supply lookup
2152 * @dev: device that will be given as the regulator "consumer"
2153 * @id: Supply name or regulator ID
2154 * @alias_dev: device that should be used to lookup the supply
2155 * @alias_id: Supply name or regulator ID that should be used to lookup the
2158 * All lookups for id on dev will instead be conducted for alias_id on
2161 int regulator_register_supply_alias(struct device
*dev
, const char *id
,
2162 struct device
*alias_dev
,
2163 const char *alias_id
)
2165 struct regulator_supply_alias
*map
;
2167 map
= regulator_find_supply_alias(dev
, id
);
2171 map
= kzalloc(sizeof(struct regulator_supply_alias
), GFP_KERNEL
);
2176 map
->src_supply
= id
;
2177 map
->alias_dev
= alias_dev
;
2178 map
->alias_supply
= alias_id
;
2180 list_add(&map
->list
, ®ulator_supply_alias_list
);
2182 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2183 id
, dev_name(dev
), alias_id
, dev_name(alias_dev
));
2187 EXPORT_SYMBOL_GPL(regulator_register_supply_alias
);
2190 * regulator_unregister_supply_alias - Remove device alias
2192 * @dev: device that will be given as the regulator "consumer"
2193 * @id: Supply name or regulator ID
2195 * Remove a lookup alias if one exists for id on dev.
2197 void regulator_unregister_supply_alias(struct device
*dev
, const char *id
)
2199 struct regulator_supply_alias
*map
;
2201 map
= regulator_find_supply_alias(dev
, id
);
2203 list_del(&map
->list
);
2207 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias
);
2210 * regulator_bulk_register_supply_alias - register multiple aliases
2212 * @dev: device that will be given as the regulator "consumer"
2213 * @id: List of supply names or regulator IDs
2214 * @alias_dev: device that should be used to lookup the supply
2215 * @alias_id: List of supply names or regulator IDs that should be used to
2217 * @num_id: Number of aliases to register
2219 * @return 0 on success, an errno on failure.
2221 * This helper function allows drivers to register several supply
2222 * aliases in one operation. If any of the aliases cannot be
2223 * registered any aliases that were registered will be removed
2224 * before returning to the caller.
2226 int regulator_bulk_register_supply_alias(struct device
*dev
,
2227 const char *const *id
,
2228 struct device
*alias_dev
,
2229 const char *const *alias_id
,
2235 for (i
= 0; i
< num_id
; ++i
) {
2236 ret
= regulator_register_supply_alias(dev
, id
[i
], alias_dev
,
2246 "Failed to create supply alias %s,%s -> %s,%s\n",
2247 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
2250 regulator_unregister_supply_alias(dev
, id
[i
]);
2254 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias
);
2257 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2259 * @dev: device that will be given as the regulator "consumer"
2260 * @id: List of supply names or regulator IDs
2261 * @num_id: Number of aliases to unregister
2263 * This helper function allows drivers to unregister several supply
2264 * aliases in one operation.
2266 void regulator_bulk_unregister_supply_alias(struct device
*dev
,
2267 const char *const *id
,
2272 for (i
= 0; i
< num_id
; ++i
)
2273 regulator_unregister_supply_alias(dev
, id
[i
]);
2275 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias
);
2278 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2279 static int regulator_ena_gpio_request(struct regulator_dev
*rdev
,
2280 const struct regulator_config
*config
)
2282 struct regulator_enable_gpio
*pin
, *new_pin
;
2283 struct gpio_desc
*gpiod
;
2285 gpiod
= config
->ena_gpiod
;
2286 new_pin
= kzalloc(sizeof(*new_pin
), GFP_KERNEL
);
2288 mutex_lock(®ulator_list_mutex
);
2290 list_for_each_entry(pin
, ®ulator_ena_gpio_list
, list
) {
2291 if (pin
->gpiod
== gpiod
) {
2292 rdev_dbg(rdev
, "GPIO is already used\n");
2293 goto update_ena_gpio_to_rdev
;
2297 if (new_pin
== NULL
) {
2298 mutex_unlock(®ulator_list_mutex
);
2306 list_add(&pin
->list
, ®ulator_ena_gpio_list
);
2308 update_ena_gpio_to_rdev
:
2309 pin
->request_count
++;
2310 rdev
->ena_pin
= pin
;
2312 mutex_unlock(®ulator_list_mutex
);
2318 static void regulator_ena_gpio_free(struct regulator_dev
*rdev
)
2320 struct regulator_enable_gpio
*pin
, *n
;
2325 /* Free the GPIO only in case of no use */
2326 list_for_each_entry_safe(pin
, n
, ®ulator_ena_gpio_list
, list
) {
2327 if (pin
!= rdev
->ena_pin
)
2330 if (--pin
->request_count
)
2333 gpiod_put(pin
->gpiod
);
2334 list_del(&pin
->list
);
2339 rdev
->ena_pin
= NULL
;
2343 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2344 * @rdev: regulator_dev structure
2345 * @enable: enable GPIO at initial use?
2347 * GPIO is enabled in case of initial use. (enable_count is 0)
2348 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2350 static int regulator_ena_gpio_ctrl(struct regulator_dev
*rdev
, bool enable
)
2352 struct regulator_enable_gpio
*pin
= rdev
->ena_pin
;
2358 /* Enable GPIO at initial use */
2359 if (pin
->enable_count
== 0)
2360 gpiod_set_value_cansleep(pin
->gpiod
, 1);
2362 pin
->enable_count
++;
2364 if (pin
->enable_count
> 1) {
2365 pin
->enable_count
--;
2369 /* Disable GPIO if not used */
2370 if (pin
->enable_count
<= 1) {
2371 gpiod_set_value_cansleep(pin
->gpiod
, 0);
2372 pin
->enable_count
= 0;
2380 * _regulator_enable_delay - a delay helper function
2381 * @delay: time to delay in microseconds
2383 * Delay for the requested amount of time as per the guidelines in:
2385 * Documentation/timers/timers-howto.rst
2387 * The assumption here is that regulators will never be enabled in
2388 * atomic context and therefore sleeping functions can be used.
2390 static void _regulator_enable_delay(unsigned int delay
)
2392 unsigned int ms
= delay
/ 1000;
2393 unsigned int us
= delay
% 1000;
2397 * For small enough values, handle super-millisecond
2398 * delays in the usleep_range() call below.
2407 * Give the scheduler some room to coalesce with any other
2408 * wakeup sources. For delays shorter than 10 us, don't even
2409 * bother setting up high-resolution timers and just busy-
2413 usleep_range(us
, us
+ 100);
2419 * _regulator_check_status_enabled
2421 * A helper function to check if the regulator status can be interpreted
2422 * as 'regulator is enabled'.
2423 * @rdev: the regulator device to check
2426 * * 1 - if status shows regulator is in enabled state
2427 * * 0 - if not enabled state
2428 * * Error Value - as received from ops->get_status()
2430 static inline int _regulator_check_status_enabled(struct regulator_dev
*rdev
)
2432 int ret
= rdev
->desc
->ops
->get_status(rdev
);
2435 rdev_info(rdev
, "get_status returned error: %d\n", ret
);
2440 case REGULATOR_STATUS_OFF
:
2441 case REGULATOR_STATUS_ERROR
:
2442 case REGULATOR_STATUS_UNDEFINED
:
2449 static int _regulator_do_enable(struct regulator_dev
*rdev
)
2453 /* Query before enabling in case configuration dependent. */
2454 ret
= _regulator_get_enable_time(rdev
);
2458 rdev_warn(rdev
, "enable_time() failed: %pe\n", ERR_PTR(ret
));
2462 trace_regulator_enable(rdev_get_name(rdev
));
2464 if (rdev
->desc
->off_on_delay
) {
2465 /* if needed, keep a distance of off_on_delay from last time
2466 * this regulator was disabled.
2468 unsigned long start_jiffy
= jiffies
;
2469 unsigned long intended
, max_delay
, remaining
;
2471 max_delay
= usecs_to_jiffies(rdev
->desc
->off_on_delay
);
2472 intended
= rdev
->last_off_jiffy
+ max_delay
;
2474 if (time_before(start_jiffy
, intended
)) {
2475 /* calc remaining jiffies to deal with one-time
2477 * in case of multiple timer wrapping, either it can be
2478 * detected by out-of-range remaining, or it cannot be
2479 * detected and we get a penalty of
2480 * _regulator_enable_delay().
2482 remaining
= intended
- start_jiffy
;
2483 if (remaining
<= max_delay
)
2484 _regulator_enable_delay(
2485 jiffies_to_usecs(remaining
));
2489 if (rdev
->ena_pin
) {
2490 if (!rdev
->ena_gpio_state
) {
2491 ret
= regulator_ena_gpio_ctrl(rdev
, true);
2494 rdev
->ena_gpio_state
= 1;
2496 } else if (rdev
->desc
->ops
->enable
) {
2497 ret
= rdev
->desc
->ops
->enable(rdev
);
2504 /* Allow the regulator to ramp; it would be useful to extend
2505 * this for bulk operations so that the regulators can ramp
2507 trace_regulator_enable_delay(rdev_get_name(rdev
));
2509 /* If poll_enabled_time is set, poll upto the delay calculated
2510 * above, delaying poll_enabled_time uS to check if the regulator
2511 * actually got enabled.
2512 * If the regulator isn't enabled after enable_delay has
2513 * expired, return -ETIMEDOUT.
2515 if (rdev
->desc
->poll_enabled_time
) {
2516 unsigned int time_remaining
= delay
;
2518 while (time_remaining
> 0) {
2519 _regulator_enable_delay(rdev
->desc
->poll_enabled_time
);
2521 if (rdev
->desc
->ops
->get_status
) {
2522 ret
= _regulator_check_status_enabled(rdev
);
2527 } else if (rdev
->desc
->ops
->is_enabled(rdev
))
2530 time_remaining
-= rdev
->desc
->poll_enabled_time
;
2533 if (time_remaining
<= 0) {
2534 rdev_err(rdev
, "Enabled check timed out\n");
2538 _regulator_enable_delay(delay
);
2541 trace_regulator_enable_complete(rdev_get_name(rdev
));
2547 * _regulator_handle_consumer_enable - handle that a consumer enabled
2548 * @regulator: regulator source
2550 * Some things on a regulator consumer (like the contribution towards total
2551 * load on the regulator) only have an effect when the consumer wants the
2552 * regulator enabled. Explained in example with two consumers of the same
2554 * consumer A: set_load(100); => total load = 0
2555 * consumer A: regulator_enable(); => total load = 100
2556 * consumer B: set_load(1000); => total load = 100
2557 * consumer B: regulator_enable(); => total load = 1100
2558 * consumer A: regulator_disable(); => total_load = 1000
2560 * This function (together with _regulator_handle_consumer_disable) is
2561 * responsible for keeping track of the refcount for a given regulator consumer
2562 * and applying / unapplying these things.
2564 * Returns 0 upon no error; -error upon error.
2566 static int _regulator_handle_consumer_enable(struct regulator
*regulator
)
2568 struct regulator_dev
*rdev
= regulator
->rdev
;
2570 lockdep_assert_held_once(&rdev
->mutex
.base
);
2572 regulator
->enable_count
++;
2573 if (regulator
->uA_load
&& regulator
->enable_count
== 1)
2574 return drms_uA_update(rdev
);
2580 * _regulator_handle_consumer_disable - handle that a consumer disabled
2581 * @regulator: regulator source
2583 * The opposite of _regulator_handle_consumer_enable().
2585 * Returns 0 upon no error; -error upon error.
2587 static int _regulator_handle_consumer_disable(struct regulator
*regulator
)
2589 struct regulator_dev
*rdev
= regulator
->rdev
;
2591 lockdep_assert_held_once(&rdev
->mutex
.base
);
2593 if (!regulator
->enable_count
) {
2594 rdev_err(rdev
, "Underflow of regulator enable count\n");
2598 regulator
->enable_count
--;
2599 if (regulator
->uA_load
&& regulator
->enable_count
== 0)
2600 return drms_uA_update(rdev
);
2605 /* locks held by regulator_enable() */
2606 static int _regulator_enable(struct regulator
*regulator
)
2608 struct regulator_dev
*rdev
= regulator
->rdev
;
2611 lockdep_assert_held_once(&rdev
->mutex
.base
);
2613 if (rdev
->use_count
== 0 && rdev
->supply
) {
2614 ret
= _regulator_enable(rdev
->supply
);
2619 /* balance only if there are regulators coupled */
2620 if (rdev
->coupling_desc
.n_coupled
> 1) {
2621 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2623 goto err_disable_supply
;
2626 ret
= _regulator_handle_consumer_enable(regulator
);
2628 goto err_disable_supply
;
2630 if (rdev
->use_count
== 0) {
2631 /* The regulator may on if it's not switchable or left on */
2632 ret
= _regulator_is_enabled(rdev
);
2633 if (ret
== -EINVAL
|| ret
== 0) {
2634 if (!regulator_ops_is_valid(rdev
,
2635 REGULATOR_CHANGE_STATUS
)) {
2637 goto err_consumer_disable
;
2640 ret
= _regulator_do_enable(rdev
);
2642 goto err_consumer_disable
;
2644 _notifier_call_chain(rdev
, REGULATOR_EVENT_ENABLE
,
2646 } else if (ret
< 0) {
2647 rdev_err(rdev
, "is_enabled() failed: %pe\n", ERR_PTR(ret
));
2648 goto err_consumer_disable
;
2650 /* Fallthrough on positive return values - already enabled */
2657 err_consumer_disable
:
2658 _regulator_handle_consumer_disable(regulator
);
2661 if (rdev
->use_count
== 0 && rdev
->supply
)
2662 _regulator_disable(rdev
->supply
);
2668 * regulator_enable - enable regulator output
2669 * @regulator: regulator source
2671 * Request that the regulator be enabled with the regulator output at
2672 * the predefined voltage or current value. Calls to regulator_enable()
2673 * must be balanced with calls to regulator_disable().
2675 * NOTE: the output value can be set by other drivers, boot loader or may be
2676 * hardwired in the regulator.
2678 int regulator_enable(struct regulator
*regulator
)
2680 struct regulator_dev
*rdev
= regulator
->rdev
;
2681 struct ww_acquire_ctx ww_ctx
;
2684 regulator_lock_dependent(rdev
, &ww_ctx
);
2685 ret
= _regulator_enable(regulator
);
2686 regulator_unlock_dependent(rdev
, &ww_ctx
);
2690 EXPORT_SYMBOL_GPL(regulator_enable
);
2692 static int _regulator_do_disable(struct regulator_dev
*rdev
)
2696 trace_regulator_disable(rdev_get_name(rdev
));
2698 if (rdev
->ena_pin
) {
2699 if (rdev
->ena_gpio_state
) {
2700 ret
= regulator_ena_gpio_ctrl(rdev
, false);
2703 rdev
->ena_gpio_state
= 0;
2706 } else if (rdev
->desc
->ops
->disable
) {
2707 ret
= rdev
->desc
->ops
->disable(rdev
);
2712 /* cares about last_off_jiffy only if off_on_delay is required by
2715 if (rdev
->desc
->off_on_delay
)
2716 rdev
->last_off_jiffy
= jiffies
;
2718 trace_regulator_disable_complete(rdev_get_name(rdev
));
2723 /* locks held by regulator_disable() */
2724 static int _regulator_disable(struct regulator
*regulator
)
2726 struct regulator_dev
*rdev
= regulator
->rdev
;
2729 lockdep_assert_held_once(&rdev
->mutex
.base
);
2731 if (WARN(rdev
->use_count
<= 0,
2732 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
2735 /* are we the last user and permitted to disable ? */
2736 if (rdev
->use_count
== 1 &&
2737 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
2739 /* we are last user */
2740 if (regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
)) {
2741 ret
= _notifier_call_chain(rdev
,
2742 REGULATOR_EVENT_PRE_DISABLE
,
2744 if (ret
& NOTIFY_STOP_MASK
)
2747 ret
= _regulator_do_disable(rdev
);
2749 rdev_err(rdev
, "failed to disable: %pe\n", ERR_PTR(ret
));
2750 _notifier_call_chain(rdev
,
2751 REGULATOR_EVENT_ABORT_DISABLE
,
2755 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
2759 rdev
->use_count
= 0;
2760 } else if (rdev
->use_count
> 1) {
2765 ret
= _regulator_handle_consumer_disable(regulator
);
2767 if (ret
== 0 && rdev
->coupling_desc
.n_coupled
> 1)
2768 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2770 if (ret
== 0 && rdev
->use_count
== 0 && rdev
->supply
)
2771 ret
= _regulator_disable(rdev
->supply
);
2777 * regulator_disable - disable regulator output
2778 * @regulator: regulator source
2780 * Disable the regulator output voltage or current. Calls to
2781 * regulator_enable() must be balanced with calls to
2782 * regulator_disable().
2784 * NOTE: this will only disable the regulator output if no other consumer
2785 * devices have it enabled, the regulator device supports disabling and
2786 * machine constraints permit this operation.
2788 int regulator_disable(struct regulator
*regulator
)
2790 struct regulator_dev
*rdev
= regulator
->rdev
;
2791 struct ww_acquire_ctx ww_ctx
;
2794 regulator_lock_dependent(rdev
, &ww_ctx
);
2795 ret
= _regulator_disable(regulator
);
2796 regulator_unlock_dependent(rdev
, &ww_ctx
);
2800 EXPORT_SYMBOL_GPL(regulator_disable
);
2802 /* locks held by regulator_force_disable() */
2803 static int _regulator_force_disable(struct regulator_dev
*rdev
)
2807 lockdep_assert_held_once(&rdev
->mutex
.base
);
2809 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2810 REGULATOR_EVENT_PRE_DISABLE
, NULL
);
2811 if (ret
& NOTIFY_STOP_MASK
)
2814 ret
= _regulator_do_disable(rdev
);
2816 rdev_err(rdev
, "failed to force disable: %pe\n", ERR_PTR(ret
));
2817 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2818 REGULATOR_EVENT_ABORT_DISABLE
, NULL
);
2822 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2823 REGULATOR_EVENT_DISABLE
, NULL
);
2829 * regulator_force_disable - force disable regulator output
2830 * @regulator: regulator source
2832 * Forcibly disable the regulator output voltage or current.
2833 * NOTE: this *will* disable the regulator output even if other consumer
2834 * devices have it enabled. This should be used for situations when device
2835 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2837 int regulator_force_disable(struct regulator
*regulator
)
2839 struct regulator_dev
*rdev
= regulator
->rdev
;
2840 struct ww_acquire_ctx ww_ctx
;
2843 regulator_lock_dependent(rdev
, &ww_ctx
);
2845 ret
= _regulator_force_disable(regulator
->rdev
);
2847 if (rdev
->coupling_desc
.n_coupled
> 1)
2848 regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2850 if (regulator
->uA_load
) {
2851 regulator
->uA_load
= 0;
2852 ret
= drms_uA_update(rdev
);
2855 if (rdev
->use_count
!= 0 && rdev
->supply
)
2856 _regulator_disable(rdev
->supply
);
2858 regulator_unlock_dependent(rdev
, &ww_ctx
);
2862 EXPORT_SYMBOL_GPL(regulator_force_disable
);
2864 static void regulator_disable_work(struct work_struct
*work
)
2866 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
2868 struct ww_acquire_ctx ww_ctx
;
2870 struct regulator
*regulator
;
2871 int total_count
= 0;
2873 regulator_lock_dependent(rdev
, &ww_ctx
);
2876 * Workqueue functions queue the new work instance while the previous
2877 * work instance is being processed. Cancel the queued work instance
2878 * as the work instance under processing does the job of the queued
2881 cancel_delayed_work(&rdev
->disable_work
);
2883 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
2884 count
= regulator
->deferred_disables
;
2889 total_count
+= count
;
2890 regulator
->deferred_disables
= 0;
2892 for (i
= 0; i
< count
; i
++) {
2893 ret
= _regulator_disable(regulator
);
2895 rdev_err(rdev
, "Deferred disable failed: %pe\n",
2899 WARN_ON(!total_count
);
2901 if (rdev
->coupling_desc
.n_coupled
> 1)
2902 regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2904 regulator_unlock_dependent(rdev
, &ww_ctx
);
2908 * regulator_disable_deferred - disable regulator output with delay
2909 * @regulator: regulator source
2910 * @ms: milliseconds until the regulator is disabled
2912 * Execute regulator_disable() on the regulator after a delay. This
2913 * is intended for use with devices that require some time to quiesce.
2915 * NOTE: this will only disable the regulator output if no other consumer
2916 * devices have it enabled, the regulator device supports disabling and
2917 * machine constraints permit this operation.
2919 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
2921 struct regulator_dev
*rdev
= regulator
->rdev
;
2924 return regulator_disable(regulator
);
2926 regulator_lock(rdev
);
2927 regulator
->deferred_disables
++;
2928 mod_delayed_work(system_power_efficient_wq
, &rdev
->disable_work
,
2929 msecs_to_jiffies(ms
));
2930 regulator_unlock(rdev
);
2934 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
2936 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
2938 /* A GPIO control always takes precedence */
2940 return rdev
->ena_gpio_state
;
2942 /* If we don't know then assume that the regulator is always on */
2943 if (!rdev
->desc
->ops
->is_enabled
)
2946 return rdev
->desc
->ops
->is_enabled(rdev
);
2949 static int _regulator_list_voltage(struct regulator_dev
*rdev
,
2950 unsigned selector
, int lock
)
2952 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2955 if (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1 && !selector
)
2956 return rdev
->desc
->fixed_uV
;
2958 if (ops
->list_voltage
) {
2959 if (selector
>= rdev
->desc
->n_voltages
)
2961 if (selector
< rdev
->desc
->linear_min_sel
)
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 (selector
< rdev
->desc
->linear_min_sel
)
3116 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
3121 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel
);
3124 * regulator_get_linear_step - return the voltage step size between VSEL values
3125 * @regulator: regulator source
3127 * Returns the voltage step size between VSEL values for linear
3128 * regulators, or return 0 if the regulator isn't a linear regulator.
3130 unsigned int regulator_get_linear_step(struct regulator
*regulator
)
3132 struct regulator_dev
*rdev
= regulator
->rdev
;
3134 return rdev
->desc
->uV_step
;
3136 EXPORT_SYMBOL_GPL(regulator_get_linear_step
);
3139 * regulator_is_supported_voltage - check if a voltage range can be supported
3141 * @regulator: Regulator to check.
3142 * @min_uV: Minimum required voltage in uV.
3143 * @max_uV: Maximum required voltage in uV.
3145 * Returns a boolean.
3147 int regulator_is_supported_voltage(struct regulator
*regulator
,
3148 int min_uV
, int max_uV
)
3150 struct regulator_dev
*rdev
= regulator
->rdev
;
3151 int i
, voltages
, ret
;
3153 /* If we can't change voltage check the current voltage */
3154 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
3155 ret
= regulator_get_voltage(regulator
);
3157 return min_uV
<= ret
&& ret
<= max_uV
;
3162 /* Any voltage within constrains range is fine? */
3163 if (rdev
->desc
->continuous_voltage_range
)
3164 return min_uV
>= rdev
->constraints
->min_uV
&&
3165 max_uV
<= rdev
->constraints
->max_uV
;
3167 ret
= regulator_count_voltages(regulator
);
3172 for (i
= 0; i
< voltages
; i
++) {
3173 ret
= regulator_list_voltage(regulator
, i
);
3175 if (ret
>= min_uV
&& ret
<= max_uV
)
3181 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
3183 static int regulator_map_voltage(struct regulator_dev
*rdev
, int min_uV
,
3186 const struct regulator_desc
*desc
= rdev
->desc
;
3188 if (desc
->ops
->map_voltage
)
3189 return desc
->ops
->map_voltage(rdev
, min_uV
, max_uV
);
3191 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear
)
3192 return regulator_map_voltage_linear(rdev
, min_uV
, max_uV
);
3194 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear_range
)
3195 return regulator_map_voltage_linear_range(rdev
, min_uV
, max_uV
);
3197 if (desc
->ops
->list_voltage
==
3198 regulator_list_voltage_pickable_linear_range
)
3199 return regulator_map_voltage_pickable_linear_range(rdev
,
3202 return regulator_map_voltage_iterate(rdev
, min_uV
, max_uV
);
3205 static int _regulator_call_set_voltage(struct regulator_dev
*rdev
,
3206 int min_uV
, int max_uV
,
3209 struct pre_voltage_change_data data
;
3212 data
.old_uV
= regulator_get_voltage_rdev(rdev
);
3213 data
.min_uV
= min_uV
;
3214 data
.max_uV
= max_uV
;
3215 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
3217 if (ret
& NOTIFY_STOP_MASK
)
3220 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
, selector
);
3224 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
3225 (void *)data
.old_uV
);
3230 static int _regulator_call_set_voltage_sel(struct regulator_dev
*rdev
,
3231 int uV
, unsigned selector
)
3233 struct pre_voltage_change_data data
;
3236 data
.old_uV
= regulator_get_voltage_rdev(rdev
);
3239 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
3241 if (ret
& NOTIFY_STOP_MASK
)
3244 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
3248 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
3249 (void *)data
.old_uV
);
3254 static int _regulator_set_voltage_sel_step(struct regulator_dev
*rdev
,
3255 int uV
, int new_selector
)
3257 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3258 int diff
, old_sel
, curr_sel
, ret
;
3260 /* Stepping is only needed if the regulator is enabled. */
3261 if (!_regulator_is_enabled(rdev
))
3264 if (!ops
->get_voltage_sel
)
3267 old_sel
= ops
->get_voltage_sel(rdev
);
3271 diff
= new_selector
- old_sel
;
3273 return 0; /* No change needed. */
3277 for (curr_sel
= old_sel
+ rdev
->desc
->vsel_step
;
3278 curr_sel
< new_selector
;
3279 curr_sel
+= rdev
->desc
->vsel_step
) {
3281 * Call the callback directly instead of using
3282 * _regulator_call_set_voltage_sel() as we don't
3283 * want to notify anyone yet. Same in the branch
3286 ret
= ops
->set_voltage_sel(rdev
, curr_sel
);
3291 /* Stepping down. */
3292 for (curr_sel
= old_sel
- rdev
->desc
->vsel_step
;
3293 curr_sel
> new_selector
;
3294 curr_sel
-= rdev
->desc
->vsel_step
) {
3295 ret
= ops
->set_voltage_sel(rdev
, curr_sel
);
3302 /* The final selector will trigger the notifiers. */
3303 return _regulator_call_set_voltage_sel(rdev
, uV
, new_selector
);
3307 * At least try to return to the previous voltage if setting a new
3310 (void)ops
->set_voltage_sel(rdev
, old_sel
);
3314 static int _regulator_set_voltage_time(struct regulator_dev
*rdev
,
3315 int old_uV
, int new_uV
)
3317 unsigned int ramp_delay
= 0;
3319 if (rdev
->constraints
->ramp_delay
)
3320 ramp_delay
= rdev
->constraints
->ramp_delay
;
3321 else if (rdev
->desc
->ramp_delay
)
3322 ramp_delay
= rdev
->desc
->ramp_delay
;
3323 else if (rdev
->constraints
->settling_time
)
3324 return rdev
->constraints
->settling_time
;
3325 else if (rdev
->constraints
->settling_time_up
&&
3327 return rdev
->constraints
->settling_time_up
;
3328 else if (rdev
->constraints
->settling_time_down
&&
3330 return rdev
->constraints
->settling_time_down
;
3332 if (ramp_delay
== 0) {
3333 rdev_dbg(rdev
, "ramp_delay not set\n");
3337 return DIV_ROUND_UP(abs(new_uV
- old_uV
), ramp_delay
);
3340 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
3341 int min_uV
, int max_uV
)
3346 unsigned int selector
;
3347 int old_selector
= -1;
3348 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3349 int old_uV
= regulator_get_voltage_rdev(rdev
);
3351 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
3353 min_uV
+= rdev
->constraints
->uV_offset
;
3354 max_uV
+= rdev
->constraints
->uV_offset
;
3357 * If we can't obtain the old selector there is not enough
3358 * info to call set_voltage_time_sel().
3360 if (_regulator_is_enabled(rdev
) &&
3361 ops
->set_voltage_time_sel
&& ops
->get_voltage_sel
) {
3362 old_selector
= ops
->get_voltage_sel(rdev
);
3363 if (old_selector
< 0)
3364 return old_selector
;
3367 if (ops
->set_voltage
) {
3368 ret
= _regulator_call_set_voltage(rdev
, min_uV
, max_uV
,
3372 if (ops
->list_voltage
)
3373 best_val
= ops
->list_voltage(rdev
,
3376 best_val
= regulator_get_voltage_rdev(rdev
);
3379 } else if (ops
->set_voltage_sel
) {
3380 ret
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3382 best_val
= ops
->list_voltage(rdev
, ret
);
3383 if (min_uV
<= best_val
&& max_uV
>= best_val
) {
3385 if (old_selector
== selector
)
3387 else if (rdev
->desc
->vsel_step
)
3388 ret
= _regulator_set_voltage_sel_step(
3389 rdev
, best_val
, selector
);
3391 ret
= _regulator_call_set_voltage_sel(
3392 rdev
, best_val
, selector
);
3404 if (ops
->set_voltage_time_sel
) {
3406 * Call set_voltage_time_sel if successfully obtained
3409 if (old_selector
>= 0 && old_selector
!= selector
)
3410 delay
= ops
->set_voltage_time_sel(rdev
, old_selector
,
3413 if (old_uV
!= best_val
) {
3414 if (ops
->set_voltage_time
)
3415 delay
= ops
->set_voltage_time(rdev
, old_uV
,
3418 delay
= _regulator_set_voltage_time(rdev
,
3425 rdev_warn(rdev
, "failed to get delay: %pe\n", ERR_PTR(delay
));
3429 /* Insert any necessary delays */
3430 if (delay
>= 1000) {
3431 mdelay(delay
/ 1000);
3432 udelay(delay
% 1000);
3437 if (best_val
>= 0) {
3438 unsigned long data
= best_val
;
3440 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
3445 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), best_val
);
3450 static int _regulator_do_set_suspend_voltage(struct regulator_dev
*rdev
,
3451 int min_uV
, int max_uV
, suspend_state_t state
)
3453 struct regulator_state
*rstate
;
3456 rstate
= regulator_get_suspend_state(rdev
, state
);
3460 if (min_uV
< rstate
->min_uV
)
3461 min_uV
= rstate
->min_uV
;
3462 if (max_uV
> rstate
->max_uV
)
3463 max_uV
= rstate
->max_uV
;
3465 sel
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3469 uV
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
3470 if (uV
>= min_uV
&& uV
<= max_uV
)
3476 static int regulator_set_voltage_unlocked(struct regulator
*regulator
,
3477 int min_uV
, int max_uV
,
3478 suspend_state_t state
)
3480 struct regulator_dev
*rdev
= regulator
->rdev
;
3481 struct regulator_voltage
*voltage
= ®ulator
->voltage
[state
];
3483 int old_min_uV
, old_max_uV
;
3486 /* If we're setting the same range as last time the change
3487 * should be a noop (some cpufreq implementations use the same
3488 * voltage for multiple frequencies, for example).
3490 if (voltage
->min_uV
== min_uV
&& voltage
->max_uV
== max_uV
)
3493 /* If we're trying to set a range that overlaps the current voltage,
3494 * return successfully even though the regulator does not support
3495 * changing the voltage.
3497 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
3498 current_uV
= regulator_get_voltage_rdev(rdev
);
3499 if (min_uV
<= current_uV
&& current_uV
<= max_uV
) {
3500 voltage
->min_uV
= min_uV
;
3501 voltage
->max_uV
= max_uV
;
3507 if (!rdev
->desc
->ops
->set_voltage
&&
3508 !rdev
->desc
->ops
->set_voltage_sel
) {
3513 /* constraints check */
3514 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
3518 /* restore original values in case of error */
3519 old_min_uV
= voltage
->min_uV
;
3520 old_max_uV
= voltage
->max_uV
;
3521 voltage
->min_uV
= min_uV
;
3522 voltage
->max_uV
= max_uV
;
3524 /* for not coupled regulators this will just set the voltage */
3525 ret
= regulator_balance_voltage(rdev
, state
);
3527 voltage
->min_uV
= old_min_uV
;
3528 voltage
->max_uV
= old_max_uV
;
3535 int regulator_set_voltage_rdev(struct regulator_dev
*rdev
, int min_uV
,
3536 int max_uV
, suspend_state_t state
)
3538 int best_supply_uV
= 0;
3539 int supply_change_uV
= 0;
3543 regulator_ops_is_valid(rdev
->supply
->rdev
,
3544 REGULATOR_CHANGE_VOLTAGE
) &&
3545 (rdev
->desc
->min_dropout_uV
|| !(rdev
->desc
->ops
->get_voltage
||
3546 rdev
->desc
->ops
->get_voltage_sel
))) {
3547 int current_supply_uV
;
3550 selector
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3556 best_supply_uV
= _regulator_list_voltage(rdev
, selector
, 0);
3557 if (best_supply_uV
< 0) {
3558 ret
= best_supply_uV
;
3562 best_supply_uV
+= rdev
->desc
->min_dropout_uV
;
3564 current_supply_uV
= regulator_get_voltage_rdev(rdev
->supply
->rdev
);
3565 if (current_supply_uV
< 0) {
3566 ret
= current_supply_uV
;
3570 supply_change_uV
= best_supply_uV
- current_supply_uV
;
3573 if (supply_change_uV
> 0) {
3574 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
3575 best_supply_uV
, INT_MAX
, state
);
3577 dev_err(&rdev
->dev
, "Failed to increase supply voltage: %pe\n",
3583 if (state
== PM_SUSPEND_ON
)
3584 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
3586 ret
= _regulator_do_set_suspend_voltage(rdev
, min_uV
,
3591 if (supply_change_uV
< 0) {
3592 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
3593 best_supply_uV
, INT_MAX
, state
);
3595 dev_warn(&rdev
->dev
, "Failed to decrease supply voltage: %pe\n",
3597 /* No need to fail here */
3604 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev
);
3606 static int regulator_limit_voltage_step(struct regulator_dev
*rdev
,
3607 int *current_uV
, int *min_uV
)
3609 struct regulation_constraints
*constraints
= rdev
->constraints
;
3611 /* Limit voltage change only if necessary */
3612 if (!constraints
->max_uV_step
|| !_regulator_is_enabled(rdev
))
3615 if (*current_uV
< 0) {
3616 *current_uV
= regulator_get_voltage_rdev(rdev
);
3618 if (*current_uV
< 0)
3622 if (abs(*current_uV
- *min_uV
) <= constraints
->max_uV_step
)
3625 /* Clamp target voltage within the given step */
3626 if (*current_uV
< *min_uV
)
3627 *min_uV
= min(*current_uV
+ constraints
->max_uV_step
,
3630 *min_uV
= max(*current_uV
- constraints
->max_uV_step
,
3636 static int regulator_get_optimal_voltage(struct regulator_dev
*rdev
,
3638 int *min_uV
, int *max_uV
,
3639 suspend_state_t state
,
3642 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
3643 struct regulator_dev
**c_rdevs
= c_desc
->coupled_rdevs
;
3644 struct regulation_constraints
*constraints
= rdev
->constraints
;
3645 int desired_min_uV
= 0, desired_max_uV
= INT_MAX
;
3646 int max_current_uV
= 0, min_current_uV
= INT_MAX
;
3647 int highest_min_uV
= 0, target_uV
, possible_uV
;
3648 int i
, ret
, max_spread
;
3654 * If there are no coupled regulators, simply set the voltage
3655 * demanded by consumers.
3657 if (n_coupled
== 1) {
3659 * If consumers don't provide any demands, set voltage
3662 desired_min_uV
= constraints
->min_uV
;
3663 desired_max_uV
= constraints
->max_uV
;
3665 ret
= regulator_check_consumers(rdev
,
3667 &desired_max_uV
, state
);
3671 possible_uV
= desired_min_uV
;
3677 /* Find highest min desired voltage */
3678 for (i
= 0; i
< n_coupled
; i
++) {
3680 int tmp_max
= INT_MAX
;
3682 lockdep_assert_held_once(&c_rdevs
[i
]->mutex
.base
);
3684 ret
= regulator_check_consumers(c_rdevs
[i
],
3690 ret
= regulator_check_voltage(c_rdevs
[i
], &tmp_min
, &tmp_max
);
3694 highest_min_uV
= max(highest_min_uV
, tmp_min
);
3697 desired_min_uV
= tmp_min
;
3698 desired_max_uV
= tmp_max
;
3702 max_spread
= constraints
->max_spread
[0];
3705 * Let target_uV be equal to the desired one if possible.
3706 * If not, set it to minimum voltage, allowed by other coupled
3709 target_uV
= max(desired_min_uV
, highest_min_uV
- max_spread
);
3712 * Find min and max voltages, which currently aren't violating
3715 for (i
= 1; i
< n_coupled
; i
++) {
3718 if (!_regulator_is_enabled(c_rdevs
[i
]))
3721 tmp_act
= regulator_get_voltage_rdev(c_rdevs
[i
]);
3725 min_current_uV
= min(tmp_act
, min_current_uV
);
3726 max_current_uV
= max(tmp_act
, max_current_uV
);
3729 /* There aren't any other regulators enabled */
3730 if (max_current_uV
== 0) {
3731 possible_uV
= target_uV
;
3734 * Correct target voltage, so as it currently isn't
3735 * violating max_spread
3737 possible_uV
= max(target_uV
, max_current_uV
- max_spread
);
3738 possible_uV
= min(possible_uV
, min_current_uV
+ max_spread
);
3741 if (possible_uV
> desired_max_uV
)
3744 done
= (possible_uV
== target_uV
);
3745 desired_min_uV
= possible_uV
;
3748 /* Apply max_uV_step constraint if necessary */
3749 if (state
== PM_SUSPEND_ON
) {
3750 ret
= regulator_limit_voltage_step(rdev
, current_uV
,
3759 /* Set current_uV if wasn't done earlier in the code and if necessary */
3760 if (n_coupled
> 1 && *current_uV
== -1) {
3762 if (_regulator_is_enabled(rdev
)) {
3763 ret
= regulator_get_voltage_rdev(rdev
);
3769 *current_uV
= desired_min_uV
;
3773 *min_uV
= desired_min_uV
;
3774 *max_uV
= desired_max_uV
;
3779 int regulator_do_balance_voltage(struct regulator_dev
*rdev
,
3780 suspend_state_t state
, bool skip_coupled
)
3782 struct regulator_dev
**c_rdevs
;
3783 struct regulator_dev
*best_rdev
;
3784 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
3785 int i
, ret
, n_coupled
, best_min_uV
, best_max_uV
, best_c_rdev
;
3786 unsigned int delta
, best_delta
;
3787 unsigned long c_rdev_done
= 0;
3788 bool best_c_rdev_done
;
3790 c_rdevs
= c_desc
->coupled_rdevs
;
3791 n_coupled
= skip_coupled
? 1 : c_desc
->n_coupled
;
3794 * Find the best possible voltage change on each loop. Leave the loop
3795 * if there isn't any possible change.
3798 best_c_rdev_done
= false;
3806 * Find highest difference between optimal voltage
3807 * and current voltage.
3809 for (i
= 0; i
< n_coupled
; i
++) {
3811 * optimal_uV is the best voltage that can be set for
3812 * i-th regulator at the moment without violating
3813 * max_spread constraint in order to balance
3814 * the coupled voltages.
3816 int optimal_uV
= 0, optimal_max_uV
= 0, current_uV
= 0;
3818 if (test_bit(i
, &c_rdev_done
))
3821 ret
= regulator_get_optimal_voltage(c_rdevs
[i
],
3829 delta
= abs(optimal_uV
- current_uV
);
3831 if (delta
&& best_delta
<= delta
) {
3832 best_c_rdev_done
= ret
;
3834 best_rdev
= c_rdevs
[i
];
3835 best_min_uV
= optimal_uV
;
3836 best_max_uV
= optimal_max_uV
;
3841 /* Nothing to change, return successfully */
3847 ret
= regulator_set_voltage_rdev(best_rdev
, best_min_uV
,
3848 best_max_uV
, state
);
3853 if (best_c_rdev_done
)
3854 set_bit(best_c_rdev
, &c_rdev_done
);
3856 } while (n_coupled
> 1);
3862 static int regulator_balance_voltage(struct regulator_dev
*rdev
,
3863 suspend_state_t state
)
3865 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
3866 struct regulator_coupler
*coupler
= c_desc
->coupler
;
3867 bool skip_coupled
= false;
3870 * If system is in a state other than PM_SUSPEND_ON, don't check
3871 * other coupled regulators.
3873 if (state
!= PM_SUSPEND_ON
)
3874 skip_coupled
= true;
3876 if (c_desc
->n_resolved
< c_desc
->n_coupled
) {
3877 rdev_err(rdev
, "Not all coupled regulators registered\n");
3881 /* Invoke custom balancer for customized couplers */
3882 if (coupler
&& coupler
->balance_voltage
)
3883 return coupler
->balance_voltage(coupler
, rdev
, state
);
3885 return regulator_do_balance_voltage(rdev
, state
, skip_coupled
);
3889 * regulator_set_voltage - set regulator output voltage
3890 * @regulator: regulator source
3891 * @min_uV: Minimum required voltage in uV
3892 * @max_uV: Maximum acceptable voltage in uV
3894 * Sets a voltage regulator to the desired output voltage. This can be set
3895 * during any regulator state. IOW, regulator can be disabled or enabled.
3897 * If the regulator is enabled then the voltage will change to the new value
3898 * immediately otherwise if the regulator is disabled the regulator will
3899 * output at the new voltage when enabled.
3901 * NOTE: If the regulator is shared between several devices then the lowest
3902 * request voltage that meets the system constraints will be used.
3903 * Regulator system constraints must be set for this regulator before
3904 * calling this function otherwise this call will fail.
3906 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
3908 struct ww_acquire_ctx ww_ctx
;
3911 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
3913 ret
= regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
,
3916 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
3920 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
3922 static inline int regulator_suspend_toggle(struct regulator_dev
*rdev
,
3923 suspend_state_t state
, bool en
)
3925 struct regulator_state
*rstate
;
3927 rstate
= regulator_get_suspend_state(rdev
, state
);
3931 if (!rstate
->changeable
)
3934 rstate
->enabled
= (en
) ? ENABLE_IN_SUSPEND
: DISABLE_IN_SUSPEND
;
3939 int regulator_suspend_enable(struct regulator_dev
*rdev
,
3940 suspend_state_t state
)
3942 return regulator_suspend_toggle(rdev
, state
, true);
3944 EXPORT_SYMBOL_GPL(regulator_suspend_enable
);
3946 int regulator_suspend_disable(struct regulator_dev
*rdev
,
3947 suspend_state_t state
)
3949 struct regulator
*regulator
;
3950 struct regulator_voltage
*voltage
;
3953 * if any consumer wants this regulator device keeping on in
3954 * suspend states, don't set it as disabled.
3956 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
3957 voltage
= ®ulator
->voltage
[state
];
3958 if (voltage
->min_uV
|| voltage
->max_uV
)
3962 return regulator_suspend_toggle(rdev
, state
, false);
3964 EXPORT_SYMBOL_GPL(regulator_suspend_disable
);
3966 static int _regulator_set_suspend_voltage(struct regulator
*regulator
,
3967 int min_uV
, int max_uV
,
3968 suspend_state_t state
)
3970 struct regulator_dev
*rdev
= regulator
->rdev
;
3971 struct regulator_state
*rstate
;
3973 rstate
= regulator_get_suspend_state(rdev
, state
);
3977 if (rstate
->min_uV
== rstate
->max_uV
) {
3978 rdev_err(rdev
, "The suspend voltage can't be changed!\n");
3982 return regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
, state
);
3985 int regulator_set_suspend_voltage(struct regulator
*regulator
, int min_uV
,
3986 int max_uV
, suspend_state_t state
)
3988 struct ww_acquire_ctx ww_ctx
;
3991 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3992 if (regulator_check_states(state
) || state
== PM_SUSPEND_ON
)
3995 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
3997 ret
= _regulator_set_suspend_voltage(regulator
, min_uV
,
4000 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
4004 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage
);
4007 * regulator_set_voltage_time - get raise/fall time
4008 * @regulator: regulator source
4009 * @old_uV: starting voltage in microvolts
4010 * @new_uV: target voltage in microvolts
4012 * Provided with the starting and ending voltage, this function attempts to
4013 * calculate the time in microseconds required to rise or fall to this new
4016 int regulator_set_voltage_time(struct regulator
*regulator
,
4017 int old_uV
, int new_uV
)
4019 struct regulator_dev
*rdev
= regulator
->rdev
;
4020 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
4026 if (ops
->set_voltage_time
)
4027 return ops
->set_voltage_time(rdev
, old_uV
, new_uV
);
4028 else if (!ops
->set_voltage_time_sel
)
4029 return _regulator_set_voltage_time(rdev
, old_uV
, new_uV
);
4031 /* Currently requires operations to do this */
4032 if (!ops
->list_voltage
|| !rdev
->desc
->n_voltages
)
4035 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
4036 /* We only look for exact voltage matches here */
4037 if (i
< rdev
->desc
->linear_min_sel
)
4040 if (old_sel
>= 0 && new_sel
>= 0)
4043 voltage
= regulator_list_voltage(regulator
, i
);
4048 if (voltage
== old_uV
)
4050 if (voltage
== new_uV
)
4054 if (old_sel
< 0 || new_sel
< 0)
4057 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
4059 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
4062 * regulator_set_voltage_time_sel - get raise/fall time
4063 * @rdev: regulator source device
4064 * @old_selector: selector for starting voltage
4065 * @new_selector: selector for target voltage
4067 * Provided with the starting and target voltage selectors, this function
4068 * returns time in microseconds required to rise or fall to this new voltage
4070 * Drivers providing ramp_delay in regulation_constraints can use this as their
4071 * set_voltage_time_sel() operation.
4073 int regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
4074 unsigned int old_selector
,
4075 unsigned int new_selector
)
4077 int old_volt
, new_volt
;
4080 if (!rdev
->desc
->ops
->list_voltage
)
4083 old_volt
= rdev
->desc
->ops
->list_voltage(rdev
, old_selector
);
4084 new_volt
= rdev
->desc
->ops
->list_voltage(rdev
, new_selector
);
4086 if (rdev
->desc
->ops
->set_voltage_time
)
4087 return rdev
->desc
->ops
->set_voltage_time(rdev
, old_volt
,
4090 return _regulator_set_voltage_time(rdev
, old_volt
, new_volt
);
4092 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel
);
4095 * regulator_sync_voltage - re-apply last regulator output voltage
4096 * @regulator: regulator source
4098 * Re-apply the last configured voltage. This is intended to be used
4099 * where some external control source the consumer is cooperating with
4100 * has caused the configured voltage to change.
4102 int regulator_sync_voltage(struct regulator
*regulator
)
4104 struct regulator_dev
*rdev
= regulator
->rdev
;
4105 struct regulator_voltage
*voltage
= ®ulator
->voltage
[PM_SUSPEND_ON
];
4106 int ret
, min_uV
, max_uV
;
4108 regulator_lock(rdev
);
4110 if (!rdev
->desc
->ops
->set_voltage
&&
4111 !rdev
->desc
->ops
->set_voltage_sel
) {
4116 /* This is only going to work if we've had a voltage configured. */
4117 if (!voltage
->min_uV
&& !voltage
->max_uV
) {
4122 min_uV
= voltage
->min_uV
;
4123 max_uV
= voltage
->max_uV
;
4125 /* This should be a paranoia check... */
4126 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
4130 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
, 0);
4134 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
4137 regulator_unlock(rdev
);
4140 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
4142 int regulator_get_voltage_rdev(struct regulator_dev
*rdev
)
4147 if (rdev
->desc
->ops
->get_bypass
) {
4148 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypassed
);
4152 /* if bypassed the regulator must have a supply */
4153 if (!rdev
->supply
) {
4155 "bypassed regulator has no supply!\n");
4156 return -EPROBE_DEFER
;
4159 return regulator_get_voltage_rdev(rdev
->supply
->rdev
);
4163 if (rdev
->desc
->ops
->get_voltage_sel
) {
4164 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
4167 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
4168 } else if (rdev
->desc
->ops
->get_voltage
) {
4169 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
4170 } else if (rdev
->desc
->ops
->list_voltage
) {
4171 ret
= rdev
->desc
->ops
->list_voltage(rdev
, 0);
4172 } else if (rdev
->desc
->fixed_uV
&& (rdev
->desc
->n_voltages
== 1)) {
4173 ret
= rdev
->desc
->fixed_uV
;
4174 } else if (rdev
->supply
) {
4175 ret
= regulator_get_voltage_rdev(rdev
->supply
->rdev
);
4176 } else if (rdev
->supply_name
) {
4177 return -EPROBE_DEFER
;
4184 return ret
- rdev
->constraints
->uV_offset
;
4186 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev
);
4189 * regulator_get_voltage - get regulator output voltage
4190 * @regulator: regulator source
4192 * This returns the current regulator voltage in uV.
4194 * NOTE: If the regulator is disabled it will return the voltage value. This
4195 * function should not be used to determine regulator state.
4197 int regulator_get_voltage(struct regulator
*regulator
)
4199 struct ww_acquire_ctx ww_ctx
;
4202 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
4203 ret
= regulator_get_voltage_rdev(regulator
->rdev
);
4204 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
4208 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
4211 * regulator_set_current_limit - set regulator output current limit
4212 * @regulator: regulator source
4213 * @min_uA: Minimum supported current in uA
4214 * @max_uA: Maximum supported current in uA
4216 * Sets current sink to the desired output current. This can be set during
4217 * any regulator state. IOW, regulator can be disabled or enabled.
4219 * If the regulator is enabled then the current will change to the new value
4220 * immediately otherwise if the regulator is disabled the regulator will
4221 * output at the new current when enabled.
4223 * NOTE: Regulator system constraints must be set for this regulator before
4224 * calling this function otherwise this call will fail.
4226 int regulator_set_current_limit(struct regulator
*regulator
,
4227 int min_uA
, int max_uA
)
4229 struct regulator_dev
*rdev
= regulator
->rdev
;
4232 regulator_lock(rdev
);
4235 if (!rdev
->desc
->ops
->set_current_limit
) {
4240 /* constraints check */
4241 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
4245 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
4247 regulator_unlock(rdev
);
4250 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
4252 static int _regulator_get_current_limit_unlocked(struct regulator_dev
*rdev
)
4255 if (!rdev
->desc
->ops
->get_current_limit
)
4258 return rdev
->desc
->ops
->get_current_limit(rdev
);
4261 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
4265 regulator_lock(rdev
);
4266 ret
= _regulator_get_current_limit_unlocked(rdev
);
4267 regulator_unlock(rdev
);
4273 * regulator_get_current_limit - get regulator output current
4274 * @regulator: regulator source
4276 * This returns the current supplied by the specified current sink in uA.
4278 * NOTE: If the regulator is disabled it will return the current value. This
4279 * function should not be used to determine regulator state.
4281 int regulator_get_current_limit(struct regulator
*regulator
)
4283 return _regulator_get_current_limit(regulator
->rdev
);
4285 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
4288 * regulator_set_mode - set regulator operating mode
4289 * @regulator: regulator source
4290 * @mode: operating mode - one of the REGULATOR_MODE constants
4292 * Set regulator operating mode to increase regulator efficiency or improve
4293 * regulation performance.
4295 * NOTE: Regulator system constraints must be set for this regulator before
4296 * calling this function otherwise this call will fail.
4298 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
4300 struct regulator_dev
*rdev
= regulator
->rdev
;
4302 int regulator_curr_mode
;
4304 regulator_lock(rdev
);
4307 if (!rdev
->desc
->ops
->set_mode
) {
4312 /* return if the same mode is requested */
4313 if (rdev
->desc
->ops
->get_mode
) {
4314 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
4315 if (regulator_curr_mode
== mode
) {
4321 /* constraints check */
4322 ret
= regulator_mode_constrain(rdev
, &mode
);
4326 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
4328 regulator_unlock(rdev
);
4331 EXPORT_SYMBOL_GPL(regulator_set_mode
);
4333 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev
*rdev
)
4336 if (!rdev
->desc
->ops
->get_mode
)
4339 return rdev
->desc
->ops
->get_mode(rdev
);
4342 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
4346 regulator_lock(rdev
);
4347 ret
= _regulator_get_mode_unlocked(rdev
);
4348 regulator_unlock(rdev
);
4354 * regulator_get_mode - get regulator operating mode
4355 * @regulator: regulator source
4357 * Get the current regulator operating mode.
4359 unsigned int regulator_get_mode(struct regulator
*regulator
)
4361 return _regulator_get_mode(regulator
->rdev
);
4363 EXPORT_SYMBOL_GPL(regulator_get_mode
);
4365 static int _regulator_get_error_flags(struct regulator_dev
*rdev
,
4366 unsigned int *flags
)
4370 regulator_lock(rdev
);
4373 if (!rdev
->desc
->ops
->get_error_flags
) {
4378 ret
= rdev
->desc
->ops
->get_error_flags(rdev
, flags
);
4380 regulator_unlock(rdev
);
4385 * regulator_get_error_flags - get regulator error information
4386 * @regulator: regulator source
4387 * @flags: pointer to store error flags
4389 * Get the current regulator error information.
4391 int regulator_get_error_flags(struct regulator
*regulator
,
4392 unsigned int *flags
)
4394 return _regulator_get_error_flags(regulator
->rdev
, flags
);
4396 EXPORT_SYMBOL_GPL(regulator_get_error_flags
);
4399 * regulator_set_load - set regulator load
4400 * @regulator: regulator source
4401 * @uA_load: load current
4403 * Notifies the regulator core of a new device load. This is then used by
4404 * DRMS (if enabled by constraints) to set the most efficient regulator
4405 * operating mode for the new regulator loading.
4407 * Consumer devices notify their supply regulator of the maximum power
4408 * they will require (can be taken from device datasheet in the power
4409 * consumption tables) when they change operational status and hence power
4410 * state. Examples of operational state changes that can affect power
4411 * consumption are :-
4413 * o Device is opened / closed.
4414 * o Device I/O is about to begin or has just finished.
4415 * o Device is idling in between work.
4417 * This information is also exported via sysfs to userspace.
4419 * DRMS will sum the total requested load on the regulator and change
4420 * to the most efficient operating mode if platform constraints allow.
4422 * NOTE: when a regulator consumer requests to have a regulator
4423 * disabled then any load that consumer requested no longer counts
4424 * toward the total requested load. If the regulator is re-enabled
4425 * then the previously requested load will start counting again.
4427 * If a regulator is an always-on regulator then an individual consumer's
4428 * load will still be removed if that consumer is fully disabled.
4430 * On error a negative errno is returned.
4432 int regulator_set_load(struct regulator
*regulator
, int uA_load
)
4434 struct regulator_dev
*rdev
= regulator
->rdev
;
4438 regulator_lock(rdev
);
4439 old_uA_load
= regulator
->uA_load
;
4440 regulator
->uA_load
= uA_load
;
4441 if (regulator
->enable_count
&& old_uA_load
!= uA_load
) {
4442 ret
= drms_uA_update(rdev
);
4444 regulator
->uA_load
= old_uA_load
;
4446 regulator_unlock(rdev
);
4450 EXPORT_SYMBOL_GPL(regulator_set_load
);
4453 * regulator_allow_bypass - allow the regulator to go into bypass mode
4455 * @regulator: Regulator to configure
4456 * @enable: enable or disable bypass mode
4458 * Allow the regulator to go into bypass mode if all other consumers
4459 * for the regulator also enable bypass mode and the machine
4460 * constraints allow this. Bypass mode means that the regulator is
4461 * simply passing the input directly to the output with no regulation.
4463 int regulator_allow_bypass(struct regulator
*regulator
, bool enable
)
4465 struct regulator_dev
*rdev
= regulator
->rdev
;
4466 const char *name
= rdev_get_name(rdev
);
4469 if (!rdev
->desc
->ops
->set_bypass
)
4472 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_BYPASS
))
4475 regulator_lock(rdev
);
4477 if (enable
&& !regulator
->bypass
) {
4478 rdev
->bypass_count
++;
4480 if (rdev
->bypass_count
== rdev
->open_count
) {
4481 trace_regulator_bypass_enable(name
);
4483 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
4485 rdev
->bypass_count
--;
4487 trace_regulator_bypass_enable_complete(name
);
4490 } else if (!enable
&& regulator
->bypass
) {
4491 rdev
->bypass_count
--;
4493 if (rdev
->bypass_count
!= rdev
->open_count
) {
4494 trace_regulator_bypass_disable(name
);
4496 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
4498 rdev
->bypass_count
++;
4500 trace_regulator_bypass_disable_complete(name
);
4505 regulator
->bypass
= enable
;
4507 regulator_unlock(rdev
);
4511 EXPORT_SYMBOL_GPL(regulator_allow_bypass
);
4514 * regulator_register_notifier - register regulator event notifier
4515 * @regulator: regulator source
4516 * @nb: notifier block
4518 * Register notifier block to receive regulator events.
4520 int regulator_register_notifier(struct regulator
*regulator
,
4521 struct notifier_block
*nb
)
4523 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
4526 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
4529 * regulator_unregister_notifier - unregister regulator event notifier
4530 * @regulator: regulator source
4531 * @nb: notifier block
4533 * Unregister regulator event notifier block.
4535 int regulator_unregister_notifier(struct regulator
*regulator
,
4536 struct notifier_block
*nb
)
4538 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
4541 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
4543 /* notify regulator consumers and downstream regulator consumers.
4544 * Note mutex must be held by caller.
4546 static int _notifier_call_chain(struct regulator_dev
*rdev
,
4547 unsigned long event
, void *data
)
4549 /* call rdev chain first */
4550 return blocking_notifier_call_chain(&rdev
->notifier
, event
, data
);
4554 * regulator_bulk_get - get multiple regulator consumers
4556 * @dev: Device to supply
4557 * @num_consumers: Number of consumers to register
4558 * @consumers: Configuration of consumers; clients are stored here.
4560 * @return 0 on success, an errno on failure.
4562 * This helper function allows drivers to get several regulator
4563 * consumers in one operation. If any of the regulators cannot be
4564 * acquired then any regulators that were allocated will be freed
4565 * before returning to the caller.
4567 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
4568 struct regulator_bulk_data
*consumers
)
4573 for (i
= 0; i
< num_consumers
; i
++)
4574 consumers
[i
].consumer
= NULL
;
4576 for (i
= 0; i
< num_consumers
; i
++) {
4577 consumers
[i
].consumer
= regulator_get(dev
,
4578 consumers
[i
].supply
);
4579 if (IS_ERR(consumers
[i
].consumer
)) {
4580 ret
= PTR_ERR(consumers
[i
].consumer
);
4581 consumers
[i
].consumer
= NULL
;
4589 if (ret
!= -EPROBE_DEFER
)
4590 dev_err(dev
, "Failed to get supply '%s': %pe\n",
4591 consumers
[i
].supply
, ERR_PTR(ret
));
4593 dev_dbg(dev
, "Failed to get supply '%s', deferring\n",
4594 consumers
[i
].supply
);
4597 regulator_put(consumers
[i
].consumer
);
4601 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
4603 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
4605 struct regulator_bulk_data
*bulk
= data
;
4607 bulk
->ret
= regulator_enable(bulk
->consumer
);
4611 * regulator_bulk_enable - enable multiple regulator consumers
4613 * @num_consumers: Number of consumers
4614 * @consumers: Consumer data; clients are stored here.
4615 * @return 0 on success, an errno on failure
4617 * This convenience API allows consumers to enable multiple regulator
4618 * clients in a single API call. If any consumers cannot be enabled
4619 * then any others that were enabled will be disabled again prior to
4622 int regulator_bulk_enable(int num_consumers
,
4623 struct regulator_bulk_data
*consumers
)
4625 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
4629 for (i
= 0; i
< num_consumers
; i
++) {
4630 async_schedule_domain(regulator_bulk_enable_async
,
4631 &consumers
[i
], &async_domain
);
4634 async_synchronize_full_domain(&async_domain
);
4636 /* If any consumer failed we need to unwind any that succeeded */
4637 for (i
= 0; i
< num_consumers
; i
++) {
4638 if (consumers
[i
].ret
!= 0) {
4639 ret
= consumers
[i
].ret
;
4647 for (i
= 0; i
< num_consumers
; i
++) {
4648 if (consumers
[i
].ret
< 0)
4649 pr_err("Failed to enable %s: %pe\n", consumers
[i
].supply
,
4650 ERR_PTR(consumers
[i
].ret
));
4652 regulator_disable(consumers
[i
].consumer
);
4657 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
4660 * regulator_bulk_disable - disable multiple regulator consumers
4662 * @num_consumers: Number of consumers
4663 * @consumers: Consumer data; clients are stored here.
4664 * @return 0 on success, an errno on failure
4666 * This convenience API allows consumers to disable multiple regulator
4667 * clients in a single API call. If any consumers cannot be disabled
4668 * then any others that were disabled will be enabled again prior to
4671 int regulator_bulk_disable(int num_consumers
,
4672 struct regulator_bulk_data
*consumers
)
4677 for (i
= num_consumers
- 1; i
>= 0; --i
) {
4678 ret
= regulator_disable(consumers
[i
].consumer
);
4686 pr_err("Failed to disable %s: %pe\n", consumers
[i
].supply
, ERR_PTR(ret
));
4687 for (++i
; i
< num_consumers
; ++i
) {
4688 r
= regulator_enable(consumers
[i
].consumer
);
4690 pr_err("Failed to re-enable %s: %pe\n",
4691 consumers
[i
].supply
, ERR_PTR(r
));
4696 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
4699 * regulator_bulk_force_disable - force disable multiple regulator consumers
4701 * @num_consumers: Number of consumers
4702 * @consumers: Consumer data; clients are stored here.
4703 * @return 0 on success, an errno on failure
4705 * This convenience API allows consumers to forcibly disable multiple regulator
4706 * clients in a single API call.
4707 * NOTE: This should be used for situations when device damage will
4708 * likely occur if the regulators are not disabled (e.g. over temp).
4709 * Although regulator_force_disable function call for some consumers can
4710 * return error numbers, the function is called for all consumers.
4712 int regulator_bulk_force_disable(int num_consumers
,
4713 struct regulator_bulk_data
*consumers
)
4718 for (i
= 0; i
< num_consumers
; i
++) {
4720 regulator_force_disable(consumers
[i
].consumer
);
4722 /* Store first error for reporting */
4723 if (consumers
[i
].ret
&& !ret
)
4724 ret
= consumers
[i
].ret
;
4729 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
4732 * regulator_bulk_free - free multiple regulator consumers
4734 * @num_consumers: Number of consumers
4735 * @consumers: Consumer data; clients are stored here.
4737 * This convenience API allows consumers to free multiple regulator
4738 * clients in a single API call.
4740 void regulator_bulk_free(int num_consumers
,
4741 struct regulator_bulk_data
*consumers
)
4745 for (i
= 0; i
< num_consumers
; i
++) {
4746 regulator_put(consumers
[i
].consumer
);
4747 consumers
[i
].consumer
= NULL
;
4750 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
4753 * regulator_notifier_call_chain - call regulator event notifier
4754 * @rdev: regulator source
4755 * @event: notifier block
4756 * @data: callback-specific data.
4758 * Called by regulator drivers to notify clients a regulator event has
4761 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
4762 unsigned long event
, void *data
)
4764 _notifier_call_chain(rdev
, event
, data
);
4768 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
4771 * regulator_mode_to_status - convert a regulator mode into a status
4773 * @mode: Mode to convert
4775 * Convert a regulator mode into a status.
4777 int regulator_mode_to_status(unsigned int mode
)
4780 case REGULATOR_MODE_FAST
:
4781 return REGULATOR_STATUS_FAST
;
4782 case REGULATOR_MODE_NORMAL
:
4783 return REGULATOR_STATUS_NORMAL
;
4784 case REGULATOR_MODE_IDLE
:
4785 return REGULATOR_STATUS_IDLE
;
4786 case REGULATOR_MODE_STANDBY
:
4787 return REGULATOR_STATUS_STANDBY
;
4789 return REGULATOR_STATUS_UNDEFINED
;
4792 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
4794 static struct attribute
*regulator_dev_attrs
[] = {
4795 &dev_attr_name
.attr
,
4796 &dev_attr_num_users
.attr
,
4797 &dev_attr_type
.attr
,
4798 &dev_attr_microvolts
.attr
,
4799 &dev_attr_microamps
.attr
,
4800 &dev_attr_opmode
.attr
,
4801 &dev_attr_state
.attr
,
4802 &dev_attr_status
.attr
,
4803 &dev_attr_bypass
.attr
,
4804 &dev_attr_requested_microamps
.attr
,
4805 &dev_attr_min_microvolts
.attr
,
4806 &dev_attr_max_microvolts
.attr
,
4807 &dev_attr_min_microamps
.attr
,
4808 &dev_attr_max_microamps
.attr
,
4809 &dev_attr_suspend_standby_state
.attr
,
4810 &dev_attr_suspend_mem_state
.attr
,
4811 &dev_attr_suspend_disk_state
.attr
,
4812 &dev_attr_suspend_standby_microvolts
.attr
,
4813 &dev_attr_suspend_mem_microvolts
.attr
,
4814 &dev_attr_suspend_disk_microvolts
.attr
,
4815 &dev_attr_suspend_standby_mode
.attr
,
4816 &dev_attr_suspend_mem_mode
.attr
,
4817 &dev_attr_suspend_disk_mode
.attr
,
4822 * To avoid cluttering sysfs (and memory) with useless state, only
4823 * create attributes that can be meaningfully displayed.
4825 static umode_t
regulator_attr_is_visible(struct kobject
*kobj
,
4826 struct attribute
*attr
, int idx
)
4828 struct device
*dev
= kobj_to_dev(kobj
);
4829 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4830 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
4831 umode_t mode
= attr
->mode
;
4833 /* these three are always present */
4834 if (attr
== &dev_attr_name
.attr
||
4835 attr
== &dev_attr_num_users
.attr
||
4836 attr
== &dev_attr_type
.attr
)
4839 /* some attributes need specific methods to be displayed */
4840 if (attr
== &dev_attr_microvolts
.attr
) {
4841 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
4842 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0) ||
4843 (ops
->list_voltage
&& ops
->list_voltage(rdev
, 0) >= 0) ||
4844 (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1))
4849 if (attr
== &dev_attr_microamps
.attr
)
4850 return ops
->get_current_limit
? mode
: 0;
4852 if (attr
== &dev_attr_opmode
.attr
)
4853 return ops
->get_mode
? mode
: 0;
4855 if (attr
== &dev_attr_state
.attr
)
4856 return (rdev
->ena_pin
|| ops
->is_enabled
) ? mode
: 0;
4858 if (attr
== &dev_attr_status
.attr
)
4859 return ops
->get_status
? mode
: 0;
4861 if (attr
== &dev_attr_bypass
.attr
)
4862 return ops
->get_bypass
? mode
: 0;
4864 /* constraints need specific supporting methods */
4865 if (attr
== &dev_attr_min_microvolts
.attr
||
4866 attr
== &dev_attr_max_microvolts
.attr
)
4867 return (ops
->set_voltage
|| ops
->set_voltage_sel
) ? mode
: 0;
4869 if (attr
== &dev_attr_min_microamps
.attr
||
4870 attr
== &dev_attr_max_microamps
.attr
)
4871 return ops
->set_current_limit
? mode
: 0;
4873 if (attr
== &dev_attr_suspend_standby_state
.attr
||
4874 attr
== &dev_attr_suspend_mem_state
.attr
||
4875 attr
== &dev_attr_suspend_disk_state
.attr
)
4878 if (attr
== &dev_attr_suspend_standby_microvolts
.attr
||
4879 attr
== &dev_attr_suspend_mem_microvolts
.attr
||
4880 attr
== &dev_attr_suspend_disk_microvolts
.attr
)
4881 return ops
->set_suspend_voltage
? mode
: 0;
4883 if (attr
== &dev_attr_suspend_standby_mode
.attr
||
4884 attr
== &dev_attr_suspend_mem_mode
.attr
||
4885 attr
== &dev_attr_suspend_disk_mode
.attr
)
4886 return ops
->set_suspend_mode
? mode
: 0;
4891 static const struct attribute_group regulator_dev_group
= {
4892 .attrs
= regulator_dev_attrs
,
4893 .is_visible
= regulator_attr_is_visible
,
4896 static const struct attribute_group
*regulator_dev_groups
[] = {
4897 ®ulator_dev_group
,
4901 static void regulator_dev_release(struct device
*dev
)
4903 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
4905 kfree(rdev
->constraints
);
4906 of_node_put(rdev
->dev
.of_node
);
4910 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
4912 struct device
*parent
= rdev
->dev
.parent
;
4913 const char *rname
= rdev_get_name(rdev
);
4914 char name
[NAME_MAX
];
4916 /* Avoid duplicate debugfs directory names */
4917 if (parent
&& rname
== rdev
->desc
->name
) {
4918 snprintf(name
, sizeof(name
), "%s-%s", dev_name(parent
),
4923 rdev
->debugfs
= debugfs_create_dir(rname
, debugfs_root
);
4924 if (!rdev
->debugfs
) {
4925 rdev_warn(rdev
, "Failed to create debugfs directory\n");
4929 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
4931 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
4933 debugfs_create_u32("bypass_count", 0444, rdev
->debugfs
,
4934 &rdev
->bypass_count
);
4937 static int regulator_register_resolve_supply(struct device
*dev
, void *data
)
4939 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4941 if (regulator_resolve_supply(rdev
))
4942 rdev_dbg(rdev
, "unable to resolve supply\n");
4947 int regulator_coupler_register(struct regulator_coupler
*coupler
)
4949 mutex_lock(®ulator_list_mutex
);
4950 list_add_tail(&coupler
->list
, ®ulator_coupler_list
);
4951 mutex_unlock(®ulator_list_mutex
);
4956 static struct regulator_coupler
*
4957 regulator_find_coupler(struct regulator_dev
*rdev
)
4959 struct regulator_coupler
*coupler
;
4963 * Note that regulators are appended to the list and the generic
4964 * coupler is registered first, hence it will be attached at last
4967 list_for_each_entry_reverse(coupler
, ®ulator_coupler_list
, list
) {
4968 err
= coupler
->attach_regulator(coupler
, rdev
);
4970 if (!coupler
->balance_voltage
&&
4971 rdev
->coupling_desc
.n_coupled
> 2)
4972 goto err_unsupported
;
4978 return ERR_PTR(err
);
4986 return ERR_PTR(-EINVAL
);
4989 if (coupler
->detach_regulator
)
4990 coupler
->detach_regulator(coupler
, rdev
);
4993 "Voltage balancing for multiple regulator couples is unimplemented\n");
4995 return ERR_PTR(-EPERM
);
4998 static void regulator_resolve_coupling(struct regulator_dev
*rdev
)
5000 struct regulator_coupler
*coupler
= rdev
->coupling_desc
.coupler
;
5001 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
5002 int n_coupled
= c_desc
->n_coupled
;
5003 struct regulator_dev
*c_rdev
;
5006 for (i
= 1; i
< n_coupled
; i
++) {
5007 /* already resolved */
5008 if (c_desc
->coupled_rdevs
[i
])
5011 c_rdev
= of_parse_coupled_regulator(rdev
, i
- 1);
5016 if (c_rdev
->coupling_desc
.coupler
!= coupler
) {
5017 rdev_err(rdev
, "coupler mismatch with %s\n",
5018 rdev_get_name(c_rdev
));
5022 c_desc
->coupled_rdevs
[i
] = c_rdev
;
5023 c_desc
->n_resolved
++;
5025 regulator_resolve_coupling(c_rdev
);
5029 static void regulator_remove_coupling(struct regulator_dev
*rdev
)
5031 struct regulator_coupler
*coupler
= rdev
->coupling_desc
.coupler
;
5032 struct coupling_desc
*__c_desc
, *c_desc
= &rdev
->coupling_desc
;
5033 struct regulator_dev
*__c_rdev
, *c_rdev
;
5034 unsigned int __n_coupled
, n_coupled
;
5038 n_coupled
= c_desc
->n_coupled
;
5040 for (i
= 1; i
< n_coupled
; i
++) {
5041 c_rdev
= c_desc
->coupled_rdevs
[i
];
5046 regulator_lock(c_rdev
);
5048 __c_desc
= &c_rdev
->coupling_desc
;
5049 __n_coupled
= __c_desc
->n_coupled
;
5051 for (k
= 1; k
< __n_coupled
; k
++) {
5052 __c_rdev
= __c_desc
->coupled_rdevs
[k
];
5054 if (__c_rdev
== rdev
) {
5055 __c_desc
->coupled_rdevs
[k
] = NULL
;
5056 __c_desc
->n_resolved
--;
5061 regulator_unlock(c_rdev
);
5063 c_desc
->coupled_rdevs
[i
] = NULL
;
5064 c_desc
->n_resolved
--;
5067 if (coupler
&& coupler
->detach_regulator
) {
5068 err
= coupler
->detach_regulator(coupler
, rdev
);
5070 rdev_err(rdev
, "failed to detach from coupler: %pe\n",
5074 kfree(rdev
->coupling_desc
.coupled_rdevs
);
5075 rdev
->coupling_desc
.coupled_rdevs
= NULL
;
5078 static int regulator_init_coupling(struct regulator_dev
*rdev
)
5080 struct regulator_dev
**coupled
;
5081 int err
, n_phandles
;
5083 if (!IS_ENABLED(CONFIG_OF
))
5086 n_phandles
= of_get_n_coupled(rdev
);
5088 coupled
= kcalloc(n_phandles
+ 1, sizeof(*coupled
), GFP_KERNEL
);
5092 rdev
->coupling_desc
.coupled_rdevs
= coupled
;
5095 * Every regulator should always have coupling descriptor filled with
5096 * at least pointer to itself.
5098 rdev
->coupling_desc
.coupled_rdevs
[0] = rdev
;
5099 rdev
->coupling_desc
.n_coupled
= n_phandles
+ 1;
5100 rdev
->coupling_desc
.n_resolved
++;
5102 /* regulator isn't coupled */
5103 if (n_phandles
== 0)
5106 if (!of_check_coupling_data(rdev
))
5109 mutex_lock(®ulator_list_mutex
);
5110 rdev
->coupling_desc
.coupler
= regulator_find_coupler(rdev
);
5111 mutex_unlock(®ulator_list_mutex
);
5113 if (IS_ERR(rdev
->coupling_desc
.coupler
)) {
5114 err
= PTR_ERR(rdev
->coupling_desc
.coupler
);
5115 rdev_err(rdev
, "failed to get coupler: %pe\n", ERR_PTR(err
));
5122 static int generic_coupler_attach(struct regulator_coupler
*coupler
,
5123 struct regulator_dev
*rdev
)
5125 if (rdev
->coupling_desc
.n_coupled
> 2) {
5127 "Voltage balancing for multiple regulator couples is unimplemented\n");
5131 if (!rdev
->constraints
->always_on
) {
5133 "Coupling of a non always-on regulator is unimplemented\n");
5140 static struct regulator_coupler generic_regulator_coupler
= {
5141 .attach_regulator
= generic_coupler_attach
,
5145 * regulator_register - register regulator
5146 * @regulator_desc: regulator to register
5147 * @cfg: runtime configuration for regulator
5149 * Called by regulator drivers to register a regulator.
5150 * Returns a valid pointer to struct regulator_dev on success
5151 * or an ERR_PTR() on error.
5153 struct regulator_dev
*
5154 regulator_register(const struct regulator_desc
*regulator_desc
,
5155 const struct regulator_config
*cfg
)
5157 const struct regulator_init_data
*init_data
;
5158 struct regulator_config
*config
= NULL
;
5159 static atomic_t regulator_no
= ATOMIC_INIT(-1);
5160 struct regulator_dev
*rdev
;
5161 bool dangling_cfg_gpiod
= false;
5162 bool dangling_of_gpiod
= false;
5167 return ERR_PTR(-EINVAL
);
5169 dangling_cfg_gpiod
= true;
5170 if (regulator_desc
== NULL
) {
5178 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
) {
5183 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
5184 regulator_desc
->type
!= REGULATOR_CURRENT
) {
5189 /* Only one of each should be implemented */
5190 WARN_ON(regulator_desc
->ops
->get_voltage
&&
5191 regulator_desc
->ops
->get_voltage_sel
);
5192 WARN_ON(regulator_desc
->ops
->set_voltage
&&
5193 regulator_desc
->ops
->set_voltage_sel
);
5195 /* If we're using selectors we must implement list_voltage. */
5196 if (regulator_desc
->ops
->get_voltage_sel
&&
5197 !regulator_desc
->ops
->list_voltage
) {
5201 if (regulator_desc
->ops
->set_voltage_sel
&&
5202 !regulator_desc
->ops
->list_voltage
) {
5207 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
5212 device_initialize(&rdev
->dev
);
5215 * Duplicate the config so the driver could override it after
5216 * parsing init data.
5218 config
= kmemdup(cfg
, sizeof(*cfg
), GFP_KERNEL
);
5219 if (config
== NULL
) {
5224 init_data
= regulator_of_get_init_data(dev
, regulator_desc
, config
,
5225 &rdev
->dev
.of_node
);
5228 * Sometimes not all resources are probed already so we need to take
5229 * that into account. This happens most the time if the ena_gpiod comes
5230 * from a gpio extender or something else.
5232 if (PTR_ERR(init_data
) == -EPROBE_DEFER
) {
5233 ret
= -EPROBE_DEFER
;
5238 * We need to keep track of any GPIO descriptor coming from the
5239 * device tree until we have handled it over to the core. If the
5240 * config that was passed in to this function DOES NOT contain
5241 * a descriptor, and the config after this call DOES contain
5242 * a descriptor, we definitely got one from parsing the device
5245 if (!cfg
->ena_gpiod
&& config
->ena_gpiod
)
5246 dangling_of_gpiod
= true;
5248 init_data
= config
->init_data
;
5249 rdev
->dev
.of_node
= of_node_get(config
->of_node
);
5252 ww_mutex_init(&rdev
->mutex
, ®ulator_ww_class
);
5253 rdev
->reg_data
= config
->driver_data
;
5254 rdev
->owner
= regulator_desc
->owner
;
5255 rdev
->desc
= regulator_desc
;
5257 rdev
->regmap
= config
->regmap
;
5258 else if (dev_get_regmap(dev
, NULL
))
5259 rdev
->regmap
= dev_get_regmap(dev
, NULL
);
5260 else if (dev
->parent
)
5261 rdev
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
5262 INIT_LIST_HEAD(&rdev
->consumer_list
);
5263 INIT_LIST_HEAD(&rdev
->list
);
5264 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
5265 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
5267 /* preform any regulator specific init */
5268 if (init_data
&& init_data
->regulator_init
) {
5269 ret
= init_data
->regulator_init(rdev
->reg_data
);
5274 if (config
->ena_gpiod
) {
5275 ret
= regulator_ena_gpio_request(rdev
, config
);
5277 rdev_err(rdev
, "Failed to request enable GPIO: %pe\n",
5281 /* The regulator core took over the GPIO descriptor */
5282 dangling_cfg_gpiod
= false;
5283 dangling_of_gpiod
= false;
5286 /* register with sysfs */
5287 rdev
->dev
.class = ®ulator_class
;
5288 rdev
->dev
.parent
= dev
;
5289 dev_set_name(&rdev
->dev
, "regulator.%lu",
5290 (unsigned long) atomic_inc_return(®ulator_no
));
5291 dev_set_drvdata(&rdev
->dev
, rdev
);
5293 /* set regulator constraints */
5295 rdev
->constraints
= kmemdup(&init_data
->constraints
,
5296 sizeof(*rdev
->constraints
),
5299 rdev
->constraints
= kzalloc(sizeof(*rdev
->constraints
),
5301 if (!rdev
->constraints
) {
5306 if (init_data
&& init_data
->supply_regulator
)
5307 rdev
->supply_name
= init_data
->supply_regulator
;
5308 else if (regulator_desc
->supply_name
)
5309 rdev
->supply_name
= regulator_desc
->supply_name
;
5311 ret
= set_machine_constraints(rdev
);
5312 if (ret
== -EPROBE_DEFER
) {
5313 /* Regulator might be in bypass mode and so needs its supply
5314 * to set the constraints */
5315 /* FIXME: this currently triggers a chicken-and-egg problem
5316 * when creating -SUPPLY symlink in sysfs to a regulator
5317 * that is just being created */
5318 rdev_dbg(rdev
, "will resolve supply early: %s\n",
5320 ret
= regulator_resolve_supply(rdev
);
5322 ret
= set_machine_constraints(rdev
);
5324 rdev_dbg(rdev
, "unable to resolve supply early: %pe\n",
5330 ret
= regulator_init_coupling(rdev
);
5334 /* add consumers devices */
5336 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
5337 ret
= set_consumer_device_supply(rdev
,
5338 init_data
->consumer_supplies
[i
].dev_name
,
5339 init_data
->consumer_supplies
[i
].supply
);
5341 dev_err(dev
, "Failed to set supply %s\n",
5342 init_data
->consumer_supplies
[i
].supply
);
5343 goto unset_supplies
;
5348 if (!rdev
->desc
->ops
->get_voltage
&&
5349 !rdev
->desc
->ops
->list_voltage
&&
5350 !rdev
->desc
->fixed_uV
)
5351 rdev
->is_switch
= true;
5353 ret
= device_add(&rdev
->dev
);
5355 goto unset_supplies
;
5357 rdev_init_debugfs(rdev
);
5359 /* try to resolve regulators coupling since a new one was registered */
5360 mutex_lock(®ulator_list_mutex
);
5361 regulator_resolve_coupling(rdev
);
5362 mutex_unlock(®ulator_list_mutex
);
5364 /* try to resolve regulators supply since a new one was registered */
5365 class_for_each_device(®ulator_class
, NULL
, NULL
,
5366 regulator_register_resolve_supply
);
5371 mutex_lock(®ulator_list_mutex
);
5372 unset_regulator_supplies(rdev
);
5373 regulator_remove_coupling(rdev
);
5374 mutex_unlock(®ulator_list_mutex
);
5376 kfree(rdev
->coupling_desc
.coupled_rdevs
);
5377 mutex_lock(®ulator_list_mutex
);
5378 regulator_ena_gpio_free(rdev
);
5379 mutex_unlock(®ulator_list_mutex
);
5381 if (dangling_of_gpiod
)
5382 gpiod_put(config
->ena_gpiod
);
5384 put_device(&rdev
->dev
);
5386 if (dangling_cfg_gpiod
)
5387 gpiod_put(cfg
->ena_gpiod
);
5388 return ERR_PTR(ret
);
5390 EXPORT_SYMBOL_GPL(regulator_register
);
5393 * regulator_unregister - unregister regulator
5394 * @rdev: regulator to unregister
5396 * Called by regulator drivers to unregister a regulator.
5398 void regulator_unregister(struct regulator_dev
*rdev
)
5404 while (rdev
->use_count
--)
5405 regulator_disable(rdev
->supply
);
5406 regulator_put(rdev
->supply
);
5409 flush_work(&rdev
->disable_work
.work
);
5411 mutex_lock(®ulator_list_mutex
);
5413 debugfs_remove_recursive(rdev
->debugfs
);
5414 WARN_ON(rdev
->open_count
);
5415 regulator_remove_coupling(rdev
);
5416 unset_regulator_supplies(rdev
);
5417 list_del(&rdev
->list
);
5418 regulator_ena_gpio_free(rdev
);
5419 device_unregister(&rdev
->dev
);
5421 mutex_unlock(®ulator_list_mutex
);
5423 EXPORT_SYMBOL_GPL(regulator_unregister
);
5425 #ifdef CONFIG_SUSPEND
5427 * regulator_suspend - prepare regulators for system wide suspend
5428 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5430 * Configure each regulator with it's suspend operating parameters for state.
5432 static int regulator_suspend(struct device
*dev
)
5434 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5435 suspend_state_t state
= pm_suspend_target_state
;
5437 const struct regulator_state
*rstate
;
5439 rstate
= regulator_get_suspend_state_check(rdev
, state
);
5443 regulator_lock(rdev
);
5444 ret
= __suspend_set_state(rdev
, rstate
);
5445 regulator_unlock(rdev
);
5450 static int regulator_resume(struct device
*dev
)
5452 suspend_state_t state
= pm_suspend_target_state
;
5453 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5454 struct regulator_state
*rstate
;
5457 rstate
= regulator_get_suspend_state(rdev
, state
);
5461 /* Avoid grabbing the lock if we don't need to */
5462 if (!rdev
->desc
->ops
->resume
)
5465 regulator_lock(rdev
);
5467 if (rstate
->enabled
== ENABLE_IN_SUSPEND
||
5468 rstate
->enabled
== DISABLE_IN_SUSPEND
)
5469 ret
= rdev
->desc
->ops
->resume(rdev
);
5471 regulator_unlock(rdev
);
5475 #else /* !CONFIG_SUSPEND */
5477 #define regulator_suspend NULL
5478 #define regulator_resume NULL
5480 #endif /* !CONFIG_SUSPEND */
5483 static const struct dev_pm_ops __maybe_unused regulator_pm_ops
= {
5484 .suspend
= regulator_suspend
,
5485 .resume
= regulator_resume
,
5489 struct class regulator_class
= {
5490 .name
= "regulator",
5491 .dev_release
= regulator_dev_release
,
5492 .dev_groups
= regulator_dev_groups
,
5494 .pm
= ®ulator_pm_ops
,
5498 * regulator_has_full_constraints - the system has fully specified constraints
5500 * Calling this function will cause the regulator API to disable all
5501 * regulators which have a zero use count and don't have an always_on
5502 * constraint in a late_initcall.
5504 * The intention is that this will become the default behaviour in a
5505 * future kernel release so users are encouraged to use this facility
5508 void regulator_has_full_constraints(void)
5510 has_full_constraints
= 1;
5512 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
5515 * rdev_get_drvdata - get rdev regulator driver data
5518 * Get rdev regulator driver private data. This call can be used in the
5519 * regulator driver context.
5521 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
5523 return rdev
->reg_data
;
5525 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
5528 * regulator_get_drvdata - get regulator driver data
5529 * @regulator: regulator
5531 * Get regulator driver private data. This call can be used in the consumer
5532 * driver context when non API regulator specific functions need to be called.
5534 void *regulator_get_drvdata(struct regulator
*regulator
)
5536 return regulator
->rdev
->reg_data
;
5538 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
5541 * regulator_set_drvdata - set regulator driver data
5542 * @regulator: regulator
5545 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
5547 regulator
->rdev
->reg_data
= data
;
5549 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
5552 * rdev_get_id - get regulator ID
5555 int rdev_get_id(struct regulator_dev
*rdev
)
5557 return rdev
->desc
->id
;
5559 EXPORT_SYMBOL_GPL(rdev_get_id
);
5561 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
5565 EXPORT_SYMBOL_GPL(rdev_get_dev
);
5567 struct regmap
*rdev_get_regmap(struct regulator_dev
*rdev
)
5569 return rdev
->regmap
;
5571 EXPORT_SYMBOL_GPL(rdev_get_regmap
);
5573 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
5575 return reg_init_data
->driver_data
;
5577 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
5579 #ifdef CONFIG_DEBUG_FS
5580 static int supply_map_show(struct seq_file
*sf
, void *data
)
5582 struct regulator_map
*map
;
5584 list_for_each_entry(map
, ®ulator_map_list
, list
) {
5585 seq_printf(sf
, "%s -> %s.%s\n",
5586 rdev_get_name(map
->regulator
), map
->dev_name
,
5592 DEFINE_SHOW_ATTRIBUTE(supply_map
);
5594 struct summary_data
{
5596 struct regulator_dev
*parent
;
5600 static void regulator_summary_show_subtree(struct seq_file
*s
,
5601 struct regulator_dev
*rdev
,
5604 static int regulator_summary_show_children(struct device
*dev
, void *data
)
5606 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5607 struct summary_data
*summary_data
= data
;
5609 if (rdev
->supply
&& rdev
->supply
->rdev
== summary_data
->parent
)
5610 regulator_summary_show_subtree(summary_data
->s
, rdev
,
5611 summary_data
->level
+ 1);
5616 static void regulator_summary_show_subtree(struct seq_file
*s
,
5617 struct regulator_dev
*rdev
,
5620 struct regulation_constraints
*c
;
5621 struct regulator
*consumer
;
5622 struct summary_data summary_data
;
5623 unsigned int opmode
;
5628 opmode
= _regulator_get_mode_unlocked(rdev
);
5629 seq_printf(s
, "%*s%-*s %3d %4d %6d %7s ",
5631 30 - level
* 3, rdev_get_name(rdev
),
5632 rdev
->use_count
, rdev
->open_count
, rdev
->bypass_count
,
5633 regulator_opmode_to_str(opmode
));
5635 seq_printf(s
, "%5dmV ", regulator_get_voltage_rdev(rdev
) / 1000);
5636 seq_printf(s
, "%5dmA ",
5637 _regulator_get_current_limit_unlocked(rdev
) / 1000);
5639 c
= rdev
->constraints
;
5641 switch (rdev
->desc
->type
) {
5642 case REGULATOR_VOLTAGE
:
5643 seq_printf(s
, "%5dmV %5dmV ",
5644 c
->min_uV
/ 1000, c
->max_uV
/ 1000);
5646 case REGULATOR_CURRENT
:
5647 seq_printf(s
, "%5dmA %5dmA ",
5648 c
->min_uA
/ 1000, c
->max_uA
/ 1000);
5655 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
) {
5656 if (consumer
->dev
&& consumer
->dev
->class == ®ulator_class
)
5659 seq_printf(s
, "%*s%-*s ",
5660 (level
+ 1) * 3 + 1, "",
5661 30 - (level
+ 1) * 3,
5662 consumer
->supply_name
? consumer
->supply_name
:
5663 consumer
->dev
? dev_name(consumer
->dev
) : "deviceless");
5665 switch (rdev
->desc
->type
) {
5666 case REGULATOR_VOLTAGE
:
5667 seq_printf(s
, "%3d %33dmA%c%5dmV %5dmV",
5668 consumer
->enable_count
,
5669 consumer
->uA_load
/ 1000,
5670 consumer
->uA_load
&& !consumer
->enable_count
?
5672 consumer
->voltage
[PM_SUSPEND_ON
].min_uV
/ 1000,
5673 consumer
->voltage
[PM_SUSPEND_ON
].max_uV
/ 1000);
5675 case REGULATOR_CURRENT
:
5683 summary_data
.level
= level
;
5684 summary_data
.parent
= rdev
;
5686 class_for_each_device(®ulator_class
, NULL
, &summary_data
,
5687 regulator_summary_show_children
);
5690 struct summary_lock_data
{
5691 struct ww_acquire_ctx
*ww_ctx
;
5692 struct regulator_dev
**new_contended_rdev
;
5693 struct regulator_dev
**old_contended_rdev
;
5696 static int regulator_summary_lock_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
->old_contended_rdev
) {
5703 ret
= regulator_lock_nested(rdev
, lock_data
->ww_ctx
);
5705 if (ret
== -EDEADLK
)
5706 *lock_data
->new_contended_rdev
= rdev
;
5710 *lock_data
->old_contended_rdev
= NULL
;
5716 static int regulator_summary_unlock_one(struct device
*dev
, void *data
)
5718 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5719 struct summary_lock_data
*lock_data
= data
;
5722 if (rdev
== *lock_data
->new_contended_rdev
)
5726 regulator_unlock(rdev
);
5731 static int regulator_summary_lock_all(struct ww_acquire_ctx
*ww_ctx
,
5732 struct regulator_dev
**new_contended_rdev
,
5733 struct regulator_dev
**old_contended_rdev
)
5735 struct summary_lock_data lock_data
;
5738 lock_data
.ww_ctx
= ww_ctx
;
5739 lock_data
.new_contended_rdev
= new_contended_rdev
;
5740 lock_data
.old_contended_rdev
= old_contended_rdev
;
5742 ret
= class_for_each_device(®ulator_class
, NULL
, &lock_data
,
5743 regulator_summary_lock_one
);
5745 class_for_each_device(®ulator_class
, NULL
, &lock_data
,
5746 regulator_summary_unlock_one
);
5751 static void regulator_summary_lock(struct ww_acquire_ctx
*ww_ctx
)
5753 struct regulator_dev
*new_contended_rdev
= NULL
;
5754 struct regulator_dev
*old_contended_rdev
= NULL
;
5757 mutex_lock(®ulator_list_mutex
);
5759 ww_acquire_init(ww_ctx
, ®ulator_ww_class
);
5762 if (new_contended_rdev
) {
5763 ww_mutex_lock_slow(&new_contended_rdev
->mutex
, ww_ctx
);
5764 old_contended_rdev
= new_contended_rdev
;
5765 old_contended_rdev
->ref_cnt
++;
5768 err
= regulator_summary_lock_all(ww_ctx
,
5769 &new_contended_rdev
,
5770 &old_contended_rdev
);
5772 if (old_contended_rdev
)
5773 regulator_unlock(old_contended_rdev
);
5775 } while (err
== -EDEADLK
);
5777 ww_acquire_done(ww_ctx
);
5780 static void regulator_summary_unlock(struct ww_acquire_ctx
*ww_ctx
)
5782 class_for_each_device(®ulator_class
, NULL
, NULL
,
5783 regulator_summary_unlock_one
);
5784 ww_acquire_fini(ww_ctx
);
5786 mutex_unlock(®ulator_list_mutex
);
5789 static int regulator_summary_show_roots(struct device
*dev
, void *data
)
5791 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5792 struct seq_file
*s
= data
;
5795 regulator_summary_show_subtree(s
, rdev
, 0);
5800 static int regulator_summary_show(struct seq_file
*s
, void *data
)
5802 struct ww_acquire_ctx ww_ctx
;
5804 seq_puts(s
, " regulator use open bypass opmode voltage current min max\n");
5805 seq_puts(s
, "---------------------------------------------------------------------------------------\n");
5807 regulator_summary_lock(&ww_ctx
);
5809 class_for_each_device(®ulator_class
, NULL
, s
,
5810 regulator_summary_show_roots
);
5812 regulator_summary_unlock(&ww_ctx
);
5816 DEFINE_SHOW_ATTRIBUTE(regulator_summary
);
5817 #endif /* CONFIG_DEBUG_FS */
5819 static int __init
regulator_init(void)
5823 ret
= class_register(®ulator_class
);
5825 debugfs_root
= debugfs_create_dir("regulator", NULL
);
5827 pr_warn("regulator: Failed to create debugfs directory\n");
5829 #ifdef CONFIG_DEBUG_FS
5830 debugfs_create_file("supply_map", 0444, debugfs_root
, NULL
,
5833 debugfs_create_file("regulator_summary", 0444, debugfs_root
,
5834 NULL
, ®ulator_summary_fops
);
5836 regulator_dummy_init();
5838 regulator_coupler_register(&generic_regulator_coupler
);
5843 /* init early to allow our consumers to complete system booting */
5844 core_initcall(regulator_init
);
5846 static int regulator_late_cleanup(struct device
*dev
, void *data
)
5848 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5849 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
5850 struct regulation_constraints
*c
= rdev
->constraints
;
5853 if (c
&& c
->always_on
)
5856 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
))
5859 regulator_lock(rdev
);
5861 if (rdev
->use_count
)
5864 /* If we can't read the status assume it's always on. */
5865 if (ops
->is_enabled
)
5866 enabled
= ops
->is_enabled(rdev
);
5870 /* But if reading the status failed, assume that it's off. */
5874 if (have_full_constraints()) {
5875 /* We log since this may kill the system if it goes
5877 rdev_info(rdev
, "disabling\n");
5878 ret
= _regulator_do_disable(rdev
);
5880 rdev_err(rdev
, "couldn't disable: %pe\n", ERR_PTR(ret
));
5882 /* The intention is that in future we will
5883 * assume that full constraints are provided
5884 * so warn even if we aren't going to do
5887 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
5891 regulator_unlock(rdev
);
5896 static void regulator_init_complete_work_function(struct work_struct
*work
)
5899 * Regulators may had failed to resolve their input supplies
5900 * when were registered, either because the input supply was
5901 * not registered yet or because its parent device was not
5902 * bound yet. So attempt to resolve the input supplies for
5903 * pending regulators before trying to disable unused ones.
5905 class_for_each_device(®ulator_class
, NULL
, NULL
,
5906 regulator_register_resolve_supply
);
5908 /* If we have a full configuration then disable any regulators
5909 * we have permission to change the status for and which are
5910 * not in use or always_on. This is effectively the default
5911 * for DT and ACPI as they have full constraints.
5913 class_for_each_device(®ulator_class
, NULL
, NULL
,
5914 regulator_late_cleanup
);
5917 static DECLARE_DELAYED_WORK(regulator_init_complete_work
,
5918 regulator_init_complete_work_function
);
5920 static int __init
regulator_init_complete(void)
5923 * Since DT doesn't provide an idiomatic mechanism for
5924 * enabling full constraints and since it's much more natural
5925 * with DT to provide them just assume that a DT enabled
5926 * system has full constraints.
5928 if (of_have_populated_dt())
5929 has_full_constraints
= true;
5932 * We punt completion for an arbitrary amount of time since
5933 * systems like distros will load many drivers from userspace
5934 * so consumers might not always be ready yet, this is
5935 * particularly an issue with laptops where this might bounce
5936 * the display off then on. Ideally we'd get a notification
5937 * from userspace when this happens but we don't so just wait
5938 * a bit and hope we waited long enough. It'd be better if
5939 * we'd only do this on systems that need it, and a kernel
5940 * command line option might be useful.
5942 schedule_delayed_work(®ulator_init_complete_work
,
5943 msecs_to_jiffies(30000));
5947 late_initcall_sync(regulator_init_complete
);