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 #define pr_fmt(fmt) "%s: " fmt, __func__
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/slab.h>
23 #include <linux/async.h>
24 #include <linux/err.h>
25 #include <linux/mutex.h>
26 #include <linux/suspend.h>
27 #include <linux/delay.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 bool has_full_constraints
;
55 static bool board_wants_dummy_regulator
;
57 #ifdef CONFIG_DEBUG_FS
58 static struct dentry
*debugfs_root
;
62 * struct regulator_map
64 * Used to provide symbolic supply names to devices.
66 struct regulator_map
{
67 struct list_head list
;
68 const char *dev_name
; /* The dev_name() for the consumer */
70 struct regulator_dev
*regulator
;
76 * One for each consumer device.
80 struct list_head list
;
85 struct device_attribute dev_attr
;
86 struct regulator_dev
*rdev
;
87 #ifdef CONFIG_DEBUG_FS
88 struct dentry
*debugfs
;
92 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
93 static int _regulator_disable(struct regulator_dev
*rdev
);
94 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
95 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
96 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
97 static void _notifier_call_chain(struct regulator_dev
*rdev
,
98 unsigned long event
, void *data
);
99 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
100 int min_uV
, int max_uV
);
101 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
103 const char *supply_name
);
105 static const char *rdev_get_name(struct regulator_dev
*rdev
)
107 if (rdev
->constraints
&& rdev
->constraints
->name
)
108 return rdev
->constraints
->name
;
109 else if (rdev
->desc
->name
)
110 return rdev
->desc
->name
;
115 /* gets the regulator for a given consumer device */
116 static struct regulator
*get_device_regulator(struct device
*dev
)
118 struct regulator
*regulator
= NULL
;
119 struct regulator_dev
*rdev
;
121 mutex_lock(®ulator_list_mutex
);
122 list_for_each_entry(rdev
, ®ulator_list
, list
) {
123 mutex_lock(&rdev
->mutex
);
124 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
125 if (regulator
->dev
== dev
) {
126 mutex_unlock(&rdev
->mutex
);
127 mutex_unlock(®ulator_list_mutex
);
131 mutex_unlock(&rdev
->mutex
);
133 mutex_unlock(®ulator_list_mutex
);
138 * of_get_regulator - get a regulator device node based on supply name
139 * @dev: Device pointer for the consumer (of regulator) device
140 * @supply: regulator supply name
142 * Extract the regulator device node corresponding to the supply name.
143 * retruns the device node corresponding to the regulator if found, else
146 static struct device_node
*of_get_regulator(struct device
*dev
, const char *supply
)
148 struct device_node
*regnode
= NULL
;
149 char prop_name
[32]; /* 32 is max size of property name */
151 dev_dbg(dev
, "Looking up %s-supply from device tree\n", supply
);
153 snprintf(prop_name
, 32, "%s-supply", supply
);
154 regnode
= of_parse_phandle(dev
->of_node
, prop_name
, 0);
157 dev_warn(dev
, "%s property in node %s references invalid phandle",
158 prop_name
, dev
->of_node
->full_name
);
164 /* Platform voltage constraint check */
165 static int regulator_check_voltage(struct regulator_dev
*rdev
,
166 int *min_uV
, int *max_uV
)
168 BUG_ON(*min_uV
> *max_uV
);
170 if (!rdev
->constraints
) {
171 rdev_err(rdev
, "no constraints\n");
174 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
175 rdev_err(rdev
, "operation not allowed\n");
179 if (*max_uV
> rdev
->constraints
->max_uV
)
180 *max_uV
= rdev
->constraints
->max_uV
;
181 if (*min_uV
< rdev
->constraints
->min_uV
)
182 *min_uV
= rdev
->constraints
->min_uV
;
184 if (*min_uV
> *max_uV
) {
185 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
193 /* Make sure we select a voltage that suits the needs of all
194 * regulator consumers
196 static int regulator_check_consumers(struct regulator_dev
*rdev
,
197 int *min_uV
, int *max_uV
)
199 struct regulator
*regulator
;
201 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
203 * Assume consumers that didn't say anything are OK
204 * with anything in the constraint range.
206 if (!regulator
->min_uV
&& !regulator
->max_uV
)
209 if (*max_uV
> regulator
->max_uV
)
210 *max_uV
= regulator
->max_uV
;
211 if (*min_uV
< regulator
->min_uV
)
212 *min_uV
= regulator
->min_uV
;
215 if (*min_uV
> *max_uV
)
221 /* current constraint check */
222 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
223 int *min_uA
, int *max_uA
)
225 BUG_ON(*min_uA
> *max_uA
);
227 if (!rdev
->constraints
) {
228 rdev_err(rdev
, "no constraints\n");
231 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
232 rdev_err(rdev
, "operation not allowed\n");
236 if (*max_uA
> rdev
->constraints
->max_uA
)
237 *max_uA
= rdev
->constraints
->max_uA
;
238 if (*min_uA
< rdev
->constraints
->min_uA
)
239 *min_uA
= rdev
->constraints
->min_uA
;
241 if (*min_uA
> *max_uA
) {
242 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
250 /* operating mode constraint check */
251 static int regulator_mode_constrain(struct regulator_dev
*rdev
, int *mode
)
254 case REGULATOR_MODE_FAST
:
255 case REGULATOR_MODE_NORMAL
:
256 case REGULATOR_MODE_IDLE
:
257 case REGULATOR_MODE_STANDBY
:
260 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
264 if (!rdev
->constraints
) {
265 rdev_err(rdev
, "no constraints\n");
268 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
269 rdev_err(rdev
, "operation not allowed\n");
273 /* The modes are bitmasks, the most power hungry modes having
274 * the lowest values. If the requested mode isn't supported
275 * try higher modes. */
277 if (rdev
->constraints
->valid_modes_mask
& *mode
)
285 /* dynamic regulator mode switching constraint check */
286 static int regulator_check_drms(struct regulator_dev
*rdev
)
288 if (!rdev
->constraints
) {
289 rdev_err(rdev
, "no constraints\n");
292 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
293 rdev_err(rdev
, "operation not allowed\n");
299 static ssize_t
device_requested_uA_show(struct device
*dev
,
300 struct device_attribute
*attr
, char *buf
)
302 struct regulator
*regulator
;
304 regulator
= get_device_regulator(dev
);
305 if (regulator
== NULL
)
308 return sprintf(buf
, "%d\n", regulator
->uA_load
);
311 static ssize_t
regulator_uV_show(struct device
*dev
,
312 struct device_attribute
*attr
, char *buf
)
314 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
317 mutex_lock(&rdev
->mutex
);
318 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
319 mutex_unlock(&rdev
->mutex
);
323 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
325 static ssize_t
regulator_uA_show(struct device
*dev
,
326 struct device_attribute
*attr
, char *buf
)
328 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
330 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
332 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
334 static ssize_t
regulator_name_show(struct device
*dev
,
335 struct device_attribute
*attr
, char *buf
)
337 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
339 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
342 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
345 case REGULATOR_MODE_FAST
:
346 return sprintf(buf
, "fast\n");
347 case REGULATOR_MODE_NORMAL
:
348 return sprintf(buf
, "normal\n");
349 case REGULATOR_MODE_IDLE
:
350 return sprintf(buf
, "idle\n");
351 case REGULATOR_MODE_STANDBY
:
352 return sprintf(buf
, "standby\n");
354 return sprintf(buf
, "unknown\n");
357 static ssize_t
regulator_opmode_show(struct device
*dev
,
358 struct device_attribute
*attr
, char *buf
)
360 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
362 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
364 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
366 static ssize_t
regulator_print_state(char *buf
, int state
)
369 return sprintf(buf
, "enabled\n");
371 return sprintf(buf
, "disabled\n");
373 return sprintf(buf
, "unknown\n");
376 static ssize_t
regulator_state_show(struct device
*dev
,
377 struct device_attribute
*attr
, char *buf
)
379 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
382 mutex_lock(&rdev
->mutex
);
383 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
384 mutex_unlock(&rdev
->mutex
);
388 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
390 static ssize_t
regulator_status_show(struct device
*dev
,
391 struct device_attribute
*attr
, char *buf
)
393 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
397 status
= rdev
->desc
->ops
->get_status(rdev
);
402 case REGULATOR_STATUS_OFF
:
405 case REGULATOR_STATUS_ON
:
408 case REGULATOR_STATUS_ERROR
:
411 case REGULATOR_STATUS_FAST
:
414 case REGULATOR_STATUS_NORMAL
:
417 case REGULATOR_STATUS_IDLE
:
420 case REGULATOR_STATUS_STANDBY
:
427 return sprintf(buf
, "%s\n", label
);
429 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
431 static ssize_t
regulator_min_uA_show(struct device
*dev
,
432 struct device_attribute
*attr
, char *buf
)
434 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
436 if (!rdev
->constraints
)
437 return sprintf(buf
, "constraint not defined\n");
439 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
441 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
443 static ssize_t
regulator_max_uA_show(struct device
*dev
,
444 struct device_attribute
*attr
, char *buf
)
446 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
448 if (!rdev
->constraints
)
449 return sprintf(buf
, "constraint not defined\n");
451 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
453 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
455 static ssize_t
regulator_min_uV_show(struct device
*dev
,
456 struct device_attribute
*attr
, char *buf
)
458 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
460 if (!rdev
->constraints
)
461 return sprintf(buf
, "constraint not defined\n");
463 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
465 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
467 static ssize_t
regulator_max_uV_show(struct device
*dev
,
468 struct device_attribute
*attr
, char *buf
)
470 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
472 if (!rdev
->constraints
)
473 return sprintf(buf
, "constraint not defined\n");
475 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
477 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
479 static ssize_t
regulator_total_uA_show(struct device
*dev
,
480 struct device_attribute
*attr
, char *buf
)
482 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
483 struct regulator
*regulator
;
486 mutex_lock(&rdev
->mutex
);
487 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
488 uA
+= regulator
->uA_load
;
489 mutex_unlock(&rdev
->mutex
);
490 return sprintf(buf
, "%d\n", uA
);
492 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
494 static ssize_t
regulator_num_users_show(struct device
*dev
,
495 struct device_attribute
*attr
, char *buf
)
497 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
498 return sprintf(buf
, "%d\n", rdev
->use_count
);
501 static ssize_t
regulator_type_show(struct device
*dev
,
502 struct device_attribute
*attr
, char *buf
)
504 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
506 switch (rdev
->desc
->type
) {
507 case REGULATOR_VOLTAGE
:
508 return sprintf(buf
, "voltage\n");
509 case REGULATOR_CURRENT
:
510 return sprintf(buf
, "current\n");
512 return sprintf(buf
, "unknown\n");
515 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
516 struct device_attribute
*attr
, char *buf
)
518 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
520 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
522 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
523 regulator_suspend_mem_uV_show
, NULL
);
525 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
526 struct device_attribute
*attr
, char *buf
)
528 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
530 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
532 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
533 regulator_suspend_disk_uV_show
, NULL
);
535 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
536 struct device_attribute
*attr
, char *buf
)
538 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
540 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
542 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
543 regulator_suspend_standby_uV_show
, NULL
);
545 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
546 struct device_attribute
*attr
, char *buf
)
548 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
550 return regulator_print_opmode(buf
,
551 rdev
->constraints
->state_mem
.mode
);
553 static DEVICE_ATTR(suspend_mem_mode
, 0444,
554 regulator_suspend_mem_mode_show
, NULL
);
556 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
557 struct device_attribute
*attr
, char *buf
)
559 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
561 return regulator_print_opmode(buf
,
562 rdev
->constraints
->state_disk
.mode
);
564 static DEVICE_ATTR(suspend_disk_mode
, 0444,
565 regulator_suspend_disk_mode_show
, NULL
);
567 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
568 struct device_attribute
*attr
, char *buf
)
570 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
572 return regulator_print_opmode(buf
,
573 rdev
->constraints
->state_standby
.mode
);
575 static DEVICE_ATTR(suspend_standby_mode
, 0444,
576 regulator_suspend_standby_mode_show
, NULL
);
578 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
579 struct device_attribute
*attr
, char *buf
)
581 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
583 return regulator_print_state(buf
,
584 rdev
->constraints
->state_mem
.enabled
);
586 static DEVICE_ATTR(suspend_mem_state
, 0444,
587 regulator_suspend_mem_state_show
, NULL
);
589 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
590 struct device_attribute
*attr
, char *buf
)
592 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
594 return regulator_print_state(buf
,
595 rdev
->constraints
->state_disk
.enabled
);
597 static DEVICE_ATTR(suspend_disk_state
, 0444,
598 regulator_suspend_disk_state_show
, NULL
);
600 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
601 struct device_attribute
*attr
, char *buf
)
603 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
605 return regulator_print_state(buf
,
606 rdev
->constraints
->state_standby
.enabled
);
608 static DEVICE_ATTR(suspend_standby_state
, 0444,
609 regulator_suspend_standby_state_show
, NULL
);
613 * These are the only attributes are present for all regulators.
614 * Other attributes are a function of regulator functionality.
616 static struct device_attribute regulator_dev_attrs
[] = {
617 __ATTR(name
, 0444, regulator_name_show
, NULL
),
618 __ATTR(num_users
, 0444, regulator_num_users_show
, NULL
),
619 __ATTR(type
, 0444, regulator_type_show
, NULL
),
623 static void regulator_dev_release(struct device
*dev
)
625 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
629 static struct class regulator_class
= {
631 .dev_release
= regulator_dev_release
,
632 .dev_attrs
= regulator_dev_attrs
,
635 /* Calculate the new optimum regulator operating mode based on the new total
636 * consumer load. All locks held by caller */
637 static void drms_uA_update(struct regulator_dev
*rdev
)
639 struct regulator
*sibling
;
640 int current_uA
= 0, output_uV
, input_uV
, err
;
643 err
= regulator_check_drms(rdev
);
644 if (err
< 0 || !rdev
->desc
->ops
->get_optimum_mode
||
645 (!rdev
->desc
->ops
->get_voltage
&&
646 !rdev
->desc
->ops
->get_voltage_sel
) ||
647 !rdev
->desc
->ops
->set_mode
)
650 /* get output voltage */
651 output_uV
= _regulator_get_voltage(rdev
);
655 /* get input voltage */
658 input_uV
= _regulator_get_voltage(rdev
);
660 input_uV
= rdev
->constraints
->input_uV
;
664 /* calc total requested load */
665 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
666 current_uA
+= sibling
->uA_load
;
668 /* now get the optimum mode for our new total regulator load */
669 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
670 output_uV
, current_uA
);
672 /* check the new mode is allowed */
673 err
= regulator_mode_constrain(rdev
, &mode
);
675 rdev
->desc
->ops
->set_mode(rdev
, mode
);
678 static int suspend_set_state(struct regulator_dev
*rdev
,
679 struct regulator_state
*rstate
)
684 can_set_state
= rdev
->desc
->ops
->set_suspend_enable
&&
685 rdev
->desc
->ops
->set_suspend_disable
;
687 /* If we have no suspend mode configration don't set anything;
688 * only warn if the driver actually makes the suspend mode
691 if (!rstate
->enabled
&& !rstate
->disabled
) {
693 rdev_warn(rdev
, "No configuration\n");
697 if (rstate
->enabled
&& rstate
->disabled
) {
698 rdev_err(rdev
, "invalid configuration\n");
702 if (!can_set_state
) {
703 rdev_err(rdev
, "no way to set suspend state\n");
708 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
710 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
712 rdev_err(rdev
, "failed to enabled/disable\n");
716 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
717 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
719 rdev_err(rdev
, "failed to set voltage\n");
724 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
725 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
727 rdev_err(rdev
, "failed to set mode\n");
734 /* locks held by caller */
735 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
737 if (!rdev
->constraints
)
741 case PM_SUSPEND_STANDBY
:
742 return suspend_set_state(rdev
,
743 &rdev
->constraints
->state_standby
);
745 return suspend_set_state(rdev
,
746 &rdev
->constraints
->state_mem
);
748 return suspend_set_state(rdev
,
749 &rdev
->constraints
->state_disk
);
755 static void print_constraints(struct regulator_dev
*rdev
)
757 struct regulation_constraints
*constraints
= rdev
->constraints
;
762 if (constraints
->min_uV
&& constraints
->max_uV
) {
763 if (constraints
->min_uV
== constraints
->max_uV
)
764 count
+= sprintf(buf
+ count
, "%d mV ",
765 constraints
->min_uV
/ 1000);
767 count
+= sprintf(buf
+ count
, "%d <--> %d mV ",
768 constraints
->min_uV
/ 1000,
769 constraints
->max_uV
/ 1000);
772 if (!constraints
->min_uV
||
773 constraints
->min_uV
!= constraints
->max_uV
) {
774 ret
= _regulator_get_voltage(rdev
);
776 count
+= sprintf(buf
+ count
, "at %d mV ", ret
/ 1000);
779 if (constraints
->uV_offset
)
780 count
+= sprintf(buf
, "%dmV offset ",
781 constraints
->uV_offset
/ 1000);
783 if (constraints
->min_uA
&& constraints
->max_uA
) {
784 if (constraints
->min_uA
== constraints
->max_uA
)
785 count
+= sprintf(buf
+ count
, "%d mA ",
786 constraints
->min_uA
/ 1000);
788 count
+= sprintf(buf
+ count
, "%d <--> %d mA ",
789 constraints
->min_uA
/ 1000,
790 constraints
->max_uA
/ 1000);
793 if (!constraints
->min_uA
||
794 constraints
->min_uA
!= constraints
->max_uA
) {
795 ret
= _regulator_get_current_limit(rdev
);
797 count
+= sprintf(buf
+ count
, "at %d mA ", ret
/ 1000);
800 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
801 count
+= sprintf(buf
+ count
, "fast ");
802 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
803 count
+= sprintf(buf
+ count
, "normal ");
804 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
805 count
+= sprintf(buf
+ count
, "idle ");
806 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
807 count
+= sprintf(buf
+ count
, "standby");
809 rdev_info(rdev
, "%s\n", buf
);
812 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
813 struct regulation_constraints
*constraints
)
815 struct regulator_ops
*ops
= rdev
->desc
->ops
;
818 /* do we need to apply the constraint voltage */
819 if (rdev
->constraints
->apply_uV
&&
820 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
) {
821 ret
= _regulator_do_set_voltage(rdev
,
822 rdev
->constraints
->min_uV
,
823 rdev
->constraints
->max_uV
);
825 rdev_err(rdev
, "failed to apply %duV constraint\n",
826 rdev
->constraints
->min_uV
);
831 /* constrain machine-level voltage specs to fit
832 * the actual range supported by this regulator.
834 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
835 int count
= rdev
->desc
->n_voltages
;
837 int min_uV
= INT_MAX
;
838 int max_uV
= INT_MIN
;
839 int cmin
= constraints
->min_uV
;
840 int cmax
= constraints
->max_uV
;
842 /* it's safe to autoconfigure fixed-voltage supplies
843 and the constraints are used by list_voltage. */
844 if (count
== 1 && !cmin
) {
847 constraints
->min_uV
= cmin
;
848 constraints
->max_uV
= cmax
;
851 /* voltage constraints are optional */
852 if ((cmin
== 0) && (cmax
== 0))
855 /* else require explicit machine-level constraints */
856 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
857 rdev_err(rdev
, "invalid voltage constraints\n");
861 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
862 for (i
= 0; i
< count
; i
++) {
865 value
= ops
->list_voltage(rdev
, i
);
869 /* maybe adjust [min_uV..max_uV] */
870 if (value
>= cmin
&& value
< min_uV
)
872 if (value
<= cmax
&& value
> max_uV
)
876 /* final: [min_uV..max_uV] valid iff constraints valid */
877 if (max_uV
< min_uV
) {
878 rdev_err(rdev
, "unsupportable voltage constraints\n");
882 /* use regulator's subset of machine constraints */
883 if (constraints
->min_uV
< min_uV
) {
884 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
885 constraints
->min_uV
, min_uV
);
886 constraints
->min_uV
= min_uV
;
888 if (constraints
->max_uV
> max_uV
) {
889 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
890 constraints
->max_uV
, max_uV
);
891 constraints
->max_uV
= max_uV
;
899 * set_machine_constraints - sets regulator constraints
900 * @rdev: regulator source
901 * @constraints: constraints to apply
903 * Allows platform initialisation code to define and constrain
904 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
905 * Constraints *must* be set by platform code in order for some
906 * regulator operations to proceed i.e. set_voltage, set_current_limit,
909 static int set_machine_constraints(struct regulator_dev
*rdev
,
910 const struct regulation_constraints
*constraints
)
913 struct regulator_ops
*ops
= rdev
->desc
->ops
;
916 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
919 rdev
->constraints
= kzalloc(sizeof(*constraints
),
921 if (!rdev
->constraints
)
924 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
928 /* do we need to setup our suspend state */
929 if (rdev
->constraints
->initial_state
) {
930 ret
= suspend_prepare(rdev
, rdev
->constraints
->initial_state
);
932 rdev_err(rdev
, "failed to set suspend state\n");
937 if (rdev
->constraints
->initial_mode
) {
938 if (!ops
->set_mode
) {
939 rdev_err(rdev
, "no set_mode operation\n");
944 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
946 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
951 /* If the constraints say the regulator should be on at this point
952 * and we have control then make sure it is enabled.
954 if ((rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) &&
956 ret
= ops
->enable(rdev
);
958 rdev_err(rdev
, "failed to enable\n");
963 print_constraints(rdev
);
966 kfree(rdev
->constraints
);
967 rdev
->constraints
= NULL
;
972 * set_supply - set regulator supply regulator
973 * @rdev: regulator name
974 * @supply_rdev: supply regulator name
976 * Called by platform initialisation code to set the supply regulator for this
977 * regulator. This ensures that a regulators supply will also be enabled by the
978 * core if it's child is enabled.
980 static int set_supply(struct regulator_dev
*rdev
,
981 struct regulator_dev
*supply_rdev
)
985 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
987 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
988 if (rdev
->supply
== NULL
) {
997 * set_consumer_device_supply - Bind a regulator to a symbolic supply
998 * @rdev: regulator source
999 * @consumer_dev: device the supply applies to
1000 * @consumer_dev_name: dev_name() string for device supply applies to
1001 * @supply: symbolic name for supply
1003 * Allows platform initialisation code to map physical regulator
1004 * sources to symbolic names for supplies for use by devices. Devices
1005 * should use these symbolic names to request regulators, avoiding the
1006 * need to provide board-specific regulator names as platform data.
1008 * Only one of consumer_dev and consumer_dev_name may be specified.
1010 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
1011 struct device
*consumer_dev
, const char *consumer_dev_name
,
1014 struct regulator_map
*node
;
1017 if (consumer_dev
&& consumer_dev_name
)
1020 if (!consumer_dev_name
&& consumer_dev
)
1021 consumer_dev_name
= dev_name(consumer_dev
);
1026 if (consumer_dev_name
!= NULL
)
1031 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1032 if (node
->dev_name
&& consumer_dev_name
) {
1033 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1035 } else if (node
->dev_name
|| consumer_dev_name
) {
1039 if (strcmp(node
->supply
, supply
) != 0)
1042 dev_dbg(consumer_dev
, "%s/%s is '%s' supply; fail %s/%s\n",
1043 dev_name(&node
->regulator
->dev
),
1044 node
->regulator
->desc
->name
,
1046 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1050 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1054 node
->regulator
= rdev
;
1055 node
->supply
= supply
;
1058 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1059 if (node
->dev_name
== NULL
) {
1065 list_add(&node
->list
, ®ulator_map_list
);
1069 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1071 struct regulator_map
*node
, *n
;
1073 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1074 if (rdev
== node
->regulator
) {
1075 list_del(&node
->list
);
1076 kfree(node
->dev_name
);
1082 #define REG_STR_SIZE 64
1084 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1086 const char *supply_name
)
1088 struct regulator
*regulator
;
1089 char buf
[REG_STR_SIZE
];
1092 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1093 if (regulator
== NULL
)
1096 mutex_lock(&rdev
->mutex
);
1097 regulator
->rdev
= rdev
;
1098 list_add(®ulator
->list
, &rdev
->consumer_list
);
1101 /* create a 'requested_microamps_name' sysfs entry */
1102 size
= scnprintf(buf
, REG_STR_SIZE
,
1103 "microamps_requested_%s-%s",
1104 dev_name(dev
), supply_name
);
1105 if (size
>= REG_STR_SIZE
)
1108 regulator
->dev
= dev
;
1109 sysfs_attr_init(®ulator
->dev_attr
.attr
);
1110 regulator
->dev_attr
.attr
.name
= kstrdup(buf
, GFP_KERNEL
);
1111 if (regulator
->dev_attr
.attr
.name
== NULL
)
1114 regulator
->dev_attr
.attr
.mode
= 0444;
1115 regulator
->dev_attr
.show
= device_requested_uA_show
;
1116 err
= device_create_file(dev
, ®ulator
->dev_attr
);
1118 rdev_warn(rdev
, "could not add regulator_dev requested microamps sysfs entry\n");
1122 /* also add a link to the device sysfs entry */
1123 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1124 dev
->kobj
.name
, supply_name
);
1125 if (size
>= REG_STR_SIZE
)
1128 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1129 if (regulator
->supply_name
== NULL
)
1132 err
= sysfs_create_link(&rdev
->dev
.kobj
, &dev
->kobj
,
1135 rdev_warn(rdev
, "could not add device link %s err %d\n",
1136 dev
->kobj
.name
, err
);
1140 regulator
->supply_name
= kstrdup(supply_name
, GFP_KERNEL
);
1141 if (regulator
->supply_name
== NULL
)
1145 #ifdef CONFIG_DEBUG_FS
1146 regulator
->debugfs
= debugfs_create_dir(regulator
->supply_name
,
1148 if (IS_ERR_OR_NULL(regulator
->debugfs
)) {
1149 rdev_warn(rdev
, "Failed to create debugfs directory\n");
1150 regulator
->debugfs
= NULL
;
1152 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1153 ®ulator
->uA_load
);
1154 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1155 ®ulator
->min_uV
);
1156 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1157 ®ulator
->max_uV
);
1161 mutex_unlock(&rdev
->mutex
);
1164 kfree(regulator
->supply_name
);
1166 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1168 kfree(regulator
->dev_attr
.attr
.name
);
1170 list_del(®ulator
->list
);
1172 mutex_unlock(&rdev
->mutex
);
1176 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1178 if (!rdev
->desc
->ops
->enable_time
)
1180 return rdev
->desc
->ops
->enable_time(rdev
);
1183 static struct regulator_dev
*regulator_dev_lookup(struct device
*dev
,
1186 struct regulator_dev
*r
;
1187 struct device_node
*node
;
1189 /* first do a dt based lookup */
1190 if (dev
&& dev
->of_node
) {
1191 node
= of_get_regulator(dev
, supply
);
1193 list_for_each_entry(r
, ®ulator_list
, list
)
1194 if (r
->dev
.parent
&&
1195 node
== r
->dev
.of_node
)
1199 /* if not found, try doing it non-dt way */
1200 list_for_each_entry(r
, ®ulator_list
, list
)
1201 if (strcmp(rdev_get_name(r
), supply
) == 0)
1207 /* Internal regulator request function */
1208 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1211 struct regulator_dev
*rdev
;
1212 struct regulator_map
*map
;
1213 struct regulator
*regulator
= ERR_PTR(-ENODEV
);
1214 const char *devname
= NULL
;
1218 pr_err("get() with no identifier\n");
1223 devname
= dev_name(dev
);
1225 mutex_lock(®ulator_list_mutex
);
1227 rdev
= regulator_dev_lookup(dev
, id
);
1231 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1232 /* If the mapping has a device set up it must match */
1233 if (map
->dev_name
&&
1234 (!devname
|| strcmp(map
->dev_name
, devname
)))
1237 if (strcmp(map
->supply
, id
) == 0) {
1238 rdev
= map
->regulator
;
1243 if (board_wants_dummy_regulator
) {
1244 rdev
= dummy_regulator_rdev
;
1248 #ifdef CONFIG_REGULATOR_DUMMY
1250 devname
= "deviceless";
1252 /* If the board didn't flag that it was fully constrained then
1253 * substitute in a dummy regulator so consumers can continue.
1255 if (!has_full_constraints
) {
1256 pr_warn("%s supply %s not found, using dummy regulator\n",
1258 rdev
= dummy_regulator_rdev
;
1263 mutex_unlock(®ulator_list_mutex
);
1267 if (rdev
->exclusive
) {
1268 regulator
= ERR_PTR(-EPERM
);
1272 if (exclusive
&& rdev
->open_count
) {
1273 regulator
= ERR_PTR(-EBUSY
);
1277 if (!try_module_get(rdev
->owner
))
1280 regulator
= create_regulator(rdev
, dev
, id
);
1281 if (regulator
== NULL
) {
1282 regulator
= ERR_PTR(-ENOMEM
);
1283 module_put(rdev
->owner
);
1289 rdev
->exclusive
= 1;
1291 ret
= _regulator_is_enabled(rdev
);
1293 rdev
->use_count
= 1;
1295 rdev
->use_count
= 0;
1299 mutex_unlock(®ulator_list_mutex
);
1305 * regulator_get - lookup and obtain a reference to a regulator.
1306 * @dev: device for regulator "consumer"
1307 * @id: Supply name or regulator ID.
1309 * Returns a struct regulator corresponding to the regulator producer,
1310 * or IS_ERR() condition containing errno.
1312 * Use of supply names configured via regulator_set_device_supply() is
1313 * strongly encouraged. It is recommended that the supply name used
1314 * should match the name used for the supply and/or the relevant
1315 * device pins in the datasheet.
1317 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1319 return _regulator_get(dev
, id
, 0);
1321 EXPORT_SYMBOL_GPL(regulator_get
);
1324 * regulator_get_exclusive - obtain exclusive access to a regulator.
1325 * @dev: device for regulator "consumer"
1326 * @id: Supply name or regulator ID.
1328 * Returns a struct regulator corresponding to the regulator producer,
1329 * or IS_ERR() condition containing errno. Other consumers will be
1330 * unable to obtain this reference is held and the use count for the
1331 * regulator will be initialised to reflect the current state of the
1334 * This is intended for use by consumers which cannot tolerate shared
1335 * use of the regulator such as those which need to force the
1336 * regulator off for correct operation of the hardware they are
1339 * Use of supply names configured via regulator_set_device_supply() is
1340 * strongly encouraged. It is recommended that the supply name used
1341 * should match the name used for the supply and/or the relevant
1342 * device pins in the datasheet.
1344 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1346 return _regulator_get(dev
, id
, 1);
1348 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1351 * regulator_put - "free" the regulator source
1352 * @regulator: regulator source
1354 * Note: drivers must ensure that all regulator_enable calls made on this
1355 * regulator source are balanced by regulator_disable calls prior to calling
1358 void regulator_put(struct regulator
*regulator
)
1360 struct regulator_dev
*rdev
;
1362 if (regulator
== NULL
|| IS_ERR(regulator
))
1365 mutex_lock(®ulator_list_mutex
);
1366 rdev
= regulator
->rdev
;
1368 #ifdef CONFIG_DEBUG_FS
1369 debugfs_remove_recursive(regulator
->debugfs
);
1372 /* remove any sysfs entries */
1373 if (regulator
->dev
) {
1374 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1375 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1376 kfree(regulator
->dev_attr
.attr
.name
);
1378 kfree(regulator
->supply_name
);
1379 list_del(®ulator
->list
);
1383 rdev
->exclusive
= 0;
1385 module_put(rdev
->owner
);
1386 mutex_unlock(®ulator_list_mutex
);
1388 EXPORT_SYMBOL_GPL(regulator_put
);
1390 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
1392 if (!rdev
->constraints
)
1395 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
1401 /* locks held by regulator_enable() */
1402 static int _regulator_enable(struct regulator_dev
*rdev
)
1406 /* check voltage and requested load before enabling */
1407 if (rdev
->constraints
&&
1408 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1409 drms_uA_update(rdev
);
1411 if (rdev
->use_count
== 0) {
1412 /* The regulator may on if it's not switchable or left on */
1413 ret
= _regulator_is_enabled(rdev
);
1414 if (ret
== -EINVAL
|| ret
== 0) {
1415 if (!_regulator_can_change_status(rdev
))
1418 if (!rdev
->desc
->ops
->enable
)
1421 /* Query before enabling in case configuration
1423 ret
= _regulator_get_enable_time(rdev
);
1427 rdev_warn(rdev
, "enable_time() failed: %d\n",
1432 trace_regulator_enable(rdev_get_name(rdev
));
1434 /* Allow the regulator to ramp; it would be useful
1435 * to extend this for bulk operations so that the
1436 * regulators can ramp together. */
1437 ret
= rdev
->desc
->ops
->enable(rdev
);
1441 trace_regulator_enable_delay(rdev_get_name(rdev
));
1443 if (delay
>= 1000) {
1444 mdelay(delay
/ 1000);
1445 udelay(delay
% 1000);
1450 trace_regulator_enable_complete(rdev_get_name(rdev
));
1452 } else if (ret
< 0) {
1453 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
1456 /* Fallthrough on positive return values - already enabled */
1465 * regulator_enable - enable regulator output
1466 * @regulator: regulator source
1468 * Request that the regulator be enabled with the regulator output at
1469 * the predefined voltage or current value. Calls to regulator_enable()
1470 * must be balanced with calls to regulator_disable().
1472 * NOTE: the output value can be set by other drivers, boot loader or may be
1473 * hardwired in the regulator.
1475 int regulator_enable(struct regulator
*regulator
)
1477 struct regulator_dev
*rdev
= regulator
->rdev
;
1481 ret
= regulator_enable(rdev
->supply
);
1486 mutex_lock(&rdev
->mutex
);
1487 ret
= _regulator_enable(rdev
);
1488 mutex_unlock(&rdev
->mutex
);
1490 if (ret
!= 0 && rdev
->supply
)
1491 regulator_disable(rdev
->supply
);
1495 EXPORT_SYMBOL_GPL(regulator_enable
);
1497 /* locks held by regulator_disable() */
1498 static int _regulator_disable(struct regulator_dev
*rdev
)
1502 if (WARN(rdev
->use_count
<= 0,
1503 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
1506 /* are we the last user and permitted to disable ? */
1507 if (rdev
->use_count
== 1 &&
1508 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
1510 /* we are last user */
1511 if (_regulator_can_change_status(rdev
) &&
1512 rdev
->desc
->ops
->disable
) {
1513 trace_regulator_disable(rdev_get_name(rdev
));
1515 ret
= rdev
->desc
->ops
->disable(rdev
);
1517 rdev_err(rdev
, "failed to disable\n");
1521 trace_regulator_disable_complete(rdev_get_name(rdev
));
1523 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
1527 rdev
->use_count
= 0;
1528 } else if (rdev
->use_count
> 1) {
1530 if (rdev
->constraints
&&
1531 (rdev
->constraints
->valid_ops_mask
&
1532 REGULATOR_CHANGE_DRMS
))
1533 drms_uA_update(rdev
);
1542 * regulator_disable - disable regulator output
1543 * @regulator: regulator source
1545 * Disable the regulator output voltage or current. Calls to
1546 * regulator_enable() must be balanced with calls to
1547 * regulator_disable().
1549 * NOTE: this will only disable the regulator output if no other consumer
1550 * devices have it enabled, the regulator device supports disabling and
1551 * machine constraints permit this operation.
1553 int regulator_disable(struct regulator
*regulator
)
1555 struct regulator_dev
*rdev
= regulator
->rdev
;
1558 mutex_lock(&rdev
->mutex
);
1559 ret
= _regulator_disable(rdev
);
1560 mutex_unlock(&rdev
->mutex
);
1562 if (ret
== 0 && rdev
->supply
)
1563 regulator_disable(rdev
->supply
);
1567 EXPORT_SYMBOL_GPL(regulator_disable
);
1569 /* locks held by regulator_force_disable() */
1570 static int _regulator_force_disable(struct regulator_dev
*rdev
)
1575 if (rdev
->desc
->ops
->disable
) {
1576 /* ah well, who wants to live forever... */
1577 ret
= rdev
->desc
->ops
->disable(rdev
);
1579 rdev_err(rdev
, "failed to force disable\n");
1582 /* notify other consumers that power has been forced off */
1583 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
1584 REGULATOR_EVENT_DISABLE
, NULL
);
1591 * regulator_force_disable - force disable regulator output
1592 * @regulator: regulator source
1594 * Forcibly disable the regulator output voltage or current.
1595 * NOTE: this *will* disable the regulator output even if other consumer
1596 * devices have it enabled. This should be used for situations when device
1597 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1599 int regulator_force_disable(struct regulator
*regulator
)
1601 struct regulator_dev
*rdev
= regulator
->rdev
;
1604 mutex_lock(&rdev
->mutex
);
1605 regulator
->uA_load
= 0;
1606 ret
= _regulator_force_disable(regulator
->rdev
);
1607 mutex_unlock(&rdev
->mutex
);
1610 while (rdev
->open_count
--)
1611 regulator_disable(rdev
->supply
);
1615 EXPORT_SYMBOL_GPL(regulator_force_disable
);
1617 static void regulator_disable_work(struct work_struct
*work
)
1619 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
1623 mutex_lock(&rdev
->mutex
);
1625 BUG_ON(!rdev
->deferred_disables
);
1627 count
= rdev
->deferred_disables
;
1628 rdev
->deferred_disables
= 0;
1630 for (i
= 0; i
< count
; i
++) {
1631 ret
= _regulator_disable(rdev
);
1633 rdev_err(rdev
, "Deferred disable failed: %d\n", ret
);
1636 mutex_unlock(&rdev
->mutex
);
1639 for (i
= 0; i
< count
; i
++) {
1640 ret
= regulator_disable(rdev
->supply
);
1643 "Supply disable failed: %d\n", ret
);
1650 * regulator_disable_deferred - disable regulator output with delay
1651 * @regulator: regulator source
1652 * @ms: miliseconds until the regulator is disabled
1654 * Execute regulator_disable() on the regulator after a delay. This
1655 * is intended for use with devices that require some time to quiesce.
1657 * NOTE: this will only disable the regulator output if no other consumer
1658 * devices have it enabled, the regulator device supports disabling and
1659 * machine constraints permit this operation.
1661 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
1663 struct regulator_dev
*rdev
= regulator
->rdev
;
1666 mutex_lock(&rdev
->mutex
);
1667 rdev
->deferred_disables
++;
1668 mutex_unlock(&rdev
->mutex
);
1670 ret
= schedule_delayed_work(&rdev
->disable_work
,
1671 msecs_to_jiffies(ms
));
1677 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
1679 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
1681 /* If we don't know then assume that the regulator is always on */
1682 if (!rdev
->desc
->ops
->is_enabled
)
1685 return rdev
->desc
->ops
->is_enabled(rdev
);
1689 * regulator_is_enabled - is the regulator output enabled
1690 * @regulator: regulator source
1692 * Returns positive if the regulator driver backing the source/client
1693 * has requested that the device be enabled, zero if it hasn't, else a
1694 * negative errno code.
1696 * Note that the device backing this regulator handle can have multiple
1697 * users, so it might be enabled even if regulator_enable() was never
1698 * called for this particular source.
1700 int regulator_is_enabled(struct regulator
*regulator
)
1704 mutex_lock(®ulator
->rdev
->mutex
);
1705 ret
= _regulator_is_enabled(regulator
->rdev
);
1706 mutex_unlock(®ulator
->rdev
->mutex
);
1710 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
1713 * regulator_count_voltages - count regulator_list_voltage() selectors
1714 * @regulator: regulator source
1716 * Returns number of selectors, or negative errno. Selectors are
1717 * numbered starting at zero, and typically correspond to bitfields
1718 * in hardware registers.
1720 int regulator_count_voltages(struct regulator
*regulator
)
1722 struct regulator_dev
*rdev
= regulator
->rdev
;
1724 return rdev
->desc
->n_voltages
? : -EINVAL
;
1726 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
1729 * regulator_list_voltage - enumerate supported voltages
1730 * @regulator: regulator source
1731 * @selector: identify voltage to list
1732 * Context: can sleep
1734 * Returns a voltage that can be passed to @regulator_set_voltage(),
1735 * zero if this selector code can't be used on this system, or a
1738 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
1740 struct regulator_dev
*rdev
= regulator
->rdev
;
1741 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1744 if (!ops
->list_voltage
|| selector
>= rdev
->desc
->n_voltages
)
1747 mutex_lock(&rdev
->mutex
);
1748 ret
= ops
->list_voltage(rdev
, selector
);
1749 mutex_unlock(&rdev
->mutex
);
1752 if (ret
< rdev
->constraints
->min_uV
)
1754 else if (ret
> rdev
->constraints
->max_uV
)
1760 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
1763 * regulator_is_supported_voltage - check if a voltage range can be supported
1765 * @regulator: Regulator to check.
1766 * @min_uV: Minimum required voltage in uV.
1767 * @max_uV: Maximum required voltage in uV.
1769 * Returns a boolean or a negative error code.
1771 int regulator_is_supported_voltage(struct regulator
*regulator
,
1772 int min_uV
, int max_uV
)
1774 int i
, voltages
, ret
;
1776 ret
= regulator_count_voltages(regulator
);
1781 for (i
= 0; i
< voltages
; i
++) {
1782 ret
= regulator_list_voltage(regulator
, i
);
1784 if (ret
>= min_uV
&& ret
<= max_uV
)
1790 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage
);
1792 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
1793 int min_uV
, int max_uV
)
1797 unsigned int selector
;
1799 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
1801 min_uV
+= rdev
->constraints
->uV_offset
;
1802 max_uV
+= rdev
->constraints
->uV_offset
;
1804 if (rdev
->desc
->ops
->set_voltage
) {
1805 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
,
1808 if (rdev
->desc
->ops
->list_voltage
)
1809 selector
= rdev
->desc
->ops
->list_voltage(rdev
,
1813 } else if (rdev
->desc
->ops
->set_voltage_sel
) {
1814 int best_val
= INT_MAX
;
1819 /* Find the smallest voltage that falls within the specified
1822 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
1823 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
1827 if (ret
< best_val
&& ret
>= min_uV
&& ret
<= max_uV
) {
1834 * If we can't obtain the old selector there is not enough
1835 * info to call set_voltage_time_sel().
1837 if (rdev
->desc
->ops
->set_voltage_time_sel
&&
1838 rdev
->desc
->ops
->get_voltage_sel
) {
1839 unsigned int old_selector
= 0;
1841 ret
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
1845 delay
= rdev
->desc
->ops
->set_voltage_time_sel(rdev
,
1846 old_selector
, selector
);
1849 if (best_val
!= INT_MAX
) {
1850 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
1851 selector
= best_val
;
1859 /* Insert any necessary delays */
1860 if (delay
>= 1000) {
1861 mdelay(delay
/ 1000);
1862 udelay(delay
% 1000);
1868 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
1871 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), selector
);
1877 * regulator_set_voltage - set regulator output voltage
1878 * @regulator: regulator source
1879 * @min_uV: Minimum required voltage in uV
1880 * @max_uV: Maximum acceptable voltage in uV
1882 * Sets a voltage regulator to the desired output voltage. This can be set
1883 * during any regulator state. IOW, regulator can be disabled or enabled.
1885 * If the regulator is enabled then the voltage will change to the new value
1886 * immediately otherwise if the regulator is disabled the regulator will
1887 * output at the new voltage when enabled.
1889 * NOTE: If the regulator is shared between several devices then the lowest
1890 * request voltage that meets the system constraints will be used.
1891 * Regulator system constraints must be set for this regulator before
1892 * calling this function otherwise this call will fail.
1894 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
1896 struct regulator_dev
*rdev
= regulator
->rdev
;
1899 mutex_lock(&rdev
->mutex
);
1901 /* If we're setting the same range as last time the change
1902 * should be a noop (some cpufreq implementations use the same
1903 * voltage for multiple frequencies, for example).
1905 if (regulator
->min_uV
== min_uV
&& regulator
->max_uV
== max_uV
)
1909 if (!rdev
->desc
->ops
->set_voltage
&&
1910 !rdev
->desc
->ops
->set_voltage_sel
) {
1915 /* constraints check */
1916 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1919 regulator
->min_uV
= min_uV
;
1920 regulator
->max_uV
= max_uV
;
1922 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
1926 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
1929 mutex_unlock(&rdev
->mutex
);
1932 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
1935 * regulator_set_voltage_time - get raise/fall time
1936 * @regulator: regulator source
1937 * @old_uV: starting voltage in microvolts
1938 * @new_uV: target voltage in microvolts
1940 * Provided with the starting and ending voltage, this function attempts to
1941 * calculate the time in microseconds required to rise or fall to this new
1944 int regulator_set_voltage_time(struct regulator
*regulator
,
1945 int old_uV
, int new_uV
)
1947 struct regulator_dev
*rdev
= regulator
->rdev
;
1948 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1954 /* Currently requires operations to do this */
1955 if (!ops
->list_voltage
|| !ops
->set_voltage_time_sel
1956 || !rdev
->desc
->n_voltages
)
1959 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
1960 /* We only look for exact voltage matches here */
1961 voltage
= regulator_list_voltage(regulator
, i
);
1966 if (voltage
== old_uV
)
1968 if (voltage
== new_uV
)
1972 if (old_sel
< 0 || new_sel
< 0)
1975 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
1977 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
1980 * regulator_sync_voltage - re-apply last regulator output voltage
1981 * @regulator: regulator source
1983 * Re-apply the last configured voltage. This is intended to be used
1984 * where some external control source the consumer is cooperating with
1985 * has caused the configured voltage to change.
1987 int regulator_sync_voltage(struct regulator
*regulator
)
1989 struct regulator_dev
*rdev
= regulator
->rdev
;
1990 int ret
, min_uV
, max_uV
;
1992 mutex_lock(&rdev
->mutex
);
1994 if (!rdev
->desc
->ops
->set_voltage
&&
1995 !rdev
->desc
->ops
->set_voltage_sel
) {
2000 /* This is only going to work if we've had a voltage configured. */
2001 if (!regulator
->min_uV
&& !regulator
->max_uV
) {
2006 min_uV
= regulator
->min_uV
;
2007 max_uV
= regulator
->max_uV
;
2009 /* This should be a paranoia check... */
2010 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
2014 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
2018 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
2021 mutex_unlock(&rdev
->mutex
);
2024 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
2026 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
2030 if (rdev
->desc
->ops
->get_voltage_sel
) {
2031 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
2034 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
2035 } else if (rdev
->desc
->ops
->get_voltage
) {
2036 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
2043 return ret
- rdev
->constraints
->uV_offset
;
2047 * regulator_get_voltage - get regulator output voltage
2048 * @regulator: regulator source
2050 * This returns the current regulator voltage in uV.
2052 * NOTE: If the regulator is disabled it will return the voltage value. This
2053 * function should not be used to determine regulator state.
2055 int regulator_get_voltage(struct regulator
*regulator
)
2059 mutex_lock(®ulator
->rdev
->mutex
);
2061 ret
= _regulator_get_voltage(regulator
->rdev
);
2063 mutex_unlock(®ulator
->rdev
->mutex
);
2067 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
2070 * regulator_set_current_limit - set regulator output current limit
2071 * @regulator: regulator source
2072 * @min_uA: Minimuum supported current in uA
2073 * @max_uA: Maximum supported current in uA
2075 * Sets current sink to the desired output current. This can be set during
2076 * any regulator state. IOW, regulator can be disabled or enabled.
2078 * If the regulator is enabled then the current will change to the new value
2079 * immediately otherwise if the regulator is disabled the regulator will
2080 * output at the new current when enabled.
2082 * NOTE: Regulator system constraints must be set for this regulator before
2083 * calling this function otherwise this call will fail.
2085 int regulator_set_current_limit(struct regulator
*regulator
,
2086 int min_uA
, int max_uA
)
2088 struct regulator_dev
*rdev
= regulator
->rdev
;
2091 mutex_lock(&rdev
->mutex
);
2094 if (!rdev
->desc
->ops
->set_current_limit
) {
2099 /* constraints check */
2100 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
2104 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
2106 mutex_unlock(&rdev
->mutex
);
2109 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
2111 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
2115 mutex_lock(&rdev
->mutex
);
2118 if (!rdev
->desc
->ops
->get_current_limit
) {
2123 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
2125 mutex_unlock(&rdev
->mutex
);
2130 * regulator_get_current_limit - get regulator output current
2131 * @regulator: regulator source
2133 * This returns the current supplied by the specified current sink in uA.
2135 * NOTE: If the regulator is disabled it will return the current value. This
2136 * function should not be used to determine regulator state.
2138 int regulator_get_current_limit(struct regulator
*regulator
)
2140 return _regulator_get_current_limit(regulator
->rdev
);
2142 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
2145 * regulator_set_mode - set regulator operating mode
2146 * @regulator: regulator source
2147 * @mode: operating mode - one of the REGULATOR_MODE constants
2149 * Set regulator operating mode to increase regulator efficiency or improve
2150 * regulation performance.
2152 * NOTE: Regulator system constraints must be set for this regulator before
2153 * calling this function otherwise this call will fail.
2155 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
2157 struct regulator_dev
*rdev
= regulator
->rdev
;
2159 int regulator_curr_mode
;
2161 mutex_lock(&rdev
->mutex
);
2164 if (!rdev
->desc
->ops
->set_mode
) {
2169 /* return if the same mode is requested */
2170 if (rdev
->desc
->ops
->get_mode
) {
2171 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
2172 if (regulator_curr_mode
== mode
) {
2178 /* constraints check */
2179 ret
= regulator_mode_constrain(rdev
, &mode
);
2183 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
2185 mutex_unlock(&rdev
->mutex
);
2188 EXPORT_SYMBOL_GPL(regulator_set_mode
);
2190 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
2194 mutex_lock(&rdev
->mutex
);
2197 if (!rdev
->desc
->ops
->get_mode
) {
2202 ret
= rdev
->desc
->ops
->get_mode(rdev
);
2204 mutex_unlock(&rdev
->mutex
);
2209 * regulator_get_mode - get regulator operating mode
2210 * @regulator: regulator source
2212 * Get the current regulator operating mode.
2214 unsigned int regulator_get_mode(struct regulator
*regulator
)
2216 return _regulator_get_mode(regulator
->rdev
);
2218 EXPORT_SYMBOL_GPL(regulator_get_mode
);
2221 * regulator_set_optimum_mode - set regulator optimum operating mode
2222 * @regulator: regulator source
2223 * @uA_load: load current
2225 * Notifies the regulator core of a new device load. This is then used by
2226 * DRMS (if enabled by constraints) to set the most efficient regulator
2227 * operating mode for the new regulator loading.
2229 * Consumer devices notify their supply regulator of the maximum power
2230 * they will require (can be taken from device datasheet in the power
2231 * consumption tables) when they change operational status and hence power
2232 * state. Examples of operational state changes that can affect power
2233 * consumption are :-
2235 * o Device is opened / closed.
2236 * o Device I/O is about to begin or has just finished.
2237 * o Device is idling in between work.
2239 * This information is also exported via sysfs to userspace.
2241 * DRMS will sum the total requested load on the regulator and change
2242 * to the most efficient operating mode if platform constraints allow.
2244 * Returns the new regulator mode or error.
2246 int regulator_set_optimum_mode(struct regulator
*regulator
, int uA_load
)
2248 struct regulator_dev
*rdev
= regulator
->rdev
;
2249 struct regulator
*consumer
;
2250 int ret
, output_uV
, input_uV
, total_uA_load
= 0;
2253 mutex_lock(&rdev
->mutex
);
2256 * first check to see if we can set modes at all, otherwise just
2257 * tell the consumer everything is OK.
2259 regulator
->uA_load
= uA_load
;
2260 ret
= regulator_check_drms(rdev
);
2266 if (!rdev
->desc
->ops
->get_optimum_mode
)
2270 * we can actually do this so any errors are indicators of
2271 * potential real failure.
2275 /* get output voltage */
2276 output_uV
= _regulator_get_voltage(rdev
);
2277 if (output_uV
<= 0) {
2278 rdev_err(rdev
, "invalid output voltage found\n");
2282 /* get input voltage */
2285 input_uV
= regulator_get_voltage(rdev
->supply
);
2287 input_uV
= rdev
->constraints
->input_uV
;
2288 if (input_uV
<= 0) {
2289 rdev_err(rdev
, "invalid input voltage found\n");
2293 /* calc total requested load for this regulator */
2294 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
)
2295 total_uA_load
+= consumer
->uA_load
;
2297 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
,
2298 input_uV
, output_uV
,
2300 ret
= regulator_mode_constrain(rdev
, &mode
);
2302 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2303 total_uA_load
, input_uV
, output_uV
);
2307 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
2309 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
2314 mutex_unlock(&rdev
->mutex
);
2317 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode
);
2320 * regulator_register_notifier - register regulator event notifier
2321 * @regulator: regulator source
2322 * @nb: notifier block
2324 * Register notifier block to receive regulator events.
2326 int regulator_register_notifier(struct regulator
*regulator
,
2327 struct notifier_block
*nb
)
2329 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
2332 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
2335 * regulator_unregister_notifier - unregister regulator event notifier
2336 * @regulator: regulator source
2337 * @nb: notifier block
2339 * Unregister regulator event notifier block.
2341 int regulator_unregister_notifier(struct regulator
*regulator
,
2342 struct notifier_block
*nb
)
2344 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
2347 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
2349 /* notify regulator consumers and downstream regulator consumers.
2350 * Note mutex must be held by caller.
2352 static void _notifier_call_chain(struct regulator_dev
*rdev
,
2353 unsigned long event
, void *data
)
2355 /* call rdev chain first */
2356 blocking_notifier_call_chain(&rdev
->notifier
, event
, NULL
);
2360 * regulator_bulk_get - get multiple regulator consumers
2362 * @dev: Device to supply
2363 * @num_consumers: Number of consumers to register
2364 * @consumers: Configuration of consumers; clients are stored here.
2366 * @return 0 on success, an errno on failure.
2368 * This helper function allows drivers to get several regulator
2369 * consumers in one operation. If any of the regulators cannot be
2370 * acquired then any regulators that were allocated will be freed
2371 * before returning to the caller.
2373 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
2374 struct regulator_bulk_data
*consumers
)
2379 for (i
= 0; i
< num_consumers
; i
++)
2380 consumers
[i
].consumer
= NULL
;
2382 for (i
= 0; i
< num_consumers
; i
++) {
2383 consumers
[i
].consumer
= regulator_get(dev
,
2384 consumers
[i
].supply
);
2385 if (IS_ERR(consumers
[i
].consumer
)) {
2386 ret
= PTR_ERR(consumers
[i
].consumer
);
2387 dev_err(dev
, "Failed to get supply '%s': %d\n",
2388 consumers
[i
].supply
, ret
);
2389 consumers
[i
].consumer
= NULL
;
2397 for (i
= 0; i
< num_consumers
&& consumers
[i
].consumer
; i
++)
2398 regulator_put(consumers
[i
].consumer
);
2402 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
2404 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
2406 struct regulator_bulk_data
*bulk
= data
;
2408 bulk
->ret
= regulator_enable(bulk
->consumer
);
2412 * regulator_bulk_enable - enable multiple regulator consumers
2414 * @num_consumers: Number of consumers
2415 * @consumers: Consumer data; clients are stored here.
2416 * @return 0 on success, an errno on failure
2418 * This convenience API allows consumers to enable multiple regulator
2419 * clients in a single API call. If any consumers cannot be enabled
2420 * then any others that were enabled will be disabled again prior to
2423 int regulator_bulk_enable(int num_consumers
,
2424 struct regulator_bulk_data
*consumers
)
2426 LIST_HEAD(async_domain
);
2430 for (i
= 0; i
< num_consumers
; i
++)
2431 async_schedule_domain(regulator_bulk_enable_async
,
2432 &consumers
[i
], &async_domain
);
2434 async_synchronize_full_domain(&async_domain
);
2436 /* If any consumer failed we need to unwind any that succeeded */
2437 for (i
= 0; i
< num_consumers
; i
++) {
2438 if (consumers
[i
].ret
!= 0) {
2439 ret
= consumers
[i
].ret
;
2447 for (i
= 0; i
< num_consumers
; i
++)
2448 if (consumers
[i
].ret
== 0)
2449 regulator_disable(consumers
[i
].consumer
);
2451 pr_err("Failed to enable %s: %d\n",
2452 consumers
[i
].supply
, consumers
[i
].ret
);
2456 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
2459 * regulator_bulk_disable - disable multiple regulator consumers
2461 * @num_consumers: Number of consumers
2462 * @consumers: Consumer data; clients are stored here.
2463 * @return 0 on success, an errno on failure
2465 * This convenience API allows consumers to disable multiple regulator
2466 * clients in a single API call. If any consumers cannot be enabled
2467 * then any others that were disabled will be disabled again prior to
2470 int regulator_bulk_disable(int num_consumers
,
2471 struct regulator_bulk_data
*consumers
)
2476 for (i
= 0; i
< num_consumers
; i
++) {
2477 ret
= regulator_disable(consumers
[i
].consumer
);
2485 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
2486 for (--i
; i
>= 0; --i
)
2487 regulator_enable(consumers
[i
].consumer
);
2491 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
2494 * regulator_bulk_force_disable - force disable multiple regulator consumers
2496 * @num_consumers: Number of consumers
2497 * @consumers: Consumer data; clients are stored here.
2498 * @return 0 on success, an errno on failure
2500 * This convenience API allows consumers to forcibly disable multiple regulator
2501 * clients in a single API call.
2502 * NOTE: This should be used for situations when device damage will
2503 * likely occur if the regulators are not disabled (e.g. over temp).
2504 * Although regulator_force_disable function call for some consumers can
2505 * return error numbers, the function is called for all consumers.
2507 int regulator_bulk_force_disable(int num_consumers
,
2508 struct regulator_bulk_data
*consumers
)
2513 for (i
= 0; i
< num_consumers
; i
++)
2515 regulator_force_disable(consumers
[i
].consumer
);
2517 for (i
= 0; i
< num_consumers
; i
++) {
2518 if (consumers
[i
].ret
!= 0) {
2519 ret
= consumers
[i
].ret
;
2528 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable
);
2531 * regulator_bulk_free - free multiple regulator consumers
2533 * @num_consumers: Number of consumers
2534 * @consumers: Consumer data; clients are stored here.
2536 * This convenience API allows consumers to free multiple regulator
2537 * clients in a single API call.
2539 void regulator_bulk_free(int num_consumers
,
2540 struct regulator_bulk_data
*consumers
)
2544 for (i
= 0; i
< num_consumers
; i
++) {
2545 regulator_put(consumers
[i
].consumer
);
2546 consumers
[i
].consumer
= NULL
;
2549 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
2552 * regulator_notifier_call_chain - call regulator event notifier
2553 * @rdev: regulator source
2554 * @event: notifier block
2555 * @data: callback-specific data.
2557 * Called by regulator drivers to notify clients a regulator event has
2558 * occurred. We also notify regulator clients downstream.
2559 * Note lock must be held by caller.
2561 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
2562 unsigned long event
, void *data
)
2564 _notifier_call_chain(rdev
, event
, data
);
2568 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
2571 * regulator_mode_to_status - convert a regulator mode into a status
2573 * @mode: Mode to convert
2575 * Convert a regulator mode into a status.
2577 int regulator_mode_to_status(unsigned int mode
)
2580 case REGULATOR_MODE_FAST
:
2581 return REGULATOR_STATUS_FAST
;
2582 case REGULATOR_MODE_NORMAL
:
2583 return REGULATOR_STATUS_NORMAL
;
2584 case REGULATOR_MODE_IDLE
:
2585 return REGULATOR_STATUS_IDLE
;
2586 case REGULATOR_STATUS_STANDBY
:
2587 return REGULATOR_STATUS_STANDBY
;
2592 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
2595 * To avoid cluttering sysfs (and memory) with useless state, only
2596 * create attributes that can be meaningfully displayed.
2598 static int add_regulator_attributes(struct regulator_dev
*rdev
)
2600 struct device
*dev
= &rdev
->dev
;
2601 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2604 /* some attributes need specific methods to be displayed */
2605 if ((ops
->get_voltage
&& ops
->get_voltage(rdev
) >= 0) ||
2606 (ops
->get_voltage_sel
&& ops
->get_voltage_sel(rdev
) >= 0)) {
2607 status
= device_create_file(dev
, &dev_attr_microvolts
);
2611 if (ops
->get_current_limit
) {
2612 status
= device_create_file(dev
, &dev_attr_microamps
);
2616 if (ops
->get_mode
) {
2617 status
= device_create_file(dev
, &dev_attr_opmode
);
2621 if (ops
->is_enabled
) {
2622 status
= device_create_file(dev
, &dev_attr_state
);
2626 if (ops
->get_status
) {
2627 status
= device_create_file(dev
, &dev_attr_status
);
2632 /* some attributes are type-specific */
2633 if (rdev
->desc
->type
== REGULATOR_CURRENT
) {
2634 status
= device_create_file(dev
, &dev_attr_requested_microamps
);
2639 /* all the other attributes exist to support constraints;
2640 * don't show them if there are no constraints, or if the
2641 * relevant supporting methods are missing.
2643 if (!rdev
->constraints
)
2646 /* constraints need specific supporting methods */
2647 if (ops
->set_voltage
|| ops
->set_voltage_sel
) {
2648 status
= device_create_file(dev
, &dev_attr_min_microvolts
);
2651 status
= device_create_file(dev
, &dev_attr_max_microvolts
);
2655 if (ops
->set_current_limit
) {
2656 status
= device_create_file(dev
, &dev_attr_min_microamps
);
2659 status
= device_create_file(dev
, &dev_attr_max_microamps
);
2664 /* suspend mode constraints need multiple supporting methods */
2665 if (!(ops
->set_suspend_enable
&& ops
->set_suspend_disable
))
2668 status
= device_create_file(dev
, &dev_attr_suspend_standby_state
);
2671 status
= device_create_file(dev
, &dev_attr_suspend_mem_state
);
2674 status
= device_create_file(dev
, &dev_attr_suspend_disk_state
);
2678 if (ops
->set_suspend_voltage
) {
2679 status
= device_create_file(dev
,
2680 &dev_attr_suspend_standby_microvolts
);
2683 status
= device_create_file(dev
,
2684 &dev_attr_suspend_mem_microvolts
);
2687 status
= device_create_file(dev
,
2688 &dev_attr_suspend_disk_microvolts
);
2693 if (ops
->set_suspend_mode
) {
2694 status
= device_create_file(dev
,
2695 &dev_attr_suspend_standby_mode
);
2698 status
= device_create_file(dev
,
2699 &dev_attr_suspend_mem_mode
);
2702 status
= device_create_file(dev
,
2703 &dev_attr_suspend_disk_mode
);
2711 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
2713 #ifdef CONFIG_DEBUG_FS
2714 rdev
->debugfs
= debugfs_create_dir(rdev_get_name(rdev
), debugfs_root
);
2715 if (IS_ERR(rdev
->debugfs
) || !rdev
->debugfs
) {
2716 rdev_warn(rdev
, "Failed to create debugfs directory\n");
2717 rdev
->debugfs
= NULL
;
2721 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
2723 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
2729 * regulator_register - register regulator
2730 * @regulator_desc: regulator to register
2731 * @dev: struct device for the regulator
2732 * @init_data: platform provided init data, passed through by driver
2733 * @driver_data: private regulator data
2734 * @of_node: OpenFirmware node to parse for device tree bindings (may be
2737 * Called by regulator drivers to register a regulator.
2738 * Returns 0 on success.
2740 struct regulator_dev
*regulator_register(struct regulator_desc
*regulator_desc
,
2741 struct device
*dev
, const struct regulator_init_data
*init_data
,
2742 void *driver_data
, struct device_node
*of_node
)
2744 const struct regulation_constraints
*constraints
= NULL
;
2745 static atomic_t regulator_no
= ATOMIC_INIT(0);
2746 struct regulator_dev
*rdev
;
2748 const char *supply
= NULL
;
2750 if (regulator_desc
== NULL
)
2751 return ERR_PTR(-EINVAL
);
2753 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
2754 return ERR_PTR(-EINVAL
);
2756 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
2757 regulator_desc
->type
!= REGULATOR_CURRENT
)
2758 return ERR_PTR(-EINVAL
);
2760 /* Only one of each should be implemented */
2761 WARN_ON(regulator_desc
->ops
->get_voltage
&&
2762 regulator_desc
->ops
->get_voltage_sel
);
2763 WARN_ON(regulator_desc
->ops
->set_voltage
&&
2764 regulator_desc
->ops
->set_voltage_sel
);
2766 /* If we're using selectors we must implement list_voltage. */
2767 if (regulator_desc
->ops
->get_voltage_sel
&&
2768 !regulator_desc
->ops
->list_voltage
) {
2769 return ERR_PTR(-EINVAL
);
2771 if (regulator_desc
->ops
->set_voltage_sel
&&
2772 !regulator_desc
->ops
->list_voltage
) {
2773 return ERR_PTR(-EINVAL
);
2776 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
2778 return ERR_PTR(-ENOMEM
);
2780 mutex_lock(®ulator_list_mutex
);
2782 mutex_init(&rdev
->mutex
);
2783 rdev
->reg_data
= driver_data
;
2784 rdev
->owner
= regulator_desc
->owner
;
2785 rdev
->desc
= regulator_desc
;
2786 INIT_LIST_HEAD(&rdev
->consumer_list
);
2787 INIT_LIST_HEAD(&rdev
->list
);
2788 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
2789 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
2791 /* preform any regulator specific init */
2792 if (init_data
&& init_data
->regulator_init
) {
2793 ret
= init_data
->regulator_init(rdev
->reg_data
);
2798 /* register with sysfs */
2799 rdev
->dev
.class = ®ulator_class
;
2800 rdev
->dev
.of_node
= of_node
;
2801 rdev
->dev
.parent
= dev
;
2802 dev_set_name(&rdev
->dev
, "regulator.%d",
2803 atomic_inc_return(®ulator_no
) - 1);
2804 ret
= device_register(&rdev
->dev
);
2806 put_device(&rdev
->dev
);
2810 dev_set_drvdata(&rdev
->dev
, rdev
);
2812 /* set regulator constraints */
2814 constraints
= &init_data
->constraints
;
2816 ret
= set_machine_constraints(rdev
, constraints
);
2820 /* add attributes supported by this regulator */
2821 ret
= add_regulator_attributes(rdev
);
2825 if (init_data
&& init_data
->supply_regulator
)
2826 supply
= init_data
->supply_regulator
;
2827 else if (regulator_desc
->supply_name
)
2828 supply
= regulator_desc
->supply_name
;
2831 struct regulator_dev
*r
;
2833 r
= regulator_dev_lookup(dev
, supply
);
2836 dev_err(dev
, "Failed to find supply %s\n", supply
);
2841 ret
= set_supply(rdev
, r
);
2845 /* Enable supply if rail is enabled */
2846 if (rdev
->desc
->ops
->is_enabled
&&
2847 rdev
->desc
->ops
->is_enabled(rdev
)) {
2848 ret
= regulator_enable(rdev
->supply
);
2854 /* add consumers devices */
2856 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
2857 ret
= set_consumer_device_supply(rdev
,
2858 init_data
->consumer_supplies
[i
].dev
,
2859 init_data
->consumer_supplies
[i
].dev_name
,
2860 init_data
->consumer_supplies
[i
].supply
);
2862 dev_err(dev
, "Failed to set supply %s\n",
2863 init_data
->consumer_supplies
[i
].supply
);
2864 goto unset_supplies
;
2869 list_add(&rdev
->list
, ®ulator_list
);
2871 rdev_init_debugfs(rdev
);
2873 mutex_unlock(®ulator_list_mutex
);
2877 unset_regulator_supplies(rdev
);
2881 regulator_put(rdev
->supply
);
2882 kfree(rdev
->constraints
);
2883 device_unregister(&rdev
->dev
);
2884 /* device core frees rdev */
2885 rdev
= ERR_PTR(ret
);
2890 rdev
= ERR_PTR(ret
);
2893 EXPORT_SYMBOL_GPL(regulator_register
);
2896 * regulator_unregister - unregister regulator
2897 * @rdev: regulator to unregister
2899 * Called by regulator drivers to unregister a regulator.
2901 void regulator_unregister(struct regulator_dev
*rdev
)
2906 mutex_lock(®ulator_list_mutex
);
2907 #ifdef CONFIG_DEBUG_FS
2908 debugfs_remove_recursive(rdev
->debugfs
);
2910 flush_work_sync(&rdev
->disable_work
.work
);
2911 WARN_ON(rdev
->open_count
);
2912 unset_regulator_supplies(rdev
);
2913 list_del(&rdev
->list
);
2915 regulator_put(rdev
->supply
);
2916 kfree(rdev
->constraints
);
2917 device_unregister(&rdev
->dev
);
2918 mutex_unlock(®ulator_list_mutex
);
2920 EXPORT_SYMBOL_GPL(regulator_unregister
);
2923 * regulator_suspend_prepare - prepare regulators for system wide suspend
2924 * @state: system suspend state
2926 * Configure each regulator with it's suspend operating parameters for state.
2927 * This will usually be called by machine suspend code prior to supending.
2929 int regulator_suspend_prepare(suspend_state_t state
)
2931 struct regulator_dev
*rdev
;
2934 /* ON is handled by regulator active state */
2935 if (state
== PM_SUSPEND_ON
)
2938 mutex_lock(®ulator_list_mutex
);
2939 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2941 mutex_lock(&rdev
->mutex
);
2942 ret
= suspend_prepare(rdev
, state
);
2943 mutex_unlock(&rdev
->mutex
);
2946 rdev_err(rdev
, "failed to prepare\n");
2951 mutex_unlock(®ulator_list_mutex
);
2954 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
2957 * regulator_suspend_finish - resume regulators from system wide suspend
2959 * Turn on regulators that might be turned off by regulator_suspend_prepare
2960 * and that should be turned on according to the regulators properties.
2962 int regulator_suspend_finish(void)
2964 struct regulator_dev
*rdev
;
2967 mutex_lock(®ulator_list_mutex
);
2968 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2969 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2971 mutex_lock(&rdev
->mutex
);
2972 if ((rdev
->use_count
> 0 || rdev
->constraints
->always_on
) &&
2974 error
= ops
->enable(rdev
);
2978 if (!has_full_constraints
)
2982 if (ops
->is_enabled
&& !ops
->is_enabled(rdev
))
2985 error
= ops
->disable(rdev
);
2990 mutex_unlock(&rdev
->mutex
);
2992 mutex_unlock(®ulator_list_mutex
);
2995 EXPORT_SYMBOL_GPL(regulator_suspend_finish
);
2998 * regulator_has_full_constraints - the system has fully specified constraints
3000 * Calling this function will cause the regulator API to disable all
3001 * regulators which have a zero use count and don't have an always_on
3002 * constraint in a late_initcall.
3004 * The intention is that this will become the default behaviour in a
3005 * future kernel release so users are encouraged to use this facility
3008 void regulator_has_full_constraints(void)
3010 has_full_constraints
= 1;
3012 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
3015 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3017 * Calling this function will cause the regulator API to provide a
3018 * dummy regulator to consumers if no physical regulator is found,
3019 * allowing most consumers to proceed as though a regulator were
3020 * configured. This allows systems such as those with software
3021 * controllable regulators for the CPU core only to be brought up more
3024 void regulator_use_dummy_regulator(void)
3026 board_wants_dummy_regulator
= true;
3028 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator
);
3031 * rdev_get_drvdata - get rdev regulator driver data
3034 * Get rdev regulator driver private data. This call can be used in the
3035 * regulator driver context.
3037 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
3039 return rdev
->reg_data
;
3041 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
3044 * regulator_get_drvdata - get regulator driver data
3045 * @regulator: regulator
3047 * Get regulator driver private data. This call can be used in the consumer
3048 * driver context when non API regulator specific functions need to be called.
3050 void *regulator_get_drvdata(struct regulator
*regulator
)
3052 return regulator
->rdev
->reg_data
;
3054 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
3057 * regulator_set_drvdata - set regulator driver data
3058 * @regulator: regulator
3061 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
3063 regulator
->rdev
->reg_data
= data
;
3065 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
3068 * regulator_get_id - get regulator ID
3071 int rdev_get_id(struct regulator_dev
*rdev
)
3073 return rdev
->desc
->id
;
3075 EXPORT_SYMBOL_GPL(rdev_get_id
);
3077 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
3081 EXPORT_SYMBOL_GPL(rdev_get_dev
);
3083 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
3085 return reg_init_data
->driver_data
;
3087 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
3089 #ifdef CONFIG_DEBUG_FS
3090 static ssize_t
supply_map_read_file(struct file
*file
, char __user
*user_buf
,
3091 size_t count
, loff_t
*ppos
)
3093 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
3094 ssize_t len
, ret
= 0;
3095 struct regulator_map
*map
;
3100 list_for_each_entry(map
, ®ulator_map_list
, list
) {
3101 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
3103 rdev_get_name(map
->regulator
), map
->dev_name
,
3107 if (ret
> PAGE_SIZE
) {
3113 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
3120 static const struct file_operations supply_map_fops
= {
3121 .read
= supply_map_read_file
,
3122 .llseek
= default_llseek
,
3126 static int __init
regulator_init(void)
3130 ret
= class_register(®ulator_class
);
3132 #ifdef CONFIG_DEBUG_FS
3133 debugfs_root
= debugfs_create_dir("regulator", NULL
);
3134 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
3135 pr_warn("regulator: Failed to create debugfs directory\n");
3136 debugfs_root
= NULL
;
3139 if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root
,
3140 NULL
, &supply_map_fops
)))
3141 pr_warn("regulator: Failed to create supplies debugfs\n");
3144 regulator_dummy_init();
3149 /* init early to allow our consumers to complete system booting */
3150 core_initcall(regulator_init
);
3152 static int __init
regulator_init_complete(void)
3154 struct regulator_dev
*rdev
;
3155 struct regulator_ops
*ops
;
3156 struct regulation_constraints
*c
;
3159 mutex_lock(®ulator_list_mutex
);
3161 /* If we have a full configuration then disable any regulators
3162 * which are not in use or always_on. This will become the
3163 * default behaviour in the future.
3165 list_for_each_entry(rdev
, ®ulator_list
, list
) {
3166 ops
= rdev
->desc
->ops
;
3167 c
= rdev
->constraints
;
3169 if (!ops
->disable
|| (c
&& c
->always_on
))
3172 mutex_lock(&rdev
->mutex
);
3174 if (rdev
->use_count
)
3177 /* If we can't read the status assume it's on. */
3178 if (ops
->is_enabled
)
3179 enabled
= ops
->is_enabled(rdev
);
3186 if (has_full_constraints
) {
3187 /* We log since this may kill the system if it
3189 rdev_info(rdev
, "disabling\n");
3190 ret
= ops
->disable(rdev
);
3192 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
3195 /* The intention is that in future we will
3196 * assume that full constraints are provided
3197 * so warn even if we aren't going to do
3200 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
3204 mutex_unlock(&rdev
->mutex
);
3207 mutex_unlock(®ulator_list_mutex
);
3211 late_initcall(regulator_init_complete
);