2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 * Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/err.h>
19 #include <linux/mutex.h>
20 #include <linux/suspend.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/regulator/driver.h>
23 #include <linux/regulator/machine.h>
25 #define REGULATOR_VERSION "0.5"
27 static DEFINE_MUTEX(regulator_list_mutex
);
28 static LIST_HEAD(regulator_list
);
29 static LIST_HEAD(regulator_map_list
);
32 * struct regulator_dev
34 * Voltage / Current regulator class device. One for each regulator.
36 struct regulator_dev
{
37 struct regulator_desc
*desc
;
40 /* lists we belong to */
41 struct list_head list
; /* list of all regulators */
42 struct list_head slist
; /* list of supplied regulators */
45 struct list_head consumer_list
; /* consumers we supply */
46 struct list_head supply_list
; /* regulators we supply */
48 struct blocking_notifier_head notifier
;
49 struct mutex mutex
; /* consumer lock */
52 struct regulation_constraints
*constraints
;
53 struct regulator_dev
*supply
; /* for tree */
55 void *reg_data
; /* regulator_dev data */
59 * struct regulator_map
61 * Used to provide symbolic supply names to devices.
63 struct regulator_map
{
64 struct list_head list
;
67 const char *regulator
;
70 static inline struct regulator_dev
*to_rdev(struct device
*d
)
72 return container_of(d
, struct regulator_dev
, dev
);
78 * One for each consumer device.
82 struct list_head list
;
86 int enabled
; /* client has called enabled */
88 struct device_attribute dev_attr
;
89 struct regulator_dev
*rdev
;
92 static int _regulator_is_enabled(struct regulator_dev
*rdev
);
93 static int _regulator_disable(struct regulator_dev
*rdev
);
94 static int _regulator_get_voltage(struct regulator_dev
*rdev
);
95 static int _regulator_get_current_limit(struct regulator_dev
*rdev
);
96 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
);
97 static void _notifier_call_chain(struct regulator_dev
*rdev
,
98 unsigned long event
, void *data
);
100 /* gets the regulator for a given consumer device */
101 static struct regulator
*get_device_regulator(struct device
*dev
)
103 struct regulator
*regulator
= NULL
;
104 struct regulator_dev
*rdev
;
106 mutex_lock(®ulator_list_mutex
);
107 list_for_each_entry(rdev
, ®ulator_list
, list
) {
108 mutex_lock(&rdev
->mutex
);
109 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
) {
110 if (regulator
->dev
== dev
) {
111 mutex_unlock(&rdev
->mutex
);
112 mutex_unlock(®ulator_list_mutex
);
116 mutex_unlock(&rdev
->mutex
);
118 mutex_unlock(®ulator_list_mutex
);
122 /* Platform voltage constraint check */
123 static int regulator_check_voltage(struct regulator_dev
*rdev
,
124 int *min_uV
, int *max_uV
)
126 BUG_ON(*min_uV
> *max_uV
);
128 if (!rdev
->constraints
) {
129 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
133 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_VOLTAGE
)) {
134 printk(KERN_ERR
"%s: operation not allowed for %s\n",
135 __func__
, rdev
->desc
->name
);
139 if (*max_uV
> rdev
->constraints
->max_uV
)
140 *max_uV
= rdev
->constraints
->max_uV
;
141 if (*min_uV
< rdev
->constraints
->min_uV
)
142 *min_uV
= rdev
->constraints
->min_uV
;
144 if (*min_uV
> *max_uV
)
150 /* current constraint check */
151 static int regulator_check_current_limit(struct regulator_dev
*rdev
,
152 int *min_uA
, int *max_uA
)
154 BUG_ON(*min_uA
> *max_uA
);
156 if (!rdev
->constraints
) {
157 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
161 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_CURRENT
)) {
162 printk(KERN_ERR
"%s: operation not allowed for %s\n",
163 __func__
, rdev
->desc
->name
);
167 if (*max_uA
> rdev
->constraints
->max_uA
)
168 *max_uA
= rdev
->constraints
->max_uA
;
169 if (*min_uA
< rdev
->constraints
->min_uA
)
170 *min_uA
= rdev
->constraints
->min_uA
;
172 if (*min_uA
> *max_uA
)
178 /* operating mode constraint check */
179 static int regulator_check_mode(struct regulator_dev
*rdev
, int mode
)
181 if (!rdev
->constraints
) {
182 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
186 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_MODE
)) {
187 printk(KERN_ERR
"%s: operation not allowed for %s\n",
188 __func__
, rdev
->desc
->name
);
191 if (!(rdev
->constraints
->valid_modes_mask
& mode
)) {
192 printk(KERN_ERR
"%s: invalid mode %x for %s\n",
193 __func__
, mode
, rdev
->desc
->name
);
199 /* dynamic regulator mode switching constraint check */
200 static int regulator_check_drms(struct regulator_dev
*rdev
)
202 if (!rdev
->constraints
) {
203 printk(KERN_ERR
"%s: no constraints for %s\n", __func__
,
207 if (!(rdev
->constraints
->valid_ops_mask
& REGULATOR_CHANGE_DRMS
)) {
208 printk(KERN_ERR
"%s: operation not allowed for %s\n",
209 __func__
, rdev
->desc
->name
);
215 static ssize_t
device_requested_uA_show(struct device
*dev
,
216 struct device_attribute
*attr
, char *buf
)
218 struct regulator
*regulator
;
220 regulator
= get_device_regulator(dev
);
221 if (regulator
== NULL
)
224 return sprintf(buf
, "%d\n", regulator
->uA_load
);
227 static ssize_t
regulator_uV_show(struct device
*dev
,
228 struct device_attribute
*attr
, char *buf
)
230 struct regulator_dev
*rdev
= to_rdev(dev
);
233 mutex_lock(&rdev
->mutex
);
234 ret
= sprintf(buf
, "%d\n", _regulator_get_voltage(rdev
));
235 mutex_unlock(&rdev
->mutex
);
240 static ssize_t
regulator_uA_show(struct device
*dev
,
241 struct device_attribute
*attr
, char *buf
)
243 struct regulator_dev
*rdev
= to_rdev(dev
);
245 return sprintf(buf
, "%d\n", _regulator_get_current_limit(rdev
));
248 static ssize_t
regulator_opmode_show(struct device
*dev
,
249 struct device_attribute
*attr
, char *buf
)
251 struct regulator_dev
*rdev
= to_rdev(dev
);
252 int mode
= _regulator_get_mode(rdev
);
255 case REGULATOR_MODE_FAST
:
256 return sprintf(buf
, "fast\n");
257 case REGULATOR_MODE_NORMAL
:
258 return sprintf(buf
, "normal\n");
259 case REGULATOR_MODE_IDLE
:
260 return sprintf(buf
, "idle\n");
261 case REGULATOR_MODE_STANDBY
:
262 return sprintf(buf
, "standby\n");
264 return sprintf(buf
, "unknown\n");
267 static ssize_t
regulator_state_show(struct device
*dev
,
268 struct device_attribute
*attr
, char *buf
)
270 struct regulator_dev
*rdev
= to_rdev(dev
);
271 int state
= _regulator_is_enabled(rdev
);
274 return sprintf(buf
, "enabled\n");
276 return sprintf(buf
, "disabled\n");
278 return sprintf(buf
, "unknown\n");
281 static ssize_t
regulator_min_uA_show(struct device
*dev
,
282 struct device_attribute
*attr
, char *buf
)
284 struct regulator_dev
*rdev
= to_rdev(dev
);
286 if (!rdev
->constraints
)
287 return sprintf(buf
, "constraint not defined\n");
289 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uA
);
292 static ssize_t
regulator_max_uA_show(struct device
*dev
,
293 struct device_attribute
*attr
, char *buf
)
295 struct regulator_dev
*rdev
= to_rdev(dev
);
297 if (!rdev
->constraints
)
298 return sprintf(buf
, "constraint not defined\n");
300 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uA
);
303 static ssize_t
regulator_min_uV_show(struct device
*dev
,
304 struct device_attribute
*attr
, char *buf
)
306 struct regulator_dev
*rdev
= to_rdev(dev
);
308 if (!rdev
->constraints
)
309 return sprintf(buf
, "constraint not defined\n");
311 return sprintf(buf
, "%d\n", rdev
->constraints
->min_uV
);
314 static ssize_t
regulator_max_uV_show(struct device
*dev
,
315 struct device_attribute
*attr
, char *buf
)
317 struct regulator_dev
*rdev
= to_rdev(dev
);
319 if (!rdev
->constraints
)
320 return sprintf(buf
, "constraint not defined\n");
322 return sprintf(buf
, "%d\n", rdev
->constraints
->max_uV
);
325 static ssize_t
regulator_total_uA_show(struct device
*dev
,
326 struct device_attribute
*attr
, char *buf
)
328 struct regulator_dev
*rdev
= to_rdev(dev
);
329 struct regulator
*regulator
;
332 mutex_lock(&rdev
->mutex
);
333 list_for_each_entry(regulator
, &rdev
->consumer_list
, list
)
334 uA
+= regulator
->uA_load
;
335 mutex_unlock(&rdev
->mutex
);
336 return sprintf(buf
, "%d\n", uA
);
339 static ssize_t
regulator_num_users_show(struct device
*dev
,
340 struct device_attribute
*attr
, char *buf
)
342 struct regulator_dev
*rdev
= to_rdev(dev
);
343 return sprintf(buf
, "%d\n", rdev
->use_count
);
346 static ssize_t
regulator_type_show(struct device
*dev
,
347 struct device_attribute
*attr
, char *buf
)
349 struct regulator_dev
*rdev
= to_rdev(dev
);
351 switch (rdev
->desc
->type
) {
352 case REGULATOR_VOLTAGE
:
353 return sprintf(buf
, "voltage\n");
354 case REGULATOR_CURRENT
:
355 return sprintf(buf
, "current\n");
357 return sprintf(buf
, "unknown\n");
360 static ssize_t
regulator_suspend_mem_uV_show(struct device
*dev
,
361 struct device_attribute
*attr
, char *buf
)
363 struct regulator_dev
*rdev
= to_rdev(dev
);
365 if (!rdev
->constraints
)
366 return sprintf(buf
, "not defined\n");
367 return sprintf(buf
, "%d\n", rdev
->constraints
->state_mem
.uV
);
370 static ssize_t
regulator_suspend_disk_uV_show(struct device
*dev
,
371 struct device_attribute
*attr
, char *buf
)
373 struct regulator_dev
*rdev
= to_rdev(dev
);
375 if (!rdev
->constraints
)
376 return sprintf(buf
, "not defined\n");
377 return sprintf(buf
, "%d\n", rdev
->constraints
->state_disk
.uV
);
380 static ssize_t
regulator_suspend_standby_uV_show(struct device
*dev
,
381 struct device_attribute
*attr
, char *buf
)
383 struct regulator_dev
*rdev
= to_rdev(dev
);
385 if (!rdev
->constraints
)
386 return sprintf(buf
, "not defined\n");
387 return sprintf(buf
, "%d\n", rdev
->constraints
->state_standby
.uV
);
390 static ssize_t
suspend_opmode_show(struct regulator_dev
*rdev
,
391 unsigned int mode
, char *buf
)
394 case REGULATOR_MODE_FAST
:
395 return sprintf(buf
, "fast\n");
396 case REGULATOR_MODE_NORMAL
:
397 return sprintf(buf
, "normal\n");
398 case REGULATOR_MODE_IDLE
:
399 return sprintf(buf
, "idle\n");
400 case REGULATOR_MODE_STANDBY
:
401 return sprintf(buf
, "standby\n");
403 return sprintf(buf
, "unknown\n");
406 static ssize_t
regulator_suspend_mem_mode_show(struct device
*dev
,
407 struct device_attribute
*attr
, char *buf
)
409 struct regulator_dev
*rdev
= to_rdev(dev
);
411 if (!rdev
->constraints
)
412 return sprintf(buf
, "not defined\n");
413 return suspend_opmode_show(rdev
,
414 rdev
->constraints
->state_mem
.mode
, buf
);
417 static ssize_t
regulator_suspend_disk_mode_show(struct device
*dev
,
418 struct device_attribute
*attr
, char *buf
)
420 struct regulator_dev
*rdev
= to_rdev(dev
);
422 if (!rdev
->constraints
)
423 return sprintf(buf
, "not defined\n");
424 return suspend_opmode_show(rdev
,
425 rdev
->constraints
->state_disk
.mode
, buf
);
428 static ssize_t
regulator_suspend_standby_mode_show(struct device
*dev
,
429 struct device_attribute
*attr
, char *buf
)
431 struct regulator_dev
*rdev
= to_rdev(dev
);
433 if (!rdev
->constraints
)
434 return sprintf(buf
, "not defined\n");
435 return suspend_opmode_show(rdev
,
436 rdev
->constraints
->state_standby
.mode
, buf
);
439 static ssize_t
regulator_suspend_mem_state_show(struct device
*dev
,
440 struct device_attribute
*attr
, char *buf
)
442 struct regulator_dev
*rdev
= to_rdev(dev
);
444 if (!rdev
->constraints
)
445 return sprintf(buf
, "not defined\n");
447 if (rdev
->constraints
->state_mem
.enabled
)
448 return sprintf(buf
, "enabled\n");
450 return sprintf(buf
, "disabled\n");
453 static ssize_t
regulator_suspend_disk_state_show(struct device
*dev
,
454 struct device_attribute
*attr
, char *buf
)
456 struct regulator_dev
*rdev
= to_rdev(dev
);
458 if (!rdev
->constraints
)
459 return sprintf(buf
, "not defined\n");
461 if (rdev
->constraints
->state_disk
.enabled
)
462 return sprintf(buf
, "enabled\n");
464 return sprintf(buf
, "disabled\n");
467 static ssize_t
regulator_suspend_standby_state_show(struct device
*dev
,
468 struct device_attribute
*attr
, char *buf
)
470 struct regulator_dev
*rdev
= to_rdev(dev
);
472 if (!rdev
->constraints
)
473 return sprintf(buf
, "not defined\n");
475 if (rdev
->constraints
->state_standby
.enabled
)
476 return sprintf(buf
, "enabled\n");
478 return sprintf(buf
, "disabled\n");
480 static struct device_attribute regulator_dev_attrs
[] = {
481 __ATTR(microvolts
, 0444, regulator_uV_show
, NULL
),
482 __ATTR(microamps
, 0444, regulator_uA_show
, NULL
),
483 __ATTR(opmode
, 0444, regulator_opmode_show
, NULL
),
484 __ATTR(state
, 0444, regulator_state_show
, NULL
),
485 __ATTR(min_microvolts
, 0444, regulator_min_uV_show
, NULL
),
486 __ATTR(min_microamps
, 0444, regulator_min_uA_show
, NULL
),
487 __ATTR(max_microvolts
, 0444, regulator_max_uV_show
, NULL
),
488 __ATTR(max_microamps
, 0444, regulator_max_uA_show
, NULL
),
489 __ATTR(requested_microamps
, 0444, regulator_total_uA_show
, NULL
),
490 __ATTR(num_users
, 0444, regulator_num_users_show
, NULL
),
491 __ATTR(type
, 0444, regulator_type_show
, NULL
),
492 __ATTR(suspend_mem_microvolts
, 0444,
493 regulator_suspend_mem_uV_show
, NULL
),
494 __ATTR(suspend_disk_microvolts
, 0444,
495 regulator_suspend_disk_uV_show
, NULL
),
496 __ATTR(suspend_standby_microvolts
, 0444,
497 regulator_suspend_standby_uV_show
, NULL
),
498 __ATTR(suspend_mem_mode
, 0444,
499 regulator_suspend_mem_mode_show
, NULL
),
500 __ATTR(suspend_disk_mode
, 0444,
501 regulator_suspend_disk_mode_show
, NULL
),
502 __ATTR(suspend_standby_mode
, 0444,
503 regulator_suspend_standby_mode_show
, NULL
),
504 __ATTR(suspend_mem_state
, 0444,
505 regulator_suspend_mem_state_show
, NULL
),
506 __ATTR(suspend_disk_state
, 0444,
507 regulator_suspend_disk_state_show
, NULL
),
508 __ATTR(suspend_standby_state
, 0444,
509 regulator_suspend_standby_state_show
, NULL
),
513 static void regulator_dev_release(struct device
*dev
)
515 struct regulator_dev
*rdev
= to_rdev(dev
);
519 static struct class regulator_class
= {
521 .dev_release
= regulator_dev_release
,
522 .dev_attrs
= regulator_dev_attrs
,
525 /* Calculate the new optimum regulator operating mode based on the new total
526 * consumer load. All locks held by caller */
527 static void drms_uA_update(struct regulator_dev
*rdev
)
529 struct regulator
*sibling
;
530 int current_uA
= 0, output_uV
, input_uV
, err
;
533 err
= regulator_check_drms(rdev
);
534 if (err
< 0 || !rdev
->desc
->ops
->get_optimum_mode
||
535 !rdev
->desc
->ops
->get_voltage
|| !rdev
->desc
->ops
->set_mode
);
538 /* get output voltage */
539 output_uV
= rdev
->desc
->ops
->get_voltage(rdev
);
543 /* get input voltage */
544 if (rdev
->supply
&& rdev
->supply
->desc
->ops
->get_voltage
)
545 input_uV
= rdev
->supply
->desc
->ops
->get_voltage(rdev
->supply
);
547 input_uV
= rdev
->constraints
->input_uV
;
551 /* calc total requested load */
552 list_for_each_entry(sibling
, &rdev
->consumer_list
, list
)
553 current_uA
+= sibling
->uA_load
;
555 /* now get the optimum mode for our new total regulator load */
556 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
, input_uV
,
557 output_uV
, current_uA
);
559 /* check the new mode is allowed */
560 err
= regulator_check_mode(rdev
, mode
);
562 rdev
->desc
->ops
->set_mode(rdev
, mode
);
565 static int suspend_set_state(struct regulator_dev
*rdev
,
566 struct regulator_state
*rstate
)
570 /* enable & disable are mandatory for suspend control */
571 if (!rdev
->desc
->ops
->set_suspend_enable
||
572 !rdev
->desc
->ops
->set_suspend_disable
)
576 ret
= rdev
->desc
->ops
->set_suspend_enable(rdev
);
578 ret
= rdev
->desc
->ops
->set_suspend_disable(rdev
);
580 printk(KERN_ERR
"%s: failed to enabled/disable\n", __func__
);
584 if (rdev
->desc
->ops
->set_suspend_voltage
&& rstate
->uV
> 0) {
585 ret
= rdev
->desc
->ops
->set_suspend_voltage(rdev
, rstate
->uV
);
587 printk(KERN_ERR
"%s: failed to set voltage\n",
593 if (rdev
->desc
->ops
->set_suspend_mode
&& rstate
->mode
> 0) {
594 ret
= rdev
->desc
->ops
->set_suspend_mode(rdev
, rstate
->mode
);
596 printk(KERN_ERR
"%s: failed to set mode\n", __func__
);
603 /* locks held by caller */
604 static int suspend_prepare(struct regulator_dev
*rdev
, suspend_state_t state
)
606 if (!rdev
->constraints
)
610 case PM_SUSPEND_STANDBY
:
611 return suspend_set_state(rdev
,
612 &rdev
->constraints
->state_standby
);
614 return suspend_set_state(rdev
,
615 &rdev
->constraints
->state_mem
);
617 return suspend_set_state(rdev
,
618 &rdev
->constraints
->state_disk
);
624 static void print_constraints(struct regulator_dev
*rdev
)
626 struct regulation_constraints
*constraints
= rdev
->constraints
;
630 if (rdev
->desc
->type
== REGULATOR_VOLTAGE
) {
631 if (constraints
->min_uV
== constraints
->max_uV
)
632 count
= sprintf(buf
, "%d mV ",
633 constraints
->min_uV
/ 1000);
635 count
= sprintf(buf
, "%d <--> %d mV ",
636 constraints
->min_uV
/ 1000,
637 constraints
->max_uV
/ 1000);
639 if (constraints
->min_uA
== constraints
->max_uA
)
640 count
= sprintf(buf
, "%d mA ",
641 constraints
->min_uA
/ 1000);
643 count
= sprintf(buf
, "%d <--> %d mA ",
644 constraints
->min_uA
/ 1000,
645 constraints
->max_uA
/ 1000);
647 if (constraints
->valid_modes_mask
& REGULATOR_MODE_FAST
)
648 count
+= sprintf(buf
+ count
, "fast ");
649 if (constraints
->valid_modes_mask
& REGULATOR_MODE_NORMAL
)
650 count
+= sprintf(buf
+ count
, "normal ");
651 if (constraints
->valid_modes_mask
& REGULATOR_MODE_IDLE
)
652 count
+= sprintf(buf
+ count
, "idle ");
653 if (constraints
->valid_modes_mask
& REGULATOR_MODE_STANDBY
)
654 count
+= sprintf(buf
+ count
, "standby");
656 printk(KERN_INFO
"regulator: %s: %s\n", rdev
->desc
->name
, buf
);
659 #define REG_STR_SIZE 32
661 static struct regulator
*create_regulator(struct regulator_dev
*rdev
,
663 const char *supply_name
)
665 struct regulator
*regulator
;
666 char buf
[REG_STR_SIZE
];
669 regulator
= kzalloc(sizeof(*regulator
), GFP_KERNEL
);
670 if (regulator
== NULL
)
673 mutex_lock(&rdev
->mutex
);
674 regulator
->rdev
= rdev
;
675 list_add(®ulator
->list
, &rdev
->consumer_list
);
678 /* create a 'requested_microamps_name' sysfs entry */
679 size
= scnprintf(buf
, REG_STR_SIZE
, "microamps_requested_%s",
681 if (size
>= REG_STR_SIZE
)
684 regulator
->dev
= dev
;
685 regulator
->dev_attr
.attr
.name
= kstrdup(buf
, GFP_KERNEL
);
686 if (regulator
->dev_attr
.attr
.name
== NULL
)
689 regulator
->dev_attr
.attr
.owner
= THIS_MODULE
;
690 regulator
->dev_attr
.attr
.mode
= 0444;
691 regulator
->dev_attr
.show
= device_requested_uA_show
;
692 err
= device_create_file(dev
, ®ulator
->dev_attr
);
694 printk(KERN_WARNING
"%s: could not add regulator_dev"
695 " load sysfs\n", __func__
);
699 /* also add a link to the device sysfs entry */
700 size
= scnprintf(buf
, REG_STR_SIZE
, "%s-%s",
701 dev
->kobj
.name
, supply_name
);
702 if (size
>= REG_STR_SIZE
)
705 regulator
->supply_name
= kstrdup(buf
, GFP_KERNEL
);
706 if (regulator
->supply_name
== NULL
)
709 err
= sysfs_create_link(&rdev
->dev
.kobj
, &dev
->kobj
,
713 "%s: could not add device link %s err %d\n",
714 __func__
, dev
->kobj
.name
, err
);
715 device_remove_file(dev
, ®ulator
->dev_attr
);
719 mutex_unlock(&rdev
->mutex
);
722 kfree(regulator
->supply_name
);
724 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
726 kfree(regulator
->dev_attr
.attr
.name
);
728 list_del(®ulator
->list
);
730 mutex_unlock(&rdev
->mutex
);
735 * regulator_get - lookup and obtain a reference to a regulator.
736 * @dev: device for regulator "consumer"
737 * @id: Supply name or regulator ID.
739 * Returns a struct regulator corresponding to the regulator producer,
740 * or IS_ERR() condition containing errno. Use of supply names
741 * configured via regulator_set_device_supply() is strongly
744 struct regulator
*regulator_get(struct device
*dev
, const char *id
)
746 struct regulator_dev
*rdev
;
747 struct regulator_map
*map
;
748 struct regulator
*regulator
= ERR_PTR(-ENODEV
);
749 const char *supply
= id
;
752 printk(KERN_ERR
"regulator: get() with no identifier\n");
756 mutex_lock(®ulator_list_mutex
);
758 list_for_each_entry(map
, ®ulator_map_list
, list
) {
759 if (dev
== map
->dev
&&
760 strcmp(map
->supply
, id
) == 0) {
761 supply
= map
->regulator
;
766 list_for_each_entry(rdev
, ®ulator_list
, list
) {
767 if (strcmp(supply
, rdev
->desc
->name
) == 0 &&
768 try_module_get(rdev
->owner
))
771 printk(KERN_ERR
"regulator: Unable to get requested regulator: %s\n",
773 mutex_unlock(®ulator_list_mutex
);
777 regulator
= create_regulator(rdev
, dev
, id
);
778 if (regulator
== NULL
) {
779 regulator
= ERR_PTR(-ENOMEM
);
780 module_put(rdev
->owner
);
783 mutex_unlock(®ulator_list_mutex
);
786 EXPORT_SYMBOL_GPL(regulator_get
);
789 * regulator_put - "free" the regulator source
790 * @regulator: regulator source
792 * Note: drivers must ensure that all regulator_enable calls made on this
793 * regulator source are balanced by regulator_disable calls prior to calling
796 void regulator_put(struct regulator
*regulator
)
798 struct regulator_dev
*rdev
;
800 if (regulator
== NULL
|| IS_ERR(regulator
))
803 if (regulator
->enabled
) {
804 printk(KERN_WARNING
"Releasing supply %s while enabled\n",
805 regulator
->supply_name
);
806 WARN_ON(regulator
->enabled
);
807 regulator_disable(regulator
);
810 mutex_lock(®ulator_list_mutex
);
811 rdev
= regulator
->rdev
;
813 /* remove any sysfs entries */
814 if (regulator
->dev
) {
815 sysfs_remove_link(&rdev
->dev
.kobj
, regulator
->supply_name
);
816 kfree(regulator
->supply_name
);
817 device_remove_file(regulator
->dev
, ®ulator
->dev_attr
);
818 kfree(regulator
->dev_attr
.attr
.name
);
820 list_del(®ulator
->list
);
823 module_put(rdev
->owner
);
824 mutex_unlock(®ulator_list_mutex
);
826 EXPORT_SYMBOL_GPL(regulator_put
);
828 /* locks held by regulator_enable() */
829 static int _regulator_enable(struct regulator_dev
*rdev
)
833 if (!rdev
->constraints
) {
834 printk(KERN_ERR
"%s: %s has no constraints\n",
835 __func__
, rdev
->desc
->name
);
839 /* do we need to enable the supply regulator first */
841 ret
= _regulator_enable(rdev
->supply
);
843 printk(KERN_ERR
"%s: failed to enable %s: %d\n",
844 __func__
, rdev
->desc
->name
, ret
);
849 /* check voltage and requested load before enabling */
850 if (rdev
->desc
->ops
->enable
) {
852 if (rdev
->constraints
&&
853 (rdev
->constraints
->valid_ops_mask
&
854 REGULATOR_CHANGE_DRMS
))
855 drms_uA_update(rdev
);
857 ret
= rdev
->desc
->ops
->enable(rdev
);
859 printk(KERN_ERR
"%s: failed to enable %s: %d\n",
860 __func__
, rdev
->desc
->name
, ret
);
871 * regulator_enable - enable regulator output
872 * @regulator: regulator source
874 * Enable the regulator output at the predefined voltage or current value.
875 * NOTE: the output value can be set by other drivers, boot loader or may be
876 * hardwired in the regulator.
877 * NOTE: calls to regulator_enable() must be balanced with calls to
878 * regulator_disable().
880 int regulator_enable(struct regulator
*regulator
)
884 if (regulator
->enabled
) {
885 printk(KERN_CRIT
"Regulator %s already enabled\n",
886 regulator
->supply_name
);
887 WARN_ON(regulator
->enabled
);
891 mutex_lock(®ulator
->rdev
->mutex
);
892 regulator
->enabled
= 1;
893 ret
= _regulator_enable(regulator
->rdev
);
895 regulator
->enabled
= 0;
896 mutex_unlock(®ulator
->rdev
->mutex
);
899 EXPORT_SYMBOL_GPL(regulator_enable
);
901 /* locks held by regulator_disable() */
902 static int _regulator_disable(struct regulator_dev
*rdev
)
906 /* are we the last user and permitted to disable ? */
907 if (rdev
->use_count
== 1 && !rdev
->constraints
->always_on
) {
909 /* we are last user */
910 if (rdev
->desc
->ops
->disable
) {
911 ret
= rdev
->desc
->ops
->disable(rdev
);
913 printk(KERN_ERR
"%s: failed to disable %s\n",
914 __func__
, rdev
->desc
->name
);
919 /* decrease our supplies ref count and disable if required */
921 _regulator_disable(rdev
->supply
);
924 } else if (rdev
->use_count
> 1) {
926 if (rdev
->constraints
&&
927 (rdev
->constraints
->valid_ops_mask
&
928 REGULATOR_CHANGE_DRMS
))
929 drms_uA_update(rdev
);
937 * regulator_disable - disable regulator output
938 * @regulator: regulator source
940 * Disable the regulator output voltage or current.
941 * NOTE: this will only disable the regulator output if no other consumer
942 * devices have it enabled.
943 * NOTE: calls to regulator_enable() must be balanced with calls to
944 * regulator_disable().
946 int regulator_disable(struct regulator
*regulator
)
950 if (!regulator
->enabled
) {
951 printk(KERN_ERR
"%s: not in use by this consumer\n",
956 mutex_lock(®ulator
->rdev
->mutex
);
957 regulator
->enabled
= 0;
958 regulator
->uA_load
= 0;
959 ret
= _regulator_disable(regulator
->rdev
);
960 mutex_unlock(®ulator
->rdev
->mutex
);
963 EXPORT_SYMBOL_GPL(regulator_disable
);
965 /* locks held by regulator_force_disable() */
966 static int _regulator_force_disable(struct regulator_dev
*rdev
)
971 if (rdev
->desc
->ops
->disable
) {
972 /* ah well, who wants to live forever... */
973 ret
= rdev
->desc
->ops
->disable(rdev
);
975 printk(KERN_ERR
"%s: failed to force disable %s\n",
976 __func__
, rdev
->desc
->name
);
979 /* notify other consumers that power has been forced off */
980 _notifier_call_chain(rdev
, REGULATOR_EVENT_FORCE_DISABLE
,
984 /* decrease our supplies ref count and disable if required */
986 _regulator_disable(rdev
->supply
);
993 * regulator_force_disable - force disable regulator output
994 * @regulator: regulator source
996 * Forcibly disable the regulator output voltage or current.
997 * NOTE: this *will* disable the regulator output even if other consumer
998 * devices have it enabled. This should be used for situations when device
999 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1001 int regulator_force_disable(struct regulator
*regulator
)
1005 mutex_lock(®ulator
->rdev
->mutex
);
1006 regulator
->enabled
= 0;
1007 regulator
->uA_load
= 0;
1008 ret
= _regulator_force_disable(regulator
->rdev
);
1009 mutex_unlock(®ulator
->rdev
->mutex
);
1012 EXPORT_SYMBOL_GPL(regulator_force_disable
);
1014 static int _regulator_is_enabled(struct regulator_dev
*rdev
)
1018 mutex_lock(&rdev
->mutex
);
1021 if (!rdev
->desc
->ops
->is_enabled
) {
1026 ret
= rdev
->desc
->ops
->is_enabled(rdev
);
1028 mutex_unlock(&rdev
->mutex
);
1033 * regulator_is_enabled - is the regulator output enabled
1034 * @regulator: regulator source
1036 * Returns zero for disabled otherwise return number of enable requests.
1038 int regulator_is_enabled(struct regulator
*regulator
)
1040 return _regulator_is_enabled(regulator
->rdev
);
1042 EXPORT_SYMBOL_GPL(regulator_is_enabled
);
1045 * regulator_set_voltage - set regulator output voltage
1046 * @regulator: regulator source
1047 * @min_uV: Minimum required voltage in uV
1048 * @max_uV: Maximum acceptable voltage in uV
1050 * Sets a voltage regulator to the desired output voltage. This can be set
1051 * during any regulator state. IOW, regulator can be disabled or enabled.
1053 * If the regulator is enabled then the voltage will change to the new value
1054 * immediately otherwise if the regulator is disabled the regulator will
1055 * output at the new voltage when enabled.
1057 * NOTE: If the regulator is shared between several devices then the lowest
1058 * request voltage that meets the system constraints will be used.
1059 * NOTE: Regulator system constraints must be set for this regulator before
1060 * calling this function otherwise this call will fail.
1062 int regulator_set_voltage(struct regulator
*regulator
, int min_uV
, int max_uV
)
1064 struct regulator_dev
*rdev
= regulator
->rdev
;
1067 mutex_lock(&rdev
->mutex
);
1070 if (!rdev
->desc
->ops
->set_voltage
) {
1075 /* constraints check */
1076 ret
= regulator_check_voltage(rdev
, &min_uV
, &max_uV
);
1079 regulator
->min_uV
= min_uV
;
1080 regulator
->max_uV
= max_uV
;
1081 ret
= rdev
->desc
->ops
->set_voltage(rdev
, min_uV
, max_uV
);
1084 mutex_unlock(&rdev
->mutex
);
1087 EXPORT_SYMBOL_GPL(regulator_set_voltage
);
1089 static int _regulator_get_voltage(struct regulator_dev
*rdev
)
1092 if (rdev
->desc
->ops
->get_voltage
)
1093 return rdev
->desc
->ops
->get_voltage(rdev
);
1099 * regulator_get_voltage - get regulator output voltage
1100 * @regulator: regulator source
1102 * This returns the current regulator voltage in uV.
1104 * NOTE: If the regulator is disabled it will return the voltage value. This
1105 * function should not be used to determine regulator state.
1107 int regulator_get_voltage(struct regulator
*regulator
)
1111 mutex_lock(®ulator
->rdev
->mutex
);
1113 ret
= _regulator_get_voltage(regulator
->rdev
);
1115 mutex_unlock(®ulator
->rdev
->mutex
);
1119 EXPORT_SYMBOL_GPL(regulator_get_voltage
);
1122 * regulator_set_current_limit - set regulator output current limit
1123 * @regulator: regulator source
1124 * @min_uA: Minimuum supported current in uA
1125 * @max_uA: Maximum supported current in uA
1127 * Sets current sink to the desired output current. This can be set during
1128 * any regulator state. IOW, regulator can be disabled or enabled.
1130 * If the regulator is enabled then the current will change to the new value
1131 * immediately otherwise if the regulator is disabled the regulator will
1132 * output at the new current when enabled.
1134 * NOTE: Regulator system constraints must be set for this regulator before
1135 * calling this function otherwise this call will fail.
1137 int regulator_set_current_limit(struct regulator
*regulator
,
1138 int min_uA
, int max_uA
)
1140 struct regulator_dev
*rdev
= regulator
->rdev
;
1143 mutex_lock(&rdev
->mutex
);
1146 if (!rdev
->desc
->ops
->set_current_limit
) {
1151 /* constraints check */
1152 ret
= regulator_check_current_limit(rdev
, &min_uA
, &max_uA
);
1156 ret
= rdev
->desc
->ops
->set_current_limit(rdev
, min_uA
, max_uA
);
1158 mutex_unlock(&rdev
->mutex
);
1161 EXPORT_SYMBOL_GPL(regulator_set_current_limit
);
1163 static int _regulator_get_current_limit(struct regulator_dev
*rdev
)
1167 mutex_lock(&rdev
->mutex
);
1170 if (!rdev
->desc
->ops
->get_current_limit
) {
1175 ret
= rdev
->desc
->ops
->get_current_limit(rdev
);
1177 mutex_unlock(&rdev
->mutex
);
1182 * regulator_get_current_limit - get regulator output current
1183 * @regulator: regulator source
1185 * This returns the current supplied by the specified current sink in uA.
1187 * NOTE: If the regulator is disabled it will return the current value. This
1188 * function should not be used to determine regulator state.
1190 int regulator_get_current_limit(struct regulator
*regulator
)
1192 return _regulator_get_current_limit(regulator
->rdev
);
1194 EXPORT_SYMBOL_GPL(regulator_get_current_limit
);
1197 * regulator_set_mode - set regulator operating mode
1198 * @regulator: regulator source
1199 * @mode: operating mode - one of the REGULATOR_MODE constants
1201 * Set regulator operating mode to increase regulator efficiency or improve
1202 * regulation performance.
1204 * NOTE: Regulator system constraints must be set for this regulator before
1205 * calling this function otherwise this call will fail.
1207 int regulator_set_mode(struct regulator
*regulator
, unsigned int mode
)
1209 struct regulator_dev
*rdev
= regulator
->rdev
;
1212 mutex_lock(&rdev
->mutex
);
1215 if (!rdev
->desc
->ops
->set_mode
) {
1220 /* constraints check */
1221 ret
= regulator_check_mode(rdev
, mode
);
1225 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1227 mutex_unlock(&rdev
->mutex
);
1230 EXPORT_SYMBOL_GPL(regulator_set_mode
);
1232 static unsigned int _regulator_get_mode(struct regulator_dev
*rdev
)
1236 mutex_lock(&rdev
->mutex
);
1239 if (!rdev
->desc
->ops
->get_mode
) {
1244 ret
= rdev
->desc
->ops
->get_mode(rdev
);
1246 mutex_unlock(&rdev
->mutex
);
1251 * regulator_get_mode - get regulator operating mode
1252 * @regulator: regulator source
1254 * Get the current regulator operating mode.
1256 unsigned int regulator_get_mode(struct regulator
*regulator
)
1258 return _regulator_get_mode(regulator
->rdev
);
1260 EXPORT_SYMBOL_GPL(regulator_get_mode
);
1263 * regulator_set_optimum_mode - set regulator optimum operating mode
1264 * @regulator: regulator source
1265 * @uA_load: load current
1267 * Notifies the regulator core of a new device load. This is then used by
1268 * DRMS (if enabled by constraints) to set the most efficient regulator
1269 * operating mode for the new regulator loading.
1271 * Consumer devices notify their supply regulator of the maximum power
1272 * they will require (can be taken from device datasheet in the power
1273 * consumption tables) when they change operational status and hence power
1274 * state. Examples of operational state changes that can affect power
1275 * consumption are :-
1277 * o Device is opened / closed.
1278 * o Device I/O is about to begin or has just finished.
1279 * o Device is idling in between work.
1281 * This information is also exported via sysfs to userspace.
1283 * DRMS will sum the total requested load on the regulator and change
1284 * to the most efficient operating mode if platform constraints allow.
1286 * Returns the new regulator mode or error.
1288 int regulator_set_optimum_mode(struct regulator
*regulator
, int uA_load
)
1290 struct regulator_dev
*rdev
= regulator
->rdev
;
1291 struct regulator
*consumer
;
1292 int ret
, output_uV
, input_uV
, total_uA_load
= 0;
1295 mutex_lock(&rdev
->mutex
);
1297 regulator
->uA_load
= uA_load
;
1298 ret
= regulator_check_drms(rdev
);
1304 if (!rdev
->desc
->ops
->get_optimum_mode
)
1307 /* get output voltage */
1308 output_uV
= rdev
->desc
->ops
->get_voltage(rdev
);
1309 if (output_uV
<= 0) {
1310 printk(KERN_ERR
"%s: invalid output voltage found for %s\n",
1311 __func__
, rdev
->desc
->name
);
1315 /* get input voltage */
1316 if (rdev
->supply
&& rdev
->supply
->desc
->ops
->get_voltage
)
1317 input_uV
= rdev
->supply
->desc
->ops
->get_voltage(rdev
->supply
);
1319 input_uV
= rdev
->constraints
->input_uV
;
1320 if (input_uV
<= 0) {
1321 printk(KERN_ERR
"%s: invalid input voltage found for %s\n",
1322 __func__
, rdev
->desc
->name
);
1326 /* calc total requested load for this regulator */
1327 list_for_each_entry(consumer
, &rdev
->consumer_list
, list
)
1328 total_uA_load
+= consumer
->uA_load
;
1330 mode
= rdev
->desc
->ops
->get_optimum_mode(rdev
,
1331 input_uV
, output_uV
,
1334 printk(KERN_ERR
"%s: failed to get optimum mode for %s @"
1335 " %d uA %d -> %d uV\n", __func__
, rdev
->desc
->name
,
1336 total_uA_load
, input_uV
, output_uV
);
1340 ret
= rdev
->desc
->ops
->set_mode(rdev
, mode
);
1342 printk(KERN_ERR
"%s: failed to set optimum mode %x for %s\n",
1343 __func__
, mode
, rdev
->desc
->name
);
1348 mutex_unlock(&rdev
->mutex
);
1351 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode
);
1354 * regulator_register_notifier - register regulator event notifier
1355 * @regulator: regulator source
1356 * @notifier_block: notifier block
1358 * Register notifier block to receive regulator events.
1360 int regulator_register_notifier(struct regulator
*regulator
,
1361 struct notifier_block
*nb
)
1363 return blocking_notifier_chain_register(®ulator
->rdev
->notifier
,
1366 EXPORT_SYMBOL_GPL(regulator_register_notifier
);
1369 * regulator_unregister_notifier - unregister regulator event notifier
1370 * @regulator: regulator source
1371 * @notifier_block: notifier block
1373 * Unregister regulator event notifier block.
1375 int regulator_unregister_notifier(struct regulator
*regulator
,
1376 struct notifier_block
*nb
)
1378 return blocking_notifier_chain_unregister(®ulator
->rdev
->notifier
,
1381 EXPORT_SYMBOL_GPL(regulator_unregister_notifier
);
1383 /* notify regulator consumers and downstream regulator consumers */
1384 static void _notifier_call_chain(struct regulator_dev
*rdev
,
1385 unsigned long event
, void *data
)
1387 struct regulator_dev
*_rdev
;
1389 /* call rdev chain first */
1390 mutex_lock(&rdev
->mutex
);
1391 blocking_notifier_call_chain(&rdev
->notifier
, event
, NULL
);
1392 mutex_unlock(&rdev
->mutex
);
1394 /* now notify regulator we supply */
1395 list_for_each_entry(_rdev
, &rdev
->supply_list
, slist
)
1396 _notifier_call_chain(_rdev
, event
, data
);
1400 * regulator_bulk_get - get multiple regulator consumers
1402 * @dev: Device to supply
1403 * @num_consumers: Number of consumers to register
1404 * @consumers: Configuration of consumers; clients are stored here.
1406 * @return 0 on success, an errno on failure.
1408 * This helper function allows drivers to get several regulator
1409 * consumers in one operation. If any of the regulators cannot be
1410 * acquired then any regulators that were allocated will be freed
1411 * before returning to the caller.
1413 int regulator_bulk_get(struct device
*dev
, int num_consumers
,
1414 struct regulator_bulk_data
*consumers
)
1419 for (i
= 0; i
< num_consumers
; i
++)
1420 consumers
[i
].consumer
= NULL
;
1422 for (i
= 0; i
< num_consumers
; i
++) {
1423 consumers
[i
].consumer
= regulator_get(dev
,
1424 consumers
[i
].supply
);
1425 if (IS_ERR(consumers
[i
].consumer
)) {
1426 dev_err(dev
, "Failed to get supply '%s'\n",
1427 consumers
[i
].supply
);
1428 ret
= PTR_ERR(consumers
[i
].consumer
);
1429 consumers
[i
].consumer
= NULL
;
1437 for (i
= 0; i
< num_consumers
&& consumers
[i
].consumer
; i
++)
1438 regulator_put(consumers
[i
].consumer
);
1442 EXPORT_SYMBOL_GPL(regulator_bulk_get
);
1445 * regulator_bulk_enable - enable multiple regulator consumers
1447 * @num_consumers: Number of consumers
1448 * @consumers: Consumer data; clients are stored here.
1449 * @return 0 on success, an errno on failure
1451 * This convenience API allows consumers to enable multiple regulator
1452 * clients in a single API call. If any consumers cannot be enabled
1453 * then any others that were enabled will be disabled again prior to
1456 int regulator_bulk_enable(int num_consumers
,
1457 struct regulator_bulk_data
*consumers
)
1462 for (i
= 0; i
< num_consumers
; i
++) {
1463 ret
= regulator_enable(consumers
[i
].consumer
);
1471 printk(KERN_ERR
"Failed to enable %s\n", consumers
[i
].supply
);
1472 for (i
= 0; i
< num_consumers
; i
++)
1473 regulator_disable(consumers
[i
].consumer
);
1477 EXPORT_SYMBOL_GPL(regulator_bulk_enable
);
1480 * regulator_bulk_disable - disable multiple regulator consumers
1482 * @num_consumers: Number of consumers
1483 * @consumers: Consumer data; clients are stored here.
1484 * @return 0 on success, an errno on failure
1486 * This convenience API allows consumers to disable multiple regulator
1487 * clients in a single API call. If any consumers cannot be enabled
1488 * then any others that were disabled will be disabled again prior to
1491 int regulator_bulk_disable(int num_consumers
,
1492 struct regulator_bulk_data
*consumers
)
1497 for (i
= 0; i
< num_consumers
; i
++) {
1498 ret
= regulator_disable(consumers
[i
].consumer
);
1506 printk(KERN_ERR
"Failed to disable %s\n", consumers
[i
].supply
);
1507 for (i
= 0; i
< num_consumers
; i
++)
1508 regulator_enable(consumers
[i
].consumer
);
1512 EXPORT_SYMBOL_GPL(regulator_bulk_disable
);
1515 * regulator_bulk_free - free multiple regulator consumers
1517 * @num_consumers: Number of consumers
1518 * @consumers: Consumer data; clients are stored here.
1520 * This convenience API allows consumers to free multiple regulator
1521 * clients in a single API call.
1523 void regulator_bulk_free(int num_consumers
,
1524 struct regulator_bulk_data
*consumers
)
1528 for (i
= 0; i
< num_consumers
; i
++) {
1529 regulator_put(consumers
[i
].consumer
);
1530 consumers
[i
].consumer
= NULL
;
1533 EXPORT_SYMBOL_GPL(regulator_bulk_free
);
1536 * regulator_notifier_call_chain - call regulator event notifier
1537 * @regulator: regulator source
1538 * @event: notifier block
1541 * Called by regulator drivers to notify clients a regulator event has
1542 * occurred. We also notify regulator clients downstream.
1544 int regulator_notifier_call_chain(struct regulator_dev
*rdev
,
1545 unsigned long event
, void *data
)
1547 _notifier_call_chain(rdev
, event
, data
);
1551 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain
);
1554 * regulator_register - register regulator
1555 * @regulator: regulator source
1556 * @reg_data: private regulator data
1558 * Called by regulator drivers to register a regulator.
1559 * Returns 0 on success.
1561 struct regulator_dev
*regulator_register(struct regulator_desc
*regulator_desc
,
1564 static atomic_t regulator_no
= ATOMIC_INIT(0);
1565 struct regulator_dev
*rdev
;
1568 if (regulator_desc
== NULL
)
1569 return ERR_PTR(-EINVAL
);
1571 if (regulator_desc
->name
== NULL
|| regulator_desc
->ops
== NULL
)
1572 return ERR_PTR(-EINVAL
);
1574 if (!regulator_desc
->type
== REGULATOR_VOLTAGE
&&
1575 !regulator_desc
->type
== REGULATOR_CURRENT
)
1576 return ERR_PTR(-EINVAL
);
1578 rdev
= kzalloc(sizeof(struct regulator_dev
), GFP_KERNEL
);
1580 return ERR_PTR(-ENOMEM
);
1582 mutex_lock(®ulator_list_mutex
);
1584 mutex_init(&rdev
->mutex
);
1585 rdev
->reg_data
= reg_data
;
1586 rdev
->owner
= regulator_desc
->owner
;
1587 rdev
->desc
= regulator_desc
;
1588 INIT_LIST_HEAD(&rdev
->consumer_list
);
1589 INIT_LIST_HEAD(&rdev
->supply_list
);
1590 INIT_LIST_HEAD(&rdev
->list
);
1591 INIT_LIST_HEAD(&rdev
->slist
);
1592 BLOCKING_INIT_NOTIFIER_HEAD(&rdev
->notifier
);
1594 rdev
->dev
.class = ®ulator_class
;
1595 device_initialize(&rdev
->dev
);
1596 snprintf(rdev
->dev
.bus_id
, sizeof(rdev
->dev
.bus_id
),
1598 (unsigned long)atomic_inc_return(®ulator_no
) - 1,
1599 regulator_desc
->name
);
1601 ret
= device_add(&rdev
->dev
);
1603 list_add(&rdev
->list
, ®ulator_list
);
1606 rdev
= ERR_PTR(ret
);
1608 mutex_unlock(®ulator_list_mutex
);
1611 EXPORT_SYMBOL_GPL(regulator_register
);
1614 * regulator_unregister - unregister regulator
1615 * @regulator: regulator source
1617 * Called by regulator drivers to unregister a regulator.
1619 void regulator_unregister(struct regulator_dev
*rdev
)
1624 mutex_lock(®ulator_list_mutex
);
1625 list_del(&rdev
->list
);
1627 sysfs_remove_link(&rdev
->dev
.kobj
, "supply");
1628 device_unregister(&rdev
->dev
);
1629 mutex_unlock(®ulator_list_mutex
);
1631 EXPORT_SYMBOL_GPL(regulator_unregister
);
1634 * regulator_set_supply - set regulator supply regulator
1635 * @regulator: regulator name
1636 * @supply: supply regulator name
1638 * Called by platform initialisation code to set the supply regulator for this
1639 * regulator. This ensures that a regulators supply will also be enabled by the
1640 * core if it's child is enabled.
1642 int regulator_set_supply(const char *regulator
, const char *supply
)
1644 struct regulator_dev
*rdev
, *supply_rdev
;
1647 if (regulator
== NULL
|| supply
== NULL
)
1650 mutex_lock(®ulator_list_mutex
);
1652 list_for_each_entry(rdev
, ®ulator_list
, list
) {
1653 if (!strcmp(rdev
->desc
->name
, regulator
))
1654 goto found_regulator
;
1656 mutex_unlock(®ulator_list_mutex
);
1660 list_for_each_entry(supply_rdev
, ®ulator_list
, list
) {
1661 if (!strcmp(supply_rdev
->desc
->name
, supply
))
1664 mutex_unlock(®ulator_list_mutex
);
1668 err
= sysfs_create_link(&rdev
->dev
.kobj
, &supply_rdev
->dev
.kobj
,
1672 "%s: could not add device link %s err %d\n",
1673 __func__
, supply_rdev
->dev
.kobj
.name
, err
);
1676 rdev
->supply
= supply_rdev
;
1677 list_add(&rdev
->slist
, &supply_rdev
->supply_list
);
1679 mutex_unlock(®ulator_list_mutex
);
1682 EXPORT_SYMBOL_GPL(regulator_set_supply
);
1685 * regulator_get_supply - get regulator supply regulator
1686 * @regulator: regulator name
1688 * Returns the supply supply regulator name or NULL if no supply regulator
1689 * exists (i.e the regulator is supplied directly from USB, Line, Battery, etc)
1691 const char *regulator_get_supply(const char *regulator
)
1693 struct regulator_dev
*rdev
;
1695 if (regulator
== NULL
)
1698 mutex_lock(®ulator_list_mutex
);
1699 list_for_each_entry(rdev
, ®ulator_list
, list
) {
1700 if (!strcmp(rdev
->desc
->name
, regulator
))
1703 mutex_unlock(®ulator_list_mutex
);
1707 mutex_unlock(®ulator_list_mutex
);
1709 return rdev
->supply
->desc
->name
;
1713 EXPORT_SYMBOL_GPL(regulator_get_supply
);
1716 * regulator_set_machine_constraints - sets regulator constraints
1717 * @regulator: regulator source
1719 * Allows platform initialisation code to define and constrain
1720 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1721 * Constraints *must* be set by platform code in order for some
1722 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1725 int regulator_set_machine_constraints(const char *regulator_name
,
1726 struct regulation_constraints
*constraints
)
1728 struct regulator_dev
*rdev
;
1731 if (regulator_name
== NULL
)
1734 mutex_lock(®ulator_list_mutex
);
1736 list_for_each_entry(rdev
, ®ulator_list
, list
) {
1737 if (!strcmp(regulator_name
, rdev
->desc
->name
))
1744 mutex_lock(&rdev
->mutex
);
1745 rdev
->constraints
= constraints
;
1747 /* do we need to apply the constraint voltage */
1748 if (rdev
->constraints
->apply_uV
&&
1749 rdev
->constraints
->min_uV
== rdev
->constraints
->max_uV
&&
1750 rdev
->desc
->ops
->set_voltage
) {
1751 ret
= rdev
->desc
->ops
->set_voltage(rdev
,
1752 rdev
->constraints
->min_uV
, rdev
->constraints
->max_uV
);
1754 printk(KERN_ERR
"%s: failed to apply %duV"
1755 " constraint\n", __func__
,
1756 rdev
->constraints
->min_uV
);
1757 rdev
->constraints
= NULL
;
1762 /* are we enabled at boot time by firmware / bootloader */
1763 if (rdev
->constraints
->boot_on
)
1764 rdev
->use_count
= 1;
1766 /* do we need to setup our suspend state */
1767 if (constraints
->initial_state
)
1768 ret
= suspend_prepare(rdev
, constraints
->initial_state
);
1770 print_constraints(rdev
);
1771 mutex_unlock(&rdev
->mutex
);
1774 mutex_unlock(®ulator_list_mutex
);
1777 EXPORT_SYMBOL_GPL(regulator_set_machine_constraints
);
1781 * regulator_set_device_supply: Bind a regulator to a symbolic supply
1782 * @regulator: regulator source
1783 * @dev: device the supply applies to
1784 * @supply: symbolic name for supply
1786 * Allows platform initialisation code to map physical regulator
1787 * sources to symbolic names for supplies for use by devices. Devices
1788 * should use these symbolic names to request regulators, avoiding the
1789 * need to provide board-specific regulator names as platform data.
1791 int regulator_set_device_supply(const char *regulator
, struct device
*dev
,
1794 struct regulator_map
*node
;
1796 if (regulator
== NULL
|| supply
== NULL
)
1799 node
= kmalloc(sizeof(struct regulator_map
), GFP_KERNEL
);
1803 node
->regulator
= regulator
;
1805 node
->supply
= supply
;
1807 mutex_lock(®ulator_list_mutex
);
1808 list_add(&node
->list
, ®ulator_map_list
);
1809 mutex_unlock(®ulator_list_mutex
);
1812 EXPORT_SYMBOL_GPL(regulator_set_device_supply
);
1815 * regulator_suspend_prepare: prepare regulators for system wide suspend
1816 * @state: system suspend state
1818 * Configure each regulator with it's suspend operating parameters for state.
1819 * This will usually be called by machine suspend code prior to supending.
1821 int regulator_suspend_prepare(suspend_state_t state
)
1823 struct regulator_dev
*rdev
;
1826 /* ON is handled by regulator active state */
1827 if (state
== PM_SUSPEND_ON
)
1830 mutex_lock(®ulator_list_mutex
);
1831 list_for_each_entry(rdev
, ®ulator_list
, list
) {
1833 mutex_lock(&rdev
->mutex
);
1834 ret
= suspend_prepare(rdev
, state
);
1835 mutex_unlock(&rdev
->mutex
);
1838 printk(KERN_ERR
"%s: failed to prepare %s\n",
1839 __func__
, rdev
->desc
->name
);
1844 mutex_unlock(®ulator_list_mutex
);
1847 EXPORT_SYMBOL_GPL(regulator_suspend_prepare
);
1850 * rdev_get_drvdata - get rdev regulator driver data
1851 * @regulator: regulator
1853 * Get rdev regulator driver private data. This call can be used in the
1854 * regulator driver context.
1856 void *rdev_get_drvdata(struct regulator_dev
*rdev
)
1858 return rdev
->reg_data
;
1860 EXPORT_SYMBOL_GPL(rdev_get_drvdata
);
1863 * regulator_get_drvdata - get regulator driver data
1864 * @regulator: regulator
1866 * Get regulator driver private data. This call can be used in the consumer
1867 * driver context when non API regulator specific functions need to be called.
1869 void *regulator_get_drvdata(struct regulator
*regulator
)
1871 return regulator
->rdev
->reg_data
;
1873 EXPORT_SYMBOL_GPL(regulator_get_drvdata
);
1876 * regulator_set_drvdata - set regulator driver data
1877 * @regulator: regulator
1880 void regulator_set_drvdata(struct regulator
*regulator
, void *data
)
1882 regulator
->rdev
->reg_data
= data
;
1884 EXPORT_SYMBOL_GPL(regulator_set_drvdata
);
1887 * regulator_get_id - get regulator ID
1888 * @regulator: regulator
1890 int rdev_get_id(struct regulator_dev
*rdev
)
1892 return rdev
->desc
->id
;
1894 EXPORT_SYMBOL_GPL(rdev_get_id
);
1896 static int __init
regulator_init(void)
1898 printk(KERN_INFO
"regulator: core version %s\n", REGULATOR_VERSION
);
1899 return class_register(®ulator_class
);
1902 /* init early to allow our consumers to complete system booting */
1903 core_initcall(regulator_init
);