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
);
675 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
676 const char *name
, struct regulation_constraints
*constraints
)
678 struct regulator_ops
*ops
= rdev
->desc
->ops
;
681 /* do we need to apply the constraint voltage */
682 if (rdev
->constraints
->apply_uV
&&
683 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
&&
685 ret
= ops
->set_voltage(rdev
,
686 rdev
->constraints
->min_uV
, rdev
->constraints
->max_uV
);
688 printk(KERN_ERR
"%s: failed to apply %duV constraint to %s\n",
690 rdev
->constraints
->min_uV
, name
);
691 rdev
->constraints
= NULL
;
696 /* constrain machine-level voltage specs to fit
697 * the actual range supported by this regulator.
699 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
700 int count
= rdev
->desc
->n_voltages
;
702 int min_uV
= INT_MAX
;
703 int max_uV
= INT_MIN
;
704 int cmin
= constraints
->min_uV
;
705 int cmax
= constraints
->max_uV
;
707 /* it's safe to autoconfigure fixed-voltage supplies
708 and the constraints are used by list_voltage. */
709 if (count
== 1 && !cmin
) {
712 constraints
->min_uV
= cmin
;
713 constraints
->max_uV
= cmax
;
716 /* voltage constraints are optional */
717 if ((cmin
== 0) && (cmax
== 0))
720 /* else require explicit machine-level constraints */
721 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
722 pr_err("%s: %s '%s' voltage constraints\n",
723 __func__
, "invalid", name
);
727 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
728 for (i
= 0; i
< count
; i
++) {
731 value
= ops
->list_voltage(rdev
, i
);
735 /* maybe adjust [min_uV..max_uV] */
736 if (value
>= cmin
&& value
< min_uV
)
738 if (value
<= cmax
&& value
> max_uV
)
742 /* final: [min_uV..max_uV] valid iff constraints valid */
743 if (max_uV
< min_uV
) {
744 pr_err("%s: %s '%s' voltage constraints\n",
745 __func__
, "unsupportable", name
);
749 /* use regulator's subset of machine constraints */
750 if (constraints
->min_uV
< min_uV
) {
751 pr_debug("%s: override '%s' %s, %d -> %d\n",
752 __func__
, name
, "min_uV",
753 constraints
->min_uV
, min_uV
);
754 constraints
->min_uV
= min_uV
;
756 if (constraints
->max_uV
> max_uV
) {
757 pr_debug("%s: override '%s' %s, %d -> %d\n",
758 __func__
, name
, "max_uV",
759 constraints
->max_uV
, max_uV
);
760 constraints
->max_uV
= max_uV
;
768 * set_machine_constraints - sets regulator constraints
769 * @rdev: regulator source
770 * @constraints: constraints to apply
772 * Allows platform initialisation code to define and constrain
773 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
774 * Constraints *must* be set by platform code in order for some
775 * regulator operations to proceed i.e. set_voltage, set_current_limit,
778 static int set_machine_constraints(struct regulator_dev
*rdev
,
779 struct regulation_constraints
*constraints
)
783 struct regulator_ops
*ops
= rdev
->desc
->ops
;
785 if (constraints
->name
)
786 name
= constraints
->name
;
787 else if (rdev
->desc
->name
)
788 name
= rdev
->desc
->name
;
792 rdev
->constraints
= constraints
;
794 ret
= machine_constraints_voltage(rdev
, name
, constraints
);
798 /* do we need to setup our suspend state */
799 if (constraints
->initial_state
) {
800 ret
= suspend_prepare(rdev
, constraints
->initial_state
);
802 printk(KERN_ERR
"%s: failed to set suspend state for %s\n",
804 rdev
->constraints
= NULL
;
809 if (constraints
->initial_mode
) {
810 if (!ops
->set_mode
) {
811 printk(KERN_ERR
"%s: no set_mode operation for %s\n",
817 ret
= ops
->set_mode(rdev
, constraints
->initial_mode
);
820 "%s: failed to set initial mode for %s: %d\n",
821 __func__
, name
, ret
);
826 /* If the constraints say the regulator should be on at this point
827 * and we have control then make sure it is enabled.
829 if ((constraints
->always_on
|| constraints
->boot_on
) && ops
->enable
) {
830 ret
= ops
->enable(rdev
);
832 printk(KERN_ERR
"%s: failed to enable %s\n",
834 rdev
->constraints
= NULL
;
839 print_constraints(rdev
);
845 * set_supply - set regulator supply regulator
846 * @rdev: regulator name
847 * @supply_rdev: supply regulator name
849 * Called by platform initialisation code to set the supply regulator for this
850 * regulator. This ensures that a regulators supply will also be enabled by the
851 * core if it's child is enabled.
853 static int set_supply(struct regulator_dev
*rdev
,
854 struct regulator_dev
*supply_rdev
)
858 err
= sysfs_create_link(&rdev
->dev
.kobj
, &supply_rdev
->dev
.kobj
,
862 "%s: could not add device link %s err %d\n",
863 __func__
, supply_rdev
->dev
.kobj
.name
, err
);
866 rdev
->supply
= supply_rdev
;
867 list_add(&rdev
->slist
, &supply_rdev
->supply_list
);
873 * set_consumer_device_supply: Bind a regulator to a symbolic supply
874 * @rdev: regulator source
875 * @consumer_dev: device the supply applies to
876 * @consumer_dev_name: dev_name() string for device supply applies to
877 * @supply: symbolic name for supply
879 * Allows platform initialisation code to map physical regulator
880 * sources to symbolic names for supplies for use by devices. Devices
881 * should use these symbolic names to request regulators, avoiding the
882 * need to provide board-specific regulator names as platform data.
884 * Only one of consumer_dev and consumer_dev_name may be specified.
886 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
887 struct device
*consumer_dev
, const char *consumer_dev_name
,
890 struct regulator_map
*node
;
893 if (consumer_dev
&& consumer_dev_name
)
896 if (!consumer_dev_name
&& consumer_dev
)
897 consumer_dev_name
= dev_name(consumer_dev
);
902 if (consumer_dev_name
!= NULL
)
907 list_for_each_entry(node
, ®ulator_map_list
, list
) {
908 if (consumer_dev_name
!= node
->dev_name
)
910 if (strcmp(node
->supply
, supply
) != 0)
913 dev_dbg(consumer_dev
, "%s/%s is '%s' supply; fail %s/%s\n",
914 dev_name(&node
->regulator
->dev
),
915 node
->regulator
->desc
->name
,
917 dev_name(&rdev
->dev
), rdev
->desc
->name
);
921 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
925 node
->regulator
= rdev
;
926 node
->supply
= supply
;
929 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
930 if (node
->dev_name
== NULL
) {
936 list_add(&node
->list
, ®ulator_map_list
);
940 static void unset_consumer_device_supply(struct regulator_dev
*rdev
,
941 const char *consumer_dev_name
, struct device
*consumer_dev
)
943 struct regulator_map
*node
, *n
;
945 if (consumer_dev
&& !consumer_dev_name
)
946 consumer_dev_name
= dev_name(consumer_dev
);
948 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
949 if (rdev
!= node
->regulator
)
952 if (consumer_dev_name
&& node
->dev_name
&&
953 strcmp(consumer_dev_name
, node
->dev_name
))
956 list_del(&node
->list
);
957 kfree(node
->dev_name
);
963 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
965 struct regulator_map
*node
, *n
;
967 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
968 if (rdev
== node
->regulator
) {
969 list_del(&node
->list
);
970 kfree(node
->dev_name
);
977 #define REG_STR_SIZE 32
979 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
981 const char *supply_name
)
983 struct regulator
*regulator
;
984 char buf
[REG_STR_SIZE
];
987 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
988 if (regulator
== NULL
)
991 mutex_lock(&rdev
->mutex
);
992 regulator
->rdev
= rdev
;
993 list_add(®ulator
->list
, &rdev
->consumer_list
);
996 /* create a 'requested_microamps_name' sysfs entry */
997 size
= scnprintf(buf
, REG_STR_SIZE
, "microamps_requested_%s",
999 if (size
>= REG_STR_SIZE
)
1002 regulator
->dev
= dev
;
1003 regulator
->dev_attr
.attr
.name
= kstrdup(buf
, GFP_KERNEL
);
1004 if (regulator
->dev_attr
.attr
.name
== NULL
)
1007 regulator
->dev_attr
.attr
.owner
= THIS_MODULE
;
1008 regulator
->dev_attr
.attr
.mode
= 0444;
1009 regulator
->dev_attr
.show
= device_requested_uA_show
;
1010 err
= device_create_file(dev
, ®ulator
->dev_attr
);
1012 printk(KERN_WARNING
"%s: could not add regulator_dev"
1013 " load sysfs\n", __func__
);
1017 /* also add a link to the device sysfs entry */
1018 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1019 dev
->kobj
.name
, supply_name
);
1020 if (size
>= REG_STR_SIZE
)
1023 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1024 if (regulator
->supply_name
== NULL
)
1027 err
= sysfs_create_link(&rdev
->dev
.kobj
, &dev
->kobj
,
1031 "%s: could not add device link %s err %d\n",
1032 __func__
, dev
->kobj
.name
, err
);
1033 device_remove_file(dev
, ®ulator
->dev_attr
);
1037 mutex_unlock(&rdev
->mutex
);
1040 kfree(regulator
->supply_name
);
1042 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1044 kfree(regulator
->dev_attr
.attr
.name
);
1046 list_del(®ulator
->list
);
1048 mutex_unlock(&rdev
->mutex
);
1052 /* Internal regulator request function */
1053 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1056 struct regulator_dev
*rdev
;
1057 struct regulator_map
*map
;
1058 struct regulator
*regulator
= ERR_PTR(-ENODEV
);
1059 const char *devname
= NULL
;
1063 printk(KERN_ERR
"regulator: get() with no identifier\n");
1068 devname
= dev_name(dev
);
1070 mutex_lock(®ulator_list_mutex
);
1072 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1073 /* If the mapping has a device set up it must match */
1074 if (map
->dev_name
&&
1075 (!devname
|| strcmp(map
->dev_name
, devname
)))
1078 if (strcmp(map
->supply
, id
) == 0) {
1079 rdev
= map
->regulator
;
1083 mutex_unlock(®ulator_list_mutex
);
1087 if (rdev
->exclusive
) {
1088 regulator
= ERR_PTR(-EPERM
);
1092 if (exclusive
&& rdev
->open_count
) {
1093 regulator
= ERR_PTR(-EBUSY
);
1097 if (!try_module_get(rdev
->owner
))
1100 regulator
= create_regulator(rdev
, dev
, id
);
1101 if (regulator
== NULL
) {
1102 regulator
= ERR_PTR(-ENOMEM
);
1103 module_put(rdev
->owner
);
1108 rdev
->exclusive
= 1;
1110 ret
= _regulator_is_enabled(rdev
);
1112 rdev
->use_count
= 1;
1114 rdev
->use_count
= 0;
1118 mutex_unlock(®ulator_list_mutex
);
1124 * regulator_get - lookup and obtain a reference to a regulator.
1125 * @dev: device for regulator "consumer"
1126 * @id: Supply name or regulator ID.
1128 * Returns a struct regulator corresponding to the regulator producer,
1129 * or IS_ERR() condition containing errno.
1131 * Use of supply names configured via regulator_set_device_supply() is
1132 * strongly encouraged. It is recommended that the supply name used
1133 * should match the name used for the supply and/or the relevant
1134 * device pins in the datasheet.
1136 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1138 return _regulator_get(dev
, id
, 0);
1140 EXPORT_SYMBOL_GPL(regulator_get
);
1143 * regulator_get_exclusive - obtain exclusive access to a regulator.
1144 * @dev: device for regulator "consumer"
1145 * @id: Supply name or regulator ID.
1147 * Returns a struct regulator corresponding to the regulator producer,
1148 * or IS_ERR() condition containing errno. Other consumers will be
1149 * unable to obtain this reference is held and the use count for the
1150 * regulator will be initialised to reflect the current state of the
1153 * This is intended for use by consumers which cannot tolerate shared
1154 * use of the regulator such as those which need to force the
1155 * regulator off for correct operation of the hardware they are
1158 * Use of supply names configured via regulator_set_device_supply() is
1159 * strongly encouraged. It is recommended that the supply name used
1160 * should match the name used for the supply and/or the relevant
1161 * device pins in the datasheet.
1163 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1165 return _regulator_get(dev
, id
, 1);
1167 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1170 * regulator_put - "free" the regulator source
1171 * @regulator: regulator source
1173 * Note: drivers must ensure that all regulator_enable calls made on this
1174 * regulator source are balanced by regulator_disable calls prior to calling
1177 void regulator_put(struct regulator
*regulator
)
1179 struct regulator_dev
*rdev
;
1181 if (regulator
== NULL
|| IS_ERR(regulator
))
1184 mutex_lock(®ulator_list_mutex
);
1185 rdev
= regulator
->rdev
;
1187 /* remove any sysfs entries */
1188 if (regulator
->dev
) {
1189 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1190 kfree(regulator
->supply_name
);
1191 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1192 kfree(regulator
->dev_attr
.attr
.name
);
1194 list_del(®ulator
->list
);
1198 rdev
->exclusive
= 0;
1200 module_put(rdev
->owner
);
1201 mutex_unlock(®ulator_list_mutex
);
1203 EXPORT_SYMBOL_GPL(regulator_put
);
1205 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
1207 if (!rdev
->constraints
)
1210 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
1216 /* locks held by regulator_enable() */
1217 static int _regulator_enable(struct regulator_dev
*rdev
)
1221 /* do we need to enable the supply regulator first */
1223 ret
= _regulator_enable(rdev
->supply
);
1225 printk(KERN_ERR
"%s: failed to enable %s: %d\n",
1226 __func__
, rdev
->desc
->name
, ret
);
1231 /* check voltage and requested load before enabling */
1232 if (rdev
->constraints
&&
1233 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1234 drms_uA_update(rdev
);
1236 if (rdev
->use_count
== 0) {
1237 /* The regulator may on if it's not switchable or left on */
1238 ret
= _regulator_is_enabled(rdev
);
1239 if (ret
== -EINVAL
|| ret
== 0) {
1240 if (!_regulator_can_change_status(rdev
))
1243 if (rdev
->desc
->ops
->enable
) {
1244 ret
= rdev
->desc
->ops
->enable(rdev
);
1250 } else if (ret
< 0) {
1251 printk(KERN_ERR
"%s: is_enabled() failed for %s: %d\n",
1252 __func__
, rdev
->desc
->name
, ret
);
1255 /* Fallthrough on positive return values - already enabled */
1264 * regulator_enable - enable regulator output
1265 * @regulator: regulator source
1267 * Request that the regulator be enabled with the regulator output at
1268 * the predefined voltage or current value. Calls to regulator_enable()
1269 * must be balanced with calls to regulator_disable().
1271 * NOTE: the output value can be set by other drivers, boot loader or may be
1272 * hardwired in the regulator.
1274 int regulator_enable(struct regulator
*regulator
)
1276 struct regulator_dev
*rdev
= regulator
->rdev
;
1279 mutex_lock(&rdev
->mutex
);
1280 ret
= _regulator_enable(rdev
);
1281 mutex_unlock(&rdev
->mutex
);
1284 EXPORT_SYMBOL_GPL(regulator_enable
);
1286 /* locks held by regulator_disable() */
1287 static int _regulator_disable(struct regulator_dev
*rdev
)
1291 if (WARN(rdev
->use_count
<= 0,
1292 "unbalanced disables for %s\n",
1296 /* are we the last user and permitted to disable ? */
1297 if (rdev
->use_count
== 1 &&
1298 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
1300 /* we are last user */
1301 if (_regulator_can_change_status(rdev
) &&
1302 rdev
->desc
->ops
->disable
) {
1303 ret
= rdev
->desc
->ops
->disable(rdev
);
1305 printk(KERN_ERR
"%s: failed to disable %s\n",
1306 __func__
, rdev
->desc
->name
);
1311 /* decrease our supplies ref count and disable if required */
1313 _regulator_disable(rdev
->supply
);
1315 rdev
->use_count
= 0;
1316 } else if (rdev
->use_count
> 1) {
1318 if (rdev
->constraints
&&
1319 (rdev
->constraints
->valid_ops_mask
&
1320 REGULATOR_CHANGE_DRMS
))
1321 drms_uA_update(rdev
);
1329 * regulator_disable - disable regulator output
1330 * @regulator: regulator source
1332 * Disable the regulator output voltage or current. Calls to
1333 * regulator_enable() must be balanced with calls to
1334 * regulator_disable().
1336 * NOTE: this will only disable the regulator output if no other consumer
1337 * devices have it enabled, the regulator device supports disabling and
1338 * machine constraints permit this operation.
1340 int regulator_disable(struct regulator
*regulator
)
1342 struct regulator_dev
*rdev
= regulator
->rdev
;
1345 mutex_lock(&rdev
->mutex
);
1346 ret
= _regulator_disable(rdev
);
1347 mutex_unlock(&rdev
->mutex
);
1350 EXPORT_SYMBOL_GPL(regulator_disable
);
1352 /* locks held by regulator_force_disable() */
1353 static int _regulator_force_disable(struct regulator_dev
*rdev
)
1358 if (rdev
->desc
->ops
->disable
) {
1359 /* ah well, who wants to live forever... */
1360 ret
= rdev
->desc
->ops
->disable(rdev
);
1362 printk(KERN_ERR
"%s: failed to force disable %s\n",
1363 __func__
, rdev
->desc
->name
);
1366 /* notify other consumers that power has been forced off */
1367 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
,
1371 /* decrease our supplies ref count and disable if required */
1373 _regulator_disable(rdev
->supply
);
1375 rdev
->use_count
= 0;
1380 * regulator_force_disable - force disable regulator output
1381 * @regulator: regulator source
1383 * Forcibly disable the regulator output voltage or current.
1384 * NOTE: this *will* disable the regulator output even if other consumer
1385 * devices have it enabled. This should be used for situations when device
1386 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1388 int regulator_force_disable(struct regulator
*regulator
)
1392 mutex_lock(®ulator
->rdev
->mutex
);
1393 regulator
->uA_load
= 0;
1394 ret
= _regulator_force_disable(regulator
->rdev
);
1395 mutex_unlock(®ulator
->rdev
->mutex
);
1398 EXPORT_SYMBOL_GPL(regulator_force_disable
);
1400 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
1403 if (!rdev
->desc
->ops
->is_enabled
)
1406 return rdev
->desc
->ops
->is_enabled(rdev
);
1410 * regulator_is_enabled - is the regulator output enabled
1411 * @regulator: regulator source
1413 * Returns positive if the regulator driver backing the source/client
1414 * has requested that the device be enabled, zero if it hasn't, else a
1415 * negative errno code.
1417 * Note that the device backing this regulator handle can have multiple
1418 * users, so it might be enabled even if regulator_enable() was never
1419 * called for this particular source.
1421 int regulator_is_enabled(struct regulator
*regulator
)
1425 mutex_lock(®ulator
->rdev
->mutex
);
1426 ret
= _regulator_is_enabled(regulator
->rdev
);
1427 mutex_unlock(®ulator
->rdev
->mutex
);
1431 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
1434 * regulator_count_voltages - count regulator_list_voltage() selectors
1435 * @regulator: regulator source
1437 * Returns number of selectors, or negative errno. Selectors are
1438 * numbered starting at zero, and typically correspond to bitfields
1439 * in hardware registers.
1441 int regulator_count_voltages(struct regulator
*regulator
)
1443 struct regulator_dev
*rdev
= regulator
->rdev
;
1445 return rdev
->desc
->n_voltages
? : -EINVAL
;
1447 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
1450 * regulator_list_voltage - enumerate supported voltages
1451 * @regulator: regulator source
1452 * @selector: identify voltage to list
1453 * Context: can sleep
1455 * Returns a voltage that can be passed to @regulator_set_voltage(),
1456 * zero if this selector code can't be used on this sytem, or a
1459 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
1461 struct regulator_dev
*rdev
= regulator
->rdev
;
1462 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1465 if (!ops
->list_voltage
|| selector
>= rdev
->desc
->n_voltages
)
1468 mutex_lock(&rdev
->mutex
);
1469 ret
= ops
->list_voltage(rdev
, selector
);
1470 mutex_unlock(&rdev
->mutex
);
1473 if (ret
< rdev
->constraints
->min_uV
)
1475 else if (ret
> rdev
->constraints
->max_uV
)
1481 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
1484 * regulator_is_supported_voltage - check if a voltage range can be supported
1486 * @regulator: Regulator to check.
1487 * @min_uV: Minimum required voltage in uV.
1488 * @max_uV: Maximum required voltage in uV.
1490 * Returns a boolean or a negative error code.
1492 int regulator_is_supported_voltage(struct regulator
*regulator
,
1493 int min_uV
, int max_uV
)
1495 int i
, voltages
, ret
;
1497 ret
= regulator_count_voltages(regulator
);
1502 for (i
= 0; i
< voltages
; i
++) {
1503 ret
= regulator_list_voltage(regulator
, i
);
1505 if (ret
>= min_uV
&& ret
<= max_uV
)
1513 * regulator_set_voltage - set regulator output voltage
1514 * @regulator: regulator source
1515 * @min_uV: Minimum required voltage in uV
1516 * @max_uV: Maximum acceptable voltage in uV
1518 * Sets a voltage regulator to the desired output voltage. This can be set
1519 * during any regulator state. IOW, regulator can be disabled or enabled.
1521 * If the regulator is enabled then the voltage will change to the new value
1522 * immediately otherwise if the regulator is disabled the regulator will
1523 * output at the new voltage when enabled.
1525 * NOTE: If the regulator is shared between several devices then the lowest
1526 * request voltage that meets the system constraints will be used.
1527 * Regulator system constraints must be set for this regulator before
1528 * calling this function otherwise this call will fail.
1530 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
1532 struct regulator_dev
*rdev
= regulator
->rdev
;
1535 mutex_lock(&rdev
->mutex
);
1538 if (!rdev
->desc
->ops
->set_voltage
) {
1543 /* constraints check */
1544 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1547 regulator
->min_uV
= min_uV
;
1548 regulator
->max_uV
= max_uV
;
1549 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
);
1552 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
, NULL
);
1553 mutex_unlock(&rdev
->mutex
);
1556 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
1558 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
1561 if (rdev
->desc
->ops
->get_voltage
)
1562 return rdev
->desc
->ops
->get_voltage(rdev
);
1568 * regulator_get_voltage - get regulator output voltage
1569 * @regulator: regulator source
1571 * This returns the current regulator voltage in uV.
1573 * NOTE: If the regulator is disabled it will return the voltage value. This
1574 * function should not be used to determine regulator state.
1576 int regulator_get_voltage(struct regulator
*regulator
)
1580 mutex_lock(®ulator
->rdev
->mutex
);
1582 ret
= _regulator_get_voltage(regulator
->rdev
);
1584 mutex_unlock(®ulator
->rdev
->mutex
);
1588 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
1591 * regulator_set_current_limit - set regulator output current limit
1592 * @regulator: regulator source
1593 * @min_uA: Minimuum supported current in uA
1594 * @max_uA: Maximum supported current in uA
1596 * Sets current sink to the desired output current. This can be set during
1597 * any regulator state. IOW, regulator can be disabled or enabled.
1599 * If the regulator is enabled then the current will change to the new value
1600 * immediately otherwise if the regulator is disabled the regulator will
1601 * output at the new current when enabled.
1603 * NOTE: Regulator system constraints must be set for this regulator before
1604 * calling this function otherwise this call will fail.
1606 int regulator_set_current_limit(struct regulator
*regulator
,
1607 int min_uA
, int max_uA
)
1609 struct regulator_dev
*rdev
= regulator
->rdev
;
1612 mutex_lock(&rdev
->mutex
);
1615 if (!rdev
->desc
->ops
->set_current_limit
) {
1620 /* constraints check */
1621 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
1625 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
1627 mutex_unlock(&rdev
->mutex
);
1630 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
1632 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
1636 mutex_lock(&rdev
->mutex
);
1639 if (!rdev
->desc
->ops
->get_current_limit
) {
1644 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
1646 mutex_unlock(&rdev
->mutex
);
1651 * regulator_get_current_limit - get regulator output current
1652 * @regulator: regulator source
1654 * This returns the current supplied by the specified current sink in uA.
1656 * NOTE: If the regulator is disabled it will return the current value. This
1657 * function should not be used to determine regulator state.
1659 int regulator_get_current_limit(struct regulator
*regulator
)
1661 return _regulator_get_current_limit(regulator
->rdev
);
1663 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
1666 * regulator_set_mode - set regulator operating mode
1667 * @regulator: regulator source
1668 * @mode: operating mode - one of the REGULATOR_MODE constants
1670 * Set regulator operating mode to increase regulator efficiency or improve
1671 * regulation performance.
1673 * NOTE: Regulator system constraints must be set for this regulator before
1674 * calling this function otherwise this call will fail.
1676 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
1678 struct regulator_dev
*rdev
= regulator
->rdev
;
1681 mutex_lock(&rdev
->mutex
);
1684 if (!rdev
->desc
->ops
->set_mode
) {
1689 /* constraints check */
1690 ret
= regulator_check_mode(rdev
, mode
);
1694 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1696 mutex_unlock(&rdev
->mutex
);
1699 EXPORT_SYMBOL_GPL(regulator_set_mode
);
1701 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
1705 mutex_lock(&rdev
->mutex
);
1708 if (!rdev
->desc
->ops
->get_mode
) {
1713 ret
= rdev
->desc
->ops
->get_mode(rdev
);
1715 mutex_unlock(&rdev
->mutex
);
1720 * regulator_get_mode - get regulator operating mode
1721 * @regulator: regulator source
1723 * Get the current regulator operating mode.
1725 unsigned int regulator_get_mode(struct regulator
*regulator
)
1727 return _regulator_get_mode(regulator
->rdev
);
1729 EXPORT_SYMBOL_GPL(regulator_get_mode
);
1732 * regulator_set_optimum_mode - set regulator optimum operating mode
1733 * @regulator: regulator source
1734 * @uA_load: load current
1736 * Notifies the regulator core of a new device load. This is then used by
1737 * DRMS (if enabled by constraints) to set the most efficient regulator
1738 * operating mode for the new regulator loading.
1740 * Consumer devices notify their supply regulator of the maximum power
1741 * they will require (can be taken from device datasheet in the power
1742 * consumption tables) when they change operational status and hence power
1743 * state. Examples of operational state changes that can affect power
1744 * consumption are :-
1746 * o Device is opened / closed.
1747 * o Device I/O is about to begin or has just finished.
1748 * o Device is idling in between work.
1750 * This information is also exported via sysfs to userspace.
1752 * DRMS will sum the total requested load on the regulator and change
1753 * to the most efficient operating mode if platform constraints allow.
1755 * Returns the new regulator mode or error.
1757 int regulator_set_optimum_mode(struct regulator
*regulator
, int uA_load
)
1759 struct regulator_dev
*rdev
= regulator
->rdev
;
1760 struct regulator
*consumer
;
1761 int ret
, output_uV
, input_uV
, total_uA_load
= 0;
1764 mutex_lock(&rdev
->mutex
);
1766 regulator
->uA_load
= uA_load
;
1767 ret
= regulator_check_drms(rdev
);
1773 if (!rdev
->desc
->ops
->get_optimum_mode
)
1776 /* get output voltage */
1777 output_uV
= rdev
->desc
->ops
->get_voltage(rdev
);
1778 if (output_uV
<= 0) {
1779 printk(KERN_ERR
"%s: invalid output voltage found for %s\n",
1780 __func__
, rdev
->desc
->name
);
1784 /* get input voltage */
1785 if (rdev
->supply
&& rdev
->supply
->desc
->ops
->get_voltage
)
1786 input_uV
= rdev
->supply
->desc
->ops
->get_voltage(rdev
->supply
);
1788 input_uV
= rdev
->constraints
->input_uV
;
1789 if (input_uV
<= 0) {
1790 printk(KERN_ERR
"%s: invalid input voltage found for %s\n",
1791 __func__
, rdev
->desc
->name
);
1795 /* calc total requested load for this regulator */
1796 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
)
1797 total_uA_load
+= consumer
->uA_load
;
1799 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
,
1800 input_uV
, output_uV
,
1802 ret
= regulator_check_mode(rdev
, mode
);
1804 printk(KERN_ERR
"%s: failed to get optimum mode for %s @"
1805 " %d uA %d -> %d uV\n", __func__
, rdev
->desc
->name
,
1806 total_uA_load
, input_uV
, output_uV
);
1810 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1812 printk(KERN_ERR
"%s: failed to set optimum mode %x for %s\n",
1813 __func__
, mode
, rdev
->desc
->name
);
1818 mutex_unlock(&rdev
->mutex
);
1821 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode
);
1824 * regulator_register_notifier - register regulator event notifier
1825 * @regulator: regulator source
1826 * @nb: notifier block
1828 * Register notifier block to receive regulator events.
1830 int regulator_register_notifier(struct regulator
*regulator
,
1831 struct notifier_block
*nb
)
1833 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
1836 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
1839 * regulator_unregister_notifier - unregister regulator event notifier
1840 * @regulator: regulator source
1841 * @nb: notifier block
1843 * Unregister regulator event notifier block.
1845 int regulator_unregister_notifier(struct regulator
*regulator
,
1846 struct notifier_block
*nb
)
1848 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
1851 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
1853 /* notify regulator consumers and downstream regulator consumers.
1854 * Note mutex must be held by caller.
1856 static void _notifier_call_chain(struct regulator_dev
*rdev
,
1857 unsigned long event
, void *data
)
1859 struct regulator_dev
*_rdev
;
1861 /* call rdev chain first */
1862 blocking_notifier_call_chain(&rdev
->notifier
, event
, NULL
);
1864 /* now notify regulator we supply */
1865 list_for_each_entry(_rdev
, &rdev
->supply_list
, slist
) {
1866 mutex_lock(&_rdev
->mutex
);
1867 _notifier_call_chain(_rdev
, event
, data
);
1868 mutex_unlock(&_rdev
->mutex
);
1873 * regulator_bulk_get - get multiple regulator consumers
1875 * @dev: Device to supply
1876 * @num_consumers: Number of consumers to register
1877 * @consumers: Configuration of consumers; clients are stored here.
1879 * @return 0 on success, an errno on failure.
1881 * This helper function allows drivers to get several regulator
1882 * consumers in one operation. If any of the regulators cannot be
1883 * acquired then any regulators that were allocated will be freed
1884 * before returning to the caller.
1886 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
1887 struct regulator_bulk_data
*consumers
)
1892 for (i
= 0; i
< num_consumers
; i
++)
1893 consumers
[i
].consumer
= NULL
;
1895 for (i
= 0; i
< num_consumers
; i
++) {
1896 consumers
[i
].consumer
= regulator_get(dev
,
1897 consumers
[i
].supply
);
1898 if (IS_ERR(consumers
[i
].consumer
)) {
1899 ret
= PTR_ERR(consumers
[i
].consumer
);
1900 dev_err(dev
, "Failed to get supply '%s': %d\n",
1901 consumers
[i
].supply
, ret
);
1902 consumers
[i
].consumer
= NULL
;
1910 for (i
= 0; i
< num_consumers
&& consumers
[i
].consumer
; i
++)
1911 regulator_put(consumers
[i
].consumer
);
1915 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
1918 * regulator_bulk_enable - enable multiple regulator consumers
1920 * @num_consumers: Number of consumers
1921 * @consumers: Consumer data; clients are stored here.
1922 * @return 0 on success, an errno on failure
1924 * This convenience API allows consumers to enable multiple regulator
1925 * clients in a single API call. If any consumers cannot be enabled
1926 * then any others that were enabled will be disabled again prior to
1929 int regulator_bulk_enable(int num_consumers
,
1930 struct regulator_bulk_data
*consumers
)
1935 for (i
= 0; i
< num_consumers
; i
++) {
1936 ret
= regulator_enable(consumers
[i
].consumer
);
1944 printk(KERN_ERR
"Failed to enable %s: %d\n", consumers
[i
].supply
, ret
);
1945 for (i
= 0; i
< num_consumers
; i
++)
1946 regulator_disable(consumers
[i
].consumer
);
1950 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
1953 * regulator_bulk_disable - disable multiple regulator consumers
1955 * @num_consumers: Number of consumers
1956 * @consumers: Consumer data; clients are stored here.
1957 * @return 0 on success, an errno on failure
1959 * This convenience API allows consumers to disable multiple regulator
1960 * clients in a single API call. If any consumers cannot be enabled
1961 * then any others that were disabled will be disabled again prior to
1964 int regulator_bulk_disable(int num_consumers
,
1965 struct regulator_bulk_data
*consumers
)
1970 for (i
= 0; i
< num_consumers
; i
++) {
1971 ret
= regulator_disable(consumers
[i
].consumer
);
1979 printk(KERN_ERR
"Failed to disable %s: %d\n", consumers
[i
].supply
,
1981 for (i
= 0; i
< num_consumers
; i
++)
1982 regulator_enable(consumers
[i
].consumer
);
1986 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
1989 * regulator_bulk_free - free multiple regulator consumers
1991 * @num_consumers: Number of consumers
1992 * @consumers: Consumer data; clients are stored here.
1994 * This convenience API allows consumers to free multiple regulator
1995 * clients in a single API call.
1997 void regulator_bulk_free(int num_consumers
,
1998 struct regulator_bulk_data
*consumers
)
2002 for (i
= 0; i
< num_consumers
; i
++) {
2003 regulator_put(consumers
[i
].consumer
);
2004 consumers
[i
].consumer
= NULL
;
2007 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
2010 * regulator_notifier_call_chain - call regulator event notifier
2011 * @rdev: regulator source
2012 * @event: notifier block
2013 * @data: callback-specific data.
2015 * Called by regulator drivers to notify clients a regulator event has
2016 * occurred. We also notify regulator clients downstream.
2017 * Note lock must be held by caller.
2019 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
2020 unsigned long event
, void *data
)
2022 _notifier_call_chain(rdev
, event
, data
);
2026 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
2029 * regulator_mode_to_status - convert a regulator mode into a status
2031 * @mode: Mode to convert
2033 * Convert a regulator mode into a status.
2035 int regulator_mode_to_status(unsigned int mode
)
2038 case REGULATOR_MODE_FAST
:
2039 return REGULATOR_STATUS_FAST
;
2040 case REGULATOR_MODE_NORMAL
:
2041 return REGULATOR_STATUS_NORMAL
;
2042 case REGULATOR_MODE_IDLE
:
2043 return REGULATOR_STATUS_IDLE
;
2044 case REGULATOR_STATUS_STANDBY
:
2045 return REGULATOR_STATUS_STANDBY
;
2050 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
2053 * To avoid cluttering sysfs (and memory) with useless state, only
2054 * create attributes that can be meaningfully displayed.
2056 static int add_regulator_attributes(struct regulator_dev
*rdev
)
2058 struct device
*dev
= &rdev
->dev
;
2059 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2062 /* some attributes need specific methods to be displayed */
2063 if (ops
->get_voltage
) {
2064 status
= device_create_file(dev
, &dev_attr_microvolts
);
2068 if (ops
->get_current_limit
) {
2069 status
= device_create_file(dev
, &dev_attr_microamps
);
2073 if (ops
->get_mode
) {
2074 status
= device_create_file(dev
, &dev_attr_opmode
);
2078 if (ops
->is_enabled
) {
2079 status
= device_create_file(dev
, &dev_attr_state
);
2083 if (ops
->get_status
) {
2084 status
= device_create_file(dev
, &dev_attr_status
);
2089 /* some attributes are type-specific */
2090 if (rdev
->desc
->type
== REGULATOR_CURRENT
) {
2091 status
= device_create_file(dev
, &dev_attr_requested_microamps
);
2096 /* all the other attributes exist to support constraints;
2097 * don't show them if there are no constraints, or if the
2098 * relevant supporting methods are missing.
2100 if (!rdev
->constraints
)
2103 /* constraints need specific supporting methods */
2104 if (ops
->set_voltage
) {
2105 status
= device_create_file(dev
, &dev_attr_min_microvolts
);
2108 status
= device_create_file(dev
, &dev_attr_max_microvolts
);
2112 if (ops
->set_current_limit
) {
2113 status
= device_create_file(dev
, &dev_attr_min_microamps
);
2116 status
= device_create_file(dev
, &dev_attr_max_microamps
);
2121 /* suspend mode constraints need multiple supporting methods */
2122 if (!(ops
->set_suspend_enable
&& ops
->set_suspend_disable
))
2125 status
= device_create_file(dev
, &dev_attr_suspend_standby_state
);
2128 status
= device_create_file(dev
, &dev_attr_suspend_mem_state
);
2131 status
= device_create_file(dev
, &dev_attr_suspend_disk_state
);
2135 if (ops
->set_suspend_voltage
) {
2136 status
= device_create_file(dev
,
2137 &dev_attr_suspend_standby_microvolts
);
2140 status
= device_create_file(dev
,
2141 &dev_attr_suspend_mem_microvolts
);
2144 status
= device_create_file(dev
,
2145 &dev_attr_suspend_disk_microvolts
);
2150 if (ops
->set_suspend_mode
) {
2151 status
= device_create_file(dev
,
2152 &dev_attr_suspend_standby_mode
);
2155 status
= device_create_file(dev
,
2156 &dev_attr_suspend_mem_mode
);
2159 status
= device_create_file(dev
,
2160 &dev_attr_suspend_disk_mode
);
2169 * regulator_register - register regulator
2170 * @regulator_desc: regulator to register
2171 * @dev: struct device for the regulator
2172 * @init_data: platform provided init data, passed through by driver
2173 * @driver_data: private regulator data
2175 * Called by regulator drivers to register a regulator.
2176 * Returns 0 on success.
2178 struct regulator_dev
*regulator_register(struct regulator_desc
*regulator_desc
,
2179 struct device
*dev
, struct regulator_init_data
*init_data
,
2182 static atomic_t regulator_no
= ATOMIC_INIT(0);
2183 struct regulator_dev
*rdev
;
2186 if (regulator_desc
== NULL
)
2187 return ERR_PTR(-EINVAL
);
2189 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
2190 return ERR_PTR(-EINVAL
);
2192 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
2193 regulator_desc
->type
!= REGULATOR_CURRENT
)
2194 return ERR_PTR(-EINVAL
);
2197 return ERR_PTR(-EINVAL
);
2199 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
2201 return ERR_PTR(-ENOMEM
);
2203 mutex_lock(®ulator_list_mutex
);
2205 mutex_init(&rdev
->mutex
);
2206 rdev
->reg_data
= driver_data
;
2207 rdev
->owner
= regulator_desc
->owner
;
2208 rdev
->desc
= regulator_desc
;
2209 INIT_LIST_HEAD(&rdev
->consumer_list
);
2210 INIT_LIST_HEAD(&rdev
->supply_list
);
2211 INIT_LIST_HEAD(&rdev
->list
);
2212 INIT_LIST_HEAD(&rdev
->slist
);
2213 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
2215 /* preform any regulator specific init */
2216 if (init_data
->regulator_init
) {
2217 ret
= init_data
->regulator_init(rdev
->reg_data
);
2222 /* register with sysfs */
2223 rdev
->dev
.class = ®ulator_class
;
2224 rdev
->dev
.parent
= dev
;
2225 dev_set_name(&rdev
->dev
, "regulator.%d",
2226 atomic_inc_return(®ulator_no
) - 1);
2227 ret
= device_register(&rdev
->dev
);
2231 dev_set_drvdata(&rdev
->dev
, rdev
);
2233 /* set regulator constraints */
2234 ret
= set_machine_constraints(rdev
, &init_data
->constraints
);
2238 /* add attributes supported by this regulator */
2239 ret
= add_regulator_attributes(rdev
);
2243 /* set supply regulator if it exists */
2244 if (init_data
->supply_regulator_dev
) {
2245 ret
= set_supply(rdev
,
2246 dev_get_drvdata(init_data
->supply_regulator_dev
));
2251 /* add consumers devices */
2252 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
2253 ret
= set_consumer_device_supply(rdev
,
2254 init_data
->consumer_supplies
[i
].dev
,
2255 init_data
->consumer_supplies
[i
].dev_name
,
2256 init_data
->consumer_supplies
[i
].supply
);
2258 for (--i
; i
>= 0; i
--)
2259 unset_consumer_device_supply(rdev
,
2260 init_data
->consumer_supplies
[i
].dev_name
,
2261 init_data
->consumer_supplies
[i
].dev
);
2266 list_add(&rdev
->list
, ®ulator_list
);
2268 mutex_unlock(®ulator_list_mutex
);
2272 device_unregister(&rdev
->dev
);
2273 /* device core frees rdev */
2274 rdev
= ERR_PTR(ret
);
2279 rdev
= ERR_PTR(ret
);
2282 EXPORT_SYMBOL_GPL(regulator_register
);
2285 * regulator_unregister - unregister regulator
2286 * @rdev: regulator to unregister
2288 * Called by regulator drivers to unregister a regulator.
2290 void regulator_unregister(struct regulator_dev
*rdev
)
2295 mutex_lock(®ulator_list_mutex
);
2296 WARN_ON(rdev
->open_count
);
2297 unset_regulator_supplies(rdev
);
2298 list_del(&rdev
->list
);
2300 sysfs_remove_link(&rdev
->dev
.kobj
, "supply");
2301 device_unregister(&rdev
->dev
);
2302 mutex_unlock(®ulator_list_mutex
);
2304 EXPORT_SYMBOL_GPL(regulator_unregister
);
2307 * regulator_suspend_prepare - prepare regulators for system wide suspend
2308 * @state: system suspend state
2310 * Configure each regulator with it's suspend operating parameters for state.
2311 * This will usually be called by machine suspend code prior to supending.
2313 int regulator_suspend_prepare(suspend_state_t state
)
2315 struct regulator_dev
*rdev
;
2318 /* ON is handled by regulator active state */
2319 if (state
== PM_SUSPEND_ON
)
2322 mutex_lock(®ulator_list_mutex
);
2323 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2325 mutex_lock(&rdev
->mutex
);
2326 ret
= suspend_prepare(rdev
, state
);
2327 mutex_unlock(&rdev
->mutex
);
2330 printk(KERN_ERR
"%s: failed to prepare %s\n",
2331 __func__
, rdev
->desc
->name
);
2336 mutex_unlock(®ulator_list_mutex
);
2339 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
2342 * regulator_has_full_constraints - the system has fully specified constraints
2344 * Calling this function will cause the regulator API to disable all
2345 * regulators which have a zero use count and don't have an always_on
2346 * constraint in a late_initcall.
2348 * The intention is that this will become the default behaviour in a
2349 * future kernel release so users are encouraged to use this facility
2352 void regulator_has_full_constraints(void)
2354 has_full_constraints
= 1;
2356 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
2359 * rdev_get_drvdata - get rdev regulator driver data
2362 * Get rdev regulator driver private data. This call can be used in the
2363 * regulator driver context.
2365 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
2367 return rdev
->reg_data
;
2369 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
2372 * regulator_get_drvdata - get regulator driver data
2373 * @regulator: regulator
2375 * Get regulator driver private data. This call can be used in the consumer
2376 * driver context when non API regulator specific functions need to be called.
2378 void *regulator_get_drvdata(struct regulator
*regulator
)
2380 return regulator
->rdev
->reg_data
;
2382 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
2385 * regulator_set_drvdata - set regulator driver data
2386 * @regulator: regulator
2389 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
2391 regulator
->rdev
->reg_data
= data
;
2393 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
2396 * regulator_get_id - get regulator ID
2399 int rdev_get_id(struct regulator_dev
*rdev
)
2401 return rdev
->desc
->id
;
2403 EXPORT_SYMBOL_GPL(rdev_get_id
);
2405 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
2409 EXPORT_SYMBOL_GPL(rdev_get_dev
);
2411 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
2413 return reg_init_data
->driver_data
;
2415 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
2417 static int __init
regulator_init(void)
2419 printk(KERN_INFO
"regulator: core version %s\n", REGULATOR_VERSION
);
2420 return class_register(®ulator_class
);
2423 /* init early to allow our consumers to complete system booting */
2424 core_initcall(regulator_init
);
2426 static int __init
regulator_init_complete(void)
2428 struct regulator_dev
*rdev
;
2429 struct regulator_ops
*ops
;
2430 struct regulation_constraints
*c
;
2434 mutex_lock(®ulator_list_mutex
);
2436 /* If we have a full configuration then disable any regulators
2437 * which are not in use or always_on. This will become the
2438 * default behaviour in the future.
2440 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2441 ops
= rdev
->desc
->ops
;
2442 c
= rdev
->constraints
;
2446 else if (rdev
->desc
->name
)
2447 name
= rdev
->desc
->name
;
2451 if (!ops
->disable
|| (c
&& c
->always_on
))
2454 mutex_lock(&rdev
->mutex
);
2456 if (rdev
->use_count
)
2459 /* If we can't read the status assume it's on. */
2460 if (ops
->is_enabled
)
2461 enabled
= ops
->is_enabled(rdev
);
2468 if (has_full_constraints
) {
2469 /* We log since this may kill the system if it
2471 printk(KERN_INFO
"%s: disabling %s\n",
2473 ret
= ops
->disable(rdev
);
2476 "%s: couldn't disable %s: %d\n",
2477 __func__
, name
, ret
);
2480 /* The intention is that in future we will
2481 * assume that full constraints are provided
2482 * so warn even if we aren't going to do
2486 "%s: incomplete constraints, leaving %s on\n",
2491 mutex_unlock(&rdev
->mutex
);
2494 mutex_unlock(®ulator_list_mutex
);
2498 late_initcall(regulator_init_complete
);