2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #define pr_fmt(fmt) "%s: " fmt, __func__
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/slab.h>
23 #include <linux/err.h>
24 #include <linux/mutex.h>
25 #include <linux/suspend.h>
26 #include <linux/delay.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/regulator/driver.h>
29 #include <linux/regulator/machine.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/regulator.h>
36 #define rdev_err(rdev, fmt, ...) \
37 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_warn(rdev, fmt, ...) \
39 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_info(rdev, fmt, ...) \
41 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_dbg(rdev, fmt, ...) \
43 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45 static DEFINE_MUTEX(regulator_list_mutex
);
46 static LIST_HEAD(regulator_list
);
47 static LIST_HEAD(regulator_map_list
);
48 static bool has_full_constraints
;
49 static bool board_wants_dummy_regulator
;
51 #ifdef CONFIG_DEBUG_FS
52 static struct dentry
*debugfs_root
;
56 * struct regulator_map
58 * Used to provide symbolic supply names to devices.
60 struct regulator_map
{
61 struct list_head list
;
62 const char *dev_name
; /* The dev_name() for the consumer */
64 struct regulator_dev
*regulator
;
70 * One for each consumer device.
74 struct list_head list
;
79 struct device_attribute dev_attr
;
80 struct regulator_dev
*rdev
;
83 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
84 static int _regulator_disable(struct regulator_dev
*rdev
,
85 struct regulator_dev
**supply_rdev_ptr
);
86 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
87 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
88 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
89 static void _notifier_call_chain(struct regulator_dev
*rdev
,
90 unsigned long event
, void *data
);
91 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
92 int min_uV
, int max_uV
);
94 static const char *rdev_get_name(struct regulator_dev
*rdev
)
96 if (rdev
->constraints
&& rdev
->constraints
->name
)
97 return rdev
->constraints
->name
;
98 else if (rdev
->desc
->name
)
99 return rdev
->desc
->name
;
104 /* gets the regulator for a given consumer device */
105 static struct regulator
*get_device_regulator(struct device
*dev
)
107 struct regulator
*regulator
= NULL
;
108 struct regulator_dev
*rdev
;
110 mutex_lock(®ulator_list_mutex
);
111 list_for_each_entry(rdev
, ®ulator_list
, list
) {
112 mutex_lock(&rdev
->mutex
);
113 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
114 if (regulator
->dev
== dev
) {
115 mutex_unlock(&rdev
->mutex
);
116 mutex_unlock(®ulator_list_mutex
);
120 mutex_unlock(&rdev
->mutex
);
122 mutex_unlock(®ulator_list_mutex
);
126 /* Platform voltage constraint check */
127 static int regulator_check_voltage(struct regulator_dev
*rdev
,
128 int *min_uV
, int *max_uV
)
130 BUG_ON(*min_uV
> *max_uV
);
132 if (!rdev
->constraints
) {
133 rdev_err(rdev
, "no constraints\n");
136 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
137 rdev_err(rdev
, "operation not allowed\n");
141 if (*max_uV
> rdev
->constraints
->max_uV
)
142 *max_uV
= rdev
->constraints
->max_uV
;
143 if (*min_uV
< rdev
->constraints
->min_uV
)
144 *min_uV
= rdev
->constraints
->min_uV
;
146 if (*min_uV
> *max_uV
)
152 /* Make sure we select a voltage that suits the needs of all
153 * regulator consumers
155 static int regulator_check_consumers(struct regulator_dev
*rdev
,
156 int *min_uV
, int *max_uV
)
158 struct regulator
*regulator
;
160 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
161 if (*max_uV
> regulator
->max_uV
)
162 *max_uV
= regulator
->max_uV
;
163 if (*min_uV
< regulator
->min_uV
)
164 *min_uV
= regulator
->min_uV
;
167 if (*min_uV
> *max_uV
)
173 /* current constraint check */
174 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
175 int *min_uA
, int *max_uA
)
177 BUG_ON(*min_uA
> *max_uA
);
179 if (!rdev
->constraints
) {
180 rdev_err(rdev
, "no constraints\n");
183 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
184 rdev_err(rdev
, "operation not allowed\n");
188 if (*max_uA
> rdev
->constraints
->max_uA
)
189 *max_uA
= rdev
->constraints
->max_uA
;
190 if (*min_uA
< rdev
->constraints
->min_uA
)
191 *min_uA
= rdev
->constraints
->min_uA
;
193 if (*min_uA
> *max_uA
)
199 /* operating mode constraint check */
200 static int regulator_check_mode(struct regulator_dev
*rdev
, int mode
)
203 case REGULATOR_MODE_FAST
:
204 case REGULATOR_MODE_NORMAL
:
205 case REGULATOR_MODE_IDLE
:
206 case REGULATOR_MODE_STANDBY
:
212 if (!rdev
->constraints
) {
213 rdev_err(rdev
, "no constraints\n");
216 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
217 rdev_err(rdev
, "operation not allowed\n");
220 if (!(rdev
->constraints
->valid_modes_mask
& mode
)) {
221 rdev_err(rdev
, "invalid mode %x\n", mode
);
227 /* dynamic regulator mode switching constraint check */
228 static int regulator_check_drms(struct regulator_dev
*rdev
)
230 if (!rdev
->constraints
) {
231 rdev_err(rdev
, "no constraints\n");
234 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
235 rdev_err(rdev
, "operation not allowed\n");
241 static ssize_t
device_requested_uA_show(struct device
*dev
,
242 struct device_attribute
*attr
, char *buf
)
244 struct regulator
*regulator
;
246 regulator
= get_device_regulator(dev
);
247 if (regulator
== NULL
)
250 return sprintf(buf
, "%d\n", regulator
->uA_load
);
253 static ssize_t
regulator_uV_show(struct device
*dev
,
254 struct device_attribute
*attr
, char *buf
)
256 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
259 mutex_lock(&rdev
->mutex
);
260 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
261 mutex_unlock(&rdev
->mutex
);
265 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
267 static ssize_t
regulator_uA_show(struct device
*dev
,
268 struct device_attribute
*attr
, char *buf
)
270 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
272 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
274 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
276 static ssize_t
regulator_name_show(struct device
*dev
,
277 struct device_attribute
*attr
, char *buf
)
279 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
281 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
284 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
287 case REGULATOR_MODE_FAST
:
288 return sprintf(buf
, "fast\n");
289 case REGULATOR_MODE_NORMAL
:
290 return sprintf(buf
, "normal\n");
291 case REGULATOR_MODE_IDLE
:
292 return sprintf(buf
, "idle\n");
293 case REGULATOR_MODE_STANDBY
:
294 return sprintf(buf
, "standby\n");
296 return sprintf(buf
, "unknown\n");
299 static ssize_t
regulator_opmode_show(struct device
*dev
,
300 struct device_attribute
*attr
, char *buf
)
302 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
304 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
306 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
308 static ssize_t
regulator_print_state(char *buf
, int state
)
311 return sprintf(buf
, "enabled\n");
313 return sprintf(buf
, "disabled\n");
315 return sprintf(buf
, "unknown\n");
318 static ssize_t
regulator_state_show(struct device
*dev
,
319 struct device_attribute
*attr
, char *buf
)
321 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
324 mutex_lock(&rdev
->mutex
);
325 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
326 mutex_unlock(&rdev
->mutex
);
330 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
332 static ssize_t
regulator_status_show(struct device
*dev
,
333 struct device_attribute
*attr
, char *buf
)
335 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
339 status
= rdev
->desc
->ops
->get_status(rdev
);
344 case REGULATOR_STATUS_OFF
:
347 case REGULATOR_STATUS_ON
:
350 case REGULATOR_STATUS_ERROR
:
353 case REGULATOR_STATUS_FAST
:
356 case REGULATOR_STATUS_NORMAL
:
359 case REGULATOR_STATUS_IDLE
:
362 case REGULATOR_STATUS_STANDBY
:
369 return sprintf(buf
, "%s\n", label
);
371 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
373 static ssize_t
regulator_min_uA_show(struct device
*dev
,
374 struct device_attribute
*attr
, char *buf
)
376 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
378 if (!rdev
->constraints
)
379 return sprintf(buf
, "constraint not defined\n");
381 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
383 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
385 static ssize_t
regulator_max_uA_show(struct device
*dev
,
386 struct device_attribute
*attr
, char *buf
)
388 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
390 if (!rdev
->constraints
)
391 return sprintf(buf
, "constraint not defined\n");
393 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
395 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
397 static ssize_t
regulator_min_uV_show(struct device
*dev
,
398 struct device_attribute
*attr
, char *buf
)
400 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
402 if (!rdev
->constraints
)
403 return sprintf(buf
, "constraint not defined\n");
405 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
407 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
409 static ssize_t
regulator_max_uV_show(struct device
*dev
,
410 struct device_attribute
*attr
, char *buf
)
412 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
414 if (!rdev
->constraints
)
415 return sprintf(buf
, "constraint not defined\n");
417 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
419 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
421 static ssize_t
regulator_total_uA_show(struct device
*dev
,
422 struct device_attribute
*attr
, char *buf
)
424 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
425 struct regulator
*regulator
;
428 mutex_lock(&rdev
->mutex
);
429 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
430 uA
+= regulator
->uA_load
;
431 mutex_unlock(&rdev
->mutex
);
432 return sprintf(buf
, "%d\n", uA
);
434 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
436 static ssize_t
regulator_num_users_show(struct device
*dev
,
437 struct device_attribute
*attr
, char *buf
)
439 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
440 return sprintf(buf
, "%d\n", rdev
->use_count
);
443 static ssize_t
regulator_type_show(struct device
*dev
,
444 struct device_attribute
*attr
, char *buf
)
446 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
448 switch (rdev
->desc
->type
) {
449 case REGULATOR_VOLTAGE
:
450 return sprintf(buf
, "voltage\n");
451 case REGULATOR_CURRENT
:
452 return sprintf(buf
, "current\n");
454 return sprintf(buf
, "unknown\n");
457 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
458 struct device_attribute
*attr
, char *buf
)
460 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
462 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
464 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
465 regulator_suspend_mem_uV_show
, NULL
);
467 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
468 struct device_attribute
*attr
, char *buf
)
470 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
472 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
474 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
475 regulator_suspend_disk_uV_show
, NULL
);
477 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
478 struct device_attribute
*attr
, char *buf
)
480 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
482 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
484 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
485 regulator_suspend_standby_uV_show
, NULL
);
487 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
488 struct device_attribute
*attr
, char *buf
)
490 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
492 return regulator_print_opmode(buf
,
493 rdev
->constraints
->state_mem
.mode
);
495 static DEVICE_ATTR(suspend_mem_mode
, 0444,
496 regulator_suspend_mem_mode_show
, NULL
);
498 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
499 struct device_attribute
*attr
, char *buf
)
501 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
503 return regulator_print_opmode(buf
,
504 rdev
->constraints
->state_disk
.mode
);
506 static DEVICE_ATTR(suspend_disk_mode
, 0444,
507 regulator_suspend_disk_mode_show
, NULL
);
509 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
510 struct device_attribute
*attr
, char *buf
)
512 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
514 return regulator_print_opmode(buf
,
515 rdev
->constraints
->state_standby
.mode
);
517 static DEVICE_ATTR(suspend_standby_mode
, 0444,
518 regulator_suspend_standby_mode_show
, NULL
);
520 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
521 struct device_attribute
*attr
, char *buf
)
523 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
525 return regulator_print_state(buf
,
526 rdev
->constraints
->state_mem
.enabled
);
528 static DEVICE_ATTR(suspend_mem_state
, 0444,
529 regulator_suspend_mem_state_show
, NULL
);
531 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
532 struct device_attribute
*attr
, char *buf
)
534 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
536 return regulator_print_state(buf
,
537 rdev
->constraints
->state_disk
.enabled
);
539 static DEVICE_ATTR(suspend_disk_state
, 0444,
540 regulator_suspend_disk_state_show
, NULL
);
542 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
543 struct device_attribute
*attr
, char *buf
)
545 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
547 return regulator_print_state(buf
,
548 rdev
->constraints
->state_standby
.enabled
);
550 static DEVICE_ATTR(suspend_standby_state
, 0444,
551 regulator_suspend_standby_state_show
, NULL
);
555 * These are the only attributes are present for all regulators.
556 * Other attributes are a function of regulator functionality.
558 static struct device_attribute regulator_dev_attrs
[] = {
559 __ATTR(name
, 0444, regulator_name_show
, NULL
),
560 __ATTR(num_users
, 0444, regulator_num_users_show
, NULL
),
561 __ATTR(type
, 0444, regulator_type_show
, NULL
),
565 static void regulator_dev_release(struct device
*dev
)
567 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
571 static struct class regulator_class
= {
573 .dev_release
= regulator_dev_release
,
574 .dev_attrs
= regulator_dev_attrs
,
577 /* Calculate the new optimum regulator operating mode based on the new total
578 * consumer load. All locks held by caller */
579 static void drms_uA_update(struct regulator_dev
*rdev
)
581 struct regulator
*sibling
;
582 int current_uA
= 0, output_uV
, input_uV
, err
;
585 err
= regulator_check_drms(rdev
);
586 if (err
< 0 || !rdev
->desc
->ops
->get_optimum_mode
||
587 (!rdev
->desc
->ops
->get_voltage
&&
588 !rdev
->desc
->ops
->get_voltage_sel
) ||
589 !rdev
->desc
->ops
->set_mode
)
592 /* get output voltage */
593 output_uV
= _regulator_get_voltage(rdev
);
597 /* get input voltage */
600 input_uV
= _regulator_get_voltage(rdev
);
602 input_uV
= rdev
->constraints
->input_uV
;
606 /* calc total requested load */
607 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
608 current_uA
+= sibling
->uA_load
;
610 /* now get the optimum mode for our new total regulator load */
611 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
612 output_uV
, current_uA
);
614 /* check the new mode is allowed */
615 err
= regulator_check_mode(rdev
, mode
);
617 rdev
->desc
->ops
->set_mode(rdev
, mode
);
620 static int suspend_set_state(struct regulator_dev
*rdev
,
621 struct regulator_state
*rstate
)
626 can_set_state
= rdev
->desc
->ops
->set_suspend_enable
&&
627 rdev
->desc
->ops
->set_suspend_disable
;
629 /* If we have no suspend mode configration don't set anything;
630 * only warn if the driver actually makes the suspend mode
633 if (!rstate
->enabled
&& !rstate
->disabled
) {
635 rdev_warn(rdev
, "No configuration\n");
639 if (rstate
->enabled
&& rstate
->disabled
) {
640 rdev_err(rdev
, "invalid configuration\n");
644 if (!can_set_state
) {
645 rdev_err(rdev
, "no way to set suspend state\n");
650 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
652 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
654 rdev_err(rdev
, "failed to enabled/disable\n");
658 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
659 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
661 rdev_err(rdev
, "failed to set voltage\n");
666 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
667 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
669 rdev_err(rdev
, "failed to set mode\n");
676 /* locks held by caller */
677 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
679 if (!rdev
->constraints
)
683 case PM_SUSPEND_STANDBY
:
684 return suspend_set_state(rdev
,
685 &rdev
->constraints
->state_standby
);
687 return suspend_set_state(rdev
,
688 &rdev
->constraints
->state_mem
);
690 return suspend_set_state(rdev
,
691 &rdev
->constraints
->state_disk
);
697 static void print_constraints(struct regulator_dev
*rdev
)
699 struct regulation_constraints
*constraints
= rdev
->constraints
;
704 if (constraints
->min_uV
&& constraints
->max_uV
) {
705 if (constraints
->min_uV
== constraints
->max_uV
)
706 count
+= sprintf(buf
+ count
, "%d mV ",
707 constraints
->min_uV
/ 1000);
709 count
+= sprintf(buf
+ count
, "%d <--> %d mV ",
710 constraints
->min_uV
/ 1000,
711 constraints
->max_uV
/ 1000);
714 if (!constraints
->min_uV
||
715 constraints
->min_uV
!= constraints
->max_uV
) {
716 ret
= _regulator_get_voltage(rdev
);
718 count
+= sprintf(buf
+ count
, "at %d mV ", ret
/ 1000);
721 if (constraints
->min_uA
&& constraints
->max_uA
) {
722 if (constraints
->min_uA
== constraints
->max_uA
)
723 count
+= sprintf(buf
+ count
, "%d mA ",
724 constraints
->min_uA
/ 1000);
726 count
+= sprintf(buf
+ count
, "%d <--> %d mA ",
727 constraints
->min_uA
/ 1000,
728 constraints
->max_uA
/ 1000);
731 if (!constraints
->min_uA
||
732 constraints
->min_uA
!= constraints
->max_uA
) {
733 ret
= _regulator_get_current_limit(rdev
);
735 count
+= sprintf(buf
+ count
, "at %d mA ", ret
/ 1000);
738 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
739 count
+= sprintf(buf
+ count
, "fast ");
740 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
741 count
+= sprintf(buf
+ count
, "normal ");
742 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
743 count
+= sprintf(buf
+ count
, "idle ");
744 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
745 count
+= sprintf(buf
+ count
, "standby");
747 rdev_info(rdev
, "%s\n", buf
);
750 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
751 struct regulation_constraints
*constraints
)
753 struct regulator_ops
*ops
= rdev
->desc
->ops
;
756 /* do we need to apply the constraint voltage */
757 if (rdev
->constraints
->apply_uV
&&
758 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
) {
759 ret
= _regulator_do_set_voltage(rdev
,
760 rdev
->constraints
->min_uV
,
761 rdev
->constraints
->max_uV
);
763 rdev_err(rdev
, "failed to apply %duV constraint\n",
764 rdev
->constraints
->min_uV
);
765 rdev
->constraints
= NULL
;
770 /* constrain machine-level voltage specs to fit
771 * the actual range supported by this regulator.
773 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
774 int count
= rdev
->desc
->n_voltages
;
776 int min_uV
= INT_MAX
;
777 int max_uV
= INT_MIN
;
778 int cmin
= constraints
->min_uV
;
779 int cmax
= constraints
->max_uV
;
781 /* it's safe to autoconfigure fixed-voltage supplies
782 and the constraints are used by list_voltage. */
783 if (count
== 1 && !cmin
) {
786 constraints
->min_uV
= cmin
;
787 constraints
->max_uV
= cmax
;
790 /* voltage constraints are optional */
791 if ((cmin
== 0) && (cmax
== 0))
794 /* else require explicit machine-level constraints */
795 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
796 rdev_err(rdev
, "invalid voltage constraints\n");
800 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
801 for (i
= 0; i
< count
; i
++) {
804 value
= ops
->list_voltage(rdev
, i
);
808 /* maybe adjust [min_uV..max_uV] */
809 if (value
>= cmin
&& value
< min_uV
)
811 if (value
<= cmax
&& value
> max_uV
)
815 /* final: [min_uV..max_uV] valid iff constraints valid */
816 if (max_uV
< min_uV
) {
817 rdev_err(rdev
, "unsupportable voltage constraints\n");
821 /* use regulator's subset of machine constraints */
822 if (constraints
->min_uV
< min_uV
) {
823 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
824 constraints
->min_uV
, min_uV
);
825 constraints
->min_uV
= min_uV
;
827 if (constraints
->max_uV
> max_uV
) {
828 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
829 constraints
->max_uV
, max_uV
);
830 constraints
->max_uV
= max_uV
;
838 * set_machine_constraints - sets regulator constraints
839 * @rdev: regulator source
840 * @constraints: constraints to apply
842 * Allows platform initialisation code to define and constrain
843 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
844 * Constraints *must* be set by platform code in order for some
845 * regulator operations to proceed i.e. set_voltage, set_current_limit,
848 static int set_machine_constraints(struct regulator_dev
*rdev
,
849 const struct regulation_constraints
*constraints
)
852 struct regulator_ops
*ops
= rdev
->desc
->ops
;
854 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
856 if (!rdev
->constraints
)
859 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
863 /* do we need to setup our suspend state */
864 if (constraints
->initial_state
) {
865 ret
= suspend_prepare(rdev
, rdev
->constraints
->initial_state
);
867 rdev_err(rdev
, "failed to set suspend state\n");
868 rdev
->constraints
= NULL
;
873 if (constraints
->initial_mode
) {
874 if (!ops
->set_mode
) {
875 rdev_err(rdev
, "no set_mode operation\n");
880 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
882 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
887 /* If the constraints say the regulator should be on at this point
888 * and we have control then make sure it is enabled.
890 if ((rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) &&
892 ret
= ops
->enable(rdev
);
894 rdev_err(rdev
, "failed to enable\n");
895 rdev
->constraints
= NULL
;
900 print_constraints(rdev
);
906 * set_supply - set regulator supply regulator
907 * @rdev: regulator name
908 * @supply_rdev: supply regulator name
910 * Called by platform initialisation code to set the supply regulator for this
911 * regulator. This ensures that a regulators supply will also be enabled by the
912 * core if it's child is enabled.
914 static int set_supply(struct regulator_dev
*rdev
,
915 struct regulator_dev
*supply_rdev
)
919 err
= sysfs_create_link(&rdev
->dev
.kobj
, &supply_rdev
->dev
.kobj
,
922 rdev_err(rdev
, "could not add device link %s err %d\n",
923 supply_rdev
->dev
.kobj
.name
, err
);
926 rdev
->supply
= supply_rdev
;
927 list_add(&rdev
->slist
, &supply_rdev
->supply_list
);
933 * set_consumer_device_supply - Bind a regulator to a symbolic supply
934 * @rdev: regulator source
935 * @consumer_dev: device the supply applies to
936 * @consumer_dev_name: dev_name() string for device supply applies to
937 * @supply: symbolic name for supply
939 * Allows platform initialisation code to map physical regulator
940 * sources to symbolic names for supplies for use by devices. Devices
941 * should use these symbolic names to request regulators, avoiding the
942 * need to provide board-specific regulator names as platform data.
944 * Only one of consumer_dev and consumer_dev_name may be specified.
946 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
947 struct device
*consumer_dev
, const char *consumer_dev_name
,
950 struct regulator_map
*node
;
953 if (consumer_dev
&& consumer_dev_name
)
956 if (!consumer_dev_name
&& consumer_dev
)
957 consumer_dev_name
= dev_name(consumer_dev
);
962 if (consumer_dev_name
!= NULL
)
967 list_for_each_entry(node
, ®ulator_map_list
, list
) {
968 if (node
->dev_name
&& consumer_dev_name
) {
969 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
971 } else if (node
->dev_name
|| consumer_dev_name
) {
975 if (strcmp(node
->supply
, supply
) != 0)
978 dev_dbg(consumer_dev
, "%s/%s is '%s' supply; fail %s/%s\n",
979 dev_name(&node
->regulator
->dev
),
980 node
->regulator
->desc
->name
,
982 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
986 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
990 node
->regulator
= rdev
;
991 node
->supply
= supply
;
994 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
995 if (node
->dev_name
== NULL
) {
1001 list_add(&node
->list
, ®ulator_map_list
);
1005 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1007 struct regulator_map
*node
, *n
;
1009 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1010 if (rdev
== node
->regulator
) {
1011 list_del(&node
->list
);
1012 kfree(node
->dev_name
);
1018 #define REG_STR_SIZE 32
1020 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1022 const char *supply_name
)
1024 struct regulator
*regulator
;
1025 char buf
[REG_STR_SIZE
];
1028 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1029 if (regulator
== NULL
)
1032 mutex_lock(&rdev
->mutex
);
1033 regulator
->rdev
= rdev
;
1034 list_add(®ulator
->list
, &rdev
->consumer_list
);
1037 /* create a 'requested_microamps_name' sysfs entry */
1038 size
= scnprintf(buf
, REG_STR_SIZE
, "microamps_requested_%s",
1040 if (size
>= REG_STR_SIZE
)
1043 regulator
->dev
= dev
;
1044 sysfs_attr_init(®ulator
->dev_attr
.attr
);
1045 regulator
->dev_attr
.attr
.name
= kstrdup(buf
, GFP_KERNEL
);
1046 if (regulator
->dev_attr
.attr
.name
== NULL
)
1049 regulator
->dev_attr
.attr
.mode
= 0444;
1050 regulator
->dev_attr
.show
= device_requested_uA_show
;
1051 err
= device_create_file(dev
, ®ulator
->dev_attr
);
1053 rdev_warn(rdev
, "could not add regulator_dev requested microamps sysfs entry\n");
1057 /* also add a link to the device sysfs entry */
1058 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1059 dev
->kobj
.name
, supply_name
);
1060 if (size
>= REG_STR_SIZE
)
1063 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1064 if (regulator
->supply_name
== NULL
)
1067 err
= sysfs_create_link(&rdev
->dev
.kobj
, &dev
->kobj
,
1070 rdev_warn(rdev
, "could not add device link %s err %d\n",
1071 dev
->kobj
.name
, err
);
1075 mutex_unlock(&rdev
->mutex
);
1078 kfree(regulator
->supply_name
);
1080 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1082 kfree(regulator
->dev_attr
.attr
.name
);
1084 list_del(®ulator
->list
);
1086 mutex_unlock(&rdev
->mutex
);
1090 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1092 if (!rdev
->desc
->ops
->enable_time
)
1094 return rdev
->desc
->ops
->enable_time(rdev
);
1097 /* Internal regulator request function */
1098 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1101 struct regulator_dev
*rdev
;
1102 struct regulator_map
*map
;
1103 struct regulator
*regulator
= ERR_PTR(-ENODEV
);
1104 const char *devname
= NULL
;
1108 pr_err("get() with no identifier\n");
1113 devname
= dev_name(dev
);
1115 mutex_lock(®ulator_list_mutex
);
1117 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1118 /* If the mapping has a device set up it must match */
1119 if (map
->dev_name
&&
1120 (!devname
|| strcmp(map
->dev_name
, devname
)))
1123 if (strcmp(map
->supply
, id
) == 0) {
1124 rdev
= map
->regulator
;
1129 if (board_wants_dummy_regulator
) {
1130 rdev
= dummy_regulator_rdev
;
1134 #ifdef CONFIG_REGULATOR_DUMMY
1136 devname
= "deviceless";
1138 /* If the board didn't flag that it was fully constrained then
1139 * substitute in a dummy regulator so consumers can continue.
1141 if (!has_full_constraints
) {
1142 pr_warn("%s supply %s not found, using dummy regulator\n",
1144 rdev
= dummy_regulator_rdev
;
1149 mutex_unlock(®ulator_list_mutex
);
1153 if (rdev
->exclusive
) {
1154 regulator
= ERR_PTR(-EPERM
);
1158 if (exclusive
&& rdev
->open_count
) {
1159 regulator
= ERR_PTR(-EBUSY
);
1163 if (!try_module_get(rdev
->owner
))
1166 regulator
= create_regulator(rdev
, dev
, id
);
1167 if (regulator
== NULL
) {
1168 regulator
= ERR_PTR(-ENOMEM
);
1169 module_put(rdev
->owner
);
1174 rdev
->exclusive
= 1;
1176 ret
= _regulator_is_enabled(rdev
);
1178 rdev
->use_count
= 1;
1180 rdev
->use_count
= 0;
1184 mutex_unlock(®ulator_list_mutex
);
1190 * regulator_get - lookup and obtain a reference to a regulator.
1191 * @dev: device for regulator "consumer"
1192 * @id: Supply name or regulator ID.
1194 * Returns a struct regulator corresponding to the regulator producer,
1195 * or IS_ERR() condition containing errno.
1197 * Use of supply names configured via regulator_set_device_supply() is
1198 * strongly encouraged. It is recommended that the supply name used
1199 * should match the name used for the supply and/or the relevant
1200 * device pins in the datasheet.
1202 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1204 return _regulator_get(dev
, id
, 0);
1206 EXPORT_SYMBOL_GPL(regulator_get
);
1209 * regulator_get_exclusive - obtain exclusive access to a regulator.
1210 * @dev: device for regulator "consumer"
1211 * @id: Supply name or regulator ID.
1213 * Returns a struct regulator corresponding to the regulator producer,
1214 * or IS_ERR() condition containing errno. Other consumers will be
1215 * unable to obtain this reference is held and the use count for the
1216 * regulator will be initialised to reflect the current state of the
1219 * This is intended for use by consumers which cannot tolerate shared
1220 * use of the regulator such as those which need to force the
1221 * regulator off for correct operation of the hardware they are
1224 * Use of supply names configured via regulator_set_device_supply() is
1225 * strongly encouraged. It is recommended that the supply name used
1226 * should match the name used for the supply and/or the relevant
1227 * device pins in the datasheet.
1229 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1231 return _regulator_get(dev
, id
, 1);
1233 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1236 * regulator_put - "free" the regulator source
1237 * @regulator: regulator source
1239 * Note: drivers must ensure that all regulator_enable calls made on this
1240 * regulator source are balanced by regulator_disable calls prior to calling
1243 void regulator_put(struct regulator
*regulator
)
1245 struct regulator_dev
*rdev
;
1247 if (regulator
== NULL
|| IS_ERR(regulator
))
1250 mutex_lock(®ulator_list_mutex
);
1251 rdev
= regulator
->rdev
;
1253 /* remove any sysfs entries */
1254 if (regulator
->dev
) {
1255 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1256 kfree(regulator
->supply_name
);
1257 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1258 kfree(regulator
->dev_attr
.attr
.name
);
1260 list_del(®ulator
->list
);
1264 rdev
->exclusive
= 0;
1266 module_put(rdev
->owner
);
1267 mutex_unlock(®ulator_list_mutex
);
1269 EXPORT_SYMBOL_GPL(regulator_put
);
1271 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
1273 if (!rdev
->constraints
)
1276 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
1282 /* locks held by regulator_enable() */
1283 static int _regulator_enable(struct regulator_dev
*rdev
)
1287 if (rdev
->use_count
== 0) {
1288 /* do we need to enable the supply regulator first */
1290 mutex_lock(&rdev
->supply
->mutex
);
1291 ret
= _regulator_enable(rdev
->supply
);
1292 mutex_unlock(&rdev
->supply
->mutex
);
1294 rdev_err(rdev
, "failed to enable: %d\n", ret
);
1300 /* check voltage and requested load before enabling */
1301 if (rdev
->constraints
&&
1302 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1303 drms_uA_update(rdev
);
1305 if (rdev
->use_count
== 0) {
1306 /* The regulator may on if it's not switchable or left on */
1307 ret
= _regulator_is_enabled(rdev
);
1308 if (ret
== -EINVAL
|| ret
== 0) {
1309 if (!_regulator_can_change_status(rdev
))
1312 if (!rdev
->desc
->ops
->enable
)
1315 /* Query before enabling in case configuration
1317 ret
= _regulator_get_enable_time(rdev
);
1321 rdev_warn(rdev
, "enable_time() failed: %d\n",
1326 trace_regulator_enable(rdev_get_name(rdev
));
1328 /* Allow the regulator to ramp; it would be useful
1329 * to extend this for bulk operations so that the
1330 * regulators can ramp together. */
1331 ret
= rdev
->desc
->ops
->enable(rdev
);
1335 trace_regulator_enable_delay(rdev_get_name(rdev
));
1337 if (delay
>= 1000) {
1338 mdelay(delay
/ 1000);
1339 udelay(delay
% 1000);
1344 trace_regulator_enable_complete(rdev_get_name(rdev
));
1346 } else if (ret
< 0) {
1347 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
1350 /* Fallthrough on positive return values - already enabled */
1359 * regulator_enable - enable regulator output
1360 * @regulator: regulator source
1362 * Request that the regulator be enabled with the regulator output at
1363 * the predefined voltage or current value. Calls to regulator_enable()
1364 * must be balanced with calls to regulator_disable().
1366 * NOTE: the output value can be set by other drivers, boot loader or may be
1367 * hardwired in the regulator.
1369 int regulator_enable(struct regulator
*regulator
)
1371 struct regulator_dev
*rdev
= regulator
->rdev
;
1374 mutex_lock(&rdev
->mutex
);
1375 ret
= _regulator_enable(rdev
);
1376 mutex_unlock(&rdev
->mutex
);
1379 EXPORT_SYMBOL_GPL(regulator_enable
);
1381 /* locks held by regulator_disable() */
1382 static int _regulator_disable(struct regulator_dev
*rdev
,
1383 struct regulator_dev
**supply_rdev_ptr
)
1386 *supply_rdev_ptr
= NULL
;
1388 if (WARN(rdev
->use_count
<= 0,
1389 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
1392 /* are we the last user and permitted to disable ? */
1393 if (rdev
->use_count
== 1 &&
1394 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
1396 /* we are last user */
1397 if (_regulator_can_change_status(rdev
) &&
1398 rdev
->desc
->ops
->disable
) {
1399 trace_regulator_disable(rdev_get_name(rdev
));
1401 ret
= rdev
->desc
->ops
->disable(rdev
);
1403 rdev_err(rdev
, "failed to disable\n");
1407 trace_regulator_disable_complete(rdev_get_name(rdev
));
1409 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
1413 /* decrease our supplies ref count and disable if required */
1414 *supply_rdev_ptr
= rdev
->supply
;
1416 rdev
->use_count
= 0;
1417 } else if (rdev
->use_count
> 1) {
1419 if (rdev
->constraints
&&
1420 (rdev
->constraints
->valid_ops_mask
&
1421 REGULATOR_CHANGE_DRMS
))
1422 drms_uA_update(rdev
);
1430 * regulator_disable - disable regulator output
1431 * @regulator: regulator source
1433 * Disable the regulator output voltage or current. Calls to
1434 * regulator_enable() must be balanced with calls to
1435 * regulator_disable().
1437 * NOTE: this will only disable the regulator output if no other consumer
1438 * devices have it enabled, the regulator device supports disabling and
1439 * machine constraints permit this operation.
1441 int regulator_disable(struct regulator
*regulator
)
1443 struct regulator_dev
*rdev
= regulator
->rdev
;
1444 struct regulator_dev
*supply_rdev
= NULL
;
1447 mutex_lock(&rdev
->mutex
);
1448 ret
= _regulator_disable(rdev
, &supply_rdev
);
1449 mutex_unlock(&rdev
->mutex
);
1451 /* decrease our supplies ref count and disable if required */
1452 while (supply_rdev
!= NULL
) {
1455 mutex_lock(&rdev
->mutex
);
1456 _regulator_disable(rdev
, &supply_rdev
);
1457 mutex_unlock(&rdev
->mutex
);
1462 EXPORT_SYMBOL_GPL(regulator_disable
);
1464 /* locks held by regulator_force_disable() */
1465 static int _regulator_force_disable(struct regulator_dev
*rdev
,
1466 struct regulator_dev
**supply_rdev_ptr
)
1471 if (rdev
->desc
->ops
->disable
) {
1472 /* ah well, who wants to live forever... */
1473 ret
= rdev
->desc
->ops
->disable(rdev
);
1475 rdev_err(rdev
, "failed to force disable\n");
1478 /* notify other consumers that power has been forced off */
1479 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
1480 REGULATOR_EVENT_DISABLE
, NULL
);
1483 /* decrease our supplies ref count and disable if required */
1484 *supply_rdev_ptr
= rdev
->supply
;
1486 rdev
->use_count
= 0;
1491 * regulator_force_disable - force disable regulator output
1492 * @regulator: regulator source
1494 * Forcibly disable the regulator output voltage or current.
1495 * NOTE: this *will* disable the regulator output even if other consumer
1496 * devices have it enabled. This should be used for situations when device
1497 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1499 int regulator_force_disable(struct regulator
*regulator
)
1501 struct regulator_dev
*supply_rdev
= NULL
;
1504 mutex_lock(®ulator
->rdev
->mutex
);
1505 regulator
->uA_load
= 0;
1506 ret
= _regulator_force_disable(regulator
->rdev
, &supply_rdev
);
1507 mutex_unlock(®ulator
->rdev
->mutex
);
1510 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev
)));
1514 EXPORT_SYMBOL_GPL(regulator_force_disable
);
1516 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
1518 /* If we don't know then assume that the regulator is always on */
1519 if (!rdev
->desc
->ops
->is_enabled
)
1522 return rdev
->desc
->ops
->is_enabled(rdev
);
1526 * regulator_is_enabled - is the regulator output enabled
1527 * @regulator: regulator source
1529 * Returns positive if the regulator driver backing the source/client
1530 * has requested that the device be enabled, zero if it hasn't, else a
1531 * negative errno code.
1533 * Note that the device backing this regulator handle can have multiple
1534 * users, so it might be enabled even if regulator_enable() was never
1535 * called for this particular source.
1537 int regulator_is_enabled(struct regulator
*regulator
)
1541 mutex_lock(®ulator
->rdev
->mutex
);
1542 ret
= _regulator_is_enabled(regulator
->rdev
);
1543 mutex_unlock(®ulator
->rdev
->mutex
);
1547 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
1550 * regulator_count_voltages - count regulator_list_voltage() selectors
1551 * @regulator: regulator source
1553 * Returns number of selectors, or negative errno. Selectors are
1554 * numbered starting at zero, and typically correspond to bitfields
1555 * in hardware registers.
1557 int regulator_count_voltages(struct regulator
*regulator
)
1559 struct regulator_dev
*rdev
= regulator
->rdev
;
1561 return rdev
->desc
->n_voltages
? : -EINVAL
;
1563 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
1566 * regulator_list_voltage - enumerate supported voltages
1567 * @regulator: regulator source
1568 * @selector: identify voltage to list
1569 * Context: can sleep
1571 * Returns a voltage that can be passed to @regulator_set_voltage(),
1572 * zero if this selector code can't be used on this system, or a
1575 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
1577 struct regulator_dev
*rdev
= regulator
->rdev
;
1578 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1581 if (!ops
->list_voltage
|| selector
>= rdev
->desc
->n_voltages
)
1584 mutex_lock(&rdev
->mutex
);
1585 ret
= ops
->list_voltage(rdev
, selector
);
1586 mutex_unlock(&rdev
->mutex
);
1589 if (ret
< rdev
->constraints
->min_uV
)
1591 else if (ret
> rdev
->constraints
->max_uV
)
1597 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
1600 * regulator_is_supported_voltage - check if a voltage range can be supported
1602 * @regulator: Regulator to check.
1603 * @min_uV: Minimum required voltage in uV.
1604 * @max_uV: Maximum required voltage in uV.
1606 * Returns a boolean or a negative error code.
1608 int regulator_is_supported_voltage(struct regulator
*regulator
,
1609 int min_uV
, int max_uV
)
1611 int i
, voltages
, ret
;
1613 ret
= regulator_count_voltages(regulator
);
1618 for (i
= 0; i
< voltages
; i
++) {
1619 ret
= regulator_list_voltage(regulator
, i
);
1621 if (ret
>= min_uV
&& ret
<= max_uV
)
1628 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
1629 int min_uV
, int max_uV
)
1633 unsigned int selector
;
1635 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
1637 if (rdev
->desc
->ops
->set_voltage
) {
1638 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
,
1641 if (rdev
->desc
->ops
->list_voltage
)
1642 selector
= rdev
->desc
->ops
->list_voltage(rdev
,
1646 } else if (rdev
->desc
->ops
->set_voltage_sel
) {
1647 int best_val
= INT_MAX
;
1652 /* Find the smallest voltage that falls within the specified
1655 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
1656 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
1660 if (ret
< best_val
&& ret
>= min_uV
&& ret
<= max_uV
) {
1667 * If we can't obtain the old selector there is not enough
1668 * info to call set_voltage_time_sel().
1670 if (rdev
->desc
->ops
->set_voltage_time_sel
&&
1671 rdev
->desc
->ops
->get_voltage_sel
) {
1672 unsigned int old_selector
= 0;
1674 ret
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
1678 delay
= rdev
->desc
->ops
->set_voltage_time_sel(rdev
,
1679 old_selector
, selector
);
1682 if (best_val
!= INT_MAX
) {
1683 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
1684 selector
= best_val
;
1692 /* Insert any necessary delays */
1693 if (delay
>= 1000) {
1694 mdelay(delay
/ 1000);
1695 udelay(delay
% 1000);
1701 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
1704 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), selector
);
1710 * regulator_set_voltage - set regulator output voltage
1711 * @regulator: regulator source
1712 * @min_uV: Minimum required voltage in uV
1713 * @max_uV: Maximum acceptable voltage in uV
1715 * Sets a voltage regulator to the desired output voltage. This can be set
1716 * during any regulator state. IOW, regulator can be disabled or enabled.
1718 * If the regulator is enabled then the voltage will change to the new value
1719 * immediately otherwise if the regulator is disabled the regulator will
1720 * output at the new voltage when enabled.
1722 * NOTE: If the regulator is shared between several devices then the lowest
1723 * request voltage that meets the system constraints will be used.
1724 * Regulator system constraints must be set for this regulator before
1725 * calling this function otherwise this call will fail.
1727 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
1729 struct regulator_dev
*rdev
= regulator
->rdev
;
1732 mutex_lock(&rdev
->mutex
);
1734 /* If we're setting the same range as last time the change
1735 * should be a noop (some cpufreq implementations use the same
1736 * voltage for multiple frequencies, for example).
1738 if (regulator
->min_uV
== min_uV
&& regulator
->max_uV
== max_uV
)
1742 if (!rdev
->desc
->ops
->set_voltage
&&
1743 !rdev
->desc
->ops
->set_voltage_sel
) {
1748 /* constraints check */
1749 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1752 regulator
->min_uV
= min_uV
;
1753 regulator
->max_uV
= max_uV
;
1755 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
1759 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
1762 mutex_unlock(&rdev
->mutex
);
1765 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
1768 * regulator_set_voltage_time - get raise/fall time
1769 * @regulator: regulator source
1770 * @old_uV: starting voltage in microvolts
1771 * @new_uV: target voltage in microvolts
1773 * Provided with the starting and ending voltage, this function attempts to
1774 * calculate the time in microseconds required to rise or fall to this new
1777 int regulator_set_voltage_time(struct regulator
*regulator
,
1778 int old_uV
, int new_uV
)
1780 struct regulator_dev
*rdev
= regulator
->rdev
;
1781 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1787 /* Currently requires operations to do this */
1788 if (!ops
->list_voltage
|| !ops
->set_voltage_time_sel
1789 || !rdev
->desc
->n_voltages
)
1792 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
1793 /* We only look for exact voltage matches here */
1794 voltage
= regulator_list_voltage(regulator
, i
);
1799 if (voltage
== old_uV
)
1801 if (voltage
== new_uV
)
1805 if (old_sel
< 0 || new_sel
< 0)
1808 return ops
->set_voltage_time_sel(rdev
, old_sel
, new_sel
);
1810 EXPORT_SYMBOL_GPL(regulator_set_voltage_time
);
1813 * regulator_sync_voltage - re-apply last regulator output voltage
1814 * @regulator: regulator source
1816 * Re-apply the last configured voltage. This is intended to be used
1817 * where some external control source the consumer is cooperating with
1818 * has caused the configured voltage to change.
1820 int regulator_sync_voltage(struct regulator
*regulator
)
1822 struct regulator_dev
*rdev
= regulator
->rdev
;
1823 int ret
, min_uV
, max_uV
;
1825 mutex_lock(&rdev
->mutex
);
1827 if (!rdev
->desc
->ops
->set_voltage
&&
1828 !rdev
->desc
->ops
->set_voltage_sel
) {
1833 /* This is only going to work if we've had a voltage configured. */
1834 if (!regulator
->min_uV
&& !regulator
->max_uV
) {
1839 min_uV
= regulator
->min_uV
;
1840 max_uV
= regulator
->max_uV
;
1842 /* This should be a paranoia check... */
1843 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1847 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
1851 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
1854 mutex_unlock(&rdev
->mutex
);
1857 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
1859 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
1863 if (rdev
->desc
->ops
->get_voltage_sel
) {
1864 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
1867 return rdev
->desc
->ops
->list_voltage(rdev
, sel
);
1869 if (rdev
->desc
->ops
->get_voltage
)
1870 return rdev
->desc
->ops
->get_voltage(rdev
);
1876 * regulator_get_voltage - get regulator output voltage
1877 * @regulator: regulator source
1879 * This returns the current regulator voltage in uV.
1881 * NOTE: If the regulator is disabled it will return the voltage value. This
1882 * function should not be used to determine regulator state.
1884 int regulator_get_voltage(struct regulator
*regulator
)
1888 mutex_lock(®ulator
->rdev
->mutex
);
1890 ret
= _regulator_get_voltage(regulator
->rdev
);
1892 mutex_unlock(®ulator
->rdev
->mutex
);
1896 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
1899 * regulator_set_current_limit - set regulator output current limit
1900 * @regulator: regulator source
1901 * @min_uA: Minimuum supported current in uA
1902 * @max_uA: Maximum supported current in uA
1904 * Sets current sink to the desired output current. This can be set during
1905 * any regulator state. IOW, regulator can be disabled or enabled.
1907 * If the regulator is enabled then the current will change to the new value
1908 * immediately otherwise if the regulator is disabled the regulator will
1909 * output at the new current when enabled.
1911 * NOTE: Regulator system constraints must be set for this regulator before
1912 * calling this function otherwise this call will fail.
1914 int regulator_set_current_limit(struct regulator
*regulator
,
1915 int min_uA
, int max_uA
)
1917 struct regulator_dev
*rdev
= regulator
->rdev
;
1920 mutex_lock(&rdev
->mutex
);
1923 if (!rdev
->desc
->ops
->set_current_limit
) {
1928 /* constraints check */
1929 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
1933 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
1935 mutex_unlock(&rdev
->mutex
);
1938 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
1940 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
1944 mutex_lock(&rdev
->mutex
);
1947 if (!rdev
->desc
->ops
->get_current_limit
) {
1952 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
1954 mutex_unlock(&rdev
->mutex
);
1959 * regulator_get_current_limit - get regulator output current
1960 * @regulator: regulator source
1962 * This returns the current supplied by the specified current sink in uA.
1964 * NOTE: If the regulator is disabled it will return the current value. This
1965 * function should not be used to determine regulator state.
1967 int regulator_get_current_limit(struct regulator
*regulator
)
1969 return _regulator_get_current_limit(regulator
->rdev
);
1971 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
1974 * regulator_set_mode - set regulator operating mode
1975 * @regulator: regulator source
1976 * @mode: operating mode - one of the REGULATOR_MODE constants
1978 * Set regulator operating mode to increase regulator efficiency or improve
1979 * regulation performance.
1981 * NOTE: Regulator system constraints must be set for this regulator before
1982 * calling this function otherwise this call will fail.
1984 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
1986 struct regulator_dev
*rdev
= regulator
->rdev
;
1988 int regulator_curr_mode
;
1990 mutex_lock(&rdev
->mutex
);
1993 if (!rdev
->desc
->ops
->set_mode
) {
1998 /* return if the same mode is requested */
1999 if (rdev
->desc
->ops
->get_mode
) {
2000 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
2001 if (regulator_curr_mode
== mode
) {
2007 /* constraints check */
2008 ret
= regulator_check_mode(rdev
, mode
);
2012 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
2014 mutex_unlock(&rdev
->mutex
);
2017 EXPORT_SYMBOL_GPL(regulator_set_mode
);
2019 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
2023 mutex_lock(&rdev
->mutex
);
2026 if (!rdev
->desc
->ops
->get_mode
) {
2031 ret
= rdev
->desc
->ops
->get_mode(rdev
);
2033 mutex_unlock(&rdev
->mutex
);
2038 * regulator_get_mode - get regulator operating mode
2039 * @regulator: regulator source
2041 * Get the current regulator operating mode.
2043 unsigned int regulator_get_mode(struct regulator
*regulator
)
2045 return _regulator_get_mode(regulator
->rdev
);
2047 EXPORT_SYMBOL_GPL(regulator_get_mode
);
2050 * regulator_set_optimum_mode - set regulator optimum operating mode
2051 * @regulator: regulator source
2052 * @uA_load: load current
2054 * Notifies the regulator core of a new device load. This is then used by
2055 * DRMS (if enabled by constraints) to set the most efficient regulator
2056 * operating mode for the new regulator loading.
2058 * Consumer devices notify their supply regulator of the maximum power
2059 * they will require (can be taken from device datasheet in the power
2060 * consumption tables) when they change operational status and hence power
2061 * state. Examples of operational state changes that can affect power
2062 * consumption are :-
2064 * o Device is opened / closed.
2065 * o Device I/O is about to begin or has just finished.
2066 * o Device is idling in between work.
2068 * This information is also exported via sysfs to userspace.
2070 * DRMS will sum the total requested load on the regulator and change
2071 * to the most efficient operating mode if platform constraints allow.
2073 * Returns the new regulator mode or error.
2075 int regulator_set_optimum_mode(struct regulator
*regulator
, int uA_load
)
2077 struct regulator_dev
*rdev
= regulator
->rdev
;
2078 struct regulator
*consumer
;
2079 int ret
, output_uV
, input_uV
, total_uA_load
= 0;
2082 mutex_lock(&rdev
->mutex
);
2084 regulator
->uA_load
= uA_load
;
2085 ret
= regulator_check_drms(rdev
);
2091 if (!rdev
->desc
->ops
->get_optimum_mode
)
2094 /* get output voltage */
2095 output_uV
= _regulator_get_voltage(rdev
);
2096 if (output_uV
<= 0) {
2097 rdev_err(rdev
, "invalid output voltage found\n");
2101 /* get input voltage */
2104 input_uV
= _regulator_get_voltage(rdev
->supply
);
2106 input_uV
= rdev
->constraints
->input_uV
;
2107 if (input_uV
<= 0) {
2108 rdev_err(rdev
, "invalid input voltage found\n");
2112 /* calc total requested load for this regulator */
2113 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
)
2114 total_uA_load
+= consumer
->uA_load
;
2116 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
,
2117 input_uV
, output_uV
,
2119 ret
= regulator_check_mode(rdev
, mode
);
2121 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2122 total_uA_load
, input_uV
, output_uV
);
2126 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
2128 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
2133 mutex_unlock(&rdev
->mutex
);
2136 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode
);
2139 * regulator_register_notifier - register regulator event notifier
2140 * @regulator: regulator source
2141 * @nb: notifier block
2143 * Register notifier block to receive regulator events.
2145 int regulator_register_notifier(struct regulator
*regulator
,
2146 struct notifier_block
*nb
)
2148 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
2151 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
2154 * regulator_unregister_notifier - unregister regulator event notifier
2155 * @regulator: regulator source
2156 * @nb: notifier block
2158 * Unregister regulator event notifier block.
2160 int regulator_unregister_notifier(struct regulator
*regulator
,
2161 struct notifier_block
*nb
)
2163 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
2166 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
2168 /* notify regulator consumers and downstream regulator consumers.
2169 * Note mutex must be held by caller.
2171 static void _notifier_call_chain(struct regulator_dev
*rdev
,
2172 unsigned long event
, void *data
)
2174 struct regulator_dev
*_rdev
;
2176 /* call rdev chain first */
2177 blocking_notifier_call_chain(&rdev
->notifier
, event
, NULL
);
2179 /* now notify regulator we supply */
2180 list_for_each_entry(_rdev
, &rdev
->supply_list
, slist
) {
2181 mutex_lock(&_rdev
->mutex
);
2182 _notifier_call_chain(_rdev
, event
, data
);
2183 mutex_unlock(&_rdev
->mutex
);
2188 * regulator_bulk_get - get multiple regulator consumers
2190 * @dev: Device to supply
2191 * @num_consumers: Number of consumers to register
2192 * @consumers: Configuration of consumers; clients are stored here.
2194 * @return 0 on success, an errno on failure.
2196 * This helper function allows drivers to get several regulator
2197 * consumers in one operation. If any of the regulators cannot be
2198 * acquired then any regulators that were allocated will be freed
2199 * before returning to the caller.
2201 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
2202 struct regulator_bulk_data
*consumers
)
2207 for (i
= 0; i
< num_consumers
; i
++)
2208 consumers
[i
].consumer
= NULL
;
2210 for (i
= 0; i
< num_consumers
; i
++) {
2211 consumers
[i
].consumer
= regulator_get(dev
,
2212 consumers
[i
].supply
);
2213 if (IS_ERR(consumers
[i
].consumer
)) {
2214 ret
= PTR_ERR(consumers
[i
].consumer
);
2215 dev_err(dev
, "Failed to get supply '%s': %d\n",
2216 consumers
[i
].supply
, ret
);
2217 consumers
[i
].consumer
= NULL
;
2225 for (i
= 0; i
< num_consumers
&& consumers
[i
].consumer
; i
++)
2226 regulator_put(consumers
[i
].consumer
);
2230 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
2233 * regulator_bulk_enable - enable multiple regulator consumers
2235 * @num_consumers: Number of consumers
2236 * @consumers: Consumer data; clients are stored here.
2237 * @return 0 on success, an errno on failure
2239 * This convenience API allows consumers to enable multiple regulator
2240 * clients in a single API call. If any consumers cannot be enabled
2241 * then any others that were enabled will be disabled again prior to
2244 int regulator_bulk_enable(int num_consumers
,
2245 struct regulator_bulk_data
*consumers
)
2250 for (i
= 0; i
< num_consumers
; i
++) {
2251 ret
= regulator_enable(consumers
[i
].consumer
);
2259 pr_err("Failed to enable %s: %d\n", consumers
[i
].supply
, ret
);
2260 for (--i
; i
>= 0; --i
)
2261 regulator_disable(consumers
[i
].consumer
);
2265 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
2268 * regulator_bulk_disable - disable multiple regulator consumers
2270 * @num_consumers: Number of consumers
2271 * @consumers: Consumer data; clients are stored here.
2272 * @return 0 on success, an errno on failure
2274 * This convenience API allows consumers to disable multiple regulator
2275 * clients in a single API call. If any consumers cannot be enabled
2276 * then any others that were disabled will be disabled again prior to
2279 int regulator_bulk_disable(int num_consumers
,
2280 struct regulator_bulk_data
*consumers
)
2285 for (i
= 0; i
< num_consumers
; i
++) {
2286 ret
= regulator_disable(consumers
[i
].consumer
);
2294 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
2295 for (--i
; i
>= 0; --i
)
2296 regulator_enable(consumers
[i
].consumer
);
2300 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
2303 * regulator_bulk_free - free multiple regulator consumers
2305 * @num_consumers: Number of consumers
2306 * @consumers: Consumer data; clients are stored here.
2308 * This convenience API allows consumers to free multiple regulator
2309 * clients in a single API call.
2311 void regulator_bulk_free(int num_consumers
,
2312 struct regulator_bulk_data
*consumers
)
2316 for (i
= 0; i
< num_consumers
; i
++) {
2317 regulator_put(consumers
[i
].consumer
);
2318 consumers
[i
].consumer
= NULL
;
2321 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
2324 * regulator_notifier_call_chain - call regulator event notifier
2325 * @rdev: regulator source
2326 * @event: notifier block
2327 * @data: callback-specific data.
2329 * Called by regulator drivers to notify clients a regulator event has
2330 * occurred. We also notify regulator clients downstream.
2331 * Note lock must be held by caller.
2333 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
2334 unsigned long event
, void *data
)
2336 _notifier_call_chain(rdev
, event
, data
);
2340 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
2343 * regulator_mode_to_status - convert a regulator mode into a status
2345 * @mode: Mode to convert
2347 * Convert a regulator mode into a status.
2349 int regulator_mode_to_status(unsigned int mode
)
2352 case REGULATOR_MODE_FAST
:
2353 return REGULATOR_STATUS_FAST
;
2354 case REGULATOR_MODE_NORMAL
:
2355 return REGULATOR_STATUS_NORMAL
;
2356 case REGULATOR_MODE_IDLE
:
2357 return REGULATOR_STATUS_IDLE
;
2358 case REGULATOR_STATUS_STANDBY
:
2359 return REGULATOR_STATUS_STANDBY
;
2364 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
2367 * To avoid cluttering sysfs (and memory) with useless state, only
2368 * create attributes that can be meaningfully displayed.
2370 static int add_regulator_attributes(struct regulator_dev
*rdev
)
2372 struct device
*dev
= &rdev
->dev
;
2373 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2376 /* some attributes need specific methods to be displayed */
2377 if (ops
->get_voltage
|| ops
->get_voltage_sel
) {
2378 status
= device_create_file(dev
, &dev_attr_microvolts
);
2382 if (ops
->get_current_limit
) {
2383 status
= device_create_file(dev
, &dev_attr_microamps
);
2387 if (ops
->get_mode
) {
2388 status
= device_create_file(dev
, &dev_attr_opmode
);
2392 if (ops
->is_enabled
) {
2393 status
= device_create_file(dev
, &dev_attr_state
);
2397 if (ops
->get_status
) {
2398 status
= device_create_file(dev
, &dev_attr_status
);
2403 /* some attributes are type-specific */
2404 if (rdev
->desc
->type
== REGULATOR_CURRENT
) {
2405 status
= device_create_file(dev
, &dev_attr_requested_microamps
);
2410 /* all the other attributes exist to support constraints;
2411 * don't show them if there are no constraints, or if the
2412 * relevant supporting methods are missing.
2414 if (!rdev
->constraints
)
2417 /* constraints need specific supporting methods */
2418 if (ops
->set_voltage
|| ops
->set_voltage_sel
) {
2419 status
= device_create_file(dev
, &dev_attr_min_microvolts
);
2422 status
= device_create_file(dev
, &dev_attr_max_microvolts
);
2426 if (ops
->set_current_limit
) {
2427 status
= device_create_file(dev
, &dev_attr_min_microamps
);
2430 status
= device_create_file(dev
, &dev_attr_max_microamps
);
2435 /* suspend mode constraints need multiple supporting methods */
2436 if (!(ops
->set_suspend_enable
&& ops
->set_suspend_disable
))
2439 status
= device_create_file(dev
, &dev_attr_suspend_standby_state
);
2442 status
= device_create_file(dev
, &dev_attr_suspend_mem_state
);
2445 status
= device_create_file(dev
, &dev_attr_suspend_disk_state
);
2449 if (ops
->set_suspend_voltage
) {
2450 status
= device_create_file(dev
,
2451 &dev_attr_suspend_standby_microvolts
);
2454 status
= device_create_file(dev
,
2455 &dev_attr_suspend_mem_microvolts
);
2458 status
= device_create_file(dev
,
2459 &dev_attr_suspend_disk_microvolts
);
2464 if (ops
->set_suspend_mode
) {
2465 status
= device_create_file(dev
,
2466 &dev_attr_suspend_standby_mode
);
2469 status
= device_create_file(dev
,
2470 &dev_attr_suspend_mem_mode
);
2473 status
= device_create_file(dev
,
2474 &dev_attr_suspend_disk_mode
);
2482 static void rdev_init_debugfs(struct regulator_dev
*rdev
)
2484 #ifdef CONFIG_DEBUG_FS
2485 rdev
->debugfs
= debugfs_create_dir(rdev_get_name(rdev
), debugfs_root
);
2486 if (IS_ERR(rdev
->debugfs
) || !rdev
->debugfs
) {
2487 rdev_warn(rdev
, "Failed to create debugfs directory\n");
2488 rdev
->debugfs
= NULL
;
2492 debugfs_create_u32("use_count", 0444, rdev
->debugfs
,
2494 debugfs_create_u32("open_count", 0444, rdev
->debugfs
,
2500 * regulator_register - register regulator
2501 * @regulator_desc: regulator to register
2502 * @dev: struct device for the regulator
2503 * @init_data: platform provided init data, passed through by driver
2504 * @driver_data: private regulator data
2506 * Called by regulator drivers to register a regulator.
2507 * Returns 0 on success.
2509 struct regulator_dev
*regulator_register(struct regulator_desc
*regulator_desc
,
2510 struct device
*dev
, const struct regulator_init_data
*init_data
,
2513 static atomic_t regulator_no
= ATOMIC_INIT(0);
2514 struct regulator_dev
*rdev
;
2517 if (regulator_desc
== NULL
)
2518 return ERR_PTR(-EINVAL
);
2520 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
2521 return ERR_PTR(-EINVAL
);
2523 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
2524 regulator_desc
->type
!= REGULATOR_CURRENT
)
2525 return ERR_PTR(-EINVAL
);
2528 return ERR_PTR(-EINVAL
);
2530 /* Only one of each should be implemented */
2531 WARN_ON(regulator_desc
->ops
->get_voltage
&&
2532 regulator_desc
->ops
->get_voltage_sel
);
2533 WARN_ON(regulator_desc
->ops
->set_voltage
&&
2534 regulator_desc
->ops
->set_voltage_sel
);
2536 /* If we're using selectors we must implement list_voltage. */
2537 if (regulator_desc
->ops
->get_voltage_sel
&&
2538 !regulator_desc
->ops
->list_voltage
) {
2539 return ERR_PTR(-EINVAL
);
2541 if (regulator_desc
->ops
->set_voltage_sel
&&
2542 !regulator_desc
->ops
->list_voltage
) {
2543 return ERR_PTR(-EINVAL
);
2546 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
2548 return ERR_PTR(-ENOMEM
);
2550 mutex_lock(®ulator_list_mutex
);
2552 mutex_init(&rdev
->mutex
);
2553 rdev
->reg_data
= driver_data
;
2554 rdev
->owner
= regulator_desc
->owner
;
2555 rdev
->desc
= regulator_desc
;
2556 INIT_LIST_HEAD(&rdev
->consumer_list
);
2557 INIT_LIST_HEAD(&rdev
->supply_list
);
2558 INIT_LIST_HEAD(&rdev
->list
);
2559 INIT_LIST_HEAD(&rdev
->slist
);
2560 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
2562 /* preform any regulator specific init */
2563 if (init_data
->regulator_init
) {
2564 ret
= init_data
->regulator_init(rdev
->reg_data
);
2569 /* register with sysfs */
2570 rdev
->dev
.class = ®ulator_class
;
2571 rdev
->dev
.parent
= dev
;
2572 dev_set_name(&rdev
->dev
, "regulator.%d",
2573 atomic_inc_return(®ulator_no
) - 1);
2574 ret
= device_register(&rdev
->dev
);
2576 put_device(&rdev
->dev
);
2580 dev_set_drvdata(&rdev
->dev
, rdev
);
2582 /* set regulator constraints */
2583 ret
= set_machine_constraints(rdev
, &init_data
->constraints
);
2587 /* add attributes supported by this regulator */
2588 ret
= add_regulator_attributes(rdev
);
2592 /* set supply regulator if it exists */
2593 if (init_data
->supply_regulator
&& init_data
->supply_regulator_dev
) {
2595 "Supply regulator specified by both name and dev\n");
2600 if (init_data
->supply_regulator
) {
2601 struct regulator_dev
*r
;
2604 list_for_each_entry(r
, ®ulator_list
, list
) {
2605 if (strcmp(rdev_get_name(r
),
2606 init_data
->supply_regulator
) == 0) {
2613 dev_err(dev
, "Failed to find supply %s\n",
2614 init_data
->supply_regulator
);
2619 ret
= set_supply(rdev
, r
);
2624 if (init_data
->supply_regulator_dev
) {
2625 dev_warn(dev
, "Uses supply_regulator_dev instead of regulator_supply\n");
2626 ret
= set_supply(rdev
,
2627 dev_get_drvdata(init_data
->supply_regulator_dev
));
2632 /* add consumers devices */
2633 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
2634 ret
= set_consumer_device_supply(rdev
,
2635 init_data
->consumer_supplies
[i
].dev
,
2636 init_data
->consumer_supplies
[i
].dev_name
,
2637 init_data
->consumer_supplies
[i
].supply
);
2639 dev_err(dev
, "Failed to set supply %s\n",
2640 init_data
->consumer_supplies
[i
].supply
);
2641 goto unset_supplies
;
2645 list_add(&rdev
->list
, ®ulator_list
);
2647 rdev_init_debugfs(rdev
);
2649 mutex_unlock(®ulator_list_mutex
);
2653 unset_regulator_supplies(rdev
);
2656 device_unregister(&rdev
->dev
);
2657 /* device core frees rdev */
2658 rdev
= ERR_PTR(ret
);
2663 rdev
= ERR_PTR(ret
);
2666 EXPORT_SYMBOL_GPL(regulator_register
);
2669 * regulator_unregister - unregister regulator
2670 * @rdev: regulator to unregister
2672 * Called by regulator drivers to unregister a regulator.
2674 void regulator_unregister(struct regulator_dev
*rdev
)
2679 mutex_lock(®ulator_list_mutex
);
2680 #ifdef CONFIG_DEBUG_FS
2681 debugfs_remove_recursive(rdev
->debugfs
);
2683 WARN_ON(rdev
->open_count
);
2684 unset_regulator_supplies(rdev
);
2685 list_del(&rdev
->list
);
2687 sysfs_remove_link(&rdev
->dev
.kobj
, "supply");
2688 device_unregister(&rdev
->dev
);
2689 kfree(rdev
->constraints
);
2690 mutex_unlock(®ulator_list_mutex
);
2692 EXPORT_SYMBOL_GPL(regulator_unregister
);
2695 * regulator_suspend_prepare - prepare regulators for system wide suspend
2696 * @state: system suspend state
2698 * Configure each regulator with it's suspend operating parameters for state.
2699 * This will usually be called by machine suspend code prior to supending.
2701 int regulator_suspend_prepare(suspend_state_t state
)
2703 struct regulator_dev
*rdev
;
2706 /* ON is handled by regulator active state */
2707 if (state
== PM_SUSPEND_ON
)
2710 mutex_lock(®ulator_list_mutex
);
2711 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2713 mutex_lock(&rdev
->mutex
);
2714 ret
= suspend_prepare(rdev
, state
);
2715 mutex_unlock(&rdev
->mutex
);
2718 rdev_err(rdev
, "failed to prepare\n");
2723 mutex_unlock(®ulator_list_mutex
);
2726 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
2729 * regulator_suspend_finish - resume regulators from system wide suspend
2731 * Turn on regulators that might be turned off by regulator_suspend_prepare
2732 * and that should be turned on according to the regulators properties.
2734 int regulator_suspend_finish(void)
2736 struct regulator_dev
*rdev
;
2739 mutex_lock(®ulator_list_mutex
);
2740 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2741 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2743 mutex_lock(&rdev
->mutex
);
2744 if ((rdev
->use_count
> 0 || rdev
->constraints
->always_on
) &&
2746 error
= ops
->enable(rdev
);
2750 if (!has_full_constraints
)
2754 if (ops
->is_enabled
&& !ops
->is_enabled(rdev
))
2757 error
= ops
->disable(rdev
);
2762 mutex_unlock(&rdev
->mutex
);
2764 mutex_unlock(®ulator_list_mutex
);
2767 EXPORT_SYMBOL_GPL(regulator_suspend_finish
);
2770 * regulator_has_full_constraints - the system has fully specified constraints
2772 * Calling this function will cause the regulator API to disable all
2773 * regulators which have a zero use count and don't have an always_on
2774 * constraint in a late_initcall.
2776 * The intention is that this will become the default behaviour in a
2777 * future kernel release so users are encouraged to use this facility
2780 void regulator_has_full_constraints(void)
2782 has_full_constraints
= 1;
2784 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
2787 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2789 * Calling this function will cause the regulator API to provide a
2790 * dummy regulator to consumers if no physical regulator is found,
2791 * allowing most consumers to proceed as though a regulator were
2792 * configured. This allows systems such as those with software
2793 * controllable regulators for the CPU core only to be brought up more
2796 void regulator_use_dummy_regulator(void)
2798 board_wants_dummy_regulator
= true;
2800 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator
);
2803 * rdev_get_drvdata - get rdev regulator driver data
2806 * Get rdev regulator driver private data. This call can be used in the
2807 * regulator driver context.
2809 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
2811 return rdev
->reg_data
;
2813 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
2816 * regulator_get_drvdata - get regulator driver data
2817 * @regulator: regulator
2819 * Get regulator driver private data. This call can be used in the consumer
2820 * driver context when non API regulator specific functions need to be called.
2822 void *regulator_get_drvdata(struct regulator
*regulator
)
2824 return regulator
->rdev
->reg_data
;
2826 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
2829 * regulator_set_drvdata - set regulator driver data
2830 * @regulator: regulator
2833 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
2835 regulator
->rdev
->reg_data
= data
;
2837 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
2840 * regulator_get_id - get regulator ID
2843 int rdev_get_id(struct regulator_dev
*rdev
)
2845 return rdev
->desc
->id
;
2847 EXPORT_SYMBOL_GPL(rdev_get_id
);
2849 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
2853 EXPORT_SYMBOL_GPL(rdev_get_dev
);
2855 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
2857 return reg_init_data
->driver_data
;
2859 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
2861 static int __init
regulator_init(void)
2865 ret
= class_register(®ulator_class
);
2867 #ifdef CONFIG_DEBUG_FS
2868 debugfs_root
= debugfs_create_dir("regulator", NULL
);
2869 if (IS_ERR(debugfs_root
) || !debugfs_root
) {
2870 pr_warn("regulator: Failed to create debugfs directory\n");
2871 debugfs_root
= NULL
;
2875 regulator_dummy_init();
2880 /* init early to allow our consumers to complete system booting */
2881 core_initcall(regulator_init
);
2883 static int __init
regulator_init_complete(void)
2885 struct regulator_dev
*rdev
;
2886 struct regulator_ops
*ops
;
2887 struct regulation_constraints
*c
;
2890 mutex_lock(®ulator_list_mutex
);
2892 /* If we have a full configuration then disable any regulators
2893 * which are not in use or always_on. This will become the
2894 * default behaviour in the future.
2896 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2897 ops
= rdev
->desc
->ops
;
2898 c
= rdev
->constraints
;
2900 if (!ops
->disable
|| (c
&& c
->always_on
))
2903 mutex_lock(&rdev
->mutex
);
2905 if (rdev
->use_count
)
2908 /* If we can't read the status assume it's on. */
2909 if (ops
->is_enabled
)
2910 enabled
= ops
->is_enabled(rdev
);
2917 if (has_full_constraints
) {
2918 /* We log since this may kill the system if it
2920 rdev_info(rdev
, "disabling\n");
2921 ret
= ops
->disable(rdev
);
2923 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
2926 /* The intention is that in future we will
2927 * assume that full constraints are provided
2928 * so warn even if we aren't going to do
2931 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
2935 mutex_unlock(&rdev
->mutex
);
2938 mutex_unlock(®ulator_list_mutex
);
2942 late_initcall(regulator_init_complete
);