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/device.h>
21 #include <linux/slab.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/machine.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
35 #define rdev_err(rdev, fmt, ...) \
36 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
37 #define rdev_warn(rdev, fmt, ...) \
38 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
39 #define rdev_info(rdev, fmt, ...) \
40 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
41 #define rdev_dbg(rdev, fmt, ...) \
42 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 static DEFINE_MUTEX(regulator_list_mutex
);
45 static LIST_HEAD(regulator_list
);
46 static LIST_HEAD(regulator_map_list
);
47 static int has_full_constraints
;
48 static bool board_wants_dummy_regulator
;
51 * struct regulator_map
53 * Used to provide symbolic supply names to devices.
55 struct regulator_map
{
56 struct list_head list
;
57 const char *dev_name
; /* The dev_name() for the consumer */
59 struct regulator_dev
*regulator
;
65 * One for each consumer device.
69 struct list_head list
;
74 struct device_attribute dev_attr
;
75 struct regulator_dev
*rdev
;
78 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
79 static int _regulator_disable(struct regulator_dev
*rdev
,
80 struct regulator_dev
**supply_rdev_ptr
);
81 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
82 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
83 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
84 static void _notifier_call_chain(struct regulator_dev
*rdev
,
85 unsigned long event
, void *data
);
86 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
87 int min_uV
, int max_uV
);
89 static const char *rdev_get_name(struct regulator_dev
*rdev
)
91 if (rdev
->constraints
&& rdev
->constraints
->name
)
92 return rdev
->constraints
->name
;
93 else if (rdev
->desc
->name
)
94 return rdev
->desc
->name
;
99 /* gets the regulator for a given consumer device */
100 static struct regulator
*get_device_regulator(struct device
*dev
)
102 struct regulator
*regulator
= NULL
;
103 struct regulator_dev
*rdev
;
105 mutex_lock(®ulator_list_mutex
);
106 list_for_each_entry(rdev
, ®ulator_list
, list
) {
107 mutex_lock(&rdev
->mutex
);
108 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
109 if (regulator
->dev
== dev
) {
110 mutex_unlock(&rdev
->mutex
);
111 mutex_unlock(®ulator_list_mutex
);
115 mutex_unlock(&rdev
->mutex
);
117 mutex_unlock(®ulator_list_mutex
);
121 /* Platform voltage constraint check */
122 static int regulator_check_voltage(struct regulator_dev
*rdev
,
123 int *min_uV
, int *max_uV
)
125 BUG_ON(*min_uV
> *max_uV
);
127 if (!rdev
->constraints
) {
128 rdev_err(rdev
, "no constraints\n");
131 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
132 rdev_err(rdev
, "operation not allowed\n");
136 if (*max_uV
> rdev
->constraints
->max_uV
)
137 *max_uV
= rdev
->constraints
->max_uV
;
138 if (*min_uV
< rdev
->constraints
->min_uV
)
139 *min_uV
= rdev
->constraints
->min_uV
;
141 if (*min_uV
> *max_uV
)
147 /* Make sure we select a voltage that suits the needs of all
148 * regulator consumers
150 static int regulator_check_consumers(struct regulator_dev
*rdev
,
151 int *min_uV
, int *max_uV
)
153 struct regulator
*regulator
;
155 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
156 if (*max_uV
> regulator
->max_uV
)
157 *max_uV
= regulator
->max_uV
;
158 if (*min_uV
< regulator
->min_uV
)
159 *min_uV
= regulator
->min_uV
;
162 if (*min_uV
> *max_uV
)
168 /* current constraint check */
169 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
170 int *min_uA
, int *max_uA
)
172 BUG_ON(*min_uA
> *max_uA
);
174 if (!rdev
->constraints
) {
175 rdev_err(rdev
, "no constraints\n");
178 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
179 rdev_err(rdev
, "operation not allowed\n");
183 if (*max_uA
> rdev
->constraints
->max_uA
)
184 *max_uA
= rdev
->constraints
->max_uA
;
185 if (*min_uA
< rdev
->constraints
->min_uA
)
186 *min_uA
= rdev
->constraints
->min_uA
;
188 if (*min_uA
> *max_uA
)
194 /* operating mode constraint check */
195 static int regulator_check_mode(struct regulator_dev
*rdev
, int mode
)
198 case REGULATOR_MODE_FAST
:
199 case REGULATOR_MODE_NORMAL
:
200 case REGULATOR_MODE_IDLE
:
201 case REGULATOR_MODE_STANDBY
:
207 if (!rdev
->constraints
) {
208 rdev_err(rdev
, "no constraints\n");
211 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
212 rdev_err(rdev
, "operation not allowed\n");
215 if (!(rdev
->constraints
->valid_modes_mask
& mode
)) {
216 rdev_err(rdev
, "invalid mode %x\n", mode
);
222 /* dynamic regulator mode switching constraint check */
223 static int regulator_check_drms(struct regulator_dev
*rdev
)
225 if (!rdev
->constraints
) {
226 rdev_err(rdev
, "no constraints\n");
229 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
230 rdev_err(rdev
, "operation not allowed\n");
236 static ssize_t
device_requested_uA_show(struct device
*dev
,
237 struct device_attribute
*attr
, char *buf
)
239 struct regulator
*regulator
;
241 regulator
= get_device_regulator(dev
);
242 if (regulator
== NULL
)
245 return sprintf(buf
, "%d\n", regulator
->uA_load
);
248 static ssize_t
regulator_uV_show(struct device
*dev
,
249 struct device_attribute
*attr
, char *buf
)
251 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
254 mutex_lock(&rdev
->mutex
);
255 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
256 mutex_unlock(&rdev
->mutex
);
260 static DEVICE_ATTR(microvolts
, 0444, regulator_uV_show
, NULL
);
262 static ssize_t
regulator_uA_show(struct device
*dev
,
263 struct device_attribute
*attr
, char *buf
)
265 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
267 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
269 static DEVICE_ATTR(microamps
, 0444, regulator_uA_show
, NULL
);
271 static ssize_t
regulator_name_show(struct device
*dev
,
272 struct device_attribute
*attr
, char *buf
)
274 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
276 return sprintf(buf
, "%s\n", rdev_get_name(rdev
));
279 static ssize_t
regulator_print_opmode(char *buf
, int mode
)
282 case REGULATOR_MODE_FAST
:
283 return sprintf(buf
, "fast\n");
284 case REGULATOR_MODE_NORMAL
:
285 return sprintf(buf
, "normal\n");
286 case REGULATOR_MODE_IDLE
:
287 return sprintf(buf
, "idle\n");
288 case REGULATOR_MODE_STANDBY
:
289 return sprintf(buf
, "standby\n");
291 return sprintf(buf
, "unknown\n");
294 static ssize_t
regulator_opmode_show(struct device
*dev
,
295 struct device_attribute
*attr
, char *buf
)
297 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
299 return regulator_print_opmode(buf
, _regulator_get_mode(rdev
));
301 static DEVICE_ATTR(opmode
, 0444, regulator_opmode_show
, NULL
);
303 static ssize_t
regulator_print_state(char *buf
, int state
)
306 return sprintf(buf
, "enabled\n");
308 return sprintf(buf
, "disabled\n");
310 return sprintf(buf
, "unknown\n");
313 static ssize_t
regulator_state_show(struct device
*dev
,
314 struct device_attribute
*attr
, char *buf
)
316 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
319 mutex_lock(&rdev
->mutex
);
320 ret
= regulator_print_state(buf
, _regulator_is_enabled(rdev
));
321 mutex_unlock(&rdev
->mutex
);
325 static DEVICE_ATTR(state
, 0444, regulator_state_show
, NULL
);
327 static ssize_t
regulator_status_show(struct device
*dev
,
328 struct device_attribute
*attr
, char *buf
)
330 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
334 status
= rdev
->desc
->ops
->get_status(rdev
);
339 case REGULATOR_STATUS_OFF
:
342 case REGULATOR_STATUS_ON
:
345 case REGULATOR_STATUS_ERROR
:
348 case REGULATOR_STATUS_FAST
:
351 case REGULATOR_STATUS_NORMAL
:
354 case REGULATOR_STATUS_IDLE
:
357 case REGULATOR_STATUS_STANDBY
:
364 return sprintf(buf
, "%s\n", label
);
366 static DEVICE_ATTR(status
, 0444, regulator_status_show
, NULL
);
368 static ssize_t
regulator_min_uA_show(struct device
*dev
,
369 struct device_attribute
*attr
, char *buf
)
371 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
373 if (!rdev
->constraints
)
374 return sprintf(buf
, "constraint not defined\n");
376 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
378 static DEVICE_ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
);
380 static ssize_t
regulator_max_uA_show(struct device
*dev
,
381 struct device_attribute
*attr
, char *buf
)
383 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
385 if (!rdev
->constraints
)
386 return sprintf(buf
, "constraint not defined\n");
388 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
390 static DEVICE_ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
);
392 static ssize_t
regulator_min_uV_show(struct device
*dev
,
393 struct device_attribute
*attr
, char *buf
)
395 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
397 if (!rdev
->constraints
)
398 return sprintf(buf
, "constraint not defined\n");
400 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
402 static DEVICE_ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
);
404 static ssize_t
regulator_max_uV_show(struct device
*dev
,
405 struct device_attribute
*attr
, char *buf
)
407 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
409 if (!rdev
->constraints
)
410 return sprintf(buf
, "constraint not defined\n");
412 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
414 static DEVICE_ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
);
416 static ssize_t
regulator_total_uA_show(struct device
*dev
,
417 struct device_attribute
*attr
, char *buf
)
419 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
420 struct regulator
*regulator
;
423 mutex_lock(&rdev
->mutex
);
424 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
425 uA
+= regulator
->uA_load
;
426 mutex_unlock(&rdev
->mutex
);
427 return sprintf(buf
, "%d\n", uA
);
429 static DEVICE_ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
);
431 static ssize_t
regulator_num_users_show(struct device
*dev
,
432 struct device_attribute
*attr
, char *buf
)
434 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
435 return sprintf(buf
, "%d\n", rdev
->use_count
);
438 static ssize_t
regulator_type_show(struct device
*dev
,
439 struct device_attribute
*attr
, char *buf
)
441 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
443 switch (rdev
->desc
->type
) {
444 case REGULATOR_VOLTAGE
:
445 return sprintf(buf
, "voltage\n");
446 case REGULATOR_CURRENT
:
447 return sprintf(buf
, "current\n");
449 return sprintf(buf
, "unknown\n");
452 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
453 struct device_attribute
*attr
, char *buf
)
455 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
457 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
459 static DEVICE_ATTR(suspend_mem_microvolts
, 0444,
460 regulator_suspend_mem_uV_show
, NULL
);
462 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
463 struct device_attribute
*attr
, char *buf
)
465 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
467 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
469 static DEVICE_ATTR(suspend_disk_microvolts
, 0444,
470 regulator_suspend_disk_uV_show
, NULL
);
472 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
473 struct device_attribute
*attr
, char *buf
)
475 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
477 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
479 static DEVICE_ATTR(suspend_standby_microvolts
, 0444,
480 regulator_suspend_standby_uV_show
, NULL
);
482 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
483 struct device_attribute
*attr
, char *buf
)
485 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
487 return regulator_print_opmode(buf
,
488 rdev
->constraints
->state_mem
.mode
);
490 static DEVICE_ATTR(suspend_mem_mode
, 0444,
491 regulator_suspend_mem_mode_show
, NULL
);
493 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
494 struct device_attribute
*attr
, char *buf
)
496 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
498 return regulator_print_opmode(buf
,
499 rdev
->constraints
->state_disk
.mode
);
501 static DEVICE_ATTR(suspend_disk_mode
, 0444,
502 regulator_suspend_disk_mode_show
, NULL
);
504 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
505 struct device_attribute
*attr
, char *buf
)
507 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
509 return regulator_print_opmode(buf
,
510 rdev
->constraints
->state_standby
.mode
);
512 static DEVICE_ATTR(suspend_standby_mode
, 0444,
513 regulator_suspend_standby_mode_show
, NULL
);
515 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
516 struct device_attribute
*attr
, char *buf
)
518 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
520 return regulator_print_state(buf
,
521 rdev
->constraints
->state_mem
.enabled
);
523 static DEVICE_ATTR(suspend_mem_state
, 0444,
524 regulator_suspend_mem_state_show
, NULL
);
526 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
527 struct device_attribute
*attr
, char *buf
)
529 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
531 return regulator_print_state(buf
,
532 rdev
->constraints
->state_disk
.enabled
);
534 static DEVICE_ATTR(suspend_disk_state
, 0444,
535 regulator_suspend_disk_state_show
, NULL
);
537 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
538 struct device_attribute
*attr
, char *buf
)
540 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
542 return regulator_print_state(buf
,
543 rdev
->constraints
->state_standby
.enabled
);
545 static DEVICE_ATTR(suspend_standby_state
, 0444,
546 regulator_suspend_standby_state_show
, NULL
);
550 * These are the only attributes are present for all regulators.
551 * Other attributes are a function of regulator functionality.
553 static struct device_attribute regulator_dev_attrs
[] = {
554 __ATTR(name
, 0444, regulator_name_show
, NULL
),
555 __ATTR(num_users
, 0444, regulator_num_users_show
, NULL
),
556 __ATTR(type
, 0444, regulator_type_show
, NULL
),
560 static void regulator_dev_release(struct device
*dev
)
562 struct regulator_dev
*rdev
= dev_get_drvdata(dev
);
566 static struct class regulator_class
= {
568 .dev_release
= regulator_dev_release
,
569 .dev_attrs
= regulator_dev_attrs
,
572 /* Calculate the new optimum regulator operating mode based on the new total
573 * consumer load. All locks held by caller */
574 static void drms_uA_update(struct regulator_dev
*rdev
)
576 struct regulator
*sibling
;
577 int current_uA
= 0, output_uV
, input_uV
, err
;
580 err
= regulator_check_drms(rdev
);
581 if (err
< 0 || !rdev
->desc
->ops
->get_optimum_mode
||
582 (!rdev
->desc
->ops
->get_voltage
&&
583 !rdev
->desc
->ops
->get_voltage_sel
) ||
584 !rdev
->desc
->ops
->set_mode
)
587 /* get output voltage */
588 output_uV
= _regulator_get_voltage(rdev
);
592 /* get input voltage */
595 input_uV
= _regulator_get_voltage(rdev
);
597 input_uV
= rdev
->constraints
->input_uV
;
601 /* calc total requested load */
602 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
603 current_uA
+= sibling
->uA_load
;
605 /* now get the optimum mode for our new total regulator load */
606 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
607 output_uV
, current_uA
);
609 /* check the new mode is allowed */
610 err
= regulator_check_mode(rdev
, mode
);
612 rdev
->desc
->ops
->set_mode(rdev
, mode
);
615 static int suspend_set_state(struct regulator_dev
*rdev
,
616 struct regulator_state
*rstate
)
621 can_set_state
= rdev
->desc
->ops
->set_suspend_enable
&&
622 rdev
->desc
->ops
->set_suspend_disable
;
624 /* If we have no suspend mode configration don't set anything;
625 * only warn if the driver actually makes the suspend mode
628 if (!rstate
->enabled
&& !rstate
->disabled
) {
630 rdev_warn(rdev
, "No configuration\n");
634 if (rstate
->enabled
&& rstate
->disabled
) {
635 rdev_err(rdev
, "invalid configuration\n");
639 if (!can_set_state
) {
640 rdev_err(rdev
, "no way to set suspend state\n");
645 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
647 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
649 rdev_err(rdev
, "failed to enabled/disable\n");
653 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
654 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
656 rdev_err(rdev
, "failed to set voltage\n");
661 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
662 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
664 rdev_err(rdev
, "failed to set mode\n");
671 /* locks held by caller */
672 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
674 if (!rdev
->constraints
)
678 case PM_SUSPEND_STANDBY
:
679 return suspend_set_state(rdev
,
680 &rdev
->constraints
->state_standby
);
682 return suspend_set_state(rdev
,
683 &rdev
->constraints
->state_mem
);
685 return suspend_set_state(rdev
,
686 &rdev
->constraints
->state_disk
);
692 static void print_constraints(struct regulator_dev
*rdev
)
694 struct regulation_constraints
*constraints
= rdev
->constraints
;
699 if (constraints
->min_uV
&& constraints
->max_uV
) {
700 if (constraints
->min_uV
== constraints
->max_uV
)
701 count
+= sprintf(buf
+ count
, "%d mV ",
702 constraints
->min_uV
/ 1000);
704 count
+= sprintf(buf
+ count
, "%d <--> %d mV ",
705 constraints
->min_uV
/ 1000,
706 constraints
->max_uV
/ 1000);
709 if (!constraints
->min_uV
||
710 constraints
->min_uV
!= constraints
->max_uV
) {
711 ret
= _regulator_get_voltage(rdev
);
713 count
+= sprintf(buf
+ count
, "at %d mV ", ret
/ 1000);
716 if (constraints
->min_uA
&& constraints
->max_uA
) {
717 if (constraints
->min_uA
== constraints
->max_uA
)
718 count
+= sprintf(buf
+ count
, "%d mA ",
719 constraints
->min_uA
/ 1000);
721 count
+= sprintf(buf
+ count
, "%d <--> %d mA ",
722 constraints
->min_uA
/ 1000,
723 constraints
->max_uA
/ 1000);
726 if (!constraints
->min_uA
||
727 constraints
->min_uA
!= constraints
->max_uA
) {
728 ret
= _regulator_get_current_limit(rdev
);
730 count
+= sprintf(buf
+ count
, "at %d mA ", ret
/ 1000);
733 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
734 count
+= sprintf(buf
+ count
, "fast ");
735 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
736 count
+= sprintf(buf
+ count
, "normal ");
737 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
738 count
+= sprintf(buf
+ count
, "idle ");
739 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
740 count
+= sprintf(buf
+ count
, "standby");
742 rdev_info(rdev
, "%s\n", buf
);
745 static int machine_constraints_voltage(struct regulator_dev
*rdev
,
746 struct regulation_constraints
*constraints
)
748 struct regulator_ops
*ops
= rdev
->desc
->ops
;
751 /* do we need to apply the constraint voltage */
752 if (rdev
->constraints
->apply_uV
&&
753 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
) {
754 ret
= _regulator_do_set_voltage(rdev
,
755 rdev
->constraints
->min_uV
,
756 rdev
->constraints
->max_uV
);
758 rdev_err(rdev
, "failed to apply %duV constraint\n",
759 rdev
->constraints
->min_uV
);
760 rdev
->constraints
= NULL
;
765 /* constrain machine-level voltage specs to fit
766 * the actual range supported by this regulator.
768 if (ops
->list_voltage
&& rdev
->desc
->n_voltages
) {
769 int count
= rdev
->desc
->n_voltages
;
771 int min_uV
= INT_MAX
;
772 int max_uV
= INT_MIN
;
773 int cmin
= constraints
->min_uV
;
774 int cmax
= constraints
->max_uV
;
776 /* it's safe to autoconfigure fixed-voltage supplies
777 and the constraints are used by list_voltage. */
778 if (count
== 1 && !cmin
) {
781 constraints
->min_uV
= cmin
;
782 constraints
->max_uV
= cmax
;
785 /* voltage constraints are optional */
786 if ((cmin
== 0) && (cmax
== 0))
789 /* else require explicit machine-level constraints */
790 if (cmin
<= 0 || cmax
<= 0 || cmax
< cmin
) {
791 rdev_err(rdev
, "invalid voltage constraints\n");
795 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
796 for (i
= 0; i
< count
; i
++) {
799 value
= ops
->list_voltage(rdev
, i
);
803 /* maybe adjust [min_uV..max_uV] */
804 if (value
>= cmin
&& value
< min_uV
)
806 if (value
<= cmax
&& value
> max_uV
)
810 /* final: [min_uV..max_uV] valid iff constraints valid */
811 if (max_uV
< min_uV
) {
812 rdev_err(rdev
, "unsupportable voltage constraints\n");
816 /* use regulator's subset of machine constraints */
817 if (constraints
->min_uV
< min_uV
) {
818 rdev_dbg(rdev
, "override min_uV, %d -> %d\n",
819 constraints
->min_uV
, min_uV
);
820 constraints
->min_uV
= min_uV
;
822 if (constraints
->max_uV
> max_uV
) {
823 rdev_dbg(rdev
, "override max_uV, %d -> %d\n",
824 constraints
->max_uV
, max_uV
);
825 constraints
->max_uV
= max_uV
;
833 * set_machine_constraints - sets regulator constraints
834 * @rdev: regulator source
835 * @constraints: constraints to apply
837 * Allows platform initialisation code to define and constrain
838 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
839 * Constraints *must* be set by platform code in order for some
840 * regulator operations to proceed i.e. set_voltage, set_current_limit,
843 static int set_machine_constraints(struct regulator_dev
*rdev
,
844 const struct regulation_constraints
*constraints
)
847 struct regulator_ops
*ops
= rdev
->desc
->ops
;
849 rdev
->constraints
= kmemdup(constraints
, sizeof(*constraints
),
851 if (!rdev
->constraints
)
854 ret
= machine_constraints_voltage(rdev
, rdev
->constraints
);
858 /* do we need to setup our suspend state */
859 if (constraints
->initial_state
) {
860 ret
= suspend_prepare(rdev
, rdev
->constraints
->initial_state
);
862 rdev_err(rdev
, "failed to set suspend state\n");
863 rdev
->constraints
= NULL
;
868 if (constraints
->initial_mode
) {
869 if (!ops
->set_mode
) {
870 rdev_err(rdev
, "no set_mode operation\n");
875 ret
= ops
->set_mode(rdev
, rdev
->constraints
->initial_mode
);
877 rdev_err(rdev
, "failed to set initial mode: %d\n", ret
);
882 /* If the constraints say the regulator should be on at this point
883 * and we have control then make sure it is enabled.
885 if ((rdev
->constraints
->always_on
|| rdev
->constraints
->boot_on
) &&
887 ret
= ops
->enable(rdev
);
889 rdev_err(rdev
, "failed to enable\n");
890 rdev
->constraints
= NULL
;
895 print_constraints(rdev
);
901 * set_supply - set regulator supply regulator
902 * @rdev: regulator name
903 * @supply_rdev: supply regulator name
905 * Called by platform initialisation code to set the supply regulator for this
906 * regulator. This ensures that a regulators supply will also be enabled by the
907 * core if it's child is enabled.
909 static int set_supply(struct regulator_dev
*rdev
,
910 struct regulator_dev
*supply_rdev
)
914 err
= sysfs_create_link(&rdev
->dev
.kobj
, &supply_rdev
->dev
.kobj
,
917 rdev_err(rdev
, "could not add device link %s err %d\n",
918 supply_rdev
->dev
.kobj
.name
, err
);
921 rdev
->supply
= supply_rdev
;
922 list_add(&rdev
->slist
, &supply_rdev
->supply_list
);
928 * set_consumer_device_supply - Bind a regulator to a symbolic supply
929 * @rdev: regulator source
930 * @consumer_dev: device the supply applies to
931 * @consumer_dev_name: dev_name() string for device supply applies to
932 * @supply: symbolic name for supply
934 * Allows platform initialisation code to map physical regulator
935 * sources to symbolic names for supplies for use by devices. Devices
936 * should use these symbolic names to request regulators, avoiding the
937 * need to provide board-specific regulator names as platform data.
939 * Only one of consumer_dev and consumer_dev_name may be specified.
941 static int set_consumer_device_supply(struct regulator_dev
*rdev
,
942 struct device
*consumer_dev
, const char *consumer_dev_name
,
945 struct regulator_map
*node
;
948 if (consumer_dev
&& consumer_dev_name
)
951 if (!consumer_dev_name
&& consumer_dev
)
952 consumer_dev_name
= dev_name(consumer_dev
);
957 if (consumer_dev_name
!= NULL
)
962 list_for_each_entry(node
, ®ulator_map_list
, list
) {
963 if (node
->dev_name
&& consumer_dev_name
) {
964 if (strcmp(node
->dev_name
, consumer_dev_name
) != 0)
966 } else if (node
->dev_name
|| consumer_dev_name
) {
970 if (strcmp(node
->supply
, supply
) != 0)
973 dev_dbg(consumer_dev
, "%s/%s is '%s' supply; fail %s/%s\n",
974 dev_name(&node
->regulator
->dev
),
975 node
->regulator
->desc
->name
,
977 dev_name(&rdev
->dev
), rdev_get_name(rdev
));
981 node
= kzalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
985 node
->regulator
= rdev
;
986 node
->supply
= supply
;
989 node
->dev_name
= kstrdup(consumer_dev_name
, GFP_KERNEL
);
990 if (node
->dev_name
== NULL
) {
996 list_add(&node
->list
, ®ulator_map_list
);
1000 static void unset_regulator_supplies(struct regulator_dev
*rdev
)
1002 struct regulator_map
*node
, *n
;
1004 list_for_each_entry_safe(node
, n
, ®ulator_map_list
, list
) {
1005 if (rdev
== node
->regulator
) {
1006 list_del(&node
->list
);
1007 kfree(node
->dev_name
);
1013 #define REG_STR_SIZE 32
1015 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
1017 const char *supply_name
)
1019 struct regulator
*regulator
;
1020 char buf
[REG_STR_SIZE
];
1023 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
1024 if (regulator
== NULL
)
1027 mutex_lock(&rdev
->mutex
);
1028 regulator
->rdev
= rdev
;
1029 list_add(®ulator
->list
, &rdev
->consumer_list
);
1032 /* create a 'requested_microamps_name' sysfs entry */
1033 size
= scnprintf(buf
, REG_STR_SIZE
, "microamps_requested_%s",
1035 if (size
>= REG_STR_SIZE
)
1038 regulator
->dev
= dev
;
1039 sysfs_attr_init(®ulator
->dev_attr
.attr
);
1040 regulator
->dev_attr
.attr
.name
= kstrdup(buf
, GFP_KERNEL
);
1041 if (regulator
->dev_attr
.attr
.name
== NULL
)
1044 regulator
->dev_attr
.attr
.mode
= 0444;
1045 regulator
->dev_attr
.show
= device_requested_uA_show
;
1046 err
= device_create_file(dev
, ®ulator
->dev_attr
);
1048 rdev_warn(rdev
, "could not add regulator_dev requested microamps sysfs entry\n");
1052 /* also add a link to the device sysfs entry */
1053 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
1054 dev
->kobj
.name
, supply_name
);
1055 if (size
>= REG_STR_SIZE
)
1058 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
1059 if (regulator
->supply_name
== NULL
)
1062 err
= sysfs_create_link(&rdev
->dev
.kobj
, &dev
->kobj
,
1065 rdev_warn(rdev
, "could not add device link %s err %d\n",
1066 dev
->kobj
.name
, err
);
1070 mutex_unlock(&rdev
->mutex
);
1073 kfree(regulator
->supply_name
);
1075 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1077 kfree(regulator
->dev_attr
.attr
.name
);
1079 list_del(®ulator
->list
);
1081 mutex_unlock(&rdev
->mutex
);
1085 static int _regulator_get_enable_time(struct regulator_dev
*rdev
)
1087 if (!rdev
->desc
->ops
->enable_time
)
1089 return rdev
->desc
->ops
->enable_time(rdev
);
1092 /* Internal regulator request function */
1093 static struct regulator
*_regulator_get(struct device
*dev
, const char *id
,
1096 struct regulator_dev
*rdev
;
1097 struct regulator_map
*map
;
1098 struct regulator
*regulator
= ERR_PTR(-ENODEV
);
1099 const char *devname
= NULL
;
1103 pr_err("get() with no identifier\n");
1108 devname
= dev_name(dev
);
1110 mutex_lock(®ulator_list_mutex
);
1112 list_for_each_entry(map
, ®ulator_map_list
, list
) {
1113 /* If the mapping has a device set up it must match */
1114 if (map
->dev_name
&&
1115 (!devname
|| strcmp(map
->dev_name
, devname
)))
1118 if (strcmp(map
->supply
, id
) == 0) {
1119 rdev
= map
->regulator
;
1124 if (board_wants_dummy_regulator
) {
1125 rdev
= dummy_regulator_rdev
;
1129 #ifdef CONFIG_REGULATOR_DUMMY
1131 devname
= "deviceless";
1133 /* If the board didn't flag that it was fully constrained then
1134 * substitute in a dummy regulator so consumers can continue.
1136 if (!has_full_constraints
) {
1137 pr_warn("%s supply %s not found, using dummy regulator\n",
1139 rdev
= dummy_regulator_rdev
;
1144 mutex_unlock(®ulator_list_mutex
);
1148 if (rdev
->exclusive
) {
1149 regulator
= ERR_PTR(-EPERM
);
1153 if (exclusive
&& rdev
->open_count
) {
1154 regulator
= ERR_PTR(-EBUSY
);
1158 if (!try_module_get(rdev
->owner
))
1161 regulator
= create_regulator(rdev
, dev
, id
);
1162 if (regulator
== NULL
) {
1163 regulator
= ERR_PTR(-ENOMEM
);
1164 module_put(rdev
->owner
);
1169 rdev
->exclusive
= 1;
1171 ret
= _regulator_is_enabled(rdev
);
1173 rdev
->use_count
= 1;
1175 rdev
->use_count
= 0;
1179 mutex_unlock(®ulator_list_mutex
);
1185 * regulator_get - lookup and obtain a reference to a regulator.
1186 * @dev: device for regulator "consumer"
1187 * @id: Supply name or regulator ID.
1189 * Returns a struct regulator corresponding to the regulator producer,
1190 * or IS_ERR() condition containing errno.
1192 * Use of supply names configured via regulator_set_device_supply() is
1193 * strongly encouraged. It is recommended that the supply name used
1194 * should match the name used for the supply and/or the relevant
1195 * device pins in the datasheet.
1197 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
1199 return _regulator_get(dev
, id
, 0);
1201 EXPORT_SYMBOL_GPL(regulator_get
);
1204 * regulator_get_exclusive - obtain exclusive access to a regulator.
1205 * @dev: device for regulator "consumer"
1206 * @id: Supply name or regulator ID.
1208 * Returns a struct regulator corresponding to the regulator producer,
1209 * or IS_ERR() condition containing errno. Other consumers will be
1210 * unable to obtain this reference is held and the use count for the
1211 * regulator will be initialised to reflect the current state of the
1214 * This is intended for use by consumers which cannot tolerate shared
1215 * use of the regulator such as those which need to force the
1216 * regulator off for correct operation of the hardware they are
1219 * Use of supply names configured via regulator_set_device_supply() is
1220 * strongly encouraged. It is recommended that the supply name used
1221 * should match the name used for the supply and/or the relevant
1222 * device pins in the datasheet.
1224 struct regulator
*regulator_get_exclusive(struct device
*dev
, const char *id
)
1226 return _regulator_get(dev
, id
, 1);
1228 EXPORT_SYMBOL_GPL(regulator_get_exclusive
);
1231 * regulator_put - "free" the regulator source
1232 * @regulator: regulator source
1234 * Note: drivers must ensure that all regulator_enable calls made on this
1235 * regulator source are balanced by regulator_disable calls prior to calling
1238 void regulator_put(struct regulator
*regulator
)
1240 struct regulator_dev
*rdev
;
1242 if (regulator
== NULL
|| IS_ERR(regulator
))
1245 mutex_lock(®ulator_list_mutex
);
1246 rdev
= regulator
->rdev
;
1248 /* remove any sysfs entries */
1249 if (regulator
->dev
) {
1250 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
1251 kfree(regulator
->supply_name
);
1252 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
1253 kfree(regulator
->dev_attr
.attr
.name
);
1255 list_del(®ulator
->list
);
1259 rdev
->exclusive
= 0;
1261 module_put(rdev
->owner
);
1262 mutex_unlock(®ulator_list_mutex
);
1264 EXPORT_SYMBOL_GPL(regulator_put
);
1266 static int _regulator_can_change_status(struct regulator_dev
*rdev
)
1268 if (!rdev
->constraints
)
1271 if (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_STATUS
)
1277 /* locks held by regulator_enable() */
1278 static int _regulator_enable(struct regulator_dev
*rdev
)
1282 if (rdev
->use_count
== 0) {
1283 /* do we need to enable the supply regulator first */
1285 mutex_lock(&rdev
->supply
->mutex
);
1286 ret
= _regulator_enable(rdev
->supply
);
1287 mutex_unlock(&rdev
->supply
->mutex
);
1289 rdev_err(rdev
, "failed to enable: %d\n", ret
);
1295 /* check voltage and requested load before enabling */
1296 if (rdev
->constraints
&&
1297 (rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
))
1298 drms_uA_update(rdev
);
1300 if (rdev
->use_count
== 0) {
1301 /* The regulator may on if it's not switchable or left on */
1302 ret
= _regulator_is_enabled(rdev
);
1303 if (ret
== -EINVAL
|| ret
== 0) {
1304 if (!_regulator_can_change_status(rdev
))
1307 if (!rdev
->desc
->ops
->enable
)
1310 /* Query before enabling in case configuration
1312 ret
= _regulator_get_enable_time(rdev
);
1316 rdev_warn(rdev
, "enable_time() failed: %d\n",
1321 trace_regulator_enable(rdev_get_name(rdev
));
1323 /* Allow the regulator to ramp; it would be useful
1324 * to extend this for bulk operations so that the
1325 * regulators can ramp together. */
1326 ret
= rdev
->desc
->ops
->enable(rdev
);
1330 trace_regulator_enable_delay(rdev_get_name(rdev
));
1332 if (delay
>= 1000) {
1333 mdelay(delay
/ 1000);
1334 udelay(delay
% 1000);
1339 trace_regulator_enable_complete(rdev_get_name(rdev
));
1341 } else if (ret
< 0) {
1342 rdev_err(rdev
, "is_enabled() failed: %d\n", ret
);
1345 /* Fallthrough on positive return values - already enabled */
1354 * regulator_enable - enable regulator output
1355 * @regulator: regulator source
1357 * Request that the regulator be enabled with the regulator output at
1358 * the predefined voltage or current value. Calls to regulator_enable()
1359 * must be balanced with calls to regulator_disable().
1361 * NOTE: the output value can be set by other drivers, boot loader or may be
1362 * hardwired in the regulator.
1364 int regulator_enable(struct regulator
*regulator
)
1366 struct regulator_dev
*rdev
= regulator
->rdev
;
1369 mutex_lock(&rdev
->mutex
);
1370 ret
= _regulator_enable(rdev
);
1371 mutex_unlock(&rdev
->mutex
);
1374 EXPORT_SYMBOL_GPL(regulator_enable
);
1376 /* locks held by regulator_disable() */
1377 static int _regulator_disable(struct regulator_dev
*rdev
,
1378 struct regulator_dev
**supply_rdev_ptr
)
1381 *supply_rdev_ptr
= NULL
;
1383 if (WARN(rdev
->use_count
<= 0,
1384 "unbalanced disables for %s\n", rdev_get_name(rdev
)))
1387 /* are we the last user and permitted to disable ? */
1388 if (rdev
->use_count
== 1 &&
1389 (rdev
->constraints
&& !rdev
->constraints
->always_on
)) {
1391 /* we are last user */
1392 if (_regulator_can_change_status(rdev
) &&
1393 rdev
->desc
->ops
->disable
) {
1394 trace_regulator_disable(rdev_get_name(rdev
));
1396 ret
= rdev
->desc
->ops
->disable(rdev
);
1398 rdev_err(rdev
, "failed to disable\n");
1402 trace_regulator_disable_complete(rdev_get_name(rdev
));
1404 _notifier_call_chain(rdev
, REGULATOR_EVENT_DISABLE
,
1408 /* decrease our supplies ref count and disable if required */
1409 *supply_rdev_ptr
= rdev
->supply
;
1411 rdev
->use_count
= 0;
1412 } else if (rdev
->use_count
> 1) {
1414 if (rdev
->constraints
&&
1415 (rdev
->constraints
->valid_ops_mask
&
1416 REGULATOR_CHANGE_DRMS
))
1417 drms_uA_update(rdev
);
1425 * regulator_disable - disable regulator output
1426 * @regulator: regulator source
1428 * Disable the regulator output voltage or current. Calls to
1429 * regulator_enable() must be balanced with calls to
1430 * regulator_disable().
1432 * NOTE: this will only disable the regulator output if no other consumer
1433 * devices have it enabled, the regulator device supports disabling and
1434 * machine constraints permit this operation.
1436 int regulator_disable(struct regulator
*regulator
)
1438 struct regulator_dev
*rdev
= regulator
->rdev
;
1439 struct regulator_dev
*supply_rdev
= NULL
;
1442 mutex_lock(&rdev
->mutex
);
1443 ret
= _regulator_disable(rdev
, &supply_rdev
);
1444 mutex_unlock(&rdev
->mutex
);
1446 /* decrease our supplies ref count and disable if required */
1447 while (supply_rdev
!= NULL
) {
1450 mutex_lock(&rdev
->mutex
);
1451 _regulator_disable(rdev
, &supply_rdev
);
1452 mutex_unlock(&rdev
->mutex
);
1457 EXPORT_SYMBOL_GPL(regulator_disable
);
1459 /* locks held by regulator_force_disable() */
1460 static int _regulator_force_disable(struct regulator_dev
*rdev
,
1461 struct regulator_dev
**supply_rdev_ptr
)
1466 if (rdev
->desc
->ops
->disable
) {
1467 /* ah well, who wants to live forever... */
1468 ret
= rdev
->desc
->ops
->disable(rdev
);
1470 rdev_err(rdev
, "failed to force disable\n");
1473 /* notify other consumers that power has been forced off */
1474 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
|
1475 REGULATOR_EVENT_DISABLE
, NULL
);
1478 /* decrease our supplies ref count and disable if required */
1479 *supply_rdev_ptr
= rdev
->supply
;
1481 rdev
->use_count
= 0;
1486 * regulator_force_disable - force disable regulator output
1487 * @regulator: regulator source
1489 * Forcibly disable the regulator output voltage or current.
1490 * NOTE: this *will* disable the regulator output even if other consumer
1491 * devices have it enabled. This should be used for situations when device
1492 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1494 int regulator_force_disable(struct regulator
*regulator
)
1496 struct regulator_dev
*supply_rdev
= NULL
;
1499 mutex_lock(®ulator
->rdev
->mutex
);
1500 regulator
->uA_load
= 0;
1501 ret
= _regulator_force_disable(regulator
->rdev
, &supply_rdev
);
1502 mutex_unlock(®ulator
->rdev
->mutex
);
1505 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev
)));
1509 EXPORT_SYMBOL_GPL(regulator_force_disable
);
1511 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
1513 /* If we don't know then assume that the regulator is always on */
1514 if (!rdev
->desc
->ops
->is_enabled
)
1517 return rdev
->desc
->ops
->is_enabled(rdev
);
1521 * regulator_is_enabled - is the regulator output enabled
1522 * @regulator: regulator source
1524 * Returns positive if the regulator driver backing the source/client
1525 * has requested that the device be enabled, zero if it hasn't, else a
1526 * negative errno code.
1528 * Note that the device backing this regulator handle can have multiple
1529 * users, so it might be enabled even if regulator_enable() was never
1530 * called for this particular source.
1532 int regulator_is_enabled(struct regulator
*regulator
)
1536 mutex_lock(®ulator
->rdev
->mutex
);
1537 ret
= _regulator_is_enabled(regulator
->rdev
);
1538 mutex_unlock(®ulator
->rdev
->mutex
);
1542 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
1545 * regulator_count_voltages - count regulator_list_voltage() selectors
1546 * @regulator: regulator source
1548 * Returns number of selectors, or negative errno. Selectors are
1549 * numbered starting at zero, and typically correspond to bitfields
1550 * in hardware registers.
1552 int regulator_count_voltages(struct regulator
*regulator
)
1554 struct regulator_dev
*rdev
= regulator
->rdev
;
1556 return rdev
->desc
->n_voltages
? : -EINVAL
;
1558 EXPORT_SYMBOL_GPL(regulator_count_voltages
);
1561 * regulator_list_voltage - enumerate supported voltages
1562 * @regulator: regulator source
1563 * @selector: identify voltage to list
1564 * Context: can sleep
1566 * Returns a voltage that can be passed to @regulator_set_voltage(),
1567 * zero if this selector code can't be used on this system, or a
1570 int regulator_list_voltage(struct regulator
*regulator
, unsigned selector
)
1572 struct regulator_dev
*rdev
= regulator
->rdev
;
1573 struct regulator_ops
*ops
= rdev
->desc
->ops
;
1576 if (!ops
->list_voltage
|| selector
>= rdev
->desc
->n_voltages
)
1579 mutex_lock(&rdev
->mutex
);
1580 ret
= ops
->list_voltage(rdev
, selector
);
1581 mutex_unlock(&rdev
->mutex
);
1584 if (ret
< rdev
->constraints
->min_uV
)
1586 else if (ret
> rdev
->constraints
->max_uV
)
1592 EXPORT_SYMBOL_GPL(regulator_list_voltage
);
1595 * regulator_is_supported_voltage - check if a voltage range can be supported
1597 * @regulator: Regulator to check.
1598 * @min_uV: Minimum required voltage in uV.
1599 * @max_uV: Maximum required voltage in uV.
1601 * Returns a boolean or a negative error code.
1603 int regulator_is_supported_voltage(struct regulator
*regulator
,
1604 int min_uV
, int max_uV
)
1606 int i
, voltages
, ret
;
1608 ret
= regulator_count_voltages(regulator
);
1613 for (i
= 0; i
< voltages
; i
++) {
1614 ret
= regulator_list_voltage(regulator
, i
);
1616 if (ret
>= min_uV
&& ret
<= max_uV
)
1623 static int _regulator_do_set_voltage(struct regulator_dev
*rdev
,
1624 int min_uV
, int max_uV
)
1627 unsigned int selector
;
1629 trace_regulator_set_voltage(rdev_get_name(rdev
), min_uV
, max_uV
);
1631 if (rdev
->desc
->ops
->set_voltage
) {
1632 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
,
1635 if (rdev
->desc
->ops
->list_voltage
)
1636 selector
= rdev
->desc
->ops
->list_voltage(rdev
,
1640 } else if (rdev
->desc
->ops
->set_voltage_sel
) {
1641 int best_val
= INT_MAX
;
1646 /* Find the smallest voltage that falls within the specified
1649 for (i
= 0; i
< rdev
->desc
->n_voltages
; i
++) {
1650 ret
= rdev
->desc
->ops
->list_voltage(rdev
, i
);
1654 if (ret
< best_val
&& ret
>= min_uV
&& ret
<= max_uV
) {
1660 if (best_val
!= INT_MAX
) {
1661 ret
= rdev
->desc
->ops
->set_voltage_sel(rdev
, selector
);
1662 selector
= best_val
;
1671 _notifier_call_chain(rdev
, REGULATOR_EVENT_VOLTAGE_CHANGE
,
1674 trace_regulator_set_voltage_complete(rdev_get_name(rdev
), selector
);
1680 * regulator_set_voltage - set regulator output voltage
1681 * @regulator: regulator source
1682 * @min_uV: Minimum required voltage in uV
1683 * @max_uV: Maximum acceptable voltage in uV
1685 * Sets a voltage regulator to the desired output voltage. This can be set
1686 * during any regulator state. IOW, regulator can be disabled or enabled.
1688 * If the regulator is enabled then the voltage will change to the new value
1689 * immediately otherwise if the regulator is disabled the regulator will
1690 * output at the new voltage when enabled.
1692 * NOTE: If the regulator is shared between several devices then the lowest
1693 * request voltage that meets the system constraints will be used.
1694 * Regulator system constraints must be set for this regulator before
1695 * calling this function otherwise this call will fail.
1697 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
1699 struct regulator_dev
*rdev
= regulator
->rdev
;
1702 mutex_lock(&rdev
->mutex
);
1704 /* If we're setting the same range as last time the change
1705 * should be a noop (some cpufreq implementations use the same
1706 * voltage for multiple frequencies, for example).
1708 if (regulator
->min_uV
== min_uV
&& regulator
->max_uV
== max_uV
)
1712 if (!rdev
->desc
->ops
->set_voltage
&&
1713 !rdev
->desc
->ops
->set_voltage_sel
) {
1718 /* constraints check */
1719 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1722 regulator
->min_uV
= min_uV
;
1723 regulator
->max_uV
= max_uV
;
1725 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
1729 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
1732 mutex_unlock(&rdev
->mutex
);
1735 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
1738 * regulator_sync_voltage - re-apply last regulator output voltage
1739 * @regulator: regulator source
1741 * Re-apply the last configured voltage. This is intended to be used
1742 * where some external control source the consumer is cooperating with
1743 * has caused the configured voltage to change.
1745 int regulator_sync_voltage(struct regulator
*regulator
)
1747 struct regulator_dev
*rdev
= regulator
->rdev
;
1748 int ret
, min_uV
, max_uV
;
1750 mutex_lock(&rdev
->mutex
);
1752 if (!rdev
->desc
->ops
->set_voltage
&&
1753 !rdev
->desc
->ops
->set_voltage_sel
) {
1758 /* This is only going to work if we've had a voltage configured. */
1759 if (!regulator
->min_uV
&& !regulator
->max_uV
) {
1764 min_uV
= regulator
->min_uV
;
1765 max_uV
= regulator
->max_uV
;
1767 /* This should be a paranoia check... */
1768 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1772 ret
= regulator_check_consumers(rdev
, &min_uV
, &max_uV
);
1776 ret
= _regulator_do_set_voltage(rdev
, min_uV
, max_uV
);
1779 mutex_unlock(&rdev
->mutex
);
1782 EXPORT_SYMBOL_GPL(regulator_sync_voltage
);
1784 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
1788 if (rdev
->desc
->ops
->get_voltage_sel
) {
1789 sel
= rdev
->desc
->ops
->get_voltage_sel(rdev
);
1792 return rdev
->desc
->ops
->list_voltage(rdev
, sel
);
1794 if (rdev
->desc
->ops
->get_voltage
)
1795 return rdev
->desc
->ops
->get_voltage(rdev
);
1801 * regulator_get_voltage - get regulator output voltage
1802 * @regulator: regulator source
1804 * This returns the current regulator voltage in uV.
1806 * NOTE: If the regulator is disabled it will return the voltage value. This
1807 * function should not be used to determine regulator state.
1809 int regulator_get_voltage(struct regulator
*regulator
)
1813 mutex_lock(®ulator
->rdev
->mutex
);
1815 ret
= _regulator_get_voltage(regulator
->rdev
);
1817 mutex_unlock(®ulator
->rdev
->mutex
);
1821 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
1824 * regulator_set_current_limit - set regulator output current limit
1825 * @regulator: regulator source
1826 * @min_uA: Minimuum supported current in uA
1827 * @max_uA: Maximum supported current in uA
1829 * Sets current sink to the desired output current. This can be set during
1830 * any regulator state. IOW, regulator can be disabled or enabled.
1832 * If the regulator is enabled then the current will change to the new value
1833 * immediately otherwise if the regulator is disabled the regulator will
1834 * output at the new current when enabled.
1836 * NOTE: Regulator system constraints must be set for this regulator before
1837 * calling this function otherwise this call will fail.
1839 int regulator_set_current_limit(struct regulator
*regulator
,
1840 int min_uA
, int max_uA
)
1842 struct regulator_dev
*rdev
= regulator
->rdev
;
1845 mutex_lock(&rdev
->mutex
);
1848 if (!rdev
->desc
->ops
->set_current_limit
) {
1853 /* constraints check */
1854 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
1858 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
1860 mutex_unlock(&rdev
->mutex
);
1863 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
1865 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
1869 mutex_lock(&rdev
->mutex
);
1872 if (!rdev
->desc
->ops
->get_current_limit
) {
1877 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
1879 mutex_unlock(&rdev
->mutex
);
1884 * regulator_get_current_limit - get regulator output current
1885 * @regulator: regulator source
1887 * This returns the current supplied by the specified current sink in uA.
1889 * NOTE: If the regulator is disabled it will return the current value. This
1890 * function should not be used to determine regulator state.
1892 int regulator_get_current_limit(struct regulator
*regulator
)
1894 return _regulator_get_current_limit(regulator
->rdev
);
1896 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
1899 * regulator_set_mode - set regulator operating mode
1900 * @regulator: regulator source
1901 * @mode: operating mode - one of the REGULATOR_MODE constants
1903 * Set regulator operating mode to increase regulator efficiency or improve
1904 * regulation performance.
1906 * NOTE: Regulator system constraints must be set for this regulator before
1907 * calling this function otherwise this call will fail.
1909 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
1911 struct regulator_dev
*rdev
= regulator
->rdev
;
1913 int regulator_curr_mode
;
1915 mutex_lock(&rdev
->mutex
);
1918 if (!rdev
->desc
->ops
->set_mode
) {
1923 /* return if the same mode is requested */
1924 if (rdev
->desc
->ops
->get_mode
) {
1925 regulator_curr_mode
= rdev
->desc
->ops
->get_mode(rdev
);
1926 if (regulator_curr_mode
== mode
) {
1932 /* constraints check */
1933 ret
= regulator_check_mode(rdev
, mode
);
1937 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1939 mutex_unlock(&rdev
->mutex
);
1942 EXPORT_SYMBOL_GPL(regulator_set_mode
);
1944 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
1948 mutex_lock(&rdev
->mutex
);
1951 if (!rdev
->desc
->ops
->get_mode
) {
1956 ret
= rdev
->desc
->ops
->get_mode(rdev
);
1958 mutex_unlock(&rdev
->mutex
);
1963 * regulator_get_mode - get regulator operating mode
1964 * @regulator: regulator source
1966 * Get the current regulator operating mode.
1968 unsigned int regulator_get_mode(struct regulator
*regulator
)
1970 return _regulator_get_mode(regulator
->rdev
);
1972 EXPORT_SYMBOL_GPL(regulator_get_mode
);
1975 * regulator_set_optimum_mode - set regulator optimum operating mode
1976 * @regulator: regulator source
1977 * @uA_load: load current
1979 * Notifies the regulator core of a new device load. This is then used by
1980 * DRMS (if enabled by constraints) to set the most efficient regulator
1981 * operating mode for the new regulator loading.
1983 * Consumer devices notify their supply regulator of the maximum power
1984 * they will require (can be taken from device datasheet in the power
1985 * consumption tables) when they change operational status and hence power
1986 * state. Examples of operational state changes that can affect power
1987 * consumption are :-
1989 * o Device is opened / closed.
1990 * o Device I/O is about to begin or has just finished.
1991 * o Device is idling in between work.
1993 * This information is also exported via sysfs to userspace.
1995 * DRMS will sum the total requested load on the regulator and change
1996 * to the most efficient operating mode if platform constraints allow.
1998 * Returns the new regulator mode or error.
2000 int regulator_set_optimum_mode(struct regulator
*regulator
, int uA_load
)
2002 struct regulator_dev
*rdev
= regulator
->rdev
;
2003 struct regulator
*consumer
;
2004 int ret
, output_uV
, input_uV
, total_uA_load
= 0;
2007 mutex_lock(&rdev
->mutex
);
2009 regulator
->uA_load
= uA_load
;
2010 ret
= regulator_check_drms(rdev
);
2016 if (!rdev
->desc
->ops
->get_optimum_mode
)
2019 /* get output voltage */
2020 output_uV
= _regulator_get_voltage(rdev
);
2021 if (output_uV
<= 0) {
2022 rdev_err(rdev
, "invalid output voltage found\n");
2026 /* get input voltage */
2029 input_uV
= _regulator_get_voltage(rdev
->supply
);
2031 input_uV
= rdev
->constraints
->input_uV
;
2032 if (input_uV
<= 0) {
2033 rdev_err(rdev
, "invalid input voltage found\n");
2037 /* calc total requested load for this regulator */
2038 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
)
2039 total_uA_load
+= consumer
->uA_load
;
2041 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
,
2042 input_uV
, output_uV
,
2044 ret
= regulator_check_mode(rdev
, mode
);
2046 rdev_err(rdev
, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2047 total_uA_load
, input_uV
, output_uV
);
2051 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
2053 rdev_err(rdev
, "failed to set optimum mode %x\n", mode
);
2058 mutex_unlock(&rdev
->mutex
);
2061 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode
);
2064 * regulator_register_notifier - register regulator event notifier
2065 * @regulator: regulator source
2066 * @nb: notifier block
2068 * Register notifier block to receive regulator events.
2070 int regulator_register_notifier(struct regulator
*regulator
,
2071 struct notifier_block
*nb
)
2073 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
2076 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
2079 * regulator_unregister_notifier - unregister regulator event notifier
2080 * @regulator: regulator source
2081 * @nb: notifier block
2083 * Unregister regulator event notifier block.
2085 int regulator_unregister_notifier(struct regulator
*regulator
,
2086 struct notifier_block
*nb
)
2088 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
2091 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
2093 /* notify regulator consumers and downstream regulator consumers.
2094 * Note mutex must be held by caller.
2096 static void _notifier_call_chain(struct regulator_dev
*rdev
,
2097 unsigned long event
, void *data
)
2099 struct regulator_dev
*_rdev
;
2101 /* call rdev chain first */
2102 blocking_notifier_call_chain(&rdev
->notifier
, event
, NULL
);
2104 /* now notify regulator we supply */
2105 list_for_each_entry(_rdev
, &rdev
->supply_list
, slist
) {
2106 mutex_lock(&_rdev
->mutex
);
2107 _notifier_call_chain(_rdev
, event
, data
);
2108 mutex_unlock(&_rdev
->mutex
);
2113 * regulator_bulk_get - get multiple regulator consumers
2115 * @dev: Device to supply
2116 * @num_consumers: Number of consumers to register
2117 * @consumers: Configuration of consumers; clients are stored here.
2119 * @return 0 on success, an errno on failure.
2121 * This helper function allows drivers to get several regulator
2122 * consumers in one operation. If any of the regulators cannot be
2123 * acquired then any regulators that were allocated will be freed
2124 * before returning to the caller.
2126 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
2127 struct regulator_bulk_data
*consumers
)
2132 for (i
= 0; i
< num_consumers
; i
++)
2133 consumers
[i
].consumer
= NULL
;
2135 for (i
= 0; i
< num_consumers
; i
++) {
2136 consumers
[i
].consumer
= regulator_get(dev
,
2137 consumers
[i
].supply
);
2138 if (IS_ERR(consumers
[i
].consumer
)) {
2139 ret
= PTR_ERR(consumers
[i
].consumer
);
2140 dev_err(dev
, "Failed to get supply '%s': %d\n",
2141 consumers
[i
].supply
, ret
);
2142 consumers
[i
].consumer
= NULL
;
2150 for (i
= 0; i
< num_consumers
&& consumers
[i
].consumer
; i
++)
2151 regulator_put(consumers
[i
].consumer
);
2155 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
2158 * regulator_bulk_enable - enable multiple regulator consumers
2160 * @num_consumers: Number of consumers
2161 * @consumers: Consumer data; clients are stored here.
2162 * @return 0 on success, an errno on failure
2164 * This convenience API allows consumers to enable multiple regulator
2165 * clients in a single API call. If any consumers cannot be enabled
2166 * then any others that were enabled will be disabled again prior to
2169 int regulator_bulk_enable(int num_consumers
,
2170 struct regulator_bulk_data
*consumers
)
2175 for (i
= 0; i
< num_consumers
; i
++) {
2176 ret
= regulator_enable(consumers
[i
].consumer
);
2184 pr_err("Failed to enable %s: %d\n", consumers
[i
].supply
, ret
);
2185 for (--i
; i
>= 0; --i
)
2186 regulator_disable(consumers
[i
].consumer
);
2190 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
2193 * regulator_bulk_disable - disable multiple regulator consumers
2195 * @num_consumers: Number of consumers
2196 * @consumers: Consumer data; clients are stored here.
2197 * @return 0 on success, an errno on failure
2199 * This convenience API allows consumers to disable multiple regulator
2200 * clients in a single API call. If any consumers cannot be enabled
2201 * then any others that were disabled will be disabled again prior to
2204 int regulator_bulk_disable(int num_consumers
,
2205 struct regulator_bulk_data
*consumers
)
2210 for (i
= 0; i
< num_consumers
; i
++) {
2211 ret
= regulator_disable(consumers
[i
].consumer
);
2219 pr_err("Failed to disable %s: %d\n", consumers
[i
].supply
, ret
);
2220 for (--i
; i
>= 0; --i
)
2221 regulator_enable(consumers
[i
].consumer
);
2225 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
2228 * regulator_bulk_free - free multiple regulator consumers
2230 * @num_consumers: Number of consumers
2231 * @consumers: Consumer data; clients are stored here.
2233 * This convenience API allows consumers to free multiple regulator
2234 * clients in a single API call.
2236 void regulator_bulk_free(int num_consumers
,
2237 struct regulator_bulk_data
*consumers
)
2241 for (i
= 0; i
< num_consumers
; i
++) {
2242 regulator_put(consumers
[i
].consumer
);
2243 consumers
[i
].consumer
= NULL
;
2246 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
2249 * regulator_notifier_call_chain - call regulator event notifier
2250 * @rdev: regulator source
2251 * @event: notifier block
2252 * @data: callback-specific data.
2254 * Called by regulator drivers to notify clients a regulator event has
2255 * occurred. We also notify regulator clients downstream.
2256 * Note lock must be held by caller.
2258 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
2259 unsigned long event
, void *data
)
2261 _notifier_call_chain(rdev
, event
, data
);
2265 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
2268 * regulator_mode_to_status - convert a regulator mode into a status
2270 * @mode: Mode to convert
2272 * Convert a regulator mode into a status.
2274 int regulator_mode_to_status(unsigned int mode
)
2277 case REGULATOR_MODE_FAST
:
2278 return REGULATOR_STATUS_FAST
;
2279 case REGULATOR_MODE_NORMAL
:
2280 return REGULATOR_STATUS_NORMAL
;
2281 case REGULATOR_MODE_IDLE
:
2282 return REGULATOR_STATUS_IDLE
;
2283 case REGULATOR_STATUS_STANDBY
:
2284 return REGULATOR_STATUS_STANDBY
;
2289 EXPORT_SYMBOL_GPL(regulator_mode_to_status
);
2292 * To avoid cluttering sysfs (and memory) with useless state, only
2293 * create attributes that can be meaningfully displayed.
2295 static int add_regulator_attributes(struct regulator_dev
*rdev
)
2297 struct device
*dev
= &rdev
->dev
;
2298 struct regulator_ops
*ops
= rdev
->desc
->ops
;
2301 /* some attributes need specific methods to be displayed */
2302 if (ops
->get_voltage
|| ops
->get_voltage_sel
) {
2303 status
= device_create_file(dev
, &dev_attr_microvolts
);
2307 if (ops
->get_current_limit
) {
2308 status
= device_create_file(dev
, &dev_attr_microamps
);
2312 if (ops
->get_mode
) {
2313 status
= device_create_file(dev
, &dev_attr_opmode
);
2317 if (ops
->is_enabled
) {
2318 status
= device_create_file(dev
, &dev_attr_state
);
2322 if (ops
->get_status
) {
2323 status
= device_create_file(dev
, &dev_attr_status
);
2328 /* some attributes are type-specific */
2329 if (rdev
->desc
->type
== REGULATOR_CURRENT
) {
2330 status
= device_create_file(dev
, &dev_attr_requested_microamps
);
2335 /* all the other attributes exist to support constraints;
2336 * don't show them if there are no constraints, or if the
2337 * relevant supporting methods are missing.
2339 if (!rdev
->constraints
)
2342 /* constraints need specific supporting methods */
2343 if (ops
->set_voltage
|| ops
->set_voltage_sel
) {
2344 status
= device_create_file(dev
, &dev_attr_min_microvolts
);
2347 status
= device_create_file(dev
, &dev_attr_max_microvolts
);
2351 if (ops
->set_current_limit
) {
2352 status
= device_create_file(dev
, &dev_attr_min_microamps
);
2355 status
= device_create_file(dev
, &dev_attr_max_microamps
);
2360 /* suspend mode constraints need multiple supporting methods */
2361 if (!(ops
->set_suspend_enable
&& ops
->set_suspend_disable
))
2364 status
= device_create_file(dev
, &dev_attr_suspend_standby_state
);
2367 status
= device_create_file(dev
, &dev_attr_suspend_mem_state
);
2370 status
= device_create_file(dev
, &dev_attr_suspend_disk_state
);
2374 if (ops
->set_suspend_voltage
) {
2375 status
= device_create_file(dev
,
2376 &dev_attr_suspend_standby_microvolts
);
2379 status
= device_create_file(dev
,
2380 &dev_attr_suspend_mem_microvolts
);
2383 status
= device_create_file(dev
,
2384 &dev_attr_suspend_disk_microvolts
);
2389 if (ops
->set_suspend_mode
) {
2390 status
= device_create_file(dev
,
2391 &dev_attr_suspend_standby_mode
);
2394 status
= device_create_file(dev
,
2395 &dev_attr_suspend_mem_mode
);
2398 status
= device_create_file(dev
,
2399 &dev_attr_suspend_disk_mode
);
2408 * regulator_register - register regulator
2409 * @regulator_desc: regulator to register
2410 * @dev: struct device for the regulator
2411 * @init_data: platform provided init data, passed through by driver
2412 * @driver_data: private regulator data
2414 * Called by regulator drivers to register a regulator.
2415 * Returns 0 on success.
2417 struct regulator_dev
*regulator_register(struct regulator_desc
*regulator_desc
,
2418 struct device
*dev
, const struct regulator_init_data
*init_data
,
2421 static atomic_t regulator_no
= ATOMIC_INIT(0);
2422 struct regulator_dev
*rdev
;
2425 if (regulator_desc
== NULL
)
2426 return ERR_PTR(-EINVAL
);
2428 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
2429 return ERR_PTR(-EINVAL
);
2431 if (regulator_desc
->type
!= REGULATOR_VOLTAGE
&&
2432 regulator_desc
->type
!= REGULATOR_CURRENT
)
2433 return ERR_PTR(-EINVAL
);
2436 return ERR_PTR(-EINVAL
);
2438 /* Only one of each should be implemented */
2439 WARN_ON(regulator_desc
->ops
->get_voltage
&&
2440 regulator_desc
->ops
->get_voltage_sel
);
2441 WARN_ON(regulator_desc
->ops
->set_voltage
&&
2442 regulator_desc
->ops
->set_voltage_sel
);
2444 /* If we're using selectors we must implement list_voltage. */
2445 if (regulator_desc
->ops
->get_voltage_sel
&&
2446 !regulator_desc
->ops
->list_voltage
) {
2447 return ERR_PTR(-EINVAL
);
2449 if (regulator_desc
->ops
->set_voltage_sel
&&
2450 !regulator_desc
->ops
->list_voltage
) {
2451 return ERR_PTR(-EINVAL
);
2454 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
2456 return ERR_PTR(-ENOMEM
);
2458 mutex_lock(®ulator_list_mutex
);
2460 mutex_init(&rdev
->mutex
);
2461 rdev
->reg_data
= driver_data
;
2462 rdev
->owner
= regulator_desc
->owner
;
2463 rdev
->desc
= regulator_desc
;
2464 INIT_LIST_HEAD(&rdev
->consumer_list
);
2465 INIT_LIST_HEAD(&rdev
->supply_list
);
2466 INIT_LIST_HEAD(&rdev
->list
);
2467 INIT_LIST_HEAD(&rdev
->slist
);
2468 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
2470 /* preform any regulator specific init */
2471 if (init_data
->regulator_init
) {
2472 ret
= init_data
->regulator_init(rdev
->reg_data
);
2477 /* register with sysfs */
2478 rdev
->dev
.class = ®ulator_class
;
2479 rdev
->dev
.parent
= dev
;
2480 dev_set_name(&rdev
->dev
, "regulator.%d",
2481 atomic_inc_return(®ulator_no
) - 1);
2482 ret
= device_register(&rdev
->dev
);
2484 put_device(&rdev
->dev
);
2488 dev_set_drvdata(&rdev
->dev
, rdev
);
2490 /* set regulator constraints */
2491 ret
= set_machine_constraints(rdev
, &init_data
->constraints
);
2495 /* add attributes supported by this regulator */
2496 ret
= add_regulator_attributes(rdev
);
2500 /* set supply regulator if it exists */
2501 if (init_data
->supply_regulator
&& init_data
->supply_regulator_dev
) {
2503 "Supply regulator specified by both name and dev\n");
2508 if (init_data
->supply_regulator
) {
2509 struct regulator_dev
*r
;
2512 list_for_each_entry(r
, ®ulator_list
, list
) {
2513 if (strcmp(rdev_get_name(r
),
2514 init_data
->supply_regulator
) == 0) {
2521 dev_err(dev
, "Failed to find supply %s\n",
2522 init_data
->supply_regulator
);
2527 ret
= set_supply(rdev
, r
);
2532 if (init_data
->supply_regulator_dev
) {
2533 dev_warn(dev
, "Uses supply_regulator_dev instead of regulator_supply\n");
2534 ret
= set_supply(rdev
,
2535 dev_get_drvdata(init_data
->supply_regulator_dev
));
2540 /* add consumers devices */
2541 for (i
= 0; i
< init_data
->num_consumer_supplies
; i
++) {
2542 ret
= set_consumer_device_supply(rdev
,
2543 init_data
->consumer_supplies
[i
].dev
,
2544 init_data
->consumer_supplies
[i
].dev_name
,
2545 init_data
->consumer_supplies
[i
].supply
);
2547 goto unset_supplies
;
2550 list_add(&rdev
->list
, ®ulator_list
);
2552 mutex_unlock(®ulator_list_mutex
);
2556 unset_regulator_supplies(rdev
);
2559 device_unregister(&rdev
->dev
);
2560 /* device core frees rdev */
2561 rdev
= ERR_PTR(ret
);
2566 rdev
= ERR_PTR(ret
);
2569 EXPORT_SYMBOL_GPL(regulator_register
);
2572 * regulator_unregister - unregister regulator
2573 * @rdev: regulator to unregister
2575 * Called by regulator drivers to unregister a regulator.
2577 void regulator_unregister(struct regulator_dev
*rdev
)
2582 mutex_lock(®ulator_list_mutex
);
2583 WARN_ON(rdev
->open_count
);
2584 unset_regulator_supplies(rdev
);
2585 list_del(&rdev
->list
);
2587 sysfs_remove_link(&rdev
->dev
.kobj
, "supply");
2588 device_unregister(&rdev
->dev
);
2589 kfree(rdev
->constraints
);
2590 mutex_unlock(®ulator_list_mutex
);
2592 EXPORT_SYMBOL_GPL(regulator_unregister
);
2595 * regulator_suspend_prepare - prepare regulators for system wide suspend
2596 * @state: system suspend state
2598 * Configure each regulator with it's suspend operating parameters for state.
2599 * This will usually be called by machine suspend code prior to supending.
2601 int regulator_suspend_prepare(suspend_state_t state
)
2603 struct regulator_dev
*rdev
;
2606 /* ON is handled by regulator active state */
2607 if (state
== PM_SUSPEND_ON
)
2610 mutex_lock(®ulator_list_mutex
);
2611 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2613 mutex_lock(&rdev
->mutex
);
2614 ret
= suspend_prepare(rdev
, state
);
2615 mutex_unlock(&rdev
->mutex
);
2618 rdev_err(rdev
, "failed to prepare\n");
2623 mutex_unlock(®ulator_list_mutex
);
2626 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
2629 * regulator_has_full_constraints - the system has fully specified constraints
2631 * Calling this function will cause the regulator API to disable all
2632 * regulators which have a zero use count and don't have an always_on
2633 * constraint in a late_initcall.
2635 * The intention is that this will become the default behaviour in a
2636 * future kernel release so users are encouraged to use this facility
2639 void regulator_has_full_constraints(void)
2641 has_full_constraints
= 1;
2643 EXPORT_SYMBOL_GPL(regulator_has_full_constraints
);
2646 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2648 * Calling this function will cause the regulator API to provide a
2649 * dummy regulator to consumers if no physical regulator is found,
2650 * allowing most consumers to proceed as though a regulator were
2651 * configured. This allows systems such as those with software
2652 * controllable regulators for the CPU core only to be brought up more
2655 void regulator_use_dummy_regulator(void)
2657 board_wants_dummy_regulator
= true;
2659 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator
);
2662 * rdev_get_drvdata - get rdev regulator driver data
2665 * Get rdev regulator driver private data. This call can be used in the
2666 * regulator driver context.
2668 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
2670 return rdev
->reg_data
;
2672 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
2675 * regulator_get_drvdata - get regulator driver data
2676 * @regulator: regulator
2678 * Get regulator driver private data. This call can be used in the consumer
2679 * driver context when non API regulator specific functions need to be called.
2681 void *regulator_get_drvdata(struct regulator
*regulator
)
2683 return regulator
->rdev
->reg_data
;
2685 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
2688 * regulator_set_drvdata - set regulator driver data
2689 * @regulator: regulator
2692 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
2694 regulator
->rdev
->reg_data
= data
;
2696 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
2699 * regulator_get_id - get regulator ID
2702 int rdev_get_id(struct regulator_dev
*rdev
)
2704 return rdev
->desc
->id
;
2706 EXPORT_SYMBOL_GPL(rdev_get_id
);
2708 struct device
*rdev_get_dev(struct regulator_dev
*rdev
)
2712 EXPORT_SYMBOL_GPL(rdev_get_dev
);
2714 void *regulator_get_init_drvdata(struct regulator_init_data
*reg_init_data
)
2716 return reg_init_data
->driver_data
;
2718 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata
);
2720 static int __init
regulator_init(void)
2724 ret
= class_register(®ulator_class
);
2726 regulator_dummy_init();
2731 /* init early to allow our consumers to complete system booting */
2732 core_initcall(regulator_init
);
2734 static int __init
regulator_init_complete(void)
2736 struct regulator_dev
*rdev
;
2737 struct regulator_ops
*ops
;
2738 struct regulation_constraints
*c
;
2741 mutex_lock(®ulator_list_mutex
);
2743 /* If we have a full configuration then disable any regulators
2744 * which are not in use or always_on. This will become the
2745 * default behaviour in the future.
2747 list_for_each_entry(rdev
, ®ulator_list
, list
) {
2748 ops
= rdev
->desc
->ops
;
2749 c
= rdev
->constraints
;
2751 if (!ops
->disable
|| (c
&& c
->always_on
))
2754 mutex_lock(&rdev
->mutex
);
2756 if (rdev
->use_count
)
2759 /* If we can't read the status assume it's on. */
2760 if (ops
->is_enabled
)
2761 enabled
= ops
->is_enabled(rdev
);
2768 if (has_full_constraints
) {
2769 /* We log since this may kill the system if it
2771 rdev_info(rdev
, "disabling\n");
2772 ret
= ops
->disable(rdev
);
2774 rdev_err(rdev
, "couldn't disable: %d\n", ret
);
2777 /* The intention is that in future we will
2778 * assume that full constraints are provided
2779 * so warn even if we aren't going to do
2782 rdev_warn(rdev
, "incomplete constraints, leaving on\n");
2786 mutex_unlock(&rdev
->mutex
);
2789 mutex_unlock(®ulator_list_mutex
);
2793 late_initcall(regulator_init_complete
);