Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/dvrabel...
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / regulator / core.c
blob9c79862615685d39d92f7e2b73cd2a8638bf21d7
1 /*
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);
31 /**
32 * struct regulator_dev
34 * Voltage / Current regulator class device. One for each regulator.
36 struct regulator_dev {
37 struct regulator_desc *desc;
38 int use_count;
40 /* lists we belong to */
41 struct list_head list; /* list of all regulators */
42 struct list_head slist; /* list of supplied regulators */
44 /* lists we own */
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 */
50 struct module *owner;
51 struct device dev;
52 struct regulation_constraints *constraints;
53 struct regulator_dev *supply; /* for tree */
55 void *reg_data; /* regulator_dev data */
58 /**
59 * struct regulator_map
61 * Used to provide symbolic supply names to devices.
63 struct regulator_map {
64 struct list_head list;
65 struct device *dev;
66 const char *supply;
67 const char *regulator;
70 static inline struct regulator_dev *to_rdev(struct device *d)
72 return container_of(d, struct regulator_dev, dev);
76 * struct regulator
78 * One for each consumer device.
80 struct regulator {
81 struct device *dev;
82 struct list_head list;
83 int uA_load;
84 int min_uV;
85 int max_uV;
86 int enabled; /* client has called enabled */
87 char *supply_name;
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(&regulator_list_mutex);
107 list_for_each_entry(rdev, &regulator_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(&regulator_list_mutex);
113 return regulator;
116 mutex_unlock(&rdev->mutex);
118 mutex_unlock(&regulator_list_mutex);
119 return NULL;
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__,
130 rdev->desc->name);
131 return -ENODEV;
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);
136 return -EPERM;
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)
145 return -EINVAL;
147 return 0;
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__,
158 rdev->desc->name);
159 return -ENODEV;
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);
164 return -EPERM;
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)
173 return -EINVAL;
175 return 0;
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__,
183 rdev->desc->name);
184 return -ENODEV;
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);
189 return -EPERM;
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);
194 return -EINVAL;
196 return 0;
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__,
204 rdev->desc->name);
205 return -ENODEV;
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);
210 return -EPERM;
212 return 0;
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)
222 return 0;
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);
231 ssize_t ret;
233 mutex_lock(&rdev->mutex);
234 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
235 mutex_unlock(&rdev->mutex);
237 return ret;
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);
254 switch (mode) {
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);
273 if (state > 0)
274 return sprintf(buf, "enabled\n");
275 else if (state == 0)
276 return sprintf(buf, "disabled\n");
277 else
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;
330 int uA = 0;
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)
393 switch (mode) {
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");
449 else
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");
463 else
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");
477 else
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),
510 __ATTR_NULL,
513 static void regulator_dev_release(struct device *dev)
515 struct regulator_dev *rdev = to_rdev(dev);
516 kfree(rdev);
519 static struct class regulator_class = {
520 .name = "regulator",
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;
531 unsigned int mode;
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);
536 return;
538 /* get output voltage */
539 output_uV = rdev->desc->ops->get_voltage(rdev);
540 if (output_uV <= 0)
541 return;
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);
546 else
547 input_uV = rdev->constraints->input_uV;
548 if (input_uV <= 0)
549 return;
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);
561 if (err == 0)
562 rdev->desc->ops->set_mode(rdev, mode);
565 static int suspend_set_state(struct regulator_dev *rdev,
566 struct regulator_state *rstate)
568 int ret = 0;
570 /* enable & disable are mandatory for suspend control */
571 if (!rdev->desc->ops->set_suspend_enable ||
572 !rdev->desc->ops->set_suspend_disable)
573 return -EINVAL;
575 if (rstate->enabled)
576 ret = rdev->desc->ops->set_suspend_enable(rdev);
577 else
578 ret = rdev->desc->ops->set_suspend_disable(rdev);
579 if (ret < 0) {
580 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
581 return ret;
584 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
585 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
586 if (ret < 0) {
587 printk(KERN_ERR "%s: failed to set voltage\n",
588 __func__);
589 return ret;
593 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
594 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
595 if (ret < 0) {
596 printk(KERN_ERR "%s: failed to set mode\n", __func__);
597 return ret;
600 return ret;
603 /* locks held by caller */
604 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
606 if (!rdev->constraints)
607 return -EINVAL;
609 switch (state) {
610 case PM_SUSPEND_STANDBY:
611 return suspend_set_state(rdev,
612 &rdev->constraints->state_standby);
613 case PM_SUSPEND_MEM:
614 return suspend_set_state(rdev,
615 &rdev->constraints->state_mem);
616 case PM_SUSPEND_MAX:
617 return suspend_set_state(rdev,
618 &rdev->constraints->state_disk);
619 default:
620 return -EINVAL;
624 static void print_constraints(struct regulator_dev *rdev)
626 struct regulation_constraints *constraints = rdev->constraints;
627 char buf[80];
628 int count;
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);
634 else
635 count = sprintf(buf, "%d <--> %d mV ",
636 constraints->min_uV / 1000,
637 constraints->max_uV / 1000);
638 } else {
639 if (constraints->min_uA == constraints->max_uA)
640 count = sprintf(buf, "%d mA ",
641 constraints->min_uA / 1000);
642 else
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,
662 struct device *dev,
663 const char *supply_name)
665 struct regulator *regulator;
666 char buf[REG_STR_SIZE];
667 int err, size;
669 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
670 if (regulator == NULL)
671 return NULL;
673 mutex_lock(&rdev->mutex);
674 regulator->rdev = rdev;
675 list_add(&regulator->list, &rdev->consumer_list);
677 if (dev) {
678 /* create a 'requested_microamps_name' sysfs entry */
679 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
680 supply_name);
681 if (size >= REG_STR_SIZE)
682 goto overflow_err;
684 regulator->dev = dev;
685 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
686 if (regulator->dev_attr.attr.name == NULL)
687 goto attr_name_err;
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, &regulator->dev_attr);
693 if (err < 0) {
694 printk(KERN_WARNING "%s: could not add regulator_dev"
695 " load sysfs\n", __func__);
696 goto attr_name_err;
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)
703 goto attr_err;
705 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
706 if (regulator->supply_name == NULL)
707 goto attr_err;
709 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
710 buf);
711 if (err) {
712 printk(KERN_WARNING
713 "%s: could not add device link %s err %d\n",
714 __func__, dev->kobj.name, err);
715 device_remove_file(dev, &regulator->dev_attr);
716 goto link_name_err;
719 mutex_unlock(&rdev->mutex);
720 return regulator;
721 link_name_err:
722 kfree(regulator->supply_name);
723 attr_err:
724 device_remove_file(regulator->dev, &regulator->dev_attr);
725 attr_name_err:
726 kfree(regulator->dev_attr.attr.name);
727 overflow_err:
728 list_del(&regulator->list);
729 kfree(regulator);
730 mutex_unlock(&rdev->mutex);
731 return NULL;
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
742 * encouraged.
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;
751 if (id == NULL) {
752 printk(KERN_ERR "regulator: get() with no identifier\n");
753 return regulator;
756 mutex_lock(&regulator_list_mutex);
758 list_for_each_entry(map, &regulator_map_list, list) {
759 if (dev == map->dev &&
760 strcmp(map->supply, id) == 0) {
761 supply = map->regulator;
762 break;
766 list_for_each_entry(rdev, &regulator_list, list) {
767 if (strcmp(supply, rdev->desc->name) == 0 &&
768 try_module_get(rdev->owner))
769 goto found;
771 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
772 id);
773 mutex_unlock(&regulator_list_mutex);
774 return regulator;
776 found:
777 regulator = create_regulator(rdev, dev, id);
778 if (regulator == NULL) {
779 regulator = ERR_PTR(-ENOMEM);
780 module_put(rdev->owner);
783 mutex_unlock(&regulator_list_mutex);
784 return regulator;
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
794 * this function.
796 void regulator_put(struct regulator *regulator)
798 struct regulator_dev *rdev;
800 if (regulator == NULL || IS_ERR(regulator))
801 return;
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(&regulator_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, &regulator->dev_attr);
818 kfree(regulator->dev_attr.attr.name);
820 list_del(&regulator->list);
821 kfree(regulator);
823 module_put(rdev->owner);
824 mutex_unlock(&regulator_list_mutex);
826 EXPORT_SYMBOL_GPL(regulator_put);
828 /* locks held by regulator_enable() */
829 static int _regulator_enable(struct regulator_dev *rdev)
831 int ret = -EINVAL;
833 if (!rdev->constraints) {
834 printk(KERN_ERR "%s: %s has no constraints\n",
835 __func__, rdev->desc->name);
836 return ret;
839 /* do we need to enable the supply regulator first */
840 if (rdev->supply) {
841 ret = _regulator_enable(rdev->supply);
842 if (ret < 0) {
843 printk(KERN_ERR "%s: failed to enable %s: %d\n",
844 __func__, rdev->desc->name, ret);
845 return 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);
858 if (ret < 0) {
859 printk(KERN_ERR "%s: failed to enable %s: %d\n",
860 __func__, rdev->desc->name, ret);
861 return ret;
863 rdev->use_count++;
864 return ret;
867 return 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)
882 int ret;
884 if (regulator->enabled) {
885 printk(KERN_CRIT "Regulator %s already enabled\n",
886 regulator->supply_name);
887 WARN_ON(regulator->enabled);
888 return 0;
891 mutex_lock(&regulator->rdev->mutex);
892 regulator->enabled = 1;
893 ret = _regulator_enable(regulator->rdev);
894 if (ret != 0)
895 regulator->enabled = 0;
896 mutex_unlock(&regulator->rdev->mutex);
897 return ret;
899 EXPORT_SYMBOL_GPL(regulator_enable);
901 /* locks held by regulator_disable() */
902 static int _regulator_disable(struct regulator_dev *rdev)
904 int ret = 0;
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);
912 if (ret < 0) {
913 printk(KERN_ERR "%s: failed to disable %s\n",
914 __func__, rdev->desc->name);
915 return ret;
919 /* decrease our supplies ref count and disable if required */
920 if (rdev->supply)
921 _regulator_disable(rdev->supply);
923 rdev->use_count = 0;
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);
931 rdev->use_count--;
933 return ret;
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)
948 int ret;
950 if (!regulator->enabled) {
951 printk(KERN_ERR "%s: not in use by this consumer\n",
952 __func__);
953 return 0;
956 mutex_lock(&regulator->rdev->mutex);
957 regulator->enabled = 0;
958 regulator->uA_load = 0;
959 ret = _regulator_disable(regulator->rdev);
960 mutex_unlock(&regulator->rdev->mutex);
961 return ret;
963 EXPORT_SYMBOL_GPL(regulator_disable);
965 /* locks held by regulator_force_disable() */
966 static int _regulator_force_disable(struct regulator_dev *rdev)
968 int ret = 0;
970 /* force disable */
971 if (rdev->desc->ops->disable) {
972 /* ah well, who wants to live forever... */
973 ret = rdev->desc->ops->disable(rdev);
974 if (ret < 0) {
975 printk(KERN_ERR "%s: failed to force disable %s\n",
976 __func__, rdev->desc->name);
977 return ret;
979 /* notify other consumers that power has been forced off */
980 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
981 NULL);
984 /* decrease our supplies ref count and disable if required */
985 if (rdev->supply)
986 _regulator_disable(rdev->supply);
988 rdev->use_count = 0;
989 return ret;
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)
1003 int ret;
1005 mutex_lock(&regulator->rdev->mutex);
1006 regulator->enabled = 0;
1007 regulator->uA_load = 0;
1008 ret = _regulator_force_disable(regulator->rdev);
1009 mutex_unlock(&regulator->rdev->mutex);
1010 return ret;
1012 EXPORT_SYMBOL_GPL(regulator_force_disable);
1014 static int _regulator_is_enabled(struct regulator_dev *rdev)
1016 int ret;
1018 mutex_lock(&rdev->mutex);
1020 /* sanity check */
1021 if (!rdev->desc->ops->is_enabled) {
1022 ret = -EINVAL;
1023 goto out;
1026 ret = rdev->desc->ops->is_enabled(rdev);
1027 out:
1028 mutex_unlock(&rdev->mutex);
1029 return ret;
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;
1065 int ret;
1067 mutex_lock(&rdev->mutex);
1069 /* sanity check */
1070 if (!rdev->desc->ops->set_voltage) {
1071 ret = -EINVAL;
1072 goto out;
1075 /* constraints check */
1076 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1077 if (ret < 0)
1078 goto out;
1079 regulator->min_uV = min_uV;
1080 regulator->max_uV = max_uV;
1081 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1083 out:
1084 mutex_unlock(&rdev->mutex);
1085 return ret;
1087 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1089 static int _regulator_get_voltage(struct regulator_dev *rdev)
1091 /* sanity check */
1092 if (rdev->desc->ops->get_voltage)
1093 return rdev->desc->ops->get_voltage(rdev);
1094 else
1095 return -EINVAL;
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)
1109 int ret;
1111 mutex_lock(&regulator->rdev->mutex);
1113 ret = _regulator_get_voltage(regulator->rdev);
1115 mutex_unlock(&regulator->rdev->mutex);
1117 return ret;
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;
1141 int ret;
1143 mutex_lock(&rdev->mutex);
1145 /* sanity check */
1146 if (!rdev->desc->ops->set_current_limit) {
1147 ret = -EINVAL;
1148 goto out;
1151 /* constraints check */
1152 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1153 if (ret < 0)
1154 goto out;
1156 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1157 out:
1158 mutex_unlock(&rdev->mutex);
1159 return ret;
1161 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1163 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1165 int ret;
1167 mutex_lock(&rdev->mutex);
1169 /* sanity check */
1170 if (!rdev->desc->ops->get_current_limit) {
1171 ret = -EINVAL;
1172 goto out;
1175 ret = rdev->desc->ops->get_current_limit(rdev);
1176 out:
1177 mutex_unlock(&rdev->mutex);
1178 return ret;
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;
1210 int ret;
1212 mutex_lock(&rdev->mutex);
1214 /* sanity check */
1215 if (!rdev->desc->ops->set_mode) {
1216 ret = -EINVAL;
1217 goto out;
1220 /* constraints check */
1221 ret = regulator_check_mode(rdev, mode);
1222 if (ret < 0)
1223 goto out;
1225 ret = rdev->desc->ops->set_mode(rdev, mode);
1226 out:
1227 mutex_unlock(&rdev->mutex);
1228 return ret;
1230 EXPORT_SYMBOL_GPL(regulator_set_mode);
1232 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1234 int ret;
1236 mutex_lock(&rdev->mutex);
1238 /* sanity check */
1239 if (!rdev->desc->ops->get_mode) {
1240 ret = -EINVAL;
1241 goto out;
1244 ret = rdev->desc->ops->get_mode(rdev);
1245 out:
1246 mutex_unlock(&rdev->mutex);
1247 return ret;
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;
1293 unsigned int mode;
1295 mutex_lock(&rdev->mutex);
1297 regulator->uA_load = uA_load;
1298 ret = regulator_check_drms(rdev);
1299 if (ret < 0)
1300 goto out;
1301 ret = -EINVAL;
1303 /* sanity check */
1304 if (!rdev->desc->ops->get_optimum_mode)
1305 goto out;
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);
1312 goto out;
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);
1318 else
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);
1323 goto out;
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,
1332 total_uA_load);
1333 if (ret <= 0) {
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);
1337 goto out;
1340 ret = rdev->desc->ops->set_mode(rdev, mode);
1341 if (ret <= 0) {
1342 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1343 __func__, mode, rdev->desc->name);
1344 goto out;
1346 ret = mode;
1347 out:
1348 mutex_unlock(&rdev->mutex);
1349 return ret;
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(&regulator->rdev->notifier,
1364 nb);
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(&regulator->rdev->notifier,
1379 nb);
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)
1416 int i;
1417 int ret;
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;
1430 goto err;
1434 return 0;
1436 err:
1437 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1438 regulator_put(consumers[i].consumer);
1440 return ret;
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
1454 * return.
1456 int regulator_bulk_enable(int num_consumers,
1457 struct regulator_bulk_data *consumers)
1459 int i;
1460 int ret;
1462 for (i = 0; i < num_consumers; i++) {
1463 ret = regulator_enable(consumers[i].consumer);
1464 if (ret != 0)
1465 goto err;
1468 return 0;
1470 err:
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);
1475 return ret;
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
1489 * return.
1491 int regulator_bulk_disable(int num_consumers,
1492 struct regulator_bulk_data *consumers)
1494 int i;
1495 int ret;
1497 for (i = 0; i < num_consumers; i++) {
1498 ret = regulator_disable(consumers[i].consumer);
1499 if (ret != 0)
1500 goto err;
1503 return 0;
1505 err:
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);
1510 return ret;
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)
1526 int i;
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
1539 * @data:
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);
1548 return NOTIFY_DONE;
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,
1562 void *reg_data)
1564 static atomic_t regulator_no = ATOMIC_INIT(0);
1565 struct regulator_dev *rdev;
1566 int ret;
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);
1579 if (rdev == NULL)
1580 return ERR_PTR(-ENOMEM);
1582 mutex_lock(&regulator_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 = &regulator_class;
1595 device_initialize(&rdev->dev);
1596 snprintf(rdev->dev.bus_id, sizeof(rdev->dev.bus_id),
1597 "regulator_%ld_%s",
1598 (unsigned long)atomic_inc_return(&regulator_no) - 1,
1599 regulator_desc->name);
1601 ret = device_add(&rdev->dev);
1602 if (ret == 0)
1603 list_add(&rdev->list, &regulator_list);
1604 else {
1605 kfree(rdev);
1606 rdev = ERR_PTR(ret);
1608 mutex_unlock(&regulator_list_mutex);
1609 return rdev;
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)
1621 if (rdev == NULL)
1622 return;
1624 mutex_lock(&regulator_list_mutex);
1625 list_del(&rdev->list);
1626 if (rdev->supply)
1627 sysfs_remove_link(&rdev->dev.kobj, "supply");
1628 device_unregister(&rdev->dev);
1629 mutex_unlock(&regulator_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;
1645 int err;
1647 if (regulator == NULL || supply == NULL)
1648 return -EINVAL;
1650 mutex_lock(&regulator_list_mutex);
1652 list_for_each_entry(rdev, &regulator_list, list) {
1653 if (!strcmp(rdev->desc->name, regulator))
1654 goto found_regulator;
1656 mutex_unlock(&regulator_list_mutex);
1657 return -ENODEV;
1659 found_regulator:
1660 list_for_each_entry(supply_rdev, &regulator_list, list) {
1661 if (!strcmp(supply_rdev->desc->name, supply))
1662 goto found_supply;
1664 mutex_unlock(&regulator_list_mutex);
1665 return -ENODEV;
1667 found_supply:
1668 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
1669 "supply");
1670 if (err) {
1671 printk(KERN_ERR
1672 "%s: could not add device link %s err %d\n",
1673 __func__, supply_rdev->dev.kobj.name, err);
1674 goto out;
1676 rdev->supply = supply_rdev;
1677 list_add(&rdev->slist, &supply_rdev->supply_list);
1678 out:
1679 mutex_unlock(&regulator_list_mutex);
1680 return err;
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)
1696 return NULL;
1698 mutex_lock(&regulator_list_mutex);
1699 list_for_each_entry(rdev, &regulator_list, list) {
1700 if (!strcmp(rdev->desc->name, regulator))
1701 goto found;
1703 mutex_unlock(&regulator_list_mutex);
1704 return NULL;
1706 found:
1707 mutex_unlock(&regulator_list_mutex);
1708 if (rdev->supply)
1709 return rdev->supply->desc->name;
1710 else
1711 return NULL;
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,
1723 * set_mode.
1725 int regulator_set_machine_constraints(const char *regulator_name,
1726 struct regulation_constraints *constraints)
1728 struct regulator_dev *rdev;
1729 int ret = 0;
1731 if (regulator_name == NULL)
1732 return -EINVAL;
1734 mutex_lock(&regulator_list_mutex);
1736 list_for_each_entry(rdev, &regulator_list, list) {
1737 if (!strcmp(regulator_name, rdev->desc->name))
1738 goto found;
1740 ret = -ENODEV;
1741 goto out;
1743 found:
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);
1753 if (ret < 0) {
1754 printk(KERN_ERR "%s: failed to apply %duV"
1755 " constraint\n", __func__,
1756 rdev->constraints->min_uV);
1757 rdev->constraints = NULL;
1758 goto out;
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);
1773 out:
1774 mutex_unlock(&regulator_list_mutex);
1775 return ret;
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,
1792 const char *supply)
1794 struct regulator_map *node;
1796 if (regulator == NULL || supply == NULL)
1797 return -EINVAL;
1799 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
1800 if (node == NULL)
1801 return -ENOMEM;
1803 node->regulator = regulator;
1804 node->dev = dev;
1805 node->supply = supply;
1807 mutex_lock(&regulator_list_mutex);
1808 list_add(&node->list, &regulator_map_list);
1809 mutex_unlock(&regulator_list_mutex);
1810 return 0;
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;
1824 int ret = 0;
1826 /* ON is handled by regulator active state */
1827 if (state == PM_SUSPEND_ON)
1828 return -EINVAL;
1830 mutex_lock(&regulator_list_mutex);
1831 list_for_each_entry(rdev, &regulator_list, list) {
1833 mutex_lock(&rdev->mutex);
1834 ret = suspend_prepare(rdev, state);
1835 mutex_unlock(&rdev->mutex);
1837 if (ret < 0) {
1838 printk(KERN_ERR "%s: failed to prepare %s\n",
1839 __func__, rdev->desc->name);
1840 goto out;
1843 out:
1844 mutex_unlock(&regulator_list_mutex);
1845 return ret;
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
1878 * @data: data
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(&regulator_class);
1902 /* init early to allow our consumers to complete system booting */
1903 core_initcall(regulator_init);