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/reboot.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/regulator/coupler.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/machine.h>
29 #include <linux/module.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/regulator.h>
38 static DEFINE_WW_CLASS(regulator_ww_class
);
39 static DEFINE_MUTEX(regulator_nesting_mutex
);
40 static DEFINE_MUTEX(regulator_list_mutex
);
41 static LIST_HEAD(regulator_map_list
);
42 static LIST_HEAD(regulator_ena_gpio_list
);
43 static LIST_HEAD(regulator_supply_alias_list
);
44 static LIST_HEAD(regulator_coupler_list
);
45 static bool has_full_constraints
;
47 static struct dentry
*debugfs_root
;
50 * struct regulator_map
52 * Used to provide symbolic supply names to devices.
54 struct regulator_map
{
55 struct list_head list
;
56 const char *dev_name
; /* The dev_name() for the consumer */
58 struct regulator_dev
*regulator
;
62 * struct regulator_enable_gpio
64 * Management for shared enable GPIO pin
66 struct regulator_enable_gpio
{
67 struct list_head list
;
68 struct gpio_desc
*gpiod
;
69 u32 enable_count
; /* a number of enabled shared GPIO */
70 u32 request_count
; /* a number of requested shared GPIO */
74 * struct regulator_supply_alias
76 * Used to map lookups for a supply onto an alternative device.
78 struct regulator_supply_alias
{
79 struct list_head list
;
80 struct device
*src_dev
;
81 const char *src_supply
;
82 struct device
*alias_dev
;
83 const char *alias_supply
;
86 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
87 static int _regulator_disable(struct regulator
*regulator
);
88 static int _regulator_get_error_flags(struct regulator_dev
*rdev
, unsigned int *flags
);
89 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
90 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
91 static int _notifier_call_chain(struct regulator_dev
*rdev
,
92 unsigned long event
, void *data
);
93 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
94 int min_uV
, int max_uV
);
95 static int regulator_balance_voltage(struct regulator_dev
*rdev
,
96 suspend_state_t state
);
97 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
99 const char *supply_name
);
100 static void destroy_regulator(struct regulator
*regulator
);
101 static void _regulator_put(struct regulator
*regulator
);
103 const char *rdev_get_name(struct regulator_dev
*rdev
)
105 if (rdev
->constraints
&& rdev
->constraints
->name
)
106 return rdev
->constraints
->name
;
107 else if (rdev
->desc
->name
)
108 return rdev
->desc
->name
;
112 EXPORT_SYMBOL_GPL(rdev_get_name
);
114 static bool have_full_constraints(void)
116 return has_full_constraints
|| of_have_populated_dt();
119 static bool regulator_ops_is_valid(struct regulator_dev
*rdev
, int ops
)
121 if (!rdev
->constraints
) {
122 rdev_err(rdev
, "no constraints\n");
126 if (rdev
->constraints
->valid_ops_mask
& ops
)
133 * regulator_lock_nested - lock a single regulator
134 * @rdev: regulator source
135 * @ww_ctx: w/w mutex acquire context
137 * This function can be called many times by one task on
138 * a single regulator and its mutex will be locked only
139 * once. If a task, which is calling this function is other
140 * than the one, which initially locked the mutex, it will
143 * Return: 0 on success or a negative error number on failure.
145 static inline int regulator_lock_nested(struct regulator_dev
*rdev
,
146 struct ww_acquire_ctx
*ww_ctx
)
151 mutex_lock(®ulator_nesting_mutex
);
153 if (!ww_mutex_trylock(&rdev
->mutex
, ww_ctx
)) {
154 if (rdev
->mutex_owner
== current
)
160 mutex_unlock(®ulator_nesting_mutex
);
161 ret
= ww_mutex_lock(&rdev
->mutex
, ww_ctx
);
162 mutex_lock(®ulator_nesting_mutex
);
168 if (lock
&& ret
!= -EDEADLK
) {
170 rdev
->mutex_owner
= current
;
173 mutex_unlock(®ulator_nesting_mutex
);
179 * regulator_lock - lock a single regulator
180 * @rdev: regulator source
182 * This function can be called many times by one task on
183 * a single regulator and its mutex will be locked only
184 * once. If a task, which is calling this function is other
185 * than the one, which initially locked the mutex, it will
188 static void regulator_lock(struct regulator_dev
*rdev
)
190 regulator_lock_nested(rdev
, NULL
);
194 * regulator_unlock - unlock a single regulator
195 * @rdev: regulator_source
197 * This function unlocks the mutex when the
198 * reference counter reaches 0.
200 static void regulator_unlock(struct regulator_dev
*rdev
)
202 mutex_lock(®ulator_nesting_mutex
);
204 if (--rdev
->ref_cnt
== 0) {
205 rdev
->mutex_owner
= NULL
;
206 ww_mutex_unlock(&rdev
->mutex
);
209 WARN_ON_ONCE(rdev
->ref_cnt
< 0);
211 mutex_unlock(®ulator_nesting_mutex
);
215 * regulator_lock_two - lock two regulators
216 * @rdev1: first regulator
217 * @rdev2: second regulator
218 * @ww_ctx: w/w mutex acquire context
220 * Locks both rdevs using the regulator_ww_class.
222 static void regulator_lock_two(struct regulator_dev
*rdev1
,
223 struct regulator_dev
*rdev2
,
224 struct ww_acquire_ctx
*ww_ctx
)
226 struct regulator_dev
*held
, *contended
;
229 ww_acquire_init(ww_ctx
, ®ulator_ww_class
);
231 /* Try to just grab both of them */
232 ret
= regulator_lock_nested(rdev1
, ww_ctx
);
234 ret
= regulator_lock_nested(rdev2
, ww_ctx
);
235 if (ret
!= -EDEADLOCK
) {
243 regulator_unlock(held
);
245 ww_mutex_lock_slow(&contended
->mutex
, ww_ctx
);
246 contended
->ref_cnt
++;
247 contended
->mutex_owner
= current
;
248 swap(held
, contended
);
249 ret
= regulator_lock_nested(contended
, ww_ctx
);
251 if (ret
!= -EDEADLOCK
) {
258 ww_acquire_done(ww_ctx
);
262 * regulator_unlock_two - unlock two regulators
263 * @rdev1: first regulator
264 * @rdev2: second regulator
265 * @ww_ctx: w/w mutex acquire context
267 * The inverse of regulator_lock_two().
270 static void regulator_unlock_two(struct regulator_dev
*rdev1
,
271 struct regulator_dev
*rdev2
,
272 struct ww_acquire_ctx
*ww_ctx
)
274 regulator_unlock(rdev2
);
275 regulator_unlock(rdev1
);
276 ww_acquire_fini(ww_ctx
);
279 static bool regulator_supply_is_couple(struct regulator_dev
*rdev
)
281 struct regulator_dev
*c_rdev
;
284 for (i
= 1; i
< rdev
->coupling_desc
.n_coupled
; i
++) {
285 c_rdev
= rdev
->coupling_desc
.coupled_rdevs
[i
];
287 if (rdev
->supply
->rdev
== c_rdev
)
294 static void regulator_unlock_recursive(struct regulator_dev
*rdev
,
295 unsigned int n_coupled
)
297 struct regulator_dev
*c_rdev
, *supply_rdev
;
298 int i
, supply_n_coupled
;
300 for (i
= n_coupled
; i
> 0; i
--) {
301 c_rdev
= rdev
->coupling_desc
.coupled_rdevs
[i
- 1];
306 if (c_rdev
->supply
&& !regulator_supply_is_couple(c_rdev
)) {
307 supply_rdev
= c_rdev
->supply
->rdev
;
308 supply_n_coupled
= supply_rdev
->coupling_desc
.n_coupled
;
310 regulator_unlock_recursive(supply_rdev
,
314 regulator_unlock(c_rdev
);
318 static int regulator_lock_recursive(struct regulator_dev
*rdev
,
319 struct regulator_dev
**new_contended_rdev
,
320 struct regulator_dev
**old_contended_rdev
,
321 struct ww_acquire_ctx
*ww_ctx
)
323 struct regulator_dev
*c_rdev
;
326 for (i
= 0; i
< rdev
->coupling_desc
.n_coupled
; i
++) {
327 c_rdev
= rdev
->coupling_desc
.coupled_rdevs
[i
];
332 if (c_rdev
!= *old_contended_rdev
) {
333 err
= regulator_lock_nested(c_rdev
, ww_ctx
);
335 if (err
== -EDEADLK
) {
336 *new_contended_rdev
= c_rdev
;
340 /* shouldn't happen */
341 WARN_ON_ONCE(err
!= -EALREADY
);
344 *old_contended_rdev
= NULL
;
347 if (c_rdev
->supply
&& !regulator_supply_is_couple(c_rdev
)) {
348 err
= regulator_lock_recursive(c_rdev
->supply
->rdev
,
353 regulator_unlock(c_rdev
);
362 regulator_unlock_recursive(rdev
, i
);
368 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
370 * @rdev: regulator source
371 * @ww_ctx: w/w mutex acquire context
373 * Unlock all regulators related with rdev by coupling or supplying.
375 static void regulator_unlock_dependent(struct regulator_dev
*rdev
,
376 struct ww_acquire_ctx
*ww_ctx
)
378 regulator_unlock_recursive(rdev
, rdev
->coupling_desc
.n_coupled
);
379 ww_acquire_fini(ww_ctx
);
383 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
384 * @rdev: regulator source
385 * @ww_ctx: w/w mutex acquire context
387 * This function as a wrapper on regulator_lock_recursive(), which locks
388 * all regulators related with rdev by coupling or supplying.
390 static void regulator_lock_dependent(struct regulator_dev
*rdev
,
391 struct ww_acquire_ctx
*ww_ctx
)
393 struct regulator_dev
*new_contended_rdev
= NULL
;
394 struct regulator_dev
*old_contended_rdev
= NULL
;
397 mutex_lock(®ulator_list_mutex
);
399 ww_acquire_init(ww_ctx
, ®ulator_ww_class
);
402 if (new_contended_rdev
) {
403 ww_mutex_lock_slow(&new_contended_rdev
->mutex
, ww_ctx
);
404 old_contended_rdev
= new_contended_rdev
;
405 old_contended_rdev
->ref_cnt
++;
406 old_contended_rdev
->mutex_owner
= current
;
409 err
= regulator_lock_recursive(rdev
,
414 if (old_contended_rdev
)
415 regulator_unlock(old_contended_rdev
);
417 } while (err
== -EDEADLK
);
419 ww_acquire_done(ww_ctx
);
421 mutex_unlock(®ulator_list_mutex
);
424 /* Platform voltage constraint check */
425 int regulator_check_voltage(struct regulator_dev
*rdev
,
426 int *min_uV
, int *max_uV
)
428 BUG_ON(*min_uV
> *max_uV
);
430 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
431 rdev_err(rdev
, "voltage operation not allowed\n");
435 if (*max_uV
> rdev
->constraints
->max_uV
)
436 *max_uV
= rdev
->constraints
->max_uV
;
437 if (*min_uV
< rdev
->constraints
->min_uV
)
438 *min_uV
= rdev
->constraints
->min_uV
;
440 if (*min_uV
> *max_uV
) {
441 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
449 /* return 0 if the state is valid */
450 static int regulator_check_states(suspend_state_t state
)
452 return (state
> PM_SUSPEND_MAX
|| state
== PM_SUSPEND_TO_IDLE
);
455 /* Make sure we select a voltage that suits the needs of all
456 * regulator consumers
458 int regulator_check_consumers(struct regulator_dev
*rdev
,
459 int *min_uV
, int *max_uV
,
460 suspend_state_t state
)
462 struct regulator
*regulator
;
463 struct regulator_voltage
*voltage
;
465 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
466 voltage
= ®ulator
->voltage
[state
];
468 * Assume consumers that didn't say anything are OK
469 * with anything in the constraint range.
471 if (!voltage
->min_uV
&& !voltage
->max_uV
)
474 if (*max_uV
> voltage
->max_uV
)
475 *max_uV
= voltage
->max_uV
;
476 if (*min_uV
< voltage
->min_uV
)
477 *min_uV
= voltage
->min_uV
;
480 if (*min_uV
> *max_uV
) {
481 rdev_err(rdev
, "Restricting voltage, %u-%uuV\n",
489 /* current constraint check */
490 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
491 int *min_uA
, int *max_uA
)
493 BUG_ON(*min_uA
> *max_uA
);
495 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_CURRENT
)) {
496 rdev_err(rdev
, "current operation not allowed\n");
500 if (*max_uA
> rdev
->constraints
->max_uA
)
501 *max_uA
= rdev
->constraints
->max_uA
;
502 if (*min_uA
< rdev
->constraints
->min_uA
)
503 *min_uA
= rdev
->constraints
->min_uA
;
505 if (*min_uA
> *max_uA
) {
506 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
514 /* operating mode constraint check */
515 static int regulator_mode_constrain(struct regulator_dev
*rdev
,
519 case REGULATOR_MODE_FAST
:
520 case REGULATOR_MODE_NORMAL
:
521 case REGULATOR_MODE_IDLE
:
522 case REGULATOR_MODE_STANDBY
:
525 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
529 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_MODE
)) {
530 rdev_err(rdev
, "mode operation not allowed\n");
534 /* The modes are bitmasks, the most power hungry modes having
535 * the lowest values. If the requested mode isn't supported
539 if (rdev
->constraints
->valid_modes_mask
& *mode
)
547 static inline struct regulator_state
*
548 regulator_get_suspend_state(struct regulator_dev
*rdev
, suspend_state_t state
)
550 if (rdev
->constraints
== NULL
)
554 case PM_SUSPEND_STANDBY
:
555 return &rdev
->constraints
->state_standby
;
557 return &rdev
->constraints
->state_mem
;
559 return &rdev
->constraints
->state_disk
;
565 static const struct regulator_state
*
566 regulator_get_suspend_state_check(struct regulator_dev
*rdev
, suspend_state_t state
)
568 const struct regulator_state
*rstate
;
570 rstate
= regulator_get_suspend_state(rdev
, state
);
574 /* If we have no suspend mode configuration don't set anything;
575 * only warn if the driver implements set_suspend_voltage or
576 * set_suspend_mode callback.
578 if (rstate
->enabled
!= ENABLE_IN_SUSPEND
&&
579 rstate
->enabled
!= DISABLE_IN_SUSPEND
) {
580 if (rdev
->desc
->ops
->set_suspend_voltage
||
581 rdev
->desc
->ops
->set_suspend_mode
)
582 rdev_warn(rdev
, "No configuration\n");
589 static ssize_t
microvolts_show(struct device
*dev
,
590 struct device_attribute
*attr
, char *buf
)
592 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
595 regulator_lock(rdev
);
596 uV
= regulator_get_voltage_rdev(rdev
);
597 regulator_unlock(rdev
);
601 return sprintf(buf
, "%d\n", uV
);
603 static DEVICE_ATTR_RO(microvolts
);
605 static ssize_t
microamps_show(struct device
*dev
,
606 struct device_attribute
*attr
, char *buf
)
608 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
610 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
612 static DEVICE_ATTR_RO(microamps
);
614 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
617 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
619 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
621 static DEVICE_ATTR_RO(name
);
623 static const char *regulator_opmode_to_str(int mode
)
626 case REGULATOR_MODE_FAST
:
628 case REGULATOR_MODE_NORMAL
:
630 case REGULATOR_MODE_IDLE
:
632 case REGULATOR_MODE_STANDBY
:
638 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
640 return sprintf(buf
, "%s\n", regulator_opmode_to_str(mode
));
643 static ssize_t
opmode_show(struct device
*dev
,
644 struct device_attribute
*attr
, char *buf
)
646 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
648 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
650 static DEVICE_ATTR_RO(opmode
);
652 static ssize_t
regulator_print_state(char *buf
, int state
)
655 return sprintf(buf
, "enabled\n");
657 return sprintf(buf
, "disabled\n");
659 return sprintf(buf
, "unknown\n");
662 static ssize_t
state_show(struct device
*dev
,
663 struct device_attribute
*attr
, char *buf
)
665 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
668 regulator_lock(rdev
);
669 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
670 regulator_unlock(rdev
);
674 static DEVICE_ATTR_RO(state
);
676 static ssize_t
status_show(struct device
*dev
,
677 struct device_attribute
*attr
, char *buf
)
679 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
683 status
= rdev
->desc
->ops
->get_status(rdev
);
688 case REGULATOR_STATUS_OFF
:
691 case REGULATOR_STATUS_ON
:
694 case REGULATOR_STATUS_ERROR
:
697 case REGULATOR_STATUS_FAST
:
700 case REGULATOR_STATUS_NORMAL
:
703 case REGULATOR_STATUS_IDLE
:
706 case REGULATOR_STATUS_STANDBY
:
709 case REGULATOR_STATUS_BYPASS
:
712 case REGULATOR_STATUS_UNDEFINED
:
719 return sprintf(buf
, "%s\n", label
);
721 static DEVICE_ATTR_RO(status
);
723 static ssize_t
min_microamps_show(struct device
*dev
,
724 struct device_attribute
*attr
, char *buf
)
726 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
728 if (!rdev
->constraints
)
729 return sprintf(buf
, "constraint not defined\n");
731 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
733 static DEVICE_ATTR_RO(min_microamps
);
735 static ssize_t
max_microamps_show(struct device
*dev
,
736 struct device_attribute
*attr
, char *buf
)
738 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
740 if (!rdev
->constraints
)
741 return sprintf(buf
, "constraint not defined\n");
743 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
745 static DEVICE_ATTR_RO(max_microamps
);
747 static ssize_t
min_microvolts_show(struct device
*dev
,
748 struct device_attribute
*attr
, char *buf
)
750 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
752 if (!rdev
->constraints
)
753 return sprintf(buf
, "constraint not defined\n");
755 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
757 static DEVICE_ATTR_RO(min_microvolts
);
759 static ssize_t
max_microvolts_show(struct device
*dev
,
760 struct device_attribute
*attr
, char *buf
)
762 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
764 if (!rdev
->constraints
)
765 return sprintf(buf
, "constraint not defined\n");
767 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
769 static DEVICE_ATTR_RO(max_microvolts
);
771 static ssize_t
requested_microamps_show(struct device
*dev
,
772 struct device_attribute
*attr
, char *buf
)
774 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
775 struct regulator
*regulator
;
778 regulator_lock(rdev
);
779 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
780 if (regulator
->enable_count
)
781 uA
+= regulator
->uA_load
;
783 regulator_unlock(rdev
);
784 return sprintf(buf
, "%d\n", uA
);
786 static DEVICE_ATTR_RO(requested_microamps
);
788 static ssize_t
num_users_show(struct device
*dev
, struct device_attribute
*attr
,
791 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
792 return sprintf(buf
, "%d\n", rdev
->use_count
);
794 static DEVICE_ATTR_RO(num_users
);
796 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
799 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
801 switch (rdev
->desc
->type
) {
802 case REGULATOR_VOLTAGE
:
803 return sprintf(buf
, "voltage\n");
804 case REGULATOR_CURRENT
:
805 return sprintf(buf
, "current\n");
807 return sprintf(buf
, "unknown\n");
809 static DEVICE_ATTR_RO(type
);
811 static ssize_t
suspend_mem_microvolts_show(struct device
*dev
,
812 struct device_attribute
*attr
, char *buf
)
814 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
816 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
818 static DEVICE_ATTR_RO(suspend_mem_microvolts
);
820 static ssize_t
suspend_disk_microvolts_show(struct device
*dev
,
821 struct device_attribute
*attr
, char *buf
)
823 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
825 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
827 static DEVICE_ATTR_RO(suspend_disk_microvolts
);
829 static ssize_t
suspend_standby_microvolts_show(struct device
*dev
,
830 struct device_attribute
*attr
, char *buf
)
832 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
834 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
836 static DEVICE_ATTR_RO(suspend_standby_microvolts
);
838 static ssize_t
suspend_mem_mode_show(struct device
*dev
,
839 struct device_attribute
*attr
, char *buf
)
841 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
843 return regulator_print_opmode(buf
,
844 rdev
->constraints
->state_mem
.mode
);
846 static DEVICE_ATTR_RO(suspend_mem_mode
);
848 static ssize_t
suspend_disk_mode_show(struct device
*dev
,
849 struct device_attribute
*attr
, char *buf
)
851 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
853 return regulator_print_opmode(buf
,
854 rdev
->constraints
->state_disk
.mode
);
856 static DEVICE_ATTR_RO(suspend_disk_mode
);
858 static ssize_t
suspend_standby_mode_show(struct device
*dev
,
859 struct device_attribute
*attr
, char *buf
)
861 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
863 return regulator_print_opmode(buf
,
864 rdev
->constraints
->state_standby
.mode
);
866 static DEVICE_ATTR_RO(suspend_standby_mode
);
868 static ssize_t
suspend_mem_state_show(struct device
*dev
,
869 struct device_attribute
*attr
, char *buf
)
871 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
873 return regulator_print_state(buf
,
874 rdev
->constraints
->state_mem
.enabled
);
876 static DEVICE_ATTR_RO(suspend_mem_state
);
878 static ssize_t
suspend_disk_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_disk
.enabled
);
886 static DEVICE_ATTR_RO(suspend_disk_state
);
888 static ssize_t
suspend_standby_state_show(struct device
*dev
,
889 struct device_attribute
*attr
, char *buf
)
891 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
893 return regulator_print_state(buf
,
894 rdev
->constraints
->state_standby
.enabled
);
896 static DEVICE_ATTR_RO(suspend_standby_state
);
898 static ssize_t
bypass_show(struct device
*dev
,
899 struct device_attribute
*attr
, char *buf
)
901 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
906 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypass
);
915 return sprintf(buf
, "%s\n", report
);
917 static DEVICE_ATTR_RO(bypass
);
919 #define REGULATOR_ERROR_ATTR(name, bit) \
920 static ssize_t name##_show(struct device *dev, struct device_attribute *attr, \
924 unsigned int flags; \
925 struct regulator_dev *rdev = dev_get_drvdata(dev); \
926 ret = _regulator_get_error_flags(rdev, &flags); \
929 return sysfs_emit(buf, "%d\n", !!(flags & (bit))); \
931 static DEVICE_ATTR_RO(name)
933 REGULATOR_ERROR_ATTR(under_voltage
, REGULATOR_ERROR_UNDER_VOLTAGE
);
934 REGULATOR_ERROR_ATTR(over_current
, REGULATOR_ERROR_OVER_CURRENT
);
935 REGULATOR_ERROR_ATTR(regulation_out
, REGULATOR_ERROR_REGULATION_OUT
);
936 REGULATOR_ERROR_ATTR(fail
, REGULATOR_ERROR_FAIL
);
937 REGULATOR_ERROR_ATTR(over_temp
, REGULATOR_ERROR_OVER_TEMP
);
938 REGULATOR_ERROR_ATTR(under_voltage_warn
, REGULATOR_ERROR_UNDER_VOLTAGE_WARN
);
939 REGULATOR_ERROR_ATTR(over_current_warn
, REGULATOR_ERROR_OVER_CURRENT_WARN
);
940 REGULATOR_ERROR_ATTR(over_voltage_warn
, REGULATOR_ERROR_OVER_VOLTAGE_WARN
);
941 REGULATOR_ERROR_ATTR(over_temp_warn
, REGULATOR_ERROR_OVER_TEMP_WARN
);
943 /* Calculate the new optimum regulator operating mode based on the new total
944 * consumer load. All locks held by caller
946 static int drms_uA_update(struct regulator_dev
*rdev
)
948 struct regulator
*sibling
;
949 int current_uA
= 0, output_uV
, input_uV
, err
;
953 * first check to see if we can set modes at all, otherwise just
954 * tell the consumer everything is OK.
956 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_DRMS
)) {
957 rdev_dbg(rdev
, "DRMS operation not allowed\n");
961 if (!rdev
->desc
->ops
->get_optimum_mode
&&
962 !rdev
->desc
->ops
->set_load
)
965 if (!rdev
->desc
->ops
->set_mode
&&
966 !rdev
->desc
->ops
->set_load
)
969 /* calc total requested load */
970 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
) {
971 if (sibling
->enable_count
)
972 current_uA
+= sibling
->uA_load
;
975 current_uA
+= rdev
->constraints
->system_load
;
977 if (rdev
->desc
->ops
->set_load
) {
978 /* set the optimum mode for our new total regulator load */
979 err
= rdev
->desc
->ops
->set_load(rdev
, current_uA
);
981 rdev_err(rdev
, "failed to set load %d: %pe\n",
982 current_uA
, ERR_PTR(err
));
985 * Unfortunately in some cases the constraints->valid_ops has
986 * REGULATOR_CHANGE_DRMS but there are no valid modes listed.
987 * That's not really legit but we won't consider it a fatal
988 * error here. We'll treat it as if REGULATOR_CHANGE_DRMS
991 if (!rdev
->constraints
->valid_modes_mask
) {
992 rdev_dbg(rdev
, "Can change modes; but no valid mode\n");
996 /* get output voltage */
997 output_uV
= regulator_get_voltage_rdev(rdev
);
1000 * Don't return an error; if regulator driver cares about
1001 * output_uV then it's up to the driver to validate.
1004 rdev_dbg(rdev
, "invalid output voltage found\n");
1006 /* get input voltage */
1009 input_uV
= regulator_get_voltage_rdev(rdev
->supply
->rdev
);
1011 input_uV
= rdev
->constraints
->input_uV
;
1014 * Don't return an error; if regulator driver cares about
1015 * input_uV then it's up to the driver to validate.
1018 rdev_dbg(rdev
, "invalid input voltage found\n");
1020 /* now get the optimum mode for our new total regulator load */
1021 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
1022 output_uV
, current_uA
);
1024 /* check the new mode is allowed */
1025 err
= regulator_mode_constrain(rdev
, &mode
);
1027 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n",
1028 current_uA
, input_uV
, output_uV
, ERR_PTR(err
));
1032 err
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1034 rdev_err(rdev
, "failed to set optimum mode %x: %pe\n",
1035 mode
, ERR_PTR(err
));
1041 static int __suspend_set_state(struct regulator_dev
*rdev
,
1042 const struct regulator_state
*rstate
)
1046 if (rstate
->enabled
== ENABLE_IN_SUSPEND
&&
1047 rdev
->desc
->ops
->set_suspend_enable
)
1048 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
1049 else if (rstate
->enabled
== DISABLE_IN_SUSPEND
&&
1050 rdev
->desc
->ops
->set_suspend_disable
)
1051 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
1052 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1056 rdev_err(rdev
, "failed to enabled/disable: %pe\n", ERR_PTR(ret
));
1060 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
1061 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
1063 rdev_err(rdev
, "failed to set voltage: %pe\n", ERR_PTR(ret
));
1068 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
1069 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
1071 rdev_err(rdev
, "failed to set mode: %pe\n", ERR_PTR(ret
));
1079 static int suspend_set_initial_state(struct regulator_dev
*rdev
)
1081 const struct regulator_state
*rstate
;
1083 rstate
= regulator_get_suspend_state_check(rdev
,
1084 rdev
->constraints
->initial_state
);
1088 return __suspend_set_state(rdev
, rstate
);
1091 #if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG)
1092 static void print_constraints_debug(struct regulator_dev
*rdev
)
1094 struct regulation_constraints
*constraints
= rdev
->constraints
;
1096 size_t len
= sizeof(buf
) - 1;
1100 if (constraints
->min_uV
&& constraints
->max_uV
) {
1101 if (constraints
->min_uV
== constraints
->max_uV
)
1102 count
+= scnprintf(buf
+ count
, len
- count
, "%d mV ",
1103 constraints
->min_uV
/ 1000);
1105 count
+= scnprintf(buf
+ count
, len
- count
,
1107 constraints
->min_uV
/ 1000,
1108 constraints
->max_uV
/ 1000);
1111 if (!constraints
->min_uV
||
1112 constraints
->min_uV
!= constraints
->max_uV
) {
1113 ret
= regulator_get_voltage_rdev(rdev
);
1115 count
+= scnprintf(buf
+ count
, len
- count
,
1116 "at %d mV ", ret
/ 1000);
1119 if (constraints
->uV_offset
)
1120 count
+= scnprintf(buf
+ count
, len
- count
, "%dmV offset ",
1121 constraints
->uV_offset
/ 1000);
1123 if (constraints
->min_uA
&& constraints
->max_uA
) {
1124 if (constraints
->min_uA
== constraints
->max_uA
)
1125 count
+= scnprintf(buf
+ count
, len
- count
, "%d mA ",
1126 constraints
->min_uA
/ 1000);
1128 count
+= scnprintf(buf
+ count
, len
- count
,
1130 constraints
->min_uA
/ 1000,
1131 constraints
->max_uA
/ 1000);
1134 if (!constraints
->min_uA
||
1135 constraints
->min_uA
!= constraints
->max_uA
) {
1136 ret
= _regulator_get_current_limit(rdev
);
1138 count
+= scnprintf(buf
+ count
, len
- count
,
1139 "at %d mA ", ret
/ 1000);
1142 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
1143 count
+= scnprintf(buf
+ count
, len
- count
, "fast ");
1144 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
1145 count
+= scnprintf(buf
+ count
, len
- count
, "normal ");
1146 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
1147 count
+= scnprintf(buf
+ count
, len
- count
, "idle ");
1148 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
1149 count
+= scnprintf(buf
+ count
, len
- count
, "standby ");
1152 count
= scnprintf(buf
, len
, "no parameters");
1156 count
+= scnprintf(buf
+ count
, len
- count
, ", %s",
1157 _regulator_is_enabled(rdev
) ? "enabled" : "disabled");
1159 rdev_dbg(rdev
, "%s\n", buf
);
1161 #else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1162 static inline void print_constraints_debug(struct regulator_dev
*rdev
) {}
1163 #endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */
1165 static void print_constraints(struct regulator_dev
*rdev
)
1167 struct regulation_constraints
*constraints
= rdev
->constraints
;
1169 print_constraints_debug(rdev
);
1171 if ((constraints
->min_uV
!= constraints
->max_uV
) &&
1172 !regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
))
1174 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1177 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
1178 struct regulation_constraints
*constraints
)
1180 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1183 /* do we need to apply the constraint voltage */
1184 if (rdev
->constraints
->apply_uV
&&
1185 rdev
->constraints
->min_uV
&& rdev
->constraints
->max_uV
) {
1186 int target_min
, target_max
;
1187 int current_uV
= regulator_get_voltage_rdev(rdev
);
1189 if (current_uV
== -ENOTRECOVERABLE
) {
1190 /* This regulator can't be read and must be initialized */
1191 rdev_info(rdev
, "Setting %d-%duV\n",
1192 rdev
->constraints
->min_uV
,
1193 rdev
->constraints
->max_uV
);
1194 _regulator_do_set_voltage(rdev
,
1195 rdev
->constraints
->min_uV
,
1196 rdev
->constraints
->max_uV
);
1197 current_uV
= regulator_get_voltage_rdev(rdev
);
1200 if (current_uV
< 0) {
1201 if (current_uV
!= -EPROBE_DEFER
)
1203 "failed to get the current voltage: %pe\n",
1204 ERR_PTR(current_uV
));
1209 * If we're below the minimum voltage move up to the
1210 * minimum voltage, if we're above the maximum voltage
1211 * then move down to the maximum.
1213 target_min
= current_uV
;
1214 target_max
= current_uV
;
1216 if (current_uV
< rdev
->constraints
->min_uV
) {
1217 target_min
= rdev
->constraints
->min_uV
;
1218 target_max
= rdev
->constraints
->min_uV
;
1221 if (current_uV
> rdev
->constraints
->max_uV
) {
1222 target_min
= rdev
->constraints
->max_uV
;
1223 target_max
= rdev
->constraints
->max_uV
;
1226 if (target_min
!= current_uV
|| target_max
!= current_uV
) {
1227 rdev_info(rdev
, "Bringing %duV into %d-%duV\n",
1228 current_uV
, target_min
, target_max
);
1229 ret
= _regulator_do_set_voltage(
1230 rdev
, target_min
, target_max
);
1233 "failed to apply %d-%duV constraint: %pe\n",
1234 target_min
, target_max
, ERR_PTR(ret
));
1240 /* constrain machine-level voltage specs to fit
1241 * the actual range supported by this regulator.
1243 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
1244 int count
= rdev
->desc
->n_voltages
;
1246 int min_uV
= INT_MAX
;
1247 int max_uV
= INT_MIN
;
1248 int cmin
= constraints
->min_uV
;
1249 int cmax
= constraints
->max_uV
;
1251 /* it's safe to autoconfigure fixed-voltage supplies
1252 * and the constraints are used by list_voltage.
1254 if (count
== 1 && !cmin
) {
1257 constraints
->min_uV
= cmin
;
1258 constraints
->max_uV
= cmax
;
1261 /* voltage constraints are optional */
1262 if ((cmin
== 0) && (cmax
== 0))
1265 /* else require explicit machine-level constraints */
1266 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
1267 rdev_err(rdev
, "invalid voltage constraints\n");
1271 /* no need to loop voltages if range is continuous */
1272 if (rdev
->desc
->continuous_voltage_range
)
1275 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1276 for (i
= 0; i
< count
; i
++) {
1279 value
= ops
->list_voltage(rdev
, i
);
1283 /* maybe adjust [min_uV..max_uV] */
1284 if (value
>= cmin
&& value
< min_uV
)
1286 if (value
<= cmax
&& value
> max_uV
)
1290 /* final: [min_uV..max_uV] valid iff constraints valid */
1291 if (max_uV
< min_uV
) {
1293 "unsupportable voltage constraints %u-%uuV\n",
1298 /* use regulator's subset of machine constraints */
1299 if (constraints
->min_uV
< min_uV
) {
1300 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
1301 constraints
->min_uV
, min_uV
);
1302 constraints
->min_uV
= min_uV
;
1304 if (constraints
->max_uV
> max_uV
) {
1305 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
1306 constraints
->max_uV
, max_uV
);
1307 constraints
->max_uV
= max_uV
;
1314 static int machine_constraints_current(struct regulator_dev
*rdev
,
1315 struct regulation_constraints
*constraints
)
1317 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1320 if (!constraints
->min_uA
&& !constraints
->max_uA
)
1323 if (constraints
->min_uA
> constraints
->max_uA
) {
1324 rdev_err(rdev
, "Invalid current constraints\n");
1328 if (!ops
->set_current_limit
|| !ops
->get_current_limit
) {
1329 rdev_warn(rdev
, "Operation of current configuration missing\n");
1333 /* Set regulator current in constraints range */
1334 ret
= ops
->set_current_limit(rdev
, constraints
->min_uA
,
1335 constraints
->max_uA
);
1337 rdev_err(rdev
, "Failed to set current constraint, %d\n", ret
);
1344 static int _regulator_do_enable(struct regulator_dev
*rdev
);
1346 static int notif_set_limit(struct regulator_dev
*rdev
,
1347 int (*set
)(struct regulator_dev
*, int, int, bool),
1348 int limit
, int severity
)
1352 if (limit
== REGULATOR_NOTIF_LIMIT_DISABLE
) {
1359 if (limit
== REGULATOR_NOTIF_LIMIT_ENABLE
)
1362 return set(rdev
, limit
, severity
, enable
);
1365 static int handle_notify_limits(struct regulator_dev
*rdev
,
1366 int (*set
)(struct regulator_dev
*, int, int, bool),
1367 struct notification_limit
*limits
)
1375 ret
= notif_set_limit(rdev
, set
, limits
->prot
,
1376 REGULATOR_SEVERITY_PROT
);
1381 ret
= notif_set_limit(rdev
, set
, limits
->err
,
1382 REGULATOR_SEVERITY_ERR
);
1387 ret
= notif_set_limit(rdev
, set
, limits
->warn
,
1388 REGULATOR_SEVERITY_WARN
);
1393 * set_machine_constraints - sets regulator constraints
1394 * @rdev: regulator source
1396 * Allows platform initialisation code to define and constrain
1397 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1398 * Constraints *must* be set by platform code in order for some
1399 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1402 * Return: 0 on success or a negative error number on failure.
1404 static int set_machine_constraints(struct regulator_dev
*rdev
)
1407 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1409 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
1413 ret
= machine_constraints_current(rdev
, rdev
->constraints
);
1417 if (rdev
->constraints
->ilim_uA
&& ops
->set_input_current_limit
) {
1418 ret
= ops
->set_input_current_limit(rdev
,
1419 rdev
->constraints
->ilim_uA
);
1421 rdev_err(rdev
, "failed to set input limit: %pe\n", ERR_PTR(ret
));
1426 /* do we need to setup our suspend state */
1427 if (rdev
->constraints
->initial_state
) {
1428 ret
= suspend_set_initial_state(rdev
);
1430 rdev_err(rdev
, "failed to set suspend state: %pe\n", ERR_PTR(ret
));
1435 if (rdev
->constraints
->initial_mode
) {
1436 if (!ops
->set_mode
) {
1437 rdev_err(rdev
, "no set_mode operation\n");
1441 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
1443 rdev_err(rdev
, "failed to set initial mode: %pe\n", ERR_PTR(ret
));
1446 } else if (rdev
->constraints
->system_load
) {
1448 * We'll only apply the initial system load if an
1449 * initial mode wasn't specified.
1451 drms_uA_update(rdev
);
1454 if ((rdev
->constraints
->ramp_delay
|| rdev
->constraints
->ramp_disable
)
1455 && ops
->set_ramp_delay
) {
1456 ret
= ops
->set_ramp_delay(rdev
, rdev
->constraints
->ramp_delay
);
1458 rdev_err(rdev
, "failed to set ramp_delay: %pe\n", ERR_PTR(ret
));
1463 if (rdev
->constraints
->pull_down
&& ops
->set_pull_down
) {
1464 ret
= ops
->set_pull_down(rdev
);
1466 rdev_err(rdev
, "failed to set pull down: %pe\n", ERR_PTR(ret
));
1471 if (rdev
->constraints
->soft_start
&& ops
->set_soft_start
) {
1472 ret
= ops
->set_soft_start(rdev
);
1474 rdev_err(rdev
, "failed to set soft start: %pe\n", ERR_PTR(ret
));
1480 * Existing logic does not warn if over_current_protection is given as
1481 * a constraint but driver does not support that. I think we should
1482 * warn about this type of issues as it is possible someone changes
1483 * PMIC on board to another type - and the another PMIC's driver does
1484 * not support setting protection. Board composer may happily believe
1485 * the DT limits are respected - especially if the new PMIC HW also
1486 * supports protection but the driver does not. I won't change the logic
1487 * without hearing more experienced opinion on this though.
1489 * If warning is seen as a good idea then we can merge handling the
1490 * over-curret protection and detection and get rid of this special
1493 if (rdev
->constraints
->over_current_protection
1494 && ops
->set_over_current_protection
) {
1495 int lim
= rdev
->constraints
->over_curr_limits
.prot
;
1497 ret
= ops
->set_over_current_protection(rdev
, lim
,
1498 REGULATOR_SEVERITY_PROT
,
1501 rdev_err(rdev
, "failed to set over current protection: %pe\n",
1507 if (rdev
->constraints
->over_current_detection
)
1508 ret
= handle_notify_limits(rdev
,
1509 ops
->set_over_current_protection
,
1510 &rdev
->constraints
->over_curr_limits
);
1512 if (ret
!= -EOPNOTSUPP
) {
1513 rdev_err(rdev
, "failed to set over current limits: %pe\n",
1518 "IC does not support requested over-current limits\n");
1521 if (rdev
->constraints
->over_voltage_detection
)
1522 ret
= handle_notify_limits(rdev
,
1523 ops
->set_over_voltage_protection
,
1524 &rdev
->constraints
->over_voltage_limits
);
1526 if (ret
!= -EOPNOTSUPP
) {
1527 rdev_err(rdev
, "failed to set over voltage limits %pe\n",
1532 "IC does not support requested over voltage limits\n");
1535 if (rdev
->constraints
->under_voltage_detection
)
1536 ret
= handle_notify_limits(rdev
,
1537 ops
->set_under_voltage_protection
,
1538 &rdev
->constraints
->under_voltage_limits
);
1540 if (ret
!= -EOPNOTSUPP
) {
1541 rdev_err(rdev
, "failed to set under voltage limits %pe\n",
1546 "IC does not support requested under voltage limits\n");
1549 if (rdev
->constraints
->over_temp_detection
)
1550 ret
= handle_notify_limits(rdev
,
1551 ops
->set_thermal_protection
,
1552 &rdev
->constraints
->temp_limits
);
1554 if (ret
!= -EOPNOTSUPP
) {
1555 rdev_err(rdev
, "failed to set temperature limits %pe\n",
1560 "IC does not support requested temperature limits\n");
1563 if (rdev
->constraints
->active_discharge
&& ops
->set_active_discharge
) {
1564 bool ad_state
= (rdev
->constraints
->active_discharge
==
1565 REGULATOR_ACTIVE_DISCHARGE_ENABLE
) ? true : false;
1567 ret
= ops
->set_active_discharge(rdev
, ad_state
);
1569 rdev_err(rdev
, "failed to set active discharge: %pe\n", ERR_PTR(ret
));
1575 * If there is no mechanism for controlling the regulator then
1576 * flag it as always_on so we don't end up duplicating checks
1577 * for this so much. Note that we could control the state of
1578 * a supply to control the output on a regulator that has no
1581 if (!rdev
->ena_pin
&& !ops
->enable
) {
1582 if (rdev
->supply_name
&& !rdev
->supply
)
1583 return -EPROBE_DEFER
;
1586 rdev
->constraints
->always_on
=
1587 rdev
->supply
->rdev
->constraints
->always_on
;
1589 rdev
->constraints
->always_on
= true;
1592 /* If the constraints say the regulator should be on at this point
1593 * and we have control then make sure it is enabled.
1595 if (rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) {
1596 /* If we want to enable this regulator, make sure that we know
1597 * the supplying regulator.
1599 if (rdev
->supply_name
&& !rdev
->supply
)
1600 return -EPROBE_DEFER
;
1602 /* If supplying regulator has already been enabled,
1603 * it's not intended to have use_count increment
1604 * when rdev is only boot-on.
1607 (rdev
->constraints
->always_on
||
1608 !regulator_is_enabled(rdev
->supply
))) {
1609 ret
= regulator_enable(rdev
->supply
);
1611 _regulator_put(rdev
->supply
);
1612 rdev
->supply
= NULL
;
1617 ret
= _regulator_do_enable(rdev
);
1618 if (ret
< 0 && ret
!= -EINVAL
) {
1619 rdev_err(rdev
, "failed to enable: %pe\n", ERR_PTR(ret
));
1623 if (rdev
->constraints
->always_on
)
1625 } else if (rdev
->desc
->off_on_delay
) {
1626 rdev
->last_off
= ktime_get();
1629 print_constraints(rdev
);
1634 * set_supply - set regulator supply regulator
1635 * @rdev: regulator (locked)
1636 * @supply_rdev: supply regulator (locked))
1638 * Called by platform initialisation code to set the supply regulator for this
1639 * regulator. This ensures that a regulators supply will also be enabled by the
1640 * core if it's child is enabled.
1642 * Return: 0 on success or a negative error number on failure.
1644 static int set_supply(struct regulator_dev
*rdev
,
1645 struct regulator_dev
*supply_rdev
)
1649 rdev_dbg(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
1651 if (!try_module_get(supply_rdev
->owner
))
1654 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
1655 if (rdev
->supply
== NULL
) {
1656 module_put(supply_rdev
->owner
);
1660 supply_rdev
->open_count
++;
1666 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1667 * @rdev: regulator source
1668 * @consumer_dev_name: dev_name() string for device supply applies to
1669 * @supply: symbolic name for supply
1671 * Allows platform initialisation code to map physical regulator
1672 * sources to symbolic names for supplies for use by devices. Devices
1673 * should use these symbolic names to request regulators, avoiding the
1674 * need to provide board-specific regulator names as platform data.
1676 * Return: 0 on success or a negative error number on failure.
1678 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1679 const char *consumer_dev_name
,
1682 struct regulator_map
*node
, *new_node
;
1688 if (consumer_dev_name
!= NULL
)
1693 new_node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1694 if (new_node
== NULL
)
1697 new_node
->regulator
= rdev
;
1698 new_node
->supply
= supply
;
1701 new_node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1702 if (new_node
->dev_name
== NULL
) {
1708 mutex_lock(®ulator_list_mutex
);
1709 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1710 if (node
->dev_name
&& consumer_dev_name
) {
1711 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1713 } else if (node
->dev_name
|| consumer_dev_name
) {
1717 if (strcmp(node
->supply
, supply
) != 0)
1720 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1722 dev_name(&node
->regulator
->dev
),
1723 node
->regulator
->desc
->name
,
1725 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1729 list_add(&new_node
->list
, ®ulator_map_list
);
1730 mutex_unlock(®ulator_list_mutex
);
1735 mutex_unlock(®ulator_list_mutex
);
1736 kfree(new_node
->dev_name
);
1741 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1743 struct regulator_map
*node
, *n
;
1745 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1746 if (rdev
== node
->regulator
) {
1747 list_del(&node
->list
);
1748 kfree(node
->dev_name
);
1754 #ifdef CONFIG_DEBUG_FS
1755 static ssize_t
constraint_flags_read_file(struct file
*file
,
1756 char __user
*user_buf
,
1757 size_t count
, loff_t
*ppos
)
1759 const struct regulator
*regulator
= file
->private_data
;
1760 const struct regulation_constraints
*c
= regulator
->rdev
->constraints
;
1767 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1771 ret
= snprintf(buf
, PAGE_SIZE
,
1775 "ramp_disable: %u\n"
1778 "over_current_protection: %u\n",
1785 c
->over_current_protection
);
1787 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1795 static const struct file_operations constraint_flags_fops
= {
1796 #ifdef CONFIG_DEBUG_FS
1797 .open
= simple_open
,
1798 .read
= constraint_flags_read_file
,
1799 .llseek
= default_llseek
,
1803 #define REG_STR_SIZE 64
1805 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1807 const char *supply_name
)
1809 struct regulator
*regulator
;
1812 lockdep_assert_held_once(&rdev
->mutex
.base
);
1815 char buf
[REG_STR_SIZE
];
1818 size
= snprintf(buf
, REG_STR_SIZE
, "%s-%s",
1819 dev
->kobj
.name
, supply_name
);
1820 if (size
>= REG_STR_SIZE
)
1823 supply_name
= kstrdup(buf
, GFP_KERNEL
);
1824 if (supply_name
== NULL
)
1827 supply_name
= kstrdup_const(supply_name
, GFP_KERNEL
);
1828 if (supply_name
== NULL
)
1832 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1833 if (regulator
== NULL
) {
1834 kfree_const(supply_name
);
1838 regulator
->rdev
= rdev
;
1839 regulator
->supply_name
= supply_name
;
1841 list_add(®ulator
->list
, &rdev
->consumer_list
);
1844 regulator
->dev
= dev
;
1846 /* Add a link to the device sysfs entry */
1847 err
= sysfs_create_link_nowarn(&rdev
->dev
.kobj
, &dev
->kobj
,
1850 rdev_dbg(rdev
, "could not add device link %s: %pe\n",
1851 dev
->kobj
.name
, ERR_PTR(err
));
1856 if (err
!= -EEXIST
) {
1857 regulator
->debugfs
= debugfs_create_dir(supply_name
, rdev
->debugfs
);
1858 if (IS_ERR(regulator
->debugfs
)) {
1859 rdev_dbg(rdev
, "Failed to create debugfs directory\n");
1860 regulator
->debugfs
= NULL
;
1864 if (regulator
->debugfs
) {
1865 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1866 ®ulator
->uA_load
);
1867 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1868 ®ulator
->voltage
[PM_SUSPEND_ON
].min_uV
);
1869 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1870 ®ulator
->voltage
[PM_SUSPEND_ON
].max_uV
);
1871 debugfs_create_file("constraint_flags", 0444, regulator
->debugfs
,
1872 regulator
, &constraint_flags_fops
);
1876 * Check now if the regulator is an always on regulator - if
1877 * it is then we don't need to do nearly so much work for
1878 * enable/disable calls.
1880 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
) &&
1881 _regulator_is_enabled(rdev
))
1882 regulator
->always_on
= true;
1887 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1889 if (rdev
->constraints
&& rdev
->constraints
->enable_time
)
1890 return rdev
->constraints
->enable_time
;
1891 if (rdev
->desc
->ops
->enable_time
)
1892 return rdev
->desc
->ops
->enable_time(rdev
);
1893 return rdev
->desc
->enable_time
;
1896 static struct regulator_supply_alias
*regulator_find_supply_alias(
1897 struct device
*dev
, const char *supply
)
1899 struct regulator_supply_alias
*map
;
1901 list_for_each_entry(map
, ®ulator_supply_alias_list
, list
)
1902 if (map
->src_dev
== dev
&& strcmp(map
->src_supply
, supply
) == 0)
1908 static void regulator_supply_alias(struct device
**dev
, const char **supply
)
1910 struct regulator_supply_alias
*map
;
1912 map
= regulator_find_supply_alias(*dev
, *supply
);
1914 dev_dbg(*dev
, "Mapping supply %s to %s,%s\n",
1915 *supply
, map
->alias_supply
,
1916 dev_name(map
->alias_dev
));
1917 *dev
= map
->alias_dev
;
1918 *supply
= map
->alias_supply
;
1922 static int regulator_match(struct device
*dev
, const void *data
)
1924 struct regulator_dev
*r
= dev_to_rdev(dev
);
1926 return strcmp(rdev_get_name(r
), data
) == 0;
1929 static struct regulator_dev
*regulator_lookup_by_name(const char *name
)
1933 dev
= class_find_device(®ulator_class
, NULL
, name
, regulator_match
);
1935 return dev
? dev_to_rdev(dev
) : NULL
;
1939 * regulator_dev_lookup - lookup a regulator device.
1940 * @dev: device for regulator "consumer".
1941 * @supply: Supply name or regulator ID.
1943 * Return: pointer to &struct regulator_dev or ERR_PTR() encoded negative error number.
1945 * If successful, returns a struct regulator_dev that corresponds to the name
1946 * @supply and with the embedded struct device refcount incremented by one.
1947 * The refcount must be dropped by calling put_device().
1948 * On failure one of the following ERR_PTR() encoded values is returned:
1949 * -%ENODEV if lookup fails permanently, -%EPROBE_DEFER if lookup could succeed
1952 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1955 struct regulator_dev
*r
= NULL
;
1956 struct regulator_map
*map
;
1957 const char *devname
= NULL
;
1959 regulator_supply_alias(&dev
, &supply
);
1961 /* first do a dt based lookup */
1962 if (dev
&& dev
->of_node
) {
1963 r
= of_regulator_dev_lookup(dev
, supply
);
1966 if (PTR_ERR(r
) == -EPROBE_DEFER
)
1969 if (PTR_ERR(r
) == -ENODEV
)
1973 /* if not found, try doing it non-dt way */
1975 devname
= dev_name(dev
);
1977 mutex_lock(®ulator_list_mutex
);
1978 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1979 /* If the mapping has a device set up it must match */
1980 if (map
->dev_name
&&
1981 (!devname
|| strcmp(map
->dev_name
, devname
)))
1984 if (strcmp(map
->supply
, supply
) == 0 &&
1985 get_device(&map
->regulator
->dev
)) {
1990 mutex_unlock(®ulator_list_mutex
);
1995 r
= regulator_lookup_by_name(supply
);
1999 return ERR_PTR(-ENODEV
);
2002 static int regulator_resolve_supply(struct regulator_dev
*rdev
)
2004 struct regulator_dev
*r
;
2005 struct device
*dev
= rdev
->dev
.parent
;
2006 struct ww_acquire_ctx ww_ctx
;
2009 /* No supply to resolve? */
2010 if (!rdev
->supply_name
)
2013 /* Supply already resolved? (fast-path without locking contention) */
2017 r
= regulator_dev_lookup(dev
, rdev
->supply_name
);
2021 /* Did the lookup explicitly defer for us? */
2022 if (ret
== -EPROBE_DEFER
)
2025 if (have_full_constraints()) {
2026 r
= dummy_regulator_rdev
;
2027 get_device(&r
->dev
);
2029 dev_err(dev
, "Failed to resolve %s-supply for %s\n",
2030 rdev
->supply_name
, rdev
->desc
->name
);
2031 ret
= -EPROBE_DEFER
;
2037 dev_err(dev
, "Supply for %s (%s) resolved to itself\n",
2038 rdev
->desc
->name
, rdev
->supply_name
);
2039 if (!have_full_constraints()) {
2043 r
= dummy_regulator_rdev
;
2044 get_device(&r
->dev
);
2048 * If the supply's parent device is not the same as the
2049 * regulator's parent device, then ensure the parent device
2050 * is bound before we resolve the supply, in case the parent
2051 * device get probe deferred and unregisters the supply.
2053 if (r
->dev
.parent
&& r
->dev
.parent
!= rdev
->dev
.parent
) {
2054 if (!device_is_bound(r
->dev
.parent
)) {
2055 put_device(&r
->dev
);
2056 ret
= -EPROBE_DEFER
;
2061 /* Recursively resolve the supply of the supply */
2062 ret
= regulator_resolve_supply(r
);
2064 put_device(&r
->dev
);
2069 * Recheck rdev->supply with rdev->mutex lock held to avoid a race
2070 * between rdev->supply null check and setting rdev->supply in
2071 * set_supply() from concurrent tasks.
2073 regulator_lock_two(rdev
, r
, &ww_ctx
);
2075 /* Supply just resolved by a concurrent task? */
2077 regulator_unlock_two(rdev
, r
, &ww_ctx
);
2078 put_device(&r
->dev
);
2082 ret
= set_supply(rdev
, r
);
2084 regulator_unlock_two(rdev
, r
, &ww_ctx
);
2085 put_device(&r
->dev
);
2089 regulator_unlock_two(rdev
, r
, &ww_ctx
);
2092 * In set_machine_constraints() we may have turned this regulator on
2093 * but we couldn't propagate to the supply if it hadn't been resolved
2096 if (rdev
->use_count
) {
2097 ret
= regulator_enable(rdev
->supply
);
2099 _regulator_put(rdev
->supply
);
2100 rdev
->supply
= NULL
;
2109 /* common pre-checks for regulator requests */
2110 int _regulator_get_common_check(struct device
*dev
, const char *id
,
2111 enum regulator_get_type get_type
)
2113 if (get_type
>= MAX_GET_TYPE
) {
2114 dev_err(dev
, "invalid type %d in %s\n", get_type
, __func__
);
2119 dev_err(dev
, "regulator request with no identifier\n");
2127 * _regulator_get_common - Common code for regulator requests
2128 * @rdev: regulator device pointer as returned by *regulator_dev_lookup()
2129 * Its reference count is expected to have been incremented.
2130 * @dev: device used for dev_printk messages
2131 * @id: Supply name or regulator ID
2132 * @get_type: enum regulator_get_type value corresponding to type of request
2134 * Returns: pointer to struct regulator corresponding to @rdev, or ERR_PTR()
2137 * This function should be chained with *regulator_dev_lookup() functions.
2139 struct regulator
*_regulator_get_common(struct regulator_dev
*rdev
, struct device
*dev
,
2140 const char *id
, enum regulator_get_type get_type
)
2142 struct regulator
*regulator
;
2143 struct device_link
*link
;
2147 ret
= PTR_ERR(rdev
);
2150 * If regulator_dev_lookup() fails with error other
2151 * than -ENODEV our job here is done, we simply return it.
2154 return ERR_PTR(ret
);
2156 if (!have_full_constraints()) {
2158 "incomplete constraints, dummy supplies not allowed (id=%s)\n", id
);
2159 return ERR_PTR(-ENODEV
);
2165 * Assume that a regulator is physically present and
2166 * enabled, even if it isn't hooked up, and just
2169 dev_warn(dev
, "supply %s not found, using dummy regulator\n", id
);
2170 rdev
= dummy_regulator_rdev
;
2171 get_device(&rdev
->dev
);
2176 "dummy supplies not allowed for exclusive requests (id=%s)\n", id
);
2180 return ERR_PTR(-ENODEV
);
2184 if (rdev
->exclusive
) {
2185 regulator
= ERR_PTR(-EPERM
);
2186 put_device(&rdev
->dev
);
2190 if (get_type
== EXCLUSIVE_GET
&& rdev
->open_count
) {
2191 regulator
= ERR_PTR(-EBUSY
);
2192 put_device(&rdev
->dev
);
2196 mutex_lock(®ulator_list_mutex
);
2197 ret
= (rdev
->coupling_desc
.n_resolved
!= rdev
->coupling_desc
.n_coupled
);
2198 mutex_unlock(®ulator_list_mutex
);
2201 regulator
= ERR_PTR(-EPROBE_DEFER
);
2202 put_device(&rdev
->dev
);
2206 ret
= regulator_resolve_supply(rdev
);
2208 regulator
= ERR_PTR(ret
);
2209 put_device(&rdev
->dev
);
2213 if (!try_module_get(rdev
->owner
)) {
2214 regulator
= ERR_PTR(-EPROBE_DEFER
);
2215 put_device(&rdev
->dev
);
2219 regulator_lock(rdev
);
2220 regulator
= create_regulator(rdev
, dev
, id
);
2221 regulator_unlock(rdev
);
2222 if (regulator
== NULL
) {
2223 regulator
= ERR_PTR(-ENOMEM
);
2224 module_put(rdev
->owner
);
2225 put_device(&rdev
->dev
);
2230 if (get_type
== EXCLUSIVE_GET
) {
2231 rdev
->exclusive
= 1;
2233 ret
= _regulator_is_enabled(rdev
);
2235 rdev
->use_count
= 1;
2236 regulator
->enable_count
= 1;
2238 /* Propagate the regulator state to its supply */
2240 ret
= regulator_enable(rdev
->supply
);
2242 destroy_regulator(regulator
);
2243 module_put(rdev
->owner
);
2244 put_device(&rdev
->dev
);
2245 return ERR_PTR(ret
);
2249 rdev
->use_count
= 0;
2250 regulator
->enable_count
= 0;
2254 link
= device_link_add(dev
, &rdev
->dev
, DL_FLAG_STATELESS
);
2255 if (!IS_ERR_OR_NULL(link
))
2256 regulator
->device_link
= true;
2261 /* Internal regulator request function */
2262 struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
2263 enum regulator_get_type get_type
)
2265 struct regulator_dev
*rdev
;
2268 ret
= _regulator_get_common_check(dev
, id
, get_type
);
2270 return ERR_PTR(ret
);
2272 rdev
= regulator_dev_lookup(dev
, id
);
2273 return _regulator_get_common(rdev
, dev
, id
, get_type
);
2277 * regulator_get - lookup and obtain a reference to a regulator.
2278 * @dev: device for regulator "consumer"
2279 * @id: Supply name or regulator ID.
2281 * Use of supply names configured via set_consumer_device_supply() is
2282 * strongly encouraged. It is recommended that the supply name used
2283 * should match the name used for the supply and/or the relevant
2284 * device pins in the datasheet.
2286 * Return: Pointer to a &struct regulator corresponding to the regulator
2287 * producer, or an ERR_PTR() encoded negative error number.
2289 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
2291 return _regulator_get(dev
, id
, NORMAL_GET
);
2293 EXPORT_SYMBOL_GPL(regulator_get
);
2296 * regulator_get_exclusive - obtain exclusive access to a regulator.
2297 * @dev: device for regulator "consumer"
2298 * @id: Supply name or regulator ID.
2300 * Other consumers will be unable to obtain this regulator while this
2301 * reference is held and the use count for the regulator will be
2302 * initialised to reflect the current state of the regulator.
2304 * This is intended for use by consumers which cannot tolerate shared
2305 * use of the regulator such as those which need to force the
2306 * regulator off for correct operation of the hardware they are
2309 * Use of supply names configured via set_consumer_device_supply() is
2310 * strongly encouraged. It is recommended that the supply name used
2311 * should match the name used for the supply and/or the relevant
2312 * device pins in the datasheet.
2314 * Return: Pointer to a &struct regulator corresponding to the regulator
2315 * producer, or an ERR_PTR() encoded negative error number.
2317 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
2319 return _regulator_get(dev
, id
, EXCLUSIVE_GET
);
2321 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
2324 * regulator_get_optional - obtain optional access to a regulator.
2325 * @dev: device for regulator "consumer"
2326 * @id: Supply name or regulator ID.
2328 * This is intended for use by consumers for devices which can have
2329 * some supplies unconnected in normal use, such as some MMC devices.
2330 * It can allow the regulator core to provide stub supplies for other
2331 * supplies requested using normal regulator_get() calls without
2332 * disrupting the operation of drivers that can handle absent
2335 * Use of supply names configured via set_consumer_device_supply() is
2336 * strongly encouraged. It is recommended that the supply name used
2337 * should match the name used for the supply and/or the relevant
2338 * device pins in the datasheet.
2340 * Return: Pointer to a &struct regulator corresponding to the regulator
2341 * producer, or an ERR_PTR() encoded negative error number.
2343 struct regulator
*regulator_get_optional(struct device
*dev
, const char *id
)
2345 return _regulator_get(dev
, id
, OPTIONAL_GET
);
2347 EXPORT_SYMBOL_GPL(regulator_get_optional
);
2349 static void destroy_regulator(struct regulator
*regulator
)
2351 struct regulator_dev
*rdev
= regulator
->rdev
;
2353 debugfs_remove_recursive(regulator
->debugfs
);
2355 if (regulator
->dev
) {
2356 if (regulator
->device_link
)
2357 device_link_remove(regulator
->dev
, &rdev
->dev
);
2359 /* remove any sysfs entries */
2360 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
2363 regulator_lock(rdev
);
2364 list_del(®ulator
->list
);
2367 rdev
->exclusive
= 0;
2368 regulator_unlock(rdev
);
2370 kfree_const(regulator
->supply_name
);
2374 /* regulator_list_mutex lock held by regulator_put() */
2375 static void _regulator_put(struct regulator
*regulator
)
2377 struct regulator_dev
*rdev
;
2379 if (IS_ERR_OR_NULL(regulator
))
2382 lockdep_assert_held_once(®ulator_list_mutex
);
2384 /* Docs say you must disable before calling regulator_put() */
2385 WARN_ON(regulator
->enable_count
);
2387 rdev
= regulator
->rdev
;
2389 destroy_regulator(regulator
);
2391 module_put(rdev
->owner
);
2392 put_device(&rdev
->dev
);
2396 * regulator_put - "free" the regulator source
2397 * @regulator: regulator source
2399 * Note: drivers must ensure that all regulator_enable calls made on this
2400 * regulator source are balanced by regulator_disable calls prior to calling
2403 void regulator_put(struct regulator
*regulator
)
2405 mutex_lock(®ulator_list_mutex
);
2406 _regulator_put(regulator
);
2407 mutex_unlock(®ulator_list_mutex
);
2409 EXPORT_SYMBOL_GPL(regulator_put
);
2412 * regulator_register_supply_alias - Provide device alias for supply lookup
2414 * @dev: device that will be given as the regulator "consumer"
2415 * @id: Supply name or regulator ID
2416 * @alias_dev: device that should be used to lookup the supply
2417 * @alias_id: Supply name or regulator ID that should be used to lookup the
2420 * All lookups for id on dev will instead be conducted for alias_id on
2423 * Return: 0 on success or a negative error number on failure.
2425 int regulator_register_supply_alias(struct device
*dev
, const char *id
,
2426 struct device
*alias_dev
,
2427 const char *alias_id
)
2429 struct regulator_supply_alias
*map
;
2431 map
= regulator_find_supply_alias(dev
, id
);
2435 map
= kzalloc(sizeof(struct regulator_supply_alias
), GFP_KERNEL
);
2440 map
->src_supply
= id
;
2441 map
->alias_dev
= alias_dev
;
2442 map
->alias_supply
= alias_id
;
2444 list_add(&map
->list
, ®ulator_supply_alias_list
);
2446 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2447 id
, dev_name(dev
), alias_id
, dev_name(alias_dev
));
2451 EXPORT_SYMBOL_GPL(regulator_register_supply_alias
);
2454 * regulator_unregister_supply_alias - Remove device alias
2456 * @dev: device that will be given as the regulator "consumer"
2457 * @id: Supply name or regulator ID
2459 * Remove a lookup alias if one exists for id on dev.
2461 void regulator_unregister_supply_alias(struct device
*dev
, const char *id
)
2463 struct regulator_supply_alias
*map
;
2465 map
= regulator_find_supply_alias(dev
, id
);
2467 list_del(&map
->list
);
2471 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias
);
2474 * regulator_bulk_register_supply_alias - register multiple aliases
2476 * @dev: device that will be given as the regulator "consumer"
2477 * @id: List of supply names or regulator IDs
2478 * @alias_dev: device that should be used to lookup the supply
2479 * @alias_id: List of supply names or regulator IDs that should be used to
2481 * @num_id: Number of aliases to register
2483 * This helper function allows drivers to register several supply
2484 * aliases in one operation. If any of the aliases cannot be
2485 * registered any aliases that were registered will be removed
2486 * before returning to the caller.
2488 * Return: 0 on success or a negative error number on failure.
2490 int regulator_bulk_register_supply_alias(struct device
*dev
,
2491 const char *const *id
,
2492 struct device
*alias_dev
,
2493 const char *const *alias_id
,
2499 for (i
= 0; i
< num_id
; ++i
) {
2500 ret
= regulator_register_supply_alias(dev
, id
[i
], alias_dev
,
2510 "Failed to create supply alias %s,%s -> %s,%s\n",
2511 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
2514 regulator_unregister_supply_alias(dev
, id
[i
]);
2518 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias
);
2521 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2523 * @dev: device that will be given as the regulator "consumer"
2524 * @id: List of supply names or regulator IDs
2525 * @num_id: Number of aliases to unregister
2527 * This helper function allows drivers to unregister several supply
2528 * aliases in one operation.
2530 void regulator_bulk_unregister_supply_alias(struct device
*dev
,
2531 const char *const *id
,
2536 for (i
= 0; i
< num_id
; ++i
)
2537 regulator_unregister_supply_alias(dev
, id
[i
]);
2539 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias
);
2542 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2543 static int regulator_ena_gpio_request(struct regulator_dev
*rdev
,
2544 const struct regulator_config
*config
)
2546 struct regulator_enable_gpio
*pin
, *new_pin
;
2547 struct gpio_desc
*gpiod
;
2549 gpiod
= config
->ena_gpiod
;
2550 new_pin
= kzalloc(sizeof(*new_pin
), GFP_KERNEL
);
2552 mutex_lock(®ulator_list_mutex
);
2554 list_for_each_entry(pin
, ®ulator_ena_gpio_list
, list
) {
2555 if (pin
->gpiod
== gpiod
) {
2556 rdev_dbg(rdev
, "GPIO is already used\n");
2557 goto update_ena_gpio_to_rdev
;
2561 if (new_pin
== NULL
) {
2562 mutex_unlock(®ulator_list_mutex
);
2570 list_add(&pin
->list
, ®ulator_ena_gpio_list
);
2572 update_ena_gpio_to_rdev
:
2573 pin
->request_count
++;
2574 rdev
->ena_pin
= pin
;
2576 mutex_unlock(®ulator_list_mutex
);
2582 static void regulator_ena_gpio_free(struct regulator_dev
*rdev
)
2584 struct regulator_enable_gpio
*pin
, *n
;
2589 /* Free the GPIO only in case of no use */
2590 list_for_each_entry_safe(pin
, n
, ®ulator_ena_gpio_list
, list
) {
2591 if (pin
!= rdev
->ena_pin
)
2594 if (--pin
->request_count
)
2597 gpiod_put(pin
->gpiod
);
2598 list_del(&pin
->list
);
2603 rdev
->ena_pin
= NULL
;
2607 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2608 * @rdev: regulator_dev structure
2609 * @enable: enable GPIO at initial use?
2611 * GPIO is enabled in case of initial use. (enable_count is 0)
2612 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2614 * Return: 0 on success or a negative error number on failure.
2616 static int regulator_ena_gpio_ctrl(struct regulator_dev
*rdev
, bool enable
)
2618 struct regulator_enable_gpio
*pin
= rdev
->ena_pin
;
2624 /* Enable GPIO at initial use */
2625 if (pin
->enable_count
== 0)
2626 gpiod_set_value_cansleep(pin
->gpiod
, 1);
2628 pin
->enable_count
++;
2630 if (pin
->enable_count
> 1) {
2631 pin
->enable_count
--;
2635 /* Disable GPIO if not used */
2636 if (pin
->enable_count
<= 1) {
2637 gpiod_set_value_cansleep(pin
->gpiod
, 0);
2638 pin
->enable_count
= 0;
2646 * _regulator_delay_helper - a delay helper function
2647 * @delay: time to delay in microseconds
2649 * Delay for the requested amount of time as per the guidelines in:
2651 * Documentation/timers/timers-howto.rst
2653 * The assumption here is that these regulator operations will never used in
2654 * atomic context and therefore sleeping functions can be used.
2656 static void _regulator_delay_helper(unsigned int delay
)
2658 unsigned int ms
= delay
/ 1000;
2659 unsigned int us
= delay
% 1000;
2663 * For small enough values, handle super-millisecond
2664 * delays in the usleep_range() call below.
2673 * Give the scheduler some room to coalesce with any other
2674 * wakeup sources. For delays shorter than 10 us, don't even
2675 * bother setting up high-resolution timers and just busy-
2679 usleep_range(us
, us
+ 100);
2685 * _regulator_check_status_enabled - check if regulator status can be
2686 * interpreted as "regulator is enabled"
2687 * @rdev: the regulator device to check
2690 * * 1 - if status shows regulator is in enabled state
2691 * * 0 - if not enabled state
2692 * * Error Value - as received from ops->get_status()
2694 static inline int _regulator_check_status_enabled(struct regulator_dev
*rdev
)
2696 int ret
= rdev
->desc
->ops
->get_status(rdev
);
2699 rdev_info(rdev
, "get_status returned error: %d\n", ret
);
2704 case REGULATOR_STATUS_OFF
:
2705 case REGULATOR_STATUS_ERROR
:
2706 case REGULATOR_STATUS_UNDEFINED
:
2713 static int _regulator_do_enable(struct regulator_dev
*rdev
)
2717 /* Query before enabling in case configuration dependent. */
2718 ret
= _regulator_get_enable_time(rdev
);
2722 rdev_warn(rdev
, "enable_time() failed: %pe\n", ERR_PTR(ret
));
2726 trace_regulator_enable(rdev_get_name(rdev
));
2728 if (rdev
->desc
->off_on_delay
) {
2729 /* if needed, keep a distance of off_on_delay from last time
2730 * this regulator was disabled.
2732 ktime_t end
= ktime_add_us(rdev
->last_off
, rdev
->desc
->off_on_delay
);
2733 s64 remaining
= ktime_us_delta(end
, ktime_get_boottime());
2736 _regulator_delay_helper(remaining
);
2739 if (rdev
->ena_pin
) {
2740 if (!rdev
->ena_gpio_state
) {
2741 ret
= regulator_ena_gpio_ctrl(rdev
, true);
2744 rdev
->ena_gpio_state
= 1;
2746 } else if (rdev
->desc
->ops
->enable
) {
2747 ret
= rdev
->desc
->ops
->enable(rdev
);
2754 /* Allow the regulator to ramp; it would be useful to extend
2755 * this for bulk operations so that the regulators can ramp
2758 trace_regulator_enable_delay(rdev_get_name(rdev
));
2760 /* If poll_enabled_time is set, poll upto the delay calculated
2761 * above, delaying poll_enabled_time uS to check if the regulator
2762 * actually got enabled.
2763 * If the regulator isn't enabled after our delay helper has expired,
2764 * return -ETIMEDOUT.
2766 if (rdev
->desc
->poll_enabled_time
) {
2767 int time_remaining
= delay
;
2769 while (time_remaining
> 0) {
2770 _regulator_delay_helper(rdev
->desc
->poll_enabled_time
);
2772 if (rdev
->desc
->ops
->get_status
) {
2773 ret
= _regulator_check_status_enabled(rdev
);
2778 } else if (rdev
->desc
->ops
->is_enabled(rdev
))
2781 time_remaining
-= rdev
->desc
->poll_enabled_time
;
2784 if (time_remaining
<= 0) {
2785 rdev_err(rdev
, "Enabled check timed out\n");
2789 _regulator_delay_helper(delay
);
2792 trace_regulator_enable_complete(rdev_get_name(rdev
));
2798 * _regulator_handle_consumer_enable - handle that a consumer enabled
2799 * @regulator: regulator source
2801 * Some things on a regulator consumer (like the contribution towards total
2802 * load on the regulator) only have an effect when the consumer wants the
2803 * regulator enabled. Explained in example with two consumers of the same
2805 * consumer A: set_load(100); => total load = 0
2806 * consumer A: regulator_enable(); => total load = 100
2807 * consumer B: set_load(1000); => total load = 100
2808 * consumer B: regulator_enable(); => total load = 1100
2809 * consumer A: regulator_disable(); => total_load = 1000
2811 * This function (together with _regulator_handle_consumer_disable) is
2812 * responsible for keeping track of the refcount for a given regulator consumer
2813 * and applying / unapplying these things.
2815 * Return: 0 on success or negative error number on failure.
2817 static int _regulator_handle_consumer_enable(struct regulator
*regulator
)
2820 struct regulator_dev
*rdev
= regulator
->rdev
;
2822 lockdep_assert_held_once(&rdev
->mutex
.base
);
2824 regulator
->enable_count
++;
2825 if (regulator
->uA_load
&& regulator
->enable_count
== 1) {
2826 ret
= drms_uA_update(rdev
);
2828 regulator
->enable_count
--;
2836 * _regulator_handle_consumer_disable - handle that a consumer disabled
2837 * @regulator: regulator source
2839 * The opposite of _regulator_handle_consumer_enable().
2841 * Return: 0 on success or a negative error number on failure.
2843 static int _regulator_handle_consumer_disable(struct regulator
*regulator
)
2845 struct regulator_dev
*rdev
= regulator
->rdev
;
2847 lockdep_assert_held_once(&rdev
->mutex
.base
);
2849 if (!regulator
->enable_count
) {
2850 rdev_err(rdev
, "Underflow of regulator enable count\n");
2854 regulator
->enable_count
--;
2855 if (regulator
->uA_load
&& regulator
->enable_count
== 0)
2856 return drms_uA_update(rdev
);
2861 /* locks held by regulator_enable() */
2862 static int _regulator_enable(struct regulator
*regulator
)
2864 struct regulator_dev
*rdev
= regulator
->rdev
;
2867 lockdep_assert_held_once(&rdev
->mutex
.base
);
2869 if (rdev
->use_count
== 0 && rdev
->supply
) {
2870 ret
= _regulator_enable(rdev
->supply
);
2875 /* balance only if there are regulators coupled */
2876 if (rdev
->coupling_desc
.n_coupled
> 1) {
2877 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2879 goto err_disable_supply
;
2882 ret
= _regulator_handle_consumer_enable(regulator
);
2884 goto err_disable_supply
;
2886 if (rdev
->use_count
== 0) {
2888 * The regulator may already be enabled if it's not switchable
2891 ret
= _regulator_is_enabled(rdev
);
2892 if (ret
== -EINVAL
|| ret
== 0) {
2893 if (!regulator_ops_is_valid(rdev
,
2894 REGULATOR_CHANGE_STATUS
)) {
2896 goto err_consumer_disable
;
2899 ret
= _regulator_do_enable(rdev
);
2901 goto err_consumer_disable
;
2903 _notifier_call_chain(rdev
, REGULATOR_EVENT_ENABLE
,
2905 } else if (ret
< 0) {
2906 rdev_err(rdev
, "is_enabled() failed: %pe\n", ERR_PTR(ret
));
2907 goto err_consumer_disable
;
2909 /* Fallthrough on positive return values - already enabled */
2912 if (regulator
->enable_count
== 1)
2917 err_consumer_disable
:
2918 _regulator_handle_consumer_disable(regulator
);
2921 if (rdev
->use_count
== 0 && rdev
->supply
)
2922 _regulator_disable(rdev
->supply
);
2928 * regulator_enable - enable regulator output
2929 * @regulator: regulator source
2931 * Request that the regulator be enabled with the regulator output at
2932 * the predefined voltage or current value. Calls to regulator_enable()
2933 * must be balanced with calls to regulator_disable().
2935 * NOTE: the output value can be set by other drivers, boot loader or may be
2936 * hardwired in the regulator.
2938 * Return: 0 on success or a negative error number on failure.
2940 int regulator_enable(struct regulator
*regulator
)
2942 struct regulator_dev
*rdev
= regulator
->rdev
;
2943 struct ww_acquire_ctx ww_ctx
;
2946 regulator_lock_dependent(rdev
, &ww_ctx
);
2947 ret
= _regulator_enable(regulator
);
2948 regulator_unlock_dependent(rdev
, &ww_ctx
);
2952 EXPORT_SYMBOL_GPL(regulator_enable
);
2954 static int _regulator_do_disable(struct regulator_dev
*rdev
)
2958 trace_regulator_disable(rdev_get_name(rdev
));
2960 if (rdev
->ena_pin
) {
2961 if (rdev
->ena_gpio_state
) {
2962 ret
= regulator_ena_gpio_ctrl(rdev
, false);
2965 rdev
->ena_gpio_state
= 0;
2968 } else if (rdev
->desc
->ops
->disable
) {
2969 ret
= rdev
->desc
->ops
->disable(rdev
);
2974 if (rdev
->desc
->off_on_delay
)
2975 rdev
->last_off
= ktime_get_boottime();
2977 trace_regulator_disable_complete(rdev_get_name(rdev
));
2982 /* locks held by regulator_disable() */
2983 static int _regulator_disable(struct regulator
*regulator
)
2985 struct regulator_dev
*rdev
= regulator
->rdev
;
2988 lockdep_assert_held_once(&rdev
->mutex
.base
);
2990 if (WARN(regulator
->enable_count
== 0,
2991 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
2994 if (regulator
->enable_count
== 1) {
2995 /* disabling last enable_count from this regulator */
2996 /* are we the last user and permitted to disable ? */
2997 if (rdev
->use_count
== 1 &&
2998 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
3000 /* we are last user */
3001 if (regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
)) {
3002 ret
= _notifier_call_chain(rdev
,
3003 REGULATOR_EVENT_PRE_DISABLE
,
3005 if (ret
& NOTIFY_STOP_MASK
)
3008 ret
= _regulator_do_disable(rdev
);
3010 rdev_err(rdev
, "failed to disable: %pe\n", ERR_PTR(ret
));
3011 _notifier_call_chain(rdev
,
3012 REGULATOR_EVENT_ABORT_DISABLE
,
3016 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
3020 rdev
->use_count
= 0;
3021 } else if (rdev
->use_count
> 1) {
3027 ret
= _regulator_handle_consumer_disable(regulator
);
3029 if (ret
== 0 && rdev
->coupling_desc
.n_coupled
> 1)
3030 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
3032 if (ret
== 0 && rdev
->use_count
== 0 && rdev
->supply
)
3033 ret
= _regulator_disable(rdev
->supply
);
3039 * regulator_disable - disable regulator output
3040 * @regulator: regulator source
3042 * Disable the regulator output voltage or current. Calls to
3043 * regulator_enable() must be balanced with calls to
3044 * regulator_disable().
3046 * NOTE: this will only disable the regulator output if no other consumer
3047 * devices have it enabled, the regulator device supports disabling and
3048 * machine constraints permit this operation.
3050 * Return: 0 on success or a negative error number on failure.
3052 int regulator_disable(struct regulator
*regulator
)
3054 struct regulator_dev
*rdev
= regulator
->rdev
;
3055 struct ww_acquire_ctx ww_ctx
;
3058 regulator_lock_dependent(rdev
, &ww_ctx
);
3059 ret
= _regulator_disable(regulator
);
3060 regulator_unlock_dependent(rdev
, &ww_ctx
);
3064 EXPORT_SYMBOL_GPL(regulator_disable
);
3066 /* locks held by regulator_force_disable() */
3067 static int _regulator_force_disable(struct regulator_dev
*rdev
)
3071 lockdep_assert_held_once(&rdev
->mutex
.base
);
3073 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
3074 REGULATOR_EVENT_PRE_DISABLE
, NULL
);
3075 if (ret
& NOTIFY_STOP_MASK
)
3078 ret
= _regulator_do_disable(rdev
);
3080 rdev_err(rdev
, "failed to force disable: %pe\n", ERR_PTR(ret
));
3081 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
3082 REGULATOR_EVENT_ABORT_DISABLE
, NULL
);
3086 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
3087 REGULATOR_EVENT_DISABLE
, NULL
);
3093 * regulator_force_disable - force disable regulator output
3094 * @regulator: regulator source
3096 * Forcibly disable the regulator output voltage or current.
3097 * NOTE: this *will* disable the regulator output even if other consumer
3098 * devices have it enabled. This should be used for situations when device
3099 * damage will likely occur if the regulator is not disabled (e.g. over temp).
3101 * Return: 0 on success or a negative error number on failure.
3103 int regulator_force_disable(struct regulator
*regulator
)
3105 struct regulator_dev
*rdev
= regulator
->rdev
;
3106 struct ww_acquire_ctx ww_ctx
;
3109 regulator_lock_dependent(rdev
, &ww_ctx
);
3111 ret
= _regulator_force_disable(regulator
->rdev
);
3113 if (rdev
->coupling_desc
.n_coupled
> 1)
3114 regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
3116 if (regulator
->uA_load
) {
3117 regulator
->uA_load
= 0;
3118 ret
= drms_uA_update(rdev
);
3121 if (rdev
->use_count
!= 0 && rdev
->supply
)
3122 _regulator_disable(rdev
->supply
);
3124 regulator_unlock_dependent(rdev
, &ww_ctx
);
3128 EXPORT_SYMBOL_GPL(regulator_force_disable
);
3130 static void regulator_disable_work(struct work_struct
*work
)
3132 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
3134 struct ww_acquire_ctx ww_ctx
;
3136 struct regulator
*regulator
;
3137 int total_count
= 0;
3139 regulator_lock_dependent(rdev
, &ww_ctx
);
3142 * Workqueue functions queue the new work instance while the previous
3143 * work instance is being processed. Cancel the queued work instance
3144 * as the work instance under processing does the job of the queued
3147 cancel_delayed_work(&rdev
->disable_work
);
3149 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
3150 count
= regulator
->deferred_disables
;
3155 total_count
+= count
;
3156 regulator
->deferred_disables
= 0;
3158 for (i
= 0; i
< count
; i
++) {
3159 ret
= _regulator_disable(regulator
);
3161 rdev_err(rdev
, "Deferred disable failed: %pe\n",
3165 WARN_ON(!total_count
);
3167 if (rdev
->coupling_desc
.n_coupled
> 1)
3168 regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
3170 regulator_unlock_dependent(rdev
, &ww_ctx
);
3174 * regulator_disable_deferred - disable regulator output with delay
3175 * @regulator: regulator source
3176 * @ms: milliseconds until the regulator is disabled
3178 * Execute regulator_disable() on the regulator after a delay. This
3179 * is intended for use with devices that require some time to quiesce.
3181 * NOTE: this will only disable the regulator output if no other consumer
3182 * devices have it enabled, the regulator device supports disabling and
3183 * machine constraints permit this operation.
3185 * Return: 0 on success or a negative error number on failure.
3187 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
3189 struct regulator_dev
*rdev
= regulator
->rdev
;
3192 return regulator_disable(regulator
);
3194 regulator_lock(rdev
);
3195 regulator
->deferred_disables
++;
3196 mod_delayed_work(system_power_efficient_wq
, &rdev
->disable_work
,
3197 msecs_to_jiffies(ms
));
3198 regulator_unlock(rdev
);
3202 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
3204 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
3206 /* A GPIO control always takes precedence */
3208 return rdev
->ena_gpio_state
;
3210 /* If we don't know then assume that the regulator is always on */
3211 if (!rdev
->desc
->ops
->is_enabled
)
3214 return rdev
->desc
->ops
->is_enabled(rdev
);
3217 static int _regulator_list_voltage(struct regulator_dev
*rdev
,
3218 unsigned selector
, int lock
)
3220 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3223 if (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1 && !selector
)
3224 return rdev
->desc
->fixed_uV
;
3226 if (ops
->list_voltage
) {
3227 if (selector
>= rdev
->desc
->n_voltages
)
3229 if (selector
< rdev
->desc
->linear_min_sel
)
3232 regulator_lock(rdev
);
3233 ret
= ops
->list_voltage(rdev
, selector
);
3235 regulator_unlock(rdev
);
3236 } else if (rdev
->is_switch
&& rdev
->supply
) {
3237 ret
= _regulator_list_voltage(rdev
->supply
->rdev
,
3244 if (ret
< rdev
->constraints
->min_uV
)
3246 else if (ret
> rdev
->constraints
->max_uV
)
3254 * regulator_is_enabled - is the regulator output enabled
3255 * @regulator: regulator source
3257 * Note that the device backing this regulator handle can have multiple
3258 * users, so it might be enabled even if regulator_enable() was never
3259 * called for this particular source.
3261 * Return: Positive if the regulator driver backing the source/client
3262 * has requested that the device be enabled, zero if it hasn't,
3263 * else a negative error number.
3265 int regulator_is_enabled(struct regulator
*regulator
)
3269 if (regulator
->always_on
)
3272 regulator_lock(regulator
->rdev
);
3273 ret
= _regulator_is_enabled(regulator
->rdev
);
3274 regulator_unlock(regulator
->rdev
);
3278 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
3281 * regulator_count_voltages - count regulator_list_voltage() selectors
3282 * @regulator: regulator source
3284 * Return: Number of selectors for @regulator, or negative error number.
3286 * Selectors are numbered starting at zero, and typically correspond to
3287 * bitfields in hardware registers.
3289 int regulator_count_voltages(struct regulator
*regulator
)
3291 struct regulator_dev
*rdev
= regulator
->rdev
;
3293 if (rdev
->desc
->n_voltages
)
3294 return rdev
->desc
->n_voltages
;
3296 if (!rdev
->is_switch
|| !rdev
->supply
)
3299 return regulator_count_voltages(rdev
->supply
);
3301 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
3304 * regulator_list_voltage - enumerate supported voltages
3305 * @regulator: regulator source
3306 * @selector: identify voltage to list
3307 * Context: can sleep
3309 * Return: Voltage for @selector that can be passed to regulator_set_voltage(),
3310 * 0 if @selector can't be used on this system, or a negative error
3311 * number on failure.
3313 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
3315 return _regulator_list_voltage(regulator
->rdev
, selector
, 1);
3317 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
3320 * regulator_get_regmap - get the regulator's register map
3321 * @regulator: regulator source
3323 * Return: Pointer to the &struct regmap for @regulator, or ERR_PTR()
3324 * encoded -%EOPNOTSUPP if @regulator doesn't use regmap.
3326 struct regmap
*regulator_get_regmap(struct regulator
*regulator
)
3328 struct regmap
*map
= regulator
->rdev
->regmap
;
3330 return map
? map
: ERR_PTR(-EOPNOTSUPP
);
3332 EXPORT_SYMBOL_GPL(regulator_get_regmap
);
3335 * regulator_get_hardware_vsel_register - get the HW voltage selector register
3336 * @regulator: regulator source
3337 * @vsel_reg: voltage selector register, output parameter
3338 * @vsel_mask: mask for voltage selector bitfield, output parameter
3340 * Returns the hardware register offset and bitmask used for setting the
3341 * regulator voltage. This might be useful when configuring voltage-scaling
3342 * hardware or firmware that can make I2C requests behind the kernel's back,
3345 * Return: 0 on success, or -%EOPNOTSUPP if the regulator does not support
3346 * voltage selectors.
3348 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
3349 * and 0 is returned, otherwise a negative error number is returned.
3351 int regulator_get_hardware_vsel_register(struct regulator
*regulator
,
3353 unsigned *vsel_mask
)
3355 struct regulator_dev
*rdev
= regulator
->rdev
;
3356 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3358 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
3361 *vsel_reg
= rdev
->desc
->vsel_reg
;
3362 *vsel_mask
= rdev
->desc
->vsel_mask
;
3366 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register
);
3369 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
3370 * @regulator: regulator source
3371 * @selector: identify voltage to list
3373 * Converts the selector to a hardware-specific voltage selector that can be
3374 * directly written to the regulator registers. The address of the voltage
3375 * register can be determined by calling @regulator_get_hardware_vsel_register.
3377 * Return: 0 on success, -%EINVAL if the selector is outside the supported
3378 * range, or -%EOPNOTSUPP if the regulator does not support voltage
3381 int regulator_list_hardware_vsel(struct regulator
*regulator
,
3384 struct regulator_dev
*rdev
= regulator
->rdev
;
3385 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3387 if (selector
>= rdev
->desc
->n_voltages
)
3389 if (selector
< rdev
->desc
->linear_min_sel
)
3391 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
3396 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel
);
3399 * regulator_hardware_enable - access the HW for enable/disable regulator
3400 * @regulator: regulator source
3401 * @enable: true for enable, false for disable
3403 * Request that the regulator be enabled/disabled with the regulator output at
3404 * the predefined voltage or current value.
3406 * Return: 0 on success or a negative error number on failure.
3408 int regulator_hardware_enable(struct regulator
*regulator
, bool enable
)
3410 struct regulator_dev
*rdev
= regulator
->rdev
;
3411 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3412 int ret
= -EOPNOTSUPP
;
3414 if (!rdev
->exclusive
|| !ops
|| !ops
->enable
|| !ops
->disable
)
3418 ret
= ops
->enable(rdev
);
3420 ret
= ops
->disable(rdev
);
3424 EXPORT_SYMBOL_GPL(regulator_hardware_enable
);
3427 * regulator_get_linear_step - return the voltage step size between VSEL values
3428 * @regulator: regulator source
3430 * Return: The voltage step size between VSEL values for linear regulators,
3431 * or 0 if the regulator isn't a linear regulator.
3433 unsigned int regulator_get_linear_step(struct regulator
*regulator
)
3435 struct regulator_dev
*rdev
= regulator
->rdev
;
3437 return rdev
->desc
->uV_step
;
3439 EXPORT_SYMBOL_GPL(regulator_get_linear_step
);
3442 * regulator_is_supported_voltage - check if a voltage range can be supported
3444 * @regulator: Regulator to check.
3445 * @min_uV: Minimum required voltage in uV.
3446 * @max_uV: Maximum required voltage in uV.
3448 * Return: 1 if the voltage range is supported, 0 if not, or a negative error
3449 * number if @regulator's voltage can't be changed and voltage readback
3452 int regulator_is_supported_voltage(struct regulator
*regulator
,
3453 int min_uV
, int max_uV
)
3455 struct regulator_dev
*rdev
= regulator
->rdev
;
3456 int i
, voltages
, ret
;
3458 /* If we can't change voltage check the current voltage */
3459 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
3460 ret
= regulator_get_voltage(regulator
);
3462 return min_uV
<= ret
&& ret
<= max_uV
;
3467 /* Any voltage within constrains range is fine? */
3468 if (rdev
->desc
->continuous_voltage_range
)
3469 return min_uV
>= rdev
->constraints
->min_uV
&&
3470 max_uV
<= rdev
->constraints
->max_uV
;
3472 ret
= regulator_count_voltages(regulator
);
3477 for (i
= 0; i
< voltages
; i
++) {
3478 ret
= regulator_list_voltage(regulator
, i
);
3480 if (ret
>= min_uV
&& ret
<= max_uV
)
3486 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
3488 static int regulator_map_voltage(struct regulator_dev
*rdev
, int min_uV
,
3491 const struct regulator_desc
*desc
= rdev
->desc
;
3493 if (desc
->ops
->map_voltage
)
3494 return desc
->ops
->map_voltage(rdev
, min_uV
, max_uV
);
3496 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear
)
3497 return regulator_map_voltage_linear(rdev
, min_uV
, max_uV
);
3499 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear_range
)
3500 return regulator_map_voltage_linear_range(rdev
, min_uV
, max_uV
);
3502 if (desc
->ops
->list_voltage
==
3503 regulator_list_voltage_pickable_linear_range
)
3504 return regulator_map_voltage_pickable_linear_range(rdev
,
3507 return regulator_map_voltage_iterate(rdev
, min_uV
, max_uV
);
3510 static int _regulator_call_set_voltage(struct regulator_dev
*rdev
,
3511 int min_uV
, int max_uV
,
3514 struct pre_voltage_change_data data
;
3517 data
.old_uV
= regulator_get_voltage_rdev(rdev
);
3518 data
.min_uV
= min_uV
;
3519 data
.max_uV
= max_uV
;
3520 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
3522 if (ret
& NOTIFY_STOP_MASK
)
3525 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
, selector
);
3529 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
3530 (void *)data
.old_uV
);
3535 static int _regulator_call_set_voltage_sel(struct regulator_dev
*rdev
,
3536 int uV
, unsigned selector
)
3538 struct pre_voltage_change_data data
;
3541 data
.old_uV
= regulator_get_voltage_rdev(rdev
);
3544 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
3546 if (ret
& NOTIFY_STOP_MASK
)
3549 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
3553 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
3554 (void *)data
.old_uV
);
3559 static int _regulator_set_voltage_sel_step(struct regulator_dev
*rdev
,
3560 int uV
, int new_selector
)
3562 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3563 int diff
, old_sel
, curr_sel
, ret
;
3565 /* Stepping is only needed if the regulator is enabled. */
3566 if (!_regulator_is_enabled(rdev
))
3569 if (!ops
->get_voltage_sel
)
3572 old_sel
= ops
->get_voltage_sel(rdev
);
3576 diff
= new_selector
- old_sel
;
3578 return 0; /* No change needed. */
3582 for (curr_sel
= old_sel
+ rdev
->desc
->vsel_step
;
3583 curr_sel
< new_selector
;
3584 curr_sel
+= rdev
->desc
->vsel_step
) {
3586 * Call the callback directly instead of using
3587 * _regulator_call_set_voltage_sel() as we don't
3588 * want to notify anyone yet. Same in the branch
3591 ret
= ops
->set_voltage_sel(rdev
, curr_sel
);
3596 /* Stepping down. */
3597 for (curr_sel
= old_sel
- rdev
->desc
->vsel_step
;
3598 curr_sel
> new_selector
;
3599 curr_sel
-= rdev
->desc
->vsel_step
) {
3600 ret
= ops
->set_voltage_sel(rdev
, curr_sel
);
3607 /* The final selector will trigger the notifiers. */
3608 return _regulator_call_set_voltage_sel(rdev
, uV
, new_selector
);
3612 * At least try to return to the previous voltage if setting a new
3615 (void)ops
->set_voltage_sel(rdev
, old_sel
);
3619 static int _regulator_set_voltage_time(struct regulator_dev
*rdev
,
3620 int old_uV
, int new_uV
)
3622 unsigned int ramp_delay
= 0;
3624 if (rdev
->constraints
->ramp_delay
)
3625 ramp_delay
= rdev
->constraints
->ramp_delay
;
3626 else if (rdev
->desc
->ramp_delay
)
3627 ramp_delay
= rdev
->desc
->ramp_delay
;
3628 else if (rdev
->constraints
->settling_time
)
3629 return rdev
->constraints
->settling_time
;
3630 else if (rdev
->constraints
->settling_time_up
&&
3632 return rdev
->constraints
->settling_time_up
;
3633 else if (rdev
->constraints
->settling_time_down
&&
3635 return rdev
->constraints
->settling_time_down
;
3637 if (ramp_delay
== 0)
3640 return DIV_ROUND_UP(abs(new_uV
- old_uV
), ramp_delay
);
3643 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
3644 int min_uV
, int max_uV
)
3649 unsigned int selector
;
3650 int old_selector
= -1;
3651 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3652 int old_uV
= regulator_get_voltage_rdev(rdev
);
3654 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
3656 min_uV
+= rdev
->constraints
->uV_offset
;
3657 max_uV
+= rdev
->constraints
->uV_offset
;
3660 * If we can't obtain the old selector there is not enough
3661 * info to call set_voltage_time_sel().
3663 if (_regulator_is_enabled(rdev
) &&
3664 ops
->set_voltage_time_sel
&& ops
->get_voltage_sel
) {
3665 old_selector
= ops
->get_voltage_sel(rdev
);
3666 if (old_selector
< 0)
3667 return old_selector
;
3670 if (ops
->set_voltage
) {
3671 ret
= _regulator_call_set_voltage(rdev
, min_uV
, max_uV
,
3675 if (ops
->list_voltage
)
3676 best_val
= ops
->list_voltage(rdev
,
3679 best_val
= regulator_get_voltage_rdev(rdev
);
3682 } else if (ops
->set_voltage_sel
) {
3683 ret
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3685 best_val
= ops
->list_voltage(rdev
, ret
);
3686 if (min_uV
<= best_val
&& max_uV
>= best_val
) {
3688 if (old_selector
== selector
)
3690 else if (rdev
->desc
->vsel_step
)
3691 ret
= _regulator_set_voltage_sel_step(
3692 rdev
, best_val
, selector
);
3694 ret
= _regulator_call_set_voltage_sel(
3695 rdev
, best_val
, selector
);
3707 if (ops
->set_voltage_time_sel
) {
3709 * Call set_voltage_time_sel if successfully obtained
3712 if (old_selector
>= 0 && old_selector
!= selector
)
3713 delay
= ops
->set_voltage_time_sel(rdev
, old_selector
,
3716 if (old_uV
!= best_val
) {
3717 if (ops
->set_voltage_time
)
3718 delay
= ops
->set_voltage_time(rdev
, old_uV
,
3721 delay
= _regulator_set_voltage_time(rdev
,
3728 rdev_warn(rdev
, "failed to get delay: %pe\n", ERR_PTR(delay
));
3732 /* Insert any necessary delays */
3733 _regulator_delay_helper(delay
);
3735 if (best_val
>= 0) {
3736 unsigned long data
= best_val
;
3738 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
3743 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), best_val
);
3748 static int _regulator_do_set_suspend_voltage(struct regulator_dev
*rdev
,
3749 int min_uV
, int max_uV
, suspend_state_t state
)
3751 struct regulator_state
*rstate
;
3754 rstate
= regulator_get_suspend_state(rdev
, state
);
3758 if (min_uV
< rstate
->min_uV
)
3759 min_uV
= rstate
->min_uV
;
3760 if (max_uV
> rstate
->max_uV
)
3761 max_uV
= rstate
->max_uV
;
3763 sel
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3767 uV
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
3768 if (uV
>= min_uV
&& uV
<= max_uV
)
3774 static int regulator_set_voltage_unlocked(struct regulator
*regulator
,
3775 int min_uV
, int max_uV
,
3776 suspend_state_t state
)
3778 struct regulator_dev
*rdev
= regulator
->rdev
;
3779 struct regulator_voltage
*voltage
= ®ulator
->voltage
[state
];
3781 int old_min_uV
, old_max_uV
;
3784 /* If we're setting the same range as last time the change
3785 * should be a noop (some cpufreq implementations use the same
3786 * voltage for multiple frequencies, for example).
3788 if (voltage
->min_uV
== min_uV
&& voltage
->max_uV
== max_uV
)
3791 /* If we're trying to set a range that overlaps the current voltage,
3792 * return successfully even though the regulator does not support
3793 * changing the voltage.
3795 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
3796 current_uV
= regulator_get_voltage_rdev(rdev
);
3797 if (min_uV
<= current_uV
&& current_uV
<= max_uV
) {
3798 voltage
->min_uV
= min_uV
;
3799 voltage
->max_uV
= max_uV
;
3805 if (!rdev
->desc
->ops
->set_voltage
&&
3806 !rdev
->desc
->ops
->set_voltage_sel
) {
3811 /* constraints check */
3812 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
3816 /* restore original values in case of error */
3817 old_min_uV
= voltage
->min_uV
;
3818 old_max_uV
= voltage
->max_uV
;
3819 voltage
->min_uV
= min_uV
;
3820 voltage
->max_uV
= max_uV
;
3822 /* for not coupled regulators this will just set the voltage */
3823 ret
= regulator_balance_voltage(rdev
, state
);
3825 voltage
->min_uV
= old_min_uV
;
3826 voltage
->max_uV
= old_max_uV
;
3833 int regulator_set_voltage_rdev(struct regulator_dev
*rdev
, int min_uV
,
3834 int max_uV
, suspend_state_t state
)
3836 int best_supply_uV
= 0;
3837 int supply_change_uV
= 0;
3841 regulator_ops_is_valid(rdev
->supply
->rdev
,
3842 REGULATOR_CHANGE_VOLTAGE
) &&
3843 (rdev
->desc
->min_dropout_uV
|| !(rdev
->desc
->ops
->get_voltage
||
3844 rdev
->desc
->ops
->get_voltage_sel
))) {
3845 int current_supply_uV
;
3848 selector
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3854 best_supply_uV
= _regulator_list_voltage(rdev
, selector
, 0);
3855 if (best_supply_uV
< 0) {
3856 ret
= best_supply_uV
;
3860 best_supply_uV
+= rdev
->desc
->min_dropout_uV
;
3862 current_supply_uV
= regulator_get_voltage_rdev(rdev
->supply
->rdev
);
3863 if (current_supply_uV
< 0) {
3864 ret
= current_supply_uV
;
3868 supply_change_uV
= best_supply_uV
- current_supply_uV
;
3871 if (supply_change_uV
> 0) {
3872 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
3873 best_supply_uV
, INT_MAX
, state
);
3875 dev_err(&rdev
->dev
, "Failed to increase supply voltage: %pe\n",
3881 if (state
== PM_SUSPEND_ON
)
3882 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
3884 ret
= _regulator_do_set_suspend_voltage(rdev
, min_uV
,
3889 if (supply_change_uV
< 0) {
3890 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
3891 best_supply_uV
, INT_MAX
, state
);
3893 dev_warn(&rdev
->dev
, "Failed to decrease supply voltage: %pe\n",
3895 /* No need to fail here */
3902 EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev
);
3904 static int regulator_limit_voltage_step(struct regulator_dev
*rdev
,
3905 int *current_uV
, int *min_uV
)
3907 struct regulation_constraints
*constraints
= rdev
->constraints
;
3909 /* Limit voltage change only if necessary */
3910 if (!constraints
->max_uV_step
|| !_regulator_is_enabled(rdev
))
3913 if (*current_uV
< 0) {
3914 *current_uV
= regulator_get_voltage_rdev(rdev
);
3916 if (*current_uV
< 0)
3920 if (abs(*current_uV
- *min_uV
) <= constraints
->max_uV_step
)
3923 /* Clamp target voltage within the given step */
3924 if (*current_uV
< *min_uV
)
3925 *min_uV
= min(*current_uV
+ constraints
->max_uV_step
,
3928 *min_uV
= max(*current_uV
- constraints
->max_uV_step
,
3934 static int regulator_get_optimal_voltage(struct regulator_dev
*rdev
,
3936 int *min_uV
, int *max_uV
,
3937 suspend_state_t state
,
3940 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
3941 struct regulator_dev
**c_rdevs
= c_desc
->coupled_rdevs
;
3942 struct regulation_constraints
*constraints
= rdev
->constraints
;
3943 int desired_min_uV
= 0, desired_max_uV
= INT_MAX
;
3944 int max_current_uV
= 0, min_current_uV
= INT_MAX
;
3945 int highest_min_uV
= 0, target_uV
, possible_uV
;
3946 int i
, ret
, max_spread
;
3952 * If there are no coupled regulators, simply set the voltage
3953 * demanded by consumers.
3955 if (n_coupled
== 1) {
3957 * If consumers don't provide any demands, set voltage
3960 desired_min_uV
= constraints
->min_uV
;
3961 desired_max_uV
= constraints
->max_uV
;
3963 ret
= regulator_check_consumers(rdev
,
3965 &desired_max_uV
, state
);
3974 /* Find highest min desired voltage */
3975 for (i
= 0; i
< n_coupled
; i
++) {
3977 int tmp_max
= INT_MAX
;
3979 lockdep_assert_held_once(&c_rdevs
[i
]->mutex
.base
);
3981 ret
= regulator_check_consumers(c_rdevs
[i
],
3987 ret
= regulator_check_voltage(c_rdevs
[i
], &tmp_min
, &tmp_max
);
3991 highest_min_uV
= max(highest_min_uV
, tmp_min
);
3994 desired_min_uV
= tmp_min
;
3995 desired_max_uV
= tmp_max
;
3999 max_spread
= constraints
->max_spread
[0];
4002 * Let target_uV be equal to the desired one if possible.
4003 * If not, set it to minimum voltage, allowed by other coupled
4006 target_uV
= max(desired_min_uV
, highest_min_uV
- max_spread
);
4009 * Find min and max voltages, which currently aren't violating
4012 for (i
= 1; i
< n_coupled
; i
++) {
4015 if (!_regulator_is_enabled(c_rdevs
[i
]))
4018 tmp_act
= regulator_get_voltage_rdev(c_rdevs
[i
]);
4022 min_current_uV
= min(tmp_act
, min_current_uV
);
4023 max_current_uV
= max(tmp_act
, max_current_uV
);
4026 /* There aren't any other regulators enabled */
4027 if (max_current_uV
== 0) {
4028 possible_uV
= target_uV
;
4031 * Correct target voltage, so as it currently isn't
4032 * violating max_spread
4034 possible_uV
= max(target_uV
, max_current_uV
- max_spread
);
4035 possible_uV
= min(possible_uV
, min_current_uV
+ max_spread
);
4038 if (possible_uV
> desired_max_uV
)
4041 done
= (possible_uV
== target_uV
);
4042 desired_min_uV
= possible_uV
;
4045 /* Apply max_uV_step constraint if necessary */
4046 if (state
== PM_SUSPEND_ON
) {
4047 ret
= regulator_limit_voltage_step(rdev
, current_uV
,
4056 /* Set current_uV if wasn't done earlier in the code and if necessary */
4057 if (n_coupled
> 1 && *current_uV
== -1) {
4059 if (_regulator_is_enabled(rdev
)) {
4060 ret
= regulator_get_voltage_rdev(rdev
);
4066 *current_uV
= desired_min_uV
;
4070 *min_uV
= desired_min_uV
;
4071 *max_uV
= desired_max_uV
;
4076 int regulator_do_balance_voltage(struct regulator_dev
*rdev
,
4077 suspend_state_t state
, bool skip_coupled
)
4079 struct regulator_dev
**c_rdevs
;
4080 struct regulator_dev
*best_rdev
;
4081 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
4082 int i
, ret
, n_coupled
, best_min_uV
, best_max_uV
, best_c_rdev
;
4083 unsigned int delta
, best_delta
;
4084 unsigned long c_rdev_done
= 0;
4085 bool best_c_rdev_done
;
4087 c_rdevs
= c_desc
->coupled_rdevs
;
4088 n_coupled
= skip_coupled
? 1 : c_desc
->n_coupled
;
4091 * Find the best possible voltage change on each loop. Leave the loop
4092 * if there isn't any possible change.
4095 best_c_rdev_done
= false;
4103 * Find highest difference between optimal voltage
4104 * and current voltage.
4106 for (i
= 0; i
< n_coupled
; i
++) {
4108 * optimal_uV is the best voltage that can be set for
4109 * i-th regulator at the moment without violating
4110 * max_spread constraint in order to balance
4111 * the coupled voltages.
4113 int optimal_uV
= 0, optimal_max_uV
= 0, current_uV
= 0;
4115 if (test_bit(i
, &c_rdev_done
))
4118 ret
= regulator_get_optimal_voltage(c_rdevs
[i
],
4126 delta
= abs(optimal_uV
- current_uV
);
4128 if (delta
&& best_delta
<= delta
) {
4129 best_c_rdev_done
= ret
;
4131 best_rdev
= c_rdevs
[i
];
4132 best_min_uV
= optimal_uV
;
4133 best_max_uV
= optimal_max_uV
;
4138 /* Nothing to change, return successfully */
4144 ret
= regulator_set_voltage_rdev(best_rdev
, best_min_uV
,
4145 best_max_uV
, state
);
4150 if (best_c_rdev_done
)
4151 set_bit(best_c_rdev
, &c_rdev_done
);
4153 } while (n_coupled
> 1);
4159 static int regulator_balance_voltage(struct regulator_dev
*rdev
,
4160 suspend_state_t state
)
4162 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
4163 struct regulator_coupler
*coupler
= c_desc
->coupler
;
4164 bool skip_coupled
= false;
4167 * If system is in a state other than PM_SUSPEND_ON, don't check
4168 * other coupled regulators.
4170 if (state
!= PM_SUSPEND_ON
)
4171 skip_coupled
= true;
4173 if (c_desc
->n_resolved
< c_desc
->n_coupled
) {
4174 rdev_err(rdev
, "Not all coupled regulators registered\n");
4178 /* Invoke custom balancer for customized couplers */
4179 if (coupler
&& coupler
->balance_voltage
)
4180 return coupler
->balance_voltage(coupler
, rdev
, state
);
4182 return regulator_do_balance_voltage(rdev
, state
, skip_coupled
);
4186 * regulator_set_voltage - set regulator output voltage
4187 * @regulator: regulator source
4188 * @min_uV: Minimum required voltage in uV
4189 * @max_uV: Maximum acceptable voltage in uV
4191 * Sets a voltage regulator to the desired output voltage. This can be set
4192 * during any regulator state. IOW, regulator can be disabled or enabled.
4194 * If the regulator is enabled then the voltage will change to the new value
4195 * immediately otherwise if the regulator is disabled the regulator will
4196 * output at the new voltage when enabled.
4198 * NOTE: If the regulator is shared between several devices then the lowest
4199 * request voltage that meets the system constraints will be used.
4200 * Regulator system constraints must be set for this regulator before
4201 * calling this function otherwise this call will fail.
4203 * Return: 0 on success or a negative error number on failure.
4205 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
4207 struct ww_acquire_ctx ww_ctx
;
4210 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
4212 ret
= regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
,
4215 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
4219 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
4221 static inline int regulator_suspend_toggle(struct regulator_dev
*rdev
,
4222 suspend_state_t state
, bool en
)
4224 struct regulator_state
*rstate
;
4226 rstate
= regulator_get_suspend_state(rdev
, state
);
4230 if (!rstate
->changeable
)
4233 rstate
->enabled
= (en
) ? ENABLE_IN_SUSPEND
: DISABLE_IN_SUSPEND
;
4238 int regulator_suspend_enable(struct regulator_dev
*rdev
,
4239 suspend_state_t state
)
4241 return regulator_suspend_toggle(rdev
, state
, true);
4243 EXPORT_SYMBOL_GPL(regulator_suspend_enable
);
4245 int regulator_suspend_disable(struct regulator_dev
*rdev
,
4246 suspend_state_t state
)
4248 struct regulator
*regulator
;
4249 struct regulator_voltage
*voltage
;
4252 * if any consumer wants this regulator device keeping on in
4253 * suspend states, don't set it as disabled.
4255 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
4256 voltage
= ®ulator
->voltage
[state
];
4257 if (voltage
->min_uV
|| voltage
->max_uV
)
4261 return regulator_suspend_toggle(rdev
, state
, false);
4263 EXPORT_SYMBOL_GPL(regulator_suspend_disable
);
4265 static int _regulator_set_suspend_voltage(struct regulator
*regulator
,
4266 int min_uV
, int max_uV
,
4267 suspend_state_t state
)
4269 struct regulator_dev
*rdev
= regulator
->rdev
;
4270 struct regulator_state
*rstate
;
4272 rstate
= regulator_get_suspend_state(rdev
, state
);
4276 if (rstate
->min_uV
== rstate
->max_uV
) {
4277 rdev_err(rdev
, "The suspend voltage can't be changed!\n");
4281 return regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
, state
);
4284 int regulator_set_suspend_voltage(struct regulator
*regulator
, int min_uV
,
4285 int max_uV
, suspend_state_t state
)
4287 struct ww_acquire_ctx ww_ctx
;
4290 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
4291 if (regulator_check_states(state
) || state
== PM_SUSPEND_ON
)
4294 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
4296 ret
= _regulator_set_suspend_voltage(regulator
, min_uV
,
4299 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
4303 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage
);
4306 * regulator_set_voltage_time - get raise/fall time
4307 * @regulator: regulator source
4308 * @old_uV: starting voltage in microvolts
4309 * @new_uV: target voltage in microvolts
4311 * Provided with the starting and ending voltage, this function attempts to
4312 * calculate the time in microseconds required to rise or fall to this new
4315 * Return: ramp time in microseconds, or a negative error number if calculation failed.
4317 int regulator_set_voltage_time(struct regulator
*regulator
,
4318 int old_uV
, int new_uV
)
4320 struct regulator_dev
*rdev
= regulator
->rdev
;
4321 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
4327 if (ops
->set_voltage_time
)
4328 return ops
->set_voltage_time(rdev
, old_uV
, new_uV
);
4329 else if (!ops
->set_voltage_time_sel
)
4330 return _regulator_set_voltage_time(rdev
, old_uV
, new_uV
);
4332 /* Currently requires operations to do this */
4333 if (!ops
->list_voltage
|| !rdev
->desc
->n_voltages
)
4336 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
4337 /* We only look for exact voltage matches here */
4338 if (i
< rdev
->desc
->linear_min_sel
)
4341 if (old_sel
>= 0 && new_sel
>= 0)
4344 voltage
= regulator_list_voltage(regulator
, i
);
4349 if (voltage
== old_uV
)
4351 if (voltage
== new_uV
)
4355 if (old_sel
< 0 || new_sel
< 0)
4358 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
4360 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
4363 * regulator_set_voltage_time_sel - get raise/fall time
4364 * @rdev: regulator source device
4365 * @old_selector: selector for starting voltage
4366 * @new_selector: selector for target voltage
4368 * Provided with the starting and target voltage selectors, this function
4369 * returns time in microseconds required to rise or fall to this new voltage
4371 * Drivers providing ramp_delay in regulation_constraints can use this as their
4372 * set_voltage_time_sel() operation.
4374 * Return: ramp time in microseconds, or a negative error number if calculation failed.
4376 int regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
4377 unsigned int old_selector
,
4378 unsigned int new_selector
)
4380 int old_volt
, new_volt
;
4383 if (!rdev
->desc
->ops
->list_voltage
)
4386 old_volt
= rdev
->desc
->ops
->list_voltage(rdev
, old_selector
);
4387 new_volt
= rdev
->desc
->ops
->list_voltage(rdev
, new_selector
);
4389 if (rdev
->desc
->ops
->set_voltage_time
)
4390 return rdev
->desc
->ops
->set_voltage_time(rdev
, old_volt
,
4393 return _regulator_set_voltage_time(rdev
, old_volt
, new_volt
);
4395 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel
);
4397 int regulator_sync_voltage_rdev(struct regulator_dev
*rdev
)
4401 regulator_lock(rdev
);
4403 if (!rdev
->desc
->ops
->set_voltage
&&
4404 !rdev
->desc
->ops
->set_voltage_sel
) {
4409 /* balance only, if regulator is coupled */
4410 if (rdev
->coupling_desc
.n_coupled
> 1)
4411 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
4416 regulator_unlock(rdev
);
4421 * regulator_sync_voltage - re-apply last regulator output voltage
4422 * @regulator: regulator source
4424 * Re-apply the last configured voltage. This is intended to be used
4425 * where some external control source the consumer is cooperating with
4426 * has caused the configured voltage to change.
4428 * Return: 0 on success or a negative error number on failure.
4430 int regulator_sync_voltage(struct regulator
*regulator
)
4432 struct regulator_dev
*rdev
= regulator
->rdev
;
4433 struct regulator_voltage
*voltage
= ®ulator
->voltage
[PM_SUSPEND_ON
];
4434 int ret
, min_uV
, max_uV
;
4436 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
))
4439 regulator_lock(rdev
);
4441 if (!rdev
->desc
->ops
->set_voltage
&&
4442 !rdev
->desc
->ops
->set_voltage_sel
) {
4447 /* This is only going to work if we've had a voltage configured. */
4448 if (!voltage
->min_uV
&& !voltage
->max_uV
) {
4453 min_uV
= voltage
->min_uV
;
4454 max_uV
= voltage
->max_uV
;
4456 /* This should be a paranoia check... */
4457 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
4461 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
, 0);
4465 /* balance only, if regulator is coupled */
4466 if (rdev
->coupling_desc
.n_coupled
> 1)
4467 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
4469 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
4472 regulator_unlock(rdev
);
4475 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
4477 int regulator_get_voltage_rdev(struct regulator_dev
*rdev
)
4482 if (rdev
->desc
->ops
->get_bypass
) {
4483 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypassed
);
4487 /* if bypassed the regulator must have a supply */
4488 if (!rdev
->supply
) {
4490 "bypassed regulator has no supply!\n");
4491 return -EPROBE_DEFER
;
4494 return regulator_get_voltage_rdev(rdev
->supply
->rdev
);
4498 if (rdev
->desc
->ops
->get_voltage_sel
) {
4499 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
4502 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
4503 } else if (rdev
->desc
->ops
->get_voltage
) {
4504 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
4505 } else if (rdev
->desc
->ops
->list_voltage
) {
4506 ret
= rdev
->desc
->ops
->list_voltage(rdev
, 0);
4507 } else if (rdev
->desc
->fixed_uV
&& (rdev
->desc
->n_voltages
== 1)) {
4508 ret
= rdev
->desc
->fixed_uV
;
4509 } else if (rdev
->supply
) {
4510 ret
= regulator_get_voltage_rdev(rdev
->supply
->rdev
);
4511 } else if (rdev
->supply_name
) {
4512 return -EPROBE_DEFER
;
4519 return ret
- rdev
->constraints
->uV_offset
;
4521 EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev
);
4524 * regulator_get_voltage - get regulator output voltage
4525 * @regulator: regulator source
4527 * Return: Current regulator voltage in uV, or a negative error number on failure.
4529 * NOTE: If the regulator is disabled it will return the voltage value. This
4530 * function should not be used to determine regulator state.
4532 int regulator_get_voltage(struct regulator
*regulator
)
4534 struct ww_acquire_ctx ww_ctx
;
4537 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
4538 ret
= regulator_get_voltage_rdev(regulator
->rdev
);
4539 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
4543 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
4546 * regulator_set_current_limit - set regulator output current limit
4547 * @regulator: regulator source
4548 * @min_uA: Minimum supported current in uA
4549 * @max_uA: Maximum supported current in uA
4551 * Sets current sink to the desired output current. This can be set during
4552 * any regulator state. IOW, regulator can be disabled or enabled.
4554 * If the regulator is enabled then the current will change to the new value
4555 * immediately otherwise if the regulator is disabled the regulator will
4556 * output at the new current when enabled.
4558 * NOTE: Regulator system constraints must be set for this regulator before
4559 * calling this function otherwise this call will fail.
4561 * Return: 0 on success or a negative error number on failure.
4563 int regulator_set_current_limit(struct regulator
*regulator
,
4564 int min_uA
, int max_uA
)
4566 struct regulator_dev
*rdev
= regulator
->rdev
;
4569 regulator_lock(rdev
);
4572 if (!rdev
->desc
->ops
->set_current_limit
) {
4577 /* constraints check */
4578 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
4582 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
4584 regulator_unlock(rdev
);
4587 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
4589 static int _regulator_get_current_limit_unlocked(struct regulator_dev
*rdev
)
4592 if (!rdev
->desc
->ops
->get_current_limit
)
4595 return rdev
->desc
->ops
->get_current_limit(rdev
);
4598 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
4602 regulator_lock(rdev
);
4603 ret
= _regulator_get_current_limit_unlocked(rdev
);
4604 regulator_unlock(rdev
);
4610 * regulator_get_current_limit - get regulator output current
4611 * @regulator: regulator source
4613 * Return: Current supplied by the specified current sink in uA,
4614 * or a negative error number on failure.
4616 * NOTE: If the regulator is disabled it will return the current value. This
4617 * function should not be used to determine regulator state.
4619 int regulator_get_current_limit(struct regulator
*regulator
)
4621 return _regulator_get_current_limit(regulator
->rdev
);
4623 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
4626 * regulator_set_mode - set regulator operating mode
4627 * @regulator: regulator source
4628 * @mode: operating mode - one of the REGULATOR_MODE constants
4630 * Set regulator operating mode to increase regulator efficiency or improve
4631 * regulation performance.
4633 * NOTE: Regulator system constraints must be set for this regulator before
4634 * calling this function otherwise this call will fail.
4636 * Return: 0 on success or a negative error number on failure.
4638 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
4640 struct regulator_dev
*rdev
= regulator
->rdev
;
4642 int regulator_curr_mode
;
4644 regulator_lock(rdev
);
4647 if (!rdev
->desc
->ops
->set_mode
) {
4652 /* return if the same mode is requested */
4653 if (rdev
->desc
->ops
->get_mode
) {
4654 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
4655 if (regulator_curr_mode
== mode
) {
4661 /* constraints check */
4662 ret
= regulator_mode_constrain(rdev
, &mode
);
4666 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
4668 regulator_unlock(rdev
);
4671 EXPORT_SYMBOL_GPL(regulator_set_mode
);
4673 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev
*rdev
)
4676 if (!rdev
->desc
->ops
->get_mode
)
4679 return rdev
->desc
->ops
->get_mode(rdev
);
4682 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
4686 regulator_lock(rdev
);
4687 ret
= _regulator_get_mode_unlocked(rdev
);
4688 regulator_unlock(rdev
);
4694 * regulator_get_mode - get regulator operating mode
4695 * @regulator: regulator source
4697 * Get the current regulator operating mode.
4699 * Return: Current operating mode as %REGULATOR_MODE_* values,
4700 * or a negative error number on failure.
4702 unsigned int regulator_get_mode(struct regulator
*regulator
)
4704 return _regulator_get_mode(regulator
->rdev
);
4706 EXPORT_SYMBOL_GPL(regulator_get_mode
);
4708 static int rdev_get_cached_err_flags(struct regulator_dev
*rdev
)
4712 if (rdev
->use_cached_err
) {
4713 spin_lock(&rdev
->err_lock
);
4714 ret
= rdev
->cached_err
;
4715 spin_unlock(&rdev
->err_lock
);
4720 static int _regulator_get_error_flags(struct regulator_dev
*rdev
,
4721 unsigned int *flags
)
4723 int cached_flags
, ret
= 0;
4725 regulator_lock(rdev
);
4727 cached_flags
= rdev_get_cached_err_flags(rdev
);
4729 if (rdev
->desc
->ops
->get_error_flags
)
4730 ret
= rdev
->desc
->ops
->get_error_flags(rdev
, flags
);
4731 else if (!rdev
->use_cached_err
)
4734 *flags
|= cached_flags
;
4736 regulator_unlock(rdev
);
4742 * regulator_get_error_flags - get regulator error information
4743 * @regulator: regulator source
4744 * @flags: pointer to store error flags
4746 * Get the current regulator error information.
4748 * Return: 0 on success or a negative error number on failure.
4750 int regulator_get_error_flags(struct regulator
*regulator
,
4751 unsigned int *flags
)
4753 return _regulator_get_error_flags(regulator
->rdev
, flags
);
4755 EXPORT_SYMBOL_GPL(regulator_get_error_flags
);
4758 * regulator_set_load - set regulator load
4759 * @regulator: regulator source
4760 * @uA_load: load current
4762 * Notifies the regulator core of a new device load. This is then used by
4763 * DRMS (if enabled by constraints) to set the most efficient regulator
4764 * operating mode for the new regulator loading.
4766 * Consumer devices notify their supply regulator of the maximum power
4767 * they will require (can be taken from device datasheet in the power
4768 * consumption tables) when they change operational status and hence power
4769 * state. Examples of operational state changes that can affect power
4770 * consumption are :-
4772 * o Device is opened / closed.
4773 * o Device I/O is about to begin or has just finished.
4774 * o Device is idling in between work.
4776 * This information is also exported via sysfs to userspace.
4778 * DRMS will sum the total requested load on the regulator and change
4779 * to the most efficient operating mode if platform constraints allow.
4781 * NOTE: when a regulator consumer requests to have a regulator
4782 * disabled then any load that consumer requested no longer counts
4783 * toward the total requested load. If the regulator is re-enabled
4784 * then the previously requested load will start counting again.
4786 * If a regulator is an always-on regulator then an individual consumer's
4787 * load will still be removed if that consumer is fully disabled.
4789 * Return: 0 on success or a negative error number on failure.
4791 int regulator_set_load(struct regulator
*regulator
, int uA_load
)
4793 struct regulator_dev
*rdev
= regulator
->rdev
;
4797 regulator_lock(rdev
);
4798 old_uA_load
= regulator
->uA_load
;
4799 regulator
->uA_load
= uA_load
;
4800 if (regulator
->enable_count
&& old_uA_load
!= uA_load
) {
4801 ret
= drms_uA_update(rdev
);
4803 regulator
->uA_load
= old_uA_load
;
4805 regulator_unlock(rdev
);
4809 EXPORT_SYMBOL_GPL(regulator_set_load
);
4812 * regulator_allow_bypass - allow the regulator to go into bypass mode
4814 * @regulator: Regulator to configure
4815 * @enable: enable or disable bypass mode
4817 * Allow the regulator to go into bypass mode if all other consumers
4818 * for the regulator also enable bypass mode and the machine
4819 * constraints allow this. Bypass mode means that the regulator is
4820 * simply passing the input directly to the output with no regulation.
4822 * Return: 0 on success or if changing bypass is not possible, or
4823 * a negative error number on failure.
4825 int regulator_allow_bypass(struct regulator
*regulator
, bool enable
)
4827 struct regulator_dev
*rdev
= regulator
->rdev
;
4828 const char *name
= rdev_get_name(rdev
);
4831 if (!rdev
->desc
->ops
->set_bypass
)
4834 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_BYPASS
))
4837 regulator_lock(rdev
);
4839 if (enable
&& !regulator
->bypass
) {
4840 rdev
->bypass_count
++;
4842 if (rdev
->bypass_count
== rdev
->open_count
) {
4843 trace_regulator_bypass_enable(name
);
4845 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
4847 rdev
->bypass_count
--;
4849 trace_regulator_bypass_enable_complete(name
);
4852 } else if (!enable
&& regulator
->bypass
) {
4853 rdev
->bypass_count
--;
4855 if (rdev
->bypass_count
!= rdev
->open_count
) {
4856 trace_regulator_bypass_disable(name
);
4858 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
4860 rdev
->bypass_count
++;
4862 trace_regulator_bypass_disable_complete(name
);
4867 regulator
->bypass
= enable
;
4869 regulator_unlock(rdev
);
4873 EXPORT_SYMBOL_GPL(regulator_allow_bypass
);
4876 * regulator_register_notifier - register regulator event notifier
4877 * @regulator: regulator source
4878 * @nb: notifier block
4880 * Register notifier block to receive regulator events.
4882 * Return: 0 on success or a negative error number on failure.
4884 int regulator_register_notifier(struct regulator
*regulator
,
4885 struct notifier_block
*nb
)
4887 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
4890 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
4893 * regulator_unregister_notifier - unregister regulator event notifier
4894 * @regulator: regulator source
4895 * @nb: notifier block
4897 * Unregister regulator event notifier block.
4899 * Return: 0 on success or a negative error number on failure.
4901 int regulator_unregister_notifier(struct regulator
*regulator
,
4902 struct notifier_block
*nb
)
4904 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
4907 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
4909 /* notify regulator consumers and downstream regulator consumers.
4910 * Note mutex must be held by caller.
4912 static int _notifier_call_chain(struct regulator_dev
*rdev
,
4913 unsigned long event
, void *data
)
4915 /* call rdev chain first */
4916 int ret
= blocking_notifier_call_chain(&rdev
->notifier
, event
, data
);
4918 if (IS_REACHABLE(CONFIG_REGULATOR_NETLINK_EVENTS
)) {
4919 struct device
*parent
= rdev
->dev
.parent
;
4920 const char *rname
= rdev_get_name(rdev
);
4923 /* Avoid duplicate debugfs directory names */
4924 if (parent
&& rname
== rdev
->desc
->name
) {
4925 snprintf(name
, sizeof(name
), "%s-%s", dev_name(parent
),
4929 reg_generate_netlink_event(rname
, event
);
4935 int _regulator_bulk_get(struct device
*dev
, int num_consumers
,
4936 struct regulator_bulk_data
*consumers
, enum regulator_get_type get_type
)
4941 for (i
= 0; i
< num_consumers
; i
++)
4942 consumers
[i
].consumer
= NULL
;
4944 for (i
= 0; i
< num_consumers
; i
++) {
4945 consumers
[i
].consumer
= _regulator_get(dev
,
4946 consumers
[i
].supply
, get_type
);
4947 if (IS_ERR(consumers
[i
].consumer
)) {
4948 ret
= dev_err_probe(dev
, PTR_ERR(consumers
[i
].consumer
),
4949 "Failed to get supply '%s'",
4950 consumers
[i
].supply
);
4951 consumers
[i
].consumer
= NULL
;
4955 if (consumers
[i
].init_load_uA
> 0) {
4956 ret
= regulator_set_load(consumers
[i
].consumer
,
4957 consumers
[i
].init_load_uA
);
4969 regulator_put(consumers
[i
].consumer
);
4975 * regulator_bulk_get - get multiple regulator consumers
4977 * @dev: Device to supply
4978 * @num_consumers: Number of consumers to register
4979 * @consumers: Configuration of consumers; clients are stored here.
4981 * This helper function allows drivers to get several regulator
4982 * consumers in one operation. If any of the regulators cannot be
4983 * acquired then any regulators that were allocated will be freed
4984 * before returning to the caller.
4986 * Return: 0 on success or a negative error number on failure.
4988 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
4989 struct regulator_bulk_data
*consumers
)
4991 return _regulator_bulk_get(dev
, num_consumers
, consumers
, NORMAL_GET
);
4993 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
4995 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
4997 struct regulator_bulk_data
*bulk
= data
;
4999 bulk
->ret
= regulator_enable(bulk
->consumer
);
5003 * regulator_bulk_enable - enable multiple regulator consumers
5005 * @num_consumers: Number of consumers
5006 * @consumers: Consumer data; clients are stored here.
5008 * This convenience API allows consumers to enable multiple regulator
5009 * clients in a single API call. If any consumers cannot be enabled
5010 * then any others that were enabled will be disabled again prior to
5013 * Return: 0 on success or a negative error number on failure.
5015 int regulator_bulk_enable(int num_consumers
,
5016 struct regulator_bulk_data
*consumers
)
5018 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
5022 for (i
= 0; i
< num_consumers
; i
++) {
5023 async_schedule_domain(regulator_bulk_enable_async
,
5024 &consumers
[i
], &async_domain
);
5027 async_synchronize_full_domain(&async_domain
);
5029 /* If any consumer failed we need to unwind any that succeeded */
5030 for (i
= 0; i
< num_consumers
; i
++) {
5031 if (consumers
[i
].ret
!= 0) {
5032 ret
= consumers
[i
].ret
;
5040 for (i
= 0; i
< num_consumers
; i
++) {
5041 if (consumers
[i
].ret
< 0)
5042 pr_err("Failed to enable %s: %pe\n", consumers
[i
].supply
,
5043 ERR_PTR(consumers
[i
].ret
));
5045 regulator_disable(consumers
[i
].consumer
);
5050 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
5053 * regulator_bulk_disable - disable multiple regulator consumers
5055 * @num_consumers: Number of consumers
5056 * @consumers: Consumer data; clients are stored here.
5058 * This convenience API allows consumers to disable multiple regulator
5059 * clients in a single API call. If any consumers cannot be disabled
5060 * then any others that were disabled will be enabled again prior to
5063 * Return: 0 on success or a negative error number on failure.
5065 int regulator_bulk_disable(int num_consumers
,
5066 struct regulator_bulk_data
*consumers
)
5071 for (i
= num_consumers
- 1; i
>= 0; --i
) {
5072 ret
= regulator_disable(consumers
[i
].consumer
);
5080 pr_err("Failed to disable %s: %pe\n", consumers
[i
].supply
, ERR_PTR(ret
));
5081 for (++i
; i
< num_consumers
; ++i
) {
5082 r
= regulator_enable(consumers
[i
].consumer
);
5084 pr_err("Failed to re-enable %s: %pe\n",
5085 consumers
[i
].supply
, ERR_PTR(r
));
5090 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
5093 * regulator_bulk_force_disable - force disable multiple regulator consumers
5095 * @num_consumers: Number of consumers
5096 * @consumers: Consumer data; clients are stored here.
5098 * This convenience API allows consumers to forcibly disable multiple regulator
5099 * clients in a single API call.
5100 * NOTE: This should be used for situations when device damage will
5101 * likely occur if the regulators are not disabled (e.g. over temp).
5102 * Although regulator_force_disable function call for some consumers can
5103 * return error numbers, the function is called for all consumers.
5105 * Return: 0 on success or a negative error number on failure.
5107 int regulator_bulk_force_disable(int num_consumers
,
5108 struct regulator_bulk_data
*consumers
)
5113 for (i
= 0; i
< num_consumers
; i
++) {
5115 regulator_force_disable(consumers
[i
].consumer
);
5117 /* Store first error for reporting */
5118 if (consumers
[i
].ret
&& !ret
)
5119 ret
= consumers
[i
].ret
;
5124 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
5127 * regulator_bulk_free - free multiple regulator consumers
5129 * @num_consumers: Number of consumers
5130 * @consumers: Consumer data; clients are stored here.
5132 * This convenience API allows consumers to free multiple regulator
5133 * clients in a single API call.
5135 void regulator_bulk_free(int num_consumers
,
5136 struct regulator_bulk_data
*consumers
)
5140 for (i
= 0; i
< num_consumers
; i
++) {
5141 regulator_put(consumers
[i
].consumer
);
5142 consumers
[i
].consumer
= NULL
;
5145 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
5148 * regulator_handle_critical - Handle events for system-critical regulators.
5149 * @rdev: The regulator device.
5150 * @event: The event being handled.
5152 * This function handles critical events such as under-voltage, over-current,
5153 * and unknown errors for regulators deemed system-critical. On detecting such
5154 * events, it triggers a hardware protection shutdown with a defined timeout.
5156 static void regulator_handle_critical(struct regulator_dev
*rdev
,
5157 unsigned long event
)
5159 const char *reason
= NULL
;
5161 if (!rdev
->constraints
->system_critical
)
5165 case REGULATOR_EVENT_UNDER_VOLTAGE
:
5166 reason
= "System critical regulator: voltage drop detected";
5168 case REGULATOR_EVENT_OVER_CURRENT
:
5169 reason
= "System critical regulator: over-current detected";
5171 case REGULATOR_EVENT_FAIL
:
5172 reason
= "System critical regulator: unknown error";
5178 hw_protection_shutdown(reason
,
5179 rdev
->constraints
->uv_less_critical_window_ms
);
5183 * regulator_notifier_call_chain - call regulator event notifier
5184 * @rdev: regulator source
5185 * @event: notifier block
5186 * @data: callback-specific data.
5188 * Called by regulator drivers to notify clients a regulator event has
5191 * Return: %NOTIFY_DONE.
5193 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
5194 unsigned long event
, void *data
)
5196 regulator_handle_critical(rdev
, event
);
5198 _notifier_call_chain(rdev
, event
, data
);
5202 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
5205 * regulator_mode_to_status - convert a regulator mode into a status
5207 * @mode: Mode to convert
5209 * Convert a regulator mode into a status.
5211 * Return: %REGULATOR_STATUS_* value corresponding to given mode.
5213 int regulator_mode_to_status(unsigned int mode
)
5216 case REGULATOR_MODE_FAST
:
5217 return REGULATOR_STATUS_FAST
;
5218 case REGULATOR_MODE_NORMAL
:
5219 return REGULATOR_STATUS_NORMAL
;
5220 case REGULATOR_MODE_IDLE
:
5221 return REGULATOR_STATUS_IDLE
;
5222 case REGULATOR_MODE_STANDBY
:
5223 return REGULATOR_STATUS_STANDBY
;
5225 return REGULATOR_STATUS_UNDEFINED
;
5228 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
5230 static struct attribute
*regulator_dev_attrs
[] = {
5231 &dev_attr_name
.attr
,
5232 &dev_attr_num_users
.attr
,
5233 &dev_attr_type
.attr
,
5234 &dev_attr_microvolts
.attr
,
5235 &dev_attr_microamps
.attr
,
5236 &dev_attr_opmode
.attr
,
5237 &dev_attr_state
.attr
,
5238 &dev_attr_status
.attr
,
5239 &dev_attr_bypass
.attr
,
5240 &dev_attr_requested_microamps
.attr
,
5241 &dev_attr_min_microvolts
.attr
,
5242 &dev_attr_max_microvolts
.attr
,
5243 &dev_attr_min_microamps
.attr
,
5244 &dev_attr_max_microamps
.attr
,
5245 &dev_attr_under_voltage
.attr
,
5246 &dev_attr_over_current
.attr
,
5247 &dev_attr_regulation_out
.attr
,
5248 &dev_attr_fail
.attr
,
5249 &dev_attr_over_temp
.attr
,
5250 &dev_attr_under_voltage_warn
.attr
,
5251 &dev_attr_over_current_warn
.attr
,
5252 &dev_attr_over_voltage_warn
.attr
,
5253 &dev_attr_over_temp_warn
.attr
,
5254 &dev_attr_suspend_standby_state
.attr
,
5255 &dev_attr_suspend_mem_state
.attr
,
5256 &dev_attr_suspend_disk_state
.attr
,
5257 &dev_attr_suspend_standby_microvolts
.attr
,
5258 &dev_attr_suspend_mem_microvolts
.attr
,
5259 &dev_attr_suspend_disk_microvolts
.attr
,
5260 &dev_attr_suspend_standby_mode
.attr
,
5261 &dev_attr_suspend_mem_mode
.attr
,
5262 &dev_attr_suspend_disk_mode
.attr
,
5267 * To avoid cluttering sysfs (and memory) with useless state, only
5268 * create attributes that can be meaningfully displayed.
5270 static umode_t
regulator_attr_is_visible(struct kobject
*kobj
,
5271 struct attribute
*attr
, int idx
)
5273 struct device
*dev
= kobj_to_dev(kobj
);
5274 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5275 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
5276 umode_t mode
= attr
->mode
;
5278 /* these three are always present */
5279 if (attr
== &dev_attr_name
.attr
||
5280 attr
== &dev_attr_num_users
.attr
||
5281 attr
== &dev_attr_type
.attr
)
5284 /* some attributes need specific methods to be displayed */
5285 if (attr
== &dev_attr_microvolts
.attr
) {
5286 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
5287 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0) ||
5288 (ops
->list_voltage
&& ops
->list_voltage(rdev
, 0) >= 0) ||
5289 (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1))
5294 if (attr
== &dev_attr_microamps
.attr
)
5295 return ops
->get_current_limit
? mode
: 0;
5297 if (attr
== &dev_attr_opmode
.attr
)
5298 return ops
->get_mode
? mode
: 0;
5300 if (attr
== &dev_attr_state
.attr
)
5301 return (rdev
->ena_pin
|| ops
->is_enabled
) ? mode
: 0;
5303 if (attr
== &dev_attr_status
.attr
)
5304 return ops
->get_status
? mode
: 0;
5306 if (attr
== &dev_attr_bypass
.attr
)
5307 return ops
->get_bypass
? mode
: 0;
5309 if (attr
== &dev_attr_under_voltage
.attr
||
5310 attr
== &dev_attr_over_current
.attr
||
5311 attr
== &dev_attr_regulation_out
.attr
||
5312 attr
== &dev_attr_fail
.attr
||
5313 attr
== &dev_attr_over_temp
.attr
||
5314 attr
== &dev_attr_under_voltage_warn
.attr
||
5315 attr
== &dev_attr_over_current_warn
.attr
||
5316 attr
== &dev_attr_over_voltage_warn
.attr
||
5317 attr
== &dev_attr_over_temp_warn
.attr
)
5318 return ops
->get_error_flags
? mode
: 0;
5320 /* constraints need specific supporting methods */
5321 if (attr
== &dev_attr_min_microvolts
.attr
||
5322 attr
== &dev_attr_max_microvolts
.attr
)
5323 return (ops
->set_voltage
|| ops
->set_voltage_sel
) ? mode
: 0;
5325 if (attr
== &dev_attr_min_microamps
.attr
||
5326 attr
== &dev_attr_max_microamps
.attr
)
5327 return ops
->set_current_limit
? mode
: 0;
5329 if (attr
== &dev_attr_suspend_standby_state
.attr
||
5330 attr
== &dev_attr_suspend_mem_state
.attr
||
5331 attr
== &dev_attr_suspend_disk_state
.attr
)
5334 if (attr
== &dev_attr_suspend_standby_microvolts
.attr
||
5335 attr
== &dev_attr_suspend_mem_microvolts
.attr
||
5336 attr
== &dev_attr_suspend_disk_microvolts
.attr
)
5337 return ops
->set_suspend_voltage
? mode
: 0;
5339 if (attr
== &dev_attr_suspend_standby_mode
.attr
||
5340 attr
== &dev_attr_suspend_mem_mode
.attr
||
5341 attr
== &dev_attr_suspend_disk_mode
.attr
)
5342 return ops
->set_suspend_mode
? mode
: 0;
5347 static const struct attribute_group regulator_dev_group
= {
5348 .attrs
= regulator_dev_attrs
,
5349 .is_visible
= regulator_attr_is_visible
,
5352 static const struct attribute_group
*regulator_dev_groups
[] = {
5353 ®ulator_dev_group
,
5357 static void regulator_dev_release(struct device
*dev
)
5359 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
5361 debugfs_remove_recursive(rdev
->debugfs
);
5362 kfree(rdev
->constraints
);
5363 of_node_put(rdev
->dev
.of_node
);
5367 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
5369 struct device
*parent
= rdev
->dev
.parent
;
5370 const char *rname
= rdev_get_name(rdev
);
5371 char name
[NAME_MAX
];
5373 /* Avoid duplicate debugfs directory names */
5374 if (parent
&& rname
== rdev
->desc
->name
) {
5375 snprintf(name
, sizeof(name
), "%s-%s", dev_name(parent
),
5380 rdev
->debugfs
= debugfs_create_dir(rname
, debugfs_root
);
5381 if (IS_ERR(rdev
->debugfs
))
5382 rdev_dbg(rdev
, "Failed to create debugfs directory\n");
5384 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
5386 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
5388 debugfs_create_u32("bypass_count", 0444, rdev
->debugfs
,
5389 &rdev
->bypass_count
);
5392 static int regulator_register_resolve_supply(struct device
*dev
, void *data
)
5394 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5396 if (regulator_resolve_supply(rdev
))
5397 rdev_dbg(rdev
, "unable to resolve supply\n");
5402 int regulator_coupler_register(struct regulator_coupler
*coupler
)
5404 mutex_lock(®ulator_list_mutex
);
5405 list_add_tail(&coupler
->list
, ®ulator_coupler_list
);
5406 mutex_unlock(®ulator_list_mutex
);
5411 static struct regulator_coupler
*
5412 regulator_find_coupler(struct regulator_dev
*rdev
)
5414 struct regulator_coupler
*coupler
;
5418 * Note that regulators are appended to the list and the generic
5419 * coupler is registered first, hence it will be attached at last
5422 list_for_each_entry_reverse(coupler
, ®ulator_coupler_list
, list
) {
5423 err
= coupler
->attach_regulator(coupler
, rdev
);
5425 if (!coupler
->balance_voltage
&&
5426 rdev
->coupling_desc
.n_coupled
> 2)
5427 goto err_unsupported
;
5433 return ERR_PTR(err
);
5441 return ERR_PTR(-EINVAL
);
5444 if (coupler
->detach_regulator
)
5445 coupler
->detach_regulator(coupler
, rdev
);
5448 "Voltage balancing for multiple regulator couples is unimplemented\n");
5450 return ERR_PTR(-EPERM
);
5453 static void regulator_resolve_coupling(struct regulator_dev
*rdev
)
5455 struct regulator_coupler
*coupler
= rdev
->coupling_desc
.coupler
;
5456 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
5457 int n_coupled
= c_desc
->n_coupled
;
5458 struct regulator_dev
*c_rdev
;
5461 for (i
= 1; i
< n_coupled
; i
++) {
5462 /* already resolved */
5463 if (c_desc
->coupled_rdevs
[i
])
5466 c_rdev
= of_parse_coupled_regulator(rdev
, i
- 1);
5471 if (c_rdev
->coupling_desc
.coupler
!= coupler
) {
5472 rdev_err(rdev
, "coupler mismatch with %s\n",
5473 rdev_get_name(c_rdev
));
5477 c_desc
->coupled_rdevs
[i
] = c_rdev
;
5478 c_desc
->n_resolved
++;
5480 regulator_resolve_coupling(c_rdev
);
5484 static void regulator_remove_coupling(struct regulator_dev
*rdev
)
5486 struct regulator_coupler
*coupler
= rdev
->coupling_desc
.coupler
;
5487 struct coupling_desc
*__c_desc
, *c_desc
= &rdev
->coupling_desc
;
5488 struct regulator_dev
*__c_rdev
, *c_rdev
;
5489 unsigned int __n_coupled
, n_coupled
;
5493 n_coupled
= c_desc
->n_coupled
;
5495 for (i
= 1; i
< n_coupled
; i
++) {
5496 c_rdev
= c_desc
->coupled_rdevs
[i
];
5501 regulator_lock(c_rdev
);
5503 __c_desc
= &c_rdev
->coupling_desc
;
5504 __n_coupled
= __c_desc
->n_coupled
;
5506 for (k
= 1; k
< __n_coupled
; k
++) {
5507 __c_rdev
= __c_desc
->coupled_rdevs
[k
];
5509 if (__c_rdev
== rdev
) {
5510 __c_desc
->coupled_rdevs
[k
] = NULL
;
5511 __c_desc
->n_resolved
--;
5516 regulator_unlock(c_rdev
);
5518 c_desc
->coupled_rdevs
[i
] = NULL
;
5519 c_desc
->n_resolved
--;
5522 if (coupler
&& coupler
->detach_regulator
) {
5523 err
= coupler
->detach_regulator(coupler
, rdev
);
5525 rdev_err(rdev
, "failed to detach from coupler: %pe\n",
5529 kfree(rdev
->coupling_desc
.coupled_rdevs
);
5530 rdev
->coupling_desc
.coupled_rdevs
= NULL
;
5533 static int regulator_init_coupling(struct regulator_dev
*rdev
)
5535 struct regulator_dev
**coupled
;
5536 int err
, n_phandles
;
5538 if (!IS_ENABLED(CONFIG_OF
))
5541 n_phandles
= of_get_n_coupled(rdev
);
5543 coupled
= kcalloc(n_phandles
+ 1, sizeof(*coupled
), GFP_KERNEL
);
5547 rdev
->coupling_desc
.coupled_rdevs
= coupled
;
5550 * Every regulator should always have coupling descriptor filled with
5551 * at least pointer to itself.
5553 rdev
->coupling_desc
.coupled_rdevs
[0] = rdev
;
5554 rdev
->coupling_desc
.n_coupled
= n_phandles
+ 1;
5555 rdev
->coupling_desc
.n_resolved
++;
5557 /* regulator isn't coupled */
5558 if (n_phandles
== 0)
5561 if (!of_check_coupling_data(rdev
))
5564 mutex_lock(®ulator_list_mutex
);
5565 rdev
->coupling_desc
.coupler
= regulator_find_coupler(rdev
);
5566 mutex_unlock(®ulator_list_mutex
);
5568 if (IS_ERR(rdev
->coupling_desc
.coupler
)) {
5569 err
= PTR_ERR(rdev
->coupling_desc
.coupler
);
5570 rdev_err(rdev
, "failed to get coupler: %pe\n", ERR_PTR(err
));
5577 static int generic_coupler_attach(struct regulator_coupler
*coupler
,
5578 struct regulator_dev
*rdev
)
5580 if (rdev
->coupling_desc
.n_coupled
> 2) {
5582 "Voltage balancing for multiple regulator couples is unimplemented\n");
5586 if (!rdev
->constraints
->always_on
) {
5588 "Coupling of a non always-on regulator is unimplemented\n");
5595 static struct regulator_coupler generic_regulator_coupler
= {
5596 .attach_regulator
= generic_coupler_attach
,
5600 * regulator_register - register regulator
5601 * @dev: the device that drive the regulator
5602 * @regulator_desc: regulator to register
5603 * @cfg: runtime configuration for regulator
5605 * Called by regulator drivers to register a regulator.
5607 * Return: Pointer to a valid &struct regulator_dev on success or
5608 * an ERR_PTR() encoded negative error number on failure.
5610 struct regulator_dev
*
5611 regulator_register(struct device
*dev
,
5612 const struct regulator_desc
*regulator_desc
,
5613 const struct regulator_config
*cfg
)
5615 const struct regulator_init_data
*init_data
;
5616 struct regulator_config
*config
= NULL
;
5617 static atomic_t regulator_no
= ATOMIC_INIT(-1);
5618 struct regulator_dev
*rdev
;
5619 bool dangling_cfg_gpiod
= false;
5620 bool dangling_of_gpiod
= false;
5622 bool resolved_early
= false;
5625 return ERR_PTR(-EINVAL
);
5627 dangling_cfg_gpiod
= true;
5628 if (regulator_desc
== NULL
) {
5633 WARN_ON(!dev
|| !cfg
->dev
);
5635 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
) {
5640 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
5641 regulator_desc
->type
!= REGULATOR_CURRENT
) {
5646 /* Only one of each should be implemented */
5647 WARN_ON(regulator_desc
->ops
->get_voltage
&&
5648 regulator_desc
->ops
->get_voltage_sel
);
5649 WARN_ON(regulator_desc
->ops
->set_voltage
&&
5650 regulator_desc
->ops
->set_voltage_sel
);
5652 /* If we're using selectors we must implement list_voltage. */
5653 if (regulator_desc
->ops
->get_voltage_sel
&&
5654 !regulator_desc
->ops
->list_voltage
) {
5658 if (regulator_desc
->ops
->set_voltage_sel
&&
5659 !regulator_desc
->ops
->list_voltage
) {
5664 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
5669 device_initialize(&rdev
->dev
);
5670 dev_set_drvdata(&rdev
->dev
, rdev
);
5671 rdev
->dev
.class = ®ulator_class
;
5672 spin_lock_init(&rdev
->err_lock
);
5675 * Duplicate the config so the driver could override it after
5676 * parsing init data.
5678 config
= kmemdup(cfg
, sizeof(*cfg
), GFP_KERNEL
);
5679 if (config
== NULL
) {
5684 init_data
= regulator_of_get_init_data(dev
, regulator_desc
, config
,
5685 &rdev
->dev
.of_node
);
5688 * Sometimes not all resources are probed already so we need to take
5689 * that into account. This happens most the time if the ena_gpiod comes
5690 * from a gpio extender or something else.
5692 if (PTR_ERR(init_data
) == -EPROBE_DEFER
) {
5693 ret
= -EPROBE_DEFER
;
5698 * We need to keep track of any GPIO descriptor coming from the
5699 * device tree until we have handled it over to the core. If the
5700 * config that was passed in to this function DOES NOT contain
5701 * a descriptor, and the config after this call DOES contain
5702 * a descriptor, we definitely got one from parsing the device
5705 if (!cfg
->ena_gpiod
&& config
->ena_gpiod
)
5706 dangling_of_gpiod
= true;
5708 init_data
= config
->init_data
;
5709 rdev
->dev
.of_node
= of_node_get(config
->of_node
);
5712 ww_mutex_init(&rdev
->mutex
, ®ulator_ww_class
);
5713 rdev
->reg_data
= config
->driver_data
;
5714 rdev
->owner
= regulator_desc
->owner
;
5715 rdev
->desc
= regulator_desc
;
5717 rdev
->regmap
= config
->regmap
;
5718 else if (dev_get_regmap(dev
, NULL
))
5719 rdev
->regmap
= dev_get_regmap(dev
, NULL
);
5720 else if (dev
->parent
)
5721 rdev
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
5722 INIT_LIST_HEAD(&rdev
->consumer_list
);
5723 INIT_LIST_HEAD(&rdev
->list
);
5724 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
5725 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
5727 if (init_data
&& init_data
->supply_regulator
)
5728 rdev
->supply_name
= init_data
->supply_regulator
;
5729 else if (regulator_desc
->supply_name
)
5730 rdev
->supply_name
= regulator_desc
->supply_name
;
5732 /* register with sysfs */
5733 rdev
->dev
.parent
= config
->dev
;
5734 dev_set_name(&rdev
->dev
, "regulator.%lu",
5735 (unsigned long) atomic_inc_return(®ulator_no
));
5737 /* set regulator constraints */
5739 rdev
->constraints
= kmemdup(&init_data
->constraints
,
5740 sizeof(*rdev
->constraints
),
5743 rdev
->constraints
= kzalloc(sizeof(*rdev
->constraints
),
5745 if (!rdev
->constraints
) {
5750 if ((rdev
->supply_name
&& !rdev
->supply
) &&
5751 (rdev
->constraints
->always_on
||
5752 rdev
->constraints
->boot_on
)) {
5753 ret
= regulator_resolve_supply(rdev
);
5755 rdev_dbg(rdev
, "unable to resolve supply early: %pe\n",
5758 resolved_early
= true;
5761 /* perform any regulator specific init */
5762 if (init_data
&& init_data
->regulator_init
) {
5763 ret
= init_data
->regulator_init(rdev
->reg_data
);
5768 if (config
->ena_gpiod
) {
5769 ret
= regulator_ena_gpio_request(rdev
, config
);
5771 rdev_err(rdev
, "Failed to request enable GPIO: %pe\n",
5775 /* The regulator core took over the GPIO descriptor */
5776 dangling_cfg_gpiod
= false;
5777 dangling_of_gpiod
= false;
5780 ret
= set_machine_constraints(rdev
);
5781 if (ret
== -EPROBE_DEFER
&& !resolved_early
) {
5782 /* Regulator might be in bypass mode and so needs its supply
5783 * to set the constraints
5785 /* FIXME: this currently triggers a chicken-and-egg problem
5786 * when creating -SUPPLY symlink in sysfs to a regulator
5787 * that is just being created
5789 rdev_dbg(rdev
, "will resolve supply early: %s\n",
5791 ret
= regulator_resolve_supply(rdev
);
5793 ret
= set_machine_constraints(rdev
);
5795 rdev_dbg(rdev
, "unable to resolve supply early: %pe\n",
5801 ret
= regulator_init_coupling(rdev
);
5805 /* add consumers devices */
5807 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
5808 ret
= set_consumer_device_supply(rdev
,
5809 init_data
->consumer_supplies
[i
].dev_name
,
5810 init_data
->consumer_supplies
[i
].supply
);
5812 dev_err(dev
, "Failed to set supply %s\n",
5813 init_data
->consumer_supplies
[i
].supply
);
5814 goto unset_supplies
;
5819 if (!rdev
->desc
->ops
->get_voltage
&&
5820 !rdev
->desc
->ops
->list_voltage
&&
5821 !rdev
->desc
->fixed_uV
)
5822 rdev
->is_switch
= true;
5824 ret
= device_add(&rdev
->dev
);
5826 goto unset_supplies
;
5828 rdev_init_debugfs(rdev
);
5830 /* try to resolve regulators coupling since a new one was registered */
5831 mutex_lock(®ulator_list_mutex
);
5832 regulator_resolve_coupling(rdev
);
5833 mutex_unlock(®ulator_list_mutex
);
5835 /* try to resolve regulators supply since a new one was registered */
5836 class_for_each_device(®ulator_class
, NULL
, NULL
,
5837 regulator_register_resolve_supply
);
5842 mutex_lock(®ulator_list_mutex
);
5843 unset_regulator_supplies(rdev
);
5844 regulator_remove_coupling(rdev
);
5845 mutex_unlock(®ulator_list_mutex
);
5847 regulator_put(rdev
->supply
);
5848 kfree(rdev
->coupling_desc
.coupled_rdevs
);
5849 mutex_lock(®ulator_list_mutex
);
5850 regulator_ena_gpio_free(rdev
);
5851 mutex_unlock(®ulator_list_mutex
);
5853 if (dangling_of_gpiod
)
5854 gpiod_put(config
->ena_gpiod
);
5856 put_device(&rdev
->dev
);
5858 if (dangling_cfg_gpiod
)
5859 gpiod_put(cfg
->ena_gpiod
);
5860 return ERR_PTR(ret
);
5862 EXPORT_SYMBOL_GPL(regulator_register
);
5865 * regulator_unregister - unregister regulator
5866 * @rdev: regulator to unregister
5868 * Called by regulator drivers to unregister a regulator.
5870 void regulator_unregister(struct regulator_dev
*rdev
)
5876 while (rdev
->use_count
--)
5877 regulator_disable(rdev
->supply
);
5878 regulator_put(rdev
->supply
);
5881 flush_work(&rdev
->disable_work
.work
);
5883 mutex_lock(®ulator_list_mutex
);
5885 WARN_ON(rdev
->open_count
);
5886 regulator_remove_coupling(rdev
);
5887 unset_regulator_supplies(rdev
);
5888 list_del(&rdev
->list
);
5889 regulator_ena_gpio_free(rdev
);
5890 device_unregister(&rdev
->dev
);
5892 mutex_unlock(®ulator_list_mutex
);
5894 EXPORT_SYMBOL_GPL(regulator_unregister
);
5896 #ifdef CONFIG_SUSPEND
5898 * regulator_suspend - prepare regulators for system wide suspend
5899 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5901 * Configure each regulator with it's suspend operating parameters for state.
5903 * Return: 0 on success or a negative error number on failure.
5905 static int regulator_suspend(struct device
*dev
)
5907 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5908 suspend_state_t state
= pm_suspend_target_state
;
5910 const struct regulator_state
*rstate
;
5912 rstate
= regulator_get_suspend_state_check(rdev
, state
);
5916 regulator_lock(rdev
);
5917 ret
= __suspend_set_state(rdev
, rstate
);
5918 regulator_unlock(rdev
);
5923 static int regulator_resume(struct device
*dev
)
5925 suspend_state_t state
= pm_suspend_target_state
;
5926 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5927 struct regulator_state
*rstate
;
5930 rstate
= regulator_get_suspend_state(rdev
, state
);
5934 /* Avoid grabbing the lock if we don't need to */
5935 if (!rdev
->desc
->ops
->resume
)
5938 regulator_lock(rdev
);
5940 if (rstate
->enabled
== ENABLE_IN_SUSPEND
||
5941 rstate
->enabled
== DISABLE_IN_SUSPEND
)
5942 ret
= rdev
->desc
->ops
->resume(rdev
);
5944 regulator_unlock(rdev
);
5948 #else /* !CONFIG_SUSPEND */
5950 #define regulator_suspend NULL
5951 #define regulator_resume NULL
5953 #endif /* !CONFIG_SUSPEND */
5956 static const struct dev_pm_ops __maybe_unused regulator_pm_ops
= {
5957 .suspend
= regulator_suspend
,
5958 .resume
= regulator_resume
,
5962 const struct class regulator_class
= {
5963 .name
= "regulator",
5964 .dev_release
= regulator_dev_release
,
5965 .dev_groups
= regulator_dev_groups
,
5967 .pm
= ®ulator_pm_ops
,
5971 * regulator_has_full_constraints - the system has fully specified constraints
5973 * Calling this function will cause the regulator API to disable all
5974 * regulators which have a zero use count and don't have an always_on
5975 * constraint in a late_initcall.
5977 * The intention is that this will become the default behaviour in a
5978 * future kernel release so users are encouraged to use this facility
5981 void regulator_has_full_constraints(void)
5983 has_full_constraints
= 1;
5985 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
5988 * rdev_get_drvdata - get rdev regulator driver data
5991 * Get rdev regulator driver private data. This call can be used in the
5992 * regulator driver context.
5994 * Return: Pointer to regulator driver private data.
5996 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
5998 return rdev
->reg_data
;
6000 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
6003 * regulator_get_drvdata - get regulator driver data
6004 * @regulator: regulator
6006 * Get regulator driver private data. This call can be used in the consumer
6007 * driver context when non API regulator specific functions need to be called.
6009 * Return: Pointer to regulator driver private data.
6011 void *regulator_get_drvdata(struct regulator
*regulator
)
6013 return regulator
->rdev
->reg_data
;
6015 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
6018 * regulator_set_drvdata - set regulator driver data
6019 * @regulator: regulator
6022 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
6024 regulator
->rdev
->reg_data
= data
;
6026 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
6029 * rdev_get_id - get regulator ID
6032 * Return: Regulator ID for @rdev.
6034 int rdev_get_id(struct regulator_dev
*rdev
)
6036 return rdev
->desc
->id
;
6038 EXPORT_SYMBOL_GPL(rdev_get_id
);
6040 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
6044 EXPORT_SYMBOL_GPL(rdev_get_dev
);
6046 struct regmap
*rdev_get_regmap(struct regulator_dev
*rdev
)
6048 return rdev
->regmap
;
6050 EXPORT_SYMBOL_GPL(rdev_get_regmap
);
6052 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
6054 return reg_init_data
->driver_data
;
6056 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
6058 #ifdef CONFIG_DEBUG_FS
6059 static int supply_map_show(struct seq_file
*sf
, void *data
)
6061 struct regulator_map
*map
;
6063 list_for_each_entry(map
, ®ulator_map_list
, list
) {
6064 seq_printf(sf
, "%s -> %s.%s\n",
6065 rdev_get_name(map
->regulator
), map
->dev_name
,
6071 DEFINE_SHOW_ATTRIBUTE(supply_map
);
6073 struct summary_data
{
6075 struct regulator_dev
*parent
;
6079 static void regulator_summary_show_subtree(struct seq_file
*s
,
6080 struct regulator_dev
*rdev
,
6083 static int regulator_summary_show_children(struct device
*dev
, void *data
)
6085 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
6086 struct summary_data
*summary_data
= data
;
6088 if (rdev
->supply
&& rdev
->supply
->rdev
== summary_data
->parent
)
6089 regulator_summary_show_subtree(summary_data
->s
, rdev
,
6090 summary_data
->level
+ 1);
6095 static void regulator_summary_show_subtree(struct seq_file
*s
,
6096 struct regulator_dev
*rdev
,
6099 struct regulation_constraints
*c
;
6100 struct regulator
*consumer
;
6101 struct summary_data summary_data
;
6102 unsigned int opmode
;
6107 opmode
= _regulator_get_mode_unlocked(rdev
);
6108 seq_printf(s
, "%*s%-*s %3d %4d %6d %7s ",
6110 30 - level
* 3, rdev_get_name(rdev
),
6111 rdev
->use_count
, rdev
->open_count
, rdev
->bypass_count
,
6112 regulator_opmode_to_str(opmode
));
6114 seq_printf(s
, "%5dmV ", regulator_get_voltage_rdev(rdev
) / 1000);
6115 seq_printf(s
, "%5dmA ",
6116 _regulator_get_current_limit_unlocked(rdev
) / 1000);
6118 c
= rdev
->constraints
;
6120 switch (rdev
->desc
->type
) {
6121 case REGULATOR_VOLTAGE
:
6122 seq_printf(s
, "%5dmV %5dmV ",
6123 c
->min_uV
/ 1000, c
->max_uV
/ 1000);
6125 case REGULATOR_CURRENT
:
6126 seq_printf(s
, "%5dmA %5dmA ",
6127 c
->min_uA
/ 1000, c
->max_uA
/ 1000);
6134 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
) {
6135 if (consumer
->dev
&& consumer
->dev
->class == ®ulator_class
)
6138 seq_printf(s
, "%*s%-*s ",
6139 (level
+ 1) * 3 + 1, "",
6140 30 - (level
+ 1) * 3,
6141 consumer
->supply_name
? consumer
->supply_name
:
6142 consumer
->dev
? dev_name(consumer
->dev
) : "deviceless");
6144 switch (rdev
->desc
->type
) {
6145 case REGULATOR_VOLTAGE
:
6146 seq_printf(s
, "%3d %33dmA%c%5dmV %5dmV",
6147 consumer
->enable_count
,
6148 consumer
->uA_load
/ 1000,
6149 consumer
->uA_load
&& !consumer
->enable_count
?
6151 consumer
->voltage
[PM_SUSPEND_ON
].min_uV
/ 1000,
6152 consumer
->voltage
[PM_SUSPEND_ON
].max_uV
/ 1000);
6154 case REGULATOR_CURRENT
:
6162 summary_data
.level
= level
;
6163 summary_data
.parent
= rdev
;
6165 class_for_each_device(®ulator_class
, NULL
, &summary_data
,
6166 regulator_summary_show_children
);
6169 struct summary_lock_data
{
6170 struct ww_acquire_ctx
*ww_ctx
;
6171 struct regulator_dev
**new_contended_rdev
;
6172 struct regulator_dev
**old_contended_rdev
;
6175 static int regulator_summary_lock_one(struct device
*dev
, void *data
)
6177 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
6178 struct summary_lock_data
*lock_data
= data
;
6181 if (rdev
!= *lock_data
->old_contended_rdev
) {
6182 ret
= regulator_lock_nested(rdev
, lock_data
->ww_ctx
);
6184 if (ret
== -EDEADLK
)
6185 *lock_data
->new_contended_rdev
= rdev
;
6189 *lock_data
->old_contended_rdev
= NULL
;
6195 static int regulator_summary_unlock_one(struct device
*dev
, void *data
)
6197 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
6198 struct summary_lock_data
*lock_data
= data
;
6201 if (rdev
== *lock_data
->new_contended_rdev
)
6205 regulator_unlock(rdev
);
6210 static int regulator_summary_lock_all(struct ww_acquire_ctx
*ww_ctx
,
6211 struct regulator_dev
**new_contended_rdev
,
6212 struct regulator_dev
**old_contended_rdev
)
6214 struct summary_lock_data lock_data
;
6217 lock_data
.ww_ctx
= ww_ctx
;
6218 lock_data
.new_contended_rdev
= new_contended_rdev
;
6219 lock_data
.old_contended_rdev
= old_contended_rdev
;
6221 ret
= class_for_each_device(®ulator_class
, NULL
, &lock_data
,
6222 regulator_summary_lock_one
);
6224 class_for_each_device(®ulator_class
, NULL
, &lock_data
,
6225 regulator_summary_unlock_one
);
6230 static void regulator_summary_lock(struct ww_acquire_ctx
*ww_ctx
)
6232 struct regulator_dev
*new_contended_rdev
= NULL
;
6233 struct regulator_dev
*old_contended_rdev
= NULL
;
6236 mutex_lock(®ulator_list_mutex
);
6238 ww_acquire_init(ww_ctx
, ®ulator_ww_class
);
6241 if (new_contended_rdev
) {
6242 ww_mutex_lock_slow(&new_contended_rdev
->mutex
, ww_ctx
);
6243 old_contended_rdev
= new_contended_rdev
;
6244 old_contended_rdev
->ref_cnt
++;
6245 old_contended_rdev
->mutex_owner
= current
;
6248 err
= regulator_summary_lock_all(ww_ctx
,
6249 &new_contended_rdev
,
6250 &old_contended_rdev
);
6252 if (old_contended_rdev
)
6253 regulator_unlock(old_contended_rdev
);
6255 } while (err
== -EDEADLK
);
6257 ww_acquire_done(ww_ctx
);
6260 static void regulator_summary_unlock(struct ww_acquire_ctx
*ww_ctx
)
6262 class_for_each_device(®ulator_class
, NULL
, NULL
,
6263 regulator_summary_unlock_one
);
6264 ww_acquire_fini(ww_ctx
);
6266 mutex_unlock(®ulator_list_mutex
);
6269 static int regulator_summary_show_roots(struct device
*dev
, void *data
)
6271 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
6272 struct seq_file
*s
= data
;
6275 regulator_summary_show_subtree(s
, rdev
, 0);
6280 static int regulator_summary_show(struct seq_file
*s
, void *data
)
6282 struct ww_acquire_ctx ww_ctx
;
6284 seq_puts(s
, " regulator use open bypass opmode voltage current min max\n");
6285 seq_puts(s
, "---------------------------------------------------------------------------------------\n");
6287 regulator_summary_lock(&ww_ctx
);
6289 class_for_each_device(®ulator_class
, NULL
, s
,
6290 regulator_summary_show_roots
);
6292 regulator_summary_unlock(&ww_ctx
);
6296 DEFINE_SHOW_ATTRIBUTE(regulator_summary
);
6297 #endif /* CONFIG_DEBUG_FS */
6299 static int __init
regulator_init(void)
6303 ret
= class_register(®ulator_class
);
6305 debugfs_root
= debugfs_create_dir("regulator", NULL
);
6306 if (IS_ERR(debugfs_root
))
6307 pr_debug("regulator: Failed to create debugfs directory\n");
6309 #ifdef CONFIG_DEBUG_FS
6310 debugfs_create_file("supply_map", 0444, debugfs_root
, NULL
,
6313 debugfs_create_file("regulator_summary", 0444, debugfs_root
,
6314 NULL
, ®ulator_summary_fops
);
6316 regulator_dummy_init();
6318 regulator_coupler_register(&generic_regulator_coupler
);
6323 /* init early to allow our consumers to complete system booting */
6324 core_initcall(regulator_init
);
6326 static int regulator_late_cleanup(struct device
*dev
, void *data
)
6328 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
6329 struct regulation_constraints
*c
= rdev
->constraints
;
6332 if (c
&& c
->always_on
)
6335 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
))
6338 regulator_lock(rdev
);
6340 if (rdev
->use_count
)
6343 /* If reading the status failed, assume that it's off. */
6344 if (_regulator_is_enabled(rdev
) <= 0)
6347 if (have_full_constraints()) {
6348 /* We log since this may kill the system if it goes
6351 rdev_info(rdev
, "disabling\n");
6352 ret
= _regulator_do_disable(rdev
);
6354 rdev_err(rdev
, "couldn't disable: %pe\n", ERR_PTR(ret
));
6356 /* The intention is that in future we will
6357 * assume that full constraints are provided
6358 * so warn even if we aren't going to do
6361 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
6365 regulator_unlock(rdev
);
6370 static bool regulator_ignore_unused
;
6371 static int __init
regulator_ignore_unused_setup(char *__unused
)
6373 regulator_ignore_unused
= true;
6376 __setup("regulator_ignore_unused", regulator_ignore_unused_setup
);
6378 static void regulator_init_complete_work_function(struct work_struct
*work
)
6381 * Regulators may had failed to resolve their input supplies
6382 * when were registered, either because the input supply was
6383 * not registered yet or because its parent device was not
6384 * bound yet. So attempt to resolve the input supplies for
6385 * pending regulators before trying to disable unused ones.
6387 class_for_each_device(®ulator_class
, NULL
, NULL
,
6388 regulator_register_resolve_supply
);
6391 * For debugging purposes, it may be useful to prevent unused
6392 * regulators from being disabled.
6394 if (regulator_ignore_unused
) {
6395 pr_warn("regulator: Not disabling unused regulators\n");
6399 /* If we have a full configuration then disable any regulators
6400 * we have permission to change the status for and which are
6401 * not in use or always_on. This is effectively the default
6402 * for DT and ACPI as they have full constraints.
6404 class_for_each_device(®ulator_class
, NULL
, NULL
,
6405 regulator_late_cleanup
);
6408 static DECLARE_DELAYED_WORK(regulator_init_complete_work
,
6409 regulator_init_complete_work_function
);
6411 static int __init
regulator_init_complete(void)
6414 * Since DT doesn't provide an idiomatic mechanism for
6415 * enabling full constraints and since it's much more natural
6416 * with DT to provide them just assume that a DT enabled
6417 * system has full constraints.
6419 if (of_have_populated_dt())
6420 has_full_constraints
= true;
6423 * We punt completion for an arbitrary amount of time since
6424 * systems like distros will load many drivers from userspace
6425 * so consumers might not always be ready yet, this is
6426 * particularly an issue with laptops where this might bounce
6427 * the display off then on. Ideally we'd get a notification
6428 * from userspace when this happens but we don't so just wait
6429 * a bit and hope we waited long enough. It'd be better if
6430 * we'd only do this on systems that need it, and a kernel
6431 * command line option might be useful.
6433 schedule_delayed_work(®ulator_init_complete_work
,
6434 msecs_to_jiffies(30000));
6438 late_initcall_sync(regulator_init_complete
);