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>
28 #include <linux/regulator/consumer.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/module.h>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/regulator.h>
38 #define rdev_crit(rdev, fmt, ...) \
39 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_err(rdev, fmt, ...) \
41 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_warn(rdev, fmt, ...) \
43 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_info(rdev, fmt, ...) \
45 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_dbg(rdev, fmt, ...) \
47 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 static DEFINE_MUTEX(regulator_list_mutex
);
50 static LIST_HEAD(regulator_list
);
51 static LIST_HEAD(regulator_map_list
);
52 static bool has_full_constraints
;
53 static bool board_wants_dummy_regulator
;
55 #ifdef CONFIG_DEBUG_FS
56 static struct dentry
*debugfs_root
;
60 * struct regulator_map
62 * Used to provide symbolic supply names to devices.
64 struct regulator_map
{
65 struct list_head list
;
66 const char *dev_name
; /* The dev_name() for the consumer */
68 struct regulator_dev
*regulator
;
74 * One for each consumer device.
78 struct list_head list
;
83 struct device_attribute dev_attr
;
84 struct regulator_dev
*rdev
;
85 #ifdef CONFIG_DEBUG_FS
86 struct dentry
*debugfs
;
90 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
91 static int _regulator_disable(struct regulator_dev
*rdev
);
92 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
93 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
94 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
95 static void _notifier_call_chain(struct regulator_dev
*rdev
,
96 unsigned long event
, void *data
);
97 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
98 int min_uV
, int max_uV
);
99 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
101 const char *supply_name
);
103 static const char *rdev_get_name(struct regulator_dev
*rdev
)
105 if (rdev
->constraints
&& rdev
->constraints
->name
)
106 return rdev
->constraints
->name
;
107 else if (rdev
->desc
->name
)
108 return rdev
->desc
->name
;
113 /* gets the regulator for a given consumer device */
114 static struct regulator
*get_device_regulator(struct device
*dev
)
116 struct regulator
*regulator
= NULL
;
117 struct regulator_dev
*rdev
;
119 mutex_lock(®ulator_list_mutex
);
120 list_for_each_entry(rdev
, ®ulator_list
, list
) {
121 mutex_lock(&rdev
->mutex
);
122 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
123 if (regulator
->dev
== dev
) {
124 mutex_unlock(&rdev
->mutex
);
125 mutex_unlock(®ulator_list_mutex
);
129 mutex_unlock(&rdev
->mutex
);
131 mutex_unlock(®ulator_list_mutex
);
135 /* Platform voltage constraint check */
136 static int regulator_check_voltage(struct regulator_dev
*rdev
,
137 int *min_uV
, int *max_uV
)
139 BUG_ON(*min_uV
> *max_uV
);
141 if (!rdev
->constraints
) {
142 rdev_err(rdev
, "no constraints\n");
145 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
146 rdev_err(rdev
, "operation not allowed\n");
150 if (*max_uV
> rdev
->constraints
->max_uV
)
151 *max_uV
= rdev
->constraints
->max_uV
;
152 if (*min_uV
< rdev
->constraints
->min_uV
)
153 *min_uV
= rdev
->constraints
->min_uV
;
155 if (*min_uV
> *max_uV
) {
156 rdev_err(rdev
, "unsupportable voltage range: %d-%duV\n",
164 /* Make sure we select a voltage that suits the needs of all
165 * regulator consumers
167 static int regulator_check_consumers(struct regulator_dev
*rdev
,
168 int *min_uV
, int *max_uV
)
170 struct regulator
*regulator
;
172 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
174 * Assume consumers that didn't say anything are OK
175 * with anything in the constraint range.
177 if (!regulator
->min_uV
&& !regulator
->max_uV
)
180 if (*max_uV
> regulator
->max_uV
)
181 *max_uV
= regulator
->max_uV
;
182 if (*min_uV
< regulator
->min_uV
)
183 *min_uV
= regulator
->min_uV
;
186 if (*min_uV
> *max_uV
)
192 /* current constraint check */
193 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
194 int *min_uA
, int *max_uA
)
196 BUG_ON(*min_uA
> *max_uA
);
198 if (!rdev
->constraints
) {
199 rdev_err(rdev
, "no constraints\n");
202 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
203 rdev_err(rdev
, "operation not allowed\n");
207 if (*max_uA
> rdev
->constraints
->max_uA
)
208 *max_uA
= rdev
->constraints
->max_uA
;
209 if (*min_uA
< rdev
->constraints
->min_uA
)
210 *min_uA
= rdev
->constraints
->min_uA
;
212 if (*min_uA
> *max_uA
) {
213 rdev_err(rdev
, "unsupportable current range: %d-%duA\n",
221 /* operating mode constraint check */
222 static int regulator_mode_constrain(struct regulator_dev
*rdev
, int *mode
)
225 case REGULATOR_MODE_FAST
:
226 case REGULATOR_MODE_NORMAL
:
227 case REGULATOR_MODE_IDLE
:
228 case REGULATOR_MODE_STANDBY
:
231 rdev_err(rdev
, "invalid mode %x specified\n", *mode
);
235 if (!rdev
->constraints
) {
236 rdev_err(rdev
, "no constraints\n");
239 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
240 rdev_err(rdev
, "operation not allowed\n");
244 /* The modes are bitmasks, the most power hungry modes having
245 * the lowest values. If the requested mode isn't supported
246 * try higher modes. */
248 if (rdev
->constraints
->valid_modes_mask
& *mode
)
256 /* dynamic regulator mode switching constraint check */
257 static int regulator_check_drms(struct regulator_dev
*rdev
)
259 if (!rdev
->constraints
) {
260 rdev_err(rdev
, "no constraints\n");
263 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
264 rdev_err(rdev
, "operation not allowed\n");
270 static ssize_t
device_requested_uA_show(struct device
*dev
,
271 struct device_attribute
*attr
, char *buf
)
273 struct regulator
*regulator
;
275 regulator
= get_device_regulator(dev
);
276 if (regulator
== NULL
)
279 return sprintf(buf
, "%d\n", regulator
->uA_load
);
282 static ssize_t
regulator_uV_show(struct device
*dev
,
283 struct device_attribute
*attr
, char *buf
)
285 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
288 mutex_lock(&rdev
->mutex
);
289 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
290 mutex_unlock(&rdev
->mutex
);
294 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
296 static ssize_t
regulator_uA_show(struct device
*dev
,
297 struct device_attribute
*attr
, char *buf
)
299 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
301 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
303 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
305 static ssize_t
regulator_name_show(struct device
*dev
,
306 struct device_attribute
*attr
, char *buf
)
308 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
310 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
313 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
316 case REGULATOR_MODE_FAST
:
317 return sprintf(buf
, "fast\n");
318 case REGULATOR_MODE_NORMAL
:
319 return sprintf(buf
, "normal\n");
320 case REGULATOR_MODE_IDLE
:
321 return sprintf(buf
, "idle\n");
322 case REGULATOR_MODE_STANDBY
:
323 return sprintf(buf
, "standby\n");
325 return sprintf(buf
, "unknown\n");
328 static ssize_t
regulator_opmode_show(struct device
*dev
,
329 struct device_attribute
*attr
, char *buf
)
331 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
333 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
335 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
337 static ssize_t
regulator_print_state(char *buf
, int state
)
340 return sprintf(buf
, "enabled\n");
342 return sprintf(buf
, "disabled\n");
344 return sprintf(buf
, "unknown\n");
347 static ssize_t
regulator_state_show(struct device
*dev
,
348 struct device_attribute
*attr
, char *buf
)
350 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
353 mutex_lock(&rdev
->mutex
);
354 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
355 mutex_unlock(&rdev
->mutex
);
359 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
361 static ssize_t
regulator_status_show(struct device
*dev
,
362 struct device_attribute
*attr
, char *buf
)
364 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
368 status
= rdev
->desc
->ops
->get_status(rdev
);
373 case REGULATOR_STATUS_OFF
:
376 case REGULATOR_STATUS_ON
:
379 case REGULATOR_STATUS_ERROR
:
382 case REGULATOR_STATUS_FAST
:
385 case REGULATOR_STATUS_NORMAL
:
388 case REGULATOR_STATUS_IDLE
:
391 case REGULATOR_STATUS_STANDBY
:
398 return sprintf(buf
, "%s\n", label
);
400 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
402 static ssize_t
regulator_min_uA_show(struct device
*dev
,
403 struct device_attribute
*attr
, char *buf
)
405 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
407 if (!rdev
->constraints
)
408 return sprintf(buf
, "constraint not defined\n");
410 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
412 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
414 static ssize_t
regulator_max_uA_show(struct device
*dev
,
415 struct device_attribute
*attr
, char *buf
)
417 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
419 if (!rdev
->constraints
)
420 return sprintf(buf
, "constraint not defined\n");
422 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
424 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
426 static ssize_t
regulator_min_uV_show(struct device
*dev
,
427 struct device_attribute
*attr
, char *buf
)
429 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
431 if (!rdev
->constraints
)
432 return sprintf(buf
, "constraint not defined\n");
434 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
436 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
438 static ssize_t
regulator_max_uV_show(struct device
*dev
,
439 struct device_attribute
*attr
, char *buf
)
441 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
443 if (!rdev
->constraints
)
444 return sprintf(buf
, "constraint not defined\n");
446 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
448 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
450 static ssize_t
regulator_total_uA_show(struct device
*dev
,
451 struct device_attribute
*attr
, char *buf
)
453 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
454 struct regulator
*regulator
;
457 mutex_lock(&rdev
->mutex
);
458 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
459 uA
+= regulator
->uA_load
;
460 mutex_unlock(&rdev
->mutex
);
461 return sprintf(buf
, "%d\n", uA
);
463 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
465 static ssize_t
regulator_num_users_show(struct device
*dev
,
466 struct device_attribute
*attr
, char *buf
)
468 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
469 return sprintf(buf
, "%d\n", rdev
->use_count
);
472 static ssize_t
regulator_type_show(struct device
*dev
,
473 struct device_attribute
*attr
, char *buf
)
475 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
477 switch (rdev
->desc
->type
) {
478 case REGULATOR_VOLTAGE
:
479 return sprintf(buf
, "voltage\n");
480 case REGULATOR_CURRENT
:
481 return sprintf(buf
, "current\n");
483 return sprintf(buf
, "unknown\n");
486 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
487 struct device_attribute
*attr
, char *buf
)
489 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
491 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
493 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
494 regulator_suspend_mem_uV_show
, NULL
);
496 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
497 struct device_attribute
*attr
, char *buf
)
499 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
501 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
503 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
504 regulator_suspend_disk_uV_show
, NULL
);
506 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
507 struct device_attribute
*attr
, char *buf
)
509 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
511 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
513 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
514 regulator_suspend_standby_uV_show
, NULL
);
516 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
517 struct device_attribute
*attr
, char *buf
)
519 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
521 return regulator_print_opmode(buf
,
522 rdev
->constraints
->state_mem
.mode
);
524 static DEVICE_ATTR(suspend_mem_mode
, 0444,
525 regulator_suspend_mem_mode_show
, NULL
);
527 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
528 struct device_attribute
*attr
, char *buf
)
530 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
532 return regulator_print_opmode(buf
,
533 rdev
->constraints
->state_disk
.mode
);
535 static DEVICE_ATTR(suspend_disk_mode
, 0444,
536 regulator_suspend_disk_mode_show
, NULL
);
538 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
539 struct device_attribute
*attr
, char *buf
)
541 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
543 return regulator_print_opmode(buf
,
544 rdev
->constraints
->state_standby
.mode
);
546 static DEVICE_ATTR(suspend_standby_mode
, 0444,
547 regulator_suspend_standby_mode_show
, NULL
);
549 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
550 struct device_attribute
*attr
, char *buf
)
552 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
554 return regulator_print_state(buf
,
555 rdev
->constraints
->state_mem
.enabled
);
557 static DEVICE_ATTR(suspend_mem_state
, 0444,
558 regulator_suspend_mem_state_show
, NULL
);
560 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
561 struct device_attribute
*attr
, char *buf
)
563 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
565 return regulator_print_state(buf
,
566 rdev
->constraints
->state_disk
.enabled
);
568 static DEVICE_ATTR(suspend_disk_state
, 0444,
569 regulator_suspend_disk_state_show
, NULL
);
571 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
572 struct device_attribute
*attr
, char *buf
)
574 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
576 return regulator_print_state(buf
,
577 rdev
->constraints
->state_standby
.enabled
);
579 static DEVICE_ATTR(suspend_standby_state
, 0444,
580 regulator_suspend_standby_state_show
, NULL
);
584 * These are the only attributes are present for all regulators.
585 * Other attributes are a function of regulator functionality.
587 static struct device_attribute regulator_dev_attrs
[] = {
588 __ATTR(name
, 0444, regulator_name_show
, NULL
),
589 __ATTR(num_users
, 0444, regulator_num_users_show
, NULL
),
590 __ATTR(type
, 0444, regulator_type_show
, NULL
),
594 static void regulator_dev_release(struct device
*dev
)
596 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
600 static struct class regulator_class
= {
602 .dev_release
= regulator_dev_release
,
603 .dev_attrs
= regulator_dev_attrs
,
606 /* Calculate the new optimum regulator operating mode based on the new total
607 * consumer load. All locks held by caller */
608 static void drms_uA_update(struct regulator_dev
*rdev
)
610 struct regulator
*sibling
;
611 int current_uA
= 0, output_uV
, input_uV
, err
;
614 err
= regulator_check_drms(rdev
);
615 if (err
< 0 || !rdev
->desc
->ops
->get_optimum_mode
||
616 (!rdev
->desc
->ops
->get_voltage
&&
617 !rdev
->desc
->ops
->get_voltage_sel
) ||
618 !rdev
->desc
->ops
->set_mode
)
621 /* get output voltage */
622 output_uV
= _regulator_get_voltage(rdev
);
626 /* get input voltage */
629 input_uV
= _regulator_get_voltage(rdev
);
631 input_uV
= rdev
->constraints
->input_uV
;
635 /* calc total requested load */
636 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
637 current_uA
+= sibling
->uA_load
;
639 /* now get the optimum mode for our new total regulator load */
640 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
641 output_uV
, current_uA
);
643 /* check the new mode is allowed */
644 err
= regulator_mode_constrain(rdev
, &mode
);
646 rdev
->desc
->ops
->set_mode(rdev
, mode
);
649 static int suspend_set_state(struct regulator_dev
*rdev
,
650 struct regulator_state
*rstate
)
655 can_set_state
= rdev
->desc
->ops
->set_suspend_enable
&&
656 rdev
->desc
->ops
->set_suspend_disable
;
658 /* If we have no suspend mode configration don't set anything;
659 * only warn if the driver actually makes the suspend mode
662 if (!rstate
->enabled
&& !rstate
->disabled
) {
664 rdev_warn(rdev
, "No configuration\n");
668 if (rstate
->enabled
&& rstate
->disabled
) {
669 rdev_err(rdev
, "invalid configuration\n");
673 if (!can_set_state
) {
674 rdev_err(rdev
, "no way to set suspend state\n");
679 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
681 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
683 rdev_err(rdev
, "failed to enabled/disable\n");
687 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
688 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
690 rdev_err(rdev
, "failed to set voltage\n");
695 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
696 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
698 rdev_err(rdev
, "failed to set mode\n");
705 /* locks held by caller */
706 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
708 if (!rdev
->constraints
)
712 case PM_SUSPEND_STANDBY
:
713 return suspend_set_state(rdev
,
714 &rdev
->constraints
->state_standby
);
716 return suspend_set_state(rdev
,
717 &rdev
->constraints
->state_mem
);
719 return suspend_set_state(rdev
,
720 &rdev
->constraints
->state_disk
);
726 static void print_constraints(struct regulator_dev
*rdev
)
728 struct regulation_constraints
*constraints
= rdev
->constraints
;
733 if (constraints
->min_uV
&& constraints
->max_uV
) {
734 if (constraints
->min_uV
== constraints
->max_uV
)
735 count
+= sprintf(buf
+ count
, "%d mV ",
736 constraints
->min_uV
/ 1000);
738 count
+= sprintf(buf
+ count
, "%d <--> %d mV ",
739 constraints
->min_uV
/ 1000,
740 constraints
->max_uV
/ 1000);
743 if (!constraints
->min_uV
||
744 constraints
->min_uV
!= constraints
->max_uV
) {
745 ret
= _regulator_get_voltage(rdev
);
747 count
+= sprintf(buf
+ count
, "at %d mV ", ret
/ 1000);
750 if (constraints
->uV_offset
)
751 count
+= sprintf(buf
, "%dmV offset ",
752 constraints
->uV_offset
/ 1000);
754 if (constraints
->min_uA
&& constraints
->max_uA
) {
755 if (constraints
->min_uA
== constraints
->max_uA
)
756 count
+= sprintf(buf
+ count
, "%d mA ",
757 constraints
->min_uA
/ 1000);
759 count
+= sprintf(buf
+ count
, "%d <--> %d mA ",
760 constraints
->min_uA
/ 1000,
761 constraints
->max_uA
/ 1000);
764 if (!constraints
->min_uA
||
765 constraints
->min_uA
!= constraints
->max_uA
) {
766 ret
= _regulator_get_current_limit(rdev
);
768 count
+= sprintf(buf
+ count
, "at %d mA ", ret
/ 1000);
771 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
772 count
+= sprintf(buf
+ count
, "fast ");
773 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
774 count
+= sprintf(buf
+ count
, "normal ");
775 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
776 count
+= sprintf(buf
+ count
, "idle ");
777 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
778 count
+= sprintf(buf
+ count
, "standby");
780 rdev_info(rdev
, "%s\n", buf
);
783 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
784 struct regulation_constraints
*constraints
)
786 struct regulator_ops
*ops
= rdev
->desc
->ops
;
789 /* do we need to apply the constraint voltage */
790 if (rdev
->constraints
->apply_uV
&&
791 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
) {
792 ret
= _regulator_do_set_voltage(rdev
,
793 rdev
->constraints
->min_uV
,
794 rdev
->constraints
->max_uV
);
796 rdev_err(rdev
, "failed to apply %duV constraint\n",
797 rdev
->constraints
->min_uV
);
802 /* constrain machine-level voltage specs to fit
803 * the actual range supported by this regulator.
805 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
806 int count
= rdev
->desc
->n_voltages
;
808 int min_uV
= INT_MAX
;
809 int max_uV
= INT_MIN
;
810 int cmin
= constraints
->min_uV
;
811 int cmax
= constraints
->max_uV
;
813 /* it's safe to autoconfigure fixed-voltage supplies
814 and the constraints are used by list_voltage. */
815 if (count
== 1 && !cmin
) {
818 constraints
->min_uV
= cmin
;
819 constraints
->max_uV
= cmax
;
822 /* voltage constraints are optional */
823 if ((cmin
== 0) && (cmax
== 0))
826 /* else require explicit machine-level constraints */
827 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
828 rdev_err(rdev
, "invalid voltage constraints\n");
832 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
833 for (i
= 0; i
< count
; i
++) {
836 value
= ops
->list_voltage(rdev
, i
);
840 /* maybe adjust [min_uV..max_uV] */
841 if (value
>= cmin
&& value
< min_uV
)
843 if (value
<= cmax
&& value
> max_uV
)
847 /* final: [min_uV..max_uV] valid iff constraints valid */
848 if (max_uV
< min_uV
) {
849 rdev_err(rdev
, "unsupportable voltage constraints\n");
853 /* use regulator's subset of machine constraints */
854 if (constraints
->min_uV
< min_uV
) {
855 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
856 constraints
->min_uV
, min_uV
);
857 constraints
->min_uV
= min_uV
;
859 if (constraints
->max_uV
> max_uV
) {
860 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
861 constraints
->max_uV
, max_uV
);
862 constraints
->max_uV
= max_uV
;
870 * set_machine_constraints - sets regulator constraints
871 * @rdev: regulator source
872 * @constraints: constraints to apply
874 * Allows platform initialisation code to define and constrain
875 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
876 * Constraints *must* be set by platform code in order for some
877 * regulator operations to proceed i.e. set_voltage, set_current_limit,
880 static int set_machine_constraints(struct regulator_dev
*rdev
,
881 const struct regulation_constraints
*constraints
)
884 struct regulator_ops
*ops
= rdev
->desc
->ops
;
886 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
888 if (!rdev
->constraints
)
891 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
895 /* do we need to setup our suspend state */
896 if (constraints
->initial_state
) {
897 ret
= suspend_prepare(rdev
, rdev
->constraints
->initial_state
);
899 rdev_err(rdev
, "failed to set suspend state\n");
904 if (constraints
->initial_mode
) {
905 if (!ops
->set_mode
) {
906 rdev_err(rdev
, "no set_mode operation\n");
911 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
913 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
918 /* If the constraints say the regulator should be on at this point
919 * and we have control then make sure it is enabled.
921 if ((rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) &&
923 ret
= ops
->enable(rdev
);
925 rdev_err(rdev
, "failed to enable\n");
930 print_constraints(rdev
);
933 kfree(rdev
->constraints
);
934 rdev
->constraints
= NULL
;
939 * set_supply - set regulator supply regulator
940 * @rdev: regulator name
941 * @supply_rdev: supply regulator name
943 * Called by platform initialisation code to set the supply regulator for this
944 * regulator. This ensures that a regulators supply will also be enabled by the
945 * core if it's child is enabled.
947 static int set_supply(struct regulator_dev
*rdev
,
948 struct regulator_dev
*supply_rdev
)
952 rdev_info(rdev
, "supplied by %s\n", rdev_get_name(supply_rdev
));
954 rdev
->supply
= create_regulator(supply_rdev
, &rdev
->dev
, "SUPPLY");
955 if (IS_ERR(rdev
->supply
)) {
956 err
= PTR_ERR(rdev
->supply
);
965 * set_consumer_device_supply - Bind a regulator to a symbolic supply
966 * @rdev: regulator source
967 * @consumer_dev: device the supply applies to
968 * @consumer_dev_name: dev_name() string for device supply applies to
969 * @supply: symbolic name for supply
971 * Allows platform initialisation code to map physical regulator
972 * sources to symbolic names for supplies for use by devices. Devices
973 * should use these symbolic names to request regulators, avoiding the
974 * need to provide board-specific regulator names as platform data.
976 * Only one of consumer_dev and consumer_dev_name may be specified.
978 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
979 struct device
*consumer_dev
, const char *consumer_dev_name
,
982 struct regulator_map
*node
;
985 if (consumer_dev
&& consumer_dev_name
)
988 if (!consumer_dev_name
&& consumer_dev
)
989 consumer_dev_name
= dev_name(consumer_dev
);
994 if (consumer_dev_name
!= NULL
)
999 list_for_each_entry(node
, ®ulator_map_list
, list
) {
1000 if (node
->dev_name
&& consumer_dev_name
) {
1001 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
1003 } else if (node
->dev_name
|| consumer_dev_name
) {
1007 if (strcmp(node
->supply
, supply
) != 0)
1010 dev_dbg(consumer_dev
, "%s/%s is '%s' supply; fail %s/%s\n",
1011 dev_name(&node
->regulator
->dev
),
1012 node
->regulator
->desc
->name
,
1014 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
1018 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1022 node
->regulator
= rdev
;
1023 node
->supply
= supply
;
1026 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
1027 if (node
->dev_name
== NULL
) {
1033 list_add(&node
->list
, ®ulator_map_list
);
1037 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1039 struct regulator_map
*node
, *n
;
1041 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1042 if (rdev
== node
->regulator
) {
1043 list_del(&node
->list
);
1044 kfree(node
->dev_name
);
1050 #define REG_STR_SIZE 64
1052 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1054 const char *supply_name
)
1056 struct regulator
*regulator
;
1057 char buf
[REG_STR_SIZE
];
1060 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1061 if (regulator
== NULL
)
1064 mutex_lock(&rdev
->mutex
);
1065 regulator
->rdev
= rdev
;
1066 list_add(®ulator
->list
, &rdev
->consumer_list
);
1069 /* create a 'requested_microamps_name' sysfs entry */
1070 size
= scnprintf(buf
, REG_STR_SIZE
,
1071 "microamps_requested_%s-%s",
1072 dev_name(dev
), supply_name
);
1073 if (size
>= REG_STR_SIZE
)
1076 regulator
->dev
= dev
;
1077 sysfs_attr_init(®ulator
->dev_attr
.attr
);
1078 regulator
->dev_attr
.attr
.name
= kstrdup(buf
, GFP_KERNEL
);
1079 if (regulator
->dev_attr
.attr
.name
== NULL
)
1082 regulator
->dev_attr
.attr
.mode
= 0444;
1083 regulator
->dev_attr
.show
= device_requested_uA_show
;
1084 err
= device_create_file(dev
, ®ulator
->dev_attr
);
1086 rdev_warn(rdev
, "could not add regulator_dev requested microamps sysfs entry\n");
1090 /* also add a link to the device sysfs entry */
1091 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1092 dev
->kobj
.name
, supply_name
);
1093 if (size
>= REG_STR_SIZE
)
1096 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1097 if (regulator
->supply_name
== NULL
)
1100 err
= sysfs_create_link(&rdev
->dev
.kobj
, &dev
->kobj
,
1103 rdev_warn(rdev
, "could not add device link %s err %d\n",
1104 dev
->kobj
.name
, err
);
1108 regulator
->supply_name
= kstrdup(supply_name
, GFP_KERNEL
);
1109 if (regulator
->supply_name
== NULL
)
1113 #ifdef CONFIG_DEBUG_FS
1114 regulator
->debugfs
= debugfs_create_dir(regulator
->supply_name
,
1116 if (IS_ERR_OR_NULL(regulator
->debugfs
)) {
1117 rdev_warn(rdev
, "Failed to create debugfs directory\n");
1118 regulator
->debugfs
= NULL
;
1120 debugfs_create_u32("uA_load", 0444, regulator
->debugfs
,
1121 ®ulator
->uA_load
);
1122 debugfs_create_u32("min_uV", 0444, regulator
->debugfs
,
1123 ®ulator
->min_uV
);
1124 debugfs_create_u32("max_uV", 0444, regulator
->debugfs
,
1125 ®ulator
->max_uV
);
1129 mutex_unlock(&rdev
->mutex
);
1132 kfree(regulator
->supply_name
);
1134 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1136 kfree(regulator
->dev_attr
.attr
.name
);
1138 list_del(®ulator
->list
);
1140 mutex_unlock(&rdev
->mutex
);
1144 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1146 if (!rdev
->desc
->ops
->enable_time
)
1148 return rdev
->desc
->ops
->enable_time(rdev
);
1151 /* Internal regulator request function */
1152 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1155 struct regulator_dev
*rdev
;
1156 struct regulator_map
*map
;
1157 struct regulator
*regulator
= ERR_PTR(-ENODEV
);
1158 const char *devname
= NULL
;
1162 pr_err("get() with no identifier\n");
1167 devname
= dev_name(dev
);
1169 mutex_lock(®ulator_list_mutex
);
1171 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1172 /* If the mapping has a device set up it must match */
1173 if (map
->dev_name
&&
1174 (!devname
|| strcmp(map
->dev_name
, devname
)))
1177 if (strcmp(map
->supply
, id
) == 0) {
1178 rdev
= map
->regulator
;
1183 if (board_wants_dummy_regulator
) {
1184 rdev
= dummy_regulator_rdev
;
1188 #ifdef CONFIG_REGULATOR_DUMMY
1190 devname
= "deviceless";
1192 /* If the board didn't flag that it was fully constrained then
1193 * substitute in a dummy regulator so consumers can continue.
1195 if (!has_full_constraints
) {
1196 pr_warn("%s supply %s not found, using dummy regulator\n",
1198 rdev
= dummy_regulator_rdev
;
1203 mutex_unlock(®ulator_list_mutex
);
1207 if (rdev
->exclusive
) {
1208 regulator
= ERR_PTR(-EPERM
);
1212 if (exclusive
&& rdev
->open_count
) {
1213 regulator
= ERR_PTR(-EBUSY
);
1217 if (!try_module_get(rdev
->owner
))
1220 regulator
= create_regulator(rdev
, dev
, id
);
1221 if (regulator
== NULL
) {
1222 regulator
= ERR_PTR(-ENOMEM
);
1223 module_put(rdev
->owner
);
1228 rdev
->exclusive
= 1;
1230 ret
= _regulator_is_enabled(rdev
);
1232 rdev
->use_count
= 1;
1234 rdev
->use_count
= 0;
1238 mutex_unlock(®ulator_list_mutex
);
1244 * regulator_get - lookup and obtain a reference to a regulator.
1245 * @dev: device for regulator "consumer"
1246 * @id: Supply name or regulator ID.
1248 * Returns a struct regulator corresponding to the regulator producer,
1249 * or IS_ERR() condition containing errno.
1251 * Use of supply names configured via regulator_set_device_supply() is
1252 * strongly encouraged. It is recommended that the supply name used
1253 * should match the name used for the supply and/or the relevant
1254 * device pins in the datasheet.
1256 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1258 return _regulator_get(dev
, id
, 0);
1260 EXPORT_SYMBOL_GPL(regulator_get
);
1263 * regulator_get_exclusive - obtain exclusive access to a regulator.
1264 * @dev: device for regulator "consumer"
1265 * @id: Supply name or regulator ID.
1267 * Returns a struct regulator corresponding to the regulator producer,
1268 * or IS_ERR() condition containing errno. Other consumers will be
1269 * unable to obtain this reference is held and the use count for the
1270 * regulator will be initialised to reflect the current state of the
1273 * This is intended for use by consumers which cannot tolerate shared
1274 * use of the regulator such as those which need to force the
1275 * regulator off for correct operation of the hardware they are
1278 * Use of supply names configured via regulator_set_device_supply() is
1279 * strongly encouraged. It is recommended that the supply name used
1280 * should match the name used for the supply and/or the relevant
1281 * device pins in the datasheet.
1283 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1285 return _regulator_get(dev
, id
, 1);
1287 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1290 * regulator_put - "free" the regulator source
1291 * @regulator: regulator source
1293 * Note: drivers must ensure that all regulator_enable calls made on this
1294 * regulator source are balanced by regulator_disable calls prior to calling
1297 void regulator_put(struct regulator
*regulator
)
1299 struct regulator_dev
*rdev
;
1301 if (regulator
== NULL
|| IS_ERR(regulator
))
1304 mutex_lock(®ulator_list_mutex
);
1305 rdev
= regulator
->rdev
;
1307 #ifdef CONFIG_DEBUG_FS
1308 debugfs_remove_recursive(regulator
->debugfs
);
1311 /* remove any sysfs entries */
1312 if (regulator
->dev
) {
1313 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1314 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1315 kfree(regulator
->dev_attr
.attr
.name
);
1317 kfree(regulator
->supply_name
);
1318 list_del(®ulator
->list
);
1322 rdev
->exclusive
= 0;
1324 module_put(rdev
->owner
);
1325 mutex_unlock(®ulator_list_mutex
);
1327 EXPORT_SYMBOL_GPL(regulator_put
);
1329 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
1331 if (!rdev
->constraints
)
1334 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
1340 /* locks held by regulator_enable() */
1341 static int _regulator_enable(struct regulator_dev
*rdev
)
1345 /* check voltage and requested load before enabling */
1346 if (rdev
->constraints
&&
1347 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1348 drms_uA_update(rdev
);
1350 if (rdev
->use_count
== 0) {
1351 /* The regulator may on if it's not switchable or left on */
1352 ret
= _regulator_is_enabled(rdev
);
1353 if (ret
== -EINVAL
|| ret
== 0) {
1354 if (!_regulator_can_change_status(rdev
))
1357 if (!rdev
->desc
->ops
->enable
)
1360 /* Query before enabling in case configuration
1362 ret
= _regulator_get_enable_time(rdev
);
1366 rdev_warn(rdev
, "enable_time() failed: %d\n",
1371 trace_regulator_enable(rdev_get_name(rdev
));
1373 /* Allow the regulator to ramp; it would be useful
1374 * to extend this for bulk operations so that the
1375 * regulators can ramp together. */
1376 ret
= rdev
->desc
->ops
->enable(rdev
);
1380 trace_regulator_enable_delay(rdev_get_name(rdev
));
1382 if (delay
>= 1000) {
1383 mdelay(delay
/ 1000);
1384 udelay(delay
% 1000);
1389 trace_regulator_enable_complete(rdev_get_name(rdev
));
1391 } else if (ret
< 0) {
1392 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
1395 /* Fallthrough on positive return values - already enabled */
1404 * regulator_enable - enable regulator output
1405 * @regulator: regulator source
1407 * Request that the regulator be enabled with the regulator output at
1408 * the predefined voltage or current value. Calls to regulator_enable()
1409 * must be balanced with calls to regulator_disable().
1411 * NOTE: the output value can be set by other drivers, boot loader or may be
1412 * hardwired in the regulator.
1414 int regulator_enable(struct regulator
*regulator
)
1416 struct regulator_dev
*rdev
= regulator
->rdev
;
1420 ret
= regulator_enable(rdev
->supply
);
1425 mutex_lock(&rdev
->mutex
);
1426 ret
= _regulator_enable(rdev
);
1427 mutex_unlock(&rdev
->mutex
);
1429 if (ret
!= 0 && rdev
->supply
)
1430 regulator_disable(rdev
->supply
);
1434 EXPORT_SYMBOL_GPL(regulator_enable
);
1436 /* locks held by regulator_disable() */
1437 static int _regulator_disable(struct regulator_dev
*rdev
)
1441 if (WARN(rdev
->use_count
<= 0,
1442 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
1445 /* are we the last user and permitted to disable ? */
1446 if (rdev
->use_count
== 1 &&
1447 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
1449 /* we are last user */
1450 if (_regulator_can_change_status(rdev
) &&
1451 rdev
->desc
->ops
->disable
) {
1452 trace_regulator_disable(rdev_get_name(rdev
));
1454 ret
= rdev
->desc
->ops
->disable(rdev
);
1456 rdev_err(rdev
, "failed to disable\n");
1460 trace_regulator_disable_complete(rdev_get_name(rdev
));
1462 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
1466 rdev
->use_count
= 0;
1467 } else if (rdev
->use_count
> 1) {
1469 if (rdev
->constraints
&&
1470 (rdev
->constraints
->valid_ops_mask
&
1471 REGULATOR_CHANGE_DRMS
))
1472 drms_uA_update(rdev
);
1481 * regulator_disable - disable regulator output
1482 * @regulator: regulator source
1484 * Disable the regulator output voltage or current. Calls to
1485 * regulator_enable() must be balanced with calls to
1486 * regulator_disable().
1488 * NOTE: this will only disable the regulator output if no other consumer
1489 * devices have it enabled, the regulator device supports disabling and
1490 * machine constraints permit this operation.
1492 int regulator_disable(struct regulator
*regulator
)
1494 struct regulator_dev
*rdev
= regulator
->rdev
;
1497 mutex_lock(&rdev
->mutex
);
1498 ret
= _regulator_disable(rdev
);
1499 mutex_unlock(&rdev
->mutex
);
1501 if (ret
== 0 && rdev
->supply
)
1502 regulator_disable(rdev
->supply
);
1506 EXPORT_SYMBOL_GPL(regulator_disable
);
1508 /* locks held by regulator_force_disable() */
1509 static int _regulator_force_disable(struct regulator_dev
*rdev
)
1514 if (rdev
->desc
->ops
->disable
) {
1515 /* ah well, who wants to live forever... */
1516 ret
= rdev
->desc
->ops
->disable(rdev
);
1518 rdev_err(rdev
, "failed to force disable\n");
1521 /* notify other consumers that power has been forced off */
1522 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
1523 REGULATOR_EVENT_DISABLE
, NULL
);
1530 * regulator_force_disable - force disable regulator output
1531 * @regulator: regulator source
1533 * Forcibly disable the regulator output voltage or current.
1534 * NOTE: this *will* disable the regulator output even if other consumer
1535 * devices have it enabled. This should be used for situations when device
1536 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1538 int regulator_force_disable(struct regulator
*regulator
)
1540 struct regulator_dev
*rdev
= regulator
->rdev
;
1543 mutex_lock(&rdev
->mutex
);
1544 regulator
->uA_load
= 0;
1545 ret
= _regulator_force_disable(regulator
->rdev
);
1546 mutex_unlock(&rdev
->mutex
);
1549 while (rdev
->open_count
--)
1550 regulator_disable(rdev
->supply
);
1554 EXPORT_SYMBOL_GPL(regulator_force_disable
);
1556 static void regulator_disable_work(struct work_struct
*work
)
1558 struct regulator_dev
*rdev
= container_of(work
, struct regulator_dev
,
1562 mutex_lock(&rdev
->mutex
);
1564 BUG_ON(!rdev
->deferred_disables
);
1566 count
= rdev
->deferred_disables
;
1567 rdev
->deferred_disables
= 0;
1569 for (i
= 0; i
< count
; i
++) {
1570 ret
= _regulator_disable(rdev
);
1572 rdev_err(rdev
, "Deferred disable failed: %d\n", ret
);
1575 mutex_unlock(&rdev
->mutex
);
1578 for (i
= 0; i
< count
; i
++) {
1579 ret
= regulator_disable(rdev
->supply
);
1582 "Supply disable failed: %d\n", ret
);
1589 * regulator_disable_deferred - disable regulator output with delay
1590 * @regulator: regulator source
1591 * @ms: miliseconds until the regulator is disabled
1593 * Execute regulator_disable() on the regulator after a delay. This
1594 * is intended for use with devices that require some time to quiesce.
1596 * NOTE: this will only disable the regulator output if no other consumer
1597 * devices have it enabled, the regulator device supports disabling and
1598 * machine constraints permit this operation.
1600 int regulator_disable_deferred(struct regulator
*regulator
, int ms
)
1602 struct regulator_dev
*rdev
= regulator
->rdev
;
1605 mutex_lock(&rdev
->mutex
);
1606 rdev
->deferred_disables
++;
1607 mutex_unlock(&rdev
->mutex
);
1609 ret
= schedule_delayed_work(&rdev
->disable_work
,
1610 msecs_to_jiffies(ms
));
1616 EXPORT_SYMBOL_GPL(regulator_disable_deferred
);
1618 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
1620 /* If we don't know then assume that the regulator is always on */
1621 if (!rdev
->desc
->ops
->is_enabled
)
1624 return rdev
->desc
->ops
->is_enabled(rdev
);
1628 * regulator_is_enabled - is the regulator output enabled
1629 * @regulator: regulator source
1631 * Returns positive if the regulator driver backing the source/client
1632 * has requested that the device be enabled, zero if it hasn't, else a
1633 * negative errno code.
1635 * Note that the device backing this regulator handle can have multiple
1636 * users, so it might be enabled even if regulator_enable() was never
1637 * called for this particular source.
1639 int regulator_is_enabled(struct regulator
*regulator
)
1643 mutex_lock(®ulator
->rdev
->mutex
);
1644 ret
= _regulator_is_enabled(regulator
->rdev
);
1645 mutex_unlock(®ulator
->rdev
->mutex
);
1649 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
1652 * regulator_count_voltages - count regulator_list_voltage() selectors
1653 * @regulator: regulator source
1655 * Returns number of selectors, or negative errno. Selectors are
1656 * numbered starting at zero, and typically correspond to bitfields
1657 * in hardware registers.
1659 int regulator_count_voltages(struct regulator
*regulator
)
1661 struct regulator_dev
*rdev
= regulator
->rdev
;
1663 return rdev
->desc
->n_voltages
? : -EINVAL
;
1665 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
1668 * regulator_list_voltage - enumerate supported voltages
1669 * @regulator: regulator source
1670 * @selector: identify voltage to list
1671 * Context: can sleep
1673 * Returns a voltage that can be passed to @regulator_set_voltage(),
1674 * zero if this selector code can't be used on this system, or a
1677 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
1679 struct regulator_dev
*rdev
= regulator
->rdev
;
1680 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1683 if (!ops
->list_voltage
|| selector
>= rdev
->desc
->n_voltages
)
1686 mutex_lock(&rdev
->mutex
);
1687 ret
= ops
->list_voltage(rdev
, selector
);
1688 mutex_unlock(&rdev
->mutex
);
1691 if (ret
< rdev
->constraints
->min_uV
)
1693 else if (ret
> rdev
->constraints
->max_uV
)
1699 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
1702 * regulator_is_supported_voltage - check if a voltage range can be supported
1704 * @regulator: Regulator to check.
1705 * @min_uV: Minimum required voltage in uV.
1706 * @max_uV: Maximum required voltage in uV.
1708 * Returns a boolean or a negative error code.
1710 int regulator_is_supported_voltage(struct regulator
*regulator
,
1711 int min_uV
, int max_uV
)
1713 int i
, voltages
, ret
;
1715 ret
= regulator_count_voltages(regulator
);
1720 for (i
= 0; i
< voltages
; i
++) {
1721 ret
= regulator_list_voltage(regulator
, i
);
1723 if (ret
>= min_uV
&& ret
<= max_uV
)
1730 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
1731 int min_uV
, int max_uV
)
1735 unsigned int selector
;
1737 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
1739 min_uV
+= rdev
->constraints
->uV_offset
;
1740 max_uV
+= rdev
->constraints
->uV_offset
;
1742 if (rdev
->desc
->ops
->set_voltage
) {
1743 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
,
1746 if (rdev
->desc
->ops
->list_voltage
)
1747 selector
= rdev
->desc
->ops
->list_voltage(rdev
,
1751 } else if (rdev
->desc
->ops
->set_voltage_sel
) {
1752 int best_val
= INT_MAX
;
1757 /* Find the smallest voltage that falls within the specified
1760 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
1761 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
1765 if (ret
< best_val
&& ret
>= min_uV
&& ret
<= max_uV
) {
1772 * If we can't obtain the old selector there is not enough
1773 * info to call set_voltage_time_sel().
1775 if (rdev
->desc
->ops
->set_voltage_time_sel
&&
1776 rdev
->desc
->ops
->get_voltage_sel
) {
1777 unsigned int old_selector
= 0;
1779 ret
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
1783 delay
= rdev
->desc
->ops
->set_voltage_time_sel(rdev
,
1784 old_selector
, selector
);
1787 if (best_val
!= INT_MAX
) {
1788 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
1789 selector
= best_val
;
1797 /* Insert any necessary delays */
1798 if (delay
>= 1000) {
1799 mdelay(delay
/ 1000);
1800 udelay(delay
% 1000);
1806 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
1809 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), selector
);
1815 * regulator_set_voltage - set regulator output voltage
1816 * @regulator: regulator source
1817 * @min_uV: Minimum required voltage in uV
1818 * @max_uV: Maximum acceptable voltage in uV
1820 * Sets a voltage regulator to the desired output voltage. This can be set
1821 * during any regulator state. IOW, regulator can be disabled or enabled.
1823 * If the regulator is enabled then the voltage will change to the new value
1824 * immediately otherwise if the regulator is disabled the regulator will
1825 * output at the new voltage when enabled.
1827 * NOTE: If the regulator is shared between several devices then the lowest
1828 * request voltage that meets the system constraints will be used.
1829 * Regulator system constraints must be set for this regulator before
1830 * calling this function otherwise this call will fail.
1832 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
1834 struct regulator_dev
*rdev
= regulator
->rdev
;
1837 mutex_lock(&rdev
->mutex
);
1839 /* If we're setting the same range as last time the change
1840 * should be a noop (some cpufreq implementations use the same
1841 * voltage for multiple frequencies, for example).
1843 if (regulator
->min_uV
== min_uV
&& regulator
->max_uV
== max_uV
)
1847 if (!rdev
->desc
->ops
->set_voltage
&&
1848 !rdev
->desc
->ops
->set_voltage_sel
) {
1853 /* constraints check */
1854 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1857 regulator
->min_uV
= min_uV
;
1858 regulator
->max_uV
= max_uV
;
1860 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
1864 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
1867 mutex_unlock(&rdev
->mutex
);
1870 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
1873 * regulator_set_voltage_time - get raise/fall time
1874 * @regulator: regulator source
1875 * @old_uV: starting voltage in microvolts
1876 * @new_uV: target voltage in microvolts
1878 * Provided with the starting and ending voltage, this function attempts to
1879 * calculate the time in microseconds required to rise or fall to this new
1882 int regulator_set_voltage_time(struct regulator
*regulator
,
1883 int old_uV
, int new_uV
)
1885 struct regulator_dev
*rdev
= regulator
->rdev
;
1886 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1892 /* Currently requires operations to do this */
1893 if (!ops
->list_voltage
|| !ops
->set_voltage_time_sel
1894 || !rdev
->desc
->n_voltages
)
1897 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
1898 /* We only look for exact voltage matches here */
1899 voltage
= regulator_list_voltage(regulator
, i
);
1904 if (voltage
== old_uV
)
1906 if (voltage
== new_uV
)
1910 if (old_sel
< 0 || new_sel
< 0)
1913 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
1915 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
1918 * regulator_sync_voltage - re-apply last regulator output voltage
1919 * @regulator: regulator source
1921 * Re-apply the last configured voltage. This is intended to be used
1922 * where some external control source the consumer is cooperating with
1923 * has caused the configured voltage to change.
1925 int regulator_sync_voltage(struct regulator
*regulator
)
1927 struct regulator_dev
*rdev
= regulator
->rdev
;
1928 int ret
, min_uV
, max_uV
;
1930 mutex_lock(&rdev
->mutex
);
1932 if (!rdev
->desc
->ops
->set_voltage
&&
1933 !rdev
->desc
->ops
->set_voltage_sel
) {
1938 /* This is only going to work if we've had a voltage configured. */
1939 if (!regulator
->min_uV
&& !regulator
->max_uV
) {
1944 min_uV
= regulator
->min_uV
;
1945 max_uV
= regulator
->max_uV
;
1947 /* This should be a paranoia check... */
1948 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1952 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
1956 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
1959 mutex_unlock(&rdev
->mutex
);
1962 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
1964 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
1968 if (rdev
->desc
->ops
->get_voltage_sel
) {
1969 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
1972 ret
= rdev
->desc
->ops
->list_voltage(rdev
, sel
);
1973 } else if (rdev
->desc
->ops
->get_voltage
) {
1974 ret
= rdev
->desc
->ops
->get_voltage(rdev
);
1981 return ret
- rdev
->constraints
->uV_offset
;
1985 * regulator_get_voltage - get regulator output voltage
1986 * @regulator: regulator source
1988 * This returns the current regulator voltage in uV.
1990 * NOTE: If the regulator is disabled it will return the voltage value. This
1991 * function should not be used to determine regulator state.
1993 int regulator_get_voltage(struct regulator
*regulator
)
1997 mutex_lock(®ulator
->rdev
->mutex
);
1999 ret
= _regulator_get_voltage(regulator
->rdev
);
2001 mutex_unlock(®ulator
->rdev
->mutex
);
2005 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
2008 * regulator_set_current_limit - set regulator output current limit
2009 * @regulator: regulator source
2010 * @min_uA: Minimuum supported current in uA
2011 * @max_uA: Maximum supported current in uA
2013 * Sets current sink to the desired output current. This can be set during
2014 * any regulator state. IOW, regulator can be disabled or enabled.
2016 * If the regulator is enabled then the current will change to the new value
2017 * immediately otherwise if the regulator is disabled the regulator will
2018 * output at the new current when enabled.
2020 * NOTE: Regulator system constraints must be set for this regulator before
2021 * calling this function otherwise this call will fail.
2023 int regulator_set_current_limit(struct regulator
*regulator
,
2024 int min_uA
, int max_uA
)
2026 struct regulator_dev
*rdev
= regulator
->rdev
;
2029 mutex_lock(&rdev
->mutex
);
2032 if (!rdev
->desc
->ops
->set_current_limit
) {
2037 /* constraints check */
2038 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
2042 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
2044 mutex_unlock(&rdev
->mutex
);
2047 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
2049 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
2053 mutex_lock(&rdev
->mutex
);
2056 if (!rdev
->desc
->ops
->get_current_limit
) {
2061 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
2063 mutex_unlock(&rdev
->mutex
);
2068 * regulator_get_current_limit - get regulator output current
2069 * @regulator: regulator source
2071 * This returns the current supplied by the specified current sink in uA.
2073 * NOTE: If the regulator is disabled it will return the current value. This
2074 * function should not be used to determine regulator state.
2076 int regulator_get_current_limit(struct regulator
*regulator
)
2078 return _regulator_get_current_limit(regulator
->rdev
);
2080 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
2083 * regulator_set_mode - set regulator operating mode
2084 * @regulator: regulator source
2085 * @mode: operating mode - one of the REGULATOR_MODE constants
2087 * Set regulator operating mode to increase regulator efficiency or improve
2088 * regulation performance.
2090 * NOTE: Regulator system constraints must be set for this regulator before
2091 * calling this function otherwise this call will fail.
2093 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
2095 struct regulator_dev
*rdev
= regulator
->rdev
;
2097 int regulator_curr_mode
;
2099 mutex_lock(&rdev
->mutex
);
2102 if (!rdev
->desc
->ops
->set_mode
) {
2107 /* return if the same mode is requested */
2108 if (rdev
->desc
->ops
->get_mode
) {
2109 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
2110 if (regulator_curr_mode
== mode
) {
2116 /* constraints check */
2117 ret
= regulator_mode_constrain(rdev
, &mode
);
2121 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
2123 mutex_unlock(&rdev
->mutex
);
2126 EXPORT_SYMBOL_GPL(regulator_set_mode
);
2128 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
2132 mutex_lock(&rdev
->mutex
);
2135 if (!rdev
->desc
->ops
->get_mode
) {
2140 ret
= rdev
->desc
->ops
->get_mode(rdev
);
2142 mutex_unlock(&rdev
->mutex
);
2147 * regulator_get_mode - get regulator operating mode
2148 * @regulator: regulator source
2150 * Get the current regulator operating mode.
2152 unsigned int regulator_get_mode(struct regulator
*regulator
)
2154 return _regulator_get_mode(regulator
->rdev
);
2156 EXPORT_SYMBOL_GPL(regulator_get_mode
);
2159 * regulator_set_optimum_mode - set regulator optimum operating mode
2160 * @regulator: regulator source
2161 * @uA_load: load current
2163 * Notifies the regulator core of a new device load. This is then used by
2164 * DRMS (if enabled by constraints) to set the most efficient regulator
2165 * operating mode for the new regulator loading.
2167 * Consumer devices notify their supply regulator of the maximum power
2168 * they will require (can be taken from device datasheet in the power
2169 * consumption tables) when they change operational status and hence power
2170 * state. Examples of operational state changes that can affect power
2171 * consumption are :-
2173 * o Device is opened / closed.
2174 * o Device I/O is about to begin or has just finished.
2175 * o Device is idling in between work.
2177 * This information is also exported via sysfs to userspace.
2179 * DRMS will sum the total requested load on the regulator and change
2180 * to the most efficient operating mode if platform constraints allow.
2182 * Returns the new regulator mode or error.
2184 int regulator_set_optimum_mode(struct regulator
*regulator
, int uA_load
)
2186 struct regulator_dev
*rdev
= regulator
->rdev
;
2187 struct regulator
*consumer
;
2188 int ret
, output_uV
, input_uV
, total_uA_load
= 0;
2191 mutex_lock(&rdev
->mutex
);
2194 * first check to see if we can set modes at all, otherwise just
2195 * tell the consumer everything is OK.
2197 regulator
->uA_load
= uA_load
;
2198 ret
= regulator_check_drms(rdev
);
2204 if (!rdev
->desc
->ops
->get_optimum_mode
)
2208 * we can actually do this so any errors are indicators of
2209 * potential real failure.
2213 /* get output voltage */
2214 output_uV
= _regulator_get_voltage(rdev
);
2215 if (output_uV
<= 0) {
2216 rdev_err(rdev
, "invalid output voltage found\n");
2220 /* get input voltage */
2223 input_uV
= regulator_get_voltage(rdev
->supply
);
2225 input_uV
= rdev
->constraints
->input_uV
;
2226 if (input_uV
<= 0) {
2227 rdev_err(rdev
, "invalid input voltage found\n");
2231 /* calc total requested load for this regulator */
2232 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
)
2233 total_uA_load
+= consumer
->uA_load
;
2235 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
,
2236 input_uV
, output_uV
,
2238 ret
= regulator_mode_constrain(rdev
, &mode
);
2240 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2241 total_uA_load
, input_uV
, output_uV
);
2245 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
2247 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
2252 mutex_unlock(&rdev
->mutex
);
2255 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode
);
2258 * regulator_register_notifier - register regulator event notifier
2259 * @regulator: regulator source
2260 * @nb: notifier block
2262 * Register notifier block to receive regulator events.
2264 int regulator_register_notifier(struct regulator
*regulator
,
2265 struct notifier_block
*nb
)
2267 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
2270 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
2273 * regulator_unregister_notifier - unregister regulator event notifier
2274 * @regulator: regulator source
2275 * @nb: notifier block
2277 * Unregister regulator event notifier block.
2279 int regulator_unregister_notifier(struct regulator
*regulator
,
2280 struct notifier_block
*nb
)
2282 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
2285 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
2287 /* notify regulator consumers and downstream regulator consumers.
2288 * Note mutex must be held by caller.
2290 static void _notifier_call_chain(struct regulator_dev
*rdev
,
2291 unsigned long event
, void *data
)
2293 /* call rdev chain first */
2294 blocking_notifier_call_chain(&rdev
->notifier
, event
, NULL
);
2298 * regulator_bulk_get - get multiple regulator consumers
2300 * @dev: Device to supply
2301 * @num_consumers: Number of consumers to register
2302 * @consumers: Configuration of consumers; clients are stored here.
2304 * @return 0 on success, an errno on failure.
2306 * This helper function allows drivers to get several regulator
2307 * consumers in one operation. If any of the regulators cannot be
2308 * acquired then any regulators that were allocated will be freed
2309 * before returning to the caller.
2311 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
2312 struct regulator_bulk_data
*consumers
)
2317 for (i
= 0; i
< num_consumers
; i
++)
2318 consumers
[i
].consumer
= NULL
;
2320 for (i
= 0; i
< num_consumers
; i
++) {
2321 consumers
[i
].consumer
= regulator_get(dev
,
2322 consumers
[i
].supply
);
2323 if (IS_ERR(consumers
[i
].consumer
)) {
2324 ret
= PTR_ERR(consumers
[i
].consumer
);
2325 dev_err(dev
, "Failed to get supply '%s': %d\n",
2326 consumers
[i
].supply
, ret
);
2327 consumers
[i
].consumer
= NULL
;
2335 for (i
= 0; i
< num_consumers
&& consumers
[i
].consumer
; i
++)
2336 regulator_put(consumers
[i
].consumer
);
2340 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
2342 static void regulator_bulk_enable_async(void *data
, async_cookie_t cookie
)
2344 struct regulator_bulk_data
*bulk
= data
;
2346 bulk
->ret
= regulator_enable(bulk
->consumer
);
2350 * regulator_bulk_enable - enable multiple regulator consumers
2352 * @num_consumers: Number of consumers
2353 * @consumers: Consumer data; clients are stored here.
2354 * @return 0 on success, an errno on failure
2356 * This convenience API allows consumers to enable multiple regulator
2357 * clients in a single API call. If any consumers cannot be enabled
2358 * then any others that were enabled will be disabled again prior to
2361 int regulator_bulk_enable(int num_consumers
,
2362 struct regulator_bulk_data
*consumers
)
2364 LIST_HEAD(async_domain
);
2368 for (i
= 0; i
< num_consumers
; i
++)
2369 async_schedule_domain(regulator_bulk_enable_async
,
2370 &consumers
[i
], &async_domain
);
2372 async_synchronize_full_domain(&async_domain
);
2374 /* If any consumer failed we need to unwind any that succeeded */
2375 for (i
= 0; i
< num_consumers
; i
++) {
2376 if (consumers
[i
].ret
!= 0) {
2377 ret
= consumers
[i
].ret
;
2385 for (i
= 0; i
< num_consumers
; i
++)
2386 if (consumers
[i
].ret
== 0)
2387 regulator_disable(consumers
[i
].consumer
);
2389 pr_err("Failed to enable %s: %d\n",
2390 consumers
[i
].supply
, consumers
[i
].ret
);
2394 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
2397 * regulator_bulk_disable - disable multiple regulator consumers
2399 * @num_consumers: Number of consumers
2400 * @consumers: Consumer data; clients are stored here.
2401 * @return 0 on success, an errno on failure
2403 * This convenience API allows consumers to disable multiple regulator
2404 * clients in a single API call. If any consumers cannot be enabled
2405 * then any others that were disabled will be disabled again prior to
2408 int regulator_bulk_disable(int num_consumers
,
2409 struct regulator_bulk_data
*consumers
)
2414 for (i
= 0; i
< num_consumers
; i
++) {
2415 ret
= regulator_disable(consumers
[i
].consumer
);
2423 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
2424 for (--i
; i
>= 0; --i
)
2425 regulator_enable(consumers
[i
].consumer
);
2429 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
2432 * regulator_bulk_free - free multiple regulator consumers
2434 * @num_consumers: Number of consumers
2435 * @consumers: Consumer data; clients are stored here.
2437 * This convenience API allows consumers to free multiple regulator
2438 * clients in a single API call.
2440 void regulator_bulk_free(int num_consumers
,
2441 struct regulator_bulk_data
*consumers
)
2445 for (i
= 0; i
< num_consumers
; i
++) {
2446 regulator_put(consumers
[i
].consumer
);
2447 consumers
[i
].consumer
= NULL
;
2450 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
2453 * regulator_notifier_call_chain - call regulator event notifier
2454 * @rdev: regulator source
2455 * @event: notifier block
2456 * @data: callback-specific data.
2458 * Called by regulator drivers to notify clients a regulator event has
2459 * occurred. We also notify regulator clients downstream.
2460 * Note lock must be held by caller.
2462 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
2463 unsigned long event
, void *data
)
2465 _notifier_call_chain(rdev
, event
, data
);
2469 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
2472 * regulator_mode_to_status - convert a regulator mode into a status
2474 * @mode: Mode to convert
2476 * Convert a regulator mode into a status.
2478 int regulator_mode_to_status(unsigned int mode
)
2481 case REGULATOR_MODE_FAST
:
2482 return REGULATOR_STATUS_FAST
;
2483 case REGULATOR_MODE_NORMAL
:
2484 return REGULATOR_STATUS_NORMAL
;
2485 case REGULATOR_MODE_IDLE
:
2486 return REGULATOR_STATUS_IDLE
;
2487 case REGULATOR_STATUS_STANDBY
:
2488 return REGULATOR_STATUS_STANDBY
;
2493 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
2496 * To avoid cluttering sysfs (and memory) with useless state, only
2497 * create attributes that can be meaningfully displayed.
2499 static int add_regulator_attributes(struct regulator_dev
*rdev
)
2501 struct device
*dev
= &rdev
->dev
;
2502 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2505 /* some attributes need specific methods to be displayed */
2506 if (ops
->get_voltage
|| ops
->get_voltage_sel
) {
2507 status
= device_create_file(dev
, &dev_attr_microvolts
);
2511 if (ops
->get_current_limit
) {
2512 status
= device_create_file(dev
, &dev_attr_microamps
);
2516 if (ops
->get_mode
) {
2517 status
= device_create_file(dev
, &dev_attr_opmode
);
2521 if (ops
->is_enabled
) {
2522 status
= device_create_file(dev
, &dev_attr_state
);
2526 if (ops
->get_status
) {
2527 status
= device_create_file(dev
, &dev_attr_status
);
2532 /* some attributes are type-specific */
2533 if (rdev
->desc
->type
== REGULATOR_CURRENT
) {
2534 status
= device_create_file(dev
, &dev_attr_requested_microamps
);
2539 /* all the other attributes exist to support constraints;
2540 * don't show them if there are no constraints, or if the
2541 * relevant supporting methods are missing.
2543 if (!rdev
->constraints
)
2546 /* constraints need specific supporting methods */
2547 if (ops
->set_voltage
|| ops
->set_voltage_sel
) {
2548 status
= device_create_file(dev
, &dev_attr_min_microvolts
);
2551 status
= device_create_file(dev
, &dev_attr_max_microvolts
);
2555 if (ops
->set_current_limit
) {
2556 status
= device_create_file(dev
, &dev_attr_min_microamps
);
2559 status
= device_create_file(dev
, &dev_attr_max_microamps
);
2564 /* suspend mode constraints need multiple supporting methods */
2565 if (!(ops
->set_suspend_enable
&& ops
->set_suspend_disable
))
2568 status
= device_create_file(dev
, &dev_attr_suspend_standby_state
);
2571 status
= device_create_file(dev
, &dev_attr_suspend_mem_state
);
2574 status
= device_create_file(dev
, &dev_attr_suspend_disk_state
);
2578 if (ops
->set_suspend_voltage
) {
2579 status
= device_create_file(dev
,
2580 &dev_attr_suspend_standby_microvolts
);
2583 status
= device_create_file(dev
,
2584 &dev_attr_suspend_mem_microvolts
);
2587 status
= device_create_file(dev
,
2588 &dev_attr_suspend_disk_microvolts
);
2593 if (ops
->set_suspend_mode
) {
2594 status
= device_create_file(dev
,
2595 &dev_attr_suspend_standby_mode
);
2598 status
= device_create_file(dev
,
2599 &dev_attr_suspend_mem_mode
);
2602 status
= device_create_file(dev
,
2603 &dev_attr_suspend_disk_mode
);
2611 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
2613 #ifdef CONFIG_DEBUG_FS
2614 rdev
->debugfs
= debugfs_create_dir(rdev_get_name(rdev
), debugfs_root
);
2615 if (IS_ERR(rdev
->debugfs
) || !rdev
->debugfs
) {
2616 rdev_warn(rdev
, "Failed to create debugfs directory\n");
2617 rdev
->debugfs
= NULL
;
2621 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
2623 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
2629 * regulator_register - register regulator
2630 * @regulator_desc: regulator to register
2631 * @dev: struct device for the regulator
2632 * @init_data: platform provided init data, passed through by driver
2633 * @driver_data: private regulator data
2635 * Called by regulator drivers to register a regulator.
2636 * Returns 0 on success.
2638 struct regulator_dev
*regulator_register(struct regulator_desc
*regulator_desc
,
2639 struct device
*dev
, const struct regulator_init_data
*init_data
,
2642 static atomic_t regulator_no
= ATOMIC_INIT(0);
2643 struct regulator_dev
*rdev
;
2646 if (regulator_desc
== NULL
)
2647 return ERR_PTR(-EINVAL
);
2649 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
2650 return ERR_PTR(-EINVAL
);
2652 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
2653 regulator_desc
->type
!= REGULATOR_CURRENT
)
2654 return ERR_PTR(-EINVAL
);
2657 return ERR_PTR(-EINVAL
);
2659 /* Only one of each should be implemented */
2660 WARN_ON(regulator_desc
->ops
->get_voltage
&&
2661 regulator_desc
->ops
->get_voltage_sel
);
2662 WARN_ON(regulator_desc
->ops
->set_voltage
&&
2663 regulator_desc
->ops
->set_voltage_sel
);
2665 /* If we're using selectors we must implement list_voltage. */
2666 if (regulator_desc
->ops
->get_voltage_sel
&&
2667 !regulator_desc
->ops
->list_voltage
) {
2668 return ERR_PTR(-EINVAL
);
2670 if (regulator_desc
->ops
->set_voltage_sel
&&
2671 !regulator_desc
->ops
->list_voltage
) {
2672 return ERR_PTR(-EINVAL
);
2675 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
2677 return ERR_PTR(-ENOMEM
);
2679 mutex_lock(®ulator_list_mutex
);
2681 mutex_init(&rdev
->mutex
);
2682 rdev
->reg_data
= driver_data
;
2683 rdev
->owner
= regulator_desc
->owner
;
2684 rdev
->desc
= regulator_desc
;
2685 INIT_LIST_HEAD(&rdev
->consumer_list
);
2686 INIT_LIST_HEAD(&rdev
->list
);
2687 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
2688 INIT_DELAYED_WORK(&rdev
->disable_work
, regulator_disable_work
);
2690 /* preform any regulator specific init */
2691 if (init_data
->regulator_init
) {
2692 ret
= init_data
->regulator_init(rdev
->reg_data
);
2697 /* register with sysfs */
2698 rdev
->dev
.class = ®ulator_class
;
2699 rdev
->dev
.parent
= dev
;
2700 dev_set_name(&rdev
->dev
, "regulator.%d",
2701 atomic_inc_return(®ulator_no
) - 1);
2702 ret
= device_register(&rdev
->dev
);
2704 put_device(&rdev
->dev
);
2708 dev_set_drvdata(&rdev
->dev
, rdev
);
2710 /* set regulator constraints */
2711 ret
= set_machine_constraints(rdev
, &init_data
->constraints
);
2715 /* add attributes supported by this regulator */
2716 ret
= add_regulator_attributes(rdev
);
2720 if (init_data
->supply_regulator
) {
2721 struct regulator_dev
*r
;
2724 list_for_each_entry(r
, ®ulator_list
, list
) {
2725 if (strcmp(rdev_get_name(r
),
2726 init_data
->supply_regulator
) == 0) {
2733 dev_err(dev
, "Failed to find supply %s\n",
2734 init_data
->supply_regulator
);
2739 ret
= set_supply(rdev
, r
);
2744 /* add consumers devices */
2745 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
2746 ret
= set_consumer_device_supply(rdev
,
2747 init_data
->consumer_supplies
[i
].dev
,
2748 init_data
->consumer_supplies
[i
].dev_name
,
2749 init_data
->consumer_supplies
[i
].supply
);
2751 dev_err(dev
, "Failed to set supply %s\n",
2752 init_data
->consumer_supplies
[i
].supply
);
2753 goto unset_supplies
;
2757 list_add(&rdev
->list
, ®ulator_list
);
2759 rdev_init_debugfs(rdev
);
2761 mutex_unlock(®ulator_list_mutex
);
2765 unset_regulator_supplies(rdev
);
2768 kfree(rdev
->constraints
);
2769 device_unregister(&rdev
->dev
);
2770 /* device core frees rdev */
2771 rdev
= ERR_PTR(ret
);
2776 rdev
= ERR_PTR(ret
);
2779 EXPORT_SYMBOL_GPL(regulator_register
);
2782 * regulator_unregister - unregister regulator
2783 * @rdev: regulator to unregister
2785 * Called by regulator drivers to unregister a regulator.
2787 void regulator_unregister(struct regulator_dev
*rdev
)
2792 mutex_lock(®ulator_list_mutex
);
2793 #ifdef CONFIG_DEBUG_FS
2794 debugfs_remove_recursive(rdev
->debugfs
);
2796 flush_work_sync(&rdev
->disable_work
.work
);
2797 WARN_ON(rdev
->open_count
);
2798 unset_regulator_supplies(rdev
);
2799 list_del(&rdev
->list
);
2801 regulator_put(rdev
->supply
);
2802 kfree(rdev
->constraints
);
2803 device_unregister(&rdev
->dev
);
2804 mutex_unlock(®ulator_list_mutex
);
2806 EXPORT_SYMBOL_GPL(regulator_unregister
);
2809 * regulator_suspend_prepare - prepare regulators for system wide suspend
2810 * @state: system suspend state
2812 * Configure each regulator with it's suspend operating parameters for state.
2813 * This will usually be called by machine suspend code prior to supending.
2815 int regulator_suspend_prepare(suspend_state_t state
)
2817 struct regulator_dev
*rdev
;
2820 /* ON is handled by regulator active state */
2821 if (state
== PM_SUSPEND_ON
)
2824 mutex_lock(®ulator_list_mutex
);
2825 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2827 mutex_lock(&rdev
->mutex
);
2828 ret
= suspend_prepare(rdev
, state
);
2829 mutex_unlock(&rdev
->mutex
);
2832 rdev_err(rdev
, "failed to prepare\n");
2837 mutex_unlock(®ulator_list_mutex
);
2840 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
2843 * regulator_suspend_finish - resume regulators from system wide suspend
2845 * Turn on regulators that might be turned off by regulator_suspend_prepare
2846 * and that should be turned on according to the regulators properties.
2848 int regulator_suspend_finish(void)
2850 struct regulator_dev
*rdev
;
2853 mutex_lock(®ulator_list_mutex
);
2854 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2855 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2857 mutex_lock(&rdev
->mutex
);
2858 if ((rdev
->use_count
> 0 || rdev
->constraints
->always_on
) &&
2860 error
= ops
->enable(rdev
);
2864 if (!has_full_constraints
)
2868 if (ops
->is_enabled
&& !ops
->is_enabled(rdev
))
2871 error
= ops
->disable(rdev
);
2876 mutex_unlock(&rdev
->mutex
);
2878 mutex_unlock(®ulator_list_mutex
);
2881 EXPORT_SYMBOL_GPL(regulator_suspend_finish
);
2884 * regulator_has_full_constraints - the system has fully specified constraints
2886 * Calling this function will cause the regulator API to disable all
2887 * regulators which have a zero use count and don't have an always_on
2888 * constraint in a late_initcall.
2890 * The intention is that this will become the default behaviour in a
2891 * future kernel release so users are encouraged to use this facility
2894 void regulator_has_full_constraints(void)
2896 has_full_constraints
= 1;
2898 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
2901 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2903 * Calling this function will cause the regulator API to provide a
2904 * dummy regulator to consumers if no physical regulator is found,
2905 * allowing most consumers to proceed as though a regulator were
2906 * configured. This allows systems such as those with software
2907 * controllable regulators for the CPU core only to be brought up more
2910 void regulator_use_dummy_regulator(void)
2912 board_wants_dummy_regulator
= true;
2914 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator
);
2917 * rdev_get_drvdata - get rdev regulator driver data
2920 * Get rdev regulator driver private data. This call can be used in the
2921 * regulator driver context.
2923 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
2925 return rdev
->reg_data
;
2927 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
2930 * regulator_get_drvdata - get regulator driver data
2931 * @regulator: regulator
2933 * Get regulator driver private data. This call can be used in the consumer
2934 * driver context when non API regulator specific functions need to be called.
2936 void *regulator_get_drvdata(struct regulator
*regulator
)
2938 return regulator
->rdev
->reg_data
;
2940 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
2943 * regulator_set_drvdata - set regulator driver data
2944 * @regulator: regulator
2947 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
2949 regulator
->rdev
->reg_data
= data
;
2951 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
2954 * regulator_get_id - get regulator ID
2957 int rdev_get_id(struct regulator_dev
*rdev
)
2959 return rdev
->desc
->id
;
2961 EXPORT_SYMBOL_GPL(rdev_get_id
);
2963 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
2967 EXPORT_SYMBOL_GPL(rdev_get_dev
);
2969 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
2971 return reg_init_data
->driver_data
;
2973 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
2975 #ifdef CONFIG_DEBUG_FS
2976 static ssize_t
supply_map_read_file(struct file
*file
, char __user
*user_buf
,
2977 size_t count
, loff_t
*ppos
)
2979 char *buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
2980 ssize_t len
, ret
= 0;
2981 struct regulator_map
*map
;
2986 list_for_each_entry(map
, ®ulator_map_list
, list
) {
2987 len
= snprintf(buf
+ ret
, PAGE_SIZE
- ret
,
2989 rdev_get_name(map
->regulator
), map
->dev_name
,
2993 if (ret
> PAGE_SIZE
) {
2999 ret
= simple_read_from_buffer(user_buf
, count
, ppos
, buf
, ret
);
3006 static const struct file_operations supply_map_fops
= {
3007 .read
= supply_map_read_file
,
3008 .llseek
= default_llseek
,
3012 static int __init
regulator_init(void)
3016 ret
= class_register(®ulator_class
);
3018 #ifdef CONFIG_DEBUG_FS
3019 debugfs_root
= debugfs_create_dir("regulator", NULL
);
3020 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
3021 pr_warn("regulator: Failed to create debugfs directory\n");
3022 debugfs_root
= NULL
;
3025 if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root
,
3026 NULL
, &supply_map_fops
)))
3027 pr_warn("regulator: Failed to create supplies debugfs\n");
3030 regulator_dummy_init();
3035 /* init early to allow our consumers to complete system booting */
3036 core_initcall(regulator_init
);
3038 static int __init
regulator_init_complete(void)
3040 struct regulator_dev
*rdev
;
3041 struct regulator_ops
*ops
;
3042 struct regulation_constraints
*c
;
3045 mutex_lock(®ulator_list_mutex
);
3047 /* If we have a full configuration then disable any regulators
3048 * which are not in use or always_on. This will become the
3049 * default behaviour in the future.
3051 list_for_each_entry(rdev
, ®ulator_list
, list
) {
3052 ops
= rdev
->desc
->ops
;
3053 c
= rdev
->constraints
;
3055 if (!ops
->disable
|| (c
&& c
->always_on
))
3058 mutex_lock(&rdev
->mutex
);
3060 if (rdev
->use_count
)
3063 /* If we can't read the status assume it's on. */
3064 if (ops
->is_enabled
)
3065 enabled
= ops
->is_enabled(rdev
);
3072 if (has_full_constraints
) {
3073 /* We log since this may kill the system if it
3075 rdev_info(rdev
, "disabling\n");
3076 ret
= ops
->disable(rdev
);
3078 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
3081 /* The intention is that in future we will
3082 * assume that full constraints are provided
3083 * so warn even if we aren't going to do
3086 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
3090 mutex_unlock(&rdev
->mutex
);
3093 mutex_unlock(®ulator_list_mutex
);
3097 late_initcall(regulator_init_complete
);