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>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/debugfs.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/async.h>
17 #include <linux/err.h>
18 #include <linux/mutex.h>
19 #include <linux/suspend.h>
20 #include <linux/delay.h>
21 #include <linux/gpio/consumer.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/of_regulator.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/module.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
36 #define rdev_crit(rdev, fmt, ...) \
37 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_err(rdev, fmt, ...) \
39 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_warn(rdev, fmt, ...) \
41 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_info(rdev, fmt, ...) \
43 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_dbg(rdev, fmt, ...) \
45 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 static DEFINE_WW_CLASS(regulator_ww_class
);
48 static DEFINE_MUTEX(regulator_nesting_mutex
);
49 static DEFINE_MUTEX(regulator_list_mutex
);
50 static LIST_HEAD(regulator_map_list
);
51 static LIST_HEAD(regulator_ena_gpio_list
);
52 static LIST_HEAD(regulator_supply_alias_list
);
53 static bool has_full_constraints
;
55 static struct dentry
*debugfs_root
;
58 * struct regulator_map
60 * Used to provide symbolic supply names to devices.
62 struct regulator_map
{
63 struct list_head list
;
64 const char *dev_name
; /* The dev_name() for the consumer */
66 struct regulator_dev
*regulator
;
70 * struct regulator_enable_gpio
72 * Management for shared enable GPIO pin
74 struct regulator_enable_gpio
{
75 struct list_head list
;
76 struct gpio_desc
*gpiod
;
77 u32 enable_count
; /* a number of enabled shared GPIO */
78 u32 request_count
; /* a number of requested shared GPIO */
82 * struct regulator_supply_alias
84 * Used to map lookups for a supply onto an alternative device.
86 struct regulator_supply_alias
{
87 struct list_head list
;
88 struct device
*src_dev
;
89 const char *src_supply
;
90 struct device
*alias_dev
;
91 const char *alias_supply
;
94 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
95 static int _regulator_disable(struct regulator
*regulator
);
96 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
97 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
98 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
99 static int _notifier_call_chain(struct regulator_dev
*rdev
,
100 unsigned long event
, void *data
);
101 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
102 int min_uV
, int max_uV
);
103 static int regulator_balance_voltage(struct regulator_dev
*rdev
,
104 suspend_state_t state
);
105 static int regulator_set_voltage_rdev(struct regulator_dev
*rdev
,
106 int min_uV
, int max_uV
,
107 suspend_state_t state
);
108 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
110 const char *supply_name
);
111 static void _regulator_put(struct regulator
*regulator
);
113 static const char *rdev_get_name(struct regulator_dev
*rdev
)
115 if (rdev
->constraints
&& rdev
->constraints
->name
)
116 return rdev
->constraints
->name
;
117 else if (rdev
->desc
->name
)
118 return rdev
->desc
->name
;
123 static bool have_full_constraints(void)
125 return has_full_constraints
|| of_have_populated_dt();
128 static bool regulator_ops_is_valid(struct regulator_dev
*rdev
, int ops
)
130 if (!rdev
->constraints
) {
131 rdev_err(rdev
, "no constraints\n");
135 if (rdev
->constraints
->valid_ops_mask
& ops
)
142 * regulator_lock_nested - lock a single regulator
143 * @rdev: regulator source
144 * @ww_ctx: w/w mutex acquire context
146 * This function can be called many times by one task on
147 * a single regulator and its mutex will be locked only
148 * once. If a task, which is calling this function is other
149 * than the one, which initially locked the mutex, it will
152 static inline int regulator_lock_nested(struct regulator_dev
*rdev
,
153 struct ww_acquire_ctx
*ww_ctx
)
158 mutex_lock(®ulator_nesting_mutex
);
160 if (ww_ctx
|| !ww_mutex_trylock(&rdev
->mutex
)) {
161 if (rdev
->mutex_owner
== current
)
167 mutex_unlock(®ulator_nesting_mutex
);
168 ret
= ww_mutex_lock(&rdev
->mutex
, ww_ctx
);
169 mutex_lock(®ulator_nesting_mutex
);
175 if (lock
&& ret
!= -EDEADLK
) {
177 rdev
->mutex_owner
= current
;
180 mutex_unlock(®ulator_nesting_mutex
);
186 * regulator_lock - lock a single regulator
187 * @rdev: regulator source
189 * This function can be called many times by one task on
190 * a single regulator and its mutex will be locked only
191 * once. If a task, which is calling this function is other
192 * than the one, which initially locked the mutex, it will
195 void regulator_lock(struct regulator_dev
*rdev
)
197 regulator_lock_nested(rdev
, NULL
);
199 EXPORT_SYMBOL_GPL(regulator_lock
);
202 * regulator_unlock - unlock a single regulator
203 * @rdev: regulator_source
205 * This function unlocks the mutex when the
206 * reference counter reaches 0.
208 void regulator_unlock(struct regulator_dev
*rdev
)
210 mutex_lock(®ulator_nesting_mutex
);
212 if (--rdev
->ref_cnt
== 0) {
213 rdev
->mutex_owner
= NULL
;
214 ww_mutex_unlock(&rdev
->mutex
);
217 WARN_ON_ONCE(rdev
->ref_cnt
< 0);
219 mutex_unlock(®ulator_nesting_mutex
);
221 EXPORT_SYMBOL_GPL(regulator_unlock
);
223 static bool regulator_supply_is_couple(struct regulator_dev
*rdev
)
225 struct regulator_dev
*c_rdev
;
228 for (i
= 1; i
< rdev
->coupling_desc
.n_coupled
; i
++) {
229 c_rdev
= rdev
->coupling_desc
.coupled_rdevs
[i
];
231 if (rdev
->supply
->rdev
== c_rdev
)
238 static void regulator_unlock_recursive(struct regulator_dev
*rdev
,
239 unsigned int n_coupled
)
241 struct regulator_dev
*c_rdev
;
244 for (i
= n_coupled
; i
> 0; i
--) {
245 c_rdev
= rdev
->coupling_desc
.coupled_rdevs
[i
- 1];
250 if (c_rdev
->supply
&& !regulator_supply_is_couple(c_rdev
))
251 regulator_unlock_recursive(
252 c_rdev
->supply
->rdev
,
253 c_rdev
->coupling_desc
.n_coupled
);
255 regulator_unlock(c_rdev
);
259 static int regulator_lock_recursive(struct regulator_dev
*rdev
,
260 struct regulator_dev
**new_contended_rdev
,
261 struct regulator_dev
**old_contended_rdev
,
262 struct ww_acquire_ctx
*ww_ctx
)
264 struct regulator_dev
*c_rdev
;
267 for (i
= 0; i
< rdev
->coupling_desc
.n_coupled
; i
++) {
268 c_rdev
= rdev
->coupling_desc
.coupled_rdevs
[i
];
273 if (c_rdev
!= *old_contended_rdev
) {
274 err
= regulator_lock_nested(c_rdev
, ww_ctx
);
276 if (err
== -EDEADLK
) {
277 *new_contended_rdev
= c_rdev
;
281 /* shouldn't happen */
282 WARN_ON_ONCE(err
!= -EALREADY
);
285 *old_contended_rdev
= NULL
;
288 if (c_rdev
->supply
&& !regulator_supply_is_couple(c_rdev
)) {
289 err
= regulator_lock_recursive(c_rdev
->supply
->rdev
,
294 regulator_unlock(c_rdev
);
303 regulator_unlock_recursive(rdev
, i
);
309 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
311 * @rdev: regulator source
312 * @ww_ctx: w/w mutex acquire context
314 * Unlock all regulators related with rdev by coupling or supplying.
316 static void regulator_unlock_dependent(struct regulator_dev
*rdev
,
317 struct ww_acquire_ctx
*ww_ctx
)
319 regulator_unlock_recursive(rdev
, rdev
->coupling_desc
.n_coupled
);
320 ww_acquire_fini(ww_ctx
);
324 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
325 * @rdev: regulator source
326 * @ww_ctx: w/w mutex acquire context
328 * This function as a wrapper on regulator_lock_recursive(), which locks
329 * all regulators related with rdev by coupling or supplying.
331 static void regulator_lock_dependent(struct regulator_dev
*rdev
,
332 struct ww_acquire_ctx
*ww_ctx
)
334 struct regulator_dev
*new_contended_rdev
= NULL
;
335 struct regulator_dev
*old_contended_rdev
= NULL
;
338 mutex_lock(®ulator_list_mutex
);
340 ww_acquire_init(ww_ctx
, ®ulator_ww_class
);
343 if (new_contended_rdev
) {
344 ww_mutex_lock_slow(&new_contended_rdev
->mutex
, ww_ctx
);
345 old_contended_rdev
= new_contended_rdev
;
346 old_contended_rdev
->ref_cnt
++;
349 err
= regulator_lock_recursive(rdev
,
354 if (old_contended_rdev
)
355 regulator_unlock(old_contended_rdev
);
357 } while (err
== -EDEADLK
);
359 ww_acquire_done(ww_ctx
);
361 mutex_unlock(®ulator_list_mutex
);
365 * of_get_child_regulator - get a child regulator device node
366 * based on supply name
367 * @parent: Parent device node
368 * @prop_name: Combination regulator supply name and "-supply"
370 * Traverse all child nodes.
371 * Extract the child regulator device node corresponding to the supply name.
372 * returns the device node corresponding to the regulator if found, else
375 static struct device_node
*of_get_child_regulator(struct device_node
*parent
,
376 const char *prop_name
)
378 struct device_node
*regnode
= NULL
;
379 struct device_node
*child
= NULL
;
381 for_each_child_of_node(parent
, child
) {
382 regnode
= of_parse_phandle(child
, prop_name
, 0);
385 regnode
= of_get_child_regulator(child
, prop_name
);
396 * of_get_regulator - get a regulator device node based on supply name
397 * @dev: Device pointer for the consumer (of regulator) device
398 * @supply: regulator supply name
400 * Extract the regulator device node corresponding to the supply name.
401 * returns the device node corresponding to the regulator if found, else
404 static struct device_node
*of_get_regulator(struct device
*dev
, const char *supply
)
406 struct device_node
*regnode
= NULL
;
407 char prop_name
[32]; /* 32 is max size of property name */
409 dev_dbg(dev
, "Looking up %s-supply from device tree\n", supply
);
411 snprintf(prop_name
, 32, "%s-supply", supply
);
412 regnode
= of_parse_phandle(dev
->of_node
, prop_name
, 0);
415 regnode
= of_get_child_regulator(dev
->of_node
, prop_name
);
419 dev_dbg(dev
, "Looking up %s property in node %pOF failed\n",
420 prop_name
, dev
->of_node
);
426 /* Platform voltage constraint check */
427 static int regulator_check_voltage(struct regulator_dev
*rdev
,
428 int *min_uV
, int *max_uV
)
430 BUG_ON(*min_uV
> *max_uV
);
432 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
433 rdev_err(rdev
, "voltage operation not allowed\n");
437 if (*max_uV
> rdev
->constraints
->max_uV
)
438 *max_uV
= rdev
->constraints
->max_uV
;
439 if (*min_uV
< rdev
->constraints
->min_uV
)
440 *min_uV
= rdev
->constraints
->min_uV
;
442 if (*min_uV
> *max_uV
) {
443 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
451 /* return 0 if the state is valid */
452 static int regulator_check_states(suspend_state_t state
)
454 return (state
> PM_SUSPEND_MAX
|| state
== PM_SUSPEND_TO_IDLE
);
457 /* Make sure we select a voltage that suits the needs of all
458 * regulator consumers
460 static int regulator_check_consumers(struct regulator_dev
*rdev
,
461 int *min_uV
, int *max_uV
,
462 suspend_state_t state
)
464 struct regulator
*regulator
;
465 struct regulator_voltage
*voltage
;
467 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
468 voltage
= ®ulator
->voltage
[state
];
470 * Assume consumers that didn't say anything are OK
471 * with anything in the constraint range.
473 if (!voltage
->min_uV
&& !voltage
->max_uV
)
476 if (*max_uV
> voltage
->max_uV
)
477 *max_uV
= voltage
->max_uV
;
478 if (*min_uV
< voltage
->min_uV
)
479 *min_uV
= voltage
->min_uV
;
482 if (*min_uV
> *max_uV
) {
483 rdev_err(rdev
, "Restricting voltage, %u-%uuV\n",
491 /* current constraint check */
492 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
493 int *min_uA
, int *max_uA
)
495 BUG_ON(*min_uA
> *max_uA
);
497 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_CURRENT
)) {
498 rdev_err(rdev
, "current operation not allowed\n");
502 if (*max_uA
> rdev
->constraints
->max_uA
)
503 *max_uA
= rdev
->constraints
->max_uA
;
504 if (*min_uA
< rdev
->constraints
->min_uA
)
505 *min_uA
= rdev
->constraints
->min_uA
;
507 if (*min_uA
> *max_uA
) {
508 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
516 /* operating mode constraint check */
517 static int regulator_mode_constrain(struct regulator_dev
*rdev
,
521 case REGULATOR_MODE_FAST
:
522 case REGULATOR_MODE_NORMAL
:
523 case REGULATOR_MODE_IDLE
:
524 case REGULATOR_MODE_STANDBY
:
527 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
531 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_MODE
)) {
532 rdev_err(rdev
, "mode operation not allowed\n");
536 /* The modes are bitmasks, the most power hungry modes having
537 * the lowest values. If the requested mode isn't supported
538 * try higher modes. */
540 if (rdev
->constraints
->valid_modes_mask
& *mode
)
548 static inline struct regulator_state
*
549 regulator_get_suspend_state(struct regulator_dev
*rdev
, suspend_state_t state
)
551 if (rdev
->constraints
== NULL
)
555 case PM_SUSPEND_STANDBY
:
556 return &rdev
->constraints
->state_standby
;
558 return &rdev
->constraints
->state_mem
;
560 return &rdev
->constraints
->state_disk
;
566 static ssize_t
regulator_uV_show(struct device
*dev
,
567 struct device_attribute
*attr
, char *buf
)
569 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
572 regulator_lock(rdev
);
573 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
574 regulator_unlock(rdev
);
578 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
580 static ssize_t
regulator_uA_show(struct device
*dev
,
581 struct device_attribute
*attr
, char *buf
)
583 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
585 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
587 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
589 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
592 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
594 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
596 static DEVICE_ATTR_RO(name
);
598 static const char *regulator_opmode_to_str(int mode
)
601 case REGULATOR_MODE_FAST
:
603 case REGULATOR_MODE_NORMAL
:
605 case REGULATOR_MODE_IDLE
:
607 case REGULATOR_MODE_STANDBY
:
613 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
615 return sprintf(buf
, "%s\n", regulator_opmode_to_str(mode
));
618 static ssize_t
regulator_opmode_show(struct device
*dev
,
619 struct device_attribute
*attr
, char *buf
)
621 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
623 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
625 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
627 static ssize_t
regulator_print_state(char *buf
, int state
)
630 return sprintf(buf
, "enabled\n");
632 return sprintf(buf
, "disabled\n");
634 return sprintf(buf
, "unknown\n");
637 static ssize_t
regulator_state_show(struct device
*dev
,
638 struct device_attribute
*attr
, char *buf
)
640 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
643 regulator_lock(rdev
);
644 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
645 regulator_unlock(rdev
);
649 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
651 static ssize_t
regulator_status_show(struct device
*dev
,
652 struct device_attribute
*attr
, char *buf
)
654 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
658 status
= rdev
->desc
->ops
->get_status(rdev
);
663 case REGULATOR_STATUS_OFF
:
666 case REGULATOR_STATUS_ON
:
669 case REGULATOR_STATUS_ERROR
:
672 case REGULATOR_STATUS_FAST
:
675 case REGULATOR_STATUS_NORMAL
:
678 case REGULATOR_STATUS_IDLE
:
681 case REGULATOR_STATUS_STANDBY
:
684 case REGULATOR_STATUS_BYPASS
:
687 case REGULATOR_STATUS_UNDEFINED
:
694 return sprintf(buf
, "%s\n", label
);
696 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
698 static ssize_t
regulator_min_uA_show(struct device
*dev
,
699 struct device_attribute
*attr
, char *buf
)
701 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
703 if (!rdev
->constraints
)
704 return sprintf(buf
, "constraint not defined\n");
706 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
708 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
710 static ssize_t
regulator_max_uA_show(struct device
*dev
,
711 struct device_attribute
*attr
, char *buf
)
713 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
715 if (!rdev
->constraints
)
716 return sprintf(buf
, "constraint not defined\n");
718 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
720 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
722 static ssize_t
regulator_min_uV_show(struct device
*dev
,
723 struct device_attribute
*attr
, char *buf
)
725 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
727 if (!rdev
->constraints
)
728 return sprintf(buf
, "constraint not defined\n");
730 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
732 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
734 static ssize_t
regulator_max_uV_show(struct device
*dev
,
735 struct device_attribute
*attr
, char *buf
)
737 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
739 if (!rdev
->constraints
)
740 return sprintf(buf
, "constraint not defined\n");
742 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
744 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
746 static ssize_t
regulator_total_uA_show(struct device
*dev
,
747 struct device_attribute
*attr
, char *buf
)
749 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
750 struct regulator
*regulator
;
753 regulator_lock(rdev
);
754 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
755 if (regulator
->enable_count
)
756 uA
+= regulator
->uA_load
;
758 regulator_unlock(rdev
);
759 return sprintf(buf
, "%d\n", uA
);
761 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
763 static ssize_t
num_users_show(struct device
*dev
, struct device_attribute
*attr
,
766 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
767 return sprintf(buf
, "%d\n", rdev
->use_count
);
769 static DEVICE_ATTR_RO(num_users
);
771 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
774 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
776 switch (rdev
->desc
->type
) {
777 case REGULATOR_VOLTAGE
:
778 return sprintf(buf
, "voltage\n");
779 case REGULATOR_CURRENT
:
780 return sprintf(buf
, "current\n");
782 return sprintf(buf
, "unknown\n");
784 static DEVICE_ATTR_RO(type
);
786 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
787 struct device_attribute
*attr
, char *buf
)
789 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
791 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
793 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
794 regulator_suspend_mem_uV_show
, NULL
);
796 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
797 struct device_attribute
*attr
, char *buf
)
799 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
801 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
803 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
804 regulator_suspend_disk_uV_show
, NULL
);
806 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
807 struct device_attribute
*attr
, char *buf
)
809 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
811 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
813 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
814 regulator_suspend_standby_uV_show
, NULL
);
816 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
817 struct device_attribute
*attr
, char *buf
)
819 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
821 return regulator_print_opmode(buf
,
822 rdev
->constraints
->state_mem
.mode
);
824 static DEVICE_ATTR(suspend_mem_mode
, 0444,
825 regulator_suspend_mem_mode_show
, NULL
);
827 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
828 struct device_attribute
*attr
, char *buf
)
830 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
832 return regulator_print_opmode(buf
,
833 rdev
->constraints
->state_disk
.mode
);
835 static DEVICE_ATTR(suspend_disk_mode
, 0444,
836 regulator_suspend_disk_mode_show
, NULL
);
838 static ssize_t
regulator_suspend_standby_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_standby
.mode
);
846 static DEVICE_ATTR(suspend_standby_mode
, 0444,
847 regulator_suspend_standby_mode_show
, NULL
);
849 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
850 struct device_attribute
*attr
, char *buf
)
852 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
854 return regulator_print_state(buf
,
855 rdev
->constraints
->state_mem
.enabled
);
857 static DEVICE_ATTR(suspend_mem_state
, 0444,
858 regulator_suspend_mem_state_show
, NULL
);
860 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
861 struct device_attribute
*attr
, char *buf
)
863 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
865 return regulator_print_state(buf
,
866 rdev
->constraints
->state_disk
.enabled
);
868 static DEVICE_ATTR(suspend_disk_state
, 0444,
869 regulator_suspend_disk_state_show
, NULL
);
871 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
872 struct device_attribute
*attr
, char *buf
)
874 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
876 return regulator_print_state(buf
,
877 rdev
->constraints
->state_standby
.enabled
);
879 static DEVICE_ATTR(suspend_standby_state
, 0444,
880 regulator_suspend_standby_state_show
, NULL
);
882 static ssize_t
regulator_bypass_show(struct device
*dev
,
883 struct device_attribute
*attr
, char *buf
)
885 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
890 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypass
);
899 return sprintf(buf
, "%s\n", report
);
901 static DEVICE_ATTR(bypass
, 0444,
902 regulator_bypass_show
, NULL
);
904 /* Calculate the new optimum regulator operating mode based on the new total
905 * consumer load. All locks held by caller */
906 static int drms_uA_update(struct regulator_dev
*rdev
)
908 struct regulator
*sibling
;
909 int current_uA
= 0, output_uV
, input_uV
, err
;
913 * first check to see if we can set modes at all, otherwise just
914 * tell the consumer everything is OK.
916 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_DRMS
)) {
917 rdev_dbg(rdev
, "DRMS operation not allowed\n");
921 if (!rdev
->desc
->ops
->get_optimum_mode
&&
922 !rdev
->desc
->ops
->set_load
)
925 if (!rdev
->desc
->ops
->set_mode
&&
926 !rdev
->desc
->ops
->set_load
)
929 /* calc total requested load */
930 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
) {
931 if (sibling
->enable_count
)
932 current_uA
+= sibling
->uA_load
;
935 current_uA
+= rdev
->constraints
->system_load
;
937 if (rdev
->desc
->ops
->set_load
) {
938 /* set the optimum mode for our new total regulator load */
939 err
= rdev
->desc
->ops
->set_load(rdev
, current_uA
);
941 rdev_err(rdev
, "failed to set load %d\n", current_uA
);
943 /* get output voltage */
944 output_uV
= _regulator_get_voltage(rdev
);
945 if (output_uV
<= 0) {
946 rdev_err(rdev
, "invalid output voltage found\n");
950 /* get input voltage */
953 input_uV
= regulator_get_voltage(rdev
->supply
);
955 input_uV
= rdev
->constraints
->input_uV
;
957 rdev_err(rdev
, "invalid input voltage found\n");
961 /* now get the optimum mode for our new total regulator load */
962 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
963 output_uV
, current_uA
);
965 /* check the new mode is allowed */
966 err
= regulator_mode_constrain(rdev
, &mode
);
968 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
969 current_uA
, input_uV
, output_uV
);
973 err
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
975 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
981 static int suspend_set_state(struct regulator_dev
*rdev
,
982 suspend_state_t state
)
985 struct regulator_state
*rstate
;
987 rstate
= regulator_get_suspend_state(rdev
, state
);
991 /* If we have no suspend mode configuration don't set anything;
992 * only warn if the driver implements set_suspend_voltage or
993 * set_suspend_mode callback.
995 if (rstate
->enabled
!= ENABLE_IN_SUSPEND
&&
996 rstate
->enabled
!= DISABLE_IN_SUSPEND
) {
997 if (rdev
->desc
->ops
->set_suspend_voltage
||
998 rdev
->desc
->ops
->set_suspend_mode
)
999 rdev_warn(rdev
, "No configuration\n");
1003 if (rstate
->enabled
== ENABLE_IN_SUSPEND
&&
1004 rdev
->desc
->ops
->set_suspend_enable
)
1005 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
1006 else if (rstate
->enabled
== DISABLE_IN_SUSPEND
&&
1007 rdev
->desc
->ops
->set_suspend_disable
)
1008 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
1009 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1013 rdev_err(rdev
, "failed to enabled/disable\n");
1017 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
1018 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
1020 rdev_err(rdev
, "failed to set voltage\n");
1025 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
1026 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
1028 rdev_err(rdev
, "failed to set mode\n");
1036 static void print_constraints(struct regulator_dev
*rdev
)
1038 struct regulation_constraints
*constraints
= rdev
->constraints
;
1040 size_t len
= sizeof(buf
) - 1;
1044 if (constraints
->min_uV
&& constraints
->max_uV
) {
1045 if (constraints
->min_uV
== constraints
->max_uV
)
1046 count
+= scnprintf(buf
+ count
, len
- count
, "%d mV ",
1047 constraints
->min_uV
/ 1000);
1049 count
+= scnprintf(buf
+ count
, len
- count
,
1051 constraints
->min_uV
/ 1000,
1052 constraints
->max_uV
/ 1000);
1055 if (!constraints
->min_uV
||
1056 constraints
->min_uV
!= constraints
->max_uV
) {
1057 ret
= _regulator_get_voltage(rdev
);
1059 count
+= scnprintf(buf
+ count
, len
- count
,
1060 "at %d mV ", ret
/ 1000);
1063 if (constraints
->uV_offset
)
1064 count
+= scnprintf(buf
+ count
, len
- count
, "%dmV offset ",
1065 constraints
->uV_offset
/ 1000);
1067 if (constraints
->min_uA
&& constraints
->max_uA
) {
1068 if (constraints
->min_uA
== constraints
->max_uA
)
1069 count
+= scnprintf(buf
+ count
, len
- count
, "%d mA ",
1070 constraints
->min_uA
/ 1000);
1072 count
+= scnprintf(buf
+ count
, len
- count
,
1074 constraints
->min_uA
/ 1000,
1075 constraints
->max_uA
/ 1000);
1078 if (!constraints
->min_uA
||
1079 constraints
->min_uA
!= constraints
->max_uA
) {
1080 ret
= _regulator_get_current_limit(rdev
);
1082 count
+= scnprintf(buf
+ count
, len
- count
,
1083 "at %d mA ", ret
/ 1000);
1086 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
1087 count
+= scnprintf(buf
+ count
, len
- count
, "fast ");
1088 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
1089 count
+= scnprintf(buf
+ count
, len
- count
, "normal ");
1090 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
1091 count
+= scnprintf(buf
+ count
, len
- count
, "idle ");
1092 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
1093 count
+= scnprintf(buf
+ count
, len
- count
, "standby");
1096 scnprintf(buf
, len
, "no parameters");
1098 rdev_dbg(rdev
, "%s\n", buf
);
1100 if ((constraints
->min_uV
!= constraints
->max_uV
) &&
1101 !regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
))
1103 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1106 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
1107 struct regulation_constraints
*constraints
)
1109 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1112 /* do we need to apply the constraint voltage */
1113 if (rdev
->constraints
->apply_uV
&&
1114 rdev
->constraints
->min_uV
&& rdev
->constraints
->max_uV
) {
1115 int target_min
, target_max
;
1116 int current_uV
= _regulator_get_voltage(rdev
);
1118 if (current_uV
== -ENOTRECOVERABLE
) {
1119 /* This regulator can't be read and must be initialized */
1120 rdev_info(rdev
, "Setting %d-%duV\n",
1121 rdev
->constraints
->min_uV
,
1122 rdev
->constraints
->max_uV
);
1123 _regulator_do_set_voltage(rdev
,
1124 rdev
->constraints
->min_uV
,
1125 rdev
->constraints
->max_uV
);
1126 current_uV
= _regulator_get_voltage(rdev
);
1129 if (current_uV
< 0) {
1131 "failed to get the current voltage(%d)\n",
1137 * If we're below the minimum voltage move up to the
1138 * minimum voltage, if we're above the maximum voltage
1139 * then move down to the maximum.
1141 target_min
= current_uV
;
1142 target_max
= current_uV
;
1144 if (current_uV
< rdev
->constraints
->min_uV
) {
1145 target_min
= rdev
->constraints
->min_uV
;
1146 target_max
= rdev
->constraints
->min_uV
;
1149 if (current_uV
> rdev
->constraints
->max_uV
) {
1150 target_min
= rdev
->constraints
->max_uV
;
1151 target_max
= rdev
->constraints
->max_uV
;
1154 if (target_min
!= current_uV
|| target_max
!= current_uV
) {
1155 rdev_info(rdev
, "Bringing %duV into %d-%duV\n",
1156 current_uV
, target_min
, target_max
);
1157 ret
= _regulator_do_set_voltage(
1158 rdev
, target_min
, target_max
);
1161 "failed to apply %d-%duV constraint(%d)\n",
1162 target_min
, target_max
, ret
);
1168 /* constrain machine-level voltage specs to fit
1169 * the actual range supported by this regulator.
1171 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
1172 int count
= rdev
->desc
->n_voltages
;
1174 int min_uV
= INT_MAX
;
1175 int max_uV
= INT_MIN
;
1176 int cmin
= constraints
->min_uV
;
1177 int cmax
= constraints
->max_uV
;
1179 /* it's safe to autoconfigure fixed-voltage supplies
1180 and the constraints are used by list_voltage. */
1181 if (count
== 1 && !cmin
) {
1184 constraints
->min_uV
= cmin
;
1185 constraints
->max_uV
= cmax
;
1188 /* voltage constraints are optional */
1189 if ((cmin
== 0) && (cmax
== 0))
1192 /* else require explicit machine-level constraints */
1193 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
1194 rdev_err(rdev
, "invalid voltage constraints\n");
1198 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1199 for (i
= 0; i
< count
; i
++) {
1202 value
= ops
->list_voltage(rdev
, i
);
1206 /* maybe adjust [min_uV..max_uV] */
1207 if (value
>= cmin
&& value
< min_uV
)
1209 if (value
<= cmax
&& value
> max_uV
)
1213 /* final: [min_uV..max_uV] valid iff constraints valid */
1214 if (max_uV
< min_uV
) {
1216 "unsupportable voltage constraints %u-%uuV\n",
1221 /* use regulator's subset of machine constraints */
1222 if (constraints
->min_uV
< min_uV
) {
1223 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
1224 constraints
->min_uV
, min_uV
);
1225 constraints
->min_uV
= min_uV
;
1227 if (constraints
->max_uV
> max_uV
) {
1228 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
1229 constraints
->max_uV
, max_uV
);
1230 constraints
->max_uV
= max_uV
;
1237 static int machine_constraints_current(struct regulator_dev
*rdev
,
1238 struct regulation_constraints
*constraints
)
1240 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1243 if (!constraints
->min_uA
&& !constraints
->max_uA
)
1246 if (constraints
->min_uA
> constraints
->max_uA
) {
1247 rdev_err(rdev
, "Invalid current constraints\n");
1251 if (!ops
->set_current_limit
|| !ops
->get_current_limit
) {
1252 rdev_warn(rdev
, "Operation of current configuration missing\n");
1256 /* Set regulator current in constraints range */
1257 ret
= ops
->set_current_limit(rdev
, constraints
->min_uA
,
1258 constraints
->max_uA
);
1260 rdev_err(rdev
, "Failed to set current constraint, %d\n", ret
);
1267 static int _regulator_do_enable(struct regulator_dev
*rdev
);
1270 * set_machine_constraints - sets regulator constraints
1271 * @rdev: regulator source
1272 * @constraints: constraints to apply
1274 * Allows platform initialisation code to define and constrain
1275 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1276 * Constraints *must* be set by platform code in order for some
1277 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1280 static int set_machine_constraints(struct regulator_dev
*rdev
,
1281 const struct regulation_constraints
*constraints
)
1284 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1287 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
1290 rdev
->constraints
= kzalloc(sizeof(*constraints
),
1292 if (!rdev
->constraints
)
1295 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
1299 ret
= machine_constraints_current(rdev
, rdev
->constraints
);
1303 if (rdev
->constraints
->ilim_uA
&& ops
->set_input_current_limit
) {
1304 ret
= ops
->set_input_current_limit(rdev
,
1305 rdev
->constraints
->ilim_uA
);
1307 rdev_err(rdev
, "failed to set input limit\n");
1312 /* do we need to setup our suspend state */
1313 if (rdev
->constraints
->initial_state
) {
1314 ret
= suspend_set_state(rdev
, rdev
->constraints
->initial_state
);
1316 rdev_err(rdev
, "failed to set suspend state\n");
1321 if (rdev
->constraints
->initial_mode
) {
1322 if (!ops
->set_mode
) {
1323 rdev_err(rdev
, "no set_mode operation\n");
1327 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
1329 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
1332 } else if (rdev
->constraints
->system_load
) {
1334 * We'll only apply the initial system load if an
1335 * initial mode wasn't specified.
1337 drms_uA_update(rdev
);
1340 if ((rdev
->constraints
->ramp_delay
|| rdev
->constraints
->ramp_disable
)
1341 && ops
->set_ramp_delay
) {
1342 ret
= ops
->set_ramp_delay(rdev
, rdev
->constraints
->ramp_delay
);
1344 rdev_err(rdev
, "failed to set ramp_delay\n");
1349 if (rdev
->constraints
->pull_down
&& ops
->set_pull_down
) {
1350 ret
= ops
->set_pull_down(rdev
);
1352 rdev_err(rdev
, "failed to set pull down\n");
1357 if (rdev
->constraints
->soft_start
&& ops
->set_soft_start
) {
1358 ret
= ops
->set_soft_start(rdev
);
1360 rdev_err(rdev
, "failed to set soft start\n");
1365 if (rdev
->constraints
->over_current_protection
1366 && ops
->set_over_current_protection
) {
1367 ret
= ops
->set_over_current_protection(rdev
);
1369 rdev_err(rdev
, "failed to set over current protection\n");
1374 if (rdev
->constraints
->active_discharge
&& ops
->set_active_discharge
) {
1375 bool ad_state
= (rdev
->constraints
->active_discharge
==
1376 REGULATOR_ACTIVE_DISCHARGE_ENABLE
) ? true : false;
1378 ret
= ops
->set_active_discharge(rdev
, ad_state
);
1380 rdev_err(rdev
, "failed to set active discharge\n");
1385 /* If the constraints say the regulator should be on at this point
1386 * and we have control then make sure it is enabled.
1388 if (rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) {
1390 ret
= regulator_enable(rdev
->supply
);
1392 _regulator_put(rdev
->supply
);
1393 rdev
->supply
= NULL
;
1398 ret
= _regulator_do_enable(rdev
);
1399 if (ret
< 0 && ret
!= -EINVAL
) {
1400 rdev_err(rdev
, "failed to enable\n");
1406 print_constraints(rdev
);
1411 * set_supply - set regulator supply regulator
1412 * @rdev: regulator name
1413 * @supply_rdev: supply regulator name
1415 * Called by platform initialisation code to set the supply regulator for this
1416 * regulator. This ensures that a regulators supply will also be enabled by the
1417 * core if it's child is enabled.
1419 static int set_supply(struct regulator_dev
*rdev
,
1420 struct regulator_dev
*supply_rdev
)
1424 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
1426 if (!try_module_get(supply_rdev
->owner
))
1429 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
1430 if (rdev
->supply
== NULL
) {
1434 supply_rdev
->open_count
++;
1440 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1441 * @rdev: regulator source
1442 * @consumer_dev_name: dev_name() string for device supply applies to
1443 * @supply: symbolic name for supply
1445 * Allows platform initialisation code to map physical regulator
1446 * sources to symbolic names for supplies for use by devices. Devices
1447 * should use these symbolic names to request regulators, avoiding the
1448 * need to provide board-specific regulator names as platform data.
1450 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1451 const char *consumer_dev_name
,
1454 struct regulator_map
*node
;
1460 if (consumer_dev_name
!= NULL
)
1465 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1466 if (node
->dev_name
&& consumer_dev_name
) {
1467 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1469 } else if (node
->dev_name
|| consumer_dev_name
) {
1473 if (strcmp(node
->supply
, supply
) != 0)
1476 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1478 dev_name(&node
->regulator
->dev
),
1479 node
->regulator
->desc
->name
,
1481 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1485 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1489 node
->regulator
= rdev
;
1490 node
->supply
= supply
;
1493 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1494 if (node
->dev_name
== NULL
) {
1500 list_add(&node
->list
, ®ulator_map_list
);
1504 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1506 struct regulator_map
*node
, *n
;
1508 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1509 if (rdev
== node
->regulator
) {
1510 list_del(&node
->list
);
1511 kfree(node
->dev_name
);
1517 #ifdef CONFIG_DEBUG_FS
1518 static ssize_t
constraint_flags_read_file(struct file
*file
,
1519 char __user
*user_buf
,
1520 size_t count
, loff_t
*ppos
)
1522 const struct regulator
*regulator
= file
->private_data
;
1523 const struct regulation_constraints
*c
= regulator
->rdev
->constraints
;
1530 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1534 ret
= snprintf(buf
, PAGE_SIZE
,
1538 "ramp_disable: %u\n"
1541 "over_current_protection: %u\n",
1548 c
->over_current_protection
);
1550 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1558 static const struct file_operations constraint_flags_fops
= {
1559 #ifdef CONFIG_DEBUG_FS
1560 .open
= simple_open
,
1561 .read
= constraint_flags_read_file
,
1562 .llseek
= default_llseek
,
1566 #define REG_STR_SIZE 64
1568 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1570 const char *supply_name
)
1572 struct regulator
*regulator
;
1573 char buf
[REG_STR_SIZE
];
1576 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1577 if (regulator
== NULL
)
1580 regulator_lock(rdev
);
1581 regulator
->rdev
= rdev
;
1582 list_add(®ulator
->list
, &rdev
->consumer_list
);
1585 regulator
->dev
= dev
;
1587 /* Add a link to the device sysfs entry */
1588 size
= snprintf(buf
, REG_STR_SIZE
, "%s-%s",
1589 dev
->kobj
.name
, supply_name
);
1590 if (size
>= REG_STR_SIZE
)
1593 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1594 if (regulator
->supply_name
== NULL
)
1597 err
= sysfs_create_link_nowarn(&rdev
->dev
.kobj
, &dev
->kobj
,
1600 rdev_dbg(rdev
, "could not add device link %s err %d\n",
1601 dev
->kobj
.name
, err
);
1605 regulator
->supply_name
= kstrdup_const(supply_name
, GFP_KERNEL
);
1606 if (regulator
->supply_name
== NULL
)
1610 regulator
->debugfs
= debugfs_create_dir(regulator
->supply_name
,
1612 if (!regulator
->debugfs
) {
1613 rdev_dbg(rdev
, "Failed to create debugfs directory\n");
1615 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1616 ®ulator
->uA_load
);
1617 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1618 ®ulator
->voltage
[PM_SUSPEND_ON
].min_uV
);
1619 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1620 ®ulator
->voltage
[PM_SUSPEND_ON
].max_uV
);
1621 debugfs_create_file("constraint_flags", 0444,
1622 regulator
->debugfs
, regulator
,
1623 &constraint_flags_fops
);
1627 * Check now if the regulator is an always on regulator - if
1628 * it is then we don't need to do nearly so much work for
1629 * enable/disable calls.
1631 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
) &&
1632 _regulator_is_enabled(rdev
))
1633 regulator
->always_on
= true;
1635 regulator_unlock(rdev
);
1638 list_del(®ulator
->list
);
1640 regulator_unlock(rdev
);
1644 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1646 if (rdev
->constraints
&& rdev
->constraints
->enable_time
)
1647 return rdev
->constraints
->enable_time
;
1648 if (!rdev
->desc
->ops
->enable_time
)
1649 return rdev
->desc
->enable_time
;
1650 return rdev
->desc
->ops
->enable_time(rdev
);
1653 static struct regulator_supply_alias
*regulator_find_supply_alias(
1654 struct device
*dev
, const char *supply
)
1656 struct regulator_supply_alias
*map
;
1658 list_for_each_entry(map
, ®ulator_supply_alias_list
, list
)
1659 if (map
->src_dev
== dev
&& strcmp(map
->src_supply
, supply
) == 0)
1665 static void regulator_supply_alias(struct device
**dev
, const char **supply
)
1667 struct regulator_supply_alias
*map
;
1669 map
= regulator_find_supply_alias(*dev
, *supply
);
1671 dev_dbg(*dev
, "Mapping supply %s to %s,%s\n",
1672 *supply
, map
->alias_supply
,
1673 dev_name(map
->alias_dev
));
1674 *dev
= map
->alias_dev
;
1675 *supply
= map
->alias_supply
;
1679 static int regulator_match(struct device
*dev
, const void *data
)
1681 struct regulator_dev
*r
= dev_to_rdev(dev
);
1683 return strcmp(rdev_get_name(r
), data
) == 0;
1686 static struct regulator_dev
*regulator_lookup_by_name(const char *name
)
1690 dev
= class_find_device(®ulator_class
, NULL
, name
, regulator_match
);
1692 return dev
? dev_to_rdev(dev
) : NULL
;
1696 * regulator_dev_lookup - lookup a regulator device.
1697 * @dev: device for regulator "consumer".
1698 * @supply: Supply name or regulator ID.
1700 * If successful, returns a struct regulator_dev that corresponds to the name
1701 * @supply and with the embedded struct device refcount incremented by one.
1702 * The refcount must be dropped by calling put_device().
1703 * On failure one of the following ERR-PTR-encoded values is returned:
1704 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1707 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1710 struct regulator_dev
*r
= NULL
;
1711 struct device_node
*node
;
1712 struct regulator_map
*map
;
1713 const char *devname
= NULL
;
1715 regulator_supply_alias(&dev
, &supply
);
1717 /* first do a dt based lookup */
1718 if (dev
&& dev
->of_node
) {
1719 node
= of_get_regulator(dev
, supply
);
1721 r
= of_find_regulator_by_node(node
);
1726 * We have a node, but there is no device.
1727 * assume it has not registered yet.
1729 return ERR_PTR(-EPROBE_DEFER
);
1733 /* if not found, try doing it non-dt way */
1735 devname
= dev_name(dev
);
1737 mutex_lock(®ulator_list_mutex
);
1738 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1739 /* If the mapping has a device set up it must match */
1740 if (map
->dev_name
&&
1741 (!devname
|| strcmp(map
->dev_name
, devname
)))
1744 if (strcmp(map
->supply
, supply
) == 0 &&
1745 get_device(&map
->regulator
->dev
)) {
1750 mutex_unlock(®ulator_list_mutex
);
1755 r
= regulator_lookup_by_name(supply
);
1759 return ERR_PTR(-ENODEV
);
1762 static int regulator_resolve_supply(struct regulator_dev
*rdev
)
1764 struct regulator_dev
*r
;
1765 struct device
*dev
= rdev
->dev
.parent
;
1768 /* No supply to resolve? */
1769 if (!rdev
->supply_name
)
1772 /* Supply already resolved? */
1776 r
= regulator_dev_lookup(dev
, rdev
->supply_name
);
1780 /* Did the lookup explicitly defer for us? */
1781 if (ret
== -EPROBE_DEFER
)
1784 if (have_full_constraints()) {
1785 r
= dummy_regulator_rdev
;
1786 get_device(&r
->dev
);
1788 dev_err(dev
, "Failed to resolve %s-supply for %s\n",
1789 rdev
->supply_name
, rdev
->desc
->name
);
1790 return -EPROBE_DEFER
;
1795 * If the supply's parent device is not the same as the
1796 * regulator's parent device, then ensure the parent device
1797 * is bound before we resolve the supply, in case the parent
1798 * device get probe deferred and unregisters the supply.
1800 if (r
->dev
.parent
&& r
->dev
.parent
!= rdev
->dev
.parent
) {
1801 if (!device_is_bound(r
->dev
.parent
)) {
1802 put_device(&r
->dev
);
1803 return -EPROBE_DEFER
;
1807 /* Recursively resolve the supply of the supply */
1808 ret
= regulator_resolve_supply(r
);
1810 put_device(&r
->dev
);
1814 ret
= set_supply(rdev
, r
);
1816 put_device(&r
->dev
);
1821 * In set_machine_constraints() we may have turned this regulator on
1822 * but we couldn't propagate to the supply if it hadn't been resolved
1825 if (rdev
->use_count
) {
1826 ret
= regulator_enable(rdev
->supply
);
1828 _regulator_put(rdev
->supply
);
1829 rdev
->supply
= NULL
;
1837 /* Internal regulator request function */
1838 struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1839 enum regulator_get_type get_type
)
1841 struct regulator_dev
*rdev
;
1842 struct regulator
*regulator
;
1843 const char *devname
= dev
? dev_name(dev
) : "deviceless";
1846 if (get_type
>= MAX_GET_TYPE
) {
1847 dev_err(dev
, "invalid type %d in %s\n", get_type
, __func__
);
1848 return ERR_PTR(-EINVAL
);
1852 pr_err("get() with no identifier\n");
1853 return ERR_PTR(-EINVAL
);
1856 rdev
= regulator_dev_lookup(dev
, id
);
1858 ret
= PTR_ERR(rdev
);
1861 * If regulator_dev_lookup() fails with error other
1862 * than -ENODEV our job here is done, we simply return it.
1865 return ERR_PTR(ret
);
1867 if (!have_full_constraints()) {
1869 "incomplete constraints, dummy supplies not allowed\n");
1870 return ERR_PTR(-ENODEV
);
1876 * Assume that a regulator is physically present and
1877 * enabled, even if it isn't hooked up, and just
1881 "%s supply %s not found, using dummy regulator\n",
1883 rdev
= dummy_regulator_rdev
;
1884 get_device(&rdev
->dev
);
1889 "dummy supplies not allowed for exclusive requests\n");
1893 return ERR_PTR(-ENODEV
);
1897 if (rdev
->exclusive
) {
1898 regulator
= ERR_PTR(-EPERM
);
1899 put_device(&rdev
->dev
);
1903 if (get_type
== EXCLUSIVE_GET
&& rdev
->open_count
) {
1904 regulator
= ERR_PTR(-EBUSY
);
1905 put_device(&rdev
->dev
);
1909 mutex_lock(®ulator_list_mutex
);
1910 ret
= (rdev
->coupling_desc
.n_resolved
!= rdev
->coupling_desc
.n_coupled
);
1911 mutex_unlock(®ulator_list_mutex
);
1914 regulator
= ERR_PTR(-EPROBE_DEFER
);
1915 put_device(&rdev
->dev
);
1919 ret
= regulator_resolve_supply(rdev
);
1921 regulator
= ERR_PTR(ret
);
1922 put_device(&rdev
->dev
);
1926 if (!try_module_get(rdev
->owner
)) {
1927 regulator
= ERR_PTR(-EPROBE_DEFER
);
1928 put_device(&rdev
->dev
);
1932 regulator
= create_regulator(rdev
, dev
, id
);
1933 if (regulator
== NULL
) {
1934 regulator
= ERR_PTR(-ENOMEM
);
1935 put_device(&rdev
->dev
);
1936 module_put(rdev
->owner
);
1941 if (get_type
== EXCLUSIVE_GET
) {
1942 rdev
->exclusive
= 1;
1944 ret
= _regulator_is_enabled(rdev
);
1946 rdev
->use_count
= 1;
1948 rdev
->use_count
= 0;
1951 device_link_add(dev
, &rdev
->dev
, DL_FLAG_STATELESS
);
1957 * regulator_get - lookup and obtain a reference to a regulator.
1958 * @dev: device for regulator "consumer"
1959 * @id: Supply name or regulator ID.
1961 * Returns a struct regulator corresponding to the regulator producer,
1962 * or IS_ERR() condition containing errno.
1964 * Use of supply names configured via regulator_set_device_supply() is
1965 * strongly encouraged. It is recommended that the supply name used
1966 * should match the name used for the supply and/or the relevant
1967 * device pins in the datasheet.
1969 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1971 return _regulator_get(dev
, id
, NORMAL_GET
);
1973 EXPORT_SYMBOL_GPL(regulator_get
);
1976 * regulator_get_exclusive - obtain exclusive access to a regulator.
1977 * @dev: device for regulator "consumer"
1978 * @id: Supply name or regulator ID.
1980 * Returns a struct regulator corresponding to the regulator producer,
1981 * or IS_ERR() condition containing errno. Other consumers will be
1982 * unable to obtain this regulator while this reference is held and the
1983 * use count for the regulator will be initialised to reflect the current
1984 * state of the regulator.
1986 * This is intended for use by consumers which cannot tolerate shared
1987 * use of the regulator such as those which need to force the
1988 * regulator off for correct operation of the hardware they are
1991 * Use of supply names configured via regulator_set_device_supply() is
1992 * strongly encouraged. It is recommended that the supply name used
1993 * should match the name used for the supply and/or the relevant
1994 * device pins in the datasheet.
1996 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1998 return _regulator_get(dev
, id
, EXCLUSIVE_GET
);
2000 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
2003 * regulator_get_optional - obtain optional access to a regulator.
2004 * @dev: device for regulator "consumer"
2005 * @id: Supply name or regulator ID.
2007 * Returns a struct regulator corresponding to the regulator producer,
2008 * or IS_ERR() condition containing errno.
2010 * This is intended for use by consumers for devices which can have
2011 * some supplies unconnected in normal use, such as some MMC devices.
2012 * It can allow the regulator core to provide stub supplies for other
2013 * supplies requested using normal regulator_get() calls without
2014 * disrupting the operation of drivers that can handle absent
2017 * Use of supply names configured via regulator_set_device_supply() is
2018 * strongly encouraged. It is recommended that the supply name used
2019 * should match the name used for the supply and/or the relevant
2020 * device pins in the datasheet.
2022 struct regulator
*regulator_get_optional(struct device
*dev
, const char *id
)
2024 return _regulator_get(dev
, id
, OPTIONAL_GET
);
2026 EXPORT_SYMBOL_GPL(regulator_get_optional
);
2028 /* regulator_list_mutex lock held by regulator_put() */
2029 static void _regulator_put(struct regulator
*regulator
)
2031 struct regulator_dev
*rdev
;
2033 if (IS_ERR_OR_NULL(regulator
))
2036 lockdep_assert_held_once(®ulator_list_mutex
);
2038 /* Docs say you must disable before calling regulator_put() */
2039 WARN_ON(regulator
->enable_count
);
2041 rdev
= regulator
->rdev
;
2043 debugfs_remove_recursive(regulator
->debugfs
);
2045 if (regulator
->dev
) {
2046 device_link_remove(regulator
->dev
, &rdev
->dev
);
2048 /* remove any sysfs entries */
2049 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
2052 regulator_lock(rdev
);
2053 list_del(®ulator
->list
);
2056 rdev
->exclusive
= 0;
2057 put_device(&rdev
->dev
);
2058 regulator_unlock(rdev
);
2060 kfree_const(regulator
->supply_name
);
2063 module_put(rdev
->owner
);
2067 * regulator_put - "free" the regulator source
2068 * @regulator: regulator source
2070 * Note: drivers must ensure that all regulator_enable calls made on this
2071 * regulator source are balanced by regulator_disable calls prior to calling
2074 void regulator_put(struct regulator
*regulator
)
2076 mutex_lock(®ulator_list_mutex
);
2077 _regulator_put(regulator
);
2078 mutex_unlock(®ulator_list_mutex
);
2080 EXPORT_SYMBOL_GPL(regulator_put
);
2083 * regulator_register_supply_alias - Provide device alias for supply lookup
2085 * @dev: device that will be given as the regulator "consumer"
2086 * @id: Supply name or regulator ID
2087 * @alias_dev: device that should be used to lookup the supply
2088 * @alias_id: Supply name or regulator ID that should be used to lookup the
2091 * All lookups for id on dev will instead be conducted for alias_id on
2094 int regulator_register_supply_alias(struct device
*dev
, const char *id
,
2095 struct device
*alias_dev
,
2096 const char *alias_id
)
2098 struct regulator_supply_alias
*map
;
2100 map
= regulator_find_supply_alias(dev
, id
);
2104 map
= kzalloc(sizeof(struct regulator_supply_alias
), GFP_KERNEL
);
2109 map
->src_supply
= id
;
2110 map
->alias_dev
= alias_dev
;
2111 map
->alias_supply
= alias_id
;
2113 list_add(&map
->list
, ®ulator_supply_alias_list
);
2115 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2116 id
, dev_name(dev
), alias_id
, dev_name(alias_dev
));
2120 EXPORT_SYMBOL_GPL(regulator_register_supply_alias
);
2123 * regulator_unregister_supply_alias - Remove device alias
2125 * @dev: device that will be given as the regulator "consumer"
2126 * @id: Supply name or regulator ID
2128 * Remove a lookup alias if one exists for id on dev.
2130 void regulator_unregister_supply_alias(struct device
*dev
, const char *id
)
2132 struct regulator_supply_alias
*map
;
2134 map
= regulator_find_supply_alias(dev
, id
);
2136 list_del(&map
->list
);
2140 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias
);
2143 * regulator_bulk_register_supply_alias - register multiple aliases
2145 * @dev: device that will be given as the regulator "consumer"
2146 * @id: List of supply names or regulator IDs
2147 * @alias_dev: device that should be used to lookup the supply
2148 * @alias_id: List of supply names or regulator IDs that should be used to
2150 * @num_id: Number of aliases to register
2152 * @return 0 on success, an errno on failure.
2154 * This helper function allows drivers to register several supply
2155 * aliases in one operation. If any of the aliases cannot be
2156 * registered any aliases that were registered will be removed
2157 * before returning to the caller.
2159 int regulator_bulk_register_supply_alias(struct device
*dev
,
2160 const char *const *id
,
2161 struct device
*alias_dev
,
2162 const char *const *alias_id
,
2168 for (i
= 0; i
< num_id
; ++i
) {
2169 ret
= regulator_register_supply_alias(dev
, id
[i
], alias_dev
,
2179 "Failed to create supply alias %s,%s -> %s,%s\n",
2180 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
2183 regulator_unregister_supply_alias(dev
, id
[i
]);
2187 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias
);
2190 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2192 * @dev: device that will be given as the regulator "consumer"
2193 * @id: List of supply names or regulator IDs
2194 * @num_id: Number of aliases to unregister
2196 * This helper function allows drivers to unregister several supply
2197 * aliases in one operation.
2199 void regulator_bulk_unregister_supply_alias(struct device
*dev
,
2200 const char *const *id
,
2205 for (i
= 0; i
< num_id
; ++i
)
2206 regulator_unregister_supply_alias(dev
, id
[i
]);
2208 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias
);
2211 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2212 static int regulator_ena_gpio_request(struct regulator_dev
*rdev
,
2213 const struct regulator_config
*config
)
2215 struct regulator_enable_gpio
*pin
;
2216 struct gpio_desc
*gpiod
;
2218 gpiod
= config
->ena_gpiod
;
2220 list_for_each_entry(pin
, ®ulator_ena_gpio_list
, list
) {
2221 if (pin
->gpiod
== gpiod
) {
2222 rdev_dbg(rdev
, "GPIO is already used\n");
2223 goto update_ena_gpio_to_rdev
;
2227 pin
= kzalloc(sizeof(struct regulator_enable_gpio
), GFP_KERNEL
);
2232 list_add(&pin
->list
, ®ulator_ena_gpio_list
);
2234 update_ena_gpio_to_rdev
:
2235 pin
->request_count
++;
2236 rdev
->ena_pin
= pin
;
2240 static void regulator_ena_gpio_free(struct regulator_dev
*rdev
)
2242 struct regulator_enable_gpio
*pin
, *n
;
2247 /* Free the GPIO only in case of no use */
2248 list_for_each_entry_safe(pin
, n
, ®ulator_ena_gpio_list
, list
) {
2249 if (pin
->gpiod
== rdev
->ena_pin
->gpiod
) {
2250 if (pin
->request_count
<= 1) {
2251 pin
->request_count
= 0;
2252 gpiod_put(pin
->gpiod
);
2253 list_del(&pin
->list
);
2255 rdev
->ena_pin
= NULL
;
2258 pin
->request_count
--;
2265 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2266 * @rdev: regulator_dev structure
2267 * @enable: enable GPIO at initial use?
2269 * GPIO is enabled in case of initial use. (enable_count is 0)
2270 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2272 static int regulator_ena_gpio_ctrl(struct regulator_dev
*rdev
, bool enable
)
2274 struct regulator_enable_gpio
*pin
= rdev
->ena_pin
;
2280 /* Enable GPIO at initial use */
2281 if (pin
->enable_count
== 0)
2282 gpiod_set_value_cansleep(pin
->gpiod
, 1);
2284 pin
->enable_count
++;
2286 if (pin
->enable_count
> 1) {
2287 pin
->enable_count
--;
2291 /* Disable GPIO if not used */
2292 if (pin
->enable_count
<= 1) {
2293 gpiod_set_value_cansleep(pin
->gpiod
, 0);
2294 pin
->enable_count
= 0;
2302 * _regulator_enable_delay - a delay helper function
2303 * @delay: time to delay in microseconds
2305 * Delay for the requested amount of time as per the guidelines in:
2307 * Documentation/timers/timers-howto.txt
2309 * The assumption here is that regulators will never be enabled in
2310 * atomic context and therefore sleeping functions can be used.
2312 static void _regulator_enable_delay(unsigned int delay
)
2314 unsigned int ms
= delay
/ 1000;
2315 unsigned int us
= delay
% 1000;
2319 * For small enough values, handle super-millisecond
2320 * delays in the usleep_range() call below.
2329 * Give the scheduler some room to coalesce with any other
2330 * wakeup sources. For delays shorter than 10 us, don't even
2331 * bother setting up high-resolution timers and just busy-
2335 usleep_range(us
, us
+ 100);
2340 static int _regulator_do_enable(struct regulator_dev
*rdev
)
2344 /* Query before enabling in case configuration dependent. */
2345 ret
= _regulator_get_enable_time(rdev
);
2349 rdev_warn(rdev
, "enable_time() failed: %d\n", ret
);
2353 trace_regulator_enable(rdev_get_name(rdev
));
2355 if (rdev
->desc
->off_on_delay
) {
2356 /* if needed, keep a distance of off_on_delay from last time
2357 * this regulator was disabled.
2359 unsigned long start_jiffy
= jiffies
;
2360 unsigned long intended
, max_delay
, remaining
;
2362 max_delay
= usecs_to_jiffies(rdev
->desc
->off_on_delay
);
2363 intended
= rdev
->last_off_jiffy
+ max_delay
;
2365 if (time_before(start_jiffy
, intended
)) {
2366 /* calc remaining jiffies to deal with one-time
2368 * in case of multiple timer wrapping, either it can be
2369 * detected by out-of-range remaining, or it cannot be
2370 * detected and we get a penalty of
2371 * _regulator_enable_delay().
2373 remaining
= intended
- start_jiffy
;
2374 if (remaining
<= max_delay
)
2375 _regulator_enable_delay(
2376 jiffies_to_usecs(remaining
));
2380 if (rdev
->ena_pin
) {
2381 if (!rdev
->ena_gpio_state
) {
2382 ret
= regulator_ena_gpio_ctrl(rdev
, true);
2385 rdev
->ena_gpio_state
= 1;
2387 } else if (rdev
->desc
->ops
->enable
) {
2388 ret
= rdev
->desc
->ops
->enable(rdev
);
2395 /* Allow the regulator to ramp; it would be useful to extend
2396 * this for bulk operations so that the regulators can ramp
2398 trace_regulator_enable_delay(rdev_get_name(rdev
));
2400 _regulator_enable_delay(delay
);
2402 trace_regulator_enable_complete(rdev_get_name(rdev
));
2408 * _regulator_handle_consumer_enable - handle that a consumer enabled
2409 * @regulator: regulator source
2411 * Some things on a regulator consumer (like the contribution towards total
2412 * load on the regulator) only have an effect when the consumer wants the
2413 * regulator enabled. Explained in example with two consumers of the same
2415 * consumer A: set_load(100); => total load = 0
2416 * consumer A: regulator_enable(); => total load = 100
2417 * consumer B: set_load(1000); => total load = 100
2418 * consumer B: regulator_enable(); => total load = 1100
2419 * consumer A: regulator_disable(); => total_load = 1000
2421 * This function (together with _regulator_handle_consumer_disable) is
2422 * responsible for keeping track of the refcount for a given regulator consumer
2423 * and applying / unapplying these things.
2425 * Returns 0 upon no error; -error upon error.
2427 static int _regulator_handle_consumer_enable(struct regulator
*regulator
)
2429 struct regulator_dev
*rdev
= regulator
->rdev
;
2431 lockdep_assert_held_once(&rdev
->mutex
.base
);
2433 regulator
->enable_count
++;
2434 if (regulator
->uA_load
&& regulator
->enable_count
== 1)
2435 return drms_uA_update(rdev
);
2441 * _regulator_handle_consumer_disable - handle that a consumer disabled
2442 * @regulator: regulator source
2444 * The opposite of _regulator_handle_consumer_enable().
2446 * Returns 0 upon no error; -error upon error.
2448 static int _regulator_handle_consumer_disable(struct regulator
*regulator
)
2450 struct regulator_dev
*rdev
= regulator
->rdev
;
2452 lockdep_assert_held_once(&rdev
->mutex
.base
);
2454 if (!regulator
->enable_count
) {
2455 rdev_err(rdev
, "Underflow of regulator enable count\n");
2459 regulator
->enable_count
--;
2460 if (regulator
->uA_load
&& regulator
->enable_count
== 0)
2461 return drms_uA_update(rdev
);
2466 /* locks held by regulator_enable() */
2467 static int _regulator_enable(struct regulator
*regulator
)
2469 struct regulator_dev
*rdev
= regulator
->rdev
;
2472 lockdep_assert_held_once(&rdev
->mutex
.base
);
2474 if (rdev
->use_count
== 0 && rdev
->supply
) {
2475 ret
= _regulator_enable(rdev
->supply
);
2480 /* balance only if there are regulators coupled */
2481 if (rdev
->coupling_desc
.n_coupled
> 1) {
2482 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2484 goto err_disable_supply
;
2487 ret
= _regulator_handle_consumer_enable(regulator
);
2489 goto err_disable_supply
;
2491 if (rdev
->use_count
== 0) {
2492 /* The regulator may on if it's not switchable or left on */
2493 ret
= _regulator_is_enabled(rdev
);
2494 if (ret
== -EINVAL
|| ret
== 0) {
2495 if (!regulator_ops_is_valid(rdev
,
2496 REGULATOR_CHANGE_STATUS
)) {
2498 goto err_consumer_disable
;
2501 ret
= _regulator_do_enable(rdev
);
2503 goto err_consumer_disable
;
2505 _notifier_call_chain(rdev
, REGULATOR_EVENT_ENABLE
,
2507 } else if (ret
< 0) {
2508 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
2509 goto err_consumer_disable
;
2511 /* Fallthrough on positive return values - already enabled */
2518 err_consumer_disable
:
2519 _regulator_handle_consumer_disable(regulator
);
2522 if (rdev
->use_count
== 0 && rdev
->supply
)
2523 _regulator_disable(rdev
->supply
);
2529 * regulator_enable - enable regulator output
2530 * @regulator: regulator source
2532 * Request that the regulator be enabled with the regulator output at
2533 * the predefined voltage or current value. Calls to regulator_enable()
2534 * must be balanced with calls to regulator_disable().
2536 * NOTE: the output value can be set by other drivers, boot loader or may be
2537 * hardwired in the regulator.
2539 int regulator_enable(struct regulator
*regulator
)
2541 struct regulator_dev
*rdev
= regulator
->rdev
;
2542 struct ww_acquire_ctx ww_ctx
;
2545 regulator_lock_dependent(rdev
, &ww_ctx
);
2546 ret
= _regulator_enable(regulator
);
2547 regulator_unlock_dependent(rdev
, &ww_ctx
);
2551 EXPORT_SYMBOL_GPL(regulator_enable
);
2553 static int _regulator_do_disable(struct regulator_dev
*rdev
)
2557 trace_regulator_disable(rdev_get_name(rdev
));
2559 if (rdev
->ena_pin
) {
2560 if (rdev
->ena_gpio_state
) {
2561 ret
= regulator_ena_gpio_ctrl(rdev
, false);
2564 rdev
->ena_gpio_state
= 0;
2567 } else if (rdev
->desc
->ops
->disable
) {
2568 ret
= rdev
->desc
->ops
->disable(rdev
);
2573 /* cares about last_off_jiffy only if off_on_delay is required by
2576 if (rdev
->desc
->off_on_delay
)
2577 rdev
->last_off_jiffy
= jiffies
;
2579 trace_regulator_disable_complete(rdev_get_name(rdev
));
2584 /* locks held by regulator_disable() */
2585 static int _regulator_disable(struct regulator
*regulator
)
2587 struct regulator_dev
*rdev
= regulator
->rdev
;
2590 lockdep_assert_held_once(&rdev
->mutex
.base
);
2592 if (WARN(rdev
->use_count
<= 0,
2593 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
2596 /* are we the last user and permitted to disable ? */
2597 if (rdev
->use_count
== 1 &&
2598 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
2600 /* we are last user */
2601 if (regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
)) {
2602 ret
= _notifier_call_chain(rdev
,
2603 REGULATOR_EVENT_PRE_DISABLE
,
2605 if (ret
& NOTIFY_STOP_MASK
)
2608 ret
= _regulator_do_disable(rdev
);
2610 rdev_err(rdev
, "failed to disable\n");
2611 _notifier_call_chain(rdev
,
2612 REGULATOR_EVENT_ABORT_DISABLE
,
2616 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
2620 rdev
->use_count
= 0;
2621 } else if (rdev
->use_count
> 1) {
2626 ret
= _regulator_handle_consumer_disable(regulator
);
2628 if (ret
== 0 && rdev
->coupling_desc
.n_coupled
> 1)
2629 ret
= regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2631 if (ret
== 0 && rdev
->use_count
== 0 && rdev
->supply
)
2632 ret
= _regulator_disable(rdev
->supply
);
2638 * regulator_disable - disable regulator output
2639 * @regulator: regulator source
2641 * Disable the regulator output voltage or current. Calls to
2642 * regulator_enable() must be balanced with calls to
2643 * regulator_disable().
2645 * NOTE: this will only disable the regulator output if no other consumer
2646 * devices have it enabled, the regulator device supports disabling and
2647 * machine constraints permit this operation.
2649 int regulator_disable(struct regulator
*regulator
)
2651 struct regulator_dev
*rdev
= regulator
->rdev
;
2652 struct ww_acquire_ctx ww_ctx
;
2655 regulator_lock_dependent(rdev
, &ww_ctx
);
2656 ret
= _regulator_disable(regulator
);
2657 regulator_unlock_dependent(rdev
, &ww_ctx
);
2661 EXPORT_SYMBOL_GPL(regulator_disable
);
2663 /* locks held by regulator_force_disable() */
2664 static int _regulator_force_disable(struct regulator_dev
*rdev
)
2668 lockdep_assert_held_once(&rdev
->mutex
.base
);
2670 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2671 REGULATOR_EVENT_PRE_DISABLE
, NULL
);
2672 if (ret
& NOTIFY_STOP_MASK
)
2675 ret
= _regulator_do_disable(rdev
);
2677 rdev_err(rdev
, "failed to force disable\n");
2678 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2679 REGULATOR_EVENT_ABORT_DISABLE
, NULL
);
2683 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2684 REGULATOR_EVENT_DISABLE
, NULL
);
2690 * regulator_force_disable - force disable regulator output
2691 * @regulator: regulator source
2693 * Forcibly disable the regulator output voltage or current.
2694 * NOTE: this *will* disable the regulator output even if other consumer
2695 * devices have it enabled. This should be used for situations when device
2696 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2698 int regulator_force_disable(struct regulator
*regulator
)
2700 struct regulator_dev
*rdev
= regulator
->rdev
;
2701 struct ww_acquire_ctx ww_ctx
;
2704 regulator_lock_dependent(rdev
, &ww_ctx
);
2706 ret
= _regulator_force_disable(regulator
->rdev
);
2708 if (rdev
->coupling_desc
.n_coupled
> 1)
2709 regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2711 if (regulator
->uA_load
) {
2712 regulator
->uA_load
= 0;
2713 ret
= drms_uA_update(rdev
);
2716 if (rdev
->use_count
!= 0 && rdev
->supply
)
2717 _regulator_disable(rdev
->supply
);
2719 regulator_unlock_dependent(rdev
, &ww_ctx
);
2723 EXPORT_SYMBOL_GPL(regulator_force_disable
);
2725 static void regulator_disable_work(struct work_struct
*work
)
2727 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
2729 struct ww_acquire_ctx ww_ctx
;
2731 struct regulator
*regulator
;
2732 int total_count
= 0;
2734 regulator_lock_dependent(rdev
, &ww_ctx
);
2737 * Workqueue functions queue the new work instance while the previous
2738 * work instance is being processed. Cancel the queued work instance
2739 * as the work instance under processing does the job of the queued
2742 cancel_delayed_work(&rdev
->disable_work
);
2744 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
2745 count
= regulator
->deferred_disables
;
2750 total_count
+= count
;
2751 regulator
->deferred_disables
= 0;
2753 for (i
= 0; i
< count
; i
++) {
2754 ret
= _regulator_disable(regulator
);
2756 rdev_err(rdev
, "Deferred disable failed: %d\n", ret
);
2759 WARN_ON(!total_count
);
2761 if (rdev
->coupling_desc
.n_coupled
> 1)
2762 regulator_balance_voltage(rdev
, PM_SUSPEND_ON
);
2764 regulator_unlock_dependent(rdev
, &ww_ctx
);
2768 * regulator_disable_deferred - disable regulator output with delay
2769 * @regulator: regulator source
2770 * @ms: milliseconds until the regulator is disabled
2772 * Execute regulator_disable() on the regulator after a delay. This
2773 * is intended for use with devices that require some time to quiesce.
2775 * NOTE: this will only disable the regulator output if no other consumer
2776 * devices have it enabled, the regulator device supports disabling and
2777 * machine constraints permit this operation.
2779 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
2781 struct regulator_dev
*rdev
= regulator
->rdev
;
2784 return regulator_disable(regulator
);
2786 regulator_lock(rdev
);
2787 regulator
->deferred_disables
++;
2788 mod_delayed_work(system_power_efficient_wq
, &rdev
->disable_work
,
2789 msecs_to_jiffies(ms
));
2790 regulator_unlock(rdev
);
2794 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
2796 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
2798 /* A GPIO control always takes precedence */
2800 return rdev
->ena_gpio_state
;
2802 /* If we don't know then assume that the regulator is always on */
2803 if (!rdev
->desc
->ops
->is_enabled
)
2806 return rdev
->desc
->ops
->is_enabled(rdev
);
2809 static int _regulator_list_voltage(struct regulator_dev
*rdev
,
2810 unsigned selector
, int lock
)
2812 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2815 if (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1 && !selector
)
2816 return rdev
->desc
->fixed_uV
;
2818 if (ops
->list_voltage
) {
2819 if (selector
>= rdev
->desc
->n_voltages
)
2822 regulator_lock(rdev
);
2823 ret
= ops
->list_voltage(rdev
, selector
);
2825 regulator_unlock(rdev
);
2826 } else if (rdev
->is_switch
&& rdev
->supply
) {
2827 ret
= _regulator_list_voltage(rdev
->supply
->rdev
,
2834 if (ret
< rdev
->constraints
->min_uV
)
2836 else if (ret
> rdev
->constraints
->max_uV
)
2844 * regulator_is_enabled - is the regulator output enabled
2845 * @regulator: regulator source
2847 * Returns positive if the regulator driver backing the source/client
2848 * has requested that the device be enabled, zero if it hasn't, else a
2849 * negative errno code.
2851 * Note that the device backing this regulator handle can have multiple
2852 * users, so it might be enabled even if regulator_enable() was never
2853 * called for this particular source.
2855 int regulator_is_enabled(struct regulator
*regulator
)
2859 if (regulator
->always_on
)
2862 regulator_lock(regulator
->rdev
);
2863 ret
= _regulator_is_enabled(regulator
->rdev
);
2864 regulator_unlock(regulator
->rdev
);
2868 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
2871 * regulator_count_voltages - count regulator_list_voltage() selectors
2872 * @regulator: regulator source
2874 * Returns number of selectors, or negative errno. Selectors are
2875 * numbered starting at zero, and typically correspond to bitfields
2876 * in hardware registers.
2878 int regulator_count_voltages(struct regulator
*regulator
)
2880 struct regulator_dev
*rdev
= regulator
->rdev
;
2882 if (rdev
->desc
->n_voltages
)
2883 return rdev
->desc
->n_voltages
;
2885 if (!rdev
->is_switch
|| !rdev
->supply
)
2888 return regulator_count_voltages(rdev
->supply
);
2890 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
2893 * regulator_list_voltage - enumerate supported voltages
2894 * @regulator: regulator source
2895 * @selector: identify voltage to list
2896 * Context: can sleep
2898 * Returns a voltage that can be passed to @regulator_set_voltage(),
2899 * zero if this selector code can't be used on this system, or a
2902 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
2904 return _regulator_list_voltage(regulator
->rdev
, selector
, 1);
2906 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
2909 * regulator_get_regmap - get the regulator's register map
2910 * @regulator: regulator source
2912 * Returns the register map for the given regulator, or an ERR_PTR value
2913 * if the regulator doesn't use regmap.
2915 struct regmap
*regulator_get_regmap(struct regulator
*regulator
)
2917 struct regmap
*map
= regulator
->rdev
->regmap
;
2919 return map
? map
: ERR_PTR(-EOPNOTSUPP
);
2923 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2924 * @regulator: regulator source
2925 * @vsel_reg: voltage selector register, output parameter
2926 * @vsel_mask: mask for voltage selector bitfield, output parameter
2928 * Returns the hardware register offset and bitmask used for setting the
2929 * regulator voltage. This might be useful when configuring voltage-scaling
2930 * hardware or firmware that can make I2C requests behind the kernel's back,
2933 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2934 * and 0 is returned, otherwise a negative errno is returned.
2936 int regulator_get_hardware_vsel_register(struct regulator
*regulator
,
2938 unsigned *vsel_mask
)
2940 struct regulator_dev
*rdev
= regulator
->rdev
;
2941 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2943 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2946 *vsel_reg
= rdev
->desc
->vsel_reg
;
2947 *vsel_mask
= rdev
->desc
->vsel_mask
;
2951 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register
);
2954 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2955 * @regulator: regulator source
2956 * @selector: identify voltage to list
2958 * Converts the selector to a hardware-specific voltage selector that can be
2959 * directly written to the regulator registers. The address of the voltage
2960 * register can be determined by calling @regulator_get_hardware_vsel_register.
2962 * On error a negative errno is returned.
2964 int regulator_list_hardware_vsel(struct regulator
*regulator
,
2967 struct regulator_dev
*rdev
= regulator
->rdev
;
2968 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2970 if (selector
>= rdev
->desc
->n_voltages
)
2972 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2977 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel
);
2980 * regulator_get_linear_step - return the voltage step size between VSEL values
2981 * @regulator: regulator source
2983 * Returns the voltage step size between VSEL values for linear
2984 * regulators, or return 0 if the regulator isn't a linear regulator.
2986 unsigned int regulator_get_linear_step(struct regulator
*regulator
)
2988 struct regulator_dev
*rdev
= regulator
->rdev
;
2990 return rdev
->desc
->uV_step
;
2992 EXPORT_SYMBOL_GPL(regulator_get_linear_step
);
2995 * regulator_is_supported_voltage - check if a voltage range can be supported
2997 * @regulator: Regulator to check.
2998 * @min_uV: Minimum required voltage in uV.
2999 * @max_uV: Maximum required voltage in uV.
3001 * Returns a boolean.
3003 int regulator_is_supported_voltage(struct regulator
*regulator
,
3004 int min_uV
, int max_uV
)
3006 struct regulator_dev
*rdev
= regulator
->rdev
;
3007 int i
, voltages
, ret
;
3009 /* If we can't change voltage check the current voltage */
3010 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
3011 ret
= regulator_get_voltage(regulator
);
3013 return min_uV
<= ret
&& ret
<= max_uV
;
3018 /* Any voltage within constrains range is fine? */
3019 if (rdev
->desc
->continuous_voltage_range
)
3020 return min_uV
>= rdev
->constraints
->min_uV
&&
3021 max_uV
<= rdev
->constraints
->max_uV
;
3023 ret
= regulator_count_voltages(regulator
);
3028 for (i
= 0; i
< voltages
; i
++) {
3029 ret
= regulator_list_voltage(regulator
, i
);
3031 if (ret
>= min_uV
&& ret
<= max_uV
)
3037 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
3039 static int regulator_map_voltage(struct regulator_dev
*rdev
, int min_uV
,
3042 const struct regulator_desc
*desc
= rdev
->desc
;
3044 if (desc
->ops
->map_voltage
)
3045 return desc
->ops
->map_voltage(rdev
, min_uV
, max_uV
);
3047 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear
)
3048 return regulator_map_voltage_linear(rdev
, min_uV
, max_uV
);
3050 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear_range
)
3051 return regulator_map_voltage_linear_range(rdev
, min_uV
, max_uV
);
3053 if (desc
->ops
->list_voltage
==
3054 regulator_list_voltage_pickable_linear_range
)
3055 return regulator_map_voltage_pickable_linear_range(rdev
,
3058 return regulator_map_voltage_iterate(rdev
, min_uV
, max_uV
);
3061 static int _regulator_call_set_voltage(struct regulator_dev
*rdev
,
3062 int min_uV
, int max_uV
,
3065 struct pre_voltage_change_data data
;
3068 data
.old_uV
= _regulator_get_voltage(rdev
);
3069 data
.min_uV
= min_uV
;
3070 data
.max_uV
= max_uV
;
3071 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
3073 if (ret
& NOTIFY_STOP_MASK
)
3076 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
, selector
);
3080 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
3081 (void *)data
.old_uV
);
3086 static int _regulator_call_set_voltage_sel(struct regulator_dev
*rdev
,
3087 int uV
, unsigned selector
)
3089 struct pre_voltage_change_data data
;
3092 data
.old_uV
= _regulator_get_voltage(rdev
);
3095 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
3097 if (ret
& NOTIFY_STOP_MASK
)
3100 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
3104 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
3105 (void *)data
.old_uV
);
3110 static int _regulator_set_voltage_time(struct regulator_dev
*rdev
,
3111 int old_uV
, int new_uV
)
3113 unsigned int ramp_delay
= 0;
3115 if (rdev
->constraints
->ramp_delay
)
3116 ramp_delay
= rdev
->constraints
->ramp_delay
;
3117 else if (rdev
->desc
->ramp_delay
)
3118 ramp_delay
= rdev
->desc
->ramp_delay
;
3119 else if (rdev
->constraints
->settling_time
)
3120 return rdev
->constraints
->settling_time
;
3121 else if (rdev
->constraints
->settling_time_up
&&
3123 return rdev
->constraints
->settling_time_up
;
3124 else if (rdev
->constraints
->settling_time_down
&&
3126 return rdev
->constraints
->settling_time_down
;
3128 if (ramp_delay
== 0) {
3129 rdev_dbg(rdev
, "ramp_delay not set\n");
3133 return DIV_ROUND_UP(abs(new_uV
- old_uV
), ramp_delay
);
3136 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
3137 int min_uV
, int max_uV
)
3142 unsigned int selector
;
3143 int old_selector
= -1;
3144 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3145 int old_uV
= _regulator_get_voltage(rdev
);
3147 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
3149 min_uV
+= rdev
->constraints
->uV_offset
;
3150 max_uV
+= rdev
->constraints
->uV_offset
;
3153 * If we can't obtain the old selector there is not enough
3154 * info to call set_voltage_time_sel().
3156 if (_regulator_is_enabled(rdev
) &&
3157 ops
->set_voltage_time_sel
&& ops
->get_voltage_sel
) {
3158 old_selector
= ops
->get_voltage_sel(rdev
);
3159 if (old_selector
< 0)
3160 return old_selector
;
3163 if (ops
->set_voltage
) {
3164 ret
= _regulator_call_set_voltage(rdev
, min_uV
, max_uV
,
3168 if (ops
->list_voltage
)
3169 best_val
= ops
->list_voltage(rdev
,
3172 best_val
= _regulator_get_voltage(rdev
);
3175 } else if (ops
->set_voltage_sel
) {
3176 ret
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3178 best_val
= ops
->list_voltage(rdev
, ret
);
3179 if (min_uV
<= best_val
&& max_uV
>= best_val
) {
3181 if (old_selector
== selector
)
3184 ret
= _regulator_call_set_voltage_sel(
3185 rdev
, best_val
, selector
);
3197 if (ops
->set_voltage_time_sel
) {
3199 * Call set_voltage_time_sel if successfully obtained
3202 if (old_selector
>= 0 && old_selector
!= selector
)
3203 delay
= ops
->set_voltage_time_sel(rdev
, old_selector
,
3206 if (old_uV
!= best_val
) {
3207 if (ops
->set_voltage_time
)
3208 delay
= ops
->set_voltage_time(rdev
, old_uV
,
3211 delay
= _regulator_set_voltage_time(rdev
,
3218 rdev_warn(rdev
, "failed to get delay: %d\n", delay
);
3222 /* Insert any necessary delays */
3223 if (delay
>= 1000) {
3224 mdelay(delay
/ 1000);
3225 udelay(delay
% 1000);
3230 if (best_val
>= 0) {
3231 unsigned long data
= best_val
;
3233 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
3238 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), best_val
);
3243 static int _regulator_do_set_suspend_voltage(struct regulator_dev
*rdev
,
3244 int min_uV
, int max_uV
, suspend_state_t state
)
3246 struct regulator_state
*rstate
;
3249 rstate
= regulator_get_suspend_state(rdev
, state
);
3253 if (min_uV
< rstate
->min_uV
)
3254 min_uV
= rstate
->min_uV
;
3255 if (max_uV
> rstate
->max_uV
)
3256 max_uV
= rstate
->max_uV
;
3258 sel
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3262 uV
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
3263 if (uV
>= min_uV
&& uV
<= max_uV
)
3269 static int regulator_set_voltage_unlocked(struct regulator
*regulator
,
3270 int min_uV
, int max_uV
,
3271 suspend_state_t state
)
3273 struct regulator_dev
*rdev
= regulator
->rdev
;
3274 struct regulator_voltage
*voltage
= ®ulator
->voltage
[state
];
3276 int old_min_uV
, old_max_uV
;
3279 /* If we're setting the same range as last time the change
3280 * should be a noop (some cpufreq implementations use the same
3281 * voltage for multiple frequencies, for example).
3283 if (voltage
->min_uV
== min_uV
&& voltage
->max_uV
== max_uV
)
3286 /* If we're trying to set a range that overlaps the current voltage,
3287 * return successfully even though the regulator does not support
3288 * changing the voltage.
3290 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
3291 current_uV
= _regulator_get_voltage(rdev
);
3292 if (min_uV
<= current_uV
&& current_uV
<= max_uV
) {
3293 voltage
->min_uV
= min_uV
;
3294 voltage
->max_uV
= max_uV
;
3300 if (!rdev
->desc
->ops
->set_voltage
&&
3301 !rdev
->desc
->ops
->set_voltage_sel
) {
3306 /* constraints check */
3307 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
3311 /* restore original values in case of error */
3312 old_min_uV
= voltage
->min_uV
;
3313 old_max_uV
= voltage
->max_uV
;
3314 voltage
->min_uV
= min_uV
;
3315 voltage
->max_uV
= max_uV
;
3317 /* for not coupled regulators this will just set the voltage */
3318 ret
= regulator_balance_voltage(rdev
, state
);
3320 voltage
->min_uV
= old_min_uV
;
3321 voltage
->max_uV
= old_max_uV
;
3328 static int regulator_set_voltage_rdev(struct regulator_dev
*rdev
, int min_uV
,
3329 int max_uV
, suspend_state_t state
)
3331 int best_supply_uV
= 0;
3332 int supply_change_uV
= 0;
3336 regulator_ops_is_valid(rdev
->supply
->rdev
,
3337 REGULATOR_CHANGE_VOLTAGE
) &&
3338 (rdev
->desc
->min_dropout_uV
|| !(rdev
->desc
->ops
->get_voltage
||
3339 rdev
->desc
->ops
->get_voltage_sel
))) {
3340 int current_supply_uV
;
3343 selector
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
3349 best_supply_uV
= _regulator_list_voltage(rdev
, selector
, 0);
3350 if (best_supply_uV
< 0) {
3351 ret
= best_supply_uV
;
3355 best_supply_uV
+= rdev
->desc
->min_dropout_uV
;
3357 current_supply_uV
= _regulator_get_voltage(rdev
->supply
->rdev
);
3358 if (current_supply_uV
< 0) {
3359 ret
= current_supply_uV
;
3363 supply_change_uV
= best_supply_uV
- current_supply_uV
;
3366 if (supply_change_uV
> 0) {
3367 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
3368 best_supply_uV
, INT_MAX
, state
);
3370 dev_err(&rdev
->dev
, "Failed to increase supply voltage: %d\n",
3376 if (state
== PM_SUSPEND_ON
)
3377 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
3379 ret
= _regulator_do_set_suspend_voltage(rdev
, min_uV
,
3384 if (supply_change_uV
< 0) {
3385 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
3386 best_supply_uV
, INT_MAX
, state
);
3388 dev_warn(&rdev
->dev
, "Failed to decrease supply voltage: %d\n",
3390 /* No need to fail here */
3398 static int regulator_limit_voltage_step(struct regulator_dev
*rdev
,
3399 int *current_uV
, int *min_uV
)
3401 struct regulation_constraints
*constraints
= rdev
->constraints
;
3403 /* Limit voltage change only if necessary */
3404 if (!constraints
->max_uV_step
|| !_regulator_is_enabled(rdev
))
3407 if (*current_uV
< 0) {
3408 *current_uV
= _regulator_get_voltage(rdev
);
3410 if (*current_uV
< 0)
3414 if (abs(*current_uV
- *min_uV
) <= constraints
->max_uV_step
)
3417 /* Clamp target voltage within the given step */
3418 if (*current_uV
< *min_uV
)
3419 *min_uV
= min(*current_uV
+ constraints
->max_uV_step
,
3422 *min_uV
= max(*current_uV
- constraints
->max_uV_step
,
3428 static int regulator_get_optimal_voltage(struct regulator_dev
*rdev
,
3430 int *min_uV
, int *max_uV
,
3431 suspend_state_t state
,
3434 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
3435 struct regulator_dev
**c_rdevs
= c_desc
->coupled_rdevs
;
3436 struct regulation_constraints
*constraints
= rdev
->constraints
;
3437 int max_spread
= constraints
->max_spread
;
3438 int desired_min_uV
= 0, desired_max_uV
= INT_MAX
;
3439 int max_current_uV
= 0, min_current_uV
= INT_MAX
;
3440 int highest_min_uV
= 0, target_uV
, possible_uV
;
3447 * If there are no coupled regulators, simply set the voltage
3448 * demanded by consumers.
3450 if (n_coupled
== 1) {
3452 * If consumers don't provide any demands, set voltage
3455 desired_min_uV
= constraints
->min_uV
;
3456 desired_max_uV
= constraints
->max_uV
;
3458 ret
= regulator_check_consumers(rdev
,
3460 &desired_max_uV
, state
);
3464 possible_uV
= desired_min_uV
;
3470 /* Find highest min desired voltage */
3471 for (i
= 0; i
< n_coupled
; i
++) {
3473 int tmp_max
= INT_MAX
;
3475 lockdep_assert_held_once(&c_rdevs
[i
]->mutex
.base
);
3477 ret
= regulator_check_consumers(c_rdevs
[i
],
3483 ret
= regulator_check_voltage(c_rdevs
[i
], &tmp_min
, &tmp_max
);
3487 highest_min_uV
= max(highest_min_uV
, tmp_min
);
3490 desired_min_uV
= tmp_min
;
3491 desired_max_uV
= tmp_max
;
3496 * Let target_uV be equal to the desired one if possible.
3497 * If not, set it to minimum voltage, allowed by other coupled
3500 target_uV
= max(desired_min_uV
, highest_min_uV
- max_spread
);
3503 * Find min and max voltages, which currently aren't violating
3506 for (i
= 1; i
< n_coupled
; i
++) {
3509 if (!_regulator_is_enabled(c_rdevs
[i
]))
3512 tmp_act
= _regulator_get_voltage(c_rdevs
[i
]);
3516 min_current_uV
= min(tmp_act
, min_current_uV
);
3517 max_current_uV
= max(tmp_act
, max_current_uV
);
3520 /* There aren't any other regulators enabled */
3521 if (max_current_uV
== 0) {
3522 possible_uV
= target_uV
;
3525 * Correct target voltage, so as it currently isn't
3526 * violating max_spread
3528 possible_uV
= max(target_uV
, max_current_uV
- max_spread
);
3529 possible_uV
= min(possible_uV
, min_current_uV
+ max_spread
);
3532 if (possible_uV
> desired_max_uV
)
3535 done
= (possible_uV
== target_uV
);
3536 desired_min_uV
= possible_uV
;
3539 /* Apply max_uV_step constraint if necessary */
3540 if (state
== PM_SUSPEND_ON
) {
3541 ret
= regulator_limit_voltage_step(rdev
, current_uV
,
3550 /* Set current_uV if wasn't done earlier in the code and if necessary */
3551 if (n_coupled
> 1 && *current_uV
== -1) {
3553 if (_regulator_is_enabled(rdev
)) {
3554 ret
= _regulator_get_voltage(rdev
);
3560 *current_uV
= desired_min_uV
;
3564 *min_uV
= desired_min_uV
;
3565 *max_uV
= desired_max_uV
;
3570 static int regulator_balance_voltage(struct regulator_dev
*rdev
,
3571 suspend_state_t state
)
3573 struct regulator_dev
**c_rdevs
;
3574 struct regulator_dev
*best_rdev
;
3575 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
3576 int i
, ret
, n_coupled
, best_min_uV
, best_max_uV
, best_c_rdev
;
3577 bool best_c_rdev_done
, c_rdev_done
[MAX_COUPLED
];
3578 unsigned int delta
, best_delta
;
3580 c_rdevs
= c_desc
->coupled_rdevs
;
3581 n_coupled
= c_desc
->n_coupled
;
3584 * If system is in a state other than PM_SUSPEND_ON, don't check
3585 * other coupled regulators.
3587 if (state
!= PM_SUSPEND_ON
)
3590 if (c_desc
->n_resolved
< n_coupled
) {
3591 rdev_err(rdev
, "Not all coupled regulators registered\n");
3595 for (i
= 0; i
< n_coupled
; i
++)
3596 c_rdev_done
[i
] = false;
3599 * Find the best possible voltage change on each loop. Leave the loop
3600 * if there isn't any possible change.
3603 best_c_rdev_done
= false;
3611 * Find highest difference between optimal voltage
3612 * and current voltage.
3614 for (i
= 0; i
< n_coupled
; i
++) {
3616 * optimal_uV is the best voltage that can be set for
3617 * i-th regulator at the moment without violating
3618 * max_spread constraint in order to balance
3619 * the coupled voltages.
3621 int optimal_uV
= 0, optimal_max_uV
= 0, current_uV
= 0;
3626 ret
= regulator_get_optimal_voltage(c_rdevs
[i
],
3634 delta
= abs(optimal_uV
- current_uV
);
3636 if (delta
&& best_delta
<= delta
) {
3637 best_c_rdev_done
= ret
;
3639 best_rdev
= c_rdevs
[i
];
3640 best_min_uV
= optimal_uV
;
3641 best_max_uV
= optimal_max_uV
;
3646 /* Nothing to change, return successfully */
3652 ret
= regulator_set_voltage_rdev(best_rdev
, best_min_uV
,
3653 best_max_uV
, state
);
3658 c_rdev_done
[best_c_rdev
] = best_c_rdev_done
;
3660 } while (n_coupled
> 1);
3667 * regulator_set_voltage - set regulator output voltage
3668 * @regulator: regulator source
3669 * @min_uV: Minimum required voltage in uV
3670 * @max_uV: Maximum acceptable voltage in uV
3672 * Sets a voltage regulator to the desired output voltage. This can be set
3673 * during any regulator state. IOW, regulator can be disabled or enabled.
3675 * If the regulator is enabled then the voltage will change to the new value
3676 * immediately otherwise if the regulator is disabled the regulator will
3677 * output at the new voltage when enabled.
3679 * NOTE: If the regulator is shared between several devices then the lowest
3680 * request voltage that meets the system constraints will be used.
3681 * Regulator system constraints must be set for this regulator before
3682 * calling this function otherwise this call will fail.
3684 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
3686 struct ww_acquire_ctx ww_ctx
;
3689 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
3691 ret
= regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
,
3694 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
3698 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
3700 static inline int regulator_suspend_toggle(struct regulator_dev
*rdev
,
3701 suspend_state_t state
, bool en
)
3703 struct regulator_state
*rstate
;
3705 rstate
= regulator_get_suspend_state(rdev
, state
);
3709 if (!rstate
->changeable
)
3712 rstate
->enabled
= (en
) ? ENABLE_IN_SUSPEND
: DISABLE_IN_SUSPEND
;
3717 int regulator_suspend_enable(struct regulator_dev
*rdev
,
3718 suspend_state_t state
)
3720 return regulator_suspend_toggle(rdev
, state
, true);
3722 EXPORT_SYMBOL_GPL(regulator_suspend_enable
);
3724 int regulator_suspend_disable(struct regulator_dev
*rdev
,
3725 suspend_state_t state
)
3727 struct regulator
*regulator
;
3728 struct regulator_voltage
*voltage
;
3731 * if any consumer wants this regulator device keeping on in
3732 * suspend states, don't set it as disabled.
3734 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
3735 voltage
= ®ulator
->voltage
[state
];
3736 if (voltage
->min_uV
|| voltage
->max_uV
)
3740 return regulator_suspend_toggle(rdev
, state
, false);
3742 EXPORT_SYMBOL_GPL(regulator_suspend_disable
);
3744 static int _regulator_set_suspend_voltage(struct regulator
*regulator
,
3745 int min_uV
, int max_uV
,
3746 suspend_state_t state
)
3748 struct regulator_dev
*rdev
= regulator
->rdev
;
3749 struct regulator_state
*rstate
;
3751 rstate
= regulator_get_suspend_state(rdev
, state
);
3755 if (rstate
->min_uV
== rstate
->max_uV
) {
3756 rdev_err(rdev
, "The suspend voltage can't be changed!\n");
3760 return regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
, state
);
3763 int regulator_set_suspend_voltage(struct regulator
*regulator
, int min_uV
,
3764 int max_uV
, suspend_state_t state
)
3766 struct ww_acquire_ctx ww_ctx
;
3769 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3770 if (regulator_check_states(state
) || state
== PM_SUSPEND_ON
)
3773 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
3775 ret
= _regulator_set_suspend_voltage(regulator
, min_uV
,
3778 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
3782 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage
);
3785 * regulator_set_voltage_time - get raise/fall time
3786 * @regulator: regulator source
3787 * @old_uV: starting voltage in microvolts
3788 * @new_uV: target voltage in microvolts
3790 * Provided with the starting and ending voltage, this function attempts to
3791 * calculate the time in microseconds required to rise or fall to this new
3794 int regulator_set_voltage_time(struct regulator
*regulator
,
3795 int old_uV
, int new_uV
)
3797 struct regulator_dev
*rdev
= regulator
->rdev
;
3798 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3804 if (ops
->set_voltage_time
)
3805 return ops
->set_voltage_time(rdev
, old_uV
, new_uV
);
3806 else if (!ops
->set_voltage_time_sel
)
3807 return _regulator_set_voltage_time(rdev
, old_uV
, new_uV
);
3809 /* Currently requires operations to do this */
3810 if (!ops
->list_voltage
|| !rdev
->desc
->n_voltages
)
3813 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
3814 /* We only look for exact voltage matches here */
3815 voltage
= regulator_list_voltage(regulator
, i
);
3820 if (voltage
== old_uV
)
3822 if (voltage
== new_uV
)
3826 if (old_sel
< 0 || new_sel
< 0)
3829 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
3831 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
3834 * regulator_set_voltage_time_sel - get raise/fall time
3835 * @rdev: regulator source device
3836 * @old_selector: selector for starting voltage
3837 * @new_selector: selector for target voltage
3839 * Provided with the starting and target voltage selectors, this function
3840 * returns time in microseconds required to rise or fall to this new voltage
3842 * Drivers providing ramp_delay in regulation_constraints can use this as their
3843 * set_voltage_time_sel() operation.
3845 int regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
3846 unsigned int old_selector
,
3847 unsigned int new_selector
)
3849 int old_volt
, new_volt
;
3852 if (!rdev
->desc
->ops
->list_voltage
)
3855 old_volt
= rdev
->desc
->ops
->list_voltage(rdev
, old_selector
);
3856 new_volt
= rdev
->desc
->ops
->list_voltage(rdev
, new_selector
);
3858 if (rdev
->desc
->ops
->set_voltage_time
)
3859 return rdev
->desc
->ops
->set_voltage_time(rdev
, old_volt
,
3862 return _regulator_set_voltage_time(rdev
, old_volt
, new_volt
);
3864 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel
);
3867 * regulator_sync_voltage - re-apply last regulator output voltage
3868 * @regulator: regulator source
3870 * Re-apply the last configured voltage. This is intended to be used
3871 * where some external control source the consumer is cooperating with
3872 * has caused the configured voltage to change.
3874 int regulator_sync_voltage(struct regulator
*regulator
)
3876 struct regulator_dev
*rdev
= regulator
->rdev
;
3877 struct regulator_voltage
*voltage
= ®ulator
->voltage
[PM_SUSPEND_ON
];
3878 int ret
, min_uV
, max_uV
;
3880 regulator_lock(rdev
);
3882 if (!rdev
->desc
->ops
->set_voltage
&&
3883 !rdev
->desc
->ops
->set_voltage_sel
) {
3888 /* This is only going to work if we've had a voltage configured. */
3889 if (!voltage
->min_uV
&& !voltage
->max_uV
) {
3894 min_uV
= voltage
->min_uV
;
3895 max_uV
= voltage
->max_uV
;
3897 /* This should be a paranoia check... */
3898 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
3902 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
, 0);
3906 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
3909 regulator_unlock(rdev
);
3912 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
3914 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
3919 if (rdev
->desc
->ops
->get_bypass
) {
3920 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypassed
);
3924 /* if bypassed the regulator must have a supply */
3925 if (!rdev
->supply
) {
3927 "bypassed regulator has no supply!\n");
3928 return -EPROBE_DEFER
;
3931 return _regulator_get_voltage(rdev
->supply
->rdev
);
3935 if (rdev
->desc
->ops
->get_voltage_sel
) {
3936 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
3939 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
3940 } else if (rdev
->desc
->ops
->get_voltage
) {
3941 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
3942 } else if (rdev
->desc
->ops
->list_voltage
) {
3943 ret
= rdev
->desc
->ops
->list_voltage(rdev
, 0);
3944 } else if (rdev
->desc
->fixed_uV
&& (rdev
->desc
->n_voltages
== 1)) {
3945 ret
= rdev
->desc
->fixed_uV
;
3946 } else if (rdev
->supply
) {
3947 ret
= _regulator_get_voltage(rdev
->supply
->rdev
);
3954 return ret
- rdev
->constraints
->uV_offset
;
3958 * regulator_get_voltage - get regulator output voltage
3959 * @regulator: regulator source
3961 * This returns the current regulator voltage in uV.
3963 * NOTE: If the regulator is disabled it will return the voltage value. This
3964 * function should not be used to determine regulator state.
3966 int regulator_get_voltage(struct regulator
*regulator
)
3968 struct ww_acquire_ctx ww_ctx
;
3971 regulator_lock_dependent(regulator
->rdev
, &ww_ctx
);
3972 ret
= _regulator_get_voltage(regulator
->rdev
);
3973 regulator_unlock_dependent(regulator
->rdev
, &ww_ctx
);
3977 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
3980 * regulator_set_current_limit - set regulator output current limit
3981 * @regulator: regulator source
3982 * @min_uA: Minimum supported current in uA
3983 * @max_uA: Maximum supported current in uA
3985 * Sets current sink to the desired output current. This can be set during
3986 * any regulator state. IOW, regulator can be disabled or enabled.
3988 * If the regulator is enabled then the current will change to the new value
3989 * immediately otherwise if the regulator is disabled the regulator will
3990 * output at the new current when enabled.
3992 * NOTE: Regulator system constraints must be set for this regulator before
3993 * calling this function otherwise this call will fail.
3995 int regulator_set_current_limit(struct regulator
*regulator
,
3996 int min_uA
, int max_uA
)
3998 struct regulator_dev
*rdev
= regulator
->rdev
;
4001 regulator_lock(rdev
);
4004 if (!rdev
->desc
->ops
->set_current_limit
) {
4009 /* constraints check */
4010 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
4014 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
4016 regulator_unlock(rdev
);
4019 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
4021 static int _regulator_get_current_limit_unlocked(struct regulator_dev
*rdev
)
4024 if (!rdev
->desc
->ops
->get_current_limit
)
4027 return rdev
->desc
->ops
->get_current_limit(rdev
);
4030 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
4034 regulator_lock(rdev
);
4035 ret
= _regulator_get_current_limit_unlocked(rdev
);
4036 regulator_unlock(rdev
);
4042 * regulator_get_current_limit - get regulator output current
4043 * @regulator: regulator source
4045 * This returns the current supplied by the specified current sink in uA.
4047 * NOTE: If the regulator is disabled it will return the current value. This
4048 * function should not be used to determine regulator state.
4050 int regulator_get_current_limit(struct regulator
*regulator
)
4052 return _regulator_get_current_limit(regulator
->rdev
);
4054 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
4057 * regulator_set_mode - set regulator operating mode
4058 * @regulator: regulator source
4059 * @mode: operating mode - one of the REGULATOR_MODE constants
4061 * Set regulator operating mode to increase regulator efficiency or improve
4062 * regulation performance.
4064 * NOTE: Regulator system constraints must be set for this regulator before
4065 * calling this function otherwise this call will fail.
4067 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
4069 struct regulator_dev
*rdev
= regulator
->rdev
;
4071 int regulator_curr_mode
;
4073 regulator_lock(rdev
);
4076 if (!rdev
->desc
->ops
->set_mode
) {
4081 /* return if the same mode is requested */
4082 if (rdev
->desc
->ops
->get_mode
) {
4083 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
4084 if (regulator_curr_mode
== mode
) {
4090 /* constraints check */
4091 ret
= regulator_mode_constrain(rdev
, &mode
);
4095 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
4097 regulator_unlock(rdev
);
4100 EXPORT_SYMBOL_GPL(regulator_set_mode
);
4102 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev
*rdev
)
4105 if (!rdev
->desc
->ops
->get_mode
)
4108 return rdev
->desc
->ops
->get_mode(rdev
);
4111 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
4115 regulator_lock(rdev
);
4116 ret
= _regulator_get_mode_unlocked(rdev
);
4117 regulator_unlock(rdev
);
4123 * regulator_get_mode - get regulator operating mode
4124 * @regulator: regulator source
4126 * Get the current regulator operating mode.
4128 unsigned int regulator_get_mode(struct regulator
*regulator
)
4130 return _regulator_get_mode(regulator
->rdev
);
4132 EXPORT_SYMBOL_GPL(regulator_get_mode
);
4134 static int _regulator_get_error_flags(struct regulator_dev
*rdev
,
4135 unsigned int *flags
)
4139 regulator_lock(rdev
);
4142 if (!rdev
->desc
->ops
->get_error_flags
) {
4147 ret
= rdev
->desc
->ops
->get_error_flags(rdev
, flags
);
4149 regulator_unlock(rdev
);
4154 * regulator_get_error_flags - get regulator error information
4155 * @regulator: regulator source
4156 * @flags: pointer to store error flags
4158 * Get the current regulator error information.
4160 int regulator_get_error_flags(struct regulator
*regulator
,
4161 unsigned int *flags
)
4163 return _regulator_get_error_flags(regulator
->rdev
, flags
);
4165 EXPORT_SYMBOL_GPL(regulator_get_error_flags
);
4168 * regulator_set_load - set regulator load
4169 * @regulator: regulator source
4170 * @uA_load: load current
4172 * Notifies the regulator core of a new device load. This is then used by
4173 * DRMS (if enabled by constraints) to set the most efficient regulator
4174 * operating mode for the new regulator loading.
4176 * Consumer devices notify their supply regulator of the maximum power
4177 * they will require (can be taken from device datasheet in the power
4178 * consumption tables) when they change operational status and hence power
4179 * state. Examples of operational state changes that can affect power
4180 * consumption are :-
4182 * o Device is opened / closed.
4183 * o Device I/O is about to begin or has just finished.
4184 * o Device is idling in between work.
4186 * This information is also exported via sysfs to userspace.
4188 * DRMS will sum the total requested load on the regulator and change
4189 * to the most efficient operating mode if platform constraints allow.
4191 * NOTE: when a regulator consumer requests to have a regulator
4192 * disabled then any load that consumer requested no longer counts
4193 * toward the total requested load. If the regulator is re-enabled
4194 * then the previously requested load will start counting again.
4196 * If a regulator is an always-on regulator then an individual consumer's
4197 * load will still be removed if that consumer is fully disabled.
4199 * On error a negative errno is returned.
4201 int regulator_set_load(struct regulator
*regulator
, int uA_load
)
4203 struct regulator_dev
*rdev
= regulator
->rdev
;
4207 regulator_lock(rdev
);
4208 old_uA_load
= regulator
->uA_load
;
4209 regulator
->uA_load
= uA_load
;
4210 if (regulator
->enable_count
&& old_uA_load
!= uA_load
) {
4211 ret
= drms_uA_update(rdev
);
4213 regulator
->uA_load
= old_uA_load
;
4215 regulator_unlock(rdev
);
4219 EXPORT_SYMBOL_GPL(regulator_set_load
);
4222 * regulator_allow_bypass - allow the regulator to go into bypass mode
4224 * @regulator: Regulator to configure
4225 * @enable: enable or disable bypass mode
4227 * Allow the regulator to go into bypass mode if all other consumers
4228 * for the regulator also enable bypass mode and the machine
4229 * constraints allow this. Bypass mode means that the regulator is
4230 * simply passing the input directly to the output with no regulation.
4232 int regulator_allow_bypass(struct regulator
*regulator
, bool enable
)
4234 struct regulator_dev
*rdev
= regulator
->rdev
;
4237 if (!rdev
->desc
->ops
->set_bypass
)
4240 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_BYPASS
))
4243 regulator_lock(rdev
);
4245 if (enable
&& !regulator
->bypass
) {
4246 rdev
->bypass_count
++;
4248 if (rdev
->bypass_count
== rdev
->open_count
) {
4249 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
4251 rdev
->bypass_count
--;
4254 } else if (!enable
&& regulator
->bypass
) {
4255 rdev
->bypass_count
--;
4257 if (rdev
->bypass_count
!= rdev
->open_count
) {
4258 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
4260 rdev
->bypass_count
++;
4265 regulator
->bypass
= enable
;
4267 regulator_unlock(rdev
);
4271 EXPORT_SYMBOL_GPL(regulator_allow_bypass
);
4274 * regulator_register_notifier - register regulator event notifier
4275 * @regulator: regulator source
4276 * @nb: notifier block
4278 * Register notifier block to receive regulator events.
4280 int regulator_register_notifier(struct regulator
*regulator
,
4281 struct notifier_block
*nb
)
4283 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
4286 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
4289 * regulator_unregister_notifier - unregister regulator event notifier
4290 * @regulator: regulator source
4291 * @nb: notifier block
4293 * Unregister regulator event notifier block.
4295 int regulator_unregister_notifier(struct regulator
*regulator
,
4296 struct notifier_block
*nb
)
4298 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
4301 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
4303 /* notify regulator consumers and downstream regulator consumers.
4304 * Note mutex must be held by caller.
4306 static int _notifier_call_chain(struct regulator_dev
*rdev
,
4307 unsigned long event
, void *data
)
4309 /* call rdev chain first */
4310 return blocking_notifier_call_chain(&rdev
->notifier
, event
, data
);
4314 * regulator_bulk_get - get multiple regulator consumers
4316 * @dev: Device to supply
4317 * @num_consumers: Number of consumers to register
4318 * @consumers: Configuration of consumers; clients are stored here.
4320 * @return 0 on success, an errno on failure.
4322 * This helper function allows drivers to get several regulator
4323 * consumers in one operation. If any of the regulators cannot be
4324 * acquired then any regulators that were allocated will be freed
4325 * before returning to the caller.
4327 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
4328 struct regulator_bulk_data
*consumers
)
4333 for (i
= 0; i
< num_consumers
; i
++)
4334 consumers
[i
].consumer
= NULL
;
4336 for (i
= 0; i
< num_consumers
; i
++) {
4337 consumers
[i
].consumer
= regulator_get(dev
,
4338 consumers
[i
].supply
);
4339 if (IS_ERR(consumers
[i
].consumer
)) {
4340 ret
= PTR_ERR(consumers
[i
].consumer
);
4341 consumers
[i
].consumer
= NULL
;
4349 if (ret
!= -EPROBE_DEFER
)
4350 dev_err(dev
, "Failed to get supply '%s': %d\n",
4351 consumers
[i
].supply
, ret
);
4353 dev_dbg(dev
, "Failed to get supply '%s', deferring\n",
4354 consumers
[i
].supply
);
4357 regulator_put(consumers
[i
].consumer
);
4361 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
4363 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
4365 struct regulator_bulk_data
*bulk
= data
;
4367 bulk
->ret
= regulator_enable(bulk
->consumer
);
4371 * regulator_bulk_enable - enable multiple regulator consumers
4373 * @num_consumers: Number of consumers
4374 * @consumers: Consumer data; clients are stored here.
4375 * @return 0 on success, an errno on failure
4377 * This convenience API allows consumers to enable multiple regulator
4378 * clients in a single API call. If any consumers cannot be enabled
4379 * then any others that were enabled will be disabled again prior to
4382 int regulator_bulk_enable(int num_consumers
,
4383 struct regulator_bulk_data
*consumers
)
4385 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
4389 for (i
= 0; i
< num_consumers
; i
++) {
4390 async_schedule_domain(regulator_bulk_enable_async
,
4391 &consumers
[i
], &async_domain
);
4394 async_synchronize_full_domain(&async_domain
);
4396 /* If any consumer failed we need to unwind any that succeeded */
4397 for (i
= 0; i
< num_consumers
; i
++) {
4398 if (consumers
[i
].ret
!= 0) {
4399 ret
= consumers
[i
].ret
;
4407 for (i
= 0; i
< num_consumers
; i
++) {
4408 if (consumers
[i
].ret
< 0)
4409 pr_err("Failed to enable %s: %d\n", consumers
[i
].supply
,
4412 regulator_disable(consumers
[i
].consumer
);
4417 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
4420 * regulator_bulk_disable - disable multiple regulator consumers
4422 * @num_consumers: Number of consumers
4423 * @consumers: Consumer data; clients are stored here.
4424 * @return 0 on success, an errno on failure
4426 * This convenience API allows consumers to disable multiple regulator
4427 * clients in a single API call. If any consumers cannot be disabled
4428 * then any others that were disabled will be enabled again prior to
4431 int regulator_bulk_disable(int num_consumers
,
4432 struct regulator_bulk_data
*consumers
)
4437 for (i
= num_consumers
- 1; i
>= 0; --i
) {
4438 ret
= regulator_disable(consumers
[i
].consumer
);
4446 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
4447 for (++i
; i
< num_consumers
; ++i
) {
4448 r
= regulator_enable(consumers
[i
].consumer
);
4450 pr_err("Failed to re-enable %s: %d\n",
4451 consumers
[i
].supply
, r
);
4456 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
4459 * regulator_bulk_force_disable - force disable multiple regulator consumers
4461 * @num_consumers: Number of consumers
4462 * @consumers: Consumer data; clients are stored here.
4463 * @return 0 on success, an errno on failure
4465 * This convenience API allows consumers to forcibly disable multiple regulator
4466 * clients in a single API call.
4467 * NOTE: This should be used for situations when device damage will
4468 * likely occur if the regulators are not disabled (e.g. over temp).
4469 * Although regulator_force_disable function call for some consumers can
4470 * return error numbers, the function is called for all consumers.
4472 int regulator_bulk_force_disable(int num_consumers
,
4473 struct regulator_bulk_data
*consumers
)
4478 for (i
= 0; i
< num_consumers
; i
++) {
4480 regulator_force_disable(consumers
[i
].consumer
);
4482 /* Store first error for reporting */
4483 if (consumers
[i
].ret
&& !ret
)
4484 ret
= consumers
[i
].ret
;
4489 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
4492 * regulator_bulk_free - free multiple regulator consumers
4494 * @num_consumers: Number of consumers
4495 * @consumers: Consumer data; clients are stored here.
4497 * This convenience API allows consumers to free multiple regulator
4498 * clients in a single API call.
4500 void regulator_bulk_free(int num_consumers
,
4501 struct regulator_bulk_data
*consumers
)
4505 for (i
= 0; i
< num_consumers
; i
++) {
4506 regulator_put(consumers
[i
].consumer
);
4507 consumers
[i
].consumer
= NULL
;
4510 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
4513 * regulator_notifier_call_chain - call regulator event notifier
4514 * @rdev: regulator source
4515 * @event: notifier block
4516 * @data: callback-specific data.
4518 * Called by regulator drivers to notify clients a regulator event has
4519 * occurred. We also notify regulator clients downstream.
4520 * Note lock must be held by caller.
4522 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
4523 unsigned long event
, void *data
)
4525 lockdep_assert_held_once(&rdev
->mutex
.base
);
4527 _notifier_call_chain(rdev
, event
, data
);
4531 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
4534 * regulator_mode_to_status - convert a regulator mode into a status
4536 * @mode: Mode to convert
4538 * Convert a regulator mode into a status.
4540 int regulator_mode_to_status(unsigned int mode
)
4543 case REGULATOR_MODE_FAST
:
4544 return REGULATOR_STATUS_FAST
;
4545 case REGULATOR_MODE_NORMAL
:
4546 return REGULATOR_STATUS_NORMAL
;
4547 case REGULATOR_MODE_IDLE
:
4548 return REGULATOR_STATUS_IDLE
;
4549 case REGULATOR_MODE_STANDBY
:
4550 return REGULATOR_STATUS_STANDBY
;
4552 return REGULATOR_STATUS_UNDEFINED
;
4555 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
4557 static struct attribute
*regulator_dev_attrs
[] = {
4558 &dev_attr_name
.attr
,
4559 &dev_attr_num_users
.attr
,
4560 &dev_attr_type
.attr
,
4561 &dev_attr_microvolts
.attr
,
4562 &dev_attr_microamps
.attr
,
4563 &dev_attr_opmode
.attr
,
4564 &dev_attr_state
.attr
,
4565 &dev_attr_status
.attr
,
4566 &dev_attr_bypass
.attr
,
4567 &dev_attr_requested_microamps
.attr
,
4568 &dev_attr_min_microvolts
.attr
,
4569 &dev_attr_max_microvolts
.attr
,
4570 &dev_attr_min_microamps
.attr
,
4571 &dev_attr_max_microamps
.attr
,
4572 &dev_attr_suspend_standby_state
.attr
,
4573 &dev_attr_suspend_mem_state
.attr
,
4574 &dev_attr_suspend_disk_state
.attr
,
4575 &dev_attr_suspend_standby_microvolts
.attr
,
4576 &dev_attr_suspend_mem_microvolts
.attr
,
4577 &dev_attr_suspend_disk_microvolts
.attr
,
4578 &dev_attr_suspend_standby_mode
.attr
,
4579 &dev_attr_suspend_mem_mode
.attr
,
4580 &dev_attr_suspend_disk_mode
.attr
,
4585 * To avoid cluttering sysfs (and memory) with useless state, only
4586 * create attributes that can be meaningfully displayed.
4588 static umode_t
regulator_attr_is_visible(struct kobject
*kobj
,
4589 struct attribute
*attr
, int idx
)
4591 struct device
*dev
= kobj_to_dev(kobj
);
4592 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4593 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
4594 umode_t mode
= attr
->mode
;
4596 /* these three are always present */
4597 if (attr
== &dev_attr_name
.attr
||
4598 attr
== &dev_attr_num_users
.attr
||
4599 attr
== &dev_attr_type
.attr
)
4602 /* some attributes need specific methods to be displayed */
4603 if (attr
== &dev_attr_microvolts
.attr
) {
4604 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
4605 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0) ||
4606 (ops
->list_voltage
&& ops
->list_voltage(rdev
, 0) >= 0) ||
4607 (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1))
4612 if (attr
== &dev_attr_microamps
.attr
)
4613 return ops
->get_current_limit
? mode
: 0;
4615 if (attr
== &dev_attr_opmode
.attr
)
4616 return ops
->get_mode
? mode
: 0;
4618 if (attr
== &dev_attr_state
.attr
)
4619 return (rdev
->ena_pin
|| ops
->is_enabled
) ? mode
: 0;
4621 if (attr
== &dev_attr_status
.attr
)
4622 return ops
->get_status
? mode
: 0;
4624 if (attr
== &dev_attr_bypass
.attr
)
4625 return ops
->get_bypass
? mode
: 0;
4627 /* constraints need specific supporting methods */
4628 if (attr
== &dev_attr_min_microvolts
.attr
||
4629 attr
== &dev_attr_max_microvolts
.attr
)
4630 return (ops
->set_voltage
|| ops
->set_voltage_sel
) ? mode
: 0;
4632 if (attr
== &dev_attr_min_microamps
.attr
||
4633 attr
== &dev_attr_max_microamps
.attr
)
4634 return ops
->set_current_limit
? mode
: 0;
4636 if (attr
== &dev_attr_suspend_standby_state
.attr
||
4637 attr
== &dev_attr_suspend_mem_state
.attr
||
4638 attr
== &dev_attr_suspend_disk_state
.attr
)
4641 if (attr
== &dev_attr_suspend_standby_microvolts
.attr
||
4642 attr
== &dev_attr_suspend_mem_microvolts
.attr
||
4643 attr
== &dev_attr_suspend_disk_microvolts
.attr
)
4644 return ops
->set_suspend_voltage
? mode
: 0;
4646 if (attr
== &dev_attr_suspend_standby_mode
.attr
||
4647 attr
== &dev_attr_suspend_mem_mode
.attr
||
4648 attr
== &dev_attr_suspend_disk_mode
.attr
)
4649 return ops
->set_suspend_mode
? mode
: 0;
4654 static const struct attribute_group regulator_dev_group
= {
4655 .attrs
= regulator_dev_attrs
,
4656 .is_visible
= regulator_attr_is_visible
,
4659 static const struct attribute_group
*regulator_dev_groups
[] = {
4660 ®ulator_dev_group
,
4664 static void regulator_dev_release(struct device
*dev
)
4666 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
4668 kfree(rdev
->constraints
);
4669 of_node_put(rdev
->dev
.of_node
);
4673 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
4675 struct device
*parent
= rdev
->dev
.parent
;
4676 const char *rname
= rdev_get_name(rdev
);
4677 char name
[NAME_MAX
];
4679 /* Avoid duplicate debugfs directory names */
4680 if (parent
&& rname
== rdev
->desc
->name
) {
4681 snprintf(name
, sizeof(name
), "%s-%s", dev_name(parent
),
4686 rdev
->debugfs
= debugfs_create_dir(rname
, debugfs_root
);
4687 if (!rdev
->debugfs
) {
4688 rdev_warn(rdev
, "Failed to create debugfs directory\n");
4692 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
4694 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
4696 debugfs_create_u32("bypass_count", 0444, rdev
->debugfs
,
4697 &rdev
->bypass_count
);
4700 static int regulator_register_resolve_supply(struct device
*dev
, void *data
)
4702 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4704 if (regulator_resolve_supply(rdev
))
4705 rdev_dbg(rdev
, "unable to resolve supply\n");
4710 static void regulator_resolve_coupling(struct regulator_dev
*rdev
)
4712 struct coupling_desc
*c_desc
= &rdev
->coupling_desc
;
4713 int n_coupled
= c_desc
->n_coupled
;
4714 struct regulator_dev
*c_rdev
;
4717 for (i
= 1; i
< n_coupled
; i
++) {
4718 /* already resolved */
4719 if (c_desc
->coupled_rdevs
[i
])
4722 c_rdev
= of_parse_coupled_regulator(rdev
, i
- 1);
4727 regulator_lock(c_rdev
);
4729 c_desc
->coupled_rdevs
[i
] = c_rdev
;
4730 c_desc
->n_resolved
++;
4732 regulator_unlock(c_rdev
);
4734 regulator_resolve_coupling(c_rdev
);
4738 static void regulator_remove_coupling(struct regulator_dev
*rdev
)
4740 struct coupling_desc
*__c_desc
, *c_desc
= &rdev
->coupling_desc
;
4741 struct regulator_dev
*__c_rdev
, *c_rdev
;
4742 unsigned int __n_coupled
, n_coupled
;
4745 n_coupled
= c_desc
->n_coupled
;
4747 for (i
= 1; i
< n_coupled
; i
++) {
4748 c_rdev
= c_desc
->coupled_rdevs
[i
];
4753 regulator_lock(c_rdev
);
4755 __c_desc
= &c_rdev
->coupling_desc
;
4756 __n_coupled
= __c_desc
->n_coupled
;
4758 for (k
= 1; k
< __n_coupled
; k
++) {
4759 __c_rdev
= __c_desc
->coupled_rdevs
[k
];
4761 if (__c_rdev
== rdev
) {
4762 __c_desc
->coupled_rdevs
[k
] = NULL
;
4763 __c_desc
->n_resolved
--;
4768 regulator_unlock(c_rdev
);
4770 c_desc
->coupled_rdevs
[i
] = NULL
;
4771 c_desc
->n_resolved
--;
4775 static int regulator_init_coupling(struct regulator_dev
*rdev
)
4779 if (!IS_ENABLED(CONFIG_OF
))
4782 n_phandles
= of_get_n_coupled(rdev
);
4784 if (n_phandles
+ 1 > MAX_COUPLED
) {
4785 rdev_err(rdev
, "too many regulators coupled\n");
4790 * Every regulator should always have coupling descriptor filled with
4791 * at least pointer to itself.
4793 rdev
->coupling_desc
.coupled_rdevs
[0] = rdev
;
4794 rdev
->coupling_desc
.n_coupled
= n_phandles
+ 1;
4795 rdev
->coupling_desc
.n_resolved
++;
4797 /* regulator isn't coupled */
4798 if (n_phandles
== 0)
4801 /* regulator, which can't change its voltage, can't be coupled */
4802 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
4803 rdev_err(rdev
, "voltage operation not allowed\n");
4807 if (rdev
->constraints
->max_spread
<= 0) {
4808 rdev_err(rdev
, "wrong max_spread value\n");
4812 if (!of_check_coupling_data(rdev
))
4819 * regulator_register - register regulator
4820 * @regulator_desc: regulator to register
4821 * @cfg: runtime configuration for regulator
4823 * Called by regulator drivers to register a regulator.
4824 * Returns a valid pointer to struct regulator_dev on success
4825 * or an ERR_PTR() on error.
4827 struct regulator_dev
*
4828 regulator_register(const struct regulator_desc
*regulator_desc
,
4829 const struct regulator_config
*cfg
)
4831 const struct regulation_constraints
*constraints
= NULL
;
4832 const struct regulator_init_data
*init_data
;
4833 struct regulator_config
*config
= NULL
;
4834 static atomic_t regulator_no
= ATOMIC_INIT(-1);
4835 struct regulator_dev
*rdev
;
4836 bool dangling_cfg_gpiod
= false;
4837 bool dangling_of_gpiod
= false;
4842 return ERR_PTR(-EINVAL
);
4844 dangling_cfg_gpiod
= true;
4845 if (regulator_desc
== NULL
) {
4853 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
) {
4858 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
4859 regulator_desc
->type
!= REGULATOR_CURRENT
) {
4864 /* Only one of each should be implemented */
4865 WARN_ON(regulator_desc
->ops
->get_voltage
&&
4866 regulator_desc
->ops
->get_voltage_sel
);
4867 WARN_ON(regulator_desc
->ops
->set_voltage
&&
4868 regulator_desc
->ops
->set_voltage_sel
);
4870 /* If we're using selectors we must implement list_voltage. */
4871 if (regulator_desc
->ops
->get_voltage_sel
&&
4872 !regulator_desc
->ops
->list_voltage
) {
4876 if (regulator_desc
->ops
->set_voltage_sel
&&
4877 !regulator_desc
->ops
->list_voltage
) {
4882 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
4889 * Duplicate the config so the driver could override it after
4890 * parsing init data.
4892 config
= kmemdup(cfg
, sizeof(*cfg
), GFP_KERNEL
);
4893 if (config
== NULL
) {
4899 init_data
= regulator_of_get_init_data(dev
, regulator_desc
, config
,
4900 &rdev
->dev
.of_node
);
4902 * We need to keep track of any GPIO descriptor coming from the
4903 * device tree until we have handled it over to the core. If the
4904 * config that was passed in to this function DOES NOT contain
4905 * a descriptor, and the config after this call DOES contain
4906 * a descriptor, we definitely got one from parsing the device
4909 if (!cfg
->ena_gpiod
&& config
->ena_gpiod
)
4910 dangling_of_gpiod
= true;
4912 init_data
= config
->init_data
;
4913 rdev
->dev
.of_node
= of_node_get(config
->of_node
);
4916 ww_mutex_init(&rdev
->mutex
, ®ulator_ww_class
);
4917 rdev
->reg_data
= config
->driver_data
;
4918 rdev
->owner
= regulator_desc
->owner
;
4919 rdev
->desc
= regulator_desc
;
4921 rdev
->regmap
= config
->regmap
;
4922 else if (dev_get_regmap(dev
, NULL
))
4923 rdev
->regmap
= dev_get_regmap(dev
, NULL
);
4924 else if (dev
->parent
)
4925 rdev
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
4926 INIT_LIST_HEAD(&rdev
->consumer_list
);
4927 INIT_LIST_HEAD(&rdev
->list
);
4928 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
4929 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
4931 /* preform any regulator specific init */
4932 if (init_data
&& init_data
->regulator_init
) {
4933 ret
= init_data
->regulator_init(rdev
->reg_data
);
4938 if (config
->ena_gpiod
) {
4939 mutex_lock(®ulator_list_mutex
);
4940 ret
= regulator_ena_gpio_request(rdev
, config
);
4941 mutex_unlock(®ulator_list_mutex
);
4943 rdev_err(rdev
, "Failed to request enable GPIO: %d\n",
4947 /* The regulator core took over the GPIO descriptor */
4948 dangling_cfg_gpiod
= false;
4949 dangling_of_gpiod
= false;
4952 /* register with sysfs */
4953 rdev
->dev
.class = ®ulator_class
;
4954 rdev
->dev
.parent
= dev
;
4955 dev_set_name(&rdev
->dev
, "regulator.%lu",
4956 (unsigned long) atomic_inc_return(®ulator_no
));
4958 /* set regulator constraints */
4960 constraints
= &init_data
->constraints
;
4962 if (init_data
&& init_data
->supply_regulator
)
4963 rdev
->supply_name
= init_data
->supply_regulator
;
4964 else if (regulator_desc
->supply_name
)
4965 rdev
->supply_name
= regulator_desc
->supply_name
;
4968 * Attempt to resolve the regulator supply, if specified,
4969 * but don't return an error if we fail because we will try
4970 * to resolve it again later as more regulators are added.
4972 if (regulator_resolve_supply(rdev
))
4973 rdev_dbg(rdev
, "unable to resolve supply\n");
4975 ret
= set_machine_constraints(rdev
, constraints
);
4979 ret
= regulator_init_coupling(rdev
);
4983 /* add consumers devices */
4985 mutex_lock(®ulator_list_mutex
);
4986 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
4987 ret
= set_consumer_device_supply(rdev
,
4988 init_data
->consumer_supplies
[i
].dev_name
,
4989 init_data
->consumer_supplies
[i
].supply
);
4991 mutex_unlock(®ulator_list_mutex
);
4992 dev_err(dev
, "Failed to set supply %s\n",
4993 init_data
->consumer_supplies
[i
].supply
);
4994 goto unset_supplies
;
4997 mutex_unlock(®ulator_list_mutex
);
5000 if (!rdev
->desc
->ops
->get_voltage
&&
5001 !rdev
->desc
->ops
->list_voltage
&&
5002 !rdev
->desc
->fixed_uV
)
5003 rdev
->is_switch
= true;
5005 dev_set_drvdata(&rdev
->dev
, rdev
);
5006 ret
= device_register(&rdev
->dev
);
5008 put_device(&rdev
->dev
);
5009 goto unset_supplies
;
5012 rdev_init_debugfs(rdev
);
5014 /* try to resolve regulators coupling since a new one was registered */
5015 mutex_lock(®ulator_list_mutex
);
5016 regulator_resolve_coupling(rdev
);
5017 mutex_unlock(®ulator_list_mutex
);
5019 /* try to resolve regulators supply since a new one was registered */
5020 class_for_each_device(®ulator_class
, NULL
, NULL
,
5021 regulator_register_resolve_supply
);
5026 mutex_lock(®ulator_list_mutex
);
5027 unset_regulator_supplies(rdev
);
5028 mutex_unlock(®ulator_list_mutex
);
5030 kfree(rdev
->constraints
);
5031 mutex_lock(®ulator_list_mutex
);
5032 regulator_ena_gpio_free(rdev
);
5033 mutex_unlock(®ulator_list_mutex
);
5035 if (dangling_of_gpiod
)
5036 gpiod_put(config
->ena_gpiod
);
5040 if (dangling_cfg_gpiod
)
5041 gpiod_put(cfg
->ena_gpiod
);
5042 return ERR_PTR(ret
);
5044 EXPORT_SYMBOL_GPL(regulator_register
);
5047 * regulator_unregister - unregister regulator
5048 * @rdev: regulator to unregister
5050 * Called by regulator drivers to unregister a regulator.
5052 void regulator_unregister(struct regulator_dev
*rdev
)
5058 while (rdev
->use_count
--)
5059 regulator_disable(rdev
->supply
);
5060 regulator_put(rdev
->supply
);
5063 flush_work(&rdev
->disable_work
.work
);
5065 mutex_lock(®ulator_list_mutex
);
5067 debugfs_remove_recursive(rdev
->debugfs
);
5068 WARN_ON(rdev
->open_count
);
5069 regulator_remove_coupling(rdev
);
5070 unset_regulator_supplies(rdev
);
5071 list_del(&rdev
->list
);
5072 regulator_ena_gpio_free(rdev
);
5073 device_unregister(&rdev
->dev
);
5075 mutex_unlock(®ulator_list_mutex
);
5077 EXPORT_SYMBOL_GPL(regulator_unregister
);
5079 #ifdef CONFIG_SUSPEND
5081 * regulator_suspend - prepare regulators for system wide suspend
5082 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5084 * Configure each regulator with it's suspend operating parameters for state.
5086 static int regulator_suspend(struct device
*dev
)
5088 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5089 suspend_state_t state
= pm_suspend_target_state
;
5092 regulator_lock(rdev
);
5093 ret
= suspend_set_state(rdev
, state
);
5094 regulator_unlock(rdev
);
5099 static int regulator_resume(struct device
*dev
)
5101 suspend_state_t state
= pm_suspend_target_state
;
5102 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5103 struct regulator_state
*rstate
;
5106 rstate
= regulator_get_suspend_state(rdev
, state
);
5110 regulator_lock(rdev
);
5112 if (rdev
->desc
->ops
->resume
&&
5113 (rstate
->enabled
== ENABLE_IN_SUSPEND
||
5114 rstate
->enabled
== DISABLE_IN_SUSPEND
))
5115 ret
= rdev
->desc
->ops
->resume(rdev
);
5117 regulator_unlock(rdev
);
5121 #else /* !CONFIG_SUSPEND */
5123 #define regulator_suspend NULL
5124 #define regulator_resume NULL
5126 #endif /* !CONFIG_SUSPEND */
5129 static const struct dev_pm_ops __maybe_unused regulator_pm_ops
= {
5130 .suspend
= regulator_suspend
,
5131 .resume
= regulator_resume
,
5135 struct class regulator_class
= {
5136 .name
= "regulator",
5137 .dev_release
= regulator_dev_release
,
5138 .dev_groups
= regulator_dev_groups
,
5140 .pm
= ®ulator_pm_ops
,
5144 * regulator_has_full_constraints - the system has fully specified constraints
5146 * Calling this function will cause the regulator API to disable all
5147 * regulators which have a zero use count and don't have an always_on
5148 * constraint in a late_initcall.
5150 * The intention is that this will become the default behaviour in a
5151 * future kernel release so users are encouraged to use this facility
5154 void regulator_has_full_constraints(void)
5156 has_full_constraints
= 1;
5158 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
5161 * rdev_get_drvdata - get rdev regulator driver data
5164 * Get rdev regulator driver private data. This call can be used in the
5165 * regulator driver context.
5167 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
5169 return rdev
->reg_data
;
5171 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
5174 * regulator_get_drvdata - get regulator driver data
5175 * @regulator: regulator
5177 * Get regulator driver private data. This call can be used in the consumer
5178 * driver context when non API regulator specific functions need to be called.
5180 void *regulator_get_drvdata(struct regulator
*regulator
)
5182 return regulator
->rdev
->reg_data
;
5184 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
5187 * regulator_set_drvdata - set regulator driver data
5188 * @regulator: regulator
5191 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
5193 regulator
->rdev
->reg_data
= data
;
5195 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
5198 * regulator_get_id - get regulator ID
5201 int rdev_get_id(struct regulator_dev
*rdev
)
5203 return rdev
->desc
->id
;
5205 EXPORT_SYMBOL_GPL(rdev_get_id
);
5207 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
5211 EXPORT_SYMBOL_GPL(rdev_get_dev
);
5213 struct regmap
*rdev_get_regmap(struct regulator_dev
*rdev
)
5215 return rdev
->regmap
;
5217 EXPORT_SYMBOL_GPL(rdev_get_regmap
);
5219 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
5221 return reg_init_data
->driver_data
;
5223 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
5225 #ifdef CONFIG_DEBUG_FS
5226 static int supply_map_show(struct seq_file
*sf
, void *data
)
5228 struct regulator_map
*map
;
5230 list_for_each_entry(map
, ®ulator_map_list
, list
) {
5231 seq_printf(sf
, "%s -> %s.%s\n",
5232 rdev_get_name(map
->regulator
), map
->dev_name
,
5238 DEFINE_SHOW_ATTRIBUTE(supply_map
);
5240 struct summary_data
{
5242 struct regulator_dev
*parent
;
5246 static void regulator_summary_show_subtree(struct seq_file
*s
,
5247 struct regulator_dev
*rdev
,
5250 static int regulator_summary_show_children(struct device
*dev
, void *data
)
5252 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5253 struct summary_data
*summary_data
= data
;
5255 if (rdev
->supply
&& rdev
->supply
->rdev
== summary_data
->parent
)
5256 regulator_summary_show_subtree(summary_data
->s
, rdev
,
5257 summary_data
->level
+ 1);
5262 static void regulator_summary_show_subtree(struct seq_file
*s
,
5263 struct regulator_dev
*rdev
,
5266 struct regulation_constraints
*c
;
5267 struct regulator
*consumer
;
5268 struct summary_data summary_data
;
5269 unsigned int opmode
;
5274 opmode
= _regulator_get_mode_unlocked(rdev
);
5275 seq_printf(s
, "%*s%-*s %3d %4d %6d %7s ",
5277 30 - level
* 3, rdev_get_name(rdev
),
5278 rdev
->use_count
, rdev
->open_count
, rdev
->bypass_count
,
5279 regulator_opmode_to_str(opmode
));
5281 seq_printf(s
, "%5dmV ", _regulator_get_voltage(rdev
) / 1000);
5282 seq_printf(s
, "%5dmA ",
5283 _regulator_get_current_limit_unlocked(rdev
) / 1000);
5285 c
= rdev
->constraints
;
5287 switch (rdev
->desc
->type
) {
5288 case REGULATOR_VOLTAGE
:
5289 seq_printf(s
, "%5dmV %5dmV ",
5290 c
->min_uV
/ 1000, c
->max_uV
/ 1000);
5292 case REGULATOR_CURRENT
:
5293 seq_printf(s
, "%5dmA %5dmA ",
5294 c
->min_uA
/ 1000, c
->max_uA
/ 1000);
5301 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
) {
5302 if (consumer
->dev
&& consumer
->dev
->class == ®ulator_class
)
5305 seq_printf(s
, "%*s%-*s ",
5306 (level
+ 1) * 3 + 1, "",
5307 30 - (level
+ 1) * 3,
5308 consumer
->dev
? dev_name(consumer
->dev
) : "deviceless");
5310 switch (rdev
->desc
->type
) {
5311 case REGULATOR_VOLTAGE
:
5312 seq_printf(s
, "%3d %33dmA%c%5dmV %5dmV",
5313 consumer
->enable_count
,
5314 consumer
->uA_load
/ 1000,
5315 consumer
->uA_load
&& !consumer
->enable_count
?
5317 consumer
->voltage
[PM_SUSPEND_ON
].min_uV
/ 1000,
5318 consumer
->voltage
[PM_SUSPEND_ON
].max_uV
/ 1000);
5320 case REGULATOR_CURRENT
:
5328 summary_data
.level
= level
;
5329 summary_data
.parent
= rdev
;
5331 class_for_each_device(®ulator_class
, NULL
, &summary_data
,
5332 regulator_summary_show_children
);
5335 struct summary_lock_data
{
5336 struct ww_acquire_ctx
*ww_ctx
;
5337 struct regulator_dev
**new_contended_rdev
;
5338 struct regulator_dev
**old_contended_rdev
;
5341 static int regulator_summary_lock_one(struct device
*dev
, void *data
)
5343 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5344 struct summary_lock_data
*lock_data
= data
;
5347 if (rdev
!= *lock_data
->old_contended_rdev
) {
5348 ret
= regulator_lock_nested(rdev
, lock_data
->ww_ctx
);
5350 if (ret
== -EDEADLK
)
5351 *lock_data
->new_contended_rdev
= rdev
;
5355 *lock_data
->old_contended_rdev
= NULL
;
5361 static int regulator_summary_unlock_one(struct device
*dev
, void *data
)
5363 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5364 struct summary_lock_data
*lock_data
= data
;
5367 if (rdev
== *lock_data
->new_contended_rdev
)
5371 regulator_unlock(rdev
);
5376 static int regulator_summary_lock_all(struct ww_acquire_ctx
*ww_ctx
,
5377 struct regulator_dev
**new_contended_rdev
,
5378 struct regulator_dev
**old_contended_rdev
)
5380 struct summary_lock_data lock_data
;
5383 lock_data
.ww_ctx
= ww_ctx
;
5384 lock_data
.new_contended_rdev
= new_contended_rdev
;
5385 lock_data
.old_contended_rdev
= old_contended_rdev
;
5387 ret
= class_for_each_device(®ulator_class
, NULL
, &lock_data
,
5388 regulator_summary_lock_one
);
5390 class_for_each_device(®ulator_class
, NULL
, &lock_data
,
5391 regulator_summary_unlock_one
);
5396 static void regulator_summary_lock(struct ww_acquire_ctx
*ww_ctx
)
5398 struct regulator_dev
*new_contended_rdev
= NULL
;
5399 struct regulator_dev
*old_contended_rdev
= NULL
;
5402 mutex_lock(®ulator_list_mutex
);
5404 ww_acquire_init(ww_ctx
, ®ulator_ww_class
);
5407 if (new_contended_rdev
) {
5408 ww_mutex_lock_slow(&new_contended_rdev
->mutex
, ww_ctx
);
5409 old_contended_rdev
= new_contended_rdev
;
5410 old_contended_rdev
->ref_cnt
++;
5413 err
= regulator_summary_lock_all(ww_ctx
,
5414 &new_contended_rdev
,
5415 &old_contended_rdev
);
5417 if (old_contended_rdev
)
5418 regulator_unlock(old_contended_rdev
);
5420 } while (err
== -EDEADLK
);
5422 ww_acquire_done(ww_ctx
);
5425 static void regulator_summary_unlock(struct ww_acquire_ctx
*ww_ctx
)
5427 class_for_each_device(®ulator_class
, NULL
, NULL
,
5428 regulator_summary_unlock_one
);
5429 ww_acquire_fini(ww_ctx
);
5431 mutex_unlock(®ulator_list_mutex
);
5434 static int regulator_summary_show_roots(struct device
*dev
, void *data
)
5436 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5437 struct seq_file
*s
= data
;
5440 regulator_summary_show_subtree(s
, rdev
, 0);
5445 static int regulator_summary_show(struct seq_file
*s
, void *data
)
5447 struct ww_acquire_ctx ww_ctx
;
5449 seq_puts(s
, " regulator use open bypass opmode voltage current min max\n");
5450 seq_puts(s
, "---------------------------------------------------------------------------------------\n");
5452 regulator_summary_lock(&ww_ctx
);
5454 class_for_each_device(®ulator_class
, NULL
, s
,
5455 regulator_summary_show_roots
);
5457 regulator_summary_unlock(&ww_ctx
);
5461 DEFINE_SHOW_ATTRIBUTE(regulator_summary
);
5462 #endif /* CONFIG_DEBUG_FS */
5464 static int __init
regulator_init(void)
5468 ret
= class_register(®ulator_class
);
5470 debugfs_root
= debugfs_create_dir("regulator", NULL
);
5472 pr_warn("regulator: Failed to create debugfs directory\n");
5474 #ifdef CONFIG_DEBUG_FS
5475 debugfs_create_file("supply_map", 0444, debugfs_root
, NULL
,
5478 debugfs_create_file("regulator_summary", 0444, debugfs_root
,
5479 NULL
, ®ulator_summary_fops
);
5481 regulator_dummy_init();
5486 /* init early to allow our consumers to complete system booting */
5487 core_initcall(regulator_init
);
5489 static int __init
regulator_late_cleanup(struct device
*dev
, void *data
)
5491 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
5492 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
5493 struct regulation_constraints
*c
= rdev
->constraints
;
5496 if (c
&& c
->always_on
)
5499 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
))
5502 regulator_lock(rdev
);
5504 if (rdev
->use_count
)
5507 /* If we can't read the status assume it's on. */
5508 if (ops
->is_enabled
)
5509 enabled
= ops
->is_enabled(rdev
);
5516 if (have_full_constraints()) {
5517 /* We log since this may kill the system if it goes
5519 rdev_info(rdev
, "disabling\n");
5520 ret
= _regulator_do_disable(rdev
);
5522 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
5524 /* The intention is that in future we will
5525 * assume that full constraints are provided
5526 * so warn even if we aren't going to do
5529 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
5533 regulator_unlock(rdev
);
5538 static int __init
regulator_init_complete(void)
5541 * Since DT doesn't provide an idiomatic mechanism for
5542 * enabling full constraints and since it's much more natural
5543 * with DT to provide them just assume that a DT enabled
5544 * system has full constraints.
5546 if (of_have_populated_dt())
5547 has_full_constraints
= true;
5550 * Regulators may had failed to resolve their input supplies
5551 * when were registered, either because the input supply was
5552 * not registered yet or because its parent device was not
5553 * bound yet. So attempt to resolve the input supplies for
5554 * pending regulators before trying to disable unused ones.
5556 class_for_each_device(®ulator_class
, NULL
, NULL
,
5557 regulator_register_resolve_supply
);
5559 /* If we have a full configuration then disable any regulators
5560 * we have permission to change the status for and which are
5561 * not in use or always_on. This is effectively the default
5562 * for DT and ACPI as they have full constraints.
5564 class_for_each_device(®ulator_class
, NULL
, NULL
,
5565 regulator_late_cleanup
);
5569 late_initcall_sync(regulator_init_complete
);