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_map_list
);
55 static LIST_HEAD(regulator_ena_gpio_list
);
56 static LIST_HEAD(regulator_supply_alias_list
);
57 static bool has_full_constraints
;
59 static struct dentry
*debugfs_root
;
61 static struct class regulator_class
;
64 * struct regulator_map
66 * Used to provide symbolic supply names to devices.
68 struct regulator_map
{
69 struct list_head list
;
70 const char *dev_name
; /* The dev_name() for the consumer */
72 struct regulator_dev
*regulator
;
76 * struct regulator_enable_gpio
78 * Management for shared enable GPIO pin
80 struct regulator_enable_gpio
{
81 struct list_head list
;
82 struct gpio_desc
*gpiod
;
83 u32 enable_count
; /* a number of enabled shared GPIO */
84 u32 request_count
; /* a number of requested shared GPIO */
85 unsigned int ena_gpio_invert
:1;
89 * struct regulator_supply_alias
91 * Used to map lookups for a supply onto an alternative device.
93 struct regulator_supply_alias
{
94 struct list_head list
;
95 struct device
*src_dev
;
96 const char *src_supply
;
97 struct device
*alias_dev
;
98 const char *alias_supply
;
101 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
102 static int _regulator_disable(struct regulator_dev
*rdev
);
103 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
104 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
105 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
106 static int _notifier_call_chain(struct regulator_dev
*rdev
,
107 unsigned long event
, void *data
);
108 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
109 int min_uV
, int max_uV
);
110 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
112 const char *supply_name
);
113 static void _regulator_put(struct regulator
*regulator
);
115 static struct regulator_dev
*dev_to_rdev(struct device
*dev
)
117 return container_of(dev
, struct regulator_dev
, dev
);
120 static const char *rdev_get_name(struct regulator_dev
*rdev
)
122 if (rdev
->constraints
&& rdev
->constraints
->name
)
123 return rdev
->constraints
->name
;
124 else if (rdev
->desc
->name
)
125 return rdev
->desc
->name
;
130 static bool have_full_constraints(void)
132 return has_full_constraints
|| of_have_populated_dt();
135 static inline struct regulator_dev
*rdev_get_supply(struct regulator_dev
*rdev
)
137 if (rdev
&& rdev
->supply
)
138 return rdev
->supply
->rdev
;
144 * regulator_lock_supply - lock a regulator and its supplies
145 * @rdev: regulator source
147 static void regulator_lock_supply(struct regulator_dev
*rdev
)
151 for (i
= 0; rdev
; rdev
= rdev_get_supply(rdev
), i
++)
152 mutex_lock_nested(&rdev
->mutex
, i
);
156 * regulator_unlock_supply - unlock a regulator and its supplies
157 * @rdev: regulator source
159 static void regulator_unlock_supply(struct regulator_dev
*rdev
)
161 struct regulator
*supply
;
164 mutex_unlock(&rdev
->mutex
);
165 supply
= rdev
->supply
;
175 * of_get_regulator - get a regulator device node based on supply name
176 * @dev: Device pointer for the consumer (of regulator) device
177 * @supply: regulator supply name
179 * Extract the regulator device node corresponding to the supply name.
180 * returns the device node corresponding to the regulator if found, else
183 static struct device_node
*of_get_regulator(struct device
*dev
, const char *supply
)
185 struct device_node
*regnode
= NULL
;
186 char prop_name
[32]; /* 32 is max size of property name */
188 dev_dbg(dev
, "Looking up %s-supply from device tree\n", supply
);
190 snprintf(prop_name
, 32, "%s-supply", supply
);
191 regnode
= of_parse_phandle(dev
->of_node
, prop_name
, 0);
194 dev_dbg(dev
, "Looking up %s property in node %s failed",
195 prop_name
, dev
->of_node
->full_name
);
201 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
203 if (!rdev
->constraints
)
206 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
212 /* Platform voltage constraint check */
213 static int regulator_check_voltage(struct regulator_dev
*rdev
,
214 int *min_uV
, int *max_uV
)
216 BUG_ON(*min_uV
> *max_uV
);
218 if (!rdev
->constraints
) {
219 rdev_err(rdev
, "no constraints\n");
222 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
223 rdev_err(rdev
, "voltage operation not allowed\n");
227 if (*max_uV
> rdev
->constraints
->max_uV
)
228 *max_uV
= rdev
->constraints
->max_uV
;
229 if (*min_uV
< rdev
->constraints
->min_uV
)
230 *min_uV
= rdev
->constraints
->min_uV
;
232 if (*min_uV
> *max_uV
) {
233 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
241 /* Make sure we select a voltage that suits the needs of all
242 * regulator consumers
244 static int regulator_check_consumers(struct regulator_dev
*rdev
,
245 int *min_uV
, int *max_uV
)
247 struct regulator
*regulator
;
249 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
251 * Assume consumers that didn't say anything are OK
252 * with anything in the constraint range.
254 if (!regulator
->min_uV
&& !regulator
->max_uV
)
257 if (*max_uV
> regulator
->max_uV
)
258 *max_uV
= regulator
->max_uV
;
259 if (*min_uV
< regulator
->min_uV
)
260 *min_uV
= regulator
->min_uV
;
263 if (*min_uV
> *max_uV
) {
264 rdev_err(rdev
, "Restricting voltage, %u-%uuV\n",
272 /* current constraint check */
273 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
274 int *min_uA
, int *max_uA
)
276 BUG_ON(*min_uA
> *max_uA
);
278 if (!rdev
->constraints
) {
279 rdev_err(rdev
, "no constraints\n");
282 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
283 rdev_err(rdev
, "current operation not allowed\n");
287 if (*max_uA
> rdev
->constraints
->max_uA
)
288 *max_uA
= rdev
->constraints
->max_uA
;
289 if (*min_uA
< rdev
->constraints
->min_uA
)
290 *min_uA
= rdev
->constraints
->min_uA
;
292 if (*min_uA
> *max_uA
) {
293 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
301 /* operating mode constraint check */
302 static int regulator_mode_constrain(struct regulator_dev
*rdev
, int *mode
)
305 case REGULATOR_MODE_FAST
:
306 case REGULATOR_MODE_NORMAL
:
307 case REGULATOR_MODE_IDLE
:
308 case REGULATOR_MODE_STANDBY
:
311 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
315 if (!rdev
->constraints
) {
316 rdev_err(rdev
, "no constraints\n");
319 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
320 rdev_err(rdev
, "mode operation not allowed\n");
324 /* The modes are bitmasks, the most power hungry modes having
325 * the lowest values. If the requested mode isn't supported
326 * try higher modes. */
328 if (rdev
->constraints
->valid_modes_mask
& *mode
)
336 /* dynamic regulator mode switching constraint check */
337 static int regulator_check_drms(struct regulator_dev
*rdev
)
339 if (!rdev
->constraints
) {
340 rdev_err(rdev
, "no constraints\n");
343 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
344 rdev_dbg(rdev
, "drms operation not allowed\n");
350 static ssize_t
regulator_uV_show(struct device
*dev
,
351 struct device_attribute
*attr
, char *buf
)
353 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
356 mutex_lock(&rdev
->mutex
);
357 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
358 mutex_unlock(&rdev
->mutex
);
362 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
364 static ssize_t
regulator_uA_show(struct device
*dev
,
365 struct device_attribute
*attr
, char *buf
)
367 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
369 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
371 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
373 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
376 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
378 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
380 static DEVICE_ATTR_RO(name
);
382 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
385 case REGULATOR_MODE_FAST
:
386 return sprintf(buf
, "fast\n");
387 case REGULATOR_MODE_NORMAL
:
388 return sprintf(buf
, "normal\n");
389 case REGULATOR_MODE_IDLE
:
390 return sprintf(buf
, "idle\n");
391 case REGULATOR_MODE_STANDBY
:
392 return sprintf(buf
, "standby\n");
394 return sprintf(buf
, "unknown\n");
397 static ssize_t
regulator_opmode_show(struct device
*dev
,
398 struct device_attribute
*attr
, char *buf
)
400 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
402 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
404 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
406 static ssize_t
regulator_print_state(char *buf
, int state
)
409 return sprintf(buf
, "enabled\n");
411 return sprintf(buf
, "disabled\n");
413 return sprintf(buf
, "unknown\n");
416 static ssize_t
regulator_state_show(struct device
*dev
,
417 struct device_attribute
*attr
, char *buf
)
419 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
422 mutex_lock(&rdev
->mutex
);
423 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
424 mutex_unlock(&rdev
->mutex
);
428 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
430 static ssize_t
regulator_status_show(struct device
*dev
,
431 struct device_attribute
*attr
, char *buf
)
433 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
437 status
= rdev
->desc
->ops
->get_status(rdev
);
442 case REGULATOR_STATUS_OFF
:
445 case REGULATOR_STATUS_ON
:
448 case REGULATOR_STATUS_ERROR
:
451 case REGULATOR_STATUS_FAST
:
454 case REGULATOR_STATUS_NORMAL
:
457 case REGULATOR_STATUS_IDLE
:
460 case REGULATOR_STATUS_STANDBY
:
463 case REGULATOR_STATUS_BYPASS
:
466 case REGULATOR_STATUS_UNDEFINED
:
473 return sprintf(buf
, "%s\n", label
);
475 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
477 static ssize_t
regulator_min_uA_show(struct device
*dev
,
478 struct device_attribute
*attr
, char *buf
)
480 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
482 if (!rdev
->constraints
)
483 return sprintf(buf
, "constraint not defined\n");
485 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
487 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
489 static ssize_t
regulator_max_uA_show(struct device
*dev
,
490 struct device_attribute
*attr
, char *buf
)
492 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
494 if (!rdev
->constraints
)
495 return sprintf(buf
, "constraint not defined\n");
497 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
499 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
501 static ssize_t
regulator_min_uV_show(struct device
*dev
,
502 struct device_attribute
*attr
, char *buf
)
504 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
506 if (!rdev
->constraints
)
507 return sprintf(buf
, "constraint not defined\n");
509 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
511 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
513 static ssize_t
regulator_max_uV_show(struct device
*dev
,
514 struct device_attribute
*attr
, char *buf
)
516 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
518 if (!rdev
->constraints
)
519 return sprintf(buf
, "constraint not defined\n");
521 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
523 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
525 static ssize_t
regulator_total_uA_show(struct device
*dev
,
526 struct device_attribute
*attr
, char *buf
)
528 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
529 struct regulator
*regulator
;
532 mutex_lock(&rdev
->mutex
);
533 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
534 uA
+= regulator
->uA_load
;
535 mutex_unlock(&rdev
->mutex
);
536 return sprintf(buf
, "%d\n", uA
);
538 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
540 static ssize_t
num_users_show(struct device
*dev
, struct device_attribute
*attr
,
543 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
544 return sprintf(buf
, "%d\n", rdev
->use_count
);
546 static DEVICE_ATTR_RO(num_users
);
548 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
551 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
553 switch (rdev
->desc
->type
) {
554 case REGULATOR_VOLTAGE
:
555 return sprintf(buf
, "voltage\n");
556 case REGULATOR_CURRENT
:
557 return sprintf(buf
, "current\n");
559 return sprintf(buf
, "unknown\n");
561 static DEVICE_ATTR_RO(type
);
563 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
564 struct device_attribute
*attr
, char *buf
)
566 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
568 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
570 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
571 regulator_suspend_mem_uV_show
, NULL
);
573 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
574 struct device_attribute
*attr
, char *buf
)
576 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
578 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
580 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
581 regulator_suspend_disk_uV_show
, NULL
);
583 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
584 struct device_attribute
*attr
, char *buf
)
586 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
588 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
590 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
591 regulator_suspend_standby_uV_show
, NULL
);
593 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
594 struct device_attribute
*attr
, char *buf
)
596 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
598 return regulator_print_opmode(buf
,
599 rdev
->constraints
->state_mem
.mode
);
601 static DEVICE_ATTR(suspend_mem_mode
, 0444,
602 regulator_suspend_mem_mode_show
, NULL
);
604 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
605 struct device_attribute
*attr
, char *buf
)
607 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
609 return regulator_print_opmode(buf
,
610 rdev
->constraints
->state_disk
.mode
);
612 static DEVICE_ATTR(suspend_disk_mode
, 0444,
613 regulator_suspend_disk_mode_show
, NULL
);
615 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
616 struct device_attribute
*attr
, char *buf
)
618 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
620 return regulator_print_opmode(buf
,
621 rdev
->constraints
->state_standby
.mode
);
623 static DEVICE_ATTR(suspend_standby_mode
, 0444,
624 regulator_suspend_standby_mode_show
, NULL
);
626 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
627 struct device_attribute
*attr
, char *buf
)
629 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
631 return regulator_print_state(buf
,
632 rdev
->constraints
->state_mem
.enabled
);
634 static DEVICE_ATTR(suspend_mem_state
, 0444,
635 regulator_suspend_mem_state_show
, NULL
);
637 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
638 struct device_attribute
*attr
, char *buf
)
640 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
642 return regulator_print_state(buf
,
643 rdev
->constraints
->state_disk
.enabled
);
645 static DEVICE_ATTR(suspend_disk_state
, 0444,
646 regulator_suspend_disk_state_show
, NULL
);
648 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
649 struct device_attribute
*attr
, char *buf
)
651 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
653 return regulator_print_state(buf
,
654 rdev
->constraints
->state_standby
.enabled
);
656 static DEVICE_ATTR(suspend_standby_state
, 0444,
657 regulator_suspend_standby_state_show
, NULL
);
659 static ssize_t
regulator_bypass_show(struct device
*dev
,
660 struct device_attribute
*attr
, char *buf
)
662 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
667 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypass
);
676 return sprintf(buf
, "%s\n", report
);
678 static DEVICE_ATTR(bypass
, 0444,
679 regulator_bypass_show
, NULL
);
681 /* Calculate the new optimum regulator operating mode based on the new total
682 * consumer load. All locks held by caller */
683 static int drms_uA_update(struct regulator_dev
*rdev
)
685 struct regulator
*sibling
;
686 int current_uA
= 0, output_uV
, input_uV
, err
;
689 lockdep_assert_held_once(&rdev
->mutex
);
692 * first check to see if we can set modes at all, otherwise just
693 * tell the consumer everything is OK.
695 err
= regulator_check_drms(rdev
);
699 if (!rdev
->desc
->ops
->get_optimum_mode
&&
700 !rdev
->desc
->ops
->set_load
)
703 if (!rdev
->desc
->ops
->set_mode
&&
704 !rdev
->desc
->ops
->set_load
)
707 /* get output voltage */
708 output_uV
= _regulator_get_voltage(rdev
);
709 if (output_uV
<= 0) {
710 rdev_err(rdev
, "invalid output voltage found\n");
714 /* get input voltage */
717 input_uV
= regulator_get_voltage(rdev
->supply
);
719 input_uV
= rdev
->constraints
->input_uV
;
721 rdev_err(rdev
, "invalid input voltage found\n");
725 /* calc total requested load */
726 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
727 current_uA
+= sibling
->uA_load
;
729 current_uA
+= rdev
->constraints
->system_load
;
731 if (rdev
->desc
->ops
->set_load
) {
732 /* set the optimum mode for our new total regulator load */
733 err
= rdev
->desc
->ops
->set_load(rdev
, current_uA
);
735 rdev_err(rdev
, "failed to set load %d\n", current_uA
);
737 /* now get the optimum mode for our new total regulator load */
738 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
739 output_uV
, current_uA
);
741 /* check the new mode is allowed */
742 err
= regulator_mode_constrain(rdev
, &mode
);
744 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
745 current_uA
, input_uV
, output_uV
);
749 err
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
751 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
757 static int suspend_set_state(struct regulator_dev
*rdev
,
758 struct regulator_state
*rstate
)
762 /* If we have no suspend mode configration don't set anything;
763 * only warn if the driver implements set_suspend_voltage or
764 * set_suspend_mode callback.
766 if (!rstate
->enabled
&& !rstate
->disabled
) {
767 if (rdev
->desc
->ops
->set_suspend_voltage
||
768 rdev
->desc
->ops
->set_suspend_mode
)
769 rdev_warn(rdev
, "No configuration\n");
773 if (rstate
->enabled
&& rstate
->disabled
) {
774 rdev_err(rdev
, "invalid configuration\n");
778 if (rstate
->enabled
&& rdev
->desc
->ops
->set_suspend_enable
)
779 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
780 else if (rstate
->disabled
&& rdev
->desc
->ops
->set_suspend_disable
)
781 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
782 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
786 rdev_err(rdev
, "failed to enabled/disable\n");
790 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
791 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
793 rdev_err(rdev
, "failed to set voltage\n");
798 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
799 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
801 rdev_err(rdev
, "failed to set mode\n");
808 /* locks held by caller */
809 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
811 if (!rdev
->constraints
)
815 case PM_SUSPEND_STANDBY
:
816 return suspend_set_state(rdev
,
817 &rdev
->constraints
->state_standby
);
819 return suspend_set_state(rdev
,
820 &rdev
->constraints
->state_mem
);
822 return suspend_set_state(rdev
,
823 &rdev
->constraints
->state_disk
);
829 static void print_constraints(struct regulator_dev
*rdev
)
831 struct regulation_constraints
*constraints
= rdev
->constraints
;
833 size_t len
= sizeof(buf
) - 1;
837 if (constraints
->min_uV
&& constraints
->max_uV
) {
838 if (constraints
->min_uV
== constraints
->max_uV
)
839 count
+= scnprintf(buf
+ count
, len
- count
, "%d mV ",
840 constraints
->min_uV
/ 1000);
842 count
+= scnprintf(buf
+ count
, len
- count
,
844 constraints
->min_uV
/ 1000,
845 constraints
->max_uV
/ 1000);
848 if (!constraints
->min_uV
||
849 constraints
->min_uV
!= constraints
->max_uV
) {
850 ret
= _regulator_get_voltage(rdev
);
852 count
+= scnprintf(buf
+ count
, len
- count
,
853 "at %d mV ", ret
/ 1000);
856 if (constraints
->uV_offset
)
857 count
+= scnprintf(buf
+ count
, len
- count
, "%dmV offset ",
858 constraints
->uV_offset
/ 1000);
860 if (constraints
->min_uA
&& constraints
->max_uA
) {
861 if (constraints
->min_uA
== constraints
->max_uA
)
862 count
+= scnprintf(buf
+ count
, len
- count
, "%d mA ",
863 constraints
->min_uA
/ 1000);
865 count
+= scnprintf(buf
+ count
, len
- count
,
867 constraints
->min_uA
/ 1000,
868 constraints
->max_uA
/ 1000);
871 if (!constraints
->min_uA
||
872 constraints
->min_uA
!= constraints
->max_uA
) {
873 ret
= _regulator_get_current_limit(rdev
);
875 count
+= scnprintf(buf
+ count
, len
- count
,
876 "at %d mA ", ret
/ 1000);
879 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
880 count
+= scnprintf(buf
+ count
, len
- count
, "fast ");
881 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
882 count
+= scnprintf(buf
+ count
, len
- count
, "normal ");
883 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
884 count
+= scnprintf(buf
+ count
, len
- count
, "idle ");
885 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
886 count
+= scnprintf(buf
+ count
, len
- count
, "standby");
889 scnprintf(buf
, len
, "no parameters");
891 rdev_dbg(rdev
, "%s\n", buf
);
893 if ((constraints
->min_uV
!= constraints
->max_uV
) &&
894 !(constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
))
896 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
899 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
900 struct regulation_constraints
*constraints
)
902 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
905 /* do we need to apply the constraint voltage */
906 if (rdev
->constraints
->apply_uV
&&
907 rdev
->constraints
->min_uV
&& rdev
->constraints
->max_uV
) {
908 int target_min
, target_max
;
909 int current_uV
= _regulator_get_voltage(rdev
);
910 if (current_uV
< 0) {
912 "failed to get the current voltage(%d)\n",
918 * If we're below the minimum voltage move up to the
919 * minimum voltage, if we're above the maximum voltage
920 * then move down to the maximum.
922 target_min
= current_uV
;
923 target_max
= current_uV
;
925 if (current_uV
< rdev
->constraints
->min_uV
) {
926 target_min
= rdev
->constraints
->min_uV
;
927 target_max
= rdev
->constraints
->min_uV
;
930 if (current_uV
> rdev
->constraints
->max_uV
) {
931 target_min
= rdev
->constraints
->max_uV
;
932 target_max
= rdev
->constraints
->max_uV
;
935 if (target_min
!= current_uV
|| target_max
!= current_uV
) {
936 ret
= _regulator_do_set_voltage(
937 rdev
, target_min
, target_max
);
940 "failed to apply %d-%duV constraint(%d)\n",
941 target_min
, target_max
, ret
);
947 /* constrain machine-level voltage specs to fit
948 * the actual range supported by this regulator.
950 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
951 int count
= rdev
->desc
->n_voltages
;
953 int min_uV
= INT_MAX
;
954 int max_uV
= INT_MIN
;
955 int cmin
= constraints
->min_uV
;
956 int cmax
= constraints
->max_uV
;
958 /* it's safe to autoconfigure fixed-voltage supplies
959 and the constraints are used by list_voltage. */
960 if (count
== 1 && !cmin
) {
963 constraints
->min_uV
= cmin
;
964 constraints
->max_uV
= cmax
;
967 /* voltage constraints are optional */
968 if ((cmin
== 0) && (cmax
== 0))
971 /* else require explicit machine-level constraints */
972 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
973 rdev_err(rdev
, "invalid voltage constraints\n");
977 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
978 for (i
= 0; i
< count
; i
++) {
981 value
= ops
->list_voltage(rdev
, i
);
985 /* maybe adjust [min_uV..max_uV] */
986 if (value
>= cmin
&& value
< min_uV
)
988 if (value
<= cmax
&& value
> max_uV
)
992 /* final: [min_uV..max_uV] valid iff constraints valid */
993 if (max_uV
< min_uV
) {
995 "unsupportable voltage constraints %u-%uuV\n",
1000 /* use regulator's subset of machine constraints */
1001 if (constraints
->min_uV
< min_uV
) {
1002 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
1003 constraints
->min_uV
, min_uV
);
1004 constraints
->min_uV
= min_uV
;
1006 if (constraints
->max_uV
> max_uV
) {
1007 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
1008 constraints
->max_uV
, max_uV
);
1009 constraints
->max_uV
= max_uV
;
1016 static int machine_constraints_current(struct regulator_dev
*rdev
,
1017 struct regulation_constraints
*constraints
)
1019 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1022 if (!constraints
->min_uA
&& !constraints
->max_uA
)
1025 if (constraints
->min_uA
> constraints
->max_uA
) {
1026 rdev_err(rdev
, "Invalid current constraints\n");
1030 if (!ops
->set_current_limit
|| !ops
->get_current_limit
) {
1031 rdev_warn(rdev
, "Operation of current configuration missing\n");
1035 /* Set regulator current in constraints range */
1036 ret
= ops
->set_current_limit(rdev
, constraints
->min_uA
,
1037 constraints
->max_uA
);
1039 rdev_err(rdev
, "Failed to set current constraint, %d\n", ret
);
1046 static int _regulator_do_enable(struct regulator_dev
*rdev
);
1049 * set_machine_constraints - sets regulator constraints
1050 * @rdev: regulator source
1051 * @constraints: constraints to apply
1053 * Allows platform initialisation code to define and constrain
1054 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1055 * Constraints *must* be set by platform code in order for some
1056 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1059 static int set_machine_constraints(struct regulator_dev
*rdev
,
1060 const struct regulation_constraints
*constraints
)
1063 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1066 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
1069 rdev
->constraints
= kzalloc(sizeof(*constraints
),
1071 if (!rdev
->constraints
)
1074 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
1078 ret
= machine_constraints_current(rdev
, rdev
->constraints
);
1082 if (rdev
->constraints
->ilim_uA
&& ops
->set_input_current_limit
) {
1083 ret
= ops
->set_input_current_limit(rdev
,
1084 rdev
->constraints
->ilim_uA
);
1086 rdev_err(rdev
, "failed to set input limit\n");
1091 /* do we need to setup our suspend state */
1092 if (rdev
->constraints
->initial_state
) {
1093 ret
= suspend_prepare(rdev
, rdev
->constraints
->initial_state
);
1095 rdev_err(rdev
, "failed to set suspend state\n");
1100 if (rdev
->constraints
->initial_mode
) {
1101 if (!ops
->set_mode
) {
1102 rdev_err(rdev
, "no set_mode operation\n");
1106 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
1108 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
1113 /* If the constraints say the regulator should be on at this point
1114 * and we have control then make sure it is enabled.
1116 if (rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) {
1117 ret
= _regulator_do_enable(rdev
);
1118 if (ret
< 0 && ret
!= -EINVAL
) {
1119 rdev_err(rdev
, "failed to enable\n");
1124 if ((rdev
->constraints
->ramp_delay
|| rdev
->constraints
->ramp_disable
)
1125 && ops
->set_ramp_delay
) {
1126 ret
= ops
->set_ramp_delay(rdev
, rdev
->constraints
->ramp_delay
);
1128 rdev_err(rdev
, "failed to set ramp_delay\n");
1133 if (rdev
->constraints
->pull_down
&& ops
->set_pull_down
) {
1134 ret
= ops
->set_pull_down(rdev
);
1136 rdev_err(rdev
, "failed to set pull down\n");
1141 if (rdev
->constraints
->soft_start
&& ops
->set_soft_start
) {
1142 ret
= ops
->set_soft_start(rdev
);
1144 rdev_err(rdev
, "failed to set soft start\n");
1149 if (rdev
->constraints
->over_current_protection
1150 && ops
->set_over_current_protection
) {
1151 ret
= ops
->set_over_current_protection(rdev
);
1153 rdev_err(rdev
, "failed to set over current protection\n");
1158 if (rdev
->constraints
->active_discharge
&& ops
->set_active_discharge
) {
1159 bool ad_state
= (rdev
->constraints
->active_discharge
==
1160 REGULATOR_ACTIVE_DISCHARGE_ENABLE
) ? true : false;
1162 ret
= ops
->set_active_discharge(rdev
, ad_state
);
1164 rdev_err(rdev
, "failed to set active discharge\n");
1169 print_constraints(rdev
);
1174 * set_supply - set regulator supply regulator
1175 * @rdev: regulator name
1176 * @supply_rdev: supply regulator name
1178 * Called by platform initialisation code to set the supply regulator for this
1179 * regulator. This ensures that a regulators supply will also be enabled by the
1180 * core if it's child is enabled.
1182 static int set_supply(struct regulator_dev
*rdev
,
1183 struct regulator_dev
*supply_rdev
)
1187 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
1189 if (!try_module_get(supply_rdev
->owner
))
1192 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
1193 if (rdev
->supply
== NULL
) {
1197 supply_rdev
->open_count
++;
1203 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1204 * @rdev: regulator source
1205 * @consumer_dev_name: dev_name() string for device supply applies to
1206 * @supply: symbolic name for supply
1208 * Allows platform initialisation code to map physical regulator
1209 * sources to symbolic names for supplies for use by devices. Devices
1210 * should use these symbolic names to request regulators, avoiding the
1211 * need to provide board-specific regulator names as platform data.
1213 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1214 const char *consumer_dev_name
,
1217 struct regulator_map
*node
;
1223 if (consumer_dev_name
!= NULL
)
1228 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1229 if (node
->dev_name
&& consumer_dev_name
) {
1230 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1232 } else if (node
->dev_name
|| consumer_dev_name
) {
1236 if (strcmp(node
->supply
, supply
) != 0)
1239 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1241 dev_name(&node
->regulator
->dev
),
1242 node
->regulator
->desc
->name
,
1244 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1248 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1252 node
->regulator
= rdev
;
1253 node
->supply
= supply
;
1256 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1257 if (node
->dev_name
== NULL
) {
1263 list_add(&node
->list
, ®ulator_map_list
);
1267 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1269 struct regulator_map
*node
, *n
;
1271 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1272 if (rdev
== node
->regulator
) {
1273 list_del(&node
->list
);
1274 kfree(node
->dev_name
);
1280 #define REG_STR_SIZE 64
1282 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1284 const char *supply_name
)
1286 struct regulator
*regulator
;
1287 char buf
[REG_STR_SIZE
];
1290 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1291 if (regulator
== NULL
)
1294 mutex_lock(&rdev
->mutex
);
1295 regulator
->rdev
= rdev
;
1296 list_add(®ulator
->list
, &rdev
->consumer_list
);
1299 regulator
->dev
= dev
;
1301 /* Add a link to the device sysfs entry */
1302 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1303 dev
->kobj
.name
, supply_name
);
1304 if (size
>= REG_STR_SIZE
)
1307 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1308 if (regulator
->supply_name
== NULL
)
1311 err
= sysfs_create_link_nowarn(&rdev
->dev
.kobj
, &dev
->kobj
,
1314 rdev_dbg(rdev
, "could not add device link %s err %d\n",
1315 dev
->kobj
.name
, err
);
1319 regulator
->supply_name
= kstrdup(supply_name
, GFP_KERNEL
);
1320 if (regulator
->supply_name
== NULL
)
1324 regulator
->debugfs
= debugfs_create_dir(regulator
->supply_name
,
1326 if (!regulator
->debugfs
) {
1327 rdev_dbg(rdev
, "Failed to create debugfs directory\n");
1329 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1330 ®ulator
->uA_load
);
1331 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1332 ®ulator
->min_uV
);
1333 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1334 ®ulator
->max_uV
);
1338 * Check now if the regulator is an always on regulator - if
1339 * it is then we don't need to do nearly so much work for
1340 * enable/disable calls.
1342 if (!_regulator_can_change_status(rdev
) &&
1343 _regulator_is_enabled(rdev
))
1344 regulator
->always_on
= true;
1346 mutex_unlock(&rdev
->mutex
);
1349 list_del(®ulator
->list
);
1351 mutex_unlock(&rdev
->mutex
);
1355 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1357 if (rdev
->constraints
&& rdev
->constraints
->enable_time
)
1358 return rdev
->constraints
->enable_time
;
1359 if (!rdev
->desc
->ops
->enable_time
)
1360 return rdev
->desc
->enable_time
;
1361 return rdev
->desc
->ops
->enable_time(rdev
);
1364 static struct regulator_supply_alias
*regulator_find_supply_alias(
1365 struct device
*dev
, const char *supply
)
1367 struct regulator_supply_alias
*map
;
1369 list_for_each_entry(map
, ®ulator_supply_alias_list
, list
)
1370 if (map
->src_dev
== dev
&& strcmp(map
->src_supply
, supply
) == 0)
1376 static void regulator_supply_alias(struct device
**dev
, const char **supply
)
1378 struct regulator_supply_alias
*map
;
1380 map
= regulator_find_supply_alias(*dev
, *supply
);
1382 dev_dbg(*dev
, "Mapping supply %s to %s,%s\n",
1383 *supply
, map
->alias_supply
,
1384 dev_name(map
->alias_dev
));
1385 *dev
= map
->alias_dev
;
1386 *supply
= map
->alias_supply
;
1390 static int of_node_match(struct device
*dev
, const void *data
)
1392 return dev
->of_node
== data
;
1395 static struct regulator_dev
*of_find_regulator_by_node(struct device_node
*np
)
1399 dev
= class_find_device(®ulator_class
, NULL
, np
, of_node_match
);
1401 return dev
? dev_to_rdev(dev
) : NULL
;
1404 static int regulator_match(struct device
*dev
, const void *data
)
1406 struct regulator_dev
*r
= dev_to_rdev(dev
);
1408 return strcmp(rdev_get_name(r
), data
) == 0;
1411 static struct regulator_dev
*regulator_lookup_by_name(const char *name
)
1415 dev
= class_find_device(®ulator_class
, NULL
, name
, regulator_match
);
1417 return dev
? dev_to_rdev(dev
) : NULL
;
1421 * regulator_dev_lookup - lookup a regulator device.
1422 * @dev: device for regulator "consumer".
1423 * @supply: Supply name or regulator ID.
1424 * @ret: 0 on success, -ENODEV if lookup fails permanently, -EPROBE_DEFER if
1425 * lookup could succeed in the future.
1427 * If successful, returns a struct regulator_dev that corresponds to the name
1428 * @supply and with the embedded struct device refcount incremented by one,
1429 * or NULL on failure. The refcount must be dropped by calling put_device().
1431 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1435 struct regulator_dev
*r
;
1436 struct device_node
*node
;
1437 struct regulator_map
*map
;
1438 const char *devname
= NULL
;
1440 regulator_supply_alias(&dev
, &supply
);
1442 /* first do a dt based lookup */
1443 if (dev
&& dev
->of_node
) {
1444 node
= of_get_regulator(dev
, supply
);
1446 r
= of_find_regulator_by_node(node
);
1449 *ret
= -EPROBE_DEFER
;
1453 * If we couldn't even get the node then it's
1454 * not just that the device didn't register
1455 * yet, there's no node and we'll never
1462 /* if not found, try doing it non-dt way */
1464 devname
= dev_name(dev
);
1466 r
= regulator_lookup_by_name(supply
);
1470 mutex_lock(®ulator_list_mutex
);
1471 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1472 /* If the mapping has a device set up it must match */
1473 if (map
->dev_name
&&
1474 (!devname
|| strcmp(map
->dev_name
, devname
)))
1477 if (strcmp(map
->supply
, supply
) == 0 &&
1478 get_device(&map
->regulator
->dev
)) {
1479 mutex_unlock(®ulator_list_mutex
);
1480 return map
->regulator
;
1483 mutex_unlock(®ulator_list_mutex
);
1488 static int regulator_resolve_supply(struct regulator_dev
*rdev
)
1490 struct regulator_dev
*r
;
1491 struct device
*dev
= rdev
->dev
.parent
;
1494 /* No supply to resovle? */
1495 if (!rdev
->supply_name
)
1498 /* Supply already resolved? */
1502 r
= regulator_dev_lookup(dev
, rdev
->supply_name
, &ret
);
1504 if (ret
== -ENODEV
) {
1506 * No supply was specified for this regulator and
1507 * there will never be one.
1512 /* Did the lookup explicitly defer for us? */
1513 if (ret
== -EPROBE_DEFER
)
1516 if (have_full_constraints()) {
1517 r
= dummy_regulator_rdev
;
1518 get_device(&r
->dev
);
1520 dev_err(dev
, "Failed to resolve %s-supply for %s\n",
1521 rdev
->supply_name
, rdev
->desc
->name
);
1522 return -EPROBE_DEFER
;
1526 /* Recursively resolve the supply of the supply */
1527 ret
= regulator_resolve_supply(r
);
1529 put_device(&r
->dev
);
1533 ret
= set_supply(rdev
, r
);
1535 put_device(&r
->dev
);
1539 /* Cascade always-on state to supply */
1540 if (_regulator_is_enabled(rdev
)) {
1541 ret
= regulator_enable(rdev
->supply
);
1543 _regulator_put(rdev
->supply
);
1544 rdev
->supply
= NULL
;
1552 /* Internal regulator request function */
1553 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1554 bool exclusive
, bool allow_dummy
)
1556 struct regulator_dev
*rdev
;
1557 struct regulator
*regulator
= ERR_PTR(-EPROBE_DEFER
);
1558 const char *devname
= NULL
;
1562 pr_err("get() with no identifier\n");
1563 return ERR_PTR(-EINVAL
);
1567 devname
= dev_name(dev
);
1569 if (have_full_constraints())
1572 ret
= -EPROBE_DEFER
;
1574 rdev
= regulator_dev_lookup(dev
, id
, &ret
);
1578 regulator
= ERR_PTR(ret
);
1581 * If we have return value from dev_lookup fail, we do not expect to
1582 * succeed, so, quit with appropriate error value
1584 if (ret
&& ret
!= -ENODEV
)
1588 devname
= "deviceless";
1591 * Assume that a regulator is physically present and enabled
1592 * even if it isn't hooked up and just provide a dummy.
1594 if (have_full_constraints() && allow_dummy
) {
1595 pr_warn("%s supply %s not found, using dummy regulator\n",
1598 rdev
= dummy_regulator_rdev
;
1599 get_device(&rdev
->dev
);
1601 /* Don't log an error when called from regulator_get_optional() */
1602 } else if (!have_full_constraints() || exclusive
) {
1603 dev_warn(dev
, "dummy supplies not allowed\n");
1609 if (rdev
->exclusive
) {
1610 regulator
= ERR_PTR(-EPERM
);
1611 put_device(&rdev
->dev
);
1615 if (exclusive
&& rdev
->open_count
) {
1616 regulator
= ERR_PTR(-EBUSY
);
1617 put_device(&rdev
->dev
);
1621 ret
= regulator_resolve_supply(rdev
);
1623 regulator
= ERR_PTR(ret
);
1624 put_device(&rdev
->dev
);
1628 if (!try_module_get(rdev
->owner
)) {
1629 put_device(&rdev
->dev
);
1633 regulator
= create_regulator(rdev
, dev
, id
);
1634 if (regulator
== NULL
) {
1635 regulator
= ERR_PTR(-ENOMEM
);
1636 put_device(&rdev
->dev
);
1637 module_put(rdev
->owner
);
1643 rdev
->exclusive
= 1;
1645 ret
= _regulator_is_enabled(rdev
);
1647 rdev
->use_count
= 1;
1649 rdev
->use_count
= 0;
1656 * regulator_get - lookup and obtain a reference to a regulator.
1657 * @dev: device for regulator "consumer"
1658 * @id: Supply name or regulator ID.
1660 * Returns a struct regulator corresponding to the regulator producer,
1661 * or IS_ERR() condition containing errno.
1663 * Use of supply names configured via regulator_set_device_supply() is
1664 * strongly encouraged. It is recommended that the supply name used
1665 * should match the name used for the supply and/or the relevant
1666 * device pins in the datasheet.
1668 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1670 return _regulator_get(dev
, id
, false, true);
1672 EXPORT_SYMBOL_GPL(regulator_get
);
1675 * regulator_get_exclusive - obtain exclusive access to a regulator.
1676 * @dev: device for regulator "consumer"
1677 * @id: Supply name or regulator ID.
1679 * Returns a struct regulator corresponding to the regulator producer,
1680 * or IS_ERR() condition containing errno. Other consumers will be
1681 * unable to obtain this regulator while this reference is held and the
1682 * use count for the regulator will be initialised to reflect the current
1683 * state of the regulator.
1685 * This is intended for use by consumers which cannot tolerate shared
1686 * use of the regulator such as those which need to force the
1687 * regulator off for correct operation of the hardware they are
1690 * Use of supply names configured via regulator_set_device_supply() is
1691 * strongly encouraged. It is recommended that the supply name used
1692 * should match the name used for the supply and/or the relevant
1693 * device pins in the datasheet.
1695 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1697 return _regulator_get(dev
, id
, true, false);
1699 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1702 * regulator_get_optional - obtain optional access to a regulator.
1703 * @dev: device for regulator "consumer"
1704 * @id: Supply name or regulator ID.
1706 * Returns a struct regulator corresponding to the regulator producer,
1707 * or IS_ERR() condition containing errno.
1709 * This is intended for use by consumers for devices which can have
1710 * some supplies unconnected in normal use, such as some MMC devices.
1711 * It can allow the regulator core to provide stub supplies for other
1712 * supplies requested using normal regulator_get() calls without
1713 * disrupting the operation of drivers that can handle absent
1716 * Use of supply names configured via regulator_set_device_supply() is
1717 * strongly encouraged. It is recommended that the supply name used
1718 * should match the name used for the supply and/or the relevant
1719 * device pins in the datasheet.
1721 struct regulator
*regulator_get_optional(struct device
*dev
, const char *id
)
1723 return _regulator_get(dev
, id
, false, false);
1725 EXPORT_SYMBOL_GPL(regulator_get_optional
);
1727 /* regulator_list_mutex lock held by regulator_put() */
1728 static void _regulator_put(struct regulator
*regulator
)
1730 struct regulator_dev
*rdev
;
1732 if (IS_ERR_OR_NULL(regulator
))
1735 lockdep_assert_held_once(®ulator_list_mutex
);
1737 rdev
= regulator
->rdev
;
1739 debugfs_remove_recursive(regulator
->debugfs
);
1741 /* remove any sysfs entries */
1743 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1744 mutex_lock(&rdev
->mutex
);
1745 list_del(®ulator
->list
);
1748 rdev
->exclusive
= 0;
1749 put_device(&rdev
->dev
);
1750 mutex_unlock(&rdev
->mutex
);
1752 kfree(regulator
->supply_name
);
1755 module_put(rdev
->owner
);
1759 * regulator_put - "free" the regulator source
1760 * @regulator: regulator source
1762 * Note: drivers must ensure that all regulator_enable calls made on this
1763 * regulator source are balanced by regulator_disable calls prior to calling
1766 void regulator_put(struct regulator
*regulator
)
1768 mutex_lock(®ulator_list_mutex
);
1769 _regulator_put(regulator
);
1770 mutex_unlock(®ulator_list_mutex
);
1772 EXPORT_SYMBOL_GPL(regulator_put
);
1775 * regulator_register_supply_alias - Provide device alias for supply lookup
1777 * @dev: device that will be given as the regulator "consumer"
1778 * @id: Supply name or regulator ID
1779 * @alias_dev: device that should be used to lookup the supply
1780 * @alias_id: Supply name or regulator ID that should be used to lookup the
1783 * All lookups for id on dev will instead be conducted for alias_id on
1786 int regulator_register_supply_alias(struct device
*dev
, const char *id
,
1787 struct device
*alias_dev
,
1788 const char *alias_id
)
1790 struct regulator_supply_alias
*map
;
1792 map
= regulator_find_supply_alias(dev
, id
);
1796 map
= kzalloc(sizeof(struct regulator_supply_alias
), GFP_KERNEL
);
1801 map
->src_supply
= id
;
1802 map
->alias_dev
= alias_dev
;
1803 map
->alias_supply
= alias_id
;
1805 list_add(&map
->list
, ®ulator_supply_alias_list
);
1807 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1808 id
, dev_name(dev
), alias_id
, dev_name(alias_dev
));
1812 EXPORT_SYMBOL_GPL(regulator_register_supply_alias
);
1815 * regulator_unregister_supply_alias - Remove device alias
1817 * @dev: device that will be given as the regulator "consumer"
1818 * @id: Supply name or regulator ID
1820 * Remove a lookup alias if one exists for id on dev.
1822 void regulator_unregister_supply_alias(struct device
*dev
, const char *id
)
1824 struct regulator_supply_alias
*map
;
1826 map
= regulator_find_supply_alias(dev
, id
);
1828 list_del(&map
->list
);
1832 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias
);
1835 * regulator_bulk_register_supply_alias - register multiple aliases
1837 * @dev: device that will be given as the regulator "consumer"
1838 * @id: List of supply names or regulator IDs
1839 * @alias_dev: device that should be used to lookup the supply
1840 * @alias_id: List of supply names or regulator IDs that should be used to
1842 * @num_id: Number of aliases to register
1844 * @return 0 on success, an errno on failure.
1846 * This helper function allows drivers to register several supply
1847 * aliases in one operation. If any of the aliases cannot be
1848 * registered any aliases that were registered will be removed
1849 * before returning to the caller.
1851 int regulator_bulk_register_supply_alias(struct device
*dev
,
1852 const char *const *id
,
1853 struct device
*alias_dev
,
1854 const char *const *alias_id
,
1860 for (i
= 0; i
< num_id
; ++i
) {
1861 ret
= regulator_register_supply_alias(dev
, id
[i
], alias_dev
,
1871 "Failed to create supply alias %s,%s -> %s,%s\n",
1872 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
1875 regulator_unregister_supply_alias(dev
, id
[i
]);
1879 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias
);
1882 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1884 * @dev: device that will be given as the regulator "consumer"
1885 * @id: List of supply names or regulator IDs
1886 * @num_id: Number of aliases to unregister
1888 * This helper function allows drivers to unregister several supply
1889 * aliases in one operation.
1891 void regulator_bulk_unregister_supply_alias(struct device
*dev
,
1892 const char *const *id
,
1897 for (i
= 0; i
< num_id
; ++i
)
1898 regulator_unregister_supply_alias(dev
, id
[i
]);
1900 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias
);
1903 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1904 static int regulator_ena_gpio_request(struct regulator_dev
*rdev
,
1905 const struct regulator_config
*config
)
1907 struct regulator_enable_gpio
*pin
;
1908 struct gpio_desc
*gpiod
;
1911 gpiod
= gpio_to_desc(config
->ena_gpio
);
1913 list_for_each_entry(pin
, ®ulator_ena_gpio_list
, list
) {
1914 if (pin
->gpiod
== gpiod
) {
1915 rdev_dbg(rdev
, "GPIO %d is already used\n",
1917 goto update_ena_gpio_to_rdev
;
1921 ret
= gpio_request_one(config
->ena_gpio
,
1922 GPIOF_DIR_OUT
| config
->ena_gpio_flags
,
1923 rdev_get_name(rdev
));
1927 pin
= kzalloc(sizeof(struct regulator_enable_gpio
), GFP_KERNEL
);
1929 gpio_free(config
->ena_gpio
);
1934 pin
->ena_gpio_invert
= config
->ena_gpio_invert
;
1935 list_add(&pin
->list
, ®ulator_ena_gpio_list
);
1937 update_ena_gpio_to_rdev
:
1938 pin
->request_count
++;
1939 rdev
->ena_pin
= pin
;
1943 static void regulator_ena_gpio_free(struct regulator_dev
*rdev
)
1945 struct regulator_enable_gpio
*pin
, *n
;
1950 /* Free the GPIO only in case of no use */
1951 list_for_each_entry_safe(pin
, n
, ®ulator_ena_gpio_list
, list
) {
1952 if (pin
->gpiod
== rdev
->ena_pin
->gpiod
) {
1953 if (pin
->request_count
<= 1) {
1954 pin
->request_count
= 0;
1955 gpiod_put(pin
->gpiod
);
1956 list_del(&pin
->list
);
1958 rdev
->ena_pin
= NULL
;
1961 pin
->request_count
--;
1968 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1969 * @rdev: regulator_dev structure
1970 * @enable: enable GPIO at initial use?
1972 * GPIO is enabled in case of initial use. (enable_count is 0)
1973 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1975 static int regulator_ena_gpio_ctrl(struct regulator_dev
*rdev
, bool enable
)
1977 struct regulator_enable_gpio
*pin
= rdev
->ena_pin
;
1983 /* Enable GPIO at initial use */
1984 if (pin
->enable_count
== 0)
1985 gpiod_set_value_cansleep(pin
->gpiod
,
1986 !pin
->ena_gpio_invert
);
1988 pin
->enable_count
++;
1990 if (pin
->enable_count
> 1) {
1991 pin
->enable_count
--;
1995 /* Disable GPIO if not used */
1996 if (pin
->enable_count
<= 1) {
1997 gpiod_set_value_cansleep(pin
->gpiod
,
1998 pin
->ena_gpio_invert
);
1999 pin
->enable_count
= 0;
2007 * _regulator_enable_delay - a delay helper function
2008 * @delay: time to delay in microseconds
2010 * Delay for the requested amount of time as per the guidelines in:
2012 * Documentation/timers/timers-howto.txt
2014 * The assumption here is that regulators will never be enabled in
2015 * atomic context and therefore sleeping functions can be used.
2017 static void _regulator_enable_delay(unsigned int delay
)
2019 unsigned int ms
= delay
/ 1000;
2020 unsigned int us
= delay
% 1000;
2024 * For small enough values, handle super-millisecond
2025 * delays in the usleep_range() call below.
2034 * Give the scheduler some room to coalesce with any other
2035 * wakeup sources. For delays shorter than 10 us, don't even
2036 * bother setting up high-resolution timers and just busy-
2040 usleep_range(us
, us
+ 100);
2045 static int _regulator_do_enable(struct regulator_dev
*rdev
)
2049 /* Query before enabling in case configuration dependent. */
2050 ret
= _regulator_get_enable_time(rdev
);
2054 rdev_warn(rdev
, "enable_time() failed: %d\n", ret
);
2058 trace_regulator_enable(rdev_get_name(rdev
));
2060 if (rdev
->desc
->off_on_delay
) {
2061 /* if needed, keep a distance of off_on_delay from last time
2062 * this regulator was disabled.
2064 unsigned long start_jiffy
= jiffies
;
2065 unsigned long intended
, max_delay
, remaining
;
2067 max_delay
= usecs_to_jiffies(rdev
->desc
->off_on_delay
);
2068 intended
= rdev
->last_off_jiffy
+ max_delay
;
2070 if (time_before(start_jiffy
, intended
)) {
2071 /* calc remaining jiffies to deal with one-time
2073 * in case of multiple timer wrapping, either it can be
2074 * detected by out-of-range remaining, or it cannot be
2075 * detected and we gets a panelty of
2076 * _regulator_enable_delay().
2078 remaining
= intended
- start_jiffy
;
2079 if (remaining
<= max_delay
)
2080 _regulator_enable_delay(
2081 jiffies_to_usecs(remaining
));
2085 if (rdev
->ena_pin
) {
2086 if (!rdev
->ena_gpio_state
) {
2087 ret
= regulator_ena_gpio_ctrl(rdev
, true);
2090 rdev
->ena_gpio_state
= 1;
2092 } else if (rdev
->desc
->ops
->enable
) {
2093 ret
= rdev
->desc
->ops
->enable(rdev
);
2100 /* Allow the regulator to ramp; it would be useful to extend
2101 * this for bulk operations so that the regulators can ramp
2103 trace_regulator_enable_delay(rdev_get_name(rdev
));
2105 _regulator_enable_delay(delay
);
2107 trace_regulator_enable_complete(rdev_get_name(rdev
));
2112 /* locks held by regulator_enable() */
2113 static int _regulator_enable(struct regulator_dev
*rdev
)
2117 lockdep_assert_held_once(&rdev
->mutex
);
2119 /* check voltage and requested load before enabling */
2120 if (rdev
->constraints
&&
2121 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
2122 drms_uA_update(rdev
);
2124 if (rdev
->use_count
== 0) {
2125 /* The regulator may on if it's not switchable or left on */
2126 ret
= _regulator_is_enabled(rdev
);
2127 if (ret
== -EINVAL
|| ret
== 0) {
2128 if (!_regulator_can_change_status(rdev
))
2131 ret
= _regulator_do_enable(rdev
);
2135 } else if (ret
< 0) {
2136 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
2139 /* Fallthrough on positive return values - already enabled */
2148 * regulator_enable - enable regulator output
2149 * @regulator: regulator source
2151 * Request that the regulator be enabled with the regulator output at
2152 * the predefined voltage or current value. Calls to regulator_enable()
2153 * must be balanced with calls to regulator_disable().
2155 * NOTE: the output value can be set by other drivers, boot loader or may be
2156 * hardwired in the regulator.
2158 int regulator_enable(struct regulator
*regulator
)
2160 struct regulator_dev
*rdev
= regulator
->rdev
;
2163 if (regulator
->always_on
)
2167 ret
= regulator_enable(rdev
->supply
);
2172 mutex_lock(&rdev
->mutex
);
2173 ret
= _regulator_enable(rdev
);
2174 mutex_unlock(&rdev
->mutex
);
2176 if (ret
!= 0 && rdev
->supply
)
2177 regulator_disable(rdev
->supply
);
2181 EXPORT_SYMBOL_GPL(regulator_enable
);
2183 static int _regulator_do_disable(struct regulator_dev
*rdev
)
2187 trace_regulator_disable(rdev_get_name(rdev
));
2189 if (rdev
->ena_pin
) {
2190 if (rdev
->ena_gpio_state
) {
2191 ret
= regulator_ena_gpio_ctrl(rdev
, false);
2194 rdev
->ena_gpio_state
= 0;
2197 } else if (rdev
->desc
->ops
->disable
) {
2198 ret
= rdev
->desc
->ops
->disable(rdev
);
2203 /* cares about last_off_jiffy only if off_on_delay is required by
2206 if (rdev
->desc
->off_on_delay
)
2207 rdev
->last_off_jiffy
= jiffies
;
2209 trace_regulator_disable_complete(rdev_get_name(rdev
));
2214 /* locks held by regulator_disable() */
2215 static int _regulator_disable(struct regulator_dev
*rdev
)
2219 lockdep_assert_held_once(&rdev
->mutex
);
2221 if (WARN(rdev
->use_count
<= 0,
2222 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
2225 /* are we the last user and permitted to disable ? */
2226 if (rdev
->use_count
== 1 &&
2227 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
2229 /* we are last user */
2230 if (_regulator_can_change_status(rdev
)) {
2231 ret
= _notifier_call_chain(rdev
,
2232 REGULATOR_EVENT_PRE_DISABLE
,
2234 if (ret
& NOTIFY_STOP_MASK
)
2237 ret
= _regulator_do_disable(rdev
);
2239 rdev_err(rdev
, "failed to disable\n");
2240 _notifier_call_chain(rdev
,
2241 REGULATOR_EVENT_ABORT_DISABLE
,
2245 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
2249 rdev
->use_count
= 0;
2250 } else if (rdev
->use_count
> 1) {
2252 if (rdev
->constraints
&&
2253 (rdev
->constraints
->valid_ops_mask
&
2254 REGULATOR_CHANGE_DRMS
))
2255 drms_uA_update(rdev
);
2264 * regulator_disable - disable regulator output
2265 * @regulator: regulator source
2267 * Disable the regulator output voltage or current. Calls to
2268 * regulator_enable() must be balanced with calls to
2269 * regulator_disable().
2271 * NOTE: this will only disable the regulator output if no other consumer
2272 * devices have it enabled, the regulator device supports disabling and
2273 * machine constraints permit this operation.
2275 int regulator_disable(struct regulator
*regulator
)
2277 struct regulator_dev
*rdev
= regulator
->rdev
;
2280 if (regulator
->always_on
)
2283 mutex_lock(&rdev
->mutex
);
2284 ret
= _regulator_disable(rdev
);
2285 mutex_unlock(&rdev
->mutex
);
2287 if (ret
== 0 && rdev
->supply
)
2288 regulator_disable(rdev
->supply
);
2292 EXPORT_SYMBOL_GPL(regulator_disable
);
2294 /* locks held by regulator_force_disable() */
2295 static int _regulator_force_disable(struct regulator_dev
*rdev
)
2299 lockdep_assert_held_once(&rdev
->mutex
);
2301 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2302 REGULATOR_EVENT_PRE_DISABLE
, NULL
);
2303 if (ret
& NOTIFY_STOP_MASK
)
2306 ret
= _regulator_do_disable(rdev
);
2308 rdev_err(rdev
, "failed to force disable\n");
2309 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2310 REGULATOR_EVENT_ABORT_DISABLE
, NULL
);
2314 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2315 REGULATOR_EVENT_DISABLE
, NULL
);
2321 * regulator_force_disable - force disable regulator output
2322 * @regulator: regulator source
2324 * Forcibly disable the regulator output voltage or current.
2325 * NOTE: this *will* disable the regulator output even if other consumer
2326 * devices have it enabled. This should be used for situations when device
2327 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2329 int regulator_force_disable(struct regulator
*regulator
)
2331 struct regulator_dev
*rdev
= regulator
->rdev
;
2334 mutex_lock(&rdev
->mutex
);
2335 regulator
->uA_load
= 0;
2336 ret
= _regulator_force_disable(regulator
->rdev
);
2337 mutex_unlock(&rdev
->mutex
);
2340 while (rdev
->open_count
--)
2341 regulator_disable(rdev
->supply
);
2345 EXPORT_SYMBOL_GPL(regulator_force_disable
);
2347 static void regulator_disable_work(struct work_struct
*work
)
2349 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
2353 mutex_lock(&rdev
->mutex
);
2355 BUG_ON(!rdev
->deferred_disables
);
2357 count
= rdev
->deferred_disables
;
2358 rdev
->deferred_disables
= 0;
2360 for (i
= 0; i
< count
; i
++) {
2361 ret
= _regulator_disable(rdev
);
2363 rdev_err(rdev
, "Deferred disable failed: %d\n", ret
);
2366 mutex_unlock(&rdev
->mutex
);
2369 for (i
= 0; i
< count
; i
++) {
2370 ret
= regulator_disable(rdev
->supply
);
2373 "Supply disable failed: %d\n", ret
);
2380 * regulator_disable_deferred - disable regulator output with delay
2381 * @regulator: regulator source
2382 * @ms: miliseconds until the regulator is disabled
2384 * Execute regulator_disable() on the regulator after a delay. This
2385 * is intended for use with devices that require some time to quiesce.
2387 * NOTE: this will only disable the regulator output if no other consumer
2388 * devices have it enabled, the regulator device supports disabling and
2389 * machine constraints permit this operation.
2391 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
2393 struct regulator_dev
*rdev
= regulator
->rdev
;
2395 if (regulator
->always_on
)
2399 return regulator_disable(regulator
);
2401 mutex_lock(&rdev
->mutex
);
2402 rdev
->deferred_disables
++;
2403 mutex_unlock(&rdev
->mutex
);
2405 queue_delayed_work(system_power_efficient_wq
, &rdev
->disable_work
,
2406 msecs_to_jiffies(ms
));
2409 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
2411 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
2413 /* A GPIO control always takes precedence */
2415 return rdev
->ena_gpio_state
;
2417 /* If we don't know then assume that the regulator is always on */
2418 if (!rdev
->desc
->ops
->is_enabled
)
2421 return rdev
->desc
->ops
->is_enabled(rdev
);
2424 static int _regulator_list_voltage(struct regulator
*regulator
,
2425 unsigned selector
, int lock
)
2427 struct regulator_dev
*rdev
= regulator
->rdev
;
2428 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2431 if (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1 && !selector
)
2432 return rdev
->desc
->fixed_uV
;
2434 if (ops
->list_voltage
) {
2435 if (selector
>= rdev
->desc
->n_voltages
)
2438 mutex_lock(&rdev
->mutex
);
2439 ret
= ops
->list_voltage(rdev
, selector
);
2441 mutex_unlock(&rdev
->mutex
);
2442 } else if (rdev
->supply
) {
2443 ret
= _regulator_list_voltage(rdev
->supply
, selector
, lock
);
2449 if (ret
< rdev
->constraints
->min_uV
)
2451 else if (ret
> rdev
->constraints
->max_uV
)
2459 * regulator_is_enabled - is the regulator output enabled
2460 * @regulator: regulator source
2462 * Returns positive if the regulator driver backing the source/client
2463 * has requested that the device be enabled, zero if it hasn't, else a
2464 * negative errno code.
2466 * Note that the device backing this regulator handle can have multiple
2467 * users, so it might be enabled even if regulator_enable() was never
2468 * called for this particular source.
2470 int regulator_is_enabled(struct regulator
*regulator
)
2474 if (regulator
->always_on
)
2477 mutex_lock(®ulator
->rdev
->mutex
);
2478 ret
= _regulator_is_enabled(regulator
->rdev
);
2479 mutex_unlock(®ulator
->rdev
->mutex
);
2483 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
2486 * regulator_can_change_voltage - check if regulator can change voltage
2487 * @regulator: regulator source
2489 * Returns positive if the regulator driver backing the source/client
2490 * can change its voltage, false otherwise. Useful for detecting fixed
2491 * or dummy regulators and disabling voltage change logic in the client
2494 int regulator_can_change_voltage(struct regulator
*regulator
)
2496 struct regulator_dev
*rdev
= regulator
->rdev
;
2498 if (rdev
->constraints
&&
2499 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2500 if (rdev
->desc
->n_voltages
- rdev
->desc
->linear_min_sel
> 1)
2503 if (rdev
->desc
->continuous_voltage_range
&&
2504 rdev
->constraints
->min_uV
&& rdev
->constraints
->max_uV
&&
2505 rdev
->constraints
->min_uV
!= rdev
->constraints
->max_uV
)
2511 EXPORT_SYMBOL_GPL(regulator_can_change_voltage
);
2514 * regulator_count_voltages - count regulator_list_voltage() selectors
2515 * @regulator: regulator source
2517 * Returns number of selectors, or negative errno. Selectors are
2518 * numbered starting at zero, and typically correspond to bitfields
2519 * in hardware registers.
2521 int regulator_count_voltages(struct regulator
*regulator
)
2523 struct regulator_dev
*rdev
= regulator
->rdev
;
2525 if (rdev
->desc
->n_voltages
)
2526 return rdev
->desc
->n_voltages
;
2531 return regulator_count_voltages(rdev
->supply
);
2533 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
2536 * regulator_list_voltage - enumerate supported voltages
2537 * @regulator: regulator source
2538 * @selector: identify voltage to list
2539 * Context: can sleep
2541 * Returns a voltage that can be passed to @regulator_set_voltage(),
2542 * zero if this selector code can't be used on this system, or a
2545 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
2547 return _regulator_list_voltage(regulator
, selector
, 1);
2549 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
2552 * regulator_get_regmap - get the regulator's register map
2553 * @regulator: regulator source
2555 * Returns the register map for the given regulator, or an ERR_PTR value
2556 * if the regulator doesn't use regmap.
2558 struct regmap
*regulator_get_regmap(struct regulator
*regulator
)
2560 struct regmap
*map
= regulator
->rdev
->regmap
;
2562 return map
? map
: ERR_PTR(-EOPNOTSUPP
);
2566 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2567 * @regulator: regulator source
2568 * @vsel_reg: voltage selector register, output parameter
2569 * @vsel_mask: mask for voltage selector bitfield, output parameter
2571 * Returns the hardware register offset and bitmask used for setting the
2572 * regulator voltage. This might be useful when configuring voltage-scaling
2573 * hardware or firmware that can make I2C requests behind the kernel's back,
2576 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2577 * and 0 is returned, otherwise a negative errno is returned.
2579 int regulator_get_hardware_vsel_register(struct regulator
*regulator
,
2581 unsigned *vsel_mask
)
2583 struct regulator_dev
*rdev
= regulator
->rdev
;
2584 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2586 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2589 *vsel_reg
= rdev
->desc
->vsel_reg
;
2590 *vsel_mask
= rdev
->desc
->vsel_mask
;
2594 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register
);
2597 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2598 * @regulator: regulator source
2599 * @selector: identify voltage to list
2601 * Converts the selector to a hardware-specific voltage selector that can be
2602 * directly written to the regulator registers. The address of the voltage
2603 * register can be determined by calling @regulator_get_hardware_vsel_register.
2605 * On error a negative errno is returned.
2607 int regulator_list_hardware_vsel(struct regulator
*regulator
,
2610 struct regulator_dev
*rdev
= regulator
->rdev
;
2611 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2613 if (selector
>= rdev
->desc
->n_voltages
)
2615 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2620 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel
);
2623 * regulator_get_linear_step - return the voltage step size between VSEL values
2624 * @regulator: regulator source
2626 * Returns the voltage step size between VSEL values for linear
2627 * regulators, or return 0 if the regulator isn't a linear regulator.
2629 unsigned int regulator_get_linear_step(struct regulator
*regulator
)
2631 struct regulator_dev
*rdev
= regulator
->rdev
;
2633 return rdev
->desc
->uV_step
;
2635 EXPORT_SYMBOL_GPL(regulator_get_linear_step
);
2638 * regulator_is_supported_voltage - check if a voltage range can be supported
2640 * @regulator: Regulator to check.
2641 * @min_uV: Minimum required voltage in uV.
2642 * @max_uV: Maximum required voltage in uV.
2644 * Returns a boolean or a negative error code.
2646 int regulator_is_supported_voltage(struct regulator
*regulator
,
2647 int min_uV
, int max_uV
)
2649 struct regulator_dev
*rdev
= regulator
->rdev
;
2650 int i
, voltages
, ret
;
2652 /* If we can't change voltage check the current voltage */
2653 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2654 ret
= regulator_get_voltage(regulator
);
2656 return min_uV
<= ret
&& ret
<= max_uV
;
2661 /* Any voltage within constrains range is fine? */
2662 if (rdev
->desc
->continuous_voltage_range
)
2663 return min_uV
>= rdev
->constraints
->min_uV
&&
2664 max_uV
<= rdev
->constraints
->max_uV
;
2666 ret
= regulator_count_voltages(regulator
);
2671 for (i
= 0; i
< voltages
; i
++) {
2672 ret
= regulator_list_voltage(regulator
, i
);
2674 if (ret
>= min_uV
&& ret
<= max_uV
)
2680 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
2682 static int regulator_map_voltage(struct regulator_dev
*rdev
, int min_uV
,
2685 const struct regulator_desc
*desc
= rdev
->desc
;
2687 if (desc
->ops
->map_voltage
)
2688 return desc
->ops
->map_voltage(rdev
, min_uV
, max_uV
);
2690 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear
)
2691 return regulator_map_voltage_linear(rdev
, min_uV
, max_uV
);
2693 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear_range
)
2694 return regulator_map_voltage_linear_range(rdev
, min_uV
, max_uV
);
2696 return regulator_map_voltage_iterate(rdev
, min_uV
, max_uV
);
2699 static int _regulator_call_set_voltage(struct regulator_dev
*rdev
,
2700 int min_uV
, int max_uV
,
2703 struct pre_voltage_change_data data
;
2706 data
.old_uV
= _regulator_get_voltage(rdev
);
2707 data
.min_uV
= min_uV
;
2708 data
.max_uV
= max_uV
;
2709 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
2711 if (ret
& NOTIFY_STOP_MASK
)
2714 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
, selector
);
2718 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
2719 (void *)data
.old_uV
);
2724 static int _regulator_call_set_voltage_sel(struct regulator_dev
*rdev
,
2725 int uV
, unsigned selector
)
2727 struct pre_voltage_change_data data
;
2730 data
.old_uV
= _regulator_get_voltage(rdev
);
2733 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
2735 if (ret
& NOTIFY_STOP_MASK
)
2738 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
2742 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
2743 (void *)data
.old_uV
);
2748 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
2749 int min_uV
, int max_uV
)
2754 unsigned int selector
;
2755 int old_selector
= -1;
2757 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
2759 min_uV
+= rdev
->constraints
->uV_offset
;
2760 max_uV
+= rdev
->constraints
->uV_offset
;
2763 * If we can't obtain the old selector there is not enough
2764 * info to call set_voltage_time_sel().
2766 if (_regulator_is_enabled(rdev
) &&
2767 rdev
->desc
->ops
->set_voltage_time_sel
&&
2768 rdev
->desc
->ops
->get_voltage_sel
) {
2769 old_selector
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
2770 if (old_selector
< 0)
2771 return old_selector
;
2774 if (rdev
->desc
->ops
->set_voltage
) {
2775 ret
= _regulator_call_set_voltage(rdev
, min_uV
, max_uV
,
2779 if (rdev
->desc
->ops
->list_voltage
)
2780 best_val
= rdev
->desc
->ops
->list_voltage(rdev
,
2783 best_val
= _regulator_get_voltage(rdev
);
2786 } else if (rdev
->desc
->ops
->set_voltage_sel
) {
2787 ret
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
2789 best_val
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
2790 if (min_uV
<= best_val
&& max_uV
>= best_val
) {
2792 if (old_selector
== selector
)
2795 ret
= _regulator_call_set_voltage_sel(
2796 rdev
, best_val
, selector
);
2805 /* Call set_voltage_time_sel if successfully obtained old_selector */
2806 if (ret
== 0 && !rdev
->constraints
->ramp_disable
&& old_selector
>= 0
2807 && old_selector
!= selector
) {
2809 delay
= rdev
->desc
->ops
->set_voltage_time_sel(rdev
,
2810 old_selector
, selector
);
2812 rdev_warn(rdev
, "set_voltage_time_sel() failed: %d\n",
2817 /* Insert any necessary delays */
2818 if (delay
>= 1000) {
2819 mdelay(delay
/ 1000);
2820 udelay(delay
% 1000);
2826 if (ret
== 0 && best_val
>= 0) {
2827 unsigned long data
= best_val
;
2829 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
2833 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), best_val
);
2838 static int regulator_set_voltage_unlocked(struct regulator
*regulator
,
2839 int min_uV
, int max_uV
)
2841 struct regulator_dev
*rdev
= regulator
->rdev
;
2843 int old_min_uV
, old_max_uV
;
2845 int best_supply_uV
= 0;
2846 int supply_change_uV
= 0;
2848 /* If we're setting the same range as last time the change
2849 * should be a noop (some cpufreq implementations use the same
2850 * voltage for multiple frequencies, for example).
2852 if (regulator
->min_uV
== min_uV
&& regulator
->max_uV
== max_uV
)
2855 /* If we're trying to set a range that overlaps the current voltage,
2856 * return successfully even though the regulator does not support
2857 * changing the voltage.
2859 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2860 current_uV
= _regulator_get_voltage(rdev
);
2861 if (min_uV
<= current_uV
&& current_uV
<= max_uV
) {
2862 regulator
->min_uV
= min_uV
;
2863 regulator
->max_uV
= max_uV
;
2869 if (!rdev
->desc
->ops
->set_voltage
&&
2870 !rdev
->desc
->ops
->set_voltage_sel
) {
2875 /* constraints check */
2876 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
2880 /* restore original values in case of error */
2881 old_min_uV
= regulator
->min_uV
;
2882 old_max_uV
= regulator
->max_uV
;
2883 regulator
->min_uV
= min_uV
;
2884 regulator
->max_uV
= max_uV
;
2886 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
2890 if (rdev
->supply
&& (rdev
->desc
->min_dropout_uV
||
2891 !rdev
->desc
->ops
->get_voltage
)) {
2892 int current_supply_uV
;
2895 selector
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
2901 best_supply_uV
= _regulator_list_voltage(regulator
, selector
, 0);
2902 if (best_supply_uV
< 0) {
2903 ret
= best_supply_uV
;
2907 best_supply_uV
+= rdev
->desc
->min_dropout_uV
;
2909 current_supply_uV
= _regulator_get_voltage(rdev
->supply
->rdev
);
2910 if (current_supply_uV
< 0) {
2911 ret
= current_supply_uV
;
2915 supply_change_uV
= best_supply_uV
- current_supply_uV
;
2918 if (supply_change_uV
> 0) {
2919 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
2920 best_supply_uV
, INT_MAX
);
2922 dev_err(&rdev
->dev
, "Failed to increase supply voltage: %d\n",
2928 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
2932 if (supply_change_uV
< 0) {
2933 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
2934 best_supply_uV
, INT_MAX
);
2936 dev_warn(&rdev
->dev
, "Failed to decrease supply voltage: %d\n",
2938 /* No need to fail here */
2945 regulator
->min_uV
= old_min_uV
;
2946 regulator
->max_uV
= old_max_uV
;
2952 * regulator_set_voltage - set regulator output voltage
2953 * @regulator: regulator source
2954 * @min_uV: Minimum required voltage in uV
2955 * @max_uV: Maximum acceptable voltage in uV
2957 * Sets a voltage regulator to the desired output voltage. This can be set
2958 * during any regulator state. IOW, regulator can be disabled or enabled.
2960 * If the regulator is enabled then the voltage will change to the new value
2961 * immediately otherwise if the regulator is disabled the regulator will
2962 * output at the new voltage when enabled.
2964 * NOTE: If the regulator is shared between several devices then the lowest
2965 * request voltage that meets the system constraints will be used.
2966 * Regulator system constraints must be set for this regulator before
2967 * calling this function otherwise this call will fail.
2969 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
2973 regulator_lock_supply(regulator
->rdev
);
2975 ret
= regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
);
2977 regulator_unlock_supply(regulator
->rdev
);
2981 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
2984 * regulator_set_voltage_time - get raise/fall time
2985 * @regulator: regulator source
2986 * @old_uV: starting voltage in microvolts
2987 * @new_uV: target voltage in microvolts
2989 * Provided with the starting and ending voltage, this function attempts to
2990 * calculate the time in microseconds required to rise or fall to this new
2993 int regulator_set_voltage_time(struct regulator
*regulator
,
2994 int old_uV
, int new_uV
)
2996 struct regulator_dev
*rdev
= regulator
->rdev
;
2997 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3003 /* Currently requires operations to do this */
3004 if (!ops
->list_voltage
|| !ops
->set_voltage_time_sel
3005 || !rdev
->desc
->n_voltages
)
3008 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
3009 /* We only look for exact voltage matches here */
3010 voltage
= regulator_list_voltage(regulator
, i
);
3015 if (voltage
== old_uV
)
3017 if (voltage
== new_uV
)
3021 if (old_sel
< 0 || new_sel
< 0)
3024 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
3026 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
3029 * regulator_set_voltage_time_sel - get raise/fall time
3030 * @rdev: regulator source device
3031 * @old_selector: selector for starting voltage
3032 * @new_selector: selector for target voltage
3034 * Provided with the starting and target voltage selectors, this function
3035 * returns time in microseconds required to rise or fall to this new voltage
3037 * Drivers providing ramp_delay in regulation_constraints can use this as their
3038 * set_voltage_time_sel() operation.
3040 int regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
3041 unsigned int old_selector
,
3042 unsigned int new_selector
)
3044 unsigned int ramp_delay
= 0;
3045 int old_volt
, new_volt
;
3047 if (rdev
->constraints
->ramp_delay
)
3048 ramp_delay
= rdev
->constraints
->ramp_delay
;
3049 else if (rdev
->desc
->ramp_delay
)
3050 ramp_delay
= rdev
->desc
->ramp_delay
;
3052 if (ramp_delay
== 0) {
3053 rdev_warn(rdev
, "ramp_delay not set\n");
3058 if (!rdev
->desc
->ops
->list_voltage
)
3061 old_volt
= rdev
->desc
->ops
->list_voltage(rdev
, old_selector
);
3062 new_volt
= rdev
->desc
->ops
->list_voltage(rdev
, new_selector
);
3064 return DIV_ROUND_UP(abs(new_volt
- old_volt
), ramp_delay
);
3066 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel
);
3069 * regulator_sync_voltage - re-apply last regulator output voltage
3070 * @regulator: regulator source
3072 * Re-apply the last configured voltage. This is intended to be used
3073 * where some external control source the consumer is cooperating with
3074 * has caused the configured voltage to change.
3076 int regulator_sync_voltage(struct regulator
*regulator
)
3078 struct regulator_dev
*rdev
= regulator
->rdev
;
3079 int ret
, min_uV
, max_uV
;
3081 mutex_lock(&rdev
->mutex
);
3083 if (!rdev
->desc
->ops
->set_voltage
&&
3084 !rdev
->desc
->ops
->set_voltage_sel
) {
3089 /* This is only going to work if we've had a voltage configured. */
3090 if (!regulator
->min_uV
&& !regulator
->max_uV
) {
3095 min_uV
= regulator
->min_uV
;
3096 max_uV
= regulator
->max_uV
;
3098 /* This should be a paranoia check... */
3099 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
3103 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
3107 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
3110 mutex_unlock(&rdev
->mutex
);
3113 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
3115 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
3120 if (rdev
->desc
->ops
->get_bypass
) {
3121 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypassed
);
3125 /* if bypassed the regulator must have a supply */
3126 if (!rdev
->supply
) {
3128 "bypassed regulator has no supply!\n");
3129 return -EPROBE_DEFER
;
3132 return _regulator_get_voltage(rdev
->supply
->rdev
);
3136 if (rdev
->desc
->ops
->get_voltage_sel
) {
3137 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
3140 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
3141 } else if (rdev
->desc
->ops
->get_voltage
) {
3142 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
3143 } else if (rdev
->desc
->ops
->list_voltage
) {
3144 ret
= rdev
->desc
->ops
->list_voltage(rdev
, 0);
3145 } else if (rdev
->desc
->fixed_uV
&& (rdev
->desc
->n_voltages
== 1)) {
3146 ret
= rdev
->desc
->fixed_uV
;
3147 } else if (rdev
->supply
) {
3148 ret
= _regulator_get_voltage(rdev
->supply
->rdev
);
3155 return ret
- rdev
->constraints
->uV_offset
;
3159 * regulator_get_voltage - get regulator output voltage
3160 * @regulator: regulator source
3162 * This returns the current regulator voltage in uV.
3164 * NOTE: If the regulator is disabled it will return the voltage value. This
3165 * function should not be used to determine regulator state.
3167 int regulator_get_voltage(struct regulator
*regulator
)
3171 regulator_lock_supply(regulator
->rdev
);
3173 ret
= _regulator_get_voltage(regulator
->rdev
);
3175 regulator_unlock_supply(regulator
->rdev
);
3179 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
3182 * regulator_set_current_limit - set regulator output current limit
3183 * @regulator: regulator source
3184 * @min_uA: Minimum supported current in uA
3185 * @max_uA: Maximum supported current in uA
3187 * Sets current sink to the desired output current. This can be set during
3188 * any regulator state. IOW, regulator can be disabled or enabled.
3190 * If the regulator is enabled then the current will change to the new value
3191 * immediately otherwise if the regulator is disabled the regulator will
3192 * output at the new current when enabled.
3194 * NOTE: Regulator system constraints must be set for this regulator before
3195 * calling this function otherwise this call will fail.
3197 int regulator_set_current_limit(struct regulator
*regulator
,
3198 int min_uA
, int max_uA
)
3200 struct regulator_dev
*rdev
= regulator
->rdev
;
3203 mutex_lock(&rdev
->mutex
);
3206 if (!rdev
->desc
->ops
->set_current_limit
) {
3211 /* constraints check */
3212 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
3216 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
3218 mutex_unlock(&rdev
->mutex
);
3221 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
3223 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
3227 mutex_lock(&rdev
->mutex
);
3230 if (!rdev
->desc
->ops
->get_current_limit
) {
3235 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
3237 mutex_unlock(&rdev
->mutex
);
3242 * regulator_get_current_limit - get regulator output current
3243 * @regulator: regulator source
3245 * This returns the current supplied by the specified current sink in uA.
3247 * NOTE: If the regulator is disabled it will return the current value. This
3248 * function should not be used to determine regulator state.
3250 int regulator_get_current_limit(struct regulator
*regulator
)
3252 return _regulator_get_current_limit(regulator
->rdev
);
3254 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
3257 * regulator_set_mode - set regulator operating mode
3258 * @regulator: regulator source
3259 * @mode: operating mode - one of the REGULATOR_MODE constants
3261 * Set regulator operating mode to increase regulator efficiency or improve
3262 * regulation performance.
3264 * NOTE: Regulator system constraints must be set for this regulator before
3265 * calling this function otherwise this call will fail.
3267 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
3269 struct regulator_dev
*rdev
= regulator
->rdev
;
3271 int regulator_curr_mode
;
3273 mutex_lock(&rdev
->mutex
);
3276 if (!rdev
->desc
->ops
->set_mode
) {
3281 /* return if the same mode is requested */
3282 if (rdev
->desc
->ops
->get_mode
) {
3283 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
3284 if (regulator_curr_mode
== mode
) {
3290 /* constraints check */
3291 ret
= regulator_mode_constrain(rdev
, &mode
);
3295 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
3297 mutex_unlock(&rdev
->mutex
);
3300 EXPORT_SYMBOL_GPL(regulator_set_mode
);
3302 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
3306 mutex_lock(&rdev
->mutex
);
3309 if (!rdev
->desc
->ops
->get_mode
) {
3314 ret
= rdev
->desc
->ops
->get_mode(rdev
);
3316 mutex_unlock(&rdev
->mutex
);
3321 * regulator_get_mode - get regulator operating mode
3322 * @regulator: regulator source
3324 * Get the current regulator operating mode.
3326 unsigned int regulator_get_mode(struct regulator
*regulator
)
3328 return _regulator_get_mode(regulator
->rdev
);
3330 EXPORT_SYMBOL_GPL(regulator_get_mode
);
3333 * regulator_set_load - set regulator load
3334 * @regulator: regulator source
3335 * @uA_load: load current
3337 * Notifies the regulator core of a new device load. This is then used by
3338 * DRMS (if enabled by constraints) to set the most efficient regulator
3339 * operating mode for the new regulator loading.
3341 * Consumer devices notify their supply regulator of the maximum power
3342 * they will require (can be taken from device datasheet in the power
3343 * consumption tables) when they change operational status and hence power
3344 * state. Examples of operational state changes that can affect power
3345 * consumption are :-
3347 * o Device is opened / closed.
3348 * o Device I/O is about to begin or has just finished.
3349 * o Device is idling in between work.
3351 * This information is also exported via sysfs to userspace.
3353 * DRMS will sum the total requested load on the regulator and change
3354 * to the most efficient operating mode if platform constraints allow.
3356 * On error a negative errno is returned.
3358 int regulator_set_load(struct regulator
*regulator
, int uA_load
)
3360 struct regulator_dev
*rdev
= regulator
->rdev
;
3363 mutex_lock(&rdev
->mutex
);
3364 regulator
->uA_load
= uA_load
;
3365 ret
= drms_uA_update(rdev
);
3366 mutex_unlock(&rdev
->mutex
);
3370 EXPORT_SYMBOL_GPL(regulator_set_load
);
3373 * regulator_allow_bypass - allow the regulator to go into bypass mode
3375 * @regulator: Regulator to configure
3376 * @enable: enable or disable bypass mode
3378 * Allow the regulator to go into bypass mode if all other consumers
3379 * for the regulator also enable bypass mode and the machine
3380 * constraints allow this. Bypass mode means that the regulator is
3381 * simply passing the input directly to the output with no regulation.
3383 int regulator_allow_bypass(struct regulator
*regulator
, bool enable
)
3385 struct regulator_dev
*rdev
= regulator
->rdev
;
3388 if (!rdev
->desc
->ops
->set_bypass
)
3391 if (rdev
->constraints
&&
3392 !(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_BYPASS
))
3395 mutex_lock(&rdev
->mutex
);
3397 if (enable
&& !regulator
->bypass
) {
3398 rdev
->bypass_count
++;
3400 if (rdev
->bypass_count
== rdev
->open_count
) {
3401 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3403 rdev
->bypass_count
--;
3406 } else if (!enable
&& regulator
->bypass
) {
3407 rdev
->bypass_count
--;
3409 if (rdev
->bypass_count
!= rdev
->open_count
) {
3410 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3412 rdev
->bypass_count
++;
3417 regulator
->bypass
= enable
;
3419 mutex_unlock(&rdev
->mutex
);
3423 EXPORT_SYMBOL_GPL(regulator_allow_bypass
);
3426 * regulator_register_notifier - register regulator event notifier
3427 * @regulator: regulator source
3428 * @nb: notifier block
3430 * Register notifier block to receive regulator events.
3432 int regulator_register_notifier(struct regulator
*regulator
,
3433 struct notifier_block
*nb
)
3435 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
3438 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
3441 * regulator_unregister_notifier - unregister regulator event notifier
3442 * @regulator: regulator source
3443 * @nb: notifier block
3445 * Unregister regulator event notifier block.
3447 int regulator_unregister_notifier(struct regulator
*regulator
,
3448 struct notifier_block
*nb
)
3450 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
3453 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
3455 /* notify regulator consumers and downstream regulator consumers.
3456 * Note mutex must be held by caller.
3458 static int _notifier_call_chain(struct regulator_dev
*rdev
,
3459 unsigned long event
, void *data
)
3461 /* call rdev chain first */
3462 return blocking_notifier_call_chain(&rdev
->notifier
, event
, data
);
3466 * regulator_bulk_get - get multiple regulator consumers
3468 * @dev: Device to supply
3469 * @num_consumers: Number of consumers to register
3470 * @consumers: Configuration of consumers; clients are stored here.
3472 * @return 0 on success, an errno on failure.
3474 * This helper function allows drivers to get several regulator
3475 * consumers in one operation. If any of the regulators cannot be
3476 * acquired then any regulators that were allocated will be freed
3477 * before returning to the caller.
3479 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
3480 struct regulator_bulk_data
*consumers
)
3485 for (i
= 0; i
< num_consumers
; i
++)
3486 consumers
[i
].consumer
= NULL
;
3488 for (i
= 0; i
< num_consumers
; i
++) {
3489 consumers
[i
].consumer
= _regulator_get(dev
,
3490 consumers
[i
].supply
,
3492 !consumers
[i
].optional
);
3493 if (IS_ERR(consumers
[i
].consumer
)) {
3494 ret
= PTR_ERR(consumers
[i
].consumer
);
3495 dev_err(dev
, "Failed to get supply '%s': %d\n",
3496 consumers
[i
].supply
, ret
);
3497 consumers
[i
].consumer
= NULL
;
3506 regulator_put(consumers
[i
].consumer
);
3510 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
3512 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
3514 struct regulator_bulk_data
*bulk
= data
;
3516 bulk
->ret
= regulator_enable(bulk
->consumer
);
3520 * regulator_bulk_enable - enable multiple regulator consumers
3522 * @num_consumers: Number of consumers
3523 * @consumers: Consumer data; clients are stored here.
3524 * @return 0 on success, an errno on failure
3526 * This convenience API allows consumers to enable multiple regulator
3527 * clients in a single API call. If any consumers cannot be enabled
3528 * then any others that were enabled will be disabled again prior to
3531 int regulator_bulk_enable(int num_consumers
,
3532 struct regulator_bulk_data
*consumers
)
3534 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
3538 for (i
= 0; i
< num_consumers
; i
++) {
3539 if (consumers
[i
].consumer
->always_on
)
3540 consumers
[i
].ret
= 0;
3542 async_schedule_domain(regulator_bulk_enable_async
,
3543 &consumers
[i
], &async_domain
);
3546 async_synchronize_full_domain(&async_domain
);
3548 /* If any consumer failed we need to unwind any that succeeded */
3549 for (i
= 0; i
< num_consumers
; i
++) {
3550 if (consumers
[i
].ret
!= 0) {
3551 ret
= consumers
[i
].ret
;
3559 for (i
= 0; i
< num_consumers
; i
++) {
3560 if (consumers
[i
].ret
< 0)
3561 pr_err("Failed to enable %s: %d\n", consumers
[i
].supply
,
3564 regulator_disable(consumers
[i
].consumer
);
3569 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
3572 * regulator_bulk_disable - disable multiple regulator consumers
3574 * @num_consumers: Number of consumers
3575 * @consumers: Consumer data; clients are stored here.
3576 * @return 0 on success, an errno on failure
3578 * This convenience API allows consumers to disable multiple regulator
3579 * clients in a single API call. If any consumers cannot be disabled
3580 * then any others that were disabled will be enabled again prior to
3583 int regulator_bulk_disable(int num_consumers
,
3584 struct regulator_bulk_data
*consumers
)
3589 for (i
= num_consumers
- 1; i
>= 0; --i
) {
3590 ret
= regulator_disable(consumers
[i
].consumer
);
3598 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
3599 for (++i
; i
< num_consumers
; ++i
) {
3600 r
= regulator_enable(consumers
[i
].consumer
);
3602 pr_err("Failed to reename %s: %d\n",
3603 consumers
[i
].supply
, r
);
3608 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
3611 * regulator_bulk_force_disable - force disable multiple regulator consumers
3613 * @num_consumers: Number of consumers
3614 * @consumers: Consumer data; clients are stored here.
3615 * @return 0 on success, an errno on failure
3617 * This convenience API allows consumers to forcibly disable multiple regulator
3618 * clients in a single API call.
3619 * NOTE: This should be used for situations when device damage will
3620 * likely occur if the regulators are not disabled (e.g. over temp).
3621 * Although regulator_force_disable function call for some consumers can
3622 * return error numbers, the function is called for all consumers.
3624 int regulator_bulk_force_disable(int num_consumers
,
3625 struct regulator_bulk_data
*consumers
)
3630 for (i
= 0; i
< num_consumers
; i
++)
3632 regulator_force_disable(consumers
[i
].consumer
);
3634 for (i
= 0; i
< num_consumers
; i
++) {
3635 if (consumers
[i
].ret
!= 0) {
3636 ret
= consumers
[i
].ret
;
3645 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
3648 * regulator_bulk_free - free multiple regulator consumers
3650 * @num_consumers: Number of consumers
3651 * @consumers: Consumer data; clients are stored here.
3653 * This convenience API allows consumers to free multiple regulator
3654 * clients in a single API call.
3656 void regulator_bulk_free(int num_consumers
,
3657 struct regulator_bulk_data
*consumers
)
3661 for (i
= 0; i
< num_consumers
; i
++) {
3662 regulator_put(consumers
[i
].consumer
);
3663 consumers
[i
].consumer
= NULL
;
3666 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
3669 * regulator_notifier_call_chain - call regulator event notifier
3670 * @rdev: regulator source
3671 * @event: notifier block
3672 * @data: callback-specific data.
3674 * Called by regulator drivers to notify clients a regulator event has
3675 * occurred. We also notify regulator clients downstream.
3676 * Note lock must be held by caller.
3678 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
3679 unsigned long event
, void *data
)
3681 lockdep_assert_held_once(&rdev
->mutex
);
3683 _notifier_call_chain(rdev
, event
, data
);
3687 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
3690 * regulator_mode_to_status - convert a regulator mode into a status
3692 * @mode: Mode to convert
3694 * Convert a regulator mode into a status.
3696 int regulator_mode_to_status(unsigned int mode
)
3699 case REGULATOR_MODE_FAST
:
3700 return REGULATOR_STATUS_FAST
;
3701 case REGULATOR_MODE_NORMAL
:
3702 return REGULATOR_STATUS_NORMAL
;
3703 case REGULATOR_MODE_IDLE
:
3704 return REGULATOR_STATUS_IDLE
;
3705 case REGULATOR_MODE_STANDBY
:
3706 return REGULATOR_STATUS_STANDBY
;
3708 return REGULATOR_STATUS_UNDEFINED
;
3711 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
3713 static struct attribute
*regulator_dev_attrs
[] = {
3714 &dev_attr_name
.attr
,
3715 &dev_attr_num_users
.attr
,
3716 &dev_attr_type
.attr
,
3717 &dev_attr_microvolts
.attr
,
3718 &dev_attr_microamps
.attr
,
3719 &dev_attr_opmode
.attr
,
3720 &dev_attr_state
.attr
,
3721 &dev_attr_status
.attr
,
3722 &dev_attr_bypass
.attr
,
3723 &dev_attr_requested_microamps
.attr
,
3724 &dev_attr_min_microvolts
.attr
,
3725 &dev_attr_max_microvolts
.attr
,
3726 &dev_attr_min_microamps
.attr
,
3727 &dev_attr_max_microamps
.attr
,
3728 &dev_attr_suspend_standby_state
.attr
,
3729 &dev_attr_suspend_mem_state
.attr
,
3730 &dev_attr_suspend_disk_state
.attr
,
3731 &dev_attr_suspend_standby_microvolts
.attr
,
3732 &dev_attr_suspend_mem_microvolts
.attr
,
3733 &dev_attr_suspend_disk_microvolts
.attr
,
3734 &dev_attr_suspend_standby_mode
.attr
,
3735 &dev_attr_suspend_mem_mode
.attr
,
3736 &dev_attr_suspend_disk_mode
.attr
,
3741 * To avoid cluttering sysfs (and memory) with useless state, only
3742 * create attributes that can be meaningfully displayed.
3744 static umode_t
regulator_attr_is_visible(struct kobject
*kobj
,
3745 struct attribute
*attr
, int idx
)
3747 struct device
*dev
= kobj_to_dev(kobj
);
3748 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
3749 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3750 umode_t mode
= attr
->mode
;
3752 /* these three are always present */
3753 if (attr
== &dev_attr_name
.attr
||
3754 attr
== &dev_attr_num_users
.attr
||
3755 attr
== &dev_attr_type
.attr
)
3758 /* some attributes need specific methods to be displayed */
3759 if (attr
== &dev_attr_microvolts
.attr
) {
3760 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
3761 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0) ||
3762 (ops
->list_voltage
&& ops
->list_voltage(rdev
, 0) >= 0) ||
3763 (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1))
3768 if (attr
== &dev_attr_microamps
.attr
)
3769 return ops
->get_current_limit
? mode
: 0;
3771 if (attr
== &dev_attr_opmode
.attr
)
3772 return ops
->get_mode
? mode
: 0;
3774 if (attr
== &dev_attr_state
.attr
)
3775 return (rdev
->ena_pin
|| ops
->is_enabled
) ? mode
: 0;
3777 if (attr
== &dev_attr_status
.attr
)
3778 return ops
->get_status
? mode
: 0;
3780 if (attr
== &dev_attr_bypass
.attr
)
3781 return ops
->get_bypass
? mode
: 0;
3783 /* some attributes are type-specific */
3784 if (attr
== &dev_attr_requested_microamps
.attr
)
3785 return rdev
->desc
->type
== REGULATOR_CURRENT
? mode
: 0;
3787 /* constraints need specific supporting methods */
3788 if (attr
== &dev_attr_min_microvolts
.attr
||
3789 attr
== &dev_attr_max_microvolts
.attr
)
3790 return (ops
->set_voltage
|| ops
->set_voltage_sel
) ? mode
: 0;
3792 if (attr
== &dev_attr_min_microamps
.attr
||
3793 attr
== &dev_attr_max_microamps
.attr
)
3794 return ops
->set_current_limit
? mode
: 0;
3796 if (attr
== &dev_attr_suspend_standby_state
.attr
||
3797 attr
== &dev_attr_suspend_mem_state
.attr
||
3798 attr
== &dev_attr_suspend_disk_state
.attr
)
3801 if (attr
== &dev_attr_suspend_standby_microvolts
.attr
||
3802 attr
== &dev_attr_suspend_mem_microvolts
.attr
||
3803 attr
== &dev_attr_suspend_disk_microvolts
.attr
)
3804 return ops
->set_suspend_voltage
? mode
: 0;
3806 if (attr
== &dev_attr_suspend_standby_mode
.attr
||
3807 attr
== &dev_attr_suspend_mem_mode
.attr
||
3808 attr
== &dev_attr_suspend_disk_mode
.attr
)
3809 return ops
->set_suspend_mode
? mode
: 0;
3814 static const struct attribute_group regulator_dev_group
= {
3815 .attrs
= regulator_dev_attrs
,
3816 .is_visible
= regulator_attr_is_visible
,
3819 static const struct attribute_group
*regulator_dev_groups
[] = {
3820 ®ulator_dev_group
,
3824 static void regulator_dev_release(struct device
*dev
)
3826 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
3828 kfree(rdev
->constraints
);
3829 of_node_put(rdev
->dev
.of_node
);
3833 static struct class regulator_class
= {
3834 .name
= "regulator",
3835 .dev_release
= regulator_dev_release
,
3836 .dev_groups
= regulator_dev_groups
,
3839 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
3841 struct device
*parent
= rdev
->dev
.parent
;
3842 const char *rname
= rdev_get_name(rdev
);
3843 char name
[NAME_MAX
];
3845 /* Avoid duplicate debugfs directory names */
3846 if (parent
&& rname
== rdev
->desc
->name
) {
3847 snprintf(name
, sizeof(name
), "%s-%s", dev_name(parent
),
3852 rdev
->debugfs
= debugfs_create_dir(rname
, debugfs_root
);
3853 if (!rdev
->debugfs
) {
3854 rdev_warn(rdev
, "Failed to create debugfs directory\n");
3858 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
3860 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
3862 debugfs_create_u32("bypass_count", 0444, rdev
->debugfs
,
3863 &rdev
->bypass_count
);
3866 static int regulator_register_resolve_supply(struct device
*dev
, void *data
)
3868 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
3870 if (regulator_resolve_supply(rdev
))
3871 rdev_dbg(rdev
, "unable to resolve supply\n");
3877 * regulator_register - register regulator
3878 * @regulator_desc: regulator to register
3879 * @cfg: runtime configuration for regulator
3881 * Called by regulator drivers to register a regulator.
3882 * Returns a valid pointer to struct regulator_dev on success
3883 * or an ERR_PTR() on error.
3885 struct regulator_dev
*
3886 regulator_register(const struct regulator_desc
*regulator_desc
,
3887 const struct regulator_config
*cfg
)
3889 const struct regulation_constraints
*constraints
= NULL
;
3890 const struct regulator_init_data
*init_data
;
3891 struct regulator_config
*config
= NULL
;
3892 static atomic_t regulator_no
= ATOMIC_INIT(-1);
3893 struct regulator_dev
*rdev
;
3897 if (regulator_desc
== NULL
|| cfg
== NULL
)
3898 return ERR_PTR(-EINVAL
);
3903 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
3904 return ERR_PTR(-EINVAL
);
3906 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
3907 regulator_desc
->type
!= REGULATOR_CURRENT
)
3908 return ERR_PTR(-EINVAL
);
3910 /* Only one of each should be implemented */
3911 WARN_ON(regulator_desc
->ops
->get_voltage
&&
3912 regulator_desc
->ops
->get_voltage_sel
);
3913 WARN_ON(regulator_desc
->ops
->set_voltage
&&
3914 regulator_desc
->ops
->set_voltage_sel
);
3916 /* If we're using selectors we must implement list_voltage. */
3917 if (regulator_desc
->ops
->get_voltage_sel
&&
3918 !regulator_desc
->ops
->list_voltage
) {
3919 return ERR_PTR(-EINVAL
);
3921 if (regulator_desc
->ops
->set_voltage_sel
&&
3922 !regulator_desc
->ops
->list_voltage
) {
3923 return ERR_PTR(-EINVAL
);
3926 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
3928 return ERR_PTR(-ENOMEM
);
3931 * Duplicate the config so the driver could override it after
3932 * parsing init data.
3934 config
= kmemdup(cfg
, sizeof(*cfg
), GFP_KERNEL
);
3935 if (config
== NULL
) {
3937 return ERR_PTR(-ENOMEM
);
3940 init_data
= regulator_of_get_init_data(dev
, regulator_desc
, config
,
3941 &rdev
->dev
.of_node
);
3943 init_data
= config
->init_data
;
3944 rdev
->dev
.of_node
= of_node_get(config
->of_node
);
3947 mutex_init(&rdev
->mutex
);
3948 rdev
->reg_data
= config
->driver_data
;
3949 rdev
->owner
= regulator_desc
->owner
;
3950 rdev
->desc
= regulator_desc
;
3952 rdev
->regmap
= config
->regmap
;
3953 else if (dev_get_regmap(dev
, NULL
))
3954 rdev
->regmap
= dev_get_regmap(dev
, NULL
);
3955 else if (dev
->parent
)
3956 rdev
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
3957 INIT_LIST_HEAD(&rdev
->consumer_list
);
3958 INIT_LIST_HEAD(&rdev
->list
);
3959 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
3960 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
3962 /* preform any regulator specific init */
3963 if (init_data
&& init_data
->regulator_init
) {
3964 ret
= init_data
->regulator_init(rdev
->reg_data
);
3969 if ((config
->ena_gpio
|| config
->ena_gpio_initialized
) &&
3970 gpio_is_valid(config
->ena_gpio
)) {
3971 mutex_lock(®ulator_list_mutex
);
3972 ret
= regulator_ena_gpio_request(rdev
, config
);
3973 mutex_unlock(®ulator_list_mutex
);
3975 rdev_err(rdev
, "Failed to request enable GPIO%d: %d\n",
3976 config
->ena_gpio
, ret
);
3981 /* register with sysfs */
3982 rdev
->dev
.class = ®ulator_class
;
3983 rdev
->dev
.parent
= dev
;
3984 dev_set_name(&rdev
->dev
, "regulator.%lu",
3985 (unsigned long) atomic_inc_return(®ulator_no
));
3987 /* set regulator constraints */
3989 constraints
= &init_data
->constraints
;
3991 if (init_data
&& init_data
->supply_regulator
)
3992 rdev
->supply_name
= init_data
->supply_regulator
;
3993 else if (regulator_desc
->supply_name
)
3994 rdev
->supply_name
= regulator_desc
->supply_name
;
3997 * Attempt to resolve the regulator supply, if specified,
3998 * but don't return an error if we fail because we will try
3999 * to resolve it again later as more regulators are added.
4001 if (regulator_resolve_supply(rdev
))
4002 rdev_dbg(rdev
, "unable to resolve supply\n");
4004 ret
= set_machine_constraints(rdev
, constraints
);
4008 /* add consumers devices */
4010 mutex_lock(®ulator_list_mutex
);
4011 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
4012 ret
= set_consumer_device_supply(rdev
,
4013 init_data
->consumer_supplies
[i
].dev_name
,
4014 init_data
->consumer_supplies
[i
].supply
);
4016 mutex_unlock(®ulator_list_mutex
);
4017 dev_err(dev
, "Failed to set supply %s\n",
4018 init_data
->consumer_supplies
[i
].supply
);
4019 goto unset_supplies
;
4022 mutex_unlock(®ulator_list_mutex
);
4025 ret
= device_register(&rdev
->dev
);
4027 put_device(&rdev
->dev
);
4028 goto unset_supplies
;
4031 dev_set_drvdata(&rdev
->dev
, rdev
);
4032 rdev_init_debugfs(rdev
);
4034 /* try to resolve regulators supply since a new one was registered */
4035 class_for_each_device(®ulator_class
, NULL
, NULL
,
4036 regulator_register_resolve_supply
);
4041 mutex_lock(®ulator_list_mutex
);
4042 unset_regulator_supplies(rdev
);
4043 mutex_unlock(®ulator_list_mutex
);
4045 kfree(rdev
->constraints
);
4046 mutex_lock(®ulator_list_mutex
);
4047 regulator_ena_gpio_free(rdev
);
4048 mutex_unlock(®ulator_list_mutex
);
4052 return ERR_PTR(ret
);
4054 EXPORT_SYMBOL_GPL(regulator_register
);
4057 * regulator_unregister - unregister regulator
4058 * @rdev: regulator to unregister
4060 * Called by regulator drivers to unregister a regulator.
4062 void regulator_unregister(struct regulator_dev
*rdev
)
4068 while (rdev
->use_count
--)
4069 regulator_disable(rdev
->supply
);
4070 regulator_put(rdev
->supply
);
4072 mutex_lock(®ulator_list_mutex
);
4073 debugfs_remove_recursive(rdev
->debugfs
);
4074 flush_work(&rdev
->disable_work
.work
);
4075 WARN_ON(rdev
->open_count
);
4076 unset_regulator_supplies(rdev
);
4077 list_del(&rdev
->list
);
4078 regulator_ena_gpio_free(rdev
);
4079 mutex_unlock(®ulator_list_mutex
);
4080 device_unregister(&rdev
->dev
);
4082 EXPORT_SYMBOL_GPL(regulator_unregister
);
4084 static int _regulator_suspend_prepare(struct device
*dev
, void *data
)
4086 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4087 const suspend_state_t
*state
= data
;
4090 mutex_lock(&rdev
->mutex
);
4091 ret
= suspend_prepare(rdev
, *state
);
4092 mutex_unlock(&rdev
->mutex
);
4098 * regulator_suspend_prepare - prepare regulators for system wide suspend
4099 * @state: system suspend state
4101 * Configure each regulator with it's suspend operating parameters for state.
4102 * This will usually be called by machine suspend code prior to supending.
4104 int regulator_suspend_prepare(suspend_state_t state
)
4106 /* ON is handled by regulator active state */
4107 if (state
== PM_SUSPEND_ON
)
4110 return class_for_each_device(®ulator_class
, NULL
, &state
,
4111 _regulator_suspend_prepare
);
4113 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
4115 static int _regulator_suspend_finish(struct device
*dev
, void *data
)
4117 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4120 mutex_lock(&rdev
->mutex
);
4121 if (rdev
->use_count
> 0 || rdev
->constraints
->always_on
) {
4122 if (!_regulator_is_enabled(rdev
)) {
4123 ret
= _regulator_do_enable(rdev
);
4126 "Failed to resume regulator %d\n",
4130 if (!have_full_constraints())
4132 if (!_regulator_is_enabled(rdev
))
4135 ret
= _regulator_do_disable(rdev
);
4137 dev_err(dev
, "Failed to suspend regulator %d\n", ret
);
4140 mutex_unlock(&rdev
->mutex
);
4142 /* Keep processing regulators in spite of any errors */
4147 * regulator_suspend_finish - resume regulators from system wide suspend
4149 * Turn on regulators that might be turned off by regulator_suspend_prepare
4150 * and that should be turned on according to the regulators properties.
4152 int regulator_suspend_finish(void)
4154 return class_for_each_device(®ulator_class
, NULL
, NULL
,
4155 _regulator_suspend_finish
);
4157 EXPORT_SYMBOL_GPL(regulator_suspend_finish
);
4160 * regulator_has_full_constraints - the system has fully specified constraints
4162 * Calling this function will cause the regulator API to disable all
4163 * regulators which have a zero use count and don't have an always_on
4164 * constraint in a late_initcall.
4166 * The intention is that this will become the default behaviour in a
4167 * future kernel release so users are encouraged to use this facility
4170 void regulator_has_full_constraints(void)
4172 has_full_constraints
= 1;
4174 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
4177 * rdev_get_drvdata - get rdev regulator driver data
4180 * Get rdev regulator driver private data. This call can be used in the
4181 * regulator driver context.
4183 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
4185 return rdev
->reg_data
;
4187 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
4190 * regulator_get_drvdata - get regulator driver data
4191 * @regulator: regulator
4193 * Get regulator driver private data. This call can be used in the consumer
4194 * driver context when non API regulator specific functions need to be called.
4196 void *regulator_get_drvdata(struct regulator
*regulator
)
4198 return regulator
->rdev
->reg_data
;
4200 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
4203 * regulator_set_drvdata - set regulator driver data
4204 * @regulator: regulator
4207 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
4209 regulator
->rdev
->reg_data
= data
;
4211 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
4214 * regulator_get_id - get regulator ID
4217 int rdev_get_id(struct regulator_dev
*rdev
)
4219 return rdev
->desc
->id
;
4221 EXPORT_SYMBOL_GPL(rdev_get_id
);
4223 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
4227 EXPORT_SYMBOL_GPL(rdev_get_dev
);
4229 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
4231 return reg_init_data
->driver_data
;
4233 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
4235 #ifdef CONFIG_DEBUG_FS
4236 static ssize_t
supply_map_read_file(struct file
*file
, char __user
*user_buf
,
4237 size_t count
, loff_t
*ppos
)
4239 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4240 ssize_t len
, ret
= 0;
4241 struct regulator_map
*map
;
4246 list_for_each_entry(map
, ®ulator_map_list
, list
) {
4247 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
4249 rdev_get_name(map
->regulator
), map
->dev_name
,
4253 if (ret
> PAGE_SIZE
) {
4259 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
4267 static const struct file_operations supply_map_fops
= {
4268 #ifdef CONFIG_DEBUG_FS
4269 .read
= supply_map_read_file
,
4270 .llseek
= default_llseek
,
4274 #ifdef CONFIG_DEBUG_FS
4275 struct summary_data
{
4277 struct regulator_dev
*parent
;
4281 static void regulator_summary_show_subtree(struct seq_file
*s
,
4282 struct regulator_dev
*rdev
,
4285 static int regulator_summary_show_children(struct device
*dev
, void *data
)
4287 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4288 struct summary_data
*summary_data
= data
;
4290 if (rdev
->supply
&& rdev
->supply
->rdev
== summary_data
->parent
)
4291 regulator_summary_show_subtree(summary_data
->s
, rdev
,
4292 summary_data
->level
+ 1);
4297 static void regulator_summary_show_subtree(struct seq_file
*s
,
4298 struct regulator_dev
*rdev
,
4301 struct regulation_constraints
*c
;
4302 struct regulator
*consumer
;
4303 struct summary_data summary_data
;
4308 seq_printf(s
, "%*s%-*s %3d %4d %6d ",
4310 30 - level
* 3, rdev_get_name(rdev
),
4311 rdev
->use_count
, rdev
->open_count
, rdev
->bypass_count
);
4313 seq_printf(s
, "%5dmV ", _regulator_get_voltage(rdev
) / 1000);
4314 seq_printf(s
, "%5dmA ", _regulator_get_current_limit(rdev
) / 1000);
4316 c
= rdev
->constraints
;
4318 switch (rdev
->desc
->type
) {
4319 case REGULATOR_VOLTAGE
:
4320 seq_printf(s
, "%5dmV %5dmV ",
4321 c
->min_uV
/ 1000, c
->max_uV
/ 1000);
4323 case REGULATOR_CURRENT
:
4324 seq_printf(s
, "%5dmA %5dmA ",
4325 c
->min_uA
/ 1000, c
->max_uA
/ 1000);
4332 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
) {
4333 if (consumer
->dev
->class == ®ulator_class
)
4336 seq_printf(s
, "%*s%-*s ",
4337 (level
+ 1) * 3 + 1, "",
4338 30 - (level
+ 1) * 3, dev_name(consumer
->dev
));
4340 switch (rdev
->desc
->type
) {
4341 case REGULATOR_VOLTAGE
:
4342 seq_printf(s
, "%37dmV %5dmV",
4343 consumer
->min_uV
/ 1000,
4344 consumer
->max_uV
/ 1000);
4346 case REGULATOR_CURRENT
:
4354 summary_data
.level
= level
;
4355 summary_data
.parent
= rdev
;
4357 class_for_each_device(®ulator_class
, NULL
, &summary_data
,
4358 regulator_summary_show_children
);
4361 static int regulator_summary_show_roots(struct device
*dev
, void *data
)
4363 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4364 struct seq_file
*s
= data
;
4367 regulator_summary_show_subtree(s
, rdev
, 0);
4372 static int regulator_summary_show(struct seq_file
*s
, void *data
)
4374 seq_puts(s
, " regulator use open bypass voltage current min max\n");
4375 seq_puts(s
, "-------------------------------------------------------------------------------\n");
4377 class_for_each_device(®ulator_class
, NULL
, s
,
4378 regulator_summary_show_roots
);
4383 static int regulator_summary_open(struct inode
*inode
, struct file
*file
)
4385 return single_open(file
, regulator_summary_show
, inode
->i_private
);
4389 static const struct file_operations regulator_summary_fops
= {
4390 #ifdef CONFIG_DEBUG_FS
4391 .open
= regulator_summary_open
,
4393 .llseek
= seq_lseek
,
4394 .release
= single_release
,
4398 static int __init
regulator_init(void)
4402 ret
= class_register(®ulator_class
);
4404 debugfs_root
= debugfs_create_dir("regulator", NULL
);
4406 pr_warn("regulator: Failed to create debugfs directory\n");
4408 debugfs_create_file("supply_map", 0444, debugfs_root
, NULL
,
4411 debugfs_create_file("regulator_summary", 0444, debugfs_root
,
4412 NULL
, ®ulator_summary_fops
);
4414 regulator_dummy_init();
4419 /* init early to allow our consumers to complete system booting */
4420 core_initcall(regulator_init
);
4422 static int __init
regulator_late_cleanup(struct device
*dev
, void *data
)
4424 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4425 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
4426 struct regulation_constraints
*c
= rdev
->constraints
;
4429 if (c
&& c
->always_on
)
4432 if (c
&& !(c
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
))
4435 mutex_lock(&rdev
->mutex
);
4437 if (rdev
->use_count
)
4440 /* If we can't read the status assume it's on. */
4441 if (ops
->is_enabled
)
4442 enabled
= ops
->is_enabled(rdev
);
4449 if (have_full_constraints()) {
4450 /* We log since this may kill the system if it goes
4452 rdev_info(rdev
, "disabling\n");
4453 ret
= _regulator_do_disable(rdev
);
4455 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
4457 /* The intention is that in future we will
4458 * assume that full constraints are provided
4459 * so warn even if we aren't going to do
4462 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
4466 mutex_unlock(&rdev
->mutex
);
4471 static int __init
regulator_init_complete(void)
4474 * Since DT doesn't provide an idiomatic mechanism for
4475 * enabling full constraints and since it's much more natural
4476 * with DT to provide them just assume that a DT enabled
4477 * system has full constraints.
4479 if (of_have_populated_dt())
4480 has_full_constraints
= true;
4482 /* If we have a full configuration then disable any regulators
4483 * we have permission to change the status for and which are
4484 * not in use or always_on. This is effectively the default
4485 * for DT and ACPI as they have full constraints.
4487 class_for_each_device(®ulator_class
, NULL
, NULL
,
4488 regulator_late_cleanup
);
4492 late_initcall_sync(regulator_init_complete
);