2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/gpio/consumer.h>
29 #include <linux/regmap.h>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/module.h>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/regulator.h>
42 #define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50 #define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
53 static DEFINE_MUTEX(regulator_list_mutex
);
54 static LIST_HEAD(regulator_list
);
55 static LIST_HEAD(regulator_map_list
);
56 static LIST_HEAD(regulator_ena_gpio_list
);
57 static LIST_HEAD(regulator_supply_alias_list
);
58 static bool has_full_constraints
;
60 static struct dentry
*debugfs_root
;
63 * struct regulator_map
65 * Used to provide symbolic supply names to devices.
67 struct regulator_map
{
68 struct list_head list
;
69 const char *dev_name
; /* The dev_name() for the consumer */
71 struct regulator_dev
*regulator
;
75 * struct regulator_enable_gpio
77 * Management for shared enable GPIO pin
79 struct regulator_enable_gpio
{
80 struct list_head list
;
81 struct gpio_desc
*gpiod
;
82 u32 enable_count
; /* a number of enabled shared GPIO */
83 u32 request_count
; /* a number of requested shared GPIO */
84 unsigned int ena_gpio_invert
:1;
88 * struct regulator_supply_alias
90 * Used to map lookups for a supply onto an alternative device.
92 struct regulator_supply_alias
{
93 struct list_head list
;
94 struct device
*src_dev
;
95 const char *src_supply
;
96 struct device
*alias_dev
;
97 const char *alias_supply
;
100 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
101 static int _regulator_disable(struct regulator_dev
*rdev
);
102 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
103 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
104 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
105 static int _notifier_call_chain(struct regulator_dev
*rdev
,
106 unsigned long event
, void *data
);
107 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
108 int min_uV
, int max_uV
);
109 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
111 const char *supply_name
);
112 static void _regulator_put(struct regulator
*regulator
);
114 static const char *rdev_get_name(struct regulator_dev
*rdev
)
116 if (rdev
->constraints
&& rdev
->constraints
->name
)
117 return rdev
->constraints
->name
;
118 else if (rdev
->desc
->name
)
119 return rdev
->desc
->name
;
124 static bool have_full_constraints(void)
126 return has_full_constraints
|| of_have_populated_dt();
130 * of_get_regulator - get a regulator device node based on supply name
131 * @dev: Device pointer for the consumer (of regulator) device
132 * @supply: regulator supply name
134 * Extract the regulator device node corresponding to the supply name.
135 * returns the device node corresponding to the regulator if found, else
138 static struct device_node
*of_get_regulator(struct device
*dev
, const char *supply
)
140 struct device_node
*regnode
= NULL
;
141 char prop_name
[32]; /* 32 is max size of property name */
143 dev_dbg(dev
, "Looking up %s-supply from device tree\n", supply
);
145 snprintf(prop_name
, 32, "%s-supply", supply
);
146 regnode
= of_parse_phandle(dev
->of_node
, prop_name
, 0);
149 dev_dbg(dev
, "Looking up %s property in node %s failed",
150 prop_name
, dev
->of_node
->full_name
);
156 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
158 if (!rdev
->constraints
)
161 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
167 /* Platform voltage constraint check */
168 static int regulator_check_voltage(struct regulator_dev
*rdev
,
169 int *min_uV
, int *max_uV
)
171 BUG_ON(*min_uV
> *max_uV
);
173 if (!rdev
->constraints
) {
174 rdev_err(rdev
, "no constraints\n");
177 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
178 rdev_err(rdev
, "operation not allowed\n");
182 if (*max_uV
> rdev
->constraints
->max_uV
)
183 *max_uV
= rdev
->constraints
->max_uV
;
184 if (*min_uV
< rdev
->constraints
->min_uV
)
185 *min_uV
= rdev
->constraints
->min_uV
;
187 if (*min_uV
> *max_uV
) {
188 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
196 /* Make sure we select a voltage that suits the needs of all
197 * regulator consumers
199 static int regulator_check_consumers(struct regulator_dev
*rdev
,
200 int *min_uV
, int *max_uV
)
202 struct regulator
*regulator
;
204 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
206 * Assume consumers that didn't say anything are OK
207 * with anything in the constraint range.
209 if (!regulator
->min_uV
&& !regulator
->max_uV
)
212 if (*max_uV
> regulator
->max_uV
)
213 *max_uV
= regulator
->max_uV
;
214 if (*min_uV
< regulator
->min_uV
)
215 *min_uV
= regulator
->min_uV
;
218 if (*min_uV
> *max_uV
) {
219 rdev_err(rdev
, "Restricting voltage, %u-%uuV\n",
227 /* current constraint check */
228 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
229 int *min_uA
, int *max_uA
)
231 BUG_ON(*min_uA
> *max_uA
);
233 if (!rdev
->constraints
) {
234 rdev_err(rdev
, "no constraints\n");
237 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
238 rdev_err(rdev
, "operation not allowed\n");
242 if (*max_uA
> rdev
->constraints
->max_uA
)
243 *max_uA
= rdev
->constraints
->max_uA
;
244 if (*min_uA
< rdev
->constraints
->min_uA
)
245 *min_uA
= rdev
->constraints
->min_uA
;
247 if (*min_uA
> *max_uA
) {
248 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
256 /* operating mode constraint check */
257 static int regulator_mode_constrain(struct regulator_dev
*rdev
, int *mode
)
260 case REGULATOR_MODE_FAST
:
261 case REGULATOR_MODE_NORMAL
:
262 case REGULATOR_MODE_IDLE
:
263 case REGULATOR_MODE_STANDBY
:
266 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
270 if (!rdev
->constraints
) {
271 rdev_err(rdev
, "no constraints\n");
274 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
275 rdev_err(rdev
, "operation not allowed\n");
279 /* The modes are bitmasks, the most power hungry modes having
280 * the lowest values. If the requested mode isn't supported
281 * try higher modes. */
283 if (rdev
->constraints
->valid_modes_mask
& *mode
)
291 /* dynamic regulator mode switching constraint check */
292 static int regulator_check_drms(struct regulator_dev
*rdev
)
294 if (!rdev
->constraints
) {
295 rdev_err(rdev
, "no constraints\n");
298 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
299 rdev_err(rdev
, "operation not allowed\n");
305 static ssize_t
regulator_uV_show(struct device
*dev
,
306 struct device_attribute
*attr
, char *buf
)
308 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
311 mutex_lock(&rdev
->mutex
);
312 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
313 mutex_unlock(&rdev
->mutex
);
317 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
319 static ssize_t
regulator_uA_show(struct device
*dev
,
320 struct device_attribute
*attr
, char *buf
)
322 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
324 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
326 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
328 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
331 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
333 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
335 static DEVICE_ATTR_RO(name
);
337 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
340 case REGULATOR_MODE_FAST
:
341 return sprintf(buf
, "fast\n");
342 case REGULATOR_MODE_NORMAL
:
343 return sprintf(buf
, "normal\n");
344 case REGULATOR_MODE_IDLE
:
345 return sprintf(buf
, "idle\n");
346 case REGULATOR_MODE_STANDBY
:
347 return sprintf(buf
, "standby\n");
349 return sprintf(buf
, "unknown\n");
352 static ssize_t
regulator_opmode_show(struct device
*dev
,
353 struct device_attribute
*attr
, char *buf
)
355 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
357 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
359 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
361 static ssize_t
regulator_print_state(char *buf
, int state
)
364 return sprintf(buf
, "enabled\n");
366 return sprintf(buf
, "disabled\n");
368 return sprintf(buf
, "unknown\n");
371 static ssize_t
regulator_state_show(struct device
*dev
,
372 struct device_attribute
*attr
, char *buf
)
374 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
377 mutex_lock(&rdev
->mutex
);
378 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
379 mutex_unlock(&rdev
->mutex
);
383 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
385 static ssize_t
regulator_status_show(struct device
*dev
,
386 struct device_attribute
*attr
, char *buf
)
388 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
392 status
= rdev
->desc
->ops
->get_status(rdev
);
397 case REGULATOR_STATUS_OFF
:
400 case REGULATOR_STATUS_ON
:
403 case REGULATOR_STATUS_ERROR
:
406 case REGULATOR_STATUS_FAST
:
409 case REGULATOR_STATUS_NORMAL
:
412 case REGULATOR_STATUS_IDLE
:
415 case REGULATOR_STATUS_STANDBY
:
418 case REGULATOR_STATUS_BYPASS
:
421 case REGULATOR_STATUS_UNDEFINED
:
428 return sprintf(buf
, "%s\n", label
);
430 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
432 static ssize_t
regulator_min_uA_show(struct device
*dev
,
433 struct device_attribute
*attr
, char *buf
)
435 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
437 if (!rdev
->constraints
)
438 return sprintf(buf
, "constraint not defined\n");
440 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
442 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
444 static ssize_t
regulator_max_uA_show(struct device
*dev
,
445 struct device_attribute
*attr
, char *buf
)
447 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
449 if (!rdev
->constraints
)
450 return sprintf(buf
, "constraint not defined\n");
452 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
454 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
456 static ssize_t
regulator_min_uV_show(struct device
*dev
,
457 struct device_attribute
*attr
, char *buf
)
459 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
461 if (!rdev
->constraints
)
462 return sprintf(buf
, "constraint not defined\n");
464 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
466 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
468 static ssize_t
regulator_max_uV_show(struct device
*dev
,
469 struct device_attribute
*attr
, char *buf
)
471 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
473 if (!rdev
->constraints
)
474 return sprintf(buf
, "constraint not defined\n");
476 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
478 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
480 static ssize_t
regulator_total_uA_show(struct device
*dev
,
481 struct device_attribute
*attr
, char *buf
)
483 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
484 struct regulator
*regulator
;
487 mutex_lock(&rdev
->mutex
);
488 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
489 uA
+= regulator
->uA_load
;
490 mutex_unlock(&rdev
->mutex
);
491 return sprintf(buf
, "%d\n", uA
);
493 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
495 static ssize_t
num_users_show(struct device
*dev
, struct device_attribute
*attr
,
498 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
499 return sprintf(buf
, "%d\n", rdev
->use_count
);
501 static DEVICE_ATTR_RO(num_users
);
503 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
506 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
508 switch (rdev
->desc
->type
) {
509 case REGULATOR_VOLTAGE
:
510 return sprintf(buf
, "voltage\n");
511 case REGULATOR_CURRENT
:
512 return sprintf(buf
, "current\n");
514 return sprintf(buf
, "unknown\n");
516 static DEVICE_ATTR_RO(type
);
518 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
519 struct device_attribute
*attr
, char *buf
)
521 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
523 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
525 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
526 regulator_suspend_mem_uV_show
, NULL
);
528 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
529 struct device_attribute
*attr
, char *buf
)
531 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
533 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
535 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
536 regulator_suspend_disk_uV_show
, NULL
);
538 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
539 struct device_attribute
*attr
, char *buf
)
541 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
543 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
545 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
546 regulator_suspend_standby_uV_show
, NULL
);
548 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
549 struct device_attribute
*attr
, char *buf
)
551 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
553 return regulator_print_opmode(buf
,
554 rdev
->constraints
->state_mem
.mode
);
556 static DEVICE_ATTR(suspend_mem_mode
, 0444,
557 regulator_suspend_mem_mode_show
, NULL
);
559 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
560 struct device_attribute
*attr
, char *buf
)
562 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
564 return regulator_print_opmode(buf
,
565 rdev
->constraints
->state_disk
.mode
);
567 static DEVICE_ATTR(suspend_disk_mode
, 0444,
568 regulator_suspend_disk_mode_show
, NULL
);
570 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
571 struct device_attribute
*attr
, char *buf
)
573 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
575 return regulator_print_opmode(buf
,
576 rdev
->constraints
->state_standby
.mode
);
578 static DEVICE_ATTR(suspend_standby_mode
, 0444,
579 regulator_suspend_standby_mode_show
, NULL
);
581 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
582 struct device_attribute
*attr
, char *buf
)
584 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
586 return regulator_print_state(buf
,
587 rdev
->constraints
->state_mem
.enabled
);
589 static DEVICE_ATTR(suspend_mem_state
, 0444,
590 regulator_suspend_mem_state_show
, NULL
);
592 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
593 struct device_attribute
*attr
, char *buf
)
595 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
597 return regulator_print_state(buf
,
598 rdev
->constraints
->state_disk
.enabled
);
600 static DEVICE_ATTR(suspend_disk_state
, 0444,
601 regulator_suspend_disk_state_show
, NULL
);
603 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
604 struct device_attribute
*attr
, char *buf
)
606 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
608 return regulator_print_state(buf
,
609 rdev
->constraints
->state_standby
.enabled
);
611 static DEVICE_ATTR(suspend_standby_state
, 0444,
612 regulator_suspend_standby_state_show
, NULL
);
614 static ssize_t
regulator_bypass_show(struct device
*dev
,
615 struct device_attribute
*attr
, char *buf
)
617 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
622 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypass
);
631 return sprintf(buf
, "%s\n", report
);
633 static DEVICE_ATTR(bypass
, 0444,
634 regulator_bypass_show
, NULL
);
636 /* Calculate the new optimum regulator operating mode based on the new total
637 * consumer load. All locks held by caller */
638 static int drms_uA_update(struct regulator_dev
*rdev
)
640 struct regulator
*sibling
;
641 int current_uA
= 0, output_uV
, input_uV
, err
;
645 * first check to see if we can set modes at all, otherwise just
646 * tell the consumer everything is OK.
648 err
= regulator_check_drms(rdev
);
652 if (!rdev
->desc
->ops
->get_optimum_mode
&&
653 !rdev
->desc
->ops
->set_load
)
656 if (!rdev
->desc
->ops
->set_mode
&&
657 !rdev
->desc
->ops
->set_load
)
660 /* get output voltage */
661 output_uV
= _regulator_get_voltage(rdev
);
662 if (output_uV
<= 0) {
663 rdev_err(rdev
, "invalid output voltage found\n");
667 /* get input voltage */
670 input_uV
= regulator_get_voltage(rdev
->supply
);
672 input_uV
= rdev
->constraints
->input_uV
;
674 rdev_err(rdev
, "invalid input voltage found\n");
678 /* calc total requested load */
679 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
680 current_uA
+= sibling
->uA_load
;
682 current_uA
+= rdev
->constraints
->system_load
;
684 if (rdev
->desc
->ops
->set_load
) {
685 /* set the optimum mode for our new total regulator load */
686 err
= rdev
->desc
->ops
->set_load(rdev
, current_uA
);
688 rdev_err(rdev
, "failed to set load %d\n", current_uA
);
690 /* now get the optimum mode for our new total regulator load */
691 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
692 output_uV
, current_uA
);
694 /* check the new mode is allowed */
695 err
= regulator_mode_constrain(rdev
, &mode
);
697 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
698 current_uA
, input_uV
, output_uV
);
702 err
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
704 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
710 static int suspend_set_state(struct regulator_dev
*rdev
,
711 struct regulator_state
*rstate
)
715 /* If we have no suspend mode configration don't set anything;
716 * only warn if the driver implements set_suspend_voltage or
717 * set_suspend_mode callback.
719 if (!rstate
->enabled
&& !rstate
->disabled
) {
720 if (rdev
->desc
->ops
->set_suspend_voltage
||
721 rdev
->desc
->ops
->set_suspend_mode
)
722 rdev_warn(rdev
, "No configuration\n");
726 if (rstate
->enabled
&& rstate
->disabled
) {
727 rdev_err(rdev
, "invalid configuration\n");
731 if (rstate
->enabled
&& rdev
->desc
->ops
->set_suspend_enable
)
732 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
733 else if (rstate
->disabled
&& rdev
->desc
->ops
->set_suspend_disable
)
734 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
735 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
739 rdev_err(rdev
, "failed to enabled/disable\n");
743 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
744 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
746 rdev_err(rdev
, "failed to set voltage\n");
751 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
752 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
754 rdev_err(rdev
, "failed to set mode\n");
761 /* locks held by caller */
762 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
764 if (!rdev
->constraints
)
768 case PM_SUSPEND_STANDBY
:
769 return suspend_set_state(rdev
,
770 &rdev
->constraints
->state_standby
);
772 return suspend_set_state(rdev
,
773 &rdev
->constraints
->state_mem
);
775 return suspend_set_state(rdev
,
776 &rdev
->constraints
->state_disk
);
782 static void print_constraints(struct regulator_dev
*rdev
)
784 struct regulation_constraints
*constraints
= rdev
->constraints
;
786 size_t len
= sizeof(buf
) - 1;
790 if (constraints
->min_uV
&& constraints
->max_uV
) {
791 if (constraints
->min_uV
== constraints
->max_uV
)
792 count
+= scnprintf(buf
+ count
, len
- count
, "%d mV ",
793 constraints
->min_uV
/ 1000);
795 count
+= scnprintf(buf
+ count
, len
- count
,
797 constraints
->min_uV
/ 1000,
798 constraints
->max_uV
/ 1000);
801 if (!constraints
->min_uV
||
802 constraints
->min_uV
!= constraints
->max_uV
) {
803 ret
= _regulator_get_voltage(rdev
);
805 count
+= scnprintf(buf
+ count
, len
- count
,
806 "at %d mV ", ret
/ 1000);
809 if (constraints
->uV_offset
)
810 count
+= scnprintf(buf
+ count
, len
- count
, "%dmV offset ",
811 constraints
->uV_offset
/ 1000);
813 if (constraints
->min_uA
&& constraints
->max_uA
) {
814 if (constraints
->min_uA
== constraints
->max_uA
)
815 count
+= scnprintf(buf
+ count
, len
- count
, "%d mA ",
816 constraints
->min_uA
/ 1000);
818 count
+= scnprintf(buf
+ count
, len
- count
,
820 constraints
->min_uA
/ 1000,
821 constraints
->max_uA
/ 1000);
824 if (!constraints
->min_uA
||
825 constraints
->min_uA
!= constraints
->max_uA
) {
826 ret
= _regulator_get_current_limit(rdev
);
828 count
+= scnprintf(buf
+ count
, len
- count
,
829 "at %d mA ", ret
/ 1000);
832 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
833 count
+= scnprintf(buf
+ count
, len
- count
, "fast ");
834 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
835 count
+= scnprintf(buf
+ count
, len
- count
, "normal ");
836 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
837 count
+= scnprintf(buf
+ count
, len
- count
, "idle ");
838 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
839 count
+= scnprintf(buf
+ count
, len
- count
, "standby");
842 scnprintf(buf
, len
, "no parameters");
844 rdev_dbg(rdev
, "%s\n", buf
);
846 if ((constraints
->min_uV
!= constraints
->max_uV
) &&
847 !(constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
))
849 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
852 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
853 struct regulation_constraints
*constraints
)
855 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
858 /* do we need to apply the constraint voltage */
859 if (rdev
->constraints
->apply_uV
&&
860 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
) {
861 int current_uV
= _regulator_get_voltage(rdev
);
862 if (current_uV
< 0) {
864 "failed to get the current voltage(%d)\n",
868 if (current_uV
< rdev
->constraints
->min_uV
||
869 current_uV
> rdev
->constraints
->max_uV
) {
870 ret
= _regulator_do_set_voltage(
871 rdev
, rdev
->constraints
->min_uV
,
872 rdev
->constraints
->max_uV
);
875 "failed to apply %duV constraint(%d)\n",
876 rdev
->constraints
->min_uV
, ret
);
882 /* constrain machine-level voltage specs to fit
883 * the actual range supported by this regulator.
885 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
886 int count
= rdev
->desc
->n_voltages
;
888 int min_uV
= INT_MAX
;
889 int max_uV
= INT_MIN
;
890 int cmin
= constraints
->min_uV
;
891 int cmax
= constraints
->max_uV
;
893 /* it's safe to autoconfigure fixed-voltage supplies
894 and the constraints are used by list_voltage. */
895 if (count
== 1 && !cmin
) {
898 constraints
->min_uV
= cmin
;
899 constraints
->max_uV
= cmax
;
902 /* voltage constraints are optional */
903 if ((cmin
== 0) && (cmax
== 0))
906 /* else require explicit machine-level constraints */
907 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
908 rdev_err(rdev
, "invalid voltage constraints\n");
912 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
913 for (i
= 0; i
< count
; i
++) {
916 value
= ops
->list_voltage(rdev
, i
);
920 /* maybe adjust [min_uV..max_uV] */
921 if (value
>= cmin
&& value
< min_uV
)
923 if (value
<= cmax
&& value
> max_uV
)
927 /* final: [min_uV..max_uV] valid iff constraints valid */
928 if (max_uV
< min_uV
) {
930 "unsupportable voltage constraints %u-%uuV\n",
935 /* use regulator's subset of machine constraints */
936 if (constraints
->min_uV
< min_uV
) {
937 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
938 constraints
->min_uV
, min_uV
);
939 constraints
->min_uV
= min_uV
;
941 if (constraints
->max_uV
> max_uV
) {
942 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
943 constraints
->max_uV
, max_uV
);
944 constraints
->max_uV
= max_uV
;
951 static int machine_constraints_current(struct regulator_dev
*rdev
,
952 struct regulation_constraints
*constraints
)
954 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
957 if (!constraints
->min_uA
&& !constraints
->max_uA
)
960 if (constraints
->min_uA
> constraints
->max_uA
) {
961 rdev_err(rdev
, "Invalid current constraints\n");
965 if (!ops
->set_current_limit
|| !ops
->get_current_limit
) {
966 rdev_warn(rdev
, "Operation of current configuration missing\n");
970 /* Set regulator current in constraints range */
971 ret
= ops
->set_current_limit(rdev
, constraints
->min_uA
,
972 constraints
->max_uA
);
974 rdev_err(rdev
, "Failed to set current constraint, %d\n", ret
);
981 static int _regulator_do_enable(struct regulator_dev
*rdev
);
984 * set_machine_constraints - sets regulator constraints
985 * @rdev: regulator source
986 * @constraints: constraints to apply
988 * Allows platform initialisation code to define and constrain
989 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
990 * Constraints *must* be set by platform code in order for some
991 * regulator operations to proceed i.e. set_voltage, set_current_limit,
994 static int set_machine_constraints(struct regulator_dev
*rdev
,
995 const struct regulation_constraints
*constraints
)
998 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1001 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
1004 rdev
->constraints
= kzalloc(sizeof(*constraints
),
1006 if (!rdev
->constraints
)
1009 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
1013 ret
= machine_constraints_current(rdev
, rdev
->constraints
);
1017 if (rdev
->constraints
->ilim_uA
&& ops
->set_input_current_limit
) {
1018 ret
= ops
->set_input_current_limit(rdev
,
1019 rdev
->constraints
->ilim_uA
);
1021 rdev_err(rdev
, "failed to set input limit\n");
1026 /* do we need to setup our suspend state */
1027 if (rdev
->constraints
->initial_state
) {
1028 ret
= suspend_prepare(rdev
, rdev
->constraints
->initial_state
);
1030 rdev_err(rdev
, "failed to set suspend state\n");
1035 if (rdev
->constraints
->initial_mode
) {
1036 if (!ops
->set_mode
) {
1037 rdev_err(rdev
, "no set_mode operation\n");
1042 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
1044 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
1049 /* If the constraints say the regulator should be on at this point
1050 * and we have control then make sure it is enabled.
1052 if (rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) {
1053 ret
= _regulator_do_enable(rdev
);
1054 if (ret
< 0 && ret
!= -EINVAL
) {
1055 rdev_err(rdev
, "failed to enable\n");
1060 if ((rdev
->constraints
->ramp_delay
|| rdev
->constraints
->ramp_disable
)
1061 && ops
->set_ramp_delay
) {
1062 ret
= ops
->set_ramp_delay(rdev
, rdev
->constraints
->ramp_delay
);
1064 rdev_err(rdev
, "failed to set ramp_delay\n");
1069 if (rdev
->constraints
->pull_down
&& ops
->set_pull_down
) {
1070 ret
= ops
->set_pull_down(rdev
);
1072 rdev_err(rdev
, "failed to set pull down\n");
1077 if (rdev
->constraints
->soft_start
&& ops
->set_soft_start
) {
1078 ret
= ops
->set_soft_start(rdev
);
1080 rdev_err(rdev
, "failed to set soft start\n");
1085 print_constraints(rdev
);
1088 kfree(rdev
->constraints
);
1089 rdev
->constraints
= NULL
;
1094 * set_supply - set regulator supply regulator
1095 * @rdev: regulator name
1096 * @supply_rdev: supply regulator name
1098 * Called by platform initialisation code to set the supply regulator for this
1099 * regulator. This ensures that a regulators supply will also be enabled by the
1100 * core if it's child is enabled.
1102 static int set_supply(struct regulator_dev
*rdev
,
1103 struct regulator_dev
*supply_rdev
)
1107 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
1109 if (!try_module_get(supply_rdev
->owner
))
1112 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
1113 if (rdev
->supply
== NULL
) {
1117 supply_rdev
->open_count
++;
1123 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1124 * @rdev: regulator source
1125 * @consumer_dev_name: dev_name() string for device supply applies to
1126 * @supply: symbolic name for supply
1128 * Allows platform initialisation code to map physical regulator
1129 * sources to symbolic names for supplies for use by devices. Devices
1130 * should use these symbolic names to request regulators, avoiding the
1131 * need to provide board-specific regulator names as platform data.
1133 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1134 const char *consumer_dev_name
,
1137 struct regulator_map
*node
;
1143 if (consumer_dev_name
!= NULL
)
1148 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1149 if (node
->dev_name
&& consumer_dev_name
) {
1150 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1152 } else if (node
->dev_name
|| consumer_dev_name
) {
1156 if (strcmp(node
->supply
, supply
) != 0)
1159 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1161 dev_name(&node
->regulator
->dev
),
1162 node
->regulator
->desc
->name
,
1164 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1168 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1172 node
->regulator
= rdev
;
1173 node
->supply
= supply
;
1176 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1177 if (node
->dev_name
== NULL
) {
1183 list_add(&node
->list
, ®ulator_map_list
);
1187 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1189 struct regulator_map
*node
, *n
;
1191 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1192 if (rdev
== node
->regulator
) {
1193 list_del(&node
->list
);
1194 kfree(node
->dev_name
);
1200 #define REG_STR_SIZE 64
1202 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1204 const char *supply_name
)
1206 struct regulator
*regulator
;
1207 char buf
[REG_STR_SIZE
];
1210 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1211 if (regulator
== NULL
)
1214 mutex_lock(&rdev
->mutex
);
1215 regulator
->rdev
= rdev
;
1216 list_add(®ulator
->list
, &rdev
->consumer_list
);
1219 regulator
->dev
= dev
;
1221 /* Add a link to the device sysfs entry */
1222 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1223 dev
->kobj
.name
, supply_name
);
1224 if (size
>= REG_STR_SIZE
)
1227 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1228 if (regulator
->supply_name
== NULL
)
1231 err
= sysfs_create_link_nowarn(&rdev
->dev
.kobj
, &dev
->kobj
,
1234 rdev_dbg(rdev
, "could not add device link %s err %d\n",
1235 dev
->kobj
.name
, err
);
1239 regulator
->supply_name
= kstrdup(supply_name
, GFP_KERNEL
);
1240 if (regulator
->supply_name
== NULL
)
1244 regulator
->debugfs
= debugfs_create_dir(regulator
->supply_name
,
1246 if (!regulator
->debugfs
) {
1247 rdev_warn(rdev
, "Failed to create debugfs directory\n");
1249 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1250 ®ulator
->uA_load
);
1251 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1252 ®ulator
->min_uV
);
1253 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1254 ®ulator
->max_uV
);
1258 * Check now if the regulator is an always on regulator - if
1259 * it is then we don't need to do nearly so much work for
1260 * enable/disable calls.
1262 if (!_regulator_can_change_status(rdev
) &&
1263 _regulator_is_enabled(rdev
))
1264 regulator
->always_on
= true;
1266 mutex_unlock(&rdev
->mutex
);
1269 list_del(®ulator
->list
);
1271 mutex_unlock(&rdev
->mutex
);
1275 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1277 if (rdev
->constraints
&& rdev
->constraints
->enable_time
)
1278 return rdev
->constraints
->enable_time
;
1279 if (!rdev
->desc
->ops
->enable_time
)
1280 return rdev
->desc
->enable_time
;
1281 return rdev
->desc
->ops
->enable_time(rdev
);
1284 static struct regulator_supply_alias
*regulator_find_supply_alias(
1285 struct device
*dev
, const char *supply
)
1287 struct regulator_supply_alias
*map
;
1289 list_for_each_entry(map
, ®ulator_supply_alias_list
, list
)
1290 if (map
->src_dev
== dev
&& strcmp(map
->src_supply
, supply
) == 0)
1296 static void regulator_supply_alias(struct device
**dev
, const char **supply
)
1298 struct regulator_supply_alias
*map
;
1300 map
= regulator_find_supply_alias(*dev
, *supply
);
1302 dev_dbg(*dev
, "Mapping supply %s to %s,%s\n",
1303 *supply
, map
->alias_supply
,
1304 dev_name(map
->alias_dev
));
1305 *dev
= map
->alias_dev
;
1306 *supply
= map
->alias_supply
;
1310 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1314 struct regulator_dev
*r
;
1315 struct device_node
*node
;
1316 struct regulator_map
*map
;
1317 const char *devname
= NULL
;
1319 regulator_supply_alias(&dev
, &supply
);
1321 /* first do a dt based lookup */
1322 if (dev
&& dev
->of_node
) {
1323 node
= of_get_regulator(dev
, supply
);
1325 list_for_each_entry(r
, ®ulator_list
, list
)
1326 if (r
->dev
.parent
&&
1327 node
== r
->dev
.of_node
)
1329 *ret
= -EPROBE_DEFER
;
1333 * If we couldn't even get the node then it's
1334 * not just that the device didn't register
1335 * yet, there's no node and we'll never
1342 /* if not found, try doing it non-dt way */
1344 devname
= dev_name(dev
);
1346 list_for_each_entry(r
, ®ulator_list
, list
)
1347 if (strcmp(rdev_get_name(r
), supply
) == 0)
1350 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1351 /* If the mapping has a device set up it must match */
1352 if (map
->dev_name
&&
1353 (!devname
|| strcmp(map
->dev_name
, devname
)))
1356 if (strcmp(map
->supply
, supply
) == 0)
1357 return map
->regulator
;
1364 static int regulator_resolve_supply(struct regulator_dev
*rdev
)
1366 struct regulator_dev
*r
;
1367 struct device
*dev
= rdev
->dev
.parent
;
1370 /* No supply to resovle? */
1371 if (!rdev
->supply_name
)
1374 /* Supply already resolved? */
1378 r
= regulator_dev_lookup(dev
, rdev
->supply_name
, &ret
);
1379 if (ret
== -ENODEV
) {
1381 * No supply was specified for this regulator and
1382 * there will never be one.
1388 if (have_full_constraints()) {
1389 r
= dummy_regulator_rdev
;
1391 dev_err(dev
, "Failed to resolve %s-supply for %s\n",
1392 rdev
->supply_name
, rdev
->desc
->name
);
1393 return -EPROBE_DEFER
;
1397 /* Recursively resolve the supply of the supply */
1398 ret
= regulator_resolve_supply(r
);
1402 ret
= set_supply(rdev
, r
);
1406 /* Cascade always-on state to supply */
1407 if (_regulator_is_enabled(rdev
)) {
1408 ret
= regulator_enable(rdev
->supply
);
1411 _regulator_put(rdev
->supply
);
1419 /* Internal regulator request function */
1420 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1421 bool exclusive
, bool allow_dummy
)
1423 struct regulator_dev
*rdev
;
1424 struct regulator
*regulator
= ERR_PTR(-EPROBE_DEFER
);
1425 const char *devname
= NULL
;
1429 pr_err("get() with no identifier\n");
1430 return ERR_PTR(-EINVAL
);
1434 devname
= dev_name(dev
);
1436 if (have_full_constraints())
1439 ret
= -EPROBE_DEFER
;
1441 mutex_lock(®ulator_list_mutex
);
1443 rdev
= regulator_dev_lookup(dev
, id
, &ret
);
1447 regulator
= ERR_PTR(ret
);
1450 * If we have return value from dev_lookup fail, we do not expect to
1451 * succeed, so, quit with appropriate error value
1453 if (ret
&& ret
!= -ENODEV
)
1457 devname
= "deviceless";
1460 * Assume that a regulator is physically present and enabled
1461 * even if it isn't hooked up and just provide a dummy.
1463 if (have_full_constraints() && allow_dummy
) {
1464 pr_warn("%s supply %s not found, using dummy regulator\n",
1467 rdev
= dummy_regulator_rdev
;
1469 /* Don't log an error when called from regulator_get_optional() */
1470 } else if (!have_full_constraints() || exclusive
) {
1471 dev_warn(dev
, "dummy supplies not allowed\n");
1474 mutex_unlock(®ulator_list_mutex
);
1478 if (rdev
->exclusive
) {
1479 regulator
= ERR_PTR(-EPERM
);
1483 if (exclusive
&& rdev
->open_count
) {
1484 regulator
= ERR_PTR(-EBUSY
);
1488 ret
= regulator_resolve_supply(rdev
);
1490 regulator
= ERR_PTR(ret
);
1494 if (!try_module_get(rdev
->owner
))
1497 regulator
= create_regulator(rdev
, dev
, id
);
1498 if (regulator
== NULL
) {
1499 regulator
= ERR_PTR(-ENOMEM
);
1500 module_put(rdev
->owner
);
1506 rdev
->exclusive
= 1;
1508 ret
= _regulator_is_enabled(rdev
);
1510 rdev
->use_count
= 1;
1512 rdev
->use_count
= 0;
1516 mutex_unlock(®ulator_list_mutex
);
1522 * regulator_get - lookup and obtain a reference to a regulator.
1523 * @dev: device for regulator "consumer"
1524 * @id: Supply name or regulator ID.
1526 * Returns a struct regulator corresponding to the regulator producer,
1527 * or IS_ERR() condition containing errno.
1529 * Use of supply names configured via regulator_set_device_supply() is
1530 * strongly encouraged. It is recommended that the supply name used
1531 * should match the name used for the supply and/or the relevant
1532 * device pins in the datasheet.
1534 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1536 return _regulator_get(dev
, id
, false, true);
1538 EXPORT_SYMBOL_GPL(regulator_get
);
1541 * regulator_get_exclusive - obtain exclusive access to a regulator.
1542 * @dev: device for regulator "consumer"
1543 * @id: Supply name or regulator ID.
1545 * Returns a struct regulator corresponding to the regulator producer,
1546 * or IS_ERR() condition containing errno. Other consumers will be
1547 * unable to obtain this regulator while this reference is held and the
1548 * use count for the regulator will be initialised to reflect the current
1549 * state of the regulator.
1551 * This is intended for use by consumers which cannot tolerate shared
1552 * use of the regulator such as those which need to force the
1553 * regulator off for correct operation of the hardware they are
1556 * Use of supply names configured via regulator_set_device_supply() is
1557 * strongly encouraged. It is recommended that the supply name used
1558 * should match the name used for the supply and/or the relevant
1559 * device pins in the datasheet.
1561 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1563 return _regulator_get(dev
, id
, true, false);
1565 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1568 * regulator_get_optional - obtain optional access to a regulator.
1569 * @dev: device for regulator "consumer"
1570 * @id: Supply name or regulator ID.
1572 * Returns a struct regulator corresponding to the regulator producer,
1573 * or IS_ERR() condition containing errno.
1575 * This is intended for use by consumers for devices which can have
1576 * some supplies unconnected in normal use, such as some MMC devices.
1577 * It can allow the regulator core to provide stub supplies for other
1578 * supplies requested using normal regulator_get() calls without
1579 * disrupting the operation of drivers that can handle absent
1582 * Use of supply names configured via regulator_set_device_supply() is
1583 * strongly encouraged. It is recommended that the supply name used
1584 * should match the name used for the supply and/or the relevant
1585 * device pins in the datasheet.
1587 struct regulator
*regulator_get_optional(struct device
*dev
, const char *id
)
1589 return _regulator_get(dev
, id
, false, false);
1591 EXPORT_SYMBOL_GPL(regulator_get_optional
);
1593 /* regulator_list_mutex lock held by regulator_put() */
1594 static void _regulator_put(struct regulator
*regulator
)
1596 struct regulator_dev
*rdev
;
1598 if (regulator
== NULL
|| IS_ERR(regulator
))
1601 rdev
= regulator
->rdev
;
1603 debugfs_remove_recursive(regulator
->debugfs
);
1605 /* remove any sysfs entries */
1607 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1608 mutex_lock(&rdev
->mutex
);
1609 kfree(regulator
->supply_name
);
1610 list_del(®ulator
->list
);
1614 rdev
->exclusive
= 0;
1615 mutex_unlock(&rdev
->mutex
);
1617 module_put(rdev
->owner
);
1621 * regulator_put - "free" the regulator source
1622 * @regulator: regulator source
1624 * Note: drivers must ensure that all regulator_enable calls made on this
1625 * regulator source are balanced by regulator_disable calls prior to calling
1628 void regulator_put(struct regulator
*regulator
)
1630 mutex_lock(®ulator_list_mutex
);
1631 _regulator_put(regulator
);
1632 mutex_unlock(®ulator_list_mutex
);
1634 EXPORT_SYMBOL_GPL(regulator_put
);
1637 * regulator_register_supply_alias - Provide device alias for supply lookup
1639 * @dev: device that will be given as the regulator "consumer"
1640 * @id: Supply name or regulator ID
1641 * @alias_dev: device that should be used to lookup the supply
1642 * @alias_id: Supply name or regulator ID that should be used to lookup the
1645 * All lookups for id on dev will instead be conducted for alias_id on
1648 int regulator_register_supply_alias(struct device
*dev
, const char *id
,
1649 struct device
*alias_dev
,
1650 const char *alias_id
)
1652 struct regulator_supply_alias
*map
;
1654 map
= regulator_find_supply_alias(dev
, id
);
1658 map
= kzalloc(sizeof(struct regulator_supply_alias
), GFP_KERNEL
);
1663 map
->src_supply
= id
;
1664 map
->alias_dev
= alias_dev
;
1665 map
->alias_supply
= alias_id
;
1667 list_add(&map
->list
, ®ulator_supply_alias_list
);
1669 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1670 id
, dev_name(dev
), alias_id
, dev_name(alias_dev
));
1674 EXPORT_SYMBOL_GPL(regulator_register_supply_alias
);
1677 * regulator_unregister_supply_alias - Remove device alias
1679 * @dev: device that will be given as the regulator "consumer"
1680 * @id: Supply name or regulator ID
1682 * Remove a lookup alias if one exists for id on dev.
1684 void regulator_unregister_supply_alias(struct device
*dev
, const char *id
)
1686 struct regulator_supply_alias
*map
;
1688 map
= regulator_find_supply_alias(dev
, id
);
1690 list_del(&map
->list
);
1694 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias
);
1697 * regulator_bulk_register_supply_alias - register multiple aliases
1699 * @dev: device that will be given as the regulator "consumer"
1700 * @id: List of supply names or regulator IDs
1701 * @alias_dev: device that should be used to lookup the supply
1702 * @alias_id: List of supply names or regulator IDs that should be used to
1704 * @num_id: Number of aliases to register
1706 * @return 0 on success, an errno on failure.
1708 * This helper function allows drivers to register several supply
1709 * aliases in one operation. If any of the aliases cannot be
1710 * registered any aliases that were registered will be removed
1711 * before returning to the caller.
1713 int regulator_bulk_register_supply_alias(struct device
*dev
,
1714 const char *const *id
,
1715 struct device
*alias_dev
,
1716 const char *const *alias_id
,
1722 for (i
= 0; i
< num_id
; ++i
) {
1723 ret
= regulator_register_supply_alias(dev
, id
[i
], alias_dev
,
1733 "Failed to create supply alias %s,%s -> %s,%s\n",
1734 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
1737 regulator_unregister_supply_alias(dev
, id
[i
]);
1741 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias
);
1744 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1746 * @dev: device that will be given as the regulator "consumer"
1747 * @id: List of supply names or regulator IDs
1748 * @num_id: Number of aliases to unregister
1750 * This helper function allows drivers to unregister several supply
1751 * aliases in one operation.
1753 void regulator_bulk_unregister_supply_alias(struct device
*dev
,
1754 const char *const *id
,
1759 for (i
= 0; i
< num_id
; ++i
)
1760 regulator_unregister_supply_alias(dev
, id
[i
]);
1762 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias
);
1765 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1766 static int regulator_ena_gpio_request(struct regulator_dev
*rdev
,
1767 const struct regulator_config
*config
)
1769 struct regulator_enable_gpio
*pin
;
1770 struct gpio_desc
*gpiod
;
1773 gpiod
= gpio_to_desc(config
->ena_gpio
);
1775 list_for_each_entry(pin
, ®ulator_ena_gpio_list
, list
) {
1776 if (pin
->gpiod
== gpiod
) {
1777 rdev_dbg(rdev
, "GPIO %d is already used\n",
1779 goto update_ena_gpio_to_rdev
;
1783 ret
= gpio_request_one(config
->ena_gpio
,
1784 GPIOF_DIR_OUT
| config
->ena_gpio_flags
,
1785 rdev_get_name(rdev
));
1789 pin
= kzalloc(sizeof(struct regulator_enable_gpio
), GFP_KERNEL
);
1791 gpio_free(config
->ena_gpio
);
1796 pin
->ena_gpio_invert
= config
->ena_gpio_invert
;
1797 list_add(&pin
->list
, ®ulator_ena_gpio_list
);
1799 update_ena_gpio_to_rdev
:
1800 pin
->request_count
++;
1801 rdev
->ena_pin
= pin
;
1805 static void regulator_ena_gpio_free(struct regulator_dev
*rdev
)
1807 struct regulator_enable_gpio
*pin
, *n
;
1812 /* Free the GPIO only in case of no use */
1813 list_for_each_entry_safe(pin
, n
, ®ulator_ena_gpio_list
, list
) {
1814 if (pin
->gpiod
== rdev
->ena_pin
->gpiod
) {
1815 if (pin
->request_count
<= 1) {
1816 pin
->request_count
= 0;
1817 gpiod_put(pin
->gpiod
);
1818 list_del(&pin
->list
);
1820 rdev
->ena_pin
= NULL
;
1823 pin
->request_count
--;
1830 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1831 * @rdev: regulator_dev structure
1832 * @enable: enable GPIO at initial use?
1834 * GPIO is enabled in case of initial use. (enable_count is 0)
1835 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1837 static int regulator_ena_gpio_ctrl(struct regulator_dev
*rdev
, bool enable
)
1839 struct regulator_enable_gpio
*pin
= rdev
->ena_pin
;
1845 /* Enable GPIO at initial use */
1846 if (pin
->enable_count
== 0)
1847 gpiod_set_value_cansleep(pin
->gpiod
,
1848 !pin
->ena_gpio_invert
);
1850 pin
->enable_count
++;
1852 if (pin
->enable_count
> 1) {
1853 pin
->enable_count
--;
1857 /* Disable GPIO if not used */
1858 if (pin
->enable_count
<= 1) {
1859 gpiod_set_value_cansleep(pin
->gpiod
,
1860 pin
->ena_gpio_invert
);
1861 pin
->enable_count
= 0;
1869 * _regulator_enable_delay - a delay helper function
1870 * @delay: time to delay in microseconds
1872 * Delay for the requested amount of time as per the guidelines in:
1874 * Documentation/timers/timers-howto.txt
1876 * The assumption here is that regulators will never be enabled in
1877 * atomic context and therefore sleeping functions can be used.
1879 static void _regulator_enable_delay(unsigned int delay
)
1881 unsigned int ms
= delay
/ 1000;
1882 unsigned int us
= delay
% 1000;
1886 * For small enough values, handle super-millisecond
1887 * delays in the usleep_range() call below.
1896 * Give the scheduler some room to coalesce with any other
1897 * wakeup sources. For delays shorter than 10 us, don't even
1898 * bother setting up high-resolution timers and just busy-
1902 usleep_range(us
, us
+ 100);
1907 static int _regulator_do_enable(struct regulator_dev
*rdev
)
1911 /* Query before enabling in case configuration dependent. */
1912 ret
= _regulator_get_enable_time(rdev
);
1916 rdev_warn(rdev
, "enable_time() failed: %d\n", ret
);
1920 trace_regulator_enable(rdev_get_name(rdev
));
1922 if (rdev
->desc
->off_on_delay
) {
1923 /* if needed, keep a distance of off_on_delay from last time
1924 * this regulator was disabled.
1926 unsigned long start_jiffy
= jiffies
;
1927 unsigned long intended
, max_delay
, remaining
;
1929 max_delay
= usecs_to_jiffies(rdev
->desc
->off_on_delay
);
1930 intended
= rdev
->last_off_jiffy
+ max_delay
;
1932 if (time_before(start_jiffy
, intended
)) {
1933 /* calc remaining jiffies to deal with one-time
1935 * in case of multiple timer wrapping, either it can be
1936 * detected by out-of-range remaining, or it cannot be
1937 * detected and we gets a panelty of
1938 * _regulator_enable_delay().
1940 remaining
= intended
- start_jiffy
;
1941 if (remaining
<= max_delay
)
1942 _regulator_enable_delay(
1943 jiffies_to_usecs(remaining
));
1947 if (rdev
->ena_pin
) {
1948 if (!rdev
->ena_gpio_state
) {
1949 ret
= regulator_ena_gpio_ctrl(rdev
, true);
1952 rdev
->ena_gpio_state
= 1;
1954 } else if (rdev
->desc
->ops
->enable
) {
1955 ret
= rdev
->desc
->ops
->enable(rdev
);
1962 /* Allow the regulator to ramp; it would be useful to extend
1963 * this for bulk operations so that the regulators can ramp
1965 trace_regulator_enable_delay(rdev_get_name(rdev
));
1967 _regulator_enable_delay(delay
);
1969 trace_regulator_enable_complete(rdev_get_name(rdev
));
1974 /* locks held by regulator_enable() */
1975 static int _regulator_enable(struct regulator_dev
*rdev
)
1979 /* check voltage and requested load before enabling */
1980 if (rdev
->constraints
&&
1981 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1982 drms_uA_update(rdev
);
1984 if (rdev
->use_count
== 0) {
1985 /* The regulator may on if it's not switchable or left on */
1986 ret
= _regulator_is_enabled(rdev
);
1987 if (ret
== -EINVAL
|| ret
== 0) {
1988 if (!_regulator_can_change_status(rdev
))
1991 ret
= _regulator_do_enable(rdev
);
1995 } else if (ret
< 0) {
1996 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
1999 /* Fallthrough on positive return values - already enabled */
2008 * regulator_enable - enable regulator output
2009 * @regulator: regulator source
2011 * Request that the regulator be enabled with the regulator output at
2012 * the predefined voltage or current value. Calls to regulator_enable()
2013 * must be balanced with calls to regulator_disable().
2015 * NOTE: the output value can be set by other drivers, boot loader or may be
2016 * hardwired in the regulator.
2018 int regulator_enable(struct regulator
*regulator
)
2020 struct regulator_dev
*rdev
= regulator
->rdev
;
2023 if (regulator
->always_on
)
2027 ret
= regulator_enable(rdev
->supply
);
2032 mutex_lock(&rdev
->mutex
);
2033 ret
= _regulator_enable(rdev
);
2034 mutex_unlock(&rdev
->mutex
);
2036 if (ret
!= 0 && rdev
->supply
)
2037 regulator_disable(rdev
->supply
);
2041 EXPORT_SYMBOL_GPL(regulator_enable
);
2043 static int _regulator_do_disable(struct regulator_dev
*rdev
)
2047 trace_regulator_disable(rdev_get_name(rdev
));
2049 if (rdev
->ena_pin
) {
2050 if (rdev
->ena_gpio_state
) {
2051 ret
= regulator_ena_gpio_ctrl(rdev
, false);
2054 rdev
->ena_gpio_state
= 0;
2057 } else if (rdev
->desc
->ops
->disable
) {
2058 ret
= rdev
->desc
->ops
->disable(rdev
);
2063 /* cares about last_off_jiffy only if off_on_delay is required by
2066 if (rdev
->desc
->off_on_delay
)
2067 rdev
->last_off_jiffy
= jiffies
;
2069 trace_regulator_disable_complete(rdev_get_name(rdev
));
2074 /* locks held by regulator_disable() */
2075 static int _regulator_disable(struct regulator_dev
*rdev
)
2079 if (WARN(rdev
->use_count
<= 0,
2080 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
2083 /* are we the last user and permitted to disable ? */
2084 if (rdev
->use_count
== 1 &&
2085 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
2087 /* we are last user */
2088 if (_regulator_can_change_status(rdev
)) {
2089 ret
= _notifier_call_chain(rdev
,
2090 REGULATOR_EVENT_PRE_DISABLE
,
2092 if (ret
& NOTIFY_STOP_MASK
)
2095 ret
= _regulator_do_disable(rdev
);
2097 rdev_err(rdev
, "failed to disable\n");
2098 _notifier_call_chain(rdev
,
2099 REGULATOR_EVENT_ABORT_DISABLE
,
2103 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
2107 rdev
->use_count
= 0;
2108 } else if (rdev
->use_count
> 1) {
2110 if (rdev
->constraints
&&
2111 (rdev
->constraints
->valid_ops_mask
&
2112 REGULATOR_CHANGE_DRMS
))
2113 drms_uA_update(rdev
);
2122 * regulator_disable - disable regulator output
2123 * @regulator: regulator source
2125 * Disable the regulator output voltage or current. Calls to
2126 * regulator_enable() must be balanced with calls to
2127 * regulator_disable().
2129 * NOTE: this will only disable the regulator output if no other consumer
2130 * devices have it enabled, the regulator device supports disabling and
2131 * machine constraints permit this operation.
2133 int regulator_disable(struct regulator
*regulator
)
2135 struct regulator_dev
*rdev
= regulator
->rdev
;
2138 if (regulator
->always_on
)
2141 mutex_lock(&rdev
->mutex
);
2142 ret
= _regulator_disable(rdev
);
2143 mutex_unlock(&rdev
->mutex
);
2145 if (ret
== 0 && rdev
->supply
)
2146 regulator_disable(rdev
->supply
);
2150 EXPORT_SYMBOL_GPL(regulator_disable
);
2152 /* locks held by regulator_force_disable() */
2153 static int _regulator_force_disable(struct regulator_dev
*rdev
)
2157 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2158 REGULATOR_EVENT_PRE_DISABLE
, NULL
);
2159 if (ret
& NOTIFY_STOP_MASK
)
2162 ret
= _regulator_do_disable(rdev
);
2164 rdev_err(rdev
, "failed to force disable\n");
2165 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2166 REGULATOR_EVENT_ABORT_DISABLE
, NULL
);
2170 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2171 REGULATOR_EVENT_DISABLE
, NULL
);
2177 * regulator_force_disable - force disable regulator output
2178 * @regulator: regulator source
2180 * Forcibly disable the regulator output voltage or current.
2181 * NOTE: this *will* disable the regulator output even if other consumer
2182 * devices have it enabled. This should be used for situations when device
2183 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2185 int regulator_force_disable(struct regulator
*regulator
)
2187 struct regulator_dev
*rdev
= regulator
->rdev
;
2190 mutex_lock(&rdev
->mutex
);
2191 regulator
->uA_load
= 0;
2192 ret
= _regulator_force_disable(regulator
->rdev
);
2193 mutex_unlock(&rdev
->mutex
);
2196 while (rdev
->open_count
--)
2197 regulator_disable(rdev
->supply
);
2201 EXPORT_SYMBOL_GPL(regulator_force_disable
);
2203 static void regulator_disable_work(struct work_struct
*work
)
2205 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
2209 mutex_lock(&rdev
->mutex
);
2211 BUG_ON(!rdev
->deferred_disables
);
2213 count
= rdev
->deferred_disables
;
2214 rdev
->deferred_disables
= 0;
2216 for (i
= 0; i
< count
; i
++) {
2217 ret
= _regulator_disable(rdev
);
2219 rdev_err(rdev
, "Deferred disable failed: %d\n", ret
);
2222 mutex_unlock(&rdev
->mutex
);
2225 for (i
= 0; i
< count
; i
++) {
2226 ret
= regulator_disable(rdev
->supply
);
2229 "Supply disable failed: %d\n", ret
);
2236 * regulator_disable_deferred - disable regulator output with delay
2237 * @regulator: regulator source
2238 * @ms: miliseconds until the regulator is disabled
2240 * Execute regulator_disable() on the regulator after a delay. This
2241 * is intended for use with devices that require some time to quiesce.
2243 * NOTE: this will only disable the regulator output if no other consumer
2244 * devices have it enabled, the regulator device supports disabling and
2245 * machine constraints permit this operation.
2247 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
2249 struct regulator_dev
*rdev
= regulator
->rdev
;
2252 if (regulator
->always_on
)
2256 return regulator_disable(regulator
);
2258 mutex_lock(&rdev
->mutex
);
2259 rdev
->deferred_disables
++;
2260 mutex_unlock(&rdev
->mutex
);
2262 ret
= queue_delayed_work(system_power_efficient_wq
,
2263 &rdev
->disable_work
,
2264 msecs_to_jiffies(ms
));
2270 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
2272 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
2274 /* A GPIO control always takes precedence */
2276 return rdev
->ena_gpio_state
;
2278 /* If we don't know then assume that the regulator is always on */
2279 if (!rdev
->desc
->ops
->is_enabled
)
2282 return rdev
->desc
->ops
->is_enabled(rdev
);
2286 * regulator_is_enabled - is the regulator output enabled
2287 * @regulator: regulator source
2289 * Returns positive if the regulator driver backing the source/client
2290 * has requested that the device be enabled, zero if it hasn't, else a
2291 * negative errno code.
2293 * Note that the device backing this regulator handle can have multiple
2294 * users, so it might be enabled even if regulator_enable() was never
2295 * called for this particular source.
2297 int regulator_is_enabled(struct regulator
*regulator
)
2301 if (regulator
->always_on
)
2304 mutex_lock(®ulator
->rdev
->mutex
);
2305 ret
= _regulator_is_enabled(regulator
->rdev
);
2306 mutex_unlock(®ulator
->rdev
->mutex
);
2310 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
2313 * regulator_can_change_voltage - check if regulator can change voltage
2314 * @regulator: regulator source
2316 * Returns positive if the regulator driver backing the source/client
2317 * can change its voltage, false otherwise. Useful for detecting fixed
2318 * or dummy regulators and disabling voltage change logic in the client
2321 int regulator_can_change_voltage(struct regulator
*regulator
)
2323 struct regulator_dev
*rdev
= regulator
->rdev
;
2325 if (rdev
->constraints
&&
2326 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2327 if (rdev
->desc
->n_voltages
- rdev
->desc
->linear_min_sel
> 1)
2330 if (rdev
->desc
->continuous_voltage_range
&&
2331 rdev
->constraints
->min_uV
&& rdev
->constraints
->max_uV
&&
2332 rdev
->constraints
->min_uV
!= rdev
->constraints
->max_uV
)
2338 EXPORT_SYMBOL_GPL(regulator_can_change_voltage
);
2341 * regulator_count_voltages - count regulator_list_voltage() selectors
2342 * @regulator: regulator source
2344 * Returns number of selectors, or negative errno. Selectors are
2345 * numbered starting at zero, and typically correspond to bitfields
2346 * in hardware registers.
2348 int regulator_count_voltages(struct regulator
*regulator
)
2350 struct regulator_dev
*rdev
= regulator
->rdev
;
2352 if (rdev
->desc
->n_voltages
)
2353 return rdev
->desc
->n_voltages
;
2358 return regulator_count_voltages(rdev
->supply
);
2360 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
2363 * regulator_list_voltage - enumerate supported voltages
2364 * @regulator: regulator source
2365 * @selector: identify voltage to list
2366 * Context: can sleep
2368 * Returns a voltage that can be passed to @regulator_set_voltage(),
2369 * zero if this selector code can't be used on this system, or a
2372 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
2374 struct regulator_dev
*rdev
= regulator
->rdev
;
2375 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2378 if (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1 && !selector
)
2379 return rdev
->desc
->fixed_uV
;
2381 if (ops
->list_voltage
) {
2382 if (selector
>= rdev
->desc
->n_voltages
)
2384 mutex_lock(&rdev
->mutex
);
2385 ret
= ops
->list_voltage(rdev
, selector
);
2386 mutex_unlock(&rdev
->mutex
);
2387 } else if (rdev
->supply
) {
2388 ret
= regulator_list_voltage(rdev
->supply
, selector
);
2394 if (ret
< rdev
->constraints
->min_uV
)
2396 else if (ret
> rdev
->constraints
->max_uV
)
2402 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
2405 * regulator_get_regmap - get the regulator's register map
2406 * @regulator: regulator source
2408 * Returns the register map for the given regulator, or an ERR_PTR value
2409 * if the regulator doesn't use regmap.
2411 struct regmap
*regulator_get_regmap(struct regulator
*regulator
)
2413 struct regmap
*map
= regulator
->rdev
->regmap
;
2415 return map
? map
: ERR_PTR(-EOPNOTSUPP
);
2419 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2420 * @regulator: regulator source
2421 * @vsel_reg: voltage selector register, output parameter
2422 * @vsel_mask: mask for voltage selector bitfield, output parameter
2424 * Returns the hardware register offset and bitmask used for setting the
2425 * regulator voltage. This might be useful when configuring voltage-scaling
2426 * hardware or firmware that can make I2C requests behind the kernel's back,
2429 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2430 * and 0 is returned, otherwise a negative errno is returned.
2432 int regulator_get_hardware_vsel_register(struct regulator
*regulator
,
2434 unsigned *vsel_mask
)
2436 struct regulator_dev
*rdev
= regulator
->rdev
;
2437 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2439 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2442 *vsel_reg
= rdev
->desc
->vsel_reg
;
2443 *vsel_mask
= rdev
->desc
->vsel_mask
;
2447 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register
);
2450 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2451 * @regulator: regulator source
2452 * @selector: identify voltage to list
2454 * Converts the selector to a hardware-specific voltage selector that can be
2455 * directly written to the regulator registers. The address of the voltage
2456 * register can be determined by calling @regulator_get_hardware_vsel_register.
2458 * On error a negative errno is returned.
2460 int regulator_list_hardware_vsel(struct regulator
*regulator
,
2463 struct regulator_dev
*rdev
= regulator
->rdev
;
2464 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2466 if (selector
>= rdev
->desc
->n_voltages
)
2468 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2473 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel
);
2476 * regulator_get_linear_step - return the voltage step size between VSEL values
2477 * @regulator: regulator source
2479 * Returns the voltage step size between VSEL values for linear
2480 * regulators, or return 0 if the regulator isn't a linear regulator.
2482 unsigned int regulator_get_linear_step(struct regulator
*regulator
)
2484 struct regulator_dev
*rdev
= regulator
->rdev
;
2486 return rdev
->desc
->uV_step
;
2488 EXPORT_SYMBOL_GPL(regulator_get_linear_step
);
2491 * regulator_is_supported_voltage - check if a voltage range can be supported
2493 * @regulator: Regulator to check.
2494 * @min_uV: Minimum required voltage in uV.
2495 * @max_uV: Maximum required voltage in uV.
2497 * Returns a boolean or a negative error code.
2499 int regulator_is_supported_voltage(struct regulator
*regulator
,
2500 int min_uV
, int max_uV
)
2502 struct regulator_dev
*rdev
= regulator
->rdev
;
2503 int i
, voltages
, ret
;
2505 /* If we can't change voltage check the current voltage */
2506 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2507 ret
= regulator_get_voltage(regulator
);
2509 return min_uV
<= ret
&& ret
<= max_uV
;
2514 /* Any voltage within constrains range is fine? */
2515 if (rdev
->desc
->continuous_voltage_range
)
2516 return min_uV
>= rdev
->constraints
->min_uV
&&
2517 max_uV
<= rdev
->constraints
->max_uV
;
2519 ret
= regulator_count_voltages(regulator
);
2524 for (i
= 0; i
< voltages
; i
++) {
2525 ret
= regulator_list_voltage(regulator
, i
);
2527 if (ret
>= min_uV
&& ret
<= max_uV
)
2533 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
2535 static int _regulator_call_set_voltage(struct regulator_dev
*rdev
,
2536 int min_uV
, int max_uV
,
2539 struct pre_voltage_change_data data
;
2542 data
.old_uV
= _regulator_get_voltage(rdev
);
2543 data
.min_uV
= min_uV
;
2544 data
.max_uV
= max_uV
;
2545 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
2547 if (ret
& NOTIFY_STOP_MASK
)
2550 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
, selector
);
2554 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
2555 (void *)data
.old_uV
);
2560 static int _regulator_call_set_voltage_sel(struct regulator_dev
*rdev
,
2561 int uV
, unsigned selector
)
2563 struct pre_voltage_change_data data
;
2566 data
.old_uV
= _regulator_get_voltage(rdev
);
2569 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
2571 if (ret
& NOTIFY_STOP_MASK
)
2574 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
2578 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
2579 (void *)data
.old_uV
);
2584 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
2585 int min_uV
, int max_uV
)
2590 unsigned int selector
;
2591 int old_selector
= -1;
2593 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
2595 min_uV
+= rdev
->constraints
->uV_offset
;
2596 max_uV
+= rdev
->constraints
->uV_offset
;
2599 * If we can't obtain the old selector there is not enough
2600 * info to call set_voltage_time_sel().
2602 if (_regulator_is_enabled(rdev
) &&
2603 rdev
->desc
->ops
->set_voltage_time_sel
&&
2604 rdev
->desc
->ops
->get_voltage_sel
) {
2605 old_selector
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
2606 if (old_selector
< 0)
2607 return old_selector
;
2610 if (rdev
->desc
->ops
->set_voltage
) {
2611 ret
= _regulator_call_set_voltage(rdev
, min_uV
, max_uV
,
2615 if (rdev
->desc
->ops
->list_voltage
)
2616 best_val
= rdev
->desc
->ops
->list_voltage(rdev
,
2619 best_val
= _regulator_get_voltage(rdev
);
2622 } else if (rdev
->desc
->ops
->set_voltage_sel
) {
2623 if (rdev
->desc
->ops
->map_voltage
) {
2624 ret
= rdev
->desc
->ops
->map_voltage(rdev
, min_uV
,
2627 if (rdev
->desc
->ops
->list_voltage
==
2628 regulator_list_voltage_linear
)
2629 ret
= regulator_map_voltage_linear(rdev
,
2631 else if (rdev
->desc
->ops
->list_voltage
==
2632 regulator_list_voltage_linear_range
)
2633 ret
= regulator_map_voltage_linear_range(rdev
,
2636 ret
= regulator_map_voltage_iterate(rdev
,
2641 best_val
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
2642 if (min_uV
<= best_val
&& max_uV
>= best_val
) {
2644 if (old_selector
== selector
)
2647 ret
= _regulator_call_set_voltage_sel(
2648 rdev
, best_val
, selector
);
2657 /* Call set_voltage_time_sel if successfully obtained old_selector */
2658 if (ret
== 0 && !rdev
->constraints
->ramp_disable
&& old_selector
>= 0
2659 && old_selector
!= selector
) {
2661 delay
= rdev
->desc
->ops
->set_voltage_time_sel(rdev
,
2662 old_selector
, selector
);
2664 rdev_warn(rdev
, "set_voltage_time_sel() failed: %d\n",
2669 /* Insert any necessary delays */
2670 if (delay
>= 1000) {
2671 mdelay(delay
/ 1000);
2672 udelay(delay
% 1000);
2678 if (ret
== 0 && best_val
>= 0) {
2679 unsigned long data
= best_val
;
2681 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
2685 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), best_val
);
2691 * regulator_set_voltage - set regulator output voltage
2692 * @regulator: regulator source
2693 * @min_uV: Minimum required voltage in uV
2694 * @max_uV: Maximum acceptable voltage in uV
2696 * Sets a voltage regulator to the desired output voltage. This can be set
2697 * during any regulator state. IOW, regulator can be disabled or enabled.
2699 * If the regulator is enabled then the voltage will change to the new value
2700 * immediately otherwise if the regulator is disabled the regulator will
2701 * output at the new voltage when enabled.
2703 * NOTE: If the regulator is shared between several devices then the lowest
2704 * request voltage that meets the system constraints will be used.
2705 * Regulator system constraints must be set for this regulator before
2706 * calling this function otherwise this call will fail.
2708 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
2710 struct regulator_dev
*rdev
= regulator
->rdev
;
2712 int old_min_uV
, old_max_uV
;
2715 mutex_lock(&rdev
->mutex
);
2717 /* If we're setting the same range as last time the change
2718 * should be a noop (some cpufreq implementations use the same
2719 * voltage for multiple frequencies, for example).
2721 if (regulator
->min_uV
== min_uV
&& regulator
->max_uV
== max_uV
)
2724 /* If we're trying to set a range that overlaps the current voltage,
2725 * return succesfully even though the regulator does not support
2726 * changing the voltage.
2728 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2729 current_uV
= _regulator_get_voltage(rdev
);
2730 if (min_uV
<= current_uV
&& current_uV
<= max_uV
) {
2731 regulator
->min_uV
= min_uV
;
2732 regulator
->max_uV
= max_uV
;
2738 if (!rdev
->desc
->ops
->set_voltage
&&
2739 !rdev
->desc
->ops
->set_voltage_sel
) {
2744 /* constraints check */
2745 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
2749 /* restore original values in case of error */
2750 old_min_uV
= regulator
->min_uV
;
2751 old_max_uV
= regulator
->max_uV
;
2752 regulator
->min_uV
= min_uV
;
2753 regulator
->max_uV
= max_uV
;
2755 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
2759 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
2764 mutex_unlock(&rdev
->mutex
);
2767 regulator
->min_uV
= old_min_uV
;
2768 regulator
->max_uV
= old_max_uV
;
2769 mutex_unlock(&rdev
->mutex
);
2772 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
2775 * regulator_set_voltage_time - get raise/fall time
2776 * @regulator: regulator source
2777 * @old_uV: starting voltage in microvolts
2778 * @new_uV: target voltage in microvolts
2780 * Provided with the starting and ending voltage, this function attempts to
2781 * calculate the time in microseconds required to rise or fall to this new
2784 int regulator_set_voltage_time(struct regulator
*regulator
,
2785 int old_uV
, int new_uV
)
2787 struct regulator_dev
*rdev
= regulator
->rdev
;
2788 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2794 /* Currently requires operations to do this */
2795 if (!ops
->list_voltage
|| !ops
->set_voltage_time_sel
2796 || !rdev
->desc
->n_voltages
)
2799 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
2800 /* We only look for exact voltage matches here */
2801 voltage
= regulator_list_voltage(regulator
, i
);
2806 if (voltage
== old_uV
)
2808 if (voltage
== new_uV
)
2812 if (old_sel
< 0 || new_sel
< 0)
2815 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
2817 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
2820 * regulator_set_voltage_time_sel - get raise/fall time
2821 * @rdev: regulator source device
2822 * @old_selector: selector for starting voltage
2823 * @new_selector: selector for target voltage
2825 * Provided with the starting and target voltage selectors, this function
2826 * returns time in microseconds required to rise or fall to this new voltage
2828 * Drivers providing ramp_delay in regulation_constraints can use this as their
2829 * set_voltage_time_sel() operation.
2831 int regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
2832 unsigned int old_selector
,
2833 unsigned int new_selector
)
2835 unsigned int ramp_delay
= 0;
2836 int old_volt
, new_volt
;
2838 if (rdev
->constraints
->ramp_delay
)
2839 ramp_delay
= rdev
->constraints
->ramp_delay
;
2840 else if (rdev
->desc
->ramp_delay
)
2841 ramp_delay
= rdev
->desc
->ramp_delay
;
2843 if (ramp_delay
== 0) {
2844 rdev_warn(rdev
, "ramp_delay not set\n");
2849 if (!rdev
->desc
->ops
->list_voltage
)
2852 old_volt
= rdev
->desc
->ops
->list_voltage(rdev
, old_selector
);
2853 new_volt
= rdev
->desc
->ops
->list_voltage(rdev
, new_selector
);
2855 return DIV_ROUND_UP(abs(new_volt
- old_volt
), ramp_delay
);
2857 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel
);
2860 * regulator_sync_voltage - re-apply last regulator output voltage
2861 * @regulator: regulator source
2863 * Re-apply the last configured voltage. This is intended to be used
2864 * where some external control source the consumer is cooperating with
2865 * has caused the configured voltage to change.
2867 int regulator_sync_voltage(struct regulator
*regulator
)
2869 struct regulator_dev
*rdev
= regulator
->rdev
;
2870 int ret
, min_uV
, max_uV
;
2872 mutex_lock(&rdev
->mutex
);
2874 if (!rdev
->desc
->ops
->set_voltage
&&
2875 !rdev
->desc
->ops
->set_voltage_sel
) {
2880 /* This is only going to work if we've had a voltage configured. */
2881 if (!regulator
->min_uV
&& !regulator
->max_uV
) {
2886 min_uV
= regulator
->min_uV
;
2887 max_uV
= regulator
->max_uV
;
2889 /* This should be a paranoia check... */
2890 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
2894 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
2898 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
2901 mutex_unlock(&rdev
->mutex
);
2904 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
2906 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
2910 if (rdev
->desc
->ops
->get_voltage_sel
) {
2911 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
2914 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
2915 } else if (rdev
->desc
->ops
->get_voltage
) {
2916 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
2917 } else if (rdev
->desc
->ops
->list_voltage
) {
2918 ret
= rdev
->desc
->ops
->list_voltage(rdev
, 0);
2919 } else if (rdev
->desc
->fixed_uV
&& (rdev
->desc
->n_voltages
== 1)) {
2920 ret
= rdev
->desc
->fixed_uV
;
2921 } else if (rdev
->supply
) {
2922 ret
= regulator_get_voltage(rdev
->supply
);
2929 return ret
- rdev
->constraints
->uV_offset
;
2933 * regulator_get_voltage - get regulator output voltage
2934 * @regulator: regulator source
2936 * This returns the current regulator voltage in uV.
2938 * NOTE: If the regulator is disabled it will return the voltage value. This
2939 * function should not be used to determine regulator state.
2941 int regulator_get_voltage(struct regulator
*regulator
)
2945 mutex_lock(®ulator
->rdev
->mutex
);
2947 ret
= _regulator_get_voltage(regulator
->rdev
);
2949 mutex_unlock(®ulator
->rdev
->mutex
);
2953 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
2956 * regulator_set_current_limit - set regulator output current limit
2957 * @regulator: regulator source
2958 * @min_uA: Minimum supported current in uA
2959 * @max_uA: Maximum supported current in uA
2961 * Sets current sink to the desired output current. This can be set during
2962 * any regulator state. IOW, regulator can be disabled or enabled.
2964 * If the regulator is enabled then the current will change to the new value
2965 * immediately otherwise if the regulator is disabled the regulator will
2966 * output at the new current when enabled.
2968 * NOTE: Regulator system constraints must be set for this regulator before
2969 * calling this function otherwise this call will fail.
2971 int regulator_set_current_limit(struct regulator
*regulator
,
2972 int min_uA
, int max_uA
)
2974 struct regulator_dev
*rdev
= regulator
->rdev
;
2977 mutex_lock(&rdev
->mutex
);
2980 if (!rdev
->desc
->ops
->set_current_limit
) {
2985 /* constraints check */
2986 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
2990 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
2992 mutex_unlock(&rdev
->mutex
);
2995 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
2997 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
3001 mutex_lock(&rdev
->mutex
);
3004 if (!rdev
->desc
->ops
->get_current_limit
) {
3009 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
3011 mutex_unlock(&rdev
->mutex
);
3016 * regulator_get_current_limit - get regulator output current
3017 * @regulator: regulator source
3019 * This returns the current supplied by the specified current sink in uA.
3021 * NOTE: If the regulator is disabled it will return the current value. This
3022 * function should not be used to determine regulator state.
3024 int regulator_get_current_limit(struct regulator
*regulator
)
3026 return _regulator_get_current_limit(regulator
->rdev
);
3028 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
3031 * regulator_set_mode - set regulator operating mode
3032 * @regulator: regulator source
3033 * @mode: operating mode - one of the REGULATOR_MODE constants
3035 * Set regulator operating mode to increase regulator efficiency or improve
3036 * regulation performance.
3038 * NOTE: Regulator system constraints must be set for this regulator before
3039 * calling this function otherwise this call will fail.
3041 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
3043 struct regulator_dev
*rdev
= regulator
->rdev
;
3045 int regulator_curr_mode
;
3047 mutex_lock(&rdev
->mutex
);
3050 if (!rdev
->desc
->ops
->set_mode
) {
3055 /* return if the same mode is requested */
3056 if (rdev
->desc
->ops
->get_mode
) {
3057 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
3058 if (regulator_curr_mode
== mode
) {
3064 /* constraints check */
3065 ret
= regulator_mode_constrain(rdev
, &mode
);
3069 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
3071 mutex_unlock(&rdev
->mutex
);
3074 EXPORT_SYMBOL_GPL(regulator_set_mode
);
3076 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
3080 mutex_lock(&rdev
->mutex
);
3083 if (!rdev
->desc
->ops
->get_mode
) {
3088 ret
= rdev
->desc
->ops
->get_mode(rdev
);
3090 mutex_unlock(&rdev
->mutex
);
3095 * regulator_get_mode - get regulator operating mode
3096 * @regulator: regulator source
3098 * Get the current regulator operating mode.
3100 unsigned int regulator_get_mode(struct regulator
*regulator
)
3102 return _regulator_get_mode(regulator
->rdev
);
3104 EXPORT_SYMBOL_GPL(regulator_get_mode
);
3107 * regulator_set_load - set regulator load
3108 * @regulator: regulator source
3109 * @uA_load: load current
3111 * Notifies the regulator core of a new device load. This is then used by
3112 * DRMS (if enabled by constraints) to set the most efficient regulator
3113 * operating mode for the new regulator loading.
3115 * Consumer devices notify their supply regulator of the maximum power
3116 * they will require (can be taken from device datasheet in the power
3117 * consumption tables) when they change operational status and hence power
3118 * state. Examples of operational state changes that can affect power
3119 * consumption are :-
3121 * o Device is opened / closed.
3122 * o Device I/O is about to begin or has just finished.
3123 * o Device is idling in between work.
3125 * This information is also exported via sysfs to userspace.
3127 * DRMS will sum the total requested load on the regulator and change
3128 * to the most efficient operating mode if platform constraints allow.
3130 * On error a negative errno is returned.
3132 int regulator_set_load(struct regulator
*regulator
, int uA_load
)
3134 struct regulator_dev
*rdev
= regulator
->rdev
;
3137 mutex_lock(&rdev
->mutex
);
3138 regulator
->uA_load
= uA_load
;
3139 ret
= drms_uA_update(rdev
);
3140 mutex_unlock(&rdev
->mutex
);
3144 EXPORT_SYMBOL_GPL(regulator_set_load
);
3147 * regulator_allow_bypass - allow the regulator to go into bypass mode
3149 * @regulator: Regulator to configure
3150 * @enable: enable or disable bypass mode
3152 * Allow the regulator to go into bypass mode if all other consumers
3153 * for the regulator also enable bypass mode and the machine
3154 * constraints allow this. Bypass mode means that the regulator is
3155 * simply passing the input directly to the output with no regulation.
3157 int regulator_allow_bypass(struct regulator
*regulator
, bool enable
)
3159 struct regulator_dev
*rdev
= regulator
->rdev
;
3162 if (!rdev
->desc
->ops
->set_bypass
)
3165 if (rdev
->constraints
&&
3166 !(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_BYPASS
))
3169 mutex_lock(&rdev
->mutex
);
3171 if (enable
&& !regulator
->bypass
) {
3172 rdev
->bypass_count
++;
3174 if (rdev
->bypass_count
== rdev
->open_count
) {
3175 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3177 rdev
->bypass_count
--;
3180 } else if (!enable
&& regulator
->bypass
) {
3181 rdev
->bypass_count
--;
3183 if (rdev
->bypass_count
!= rdev
->open_count
) {
3184 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3186 rdev
->bypass_count
++;
3191 regulator
->bypass
= enable
;
3193 mutex_unlock(&rdev
->mutex
);
3197 EXPORT_SYMBOL_GPL(regulator_allow_bypass
);
3200 * regulator_register_notifier - register regulator event notifier
3201 * @regulator: regulator source
3202 * @nb: notifier block
3204 * Register notifier block to receive regulator events.
3206 int regulator_register_notifier(struct regulator
*regulator
,
3207 struct notifier_block
*nb
)
3209 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
3212 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
3215 * regulator_unregister_notifier - unregister regulator event notifier
3216 * @regulator: regulator source
3217 * @nb: notifier block
3219 * Unregister regulator event notifier block.
3221 int regulator_unregister_notifier(struct regulator
*regulator
,
3222 struct notifier_block
*nb
)
3224 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
3227 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
3229 /* notify regulator consumers and downstream regulator consumers.
3230 * Note mutex must be held by caller.
3232 static int _notifier_call_chain(struct regulator_dev
*rdev
,
3233 unsigned long event
, void *data
)
3235 /* call rdev chain first */
3236 return blocking_notifier_call_chain(&rdev
->notifier
, event
, data
);
3240 * regulator_bulk_get - get multiple regulator consumers
3242 * @dev: Device to supply
3243 * @num_consumers: Number of consumers to register
3244 * @consumers: Configuration of consumers; clients are stored here.
3246 * @return 0 on success, an errno on failure.
3248 * This helper function allows drivers to get several regulator
3249 * consumers in one operation. If any of the regulators cannot be
3250 * acquired then any regulators that were allocated will be freed
3251 * before returning to the caller.
3253 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
3254 struct regulator_bulk_data
*consumers
)
3259 for (i
= 0; i
< num_consumers
; i
++)
3260 consumers
[i
].consumer
= NULL
;
3262 for (i
= 0; i
< num_consumers
; i
++) {
3263 consumers
[i
].consumer
= regulator_get(dev
,
3264 consumers
[i
].supply
);
3265 if (IS_ERR(consumers
[i
].consumer
)) {
3266 ret
= PTR_ERR(consumers
[i
].consumer
);
3267 dev_err(dev
, "Failed to get supply '%s': %d\n",
3268 consumers
[i
].supply
, ret
);
3269 consumers
[i
].consumer
= NULL
;
3278 regulator_put(consumers
[i
].consumer
);
3282 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
3284 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
3286 struct regulator_bulk_data
*bulk
= data
;
3288 bulk
->ret
= regulator_enable(bulk
->consumer
);
3292 * regulator_bulk_enable - enable multiple regulator consumers
3294 * @num_consumers: Number of consumers
3295 * @consumers: Consumer data; clients are stored here.
3296 * @return 0 on success, an errno on failure
3298 * This convenience API allows consumers to enable multiple regulator
3299 * clients in a single API call. If any consumers cannot be enabled
3300 * then any others that were enabled will be disabled again prior to
3303 int regulator_bulk_enable(int num_consumers
,
3304 struct regulator_bulk_data
*consumers
)
3306 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
3310 for (i
= 0; i
< num_consumers
; i
++) {
3311 if (consumers
[i
].consumer
->always_on
)
3312 consumers
[i
].ret
= 0;
3314 async_schedule_domain(regulator_bulk_enable_async
,
3315 &consumers
[i
], &async_domain
);
3318 async_synchronize_full_domain(&async_domain
);
3320 /* If any consumer failed we need to unwind any that succeeded */
3321 for (i
= 0; i
< num_consumers
; i
++) {
3322 if (consumers
[i
].ret
!= 0) {
3323 ret
= consumers
[i
].ret
;
3331 for (i
= 0; i
< num_consumers
; i
++) {
3332 if (consumers
[i
].ret
< 0)
3333 pr_err("Failed to enable %s: %d\n", consumers
[i
].supply
,
3336 regulator_disable(consumers
[i
].consumer
);
3341 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
3344 * regulator_bulk_disable - disable multiple regulator consumers
3346 * @num_consumers: Number of consumers
3347 * @consumers: Consumer data; clients are stored here.
3348 * @return 0 on success, an errno on failure
3350 * This convenience API allows consumers to disable multiple regulator
3351 * clients in a single API call. If any consumers cannot be disabled
3352 * then any others that were disabled will be enabled again prior to
3355 int regulator_bulk_disable(int num_consumers
,
3356 struct regulator_bulk_data
*consumers
)
3361 for (i
= num_consumers
- 1; i
>= 0; --i
) {
3362 ret
= regulator_disable(consumers
[i
].consumer
);
3370 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
3371 for (++i
; i
< num_consumers
; ++i
) {
3372 r
= regulator_enable(consumers
[i
].consumer
);
3374 pr_err("Failed to reename %s: %d\n",
3375 consumers
[i
].supply
, r
);
3380 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
3383 * regulator_bulk_force_disable - force disable multiple regulator consumers
3385 * @num_consumers: Number of consumers
3386 * @consumers: Consumer data; clients are stored here.
3387 * @return 0 on success, an errno on failure
3389 * This convenience API allows consumers to forcibly disable multiple regulator
3390 * clients in a single API call.
3391 * NOTE: This should be used for situations when device damage will
3392 * likely occur if the regulators are not disabled (e.g. over temp).
3393 * Although regulator_force_disable function call for some consumers can
3394 * return error numbers, the function is called for all consumers.
3396 int regulator_bulk_force_disable(int num_consumers
,
3397 struct regulator_bulk_data
*consumers
)
3402 for (i
= 0; i
< num_consumers
; i
++)
3404 regulator_force_disable(consumers
[i
].consumer
);
3406 for (i
= 0; i
< num_consumers
; i
++) {
3407 if (consumers
[i
].ret
!= 0) {
3408 ret
= consumers
[i
].ret
;
3417 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
3420 * regulator_bulk_free - free multiple regulator consumers
3422 * @num_consumers: Number of consumers
3423 * @consumers: Consumer data; clients are stored here.
3425 * This convenience API allows consumers to free multiple regulator
3426 * clients in a single API call.
3428 void regulator_bulk_free(int num_consumers
,
3429 struct regulator_bulk_data
*consumers
)
3433 for (i
= 0; i
< num_consumers
; i
++) {
3434 regulator_put(consumers
[i
].consumer
);
3435 consumers
[i
].consumer
= NULL
;
3438 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
3441 * regulator_notifier_call_chain - call regulator event notifier
3442 * @rdev: regulator source
3443 * @event: notifier block
3444 * @data: callback-specific data.
3446 * Called by regulator drivers to notify clients a regulator event has
3447 * occurred. We also notify regulator clients downstream.
3448 * Note lock must be held by caller.
3450 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
3451 unsigned long event
, void *data
)
3453 _notifier_call_chain(rdev
, event
, data
);
3457 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
3460 * regulator_mode_to_status - convert a regulator mode into a status
3462 * @mode: Mode to convert
3464 * Convert a regulator mode into a status.
3466 int regulator_mode_to_status(unsigned int mode
)
3469 case REGULATOR_MODE_FAST
:
3470 return REGULATOR_STATUS_FAST
;
3471 case REGULATOR_MODE_NORMAL
:
3472 return REGULATOR_STATUS_NORMAL
;
3473 case REGULATOR_MODE_IDLE
:
3474 return REGULATOR_STATUS_IDLE
;
3475 case REGULATOR_MODE_STANDBY
:
3476 return REGULATOR_STATUS_STANDBY
;
3478 return REGULATOR_STATUS_UNDEFINED
;
3481 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
3483 static struct attribute
*regulator_dev_attrs
[] = {
3484 &dev_attr_name
.attr
,
3485 &dev_attr_num_users
.attr
,
3486 &dev_attr_type
.attr
,
3487 &dev_attr_microvolts
.attr
,
3488 &dev_attr_microamps
.attr
,
3489 &dev_attr_opmode
.attr
,
3490 &dev_attr_state
.attr
,
3491 &dev_attr_status
.attr
,
3492 &dev_attr_bypass
.attr
,
3493 &dev_attr_requested_microamps
.attr
,
3494 &dev_attr_min_microvolts
.attr
,
3495 &dev_attr_max_microvolts
.attr
,
3496 &dev_attr_min_microamps
.attr
,
3497 &dev_attr_max_microamps
.attr
,
3498 &dev_attr_suspend_standby_state
.attr
,
3499 &dev_attr_suspend_mem_state
.attr
,
3500 &dev_attr_suspend_disk_state
.attr
,
3501 &dev_attr_suspend_standby_microvolts
.attr
,
3502 &dev_attr_suspend_mem_microvolts
.attr
,
3503 &dev_attr_suspend_disk_microvolts
.attr
,
3504 &dev_attr_suspend_standby_mode
.attr
,
3505 &dev_attr_suspend_mem_mode
.attr
,
3506 &dev_attr_suspend_disk_mode
.attr
,
3511 * To avoid cluttering sysfs (and memory) with useless state, only
3512 * create attributes that can be meaningfully displayed.
3514 static umode_t
regulator_attr_is_visible(struct kobject
*kobj
,
3515 struct attribute
*attr
, int idx
)
3517 struct device
*dev
= kobj_to_dev(kobj
);
3518 struct regulator_dev
*rdev
= container_of(dev
, struct regulator_dev
, dev
);
3519 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3520 umode_t mode
= attr
->mode
;
3522 /* these three are always present */
3523 if (attr
== &dev_attr_name
.attr
||
3524 attr
== &dev_attr_num_users
.attr
||
3525 attr
== &dev_attr_type
.attr
)
3528 /* some attributes need specific methods to be displayed */
3529 if (attr
== &dev_attr_microvolts
.attr
) {
3530 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
3531 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0) ||
3532 (ops
->list_voltage
&& ops
->list_voltage(rdev
, 0) >= 0) ||
3533 (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1))
3538 if (attr
== &dev_attr_microamps
.attr
)
3539 return ops
->get_current_limit
? mode
: 0;
3541 if (attr
== &dev_attr_opmode
.attr
)
3542 return ops
->get_mode
? mode
: 0;
3544 if (attr
== &dev_attr_state
.attr
)
3545 return (rdev
->ena_pin
|| ops
->is_enabled
) ? mode
: 0;
3547 if (attr
== &dev_attr_status
.attr
)
3548 return ops
->get_status
? mode
: 0;
3550 if (attr
== &dev_attr_bypass
.attr
)
3551 return ops
->get_bypass
? mode
: 0;
3553 /* some attributes are type-specific */
3554 if (attr
== &dev_attr_requested_microamps
.attr
)
3555 return rdev
->desc
->type
== REGULATOR_CURRENT
? mode
: 0;
3557 /* constraints need specific supporting methods */
3558 if (attr
== &dev_attr_min_microvolts
.attr
||
3559 attr
== &dev_attr_max_microvolts
.attr
)
3560 return (ops
->set_voltage
|| ops
->set_voltage_sel
) ? mode
: 0;
3562 if (attr
== &dev_attr_min_microamps
.attr
||
3563 attr
== &dev_attr_max_microamps
.attr
)
3564 return ops
->set_current_limit
? mode
: 0;
3566 if (attr
== &dev_attr_suspend_standby_state
.attr
||
3567 attr
== &dev_attr_suspend_mem_state
.attr
||
3568 attr
== &dev_attr_suspend_disk_state
.attr
)
3571 if (attr
== &dev_attr_suspend_standby_microvolts
.attr
||
3572 attr
== &dev_attr_suspend_mem_microvolts
.attr
||
3573 attr
== &dev_attr_suspend_disk_microvolts
.attr
)
3574 return ops
->set_suspend_voltage
? mode
: 0;
3576 if (attr
== &dev_attr_suspend_standby_mode
.attr
||
3577 attr
== &dev_attr_suspend_mem_mode
.attr
||
3578 attr
== &dev_attr_suspend_disk_mode
.attr
)
3579 return ops
->set_suspend_mode
? mode
: 0;
3584 static const struct attribute_group regulator_dev_group
= {
3585 .attrs
= regulator_dev_attrs
,
3586 .is_visible
= regulator_attr_is_visible
,
3589 static const struct attribute_group
*regulator_dev_groups
[] = {
3590 ®ulator_dev_group
,
3594 static void regulator_dev_release(struct device
*dev
)
3596 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
3600 static struct class regulator_class
= {
3601 .name
= "regulator",
3602 .dev_release
= regulator_dev_release
,
3603 .dev_groups
= regulator_dev_groups
,
3606 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
3608 struct device
*parent
= rdev
->dev
.parent
;
3609 const char *rname
= rdev_get_name(rdev
);
3610 char name
[NAME_MAX
];
3612 /* Avoid duplicate debugfs directory names */
3613 if (parent
&& rname
== rdev
->desc
->name
) {
3614 snprintf(name
, sizeof(name
), "%s-%s", dev_name(parent
),
3619 rdev
->debugfs
= debugfs_create_dir(rname
, debugfs_root
);
3620 if (!rdev
->debugfs
) {
3621 rdev_warn(rdev
, "Failed to create debugfs directory\n");
3625 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
3627 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
3629 debugfs_create_u32("bypass_count", 0444, rdev
->debugfs
,
3630 &rdev
->bypass_count
);
3634 * regulator_register - register regulator
3635 * @regulator_desc: regulator to register
3636 * @cfg: runtime configuration for regulator
3638 * Called by regulator drivers to register a regulator.
3639 * Returns a valid pointer to struct regulator_dev on success
3640 * or an ERR_PTR() on error.
3642 struct regulator_dev
*
3643 regulator_register(const struct regulator_desc
*regulator_desc
,
3644 const struct regulator_config
*cfg
)
3646 const struct regulation_constraints
*constraints
= NULL
;
3647 const struct regulator_init_data
*init_data
;
3648 struct regulator_config
*config
= NULL
;
3649 static atomic_t regulator_no
= ATOMIC_INIT(-1);
3650 struct regulator_dev
*rdev
;
3654 if (regulator_desc
== NULL
|| cfg
== NULL
)
3655 return ERR_PTR(-EINVAL
);
3660 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
3661 return ERR_PTR(-EINVAL
);
3663 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
3664 regulator_desc
->type
!= REGULATOR_CURRENT
)
3665 return ERR_PTR(-EINVAL
);
3667 /* Only one of each should be implemented */
3668 WARN_ON(regulator_desc
->ops
->get_voltage
&&
3669 regulator_desc
->ops
->get_voltage_sel
);
3670 WARN_ON(regulator_desc
->ops
->set_voltage
&&
3671 regulator_desc
->ops
->set_voltage_sel
);
3673 /* If we're using selectors we must implement list_voltage. */
3674 if (regulator_desc
->ops
->get_voltage_sel
&&
3675 !regulator_desc
->ops
->list_voltage
) {
3676 return ERR_PTR(-EINVAL
);
3678 if (regulator_desc
->ops
->set_voltage_sel
&&
3679 !regulator_desc
->ops
->list_voltage
) {
3680 return ERR_PTR(-EINVAL
);
3683 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
3685 return ERR_PTR(-ENOMEM
);
3688 * Duplicate the config so the driver could override it after
3689 * parsing init data.
3691 config
= kmemdup(cfg
, sizeof(*cfg
), GFP_KERNEL
);
3692 if (config
== NULL
) {
3694 return ERR_PTR(-ENOMEM
);
3697 init_data
= regulator_of_get_init_data(dev
, regulator_desc
, config
,
3698 &rdev
->dev
.of_node
);
3700 init_data
= config
->init_data
;
3701 rdev
->dev
.of_node
= of_node_get(config
->of_node
);
3704 mutex_lock(®ulator_list_mutex
);
3706 mutex_init(&rdev
->mutex
);
3707 rdev
->reg_data
= config
->driver_data
;
3708 rdev
->owner
= regulator_desc
->owner
;
3709 rdev
->desc
= regulator_desc
;
3711 rdev
->regmap
= config
->regmap
;
3712 else if (dev_get_regmap(dev
, NULL
))
3713 rdev
->regmap
= dev_get_regmap(dev
, NULL
);
3714 else if (dev
->parent
)
3715 rdev
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
3716 INIT_LIST_HEAD(&rdev
->consumer_list
);
3717 INIT_LIST_HEAD(&rdev
->list
);
3718 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
3719 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
3721 /* preform any regulator specific init */
3722 if (init_data
&& init_data
->regulator_init
) {
3723 ret
= init_data
->regulator_init(rdev
->reg_data
);
3728 /* register with sysfs */
3729 rdev
->dev
.class = ®ulator_class
;
3730 rdev
->dev
.parent
= dev
;
3731 dev_set_name(&rdev
->dev
, "regulator.%lu",
3732 (unsigned long) atomic_inc_return(®ulator_no
));
3733 ret
= device_register(&rdev
->dev
);
3735 put_device(&rdev
->dev
);
3739 dev_set_drvdata(&rdev
->dev
, rdev
);
3741 if ((config
->ena_gpio
|| config
->ena_gpio_initialized
) &&
3742 gpio_is_valid(config
->ena_gpio
)) {
3743 ret
= regulator_ena_gpio_request(rdev
, config
);
3745 rdev_err(rdev
, "Failed to request enable GPIO%d: %d\n",
3746 config
->ena_gpio
, ret
);
3751 /* set regulator constraints */
3753 constraints
= &init_data
->constraints
;
3755 ret
= set_machine_constraints(rdev
, constraints
);
3759 if (init_data
&& init_data
->supply_regulator
)
3760 rdev
->supply_name
= init_data
->supply_regulator
;
3761 else if (regulator_desc
->supply_name
)
3762 rdev
->supply_name
= regulator_desc
->supply_name
;
3764 /* add consumers devices */
3766 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
3767 ret
= set_consumer_device_supply(rdev
,
3768 init_data
->consumer_supplies
[i
].dev_name
,
3769 init_data
->consumer_supplies
[i
].supply
);
3771 dev_err(dev
, "Failed to set supply %s\n",
3772 init_data
->consumer_supplies
[i
].supply
);
3773 goto unset_supplies
;
3778 list_add(&rdev
->list
, ®ulator_list
);
3780 rdev_init_debugfs(rdev
);
3782 mutex_unlock(®ulator_list_mutex
);
3787 unset_regulator_supplies(rdev
);
3790 regulator_ena_gpio_free(rdev
);
3791 kfree(rdev
->constraints
);
3793 device_unregister(&rdev
->dev
);
3794 /* device core frees rdev */
3795 rdev
= ERR_PTR(ret
);
3800 rdev
= ERR_PTR(ret
);
3803 EXPORT_SYMBOL_GPL(regulator_register
);
3806 * regulator_unregister - unregister regulator
3807 * @rdev: regulator to unregister
3809 * Called by regulator drivers to unregister a regulator.
3811 void regulator_unregister(struct regulator_dev
*rdev
)
3817 while (rdev
->use_count
--)
3818 regulator_disable(rdev
->supply
);
3819 regulator_put(rdev
->supply
);
3821 mutex_lock(®ulator_list_mutex
);
3822 debugfs_remove_recursive(rdev
->debugfs
);
3823 flush_work(&rdev
->disable_work
.work
);
3824 WARN_ON(rdev
->open_count
);
3825 unset_regulator_supplies(rdev
);
3826 list_del(&rdev
->list
);
3827 kfree(rdev
->constraints
);
3828 regulator_ena_gpio_free(rdev
);
3829 of_node_put(rdev
->dev
.of_node
);
3830 device_unregister(&rdev
->dev
);
3831 mutex_unlock(®ulator_list_mutex
);
3833 EXPORT_SYMBOL_GPL(regulator_unregister
);
3836 * regulator_suspend_prepare - prepare regulators for system wide suspend
3837 * @state: system suspend state
3839 * Configure each regulator with it's suspend operating parameters for state.
3840 * This will usually be called by machine suspend code prior to supending.
3842 int regulator_suspend_prepare(suspend_state_t state
)
3844 struct regulator_dev
*rdev
;
3847 /* ON is handled by regulator active state */
3848 if (state
== PM_SUSPEND_ON
)
3851 mutex_lock(®ulator_list_mutex
);
3852 list_for_each_entry(rdev
, ®ulator_list
, list
) {
3854 mutex_lock(&rdev
->mutex
);
3855 ret
= suspend_prepare(rdev
, state
);
3856 mutex_unlock(&rdev
->mutex
);
3859 rdev_err(rdev
, "failed to prepare\n");
3864 mutex_unlock(®ulator_list_mutex
);
3867 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
3870 * regulator_suspend_finish - resume regulators from system wide suspend
3872 * Turn on regulators that might be turned off by regulator_suspend_prepare
3873 * and that should be turned on according to the regulators properties.
3875 int regulator_suspend_finish(void)
3877 struct regulator_dev
*rdev
;
3880 mutex_lock(®ulator_list_mutex
);
3881 list_for_each_entry(rdev
, ®ulator_list
, list
) {
3882 mutex_lock(&rdev
->mutex
);
3883 if (rdev
->use_count
> 0 || rdev
->constraints
->always_on
) {
3884 if (!_regulator_is_enabled(rdev
)) {
3885 error
= _regulator_do_enable(rdev
);
3890 if (!have_full_constraints())
3892 if (!_regulator_is_enabled(rdev
))
3895 error
= _regulator_do_disable(rdev
);
3900 mutex_unlock(&rdev
->mutex
);
3902 mutex_unlock(®ulator_list_mutex
);
3905 EXPORT_SYMBOL_GPL(regulator_suspend_finish
);
3908 * regulator_has_full_constraints - the system has fully specified constraints
3910 * Calling this function will cause the regulator API to disable all
3911 * regulators which have a zero use count and don't have an always_on
3912 * constraint in a late_initcall.
3914 * The intention is that this will become the default behaviour in a
3915 * future kernel release so users are encouraged to use this facility
3918 void regulator_has_full_constraints(void)
3920 has_full_constraints
= 1;
3922 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
3925 * rdev_get_drvdata - get rdev regulator driver data
3928 * Get rdev regulator driver private data. This call can be used in the
3929 * regulator driver context.
3931 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
3933 return rdev
->reg_data
;
3935 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
3938 * regulator_get_drvdata - get regulator driver data
3939 * @regulator: regulator
3941 * Get regulator driver private data. This call can be used in the consumer
3942 * driver context when non API regulator specific functions need to be called.
3944 void *regulator_get_drvdata(struct regulator
*regulator
)
3946 return regulator
->rdev
->reg_data
;
3948 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
3951 * regulator_set_drvdata - set regulator driver data
3952 * @regulator: regulator
3955 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
3957 regulator
->rdev
->reg_data
= data
;
3959 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
3962 * regulator_get_id - get regulator ID
3965 int rdev_get_id(struct regulator_dev
*rdev
)
3967 return rdev
->desc
->id
;
3969 EXPORT_SYMBOL_GPL(rdev_get_id
);
3971 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
3975 EXPORT_SYMBOL_GPL(rdev_get_dev
);
3977 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
3979 return reg_init_data
->driver_data
;
3981 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
3983 #ifdef CONFIG_DEBUG_FS
3984 static ssize_t
supply_map_read_file(struct file
*file
, char __user
*user_buf
,
3985 size_t count
, loff_t
*ppos
)
3987 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3988 ssize_t len
, ret
= 0;
3989 struct regulator_map
*map
;
3994 list_for_each_entry(map
, ®ulator_map_list
, list
) {
3995 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
3997 rdev_get_name(map
->regulator
), map
->dev_name
,
4001 if (ret
> PAGE_SIZE
) {
4007 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
4015 static const struct file_operations supply_map_fops
= {
4016 #ifdef CONFIG_DEBUG_FS
4017 .read
= supply_map_read_file
,
4018 .llseek
= default_llseek
,
4022 #ifdef CONFIG_DEBUG_FS
4023 static void regulator_summary_show_subtree(struct seq_file
*s
,
4024 struct regulator_dev
*rdev
,
4027 struct list_head
*list
= s
->private;
4028 struct regulator_dev
*child
;
4029 struct regulation_constraints
*c
;
4030 struct regulator
*consumer
;
4035 seq_printf(s
, "%*s%-*s %3d %4d %6d ",
4037 30 - level
* 3, rdev_get_name(rdev
),
4038 rdev
->use_count
, rdev
->open_count
, rdev
->bypass_count
);
4040 seq_printf(s
, "%5dmV ", _regulator_get_voltage(rdev
) / 1000);
4041 seq_printf(s
, "%5dmA ", _regulator_get_current_limit(rdev
) / 1000);
4043 c
= rdev
->constraints
;
4045 switch (rdev
->desc
->type
) {
4046 case REGULATOR_VOLTAGE
:
4047 seq_printf(s
, "%5dmV %5dmV ",
4048 c
->min_uV
/ 1000, c
->max_uV
/ 1000);
4050 case REGULATOR_CURRENT
:
4051 seq_printf(s
, "%5dmA %5dmA ",
4052 c
->min_uA
/ 1000, c
->max_uA
/ 1000);
4059 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
) {
4060 if (consumer
->dev
->class == ®ulator_class
)
4063 seq_printf(s
, "%*s%-*s ",
4064 (level
+ 1) * 3 + 1, "",
4065 30 - (level
+ 1) * 3, dev_name(consumer
->dev
));
4067 switch (rdev
->desc
->type
) {
4068 case REGULATOR_VOLTAGE
:
4069 seq_printf(s
, "%37dmV %5dmV",
4070 consumer
->min_uV
/ 1000,
4071 consumer
->max_uV
/ 1000);
4073 case REGULATOR_CURRENT
:
4080 list_for_each_entry(child
, list
, list
) {
4081 /* handle only non-root regulators supplied by current rdev */
4082 if (!child
->supply
|| child
->supply
->rdev
!= rdev
)
4085 regulator_summary_show_subtree(s
, child
, level
+ 1);
4089 static int regulator_summary_show(struct seq_file
*s
, void *data
)
4091 struct list_head
*list
= s
->private;
4092 struct regulator_dev
*rdev
;
4094 seq_puts(s
, " regulator use open bypass voltage current min max\n");
4095 seq_puts(s
, "-------------------------------------------------------------------------------\n");
4097 mutex_lock(®ulator_list_mutex
);
4099 list_for_each_entry(rdev
, list
, list
) {
4103 regulator_summary_show_subtree(s
, rdev
, 0);
4106 mutex_unlock(®ulator_list_mutex
);
4111 static int regulator_summary_open(struct inode
*inode
, struct file
*file
)
4113 return single_open(file
, regulator_summary_show
, inode
->i_private
);
4117 static const struct file_operations regulator_summary_fops
= {
4118 #ifdef CONFIG_DEBUG_FS
4119 .open
= regulator_summary_open
,
4121 .llseek
= seq_lseek
,
4122 .release
= single_release
,
4126 static int __init
regulator_init(void)
4130 ret
= class_register(®ulator_class
);
4132 debugfs_root
= debugfs_create_dir("regulator", NULL
);
4134 pr_warn("regulator: Failed to create debugfs directory\n");
4136 debugfs_create_file("supply_map", 0444, debugfs_root
, NULL
,
4139 debugfs_create_file("regulator_summary", 0444, debugfs_root
,
4140 ®ulator_list
, ®ulator_summary_fops
);
4142 regulator_dummy_init();
4147 /* init early to allow our consumers to complete system booting */
4148 core_initcall(regulator_init
);
4150 static int __init
regulator_init_complete(void)
4152 struct regulator_dev
*rdev
;
4153 const struct regulator_ops
*ops
;
4154 struct regulation_constraints
*c
;
4158 * Since DT doesn't provide an idiomatic mechanism for
4159 * enabling full constraints and since it's much more natural
4160 * with DT to provide them just assume that a DT enabled
4161 * system has full constraints.
4163 if (of_have_populated_dt())
4164 has_full_constraints
= true;
4166 mutex_lock(®ulator_list_mutex
);
4168 /* If we have a full configuration then disable any regulators
4169 * we have permission to change the status for and which are
4170 * not in use or always_on. This is effectively the default
4171 * for DT and ACPI as they have full constraints.
4173 list_for_each_entry(rdev
, ®ulator_list
, list
) {
4174 ops
= rdev
->desc
->ops
;
4175 c
= rdev
->constraints
;
4177 if (c
&& c
->always_on
)
4180 if (c
&& !(c
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
))
4183 mutex_lock(&rdev
->mutex
);
4185 if (rdev
->use_count
)
4188 /* If we can't read the status assume it's on. */
4189 if (ops
->is_enabled
)
4190 enabled
= ops
->is_enabled(rdev
);
4197 if (have_full_constraints()) {
4198 /* We log since this may kill the system if it
4200 rdev_info(rdev
, "disabling\n");
4201 ret
= _regulator_do_disable(rdev
);
4203 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
4205 /* The intention is that in future we will
4206 * assume that full constraints are provided
4207 * so warn even if we aren't going to do
4210 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
4214 mutex_unlock(&rdev
->mutex
);
4217 mutex_unlock(®ulator_list_mutex
);
4221 late_initcall_sync(regulator_init_complete
);