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>
28 #include <linux/regmap.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/module.h>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/regulator.h>
40 #define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51 static DEFINE_MUTEX(regulator_list_mutex
);
52 static LIST_HEAD(regulator_list
);
53 static LIST_HEAD(regulator_map_list
);
54 static LIST_HEAD(regulator_ena_gpio_list
);
55 static bool has_full_constraints
;
56 static bool board_wants_dummy_regulator
;
58 static struct dentry
*debugfs_root
;
61 * struct regulator_map
63 * Used to provide symbolic supply names to devices.
65 struct regulator_map
{
66 struct list_head list
;
67 const char *dev_name
; /* The dev_name() for the consumer */
69 struct regulator_dev
*regulator
;
73 * struct regulator_enable_gpio
75 * Management for shared enable GPIO pin
77 struct regulator_enable_gpio
{
78 struct list_head list
;
80 u32 enable_count
; /* a number of enabled shared GPIO */
81 u32 request_count
; /* a number of requested shared GPIO */
82 unsigned int ena_gpio_invert
:1;
88 * One for each consumer device.
92 struct list_head list
;
93 unsigned int always_on
:1;
94 unsigned int bypass
:1;
99 struct device_attribute dev_attr
;
100 struct regulator_dev
*rdev
;
101 struct dentry
*debugfs
;
104 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
105 static int _regulator_disable(struct regulator_dev
*rdev
);
106 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
107 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
108 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
109 static void _notifier_call_chain(struct regulator_dev
*rdev
,
110 unsigned long event
, void *data
);
111 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
112 int min_uV
, int max_uV
);
113 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
115 const char *supply_name
);
117 static const char *rdev_get_name(struct regulator_dev
*rdev
)
119 if (rdev
->constraints
&& rdev
->constraints
->name
)
120 return rdev
->constraints
->name
;
121 else if (rdev
->desc
->name
)
122 return rdev
->desc
->name
;
128 * of_get_regulator - get a regulator device node based on supply name
129 * @dev: Device pointer for the consumer (of regulator) device
130 * @supply: regulator supply name
132 * Extract the regulator device node corresponding to the supply name.
133 * returns the device node corresponding to the regulator if found, else
136 static struct device_node
*of_get_regulator(struct device
*dev
, const char *supply
)
138 struct device_node
*regnode
= NULL
;
139 char prop_name
[32]; /* 32 is max size of property name */
141 dev_dbg(dev
, "Looking up %s-supply from device tree\n", supply
);
143 snprintf(prop_name
, 32, "%s-supply", supply
);
144 regnode
= of_parse_phandle(dev
->of_node
, prop_name
, 0);
147 dev_dbg(dev
, "Looking up %s property in node %s failed",
148 prop_name
, dev
->of_node
->full_name
);
154 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
156 if (!rdev
->constraints
)
159 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
165 /* Platform voltage constraint check */
166 static int regulator_check_voltage(struct regulator_dev
*rdev
,
167 int *min_uV
, int *max_uV
)
169 BUG_ON(*min_uV
> *max_uV
);
171 if (!rdev
->constraints
) {
172 rdev_err(rdev
, "no constraints\n");
175 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
176 rdev_err(rdev
, "operation not allowed\n");
180 if (*max_uV
> rdev
->constraints
->max_uV
)
181 *max_uV
= rdev
->constraints
->max_uV
;
182 if (*min_uV
< rdev
->constraints
->min_uV
)
183 *min_uV
= rdev
->constraints
->min_uV
;
185 if (*min_uV
> *max_uV
) {
186 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
194 /* Make sure we select a voltage that suits the needs of all
195 * regulator consumers
197 static int regulator_check_consumers(struct regulator_dev
*rdev
,
198 int *min_uV
, int *max_uV
)
200 struct regulator
*regulator
;
202 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
204 * Assume consumers that didn't say anything are OK
205 * with anything in the constraint range.
207 if (!regulator
->min_uV
&& !regulator
->max_uV
)
210 if (*max_uV
> regulator
->max_uV
)
211 *max_uV
= regulator
->max_uV
;
212 if (*min_uV
< regulator
->min_uV
)
213 *min_uV
= regulator
->min_uV
;
216 if (*min_uV
> *max_uV
) {
217 rdev_err(rdev
, "Restricting voltage, %u-%uuV\n",
225 /* current constraint check */
226 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
227 int *min_uA
, int *max_uA
)
229 BUG_ON(*min_uA
> *max_uA
);
231 if (!rdev
->constraints
) {
232 rdev_err(rdev
, "no constraints\n");
235 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
236 rdev_err(rdev
, "operation not allowed\n");
240 if (*max_uA
> rdev
->constraints
->max_uA
)
241 *max_uA
= rdev
->constraints
->max_uA
;
242 if (*min_uA
< rdev
->constraints
->min_uA
)
243 *min_uA
= rdev
->constraints
->min_uA
;
245 if (*min_uA
> *max_uA
) {
246 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
254 /* operating mode constraint check */
255 static int regulator_mode_constrain(struct regulator_dev
*rdev
, int *mode
)
258 case REGULATOR_MODE_FAST
:
259 case REGULATOR_MODE_NORMAL
:
260 case REGULATOR_MODE_IDLE
:
261 case REGULATOR_MODE_STANDBY
:
264 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
268 if (!rdev
->constraints
) {
269 rdev_err(rdev
, "no constraints\n");
272 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
273 rdev_err(rdev
, "operation not allowed\n");
277 /* The modes are bitmasks, the most power hungry modes having
278 * the lowest values. If the requested mode isn't supported
279 * try higher modes. */
281 if (rdev
->constraints
->valid_modes_mask
& *mode
)
289 /* dynamic regulator mode switching constraint check */
290 static int regulator_check_drms(struct regulator_dev
*rdev
)
292 if (!rdev
->constraints
) {
293 rdev_err(rdev
, "no constraints\n");
296 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
297 rdev_err(rdev
, "operation not allowed\n");
303 static ssize_t
regulator_uV_show(struct device
*dev
,
304 struct device_attribute
*attr
, char *buf
)
306 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
309 mutex_lock(&rdev
->mutex
);
310 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
311 mutex_unlock(&rdev
->mutex
);
315 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
317 static ssize_t
regulator_uA_show(struct device
*dev
,
318 struct device_attribute
*attr
, char *buf
)
320 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
322 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
324 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
326 static ssize_t
regulator_name_show(struct device
*dev
,
327 struct device_attribute
*attr
, char *buf
)
329 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
331 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
334 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
337 case REGULATOR_MODE_FAST
:
338 return sprintf(buf
, "fast\n");
339 case REGULATOR_MODE_NORMAL
:
340 return sprintf(buf
, "normal\n");
341 case REGULATOR_MODE_IDLE
:
342 return sprintf(buf
, "idle\n");
343 case REGULATOR_MODE_STANDBY
:
344 return sprintf(buf
, "standby\n");
346 return sprintf(buf
, "unknown\n");
349 static ssize_t
regulator_opmode_show(struct device
*dev
,
350 struct device_attribute
*attr
, char *buf
)
352 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
354 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
356 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
358 static ssize_t
regulator_print_state(char *buf
, int state
)
361 return sprintf(buf
, "enabled\n");
363 return sprintf(buf
, "disabled\n");
365 return sprintf(buf
, "unknown\n");
368 static ssize_t
regulator_state_show(struct device
*dev
,
369 struct device_attribute
*attr
, char *buf
)
371 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
374 mutex_lock(&rdev
->mutex
);
375 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
376 mutex_unlock(&rdev
->mutex
);
380 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
382 static ssize_t
regulator_status_show(struct device
*dev
,
383 struct device_attribute
*attr
, char *buf
)
385 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
389 status
= rdev
->desc
->ops
->get_status(rdev
);
394 case REGULATOR_STATUS_OFF
:
397 case REGULATOR_STATUS_ON
:
400 case REGULATOR_STATUS_ERROR
:
403 case REGULATOR_STATUS_FAST
:
406 case REGULATOR_STATUS_NORMAL
:
409 case REGULATOR_STATUS_IDLE
:
412 case REGULATOR_STATUS_STANDBY
:
415 case REGULATOR_STATUS_BYPASS
:
418 case REGULATOR_STATUS_UNDEFINED
:
425 return sprintf(buf
, "%s\n", label
);
427 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
429 static ssize_t
regulator_min_uA_show(struct device
*dev
,
430 struct device_attribute
*attr
, char *buf
)
432 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
434 if (!rdev
->constraints
)
435 return sprintf(buf
, "constraint not defined\n");
437 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
439 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
441 static ssize_t
regulator_max_uA_show(struct device
*dev
,
442 struct device_attribute
*attr
, char *buf
)
444 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
446 if (!rdev
->constraints
)
447 return sprintf(buf
, "constraint not defined\n");
449 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
451 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
453 static ssize_t
regulator_min_uV_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_uV
);
463 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
465 static ssize_t
regulator_max_uV_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_uV
);
475 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
477 static ssize_t
regulator_total_uA_show(struct device
*dev
,
478 struct device_attribute
*attr
, char *buf
)
480 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
481 struct regulator
*regulator
;
484 mutex_lock(&rdev
->mutex
);
485 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
486 uA
+= regulator
->uA_load
;
487 mutex_unlock(&rdev
->mutex
);
488 return sprintf(buf
, "%d\n", uA
);
490 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
492 static ssize_t
regulator_num_users_show(struct device
*dev
,
493 struct device_attribute
*attr
, char *buf
)
495 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
496 return sprintf(buf
, "%d\n", rdev
->use_count
);
499 static ssize_t
regulator_type_show(struct device
*dev
,
500 struct device_attribute
*attr
, char *buf
)
502 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
504 switch (rdev
->desc
->type
) {
505 case REGULATOR_VOLTAGE
:
506 return sprintf(buf
, "voltage\n");
507 case REGULATOR_CURRENT
:
508 return sprintf(buf
, "current\n");
510 return sprintf(buf
, "unknown\n");
513 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
514 struct device_attribute
*attr
, char *buf
)
516 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
518 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
520 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
521 regulator_suspend_mem_uV_show
, NULL
);
523 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
524 struct device_attribute
*attr
, char *buf
)
526 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
528 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
530 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
531 regulator_suspend_disk_uV_show
, NULL
);
533 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
534 struct device_attribute
*attr
, char *buf
)
536 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
538 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
540 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
541 regulator_suspend_standby_uV_show
, NULL
);
543 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
544 struct device_attribute
*attr
, char *buf
)
546 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
548 return regulator_print_opmode(buf
,
549 rdev
->constraints
->state_mem
.mode
);
551 static DEVICE_ATTR(suspend_mem_mode
, 0444,
552 regulator_suspend_mem_mode_show
, NULL
);
554 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
555 struct device_attribute
*attr
, char *buf
)
557 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
559 return regulator_print_opmode(buf
,
560 rdev
->constraints
->state_disk
.mode
);
562 static DEVICE_ATTR(suspend_disk_mode
, 0444,
563 regulator_suspend_disk_mode_show
, NULL
);
565 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
566 struct device_attribute
*attr
, char *buf
)
568 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
570 return regulator_print_opmode(buf
,
571 rdev
->constraints
->state_standby
.mode
);
573 static DEVICE_ATTR(suspend_standby_mode
, 0444,
574 regulator_suspend_standby_mode_show
, NULL
);
576 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
577 struct device_attribute
*attr
, char *buf
)
579 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
581 return regulator_print_state(buf
,
582 rdev
->constraints
->state_mem
.enabled
);
584 static DEVICE_ATTR(suspend_mem_state
, 0444,
585 regulator_suspend_mem_state_show
, NULL
);
587 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
588 struct device_attribute
*attr
, char *buf
)
590 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
592 return regulator_print_state(buf
,
593 rdev
->constraints
->state_disk
.enabled
);
595 static DEVICE_ATTR(suspend_disk_state
, 0444,
596 regulator_suspend_disk_state_show
, NULL
);
598 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
599 struct device_attribute
*attr
, char *buf
)
601 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
603 return regulator_print_state(buf
,
604 rdev
->constraints
->state_standby
.enabled
);
606 static DEVICE_ATTR(suspend_standby_state
, 0444,
607 regulator_suspend_standby_state_show
, NULL
);
609 static ssize_t
regulator_bypass_show(struct device
*dev
,
610 struct device_attribute
*attr
, char *buf
)
612 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
617 ret
= rdev
->desc
->ops
->get_bypass(rdev
, &bypass
);
626 return sprintf(buf
, "%s\n", report
);
628 static DEVICE_ATTR(bypass
, 0444,
629 regulator_bypass_show
, NULL
);
632 * These are the only attributes are present for all regulators.
633 * Other attributes are a function of regulator functionality.
635 static struct device_attribute regulator_dev_attrs
[] = {
636 __ATTR(name
, 0444, regulator_name_show
, NULL
),
637 __ATTR(num_users
, 0444, regulator_num_users_show
, NULL
),
638 __ATTR(type
, 0444, regulator_type_show
, NULL
),
642 static void regulator_dev_release(struct device
*dev
)
644 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
648 static struct class regulator_class
= {
650 .dev_release
= regulator_dev_release
,
651 .dev_attrs
= regulator_dev_attrs
,
654 /* Calculate the new optimum regulator operating mode based on the new total
655 * consumer load. All locks held by caller */
656 static void drms_uA_update(struct regulator_dev
*rdev
)
658 struct regulator
*sibling
;
659 int current_uA
= 0, output_uV
, input_uV
, err
;
662 err
= regulator_check_drms(rdev
);
663 if (err
< 0 || !rdev
->desc
->ops
->get_optimum_mode
||
664 (!rdev
->desc
->ops
->get_voltage
&&
665 !rdev
->desc
->ops
->get_voltage_sel
) ||
666 !rdev
->desc
->ops
->set_mode
)
669 /* get output voltage */
670 output_uV
= _regulator_get_voltage(rdev
);
674 /* get input voltage */
677 input_uV
= regulator_get_voltage(rdev
->supply
);
679 input_uV
= rdev
->constraints
->input_uV
;
683 /* calc total requested load */
684 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
685 current_uA
+= sibling
->uA_load
;
687 /* now get the optimum mode for our new total regulator load */
688 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
689 output_uV
, current_uA
);
691 /* check the new mode is allowed */
692 err
= regulator_mode_constrain(rdev
, &mode
);
694 rdev
->desc
->ops
->set_mode(rdev
, mode
);
697 static int suspend_set_state(struct regulator_dev
*rdev
,
698 struct regulator_state
*rstate
)
702 /* If we have no suspend mode configration don't set anything;
703 * only warn if the driver implements set_suspend_voltage or
704 * set_suspend_mode callback.
706 if (!rstate
->enabled
&& !rstate
->disabled
) {
707 if (rdev
->desc
->ops
->set_suspend_voltage
||
708 rdev
->desc
->ops
->set_suspend_mode
)
709 rdev_warn(rdev
, "No configuration\n");
713 if (rstate
->enabled
&& rstate
->disabled
) {
714 rdev_err(rdev
, "invalid configuration\n");
718 if (rstate
->enabled
&& rdev
->desc
->ops
->set_suspend_enable
)
719 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
720 else if (rstate
->disabled
&& rdev
->desc
->ops
->set_suspend_disable
)
721 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
722 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
726 rdev_err(rdev
, "failed to enabled/disable\n");
730 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
731 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
733 rdev_err(rdev
, "failed to set voltage\n");
738 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
739 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
741 rdev_err(rdev
, "failed to set mode\n");
748 /* locks held by caller */
749 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
751 if (!rdev
->constraints
)
755 case PM_SUSPEND_STANDBY
:
756 return suspend_set_state(rdev
,
757 &rdev
->constraints
->state_standby
);
759 return suspend_set_state(rdev
,
760 &rdev
->constraints
->state_mem
);
762 return suspend_set_state(rdev
,
763 &rdev
->constraints
->state_disk
);
769 static void print_constraints(struct regulator_dev
*rdev
)
771 struct regulation_constraints
*constraints
= rdev
->constraints
;
776 if (constraints
->min_uV
&& constraints
->max_uV
) {
777 if (constraints
->min_uV
== constraints
->max_uV
)
778 count
+= sprintf(buf
+ count
, "%d mV ",
779 constraints
->min_uV
/ 1000);
781 count
+= sprintf(buf
+ count
, "%d <--> %d mV ",
782 constraints
->min_uV
/ 1000,
783 constraints
->max_uV
/ 1000);
786 if (!constraints
->min_uV
||
787 constraints
->min_uV
!= constraints
->max_uV
) {
788 ret
= _regulator_get_voltage(rdev
);
790 count
+= sprintf(buf
+ count
, "at %d mV ", ret
/ 1000);
793 if (constraints
->uV_offset
)
794 count
+= sprintf(buf
, "%dmV offset ",
795 constraints
->uV_offset
/ 1000);
797 if (constraints
->min_uA
&& constraints
->max_uA
) {
798 if (constraints
->min_uA
== constraints
->max_uA
)
799 count
+= sprintf(buf
+ count
, "%d mA ",
800 constraints
->min_uA
/ 1000);
802 count
+= sprintf(buf
+ count
, "%d <--> %d mA ",
803 constraints
->min_uA
/ 1000,
804 constraints
->max_uA
/ 1000);
807 if (!constraints
->min_uA
||
808 constraints
->min_uA
!= constraints
->max_uA
) {
809 ret
= _regulator_get_current_limit(rdev
);
811 count
+= sprintf(buf
+ count
, "at %d mA ", ret
/ 1000);
814 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
815 count
+= sprintf(buf
+ count
, "fast ");
816 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
817 count
+= sprintf(buf
+ count
, "normal ");
818 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
819 count
+= sprintf(buf
+ count
, "idle ");
820 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
821 count
+= sprintf(buf
+ count
, "standby");
824 sprintf(buf
, "no parameters");
826 rdev_info(rdev
, "%s\n", buf
);
828 if ((constraints
->min_uV
!= constraints
->max_uV
) &&
829 !(constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
))
831 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
834 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
835 struct regulation_constraints
*constraints
)
837 struct regulator_ops
*ops
= rdev
->desc
->ops
;
840 /* do we need to apply the constraint voltage */
841 if (rdev
->constraints
->apply_uV
&&
842 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
) {
843 ret
= _regulator_do_set_voltage(rdev
,
844 rdev
->constraints
->min_uV
,
845 rdev
->constraints
->max_uV
);
847 rdev_err(rdev
, "failed to apply %duV constraint\n",
848 rdev
->constraints
->min_uV
);
853 /* constrain machine-level voltage specs to fit
854 * the actual range supported by this regulator.
856 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
857 int count
= rdev
->desc
->n_voltages
;
859 int min_uV
= INT_MAX
;
860 int max_uV
= INT_MIN
;
861 int cmin
= constraints
->min_uV
;
862 int cmax
= constraints
->max_uV
;
864 /* it's safe to autoconfigure fixed-voltage supplies
865 and the constraints are used by list_voltage. */
866 if (count
== 1 && !cmin
) {
869 constraints
->min_uV
= cmin
;
870 constraints
->max_uV
= cmax
;
873 /* voltage constraints are optional */
874 if ((cmin
== 0) && (cmax
== 0))
877 /* else require explicit machine-level constraints */
878 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
879 rdev_err(rdev
, "invalid voltage constraints\n");
883 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
884 for (i
= 0; i
< count
; i
++) {
887 value
= ops
->list_voltage(rdev
, i
);
891 /* maybe adjust [min_uV..max_uV] */
892 if (value
>= cmin
&& value
< min_uV
)
894 if (value
<= cmax
&& value
> max_uV
)
898 /* final: [min_uV..max_uV] valid iff constraints valid */
899 if (max_uV
< min_uV
) {
901 "unsupportable voltage constraints %u-%uuV\n",
906 /* use regulator's subset of machine constraints */
907 if (constraints
->min_uV
< min_uV
) {
908 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
909 constraints
->min_uV
, min_uV
);
910 constraints
->min_uV
= min_uV
;
912 if (constraints
->max_uV
> max_uV
) {
913 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
914 constraints
->max_uV
, max_uV
);
915 constraints
->max_uV
= max_uV
;
923 * set_machine_constraints - sets regulator constraints
924 * @rdev: regulator source
925 * @constraints: constraints to apply
927 * Allows platform initialisation code to define and constrain
928 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
929 * Constraints *must* be set by platform code in order for some
930 * regulator operations to proceed i.e. set_voltage, set_current_limit,
933 static int set_machine_constraints(struct regulator_dev
*rdev
,
934 const struct regulation_constraints
*constraints
)
937 struct regulator_ops
*ops
= rdev
->desc
->ops
;
940 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
943 rdev
->constraints
= kzalloc(sizeof(*constraints
),
945 if (!rdev
->constraints
)
948 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
952 /* do we need to setup our suspend state */
953 if (rdev
->constraints
->initial_state
) {
954 ret
= suspend_prepare(rdev
, rdev
->constraints
->initial_state
);
956 rdev_err(rdev
, "failed to set suspend state\n");
961 if (rdev
->constraints
->initial_mode
) {
962 if (!ops
->set_mode
) {
963 rdev_err(rdev
, "no set_mode operation\n");
968 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
970 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
975 /* If the constraints say the regulator should be on at this point
976 * and we have control then make sure it is enabled.
978 if ((rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) &&
980 ret
= ops
->enable(rdev
);
982 rdev_err(rdev
, "failed to enable\n");
987 if (rdev
->constraints
->ramp_delay
&& ops
->set_ramp_delay
) {
988 ret
= ops
->set_ramp_delay(rdev
, rdev
->constraints
->ramp_delay
);
990 rdev_err(rdev
, "failed to set ramp_delay\n");
995 print_constraints(rdev
);
998 kfree(rdev
->constraints
);
999 rdev
->constraints
= NULL
;
1004 * set_supply - set regulator supply regulator
1005 * @rdev: regulator name
1006 * @supply_rdev: supply regulator name
1008 * Called by platform initialisation code to set the supply regulator for this
1009 * regulator. This ensures that a regulators supply will also be enabled by the
1010 * core if it's child is enabled.
1012 static int set_supply(struct regulator_dev
*rdev
,
1013 struct regulator_dev
*supply_rdev
)
1017 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
1019 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
1020 if (rdev
->supply
== NULL
) {
1024 supply_rdev
->open_count
++;
1030 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1031 * @rdev: regulator source
1032 * @consumer_dev_name: dev_name() string for device supply applies to
1033 * @supply: symbolic name for supply
1035 * Allows platform initialisation code to map physical regulator
1036 * sources to symbolic names for supplies for use by devices. Devices
1037 * should use these symbolic names to request regulators, avoiding the
1038 * need to provide board-specific regulator names as platform data.
1040 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1041 const char *consumer_dev_name
,
1044 struct regulator_map
*node
;
1050 if (consumer_dev_name
!= NULL
)
1055 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1056 if (node
->dev_name
&& consumer_dev_name
) {
1057 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1059 } else if (node
->dev_name
|| consumer_dev_name
) {
1063 if (strcmp(node
->supply
, supply
) != 0)
1066 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1068 dev_name(&node
->regulator
->dev
),
1069 node
->regulator
->desc
->name
,
1071 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1075 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1079 node
->regulator
= rdev
;
1080 node
->supply
= supply
;
1083 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1084 if (node
->dev_name
== NULL
) {
1090 list_add(&node
->list
, ®ulator_map_list
);
1094 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1096 struct regulator_map
*node
, *n
;
1098 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1099 if (rdev
== node
->regulator
) {
1100 list_del(&node
->list
);
1101 kfree(node
->dev_name
);
1107 #define REG_STR_SIZE 64
1109 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1111 const char *supply_name
)
1113 struct regulator
*regulator
;
1114 char buf
[REG_STR_SIZE
];
1117 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1118 if (regulator
== NULL
)
1121 mutex_lock(&rdev
->mutex
);
1122 regulator
->rdev
= rdev
;
1123 list_add(®ulator
->list
, &rdev
->consumer_list
);
1126 regulator
->dev
= dev
;
1128 /* Add a link to the device sysfs entry */
1129 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1130 dev
->kobj
.name
, supply_name
);
1131 if (size
>= REG_STR_SIZE
)
1134 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1135 if (regulator
->supply_name
== NULL
)
1138 err
= sysfs_create_link(&rdev
->dev
.kobj
, &dev
->kobj
,
1141 rdev_warn(rdev
, "could not add device link %s err %d\n",
1142 dev
->kobj
.name
, err
);
1146 regulator
->supply_name
= kstrdup(supply_name
, GFP_KERNEL
);
1147 if (regulator
->supply_name
== NULL
)
1151 regulator
->debugfs
= debugfs_create_dir(regulator
->supply_name
,
1153 if (!regulator
->debugfs
) {
1154 rdev_warn(rdev
, "Failed to create debugfs directory\n");
1156 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1157 ®ulator
->uA_load
);
1158 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1159 ®ulator
->min_uV
);
1160 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1161 ®ulator
->max_uV
);
1165 * Check now if the regulator is an always on regulator - if
1166 * it is then we don't need to do nearly so much work for
1167 * enable/disable calls.
1169 if (!_regulator_can_change_status(rdev
) &&
1170 _regulator_is_enabled(rdev
))
1171 regulator
->always_on
= true;
1173 mutex_unlock(&rdev
->mutex
);
1176 list_del(®ulator
->list
);
1178 mutex_unlock(&rdev
->mutex
);
1182 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1184 if (!rdev
->desc
->ops
->enable_time
)
1185 return rdev
->desc
->enable_time
;
1186 return rdev
->desc
->ops
->enable_time(rdev
);
1189 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1193 struct regulator_dev
*r
;
1194 struct device_node
*node
;
1195 struct regulator_map
*map
;
1196 const char *devname
= NULL
;
1198 /* first do a dt based lookup */
1199 if (dev
&& dev
->of_node
) {
1200 node
= of_get_regulator(dev
, supply
);
1202 list_for_each_entry(r
, ®ulator_list
, list
)
1203 if (r
->dev
.parent
&&
1204 node
== r
->dev
.of_node
)
1208 * If we couldn't even get the node then it's
1209 * not just that the device didn't register
1210 * yet, there's no node and we'll never
1217 /* if not found, try doing it non-dt way */
1219 devname
= dev_name(dev
);
1221 list_for_each_entry(r
, ®ulator_list
, list
)
1222 if (strcmp(rdev_get_name(r
), supply
) == 0)
1225 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1226 /* If the mapping has a device set up it must match */
1227 if (map
->dev_name
&&
1228 (!devname
|| strcmp(map
->dev_name
, devname
)))
1231 if (strcmp(map
->supply
, supply
) == 0)
1232 return map
->regulator
;
1239 /* Internal regulator request function */
1240 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1243 struct regulator_dev
*rdev
;
1244 struct regulator
*regulator
= ERR_PTR(-EPROBE_DEFER
);
1245 const char *devname
= NULL
;
1249 pr_err("get() with no identifier\n");
1254 devname
= dev_name(dev
);
1256 mutex_lock(®ulator_list_mutex
);
1258 rdev
= regulator_dev_lookup(dev
, id
, &ret
);
1263 * If we have return value from dev_lookup fail, we do not expect to
1264 * succeed, so, quit with appropriate error value
1267 regulator
= ERR_PTR(ret
);
1271 if (board_wants_dummy_regulator
) {
1272 rdev
= dummy_regulator_rdev
;
1276 #ifdef CONFIG_REGULATOR_DUMMY
1278 devname
= "deviceless";
1280 /* If the board didn't flag that it was fully constrained then
1281 * substitute in a dummy regulator so consumers can continue.
1283 if (!has_full_constraints
) {
1284 pr_warn("%s supply %s not found, using dummy regulator\n",
1286 rdev
= dummy_regulator_rdev
;
1291 mutex_unlock(®ulator_list_mutex
);
1295 if (rdev
->exclusive
) {
1296 regulator
= ERR_PTR(-EPERM
);
1300 if (exclusive
&& rdev
->open_count
) {
1301 regulator
= ERR_PTR(-EBUSY
);
1305 if (!try_module_get(rdev
->owner
))
1308 regulator
= create_regulator(rdev
, dev
, id
);
1309 if (regulator
== NULL
) {
1310 regulator
= ERR_PTR(-ENOMEM
);
1311 module_put(rdev
->owner
);
1317 rdev
->exclusive
= 1;
1319 ret
= _regulator_is_enabled(rdev
);
1321 rdev
->use_count
= 1;
1323 rdev
->use_count
= 0;
1327 mutex_unlock(®ulator_list_mutex
);
1333 * regulator_get - lookup and obtain a reference to a regulator.
1334 * @dev: device for regulator "consumer"
1335 * @id: Supply name or regulator ID.
1337 * Returns a struct regulator corresponding to the regulator producer,
1338 * or IS_ERR() condition containing errno.
1340 * Use of supply names configured via regulator_set_device_supply() is
1341 * strongly encouraged. It is recommended that the supply name used
1342 * should match the name used for the supply and/or the relevant
1343 * device pins in the datasheet.
1345 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1347 return _regulator_get(dev
, id
, 0);
1349 EXPORT_SYMBOL_GPL(regulator_get
);
1351 static void devm_regulator_release(struct device
*dev
, void *res
)
1353 regulator_put(*(struct regulator
**)res
);
1357 * devm_regulator_get - Resource managed regulator_get()
1358 * @dev: device for regulator "consumer"
1359 * @id: Supply name or regulator ID.
1361 * Managed regulator_get(). Regulators returned from this function are
1362 * automatically regulator_put() on driver detach. See regulator_get() for more
1365 struct regulator
*devm_regulator_get(struct device
*dev
, const char *id
)
1367 struct regulator
**ptr
, *regulator
;
1369 ptr
= devres_alloc(devm_regulator_release
, sizeof(*ptr
), GFP_KERNEL
);
1371 return ERR_PTR(-ENOMEM
);
1373 regulator
= regulator_get(dev
, id
);
1374 if (!IS_ERR(regulator
)) {
1376 devres_add(dev
, ptr
);
1383 EXPORT_SYMBOL_GPL(devm_regulator_get
);
1386 * regulator_get_exclusive - obtain exclusive access to a regulator.
1387 * @dev: device for regulator "consumer"
1388 * @id: Supply name or regulator ID.
1390 * Returns a struct regulator corresponding to the regulator producer,
1391 * or IS_ERR() condition containing errno. Other consumers will be
1392 * unable to obtain this reference is held and the use count for the
1393 * regulator will be initialised to reflect the current state of the
1396 * This is intended for use by consumers which cannot tolerate shared
1397 * use of the regulator such as those which need to force the
1398 * regulator off for correct operation of the hardware they are
1401 * Use of supply names configured via regulator_set_device_supply() is
1402 * strongly encouraged. It is recommended that the supply name used
1403 * should match the name used for the supply and/or the relevant
1404 * device pins in the datasheet.
1406 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1408 return _regulator_get(dev
, id
, 1);
1410 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1412 /* Locks held by regulator_put() */
1413 static void _regulator_put(struct regulator
*regulator
)
1415 struct regulator_dev
*rdev
;
1417 if (regulator
== NULL
|| IS_ERR(regulator
))
1420 rdev
= regulator
->rdev
;
1422 debugfs_remove_recursive(regulator
->debugfs
);
1424 /* remove any sysfs entries */
1426 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1427 kfree(regulator
->supply_name
);
1428 list_del(®ulator
->list
);
1432 rdev
->exclusive
= 0;
1434 module_put(rdev
->owner
);
1438 * regulator_put - "free" the regulator source
1439 * @regulator: regulator source
1441 * Note: drivers must ensure that all regulator_enable calls made on this
1442 * regulator source are balanced by regulator_disable calls prior to calling
1445 void regulator_put(struct regulator
*regulator
)
1447 mutex_lock(®ulator_list_mutex
);
1448 _regulator_put(regulator
);
1449 mutex_unlock(®ulator_list_mutex
);
1451 EXPORT_SYMBOL_GPL(regulator_put
);
1453 static int devm_regulator_match(struct device
*dev
, void *res
, void *data
)
1455 struct regulator
**r
= res
;
1464 * devm_regulator_put - Resource managed regulator_put()
1465 * @regulator: regulator to free
1467 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1468 * this function will not need to be called and the resource management
1469 * code will ensure that the resource is freed.
1471 void devm_regulator_put(struct regulator
*regulator
)
1475 rc
= devres_release(regulator
->dev
, devm_regulator_release
,
1476 devm_regulator_match
, regulator
);
1480 EXPORT_SYMBOL_GPL(devm_regulator_put
);
1482 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1483 static int regulator_ena_gpio_request(struct regulator_dev
*rdev
,
1484 const struct regulator_config
*config
)
1486 struct regulator_enable_gpio
*pin
;
1489 list_for_each_entry(pin
, ®ulator_ena_gpio_list
, list
) {
1490 if (pin
->gpio
== config
->ena_gpio
) {
1491 rdev_dbg(rdev
, "GPIO %d is already used\n",
1493 goto update_ena_gpio_to_rdev
;
1497 ret
= gpio_request_one(config
->ena_gpio
,
1498 GPIOF_DIR_OUT
| config
->ena_gpio_flags
,
1499 rdev_get_name(rdev
));
1503 pin
= kzalloc(sizeof(struct regulator_enable_gpio
), GFP_KERNEL
);
1505 gpio_free(config
->ena_gpio
);
1509 pin
->gpio
= config
->ena_gpio
;
1510 pin
->ena_gpio_invert
= config
->ena_gpio_invert
;
1511 list_add(&pin
->list
, ®ulator_ena_gpio_list
);
1513 update_ena_gpio_to_rdev
:
1514 pin
->request_count
++;
1515 rdev
->ena_pin
= pin
;
1519 static void regulator_ena_gpio_free(struct regulator_dev
*rdev
)
1521 struct regulator_enable_gpio
*pin
, *n
;
1526 /* Free the GPIO only in case of no use */
1527 list_for_each_entry_safe(pin
, n
, ®ulator_ena_gpio_list
, list
) {
1528 if (pin
->gpio
== rdev
->ena_pin
->gpio
) {
1529 if (pin
->request_count
<= 1) {
1530 pin
->request_count
= 0;
1531 gpio_free(pin
->gpio
);
1532 list_del(&pin
->list
);
1535 pin
->request_count
--;
1542 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1543 * @rdev: regulator_dev structure
1544 * @enable: enable GPIO at initial use?
1546 * GPIO is enabled in case of initial use. (enable_count is 0)
1547 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1549 static int regulator_ena_gpio_ctrl(struct regulator_dev
*rdev
, bool enable
)
1551 struct regulator_enable_gpio
*pin
= rdev
->ena_pin
;
1557 /* Enable GPIO at initial use */
1558 if (pin
->enable_count
== 0)
1559 gpio_set_value_cansleep(pin
->gpio
,
1560 !pin
->ena_gpio_invert
);
1562 pin
->enable_count
++;
1564 if (pin
->enable_count
> 1) {
1565 pin
->enable_count
--;
1569 /* Disable GPIO if not used */
1570 if (pin
->enable_count
<= 1) {
1571 gpio_set_value_cansleep(pin
->gpio
,
1572 pin
->ena_gpio_invert
);
1573 pin
->enable_count
= 0;
1580 static int _regulator_do_enable(struct regulator_dev
*rdev
)
1584 /* Query before enabling in case configuration dependent. */
1585 ret
= _regulator_get_enable_time(rdev
);
1589 rdev_warn(rdev
, "enable_time() failed: %d\n", ret
);
1593 trace_regulator_enable(rdev_get_name(rdev
));
1595 if (rdev
->ena_pin
) {
1596 ret
= regulator_ena_gpio_ctrl(rdev
, true);
1599 rdev
->ena_gpio_state
= 1;
1600 } else if (rdev
->desc
->ops
->enable
) {
1601 ret
= rdev
->desc
->ops
->enable(rdev
);
1608 /* Allow the regulator to ramp; it would be useful to extend
1609 * this for bulk operations so that the regulators can ramp
1611 trace_regulator_enable_delay(rdev_get_name(rdev
));
1613 if (delay
>= 1000) {
1614 mdelay(delay
/ 1000);
1615 udelay(delay
% 1000);
1620 trace_regulator_enable_complete(rdev_get_name(rdev
));
1625 /* locks held by regulator_enable() */
1626 static int _regulator_enable(struct regulator_dev
*rdev
)
1630 /* check voltage and requested load before enabling */
1631 if (rdev
->constraints
&&
1632 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1633 drms_uA_update(rdev
);
1635 if (rdev
->use_count
== 0) {
1636 /* The regulator may on if it's not switchable or left on */
1637 ret
= _regulator_is_enabled(rdev
);
1638 if (ret
== -EINVAL
|| ret
== 0) {
1639 if (!_regulator_can_change_status(rdev
))
1642 ret
= _regulator_do_enable(rdev
);
1646 } else if (ret
< 0) {
1647 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
1650 /* Fallthrough on positive return values - already enabled */
1659 * regulator_enable - enable regulator output
1660 * @regulator: regulator source
1662 * Request that the regulator be enabled with the regulator output at
1663 * the predefined voltage or current value. Calls to regulator_enable()
1664 * must be balanced with calls to regulator_disable().
1666 * NOTE: the output value can be set by other drivers, boot loader or may be
1667 * hardwired in the regulator.
1669 int regulator_enable(struct regulator
*regulator
)
1671 struct regulator_dev
*rdev
= regulator
->rdev
;
1674 if (regulator
->always_on
)
1678 ret
= regulator_enable(rdev
->supply
);
1683 mutex_lock(&rdev
->mutex
);
1684 ret
= _regulator_enable(rdev
);
1685 mutex_unlock(&rdev
->mutex
);
1687 if (ret
!= 0 && rdev
->supply
)
1688 regulator_disable(rdev
->supply
);
1692 EXPORT_SYMBOL_GPL(regulator_enable
);
1694 static int _regulator_do_disable(struct regulator_dev
*rdev
)
1698 trace_regulator_disable(rdev_get_name(rdev
));
1700 if (rdev
->ena_pin
) {
1701 ret
= regulator_ena_gpio_ctrl(rdev
, false);
1704 rdev
->ena_gpio_state
= 0;
1706 } else if (rdev
->desc
->ops
->disable
) {
1707 ret
= rdev
->desc
->ops
->disable(rdev
);
1712 trace_regulator_disable_complete(rdev_get_name(rdev
));
1714 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
1719 /* locks held by regulator_disable() */
1720 static int _regulator_disable(struct regulator_dev
*rdev
)
1724 if (WARN(rdev
->use_count
<= 0,
1725 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
1728 /* are we the last user and permitted to disable ? */
1729 if (rdev
->use_count
== 1 &&
1730 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
1732 /* we are last user */
1733 if (_regulator_can_change_status(rdev
)) {
1734 ret
= _regulator_do_disable(rdev
);
1736 rdev_err(rdev
, "failed to disable\n");
1741 rdev
->use_count
= 0;
1742 } else if (rdev
->use_count
> 1) {
1744 if (rdev
->constraints
&&
1745 (rdev
->constraints
->valid_ops_mask
&
1746 REGULATOR_CHANGE_DRMS
))
1747 drms_uA_update(rdev
);
1756 * regulator_disable - disable regulator output
1757 * @regulator: regulator source
1759 * Disable the regulator output voltage or current. Calls to
1760 * regulator_enable() must be balanced with calls to
1761 * regulator_disable().
1763 * NOTE: this will only disable the regulator output if no other consumer
1764 * devices have it enabled, the regulator device supports disabling and
1765 * machine constraints permit this operation.
1767 int regulator_disable(struct regulator
*regulator
)
1769 struct regulator_dev
*rdev
= regulator
->rdev
;
1772 if (regulator
->always_on
)
1775 mutex_lock(&rdev
->mutex
);
1776 ret
= _regulator_disable(rdev
);
1777 mutex_unlock(&rdev
->mutex
);
1779 if (ret
== 0 && rdev
->supply
)
1780 regulator_disable(rdev
->supply
);
1784 EXPORT_SYMBOL_GPL(regulator_disable
);
1786 /* locks held by regulator_force_disable() */
1787 static int _regulator_force_disable(struct regulator_dev
*rdev
)
1792 if (rdev
->desc
->ops
->disable
) {
1793 /* ah well, who wants to live forever... */
1794 ret
= rdev
->desc
->ops
->disable(rdev
);
1796 rdev_err(rdev
, "failed to force disable\n");
1799 /* notify other consumers that power has been forced off */
1800 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
1801 REGULATOR_EVENT_DISABLE
, NULL
);
1808 * regulator_force_disable - force disable regulator output
1809 * @regulator: regulator source
1811 * Forcibly disable the regulator output voltage or current.
1812 * NOTE: this *will* disable the regulator output even if other consumer
1813 * devices have it enabled. This should be used for situations when device
1814 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1816 int regulator_force_disable(struct regulator
*regulator
)
1818 struct regulator_dev
*rdev
= regulator
->rdev
;
1821 mutex_lock(&rdev
->mutex
);
1822 regulator
->uA_load
= 0;
1823 ret
= _regulator_force_disable(regulator
->rdev
);
1824 mutex_unlock(&rdev
->mutex
);
1827 while (rdev
->open_count
--)
1828 regulator_disable(rdev
->supply
);
1832 EXPORT_SYMBOL_GPL(regulator_force_disable
);
1834 static void regulator_disable_work(struct work_struct
*work
)
1836 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
1840 mutex_lock(&rdev
->mutex
);
1842 BUG_ON(!rdev
->deferred_disables
);
1844 count
= rdev
->deferred_disables
;
1845 rdev
->deferred_disables
= 0;
1847 for (i
= 0; i
< count
; i
++) {
1848 ret
= _regulator_disable(rdev
);
1850 rdev_err(rdev
, "Deferred disable failed: %d\n", ret
);
1853 mutex_unlock(&rdev
->mutex
);
1856 for (i
= 0; i
< count
; i
++) {
1857 ret
= regulator_disable(rdev
->supply
);
1860 "Supply disable failed: %d\n", ret
);
1867 * regulator_disable_deferred - disable regulator output with delay
1868 * @regulator: regulator source
1869 * @ms: miliseconds until the regulator is disabled
1871 * Execute regulator_disable() on the regulator after a delay. This
1872 * is intended for use with devices that require some time to quiesce.
1874 * NOTE: this will only disable the regulator output if no other consumer
1875 * devices have it enabled, the regulator device supports disabling and
1876 * machine constraints permit this operation.
1878 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
1880 struct regulator_dev
*rdev
= regulator
->rdev
;
1883 if (regulator
->always_on
)
1887 return regulator_disable(regulator
);
1889 mutex_lock(&rdev
->mutex
);
1890 rdev
->deferred_disables
++;
1891 mutex_unlock(&rdev
->mutex
);
1893 ret
= schedule_delayed_work(&rdev
->disable_work
,
1894 msecs_to_jiffies(ms
));
1900 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
1903 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1905 * @rdev: regulator to operate on
1907 * Regulators that use regmap for their register I/O can set the
1908 * enable_reg and enable_mask fields in their descriptor and then use
1909 * this as their is_enabled operation, saving some code.
1911 int regulator_is_enabled_regmap(struct regulator_dev
*rdev
)
1916 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->enable_reg
, &val
);
1920 if (rdev
->desc
->enable_is_inverted
)
1921 return (val
& rdev
->desc
->enable_mask
) == 0;
1923 return (val
& rdev
->desc
->enable_mask
) != 0;
1925 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap
);
1928 * regulator_enable_regmap - standard enable() for regmap users
1930 * @rdev: regulator to operate on
1932 * Regulators that use regmap for their register I/O can set the
1933 * enable_reg and enable_mask fields in their descriptor and then use
1934 * this as their enable() operation, saving some code.
1936 int regulator_enable_regmap(struct regulator_dev
*rdev
)
1940 if (rdev
->desc
->enable_is_inverted
)
1943 val
= rdev
->desc
->enable_mask
;
1945 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
1946 rdev
->desc
->enable_mask
, val
);
1948 EXPORT_SYMBOL_GPL(regulator_enable_regmap
);
1951 * regulator_disable_regmap - standard disable() for regmap users
1953 * @rdev: regulator to operate on
1955 * Regulators that use regmap for their register I/O can set the
1956 * enable_reg and enable_mask fields in their descriptor and then use
1957 * this as their disable() operation, saving some code.
1959 int regulator_disable_regmap(struct regulator_dev
*rdev
)
1963 if (rdev
->desc
->enable_is_inverted
)
1964 val
= rdev
->desc
->enable_mask
;
1968 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->enable_reg
,
1969 rdev
->desc
->enable_mask
, val
);
1971 EXPORT_SYMBOL_GPL(regulator_disable_regmap
);
1973 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
1975 /* A GPIO control always takes precedence */
1977 return rdev
->ena_gpio_state
;
1979 /* If we don't know then assume that the regulator is always on */
1980 if (!rdev
->desc
->ops
->is_enabled
)
1983 return rdev
->desc
->ops
->is_enabled(rdev
);
1987 * regulator_is_enabled - is the regulator output enabled
1988 * @regulator: regulator source
1990 * Returns positive if the regulator driver backing the source/client
1991 * has requested that the device be enabled, zero if it hasn't, else a
1992 * negative errno code.
1994 * Note that the device backing this regulator handle can have multiple
1995 * users, so it might be enabled even if regulator_enable() was never
1996 * called for this particular source.
1998 int regulator_is_enabled(struct regulator
*regulator
)
2002 if (regulator
->always_on
)
2005 mutex_lock(®ulator
->rdev
->mutex
);
2006 ret
= _regulator_is_enabled(regulator
->rdev
);
2007 mutex_unlock(®ulator
->rdev
->mutex
);
2011 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
2014 * regulator_can_change_voltage - check if regulator can change voltage
2015 * @regulator: regulator source
2017 * Returns positive if the regulator driver backing the source/client
2018 * can change its voltage, false otherwise. Usefull for detecting fixed
2019 * or dummy regulators and disabling voltage change logic in the client
2022 int regulator_can_change_voltage(struct regulator
*regulator
)
2024 struct regulator_dev
*rdev
= regulator
->rdev
;
2026 if (rdev
->constraints
&&
2027 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2028 if (rdev
->desc
->n_voltages
- rdev
->desc
->linear_min_sel
> 1)
2031 if (rdev
->desc
->continuous_voltage_range
&&
2032 rdev
->constraints
->min_uV
&& rdev
->constraints
->max_uV
&&
2033 rdev
->constraints
->min_uV
!= rdev
->constraints
->max_uV
)
2039 EXPORT_SYMBOL_GPL(regulator_can_change_voltage
);
2042 * regulator_count_voltages - count regulator_list_voltage() selectors
2043 * @regulator: regulator source
2045 * Returns number of selectors, or negative errno. Selectors are
2046 * numbered starting at zero, and typically correspond to bitfields
2047 * in hardware registers.
2049 int regulator_count_voltages(struct regulator
*regulator
)
2051 struct regulator_dev
*rdev
= regulator
->rdev
;
2053 return rdev
->desc
->n_voltages
? : -EINVAL
;
2055 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
2058 * regulator_list_voltage_linear - List voltages with simple calculation
2060 * @rdev: Regulator device
2061 * @selector: Selector to convert into a voltage
2063 * Regulators with a simple linear mapping between voltages and
2064 * selectors can set min_uV and uV_step in the regulator descriptor
2065 * and then use this function as their list_voltage() operation,
2067 int regulator_list_voltage_linear(struct regulator_dev
*rdev
,
2068 unsigned int selector
)
2070 if (selector
>= rdev
->desc
->n_voltages
)
2072 if (selector
< rdev
->desc
->linear_min_sel
)
2075 selector
-= rdev
->desc
->linear_min_sel
;
2077 return rdev
->desc
->min_uV
+ (rdev
->desc
->uV_step
* selector
);
2079 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear
);
2082 * regulator_list_voltage_table - List voltages with table based mapping
2084 * @rdev: Regulator device
2085 * @selector: Selector to convert into a voltage
2087 * Regulators with table based mapping between voltages and
2088 * selectors can set volt_table in the regulator descriptor
2089 * and then use this function as their list_voltage() operation.
2091 int regulator_list_voltage_table(struct regulator_dev
*rdev
,
2092 unsigned int selector
)
2094 if (!rdev
->desc
->volt_table
) {
2095 BUG_ON(!rdev
->desc
->volt_table
);
2099 if (selector
>= rdev
->desc
->n_voltages
)
2102 return rdev
->desc
->volt_table
[selector
];
2104 EXPORT_SYMBOL_GPL(regulator_list_voltage_table
);
2107 * regulator_list_voltage - enumerate supported voltages
2108 * @regulator: regulator source
2109 * @selector: identify voltage to list
2110 * Context: can sleep
2112 * Returns a voltage that can be passed to @regulator_set_voltage(),
2113 * zero if this selector code can't be used on this system, or a
2116 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
2118 struct regulator_dev
*rdev
= regulator
->rdev
;
2119 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2122 if (!ops
->list_voltage
|| selector
>= rdev
->desc
->n_voltages
)
2125 mutex_lock(&rdev
->mutex
);
2126 ret
= ops
->list_voltage(rdev
, selector
);
2127 mutex_unlock(&rdev
->mutex
);
2130 if (ret
< rdev
->constraints
->min_uV
)
2132 else if (ret
> rdev
->constraints
->max_uV
)
2138 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
2141 * regulator_get_linear_step - return the voltage step size between VSEL values
2142 * @regulator: regulator source
2144 * Returns the voltage step size between VSEL values for linear
2145 * regulators, or return 0 if the regulator isn't a linear regulator.
2147 unsigned int regulator_get_linear_step(struct regulator
*regulator
)
2149 struct regulator_dev
*rdev
= regulator
->rdev
;
2151 return rdev
->desc
->uV_step
;
2153 EXPORT_SYMBOL_GPL(regulator_get_linear_step
);
2156 * regulator_is_supported_voltage - check if a voltage range can be supported
2158 * @regulator: Regulator to check.
2159 * @min_uV: Minimum required voltage in uV.
2160 * @max_uV: Maximum required voltage in uV.
2162 * Returns a boolean or a negative error code.
2164 int regulator_is_supported_voltage(struct regulator
*regulator
,
2165 int min_uV
, int max_uV
)
2167 struct regulator_dev
*rdev
= regulator
->rdev
;
2168 int i
, voltages
, ret
;
2170 /* If we can't change voltage check the current voltage */
2171 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
2172 ret
= regulator_get_voltage(regulator
);
2174 return (min_uV
<= ret
&& ret
<= max_uV
);
2179 /* Any voltage within constrains range is fine? */
2180 if (rdev
->desc
->continuous_voltage_range
)
2181 return min_uV
>= rdev
->constraints
->min_uV
&&
2182 max_uV
<= rdev
->constraints
->max_uV
;
2184 ret
= regulator_count_voltages(regulator
);
2189 for (i
= 0; i
< voltages
; i
++) {
2190 ret
= regulator_list_voltage(regulator
, i
);
2192 if (ret
>= min_uV
&& ret
<= max_uV
)
2198 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
2201 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2203 * @rdev: regulator to operate on
2205 * Regulators that use regmap for their register I/O can set the
2206 * vsel_reg and vsel_mask fields in their descriptor and then use this
2207 * as their get_voltage_vsel operation, saving some code.
2209 int regulator_get_voltage_sel_regmap(struct regulator_dev
*rdev
)
2214 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->vsel_reg
, &val
);
2218 val
&= rdev
->desc
->vsel_mask
;
2219 val
>>= ffs(rdev
->desc
->vsel_mask
) - 1;
2223 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap
);
2226 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2228 * @rdev: regulator to operate on
2229 * @sel: Selector to set
2231 * Regulators that use regmap for their register I/O can set the
2232 * vsel_reg and vsel_mask fields in their descriptor and then use this
2233 * as their set_voltage_vsel operation, saving some code.
2235 int regulator_set_voltage_sel_regmap(struct regulator_dev
*rdev
, unsigned sel
)
2239 sel
<<= ffs(rdev
->desc
->vsel_mask
) - 1;
2241 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->vsel_reg
,
2242 rdev
->desc
->vsel_mask
, sel
);
2246 if (rdev
->desc
->apply_bit
)
2247 ret
= regmap_update_bits(rdev
->regmap
, rdev
->desc
->apply_reg
,
2248 rdev
->desc
->apply_bit
,
2249 rdev
->desc
->apply_bit
);
2252 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap
);
2255 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2257 * @rdev: Regulator to operate on
2258 * @min_uV: Lower bound for voltage
2259 * @max_uV: Upper bound for voltage
2261 * Drivers implementing set_voltage_sel() and list_voltage() can use
2262 * this as their map_voltage() operation. It will find a suitable
2263 * voltage by calling list_voltage() until it gets something in bounds
2264 * for the requested voltages.
2266 int regulator_map_voltage_iterate(struct regulator_dev
*rdev
,
2267 int min_uV
, int max_uV
)
2269 int best_val
= INT_MAX
;
2273 /* Find the smallest voltage that falls within the specified
2276 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
2277 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
2281 if (ret
< best_val
&& ret
>= min_uV
&& ret
<= max_uV
) {
2287 if (best_val
!= INT_MAX
)
2292 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate
);
2295 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2297 * @rdev: Regulator to operate on
2298 * @min_uV: Lower bound for voltage
2299 * @max_uV: Upper bound for voltage
2301 * Drivers that have ascendant voltage list can use this as their
2302 * map_voltage() operation.
2304 int regulator_map_voltage_ascend(struct regulator_dev
*rdev
,
2305 int min_uV
, int max_uV
)
2309 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
2310 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
2317 if (ret
>= min_uV
&& ret
<= max_uV
)
2323 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend
);
2326 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2328 * @rdev: Regulator to operate on
2329 * @min_uV: Lower bound for voltage
2330 * @max_uV: Upper bound for voltage
2332 * Drivers providing min_uV and uV_step in their regulator_desc can
2333 * use this as their map_voltage() operation.
2335 int regulator_map_voltage_linear(struct regulator_dev
*rdev
,
2336 int min_uV
, int max_uV
)
2340 /* Allow uV_step to be 0 for fixed voltage */
2341 if (rdev
->desc
->n_voltages
== 1 && rdev
->desc
->uV_step
== 0) {
2342 if (min_uV
<= rdev
->desc
->min_uV
&& rdev
->desc
->min_uV
<= max_uV
)
2348 if (!rdev
->desc
->uV_step
) {
2349 BUG_ON(!rdev
->desc
->uV_step
);
2353 if (min_uV
< rdev
->desc
->min_uV
)
2354 min_uV
= rdev
->desc
->min_uV
;
2356 ret
= DIV_ROUND_UP(min_uV
- rdev
->desc
->min_uV
, rdev
->desc
->uV_step
);
2360 ret
+= rdev
->desc
->linear_min_sel
;
2362 /* Map back into a voltage to verify we're still in bounds */
2363 voltage
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
2364 if (voltage
< min_uV
|| voltage
> max_uV
)
2369 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear
);
2371 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
2372 int min_uV
, int max_uV
)
2377 unsigned int selector
;
2378 int old_selector
= -1;
2380 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
2382 min_uV
+= rdev
->constraints
->uV_offset
;
2383 max_uV
+= rdev
->constraints
->uV_offset
;
2386 * If we can't obtain the old selector there is not enough
2387 * info to call set_voltage_time_sel().
2389 if (_regulator_is_enabled(rdev
) &&
2390 rdev
->desc
->ops
->set_voltage_time_sel
&&
2391 rdev
->desc
->ops
->get_voltage_sel
) {
2392 old_selector
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
2393 if (old_selector
< 0)
2394 return old_selector
;
2397 if (rdev
->desc
->ops
->set_voltage
) {
2398 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
,
2402 if (rdev
->desc
->ops
->list_voltage
)
2403 best_val
= rdev
->desc
->ops
->list_voltage(rdev
,
2406 best_val
= _regulator_get_voltage(rdev
);
2409 } else if (rdev
->desc
->ops
->set_voltage_sel
) {
2410 if (rdev
->desc
->ops
->map_voltage
) {
2411 ret
= rdev
->desc
->ops
->map_voltage(rdev
, min_uV
,
2414 if (rdev
->desc
->ops
->list_voltage
==
2415 regulator_list_voltage_linear
)
2416 ret
= regulator_map_voltage_linear(rdev
,
2419 ret
= regulator_map_voltage_iterate(rdev
,
2424 best_val
= rdev
->desc
->ops
->list_voltage(rdev
, ret
);
2425 if (min_uV
<= best_val
&& max_uV
>= best_val
) {
2427 if (old_selector
== selector
)
2430 ret
= rdev
->desc
->ops
->set_voltage_sel(
2440 /* Call set_voltage_time_sel if successfully obtained old_selector */
2441 if (ret
== 0 && _regulator_is_enabled(rdev
) && old_selector
>= 0 &&
2442 old_selector
!= selector
&& rdev
->desc
->ops
->set_voltage_time_sel
) {
2444 delay
= rdev
->desc
->ops
->set_voltage_time_sel(rdev
,
2445 old_selector
, selector
);
2447 rdev_warn(rdev
, "set_voltage_time_sel() failed: %d\n",
2452 /* Insert any necessary delays */
2453 if (delay
>= 1000) {
2454 mdelay(delay
/ 1000);
2455 udelay(delay
% 1000);
2461 if (ret
== 0 && best_val
>= 0) {
2462 unsigned long data
= best_val
;
2464 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
2468 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), best_val
);
2474 * regulator_set_voltage - set regulator output voltage
2475 * @regulator: regulator source
2476 * @min_uV: Minimum required voltage in uV
2477 * @max_uV: Maximum acceptable voltage in uV
2479 * Sets a voltage regulator to the desired output voltage. This can be set
2480 * during any regulator state. IOW, regulator can be disabled or enabled.
2482 * If the regulator is enabled then the voltage will change to the new value
2483 * immediately otherwise if the regulator is disabled the regulator will
2484 * output at the new voltage when enabled.
2486 * NOTE: If the regulator is shared between several devices then the lowest
2487 * request voltage that meets the system constraints will be used.
2488 * Regulator system constraints must be set for this regulator before
2489 * calling this function otherwise this call will fail.
2491 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
2493 struct regulator_dev
*rdev
= regulator
->rdev
;
2495 int old_min_uV
, old_max_uV
;
2497 mutex_lock(&rdev
->mutex
);
2499 /* If we're setting the same range as last time the change
2500 * should be a noop (some cpufreq implementations use the same
2501 * voltage for multiple frequencies, for example).
2503 if (regulator
->min_uV
== min_uV
&& regulator
->max_uV
== max_uV
)
2507 if (!rdev
->desc
->ops
->set_voltage
&&
2508 !rdev
->desc
->ops
->set_voltage_sel
) {
2513 /* constraints check */
2514 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
2518 /* restore original values in case of error */
2519 old_min_uV
= regulator
->min_uV
;
2520 old_max_uV
= regulator
->max_uV
;
2521 regulator
->min_uV
= min_uV
;
2522 regulator
->max_uV
= max_uV
;
2524 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
2528 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
2533 mutex_unlock(&rdev
->mutex
);
2536 regulator
->min_uV
= old_min_uV
;
2537 regulator
->max_uV
= old_max_uV
;
2538 mutex_unlock(&rdev
->mutex
);
2541 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
2544 * regulator_set_voltage_time - get raise/fall time
2545 * @regulator: regulator source
2546 * @old_uV: starting voltage in microvolts
2547 * @new_uV: target voltage in microvolts
2549 * Provided with the starting and ending voltage, this function attempts to
2550 * calculate the time in microseconds required to rise or fall to this new
2553 int regulator_set_voltage_time(struct regulator
*regulator
,
2554 int old_uV
, int new_uV
)
2556 struct regulator_dev
*rdev
= regulator
->rdev
;
2557 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2563 /* Currently requires operations to do this */
2564 if (!ops
->list_voltage
|| !ops
->set_voltage_time_sel
2565 || !rdev
->desc
->n_voltages
)
2568 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
2569 /* We only look for exact voltage matches here */
2570 voltage
= regulator_list_voltage(regulator
, i
);
2575 if (voltage
== old_uV
)
2577 if (voltage
== new_uV
)
2581 if (old_sel
< 0 || new_sel
< 0)
2584 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
2586 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
2589 * regulator_set_voltage_time_sel - get raise/fall time
2590 * @rdev: regulator source device
2591 * @old_selector: selector for starting voltage
2592 * @new_selector: selector for target voltage
2594 * Provided with the starting and target voltage selectors, this function
2595 * returns time in microseconds required to rise or fall to this new voltage
2597 * Drivers providing ramp_delay in regulation_constraints can use this as their
2598 * set_voltage_time_sel() operation.
2600 int regulator_set_voltage_time_sel(struct regulator_dev
*rdev
,
2601 unsigned int old_selector
,
2602 unsigned int new_selector
)
2604 unsigned int ramp_delay
= 0;
2605 int old_volt
, new_volt
;
2607 if (rdev
->constraints
->ramp_delay
)
2608 ramp_delay
= rdev
->constraints
->ramp_delay
;
2609 else if (rdev
->desc
->ramp_delay
)
2610 ramp_delay
= rdev
->desc
->ramp_delay
;
2612 if (ramp_delay
== 0) {
2613 rdev_warn(rdev
, "ramp_delay not set\n");
2618 if (!rdev
->desc
->ops
->list_voltage
)
2621 old_volt
= rdev
->desc
->ops
->list_voltage(rdev
, old_selector
);
2622 new_volt
= rdev
->desc
->ops
->list_voltage(rdev
, new_selector
);
2624 return DIV_ROUND_UP(abs(new_volt
- old_volt
), ramp_delay
);
2626 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel
);
2629 * regulator_sync_voltage - re-apply last regulator output voltage
2630 * @regulator: regulator source
2632 * Re-apply the last configured voltage. This is intended to be used
2633 * where some external control source the consumer is cooperating with
2634 * has caused the configured voltage to change.
2636 int regulator_sync_voltage(struct regulator
*regulator
)
2638 struct regulator_dev
*rdev
= regulator
->rdev
;
2639 int ret
, min_uV
, max_uV
;
2641 mutex_lock(&rdev
->mutex
);
2643 if (!rdev
->desc
->ops
->set_voltage
&&
2644 !rdev
->desc
->ops
->set_voltage_sel
) {
2649 /* This is only going to work if we've had a voltage configured. */
2650 if (!regulator
->min_uV
&& !regulator
->max_uV
) {
2655 min_uV
= regulator
->min_uV
;
2656 max_uV
= regulator
->max_uV
;
2658 /* This should be a paranoia check... */
2659 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
2663 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
2667 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
2670 mutex_unlock(&rdev
->mutex
);
2673 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
2675 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
2679 if (rdev
->desc
->ops
->get_voltage_sel
) {
2680 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
2683 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
2684 } else if (rdev
->desc
->ops
->get_voltage
) {
2685 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
2686 } else if (rdev
->desc
->ops
->list_voltage
) {
2687 ret
= rdev
->desc
->ops
->list_voltage(rdev
, 0);
2694 return ret
- rdev
->constraints
->uV_offset
;
2698 * regulator_get_voltage - get regulator output voltage
2699 * @regulator: regulator source
2701 * This returns the current regulator voltage in uV.
2703 * NOTE: If the regulator is disabled it will return the voltage value. This
2704 * function should not be used to determine regulator state.
2706 int regulator_get_voltage(struct regulator
*regulator
)
2710 mutex_lock(®ulator
->rdev
->mutex
);
2712 ret
= _regulator_get_voltage(regulator
->rdev
);
2714 mutex_unlock(®ulator
->rdev
->mutex
);
2718 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
2721 * regulator_set_current_limit - set regulator output current limit
2722 * @regulator: regulator source
2723 * @min_uA: Minimum supported current in uA
2724 * @max_uA: Maximum supported current in uA
2726 * Sets current sink to the desired output current. This can be set during
2727 * any regulator state. IOW, regulator can be disabled or enabled.
2729 * If the regulator is enabled then the current will change to the new value
2730 * immediately otherwise if the regulator is disabled the regulator will
2731 * output at the new current when enabled.
2733 * NOTE: Regulator system constraints must be set for this regulator before
2734 * calling this function otherwise this call will fail.
2736 int regulator_set_current_limit(struct regulator
*regulator
,
2737 int min_uA
, int max_uA
)
2739 struct regulator_dev
*rdev
= regulator
->rdev
;
2742 mutex_lock(&rdev
->mutex
);
2745 if (!rdev
->desc
->ops
->set_current_limit
) {
2750 /* constraints check */
2751 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
2755 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
2757 mutex_unlock(&rdev
->mutex
);
2760 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
2762 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
2766 mutex_lock(&rdev
->mutex
);
2769 if (!rdev
->desc
->ops
->get_current_limit
) {
2774 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
2776 mutex_unlock(&rdev
->mutex
);
2781 * regulator_get_current_limit - get regulator output current
2782 * @regulator: regulator source
2784 * This returns the current supplied by the specified current sink in uA.
2786 * NOTE: If the regulator is disabled it will return the current value. This
2787 * function should not be used to determine regulator state.
2789 int regulator_get_current_limit(struct regulator
*regulator
)
2791 return _regulator_get_current_limit(regulator
->rdev
);
2793 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
2796 * regulator_set_mode - set regulator operating mode
2797 * @regulator: regulator source
2798 * @mode: operating mode - one of the REGULATOR_MODE constants
2800 * Set regulator operating mode to increase regulator efficiency or improve
2801 * regulation performance.
2803 * NOTE: Regulator system constraints must be set for this regulator before
2804 * calling this function otherwise this call will fail.
2806 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
2808 struct regulator_dev
*rdev
= regulator
->rdev
;
2810 int regulator_curr_mode
;
2812 mutex_lock(&rdev
->mutex
);
2815 if (!rdev
->desc
->ops
->set_mode
) {
2820 /* return if the same mode is requested */
2821 if (rdev
->desc
->ops
->get_mode
) {
2822 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
2823 if (regulator_curr_mode
== mode
) {
2829 /* constraints check */
2830 ret
= regulator_mode_constrain(rdev
, &mode
);
2834 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
2836 mutex_unlock(&rdev
->mutex
);
2839 EXPORT_SYMBOL_GPL(regulator_set_mode
);
2841 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
2845 mutex_lock(&rdev
->mutex
);
2848 if (!rdev
->desc
->ops
->get_mode
) {
2853 ret
= rdev
->desc
->ops
->get_mode(rdev
);
2855 mutex_unlock(&rdev
->mutex
);
2860 * regulator_get_mode - get regulator operating mode
2861 * @regulator: regulator source
2863 * Get the current regulator operating mode.
2865 unsigned int regulator_get_mode(struct regulator
*regulator
)
2867 return _regulator_get_mode(regulator
->rdev
);
2869 EXPORT_SYMBOL_GPL(regulator_get_mode
);
2872 * regulator_set_optimum_mode - set regulator optimum operating mode
2873 * @regulator: regulator source
2874 * @uA_load: load current
2876 * Notifies the regulator core of a new device load. This is then used by
2877 * DRMS (if enabled by constraints) to set the most efficient regulator
2878 * operating mode for the new regulator loading.
2880 * Consumer devices notify their supply regulator of the maximum power
2881 * they will require (can be taken from device datasheet in the power
2882 * consumption tables) when they change operational status and hence power
2883 * state. Examples of operational state changes that can affect power
2884 * consumption are :-
2886 * o Device is opened / closed.
2887 * o Device I/O is about to begin or has just finished.
2888 * o Device is idling in between work.
2890 * This information is also exported via sysfs to userspace.
2892 * DRMS will sum the total requested load on the regulator and change
2893 * to the most efficient operating mode if platform constraints allow.
2895 * Returns the new regulator mode or error.
2897 int regulator_set_optimum_mode(struct regulator
*regulator
, int uA_load
)
2899 struct regulator_dev
*rdev
= regulator
->rdev
;
2900 struct regulator
*consumer
;
2901 int ret
, output_uV
, input_uV
= 0, total_uA_load
= 0;
2905 input_uV
= regulator_get_voltage(rdev
->supply
);
2907 mutex_lock(&rdev
->mutex
);
2910 * first check to see if we can set modes at all, otherwise just
2911 * tell the consumer everything is OK.
2913 regulator
->uA_load
= uA_load
;
2914 ret
= regulator_check_drms(rdev
);
2920 if (!rdev
->desc
->ops
->get_optimum_mode
)
2924 * we can actually do this so any errors are indicators of
2925 * potential real failure.
2929 if (!rdev
->desc
->ops
->set_mode
)
2932 /* get output voltage */
2933 output_uV
= _regulator_get_voltage(rdev
);
2934 if (output_uV
<= 0) {
2935 rdev_err(rdev
, "invalid output voltage found\n");
2939 /* No supply? Use constraint voltage */
2941 input_uV
= rdev
->constraints
->input_uV
;
2942 if (input_uV
<= 0) {
2943 rdev_err(rdev
, "invalid input voltage found\n");
2947 /* calc total requested load for this regulator */
2948 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
)
2949 total_uA_load
+= consumer
->uA_load
;
2951 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
,
2952 input_uV
, output_uV
,
2954 ret
= regulator_mode_constrain(rdev
, &mode
);
2956 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2957 total_uA_load
, input_uV
, output_uV
);
2961 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
2963 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
2968 mutex_unlock(&rdev
->mutex
);
2971 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode
);
2974 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2976 * @rdev: device to operate on.
2977 * @enable: state to set.
2979 int regulator_set_bypass_regmap(struct regulator_dev
*rdev
, bool enable
)
2984 val
= rdev
->desc
->bypass_mask
;
2988 return regmap_update_bits(rdev
->regmap
, rdev
->desc
->bypass_reg
,
2989 rdev
->desc
->bypass_mask
, val
);
2991 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap
);
2994 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2996 * @rdev: device to operate on.
2997 * @enable: current state.
2999 int regulator_get_bypass_regmap(struct regulator_dev
*rdev
, bool *enable
)
3004 ret
= regmap_read(rdev
->regmap
, rdev
->desc
->bypass_reg
, &val
);
3008 *enable
= val
& rdev
->desc
->bypass_mask
;
3012 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap
);
3015 * regulator_allow_bypass - allow the regulator to go into bypass mode
3017 * @regulator: Regulator to configure
3018 * @enable: enable or disable bypass mode
3020 * Allow the regulator to go into bypass mode if all other consumers
3021 * for the regulator also enable bypass mode and the machine
3022 * constraints allow this. Bypass mode means that the regulator is
3023 * simply passing the input directly to the output with no regulation.
3025 int regulator_allow_bypass(struct regulator
*regulator
, bool enable
)
3027 struct regulator_dev
*rdev
= regulator
->rdev
;
3030 if (!rdev
->desc
->ops
->set_bypass
)
3033 if (rdev
->constraints
&&
3034 !(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_BYPASS
))
3037 mutex_lock(&rdev
->mutex
);
3039 if (enable
&& !regulator
->bypass
) {
3040 rdev
->bypass_count
++;
3042 if (rdev
->bypass_count
== rdev
->open_count
) {
3043 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3045 rdev
->bypass_count
--;
3048 } else if (!enable
&& regulator
->bypass
) {
3049 rdev
->bypass_count
--;
3051 if (rdev
->bypass_count
!= rdev
->open_count
) {
3052 ret
= rdev
->desc
->ops
->set_bypass(rdev
, enable
);
3054 rdev
->bypass_count
++;
3059 regulator
->bypass
= enable
;
3061 mutex_unlock(&rdev
->mutex
);
3065 EXPORT_SYMBOL_GPL(regulator_allow_bypass
);
3068 * regulator_register_notifier - register regulator event notifier
3069 * @regulator: regulator source
3070 * @nb: notifier block
3072 * Register notifier block to receive regulator events.
3074 int regulator_register_notifier(struct regulator
*regulator
,
3075 struct notifier_block
*nb
)
3077 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
3080 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
3083 * regulator_unregister_notifier - unregister regulator event notifier
3084 * @regulator: regulator source
3085 * @nb: notifier block
3087 * Unregister regulator event notifier block.
3089 int regulator_unregister_notifier(struct regulator
*regulator
,
3090 struct notifier_block
*nb
)
3092 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
3095 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
3097 /* notify regulator consumers and downstream regulator consumers.
3098 * Note mutex must be held by caller.
3100 static void _notifier_call_chain(struct regulator_dev
*rdev
,
3101 unsigned long event
, void *data
)
3103 /* call rdev chain first */
3104 blocking_notifier_call_chain(&rdev
->notifier
, event
, data
);
3108 * regulator_bulk_get - get multiple regulator consumers
3110 * @dev: Device to supply
3111 * @num_consumers: Number of consumers to register
3112 * @consumers: Configuration of consumers; clients are stored here.
3114 * @return 0 on success, an errno on failure.
3116 * This helper function allows drivers to get several regulator
3117 * consumers in one operation. If any of the regulators cannot be
3118 * acquired then any regulators that were allocated will be freed
3119 * before returning to the caller.
3121 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
3122 struct regulator_bulk_data
*consumers
)
3127 for (i
= 0; i
< num_consumers
; i
++)
3128 consumers
[i
].consumer
= NULL
;
3130 for (i
= 0; i
< num_consumers
; i
++) {
3131 consumers
[i
].consumer
= regulator_get(dev
,
3132 consumers
[i
].supply
);
3133 if (IS_ERR(consumers
[i
].consumer
)) {
3134 ret
= PTR_ERR(consumers
[i
].consumer
);
3135 dev_err(dev
, "Failed to get supply '%s': %d\n",
3136 consumers
[i
].supply
, ret
);
3137 consumers
[i
].consumer
= NULL
;
3146 regulator_put(consumers
[i
].consumer
);
3150 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
3153 * devm_regulator_bulk_get - managed get multiple regulator consumers
3155 * @dev: Device to supply
3156 * @num_consumers: Number of consumers to register
3157 * @consumers: Configuration of consumers; clients are stored here.
3159 * @return 0 on success, an errno on failure.
3161 * This helper function allows drivers to get several regulator
3162 * consumers in one operation with management, the regulators will
3163 * automatically be freed when the device is unbound. If any of the
3164 * regulators cannot be acquired then any regulators that were
3165 * allocated will be freed before returning to the caller.
3167 int devm_regulator_bulk_get(struct device
*dev
, int num_consumers
,
3168 struct regulator_bulk_data
*consumers
)
3173 for (i
= 0; i
< num_consumers
; i
++)
3174 consumers
[i
].consumer
= NULL
;
3176 for (i
= 0; i
< num_consumers
; i
++) {
3177 consumers
[i
].consumer
= devm_regulator_get(dev
,
3178 consumers
[i
].supply
);
3179 if (IS_ERR(consumers
[i
].consumer
)) {
3180 ret
= PTR_ERR(consumers
[i
].consumer
);
3181 dev_err(dev
, "Failed to get supply '%s': %d\n",
3182 consumers
[i
].supply
, ret
);
3183 consumers
[i
].consumer
= NULL
;
3191 for (i
= 0; i
< num_consumers
&& consumers
[i
].consumer
; i
++)
3192 devm_regulator_put(consumers
[i
].consumer
);
3196 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get
);
3198 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
3200 struct regulator_bulk_data
*bulk
= data
;
3202 bulk
->ret
= regulator_enable(bulk
->consumer
);
3206 * regulator_bulk_enable - enable multiple regulator consumers
3208 * @num_consumers: Number of consumers
3209 * @consumers: Consumer data; clients are stored here.
3210 * @return 0 on success, an errno on failure
3212 * This convenience API allows consumers to enable multiple regulator
3213 * clients in a single API call. If any consumers cannot be enabled
3214 * then any others that were enabled will be disabled again prior to
3217 int regulator_bulk_enable(int num_consumers
,
3218 struct regulator_bulk_data
*consumers
)
3220 ASYNC_DOMAIN_EXCLUSIVE(async_domain
);
3224 for (i
= 0; i
< num_consumers
; i
++) {
3225 if (consumers
[i
].consumer
->always_on
)
3226 consumers
[i
].ret
= 0;
3228 async_schedule_domain(regulator_bulk_enable_async
,
3229 &consumers
[i
], &async_domain
);
3232 async_synchronize_full_domain(&async_domain
);
3234 /* If any consumer failed we need to unwind any that succeeded */
3235 for (i
= 0; i
< num_consumers
; i
++) {
3236 if (consumers
[i
].ret
!= 0) {
3237 ret
= consumers
[i
].ret
;
3245 for (i
= 0; i
< num_consumers
; i
++) {
3246 if (consumers
[i
].ret
< 0)
3247 pr_err("Failed to enable %s: %d\n", consumers
[i
].supply
,
3250 regulator_disable(consumers
[i
].consumer
);
3255 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
3258 * regulator_bulk_disable - disable multiple regulator consumers
3260 * @num_consumers: Number of consumers
3261 * @consumers: Consumer data; clients are stored here.
3262 * @return 0 on success, an errno on failure
3264 * This convenience API allows consumers to disable multiple regulator
3265 * clients in a single API call. If any consumers cannot be disabled
3266 * then any others that were disabled will be enabled again prior to
3269 int regulator_bulk_disable(int num_consumers
,
3270 struct regulator_bulk_data
*consumers
)
3275 for (i
= num_consumers
- 1; i
>= 0; --i
) {
3276 ret
= regulator_disable(consumers
[i
].consumer
);
3284 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
3285 for (++i
; i
< num_consumers
; ++i
) {
3286 r
= regulator_enable(consumers
[i
].consumer
);
3288 pr_err("Failed to reename %s: %d\n",
3289 consumers
[i
].supply
, r
);
3294 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
3297 * regulator_bulk_force_disable - force disable multiple regulator consumers
3299 * @num_consumers: Number of consumers
3300 * @consumers: Consumer data; clients are stored here.
3301 * @return 0 on success, an errno on failure
3303 * This convenience API allows consumers to forcibly disable multiple regulator
3304 * clients in a single API call.
3305 * NOTE: This should be used for situations when device damage will
3306 * likely occur if the regulators are not disabled (e.g. over temp).
3307 * Although regulator_force_disable function call for some consumers can
3308 * return error numbers, the function is called for all consumers.
3310 int regulator_bulk_force_disable(int num_consumers
,
3311 struct regulator_bulk_data
*consumers
)
3316 for (i
= 0; i
< num_consumers
; i
++)
3318 regulator_force_disable(consumers
[i
].consumer
);
3320 for (i
= 0; i
< num_consumers
; i
++) {
3321 if (consumers
[i
].ret
!= 0) {
3322 ret
= consumers
[i
].ret
;
3331 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
3334 * regulator_bulk_free - free multiple regulator consumers
3336 * @num_consumers: Number of consumers
3337 * @consumers: Consumer data; clients are stored here.
3339 * This convenience API allows consumers to free multiple regulator
3340 * clients in a single API call.
3342 void regulator_bulk_free(int num_consumers
,
3343 struct regulator_bulk_data
*consumers
)
3347 for (i
= 0; i
< num_consumers
; i
++) {
3348 regulator_put(consumers
[i
].consumer
);
3349 consumers
[i
].consumer
= NULL
;
3352 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
3355 * regulator_notifier_call_chain - call regulator event notifier
3356 * @rdev: regulator source
3357 * @event: notifier block
3358 * @data: callback-specific data.
3360 * Called by regulator drivers to notify clients a regulator event has
3361 * occurred. We also notify regulator clients downstream.
3362 * Note lock must be held by caller.
3364 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
3365 unsigned long event
, void *data
)
3367 _notifier_call_chain(rdev
, event
, data
);
3371 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
3374 * regulator_mode_to_status - convert a regulator mode into a status
3376 * @mode: Mode to convert
3378 * Convert a regulator mode into a status.
3380 int regulator_mode_to_status(unsigned int mode
)
3383 case REGULATOR_MODE_FAST
:
3384 return REGULATOR_STATUS_FAST
;
3385 case REGULATOR_MODE_NORMAL
:
3386 return REGULATOR_STATUS_NORMAL
;
3387 case REGULATOR_MODE_IDLE
:
3388 return REGULATOR_STATUS_IDLE
;
3389 case REGULATOR_MODE_STANDBY
:
3390 return REGULATOR_STATUS_STANDBY
;
3392 return REGULATOR_STATUS_UNDEFINED
;
3395 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
3398 * To avoid cluttering sysfs (and memory) with useless state, only
3399 * create attributes that can be meaningfully displayed.
3401 static int add_regulator_attributes(struct regulator_dev
*rdev
)
3403 struct device
*dev
= &rdev
->dev
;
3404 struct regulator_ops
*ops
= rdev
->desc
->ops
;
3407 /* some attributes need specific methods to be displayed */
3408 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
3409 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0) ||
3410 (ops
->list_voltage
&& ops
->list_voltage(rdev
, 0) >= 0)) {
3411 status
= device_create_file(dev
, &dev_attr_microvolts
);
3415 if (ops
->get_current_limit
) {
3416 status
= device_create_file(dev
, &dev_attr_microamps
);
3420 if (ops
->get_mode
) {
3421 status
= device_create_file(dev
, &dev_attr_opmode
);
3425 if (rdev
->ena_pin
|| ops
->is_enabled
) {
3426 status
= device_create_file(dev
, &dev_attr_state
);
3430 if (ops
->get_status
) {
3431 status
= device_create_file(dev
, &dev_attr_status
);
3435 if (ops
->get_bypass
) {
3436 status
= device_create_file(dev
, &dev_attr_bypass
);
3441 /* some attributes are type-specific */
3442 if (rdev
->desc
->type
== REGULATOR_CURRENT
) {
3443 status
= device_create_file(dev
, &dev_attr_requested_microamps
);
3448 /* all the other attributes exist to support constraints;
3449 * don't show them if there are no constraints, or if the
3450 * relevant supporting methods are missing.
3452 if (!rdev
->constraints
)
3455 /* constraints need specific supporting methods */
3456 if (ops
->set_voltage
|| ops
->set_voltage_sel
) {
3457 status
= device_create_file(dev
, &dev_attr_min_microvolts
);
3460 status
= device_create_file(dev
, &dev_attr_max_microvolts
);
3464 if (ops
->set_current_limit
) {
3465 status
= device_create_file(dev
, &dev_attr_min_microamps
);
3468 status
= device_create_file(dev
, &dev_attr_max_microamps
);
3473 status
= device_create_file(dev
, &dev_attr_suspend_standby_state
);
3476 status
= device_create_file(dev
, &dev_attr_suspend_mem_state
);
3479 status
= device_create_file(dev
, &dev_attr_suspend_disk_state
);
3483 if (ops
->set_suspend_voltage
) {
3484 status
= device_create_file(dev
,
3485 &dev_attr_suspend_standby_microvolts
);
3488 status
= device_create_file(dev
,
3489 &dev_attr_suspend_mem_microvolts
);
3492 status
= device_create_file(dev
,
3493 &dev_attr_suspend_disk_microvolts
);
3498 if (ops
->set_suspend_mode
) {
3499 status
= device_create_file(dev
,
3500 &dev_attr_suspend_standby_mode
);
3503 status
= device_create_file(dev
,
3504 &dev_attr_suspend_mem_mode
);
3507 status
= device_create_file(dev
,
3508 &dev_attr_suspend_disk_mode
);
3516 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
3518 rdev
->debugfs
= debugfs_create_dir(rdev_get_name(rdev
), debugfs_root
);
3519 if (!rdev
->debugfs
) {
3520 rdev_warn(rdev
, "Failed to create debugfs directory\n");
3524 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
3526 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
3528 debugfs_create_u32("bypass_count", 0444, rdev
->debugfs
,
3529 &rdev
->bypass_count
);
3533 * regulator_register - register regulator
3534 * @regulator_desc: regulator to register
3535 * @config: runtime configuration for regulator
3537 * Called by regulator drivers to register a regulator.
3538 * Returns a valid pointer to struct regulator_dev on success
3539 * or an ERR_PTR() on error.
3541 struct regulator_dev
*
3542 regulator_register(const struct regulator_desc
*regulator_desc
,
3543 const struct regulator_config
*config
)
3545 const struct regulation_constraints
*constraints
= NULL
;
3546 const struct regulator_init_data
*init_data
;
3547 static atomic_t regulator_no
= ATOMIC_INIT(0);
3548 struct regulator_dev
*rdev
;
3551 const char *supply
= NULL
;
3553 if (regulator_desc
== NULL
|| config
== NULL
)
3554 return ERR_PTR(-EINVAL
);
3559 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
3560 return ERR_PTR(-EINVAL
);
3562 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
3563 regulator_desc
->type
!= REGULATOR_CURRENT
)
3564 return ERR_PTR(-EINVAL
);
3566 /* Only one of each should be implemented */
3567 WARN_ON(regulator_desc
->ops
->get_voltage
&&
3568 regulator_desc
->ops
->get_voltage_sel
);
3569 WARN_ON(regulator_desc
->ops
->set_voltage
&&
3570 regulator_desc
->ops
->set_voltage_sel
);
3572 /* If we're using selectors we must implement list_voltage. */
3573 if (regulator_desc
->ops
->get_voltage_sel
&&
3574 !regulator_desc
->ops
->list_voltage
) {
3575 return ERR_PTR(-EINVAL
);
3577 if (regulator_desc
->ops
->set_voltage_sel
&&
3578 !regulator_desc
->ops
->list_voltage
) {
3579 return ERR_PTR(-EINVAL
);
3582 init_data
= config
->init_data
;
3584 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
3586 return ERR_PTR(-ENOMEM
);
3588 mutex_lock(®ulator_list_mutex
);
3590 mutex_init(&rdev
->mutex
);
3591 rdev
->reg_data
= config
->driver_data
;
3592 rdev
->owner
= regulator_desc
->owner
;
3593 rdev
->desc
= regulator_desc
;
3595 rdev
->regmap
= config
->regmap
;
3596 else if (dev_get_regmap(dev
, NULL
))
3597 rdev
->regmap
= dev_get_regmap(dev
, NULL
);
3598 else if (dev
->parent
)
3599 rdev
->regmap
= dev_get_regmap(dev
->parent
, NULL
);
3600 INIT_LIST_HEAD(&rdev
->consumer_list
);
3601 INIT_LIST_HEAD(&rdev
->list
);
3602 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
3603 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
3605 /* preform any regulator specific init */
3606 if (init_data
&& init_data
->regulator_init
) {
3607 ret
= init_data
->regulator_init(rdev
->reg_data
);
3612 /* register with sysfs */
3613 rdev
->dev
.class = ®ulator_class
;
3614 rdev
->dev
.of_node
= config
->of_node
;
3615 rdev
->dev
.parent
= dev
;
3616 dev_set_name(&rdev
->dev
, "regulator.%d",
3617 atomic_inc_return(®ulator_no
) - 1);
3618 ret
= device_register(&rdev
->dev
);
3620 put_device(&rdev
->dev
);
3624 dev_set_drvdata(&rdev
->dev
, rdev
);
3626 if (config
->ena_gpio
&& gpio_is_valid(config
->ena_gpio
)) {
3627 ret
= regulator_ena_gpio_request(rdev
, config
);
3629 rdev_err(rdev
, "Failed to request enable GPIO%d: %d\n",
3630 config
->ena_gpio
, ret
);
3634 if (config
->ena_gpio_flags
& GPIOF_OUT_INIT_HIGH
)
3635 rdev
->ena_gpio_state
= 1;
3637 if (config
->ena_gpio_invert
)
3638 rdev
->ena_gpio_state
= !rdev
->ena_gpio_state
;
3641 /* set regulator constraints */
3643 constraints
= &init_data
->constraints
;
3645 ret
= set_machine_constraints(rdev
, constraints
);
3649 /* add attributes supported by this regulator */
3650 ret
= add_regulator_attributes(rdev
);
3654 if (init_data
&& init_data
->supply_regulator
)
3655 supply
= init_data
->supply_regulator
;
3656 else if (regulator_desc
->supply_name
)
3657 supply
= regulator_desc
->supply_name
;
3660 struct regulator_dev
*r
;
3662 r
= regulator_dev_lookup(dev
, supply
, &ret
);
3664 if (ret
== -ENODEV
) {
3666 * No supply was specified for this regulator and
3667 * there will never be one.
3672 dev_err(dev
, "Failed to find supply %s\n", supply
);
3673 ret
= -EPROBE_DEFER
;
3677 ret
= set_supply(rdev
, r
);
3681 /* Enable supply if rail is enabled */
3682 if (_regulator_is_enabled(rdev
)) {
3683 ret
= regulator_enable(rdev
->supply
);
3690 /* add consumers devices */
3692 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
3693 ret
= set_consumer_device_supply(rdev
,
3694 init_data
->consumer_supplies
[i
].dev_name
,
3695 init_data
->consumer_supplies
[i
].supply
);
3697 dev_err(dev
, "Failed to set supply %s\n",
3698 init_data
->consumer_supplies
[i
].supply
);
3699 goto unset_supplies
;
3704 list_add(&rdev
->list
, ®ulator_list
);
3706 rdev_init_debugfs(rdev
);
3708 mutex_unlock(®ulator_list_mutex
);
3712 unset_regulator_supplies(rdev
);
3716 _regulator_put(rdev
->supply
);
3717 regulator_ena_gpio_free(rdev
);
3718 kfree(rdev
->constraints
);
3720 device_unregister(&rdev
->dev
);
3721 /* device core frees rdev */
3722 rdev
= ERR_PTR(ret
);
3727 rdev
= ERR_PTR(ret
);
3730 EXPORT_SYMBOL_GPL(regulator_register
);
3733 * regulator_unregister - unregister regulator
3734 * @rdev: regulator to unregister
3736 * Called by regulator drivers to unregister a regulator.
3738 void regulator_unregister(struct regulator_dev
*rdev
)
3744 regulator_put(rdev
->supply
);
3745 mutex_lock(®ulator_list_mutex
);
3746 debugfs_remove_recursive(rdev
->debugfs
);
3747 flush_work(&rdev
->disable_work
.work
);
3748 WARN_ON(rdev
->open_count
);
3749 unset_regulator_supplies(rdev
);
3750 list_del(&rdev
->list
);
3751 kfree(rdev
->constraints
);
3752 regulator_ena_gpio_free(rdev
);
3753 device_unregister(&rdev
->dev
);
3754 mutex_unlock(®ulator_list_mutex
);
3756 EXPORT_SYMBOL_GPL(regulator_unregister
);
3759 * regulator_suspend_prepare - prepare regulators for system wide suspend
3760 * @state: system suspend state
3762 * Configure each regulator with it's suspend operating parameters for state.
3763 * This will usually be called by machine suspend code prior to supending.
3765 int regulator_suspend_prepare(suspend_state_t state
)
3767 struct regulator_dev
*rdev
;
3770 /* ON is handled by regulator active state */
3771 if (state
== PM_SUSPEND_ON
)
3774 mutex_lock(®ulator_list_mutex
);
3775 list_for_each_entry(rdev
, ®ulator_list
, list
) {
3777 mutex_lock(&rdev
->mutex
);
3778 ret
= suspend_prepare(rdev
, state
);
3779 mutex_unlock(&rdev
->mutex
);
3782 rdev_err(rdev
, "failed to prepare\n");
3787 mutex_unlock(®ulator_list_mutex
);
3790 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
3793 * regulator_suspend_finish - resume regulators from system wide suspend
3795 * Turn on regulators that might be turned off by regulator_suspend_prepare
3796 * and that should be turned on according to the regulators properties.
3798 int regulator_suspend_finish(void)
3800 struct regulator_dev
*rdev
;
3803 mutex_lock(®ulator_list_mutex
);
3804 list_for_each_entry(rdev
, ®ulator_list
, list
) {
3805 struct regulator_ops
*ops
= rdev
->desc
->ops
;
3807 mutex_lock(&rdev
->mutex
);
3808 if ((rdev
->use_count
> 0 || rdev
->constraints
->always_on
) &&
3810 error
= ops
->enable(rdev
);
3814 if (!has_full_constraints
)
3818 if (!_regulator_is_enabled(rdev
))
3821 error
= ops
->disable(rdev
);
3826 mutex_unlock(&rdev
->mutex
);
3828 mutex_unlock(®ulator_list_mutex
);
3831 EXPORT_SYMBOL_GPL(regulator_suspend_finish
);
3834 * regulator_has_full_constraints - the system has fully specified constraints
3836 * Calling this function will cause the regulator API to disable all
3837 * regulators which have a zero use count and don't have an always_on
3838 * constraint in a late_initcall.
3840 * The intention is that this will become the default behaviour in a
3841 * future kernel release so users are encouraged to use this facility
3844 void regulator_has_full_constraints(void)
3846 has_full_constraints
= 1;
3848 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
3851 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3853 * Calling this function will cause the regulator API to provide a
3854 * dummy regulator to consumers if no physical regulator is found,
3855 * allowing most consumers to proceed as though a regulator were
3856 * configured. This allows systems such as those with software
3857 * controllable regulators for the CPU core only to be brought up more
3860 void regulator_use_dummy_regulator(void)
3862 board_wants_dummy_regulator
= true;
3864 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator
);
3867 * rdev_get_drvdata - get rdev regulator driver data
3870 * Get rdev regulator driver private data. This call can be used in the
3871 * regulator driver context.
3873 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
3875 return rdev
->reg_data
;
3877 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
3880 * regulator_get_drvdata - get regulator driver data
3881 * @regulator: regulator
3883 * Get regulator driver private data. This call can be used in the consumer
3884 * driver context when non API regulator specific functions need to be called.
3886 void *regulator_get_drvdata(struct regulator
*regulator
)
3888 return regulator
->rdev
->reg_data
;
3890 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
3893 * regulator_set_drvdata - set regulator driver data
3894 * @regulator: regulator
3897 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
3899 regulator
->rdev
->reg_data
= data
;
3901 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
3904 * regulator_get_id - get regulator ID
3907 int rdev_get_id(struct regulator_dev
*rdev
)
3909 return rdev
->desc
->id
;
3911 EXPORT_SYMBOL_GPL(rdev_get_id
);
3913 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
3917 EXPORT_SYMBOL_GPL(rdev_get_dev
);
3919 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
3921 return reg_init_data
->driver_data
;
3923 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
3925 #ifdef CONFIG_DEBUG_FS
3926 static ssize_t
supply_map_read_file(struct file
*file
, char __user
*user_buf
,
3927 size_t count
, loff_t
*ppos
)
3929 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3930 ssize_t len
, ret
= 0;
3931 struct regulator_map
*map
;
3936 list_for_each_entry(map
, ®ulator_map_list
, list
) {
3937 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
3939 rdev_get_name(map
->regulator
), map
->dev_name
,
3943 if (ret
> PAGE_SIZE
) {
3949 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
3957 static const struct file_operations supply_map_fops
= {
3958 #ifdef CONFIG_DEBUG_FS
3959 .read
= supply_map_read_file
,
3960 .llseek
= default_llseek
,
3964 static int __init
regulator_init(void)
3968 ret
= class_register(®ulator_class
);
3970 debugfs_root
= debugfs_create_dir("regulator", NULL
);
3972 pr_warn("regulator: Failed to create debugfs directory\n");
3974 debugfs_create_file("supply_map", 0444, debugfs_root
, NULL
,
3977 regulator_dummy_init();
3982 /* init early to allow our consumers to complete system booting */
3983 core_initcall(regulator_init
);
3985 static int __init
regulator_init_complete(void)
3987 struct regulator_dev
*rdev
;
3988 struct regulator_ops
*ops
;
3989 struct regulation_constraints
*c
;
3993 * Since DT doesn't provide an idiomatic mechanism for
3994 * enabling full constraints and since it's much more natural
3995 * with DT to provide them just assume that a DT enabled
3996 * system has full constraints.
3998 if (of_have_populated_dt())
3999 has_full_constraints
= true;
4001 mutex_lock(®ulator_list_mutex
);
4003 /* If we have a full configuration then disable any regulators
4004 * which are not in use or always_on. This will become the
4005 * default behaviour in the future.
4007 list_for_each_entry(rdev
, ®ulator_list
, list
) {
4008 ops
= rdev
->desc
->ops
;
4009 c
= rdev
->constraints
;
4011 if (!ops
->disable
|| (c
&& c
->always_on
))
4014 mutex_lock(&rdev
->mutex
);
4016 if (rdev
->use_count
)
4019 /* If we can't read the status assume it's on. */
4020 if (ops
->is_enabled
)
4021 enabled
= ops
->is_enabled(rdev
);
4028 if (has_full_constraints
) {
4029 /* We log since this may kill the system if it
4031 rdev_info(rdev
, "disabling\n");
4032 ret
= ops
->disable(rdev
);
4034 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
4037 /* The intention is that in future we will
4038 * assume that full constraints are provided
4039 * so warn even if we aren't going to do
4042 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
4046 mutex_unlock(&rdev
->mutex
);
4049 mutex_unlock(®ulator_list_mutex
);
4053 late_initcall(regulator_init_complete
);