2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/mutex.h>
21 #include <linux/suspend.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
26 #define REGULATOR_VERSION "0.5"
28 static DEFINE_MUTEX(regulator_list_mutex
);
29 static LIST_HEAD(regulator_list
);
30 static LIST_HEAD(regulator_map_list
);
31 static int has_full_constraints
;
34 * struct regulator_map
36 * Used to provide symbolic supply names to devices.
38 struct regulator_map
{
39 struct list_head list
;
40 const char *dev_name
; /* The dev_name() for the consumer */
42 struct regulator_dev
*regulator
;
48 * One for each consumer device.
52 struct list_head list
;
57 struct device_attribute dev_attr
;
58 struct regulator_dev
*rdev
;
61 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
62 static int _regulator_disable(struct regulator_dev
*rdev
);
63 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
64 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
65 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
66 static void _notifier_call_chain(struct regulator_dev
*rdev
,
67 unsigned long event
, void *data
);
69 /* gets the regulator for a given consumer device */
70 static struct regulator
*get_device_regulator(struct device
*dev
)
72 struct regulator
*regulator
= NULL
;
73 struct regulator_dev
*rdev
;
75 mutex_lock(®ulator_list_mutex
);
76 list_for_each_entry(rdev
, ®ulator_list
, list
) {
77 mutex_lock(&rdev
->mutex
);
78 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
79 if (regulator
->dev
== dev
) {
80 mutex_unlock(&rdev
->mutex
);
81 mutex_unlock(®ulator_list_mutex
);
85 mutex_unlock(&rdev
->mutex
);
87 mutex_unlock(®ulator_list_mutex
);
91 /* Platform voltage constraint check */
92 static int regulator_check_voltage(struct regulator_dev
*rdev
,
93 int *min_uV
, int *max_uV
)
95 BUG_ON(*min_uV
> *max_uV
);
97 if (!rdev
->constraints
) {
98 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
102 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
103 printk(KERN_ERR
"%s: operation not allowed for %s\n",
104 __func__
, rdev
->desc
->name
);
108 if (*max_uV
> rdev
->constraints
->max_uV
)
109 *max_uV
= rdev
->constraints
->max_uV
;
110 if (*min_uV
< rdev
->constraints
->min_uV
)
111 *min_uV
= rdev
->constraints
->min_uV
;
113 if (*min_uV
> *max_uV
)
119 /* current constraint check */
120 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
121 int *min_uA
, int *max_uA
)
123 BUG_ON(*min_uA
> *max_uA
);
125 if (!rdev
->constraints
) {
126 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
130 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
131 printk(KERN_ERR
"%s: operation not allowed for %s\n",
132 __func__
, rdev
->desc
->name
);
136 if (*max_uA
> rdev
->constraints
->max_uA
)
137 *max_uA
= rdev
->constraints
->max_uA
;
138 if (*min_uA
< rdev
->constraints
->min_uA
)
139 *min_uA
= rdev
->constraints
->min_uA
;
141 if (*min_uA
> *max_uA
)
147 /* operating mode constraint check */
148 static int regulator_check_mode(struct regulator_dev
*rdev
, int mode
)
151 case REGULATOR_MODE_FAST
:
152 case REGULATOR_MODE_NORMAL
:
153 case REGULATOR_MODE_IDLE
:
154 case REGULATOR_MODE_STANDBY
:
160 if (!rdev
->constraints
) {
161 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
165 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
166 printk(KERN_ERR
"%s: operation not allowed for %s\n",
167 __func__
, rdev
->desc
->name
);
170 if (!(rdev
->constraints
->valid_modes_mask
& mode
)) {
171 printk(KERN_ERR
"%s: invalid mode %x for %s\n",
172 __func__
, mode
, rdev
->desc
->name
);
178 /* dynamic regulator mode switching constraint check */
179 static int regulator_check_drms(struct regulator_dev
*rdev
)
181 if (!rdev
->constraints
) {
182 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
186 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
187 printk(KERN_ERR
"%s: operation not allowed for %s\n",
188 __func__
, rdev
->desc
->name
);
194 static ssize_t
device_requested_uA_show(struct device
*dev
,
195 struct device_attribute
*attr
, char *buf
)
197 struct regulator
*regulator
;
199 regulator
= get_device_regulator(dev
);
200 if (regulator
== NULL
)
203 return sprintf(buf
, "%d\n", regulator
->uA_load
);
206 static ssize_t
regulator_uV_show(struct device
*dev
,
207 struct device_attribute
*attr
, char *buf
)
209 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
212 mutex_lock(&rdev
->mutex
);
213 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
214 mutex_unlock(&rdev
->mutex
);
218 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
220 static ssize_t
regulator_uA_show(struct device
*dev
,
221 struct device_attribute
*attr
, char *buf
)
223 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
225 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
227 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
229 static ssize_t
regulator_name_show(struct device
*dev
,
230 struct device_attribute
*attr
, char *buf
)
232 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
235 if (rdev
->constraints
&& rdev
->constraints
->name
)
236 name
= rdev
->constraints
->name
;
237 else if (rdev
->desc
->name
)
238 name
= rdev
->desc
->name
;
242 return sprintf(buf
, "%s\n", name
);
245 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
248 case REGULATOR_MODE_FAST
:
249 return sprintf(buf
, "fast\n");
250 case REGULATOR_MODE_NORMAL
:
251 return sprintf(buf
, "normal\n");
252 case REGULATOR_MODE_IDLE
:
253 return sprintf(buf
, "idle\n");
254 case REGULATOR_MODE_STANDBY
:
255 return sprintf(buf
, "standby\n");
257 return sprintf(buf
, "unknown\n");
260 static ssize_t
regulator_opmode_show(struct device
*dev
,
261 struct device_attribute
*attr
, char *buf
)
263 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
265 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
267 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
269 static ssize_t
regulator_print_state(char *buf
, int state
)
272 return sprintf(buf
, "enabled\n");
274 return sprintf(buf
, "disabled\n");
276 return sprintf(buf
, "unknown\n");
279 static ssize_t
regulator_state_show(struct device
*dev
,
280 struct device_attribute
*attr
, char *buf
)
282 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
285 mutex_lock(&rdev
->mutex
);
286 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
287 mutex_unlock(&rdev
->mutex
);
291 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
293 static ssize_t
regulator_status_show(struct device
*dev
,
294 struct device_attribute
*attr
, char *buf
)
296 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
300 status
= rdev
->desc
->ops
->get_status(rdev
);
305 case REGULATOR_STATUS_OFF
:
308 case REGULATOR_STATUS_ON
:
311 case REGULATOR_STATUS_ERROR
:
314 case REGULATOR_STATUS_FAST
:
317 case REGULATOR_STATUS_NORMAL
:
320 case REGULATOR_STATUS_IDLE
:
323 case REGULATOR_STATUS_STANDBY
:
330 return sprintf(buf
, "%s\n", label
);
332 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
334 static ssize_t
regulator_min_uA_show(struct device
*dev
,
335 struct device_attribute
*attr
, char *buf
)
337 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
339 if (!rdev
->constraints
)
340 return sprintf(buf
, "constraint not defined\n");
342 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
344 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
346 static ssize_t
regulator_max_uA_show(struct device
*dev
,
347 struct device_attribute
*attr
, char *buf
)
349 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
351 if (!rdev
->constraints
)
352 return sprintf(buf
, "constraint not defined\n");
354 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
356 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
358 static ssize_t
regulator_min_uV_show(struct device
*dev
,
359 struct device_attribute
*attr
, char *buf
)
361 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
363 if (!rdev
->constraints
)
364 return sprintf(buf
, "constraint not defined\n");
366 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
368 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
370 static ssize_t
regulator_max_uV_show(struct device
*dev
,
371 struct device_attribute
*attr
, char *buf
)
373 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
375 if (!rdev
->constraints
)
376 return sprintf(buf
, "constraint not defined\n");
378 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
380 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
382 static ssize_t
regulator_total_uA_show(struct device
*dev
,
383 struct device_attribute
*attr
, char *buf
)
385 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
386 struct regulator
*regulator
;
389 mutex_lock(&rdev
->mutex
);
390 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
391 uA
+= regulator
->uA_load
;
392 mutex_unlock(&rdev
->mutex
);
393 return sprintf(buf
, "%d\n", uA
);
395 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
397 static ssize_t
regulator_num_users_show(struct device
*dev
,
398 struct device_attribute
*attr
, char *buf
)
400 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
401 return sprintf(buf
, "%d\n", rdev
->use_count
);
404 static ssize_t
regulator_type_show(struct device
*dev
,
405 struct device_attribute
*attr
, char *buf
)
407 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
409 switch (rdev
->desc
->type
) {
410 case REGULATOR_VOLTAGE
:
411 return sprintf(buf
, "voltage\n");
412 case REGULATOR_CURRENT
:
413 return sprintf(buf
, "current\n");
415 return sprintf(buf
, "unknown\n");
418 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
419 struct device_attribute
*attr
, char *buf
)
421 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
423 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
425 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
426 regulator_suspend_mem_uV_show
, NULL
);
428 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
429 struct device_attribute
*attr
, char *buf
)
431 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
433 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
435 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
436 regulator_suspend_disk_uV_show
, NULL
);
438 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
439 struct device_attribute
*attr
, char *buf
)
441 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
443 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
445 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
446 regulator_suspend_standby_uV_show
, NULL
);
448 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
449 struct device_attribute
*attr
, char *buf
)
451 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
453 return regulator_print_opmode(buf
,
454 rdev
->constraints
->state_mem
.mode
);
456 static DEVICE_ATTR(suspend_mem_mode
, 0444,
457 regulator_suspend_mem_mode_show
, NULL
);
459 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
460 struct device_attribute
*attr
, char *buf
)
462 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
464 return regulator_print_opmode(buf
,
465 rdev
->constraints
->state_disk
.mode
);
467 static DEVICE_ATTR(suspend_disk_mode
, 0444,
468 regulator_suspend_disk_mode_show
, NULL
);
470 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
471 struct device_attribute
*attr
, char *buf
)
473 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
475 return regulator_print_opmode(buf
,
476 rdev
->constraints
->state_standby
.mode
);
478 static DEVICE_ATTR(suspend_standby_mode
, 0444,
479 regulator_suspend_standby_mode_show
, NULL
);
481 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
482 struct device_attribute
*attr
, char *buf
)
484 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
486 return regulator_print_state(buf
,
487 rdev
->constraints
->state_mem
.enabled
);
489 static DEVICE_ATTR(suspend_mem_state
, 0444,
490 regulator_suspend_mem_state_show
, NULL
);
492 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
493 struct device_attribute
*attr
, char *buf
)
495 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
497 return regulator_print_state(buf
,
498 rdev
->constraints
->state_disk
.enabled
);
500 static DEVICE_ATTR(suspend_disk_state
, 0444,
501 regulator_suspend_disk_state_show
, NULL
);
503 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
504 struct device_attribute
*attr
, char *buf
)
506 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
508 return regulator_print_state(buf
,
509 rdev
->constraints
->state_standby
.enabled
);
511 static DEVICE_ATTR(suspend_standby_state
, 0444,
512 regulator_suspend_standby_state_show
, NULL
);
516 * These are the only attributes are present for all regulators.
517 * Other attributes are a function of regulator functionality.
519 static struct device_attribute regulator_dev_attrs
[] = {
520 __ATTR(name
, 0444, regulator_name_show
, NULL
),
521 __ATTR(num_users
, 0444, regulator_num_users_show
, NULL
),
522 __ATTR(type
, 0444, regulator_type_show
, NULL
),
526 static void regulator_dev_release(struct device
*dev
)
528 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
532 static struct class regulator_class
= {
534 .dev_release
= regulator_dev_release
,
535 .dev_attrs
= regulator_dev_attrs
,
538 /* Calculate the new optimum regulator operating mode based on the new total
539 * consumer load. All locks held by caller */
540 static void drms_uA_update(struct regulator_dev
*rdev
)
542 struct regulator
*sibling
;
543 int current_uA
= 0, output_uV
, input_uV
, err
;
546 err
= regulator_check_drms(rdev
);
547 if (err
< 0 || !rdev
->desc
->ops
->get_optimum_mode
||
548 !rdev
->desc
->ops
->get_voltage
|| !rdev
->desc
->ops
->set_mode
)
551 /* get output voltage */
552 output_uV
= rdev
->desc
->ops
->get_voltage(rdev
);
556 /* get input voltage */
557 if (rdev
->supply
&& rdev
->supply
->desc
->ops
->get_voltage
)
558 input_uV
= rdev
->supply
->desc
->ops
->get_voltage(rdev
->supply
);
560 input_uV
= rdev
->constraints
->input_uV
;
564 /* calc total requested load */
565 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
566 current_uA
+= sibling
->uA_load
;
568 /* now get the optimum mode for our new total regulator load */
569 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
570 output_uV
, current_uA
);
572 /* check the new mode is allowed */
573 err
= regulator_check_mode(rdev
, mode
);
575 rdev
->desc
->ops
->set_mode(rdev
, mode
);
578 static int suspend_set_state(struct regulator_dev
*rdev
,
579 struct regulator_state
*rstate
)
583 /* enable & disable are mandatory for suspend control */
584 if (!rdev
->desc
->ops
->set_suspend_enable
||
585 !rdev
->desc
->ops
->set_suspend_disable
) {
586 printk(KERN_ERR
"%s: no way to set suspend state\n",
592 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
594 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
596 printk(KERN_ERR
"%s: failed to enabled/disable\n", __func__
);
600 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
601 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
603 printk(KERN_ERR
"%s: failed to set voltage\n",
609 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
610 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
612 printk(KERN_ERR
"%s: failed to set mode\n", __func__
);
619 /* locks held by caller */
620 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
622 if (!rdev
->constraints
)
626 case PM_SUSPEND_STANDBY
:
627 return suspend_set_state(rdev
,
628 &rdev
->constraints
->state_standby
);
630 return suspend_set_state(rdev
,
631 &rdev
->constraints
->state_mem
);
633 return suspend_set_state(rdev
,
634 &rdev
->constraints
->state_disk
);
640 static void print_constraints(struct regulator_dev
*rdev
)
642 struct regulation_constraints
*constraints
= rdev
->constraints
;
646 if (rdev
->desc
->type
== REGULATOR_VOLTAGE
) {
647 if (constraints
->min_uV
== constraints
->max_uV
)
648 count
= sprintf(buf
, "%d mV ",
649 constraints
->min_uV
/ 1000);
651 count
= sprintf(buf
, "%d <--> %d mV ",
652 constraints
->min_uV
/ 1000,
653 constraints
->max_uV
/ 1000);
655 if (constraints
->min_uA
== constraints
->max_uA
)
656 count
= sprintf(buf
, "%d mA ",
657 constraints
->min_uA
/ 1000);
659 count
= sprintf(buf
, "%d <--> %d mA ",
660 constraints
->min_uA
/ 1000,
661 constraints
->max_uA
/ 1000);
663 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
664 count
+= sprintf(buf
+ count
, "fast ");
665 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
666 count
+= sprintf(buf
+ count
, "normal ");
667 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
668 count
+= sprintf(buf
+ count
, "idle ");
669 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
670 count
+= sprintf(buf
+ count
, "standby");
672 printk(KERN_INFO
"regulator: %s: %s\n", rdev
->desc
->name
, buf
);
676 * set_machine_constraints - sets regulator constraints
677 * @rdev: regulator source
678 * @constraints: constraints to apply
680 * Allows platform initialisation code to define and constrain
681 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
682 * Constraints *must* be set by platform code in order for some
683 * regulator operations to proceed i.e. set_voltage, set_current_limit,
686 static int set_machine_constraints(struct regulator_dev
*rdev
,
687 struct regulation_constraints
*constraints
)
691 struct regulator_ops
*ops
= rdev
->desc
->ops
;
693 if (constraints
->name
)
694 name
= constraints
->name
;
695 else if (rdev
->desc
->name
)
696 name
= rdev
->desc
->name
;
700 /* constrain machine-level voltage specs to fit
701 * the actual range supported by this regulator.
703 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
704 int count
= rdev
->desc
->n_voltages
;
706 int min_uV
= INT_MAX
;
707 int max_uV
= INT_MIN
;
708 int cmin
= constraints
->min_uV
;
709 int cmax
= constraints
->max_uV
;
711 /* it's safe to autoconfigure fixed-voltage supplies
712 and the constraints are used by list_voltage. */
713 if (count
== 1 && !cmin
) {
716 constraints
->min_uV
= cmin
;
717 constraints
->max_uV
= cmax
;
720 /* voltage constraints are optional */
721 if ((cmin
== 0) && (cmax
== 0))
724 /* else require explicit machine-level constraints */
725 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
726 pr_err("%s: %s '%s' voltage constraints\n",
727 __func__
, "invalid", name
);
732 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
733 for (i
= 0; i
< count
; i
++) {
736 value
= ops
->list_voltage(rdev
, i
);
740 /* maybe adjust [min_uV..max_uV] */
741 if (value
>= cmin
&& value
< min_uV
)
743 if (value
<= cmax
&& value
> max_uV
)
747 /* final: [min_uV..max_uV] valid iff constraints valid */
748 if (max_uV
< min_uV
) {
749 pr_err("%s: %s '%s' voltage constraints\n",
750 __func__
, "unsupportable", name
);
755 /* use regulator's subset of machine constraints */
756 if (constraints
->min_uV
< min_uV
) {
757 pr_debug("%s: override '%s' %s, %d -> %d\n",
758 __func__
, name
, "min_uV",
759 constraints
->min_uV
, min_uV
);
760 constraints
->min_uV
= min_uV
;
762 if (constraints
->max_uV
> max_uV
) {
763 pr_debug("%s: override '%s' %s, %d -> %d\n",
764 __func__
, name
, "max_uV",
765 constraints
->max_uV
, max_uV
);
766 constraints
->max_uV
= max_uV
;
770 rdev
->constraints
= constraints
;
772 /* do we need to apply the constraint voltage */
773 if (rdev
->constraints
->apply_uV
&&
774 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
&&
776 ret
= ops
->set_voltage(rdev
,
777 rdev
->constraints
->min_uV
, rdev
->constraints
->max_uV
);
779 printk(KERN_ERR
"%s: failed to apply %duV constraint to %s\n",
781 rdev
->constraints
->min_uV
, name
);
782 rdev
->constraints
= NULL
;
787 /* do we need to setup our suspend state */
788 if (constraints
->initial_state
) {
789 ret
= suspend_prepare(rdev
, constraints
->initial_state
);
791 printk(KERN_ERR
"%s: failed to set suspend state for %s\n",
793 rdev
->constraints
= NULL
;
798 if (constraints
->initial_mode
) {
799 if (!ops
->set_mode
) {
800 printk(KERN_ERR
"%s: no set_mode operation for %s\n",
806 ret
= ops
->set_mode(rdev
, constraints
->initial_mode
);
809 "%s: failed to set initial mode for %s: %d\n",
810 __func__
, name
, ret
);
815 /* If the constraints say the regulator should be on at this point
816 * and we have control then make sure it is enabled.
818 if ((constraints
->always_on
|| constraints
->boot_on
) && ops
->enable
) {
819 ret
= ops
->enable(rdev
);
821 printk(KERN_ERR
"%s: failed to enable %s\n",
823 rdev
->constraints
= NULL
;
828 print_constraints(rdev
);
834 * set_supply - set regulator supply regulator
835 * @rdev: regulator name
836 * @supply_rdev: supply regulator name
838 * Called by platform initialisation code to set the supply regulator for this
839 * regulator. This ensures that a regulators supply will also be enabled by the
840 * core if it's child is enabled.
842 static int set_supply(struct regulator_dev
*rdev
,
843 struct regulator_dev
*supply_rdev
)
847 err
= sysfs_create_link(&rdev
->dev
.kobj
, &supply_rdev
->dev
.kobj
,
851 "%s: could not add device link %s err %d\n",
852 __func__
, supply_rdev
->dev
.kobj
.name
, err
);
855 rdev
->supply
= supply_rdev
;
856 list_add(&rdev
->slist
, &supply_rdev
->supply_list
);
862 * set_consumer_device_supply: Bind a regulator to a symbolic supply
863 * @rdev: regulator source
864 * @consumer_dev: device the supply applies to
865 * @consumer_dev_name: dev_name() string for device supply applies to
866 * @supply: symbolic name for supply
868 * Allows platform initialisation code to map physical regulator
869 * sources to symbolic names for supplies for use by devices. Devices
870 * should use these symbolic names to request regulators, avoiding the
871 * need to provide board-specific regulator names as platform data.
873 * Only one of consumer_dev and consumer_dev_name may be specified.
875 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
876 struct device
*consumer_dev
, const char *consumer_dev_name
,
879 struct regulator_map
*node
;
882 if (consumer_dev
&& consumer_dev_name
)
885 if (!consumer_dev_name
&& consumer_dev
)
886 consumer_dev_name
= dev_name(consumer_dev
);
891 if (consumer_dev_name
!= NULL
)
896 list_for_each_entry(node
, ®ulator_map_list
, list
) {
897 if (consumer_dev_name
!= node
->dev_name
)
899 if (strcmp(node
->supply
, supply
) != 0)
902 dev_dbg(consumer_dev
, "%s/%s is '%s' supply; fail %s/%s\n",
903 dev_name(&node
->regulator
->dev
),
904 node
->regulator
->desc
->name
,
906 dev_name(&rdev
->dev
), rdev
->desc
->name
);
910 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
914 node
->regulator
= rdev
;
915 node
->supply
= supply
;
918 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
919 if (node
->dev_name
== NULL
) {
925 list_add(&node
->list
, ®ulator_map_list
);
929 static void unset_consumer_device_supply(struct regulator_dev
*rdev
,
930 const char *consumer_dev_name
, struct device
*consumer_dev
)
932 struct regulator_map
*node
, *n
;
934 if (consumer_dev
&& !consumer_dev_name
)
935 consumer_dev_name
= dev_name(consumer_dev
);
937 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
938 if (rdev
!= node
->regulator
)
941 if (consumer_dev_name
&& node
->dev_name
&&
942 strcmp(consumer_dev_name
, node
->dev_name
))
945 list_del(&node
->list
);
946 kfree(node
->dev_name
);
952 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
954 struct regulator_map
*node
, *n
;
956 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
957 if (rdev
== node
->regulator
) {
958 list_del(&node
->list
);
959 kfree(node
->dev_name
);
966 #define REG_STR_SIZE 32
968 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
970 const char *supply_name
)
972 struct regulator
*regulator
;
973 char buf
[REG_STR_SIZE
];
976 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
977 if (regulator
== NULL
)
980 mutex_lock(&rdev
->mutex
);
981 regulator
->rdev
= rdev
;
982 list_add(®ulator
->list
, &rdev
->consumer_list
);
985 /* create a 'requested_microamps_name' sysfs entry */
986 size
= scnprintf(buf
, REG_STR_SIZE
, "microamps_requested_%s",
988 if (size
>= REG_STR_SIZE
)
991 regulator
->dev
= dev
;
992 regulator
->dev_attr
.attr
.name
= kstrdup(buf
, GFP_KERNEL
);
993 if (regulator
->dev_attr
.attr
.name
== NULL
)
996 regulator
->dev_attr
.attr
.owner
= THIS_MODULE
;
997 regulator
->dev_attr
.attr
.mode
= 0444;
998 regulator
->dev_attr
.show
= device_requested_uA_show
;
999 err
= device_create_file(dev
, ®ulator
->dev_attr
);
1001 printk(KERN_WARNING
"%s: could not add regulator_dev"
1002 " load sysfs\n", __func__
);
1006 /* also add a link to the device sysfs entry */
1007 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1008 dev
->kobj
.name
, supply_name
);
1009 if (size
>= REG_STR_SIZE
)
1012 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1013 if (regulator
->supply_name
== NULL
)
1016 err
= sysfs_create_link(&rdev
->dev
.kobj
, &dev
->kobj
,
1020 "%s: could not add device link %s err %d\n",
1021 __func__
, dev
->kobj
.name
, err
);
1022 device_remove_file(dev
, ®ulator
->dev_attr
);
1026 mutex_unlock(&rdev
->mutex
);
1029 kfree(regulator
->supply_name
);
1031 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1033 kfree(regulator
->dev_attr
.attr
.name
);
1035 list_del(®ulator
->list
);
1037 mutex_unlock(&rdev
->mutex
);
1041 /* Internal regulator request function */
1042 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1045 struct regulator_dev
*rdev
;
1046 struct regulator_map
*map
;
1047 struct regulator
*regulator
= ERR_PTR(-ENODEV
);
1048 const char *devname
= NULL
;
1052 printk(KERN_ERR
"regulator: get() with no identifier\n");
1057 devname
= dev_name(dev
);
1059 mutex_lock(®ulator_list_mutex
);
1061 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1062 /* If the mapping has a device set up it must match */
1063 if (map
->dev_name
&&
1064 (!devname
|| strcmp(map
->dev_name
, devname
)))
1067 if (strcmp(map
->supply
, id
) == 0) {
1068 rdev
= map
->regulator
;
1072 mutex_unlock(®ulator_list_mutex
);
1076 if (rdev
->exclusive
) {
1077 regulator
= ERR_PTR(-EPERM
);
1081 if (exclusive
&& rdev
->open_count
) {
1082 regulator
= ERR_PTR(-EBUSY
);
1086 if (!try_module_get(rdev
->owner
))
1089 regulator
= create_regulator(rdev
, dev
, id
);
1090 if (regulator
== NULL
) {
1091 regulator
= ERR_PTR(-ENOMEM
);
1092 module_put(rdev
->owner
);
1097 rdev
->exclusive
= 1;
1099 ret
= _regulator_is_enabled(rdev
);
1101 rdev
->use_count
= 1;
1103 rdev
->use_count
= 0;
1107 mutex_unlock(®ulator_list_mutex
);
1113 * regulator_get - lookup and obtain a reference to a regulator.
1114 * @dev: device for regulator "consumer"
1115 * @id: Supply name or regulator ID.
1117 * Returns a struct regulator corresponding to the regulator producer,
1118 * or IS_ERR() condition containing errno.
1120 * Use of supply names configured via regulator_set_device_supply() is
1121 * strongly encouraged. It is recommended that the supply name used
1122 * should match the name used for the supply and/or the relevant
1123 * device pins in the datasheet.
1125 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1127 return _regulator_get(dev
, id
, 0);
1129 EXPORT_SYMBOL_GPL(regulator_get
);
1132 * regulator_get_exclusive - obtain exclusive access to a regulator.
1133 * @dev: device for regulator "consumer"
1134 * @id: Supply name or regulator ID.
1136 * Returns a struct regulator corresponding to the regulator producer,
1137 * or IS_ERR() condition containing errno. Other consumers will be
1138 * unable to obtain this reference is held and the use count for the
1139 * regulator will be initialised to reflect the current state of the
1142 * This is intended for use by consumers which cannot tolerate shared
1143 * use of the regulator such as those which need to force the
1144 * regulator off for correct operation of the hardware they are
1147 * Use of supply names configured via regulator_set_device_supply() is
1148 * strongly encouraged. It is recommended that the supply name used
1149 * should match the name used for the supply and/or the relevant
1150 * device pins in the datasheet.
1152 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1154 return _regulator_get(dev
, id
, 1);
1156 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1159 * regulator_put - "free" the regulator source
1160 * @regulator: regulator source
1162 * Note: drivers must ensure that all regulator_enable calls made on this
1163 * regulator source are balanced by regulator_disable calls prior to calling
1166 void regulator_put(struct regulator
*regulator
)
1168 struct regulator_dev
*rdev
;
1170 if (regulator
== NULL
|| IS_ERR(regulator
))
1173 mutex_lock(®ulator_list_mutex
);
1174 rdev
= regulator
->rdev
;
1176 /* remove any sysfs entries */
1177 if (regulator
->dev
) {
1178 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1179 kfree(regulator
->supply_name
);
1180 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1181 kfree(regulator
->dev_attr
.attr
.name
);
1183 list_del(®ulator
->list
);
1187 rdev
->exclusive
= 0;
1189 module_put(rdev
->owner
);
1190 mutex_unlock(®ulator_list_mutex
);
1192 EXPORT_SYMBOL_GPL(regulator_put
);
1194 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
1196 if (!rdev
->constraints
)
1199 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
1205 /* locks held by regulator_enable() */
1206 static int _regulator_enable(struct regulator_dev
*rdev
)
1210 /* do we need to enable the supply regulator first */
1212 ret
= _regulator_enable(rdev
->supply
);
1214 printk(KERN_ERR
"%s: failed to enable %s: %d\n",
1215 __func__
, rdev
->desc
->name
, ret
);
1220 /* check voltage and requested load before enabling */
1221 if (rdev
->constraints
&&
1222 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1223 drms_uA_update(rdev
);
1225 if (rdev
->use_count
== 0) {
1226 /* The regulator may on if it's not switchable or left on */
1227 ret
= _regulator_is_enabled(rdev
);
1228 if (ret
== -EINVAL
|| ret
== 0) {
1229 if (!_regulator_can_change_status(rdev
))
1232 if (rdev
->desc
->ops
->enable
) {
1233 ret
= rdev
->desc
->ops
->enable(rdev
);
1239 } else if (ret
< 0) {
1240 printk(KERN_ERR
"%s: is_enabled() failed for %s: %d\n",
1241 __func__
, rdev
->desc
->name
, ret
);
1244 /* Fallthrough on positive return values - already enabled */
1253 * regulator_enable - enable regulator output
1254 * @regulator: regulator source
1256 * Request that the regulator be enabled with the regulator output at
1257 * the predefined voltage or current value. Calls to regulator_enable()
1258 * must be balanced with calls to regulator_disable().
1260 * NOTE: the output value can be set by other drivers, boot loader or may be
1261 * hardwired in the regulator.
1263 int regulator_enable(struct regulator
*regulator
)
1265 struct regulator_dev
*rdev
= regulator
->rdev
;
1268 mutex_lock(&rdev
->mutex
);
1269 ret
= _regulator_enable(rdev
);
1270 mutex_unlock(&rdev
->mutex
);
1273 EXPORT_SYMBOL_GPL(regulator_enable
);
1275 /* locks held by regulator_disable() */
1276 static int _regulator_disable(struct regulator_dev
*rdev
)
1280 if (WARN(rdev
->use_count
<= 0,
1281 "unbalanced disables for %s\n",
1285 /* are we the last user and permitted to disable ? */
1286 if (rdev
->use_count
== 1 && !rdev
->constraints
->always_on
) {
1288 /* we are last user */
1289 if (_regulator_can_change_status(rdev
) &&
1290 rdev
->desc
->ops
->disable
) {
1291 ret
= rdev
->desc
->ops
->disable(rdev
);
1293 printk(KERN_ERR
"%s: failed to disable %s\n",
1294 __func__
, rdev
->desc
->name
);
1299 /* decrease our supplies ref count and disable if required */
1301 _regulator_disable(rdev
->supply
);
1303 rdev
->use_count
= 0;
1304 } else if (rdev
->use_count
> 1) {
1306 if (rdev
->constraints
&&
1307 (rdev
->constraints
->valid_ops_mask
&
1308 REGULATOR_CHANGE_DRMS
))
1309 drms_uA_update(rdev
);
1317 * regulator_disable - disable regulator output
1318 * @regulator: regulator source
1320 * Disable the regulator output voltage or current. Calls to
1321 * regulator_enable() must be balanced with calls to
1322 * regulator_disable().
1324 * NOTE: this will only disable the regulator output if no other consumer
1325 * devices have it enabled, the regulator device supports disabling and
1326 * machine constraints permit this operation.
1328 int regulator_disable(struct regulator
*regulator
)
1330 struct regulator_dev
*rdev
= regulator
->rdev
;
1333 mutex_lock(&rdev
->mutex
);
1334 ret
= _regulator_disable(rdev
);
1335 mutex_unlock(&rdev
->mutex
);
1338 EXPORT_SYMBOL_GPL(regulator_disable
);
1340 /* locks held by regulator_force_disable() */
1341 static int _regulator_force_disable(struct regulator_dev
*rdev
)
1346 if (rdev
->desc
->ops
->disable
) {
1347 /* ah well, who wants to live forever... */
1348 ret
= rdev
->desc
->ops
->disable(rdev
);
1350 printk(KERN_ERR
"%s: failed to force disable %s\n",
1351 __func__
, rdev
->desc
->name
);
1354 /* notify other consumers that power has been forced off */
1355 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
,
1359 /* decrease our supplies ref count and disable if required */
1361 _regulator_disable(rdev
->supply
);
1363 rdev
->use_count
= 0;
1368 * regulator_force_disable - force disable regulator output
1369 * @regulator: regulator source
1371 * Forcibly disable the regulator output voltage or current.
1372 * NOTE: this *will* disable the regulator output even if other consumer
1373 * devices have it enabled. This should be used for situations when device
1374 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1376 int regulator_force_disable(struct regulator
*regulator
)
1380 mutex_lock(®ulator
->rdev
->mutex
);
1381 regulator
->uA_load
= 0;
1382 ret
= _regulator_force_disable(regulator
->rdev
);
1383 mutex_unlock(®ulator
->rdev
->mutex
);
1386 EXPORT_SYMBOL_GPL(regulator_force_disable
);
1388 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
1391 if (!rdev
->desc
->ops
->is_enabled
)
1394 return rdev
->desc
->ops
->is_enabled(rdev
);
1398 * regulator_is_enabled - is the regulator output enabled
1399 * @regulator: regulator source
1401 * Returns positive if the regulator driver backing the source/client
1402 * has requested that the device be enabled, zero if it hasn't, else a
1403 * negative errno code.
1405 * Note that the device backing this regulator handle can have multiple
1406 * users, so it might be enabled even if regulator_enable() was never
1407 * called for this particular source.
1409 int regulator_is_enabled(struct regulator
*regulator
)
1413 mutex_lock(®ulator
->rdev
->mutex
);
1414 ret
= _regulator_is_enabled(regulator
->rdev
);
1415 mutex_unlock(®ulator
->rdev
->mutex
);
1419 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
1422 * regulator_count_voltages - count regulator_list_voltage() selectors
1423 * @regulator: regulator source
1425 * Returns number of selectors, or negative errno. Selectors are
1426 * numbered starting at zero, and typically correspond to bitfields
1427 * in hardware registers.
1429 int regulator_count_voltages(struct regulator
*regulator
)
1431 struct regulator_dev
*rdev
= regulator
->rdev
;
1433 return rdev
->desc
->n_voltages
? : -EINVAL
;
1435 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
1438 * regulator_list_voltage - enumerate supported voltages
1439 * @regulator: regulator source
1440 * @selector: identify voltage to list
1441 * Context: can sleep
1443 * Returns a voltage that can be passed to @regulator_set_voltage(),
1444 * zero if this selector code can't be used on this sytem, or a
1447 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
1449 struct regulator_dev
*rdev
= regulator
->rdev
;
1450 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1453 if (!ops
->list_voltage
|| selector
>= rdev
->desc
->n_voltages
)
1456 mutex_lock(&rdev
->mutex
);
1457 ret
= ops
->list_voltage(rdev
, selector
);
1458 mutex_unlock(&rdev
->mutex
);
1461 if (ret
< rdev
->constraints
->min_uV
)
1463 else if (ret
> rdev
->constraints
->max_uV
)
1469 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
1472 * regulator_is_supported_voltage - check if a voltage range can be supported
1474 * @regulator: Regulator to check.
1475 * @min_uV: Minimum required voltage in uV.
1476 * @max_uV: Maximum required voltage in uV.
1478 * Returns a boolean or a negative error code.
1480 int regulator_is_supported_voltage(struct regulator
*regulator
,
1481 int min_uV
, int max_uV
)
1483 int i
, voltages
, ret
;
1485 ret
= regulator_count_voltages(regulator
);
1490 for (i
= 0; i
< voltages
; i
++) {
1491 ret
= regulator_list_voltage(regulator
, i
);
1493 if (ret
>= min_uV
&& ret
<= max_uV
)
1501 * regulator_set_voltage - set regulator output voltage
1502 * @regulator: regulator source
1503 * @min_uV: Minimum required voltage in uV
1504 * @max_uV: Maximum acceptable voltage in uV
1506 * Sets a voltage regulator to the desired output voltage. This can be set
1507 * during any regulator state. IOW, regulator can be disabled or enabled.
1509 * If the regulator is enabled then the voltage will change to the new value
1510 * immediately otherwise if the regulator is disabled the regulator will
1511 * output at the new voltage when enabled.
1513 * NOTE: If the regulator is shared between several devices then the lowest
1514 * request voltage that meets the system constraints will be used.
1515 * Regulator system constraints must be set for this regulator before
1516 * calling this function otherwise this call will fail.
1518 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
1520 struct regulator_dev
*rdev
= regulator
->rdev
;
1523 mutex_lock(&rdev
->mutex
);
1526 if (!rdev
->desc
->ops
->set_voltage
) {
1531 /* constraints check */
1532 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1535 regulator
->min_uV
= min_uV
;
1536 regulator
->max_uV
= max_uV
;
1537 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
);
1540 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
, NULL
);
1541 mutex_unlock(&rdev
->mutex
);
1544 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
1546 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
1549 if (rdev
->desc
->ops
->get_voltage
)
1550 return rdev
->desc
->ops
->get_voltage(rdev
);
1556 * regulator_get_voltage - get regulator output voltage
1557 * @regulator: regulator source
1559 * This returns the current regulator voltage in uV.
1561 * NOTE: If the regulator is disabled it will return the voltage value. This
1562 * function should not be used to determine regulator state.
1564 int regulator_get_voltage(struct regulator
*regulator
)
1568 mutex_lock(®ulator
->rdev
->mutex
);
1570 ret
= _regulator_get_voltage(regulator
->rdev
);
1572 mutex_unlock(®ulator
->rdev
->mutex
);
1576 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
1579 * regulator_set_current_limit - set regulator output current limit
1580 * @regulator: regulator source
1581 * @min_uA: Minimuum supported current in uA
1582 * @max_uA: Maximum supported current in uA
1584 * Sets current sink to the desired output current. This can be set during
1585 * any regulator state. IOW, regulator can be disabled or enabled.
1587 * If the regulator is enabled then the current will change to the new value
1588 * immediately otherwise if the regulator is disabled the regulator will
1589 * output at the new current when enabled.
1591 * NOTE: Regulator system constraints must be set for this regulator before
1592 * calling this function otherwise this call will fail.
1594 int regulator_set_current_limit(struct regulator
*regulator
,
1595 int min_uA
, int max_uA
)
1597 struct regulator_dev
*rdev
= regulator
->rdev
;
1600 mutex_lock(&rdev
->mutex
);
1603 if (!rdev
->desc
->ops
->set_current_limit
) {
1608 /* constraints check */
1609 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
1613 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
1615 mutex_unlock(&rdev
->mutex
);
1618 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
1620 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
1624 mutex_lock(&rdev
->mutex
);
1627 if (!rdev
->desc
->ops
->get_current_limit
) {
1632 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
1634 mutex_unlock(&rdev
->mutex
);
1639 * regulator_get_current_limit - get regulator output current
1640 * @regulator: regulator source
1642 * This returns the current supplied by the specified current sink in uA.
1644 * NOTE: If the regulator is disabled it will return the current value. This
1645 * function should not be used to determine regulator state.
1647 int regulator_get_current_limit(struct regulator
*regulator
)
1649 return _regulator_get_current_limit(regulator
->rdev
);
1651 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
1654 * regulator_set_mode - set regulator operating mode
1655 * @regulator: regulator source
1656 * @mode: operating mode - one of the REGULATOR_MODE constants
1658 * Set regulator operating mode to increase regulator efficiency or improve
1659 * regulation performance.
1661 * NOTE: Regulator system constraints must be set for this regulator before
1662 * calling this function otherwise this call will fail.
1664 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
1666 struct regulator_dev
*rdev
= regulator
->rdev
;
1669 mutex_lock(&rdev
->mutex
);
1672 if (!rdev
->desc
->ops
->set_mode
) {
1677 /* constraints check */
1678 ret
= regulator_check_mode(rdev
, mode
);
1682 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1684 mutex_unlock(&rdev
->mutex
);
1687 EXPORT_SYMBOL_GPL(regulator_set_mode
);
1689 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
1693 mutex_lock(&rdev
->mutex
);
1696 if (!rdev
->desc
->ops
->get_mode
) {
1701 ret
= rdev
->desc
->ops
->get_mode(rdev
);
1703 mutex_unlock(&rdev
->mutex
);
1708 * regulator_get_mode - get regulator operating mode
1709 * @regulator: regulator source
1711 * Get the current regulator operating mode.
1713 unsigned int regulator_get_mode(struct regulator
*regulator
)
1715 return _regulator_get_mode(regulator
->rdev
);
1717 EXPORT_SYMBOL_GPL(regulator_get_mode
);
1720 * regulator_set_optimum_mode - set regulator optimum operating mode
1721 * @regulator: regulator source
1722 * @uA_load: load current
1724 * Notifies the regulator core of a new device load. This is then used by
1725 * DRMS (if enabled by constraints) to set the most efficient regulator
1726 * operating mode for the new regulator loading.
1728 * Consumer devices notify their supply regulator of the maximum power
1729 * they will require (can be taken from device datasheet in the power
1730 * consumption tables) when they change operational status and hence power
1731 * state. Examples of operational state changes that can affect power
1732 * consumption are :-
1734 * o Device is opened / closed.
1735 * o Device I/O is about to begin or has just finished.
1736 * o Device is idling in between work.
1738 * This information is also exported via sysfs to userspace.
1740 * DRMS will sum the total requested load on the regulator and change
1741 * to the most efficient operating mode if platform constraints allow.
1743 * Returns the new regulator mode or error.
1745 int regulator_set_optimum_mode(struct regulator
*regulator
, int uA_load
)
1747 struct regulator_dev
*rdev
= regulator
->rdev
;
1748 struct regulator
*consumer
;
1749 int ret
, output_uV
, input_uV
, total_uA_load
= 0;
1752 mutex_lock(&rdev
->mutex
);
1754 regulator
->uA_load
= uA_load
;
1755 ret
= regulator_check_drms(rdev
);
1761 if (!rdev
->desc
->ops
->get_optimum_mode
)
1764 /* get output voltage */
1765 output_uV
= rdev
->desc
->ops
->get_voltage(rdev
);
1766 if (output_uV
<= 0) {
1767 printk(KERN_ERR
"%s: invalid output voltage found for %s\n",
1768 __func__
, rdev
->desc
->name
);
1772 /* get input voltage */
1773 if (rdev
->supply
&& rdev
->supply
->desc
->ops
->get_voltage
)
1774 input_uV
= rdev
->supply
->desc
->ops
->get_voltage(rdev
->supply
);
1776 input_uV
= rdev
->constraints
->input_uV
;
1777 if (input_uV
<= 0) {
1778 printk(KERN_ERR
"%s: invalid input voltage found for %s\n",
1779 __func__
, rdev
->desc
->name
);
1783 /* calc total requested load for this regulator */
1784 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
)
1785 total_uA_load
+= consumer
->uA_load
;
1787 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
,
1788 input_uV
, output_uV
,
1790 ret
= regulator_check_mode(rdev
, mode
);
1792 printk(KERN_ERR
"%s: failed to get optimum mode for %s @"
1793 " %d uA %d -> %d uV\n", __func__
, rdev
->desc
->name
,
1794 total_uA_load
, input_uV
, output_uV
);
1798 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1800 printk(KERN_ERR
"%s: failed to set optimum mode %x for %s\n",
1801 __func__
, mode
, rdev
->desc
->name
);
1806 mutex_unlock(&rdev
->mutex
);
1809 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode
);
1812 * regulator_register_notifier - register regulator event notifier
1813 * @regulator: regulator source
1814 * @nb: notifier block
1816 * Register notifier block to receive regulator events.
1818 int regulator_register_notifier(struct regulator
*regulator
,
1819 struct notifier_block
*nb
)
1821 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
1824 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
1827 * regulator_unregister_notifier - unregister regulator event notifier
1828 * @regulator: regulator source
1829 * @nb: notifier block
1831 * Unregister regulator event notifier block.
1833 int regulator_unregister_notifier(struct regulator
*regulator
,
1834 struct notifier_block
*nb
)
1836 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
1839 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
1841 /* notify regulator consumers and downstream regulator consumers.
1842 * Note mutex must be held by caller.
1844 static void _notifier_call_chain(struct regulator_dev
*rdev
,
1845 unsigned long event
, void *data
)
1847 struct regulator_dev
*_rdev
;
1849 /* call rdev chain first */
1850 blocking_notifier_call_chain(&rdev
->notifier
, event
, NULL
);
1852 /* now notify regulator we supply */
1853 list_for_each_entry(_rdev
, &rdev
->supply_list
, slist
) {
1854 mutex_lock(&_rdev
->mutex
);
1855 _notifier_call_chain(_rdev
, event
, data
);
1856 mutex_unlock(&_rdev
->mutex
);
1861 * regulator_bulk_get - get multiple regulator consumers
1863 * @dev: Device to supply
1864 * @num_consumers: Number of consumers to register
1865 * @consumers: Configuration of consumers; clients are stored here.
1867 * @return 0 on success, an errno on failure.
1869 * This helper function allows drivers to get several regulator
1870 * consumers in one operation. If any of the regulators cannot be
1871 * acquired then any regulators that were allocated will be freed
1872 * before returning to the caller.
1874 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
1875 struct regulator_bulk_data
*consumers
)
1880 for (i
= 0; i
< num_consumers
; i
++)
1881 consumers
[i
].consumer
= NULL
;
1883 for (i
= 0; i
< num_consumers
; i
++) {
1884 consumers
[i
].consumer
= regulator_get(dev
,
1885 consumers
[i
].supply
);
1886 if (IS_ERR(consumers
[i
].consumer
)) {
1887 dev_err(dev
, "Failed to get supply '%s'\n",
1888 consumers
[i
].supply
);
1889 ret
= PTR_ERR(consumers
[i
].consumer
);
1890 consumers
[i
].consumer
= NULL
;
1898 for (i
= 0; i
< num_consumers
&& consumers
[i
].consumer
; i
++)
1899 regulator_put(consumers
[i
].consumer
);
1903 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
1906 * regulator_bulk_enable - enable multiple regulator consumers
1908 * @num_consumers: Number of consumers
1909 * @consumers: Consumer data; clients are stored here.
1910 * @return 0 on success, an errno on failure
1912 * This convenience API allows consumers to enable multiple regulator
1913 * clients in a single API call. If any consumers cannot be enabled
1914 * then any others that were enabled will be disabled again prior to
1917 int regulator_bulk_enable(int num_consumers
,
1918 struct regulator_bulk_data
*consumers
)
1923 for (i
= 0; i
< num_consumers
; i
++) {
1924 ret
= regulator_enable(consumers
[i
].consumer
);
1932 printk(KERN_ERR
"Failed to enable %s\n", consumers
[i
].supply
);
1933 for (i
= 0; i
< num_consumers
; i
++)
1934 regulator_disable(consumers
[i
].consumer
);
1938 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
1941 * regulator_bulk_disable - disable multiple regulator consumers
1943 * @num_consumers: Number of consumers
1944 * @consumers: Consumer data; clients are stored here.
1945 * @return 0 on success, an errno on failure
1947 * This convenience API allows consumers to disable multiple regulator
1948 * clients in a single API call. If any consumers cannot be enabled
1949 * then any others that were disabled will be disabled again prior to
1952 int regulator_bulk_disable(int num_consumers
,
1953 struct regulator_bulk_data
*consumers
)
1958 for (i
= 0; i
< num_consumers
; i
++) {
1959 ret
= regulator_disable(consumers
[i
].consumer
);
1967 printk(KERN_ERR
"Failed to disable %s\n", consumers
[i
].supply
);
1968 for (i
= 0; i
< num_consumers
; i
++)
1969 regulator_enable(consumers
[i
].consumer
);
1973 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
1976 * regulator_bulk_free - free multiple regulator consumers
1978 * @num_consumers: Number of consumers
1979 * @consumers: Consumer data; clients are stored here.
1981 * This convenience API allows consumers to free multiple regulator
1982 * clients in a single API call.
1984 void regulator_bulk_free(int num_consumers
,
1985 struct regulator_bulk_data
*consumers
)
1989 for (i
= 0; i
< num_consumers
; i
++) {
1990 regulator_put(consumers
[i
].consumer
);
1991 consumers
[i
].consumer
= NULL
;
1994 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
1997 * regulator_notifier_call_chain - call regulator event notifier
1998 * @rdev: regulator source
1999 * @event: notifier block
2000 * @data: callback-specific data.
2002 * Called by regulator drivers to notify clients a regulator event has
2003 * occurred. We also notify regulator clients downstream.
2004 * Note lock must be held by caller.
2006 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
2007 unsigned long event
, void *data
)
2009 _notifier_call_chain(rdev
, event
, data
);
2013 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
2016 * regulator_mode_to_status - convert a regulator mode into a status
2018 * @mode: Mode to convert
2020 * Convert a regulator mode into a status.
2022 int regulator_mode_to_status(unsigned int mode
)
2025 case REGULATOR_MODE_FAST
:
2026 return REGULATOR_STATUS_FAST
;
2027 case REGULATOR_MODE_NORMAL
:
2028 return REGULATOR_STATUS_NORMAL
;
2029 case REGULATOR_MODE_IDLE
:
2030 return REGULATOR_STATUS_IDLE
;
2031 case REGULATOR_STATUS_STANDBY
:
2032 return REGULATOR_STATUS_STANDBY
;
2037 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
2040 * To avoid cluttering sysfs (and memory) with useless state, only
2041 * create attributes that can be meaningfully displayed.
2043 static int add_regulator_attributes(struct regulator_dev
*rdev
)
2045 struct device
*dev
= &rdev
->dev
;
2046 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2049 /* some attributes need specific methods to be displayed */
2050 if (ops
->get_voltage
) {
2051 status
= device_create_file(dev
, &dev_attr_microvolts
);
2055 if (ops
->get_current_limit
) {
2056 status
= device_create_file(dev
, &dev_attr_microamps
);
2060 if (ops
->get_mode
) {
2061 status
= device_create_file(dev
, &dev_attr_opmode
);
2065 if (ops
->is_enabled
) {
2066 status
= device_create_file(dev
, &dev_attr_state
);
2070 if (ops
->get_status
) {
2071 status
= device_create_file(dev
, &dev_attr_status
);
2076 /* some attributes are type-specific */
2077 if (rdev
->desc
->type
== REGULATOR_CURRENT
) {
2078 status
= device_create_file(dev
, &dev_attr_requested_microamps
);
2083 /* all the other attributes exist to support constraints;
2084 * don't show them if there are no constraints, or if the
2085 * relevant supporting methods are missing.
2087 if (!rdev
->constraints
)
2090 /* constraints need specific supporting methods */
2091 if (ops
->set_voltage
) {
2092 status
= device_create_file(dev
, &dev_attr_min_microvolts
);
2095 status
= device_create_file(dev
, &dev_attr_max_microvolts
);
2099 if (ops
->set_current_limit
) {
2100 status
= device_create_file(dev
, &dev_attr_min_microamps
);
2103 status
= device_create_file(dev
, &dev_attr_max_microamps
);
2108 /* suspend mode constraints need multiple supporting methods */
2109 if (!(ops
->set_suspend_enable
&& ops
->set_suspend_disable
))
2112 status
= device_create_file(dev
, &dev_attr_suspend_standby_state
);
2115 status
= device_create_file(dev
, &dev_attr_suspend_mem_state
);
2118 status
= device_create_file(dev
, &dev_attr_suspend_disk_state
);
2122 if (ops
->set_suspend_voltage
) {
2123 status
= device_create_file(dev
,
2124 &dev_attr_suspend_standby_microvolts
);
2127 status
= device_create_file(dev
,
2128 &dev_attr_suspend_mem_microvolts
);
2131 status
= device_create_file(dev
,
2132 &dev_attr_suspend_disk_microvolts
);
2137 if (ops
->set_suspend_mode
) {
2138 status
= device_create_file(dev
,
2139 &dev_attr_suspend_standby_mode
);
2142 status
= device_create_file(dev
,
2143 &dev_attr_suspend_mem_mode
);
2146 status
= device_create_file(dev
,
2147 &dev_attr_suspend_disk_mode
);
2156 * regulator_register - register regulator
2157 * @regulator_desc: regulator to register
2158 * @dev: struct device for the regulator
2159 * @init_data: platform provided init data, passed through by driver
2160 * @driver_data: private regulator data
2162 * Called by regulator drivers to register a regulator.
2163 * Returns 0 on success.
2165 struct regulator_dev
*regulator_register(struct regulator_desc
*regulator_desc
,
2166 struct device
*dev
, struct regulator_init_data
*init_data
,
2169 static atomic_t regulator_no
= ATOMIC_INIT(0);
2170 struct regulator_dev
*rdev
;
2173 if (regulator_desc
== NULL
)
2174 return ERR_PTR(-EINVAL
);
2176 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
2177 return ERR_PTR(-EINVAL
);
2179 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
2180 regulator_desc
->type
!= REGULATOR_CURRENT
)
2181 return ERR_PTR(-EINVAL
);
2184 return ERR_PTR(-EINVAL
);
2186 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
2188 return ERR_PTR(-ENOMEM
);
2190 mutex_lock(®ulator_list_mutex
);
2192 mutex_init(&rdev
->mutex
);
2193 rdev
->reg_data
= driver_data
;
2194 rdev
->owner
= regulator_desc
->owner
;
2195 rdev
->desc
= regulator_desc
;
2196 INIT_LIST_HEAD(&rdev
->consumer_list
);
2197 INIT_LIST_HEAD(&rdev
->supply_list
);
2198 INIT_LIST_HEAD(&rdev
->list
);
2199 INIT_LIST_HEAD(&rdev
->slist
);
2200 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
2202 /* preform any regulator specific init */
2203 if (init_data
->regulator_init
) {
2204 ret
= init_data
->regulator_init(rdev
->reg_data
);
2209 /* register with sysfs */
2210 rdev
->dev
.class = ®ulator_class
;
2211 rdev
->dev
.parent
= dev
;
2212 dev_set_name(&rdev
->dev
, "regulator.%d",
2213 atomic_inc_return(®ulator_no
) - 1);
2214 ret
= device_register(&rdev
->dev
);
2218 dev_set_drvdata(&rdev
->dev
, rdev
);
2220 /* set regulator constraints */
2221 ret
= set_machine_constraints(rdev
, &init_data
->constraints
);
2225 /* add attributes supported by this regulator */
2226 ret
= add_regulator_attributes(rdev
);
2230 /* set supply regulator if it exists */
2231 if (init_data
->supply_regulator_dev
) {
2232 ret
= set_supply(rdev
,
2233 dev_get_drvdata(init_data
->supply_regulator_dev
));
2238 /* add consumers devices */
2239 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
2240 ret
= set_consumer_device_supply(rdev
,
2241 init_data
->consumer_supplies
[i
].dev
,
2242 init_data
->consumer_supplies
[i
].dev_name
,
2243 init_data
->consumer_supplies
[i
].supply
);
2245 for (--i
; i
>= 0; i
--)
2246 unset_consumer_device_supply(rdev
,
2247 init_data
->consumer_supplies
[i
].dev_name
,
2248 init_data
->consumer_supplies
[i
].dev
);
2253 list_add(&rdev
->list
, ®ulator_list
);
2255 mutex_unlock(®ulator_list_mutex
);
2259 device_unregister(&rdev
->dev
);
2260 /* device core frees rdev */
2261 rdev
= ERR_PTR(ret
);
2266 rdev
= ERR_PTR(ret
);
2269 EXPORT_SYMBOL_GPL(regulator_register
);
2272 * regulator_unregister - unregister regulator
2273 * @rdev: regulator to unregister
2275 * Called by regulator drivers to unregister a regulator.
2277 void regulator_unregister(struct regulator_dev
*rdev
)
2282 mutex_lock(®ulator_list_mutex
);
2283 WARN_ON(rdev
->open_count
);
2284 unset_regulator_supplies(rdev
);
2285 list_del(&rdev
->list
);
2287 sysfs_remove_link(&rdev
->dev
.kobj
, "supply");
2288 device_unregister(&rdev
->dev
);
2289 mutex_unlock(®ulator_list_mutex
);
2291 EXPORT_SYMBOL_GPL(regulator_unregister
);
2294 * regulator_suspend_prepare - prepare regulators for system wide suspend
2295 * @state: system suspend state
2297 * Configure each regulator with it's suspend operating parameters for state.
2298 * This will usually be called by machine suspend code prior to supending.
2300 int regulator_suspend_prepare(suspend_state_t state
)
2302 struct regulator_dev
*rdev
;
2305 /* ON is handled by regulator active state */
2306 if (state
== PM_SUSPEND_ON
)
2309 mutex_lock(®ulator_list_mutex
);
2310 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2312 mutex_lock(&rdev
->mutex
);
2313 ret
= suspend_prepare(rdev
, state
);
2314 mutex_unlock(&rdev
->mutex
);
2317 printk(KERN_ERR
"%s: failed to prepare %s\n",
2318 __func__
, rdev
->desc
->name
);
2323 mutex_unlock(®ulator_list_mutex
);
2326 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
2329 * regulator_has_full_constraints - the system has fully specified constraints
2331 * Calling this function will cause the regulator API to disable all
2332 * regulators which have a zero use count and don't have an always_on
2333 * constraint in a late_initcall.
2335 * The intention is that this will become the default behaviour in a
2336 * future kernel release so users are encouraged to use this facility
2339 void regulator_has_full_constraints(void)
2341 has_full_constraints
= 1;
2343 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
2346 * rdev_get_drvdata - get rdev regulator driver data
2349 * Get rdev regulator driver private data. This call can be used in the
2350 * regulator driver context.
2352 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
2354 return rdev
->reg_data
;
2356 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
2359 * regulator_get_drvdata - get regulator driver data
2360 * @regulator: regulator
2362 * Get regulator driver private data. This call can be used in the consumer
2363 * driver context when non API regulator specific functions need to be called.
2365 void *regulator_get_drvdata(struct regulator
*regulator
)
2367 return regulator
->rdev
->reg_data
;
2369 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
2372 * regulator_set_drvdata - set regulator driver data
2373 * @regulator: regulator
2376 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
2378 regulator
->rdev
->reg_data
= data
;
2380 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
2383 * regulator_get_id - get regulator ID
2386 int rdev_get_id(struct regulator_dev
*rdev
)
2388 return rdev
->desc
->id
;
2390 EXPORT_SYMBOL_GPL(rdev_get_id
);
2392 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
2396 EXPORT_SYMBOL_GPL(rdev_get_dev
);
2398 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
2400 return reg_init_data
->driver_data
;
2402 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
2404 static int __init
regulator_init(void)
2406 printk(KERN_INFO
"regulator: core version %s\n", REGULATOR_VERSION
);
2407 return class_register(®ulator_class
);
2410 /* init early to allow our consumers to complete system booting */
2411 core_initcall(regulator_init
);
2413 static int __init
regulator_init_complete(void)
2415 struct regulator_dev
*rdev
;
2416 struct regulator_ops
*ops
;
2417 struct regulation_constraints
*c
;
2421 mutex_lock(®ulator_list_mutex
);
2423 /* If we have a full configuration then disable any regulators
2424 * which are not in use or always_on. This will become the
2425 * default behaviour in the future.
2427 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2428 ops
= rdev
->desc
->ops
;
2429 c
= rdev
->constraints
;
2433 else if (rdev
->desc
->name
)
2434 name
= rdev
->desc
->name
;
2438 if (!ops
->disable
|| (c
&& c
->always_on
))
2441 mutex_lock(&rdev
->mutex
);
2443 if (rdev
->use_count
)
2446 /* If we can't read the status assume it's on. */
2447 if (ops
->is_enabled
)
2448 enabled
= ops
->is_enabled(rdev
);
2455 if (has_full_constraints
) {
2456 /* We log since this may kill the system if it
2458 printk(KERN_INFO
"%s: disabling %s\n",
2460 ret
= ops
->disable(rdev
);
2463 "%s: couldn't disable %s: %d\n",
2464 __func__
, name
, ret
);
2467 /* The intention is that in future we will
2468 * assume that full constraints are provided
2469 * so warn even if we aren't going to do
2473 "%s: incomplete constraints, leaving %s on\n",
2478 mutex_unlock(&rdev
->mutex
);
2481 mutex_unlock(®ulator_list_mutex
);
2485 late_initcall(regulator_init_complete
);