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/slab.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include <linux/suspend.h>
23 #include <linux/delay.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
30 #define REGULATOR_VERSION "0.5"
32 static DEFINE_MUTEX(regulator_list_mutex
);
33 static LIST_HEAD(regulator_list
);
34 static LIST_HEAD(regulator_map_list
);
35 static int has_full_constraints
;
36 static bool board_wants_dummy_regulator
;
39 * struct regulator_map
41 * Used to provide symbolic supply names to devices.
43 struct regulator_map
{
44 struct list_head list
;
45 const char *dev_name
; /* The dev_name() for the consumer */
47 struct regulator_dev
*regulator
;
53 * One for each consumer device.
57 struct list_head list
;
62 struct device_attribute dev_attr
;
63 struct regulator_dev
*rdev
;
66 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
67 static int _regulator_disable(struct regulator_dev
*rdev
,
68 struct regulator_dev
**supply_rdev_ptr
);
69 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
70 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
71 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
72 static void _notifier_call_chain(struct regulator_dev
*rdev
,
73 unsigned long event
, void *data
);
75 static const char *rdev_get_name(struct regulator_dev
*rdev
)
77 if (rdev
->constraints
&& rdev
->constraints
->name
)
78 return rdev
->constraints
->name
;
79 else if (rdev
->desc
->name
)
80 return rdev
->desc
->name
;
85 /* gets the regulator for a given consumer device */
86 static struct regulator
*get_device_regulator(struct device
*dev
)
88 struct regulator
*regulator
= NULL
;
89 struct regulator_dev
*rdev
;
91 mutex_lock(®ulator_list_mutex
);
92 list_for_each_entry(rdev
, ®ulator_list
, list
) {
93 mutex_lock(&rdev
->mutex
);
94 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
95 if (regulator
->dev
== dev
) {
96 mutex_unlock(&rdev
->mutex
);
97 mutex_unlock(®ulator_list_mutex
);
101 mutex_unlock(&rdev
->mutex
);
103 mutex_unlock(®ulator_list_mutex
);
107 /* Platform voltage constraint check */
108 static int regulator_check_voltage(struct regulator_dev
*rdev
,
109 int *min_uV
, int *max_uV
)
111 BUG_ON(*min_uV
> *max_uV
);
113 if (!rdev
->constraints
) {
114 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
115 rdev_get_name(rdev
));
118 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
119 printk(KERN_ERR
"%s: operation not allowed for %s\n",
120 __func__
, rdev_get_name(rdev
));
124 if (*max_uV
> rdev
->constraints
->max_uV
)
125 *max_uV
= rdev
->constraints
->max_uV
;
126 if (*min_uV
< rdev
->constraints
->min_uV
)
127 *min_uV
= rdev
->constraints
->min_uV
;
129 if (*min_uV
> *max_uV
)
135 /* current constraint check */
136 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
137 int *min_uA
, int *max_uA
)
139 BUG_ON(*min_uA
> *max_uA
);
141 if (!rdev
->constraints
) {
142 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
143 rdev_get_name(rdev
));
146 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
147 printk(KERN_ERR
"%s: operation not allowed for %s\n",
148 __func__
, rdev_get_name(rdev
));
152 if (*max_uA
> rdev
->constraints
->max_uA
)
153 *max_uA
= rdev
->constraints
->max_uA
;
154 if (*min_uA
< rdev
->constraints
->min_uA
)
155 *min_uA
= rdev
->constraints
->min_uA
;
157 if (*min_uA
> *max_uA
)
163 /* operating mode constraint check */
164 static int regulator_check_mode(struct regulator_dev
*rdev
, int mode
)
167 case REGULATOR_MODE_FAST
:
168 case REGULATOR_MODE_NORMAL
:
169 case REGULATOR_MODE_IDLE
:
170 case REGULATOR_MODE_STANDBY
:
176 if (!rdev
->constraints
) {
177 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
178 rdev_get_name(rdev
));
181 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
182 printk(KERN_ERR
"%s: operation not allowed for %s\n",
183 __func__
, rdev_get_name(rdev
));
186 if (!(rdev
->constraints
->valid_modes_mask
& mode
)) {
187 printk(KERN_ERR
"%s: invalid mode %x for %s\n",
188 __func__
, mode
, rdev_get_name(rdev
));
194 /* dynamic regulator mode switching constraint check */
195 static int regulator_check_drms(struct regulator_dev
*rdev
)
197 if (!rdev
->constraints
) {
198 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
199 rdev_get_name(rdev
));
202 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
203 printk(KERN_ERR
"%s: operation not allowed for %s\n",
204 __func__
, rdev_get_name(rdev
));
210 static ssize_t
device_requested_uA_show(struct device
*dev
,
211 struct device_attribute
*attr
, char *buf
)
213 struct regulator
*regulator
;
215 regulator
= get_device_regulator(dev
);
216 if (regulator
== NULL
)
219 return sprintf(buf
, "%d\n", regulator
->uA_load
);
222 static ssize_t
regulator_uV_show(struct device
*dev
,
223 struct device_attribute
*attr
, char *buf
)
225 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
228 mutex_lock(&rdev
->mutex
);
229 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
230 mutex_unlock(&rdev
->mutex
);
234 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
236 static ssize_t
regulator_uA_show(struct device
*dev
,
237 struct device_attribute
*attr
, char *buf
)
239 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
241 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
243 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
245 static ssize_t
regulator_name_show(struct device
*dev
,
246 struct device_attribute
*attr
, char *buf
)
248 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
250 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
253 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
256 case REGULATOR_MODE_FAST
:
257 return sprintf(buf
, "fast\n");
258 case REGULATOR_MODE_NORMAL
:
259 return sprintf(buf
, "normal\n");
260 case REGULATOR_MODE_IDLE
:
261 return sprintf(buf
, "idle\n");
262 case REGULATOR_MODE_STANDBY
:
263 return sprintf(buf
, "standby\n");
265 return sprintf(buf
, "unknown\n");
268 static ssize_t
regulator_opmode_show(struct device
*dev
,
269 struct device_attribute
*attr
, char *buf
)
271 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
273 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
275 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
277 static ssize_t
regulator_print_state(char *buf
, int state
)
280 return sprintf(buf
, "enabled\n");
282 return sprintf(buf
, "disabled\n");
284 return sprintf(buf
, "unknown\n");
287 static ssize_t
regulator_state_show(struct device
*dev
,
288 struct device_attribute
*attr
, char *buf
)
290 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
293 mutex_lock(&rdev
->mutex
);
294 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
295 mutex_unlock(&rdev
->mutex
);
299 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
301 static ssize_t
regulator_status_show(struct device
*dev
,
302 struct device_attribute
*attr
, char *buf
)
304 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
308 status
= rdev
->desc
->ops
->get_status(rdev
);
313 case REGULATOR_STATUS_OFF
:
316 case REGULATOR_STATUS_ON
:
319 case REGULATOR_STATUS_ERROR
:
322 case REGULATOR_STATUS_FAST
:
325 case REGULATOR_STATUS_NORMAL
:
328 case REGULATOR_STATUS_IDLE
:
331 case REGULATOR_STATUS_STANDBY
:
338 return sprintf(buf
, "%s\n", label
);
340 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
342 static ssize_t
regulator_min_uA_show(struct device
*dev
,
343 struct device_attribute
*attr
, char *buf
)
345 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
347 if (!rdev
->constraints
)
348 return sprintf(buf
, "constraint not defined\n");
350 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
352 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
354 static ssize_t
regulator_max_uA_show(struct device
*dev
,
355 struct device_attribute
*attr
, char *buf
)
357 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
359 if (!rdev
->constraints
)
360 return sprintf(buf
, "constraint not defined\n");
362 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
364 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
366 static ssize_t
regulator_min_uV_show(struct device
*dev
,
367 struct device_attribute
*attr
, char *buf
)
369 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
371 if (!rdev
->constraints
)
372 return sprintf(buf
, "constraint not defined\n");
374 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
376 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
378 static ssize_t
regulator_max_uV_show(struct device
*dev
,
379 struct device_attribute
*attr
, char *buf
)
381 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
383 if (!rdev
->constraints
)
384 return sprintf(buf
, "constraint not defined\n");
386 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
388 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
390 static ssize_t
regulator_total_uA_show(struct device
*dev
,
391 struct device_attribute
*attr
, char *buf
)
393 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
394 struct regulator
*regulator
;
397 mutex_lock(&rdev
->mutex
);
398 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
399 uA
+= regulator
->uA_load
;
400 mutex_unlock(&rdev
->mutex
);
401 return sprintf(buf
, "%d\n", uA
);
403 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
405 static ssize_t
regulator_num_users_show(struct device
*dev
,
406 struct device_attribute
*attr
, char *buf
)
408 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
409 return sprintf(buf
, "%d\n", rdev
->use_count
);
412 static ssize_t
regulator_type_show(struct device
*dev
,
413 struct device_attribute
*attr
, char *buf
)
415 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
417 switch (rdev
->desc
->type
) {
418 case REGULATOR_VOLTAGE
:
419 return sprintf(buf
, "voltage\n");
420 case REGULATOR_CURRENT
:
421 return sprintf(buf
, "current\n");
423 return sprintf(buf
, "unknown\n");
426 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
427 struct device_attribute
*attr
, char *buf
)
429 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
431 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
433 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
434 regulator_suspend_mem_uV_show
, NULL
);
436 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
437 struct device_attribute
*attr
, char *buf
)
439 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
441 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
443 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
444 regulator_suspend_disk_uV_show
, NULL
);
446 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
447 struct device_attribute
*attr
, char *buf
)
449 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
451 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
453 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
454 regulator_suspend_standby_uV_show
, NULL
);
456 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
457 struct device_attribute
*attr
, char *buf
)
459 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
461 return regulator_print_opmode(buf
,
462 rdev
->constraints
->state_mem
.mode
);
464 static DEVICE_ATTR(suspend_mem_mode
, 0444,
465 regulator_suspend_mem_mode_show
, NULL
);
467 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
468 struct device_attribute
*attr
, char *buf
)
470 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
472 return regulator_print_opmode(buf
,
473 rdev
->constraints
->state_disk
.mode
);
475 static DEVICE_ATTR(suspend_disk_mode
, 0444,
476 regulator_suspend_disk_mode_show
, NULL
);
478 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
479 struct device_attribute
*attr
, char *buf
)
481 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
483 return regulator_print_opmode(buf
,
484 rdev
->constraints
->state_standby
.mode
);
486 static DEVICE_ATTR(suspend_standby_mode
, 0444,
487 regulator_suspend_standby_mode_show
, NULL
);
489 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
490 struct device_attribute
*attr
, char *buf
)
492 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
494 return regulator_print_state(buf
,
495 rdev
->constraints
->state_mem
.enabled
);
497 static DEVICE_ATTR(suspend_mem_state
, 0444,
498 regulator_suspend_mem_state_show
, NULL
);
500 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
501 struct device_attribute
*attr
, char *buf
)
503 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
505 return regulator_print_state(buf
,
506 rdev
->constraints
->state_disk
.enabled
);
508 static DEVICE_ATTR(suspend_disk_state
, 0444,
509 regulator_suspend_disk_state_show
, NULL
);
511 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
512 struct device_attribute
*attr
, char *buf
)
514 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
516 return regulator_print_state(buf
,
517 rdev
->constraints
->state_standby
.enabled
);
519 static DEVICE_ATTR(suspend_standby_state
, 0444,
520 regulator_suspend_standby_state_show
, NULL
);
524 * These are the only attributes are present for all regulators.
525 * Other attributes are a function of regulator functionality.
527 static struct device_attribute regulator_dev_attrs
[] = {
528 __ATTR(name
, 0444, regulator_name_show
, NULL
),
529 __ATTR(num_users
, 0444, regulator_num_users_show
, NULL
),
530 __ATTR(type
, 0444, regulator_type_show
, NULL
),
534 static void regulator_dev_release(struct device
*dev
)
536 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
540 static struct class regulator_class
= {
542 .dev_release
= regulator_dev_release
,
543 .dev_attrs
= regulator_dev_attrs
,
546 /* Calculate the new optimum regulator operating mode based on the new total
547 * consumer load. All locks held by caller */
548 static void drms_uA_update(struct regulator_dev
*rdev
)
550 struct regulator
*sibling
;
551 int current_uA
= 0, output_uV
, input_uV
, err
;
554 err
= regulator_check_drms(rdev
);
555 if (err
< 0 || !rdev
->desc
->ops
->get_optimum_mode
||
556 !rdev
->desc
->ops
->get_voltage
|| !rdev
->desc
->ops
->set_mode
)
559 /* get output voltage */
560 output_uV
= rdev
->desc
->ops
->get_voltage(rdev
);
564 /* get input voltage */
565 if (rdev
->supply
&& rdev
->supply
->desc
->ops
->get_voltage
)
566 input_uV
= rdev
->supply
->desc
->ops
->get_voltage(rdev
->supply
);
568 input_uV
= rdev
->constraints
->input_uV
;
572 /* calc total requested load */
573 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
574 current_uA
+= sibling
->uA_load
;
576 /* now get the optimum mode for our new total regulator load */
577 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
578 output_uV
, current_uA
);
580 /* check the new mode is allowed */
581 err
= regulator_check_mode(rdev
, mode
);
583 rdev
->desc
->ops
->set_mode(rdev
, mode
);
586 static int suspend_set_state(struct regulator_dev
*rdev
,
587 struct regulator_state
*rstate
)
592 can_set_state
= rdev
->desc
->ops
->set_suspend_enable
&&
593 rdev
->desc
->ops
->set_suspend_disable
;
595 /* If we have no suspend mode configration don't set anything;
596 * only warn if the driver actually makes the suspend mode
599 if (!rstate
->enabled
&& !rstate
->disabled
) {
601 printk(KERN_WARNING
"%s: No configuration for %s\n",
602 __func__
, rdev_get_name(rdev
));
606 if (rstate
->enabled
&& rstate
->disabled
) {
607 printk(KERN_ERR
"%s: invalid configuration for %s\n",
608 __func__
, rdev_get_name(rdev
));
612 if (!can_set_state
) {
613 printk(KERN_ERR
"%s: no way to set suspend state\n",
619 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
621 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
623 printk(KERN_ERR
"%s: failed to enabled/disable\n", __func__
);
627 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
628 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
630 printk(KERN_ERR
"%s: failed to set voltage\n",
636 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
637 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
639 printk(KERN_ERR
"%s: failed to set mode\n", __func__
);
646 /* locks held by caller */
647 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
649 if (!rdev
->constraints
)
653 case PM_SUSPEND_STANDBY
:
654 return suspend_set_state(rdev
,
655 &rdev
->constraints
->state_standby
);
657 return suspend_set_state(rdev
,
658 &rdev
->constraints
->state_mem
);
660 return suspend_set_state(rdev
,
661 &rdev
->constraints
->state_disk
);
667 static void print_constraints(struct regulator_dev
*rdev
)
669 struct regulation_constraints
*constraints
= rdev
->constraints
;
674 if (constraints
->min_uV
&& constraints
->max_uV
) {
675 if (constraints
->min_uV
== constraints
->max_uV
)
676 count
+= sprintf(buf
+ count
, "%d mV ",
677 constraints
->min_uV
/ 1000);
679 count
+= sprintf(buf
+ count
, "%d <--> %d mV ",
680 constraints
->min_uV
/ 1000,
681 constraints
->max_uV
/ 1000);
684 if (!constraints
->min_uV
||
685 constraints
->min_uV
!= constraints
->max_uV
) {
686 ret
= _regulator_get_voltage(rdev
);
688 count
+= sprintf(buf
+ count
, "at %d mV ", ret
/ 1000);
691 if (constraints
->min_uA
&& constraints
->max_uA
) {
692 if (constraints
->min_uA
== constraints
->max_uA
)
693 count
+= sprintf(buf
+ count
, "%d mA ",
694 constraints
->min_uA
/ 1000);
696 count
+= sprintf(buf
+ count
, "%d <--> %d mA ",
697 constraints
->min_uA
/ 1000,
698 constraints
->max_uA
/ 1000);
701 if (!constraints
->min_uA
||
702 constraints
->min_uA
!= constraints
->max_uA
) {
703 ret
= _regulator_get_current_limit(rdev
);
705 count
+= sprintf(buf
+ count
, "at %d mA ", ret
/ 1000);
708 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
709 count
+= sprintf(buf
+ count
, "fast ");
710 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
711 count
+= sprintf(buf
+ count
, "normal ");
712 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
713 count
+= sprintf(buf
+ count
, "idle ");
714 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
715 count
+= sprintf(buf
+ count
, "standby");
717 printk(KERN_INFO
"regulator: %s: %s\n", rdev_get_name(rdev
), buf
);
720 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
721 struct regulation_constraints
*constraints
)
723 struct regulator_ops
*ops
= rdev
->desc
->ops
;
724 const char *name
= rdev_get_name(rdev
);
727 /* do we need to apply the constraint voltage */
728 if (rdev
->constraints
->apply_uV
&&
729 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
&&
731 ret
= ops
->set_voltage(rdev
,
732 rdev
->constraints
->min_uV
, rdev
->constraints
->max_uV
);
734 printk(KERN_ERR
"%s: failed to apply %duV constraint to %s\n",
736 rdev
->constraints
->min_uV
, name
);
737 rdev
->constraints
= NULL
;
742 /* constrain machine-level voltage specs to fit
743 * the actual range supported by this regulator.
745 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
746 int count
= rdev
->desc
->n_voltages
;
748 int min_uV
= INT_MAX
;
749 int max_uV
= INT_MIN
;
750 int cmin
= constraints
->min_uV
;
751 int cmax
= constraints
->max_uV
;
753 /* it's safe to autoconfigure fixed-voltage supplies
754 and the constraints are used by list_voltage. */
755 if (count
== 1 && !cmin
) {
758 constraints
->min_uV
= cmin
;
759 constraints
->max_uV
= cmax
;
762 /* voltage constraints are optional */
763 if ((cmin
== 0) && (cmax
== 0))
766 /* else require explicit machine-level constraints */
767 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
768 pr_err("%s: %s '%s' voltage constraints\n",
769 __func__
, "invalid", name
);
773 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
774 for (i
= 0; i
< count
; i
++) {
777 value
= ops
->list_voltage(rdev
, i
);
781 /* maybe adjust [min_uV..max_uV] */
782 if (value
>= cmin
&& value
< min_uV
)
784 if (value
<= cmax
&& value
> max_uV
)
788 /* final: [min_uV..max_uV] valid iff constraints valid */
789 if (max_uV
< min_uV
) {
790 pr_err("%s: %s '%s' voltage constraints\n",
791 __func__
, "unsupportable", name
);
795 /* use regulator's subset of machine constraints */
796 if (constraints
->min_uV
< min_uV
) {
797 pr_debug("%s: override '%s' %s, %d -> %d\n",
798 __func__
, name
, "min_uV",
799 constraints
->min_uV
, min_uV
);
800 constraints
->min_uV
= min_uV
;
802 if (constraints
->max_uV
> max_uV
) {
803 pr_debug("%s: override '%s' %s, %d -> %d\n",
804 __func__
, name
, "max_uV",
805 constraints
->max_uV
, max_uV
);
806 constraints
->max_uV
= max_uV
;
814 * set_machine_constraints - sets regulator constraints
815 * @rdev: regulator source
816 * @constraints: constraints to apply
818 * Allows platform initialisation code to define and constrain
819 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
820 * Constraints *must* be set by platform code in order for some
821 * regulator operations to proceed i.e. set_voltage, set_current_limit,
824 static int set_machine_constraints(struct regulator_dev
*rdev
,
825 struct regulation_constraints
*constraints
)
829 struct regulator_ops
*ops
= rdev
->desc
->ops
;
831 rdev
->constraints
= constraints
;
833 name
= rdev_get_name(rdev
);
835 ret
= machine_constraints_voltage(rdev
, constraints
);
839 /* do we need to setup our suspend state */
840 if (constraints
->initial_state
) {
841 ret
= suspend_prepare(rdev
, constraints
->initial_state
);
843 printk(KERN_ERR
"%s: failed to set suspend state for %s\n",
845 rdev
->constraints
= NULL
;
850 if (constraints
->initial_mode
) {
851 if (!ops
->set_mode
) {
852 printk(KERN_ERR
"%s: no set_mode operation for %s\n",
858 ret
= ops
->set_mode(rdev
, constraints
->initial_mode
);
861 "%s: failed to set initial mode for %s: %d\n",
862 __func__
, name
, ret
);
867 /* If the constraints say the regulator should be on at this point
868 * and we have control then make sure it is enabled.
870 if ((constraints
->always_on
|| constraints
->boot_on
) && ops
->enable
) {
871 ret
= ops
->enable(rdev
);
873 printk(KERN_ERR
"%s: failed to enable %s\n",
875 rdev
->constraints
= NULL
;
880 print_constraints(rdev
);
886 * set_supply - set regulator supply regulator
887 * @rdev: regulator name
888 * @supply_rdev: supply regulator name
890 * Called by platform initialisation code to set the supply regulator for this
891 * regulator. This ensures that a regulators supply will also be enabled by the
892 * core if it's child is enabled.
894 static int set_supply(struct regulator_dev
*rdev
,
895 struct regulator_dev
*supply_rdev
)
899 err
= sysfs_create_link(&rdev
->dev
.kobj
, &supply_rdev
->dev
.kobj
,
903 "%s: could not add device link %s err %d\n",
904 __func__
, supply_rdev
->dev
.kobj
.name
, err
);
907 rdev
->supply
= supply_rdev
;
908 list_add(&rdev
->slist
, &supply_rdev
->supply_list
);
914 * set_consumer_device_supply: Bind a regulator to a symbolic supply
915 * @rdev: regulator source
916 * @consumer_dev: device the supply applies to
917 * @consumer_dev_name: dev_name() string for device supply applies to
918 * @supply: symbolic name for supply
920 * Allows platform initialisation code to map physical regulator
921 * sources to symbolic names for supplies for use by devices. Devices
922 * should use these symbolic names to request regulators, avoiding the
923 * need to provide board-specific regulator names as platform data.
925 * Only one of consumer_dev and consumer_dev_name may be specified.
927 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
928 struct device
*consumer_dev
, const char *consumer_dev_name
,
931 struct regulator_map
*node
;
934 if (consumer_dev
&& consumer_dev_name
)
937 if (!consumer_dev_name
&& consumer_dev
)
938 consumer_dev_name
= dev_name(consumer_dev
);
943 if (consumer_dev_name
!= NULL
)
948 list_for_each_entry(node
, ®ulator_map_list
, list
) {
949 if (node
->dev_name
&& consumer_dev_name
) {
950 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
952 } else if (node
->dev_name
|| consumer_dev_name
) {
956 if (strcmp(node
->supply
, supply
) != 0)
959 dev_dbg(consumer_dev
, "%s/%s is '%s' supply; fail %s/%s\n",
960 dev_name(&node
->regulator
->dev
),
961 node
->regulator
->desc
->name
,
963 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
967 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
971 node
->regulator
= rdev
;
972 node
->supply
= supply
;
975 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
976 if (node
->dev_name
== NULL
) {
982 list_add(&node
->list
, ®ulator_map_list
);
986 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
988 struct regulator_map
*node
, *n
;
990 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
991 if (rdev
== node
->regulator
) {
992 list_del(&node
->list
);
993 kfree(node
->dev_name
);
999 #define REG_STR_SIZE 32
1001 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1003 const char *supply_name
)
1005 struct regulator
*regulator
;
1006 char buf
[REG_STR_SIZE
];
1009 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1010 if (regulator
== NULL
)
1013 mutex_lock(&rdev
->mutex
);
1014 regulator
->rdev
= rdev
;
1015 list_add(®ulator
->list
, &rdev
->consumer_list
);
1018 /* create a 'requested_microamps_name' sysfs entry */
1019 size
= scnprintf(buf
, REG_STR_SIZE
, "microamps_requested_%s",
1021 if (size
>= REG_STR_SIZE
)
1024 regulator
->dev
= dev
;
1025 sysfs_attr_init(®ulator
->dev_attr
.attr
);
1026 regulator
->dev_attr
.attr
.name
= kstrdup(buf
, GFP_KERNEL
);
1027 if (regulator
->dev_attr
.attr
.name
== NULL
)
1030 regulator
->dev_attr
.attr
.mode
= 0444;
1031 regulator
->dev_attr
.show
= device_requested_uA_show
;
1032 err
= device_create_file(dev
, ®ulator
->dev_attr
);
1034 printk(KERN_WARNING
"%s: could not add regulator_dev"
1035 " load sysfs\n", __func__
);
1039 /* also add a link to the device sysfs entry */
1040 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1041 dev
->kobj
.name
, supply_name
);
1042 if (size
>= REG_STR_SIZE
)
1045 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1046 if (regulator
->supply_name
== NULL
)
1049 err
= sysfs_create_link(&rdev
->dev
.kobj
, &dev
->kobj
,
1053 "%s: could not add device link %s err %d\n",
1054 __func__
, dev
->kobj
.name
, err
);
1055 device_remove_file(dev
, ®ulator
->dev_attr
);
1059 mutex_unlock(&rdev
->mutex
);
1062 kfree(regulator
->supply_name
);
1064 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1066 kfree(regulator
->dev_attr
.attr
.name
);
1068 list_del(®ulator
->list
);
1070 mutex_unlock(&rdev
->mutex
);
1074 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1076 if (!rdev
->desc
->ops
->enable_time
)
1078 return rdev
->desc
->ops
->enable_time(rdev
);
1081 /* Internal regulator request function */
1082 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1085 struct regulator_dev
*rdev
;
1086 struct regulator_map
*map
;
1087 struct regulator
*regulator
= ERR_PTR(-ENODEV
);
1088 const char *devname
= NULL
;
1092 printk(KERN_ERR
"regulator: get() with no identifier\n");
1097 devname
= dev_name(dev
);
1099 mutex_lock(®ulator_list_mutex
);
1101 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1102 /* If the mapping has a device set up it must match */
1103 if (map
->dev_name
&&
1104 (!devname
|| strcmp(map
->dev_name
, devname
)))
1107 if (strcmp(map
->supply
, id
) == 0) {
1108 rdev
= map
->regulator
;
1113 if (board_wants_dummy_regulator
) {
1114 rdev
= dummy_regulator_rdev
;
1118 #ifdef CONFIG_REGULATOR_DUMMY
1120 devname
= "deviceless";
1122 /* If the board didn't flag that it was fully constrained then
1123 * substitute in a dummy regulator so consumers can continue.
1125 if (!has_full_constraints
) {
1126 pr_warning("%s supply %s not found, using dummy regulator\n",
1128 rdev
= dummy_regulator_rdev
;
1133 mutex_unlock(®ulator_list_mutex
);
1137 if (rdev
->exclusive
) {
1138 regulator
= ERR_PTR(-EPERM
);
1142 if (exclusive
&& rdev
->open_count
) {
1143 regulator
= ERR_PTR(-EBUSY
);
1147 if (!try_module_get(rdev
->owner
))
1150 regulator
= create_regulator(rdev
, dev
, id
);
1151 if (regulator
== NULL
) {
1152 regulator
= ERR_PTR(-ENOMEM
);
1153 module_put(rdev
->owner
);
1158 rdev
->exclusive
= 1;
1160 ret
= _regulator_is_enabled(rdev
);
1162 rdev
->use_count
= 1;
1164 rdev
->use_count
= 0;
1168 mutex_unlock(®ulator_list_mutex
);
1174 * regulator_get - lookup and obtain a reference to a regulator.
1175 * @dev: device for regulator "consumer"
1176 * @id: Supply name or regulator ID.
1178 * Returns a struct regulator corresponding to the regulator producer,
1179 * or IS_ERR() condition containing errno.
1181 * Use of supply names configured via regulator_set_device_supply() is
1182 * strongly encouraged. It is recommended that the supply name used
1183 * should match the name used for the supply and/or the relevant
1184 * device pins in the datasheet.
1186 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1188 return _regulator_get(dev
, id
, 0);
1190 EXPORT_SYMBOL_GPL(regulator_get
);
1193 * regulator_get_exclusive - obtain exclusive access to a regulator.
1194 * @dev: device for regulator "consumer"
1195 * @id: Supply name or regulator ID.
1197 * Returns a struct regulator corresponding to the regulator producer,
1198 * or IS_ERR() condition containing errno. Other consumers will be
1199 * unable to obtain this reference is held and the use count for the
1200 * regulator will be initialised to reflect the current state of the
1203 * This is intended for use by consumers which cannot tolerate shared
1204 * use of the regulator such as those which need to force the
1205 * regulator off for correct operation of the hardware they are
1208 * Use of supply names configured via regulator_set_device_supply() is
1209 * strongly encouraged. It is recommended that the supply name used
1210 * should match the name used for the supply and/or the relevant
1211 * device pins in the datasheet.
1213 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1215 return _regulator_get(dev
, id
, 1);
1217 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1220 * regulator_put - "free" the regulator source
1221 * @regulator: regulator source
1223 * Note: drivers must ensure that all regulator_enable calls made on this
1224 * regulator source are balanced by regulator_disable calls prior to calling
1227 void regulator_put(struct regulator
*regulator
)
1229 struct regulator_dev
*rdev
;
1231 if (regulator
== NULL
|| IS_ERR(regulator
))
1234 mutex_lock(®ulator_list_mutex
);
1235 rdev
= regulator
->rdev
;
1237 /* remove any sysfs entries */
1238 if (regulator
->dev
) {
1239 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1240 kfree(regulator
->supply_name
);
1241 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1242 kfree(regulator
->dev_attr
.attr
.name
);
1244 list_del(®ulator
->list
);
1248 rdev
->exclusive
= 0;
1250 module_put(rdev
->owner
);
1251 mutex_unlock(®ulator_list_mutex
);
1253 EXPORT_SYMBOL_GPL(regulator_put
);
1255 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
1257 if (!rdev
->constraints
)
1260 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
1266 /* locks held by regulator_enable() */
1267 static int _regulator_enable(struct regulator_dev
*rdev
)
1271 /* do we need to enable the supply regulator first */
1273 ret
= _regulator_enable(rdev
->supply
);
1275 printk(KERN_ERR
"%s: failed to enable %s: %d\n",
1276 __func__
, rdev_get_name(rdev
), ret
);
1281 /* check voltage and requested load before enabling */
1282 if (rdev
->constraints
&&
1283 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1284 drms_uA_update(rdev
);
1286 if (rdev
->use_count
== 0) {
1287 /* The regulator may on if it's not switchable or left on */
1288 ret
= _regulator_is_enabled(rdev
);
1289 if (ret
== -EINVAL
|| ret
== 0) {
1290 if (!_regulator_can_change_status(rdev
))
1293 if (!rdev
->desc
->ops
->enable
)
1296 /* Query before enabling in case configuration
1298 ret
= _regulator_get_enable_time(rdev
);
1303 "%s: enable_time() failed for %s: %d\n",
1304 __func__
, rdev_get_name(rdev
),
1309 /* Allow the regulator to ramp; it would be useful
1310 * to extend this for bulk operations so that the
1311 * regulators can ramp together. */
1312 ret
= rdev
->desc
->ops
->enable(rdev
);
1317 mdelay(delay
/ 1000);
1321 } else if (ret
< 0) {
1322 printk(KERN_ERR
"%s: is_enabled() failed for %s: %d\n",
1323 __func__
, rdev_get_name(rdev
), ret
);
1326 /* Fallthrough on positive return values - already enabled */
1335 * regulator_enable - enable regulator output
1336 * @regulator: regulator source
1338 * Request that the regulator be enabled with the regulator output at
1339 * the predefined voltage or current value. Calls to regulator_enable()
1340 * must be balanced with calls to regulator_disable().
1342 * NOTE: the output value can be set by other drivers, boot loader or may be
1343 * hardwired in the regulator.
1345 int regulator_enable(struct regulator
*regulator
)
1347 struct regulator_dev
*rdev
= regulator
->rdev
;
1350 mutex_lock(&rdev
->mutex
);
1351 ret
= _regulator_enable(rdev
);
1352 mutex_unlock(&rdev
->mutex
);
1355 EXPORT_SYMBOL_GPL(regulator_enable
);
1357 /* locks held by regulator_disable() */
1358 static int _regulator_disable(struct regulator_dev
*rdev
,
1359 struct regulator_dev
**supply_rdev_ptr
)
1363 if (WARN(rdev
->use_count
<= 0,
1364 "unbalanced disables for %s\n",
1365 rdev_get_name(rdev
)))
1368 /* are we the last user and permitted to disable ? */
1369 if (rdev
->use_count
== 1 &&
1370 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
1372 /* we are last user */
1373 if (_regulator_can_change_status(rdev
) &&
1374 rdev
->desc
->ops
->disable
) {
1375 ret
= rdev
->desc
->ops
->disable(rdev
);
1377 printk(KERN_ERR
"%s: failed to disable %s\n",
1378 __func__
, rdev_get_name(rdev
));
1382 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
1386 /* decrease our supplies ref count and disable if required */
1387 *supply_rdev_ptr
= rdev
->supply
;
1389 rdev
->use_count
= 0;
1390 } else if (rdev
->use_count
> 1) {
1392 if (rdev
->constraints
&&
1393 (rdev
->constraints
->valid_ops_mask
&
1394 REGULATOR_CHANGE_DRMS
))
1395 drms_uA_update(rdev
);
1403 * regulator_disable - disable regulator output
1404 * @regulator: regulator source
1406 * Disable the regulator output voltage or current. Calls to
1407 * regulator_enable() must be balanced with calls to
1408 * regulator_disable().
1410 * NOTE: this will only disable the regulator output if no other consumer
1411 * devices have it enabled, the regulator device supports disabling and
1412 * machine constraints permit this operation.
1414 int regulator_disable(struct regulator
*regulator
)
1416 struct regulator_dev
*rdev
= regulator
->rdev
;
1417 struct regulator_dev
*supply_rdev
= NULL
;
1420 mutex_lock(&rdev
->mutex
);
1421 ret
= _regulator_disable(rdev
, &supply_rdev
);
1422 mutex_unlock(&rdev
->mutex
);
1424 /* decrease our supplies ref count and disable if required */
1425 while (supply_rdev
!= NULL
) {
1428 mutex_lock(&rdev
->mutex
);
1429 _regulator_disable(rdev
, &supply_rdev
);
1430 mutex_unlock(&rdev
->mutex
);
1435 EXPORT_SYMBOL_GPL(regulator_disable
);
1437 /* locks held by regulator_force_disable() */
1438 static int _regulator_force_disable(struct regulator_dev
*rdev
,
1439 struct regulator_dev
**supply_rdev_ptr
)
1444 if (rdev
->desc
->ops
->disable
) {
1445 /* ah well, who wants to live forever... */
1446 ret
= rdev
->desc
->ops
->disable(rdev
);
1448 printk(KERN_ERR
"%s: failed to force disable %s\n",
1449 __func__
, rdev_get_name(rdev
));
1452 /* notify other consumers that power has been forced off */
1453 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
1454 REGULATOR_EVENT_DISABLE
, NULL
);
1457 /* decrease our supplies ref count and disable if required */
1458 *supply_rdev_ptr
= rdev
->supply
;
1460 rdev
->use_count
= 0;
1465 * regulator_force_disable - force disable regulator output
1466 * @regulator: regulator source
1468 * Forcibly disable the regulator output voltage or current.
1469 * NOTE: this *will* disable the regulator output even if other consumer
1470 * devices have it enabled. This should be used for situations when device
1471 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1473 int regulator_force_disable(struct regulator
*regulator
)
1475 struct regulator_dev
*supply_rdev
= NULL
;
1478 mutex_lock(®ulator
->rdev
->mutex
);
1479 regulator
->uA_load
= 0;
1480 ret
= _regulator_force_disable(regulator
->rdev
, &supply_rdev
);
1481 mutex_unlock(®ulator
->rdev
->mutex
);
1484 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev
)));
1488 EXPORT_SYMBOL_GPL(regulator_force_disable
);
1490 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
1492 /* If we don't know then assume that the regulator is always on */
1493 if (!rdev
->desc
->ops
->is_enabled
)
1496 return rdev
->desc
->ops
->is_enabled(rdev
);
1500 * regulator_is_enabled - is the regulator output enabled
1501 * @regulator: regulator source
1503 * Returns positive if the regulator driver backing the source/client
1504 * has requested that the device be enabled, zero if it hasn't, else a
1505 * negative errno code.
1507 * Note that the device backing this regulator handle can have multiple
1508 * users, so it might be enabled even if regulator_enable() was never
1509 * called for this particular source.
1511 int regulator_is_enabled(struct regulator
*regulator
)
1515 mutex_lock(®ulator
->rdev
->mutex
);
1516 ret
= _regulator_is_enabled(regulator
->rdev
);
1517 mutex_unlock(®ulator
->rdev
->mutex
);
1521 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
1524 * regulator_count_voltages - count regulator_list_voltage() selectors
1525 * @regulator: regulator source
1527 * Returns number of selectors, or negative errno. Selectors are
1528 * numbered starting at zero, and typically correspond to bitfields
1529 * in hardware registers.
1531 int regulator_count_voltages(struct regulator
*regulator
)
1533 struct regulator_dev
*rdev
= regulator
->rdev
;
1535 return rdev
->desc
->n_voltages
? : -EINVAL
;
1537 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
1540 * regulator_list_voltage - enumerate supported voltages
1541 * @regulator: regulator source
1542 * @selector: identify voltage to list
1543 * Context: can sleep
1545 * Returns a voltage that can be passed to @regulator_set_voltage(),
1546 * zero if this selector code can't be used on this system, or a
1549 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
1551 struct regulator_dev
*rdev
= regulator
->rdev
;
1552 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1555 if (!ops
->list_voltage
|| selector
>= rdev
->desc
->n_voltages
)
1558 mutex_lock(&rdev
->mutex
);
1559 ret
= ops
->list_voltage(rdev
, selector
);
1560 mutex_unlock(&rdev
->mutex
);
1563 if (ret
< rdev
->constraints
->min_uV
)
1565 else if (ret
> rdev
->constraints
->max_uV
)
1571 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
1574 * regulator_is_supported_voltage - check if a voltage range can be supported
1576 * @regulator: Regulator to check.
1577 * @min_uV: Minimum required voltage in uV.
1578 * @max_uV: Maximum required voltage in uV.
1580 * Returns a boolean or a negative error code.
1582 int regulator_is_supported_voltage(struct regulator
*regulator
,
1583 int min_uV
, int max_uV
)
1585 int i
, voltages
, ret
;
1587 ret
= regulator_count_voltages(regulator
);
1592 for (i
= 0; i
< voltages
; i
++) {
1593 ret
= regulator_list_voltage(regulator
, i
);
1595 if (ret
>= min_uV
&& ret
<= max_uV
)
1603 * regulator_set_voltage - set regulator output voltage
1604 * @regulator: regulator source
1605 * @min_uV: Minimum required voltage in uV
1606 * @max_uV: Maximum acceptable voltage in uV
1608 * Sets a voltage regulator to the desired output voltage. This can be set
1609 * during any regulator state. IOW, regulator can be disabled or enabled.
1611 * If the regulator is enabled then the voltage will change to the new value
1612 * immediately otherwise if the regulator is disabled the regulator will
1613 * output at the new voltage when enabled.
1615 * NOTE: If the regulator is shared between several devices then the lowest
1616 * request voltage that meets the system constraints will be used.
1617 * Regulator system constraints must be set for this regulator before
1618 * calling this function otherwise this call will fail.
1620 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
1622 struct regulator_dev
*rdev
= regulator
->rdev
;
1625 mutex_lock(&rdev
->mutex
);
1628 if (!rdev
->desc
->ops
->set_voltage
) {
1633 /* constraints check */
1634 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1637 regulator
->min_uV
= min_uV
;
1638 regulator
->max_uV
= max_uV
;
1639 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
);
1642 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
, NULL
);
1643 mutex_unlock(&rdev
->mutex
);
1646 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
1648 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
1651 if (rdev
->desc
->ops
->get_voltage
)
1652 return rdev
->desc
->ops
->get_voltage(rdev
);
1658 * regulator_get_voltage - get regulator output voltage
1659 * @regulator: regulator source
1661 * This returns the current regulator voltage in uV.
1663 * NOTE: If the regulator is disabled it will return the voltage value. This
1664 * function should not be used to determine regulator state.
1666 int regulator_get_voltage(struct regulator
*regulator
)
1670 mutex_lock(®ulator
->rdev
->mutex
);
1672 ret
= _regulator_get_voltage(regulator
->rdev
);
1674 mutex_unlock(®ulator
->rdev
->mutex
);
1678 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
1681 * regulator_set_current_limit - set regulator output current limit
1682 * @regulator: regulator source
1683 * @min_uA: Minimuum supported current in uA
1684 * @max_uA: Maximum supported current in uA
1686 * Sets current sink to the desired output current. This can be set during
1687 * any regulator state. IOW, regulator can be disabled or enabled.
1689 * If the regulator is enabled then the current will change to the new value
1690 * immediately otherwise if the regulator is disabled the regulator will
1691 * output at the new current when enabled.
1693 * NOTE: Regulator system constraints must be set for this regulator before
1694 * calling this function otherwise this call will fail.
1696 int regulator_set_current_limit(struct regulator
*regulator
,
1697 int min_uA
, int max_uA
)
1699 struct regulator_dev
*rdev
= regulator
->rdev
;
1702 mutex_lock(&rdev
->mutex
);
1705 if (!rdev
->desc
->ops
->set_current_limit
) {
1710 /* constraints check */
1711 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
1715 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
1717 mutex_unlock(&rdev
->mutex
);
1720 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
1722 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
1726 mutex_lock(&rdev
->mutex
);
1729 if (!rdev
->desc
->ops
->get_current_limit
) {
1734 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
1736 mutex_unlock(&rdev
->mutex
);
1741 * regulator_get_current_limit - get regulator output current
1742 * @regulator: regulator source
1744 * This returns the current supplied by the specified current sink in uA.
1746 * NOTE: If the regulator is disabled it will return the current value. This
1747 * function should not be used to determine regulator state.
1749 int regulator_get_current_limit(struct regulator
*regulator
)
1751 return _regulator_get_current_limit(regulator
->rdev
);
1753 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
1756 * regulator_set_mode - set regulator operating mode
1757 * @regulator: regulator source
1758 * @mode: operating mode - one of the REGULATOR_MODE constants
1760 * Set regulator operating mode to increase regulator efficiency or improve
1761 * regulation performance.
1763 * NOTE: Regulator system constraints must be set for this regulator before
1764 * calling this function otherwise this call will fail.
1766 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
1768 struct regulator_dev
*rdev
= regulator
->rdev
;
1770 int regulator_curr_mode
;
1772 mutex_lock(&rdev
->mutex
);
1775 if (!rdev
->desc
->ops
->set_mode
) {
1780 /* return if the same mode is requested */
1781 if (rdev
->desc
->ops
->get_mode
) {
1782 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
1783 if (regulator_curr_mode
== mode
) {
1789 /* constraints check */
1790 ret
= regulator_check_mode(rdev
, mode
);
1794 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1796 mutex_unlock(&rdev
->mutex
);
1799 EXPORT_SYMBOL_GPL(regulator_set_mode
);
1801 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
1805 mutex_lock(&rdev
->mutex
);
1808 if (!rdev
->desc
->ops
->get_mode
) {
1813 ret
= rdev
->desc
->ops
->get_mode(rdev
);
1815 mutex_unlock(&rdev
->mutex
);
1820 * regulator_get_mode - get regulator operating mode
1821 * @regulator: regulator source
1823 * Get the current regulator operating mode.
1825 unsigned int regulator_get_mode(struct regulator
*regulator
)
1827 return _regulator_get_mode(regulator
->rdev
);
1829 EXPORT_SYMBOL_GPL(regulator_get_mode
);
1832 * regulator_set_optimum_mode - set regulator optimum operating mode
1833 * @regulator: regulator source
1834 * @uA_load: load current
1836 * Notifies the regulator core of a new device load. This is then used by
1837 * DRMS (if enabled by constraints) to set the most efficient regulator
1838 * operating mode for the new regulator loading.
1840 * Consumer devices notify their supply regulator of the maximum power
1841 * they will require (can be taken from device datasheet in the power
1842 * consumption tables) when they change operational status and hence power
1843 * state. Examples of operational state changes that can affect power
1844 * consumption are :-
1846 * o Device is opened / closed.
1847 * o Device I/O is about to begin or has just finished.
1848 * o Device is idling in between work.
1850 * This information is also exported via sysfs to userspace.
1852 * DRMS will sum the total requested load on the regulator and change
1853 * to the most efficient operating mode if platform constraints allow.
1855 * Returns the new regulator mode or error.
1857 int regulator_set_optimum_mode(struct regulator
*regulator
, int uA_load
)
1859 struct regulator_dev
*rdev
= regulator
->rdev
;
1860 struct regulator
*consumer
;
1861 int ret
, output_uV
, input_uV
, total_uA_load
= 0;
1864 mutex_lock(&rdev
->mutex
);
1866 regulator
->uA_load
= uA_load
;
1867 ret
= regulator_check_drms(rdev
);
1873 if (!rdev
->desc
->ops
->get_optimum_mode
)
1876 /* get output voltage */
1877 output_uV
= rdev
->desc
->ops
->get_voltage(rdev
);
1878 if (output_uV
<= 0) {
1879 printk(KERN_ERR
"%s: invalid output voltage found for %s\n",
1880 __func__
, rdev_get_name(rdev
));
1884 /* get input voltage */
1885 if (rdev
->supply
&& rdev
->supply
->desc
->ops
->get_voltage
)
1886 input_uV
= rdev
->supply
->desc
->ops
->get_voltage(rdev
->supply
);
1888 input_uV
= rdev
->constraints
->input_uV
;
1889 if (input_uV
<= 0) {
1890 printk(KERN_ERR
"%s: invalid input voltage found for %s\n",
1891 __func__
, rdev_get_name(rdev
));
1895 /* calc total requested load for this regulator */
1896 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
)
1897 total_uA_load
+= consumer
->uA_load
;
1899 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
,
1900 input_uV
, output_uV
,
1902 ret
= regulator_check_mode(rdev
, mode
);
1904 printk(KERN_ERR
"%s: failed to get optimum mode for %s @"
1905 " %d uA %d -> %d uV\n", __func__
, rdev_get_name(rdev
),
1906 total_uA_load
, input_uV
, output_uV
);
1910 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1912 printk(KERN_ERR
"%s: failed to set optimum mode %x for %s\n",
1913 __func__
, mode
, rdev_get_name(rdev
));
1918 mutex_unlock(&rdev
->mutex
);
1921 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode
);
1924 * regulator_register_notifier - register regulator event notifier
1925 * @regulator: regulator source
1926 * @nb: notifier block
1928 * Register notifier block to receive regulator events.
1930 int regulator_register_notifier(struct regulator
*regulator
,
1931 struct notifier_block
*nb
)
1933 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
1936 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
1939 * regulator_unregister_notifier - unregister regulator event notifier
1940 * @regulator: regulator source
1941 * @nb: notifier block
1943 * Unregister regulator event notifier block.
1945 int regulator_unregister_notifier(struct regulator
*regulator
,
1946 struct notifier_block
*nb
)
1948 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
1951 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
1953 /* notify regulator consumers and downstream regulator consumers.
1954 * Note mutex must be held by caller.
1956 static void _notifier_call_chain(struct regulator_dev
*rdev
,
1957 unsigned long event
, void *data
)
1959 struct regulator_dev
*_rdev
;
1961 /* call rdev chain first */
1962 blocking_notifier_call_chain(&rdev
->notifier
, event
, NULL
);
1964 /* now notify regulator we supply */
1965 list_for_each_entry(_rdev
, &rdev
->supply_list
, slist
) {
1966 mutex_lock(&_rdev
->mutex
);
1967 _notifier_call_chain(_rdev
, event
, data
);
1968 mutex_unlock(&_rdev
->mutex
);
1973 * regulator_bulk_get - get multiple regulator consumers
1975 * @dev: Device to supply
1976 * @num_consumers: Number of consumers to register
1977 * @consumers: Configuration of consumers; clients are stored here.
1979 * @return 0 on success, an errno on failure.
1981 * This helper function allows drivers to get several regulator
1982 * consumers in one operation. If any of the regulators cannot be
1983 * acquired then any regulators that were allocated will be freed
1984 * before returning to the caller.
1986 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
1987 struct regulator_bulk_data
*consumers
)
1992 for (i
= 0; i
< num_consumers
; i
++)
1993 consumers
[i
].consumer
= NULL
;
1995 for (i
= 0; i
< num_consumers
; i
++) {
1996 consumers
[i
].consumer
= regulator_get(dev
,
1997 consumers
[i
].supply
);
1998 if (IS_ERR(consumers
[i
].consumer
)) {
1999 ret
= PTR_ERR(consumers
[i
].consumer
);
2000 dev_err(dev
, "Failed to get supply '%s': %d\n",
2001 consumers
[i
].supply
, ret
);
2002 consumers
[i
].consumer
= NULL
;
2010 for (i
= 0; i
< num_consumers
&& consumers
[i
].consumer
; i
++)
2011 regulator_put(consumers
[i
].consumer
);
2015 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
2018 * regulator_bulk_enable - enable multiple regulator consumers
2020 * @num_consumers: Number of consumers
2021 * @consumers: Consumer data; clients are stored here.
2022 * @return 0 on success, an errno on failure
2024 * This convenience API allows consumers to enable multiple regulator
2025 * clients in a single API call. If any consumers cannot be enabled
2026 * then any others that were enabled will be disabled again prior to
2029 int regulator_bulk_enable(int num_consumers
,
2030 struct regulator_bulk_data
*consumers
)
2035 for (i
= 0; i
< num_consumers
; i
++) {
2036 ret
= regulator_enable(consumers
[i
].consumer
);
2044 printk(KERN_ERR
"Failed to enable %s: %d\n", consumers
[i
].supply
, ret
);
2045 for (--i
; i
>= 0; --i
)
2046 regulator_disable(consumers
[i
].consumer
);
2050 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
2053 * regulator_bulk_disable - disable multiple regulator consumers
2055 * @num_consumers: Number of consumers
2056 * @consumers: Consumer data; clients are stored here.
2057 * @return 0 on success, an errno on failure
2059 * This convenience API allows consumers to disable multiple regulator
2060 * clients in a single API call. If any consumers cannot be enabled
2061 * then any others that were disabled will be disabled again prior to
2064 int regulator_bulk_disable(int num_consumers
,
2065 struct regulator_bulk_data
*consumers
)
2070 for (i
= 0; i
< num_consumers
; i
++) {
2071 ret
= regulator_disable(consumers
[i
].consumer
);
2079 printk(KERN_ERR
"Failed to disable %s: %d\n", consumers
[i
].supply
,
2081 for (--i
; i
>= 0; --i
)
2082 regulator_enable(consumers
[i
].consumer
);
2086 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
2089 * regulator_bulk_free - free multiple regulator consumers
2091 * @num_consumers: Number of consumers
2092 * @consumers: Consumer data; clients are stored here.
2094 * This convenience API allows consumers to free multiple regulator
2095 * clients in a single API call.
2097 void regulator_bulk_free(int num_consumers
,
2098 struct regulator_bulk_data
*consumers
)
2102 for (i
= 0; i
< num_consumers
; i
++) {
2103 regulator_put(consumers
[i
].consumer
);
2104 consumers
[i
].consumer
= NULL
;
2107 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
2110 * regulator_notifier_call_chain - call regulator event notifier
2111 * @rdev: regulator source
2112 * @event: notifier block
2113 * @data: callback-specific data.
2115 * Called by regulator drivers to notify clients a regulator event has
2116 * occurred. We also notify regulator clients downstream.
2117 * Note lock must be held by caller.
2119 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
2120 unsigned long event
, void *data
)
2122 _notifier_call_chain(rdev
, event
, data
);
2126 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
2129 * regulator_mode_to_status - convert a regulator mode into a status
2131 * @mode: Mode to convert
2133 * Convert a regulator mode into a status.
2135 int regulator_mode_to_status(unsigned int mode
)
2138 case REGULATOR_MODE_FAST
:
2139 return REGULATOR_STATUS_FAST
;
2140 case REGULATOR_MODE_NORMAL
:
2141 return REGULATOR_STATUS_NORMAL
;
2142 case REGULATOR_MODE_IDLE
:
2143 return REGULATOR_STATUS_IDLE
;
2144 case REGULATOR_STATUS_STANDBY
:
2145 return REGULATOR_STATUS_STANDBY
;
2150 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
2153 * To avoid cluttering sysfs (and memory) with useless state, only
2154 * create attributes that can be meaningfully displayed.
2156 static int add_regulator_attributes(struct regulator_dev
*rdev
)
2158 struct device
*dev
= &rdev
->dev
;
2159 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2162 /* some attributes need specific methods to be displayed */
2163 if (ops
->get_voltage
) {
2164 status
= device_create_file(dev
, &dev_attr_microvolts
);
2168 if (ops
->get_current_limit
) {
2169 status
= device_create_file(dev
, &dev_attr_microamps
);
2173 if (ops
->get_mode
) {
2174 status
= device_create_file(dev
, &dev_attr_opmode
);
2178 if (ops
->is_enabled
) {
2179 status
= device_create_file(dev
, &dev_attr_state
);
2183 if (ops
->get_status
) {
2184 status
= device_create_file(dev
, &dev_attr_status
);
2189 /* some attributes are type-specific */
2190 if (rdev
->desc
->type
== REGULATOR_CURRENT
) {
2191 status
= device_create_file(dev
, &dev_attr_requested_microamps
);
2196 /* all the other attributes exist to support constraints;
2197 * don't show them if there are no constraints, or if the
2198 * relevant supporting methods are missing.
2200 if (!rdev
->constraints
)
2203 /* constraints need specific supporting methods */
2204 if (ops
->set_voltage
) {
2205 status
= device_create_file(dev
, &dev_attr_min_microvolts
);
2208 status
= device_create_file(dev
, &dev_attr_max_microvolts
);
2212 if (ops
->set_current_limit
) {
2213 status
= device_create_file(dev
, &dev_attr_min_microamps
);
2216 status
= device_create_file(dev
, &dev_attr_max_microamps
);
2221 /* suspend mode constraints need multiple supporting methods */
2222 if (!(ops
->set_suspend_enable
&& ops
->set_suspend_disable
))
2225 status
= device_create_file(dev
, &dev_attr_suspend_standby_state
);
2228 status
= device_create_file(dev
, &dev_attr_suspend_mem_state
);
2231 status
= device_create_file(dev
, &dev_attr_suspend_disk_state
);
2235 if (ops
->set_suspend_voltage
) {
2236 status
= device_create_file(dev
,
2237 &dev_attr_suspend_standby_microvolts
);
2240 status
= device_create_file(dev
,
2241 &dev_attr_suspend_mem_microvolts
);
2244 status
= device_create_file(dev
,
2245 &dev_attr_suspend_disk_microvolts
);
2250 if (ops
->set_suspend_mode
) {
2251 status
= device_create_file(dev
,
2252 &dev_attr_suspend_standby_mode
);
2255 status
= device_create_file(dev
,
2256 &dev_attr_suspend_mem_mode
);
2259 status
= device_create_file(dev
,
2260 &dev_attr_suspend_disk_mode
);
2269 * regulator_register - register regulator
2270 * @regulator_desc: regulator to register
2271 * @dev: struct device for the regulator
2272 * @init_data: platform provided init data, passed through by driver
2273 * @driver_data: private regulator data
2275 * Called by regulator drivers to register a regulator.
2276 * Returns 0 on success.
2278 struct regulator_dev
*regulator_register(struct regulator_desc
*regulator_desc
,
2279 struct device
*dev
, struct regulator_init_data
*init_data
,
2282 static atomic_t regulator_no
= ATOMIC_INIT(0);
2283 struct regulator_dev
*rdev
;
2286 if (regulator_desc
== NULL
)
2287 return ERR_PTR(-EINVAL
);
2289 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
2290 return ERR_PTR(-EINVAL
);
2292 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
2293 regulator_desc
->type
!= REGULATOR_CURRENT
)
2294 return ERR_PTR(-EINVAL
);
2297 return ERR_PTR(-EINVAL
);
2299 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
2301 return ERR_PTR(-ENOMEM
);
2303 mutex_lock(®ulator_list_mutex
);
2305 mutex_init(&rdev
->mutex
);
2306 rdev
->reg_data
= driver_data
;
2307 rdev
->owner
= regulator_desc
->owner
;
2308 rdev
->desc
= regulator_desc
;
2309 INIT_LIST_HEAD(&rdev
->consumer_list
);
2310 INIT_LIST_HEAD(&rdev
->supply_list
);
2311 INIT_LIST_HEAD(&rdev
->list
);
2312 INIT_LIST_HEAD(&rdev
->slist
);
2313 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
2315 /* preform any regulator specific init */
2316 if (init_data
->regulator_init
) {
2317 ret
= init_data
->regulator_init(rdev
->reg_data
);
2322 /* register with sysfs */
2323 rdev
->dev
.class = ®ulator_class
;
2324 rdev
->dev
.parent
= dev
;
2325 dev_set_name(&rdev
->dev
, "regulator.%d",
2326 atomic_inc_return(®ulator_no
) - 1);
2327 ret
= device_register(&rdev
->dev
);
2329 put_device(&rdev
->dev
);
2333 dev_set_drvdata(&rdev
->dev
, rdev
);
2335 /* set regulator constraints */
2336 ret
= set_machine_constraints(rdev
, &init_data
->constraints
);
2340 /* add attributes supported by this regulator */
2341 ret
= add_regulator_attributes(rdev
);
2345 /* set supply regulator if it exists */
2346 if (init_data
->supply_regulator
&& init_data
->supply_regulator_dev
) {
2348 "Supply regulator specified by both name and dev\n");
2352 if (init_data
->supply_regulator
) {
2353 struct regulator_dev
*r
;
2356 list_for_each_entry(r
, ®ulator_list
, list
) {
2357 if (strcmp(rdev_get_name(r
),
2358 init_data
->supply_regulator
) == 0) {
2365 dev_err(dev
, "Failed to find supply %s\n",
2366 init_data
->supply_regulator
);
2370 ret
= set_supply(rdev
, r
);
2375 if (init_data
->supply_regulator_dev
) {
2376 dev_warn(dev
, "Uses supply_regulator_dev instead of regulator_supply\n");
2377 ret
= set_supply(rdev
,
2378 dev_get_drvdata(init_data
->supply_regulator_dev
));
2383 /* add consumers devices */
2384 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
2385 ret
= set_consumer_device_supply(rdev
,
2386 init_data
->consumer_supplies
[i
].dev
,
2387 init_data
->consumer_supplies
[i
].dev_name
,
2388 init_data
->consumer_supplies
[i
].supply
);
2390 goto unset_supplies
;
2393 list_add(&rdev
->list
, ®ulator_list
);
2395 mutex_unlock(®ulator_list_mutex
);
2399 unset_regulator_supplies(rdev
);
2402 device_unregister(&rdev
->dev
);
2403 /* device core frees rdev */
2404 rdev
= ERR_PTR(ret
);
2409 rdev
= ERR_PTR(ret
);
2412 EXPORT_SYMBOL_GPL(regulator_register
);
2415 * regulator_unregister - unregister regulator
2416 * @rdev: regulator to unregister
2418 * Called by regulator drivers to unregister a regulator.
2420 void regulator_unregister(struct regulator_dev
*rdev
)
2425 mutex_lock(®ulator_list_mutex
);
2426 WARN_ON(rdev
->open_count
);
2427 unset_regulator_supplies(rdev
);
2428 list_del(&rdev
->list
);
2430 sysfs_remove_link(&rdev
->dev
.kobj
, "supply");
2431 device_unregister(&rdev
->dev
);
2432 mutex_unlock(®ulator_list_mutex
);
2434 EXPORT_SYMBOL_GPL(regulator_unregister
);
2437 * regulator_suspend_prepare - prepare regulators for system wide suspend
2438 * @state: system suspend state
2440 * Configure each regulator with it's suspend operating parameters for state.
2441 * This will usually be called by machine suspend code prior to supending.
2443 int regulator_suspend_prepare(suspend_state_t state
)
2445 struct regulator_dev
*rdev
;
2448 /* ON is handled by regulator active state */
2449 if (state
== PM_SUSPEND_ON
)
2452 mutex_lock(®ulator_list_mutex
);
2453 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2455 mutex_lock(&rdev
->mutex
);
2456 ret
= suspend_prepare(rdev
, state
);
2457 mutex_unlock(&rdev
->mutex
);
2460 printk(KERN_ERR
"%s: failed to prepare %s\n",
2461 __func__
, rdev_get_name(rdev
));
2466 mutex_unlock(®ulator_list_mutex
);
2469 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
2472 * regulator_has_full_constraints - the system has fully specified constraints
2474 * Calling this function will cause the regulator API to disable all
2475 * regulators which have a zero use count and don't have an always_on
2476 * constraint in a late_initcall.
2478 * The intention is that this will become the default behaviour in a
2479 * future kernel release so users are encouraged to use this facility
2482 void regulator_has_full_constraints(void)
2484 has_full_constraints
= 1;
2486 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
2489 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2491 * Calling this function will cause the regulator API to provide a
2492 * dummy regulator to consumers if no physical regulator is found,
2493 * allowing most consumers to proceed as though a regulator were
2494 * configured. This allows systems such as those with software
2495 * controllable regulators for the CPU core only to be brought up more
2498 void regulator_use_dummy_regulator(void)
2500 board_wants_dummy_regulator
= true;
2502 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator
);
2505 * rdev_get_drvdata - get rdev regulator driver data
2508 * Get rdev regulator driver private data. This call can be used in the
2509 * regulator driver context.
2511 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
2513 return rdev
->reg_data
;
2515 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
2518 * regulator_get_drvdata - get regulator driver data
2519 * @regulator: regulator
2521 * Get regulator driver private data. This call can be used in the consumer
2522 * driver context when non API regulator specific functions need to be called.
2524 void *regulator_get_drvdata(struct regulator
*regulator
)
2526 return regulator
->rdev
->reg_data
;
2528 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
2531 * regulator_set_drvdata - set regulator driver data
2532 * @regulator: regulator
2535 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
2537 regulator
->rdev
->reg_data
= data
;
2539 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
2542 * regulator_get_id - get regulator ID
2545 int rdev_get_id(struct regulator_dev
*rdev
)
2547 return rdev
->desc
->id
;
2549 EXPORT_SYMBOL_GPL(rdev_get_id
);
2551 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
2555 EXPORT_SYMBOL_GPL(rdev_get_dev
);
2557 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
2559 return reg_init_data
->driver_data
;
2561 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
2563 static int __init
regulator_init(void)
2567 printk(KERN_INFO
"regulator: core version %s\n", REGULATOR_VERSION
);
2569 ret
= class_register(®ulator_class
);
2571 regulator_dummy_init();
2576 /* init early to allow our consumers to complete system booting */
2577 core_initcall(regulator_init
);
2579 static int __init
regulator_init_complete(void)
2581 struct regulator_dev
*rdev
;
2582 struct regulator_ops
*ops
;
2583 struct regulation_constraints
*c
;
2587 mutex_lock(®ulator_list_mutex
);
2589 /* If we have a full configuration then disable any regulators
2590 * which are not in use or always_on. This will become the
2591 * default behaviour in the future.
2593 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2594 ops
= rdev
->desc
->ops
;
2595 c
= rdev
->constraints
;
2597 name
= rdev_get_name(rdev
);
2599 if (!ops
->disable
|| (c
&& c
->always_on
))
2602 mutex_lock(&rdev
->mutex
);
2604 if (rdev
->use_count
)
2607 /* If we can't read the status assume it's on. */
2608 if (ops
->is_enabled
)
2609 enabled
= ops
->is_enabled(rdev
);
2616 if (has_full_constraints
) {
2617 /* We log since this may kill the system if it
2619 printk(KERN_INFO
"%s: disabling %s\n",
2621 ret
= ops
->disable(rdev
);
2624 "%s: couldn't disable %s: %d\n",
2625 __func__
, name
, ret
);
2628 /* The intention is that in future we will
2629 * assume that full constraints are provided
2630 * so warn even if we aren't going to do
2634 "%s: incomplete constraints, leaving %s on\n",
2639 mutex_unlock(&rdev
->mutex
);
2642 mutex_unlock(®ulator_list_mutex
);
2646 late_initcall(regulator_init_complete
);