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 bool regulator_ops_is_valid(struct regulator_dev
*rdev
, int ops
)
137 if (!rdev
->constraints
) {
138 rdev_err(rdev
, "no constraints\n");
142 if (rdev
->constraints
->valid_ops_mask
& ops
)
148 static inline struct regulator_dev
*rdev_get_supply(struct regulator_dev
*rdev
)
150 if (rdev
&& rdev
->supply
)
151 return rdev
->supply
->rdev
;
157 * regulator_lock_supply - lock a regulator and its supplies
158 * @rdev: regulator source
160 static void regulator_lock_supply(struct regulator_dev
*rdev
)
164 for (i
= 0; rdev
; rdev
= rdev_get_supply(rdev
), i
++)
165 mutex_lock_nested(&rdev
->mutex
, i
);
169 * regulator_unlock_supply - unlock a regulator and its supplies
170 * @rdev: regulator source
172 static void regulator_unlock_supply(struct regulator_dev
*rdev
)
174 struct regulator
*supply
;
177 mutex_unlock(&rdev
->mutex
);
178 supply
= rdev
->supply
;
188 * of_get_regulator - get a regulator device node based on supply name
189 * @dev: Device pointer for the consumer (of regulator) device
190 * @supply: regulator supply name
192 * Extract the regulator device node corresponding to the supply name.
193 * returns the device node corresponding to the regulator if found, else
196 static struct device_node
*of_get_regulator(struct device
*dev
, const char *supply
)
198 struct device_node
*regnode
= NULL
;
199 char prop_name
[32]; /* 32 is max size of property name */
201 dev_dbg(dev
, "Looking up %s-supply from device tree\n", supply
);
203 snprintf(prop_name
, 32, "%s-supply", supply
);
204 regnode
= of_parse_phandle(dev
->of_node
, prop_name
, 0);
207 dev_dbg(dev
, "Looking up %s property in node %s failed",
208 prop_name
, dev
->of_node
->full_name
);
214 /* Platform voltage constraint check */
215 static int regulator_check_voltage(struct regulator_dev
*rdev
,
216 int *min_uV
, int *max_uV
)
218 BUG_ON(*min_uV
> *max_uV
);
220 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
221 rdev_err(rdev
, "voltage operation not allowed\n");
225 if (*max_uV
> rdev
->constraints
->max_uV
)
226 *max_uV
= rdev
->constraints
->max_uV
;
227 if (*min_uV
< rdev
->constraints
->min_uV
)
228 *min_uV
= rdev
->constraints
->min_uV
;
230 if (*min_uV
> *max_uV
) {
231 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
239 /* Make sure we select a voltage that suits the needs of all
240 * regulator consumers
242 static int regulator_check_consumers(struct regulator_dev
*rdev
,
243 int *min_uV
, int *max_uV
)
245 struct regulator
*regulator
;
247 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
249 * Assume consumers that didn't say anything are OK
250 * with anything in the constraint range.
252 if (!regulator
->min_uV
&& !regulator
->max_uV
)
255 if (*max_uV
> regulator
->max_uV
)
256 *max_uV
= regulator
->max_uV
;
257 if (*min_uV
< regulator
->min_uV
)
258 *min_uV
= regulator
->min_uV
;
261 if (*min_uV
> *max_uV
) {
262 rdev_err(rdev
, "Restricting voltage, %u-%uuV\n",
270 /* current constraint check */
271 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
272 int *min_uA
, int *max_uA
)
274 BUG_ON(*min_uA
> *max_uA
);
276 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_CURRENT
)) {
277 rdev_err(rdev
, "current operation not allowed\n");
281 if (*max_uA
> rdev
->constraints
->max_uA
)
282 *max_uA
= rdev
->constraints
->max_uA
;
283 if (*min_uA
< rdev
->constraints
->min_uA
)
284 *min_uA
= rdev
->constraints
->min_uA
;
286 if (*min_uA
> *max_uA
) {
287 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
295 /* operating mode constraint check */
296 static int regulator_mode_constrain(struct regulator_dev
*rdev
, int *mode
)
299 case REGULATOR_MODE_FAST
:
300 case REGULATOR_MODE_NORMAL
:
301 case REGULATOR_MODE_IDLE
:
302 case REGULATOR_MODE_STANDBY
:
305 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
309 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_MODE
)) {
310 rdev_err(rdev
, "mode operation not allowed\n");
314 /* The modes are bitmasks, the most power hungry modes having
315 * the lowest values. If the requested mode isn't supported
316 * try higher modes. */
318 if (rdev
->constraints
->valid_modes_mask
& *mode
)
326 static ssize_t
regulator_uV_show(struct device
*dev
,
327 struct device_attribute
*attr
, char *buf
)
329 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
332 mutex_lock(&rdev
->mutex
);
333 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
334 mutex_unlock(&rdev
->mutex
);
338 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
340 static ssize_t
regulator_uA_show(struct device
*dev
,
341 struct device_attribute
*attr
, char *buf
)
343 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
345 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
347 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
349 static ssize_t
name_show(struct device
*dev
, struct device_attribute
*attr
,
352 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
354 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
356 static DEVICE_ATTR_RO(name
);
358 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
361 case REGULATOR_MODE_FAST
:
362 return sprintf(buf
, "fast\n");
363 case REGULATOR_MODE_NORMAL
:
364 return sprintf(buf
, "normal\n");
365 case REGULATOR_MODE_IDLE
:
366 return sprintf(buf
, "idle\n");
367 case REGULATOR_MODE_STANDBY
:
368 return sprintf(buf
, "standby\n");
370 return sprintf(buf
, "unknown\n");
373 static ssize_t
regulator_opmode_show(struct device
*dev
,
374 struct device_attribute
*attr
, char *buf
)
376 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
378 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
380 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
382 static ssize_t
regulator_print_state(char *buf
, int state
)
385 return sprintf(buf
, "enabled\n");
387 return sprintf(buf
, "disabled\n");
389 return sprintf(buf
, "unknown\n");
392 static ssize_t
regulator_state_show(struct device
*dev
,
393 struct device_attribute
*attr
, char *buf
)
395 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
398 mutex_lock(&rdev
->mutex
);
399 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
400 mutex_unlock(&rdev
->mutex
);
404 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
406 static ssize_t
regulator_status_show(struct device
*dev
,
407 struct device_attribute
*attr
, char *buf
)
409 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
413 status
= rdev
->desc
->ops
->get_status(rdev
);
418 case REGULATOR_STATUS_OFF
:
421 case REGULATOR_STATUS_ON
:
424 case REGULATOR_STATUS_ERROR
:
427 case REGULATOR_STATUS_FAST
:
430 case REGULATOR_STATUS_NORMAL
:
433 case REGULATOR_STATUS_IDLE
:
436 case REGULATOR_STATUS_STANDBY
:
439 case REGULATOR_STATUS_BYPASS
:
442 case REGULATOR_STATUS_UNDEFINED
:
449 return sprintf(buf
, "%s\n", label
);
451 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
453 static ssize_t
regulator_min_uA_show(struct device
*dev
,
454 struct device_attribute
*attr
, char *buf
)
456 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
458 if (!rdev
->constraints
)
459 return sprintf(buf
, "constraint not defined\n");
461 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
463 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
465 static ssize_t
regulator_max_uA_show(struct device
*dev
,
466 struct device_attribute
*attr
, char *buf
)
468 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
470 if (!rdev
->constraints
)
471 return sprintf(buf
, "constraint not defined\n");
473 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
475 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
477 static ssize_t
regulator_min_uV_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_uV
);
487 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
489 static ssize_t
regulator_max_uV_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_uV
);
499 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
501 static ssize_t
regulator_total_uA_show(struct device
*dev
,
502 struct device_attribute
*attr
, char *buf
)
504 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
505 struct regulator
*regulator
;
508 mutex_lock(&rdev
->mutex
);
509 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
510 uA
+= regulator
->uA_load
;
511 mutex_unlock(&rdev
->mutex
);
512 return sprintf(buf
, "%d\n", uA
);
514 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
516 static ssize_t
num_users_show(struct device
*dev
, struct device_attribute
*attr
,
519 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
520 return sprintf(buf
, "%d\n", rdev
->use_count
);
522 static DEVICE_ATTR_RO(num_users
);
524 static ssize_t
type_show(struct device
*dev
, struct device_attribute
*attr
,
527 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
529 switch (rdev
->desc
->type
) {
530 case REGULATOR_VOLTAGE
:
531 return sprintf(buf
, "voltage\n");
532 case REGULATOR_CURRENT
:
533 return sprintf(buf
, "current\n");
535 return sprintf(buf
, "unknown\n");
537 static DEVICE_ATTR_RO(type
);
539 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
540 struct device_attribute
*attr
, char *buf
)
542 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
544 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
546 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
547 regulator_suspend_mem_uV_show
, NULL
);
549 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
550 struct device_attribute
*attr
, char *buf
)
552 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
554 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
556 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
557 regulator_suspend_disk_uV_show
, NULL
);
559 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
560 struct device_attribute
*attr
, char *buf
)
562 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
564 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
566 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
567 regulator_suspend_standby_uV_show
, NULL
);
569 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
570 struct device_attribute
*attr
, char *buf
)
572 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
574 return regulator_print_opmode(buf
,
575 rdev
->constraints
->state_mem
.mode
);
577 static DEVICE_ATTR(suspend_mem_mode
, 0444,
578 regulator_suspend_mem_mode_show
, NULL
);
580 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
581 struct device_attribute
*attr
, char *buf
)
583 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
585 return regulator_print_opmode(buf
,
586 rdev
->constraints
->state_disk
.mode
);
588 static DEVICE_ATTR(suspend_disk_mode
, 0444,
589 regulator_suspend_disk_mode_show
, NULL
);
591 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
592 struct device_attribute
*attr
, char *buf
)
594 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
596 return regulator_print_opmode(buf
,
597 rdev
->constraints
->state_standby
.mode
);
599 static DEVICE_ATTR(suspend_standby_mode
, 0444,
600 regulator_suspend_standby_mode_show
, NULL
);
602 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
603 struct device_attribute
*attr
, char *buf
)
605 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
607 return regulator_print_state(buf
,
608 rdev
->constraints
->state_mem
.enabled
);
610 static DEVICE_ATTR(suspend_mem_state
, 0444,
611 regulator_suspend_mem_state_show
, NULL
);
613 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
614 struct device_attribute
*attr
, char *buf
)
616 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
618 return regulator_print_state(buf
,
619 rdev
->constraints
->state_disk
.enabled
);
621 static DEVICE_ATTR(suspend_disk_state
, 0444,
622 regulator_suspend_disk_state_show
, NULL
);
624 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
625 struct device_attribute
*attr
, char *buf
)
627 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
629 return regulator_print_state(buf
,
630 rdev
->constraints
->state_standby
.enabled
);
632 static DEVICE_ATTR(suspend_standby_state
, 0444,
633 regulator_suspend_standby_state_show
, NULL
);
635 static ssize_t
regulator_bypass_show(struct device
*dev
,
636 struct device_attribute
*attr
, char *buf
)
638 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
643 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypass
);
652 return sprintf(buf
, "%s\n", report
);
654 static DEVICE_ATTR(bypass
, 0444,
655 regulator_bypass_show
, NULL
);
657 /* Calculate the new optimum regulator operating mode based on the new total
658 * consumer load. All locks held by caller */
659 static int drms_uA_update(struct regulator_dev
*rdev
)
661 struct regulator
*sibling
;
662 int current_uA
= 0, output_uV
, input_uV
, err
;
665 lockdep_assert_held_once(&rdev
->mutex
);
668 * first check to see if we can set modes at all, otherwise just
669 * tell the consumer everything is OK.
671 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_DRMS
))
674 if (!rdev
->desc
->ops
->get_optimum_mode
&&
675 !rdev
->desc
->ops
->set_load
)
678 if (!rdev
->desc
->ops
->set_mode
&&
679 !rdev
->desc
->ops
->set_load
)
682 /* get output voltage */
683 output_uV
= _regulator_get_voltage(rdev
);
684 if (output_uV
<= 0) {
685 rdev_err(rdev
, "invalid output voltage found\n");
689 /* get input voltage */
692 input_uV
= regulator_get_voltage(rdev
->supply
);
694 input_uV
= rdev
->constraints
->input_uV
;
696 rdev_err(rdev
, "invalid input voltage found\n");
700 /* calc total requested load */
701 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
702 current_uA
+= sibling
->uA_load
;
704 current_uA
+= rdev
->constraints
->system_load
;
706 if (rdev
->desc
->ops
->set_load
) {
707 /* set the optimum mode for our new total regulator load */
708 err
= rdev
->desc
->ops
->set_load(rdev
, current_uA
);
710 rdev_err(rdev
, "failed to set load %d\n", current_uA
);
712 /* now get the optimum mode for our new total regulator load */
713 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
714 output_uV
, current_uA
);
716 /* check the new mode is allowed */
717 err
= regulator_mode_constrain(rdev
, &mode
);
719 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
720 current_uA
, input_uV
, output_uV
);
724 err
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
726 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
732 static int suspend_set_state(struct regulator_dev
*rdev
,
733 struct regulator_state
*rstate
)
737 /* If we have no suspend mode configration don't set anything;
738 * only warn if the driver implements set_suspend_voltage or
739 * set_suspend_mode callback.
741 if (!rstate
->enabled
&& !rstate
->disabled
) {
742 if (rdev
->desc
->ops
->set_suspend_voltage
||
743 rdev
->desc
->ops
->set_suspend_mode
)
744 rdev_warn(rdev
, "No configuration\n");
748 if (rstate
->enabled
&& rstate
->disabled
) {
749 rdev_err(rdev
, "invalid configuration\n");
753 if (rstate
->enabled
&& rdev
->desc
->ops
->set_suspend_enable
)
754 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
755 else if (rstate
->disabled
&& rdev
->desc
->ops
->set_suspend_disable
)
756 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
757 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
761 rdev_err(rdev
, "failed to enabled/disable\n");
765 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
766 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
768 rdev_err(rdev
, "failed to set voltage\n");
773 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
774 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
776 rdev_err(rdev
, "failed to set mode\n");
783 /* locks held by caller */
784 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
786 if (!rdev
->constraints
)
790 case PM_SUSPEND_STANDBY
:
791 return suspend_set_state(rdev
,
792 &rdev
->constraints
->state_standby
);
794 return suspend_set_state(rdev
,
795 &rdev
->constraints
->state_mem
);
797 return suspend_set_state(rdev
,
798 &rdev
->constraints
->state_disk
);
804 static void print_constraints(struct regulator_dev
*rdev
)
806 struct regulation_constraints
*constraints
= rdev
->constraints
;
808 size_t len
= sizeof(buf
) - 1;
812 if (constraints
->min_uV
&& constraints
->max_uV
) {
813 if (constraints
->min_uV
== constraints
->max_uV
)
814 count
+= scnprintf(buf
+ count
, len
- count
, "%d mV ",
815 constraints
->min_uV
/ 1000);
817 count
+= scnprintf(buf
+ count
, len
- count
,
819 constraints
->min_uV
/ 1000,
820 constraints
->max_uV
/ 1000);
823 if (!constraints
->min_uV
||
824 constraints
->min_uV
!= constraints
->max_uV
) {
825 ret
= _regulator_get_voltage(rdev
);
827 count
+= scnprintf(buf
+ count
, len
- count
,
828 "at %d mV ", ret
/ 1000);
831 if (constraints
->uV_offset
)
832 count
+= scnprintf(buf
+ count
, len
- count
, "%dmV offset ",
833 constraints
->uV_offset
/ 1000);
835 if (constraints
->min_uA
&& constraints
->max_uA
) {
836 if (constraints
->min_uA
== constraints
->max_uA
)
837 count
+= scnprintf(buf
+ count
, len
- count
, "%d mA ",
838 constraints
->min_uA
/ 1000);
840 count
+= scnprintf(buf
+ count
, len
- count
,
842 constraints
->min_uA
/ 1000,
843 constraints
->max_uA
/ 1000);
846 if (!constraints
->min_uA
||
847 constraints
->min_uA
!= constraints
->max_uA
) {
848 ret
= _regulator_get_current_limit(rdev
);
850 count
+= scnprintf(buf
+ count
, len
- count
,
851 "at %d mA ", ret
/ 1000);
854 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
855 count
+= scnprintf(buf
+ count
, len
- count
, "fast ");
856 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
857 count
+= scnprintf(buf
+ count
, len
- count
, "normal ");
858 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
859 count
+= scnprintf(buf
+ count
, len
- count
, "idle ");
860 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
861 count
+= scnprintf(buf
+ count
, len
- count
, "standby");
864 scnprintf(buf
, len
, "no parameters");
866 rdev_dbg(rdev
, "%s\n", buf
);
868 if ((constraints
->min_uV
!= constraints
->max_uV
) &&
869 !regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
))
871 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
874 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
875 struct regulation_constraints
*constraints
)
877 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
880 /* do we need to apply the constraint voltage */
881 if (rdev
->constraints
->apply_uV
&&
882 rdev
->constraints
->min_uV
&& rdev
->constraints
->max_uV
) {
883 int target_min
, target_max
;
884 int current_uV
= _regulator_get_voltage(rdev
);
885 if (current_uV
< 0) {
887 "failed to get the current voltage(%d)\n",
893 * If we're below the minimum voltage move up to the
894 * minimum voltage, if we're above the maximum voltage
895 * then move down to the maximum.
897 target_min
= current_uV
;
898 target_max
= current_uV
;
900 if (current_uV
< rdev
->constraints
->min_uV
) {
901 target_min
= rdev
->constraints
->min_uV
;
902 target_max
= rdev
->constraints
->min_uV
;
905 if (current_uV
> rdev
->constraints
->max_uV
) {
906 target_min
= rdev
->constraints
->max_uV
;
907 target_max
= rdev
->constraints
->max_uV
;
910 if (target_min
!= current_uV
|| target_max
!= current_uV
) {
911 rdev_info(rdev
, "Bringing %duV into %d-%duV\n",
912 current_uV
, target_min
, target_max
);
913 ret
= _regulator_do_set_voltage(
914 rdev
, target_min
, target_max
);
917 "failed to apply %d-%duV constraint(%d)\n",
918 target_min
, target_max
, ret
);
924 /* constrain machine-level voltage specs to fit
925 * the actual range supported by this regulator.
927 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
928 int count
= rdev
->desc
->n_voltages
;
930 int min_uV
= INT_MAX
;
931 int max_uV
= INT_MIN
;
932 int cmin
= constraints
->min_uV
;
933 int cmax
= constraints
->max_uV
;
935 /* it's safe to autoconfigure fixed-voltage supplies
936 and the constraints are used by list_voltage. */
937 if (count
== 1 && !cmin
) {
940 constraints
->min_uV
= cmin
;
941 constraints
->max_uV
= cmax
;
944 /* voltage constraints are optional */
945 if ((cmin
== 0) && (cmax
== 0))
948 /* else require explicit machine-level constraints */
949 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
950 rdev_err(rdev
, "invalid voltage constraints\n");
954 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
955 for (i
= 0; i
< count
; i
++) {
958 value
= ops
->list_voltage(rdev
, i
);
962 /* maybe adjust [min_uV..max_uV] */
963 if (value
>= cmin
&& value
< min_uV
)
965 if (value
<= cmax
&& value
> max_uV
)
969 /* final: [min_uV..max_uV] valid iff constraints valid */
970 if (max_uV
< min_uV
) {
972 "unsupportable voltage constraints %u-%uuV\n",
977 /* use regulator's subset of machine constraints */
978 if (constraints
->min_uV
< min_uV
) {
979 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
980 constraints
->min_uV
, min_uV
);
981 constraints
->min_uV
= min_uV
;
983 if (constraints
->max_uV
> max_uV
) {
984 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
985 constraints
->max_uV
, max_uV
);
986 constraints
->max_uV
= max_uV
;
993 static int machine_constraints_current(struct regulator_dev
*rdev
,
994 struct regulation_constraints
*constraints
)
996 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
999 if (!constraints
->min_uA
&& !constraints
->max_uA
)
1002 if (constraints
->min_uA
> constraints
->max_uA
) {
1003 rdev_err(rdev
, "Invalid current constraints\n");
1007 if (!ops
->set_current_limit
|| !ops
->get_current_limit
) {
1008 rdev_warn(rdev
, "Operation of current configuration missing\n");
1012 /* Set regulator current in constraints range */
1013 ret
= ops
->set_current_limit(rdev
, constraints
->min_uA
,
1014 constraints
->max_uA
);
1016 rdev_err(rdev
, "Failed to set current constraint, %d\n", ret
);
1023 static int _regulator_do_enable(struct regulator_dev
*rdev
);
1026 * set_machine_constraints - sets regulator constraints
1027 * @rdev: regulator source
1028 * @constraints: constraints to apply
1030 * Allows platform initialisation code to define and constrain
1031 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1032 * Constraints *must* be set by platform code in order for some
1033 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1036 static int set_machine_constraints(struct regulator_dev
*rdev
,
1037 const struct regulation_constraints
*constraints
)
1040 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
1043 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
1046 rdev
->constraints
= kzalloc(sizeof(*constraints
),
1048 if (!rdev
->constraints
)
1051 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
1055 ret
= machine_constraints_current(rdev
, rdev
->constraints
);
1059 if (rdev
->constraints
->ilim_uA
&& ops
->set_input_current_limit
) {
1060 ret
= ops
->set_input_current_limit(rdev
,
1061 rdev
->constraints
->ilim_uA
);
1063 rdev_err(rdev
, "failed to set input limit\n");
1068 /* do we need to setup our suspend state */
1069 if (rdev
->constraints
->initial_state
) {
1070 ret
= suspend_prepare(rdev
, rdev
->constraints
->initial_state
);
1072 rdev_err(rdev
, "failed to set suspend state\n");
1077 if (rdev
->constraints
->initial_mode
) {
1078 if (!ops
->set_mode
) {
1079 rdev_err(rdev
, "no set_mode operation\n");
1083 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
1085 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
1090 /* If the constraints say the regulator should be on at this point
1091 * and we have control then make sure it is enabled.
1093 if (rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) {
1094 ret
= _regulator_do_enable(rdev
);
1095 if (ret
< 0 && ret
!= -EINVAL
) {
1096 rdev_err(rdev
, "failed to enable\n");
1101 if ((rdev
->constraints
->ramp_delay
|| rdev
->constraints
->ramp_disable
)
1102 && ops
->set_ramp_delay
) {
1103 ret
= ops
->set_ramp_delay(rdev
, rdev
->constraints
->ramp_delay
);
1105 rdev_err(rdev
, "failed to set ramp_delay\n");
1110 if (rdev
->constraints
->pull_down
&& ops
->set_pull_down
) {
1111 ret
= ops
->set_pull_down(rdev
);
1113 rdev_err(rdev
, "failed to set pull down\n");
1118 if (rdev
->constraints
->soft_start
&& ops
->set_soft_start
) {
1119 ret
= ops
->set_soft_start(rdev
);
1121 rdev_err(rdev
, "failed to set soft start\n");
1126 if (rdev
->constraints
->over_current_protection
1127 && ops
->set_over_current_protection
) {
1128 ret
= ops
->set_over_current_protection(rdev
);
1130 rdev_err(rdev
, "failed to set over current protection\n");
1135 if (rdev
->constraints
->active_discharge
&& ops
->set_active_discharge
) {
1136 bool ad_state
= (rdev
->constraints
->active_discharge
==
1137 REGULATOR_ACTIVE_DISCHARGE_ENABLE
) ? true : false;
1139 ret
= ops
->set_active_discharge(rdev
, ad_state
);
1141 rdev_err(rdev
, "failed to set active discharge\n");
1146 print_constraints(rdev
);
1151 * set_supply - set regulator supply regulator
1152 * @rdev: regulator name
1153 * @supply_rdev: supply regulator name
1155 * Called by platform initialisation code to set the supply regulator for this
1156 * regulator. This ensures that a regulators supply will also be enabled by the
1157 * core if it's child is enabled.
1159 static int set_supply(struct regulator_dev
*rdev
,
1160 struct regulator_dev
*supply_rdev
)
1164 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
1166 if (!try_module_get(supply_rdev
->owner
))
1169 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
1170 if (rdev
->supply
== NULL
) {
1174 supply_rdev
->open_count
++;
1180 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1181 * @rdev: regulator source
1182 * @consumer_dev_name: dev_name() string for device supply applies to
1183 * @supply: symbolic name for supply
1185 * Allows platform initialisation code to map physical regulator
1186 * sources to symbolic names for supplies for use by devices. Devices
1187 * should use these symbolic names to request regulators, avoiding the
1188 * need to provide board-specific regulator names as platform data.
1190 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1191 const char *consumer_dev_name
,
1194 struct regulator_map
*node
;
1200 if (consumer_dev_name
!= NULL
)
1205 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1206 if (node
->dev_name
&& consumer_dev_name
) {
1207 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1209 } else if (node
->dev_name
|| consumer_dev_name
) {
1213 if (strcmp(node
->supply
, supply
) != 0)
1216 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1218 dev_name(&node
->regulator
->dev
),
1219 node
->regulator
->desc
->name
,
1221 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1225 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1229 node
->regulator
= rdev
;
1230 node
->supply
= supply
;
1233 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1234 if (node
->dev_name
== NULL
) {
1240 list_add(&node
->list
, ®ulator_map_list
);
1244 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1246 struct regulator_map
*node
, *n
;
1248 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1249 if (rdev
== node
->regulator
) {
1250 list_del(&node
->list
);
1251 kfree(node
->dev_name
);
1257 #ifdef CONFIG_DEBUG_FS
1258 static ssize_t
constraint_flags_read_file(struct file
*file
,
1259 char __user
*user_buf
,
1260 size_t count
, loff_t
*ppos
)
1262 const struct regulator
*regulator
= file
->private_data
;
1263 const struct regulation_constraints
*c
= regulator
->rdev
->constraints
;
1270 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
1274 ret
= snprintf(buf
, PAGE_SIZE
,
1278 "ramp_disable: %u\n"
1281 "over_current_protection: %u\n",
1288 c
->over_current_protection
);
1290 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
1298 static const struct file_operations constraint_flags_fops
= {
1299 #ifdef CONFIG_DEBUG_FS
1300 .open
= simple_open
,
1301 .read
= constraint_flags_read_file
,
1302 .llseek
= default_llseek
,
1306 #define REG_STR_SIZE 64
1308 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1310 const char *supply_name
)
1312 struct regulator
*regulator
;
1313 char buf
[REG_STR_SIZE
];
1316 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1317 if (regulator
== NULL
)
1320 mutex_lock(&rdev
->mutex
);
1321 regulator
->rdev
= rdev
;
1322 list_add(®ulator
->list
, &rdev
->consumer_list
);
1325 regulator
->dev
= dev
;
1327 /* Add a link to the device sysfs entry */
1328 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1329 dev
->kobj
.name
, supply_name
);
1330 if (size
>= REG_STR_SIZE
)
1333 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1334 if (regulator
->supply_name
== NULL
)
1337 err
= sysfs_create_link_nowarn(&rdev
->dev
.kobj
, &dev
->kobj
,
1340 rdev_dbg(rdev
, "could not add device link %s err %d\n",
1341 dev
->kobj
.name
, err
);
1345 regulator
->supply_name
= kstrdup(supply_name
, GFP_KERNEL
);
1346 if (regulator
->supply_name
== NULL
)
1350 regulator
->debugfs
= debugfs_create_dir(regulator
->supply_name
,
1352 if (!regulator
->debugfs
) {
1353 rdev_dbg(rdev
, "Failed to create debugfs directory\n");
1355 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1356 ®ulator
->uA_load
);
1357 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1358 ®ulator
->min_uV
);
1359 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1360 ®ulator
->max_uV
);
1361 debugfs_create_file("constraint_flags", 0444,
1362 regulator
->debugfs
, regulator
,
1363 &constraint_flags_fops
);
1367 * Check now if the regulator is an always on regulator - if
1368 * it is then we don't need to do nearly so much work for
1369 * enable/disable calls.
1371 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
) &&
1372 _regulator_is_enabled(rdev
))
1373 regulator
->always_on
= true;
1375 mutex_unlock(&rdev
->mutex
);
1378 list_del(®ulator
->list
);
1380 mutex_unlock(&rdev
->mutex
);
1384 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1386 if (rdev
->constraints
&& rdev
->constraints
->enable_time
)
1387 return rdev
->constraints
->enable_time
;
1388 if (!rdev
->desc
->ops
->enable_time
)
1389 return rdev
->desc
->enable_time
;
1390 return rdev
->desc
->ops
->enable_time(rdev
);
1393 static struct regulator_supply_alias
*regulator_find_supply_alias(
1394 struct device
*dev
, const char *supply
)
1396 struct regulator_supply_alias
*map
;
1398 list_for_each_entry(map
, ®ulator_supply_alias_list
, list
)
1399 if (map
->src_dev
== dev
&& strcmp(map
->src_supply
, supply
) == 0)
1405 static void regulator_supply_alias(struct device
**dev
, const char **supply
)
1407 struct regulator_supply_alias
*map
;
1409 map
= regulator_find_supply_alias(*dev
, *supply
);
1411 dev_dbg(*dev
, "Mapping supply %s to %s,%s\n",
1412 *supply
, map
->alias_supply
,
1413 dev_name(map
->alias_dev
));
1414 *dev
= map
->alias_dev
;
1415 *supply
= map
->alias_supply
;
1419 static int of_node_match(struct device
*dev
, const void *data
)
1421 return dev
->of_node
== data
;
1424 static struct regulator_dev
*of_find_regulator_by_node(struct device_node
*np
)
1428 dev
= class_find_device(®ulator_class
, NULL
, np
, of_node_match
);
1430 return dev
? dev_to_rdev(dev
) : NULL
;
1433 static int regulator_match(struct device
*dev
, const void *data
)
1435 struct regulator_dev
*r
= dev_to_rdev(dev
);
1437 return strcmp(rdev_get_name(r
), data
) == 0;
1440 static struct regulator_dev
*regulator_lookup_by_name(const char *name
)
1444 dev
= class_find_device(®ulator_class
, NULL
, name
, regulator_match
);
1446 return dev
? dev_to_rdev(dev
) : NULL
;
1450 * regulator_dev_lookup - lookup a regulator device.
1451 * @dev: device for regulator "consumer".
1452 * @supply: Supply name or regulator ID.
1453 * @ret: 0 on success, -ENODEV if lookup fails permanently, -EPROBE_DEFER if
1454 * lookup could succeed in the future.
1456 * If successful, returns a struct regulator_dev that corresponds to the name
1457 * @supply and with the embedded struct device refcount incremented by one,
1458 * or NULL on failure. The refcount must be dropped by calling put_device().
1460 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1464 struct regulator_dev
*r
;
1465 struct device_node
*node
;
1466 struct regulator_map
*map
;
1467 const char *devname
= NULL
;
1469 regulator_supply_alias(&dev
, &supply
);
1471 /* first do a dt based lookup */
1472 if (dev
&& dev
->of_node
) {
1473 node
= of_get_regulator(dev
, supply
);
1475 r
= of_find_regulator_by_node(node
);
1478 *ret
= -EPROBE_DEFER
;
1482 * If we couldn't even get the node then it's
1483 * not just that the device didn't register
1484 * yet, there's no node and we'll never
1491 /* if not found, try doing it non-dt way */
1493 devname
= dev_name(dev
);
1495 r
= regulator_lookup_by_name(supply
);
1499 mutex_lock(®ulator_list_mutex
);
1500 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1501 /* If the mapping has a device set up it must match */
1502 if (map
->dev_name
&&
1503 (!devname
|| strcmp(map
->dev_name
, devname
)))
1506 if (strcmp(map
->supply
, supply
) == 0 &&
1507 get_device(&map
->regulator
->dev
)) {
1508 mutex_unlock(®ulator_list_mutex
);
1509 return map
->regulator
;
1512 mutex_unlock(®ulator_list_mutex
);
1517 static int regulator_resolve_supply(struct regulator_dev
*rdev
)
1519 struct regulator_dev
*r
;
1520 struct device
*dev
= rdev
->dev
.parent
;
1523 /* No supply to resovle? */
1524 if (!rdev
->supply_name
)
1527 /* Supply already resolved? */
1531 r
= regulator_dev_lookup(dev
, rdev
->supply_name
, &ret
);
1533 if (ret
== -ENODEV
) {
1535 * No supply was specified for this regulator and
1536 * there will never be one.
1541 /* Did the lookup explicitly defer for us? */
1542 if (ret
== -EPROBE_DEFER
)
1545 if (have_full_constraints()) {
1546 r
= dummy_regulator_rdev
;
1547 get_device(&r
->dev
);
1549 dev_err(dev
, "Failed to resolve %s-supply for %s\n",
1550 rdev
->supply_name
, rdev
->desc
->name
);
1551 return -EPROBE_DEFER
;
1555 /* Recursively resolve the supply of the supply */
1556 ret
= regulator_resolve_supply(r
);
1558 put_device(&r
->dev
);
1562 ret
= set_supply(rdev
, r
);
1564 put_device(&r
->dev
);
1568 /* Cascade always-on state to supply */
1569 if (_regulator_is_enabled(rdev
)) {
1570 ret
= regulator_enable(rdev
->supply
);
1572 _regulator_put(rdev
->supply
);
1573 rdev
->supply
= NULL
;
1581 /* Internal regulator request function */
1582 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1583 bool exclusive
, bool allow_dummy
)
1585 struct regulator_dev
*rdev
;
1586 struct regulator
*regulator
= ERR_PTR(-EPROBE_DEFER
);
1587 const char *devname
= NULL
;
1591 pr_err("get() with no identifier\n");
1592 return ERR_PTR(-EINVAL
);
1596 devname
= dev_name(dev
);
1598 if (have_full_constraints())
1601 ret
= -EPROBE_DEFER
;
1603 rdev
= regulator_dev_lookup(dev
, id
, &ret
);
1607 regulator
= ERR_PTR(ret
);
1610 * If we have return value from dev_lookup fail, we do not expect to
1611 * succeed, so, quit with appropriate error value
1613 if (ret
&& ret
!= -ENODEV
)
1617 devname
= "deviceless";
1620 * Assume that a regulator is physically present and enabled
1621 * even if it isn't hooked up and just provide a dummy.
1623 if (have_full_constraints() && allow_dummy
) {
1624 pr_warn("%s supply %s not found, using dummy regulator\n",
1627 rdev
= dummy_regulator_rdev
;
1628 get_device(&rdev
->dev
);
1630 /* Don't log an error when called from regulator_get_optional() */
1631 } else if (!have_full_constraints() || exclusive
) {
1632 dev_warn(dev
, "dummy supplies not allowed\n");
1638 if (rdev
->exclusive
) {
1639 regulator
= ERR_PTR(-EPERM
);
1640 put_device(&rdev
->dev
);
1644 if (exclusive
&& rdev
->open_count
) {
1645 regulator
= ERR_PTR(-EBUSY
);
1646 put_device(&rdev
->dev
);
1650 ret
= regulator_resolve_supply(rdev
);
1652 regulator
= ERR_PTR(ret
);
1653 put_device(&rdev
->dev
);
1657 if (!try_module_get(rdev
->owner
)) {
1658 put_device(&rdev
->dev
);
1662 regulator
= create_regulator(rdev
, dev
, id
);
1663 if (regulator
== NULL
) {
1664 regulator
= ERR_PTR(-ENOMEM
);
1665 put_device(&rdev
->dev
);
1666 module_put(rdev
->owner
);
1672 rdev
->exclusive
= 1;
1674 ret
= _regulator_is_enabled(rdev
);
1676 rdev
->use_count
= 1;
1678 rdev
->use_count
= 0;
1685 * regulator_get - lookup and obtain a reference to a regulator.
1686 * @dev: device for regulator "consumer"
1687 * @id: Supply name or regulator ID.
1689 * Returns a struct regulator corresponding to the regulator producer,
1690 * or IS_ERR() condition containing errno.
1692 * Use of supply names configured via regulator_set_device_supply() is
1693 * strongly encouraged. It is recommended that the supply name used
1694 * should match the name used for the supply and/or the relevant
1695 * device pins in the datasheet.
1697 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1699 return _regulator_get(dev
, id
, false, true);
1701 EXPORT_SYMBOL_GPL(regulator_get
);
1704 * regulator_get_exclusive - obtain exclusive access to a regulator.
1705 * @dev: device for regulator "consumer"
1706 * @id: Supply name or regulator ID.
1708 * Returns a struct regulator corresponding to the regulator producer,
1709 * or IS_ERR() condition containing errno. Other consumers will be
1710 * unable to obtain this regulator while this reference is held and the
1711 * use count for the regulator will be initialised to reflect the current
1712 * state of the regulator.
1714 * This is intended for use by consumers which cannot tolerate shared
1715 * use of the regulator such as those which need to force the
1716 * regulator off for correct operation of the hardware they are
1719 * Use of supply names configured via regulator_set_device_supply() is
1720 * strongly encouraged. It is recommended that the supply name used
1721 * should match the name used for the supply and/or the relevant
1722 * device pins in the datasheet.
1724 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1726 return _regulator_get(dev
, id
, true, false);
1728 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1731 * regulator_get_optional - obtain optional access to a regulator.
1732 * @dev: device for regulator "consumer"
1733 * @id: Supply name or regulator ID.
1735 * Returns a struct regulator corresponding to the regulator producer,
1736 * or IS_ERR() condition containing errno.
1738 * This is intended for use by consumers for devices which can have
1739 * some supplies unconnected in normal use, such as some MMC devices.
1740 * It can allow the regulator core to provide stub supplies for other
1741 * supplies requested using normal regulator_get() calls without
1742 * disrupting the operation of drivers that can handle absent
1745 * Use of supply names configured via regulator_set_device_supply() is
1746 * strongly encouraged. It is recommended that the supply name used
1747 * should match the name used for the supply and/or the relevant
1748 * device pins in the datasheet.
1750 struct regulator
*regulator_get_optional(struct device
*dev
, const char *id
)
1752 return _regulator_get(dev
, id
, false, false);
1754 EXPORT_SYMBOL_GPL(regulator_get_optional
);
1756 /* regulator_list_mutex lock held by regulator_put() */
1757 static void _regulator_put(struct regulator
*regulator
)
1759 struct regulator_dev
*rdev
;
1761 if (IS_ERR_OR_NULL(regulator
))
1764 lockdep_assert_held_once(®ulator_list_mutex
);
1766 rdev
= regulator
->rdev
;
1768 debugfs_remove_recursive(regulator
->debugfs
);
1770 /* remove any sysfs entries */
1772 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1773 mutex_lock(&rdev
->mutex
);
1774 list_del(®ulator
->list
);
1777 rdev
->exclusive
= 0;
1778 put_device(&rdev
->dev
);
1779 mutex_unlock(&rdev
->mutex
);
1781 kfree(regulator
->supply_name
);
1784 module_put(rdev
->owner
);
1788 * regulator_put - "free" the regulator source
1789 * @regulator: regulator source
1791 * Note: drivers must ensure that all regulator_enable calls made on this
1792 * regulator source are balanced by regulator_disable calls prior to calling
1795 void regulator_put(struct regulator
*regulator
)
1797 mutex_lock(®ulator_list_mutex
);
1798 _regulator_put(regulator
);
1799 mutex_unlock(®ulator_list_mutex
);
1801 EXPORT_SYMBOL_GPL(regulator_put
);
1804 * regulator_register_supply_alias - Provide device alias for supply lookup
1806 * @dev: device that will be given as the regulator "consumer"
1807 * @id: Supply name or regulator ID
1808 * @alias_dev: device that should be used to lookup the supply
1809 * @alias_id: Supply name or regulator ID that should be used to lookup the
1812 * All lookups for id on dev will instead be conducted for alias_id on
1815 int regulator_register_supply_alias(struct device
*dev
, const char *id
,
1816 struct device
*alias_dev
,
1817 const char *alias_id
)
1819 struct regulator_supply_alias
*map
;
1821 map
= regulator_find_supply_alias(dev
, id
);
1825 map
= kzalloc(sizeof(struct regulator_supply_alias
), GFP_KERNEL
);
1830 map
->src_supply
= id
;
1831 map
->alias_dev
= alias_dev
;
1832 map
->alias_supply
= alias_id
;
1834 list_add(&map
->list
, ®ulator_supply_alias_list
);
1836 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1837 id
, dev_name(dev
), alias_id
, dev_name(alias_dev
));
1841 EXPORT_SYMBOL_GPL(regulator_register_supply_alias
);
1844 * regulator_unregister_supply_alias - Remove device alias
1846 * @dev: device that will be given as the regulator "consumer"
1847 * @id: Supply name or regulator ID
1849 * Remove a lookup alias if one exists for id on dev.
1851 void regulator_unregister_supply_alias(struct device
*dev
, const char *id
)
1853 struct regulator_supply_alias
*map
;
1855 map
= regulator_find_supply_alias(dev
, id
);
1857 list_del(&map
->list
);
1861 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias
);
1864 * regulator_bulk_register_supply_alias - register multiple aliases
1866 * @dev: device that will be given as the regulator "consumer"
1867 * @id: List of supply names or regulator IDs
1868 * @alias_dev: device that should be used to lookup the supply
1869 * @alias_id: List of supply names or regulator IDs that should be used to
1871 * @num_id: Number of aliases to register
1873 * @return 0 on success, an errno on failure.
1875 * This helper function allows drivers to register several supply
1876 * aliases in one operation. If any of the aliases cannot be
1877 * registered any aliases that were registered will be removed
1878 * before returning to the caller.
1880 int regulator_bulk_register_supply_alias(struct device
*dev
,
1881 const char *const *id
,
1882 struct device
*alias_dev
,
1883 const char *const *alias_id
,
1889 for (i
= 0; i
< num_id
; ++i
) {
1890 ret
= regulator_register_supply_alias(dev
, id
[i
], alias_dev
,
1900 "Failed to create supply alias %s,%s -> %s,%s\n",
1901 id
[i
], dev_name(dev
), alias_id
[i
], dev_name(alias_dev
));
1904 regulator_unregister_supply_alias(dev
, id
[i
]);
1908 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias
);
1911 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1913 * @dev: device that will be given as the regulator "consumer"
1914 * @id: List of supply names or regulator IDs
1915 * @num_id: Number of aliases to unregister
1917 * This helper function allows drivers to unregister several supply
1918 * aliases in one operation.
1920 void regulator_bulk_unregister_supply_alias(struct device
*dev
,
1921 const char *const *id
,
1926 for (i
= 0; i
< num_id
; ++i
)
1927 regulator_unregister_supply_alias(dev
, id
[i
]);
1929 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias
);
1932 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1933 static int regulator_ena_gpio_request(struct regulator_dev
*rdev
,
1934 const struct regulator_config
*config
)
1936 struct regulator_enable_gpio
*pin
;
1937 struct gpio_desc
*gpiod
;
1940 gpiod
= gpio_to_desc(config
->ena_gpio
);
1942 list_for_each_entry(pin
, ®ulator_ena_gpio_list
, list
) {
1943 if (pin
->gpiod
== gpiod
) {
1944 rdev_dbg(rdev
, "GPIO %d is already used\n",
1946 goto update_ena_gpio_to_rdev
;
1950 ret
= gpio_request_one(config
->ena_gpio
,
1951 GPIOF_DIR_OUT
| config
->ena_gpio_flags
,
1952 rdev_get_name(rdev
));
1956 pin
= kzalloc(sizeof(struct regulator_enable_gpio
), GFP_KERNEL
);
1958 gpio_free(config
->ena_gpio
);
1963 pin
->ena_gpio_invert
= config
->ena_gpio_invert
;
1964 list_add(&pin
->list
, ®ulator_ena_gpio_list
);
1966 update_ena_gpio_to_rdev
:
1967 pin
->request_count
++;
1968 rdev
->ena_pin
= pin
;
1972 static void regulator_ena_gpio_free(struct regulator_dev
*rdev
)
1974 struct regulator_enable_gpio
*pin
, *n
;
1979 /* Free the GPIO only in case of no use */
1980 list_for_each_entry_safe(pin
, n
, ®ulator_ena_gpio_list
, list
) {
1981 if (pin
->gpiod
== rdev
->ena_pin
->gpiod
) {
1982 if (pin
->request_count
<= 1) {
1983 pin
->request_count
= 0;
1984 gpiod_put(pin
->gpiod
);
1985 list_del(&pin
->list
);
1987 rdev
->ena_pin
= NULL
;
1990 pin
->request_count
--;
1997 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1998 * @rdev: regulator_dev structure
1999 * @enable: enable GPIO at initial use?
2001 * GPIO is enabled in case of initial use. (enable_count is 0)
2002 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2004 static int regulator_ena_gpio_ctrl(struct regulator_dev
*rdev
, bool enable
)
2006 struct regulator_enable_gpio
*pin
= rdev
->ena_pin
;
2012 /* Enable GPIO at initial use */
2013 if (pin
->enable_count
== 0)
2014 gpiod_set_value_cansleep(pin
->gpiod
,
2015 !pin
->ena_gpio_invert
);
2017 pin
->enable_count
++;
2019 if (pin
->enable_count
> 1) {
2020 pin
->enable_count
--;
2024 /* Disable GPIO if not used */
2025 if (pin
->enable_count
<= 1) {
2026 gpiod_set_value_cansleep(pin
->gpiod
,
2027 pin
->ena_gpio_invert
);
2028 pin
->enable_count
= 0;
2036 * _regulator_enable_delay - a delay helper function
2037 * @delay: time to delay in microseconds
2039 * Delay for the requested amount of time as per the guidelines in:
2041 * Documentation/timers/timers-howto.txt
2043 * The assumption here is that regulators will never be enabled in
2044 * atomic context and therefore sleeping functions can be used.
2046 static void _regulator_enable_delay(unsigned int delay
)
2048 unsigned int ms
= delay
/ 1000;
2049 unsigned int us
= delay
% 1000;
2053 * For small enough values, handle super-millisecond
2054 * delays in the usleep_range() call below.
2063 * Give the scheduler some room to coalesce with any other
2064 * wakeup sources. For delays shorter than 10 us, don't even
2065 * bother setting up high-resolution timers and just busy-
2069 usleep_range(us
, us
+ 100);
2074 static int _regulator_do_enable(struct regulator_dev
*rdev
)
2078 /* Query before enabling in case configuration dependent. */
2079 ret
= _regulator_get_enable_time(rdev
);
2083 rdev_warn(rdev
, "enable_time() failed: %d\n", ret
);
2087 trace_regulator_enable(rdev_get_name(rdev
));
2089 if (rdev
->desc
->off_on_delay
) {
2090 /* if needed, keep a distance of off_on_delay from last time
2091 * this regulator was disabled.
2093 unsigned long start_jiffy
= jiffies
;
2094 unsigned long intended
, max_delay
, remaining
;
2096 max_delay
= usecs_to_jiffies(rdev
->desc
->off_on_delay
);
2097 intended
= rdev
->last_off_jiffy
+ max_delay
;
2099 if (time_before(start_jiffy
, intended
)) {
2100 /* calc remaining jiffies to deal with one-time
2102 * in case of multiple timer wrapping, either it can be
2103 * detected by out-of-range remaining, or it cannot be
2104 * detected and we gets a panelty of
2105 * _regulator_enable_delay().
2107 remaining
= intended
- start_jiffy
;
2108 if (remaining
<= max_delay
)
2109 _regulator_enable_delay(
2110 jiffies_to_usecs(remaining
));
2114 if (rdev
->ena_pin
) {
2115 if (!rdev
->ena_gpio_state
) {
2116 ret
= regulator_ena_gpio_ctrl(rdev
, true);
2119 rdev
->ena_gpio_state
= 1;
2121 } else if (rdev
->desc
->ops
->enable
) {
2122 ret
= rdev
->desc
->ops
->enable(rdev
);
2129 /* Allow the regulator to ramp; it would be useful to extend
2130 * this for bulk operations so that the regulators can ramp
2132 trace_regulator_enable_delay(rdev_get_name(rdev
));
2134 _regulator_enable_delay(delay
);
2136 trace_regulator_enable_complete(rdev_get_name(rdev
));
2141 /* locks held by regulator_enable() */
2142 static int _regulator_enable(struct regulator_dev
*rdev
)
2146 lockdep_assert_held_once(&rdev
->mutex
);
2148 /* check voltage and requested load before enabling */
2149 if (regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_DRMS
))
2150 drms_uA_update(rdev
);
2152 if (rdev
->use_count
== 0) {
2153 /* The regulator may on if it's not switchable or left on */
2154 ret
= _regulator_is_enabled(rdev
);
2155 if (ret
== -EINVAL
|| ret
== 0) {
2156 if (!regulator_ops_is_valid(rdev
,
2157 REGULATOR_CHANGE_STATUS
))
2160 ret
= _regulator_do_enable(rdev
);
2164 } else if (ret
< 0) {
2165 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
2168 /* Fallthrough on positive return values - already enabled */
2177 * regulator_enable - enable regulator output
2178 * @regulator: regulator source
2180 * Request that the regulator be enabled with the regulator output at
2181 * the predefined voltage or current value. Calls to regulator_enable()
2182 * must be balanced with calls to regulator_disable().
2184 * NOTE: the output value can be set by other drivers, boot loader or may be
2185 * hardwired in the regulator.
2187 int regulator_enable(struct regulator
*regulator
)
2189 struct regulator_dev
*rdev
= regulator
->rdev
;
2192 if (regulator
->always_on
)
2196 ret
= regulator_enable(rdev
->supply
);
2201 mutex_lock(&rdev
->mutex
);
2202 ret
= _regulator_enable(rdev
);
2203 mutex_unlock(&rdev
->mutex
);
2205 if (ret
!= 0 && rdev
->supply
)
2206 regulator_disable(rdev
->supply
);
2210 EXPORT_SYMBOL_GPL(regulator_enable
);
2212 static int _regulator_do_disable(struct regulator_dev
*rdev
)
2216 trace_regulator_disable(rdev_get_name(rdev
));
2218 if (rdev
->ena_pin
) {
2219 if (rdev
->ena_gpio_state
) {
2220 ret
= regulator_ena_gpio_ctrl(rdev
, false);
2223 rdev
->ena_gpio_state
= 0;
2226 } else if (rdev
->desc
->ops
->disable
) {
2227 ret
= rdev
->desc
->ops
->disable(rdev
);
2232 /* cares about last_off_jiffy only if off_on_delay is required by
2235 if (rdev
->desc
->off_on_delay
)
2236 rdev
->last_off_jiffy
= jiffies
;
2238 trace_regulator_disable_complete(rdev_get_name(rdev
));
2243 /* locks held by regulator_disable() */
2244 static int _regulator_disable(struct regulator_dev
*rdev
)
2248 lockdep_assert_held_once(&rdev
->mutex
);
2250 if (WARN(rdev
->use_count
<= 0,
2251 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
2254 /* are we the last user and permitted to disable ? */
2255 if (rdev
->use_count
== 1 &&
2256 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
2258 /* we are last user */
2259 if (regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
)) {
2260 ret
= _notifier_call_chain(rdev
,
2261 REGULATOR_EVENT_PRE_DISABLE
,
2263 if (ret
& NOTIFY_STOP_MASK
)
2266 ret
= _regulator_do_disable(rdev
);
2268 rdev_err(rdev
, "failed to disable\n");
2269 _notifier_call_chain(rdev
,
2270 REGULATOR_EVENT_ABORT_DISABLE
,
2274 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
2278 rdev
->use_count
= 0;
2279 } else if (rdev
->use_count
> 1) {
2280 if (regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_DRMS
))
2281 drms_uA_update(rdev
);
2290 * regulator_disable - disable regulator output
2291 * @regulator: regulator source
2293 * Disable the regulator output voltage or current. Calls to
2294 * regulator_enable() must be balanced with calls to
2295 * regulator_disable().
2297 * NOTE: this will only disable the regulator output if no other consumer
2298 * devices have it enabled, the regulator device supports disabling and
2299 * machine constraints permit this operation.
2301 int regulator_disable(struct regulator
*regulator
)
2303 struct regulator_dev
*rdev
= regulator
->rdev
;
2306 if (regulator
->always_on
)
2309 mutex_lock(&rdev
->mutex
);
2310 ret
= _regulator_disable(rdev
);
2311 mutex_unlock(&rdev
->mutex
);
2313 if (ret
== 0 && rdev
->supply
)
2314 regulator_disable(rdev
->supply
);
2318 EXPORT_SYMBOL_GPL(regulator_disable
);
2320 /* locks held by regulator_force_disable() */
2321 static int _regulator_force_disable(struct regulator_dev
*rdev
)
2325 lockdep_assert_held_once(&rdev
->mutex
);
2327 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2328 REGULATOR_EVENT_PRE_DISABLE
, NULL
);
2329 if (ret
& NOTIFY_STOP_MASK
)
2332 ret
= _regulator_do_disable(rdev
);
2334 rdev_err(rdev
, "failed to force disable\n");
2335 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2336 REGULATOR_EVENT_ABORT_DISABLE
, NULL
);
2340 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
2341 REGULATOR_EVENT_DISABLE
, NULL
);
2347 * regulator_force_disable - force disable regulator output
2348 * @regulator: regulator source
2350 * Forcibly disable the regulator output voltage or current.
2351 * NOTE: this *will* disable the regulator output even if other consumer
2352 * devices have it enabled. This should be used for situations when device
2353 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2355 int regulator_force_disable(struct regulator
*regulator
)
2357 struct regulator_dev
*rdev
= regulator
->rdev
;
2360 mutex_lock(&rdev
->mutex
);
2361 regulator
->uA_load
= 0;
2362 ret
= _regulator_force_disable(regulator
->rdev
);
2363 mutex_unlock(&rdev
->mutex
);
2366 while (rdev
->open_count
--)
2367 regulator_disable(rdev
->supply
);
2371 EXPORT_SYMBOL_GPL(regulator_force_disable
);
2373 static void regulator_disable_work(struct work_struct
*work
)
2375 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
2379 mutex_lock(&rdev
->mutex
);
2381 BUG_ON(!rdev
->deferred_disables
);
2383 count
= rdev
->deferred_disables
;
2384 rdev
->deferred_disables
= 0;
2386 for (i
= 0; i
< count
; i
++) {
2387 ret
= _regulator_disable(rdev
);
2389 rdev_err(rdev
, "Deferred disable failed: %d\n", ret
);
2392 mutex_unlock(&rdev
->mutex
);
2395 for (i
= 0; i
< count
; i
++) {
2396 ret
= regulator_disable(rdev
->supply
);
2399 "Supply disable failed: %d\n", ret
);
2406 * regulator_disable_deferred - disable regulator output with delay
2407 * @regulator: regulator source
2408 * @ms: miliseconds until the regulator is disabled
2410 * Execute regulator_disable() on the regulator after a delay. This
2411 * is intended for use with devices that require some time to quiesce.
2413 * NOTE: this will only disable the regulator output if no other consumer
2414 * devices have it enabled, the regulator device supports disabling and
2415 * machine constraints permit this operation.
2417 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
2419 struct regulator_dev
*rdev
= regulator
->rdev
;
2421 if (regulator
->always_on
)
2425 return regulator_disable(regulator
);
2427 mutex_lock(&rdev
->mutex
);
2428 rdev
->deferred_disables
++;
2429 mutex_unlock(&rdev
->mutex
);
2431 queue_delayed_work(system_power_efficient_wq
, &rdev
->disable_work
,
2432 msecs_to_jiffies(ms
));
2435 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
2437 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
2439 /* A GPIO control always takes precedence */
2441 return rdev
->ena_gpio_state
;
2443 /* If we don't know then assume that the regulator is always on */
2444 if (!rdev
->desc
->ops
->is_enabled
)
2447 return rdev
->desc
->ops
->is_enabled(rdev
);
2450 static int _regulator_list_voltage(struct regulator
*regulator
,
2451 unsigned selector
, int lock
)
2453 struct regulator_dev
*rdev
= regulator
->rdev
;
2454 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2457 if (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1 && !selector
)
2458 return rdev
->desc
->fixed_uV
;
2460 if (ops
->list_voltage
) {
2461 if (selector
>= rdev
->desc
->n_voltages
)
2464 mutex_lock(&rdev
->mutex
);
2465 ret
= ops
->list_voltage(rdev
, selector
);
2467 mutex_unlock(&rdev
->mutex
);
2468 } else if (rdev
->supply
) {
2469 ret
= _regulator_list_voltage(rdev
->supply
, selector
, lock
);
2475 if (ret
< rdev
->constraints
->min_uV
)
2477 else if (ret
> rdev
->constraints
->max_uV
)
2485 * regulator_is_enabled - is the regulator output enabled
2486 * @regulator: regulator source
2488 * Returns positive if the regulator driver backing the source/client
2489 * has requested that the device be enabled, zero if it hasn't, else a
2490 * negative errno code.
2492 * Note that the device backing this regulator handle can have multiple
2493 * users, so it might be enabled even if regulator_enable() was never
2494 * called for this particular source.
2496 int regulator_is_enabled(struct regulator
*regulator
)
2500 if (regulator
->always_on
)
2503 mutex_lock(®ulator
->rdev
->mutex
);
2504 ret
= _regulator_is_enabled(regulator
->rdev
);
2505 mutex_unlock(®ulator
->rdev
->mutex
);
2509 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
2512 * regulator_count_voltages - count regulator_list_voltage() selectors
2513 * @regulator: regulator source
2515 * Returns number of selectors, or negative errno. Selectors are
2516 * numbered starting at zero, and typically correspond to bitfields
2517 * in hardware registers.
2519 int regulator_count_voltages(struct regulator
*regulator
)
2521 struct regulator_dev
*rdev
= regulator
->rdev
;
2523 if (rdev
->desc
->n_voltages
)
2524 return rdev
->desc
->n_voltages
;
2529 return regulator_count_voltages(rdev
->supply
);
2531 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
2534 * regulator_list_voltage - enumerate supported voltages
2535 * @regulator: regulator source
2536 * @selector: identify voltage to list
2537 * Context: can sleep
2539 * Returns a voltage that can be passed to @regulator_set_voltage(),
2540 * zero if this selector code can't be used on this system, or a
2543 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
2545 return _regulator_list_voltage(regulator
, selector
, 1);
2547 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
2550 * regulator_get_regmap - get the regulator's register map
2551 * @regulator: regulator source
2553 * Returns the register map for the given regulator, or an ERR_PTR value
2554 * if the regulator doesn't use regmap.
2556 struct regmap
*regulator_get_regmap(struct regulator
*regulator
)
2558 struct regmap
*map
= regulator
->rdev
->regmap
;
2560 return map
? map
: ERR_PTR(-EOPNOTSUPP
);
2564 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2565 * @regulator: regulator source
2566 * @vsel_reg: voltage selector register, output parameter
2567 * @vsel_mask: mask for voltage selector bitfield, output parameter
2569 * Returns the hardware register offset and bitmask used for setting the
2570 * regulator voltage. This might be useful when configuring voltage-scaling
2571 * hardware or firmware that can make I2C requests behind the kernel's back,
2574 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2575 * and 0 is returned, otherwise a negative errno is returned.
2577 int regulator_get_hardware_vsel_register(struct regulator
*regulator
,
2579 unsigned *vsel_mask
)
2581 struct regulator_dev
*rdev
= regulator
->rdev
;
2582 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2584 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2587 *vsel_reg
= rdev
->desc
->vsel_reg
;
2588 *vsel_mask
= rdev
->desc
->vsel_mask
;
2592 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register
);
2595 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2596 * @regulator: regulator source
2597 * @selector: identify voltage to list
2599 * Converts the selector to a hardware-specific voltage selector that can be
2600 * directly written to the regulator registers. The address of the voltage
2601 * register can be determined by calling @regulator_get_hardware_vsel_register.
2603 * On error a negative errno is returned.
2605 int regulator_list_hardware_vsel(struct regulator
*regulator
,
2608 struct regulator_dev
*rdev
= regulator
->rdev
;
2609 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
2611 if (selector
>= rdev
->desc
->n_voltages
)
2613 if (ops
->set_voltage_sel
!= regulator_set_voltage_sel_regmap
)
2618 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel
);
2621 * regulator_get_linear_step - return the voltage step size between VSEL values
2622 * @regulator: regulator source
2624 * Returns the voltage step size between VSEL values for linear
2625 * regulators, or return 0 if the regulator isn't a linear regulator.
2627 unsigned int regulator_get_linear_step(struct regulator
*regulator
)
2629 struct regulator_dev
*rdev
= regulator
->rdev
;
2631 return rdev
->desc
->uV_step
;
2633 EXPORT_SYMBOL_GPL(regulator_get_linear_step
);
2636 * regulator_is_supported_voltage - check if a voltage range can be supported
2638 * @regulator: Regulator to check.
2639 * @min_uV: Minimum required voltage in uV.
2640 * @max_uV: Maximum required voltage in uV.
2642 * Returns a boolean or a negative error code.
2644 int regulator_is_supported_voltage(struct regulator
*regulator
,
2645 int min_uV
, int max_uV
)
2647 struct regulator_dev
*rdev
= regulator
->rdev
;
2648 int i
, voltages
, ret
;
2650 /* If we can't change voltage check the current voltage */
2651 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
2652 ret
= regulator_get_voltage(regulator
);
2654 return min_uV
<= ret
&& ret
<= max_uV
;
2659 /* Any voltage within constrains range is fine? */
2660 if (rdev
->desc
->continuous_voltage_range
)
2661 return min_uV
>= rdev
->constraints
->min_uV
&&
2662 max_uV
<= rdev
->constraints
->max_uV
;
2664 ret
= regulator_count_voltages(regulator
);
2669 for (i
= 0; i
< voltages
; i
++) {
2670 ret
= regulator_list_voltage(regulator
, i
);
2672 if (ret
>= min_uV
&& ret
<= max_uV
)
2678 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
2680 static int regulator_map_voltage(struct regulator_dev
*rdev
, int min_uV
,
2683 const struct regulator_desc
*desc
= rdev
->desc
;
2685 if (desc
->ops
->map_voltage
)
2686 return desc
->ops
->map_voltage(rdev
, min_uV
, max_uV
);
2688 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear
)
2689 return regulator_map_voltage_linear(rdev
, min_uV
, max_uV
);
2691 if (desc
->ops
->list_voltage
== regulator_list_voltage_linear_range
)
2692 return regulator_map_voltage_linear_range(rdev
, min_uV
, max_uV
);
2694 return regulator_map_voltage_iterate(rdev
, min_uV
, max_uV
);
2697 static int _regulator_call_set_voltage(struct regulator_dev
*rdev
,
2698 int min_uV
, int max_uV
,
2701 struct pre_voltage_change_data data
;
2704 data
.old_uV
= _regulator_get_voltage(rdev
);
2705 data
.min_uV
= min_uV
;
2706 data
.max_uV
= max_uV
;
2707 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
2709 if (ret
& NOTIFY_STOP_MASK
)
2712 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
, selector
);
2716 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
2717 (void *)data
.old_uV
);
2722 static int _regulator_call_set_voltage_sel(struct regulator_dev
*rdev
,
2723 int uV
, unsigned selector
)
2725 struct pre_voltage_change_data data
;
2728 data
.old_uV
= _regulator_get_voltage(rdev
);
2731 ret
= _notifier_call_chain(rdev
, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE
,
2733 if (ret
& NOTIFY_STOP_MASK
)
2736 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
2740 _notifier_call_chain(rdev
, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE
,
2741 (void *)data
.old_uV
);
2746 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
2747 int min_uV
, int max_uV
)
2752 unsigned int selector
;
2753 int old_selector
= -1;
2755 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
2757 min_uV
+= rdev
->constraints
->uV_offset
;
2758 max_uV
+= rdev
->constraints
->uV_offset
;
2761 * If we can't obtain the old selector there is not enough
2762 * info to call set_voltage_time_sel().
2764 if (_regulator_is_enabled(rdev
) &&
2765 rdev
->desc
->ops
->set_voltage_time_sel
&&
2766 rdev
->desc
->ops
->get_voltage_sel
) {
2767 old_selector
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
2768 if (old_selector
< 0)
2769 return old_selector
;
2772 if (rdev
->desc
->ops
->set_voltage
) {
2773 ret
= _regulator_call_set_voltage(rdev
, min_uV
, max_uV
,
2777 if (rdev
->desc
->ops
->list_voltage
)
2778 best_val
= rdev
->desc
->ops
->list_voltage(rdev
,
2781 best_val
= _regulator_get_voltage(rdev
);
2784 } else if (rdev
->desc
->ops
->set_voltage_sel
) {
2785 ret
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
2787 best_val
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
2788 if (min_uV
<= best_val
&& max_uV
>= best_val
) {
2790 if (old_selector
== selector
)
2793 ret
= _regulator_call_set_voltage_sel(
2794 rdev
, best_val
, selector
);
2803 /* Call set_voltage_time_sel if successfully obtained old_selector */
2804 if (ret
== 0 && !rdev
->constraints
->ramp_disable
&& old_selector
>= 0
2805 && old_selector
!= selector
) {
2807 delay
= rdev
->desc
->ops
->set_voltage_time_sel(rdev
,
2808 old_selector
, selector
);
2810 rdev_warn(rdev
, "set_voltage_time_sel() failed: %d\n",
2815 /* Insert any necessary delays */
2816 if (delay
>= 1000) {
2817 mdelay(delay
/ 1000);
2818 udelay(delay
% 1000);
2824 if (ret
== 0 && best_val
>= 0) {
2825 unsigned long data
= best_val
;
2827 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
2831 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), best_val
);
2836 static int regulator_set_voltage_unlocked(struct regulator
*regulator
,
2837 int min_uV
, int max_uV
)
2839 struct regulator_dev
*rdev
= regulator
->rdev
;
2841 int old_min_uV
, old_max_uV
;
2843 int best_supply_uV
= 0;
2844 int supply_change_uV
= 0;
2846 /* If we're setting the same range as last time the change
2847 * should be a noop (some cpufreq implementations use the same
2848 * voltage for multiple frequencies, for example).
2850 if (regulator
->min_uV
== min_uV
&& regulator
->max_uV
== max_uV
)
2853 /* If we're trying to set a range that overlaps the current voltage,
2854 * return successfully even though the regulator does not support
2855 * changing the voltage.
2857 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_VOLTAGE
)) {
2858 current_uV
= _regulator_get_voltage(rdev
);
2859 if (min_uV
<= current_uV
&& current_uV
<= max_uV
) {
2860 regulator
->min_uV
= min_uV
;
2861 regulator
->max_uV
= max_uV
;
2867 if (!rdev
->desc
->ops
->set_voltage
&&
2868 !rdev
->desc
->ops
->set_voltage_sel
) {
2873 /* constraints check */
2874 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
2878 /* restore original values in case of error */
2879 old_min_uV
= regulator
->min_uV
;
2880 old_max_uV
= regulator
->max_uV
;
2881 regulator
->min_uV
= min_uV
;
2882 regulator
->max_uV
= max_uV
;
2884 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
2888 if (rdev
->supply
&& (rdev
->desc
->min_dropout_uV
||
2889 !rdev
->desc
->ops
->get_voltage
)) {
2890 int current_supply_uV
;
2893 selector
= regulator_map_voltage(rdev
, min_uV
, max_uV
);
2899 best_supply_uV
= _regulator_list_voltage(regulator
, selector
, 0);
2900 if (best_supply_uV
< 0) {
2901 ret
= best_supply_uV
;
2905 best_supply_uV
+= rdev
->desc
->min_dropout_uV
;
2907 current_supply_uV
= _regulator_get_voltage(rdev
->supply
->rdev
);
2908 if (current_supply_uV
< 0) {
2909 ret
= current_supply_uV
;
2913 supply_change_uV
= best_supply_uV
- current_supply_uV
;
2916 if (supply_change_uV
> 0) {
2917 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
2918 best_supply_uV
, INT_MAX
);
2920 dev_err(&rdev
->dev
, "Failed to increase supply voltage: %d\n",
2926 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
2930 if (supply_change_uV
< 0) {
2931 ret
= regulator_set_voltage_unlocked(rdev
->supply
,
2932 best_supply_uV
, INT_MAX
);
2934 dev_warn(&rdev
->dev
, "Failed to decrease supply voltage: %d\n",
2936 /* No need to fail here */
2943 regulator
->min_uV
= old_min_uV
;
2944 regulator
->max_uV
= old_max_uV
;
2950 * regulator_set_voltage - set regulator output voltage
2951 * @regulator: regulator source
2952 * @min_uV: Minimum required voltage in uV
2953 * @max_uV: Maximum acceptable voltage in uV
2955 * Sets a voltage regulator to the desired output voltage. This can be set
2956 * during any regulator state. IOW, regulator can be disabled or enabled.
2958 * If the regulator is enabled then the voltage will change to the new value
2959 * immediately otherwise if the regulator is disabled the regulator will
2960 * output at the new voltage when enabled.
2962 * NOTE: If the regulator is shared between several devices then the lowest
2963 * request voltage that meets the system constraints will be used.
2964 * Regulator system constraints must be set for this regulator before
2965 * calling this function otherwise this call will fail.
2967 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
2971 regulator_lock_supply(regulator
->rdev
);
2973 ret
= regulator_set_voltage_unlocked(regulator
, min_uV
, max_uV
);
2975 regulator_unlock_supply(regulator
->rdev
);
2979 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
2982 * regulator_set_voltage_time - get raise/fall time
2983 * @regulator: regulator source
2984 * @old_uV: starting voltage in microvolts
2985 * @new_uV: target voltage in microvolts
2987 * Provided with the starting and ending voltage, this function attempts to
2988 * calculate the time in microseconds required to rise or fall to this new
2991 int regulator_set_voltage_time(struct regulator
*regulator
,
2992 int old_uV
, int new_uV
)
2994 struct regulator_dev
*rdev
= regulator
->rdev
;
2995 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3001 /* Currently requires operations to do this */
3002 if (!ops
->list_voltage
|| !ops
->set_voltage_time_sel
3003 || !rdev
->desc
->n_voltages
)
3006 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
3007 /* We only look for exact voltage matches here */
3008 voltage
= regulator_list_voltage(regulator
, i
);
3013 if (voltage
== old_uV
)
3015 if (voltage
== new_uV
)
3019 if (old_sel
< 0 || new_sel
< 0)
3022 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
3024 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
3027 * regulator_set_voltage_time_sel - get raise/fall time
3028 * @rdev: regulator source device
3029 * @old_selector: selector for starting voltage
3030 * @new_selector: selector for target voltage
3032 * Provided with the starting and target voltage selectors, this function
3033 * returns time in microseconds required to rise or fall to this new voltage
3035 * Drivers providing ramp_delay in regulation_constraints can use this as their
3036 * set_voltage_time_sel() operation.
3038 int regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
3039 unsigned int old_selector
,
3040 unsigned int new_selector
)
3042 unsigned int ramp_delay
= 0;
3043 int old_volt
, new_volt
;
3045 if (rdev
->constraints
->ramp_delay
)
3046 ramp_delay
= rdev
->constraints
->ramp_delay
;
3047 else if (rdev
->desc
->ramp_delay
)
3048 ramp_delay
= rdev
->desc
->ramp_delay
;
3050 if (ramp_delay
== 0) {
3051 rdev_warn(rdev
, "ramp_delay not set\n");
3056 if (!rdev
->desc
->ops
->list_voltage
)
3059 old_volt
= rdev
->desc
->ops
->list_voltage(rdev
, old_selector
);
3060 new_volt
= rdev
->desc
->ops
->list_voltage(rdev
, new_selector
);
3062 return DIV_ROUND_UP(abs(new_volt
- old_volt
), ramp_delay
);
3064 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel
);
3067 * regulator_sync_voltage - re-apply last regulator output voltage
3068 * @regulator: regulator source
3070 * Re-apply the last configured voltage. This is intended to be used
3071 * where some external control source the consumer is cooperating with
3072 * has caused the configured voltage to change.
3074 int regulator_sync_voltage(struct regulator
*regulator
)
3076 struct regulator_dev
*rdev
= regulator
->rdev
;
3077 int ret
, min_uV
, max_uV
;
3079 mutex_lock(&rdev
->mutex
);
3081 if (!rdev
->desc
->ops
->set_voltage
&&
3082 !rdev
->desc
->ops
->set_voltage_sel
) {
3087 /* This is only going to work if we've had a voltage configured. */
3088 if (!regulator
->min_uV
&& !regulator
->max_uV
) {
3093 min_uV
= regulator
->min_uV
;
3094 max_uV
= regulator
->max_uV
;
3096 /* This should be a paranoia check... */
3097 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
3101 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
3105 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
3108 mutex_unlock(&rdev
->mutex
);
3111 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
3113 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
3118 if (rdev
->desc
->ops
->get_bypass
) {
3119 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypassed
);
3123 /* if bypassed the regulator must have a supply */
3124 if (!rdev
->supply
) {
3126 "bypassed regulator has no supply!\n");
3127 return -EPROBE_DEFER
;
3130 return _regulator_get_voltage(rdev
->supply
->rdev
);
3134 if (rdev
->desc
->ops
->get_voltage_sel
) {
3135 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
3138 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
3139 } else if (rdev
->desc
->ops
->get_voltage
) {
3140 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
3141 } else if (rdev
->desc
->ops
->list_voltage
) {
3142 ret
= rdev
->desc
->ops
->list_voltage(rdev
, 0);
3143 } else if (rdev
->desc
->fixed_uV
&& (rdev
->desc
->n_voltages
== 1)) {
3144 ret
= rdev
->desc
->fixed_uV
;
3145 } else if (rdev
->supply
) {
3146 ret
= _regulator_get_voltage(rdev
->supply
->rdev
);
3153 return ret
- rdev
->constraints
->uV_offset
;
3157 * regulator_get_voltage - get regulator output voltage
3158 * @regulator: regulator source
3160 * This returns the current regulator voltage in uV.
3162 * NOTE: If the regulator is disabled it will return the voltage value. This
3163 * function should not be used to determine regulator state.
3165 int regulator_get_voltage(struct regulator
*regulator
)
3169 regulator_lock_supply(regulator
->rdev
);
3171 ret
= _regulator_get_voltage(regulator
->rdev
);
3173 regulator_unlock_supply(regulator
->rdev
);
3177 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
3180 * regulator_set_current_limit - set regulator output current limit
3181 * @regulator: regulator source
3182 * @min_uA: Minimum supported current in uA
3183 * @max_uA: Maximum supported current in uA
3185 * Sets current sink to the desired output current. This can be set during
3186 * any regulator state. IOW, regulator can be disabled or enabled.
3188 * If the regulator is enabled then the current will change to the new value
3189 * immediately otherwise if the regulator is disabled the regulator will
3190 * output at the new current when enabled.
3192 * NOTE: Regulator system constraints must be set for this regulator before
3193 * calling this function otherwise this call will fail.
3195 int regulator_set_current_limit(struct regulator
*regulator
,
3196 int min_uA
, int max_uA
)
3198 struct regulator_dev
*rdev
= regulator
->rdev
;
3201 mutex_lock(&rdev
->mutex
);
3204 if (!rdev
->desc
->ops
->set_current_limit
) {
3209 /* constraints check */
3210 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
3214 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
3216 mutex_unlock(&rdev
->mutex
);
3219 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
3221 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
3225 mutex_lock(&rdev
->mutex
);
3228 if (!rdev
->desc
->ops
->get_current_limit
) {
3233 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
3235 mutex_unlock(&rdev
->mutex
);
3240 * regulator_get_current_limit - get regulator output current
3241 * @regulator: regulator source
3243 * This returns the current supplied by the specified current sink in uA.
3245 * NOTE: If the regulator is disabled it will return the current value. This
3246 * function should not be used to determine regulator state.
3248 int regulator_get_current_limit(struct regulator
*regulator
)
3250 return _regulator_get_current_limit(regulator
->rdev
);
3252 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
3255 * regulator_set_mode - set regulator operating mode
3256 * @regulator: regulator source
3257 * @mode: operating mode - one of the REGULATOR_MODE constants
3259 * Set regulator operating mode to increase regulator efficiency or improve
3260 * regulation performance.
3262 * NOTE: Regulator system constraints must be set for this regulator before
3263 * calling this function otherwise this call will fail.
3265 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
3267 struct regulator_dev
*rdev
= regulator
->rdev
;
3269 int regulator_curr_mode
;
3271 mutex_lock(&rdev
->mutex
);
3274 if (!rdev
->desc
->ops
->set_mode
) {
3279 /* return if the same mode is requested */
3280 if (rdev
->desc
->ops
->get_mode
) {
3281 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
3282 if (regulator_curr_mode
== mode
) {
3288 /* constraints check */
3289 ret
= regulator_mode_constrain(rdev
, &mode
);
3293 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
3295 mutex_unlock(&rdev
->mutex
);
3298 EXPORT_SYMBOL_GPL(regulator_set_mode
);
3300 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
3304 mutex_lock(&rdev
->mutex
);
3307 if (!rdev
->desc
->ops
->get_mode
) {
3312 ret
= rdev
->desc
->ops
->get_mode(rdev
);
3314 mutex_unlock(&rdev
->mutex
);
3319 * regulator_get_mode - get regulator operating mode
3320 * @regulator: regulator source
3322 * Get the current regulator operating mode.
3324 unsigned int regulator_get_mode(struct regulator
*regulator
)
3326 return _regulator_get_mode(regulator
->rdev
);
3328 EXPORT_SYMBOL_GPL(regulator_get_mode
);
3331 * regulator_set_load - set regulator load
3332 * @regulator: regulator source
3333 * @uA_load: load current
3335 * Notifies the regulator core of a new device load. This is then used by
3336 * DRMS (if enabled by constraints) to set the most efficient regulator
3337 * operating mode for the new regulator loading.
3339 * Consumer devices notify their supply regulator of the maximum power
3340 * they will require (can be taken from device datasheet in the power
3341 * consumption tables) when they change operational status and hence power
3342 * state. Examples of operational state changes that can affect power
3343 * consumption are :-
3345 * o Device is opened / closed.
3346 * o Device I/O is about to begin or has just finished.
3347 * o Device is idling in between work.
3349 * This information is also exported via sysfs to userspace.
3351 * DRMS will sum the total requested load on the regulator and change
3352 * to the most efficient operating mode if platform constraints allow.
3354 * On error a negative errno is returned.
3356 int regulator_set_load(struct regulator
*regulator
, int uA_load
)
3358 struct regulator_dev
*rdev
= regulator
->rdev
;
3361 mutex_lock(&rdev
->mutex
);
3362 regulator
->uA_load
= uA_load
;
3363 ret
= drms_uA_update(rdev
);
3364 mutex_unlock(&rdev
->mutex
);
3368 EXPORT_SYMBOL_GPL(regulator_set_load
);
3371 * regulator_allow_bypass - allow the regulator to go into bypass mode
3373 * @regulator: Regulator to configure
3374 * @enable: enable or disable bypass mode
3376 * Allow the regulator to go into bypass mode if all other consumers
3377 * for the regulator also enable bypass mode and the machine
3378 * constraints allow this. Bypass mode means that the regulator is
3379 * simply passing the input directly to the output with no regulation.
3381 int regulator_allow_bypass(struct regulator
*regulator
, bool enable
)
3383 struct regulator_dev
*rdev
= regulator
->rdev
;
3386 if (!rdev
->desc
->ops
->set_bypass
)
3389 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_BYPASS
))
3392 mutex_lock(&rdev
->mutex
);
3394 if (enable
&& !regulator
->bypass
) {
3395 rdev
->bypass_count
++;
3397 if (rdev
->bypass_count
== rdev
->open_count
) {
3398 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3400 rdev
->bypass_count
--;
3403 } else if (!enable
&& regulator
->bypass
) {
3404 rdev
->bypass_count
--;
3406 if (rdev
->bypass_count
!= rdev
->open_count
) {
3407 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3409 rdev
->bypass_count
++;
3414 regulator
->bypass
= enable
;
3416 mutex_unlock(&rdev
->mutex
);
3420 EXPORT_SYMBOL_GPL(regulator_allow_bypass
);
3423 * regulator_register_notifier - register regulator event notifier
3424 * @regulator: regulator source
3425 * @nb: notifier block
3427 * Register notifier block to receive regulator events.
3429 int regulator_register_notifier(struct regulator
*regulator
,
3430 struct notifier_block
*nb
)
3432 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
3435 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
3438 * regulator_unregister_notifier - unregister regulator event notifier
3439 * @regulator: regulator source
3440 * @nb: notifier block
3442 * Unregister regulator event notifier block.
3444 int regulator_unregister_notifier(struct regulator
*regulator
,
3445 struct notifier_block
*nb
)
3447 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
3450 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
3452 /* notify regulator consumers and downstream regulator consumers.
3453 * Note mutex must be held by caller.
3455 static int _notifier_call_chain(struct regulator_dev
*rdev
,
3456 unsigned long event
, void *data
)
3458 /* call rdev chain first */
3459 return blocking_notifier_call_chain(&rdev
->notifier
, event
, data
);
3463 * regulator_bulk_get - get multiple regulator consumers
3465 * @dev: Device to supply
3466 * @num_consumers: Number of consumers to register
3467 * @consumers: Configuration of consumers; clients are stored here.
3469 * @return 0 on success, an errno on failure.
3471 * This helper function allows drivers to get several regulator
3472 * consumers in one operation. If any of the regulators cannot be
3473 * acquired then any regulators that were allocated will be freed
3474 * before returning to the caller.
3476 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
3477 struct regulator_bulk_data
*consumers
)
3482 for (i
= 0; i
< num_consumers
; i
++)
3483 consumers
[i
].consumer
= NULL
;
3485 for (i
= 0; i
< num_consumers
; i
++) {
3486 consumers
[i
].consumer
= _regulator_get(dev
,
3487 consumers
[i
].supply
,
3489 !consumers
[i
].optional
);
3490 if (IS_ERR(consumers
[i
].consumer
)) {
3491 ret
= PTR_ERR(consumers
[i
].consumer
);
3492 dev_err(dev
, "Failed to get supply '%s': %d\n",
3493 consumers
[i
].supply
, ret
);
3494 consumers
[i
].consumer
= NULL
;
3503 regulator_put(consumers
[i
].consumer
);
3507 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
3509 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
3511 struct regulator_bulk_data
*bulk
= data
;
3513 bulk
->ret
= regulator_enable(bulk
->consumer
);
3517 * regulator_bulk_enable - enable multiple regulator consumers
3519 * @num_consumers: Number of consumers
3520 * @consumers: Consumer data; clients are stored here.
3521 * @return 0 on success, an errno on failure
3523 * This convenience API allows consumers to enable multiple regulator
3524 * clients in a single API call. If any consumers cannot be enabled
3525 * then any others that were enabled will be disabled again prior to
3528 int regulator_bulk_enable(int num_consumers
,
3529 struct regulator_bulk_data
*consumers
)
3531 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
3535 for (i
= 0; i
< num_consumers
; i
++) {
3536 if (consumers
[i
].consumer
->always_on
)
3537 consumers
[i
].ret
= 0;
3539 async_schedule_domain(regulator_bulk_enable_async
,
3540 &consumers
[i
], &async_domain
);
3543 async_synchronize_full_domain(&async_domain
);
3545 /* If any consumer failed we need to unwind any that succeeded */
3546 for (i
= 0; i
< num_consumers
; i
++) {
3547 if (consumers
[i
].ret
!= 0) {
3548 ret
= consumers
[i
].ret
;
3556 for (i
= 0; i
< num_consumers
; i
++) {
3557 if (consumers
[i
].ret
< 0)
3558 pr_err("Failed to enable %s: %d\n", consumers
[i
].supply
,
3561 regulator_disable(consumers
[i
].consumer
);
3566 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
3569 * regulator_bulk_disable - disable multiple regulator consumers
3571 * @num_consumers: Number of consumers
3572 * @consumers: Consumer data; clients are stored here.
3573 * @return 0 on success, an errno on failure
3575 * This convenience API allows consumers to disable multiple regulator
3576 * clients in a single API call. If any consumers cannot be disabled
3577 * then any others that were disabled will be enabled again prior to
3580 int regulator_bulk_disable(int num_consumers
,
3581 struct regulator_bulk_data
*consumers
)
3586 for (i
= num_consumers
- 1; i
>= 0; --i
) {
3587 ret
= regulator_disable(consumers
[i
].consumer
);
3595 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
3596 for (++i
; i
< num_consumers
; ++i
) {
3597 r
= regulator_enable(consumers
[i
].consumer
);
3599 pr_err("Failed to reename %s: %d\n",
3600 consumers
[i
].supply
, r
);
3605 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
3608 * regulator_bulk_force_disable - force disable multiple regulator consumers
3610 * @num_consumers: Number of consumers
3611 * @consumers: Consumer data; clients are stored here.
3612 * @return 0 on success, an errno on failure
3614 * This convenience API allows consumers to forcibly disable multiple regulator
3615 * clients in a single API call.
3616 * NOTE: This should be used for situations when device damage will
3617 * likely occur if the regulators are not disabled (e.g. over temp).
3618 * Although regulator_force_disable function call for some consumers can
3619 * return error numbers, the function is called for all consumers.
3621 int regulator_bulk_force_disable(int num_consumers
,
3622 struct regulator_bulk_data
*consumers
)
3627 for (i
= 0; i
< num_consumers
; i
++)
3629 regulator_force_disable(consumers
[i
].consumer
);
3631 for (i
= 0; i
< num_consumers
; i
++) {
3632 if (consumers
[i
].ret
!= 0) {
3633 ret
= consumers
[i
].ret
;
3642 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
3645 * regulator_bulk_free - free multiple regulator consumers
3647 * @num_consumers: Number of consumers
3648 * @consumers: Consumer data; clients are stored here.
3650 * This convenience API allows consumers to free multiple regulator
3651 * clients in a single API call.
3653 void regulator_bulk_free(int num_consumers
,
3654 struct regulator_bulk_data
*consumers
)
3658 for (i
= 0; i
< num_consumers
; i
++) {
3659 regulator_put(consumers
[i
].consumer
);
3660 consumers
[i
].consumer
= NULL
;
3663 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
3666 * regulator_notifier_call_chain - call regulator event notifier
3667 * @rdev: regulator source
3668 * @event: notifier block
3669 * @data: callback-specific data.
3671 * Called by regulator drivers to notify clients a regulator event has
3672 * occurred. We also notify regulator clients downstream.
3673 * Note lock must be held by caller.
3675 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
3676 unsigned long event
, void *data
)
3678 lockdep_assert_held_once(&rdev
->mutex
);
3680 _notifier_call_chain(rdev
, event
, data
);
3684 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
3687 * regulator_mode_to_status - convert a regulator mode into a status
3689 * @mode: Mode to convert
3691 * Convert a regulator mode into a status.
3693 int regulator_mode_to_status(unsigned int mode
)
3696 case REGULATOR_MODE_FAST
:
3697 return REGULATOR_STATUS_FAST
;
3698 case REGULATOR_MODE_NORMAL
:
3699 return REGULATOR_STATUS_NORMAL
;
3700 case REGULATOR_MODE_IDLE
:
3701 return REGULATOR_STATUS_IDLE
;
3702 case REGULATOR_MODE_STANDBY
:
3703 return REGULATOR_STATUS_STANDBY
;
3705 return REGULATOR_STATUS_UNDEFINED
;
3708 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
3710 static struct attribute
*regulator_dev_attrs
[] = {
3711 &dev_attr_name
.attr
,
3712 &dev_attr_num_users
.attr
,
3713 &dev_attr_type
.attr
,
3714 &dev_attr_microvolts
.attr
,
3715 &dev_attr_microamps
.attr
,
3716 &dev_attr_opmode
.attr
,
3717 &dev_attr_state
.attr
,
3718 &dev_attr_status
.attr
,
3719 &dev_attr_bypass
.attr
,
3720 &dev_attr_requested_microamps
.attr
,
3721 &dev_attr_min_microvolts
.attr
,
3722 &dev_attr_max_microvolts
.attr
,
3723 &dev_attr_min_microamps
.attr
,
3724 &dev_attr_max_microamps
.attr
,
3725 &dev_attr_suspend_standby_state
.attr
,
3726 &dev_attr_suspend_mem_state
.attr
,
3727 &dev_attr_suspend_disk_state
.attr
,
3728 &dev_attr_suspend_standby_microvolts
.attr
,
3729 &dev_attr_suspend_mem_microvolts
.attr
,
3730 &dev_attr_suspend_disk_microvolts
.attr
,
3731 &dev_attr_suspend_standby_mode
.attr
,
3732 &dev_attr_suspend_mem_mode
.attr
,
3733 &dev_attr_suspend_disk_mode
.attr
,
3738 * To avoid cluttering sysfs (and memory) with useless state, only
3739 * create attributes that can be meaningfully displayed.
3741 static umode_t
regulator_attr_is_visible(struct kobject
*kobj
,
3742 struct attribute
*attr
, int idx
)
3744 struct device
*dev
= kobj_to_dev(kobj
);
3745 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
3746 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
3747 umode_t mode
= attr
->mode
;
3749 /* these three are always present */
3750 if (attr
== &dev_attr_name
.attr
||
3751 attr
== &dev_attr_num_users
.attr
||
3752 attr
== &dev_attr_type
.attr
)
3755 /* some attributes need specific methods to be displayed */
3756 if (attr
== &dev_attr_microvolts
.attr
) {
3757 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
3758 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0) ||
3759 (ops
->list_voltage
&& ops
->list_voltage(rdev
, 0) >= 0) ||
3760 (rdev
->desc
->fixed_uV
&& rdev
->desc
->n_voltages
== 1))
3765 if (attr
== &dev_attr_microamps
.attr
)
3766 return ops
->get_current_limit
? mode
: 0;
3768 if (attr
== &dev_attr_opmode
.attr
)
3769 return ops
->get_mode
? mode
: 0;
3771 if (attr
== &dev_attr_state
.attr
)
3772 return (rdev
->ena_pin
|| ops
->is_enabled
) ? mode
: 0;
3774 if (attr
== &dev_attr_status
.attr
)
3775 return ops
->get_status
? mode
: 0;
3777 if (attr
== &dev_attr_bypass
.attr
)
3778 return ops
->get_bypass
? mode
: 0;
3780 /* some attributes are type-specific */
3781 if (attr
== &dev_attr_requested_microamps
.attr
)
3782 return rdev
->desc
->type
== REGULATOR_CURRENT
? mode
: 0;
3784 /* constraints need specific supporting methods */
3785 if (attr
== &dev_attr_min_microvolts
.attr
||
3786 attr
== &dev_attr_max_microvolts
.attr
)
3787 return (ops
->set_voltage
|| ops
->set_voltage_sel
) ? mode
: 0;
3789 if (attr
== &dev_attr_min_microamps
.attr
||
3790 attr
== &dev_attr_max_microamps
.attr
)
3791 return ops
->set_current_limit
? mode
: 0;
3793 if (attr
== &dev_attr_suspend_standby_state
.attr
||
3794 attr
== &dev_attr_suspend_mem_state
.attr
||
3795 attr
== &dev_attr_suspend_disk_state
.attr
)
3798 if (attr
== &dev_attr_suspend_standby_microvolts
.attr
||
3799 attr
== &dev_attr_suspend_mem_microvolts
.attr
||
3800 attr
== &dev_attr_suspend_disk_microvolts
.attr
)
3801 return ops
->set_suspend_voltage
? mode
: 0;
3803 if (attr
== &dev_attr_suspend_standby_mode
.attr
||
3804 attr
== &dev_attr_suspend_mem_mode
.attr
||
3805 attr
== &dev_attr_suspend_disk_mode
.attr
)
3806 return ops
->set_suspend_mode
? mode
: 0;
3811 static const struct attribute_group regulator_dev_group
= {
3812 .attrs
= regulator_dev_attrs
,
3813 .is_visible
= regulator_attr_is_visible
,
3816 static const struct attribute_group
*regulator_dev_groups
[] = {
3817 ®ulator_dev_group
,
3821 static void regulator_dev_release(struct device
*dev
)
3823 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
3825 kfree(rdev
->constraints
);
3826 of_node_put(rdev
->dev
.of_node
);
3830 static struct class regulator_class
= {
3831 .name
= "regulator",
3832 .dev_release
= regulator_dev_release
,
3833 .dev_groups
= regulator_dev_groups
,
3836 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
3838 struct device
*parent
= rdev
->dev
.parent
;
3839 const char *rname
= rdev_get_name(rdev
);
3840 char name
[NAME_MAX
];
3842 /* Avoid duplicate debugfs directory names */
3843 if (parent
&& rname
== rdev
->desc
->name
) {
3844 snprintf(name
, sizeof(name
), "%s-%s", dev_name(parent
),
3849 rdev
->debugfs
= debugfs_create_dir(rname
, debugfs_root
);
3850 if (!rdev
->debugfs
) {
3851 rdev_warn(rdev
, "Failed to create debugfs directory\n");
3855 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
3857 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
3859 debugfs_create_u32("bypass_count", 0444, rdev
->debugfs
,
3860 &rdev
->bypass_count
);
3863 static int regulator_register_resolve_supply(struct device
*dev
, void *data
)
3865 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
3867 if (regulator_resolve_supply(rdev
))
3868 rdev_dbg(rdev
, "unable to resolve supply\n");
3874 * regulator_register - register regulator
3875 * @regulator_desc: regulator to register
3876 * @cfg: runtime configuration for regulator
3878 * Called by regulator drivers to register a regulator.
3879 * Returns a valid pointer to struct regulator_dev on success
3880 * or an ERR_PTR() on error.
3882 struct regulator_dev
*
3883 regulator_register(const struct regulator_desc
*regulator_desc
,
3884 const struct regulator_config
*cfg
)
3886 const struct regulation_constraints
*constraints
= NULL
;
3887 const struct regulator_init_data
*init_data
;
3888 struct regulator_config
*config
= NULL
;
3889 static atomic_t regulator_no
= ATOMIC_INIT(-1);
3890 struct regulator_dev
*rdev
;
3894 if (regulator_desc
== NULL
|| cfg
== NULL
)
3895 return ERR_PTR(-EINVAL
);
3900 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
3901 return ERR_PTR(-EINVAL
);
3903 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
3904 regulator_desc
->type
!= REGULATOR_CURRENT
)
3905 return ERR_PTR(-EINVAL
);
3907 /* Only one of each should be implemented */
3908 WARN_ON(regulator_desc
->ops
->get_voltage
&&
3909 regulator_desc
->ops
->get_voltage_sel
);
3910 WARN_ON(regulator_desc
->ops
->set_voltage
&&
3911 regulator_desc
->ops
->set_voltage_sel
);
3913 /* If we're using selectors we must implement list_voltage. */
3914 if (regulator_desc
->ops
->get_voltage_sel
&&
3915 !regulator_desc
->ops
->list_voltage
) {
3916 return ERR_PTR(-EINVAL
);
3918 if (regulator_desc
->ops
->set_voltage_sel
&&
3919 !regulator_desc
->ops
->list_voltage
) {
3920 return ERR_PTR(-EINVAL
);
3923 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
3925 return ERR_PTR(-ENOMEM
);
3928 * Duplicate the config so the driver could override it after
3929 * parsing init data.
3931 config
= kmemdup(cfg
, sizeof(*cfg
), GFP_KERNEL
);
3932 if (config
== NULL
) {
3934 return ERR_PTR(-ENOMEM
);
3937 init_data
= regulator_of_get_init_data(dev
, regulator_desc
, config
,
3938 &rdev
->dev
.of_node
);
3940 init_data
= config
->init_data
;
3941 rdev
->dev
.of_node
= of_node_get(config
->of_node
);
3944 mutex_init(&rdev
->mutex
);
3945 rdev
->reg_data
= config
->driver_data
;
3946 rdev
->owner
= regulator_desc
->owner
;
3947 rdev
->desc
= regulator_desc
;
3949 rdev
->regmap
= config
->regmap
;
3950 else if (dev_get_regmap(dev
, NULL
))
3951 rdev
->regmap
= dev_get_regmap(dev
, NULL
);
3952 else if (dev
->parent
)
3953 rdev
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
3954 INIT_LIST_HEAD(&rdev
->consumer_list
);
3955 INIT_LIST_HEAD(&rdev
->list
);
3956 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
3957 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
3959 /* preform any regulator specific init */
3960 if (init_data
&& init_data
->regulator_init
) {
3961 ret
= init_data
->regulator_init(rdev
->reg_data
);
3966 if ((config
->ena_gpio
|| config
->ena_gpio_initialized
) &&
3967 gpio_is_valid(config
->ena_gpio
)) {
3968 mutex_lock(®ulator_list_mutex
);
3969 ret
= regulator_ena_gpio_request(rdev
, config
);
3970 mutex_unlock(®ulator_list_mutex
);
3972 rdev_err(rdev
, "Failed to request enable GPIO%d: %d\n",
3973 config
->ena_gpio
, ret
);
3978 /* register with sysfs */
3979 rdev
->dev
.class = ®ulator_class
;
3980 rdev
->dev
.parent
= dev
;
3981 dev_set_name(&rdev
->dev
, "regulator.%lu",
3982 (unsigned long) atomic_inc_return(®ulator_no
));
3984 /* set regulator constraints */
3986 constraints
= &init_data
->constraints
;
3988 if (init_data
&& init_data
->supply_regulator
)
3989 rdev
->supply_name
= init_data
->supply_regulator
;
3990 else if (regulator_desc
->supply_name
)
3991 rdev
->supply_name
= regulator_desc
->supply_name
;
3994 * Attempt to resolve the regulator supply, if specified,
3995 * but don't return an error if we fail because we will try
3996 * to resolve it again later as more regulators are added.
3998 if (regulator_resolve_supply(rdev
))
3999 rdev_dbg(rdev
, "unable to resolve supply\n");
4001 ret
= set_machine_constraints(rdev
, constraints
);
4005 /* add consumers devices */
4007 mutex_lock(®ulator_list_mutex
);
4008 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
4009 ret
= set_consumer_device_supply(rdev
,
4010 init_data
->consumer_supplies
[i
].dev_name
,
4011 init_data
->consumer_supplies
[i
].supply
);
4013 mutex_unlock(®ulator_list_mutex
);
4014 dev_err(dev
, "Failed to set supply %s\n",
4015 init_data
->consumer_supplies
[i
].supply
);
4016 goto unset_supplies
;
4019 mutex_unlock(®ulator_list_mutex
);
4022 ret
= device_register(&rdev
->dev
);
4024 put_device(&rdev
->dev
);
4025 goto unset_supplies
;
4028 dev_set_drvdata(&rdev
->dev
, rdev
);
4029 rdev_init_debugfs(rdev
);
4031 /* try to resolve regulators supply since a new one was registered */
4032 class_for_each_device(®ulator_class
, NULL
, NULL
,
4033 regulator_register_resolve_supply
);
4038 mutex_lock(®ulator_list_mutex
);
4039 unset_regulator_supplies(rdev
);
4040 mutex_unlock(®ulator_list_mutex
);
4042 kfree(rdev
->constraints
);
4043 mutex_lock(®ulator_list_mutex
);
4044 regulator_ena_gpio_free(rdev
);
4045 mutex_unlock(®ulator_list_mutex
);
4049 return ERR_PTR(ret
);
4051 EXPORT_SYMBOL_GPL(regulator_register
);
4054 * regulator_unregister - unregister regulator
4055 * @rdev: regulator to unregister
4057 * Called by regulator drivers to unregister a regulator.
4059 void regulator_unregister(struct regulator_dev
*rdev
)
4065 while (rdev
->use_count
--)
4066 regulator_disable(rdev
->supply
);
4067 regulator_put(rdev
->supply
);
4069 mutex_lock(®ulator_list_mutex
);
4070 debugfs_remove_recursive(rdev
->debugfs
);
4071 flush_work(&rdev
->disable_work
.work
);
4072 WARN_ON(rdev
->open_count
);
4073 unset_regulator_supplies(rdev
);
4074 list_del(&rdev
->list
);
4075 regulator_ena_gpio_free(rdev
);
4076 mutex_unlock(®ulator_list_mutex
);
4077 device_unregister(&rdev
->dev
);
4079 EXPORT_SYMBOL_GPL(regulator_unregister
);
4081 static int _regulator_suspend_prepare(struct device
*dev
, void *data
)
4083 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4084 const suspend_state_t
*state
= data
;
4087 mutex_lock(&rdev
->mutex
);
4088 ret
= suspend_prepare(rdev
, *state
);
4089 mutex_unlock(&rdev
->mutex
);
4095 * regulator_suspend_prepare - prepare regulators for system wide suspend
4096 * @state: system suspend state
4098 * Configure each regulator with it's suspend operating parameters for state.
4099 * This will usually be called by machine suspend code prior to supending.
4101 int regulator_suspend_prepare(suspend_state_t state
)
4103 /* ON is handled by regulator active state */
4104 if (state
== PM_SUSPEND_ON
)
4107 return class_for_each_device(®ulator_class
, NULL
, &state
,
4108 _regulator_suspend_prepare
);
4110 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
4112 static int _regulator_suspend_finish(struct device
*dev
, void *data
)
4114 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4117 mutex_lock(&rdev
->mutex
);
4118 if (rdev
->use_count
> 0 || rdev
->constraints
->always_on
) {
4119 if (!_regulator_is_enabled(rdev
)) {
4120 ret
= _regulator_do_enable(rdev
);
4123 "Failed to resume regulator %d\n",
4127 if (!have_full_constraints())
4129 if (!_regulator_is_enabled(rdev
))
4132 ret
= _regulator_do_disable(rdev
);
4134 dev_err(dev
, "Failed to suspend regulator %d\n", ret
);
4137 mutex_unlock(&rdev
->mutex
);
4139 /* Keep processing regulators in spite of any errors */
4144 * regulator_suspend_finish - resume regulators from system wide suspend
4146 * Turn on regulators that might be turned off by regulator_suspend_prepare
4147 * and that should be turned on according to the regulators properties.
4149 int regulator_suspend_finish(void)
4151 return class_for_each_device(®ulator_class
, NULL
, NULL
,
4152 _regulator_suspend_finish
);
4154 EXPORT_SYMBOL_GPL(regulator_suspend_finish
);
4157 * regulator_has_full_constraints - the system has fully specified constraints
4159 * Calling this function will cause the regulator API to disable all
4160 * regulators which have a zero use count and don't have an always_on
4161 * constraint in a late_initcall.
4163 * The intention is that this will become the default behaviour in a
4164 * future kernel release so users are encouraged to use this facility
4167 void regulator_has_full_constraints(void)
4169 has_full_constraints
= 1;
4171 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
4174 * rdev_get_drvdata - get rdev regulator driver data
4177 * Get rdev regulator driver private data. This call can be used in the
4178 * regulator driver context.
4180 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
4182 return rdev
->reg_data
;
4184 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
4187 * regulator_get_drvdata - get regulator driver data
4188 * @regulator: regulator
4190 * Get regulator driver private data. This call can be used in the consumer
4191 * driver context when non API regulator specific functions need to be called.
4193 void *regulator_get_drvdata(struct regulator
*regulator
)
4195 return regulator
->rdev
->reg_data
;
4197 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
4200 * regulator_set_drvdata - set regulator driver data
4201 * @regulator: regulator
4204 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
4206 regulator
->rdev
->reg_data
= data
;
4208 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
4211 * regulator_get_id - get regulator ID
4214 int rdev_get_id(struct regulator_dev
*rdev
)
4216 return rdev
->desc
->id
;
4218 EXPORT_SYMBOL_GPL(rdev_get_id
);
4220 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
4224 EXPORT_SYMBOL_GPL(rdev_get_dev
);
4226 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
4228 return reg_init_data
->driver_data
;
4230 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
4232 #ifdef CONFIG_DEBUG_FS
4233 static ssize_t
supply_map_read_file(struct file
*file
, char __user
*user_buf
,
4234 size_t count
, loff_t
*ppos
)
4236 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
4237 ssize_t len
, ret
= 0;
4238 struct regulator_map
*map
;
4243 list_for_each_entry(map
, ®ulator_map_list
, list
) {
4244 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
4246 rdev_get_name(map
->regulator
), map
->dev_name
,
4250 if (ret
> PAGE_SIZE
) {
4256 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
4264 static const struct file_operations supply_map_fops
= {
4265 #ifdef CONFIG_DEBUG_FS
4266 .read
= supply_map_read_file
,
4267 .llseek
= default_llseek
,
4271 #ifdef CONFIG_DEBUG_FS
4272 struct summary_data
{
4274 struct regulator_dev
*parent
;
4278 static void regulator_summary_show_subtree(struct seq_file
*s
,
4279 struct regulator_dev
*rdev
,
4282 static int regulator_summary_show_children(struct device
*dev
, void *data
)
4284 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4285 struct summary_data
*summary_data
= data
;
4287 if (rdev
->supply
&& rdev
->supply
->rdev
== summary_data
->parent
)
4288 regulator_summary_show_subtree(summary_data
->s
, rdev
,
4289 summary_data
->level
+ 1);
4294 static void regulator_summary_show_subtree(struct seq_file
*s
,
4295 struct regulator_dev
*rdev
,
4298 struct regulation_constraints
*c
;
4299 struct regulator
*consumer
;
4300 struct summary_data summary_data
;
4305 seq_printf(s
, "%*s%-*s %3d %4d %6d ",
4307 30 - level
* 3, rdev_get_name(rdev
),
4308 rdev
->use_count
, rdev
->open_count
, rdev
->bypass_count
);
4310 seq_printf(s
, "%5dmV ", _regulator_get_voltage(rdev
) / 1000);
4311 seq_printf(s
, "%5dmA ", _regulator_get_current_limit(rdev
) / 1000);
4313 c
= rdev
->constraints
;
4315 switch (rdev
->desc
->type
) {
4316 case REGULATOR_VOLTAGE
:
4317 seq_printf(s
, "%5dmV %5dmV ",
4318 c
->min_uV
/ 1000, c
->max_uV
/ 1000);
4320 case REGULATOR_CURRENT
:
4321 seq_printf(s
, "%5dmA %5dmA ",
4322 c
->min_uA
/ 1000, c
->max_uA
/ 1000);
4329 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
) {
4330 if (consumer
->dev
->class == ®ulator_class
)
4333 seq_printf(s
, "%*s%-*s ",
4334 (level
+ 1) * 3 + 1, "",
4335 30 - (level
+ 1) * 3, dev_name(consumer
->dev
));
4337 switch (rdev
->desc
->type
) {
4338 case REGULATOR_VOLTAGE
:
4339 seq_printf(s
, "%37dmV %5dmV",
4340 consumer
->min_uV
/ 1000,
4341 consumer
->max_uV
/ 1000);
4343 case REGULATOR_CURRENT
:
4351 summary_data
.level
= level
;
4352 summary_data
.parent
= rdev
;
4354 class_for_each_device(®ulator_class
, NULL
, &summary_data
,
4355 regulator_summary_show_children
);
4358 static int regulator_summary_show_roots(struct device
*dev
, void *data
)
4360 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4361 struct seq_file
*s
= data
;
4364 regulator_summary_show_subtree(s
, rdev
, 0);
4369 static int regulator_summary_show(struct seq_file
*s
, void *data
)
4371 seq_puts(s
, " regulator use open bypass voltage current min max\n");
4372 seq_puts(s
, "-------------------------------------------------------------------------------\n");
4374 class_for_each_device(®ulator_class
, NULL
, s
,
4375 regulator_summary_show_roots
);
4380 static int regulator_summary_open(struct inode
*inode
, struct file
*file
)
4382 return single_open(file
, regulator_summary_show
, inode
->i_private
);
4386 static const struct file_operations regulator_summary_fops
= {
4387 #ifdef CONFIG_DEBUG_FS
4388 .open
= regulator_summary_open
,
4390 .llseek
= seq_lseek
,
4391 .release
= single_release
,
4395 static int __init
regulator_init(void)
4399 ret
= class_register(®ulator_class
);
4401 debugfs_root
= debugfs_create_dir("regulator", NULL
);
4403 pr_warn("regulator: Failed to create debugfs directory\n");
4405 debugfs_create_file("supply_map", 0444, debugfs_root
, NULL
,
4408 debugfs_create_file("regulator_summary", 0444, debugfs_root
,
4409 NULL
, ®ulator_summary_fops
);
4411 regulator_dummy_init();
4416 /* init early to allow our consumers to complete system booting */
4417 core_initcall(regulator_init
);
4419 static int __init
regulator_late_cleanup(struct device
*dev
, void *data
)
4421 struct regulator_dev
*rdev
= dev_to_rdev(dev
);
4422 const struct regulator_ops
*ops
= rdev
->desc
->ops
;
4423 struct regulation_constraints
*c
= rdev
->constraints
;
4426 if (c
&& c
->always_on
)
4429 if (!regulator_ops_is_valid(rdev
, REGULATOR_CHANGE_STATUS
))
4432 mutex_lock(&rdev
->mutex
);
4434 if (rdev
->use_count
)
4437 /* If we can't read the status assume it's on. */
4438 if (ops
->is_enabled
)
4439 enabled
= ops
->is_enabled(rdev
);
4446 if (have_full_constraints()) {
4447 /* We log since this may kill the system if it goes
4449 rdev_info(rdev
, "disabling\n");
4450 ret
= _regulator_do_disable(rdev
);
4452 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
4454 /* The intention is that in future we will
4455 * assume that full constraints are provided
4456 * so warn even if we aren't going to do
4459 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
4463 mutex_unlock(&rdev
->mutex
);
4468 static int __init
regulator_init_complete(void)
4471 * Since DT doesn't provide an idiomatic mechanism for
4472 * enabling full constraints and since it's much more natural
4473 * with DT to provide them just assume that a DT enabled
4474 * system has full constraints.
4476 if (of_have_populated_dt())
4477 has_full_constraints
= true;
4479 /* If we have a full configuration then disable any regulators
4480 * we have permission to change the status for and which are
4481 * not in use or always_on. This is effectively the default
4482 * for DT and ACPI as they have full constraints.
4484 class_for_each_device(®ulator_class
, NULL
, NULL
,
4485 regulator_late_cleanup
);
4489 late_initcall_sync(regulator_init_complete
);