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
);
113 static struct regulator_dev
*dev_to_rdev(struct device
*dev
)
115 return container_of(dev
, struct regulator_dev
, dev
);
118 static const char *rdev_get_name(struct regulator_dev
*rdev
)
120 if (rdev
->constraints
&& rdev
->constraints
->name
)
121 return rdev
->constraints
->name
;
122 else if (rdev
->desc
->name
)
123 return rdev
->desc
->name
;
128 static bool have_full_constraints(void)
130 return has_full_constraints
|| of_have_populated_dt();
134 * of_get_regulator - get a regulator device node based on supply name
135 * @dev: Device pointer for the consumer (of regulator) device
136 * @supply: regulator supply name
138 * Extract the regulator device node corresponding to the supply name.
139 * returns the device node corresponding to the regulator if found, else
142 static struct device_node
*of_get_regulator(struct device
*dev
, const char *supply
)
144 struct device_node
*regnode
= NULL
;
145 char prop_name
[32]; /* 32 is max size of property name */
147 dev_dbg(dev
, "Looking up %s-supply from device tree\n", supply
);
149 snprintf(prop_name
, 32, "%s-supply", supply
);
150 regnode
= of_parse_phandle(dev
->of_node
, prop_name
, 0);
153 dev_dbg(dev
, "Looking up %s property in node %s failed",
154 prop_name
, dev
->of_node
->full_name
);
160 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
162 if (!rdev
->constraints
)
165 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
171 /* Platform voltage constraint check */
172 static int regulator_check_voltage(struct regulator_dev
*rdev
,
173 int *min_uV
, int *max_uV
)
175 BUG_ON(*min_uV
> *max_uV
);
177 if (!rdev
->constraints
) {
178 rdev_err(rdev
, "no constraints\n");
181 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
182 rdev_err(rdev
, "operation not allowed\n");
186 if (*max_uV
> rdev
->constraints
->max_uV
)
187 *max_uV
= rdev
->constraints
->max_uV
;
188 if (*min_uV
< rdev
->constraints
->min_uV
)
189 *min_uV
= rdev
->constraints
->min_uV
;
191 if (*min_uV
> *max_uV
) {
192 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
200 /* Make sure we select a voltage that suits the needs of all
201 * regulator consumers
203 static int regulator_check_consumers(struct regulator_dev
*rdev
,
204 int *min_uV
, int *max_uV
)
206 struct regulator
*regulator
;
208 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
210 * Assume consumers that didn't say anything are OK
211 * with anything in the constraint range.
213 if (!regulator
->min_uV
&& !regulator
->max_uV
)
216 if (*max_uV
> regulator
->max_uV
)
217 *max_uV
= regulator
->max_uV
;
218 if (*min_uV
< regulator
->min_uV
)
219 *min_uV
= regulator
->min_uV
;
222 if (*min_uV
> *max_uV
) {
223 rdev_err(rdev
, "Restricting voltage, %u-%uuV\n",
231 /* current constraint check */
232 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
233 int *min_uA
, int *max_uA
)
235 BUG_ON(*min_uA
> *max_uA
);
237 if (!rdev
->constraints
) {
238 rdev_err(rdev
, "no constraints\n");
241 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
242 rdev_err(rdev
, "operation not allowed\n");
246 if (*max_uA
> rdev
->constraints
->max_uA
)
247 *max_uA
= rdev
->constraints
->max_uA
;
248 if (*min_uA
< rdev
->constraints
->min_uA
)
249 *min_uA
= rdev
->constraints
->min_uA
;
251 if (*min_uA
> *max_uA
) {
252 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
260 /* operating mode constraint check */
261 static int regulator_mode_constrain(struct regulator_dev
*rdev
, int *mode
)
264 case REGULATOR_MODE_FAST
:
265 case REGULATOR_MODE_NORMAL
:
266 case REGULATOR_MODE_IDLE
:
267 case REGULATOR_MODE_STANDBY
:
270 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
274 if (!rdev
->constraints
) {
275 rdev_err(rdev
, "no constraints\n");
278 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
279 rdev_err(rdev
, "operation not allowed\n");
283 /* The modes are bitmasks, the most power hungry modes having
284 * the lowest values. If the requested mode isn't supported
285 * try higher modes. */
287 if (rdev
->constraints
->valid_modes_mask
& *mode
)
295 /* dynamic regulator mode switching constraint check */
296 static int regulator_check_drms(struct regulator_dev
*rdev
)
298 if (!rdev
->constraints
) {
299 rdev_err(rdev
, "no constraints\n");
302 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
303 rdev_err(rdev
, "operation not allowed\n");
309 static ssize_t
regulator_uV_show(struct device
*dev
,
310 struct device_attribute
*attr
, char *buf
)
312 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
315 mutex_lock(&rdev
->mutex
);
316 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
317 mutex_unlock(&rdev
->mutex
);
321 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
323 static ssize_t
regulator_uA_show(struct device
*dev
,
324 struct device_attribute
*attr
, char *buf
)
326 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
328 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
330 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
332 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
335 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
337 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
339 static DEVICE_ATTR_RO(name
);
341 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
344 case REGULATOR_MODE_FAST
:
345 return sprintf(buf
, "fast\n");
346 case REGULATOR_MODE_NORMAL
:
347 return sprintf(buf
, "normal\n");
348 case REGULATOR_MODE_IDLE
:
349 return sprintf(buf
, "idle\n");
350 case REGULATOR_MODE_STANDBY
:
351 return sprintf(buf
, "standby\n");
353 return sprintf(buf
, "unknown\n");
356 static ssize_t
regulator_opmode_show(struct device
*dev
,
357 struct device_attribute
*attr
, char *buf
)
359 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
361 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
363 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
365 static ssize_t
regulator_print_state(char *buf
, int state
)
368 return sprintf(buf
, "enabled\n");
370 return sprintf(buf
, "disabled\n");
372 return sprintf(buf
, "unknown\n");
375 static ssize_t
regulator_state_show(struct device
*dev
,
376 struct device_attribute
*attr
, char *buf
)
378 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
381 mutex_lock(&rdev
->mutex
);
382 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
383 mutex_unlock(&rdev
->mutex
);
387 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
389 static ssize_t
regulator_status_show(struct device
*dev
,
390 struct device_attribute
*attr
, char *buf
)
392 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
396 status
= rdev
->desc
->ops
->get_status(rdev
);
401 case REGULATOR_STATUS_OFF
:
404 case REGULATOR_STATUS_ON
:
407 case REGULATOR_STATUS_ERROR
:
410 case REGULATOR_STATUS_FAST
:
413 case REGULATOR_STATUS_NORMAL
:
416 case REGULATOR_STATUS_IDLE
:
419 case REGULATOR_STATUS_STANDBY
:
422 case REGULATOR_STATUS_BYPASS
:
425 case REGULATOR_STATUS_UNDEFINED
:
432 return sprintf(buf
, "%s\n", label
);
434 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
436 static ssize_t
regulator_min_uA_show(struct device
*dev
,
437 struct device_attribute
*attr
, char *buf
)
439 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
441 if (!rdev
->constraints
)
442 return sprintf(buf
, "constraint not defined\n");
444 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
446 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
448 static ssize_t
regulator_max_uA_show(struct device
*dev
,
449 struct device_attribute
*attr
, char *buf
)
451 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
453 if (!rdev
->constraints
)
454 return sprintf(buf
, "constraint not defined\n");
456 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
458 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
460 static ssize_t
regulator_min_uV_show(struct device
*dev
,
461 struct device_attribute
*attr
, char *buf
)
463 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
465 if (!rdev
->constraints
)
466 return sprintf(buf
, "constraint not defined\n");
468 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
470 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
472 static ssize_t
regulator_max_uV_show(struct device
*dev
,
473 struct device_attribute
*attr
, char *buf
)
475 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
477 if (!rdev
->constraints
)
478 return sprintf(buf
, "constraint not defined\n");
480 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
482 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
484 static ssize_t
regulator_total_uA_show(struct device
*dev
,
485 struct device_attribute
*attr
, char *buf
)
487 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
488 struct regulator
*regulator
;
491 mutex_lock(&rdev
->mutex
);
492 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
493 uA
+= regulator
->uA_load
;
494 mutex_unlock(&rdev
->mutex
);
495 return sprintf(buf
, "%d\n", uA
);
497 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
499 static ssize_t
num_users_show(struct device
*dev
, struct device_attribute
*attr
,
502 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
503 return sprintf(buf
, "%d\n", rdev
->use_count
);
505 static DEVICE_ATTR_RO(num_users
);
507 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
510 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
512 switch (rdev
->desc
->type
) {
513 case REGULATOR_VOLTAGE
:
514 return sprintf(buf
, "voltage\n");
515 case REGULATOR_CURRENT
:
516 return sprintf(buf
, "current\n");
518 return sprintf(buf
, "unknown\n");
520 static DEVICE_ATTR_RO(type
);
522 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
523 struct device_attribute
*attr
, char *buf
)
525 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
527 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
529 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
530 regulator_suspend_mem_uV_show
, NULL
);
532 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
533 struct device_attribute
*attr
, char *buf
)
535 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
537 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
539 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
540 regulator_suspend_disk_uV_show
, NULL
);
542 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
543 struct device_attribute
*attr
, char *buf
)
545 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
547 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
549 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
550 regulator_suspend_standby_uV_show
, NULL
);
552 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
553 struct device_attribute
*attr
, char *buf
)
555 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
557 return regulator_print_opmode(buf
,
558 rdev
->constraints
->state_mem
.mode
);
560 static DEVICE_ATTR(suspend_mem_mode
, 0444,
561 regulator_suspend_mem_mode_show
, NULL
);
563 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
564 struct device_attribute
*attr
, char *buf
)
566 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
568 return regulator_print_opmode(buf
,
569 rdev
->constraints
->state_disk
.mode
);
571 static DEVICE_ATTR(suspend_disk_mode
, 0444,
572 regulator_suspend_disk_mode_show
, NULL
);
574 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
575 struct device_attribute
*attr
, char *buf
)
577 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
579 return regulator_print_opmode(buf
,
580 rdev
->constraints
->state_standby
.mode
);
582 static DEVICE_ATTR(suspend_standby_mode
, 0444,
583 regulator_suspend_standby_mode_show
, NULL
);
585 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
586 struct device_attribute
*attr
, char *buf
)
588 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
590 return regulator_print_state(buf
,
591 rdev
->constraints
->state_mem
.enabled
);
593 static DEVICE_ATTR(suspend_mem_state
, 0444,
594 regulator_suspend_mem_state_show
, NULL
);
596 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
597 struct device_attribute
*attr
, char *buf
)
599 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
601 return regulator_print_state(buf
,
602 rdev
->constraints
->state_disk
.enabled
);
604 static DEVICE_ATTR(suspend_disk_state
, 0444,
605 regulator_suspend_disk_state_show
, NULL
);
607 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
608 struct device_attribute
*attr
, char *buf
)
610 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
612 return regulator_print_state(buf
,
613 rdev
->constraints
->state_standby
.enabled
);
615 static DEVICE_ATTR(suspend_standby_state
, 0444,
616 regulator_suspend_standby_state_show
, NULL
);
618 static ssize_t
regulator_bypass_show(struct device
*dev
,
619 struct device_attribute
*attr
, char *buf
)
621 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
626 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypass
);
635 return sprintf(buf
, "%s\n", report
);
637 static DEVICE_ATTR(bypass
, 0444,
638 regulator_bypass_show
, NULL
);
640 /* Calculate the new optimum regulator operating mode based on the new total
641 * consumer load. All locks held by caller */
642 static int drms_uA_update(struct regulator_dev
*rdev
)
644 struct regulator
*sibling
;
645 int current_uA
= 0, output_uV
, input_uV
, err
;
648 lockdep_assert_held_once(&rdev
->mutex
);
651 * first check to see if we can set modes at all, otherwise just
652 * tell the consumer everything is OK.
654 err
= regulator_check_drms(rdev
);
658 if (!rdev
->desc
->ops
->get_optimum_mode
&&
659 !rdev
->desc
->ops
->set_load
)
662 if (!rdev
->desc
->ops
->set_mode
&&
663 !rdev
->desc
->ops
->set_load
)
666 /* get output voltage */
667 output_uV
= _regulator_get_voltage(rdev
);
668 if (output_uV
<= 0) {
669 rdev_err(rdev
, "invalid output voltage found\n");
673 /* get input voltage */
676 input_uV
= regulator_get_voltage(rdev
->supply
);
678 input_uV
= rdev
->constraints
->input_uV
;
680 rdev_err(rdev
, "invalid input voltage found\n");
684 /* calc total requested load */
685 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
686 current_uA
+= sibling
->uA_load
;
688 current_uA
+= rdev
->constraints
->system_load
;
690 if (rdev
->desc
->ops
->set_load
) {
691 /* set the optimum mode for our new total regulator load */
692 err
= rdev
->desc
->ops
->set_load(rdev
, current_uA
);
694 rdev_err(rdev
, "failed to set load %d\n", current_uA
);
696 /* now get the optimum mode for our new total regulator load */
697 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
698 output_uV
, current_uA
);
700 /* check the new mode is allowed */
701 err
= regulator_mode_constrain(rdev
, &mode
);
703 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
704 current_uA
, input_uV
, output_uV
);
708 err
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
710 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
716 static int suspend_set_state(struct regulator_dev
*rdev
,
717 struct regulator_state
*rstate
)
721 /* If we have no suspend mode configration don't set anything;
722 * only warn if the driver implements set_suspend_voltage or
723 * set_suspend_mode callback.
725 if (!rstate
->enabled
&& !rstate
->disabled
) {
726 if (rdev
->desc
->ops
->set_suspend_voltage
||
727 rdev
->desc
->ops
->set_suspend_mode
)
728 rdev_warn(rdev
, "No configuration\n");
732 if (rstate
->enabled
&& rstate
->disabled
) {
733 rdev_err(rdev
, "invalid configuration\n");
737 if (rstate
->enabled
&& rdev
->desc
->ops
->set_suspend_enable
)
738 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
739 else if (rstate
->disabled
&& rdev
->desc
->ops
->set_suspend_disable
)
740 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
741 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
745 rdev_err(rdev
, "failed to enabled/disable\n");
749 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
750 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
752 rdev_err(rdev
, "failed to set voltage\n");
757 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
758 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
760 rdev_err(rdev
, "failed to set mode\n");
767 /* locks held by caller */
768 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
770 lockdep_assert_held_once(&rdev
->mutex
);
772 if (!rdev
->constraints
)
776 case PM_SUSPEND_STANDBY
:
777 return suspend_set_state(rdev
,
778 &rdev
->constraints
->state_standby
);
780 return suspend_set_state(rdev
,
781 &rdev
->constraints
->state_mem
);
783 return suspend_set_state(rdev
,
784 &rdev
->constraints
->state_disk
);
790 static void print_constraints(struct regulator_dev
*rdev
)
792 struct regulation_constraints
*constraints
= rdev
->constraints
;
794 size_t len
= sizeof(buf
) - 1;
798 if (constraints
->min_uV
&& constraints
->max_uV
) {
799 if (constraints
->min_uV
== constraints
->max_uV
)
800 count
+= scnprintf(buf
+ count
, len
- count
, "%d mV ",
801 constraints
->min_uV
/ 1000);
803 count
+= scnprintf(buf
+ count
, len
- count
,
805 constraints
->min_uV
/ 1000,
806 constraints
->max_uV
/ 1000);
809 if (!constraints
->min_uV
||
810 constraints
->min_uV
!= constraints
->max_uV
) {
811 ret
= _regulator_get_voltage(rdev
);
813 count
+= scnprintf(buf
+ count
, len
- count
,
814 "at %d mV ", ret
/ 1000);
817 if (constraints
->uV_offset
)
818 count
+= scnprintf(buf
+ count
, len
- count
, "%dmV offset ",
819 constraints
->uV_offset
/ 1000);
821 if (constraints
->min_uA
&& constraints
->max_uA
) {
822 if (constraints
->min_uA
== constraints
->max_uA
)
823 count
+= scnprintf(buf
+ count
, len
- count
, "%d mA ",
824 constraints
->min_uA
/ 1000);
826 count
+= scnprintf(buf
+ count
, len
- count
,
828 constraints
->min_uA
/ 1000,
829 constraints
->max_uA
/ 1000);
832 if (!constraints
->min_uA
||
833 constraints
->min_uA
!= constraints
->max_uA
) {
834 ret
= _regulator_get_current_limit(rdev
);
836 count
+= scnprintf(buf
+ count
, len
- count
,
837 "at %d mA ", ret
/ 1000);
840 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
841 count
+= scnprintf(buf
+ count
, len
- count
, "fast ");
842 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
843 count
+= scnprintf(buf
+ count
, len
- count
, "normal ");
844 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
845 count
+= scnprintf(buf
+ count
, len
- count
, "idle ");
846 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
847 count
+= scnprintf(buf
+ count
, len
- count
, "standby");
850 scnprintf(buf
, len
, "no parameters");
852 rdev_dbg(rdev
, "%s\n", buf
);
854 if ((constraints
->min_uV
!= constraints
->max_uV
) &&
855 !(constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
))
857 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
860 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
861 struct regulation_constraints
*constraints
)
863 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
866 /* do we need to apply the constraint voltage */
867 if (rdev
->constraints
->apply_uV
&&
868 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
) {
869 int current_uV
= _regulator_get_voltage(rdev
);
870 if (current_uV
< 0) {
872 "failed to get the current voltage(%d)\n",
876 if (current_uV
< rdev
->constraints
->min_uV
||
877 current_uV
> rdev
->constraints
->max_uV
) {
878 ret
= _regulator_do_set_voltage(
879 rdev
, rdev
->constraints
->min_uV
,
880 rdev
->constraints
->max_uV
);
883 "failed to apply %duV constraint(%d)\n",
884 rdev
->constraints
->min_uV
, ret
);
890 /* constrain machine-level voltage specs to fit
891 * the actual range supported by this regulator.
893 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
894 int count
= rdev
->desc
->n_voltages
;
896 int min_uV
= INT_MAX
;
897 int max_uV
= INT_MIN
;
898 int cmin
= constraints
->min_uV
;
899 int cmax
= constraints
->max_uV
;
901 /* it's safe to autoconfigure fixed-voltage supplies
902 and the constraints are used by list_voltage. */
903 if (count
== 1 && !cmin
) {
906 constraints
->min_uV
= cmin
;
907 constraints
->max_uV
= cmax
;
910 /* voltage constraints are optional */
911 if ((cmin
== 0) && (cmax
== 0))
914 /* else require explicit machine-level constraints */
915 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
916 rdev_err(rdev
, "invalid voltage constraints\n");
920 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
921 for (i
= 0; i
< count
; i
++) {
924 value
= ops
->list_voltage(rdev
, i
);
928 /* maybe adjust [min_uV..max_uV] */
929 if (value
>= cmin
&& value
< min_uV
)
931 if (value
<= cmax
&& value
> max_uV
)
935 /* final: [min_uV..max_uV] valid iff constraints valid */
936 if (max_uV
< min_uV
) {
938 "unsupportable voltage constraints %u-%uuV\n",
943 /* use regulator's subset of machine constraints */
944 if (constraints
->min_uV
< min_uV
) {
945 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
946 constraints
->min_uV
, min_uV
);
947 constraints
->min_uV
= min_uV
;
949 if (constraints
->max_uV
> max_uV
) {
950 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
951 constraints
->max_uV
, max_uV
);
952 constraints
->max_uV
= max_uV
;
959 static int machine_constraints_current(struct regulator_dev
*rdev
,
960 struct regulation_constraints
*constraints
)
962 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
965 if (!constraints
->min_uA
&& !constraints
->max_uA
)
968 if (constraints
->min_uA
> constraints
->max_uA
) {
969 rdev_err(rdev
, "Invalid current constraints\n");
973 if (!ops
->set_current_limit
|| !ops
->get_current_limit
) {
974 rdev_warn(rdev
, "Operation of current configuration missing\n");
978 /* Set regulator current in constraints range */
979 ret
= ops
->set_current_limit(rdev
, constraints
->min_uA
,
980 constraints
->max_uA
);
982 rdev_err(rdev
, "Failed to set current constraint, %d\n", ret
);
989 static int _regulator_do_enable(struct regulator_dev
*rdev
);
992 * set_machine_constraints - sets regulator constraints
993 * @rdev: regulator source
994 * @constraints: constraints to apply
996 * Allows platform initialisation code to define and constrain
997 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
998 * Constraints *must* be set by platform code in order for some
999 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1002 static int set_machine_constraints(struct regulator_dev
*rdev
,
1003 const struct regulation_constraints
*constraints
)
1006 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1009 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
1012 rdev
->constraints
= kzalloc(sizeof(*constraints
),
1014 if (!rdev
->constraints
)
1017 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
1021 ret
= machine_constraints_current(rdev
, rdev
->constraints
);
1025 if (rdev
->constraints
->ilim_uA
&& ops
->set_input_current_limit
) {
1026 ret
= ops
->set_input_current_limit(rdev
,
1027 rdev
->constraints
->ilim_uA
);
1029 rdev_err(rdev
, "failed to set input limit\n");
1034 /* do we need to setup our suspend state */
1035 if (rdev
->constraints
->initial_state
) {
1036 ret
= suspend_prepare(rdev
, rdev
->constraints
->initial_state
);
1038 rdev_err(rdev
, "failed to set suspend state\n");
1043 if (rdev
->constraints
->initial_mode
) {
1044 if (!ops
->set_mode
) {
1045 rdev_err(rdev
, "no set_mode operation\n");
1050 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
1052 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
1057 /* If the constraints say the regulator should be on at this point
1058 * and we have control then make sure it is enabled.
1060 if (rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) {
1061 ret
= _regulator_do_enable(rdev
);
1062 if (ret
< 0 && ret
!= -EINVAL
) {
1063 rdev_err(rdev
, "failed to enable\n");
1068 if ((rdev
->constraints
->ramp_delay
|| rdev
->constraints
->ramp_disable
)
1069 && ops
->set_ramp_delay
) {
1070 ret
= ops
->set_ramp_delay(rdev
, rdev
->constraints
->ramp_delay
);
1072 rdev_err(rdev
, "failed to set ramp_delay\n");
1077 if (rdev
->constraints
->pull_down
&& ops
->set_pull_down
) {
1078 ret
= ops
->set_pull_down(rdev
);
1080 rdev_err(rdev
, "failed to set pull down\n");
1085 if (rdev
->constraints
->soft_start
&& ops
->set_soft_start
) {
1086 ret
= ops
->set_soft_start(rdev
);
1088 rdev_err(rdev
, "failed to set soft start\n");
1093 print_constraints(rdev
);
1096 kfree(rdev
->constraints
);
1097 rdev
->constraints
= NULL
;
1102 * set_supply - set regulator supply regulator
1103 * @rdev: regulator name
1104 * @supply_rdev: supply regulator name
1106 * Called by platform initialisation code to set the supply regulator for this
1107 * regulator. This ensures that a regulators supply will also be enabled by the
1108 * core if it's child is enabled.
1110 static int set_supply(struct regulator_dev
*rdev
,
1111 struct regulator_dev
*supply_rdev
)
1115 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
1117 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
1118 if (rdev
->supply
== NULL
) {
1122 supply_rdev
->open_count
++;
1128 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1129 * @rdev: regulator source
1130 * @consumer_dev_name: dev_name() string for device supply applies to
1131 * @supply: symbolic name for supply
1133 * Allows platform initialisation code to map physical regulator
1134 * sources to symbolic names for supplies for use by devices. Devices
1135 * should use these symbolic names to request regulators, avoiding the
1136 * need to provide board-specific regulator names as platform data.
1138 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1139 const char *consumer_dev_name
,
1142 struct regulator_map
*node
;
1148 if (consumer_dev_name
!= NULL
)
1153 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1154 if (node
->dev_name
&& consumer_dev_name
) {
1155 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1157 } else if (node
->dev_name
|| consumer_dev_name
) {
1161 if (strcmp(node
->supply
, supply
) != 0)
1164 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1166 dev_name(&node
->regulator
->dev
),
1167 node
->regulator
->desc
->name
,
1169 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1173 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1177 node
->regulator
= rdev
;
1178 node
->supply
= supply
;
1181 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1182 if (node
->dev_name
== NULL
) {
1188 list_add(&node
->list
, ®ulator_map_list
);
1192 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1194 struct regulator_map
*node
, *n
;
1196 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1197 if (rdev
== node
->regulator
) {
1198 list_del(&node
->list
);
1199 kfree(node
->dev_name
);
1205 #define REG_STR_SIZE 64
1207 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1209 const char *supply_name
)
1211 struct regulator
*regulator
;
1212 char buf
[REG_STR_SIZE
];
1215 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1216 if (regulator
== NULL
)
1219 mutex_lock(&rdev
->mutex
);
1220 regulator
->rdev
= rdev
;
1221 list_add(®ulator
->list
, &rdev
->consumer_list
);
1224 regulator
->dev
= dev
;
1226 /* Add a link to the device sysfs entry */
1227 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1228 dev
->kobj
.name
, supply_name
);
1229 if (size
>= REG_STR_SIZE
)
1232 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1233 if (regulator
->supply_name
== NULL
)
1236 err
= sysfs_create_link_nowarn(&rdev
->dev
.kobj
, &dev
->kobj
,
1239 rdev_dbg(rdev
, "could not add device link %s err %d\n",
1240 dev
->kobj
.name
, err
);
1244 regulator
->supply_name
= kstrdup(supply_name
, GFP_KERNEL
);
1245 if (regulator
->supply_name
== NULL
)
1249 regulator
->debugfs
= debugfs_create_dir(regulator
->supply_name
,
1251 if (!regulator
->debugfs
) {
1252 rdev_warn(rdev
, "Failed to create debugfs directory\n");
1254 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1255 ®ulator
->uA_load
);
1256 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1257 ®ulator
->min_uV
);
1258 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1259 ®ulator
->max_uV
);
1263 * Check now if the regulator is an always on regulator - if
1264 * it is then we don't need to do nearly so much work for
1265 * enable/disable calls.
1267 if (!_regulator_can_change_status(rdev
) &&
1268 _regulator_is_enabled(rdev
))
1269 regulator
->always_on
= true;
1271 mutex_unlock(&rdev
->mutex
);
1274 list_del(®ulator
->list
);
1276 mutex_unlock(&rdev
->mutex
);
1280 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1282 if (rdev
->constraints
&& rdev
->constraints
->enable_time
)
1283 return rdev
->constraints
->enable_time
;
1284 if (!rdev
->desc
->ops
->enable_time
)
1285 return rdev
->desc
->enable_time
;
1286 return rdev
->desc
->ops
->enable_time(rdev
);
1289 static struct regulator_supply_alias
*regulator_find_supply_alias(
1290 struct device
*dev
, const char *supply
)
1292 struct regulator_supply_alias
*map
;
1294 list_for_each_entry(map
, ®ulator_supply_alias_list
, list
)
1295 if (map
->src_dev
== dev
&& strcmp(map
->src_supply
, supply
) == 0)
1301 static void regulator_supply_alias(struct device
**dev
, const char **supply
)
1303 struct regulator_supply_alias
*map
;
1305 map
= regulator_find_supply_alias(*dev
, *supply
);
1307 dev_dbg(*dev
, "Mapping supply %s to %s,%s\n",
1308 *supply
, map
->alias_supply
,
1309 dev_name(map
->alias_dev
));
1310 *dev
= map
->alias_dev
;
1311 *supply
= map
->alias_supply
;
1315 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1319 struct regulator_dev
*r
;
1320 struct device_node
*node
;
1321 struct regulator_map
*map
;
1322 const char *devname
= NULL
;
1324 regulator_supply_alias(&dev
, &supply
);
1326 /* first do a dt based lookup */
1327 if (dev
&& dev
->of_node
) {
1328 node
= of_get_regulator(dev
, supply
);
1330 list_for_each_entry(r
, ®ulator_list
, list
)
1331 if (r
->dev
.parent
&&
1332 node
== r
->dev
.of_node
)
1334 *ret
= -EPROBE_DEFER
;
1338 * If we couldn't even get the node then it's
1339 * not just that the device didn't register
1340 * yet, there's no node and we'll never
1347 /* if not found, try doing it non-dt way */
1349 devname
= dev_name(dev
);
1351 list_for_each_entry(r
, ®ulator_list
, list
)
1352 if (strcmp(rdev_get_name(r
), supply
) == 0)
1355 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1356 /* If the mapping has a device set up it must match */
1357 if (map
->dev_name
&&
1358 (!devname
|| strcmp(map
->dev_name
, devname
)))
1361 if (strcmp(map
->supply
, supply
) == 0)
1362 return map
->regulator
;
1369 static int regulator_resolve_supply(struct regulator_dev
*rdev
)
1371 struct regulator_dev
*r
;
1372 struct device
*dev
= rdev
->dev
.parent
;
1375 /* No supply to resovle? */
1376 if (!rdev
->supply_name
)
1379 /* Supply already resolved? */
1383 r
= regulator_dev_lookup(dev
, rdev
->supply_name
, &ret
);
1384 if (ret
== -ENODEV
) {
1386 * No supply was specified for this regulator and
1387 * there will never be one.
1393 dev_err(dev
, "Failed to resolve %s-supply for %s\n",
1394 rdev
->supply_name
, rdev
->desc
->name
);
1395 return -EPROBE_DEFER
;
1398 /* Recursively resolve the supply of the supply */
1399 ret
= regulator_resolve_supply(r
);
1403 ret
= set_supply(rdev
, r
);
1407 /* Cascade always-on state to supply */
1408 if (_regulator_is_enabled(rdev
)) {
1409 ret
= regulator_enable(rdev
->supply
);
1417 /* Internal regulator request function */
1418 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1419 bool exclusive
, bool allow_dummy
)
1421 struct regulator_dev
*rdev
;
1422 struct regulator
*regulator
= ERR_PTR(-EPROBE_DEFER
);
1423 const char *devname
= NULL
;
1427 pr_err("get() with no identifier\n");
1428 return ERR_PTR(-EINVAL
);
1432 devname
= dev_name(dev
);
1434 if (have_full_constraints())
1437 ret
= -EPROBE_DEFER
;
1439 mutex_lock(®ulator_list_mutex
);
1441 rdev
= regulator_dev_lookup(dev
, id
, &ret
);
1445 regulator
= ERR_PTR(ret
);
1448 * If we have return value from dev_lookup fail, we do not expect to
1449 * succeed, so, quit with appropriate error value
1451 if (ret
&& ret
!= -ENODEV
)
1455 devname
= "deviceless";
1458 * Assume that a regulator is physically present and enabled
1459 * even if it isn't hooked up and just provide a dummy.
1461 if (have_full_constraints() && allow_dummy
) {
1462 pr_warn("%s supply %s not found, using dummy regulator\n",
1465 rdev
= dummy_regulator_rdev
;
1467 /* Don't log an error when called from regulator_get_optional() */
1468 } else if (!have_full_constraints() || exclusive
) {
1469 dev_warn(dev
, "dummy supplies not allowed\n");
1472 mutex_unlock(®ulator_list_mutex
);
1476 if (rdev
->exclusive
) {
1477 regulator
= ERR_PTR(-EPERM
);
1481 if (exclusive
&& rdev
->open_count
) {
1482 regulator
= ERR_PTR(-EBUSY
);
1486 ret
= regulator_resolve_supply(rdev
);
1488 regulator
= ERR_PTR(ret
);
1492 if (!try_module_get(rdev
->owner
))
1495 regulator
= create_regulator(rdev
, dev
, id
);
1496 if (regulator
== NULL
) {
1497 regulator
= ERR_PTR(-ENOMEM
);
1498 module_put(rdev
->owner
);
1504 rdev
->exclusive
= 1;
1506 ret
= _regulator_is_enabled(rdev
);
1508 rdev
->use_count
= 1;
1510 rdev
->use_count
= 0;
1514 mutex_unlock(®ulator_list_mutex
);
1520 * regulator_get - lookup and obtain a reference to a regulator.
1521 * @dev: device for regulator "consumer"
1522 * @id: Supply name or regulator ID.
1524 * Returns a struct regulator corresponding to the regulator producer,
1525 * or IS_ERR() condition containing errno.
1527 * Use of supply names configured via regulator_set_device_supply() is
1528 * strongly encouraged. It is recommended that the supply name used
1529 * should match the name used for the supply and/or the relevant
1530 * device pins in the datasheet.
1532 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1534 return _regulator_get(dev
, id
, false, true);
1536 EXPORT_SYMBOL_GPL(regulator_get
);
1539 * regulator_get_exclusive - obtain exclusive access to a regulator.
1540 * @dev: device for regulator "consumer"
1541 * @id: Supply name or regulator ID.
1543 * Returns a struct regulator corresponding to the regulator producer,
1544 * or IS_ERR() condition containing errno. Other consumers will be
1545 * unable to obtain this regulator while this reference is held and the
1546 * use count for the regulator will be initialised to reflect the current
1547 * state of the regulator.
1549 * This is intended for use by consumers which cannot tolerate shared
1550 * use of the regulator such as those which need to force the
1551 * regulator off for correct operation of the hardware they are
1554 * Use of supply names configured via regulator_set_device_supply() is
1555 * strongly encouraged. It is recommended that the supply name used
1556 * should match the name used for the supply and/or the relevant
1557 * device pins in the datasheet.
1559 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1561 return _regulator_get(dev
, id
, true, false);
1563 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1566 * regulator_get_optional - obtain optional access to a regulator.
1567 * @dev: device for regulator "consumer"
1568 * @id: Supply name or regulator ID.
1570 * Returns a struct regulator corresponding to the regulator producer,
1571 * or IS_ERR() condition containing errno.
1573 * This is intended for use by consumers for devices which can have
1574 * some supplies unconnected in normal use, such as some MMC devices.
1575 * It can allow the regulator core to provide stub supplies for other
1576 * supplies requested using normal regulator_get() calls without
1577 * disrupting the operation of drivers that can handle absent
1580 * Use of supply names configured via regulator_set_device_supply() is
1581 * strongly encouraged. It is recommended that the supply name used
1582 * should match the name used for the supply and/or the relevant
1583 * device pins in the datasheet.
1585 struct regulator
*regulator_get_optional(struct device
*dev
, const char *id
)
1587 return _regulator_get(dev
, id
, false, false);
1589 EXPORT_SYMBOL_GPL(regulator_get_optional
);
1591 /* regulator_list_mutex lock held by regulator_put() */
1592 static void _regulator_put(struct regulator
*regulator
)
1594 struct regulator_dev
*rdev
;
1596 if (regulator
== NULL
|| IS_ERR(regulator
))
1599 lockdep_assert_held_once(®ulator_list_mutex
);
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 list_del(®ulator
->list
);
1612 rdev
->exclusive
= 0;
1613 mutex_unlock(&rdev
->mutex
);
1615 kfree(regulator
->supply_name
);
1618 module_put(rdev
->owner
);
1622 * regulator_put - "free" the regulator source
1623 * @regulator: regulator source
1625 * Note: drivers must ensure that all regulator_enable calls made on this
1626 * regulator source are balanced by regulator_disable calls prior to calling
1629 void regulator_put(struct regulator
*regulator
)
1631 mutex_lock(®ulator_list_mutex
);
1632 _regulator_put(regulator
);
1633 mutex_unlock(®ulator_list_mutex
);
1635 EXPORT_SYMBOL_GPL(regulator_put
);
1638 * regulator_register_supply_alias - Provide device alias for supply lookup
1640 * @dev: device that will be given as the regulator "consumer"
1641 * @id: Supply name or regulator ID
1642 * @alias_dev: device that should be used to lookup the supply
1643 * @alias_id: Supply name or regulator ID that should be used to lookup the
1646 * All lookups for id on dev will instead be conducted for alias_id on
1649 int regulator_register_supply_alias(struct device
*dev
, const char *id
,
1650 struct device
*alias_dev
,
1651 const char *alias_id
)
1653 struct regulator_supply_alias
*map
;
1655 map
= regulator_find_supply_alias(dev
, id
);
1659 map
= kzalloc(sizeof(struct regulator_supply_alias
), GFP_KERNEL
);
1664 map
->src_supply
= id
;
1665 map
->alias_dev
= alias_dev
;
1666 map
->alias_supply
= alias_id
;
1668 list_add(&map
->list
, ®ulator_supply_alias_list
);
1670 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1671 id
, dev_name(dev
), alias_id
, dev_name(alias_dev
));
1675 EXPORT_SYMBOL_GPL(regulator_register_supply_alias
);
1678 * regulator_unregister_supply_alias - Remove device alias
1680 * @dev: device that will be given as the regulator "consumer"
1681 * @id: Supply name or regulator ID
1683 * Remove a lookup alias if one exists for id on dev.
1685 void regulator_unregister_supply_alias(struct device
*dev
, const char *id
)
1687 struct regulator_supply_alias
*map
;
1689 map
= regulator_find_supply_alias(dev
, id
);
1691 list_del(&map
->list
);
1695 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias
);
1698 * regulator_bulk_register_supply_alias - register multiple aliases
1700 * @dev: device that will be given as the regulator "consumer"
1701 * @id: List of supply names or regulator IDs
1702 * @alias_dev: device that should be used to lookup the supply
1703 * @alias_id: List of supply names or regulator IDs that should be used to
1705 * @num_id: Number of aliases to register
1707 * @return 0 on success, an errno on failure.
1709 * This helper function allows drivers to register several supply
1710 * aliases in one operation. If any of the aliases cannot be
1711 * registered any aliases that were registered will be removed
1712 * before returning to the caller.
1714 int regulator_bulk_register_supply_alias(struct device
*dev
,
1715 const char *const *id
,
1716 struct device
*alias_dev
,
1717 const char *const *alias_id
,
1723 for (i
= 0; i
< num_id
; ++i
) {
1724 ret
= regulator_register_supply_alias(dev
, id
[i
], alias_dev
,
1734 "Failed to create supply alias %s,%s -> %s,%s\n",
1735 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
1738 regulator_unregister_supply_alias(dev
, id
[i
]);
1742 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias
);
1745 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1747 * @dev: device that will be given as the regulator "consumer"
1748 * @id: List of supply names or regulator IDs
1749 * @num_id: Number of aliases to unregister
1751 * This helper function allows drivers to unregister several supply
1752 * aliases in one operation.
1754 void regulator_bulk_unregister_supply_alias(struct device
*dev
,
1755 const char *const *id
,
1760 for (i
= 0; i
< num_id
; ++i
)
1761 regulator_unregister_supply_alias(dev
, id
[i
]);
1763 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias
);
1766 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1767 static int regulator_ena_gpio_request(struct regulator_dev
*rdev
,
1768 const struct regulator_config
*config
)
1770 struct regulator_enable_gpio
*pin
;
1771 struct gpio_desc
*gpiod
;
1774 gpiod
= gpio_to_desc(config
->ena_gpio
);
1776 list_for_each_entry(pin
, ®ulator_ena_gpio_list
, list
) {
1777 if (pin
->gpiod
== gpiod
) {
1778 rdev_dbg(rdev
, "GPIO %d is already used\n",
1780 goto update_ena_gpio_to_rdev
;
1784 ret
= gpio_request_one(config
->ena_gpio
,
1785 GPIOF_DIR_OUT
| config
->ena_gpio_flags
,
1786 rdev_get_name(rdev
));
1790 pin
= kzalloc(sizeof(struct regulator_enable_gpio
), GFP_KERNEL
);
1792 gpio_free(config
->ena_gpio
);
1797 pin
->ena_gpio_invert
= config
->ena_gpio_invert
;
1798 list_add(&pin
->list
, ®ulator_ena_gpio_list
);
1800 update_ena_gpio_to_rdev
:
1801 pin
->request_count
++;
1802 rdev
->ena_pin
= pin
;
1806 static void regulator_ena_gpio_free(struct regulator_dev
*rdev
)
1808 struct regulator_enable_gpio
*pin
, *n
;
1813 /* Free the GPIO only in case of no use */
1814 list_for_each_entry_safe(pin
, n
, ®ulator_ena_gpio_list
, list
) {
1815 if (pin
->gpiod
== rdev
->ena_pin
->gpiod
) {
1816 if (pin
->request_count
<= 1) {
1817 pin
->request_count
= 0;
1818 gpiod_put(pin
->gpiod
);
1819 list_del(&pin
->list
);
1821 rdev
->ena_pin
= NULL
;
1824 pin
->request_count
--;
1831 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1832 * @rdev: regulator_dev structure
1833 * @enable: enable GPIO at initial use?
1835 * GPIO is enabled in case of initial use. (enable_count is 0)
1836 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1838 static int regulator_ena_gpio_ctrl(struct regulator_dev
*rdev
, bool enable
)
1840 struct regulator_enable_gpio
*pin
= rdev
->ena_pin
;
1846 /* Enable GPIO at initial use */
1847 if (pin
->enable_count
== 0)
1848 gpiod_set_value_cansleep(pin
->gpiod
,
1849 !pin
->ena_gpio_invert
);
1851 pin
->enable_count
++;
1853 if (pin
->enable_count
> 1) {
1854 pin
->enable_count
--;
1858 /* Disable GPIO if not used */
1859 if (pin
->enable_count
<= 1) {
1860 gpiod_set_value_cansleep(pin
->gpiod
,
1861 pin
->ena_gpio_invert
);
1862 pin
->enable_count
= 0;
1870 * _regulator_enable_delay - a delay helper function
1871 * @delay: time to delay in microseconds
1873 * Delay for the requested amount of time as per the guidelines in:
1875 * Documentation/timers/timers-howto.txt
1877 * The assumption here is that regulators will never be enabled in
1878 * atomic context and therefore sleeping functions can be used.
1880 static void _regulator_enable_delay(unsigned int delay
)
1882 unsigned int ms
= delay
/ 1000;
1883 unsigned int us
= delay
% 1000;
1887 * For small enough values, handle super-millisecond
1888 * delays in the usleep_range() call below.
1897 * Give the scheduler some room to coalesce with any other
1898 * wakeup sources. For delays shorter than 10 us, don't even
1899 * bother setting up high-resolution timers and just busy-
1903 usleep_range(us
, us
+ 100);
1908 static int _regulator_do_enable(struct regulator_dev
*rdev
)
1912 /* Query before enabling in case configuration dependent. */
1913 ret
= _regulator_get_enable_time(rdev
);
1917 rdev_warn(rdev
, "enable_time() failed: %d\n", ret
);
1921 trace_regulator_enable(rdev_get_name(rdev
));
1923 if (rdev
->desc
->off_on_delay
) {
1924 /* if needed, keep a distance of off_on_delay from last time
1925 * this regulator was disabled.
1927 unsigned long start_jiffy
= jiffies
;
1928 unsigned long intended
, max_delay
, remaining
;
1930 max_delay
= usecs_to_jiffies(rdev
->desc
->off_on_delay
);
1931 intended
= rdev
->last_off_jiffy
+ max_delay
;
1933 if (time_before(start_jiffy
, intended
)) {
1934 /* calc remaining jiffies to deal with one-time
1936 * in case of multiple timer wrapping, either it can be
1937 * detected by out-of-range remaining, or it cannot be
1938 * detected and we gets a panelty of
1939 * _regulator_enable_delay().
1941 remaining
= intended
- start_jiffy
;
1942 if (remaining
<= max_delay
)
1943 _regulator_enable_delay(
1944 jiffies_to_usecs(remaining
));
1948 if (rdev
->ena_pin
) {
1949 if (!rdev
->ena_gpio_state
) {
1950 ret
= regulator_ena_gpio_ctrl(rdev
, true);
1953 rdev
->ena_gpio_state
= 1;
1955 } else if (rdev
->desc
->ops
->enable
) {
1956 ret
= rdev
->desc
->ops
->enable(rdev
);
1963 /* Allow the regulator to ramp; it would be useful to extend
1964 * this for bulk operations so that the regulators can ramp
1966 trace_regulator_enable_delay(rdev_get_name(rdev
));
1968 _regulator_enable_delay(delay
);
1970 trace_regulator_enable_complete(rdev_get_name(rdev
));
1975 /* locks held by regulator_enable() */
1976 static int _regulator_enable(struct regulator_dev
*rdev
)
1980 lockdep_assert_held_once(&rdev
->mutex
);
1982 /* check voltage and requested load before enabling */
1983 if (rdev
->constraints
&&
1984 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1985 drms_uA_update(rdev
);
1987 if (rdev
->use_count
== 0) {
1988 /* The regulator may on if it's not switchable or left on */
1989 ret
= _regulator_is_enabled(rdev
);
1990 if (ret
== -EINVAL
|| ret
== 0) {
1991 if (!_regulator_can_change_status(rdev
))
1994 ret
= _regulator_do_enable(rdev
);
1998 } else if (ret
< 0) {
1999 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
2002 /* Fallthrough on positive return values - already enabled */
2011 * regulator_enable - enable regulator output
2012 * @regulator: regulator source
2014 * Request that the regulator be enabled with the regulator output at
2015 * the predefined voltage or current value. Calls to regulator_enable()
2016 * must be balanced with calls to regulator_disable().
2018 * NOTE: the output value can be set by other drivers, boot loader or may be
2019 * hardwired in the regulator.
2021 int regulator_enable(struct regulator
*regulator
)
2023 struct regulator_dev
*rdev
= regulator
->rdev
;
2026 if (regulator
->always_on
)
2030 ret
= regulator_enable(rdev
->supply
);
2035 mutex_lock(&rdev
->mutex
);
2036 ret
= _regulator_enable(rdev
);
2037 mutex_unlock(&rdev
->mutex
);
2039 if (ret
!= 0 && rdev
->supply
)
2040 regulator_disable(rdev
->supply
);
2044 EXPORT_SYMBOL_GPL(regulator_enable
);
2046 static int _regulator_do_disable(struct regulator_dev
*rdev
)
2050 trace_regulator_disable(rdev_get_name(rdev
));
2052 if (rdev
->ena_pin
) {
2053 if (rdev
->ena_gpio_state
) {
2054 ret
= regulator_ena_gpio_ctrl(rdev
, false);
2057 rdev
->ena_gpio_state
= 0;
2060 } else if (rdev
->desc
->ops
->disable
) {
2061 ret
= rdev
->desc
->ops
->disable(rdev
);
2066 /* cares about last_off_jiffy only if off_on_delay is required by
2069 if (rdev
->desc
->off_on_delay
)
2070 rdev
->last_off_jiffy
= jiffies
;
2072 trace_regulator_disable_complete(rdev_get_name(rdev
));
2077 /* locks held by regulator_disable() */
2078 static int _regulator_disable(struct regulator_dev
*rdev
)
2082 lockdep_assert_held_once(&rdev
->mutex
);
2084 if (WARN(rdev
->use_count
<= 0,
2085 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
2088 /* are we the last user and permitted to disable ? */
2089 if (rdev
->use_count
== 1 &&
2090 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
2092 /* we are last user */
2093 if (_regulator_can_change_status(rdev
)) {
2094 ret
= _notifier_call_chain(rdev
,
2095 REGULATOR_EVENT_PRE_DISABLE
,
2097 if (ret
& NOTIFY_STOP_MASK
)
2100 ret
= _regulator_do_disable(rdev
);
2102 rdev_err(rdev
, "failed to disable\n");
2103 _notifier_call_chain(rdev
,
2104 REGULATOR_EVENT_ABORT_DISABLE
,
2108 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
2112 rdev
->use_count
= 0;
2113 } else if (rdev
->use_count
> 1) {
2115 if (rdev
->constraints
&&
2116 (rdev
->constraints
->valid_ops_mask
&
2117 REGULATOR_CHANGE_DRMS
))
2118 drms_uA_update(rdev
);
2127 * regulator_disable - disable regulator output
2128 * @regulator: regulator source
2130 * Disable the regulator output voltage or current. Calls to
2131 * regulator_enable() must be balanced with calls to
2132 * regulator_disable().
2134 * NOTE: this will only disable the regulator output if no other consumer
2135 * devices have it enabled, the regulator device supports disabling and
2136 * machine constraints permit this operation.
2138 int regulator_disable(struct regulator
*regulator
)
2140 struct regulator_dev
*rdev
= regulator
->rdev
;
2143 if (regulator
->always_on
)
2146 mutex_lock(&rdev
->mutex
);
2147 ret
= _regulator_disable(rdev
);
2148 mutex_unlock(&rdev
->mutex
);
2150 if (ret
== 0 && rdev
->supply
)
2151 regulator_disable(rdev
->supply
);
2155 EXPORT_SYMBOL_GPL(regulator_disable
);
2157 /* locks held by regulator_force_disable() */
2158 static int _regulator_force_disable(struct regulator_dev
*rdev
)
2162 lockdep_assert_held_once(&rdev
->mutex
);
2164 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2165 REGULATOR_EVENT_PRE_DISABLE
, NULL
);
2166 if (ret
& NOTIFY_STOP_MASK
)
2169 ret
= _regulator_do_disable(rdev
);
2171 rdev_err(rdev
, "failed to force disable\n");
2172 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2173 REGULATOR_EVENT_ABORT_DISABLE
, NULL
);
2177 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2178 REGULATOR_EVENT_DISABLE
, NULL
);
2184 * regulator_force_disable - force disable regulator output
2185 * @regulator: regulator source
2187 * Forcibly disable the regulator output voltage or current.
2188 * NOTE: this *will* disable the regulator output even if other consumer
2189 * devices have it enabled. This should be used for situations when device
2190 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2192 int regulator_force_disable(struct regulator
*regulator
)
2194 struct regulator_dev
*rdev
= regulator
->rdev
;
2197 mutex_lock(&rdev
->mutex
);
2198 regulator
->uA_load
= 0;
2199 ret
= _regulator_force_disable(regulator
->rdev
);
2200 mutex_unlock(&rdev
->mutex
);
2203 while (rdev
->open_count
--)
2204 regulator_disable(rdev
->supply
);
2208 EXPORT_SYMBOL_GPL(regulator_force_disable
);
2210 static void regulator_disable_work(struct work_struct
*work
)
2212 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
2216 mutex_lock(&rdev
->mutex
);
2218 BUG_ON(!rdev
->deferred_disables
);
2220 count
= rdev
->deferred_disables
;
2221 rdev
->deferred_disables
= 0;
2223 for (i
= 0; i
< count
; i
++) {
2224 ret
= _regulator_disable(rdev
);
2226 rdev_err(rdev
, "Deferred disable failed: %d\n", ret
);
2229 mutex_unlock(&rdev
->mutex
);
2232 for (i
= 0; i
< count
; i
++) {
2233 ret
= regulator_disable(rdev
->supply
);
2236 "Supply disable failed: %d\n", ret
);
2243 * regulator_disable_deferred - disable regulator output with delay
2244 * @regulator: regulator source
2245 * @ms: miliseconds until the regulator is disabled
2247 * Execute regulator_disable() on the regulator after a delay. This
2248 * is intended for use with devices that require some time to quiesce.
2250 * NOTE: this will only disable the regulator output if no other consumer
2251 * devices have it enabled, the regulator device supports disabling and
2252 * machine constraints permit this operation.
2254 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
2256 struct regulator_dev
*rdev
= regulator
->rdev
;
2259 if (regulator
->always_on
)
2263 return regulator_disable(regulator
);
2265 mutex_lock(&rdev
->mutex
);
2266 rdev
->deferred_disables
++;
2267 mutex_unlock(&rdev
->mutex
);
2269 ret
= queue_delayed_work(system_power_efficient_wq
,
2270 &rdev
->disable_work
,
2271 msecs_to_jiffies(ms
));
2277 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
2279 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
2281 /* A GPIO control always takes precedence */
2283 return rdev
->ena_gpio_state
;
2285 /* If we don't know then assume that the regulator is always on */
2286 if (!rdev
->desc
->ops
->is_enabled
)
2289 return rdev
->desc
->ops
->is_enabled(rdev
);
2293 * regulator_is_enabled - is the regulator output enabled
2294 * @regulator: regulator source
2296 * Returns positive if the regulator driver backing the source/client
2297 * has requested that the device be enabled, zero if it hasn't, else a
2298 * negative errno code.
2300 * Note that the device backing this regulator handle can have multiple
2301 * users, so it might be enabled even if regulator_enable() was never
2302 * called for this particular source.
2304 int regulator_is_enabled(struct regulator
*regulator
)
2308 if (regulator
->always_on
)
2311 mutex_lock(®ulator
->rdev
->mutex
);
2312 ret
= _regulator_is_enabled(regulator
->rdev
);
2313 mutex_unlock(®ulator
->rdev
->mutex
);
2317 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
2320 * regulator_can_change_voltage - check if regulator can change voltage
2321 * @regulator: regulator source
2323 * Returns positive if the regulator driver backing the source/client
2324 * can change its voltage, false otherwise. Useful for detecting fixed
2325 * or dummy regulators and disabling voltage change logic in the client
2328 int regulator_can_change_voltage(struct regulator
*regulator
)
2330 struct regulator_dev
*rdev
= regulator
->rdev
;
2332 if (rdev
->constraints
&&
2333 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2334 if (rdev
->desc
->n_voltages
- rdev
->desc
->linear_min_sel
> 1)
2337 if (rdev
->desc
->continuous_voltage_range
&&
2338 rdev
->constraints
->min_uV
&& rdev
->constraints
->max_uV
&&
2339 rdev
->constraints
->min_uV
!= rdev
->constraints
->max_uV
)
2345 EXPORT_SYMBOL_GPL(regulator_can_change_voltage
);
2348 * regulator_count_voltages - count regulator_list_voltage() selectors
2349 * @regulator: regulator source
2351 * Returns number of selectors, or negative errno. Selectors are
2352 * numbered starting at zero, and typically correspond to bitfields
2353 * in hardware registers.
2355 int regulator_count_voltages(struct regulator
*regulator
)
2357 struct regulator_dev
*rdev
= regulator
->rdev
;
2359 if (rdev
->desc
->n_voltages
)
2360 return rdev
->desc
->n_voltages
;
2365 return regulator_count_voltages(rdev
->supply
);
2367 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
2370 * regulator_list_voltage - enumerate supported voltages
2371 * @regulator: regulator source
2372 * @selector: identify voltage to list
2373 * Context: can sleep
2375 * Returns a voltage that can be passed to @regulator_set_voltage(),
2376 * zero if this selector code can't be used on this system, or a
2379 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
2381 struct regulator_dev
*rdev
= regulator
->rdev
;
2382 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2385 if (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1 && !selector
)
2386 return rdev
->desc
->fixed_uV
;
2388 if (ops
->list_voltage
) {
2389 if (selector
>= rdev
->desc
->n_voltages
)
2391 mutex_lock(&rdev
->mutex
);
2392 ret
= ops
->list_voltage(rdev
, selector
);
2393 mutex_unlock(&rdev
->mutex
);
2394 } else if (rdev
->supply
) {
2395 ret
= regulator_list_voltage(rdev
->supply
, selector
);
2401 if (ret
< rdev
->constraints
->min_uV
)
2403 else if (ret
> rdev
->constraints
->max_uV
)
2409 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
2412 * regulator_get_regmap - get the regulator's register map
2413 * @regulator: regulator source
2415 * Returns the register map for the given regulator, or an ERR_PTR value
2416 * if the regulator doesn't use regmap.
2418 struct regmap
*regulator_get_regmap(struct regulator
*regulator
)
2420 struct regmap
*map
= regulator
->rdev
->regmap
;
2422 return map
? map
: ERR_PTR(-EOPNOTSUPP
);
2426 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2427 * @regulator: regulator source
2428 * @vsel_reg: voltage selector register, output parameter
2429 * @vsel_mask: mask for voltage selector bitfield, output parameter
2431 * Returns the hardware register offset and bitmask used for setting the
2432 * regulator voltage. This might be useful when configuring voltage-scaling
2433 * hardware or firmware that can make I2C requests behind the kernel's back,
2436 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2437 * and 0 is returned, otherwise a negative errno is returned.
2439 int regulator_get_hardware_vsel_register(struct regulator
*regulator
,
2441 unsigned *vsel_mask
)
2443 struct regulator_dev
*rdev
= regulator
->rdev
;
2444 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2446 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2449 *vsel_reg
= rdev
->desc
->vsel_reg
;
2450 *vsel_mask
= rdev
->desc
->vsel_mask
;
2454 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register
);
2457 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2458 * @regulator: regulator source
2459 * @selector: identify voltage to list
2461 * Converts the selector to a hardware-specific voltage selector that can be
2462 * directly written to the regulator registers. The address of the voltage
2463 * register can be determined by calling @regulator_get_hardware_vsel_register.
2465 * On error a negative errno is returned.
2467 int regulator_list_hardware_vsel(struct regulator
*regulator
,
2470 struct regulator_dev
*rdev
= regulator
->rdev
;
2471 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2473 if (selector
>= rdev
->desc
->n_voltages
)
2475 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2480 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel
);
2483 * regulator_get_linear_step - return the voltage step size between VSEL values
2484 * @regulator: regulator source
2486 * Returns the voltage step size between VSEL values for linear
2487 * regulators, or return 0 if the regulator isn't a linear regulator.
2489 unsigned int regulator_get_linear_step(struct regulator
*regulator
)
2491 struct regulator_dev
*rdev
= regulator
->rdev
;
2493 return rdev
->desc
->uV_step
;
2495 EXPORT_SYMBOL_GPL(regulator_get_linear_step
);
2498 * regulator_is_supported_voltage - check if a voltage range can be supported
2500 * @regulator: Regulator to check.
2501 * @min_uV: Minimum required voltage in uV.
2502 * @max_uV: Maximum required voltage in uV.
2504 * Returns a boolean or a negative error code.
2506 int regulator_is_supported_voltage(struct regulator
*regulator
,
2507 int min_uV
, int max_uV
)
2509 struct regulator_dev
*rdev
= regulator
->rdev
;
2510 int i
, voltages
, ret
;
2512 /* If we can't change voltage check the current voltage */
2513 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2514 ret
= regulator_get_voltage(regulator
);
2516 return min_uV
<= ret
&& ret
<= max_uV
;
2521 /* Any voltage within constrains range is fine? */
2522 if (rdev
->desc
->continuous_voltage_range
)
2523 return min_uV
>= rdev
->constraints
->min_uV
&&
2524 max_uV
<= rdev
->constraints
->max_uV
;
2526 ret
= regulator_count_voltages(regulator
);
2531 for (i
= 0; i
< voltages
; i
++) {
2532 ret
= regulator_list_voltage(regulator
, i
);
2534 if (ret
>= min_uV
&& ret
<= max_uV
)
2540 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
2542 static int _regulator_call_set_voltage(struct regulator_dev
*rdev
,
2543 int min_uV
, int max_uV
,
2546 struct pre_voltage_change_data data
;
2549 data
.old_uV
= _regulator_get_voltage(rdev
);
2550 data
.min_uV
= min_uV
;
2551 data
.max_uV
= max_uV
;
2552 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
2554 if (ret
& NOTIFY_STOP_MASK
)
2557 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
, selector
);
2561 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
2562 (void *)data
.old_uV
);
2567 static int _regulator_call_set_voltage_sel(struct regulator_dev
*rdev
,
2568 int uV
, unsigned selector
)
2570 struct pre_voltage_change_data data
;
2573 data
.old_uV
= _regulator_get_voltage(rdev
);
2576 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
2578 if (ret
& NOTIFY_STOP_MASK
)
2581 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
2585 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
2586 (void *)data
.old_uV
);
2591 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
2592 int min_uV
, int max_uV
)
2597 unsigned int selector
;
2598 int old_selector
= -1;
2600 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
2602 min_uV
+= rdev
->constraints
->uV_offset
;
2603 max_uV
+= rdev
->constraints
->uV_offset
;
2606 * If we can't obtain the old selector there is not enough
2607 * info to call set_voltage_time_sel().
2609 if (_regulator_is_enabled(rdev
) &&
2610 rdev
->desc
->ops
->set_voltage_time_sel
&&
2611 rdev
->desc
->ops
->get_voltage_sel
) {
2612 old_selector
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
2613 if (old_selector
< 0)
2614 return old_selector
;
2617 if (rdev
->desc
->ops
->set_voltage
) {
2618 ret
= _regulator_call_set_voltage(rdev
, min_uV
, max_uV
,
2622 if (rdev
->desc
->ops
->list_voltage
)
2623 best_val
= rdev
->desc
->ops
->list_voltage(rdev
,
2626 best_val
= _regulator_get_voltage(rdev
);
2629 } else if (rdev
->desc
->ops
->set_voltage_sel
) {
2630 if (rdev
->desc
->ops
->map_voltage
) {
2631 ret
= rdev
->desc
->ops
->map_voltage(rdev
, min_uV
,
2634 if (rdev
->desc
->ops
->list_voltage
==
2635 regulator_list_voltage_linear
)
2636 ret
= regulator_map_voltage_linear(rdev
,
2638 else if (rdev
->desc
->ops
->list_voltage
==
2639 regulator_list_voltage_linear_range
)
2640 ret
= regulator_map_voltage_linear_range(rdev
,
2643 ret
= regulator_map_voltage_iterate(rdev
,
2648 best_val
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
2649 if (min_uV
<= best_val
&& max_uV
>= best_val
) {
2651 if (old_selector
== selector
)
2654 ret
= _regulator_call_set_voltage_sel(
2655 rdev
, best_val
, selector
);
2664 /* Call set_voltage_time_sel if successfully obtained old_selector */
2665 if (ret
== 0 && !rdev
->constraints
->ramp_disable
&& old_selector
>= 0
2666 && old_selector
!= selector
) {
2668 delay
= rdev
->desc
->ops
->set_voltage_time_sel(rdev
,
2669 old_selector
, selector
);
2671 rdev_warn(rdev
, "set_voltage_time_sel() failed: %d\n",
2676 /* Insert any necessary delays */
2677 if (delay
>= 1000) {
2678 mdelay(delay
/ 1000);
2679 udelay(delay
% 1000);
2685 if (ret
== 0 && best_val
>= 0) {
2686 unsigned long data
= best_val
;
2688 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
2692 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), best_val
);
2698 * regulator_set_voltage - set regulator output voltage
2699 * @regulator: regulator source
2700 * @min_uV: Minimum required voltage in uV
2701 * @max_uV: Maximum acceptable voltage in uV
2703 * Sets a voltage regulator to the desired output voltage. This can be set
2704 * during any regulator state. IOW, regulator can be disabled or enabled.
2706 * If the regulator is enabled then the voltage will change to the new value
2707 * immediately otherwise if the regulator is disabled the regulator will
2708 * output at the new voltage when enabled.
2710 * NOTE: If the regulator is shared between several devices then the lowest
2711 * request voltage that meets the system constraints will be used.
2712 * Regulator system constraints must be set for this regulator before
2713 * calling this function otherwise this call will fail.
2715 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
2717 struct regulator_dev
*rdev
= regulator
->rdev
;
2719 int old_min_uV
, old_max_uV
;
2722 mutex_lock(&rdev
->mutex
);
2724 /* If we're setting the same range as last time the change
2725 * should be a noop (some cpufreq implementations use the same
2726 * voltage for multiple frequencies, for example).
2728 if (regulator
->min_uV
== min_uV
&& regulator
->max_uV
== max_uV
)
2731 /* If we're trying to set a range that overlaps the current voltage,
2732 * return succesfully even though the regulator does not support
2733 * changing the voltage.
2735 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2736 current_uV
= _regulator_get_voltage(rdev
);
2737 if (min_uV
<= current_uV
&& current_uV
<= max_uV
) {
2738 regulator
->min_uV
= min_uV
;
2739 regulator
->max_uV
= max_uV
;
2745 if (!rdev
->desc
->ops
->set_voltage
&&
2746 !rdev
->desc
->ops
->set_voltage_sel
) {
2751 /* constraints check */
2752 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
2756 /* restore original values in case of error */
2757 old_min_uV
= regulator
->min_uV
;
2758 old_max_uV
= regulator
->max_uV
;
2759 regulator
->min_uV
= min_uV
;
2760 regulator
->max_uV
= max_uV
;
2762 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
2766 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
2771 mutex_unlock(&rdev
->mutex
);
2774 regulator
->min_uV
= old_min_uV
;
2775 regulator
->max_uV
= old_max_uV
;
2776 mutex_unlock(&rdev
->mutex
);
2779 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
2782 * regulator_set_voltage_time - get raise/fall time
2783 * @regulator: regulator source
2784 * @old_uV: starting voltage in microvolts
2785 * @new_uV: target voltage in microvolts
2787 * Provided with the starting and ending voltage, this function attempts to
2788 * calculate the time in microseconds required to rise or fall to this new
2791 int regulator_set_voltage_time(struct regulator
*regulator
,
2792 int old_uV
, int new_uV
)
2794 struct regulator_dev
*rdev
= regulator
->rdev
;
2795 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2801 /* Currently requires operations to do this */
2802 if (!ops
->list_voltage
|| !ops
->set_voltage_time_sel
2803 || !rdev
->desc
->n_voltages
)
2806 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
2807 /* We only look for exact voltage matches here */
2808 voltage
= regulator_list_voltage(regulator
, i
);
2813 if (voltage
== old_uV
)
2815 if (voltage
== new_uV
)
2819 if (old_sel
< 0 || new_sel
< 0)
2822 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
2824 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
2827 * regulator_set_voltage_time_sel - get raise/fall time
2828 * @rdev: regulator source device
2829 * @old_selector: selector for starting voltage
2830 * @new_selector: selector for target voltage
2832 * Provided with the starting and target voltage selectors, this function
2833 * returns time in microseconds required to rise or fall to this new voltage
2835 * Drivers providing ramp_delay in regulation_constraints can use this as their
2836 * set_voltage_time_sel() operation.
2838 int regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
2839 unsigned int old_selector
,
2840 unsigned int new_selector
)
2842 unsigned int ramp_delay
= 0;
2843 int old_volt
, new_volt
;
2845 if (rdev
->constraints
->ramp_delay
)
2846 ramp_delay
= rdev
->constraints
->ramp_delay
;
2847 else if (rdev
->desc
->ramp_delay
)
2848 ramp_delay
= rdev
->desc
->ramp_delay
;
2850 if (ramp_delay
== 0) {
2851 rdev_warn(rdev
, "ramp_delay not set\n");
2856 if (!rdev
->desc
->ops
->list_voltage
)
2859 old_volt
= rdev
->desc
->ops
->list_voltage(rdev
, old_selector
);
2860 new_volt
= rdev
->desc
->ops
->list_voltage(rdev
, new_selector
);
2862 return DIV_ROUND_UP(abs(new_volt
- old_volt
), ramp_delay
);
2864 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel
);
2867 * regulator_sync_voltage - re-apply last regulator output voltage
2868 * @regulator: regulator source
2870 * Re-apply the last configured voltage. This is intended to be used
2871 * where some external control source the consumer is cooperating with
2872 * has caused the configured voltage to change.
2874 int regulator_sync_voltage(struct regulator
*regulator
)
2876 struct regulator_dev
*rdev
= regulator
->rdev
;
2877 int ret
, min_uV
, max_uV
;
2879 mutex_lock(&rdev
->mutex
);
2881 if (!rdev
->desc
->ops
->set_voltage
&&
2882 !rdev
->desc
->ops
->set_voltage_sel
) {
2887 /* This is only going to work if we've had a voltage configured. */
2888 if (!regulator
->min_uV
&& !regulator
->max_uV
) {
2893 min_uV
= regulator
->min_uV
;
2894 max_uV
= regulator
->max_uV
;
2896 /* This should be a paranoia check... */
2897 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
2901 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
2905 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
2908 mutex_unlock(&rdev
->mutex
);
2911 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
2913 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
2917 if (rdev
->desc
->ops
->get_voltage_sel
) {
2918 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
2921 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
2922 } else if (rdev
->desc
->ops
->get_voltage
) {
2923 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
2924 } else if (rdev
->desc
->ops
->list_voltage
) {
2925 ret
= rdev
->desc
->ops
->list_voltage(rdev
, 0);
2926 } else if (rdev
->desc
->fixed_uV
&& (rdev
->desc
->n_voltages
== 1)) {
2927 ret
= rdev
->desc
->fixed_uV
;
2928 } else if (rdev
->supply
) {
2929 ret
= regulator_get_voltage(rdev
->supply
);
2936 return ret
- rdev
->constraints
->uV_offset
;
2940 * regulator_get_voltage - get regulator output voltage
2941 * @regulator: regulator source
2943 * This returns the current regulator voltage in uV.
2945 * NOTE: If the regulator is disabled it will return the voltage value. This
2946 * function should not be used to determine regulator state.
2948 int regulator_get_voltage(struct regulator
*regulator
)
2952 mutex_lock(®ulator
->rdev
->mutex
);
2954 ret
= _regulator_get_voltage(regulator
->rdev
);
2956 mutex_unlock(®ulator
->rdev
->mutex
);
2960 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
2963 * regulator_set_current_limit - set regulator output current limit
2964 * @regulator: regulator source
2965 * @min_uA: Minimum supported current in uA
2966 * @max_uA: Maximum supported current in uA
2968 * Sets current sink to the desired output current. This can be set during
2969 * any regulator state. IOW, regulator can be disabled or enabled.
2971 * If the regulator is enabled then the current will change to the new value
2972 * immediately otherwise if the regulator is disabled the regulator will
2973 * output at the new current when enabled.
2975 * NOTE: Regulator system constraints must be set for this regulator before
2976 * calling this function otherwise this call will fail.
2978 int regulator_set_current_limit(struct regulator
*regulator
,
2979 int min_uA
, int max_uA
)
2981 struct regulator_dev
*rdev
= regulator
->rdev
;
2984 mutex_lock(&rdev
->mutex
);
2987 if (!rdev
->desc
->ops
->set_current_limit
) {
2992 /* constraints check */
2993 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
2997 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
2999 mutex_unlock(&rdev
->mutex
);
3002 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
3004 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
3008 mutex_lock(&rdev
->mutex
);
3011 if (!rdev
->desc
->ops
->get_current_limit
) {
3016 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
3018 mutex_unlock(&rdev
->mutex
);
3023 * regulator_get_current_limit - get regulator output current
3024 * @regulator: regulator source
3026 * This returns the current supplied by the specified current sink in uA.
3028 * NOTE: If the regulator is disabled it will return the current value. This
3029 * function should not be used to determine regulator state.
3031 int regulator_get_current_limit(struct regulator
*regulator
)
3033 return _regulator_get_current_limit(regulator
->rdev
);
3035 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
3038 * regulator_set_mode - set regulator operating mode
3039 * @regulator: regulator source
3040 * @mode: operating mode - one of the REGULATOR_MODE constants
3042 * Set regulator operating mode to increase regulator efficiency or improve
3043 * regulation performance.
3045 * NOTE: Regulator system constraints must be set for this regulator before
3046 * calling this function otherwise this call will fail.
3048 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
3050 struct regulator_dev
*rdev
= regulator
->rdev
;
3052 int regulator_curr_mode
;
3054 mutex_lock(&rdev
->mutex
);
3057 if (!rdev
->desc
->ops
->set_mode
) {
3062 /* return if the same mode is requested */
3063 if (rdev
->desc
->ops
->get_mode
) {
3064 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
3065 if (regulator_curr_mode
== mode
) {
3071 /* constraints check */
3072 ret
= regulator_mode_constrain(rdev
, &mode
);
3076 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
3078 mutex_unlock(&rdev
->mutex
);
3081 EXPORT_SYMBOL_GPL(regulator_set_mode
);
3083 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
3087 mutex_lock(&rdev
->mutex
);
3090 if (!rdev
->desc
->ops
->get_mode
) {
3095 ret
= rdev
->desc
->ops
->get_mode(rdev
);
3097 mutex_unlock(&rdev
->mutex
);
3102 * regulator_get_mode - get regulator operating mode
3103 * @regulator: regulator source
3105 * Get the current regulator operating mode.
3107 unsigned int regulator_get_mode(struct regulator
*regulator
)
3109 return _regulator_get_mode(regulator
->rdev
);
3111 EXPORT_SYMBOL_GPL(regulator_get_mode
);
3114 * regulator_set_load - set regulator load
3115 * @regulator: regulator source
3116 * @uA_load: load current
3118 * Notifies the regulator core of a new device load. This is then used by
3119 * DRMS (if enabled by constraints) to set the most efficient regulator
3120 * operating mode for the new regulator loading.
3122 * Consumer devices notify their supply regulator of the maximum power
3123 * they will require (can be taken from device datasheet in the power
3124 * consumption tables) when they change operational status and hence power
3125 * state. Examples of operational state changes that can affect power
3126 * consumption are :-
3128 * o Device is opened / closed.
3129 * o Device I/O is about to begin or has just finished.
3130 * o Device is idling in between work.
3132 * This information is also exported via sysfs to userspace.
3134 * DRMS will sum the total requested load on the regulator and change
3135 * to the most efficient operating mode if platform constraints allow.
3137 * On error a negative errno is returned.
3139 int regulator_set_load(struct regulator
*regulator
, int uA_load
)
3141 struct regulator_dev
*rdev
= regulator
->rdev
;
3144 mutex_lock(&rdev
->mutex
);
3145 regulator
->uA_load
= uA_load
;
3146 ret
= drms_uA_update(rdev
);
3147 mutex_unlock(&rdev
->mutex
);
3151 EXPORT_SYMBOL_GPL(regulator_set_load
);
3154 * regulator_allow_bypass - allow the regulator to go into bypass mode
3156 * @regulator: Regulator to configure
3157 * @enable: enable or disable bypass mode
3159 * Allow the regulator to go into bypass mode if all other consumers
3160 * for the regulator also enable bypass mode and the machine
3161 * constraints allow this. Bypass mode means that the regulator is
3162 * simply passing the input directly to the output with no regulation.
3164 int regulator_allow_bypass(struct regulator
*regulator
, bool enable
)
3166 struct regulator_dev
*rdev
= regulator
->rdev
;
3169 if (!rdev
->desc
->ops
->set_bypass
)
3172 if (rdev
->constraints
&&
3173 !(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_BYPASS
))
3176 mutex_lock(&rdev
->mutex
);
3178 if (enable
&& !regulator
->bypass
) {
3179 rdev
->bypass_count
++;
3181 if (rdev
->bypass_count
== rdev
->open_count
) {
3182 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3184 rdev
->bypass_count
--;
3187 } else if (!enable
&& regulator
->bypass
) {
3188 rdev
->bypass_count
--;
3190 if (rdev
->bypass_count
!= rdev
->open_count
) {
3191 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3193 rdev
->bypass_count
++;
3198 regulator
->bypass
= enable
;
3200 mutex_unlock(&rdev
->mutex
);
3204 EXPORT_SYMBOL_GPL(regulator_allow_bypass
);
3207 * regulator_register_notifier - register regulator event notifier
3208 * @regulator: regulator source
3209 * @nb: notifier block
3211 * Register notifier block to receive regulator events.
3213 int regulator_register_notifier(struct regulator
*regulator
,
3214 struct notifier_block
*nb
)
3216 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
3219 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
3222 * regulator_unregister_notifier - unregister regulator event notifier
3223 * @regulator: regulator source
3224 * @nb: notifier block
3226 * Unregister regulator event notifier block.
3228 int regulator_unregister_notifier(struct regulator
*regulator
,
3229 struct notifier_block
*nb
)
3231 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
3234 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
3236 /* notify regulator consumers and downstream regulator consumers.
3237 * Note mutex must be held by caller.
3239 static int _notifier_call_chain(struct regulator_dev
*rdev
,
3240 unsigned long event
, void *data
)
3242 /* call rdev chain first */
3243 return blocking_notifier_call_chain(&rdev
->notifier
, event
, data
);
3247 * regulator_bulk_get - get multiple regulator consumers
3249 * @dev: Device to supply
3250 * @num_consumers: Number of consumers to register
3251 * @consumers: Configuration of consumers; clients are stored here.
3253 * @return 0 on success, an errno on failure.
3255 * This helper function allows drivers to get several regulator
3256 * consumers in one operation. If any of the regulators cannot be
3257 * acquired then any regulators that were allocated will be freed
3258 * before returning to the caller.
3260 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
3261 struct regulator_bulk_data
*consumers
)
3266 for (i
= 0; i
< num_consumers
; i
++)
3267 consumers
[i
].consumer
= NULL
;
3269 for (i
= 0; i
< num_consumers
; i
++) {
3270 consumers
[i
].consumer
= regulator_get(dev
,
3271 consumers
[i
].supply
);
3272 if (IS_ERR(consumers
[i
].consumer
)) {
3273 ret
= PTR_ERR(consumers
[i
].consumer
);
3274 dev_err(dev
, "Failed to get supply '%s': %d\n",
3275 consumers
[i
].supply
, ret
);
3276 consumers
[i
].consumer
= NULL
;
3285 regulator_put(consumers
[i
].consumer
);
3289 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
3291 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
3293 struct regulator_bulk_data
*bulk
= data
;
3295 bulk
->ret
= regulator_enable(bulk
->consumer
);
3299 * regulator_bulk_enable - enable multiple regulator consumers
3301 * @num_consumers: Number of consumers
3302 * @consumers: Consumer data; clients are stored here.
3303 * @return 0 on success, an errno on failure
3305 * This convenience API allows consumers to enable multiple regulator
3306 * clients in a single API call. If any consumers cannot be enabled
3307 * then any others that were enabled will be disabled again prior to
3310 int regulator_bulk_enable(int num_consumers
,
3311 struct regulator_bulk_data
*consumers
)
3313 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
3317 for (i
= 0; i
< num_consumers
; i
++) {
3318 if (consumers
[i
].consumer
->always_on
)
3319 consumers
[i
].ret
= 0;
3321 async_schedule_domain(regulator_bulk_enable_async
,
3322 &consumers
[i
], &async_domain
);
3325 async_synchronize_full_domain(&async_domain
);
3327 /* If any consumer failed we need to unwind any that succeeded */
3328 for (i
= 0; i
< num_consumers
; i
++) {
3329 if (consumers
[i
].ret
!= 0) {
3330 ret
= consumers
[i
].ret
;
3338 for (i
= 0; i
< num_consumers
; i
++) {
3339 if (consumers
[i
].ret
< 0)
3340 pr_err("Failed to enable %s: %d\n", consumers
[i
].supply
,
3343 regulator_disable(consumers
[i
].consumer
);
3348 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
3351 * regulator_bulk_disable - disable multiple regulator consumers
3353 * @num_consumers: Number of consumers
3354 * @consumers: Consumer data; clients are stored here.
3355 * @return 0 on success, an errno on failure
3357 * This convenience API allows consumers to disable multiple regulator
3358 * clients in a single API call. If any consumers cannot be disabled
3359 * then any others that were disabled will be enabled again prior to
3362 int regulator_bulk_disable(int num_consumers
,
3363 struct regulator_bulk_data
*consumers
)
3368 for (i
= num_consumers
- 1; i
>= 0; --i
) {
3369 ret
= regulator_disable(consumers
[i
].consumer
);
3377 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
3378 for (++i
; i
< num_consumers
; ++i
) {
3379 r
= regulator_enable(consumers
[i
].consumer
);
3381 pr_err("Failed to reename %s: %d\n",
3382 consumers
[i
].supply
, r
);
3387 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
3390 * regulator_bulk_force_disable - force disable multiple regulator consumers
3392 * @num_consumers: Number of consumers
3393 * @consumers: Consumer data; clients are stored here.
3394 * @return 0 on success, an errno on failure
3396 * This convenience API allows consumers to forcibly disable multiple regulator
3397 * clients in a single API call.
3398 * NOTE: This should be used for situations when device damage will
3399 * likely occur if the regulators are not disabled (e.g. over temp).
3400 * Although regulator_force_disable function call for some consumers can
3401 * return error numbers, the function is called for all consumers.
3403 int regulator_bulk_force_disable(int num_consumers
,
3404 struct regulator_bulk_data
*consumers
)
3409 for (i
= 0; i
< num_consumers
; i
++)
3411 regulator_force_disable(consumers
[i
].consumer
);
3413 for (i
= 0; i
< num_consumers
; i
++) {
3414 if (consumers
[i
].ret
!= 0) {
3415 ret
= consumers
[i
].ret
;
3424 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
3427 * regulator_bulk_free - free multiple regulator consumers
3429 * @num_consumers: Number of consumers
3430 * @consumers: Consumer data; clients are stored here.
3432 * This convenience API allows consumers to free multiple regulator
3433 * clients in a single API call.
3435 void regulator_bulk_free(int num_consumers
,
3436 struct regulator_bulk_data
*consumers
)
3440 for (i
= 0; i
< num_consumers
; i
++) {
3441 regulator_put(consumers
[i
].consumer
);
3442 consumers
[i
].consumer
= NULL
;
3445 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
3448 * regulator_notifier_call_chain - call regulator event notifier
3449 * @rdev: regulator source
3450 * @event: notifier block
3451 * @data: callback-specific data.
3453 * Called by regulator drivers to notify clients a regulator event has
3454 * occurred. We also notify regulator clients downstream.
3455 * Note lock must be held by caller.
3457 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
3458 unsigned long event
, void *data
)
3460 lockdep_assert_held_once(&rdev
->mutex
);
3462 _notifier_call_chain(rdev
, event
, data
);
3466 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
3469 * regulator_mode_to_status - convert a regulator mode into a status
3471 * @mode: Mode to convert
3473 * Convert a regulator mode into a status.
3475 int regulator_mode_to_status(unsigned int mode
)
3478 case REGULATOR_MODE_FAST
:
3479 return REGULATOR_STATUS_FAST
;
3480 case REGULATOR_MODE_NORMAL
:
3481 return REGULATOR_STATUS_NORMAL
;
3482 case REGULATOR_MODE_IDLE
:
3483 return REGULATOR_STATUS_IDLE
;
3484 case REGULATOR_MODE_STANDBY
:
3485 return REGULATOR_STATUS_STANDBY
;
3487 return REGULATOR_STATUS_UNDEFINED
;
3490 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
3492 static struct attribute
*regulator_dev_attrs
[] = {
3493 &dev_attr_name
.attr
,
3494 &dev_attr_num_users
.attr
,
3495 &dev_attr_type
.attr
,
3496 &dev_attr_microvolts
.attr
,
3497 &dev_attr_microamps
.attr
,
3498 &dev_attr_opmode
.attr
,
3499 &dev_attr_state
.attr
,
3500 &dev_attr_status
.attr
,
3501 &dev_attr_bypass
.attr
,
3502 &dev_attr_requested_microamps
.attr
,
3503 &dev_attr_min_microvolts
.attr
,
3504 &dev_attr_max_microvolts
.attr
,
3505 &dev_attr_min_microamps
.attr
,
3506 &dev_attr_max_microamps
.attr
,
3507 &dev_attr_suspend_standby_state
.attr
,
3508 &dev_attr_suspend_mem_state
.attr
,
3509 &dev_attr_suspend_disk_state
.attr
,
3510 &dev_attr_suspend_standby_microvolts
.attr
,
3511 &dev_attr_suspend_mem_microvolts
.attr
,
3512 &dev_attr_suspend_disk_microvolts
.attr
,
3513 &dev_attr_suspend_standby_mode
.attr
,
3514 &dev_attr_suspend_mem_mode
.attr
,
3515 &dev_attr_suspend_disk_mode
.attr
,
3520 * To avoid cluttering sysfs (and memory) with useless state, only
3521 * create attributes that can be meaningfully displayed.
3523 static umode_t
regulator_attr_is_visible(struct kobject
*kobj
,
3524 struct attribute
*attr
, int idx
)
3526 struct device
*dev
= kobj_to_dev(kobj
);
3527 struct regulator_dev
*rdev
= container_of(dev
, struct regulator_dev
, dev
);
3528 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3529 umode_t mode
= attr
->mode
;
3531 /* these three are always present */
3532 if (attr
== &dev_attr_name
.attr
||
3533 attr
== &dev_attr_num_users
.attr
||
3534 attr
== &dev_attr_type
.attr
)
3537 /* some attributes need specific methods to be displayed */
3538 if (attr
== &dev_attr_microvolts
.attr
) {
3539 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
3540 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0) ||
3541 (ops
->list_voltage
&& ops
->list_voltage(rdev
, 0) >= 0) ||
3542 (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1))
3547 if (attr
== &dev_attr_microamps
.attr
)
3548 return ops
->get_current_limit
? mode
: 0;
3550 if (attr
== &dev_attr_opmode
.attr
)
3551 return ops
->get_mode
? mode
: 0;
3553 if (attr
== &dev_attr_state
.attr
)
3554 return (rdev
->ena_pin
|| ops
->is_enabled
) ? mode
: 0;
3556 if (attr
== &dev_attr_status
.attr
)
3557 return ops
->get_status
? mode
: 0;
3559 if (attr
== &dev_attr_bypass
.attr
)
3560 return ops
->get_bypass
? mode
: 0;
3562 /* some attributes are type-specific */
3563 if (attr
== &dev_attr_requested_microamps
.attr
)
3564 return rdev
->desc
->type
== REGULATOR_CURRENT
? mode
: 0;
3566 /* constraints need specific supporting methods */
3567 if (attr
== &dev_attr_min_microvolts
.attr
||
3568 attr
== &dev_attr_max_microvolts
.attr
)
3569 return (ops
->set_voltage
|| ops
->set_voltage_sel
) ? mode
: 0;
3571 if (attr
== &dev_attr_min_microamps
.attr
||
3572 attr
== &dev_attr_max_microamps
.attr
)
3573 return ops
->set_current_limit
? mode
: 0;
3575 if (attr
== &dev_attr_suspend_standby_state
.attr
||
3576 attr
== &dev_attr_suspend_mem_state
.attr
||
3577 attr
== &dev_attr_suspend_disk_state
.attr
)
3580 if (attr
== &dev_attr_suspend_standby_microvolts
.attr
||
3581 attr
== &dev_attr_suspend_mem_microvolts
.attr
||
3582 attr
== &dev_attr_suspend_disk_microvolts
.attr
)
3583 return ops
->set_suspend_voltage
? mode
: 0;
3585 if (attr
== &dev_attr_suspend_standby_mode
.attr
||
3586 attr
== &dev_attr_suspend_mem_mode
.attr
||
3587 attr
== &dev_attr_suspend_disk_mode
.attr
)
3588 return ops
->set_suspend_mode
? mode
: 0;
3593 static const struct attribute_group regulator_dev_group
= {
3594 .attrs
= regulator_dev_attrs
,
3595 .is_visible
= regulator_attr_is_visible
,
3598 static const struct attribute_group
*regulator_dev_groups
[] = {
3599 ®ulator_dev_group
,
3603 static void regulator_dev_release(struct device
*dev
)
3605 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
3607 kfree(rdev
->constraints
);
3608 of_node_put(rdev
->dev
.of_node
);
3612 static struct class regulator_class
= {
3613 .name
= "regulator",
3614 .dev_release
= regulator_dev_release
,
3615 .dev_groups
= regulator_dev_groups
,
3618 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
3620 struct device
*parent
= rdev
->dev
.parent
;
3621 const char *rname
= rdev_get_name(rdev
);
3622 char name
[NAME_MAX
];
3624 /* Avoid duplicate debugfs directory names */
3625 if (parent
&& rname
== rdev
->desc
->name
) {
3626 snprintf(name
, sizeof(name
), "%s-%s", dev_name(parent
),
3631 rdev
->debugfs
= debugfs_create_dir(rname
, debugfs_root
);
3632 if (!rdev
->debugfs
) {
3633 rdev_warn(rdev
, "Failed to create debugfs directory\n");
3637 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
3639 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
3641 debugfs_create_u32("bypass_count", 0444, rdev
->debugfs
,
3642 &rdev
->bypass_count
);
3646 * regulator_register - register regulator
3647 * @regulator_desc: regulator to register
3648 * @cfg: runtime configuration for regulator
3650 * Called by regulator drivers to register a regulator.
3651 * Returns a valid pointer to struct regulator_dev on success
3652 * or an ERR_PTR() on error.
3654 struct regulator_dev
*
3655 regulator_register(const struct regulator_desc
*regulator_desc
,
3656 const struct regulator_config
*cfg
)
3658 const struct regulation_constraints
*constraints
= NULL
;
3659 const struct regulator_init_data
*init_data
;
3660 struct regulator_config
*config
= NULL
;
3661 static atomic_t regulator_no
= ATOMIC_INIT(-1);
3662 struct regulator_dev
*rdev
;
3666 if (regulator_desc
== NULL
|| cfg
== NULL
)
3667 return ERR_PTR(-EINVAL
);
3672 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
3673 return ERR_PTR(-EINVAL
);
3675 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
3676 regulator_desc
->type
!= REGULATOR_CURRENT
)
3677 return ERR_PTR(-EINVAL
);
3679 /* Only one of each should be implemented */
3680 WARN_ON(regulator_desc
->ops
->get_voltage
&&
3681 regulator_desc
->ops
->get_voltage_sel
);
3682 WARN_ON(regulator_desc
->ops
->set_voltage
&&
3683 regulator_desc
->ops
->set_voltage_sel
);
3685 /* If we're using selectors we must implement list_voltage. */
3686 if (regulator_desc
->ops
->get_voltage_sel
&&
3687 !regulator_desc
->ops
->list_voltage
) {
3688 return ERR_PTR(-EINVAL
);
3690 if (regulator_desc
->ops
->set_voltage_sel
&&
3691 !regulator_desc
->ops
->list_voltage
) {
3692 return ERR_PTR(-EINVAL
);
3695 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
3697 return ERR_PTR(-ENOMEM
);
3700 * Duplicate the config so the driver could override it after
3701 * parsing init data.
3703 config
= kmemdup(cfg
, sizeof(*cfg
), GFP_KERNEL
);
3704 if (config
== NULL
) {
3706 return ERR_PTR(-ENOMEM
);
3709 init_data
= regulator_of_get_init_data(dev
, regulator_desc
, config
,
3710 &rdev
->dev
.of_node
);
3712 init_data
= config
->init_data
;
3713 rdev
->dev
.of_node
= of_node_get(config
->of_node
);
3716 mutex_lock(®ulator_list_mutex
);
3718 mutex_init(&rdev
->mutex
);
3719 rdev
->reg_data
= config
->driver_data
;
3720 rdev
->owner
= regulator_desc
->owner
;
3721 rdev
->desc
= regulator_desc
;
3723 rdev
->regmap
= config
->regmap
;
3724 else if (dev_get_regmap(dev
, NULL
))
3725 rdev
->regmap
= dev_get_regmap(dev
, NULL
);
3726 else if (dev
->parent
)
3727 rdev
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
3728 INIT_LIST_HEAD(&rdev
->consumer_list
);
3729 INIT_LIST_HEAD(&rdev
->list
);
3730 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
3731 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
3733 /* preform any regulator specific init */
3734 if (init_data
&& init_data
->regulator_init
) {
3735 ret
= init_data
->regulator_init(rdev
->reg_data
);
3740 /* register with sysfs */
3741 rdev
->dev
.class = ®ulator_class
;
3742 rdev
->dev
.parent
= dev
;
3743 dev_set_name(&rdev
->dev
, "regulator.%lu",
3744 (unsigned long) atomic_inc_return(®ulator_no
));
3745 ret
= device_register(&rdev
->dev
);
3747 put_device(&rdev
->dev
);
3751 dev_set_drvdata(&rdev
->dev
, rdev
);
3753 if ((config
->ena_gpio
|| config
->ena_gpio_initialized
) &&
3754 gpio_is_valid(config
->ena_gpio
)) {
3755 ret
= regulator_ena_gpio_request(rdev
, config
);
3757 rdev_err(rdev
, "Failed to request enable GPIO%d: %d\n",
3758 config
->ena_gpio
, ret
);
3763 /* set regulator constraints */
3765 constraints
= &init_data
->constraints
;
3767 ret
= set_machine_constraints(rdev
, constraints
);
3771 if (init_data
&& init_data
->supply_regulator
)
3772 rdev
->supply_name
= init_data
->supply_regulator
;
3773 else if (regulator_desc
->supply_name
)
3774 rdev
->supply_name
= regulator_desc
->supply_name
;
3776 /* add consumers devices */
3778 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
3779 ret
= set_consumer_device_supply(rdev
,
3780 init_data
->consumer_supplies
[i
].dev_name
,
3781 init_data
->consumer_supplies
[i
].supply
);
3783 dev_err(dev
, "Failed to set supply %s\n",
3784 init_data
->consumer_supplies
[i
].supply
);
3785 goto unset_supplies
;
3790 list_add(&rdev
->list
, ®ulator_list
);
3792 rdev_init_debugfs(rdev
);
3794 mutex_unlock(®ulator_list_mutex
);
3799 unset_regulator_supplies(rdev
);
3802 regulator_ena_gpio_free(rdev
);
3803 kfree(rdev
->constraints
);
3805 device_unregister(&rdev
->dev
);
3806 /* device core frees rdev */
3807 rdev
= ERR_PTR(ret
);
3812 rdev
= ERR_PTR(ret
);
3815 EXPORT_SYMBOL_GPL(regulator_register
);
3818 * regulator_unregister - unregister regulator
3819 * @rdev: regulator to unregister
3821 * Called by regulator drivers to unregister a regulator.
3823 void regulator_unregister(struct regulator_dev
*rdev
)
3829 while (rdev
->use_count
--)
3830 regulator_disable(rdev
->supply
);
3831 regulator_put(rdev
->supply
);
3833 mutex_lock(®ulator_list_mutex
);
3834 debugfs_remove_recursive(rdev
->debugfs
);
3835 flush_work(&rdev
->disable_work
.work
);
3836 WARN_ON(rdev
->open_count
);
3837 unset_regulator_supplies(rdev
);
3838 list_del(&rdev
->list
);
3839 mutex_unlock(®ulator_list_mutex
);
3840 regulator_ena_gpio_free(rdev
);
3841 device_unregister(&rdev
->dev
);
3843 EXPORT_SYMBOL_GPL(regulator_unregister
);
3846 * regulator_suspend_prepare - prepare regulators for system wide suspend
3847 * @state: system suspend state
3849 * Configure each regulator with it's suspend operating parameters for state.
3850 * This will usually be called by machine suspend code prior to supending.
3852 int regulator_suspend_prepare(suspend_state_t state
)
3854 struct regulator_dev
*rdev
;
3857 /* ON is handled by regulator active state */
3858 if (state
== PM_SUSPEND_ON
)
3861 mutex_lock(®ulator_list_mutex
);
3862 list_for_each_entry(rdev
, ®ulator_list
, list
) {
3864 mutex_lock(&rdev
->mutex
);
3865 ret
= suspend_prepare(rdev
, state
);
3866 mutex_unlock(&rdev
->mutex
);
3869 rdev_err(rdev
, "failed to prepare\n");
3874 mutex_unlock(®ulator_list_mutex
);
3877 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
3880 * regulator_suspend_finish - resume regulators from system wide suspend
3882 * Turn on regulators that might be turned off by regulator_suspend_prepare
3883 * and that should be turned on according to the regulators properties.
3885 int regulator_suspend_finish(void)
3887 struct regulator_dev
*rdev
;
3890 mutex_lock(®ulator_list_mutex
);
3891 list_for_each_entry(rdev
, ®ulator_list
, list
) {
3892 mutex_lock(&rdev
->mutex
);
3893 if (rdev
->use_count
> 0 || rdev
->constraints
->always_on
) {
3894 if (!_regulator_is_enabled(rdev
)) {
3895 error
= _regulator_do_enable(rdev
);
3900 if (!have_full_constraints())
3902 if (!_regulator_is_enabled(rdev
))
3905 error
= _regulator_do_disable(rdev
);
3910 mutex_unlock(&rdev
->mutex
);
3912 mutex_unlock(®ulator_list_mutex
);
3915 EXPORT_SYMBOL_GPL(regulator_suspend_finish
);
3918 * regulator_has_full_constraints - the system has fully specified constraints
3920 * Calling this function will cause the regulator API to disable all
3921 * regulators which have a zero use count and don't have an always_on
3922 * constraint in a late_initcall.
3924 * The intention is that this will become the default behaviour in a
3925 * future kernel release so users are encouraged to use this facility
3928 void regulator_has_full_constraints(void)
3930 has_full_constraints
= 1;
3932 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
3935 * rdev_get_drvdata - get rdev regulator driver data
3938 * Get rdev regulator driver private data. This call can be used in the
3939 * regulator driver context.
3941 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
3943 return rdev
->reg_data
;
3945 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
3948 * regulator_get_drvdata - get regulator driver data
3949 * @regulator: regulator
3951 * Get regulator driver private data. This call can be used in the consumer
3952 * driver context when non API regulator specific functions need to be called.
3954 void *regulator_get_drvdata(struct regulator
*regulator
)
3956 return regulator
->rdev
->reg_data
;
3958 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
3961 * regulator_set_drvdata - set regulator driver data
3962 * @regulator: regulator
3965 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
3967 regulator
->rdev
->reg_data
= data
;
3969 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
3972 * regulator_get_id - get regulator ID
3975 int rdev_get_id(struct regulator_dev
*rdev
)
3977 return rdev
->desc
->id
;
3979 EXPORT_SYMBOL_GPL(rdev_get_id
);
3981 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
3985 EXPORT_SYMBOL_GPL(rdev_get_dev
);
3987 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
3989 return reg_init_data
->driver_data
;
3991 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
3993 #ifdef CONFIG_DEBUG_FS
3994 static ssize_t
supply_map_read_file(struct file
*file
, char __user
*user_buf
,
3995 size_t count
, loff_t
*ppos
)
3997 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3998 ssize_t len
, ret
= 0;
3999 struct regulator_map
*map
;
4004 list_for_each_entry(map
, ®ulator_map_list
, list
) {
4005 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
4007 rdev_get_name(map
->regulator
), map
->dev_name
,
4011 if (ret
> PAGE_SIZE
) {
4017 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
4025 static const struct file_operations supply_map_fops
= {
4026 #ifdef CONFIG_DEBUG_FS
4027 .read
= supply_map_read_file
,
4028 .llseek
= default_llseek
,
4032 #ifdef CONFIG_DEBUG_FS
4033 static void regulator_summary_show_subtree(struct seq_file
*s
,
4034 struct regulator_dev
*rdev
,
4037 struct list_head
*list
= s
->private;
4038 struct regulator_dev
*child
;
4039 struct regulation_constraints
*c
;
4040 struct regulator
*consumer
;
4045 seq_printf(s
, "%*s%-*s %3d %4d %6d ",
4047 30 - level
* 3, rdev_get_name(rdev
),
4048 rdev
->use_count
, rdev
->open_count
, rdev
->bypass_count
);
4050 seq_printf(s
, "%5dmV ", _regulator_get_voltage(rdev
) / 1000);
4051 seq_printf(s
, "%5dmA ", _regulator_get_current_limit(rdev
) / 1000);
4053 c
= rdev
->constraints
;
4055 switch (rdev
->desc
->type
) {
4056 case REGULATOR_VOLTAGE
:
4057 seq_printf(s
, "%5dmV %5dmV ",
4058 c
->min_uV
/ 1000, c
->max_uV
/ 1000);
4060 case REGULATOR_CURRENT
:
4061 seq_printf(s
, "%5dmA %5dmA ",
4062 c
->min_uA
/ 1000, c
->max_uA
/ 1000);
4069 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
) {
4070 if (consumer
->dev
->class == ®ulator_class
)
4073 seq_printf(s
, "%*s%-*s ",
4074 (level
+ 1) * 3 + 1, "",
4075 30 - (level
+ 1) * 3, dev_name(consumer
->dev
));
4077 switch (rdev
->desc
->type
) {
4078 case REGULATOR_VOLTAGE
:
4079 seq_printf(s
, "%37dmV %5dmV",
4080 consumer
->min_uV
/ 1000,
4081 consumer
->max_uV
/ 1000);
4083 case REGULATOR_CURRENT
:
4090 list_for_each_entry(child
, list
, list
) {
4091 /* handle only non-root regulators supplied by current rdev */
4092 if (!child
->supply
|| child
->supply
->rdev
!= rdev
)
4095 regulator_summary_show_subtree(s
, child
, level
+ 1);
4099 static int regulator_summary_show(struct seq_file
*s
, void *data
)
4101 struct list_head
*list
= s
->private;
4102 struct regulator_dev
*rdev
;
4104 seq_puts(s
, " regulator use open bypass voltage current min max\n");
4105 seq_puts(s
, "-------------------------------------------------------------------------------\n");
4107 mutex_lock(®ulator_list_mutex
);
4109 list_for_each_entry(rdev
, list
, list
) {
4113 regulator_summary_show_subtree(s
, rdev
, 0);
4116 mutex_unlock(®ulator_list_mutex
);
4121 static int regulator_summary_open(struct inode
*inode
, struct file
*file
)
4123 return single_open(file
, regulator_summary_show
, inode
->i_private
);
4127 static const struct file_operations regulator_summary_fops
= {
4128 #ifdef CONFIG_DEBUG_FS
4129 .open
= regulator_summary_open
,
4131 .llseek
= seq_lseek
,
4132 .release
= single_release
,
4136 static int __init
regulator_init(void)
4140 ret
= class_register(®ulator_class
);
4142 debugfs_root
= debugfs_create_dir("regulator", NULL
);
4144 pr_warn("regulator: Failed to create debugfs directory\n");
4146 debugfs_create_file("supply_map", 0444, debugfs_root
, NULL
,
4149 debugfs_create_file("regulator_summary", 0444, debugfs_root
,
4150 ®ulator_list
, ®ulator_summary_fops
);
4152 regulator_dummy_init();
4157 /* init early to allow our consumers to complete system booting */
4158 core_initcall(regulator_init
);
4160 static int __init
regulator_late_cleanup(struct device
*dev
, void *data
)
4162 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4163 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
4164 struct regulation_constraints
*c
= rdev
->constraints
;
4167 if (c
&& c
->always_on
)
4170 if (c
&& !(c
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
))
4173 mutex_lock(&rdev
->mutex
);
4175 if (rdev
->use_count
)
4178 /* If we can't read the status assume it's on. */
4179 if (ops
->is_enabled
)
4180 enabled
= ops
->is_enabled(rdev
);
4187 if (have_full_constraints()) {
4188 /* We log since this may kill the system if it goes
4190 rdev_info(rdev
, "disabling\n");
4191 ret
= _regulator_do_disable(rdev
);
4193 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
4195 /* The intention is that in future we will
4196 * assume that full constraints are provided
4197 * so warn even if we aren't going to do
4200 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
4204 mutex_unlock(&rdev
->mutex
);
4209 static int __init
regulator_init_complete(void)
4212 * Since DT doesn't provide an idiomatic mechanism for
4213 * enabling full constraints and since it's much more natural
4214 * with DT to provide them just assume that a DT enabled
4215 * system has full constraints.
4217 if (of_have_populated_dt())
4218 has_full_constraints
= true;
4220 /* If we have a full configuration then disable any regulators
4221 * we have permission to change the status for and which are
4222 * not in use or always_on. This is effectively the default
4223 * for DT and ACPI as they have full constraints.
4225 class_for_each_device(®ulator_class
, NULL
, NULL
,
4226 regulator_late_cleanup
);
4230 late_initcall_sync(regulator_init_complete
);