2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/mutex.h>
21 #include <linux/suspend.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
26 #define REGULATOR_VERSION "0.5"
28 static DEFINE_MUTEX(regulator_list_mutex);
29 static LIST_HEAD(regulator_list);
30 static LIST_HEAD(regulator_map_list);
33 * struct regulator_dev
35 * Voltage / Current regulator class device. One for each regulator.
37 struct regulator_dev {
38 struct regulator_desc *desc;
41 /* lists we belong to */
42 struct list_head list; /* list of all regulators */
43 struct list_head slist; /* list of supplied regulators */
46 struct list_head consumer_list; /* consumers we supply */
47 struct list_head supply_list; /* regulators we supply */
49 struct blocking_notifier_head notifier;
50 struct mutex mutex; /* consumer lock */
53 struct regulation_constraints *constraints;
54 struct regulator_dev *supply; /* for tree */
56 void *reg_data; /* regulator_dev data */
60 * struct regulator_map
62 * Used to provide symbolic supply names to devices.
64 struct regulator_map {
65 struct list_head list;
68 struct regulator_dev *regulator;
74 * One for each consumer device.
78 struct list_head list;
82 int enabled; /* count of client enables */
84 struct device_attribute dev_attr;
85 struct regulator_dev *rdev;
88 static int _regulator_is_enabled(struct regulator_dev *rdev);
89 static int _regulator_disable(struct regulator_dev *rdev);
90 static int _regulator_get_voltage(struct regulator_dev *rdev);
91 static int _regulator_get_current_limit(struct regulator_dev *rdev);
92 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
93 static void _notifier_call_chain(struct regulator_dev *rdev,
94 unsigned long event, void *data);
96 /* gets the regulator for a given consumer device */
97 static struct regulator *get_device_regulator(struct device *dev)
99 struct regulator *regulator = NULL;
100 struct regulator_dev *rdev;
102 mutex_lock(®ulator_list_mutex);
103 list_for_each_entry(rdev, ®ulator_list, list) {
104 mutex_lock(&rdev->mutex);
105 list_for_each_entry(regulator, &rdev->consumer_list, list) {
106 if (regulator->dev == dev) {
107 mutex_unlock(&rdev->mutex);
108 mutex_unlock(®ulator_list_mutex);
112 mutex_unlock(&rdev->mutex);
114 mutex_unlock(®ulator_list_mutex);
118 /* Platform voltage constraint check */
119 static int regulator_check_voltage(struct regulator_dev *rdev,
120 int *min_uV, int *max_uV)
122 BUG_ON(*min_uV > *max_uV);
124 if (!rdev->constraints) {
125 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
129 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
130 printk(KERN_ERR "%s: operation not allowed for %s\n",
131 __func__, rdev->desc->name);
135 if (*max_uV > rdev->constraints->max_uV)
136 *max_uV = rdev->constraints->max_uV;
137 if (*min_uV < rdev->constraints->min_uV)
138 *min_uV = rdev->constraints->min_uV;
140 if (*min_uV > *max_uV)
146 /* current constraint check */
147 static int regulator_check_current_limit(struct regulator_dev *rdev,
148 int *min_uA, int *max_uA)
150 BUG_ON(*min_uA > *max_uA);
152 if (!rdev->constraints) {
153 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
157 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
158 printk(KERN_ERR "%s: operation not allowed for %s\n",
159 __func__, rdev->desc->name);
163 if (*max_uA > rdev->constraints->max_uA)
164 *max_uA = rdev->constraints->max_uA;
165 if (*min_uA < rdev->constraints->min_uA)
166 *min_uA = rdev->constraints->min_uA;
168 if (*min_uA > *max_uA)
174 /* operating mode constraint check */
175 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
177 if (!rdev->constraints) {
178 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
182 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
183 printk(KERN_ERR "%s: operation not allowed for %s\n",
184 __func__, rdev->desc->name);
187 if (!(rdev->constraints->valid_modes_mask & mode)) {
188 printk(KERN_ERR "%s: invalid mode %x for %s\n",
189 __func__, mode, rdev->desc->name);
195 /* dynamic regulator mode switching constraint check */
196 static int regulator_check_drms(struct regulator_dev *rdev)
198 if (!rdev->constraints) {
199 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
203 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
204 printk(KERN_ERR "%s: operation not allowed for %s\n",
205 __func__, rdev->desc->name);
211 static ssize_t device_requested_uA_show(struct device *dev,
212 struct device_attribute *attr, char *buf)
214 struct regulator *regulator;
216 regulator = get_device_regulator(dev);
217 if (regulator == NULL)
220 return sprintf(buf, "%d\n", regulator->uA_load);
223 static ssize_t regulator_uV_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
226 struct regulator_dev *rdev = dev_get_drvdata(dev);
229 mutex_lock(&rdev->mutex);
230 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
231 mutex_unlock(&rdev->mutex);
236 static ssize_t regulator_uA_show(struct device *dev,
237 struct device_attribute *attr, char *buf)
239 struct regulator_dev *rdev = dev_get_drvdata(dev);
241 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
244 static ssize_t regulator_name_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 struct regulator_dev *rdev = dev_get_drvdata(dev);
250 if (rdev->constraints->name)
251 name = rdev->constraints->name;
252 else if (rdev->desc->name)
253 name = rdev->desc->name;
257 return sprintf(buf, "%s\n", name);
260 static ssize_t regulator_opmode_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
263 struct regulator_dev *rdev = dev_get_drvdata(dev);
264 int mode = _regulator_get_mode(rdev);
267 case REGULATOR_MODE_FAST:
268 return sprintf(buf, "fast\n");
269 case REGULATOR_MODE_NORMAL:
270 return sprintf(buf, "normal\n");
271 case REGULATOR_MODE_IDLE:
272 return sprintf(buf, "idle\n");
273 case REGULATOR_MODE_STANDBY:
274 return sprintf(buf, "standby\n");
276 return sprintf(buf, "unknown\n");
279 static ssize_t regulator_state_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
282 struct regulator_dev *rdev = dev_get_drvdata(dev);
283 int state = _regulator_is_enabled(rdev);
286 return sprintf(buf, "enabled\n");
288 return sprintf(buf, "disabled\n");
290 return sprintf(buf, "unknown\n");
293 static ssize_t regulator_min_uA_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct regulator_dev *rdev = dev_get_drvdata(dev);
298 if (!rdev->constraints)
299 return sprintf(buf, "constraint not defined\n");
301 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
304 static ssize_t regulator_max_uA_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 struct regulator_dev *rdev = dev_get_drvdata(dev);
309 if (!rdev->constraints)
310 return sprintf(buf, "constraint not defined\n");
312 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
315 static ssize_t regulator_min_uV_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct regulator_dev *rdev = dev_get_drvdata(dev);
320 if (!rdev->constraints)
321 return sprintf(buf, "constraint not defined\n");
323 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
326 static ssize_t regulator_max_uV_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 struct regulator_dev *rdev = dev_get_drvdata(dev);
331 if (!rdev->constraints)
332 return sprintf(buf, "constraint not defined\n");
334 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
337 static ssize_t regulator_total_uA_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
340 struct regulator_dev *rdev = dev_get_drvdata(dev);
341 struct regulator *regulator;
344 mutex_lock(&rdev->mutex);
345 list_for_each_entry(regulator, &rdev->consumer_list, list)
346 uA += regulator->uA_load;
347 mutex_unlock(&rdev->mutex);
348 return sprintf(buf, "%d\n", uA);
351 static ssize_t regulator_num_users_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
354 struct regulator_dev *rdev = dev_get_drvdata(dev);
355 return sprintf(buf, "%d\n", rdev->use_count);
358 static ssize_t regulator_type_show(struct device *dev,
359 struct device_attribute *attr, char *buf)
361 struct regulator_dev *rdev = dev_get_drvdata(dev);
363 switch (rdev->desc->type) {
364 case REGULATOR_VOLTAGE:
365 return sprintf(buf, "voltage\n");
366 case REGULATOR_CURRENT:
367 return sprintf(buf, "current\n");
369 return sprintf(buf, "unknown\n");
372 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
373 struct device_attribute *attr, char *buf)
375 struct regulator_dev *rdev = dev_get_drvdata(dev);
377 if (!rdev->constraints)
378 return sprintf(buf, "not defined\n");
379 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
382 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
385 struct regulator_dev *rdev = dev_get_drvdata(dev);
387 if (!rdev->constraints)
388 return sprintf(buf, "not defined\n");
389 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
392 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
395 struct regulator_dev *rdev = dev_get_drvdata(dev);
397 if (!rdev->constraints)
398 return sprintf(buf, "not defined\n");
399 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
402 static ssize_t suspend_opmode_show(struct regulator_dev *rdev,
403 unsigned int mode, char *buf)
406 case REGULATOR_MODE_FAST:
407 return sprintf(buf, "fast\n");
408 case REGULATOR_MODE_NORMAL:
409 return sprintf(buf, "normal\n");
410 case REGULATOR_MODE_IDLE:
411 return sprintf(buf, "idle\n");
412 case REGULATOR_MODE_STANDBY:
413 return sprintf(buf, "standby\n");
415 return sprintf(buf, "unknown\n");
418 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
419 struct device_attribute *attr, char *buf)
421 struct regulator_dev *rdev = dev_get_drvdata(dev);
423 if (!rdev->constraints)
424 return sprintf(buf, "not defined\n");
425 return suspend_opmode_show(rdev,
426 rdev->constraints->state_mem.mode, buf);
429 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 if (!rdev->constraints)
435 return sprintf(buf, "not defined\n");
436 return suspend_opmode_show(rdev,
437 rdev->constraints->state_disk.mode, buf);
440 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct regulator_dev *rdev = dev_get_drvdata(dev);
445 if (!rdev->constraints)
446 return sprintf(buf, "not defined\n");
447 return suspend_opmode_show(rdev,
448 rdev->constraints->state_standby.mode, buf);
451 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
454 struct regulator_dev *rdev = dev_get_drvdata(dev);
456 if (!rdev->constraints)
457 return sprintf(buf, "not defined\n");
459 if (rdev->constraints->state_mem.enabled)
460 return sprintf(buf, "enabled\n");
462 return sprintf(buf, "disabled\n");
465 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct regulator_dev *rdev = dev_get_drvdata(dev);
470 if (!rdev->constraints)
471 return sprintf(buf, "not defined\n");
473 if (rdev->constraints->state_disk.enabled)
474 return sprintf(buf, "enabled\n");
476 return sprintf(buf, "disabled\n");
479 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
482 struct regulator_dev *rdev = dev_get_drvdata(dev);
484 if (!rdev->constraints)
485 return sprintf(buf, "not defined\n");
487 if (rdev->constraints->state_standby.enabled)
488 return sprintf(buf, "enabled\n");
490 return sprintf(buf, "disabled\n");
493 static struct device_attribute regulator_dev_attrs[] = {
494 __ATTR(name, 0444, regulator_name_show, NULL),
495 __ATTR(microvolts, 0444, regulator_uV_show, NULL),
496 __ATTR(microamps, 0444, regulator_uA_show, NULL),
497 __ATTR(opmode, 0444, regulator_opmode_show, NULL),
498 __ATTR(state, 0444, regulator_state_show, NULL),
499 __ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL),
500 __ATTR(min_microamps, 0444, regulator_min_uA_show, NULL),
501 __ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL),
502 __ATTR(max_microamps, 0444, regulator_max_uA_show, NULL),
503 __ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL),
504 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
505 __ATTR(type, 0444, regulator_type_show, NULL),
506 __ATTR(suspend_mem_microvolts, 0444,
507 regulator_suspend_mem_uV_show, NULL),
508 __ATTR(suspend_disk_microvolts, 0444,
509 regulator_suspend_disk_uV_show, NULL),
510 __ATTR(suspend_standby_microvolts, 0444,
511 regulator_suspend_standby_uV_show, NULL),
512 __ATTR(suspend_mem_mode, 0444,
513 regulator_suspend_mem_mode_show, NULL),
514 __ATTR(suspend_disk_mode, 0444,
515 regulator_suspend_disk_mode_show, NULL),
516 __ATTR(suspend_standby_mode, 0444,
517 regulator_suspend_standby_mode_show, NULL),
518 __ATTR(suspend_mem_state, 0444,
519 regulator_suspend_mem_state_show, NULL),
520 __ATTR(suspend_disk_state, 0444,
521 regulator_suspend_disk_state_show, NULL),
522 __ATTR(suspend_standby_state, 0444,
523 regulator_suspend_standby_state_show, NULL),
527 static void regulator_dev_release(struct device *dev)
529 struct regulator_dev *rdev = dev_get_drvdata(dev);
533 static struct class regulator_class = {
535 .dev_release = regulator_dev_release,
536 .dev_attrs = regulator_dev_attrs,
539 /* Calculate the new optimum regulator operating mode based on the new total
540 * consumer load. All locks held by caller */
541 static void drms_uA_update(struct regulator_dev *rdev)
543 struct regulator *sibling;
544 int current_uA = 0, output_uV, input_uV, err;
547 err = regulator_check_drms(rdev);
548 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
549 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
552 /* get output voltage */
553 output_uV = rdev->desc->ops->get_voltage(rdev);
557 /* get input voltage */
558 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
559 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
561 input_uV = rdev->constraints->input_uV;
565 /* calc total requested load */
566 list_for_each_entry(sibling, &rdev->consumer_list, list)
567 current_uA += sibling->uA_load;
569 /* now get the optimum mode for our new total regulator load */
570 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
571 output_uV, current_uA);
573 /* check the new mode is allowed */
574 err = regulator_check_mode(rdev, mode);
576 rdev->desc->ops->set_mode(rdev, mode);
579 static int suspend_set_state(struct regulator_dev *rdev,
580 struct regulator_state *rstate)
584 /* enable & disable are mandatory for suspend control */
585 if (!rdev->desc->ops->set_suspend_enable ||
586 !rdev->desc->ops->set_suspend_disable) {
587 printk(KERN_ERR "%s: no way to set suspend state\n",
593 ret = rdev->desc->ops->set_suspend_enable(rdev);
595 ret = rdev->desc->ops->set_suspend_disable(rdev);
597 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
601 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
602 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
604 printk(KERN_ERR "%s: failed to set voltage\n",
610 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
611 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
613 printk(KERN_ERR "%s: failed to set mode\n", __func__);
620 /* locks held by caller */
621 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
623 if (!rdev->constraints)
627 case PM_SUSPEND_STANDBY:
628 return suspend_set_state(rdev,
629 &rdev->constraints->state_standby);
631 return suspend_set_state(rdev,
632 &rdev->constraints->state_mem);
634 return suspend_set_state(rdev,
635 &rdev->constraints->state_disk);
641 static void print_constraints(struct regulator_dev *rdev)
643 struct regulation_constraints *constraints = rdev->constraints;
647 if (rdev->desc->type == REGULATOR_VOLTAGE) {
648 if (constraints->min_uV == constraints->max_uV)
649 count = sprintf(buf, "%d mV ",
650 constraints->min_uV / 1000);
652 count = sprintf(buf, "%d <--> %d mV ",
653 constraints->min_uV / 1000,
654 constraints->max_uV / 1000);
656 if (constraints->min_uA == constraints->max_uA)
657 count = sprintf(buf, "%d mA ",
658 constraints->min_uA / 1000);
660 count = sprintf(buf, "%d <--> %d mA ",
661 constraints->min_uA / 1000,
662 constraints->max_uA / 1000);
664 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
665 count += sprintf(buf + count, "fast ");
666 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
667 count += sprintf(buf + count, "normal ");
668 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
669 count += sprintf(buf + count, "idle ");
670 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
671 count += sprintf(buf + count, "standby");
673 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
677 * set_machine_constraints - sets regulator constraints
678 * @regulator: regulator source
680 * Allows platform initialisation code to define and constrain
681 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
682 * Constraints *must* be set by platform code in order for some
683 * regulator operations to proceed i.e. set_voltage, set_current_limit,
686 static int set_machine_constraints(struct regulator_dev *rdev,
687 struct regulation_constraints *constraints)
691 struct regulator_ops *ops = rdev->desc->ops;
693 if (constraints->name)
694 name = constraints->name;
695 else if (rdev->desc->name)
696 name = rdev->desc->name;
700 rdev->constraints = constraints;
702 /* do we need to apply the constraint voltage */
703 if (rdev->constraints->apply_uV &&
704 rdev->constraints->min_uV == rdev->constraints->max_uV &&
706 ret = ops->set_voltage(rdev,
707 rdev->constraints->min_uV, rdev->constraints->max_uV);
709 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
711 rdev->constraints->min_uV, name);
712 rdev->constraints = NULL;
717 /* are we enabled at boot time by firmware / bootloader */
718 if (rdev->constraints->boot_on)
721 /* do we need to setup our suspend state */
722 if (constraints->initial_state) {
723 ret = suspend_prepare(rdev, constraints->initial_state);
725 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
727 rdev->constraints = NULL;
732 /* if always_on is set then turn the regulator on if it's not
734 if (constraints->always_on && ops->enable &&
735 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
736 (!ops->is_enabled && !constraints->boot_on))) {
737 ret = ops->enable(rdev);
739 printk(KERN_ERR "%s: failed to enable %s\n",
741 rdev->constraints = NULL;
746 print_constraints(rdev);
752 * set_supply - set regulator supply regulator
753 * @regulator: regulator name
754 * @supply: supply regulator name
756 * Called by platform initialisation code to set the supply regulator for this
757 * regulator. This ensures that a regulators supply will also be enabled by the
758 * core if it's child is enabled.
760 static int set_supply(struct regulator_dev *rdev,
761 struct regulator_dev *supply_rdev)
765 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
769 "%s: could not add device link %s err %d\n",
770 __func__, supply_rdev->dev.kobj.name, err);
773 rdev->supply = supply_rdev;
774 list_add(&rdev->slist, &supply_rdev->supply_list);
780 * set_consumer_device_supply: Bind a regulator to a symbolic supply
781 * @regulator: regulator source
782 * @dev: device the supply applies to
783 * @supply: symbolic name for supply
785 * Allows platform initialisation code to map physical regulator
786 * sources to symbolic names for supplies for use by devices. Devices
787 * should use these symbolic names to request regulators, avoiding the
788 * need to provide board-specific regulator names as platform data.
790 static int set_consumer_device_supply(struct regulator_dev *rdev,
791 struct device *consumer_dev, const char *supply)
793 struct regulator_map *node;
798 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
802 node->regulator = rdev;
803 node->dev = consumer_dev;
804 node->supply = supply;
806 list_add(&node->list, ®ulator_map_list);
810 static void unset_consumer_device_supply(struct regulator_dev *rdev,
811 struct device *consumer_dev)
813 struct regulator_map *node, *n;
815 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
816 if (rdev == node->regulator &&
817 consumer_dev == node->dev) {
818 list_del(&node->list);
825 #define REG_STR_SIZE 32
827 static struct regulator *create_regulator(struct regulator_dev *rdev,
829 const char *supply_name)
831 struct regulator *regulator;
832 char buf[REG_STR_SIZE];
835 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
836 if (regulator == NULL)
839 mutex_lock(&rdev->mutex);
840 regulator->rdev = rdev;
841 list_add(®ulator->list, &rdev->consumer_list);
844 /* create a 'requested_microamps_name' sysfs entry */
845 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
847 if (size >= REG_STR_SIZE)
850 regulator->dev = dev;
851 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
852 if (regulator->dev_attr.attr.name == NULL)
855 regulator->dev_attr.attr.owner = THIS_MODULE;
856 regulator->dev_attr.attr.mode = 0444;
857 regulator->dev_attr.show = device_requested_uA_show;
858 err = device_create_file(dev, ®ulator->dev_attr);
860 printk(KERN_WARNING "%s: could not add regulator_dev"
861 " load sysfs\n", __func__);
865 /* also add a link to the device sysfs entry */
866 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
867 dev->kobj.name, supply_name);
868 if (size >= REG_STR_SIZE)
871 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
872 if (regulator->supply_name == NULL)
875 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
879 "%s: could not add device link %s err %d\n",
880 __func__, dev->kobj.name, err);
881 device_remove_file(dev, ®ulator->dev_attr);
885 mutex_unlock(&rdev->mutex);
888 kfree(regulator->supply_name);
890 device_remove_file(regulator->dev, ®ulator->dev_attr);
892 kfree(regulator->dev_attr.attr.name);
894 list_del(®ulator->list);
896 mutex_unlock(&rdev->mutex);
901 * regulator_get - lookup and obtain a reference to a regulator.
902 * @dev: device for regulator "consumer"
903 * @id: Supply name or regulator ID.
905 * Returns a struct regulator corresponding to the regulator producer,
906 * or IS_ERR() condition containing errno. Use of supply names
907 * configured via regulator_set_device_supply() is strongly
910 struct regulator *regulator_get(struct device *dev, const char *id)
912 struct regulator_dev *rdev;
913 struct regulator_map *map;
914 struct regulator *regulator = ERR_PTR(-ENODEV);
917 printk(KERN_ERR "regulator: get() with no identifier\n");
921 mutex_lock(®ulator_list_mutex);
923 list_for_each_entry(map, ®ulator_map_list, list) {
924 if (dev == map->dev &&
925 strcmp(map->supply, id) == 0) {
926 rdev = map->regulator;
930 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
932 mutex_unlock(®ulator_list_mutex);
936 if (!try_module_get(rdev->owner))
939 regulator = create_regulator(rdev, dev, id);
940 if (regulator == NULL) {
941 regulator = ERR_PTR(-ENOMEM);
942 module_put(rdev->owner);
946 mutex_unlock(®ulator_list_mutex);
949 EXPORT_SYMBOL_GPL(regulator_get);
952 * regulator_put - "free" the regulator source
953 * @regulator: regulator source
955 * Note: drivers must ensure that all regulator_enable calls made on this
956 * regulator source are balanced by regulator_disable calls prior to calling
959 void regulator_put(struct regulator *regulator)
961 struct regulator_dev *rdev;
963 if (regulator == NULL || IS_ERR(regulator))
966 mutex_lock(®ulator_list_mutex);
967 rdev = regulator->rdev;
969 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
970 regulator->supply_name))
971 _regulator_disable(rdev);
973 /* remove any sysfs entries */
974 if (regulator->dev) {
975 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
976 kfree(regulator->supply_name);
977 device_remove_file(regulator->dev, ®ulator->dev_attr);
978 kfree(regulator->dev_attr.attr.name);
980 list_del(®ulator->list);
983 module_put(rdev->owner);
984 mutex_unlock(®ulator_list_mutex);
986 EXPORT_SYMBOL_GPL(regulator_put);
988 /* locks held by regulator_enable() */
989 static int _regulator_enable(struct regulator_dev *rdev)
993 if (!rdev->constraints) {
994 printk(KERN_ERR "%s: %s has no constraints\n",
995 __func__, rdev->desc->name);
999 /* do we need to enable the supply regulator first */
1001 ret = _regulator_enable(rdev->supply);
1003 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1004 __func__, rdev->desc->name, ret);
1009 /* check voltage and requested load before enabling */
1010 if (rdev->desc->ops->enable) {
1012 if (rdev->constraints &&
1013 (rdev->constraints->valid_ops_mask &
1014 REGULATOR_CHANGE_DRMS))
1015 drms_uA_update(rdev);
1017 ret = rdev->desc->ops->enable(rdev);
1019 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1020 __func__, rdev->desc->name, ret);
1031 * regulator_enable - enable regulator output
1032 * @regulator: regulator source
1034 * Enable the regulator output at the predefined voltage or current value.
1035 * NOTE: the output value can be set by other drivers, boot loader or may be
1036 * hardwired in the regulator.
1037 * NOTE: calls to regulator_enable() must be balanced with calls to
1038 * regulator_disable().
1040 int regulator_enable(struct regulator *regulator)
1042 struct regulator_dev *rdev = regulator->rdev;
1045 mutex_lock(&rdev->mutex);
1046 if (regulator->enabled == 0)
1047 ret = _regulator_enable(rdev);
1048 else if (regulator->enabled < 0)
1051 regulator->enabled++;
1052 mutex_unlock(&rdev->mutex);
1055 EXPORT_SYMBOL_GPL(regulator_enable);
1057 /* locks held by regulator_disable() */
1058 static int _regulator_disable(struct regulator_dev *rdev)
1062 /* are we the last user and permitted to disable ? */
1063 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1065 /* we are last user */
1066 if (rdev->desc->ops->disable) {
1067 ret = rdev->desc->ops->disable(rdev);
1069 printk(KERN_ERR "%s: failed to disable %s\n",
1070 __func__, rdev->desc->name);
1075 /* decrease our supplies ref count and disable if required */
1077 _regulator_disable(rdev->supply);
1079 rdev->use_count = 0;
1080 } else if (rdev->use_count > 1) {
1082 if (rdev->constraints &&
1083 (rdev->constraints->valid_ops_mask &
1084 REGULATOR_CHANGE_DRMS))
1085 drms_uA_update(rdev);
1093 * regulator_disable - disable regulator output
1094 * @regulator: regulator source
1096 * Disable the regulator output voltage or current.
1097 * NOTE: this will only disable the regulator output if no other consumer
1098 * devices have it enabled.
1099 * NOTE: calls to regulator_enable() must be balanced with calls to
1100 * regulator_disable().
1102 int regulator_disable(struct regulator *regulator)
1104 struct regulator_dev *rdev = regulator->rdev;
1107 mutex_lock(&rdev->mutex);
1108 if (regulator->enabled == 1) {
1109 ret = _regulator_disable(rdev);
1111 regulator->uA_load = 0;
1112 } else if (WARN(regulator->enabled <= 0,
1113 "unbalanced disables for supply %s\n",
1114 regulator->supply_name))
1117 regulator->enabled--;
1118 mutex_unlock(&rdev->mutex);
1121 EXPORT_SYMBOL_GPL(regulator_disable);
1123 /* locks held by regulator_force_disable() */
1124 static int _regulator_force_disable(struct regulator_dev *rdev)
1129 if (rdev->desc->ops->disable) {
1130 /* ah well, who wants to live forever... */
1131 ret = rdev->desc->ops->disable(rdev);
1133 printk(KERN_ERR "%s: failed to force disable %s\n",
1134 __func__, rdev->desc->name);
1137 /* notify other consumers that power has been forced off */
1138 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1142 /* decrease our supplies ref count and disable if required */
1144 _regulator_disable(rdev->supply);
1146 rdev->use_count = 0;
1151 * regulator_force_disable - force disable regulator output
1152 * @regulator: regulator source
1154 * Forcibly disable the regulator output voltage or current.
1155 * NOTE: this *will* disable the regulator output even if other consumer
1156 * devices have it enabled. This should be used for situations when device
1157 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1159 int regulator_force_disable(struct regulator *regulator)
1163 mutex_lock(®ulator->rdev->mutex);
1164 regulator->enabled = 0;
1165 regulator->uA_load = 0;
1166 ret = _regulator_force_disable(regulator->rdev);
1167 mutex_unlock(®ulator->rdev->mutex);
1170 EXPORT_SYMBOL_GPL(regulator_force_disable);
1172 static int _regulator_is_enabled(struct regulator_dev *rdev)
1176 mutex_lock(&rdev->mutex);
1179 if (!rdev->desc->ops->is_enabled) {
1184 ret = rdev->desc->ops->is_enabled(rdev);
1186 mutex_unlock(&rdev->mutex);
1191 * regulator_is_enabled - is the regulator output enabled
1192 * @regulator: regulator source
1194 * Returns positive if the regulator driver backing the source/client
1195 * has requested that the device be enabled, zero if it hasn't, else a
1196 * negative errno code.
1198 * Note that the device backing this regulator handle can have multiple
1199 * users, so it might be enabled even if regulator_enable() was never
1200 * called for this particular source.
1202 int regulator_is_enabled(struct regulator *regulator)
1204 return _regulator_is_enabled(regulator->rdev);
1206 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1209 * regulator_set_voltage - set regulator output voltage
1210 * @regulator: regulator source
1211 * @min_uV: Minimum required voltage in uV
1212 * @max_uV: Maximum acceptable voltage in uV
1214 * Sets a voltage regulator to the desired output voltage. This can be set
1215 * during any regulator state. IOW, regulator can be disabled or enabled.
1217 * If the regulator is enabled then the voltage will change to the new value
1218 * immediately otherwise if the regulator is disabled the regulator will
1219 * output at the new voltage when enabled.
1221 * NOTE: If the regulator is shared between several devices then the lowest
1222 * request voltage that meets the system constraints will be used.
1223 * NOTE: Regulator system constraints must be set for this regulator before
1224 * calling this function otherwise this call will fail.
1226 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1228 struct regulator_dev *rdev = regulator->rdev;
1231 mutex_lock(&rdev->mutex);
1234 if (!rdev->desc->ops->set_voltage) {
1239 /* constraints check */
1240 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1243 regulator->min_uV = min_uV;
1244 regulator->max_uV = max_uV;
1245 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1248 mutex_unlock(&rdev->mutex);
1251 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1253 static int _regulator_get_voltage(struct regulator_dev *rdev)
1256 if (rdev->desc->ops->get_voltage)
1257 return rdev->desc->ops->get_voltage(rdev);
1263 * regulator_get_voltage - get regulator output voltage
1264 * @regulator: regulator source
1266 * This returns the current regulator voltage in uV.
1268 * NOTE: If the regulator is disabled it will return the voltage value. This
1269 * function should not be used to determine regulator state.
1271 int regulator_get_voltage(struct regulator *regulator)
1275 mutex_lock(®ulator->rdev->mutex);
1277 ret = _regulator_get_voltage(regulator->rdev);
1279 mutex_unlock(®ulator->rdev->mutex);
1283 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1286 * regulator_set_current_limit - set regulator output current limit
1287 * @regulator: regulator source
1288 * @min_uA: Minimuum supported current in uA
1289 * @max_uA: Maximum supported current in uA
1291 * Sets current sink to the desired output current. This can be set during
1292 * any regulator state. IOW, regulator can be disabled or enabled.
1294 * If the regulator is enabled then the current will change to the new value
1295 * immediately otherwise if the regulator is disabled the regulator will
1296 * output at the new current when enabled.
1298 * NOTE: Regulator system constraints must be set for this regulator before
1299 * calling this function otherwise this call will fail.
1301 int regulator_set_current_limit(struct regulator *regulator,
1302 int min_uA, int max_uA)
1304 struct regulator_dev *rdev = regulator->rdev;
1307 mutex_lock(&rdev->mutex);
1310 if (!rdev->desc->ops->set_current_limit) {
1315 /* constraints check */
1316 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1320 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1322 mutex_unlock(&rdev->mutex);
1325 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1327 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1331 mutex_lock(&rdev->mutex);
1334 if (!rdev->desc->ops->get_current_limit) {
1339 ret = rdev->desc->ops->get_current_limit(rdev);
1341 mutex_unlock(&rdev->mutex);
1346 * regulator_get_current_limit - get regulator output current
1347 * @regulator: regulator source
1349 * This returns the current supplied by the specified current sink in uA.
1351 * NOTE: If the regulator is disabled it will return the current value. This
1352 * function should not be used to determine regulator state.
1354 int regulator_get_current_limit(struct regulator *regulator)
1356 return _regulator_get_current_limit(regulator->rdev);
1358 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1361 * regulator_set_mode - set regulator operating mode
1362 * @regulator: regulator source
1363 * @mode: operating mode - one of the REGULATOR_MODE constants
1365 * Set regulator operating mode to increase regulator efficiency or improve
1366 * regulation performance.
1368 * NOTE: Regulator system constraints must be set for this regulator before
1369 * calling this function otherwise this call will fail.
1371 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1373 struct regulator_dev *rdev = regulator->rdev;
1376 mutex_lock(&rdev->mutex);
1379 if (!rdev->desc->ops->set_mode) {
1384 /* constraints check */
1385 ret = regulator_check_mode(rdev, mode);
1389 ret = rdev->desc->ops->set_mode(rdev, mode);
1391 mutex_unlock(&rdev->mutex);
1394 EXPORT_SYMBOL_GPL(regulator_set_mode);
1396 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1400 mutex_lock(&rdev->mutex);
1403 if (!rdev->desc->ops->get_mode) {
1408 ret = rdev->desc->ops->get_mode(rdev);
1410 mutex_unlock(&rdev->mutex);
1415 * regulator_get_mode - get regulator operating mode
1416 * @regulator: regulator source
1418 * Get the current regulator operating mode.
1420 unsigned int regulator_get_mode(struct regulator *regulator)
1422 return _regulator_get_mode(regulator->rdev);
1424 EXPORT_SYMBOL_GPL(regulator_get_mode);
1427 * regulator_set_optimum_mode - set regulator optimum operating mode
1428 * @regulator: regulator source
1429 * @uA_load: load current
1431 * Notifies the regulator core of a new device load. This is then used by
1432 * DRMS (if enabled by constraints) to set the most efficient regulator
1433 * operating mode for the new regulator loading.
1435 * Consumer devices notify their supply regulator of the maximum power
1436 * they will require (can be taken from device datasheet in the power
1437 * consumption tables) when they change operational status and hence power
1438 * state. Examples of operational state changes that can affect power
1439 * consumption are :-
1441 * o Device is opened / closed.
1442 * o Device I/O is about to begin or has just finished.
1443 * o Device is idling in between work.
1445 * This information is also exported via sysfs to userspace.
1447 * DRMS will sum the total requested load on the regulator and change
1448 * to the most efficient operating mode if platform constraints allow.
1450 * Returns the new regulator mode or error.
1452 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1454 struct regulator_dev *rdev = regulator->rdev;
1455 struct regulator *consumer;
1456 int ret, output_uV, input_uV, total_uA_load = 0;
1459 mutex_lock(&rdev->mutex);
1461 regulator->uA_load = uA_load;
1462 ret = regulator_check_drms(rdev);
1468 if (!rdev->desc->ops->get_optimum_mode)
1471 /* get output voltage */
1472 output_uV = rdev->desc->ops->get_voltage(rdev);
1473 if (output_uV <= 0) {
1474 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1475 __func__, rdev->desc->name);
1479 /* get input voltage */
1480 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1481 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1483 input_uV = rdev->constraints->input_uV;
1484 if (input_uV <= 0) {
1485 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1486 __func__, rdev->desc->name);
1490 /* calc total requested load for this regulator */
1491 list_for_each_entry(consumer, &rdev->consumer_list, list)
1492 total_uA_load += consumer->uA_load;
1494 mode = rdev->desc->ops->get_optimum_mode(rdev,
1495 input_uV, output_uV,
1498 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1499 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1500 total_uA_load, input_uV, output_uV);
1504 ret = rdev->desc->ops->set_mode(rdev, mode);
1506 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1507 __func__, mode, rdev->desc->name);
1512 mutex_unlock(&rdev->mutex);
1515 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1518 * regulator_register_notifier - register regulator event notifier
1519 * @regulator: regulator source
1520 * @notifier_block: notifier block
1522 * Register notifier block to receive regulator events.
1524 int regulator_register_notifier(struct regulator *regulator,
1525 struct notifier_block *nb)
1527 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1530 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1533 * regulator_unregister_notifier - unregister regulator event notifier
1534 * @regulator: regulator source
1535 * @notifier_block: notifier block
1537 * Unregister regulator event notifier block.
1539 int regulator_unregister_notifier(struct regulator *regulator,
1540 struct notifier_block *nb)
1542 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1545 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1547 /* notify regulator consumers and downstream regulator consumers */
1548 static void _notifier_call_chain(struct regulator_dev *rdev,
1549 unsigned long event, void *data)
1551 struct regulator_dev *_rdev;
1553 /* call rdev chain first */
1554 mutex_lock(&rdev->mutex);
1555 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1556 mutex_unlock(&rdev->mutex);
1558 /* now notify regulator we supply */
1559 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1560 _notifier_call_chain(_rdev, event, data);
1564 * regulator_bulk_get - get multiple regulator consumers
1566 * @dev: Device to supply
1567 * @num_consumers: Number of consumers to register
1568 * @consumers: Configuration of consumers; clients are stored here.
1570 * @return 0 on success, an errno on failure.
1572 * This helper function allows drivers to get several regulator
1573 * consumers in one operation. If any of the regulators cannot be
1574 * acquired then any regulators that were allocated will be freed
1575 * before returning to the caller.
1577 int regulator_bulk_get(struct device *dev, int num_consumers,
1578 struct regulator_bulk_data *consumers)
1583 for (i = 0; i < num_consumers; i++)
1584 consumers[i].consumer = NULL;
1586 for (i = 0; i < num_consumers; i++) {
1587 consumers[i].consumer = regulator_get(dev,
1588 consumers[i].supply);
1589 if (IS_ERR(consumers[i].consumer)) {
1590 dev_err(dev, "Failed to get supply '%s'\n",
1591 consumers[i].supply);
1592 ret = PTR_ERR(consumers[i].consumer);
1593 consumers[i].consumer = NULL;
1601 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1602 regulator_put(consumers[i].consumer);
1606 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1609 * regulator_bulk_enable - enable multiple regulator consumers
1611 * @num_consumers: Number of consumers
1612 * @consumers: Consumer data; clients are stored here.
1613 * @return 0 on success, an errno on failure
1615 * This convenience API allows consumers to enable multiple regulator
1616 * clients in a single API call. If any consumers cannot be enabled
1617 * then any others that were enabled will be disabled again prior to
1620 int regulator_bulk_enable(int num_consumers,
1621 struct regulator_bulk_data *consumers)
1626 for (i = 0; i < num_consumers; i++) {
1627 ret = regulator_enable(consumers[i].consumer);
1635 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1636 for (i = 0; i < num_consumers; i++)
1637 regulator_disable(consumers[i].consumer);
1641 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1644 * regulator_bulk_disable - disable multiple regulator consumers
1646 * @num_consumers: Number of consumers
1647 * @consumers: Consumer data; clients are stored here.
1648 * @return 0 on success, an errno on failure
1650 * This convenience API allows consumers to disable multiple regulator
1651 * clients in a single API call. If any consumers cannot be enabled
1652 * then any others that were disabled will be disabled again prior to
1655 int regulator_bulk_disable(int num_consumers,
1656 struct regulator_bulk_data *consumers)
1661 for (i = 0; i < num_consumers; i++) {
1662 ret = regulator_disable(consumers[i].consumer);
1670 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1671 for (i = 0; i < num_consumers; i++)
1672 regulator_enable(consumers[i].consumer);
1676 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1679 * regulator_bulk_free - free multiple regulator consumers
1681 * @num_consumers: Number of consumers
1682 * @consumers: Consumer data; clients are stored here.
1684 * This convenience API allows consumers to free multiple regulator
1685 * clients in a single API call.
1687 void regulator_bulk_free(int num_consumers,
1688 struct regulator_bulk_data *consumers)
1692 for (i = 0; i < num_consumers; i++) {
1693 regulator_put(consumers[i].consumer);
1694 consumers[i].consumer = NULL;
1697 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1700 * regulator_notifier_call_chain - call regulator event notifier
1701 * @regulator: regulator source
1702 * @event: notifier block
1705 * Called by regulator drivers to notify clients a regulator event has
1706 * occurred. We also notify regulator clients downstream.
1708 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1709 unsigned long event, void *data)
1711 _notifier_call_chain(rdev, event, data);
1715 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1718 * regulator_register - register regulator
1719 * @regulator: regulator source
1720 * @reg_data: private regulator data
1722 * Called by regulator drivers to register a regulator.
1723 * Returns 0 on success.
1725 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1726 struct device *dev, void *driver_data)
1728 static atomic_t regulator_no = ATOMIC_INIT(0);
1729 struct regulator_dev *rdev;
1730 struct regulator_init_data *init_data = dev->platform_data;
1733 if (regulator_desc == NULL)
1734 return ERR_PTR(-EINVAL);
1736 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1737 return ERR_PTR(-EINVAL);
1739 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1740 !regulator_desc->type == REGULATOR_CURRENT)
1741 return ERR_PTR(-EINVAL);
1744 return ERR_PTR(-EINVAL);
1746 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1748 return ERR_PTR(-ENOMEM);
1750 mutex_lock(®ulator_list_mutex);
1752 mutex_init(&rdev->mutex);
1753 rdev->reg_data = driver_data;
1754 rdev->owner = regulator_desc->owner;
1755 rdev->desc = regulator_desc;
1756 INIT_LIST_HEAD(&rdev->consumer_list);
1757 INIT_LIST_HEAD(&rdev->supply_list);
1758 INIT_LIST_HEAD(&rdev->list);
1759 INIT_LIST_HEAD(&rdev->slist);
1760 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1762 /* preform any regulator specific init */
1763 if (init_data->regulator_init) {
1764 ret = init_data->regulator_init(rdev->reg_data);
1767 rdev = ERR_PTR(ret);
1772 /* set regulator constraints */
1773 ret = set_machine_constraints(rdev, &init_data->constraints);
1776 rdev = ERR_PTR(ret);
1780 /* register with sysfs */
1781 rdev->dev.class = ®ulator_class;
1782 rdev->dev.parent = dev;
1783 dev_set_name(&rdev->dev, "regulator.%d",
1784 atomic_inc_return(®ulator_no) - 1);
1785 ret = device_register(&rdev->dev);
1788 rdev = ERR_PTR(ret);
1792 dev_set_drvdata(&rdev->dev, rdev);
1794 /* set supply regulator if it exists */
1795 if (init_data->supply_regulator_dev) {
1796 ret = set_supply(rdev,
1797 dev_get_drvdata(init_data->supply_regulator_dev));
1799 device_unregister(&rdev->dev);
1801 rdev = ERR_PTR(ret);
1806 /* add consumers devices */
1807 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1808 ret = set_consumer_device_supply(rdev,
1809 init_data->consumer_supplies[i].dev,
1810 init_data->consumer_supplies[i].supply);
1812 for (--i; i >= 0; i--)
1813 unset_consumer_device_supply(rdev,
1814 init_data->consumer_supplies[i].dev);
1815 device_unregister(&rdev->dev);
1817 rdev = ERR_PTR(ret);
1822 list_add(&rdev->list, ®ulator_list);
1824 mutex_unlock(®ulator_list_mutex);
1827 EXPORT_SYMBOL_GPL(regulator_register);
1830 * regulator_unregister - unregister regulator
1831 * @regulator: regulator source
1833 * Called by regulator drivers to unregister a regulator.
1835 void regulator_unregister(struct regulator_dev *rdev)
1840 mutex_lock(®ulator_list_mutex);
1841 list_del(&rdev->list);
1843 sysfs_remove_link(&rdev->dev.kobj, "supply");
1844 device_unregister(&rdev->dev);
1845 mutex_unlock(®ulator_list_mutex);
1847 EXPORT_SYMBOL_GPL(regulator_unregister);
1850 * regulator_suspend_prepare: prepare regulators for system wide suspend
1851 * @state: system suspend state
1853 * Configure each regulator with it's suspend operating parameters for state.
1854 * This will usually be called by machine suspend code prior to supending.
1856 int regulator_suspend_prepare(suspend_state_t state)
1858 struct regulator_dev *rdev;
1861 /* ON is handled by regulator active state */
1862 if (state == PM_SUSPEND_ON)
1865 mutex_lock(®ulator_list_mutex);
1866 list_for_each_entry(rdev, ®ulator_list, list) {
1868 mutex_lock(&rdev->mutex);
1869 ret = suspend_prepare(rdev, state);
1870 mutex_unlock(&rdev->mutex);
1873 printk(KERN_ERR "%s: failed to prepare %s\n",
1874 __func__, rdev->desc->name);
1879 mutex_unlock(®ulator_list_mutex);
1882 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1885 * rdev_get_drvdata - get rdev regulator driver data
1886 * @regulator: regulator
1888 * Get rdev regulator driver private data. This call can be used in the
1889 * regulator driver context.
1891 void *rdev_get_drvdata(struct regulator_dev *rdev)
1893 return rdev->reg_data;
1895 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1898 * regulator_get_drvdata - get regulator driver data
1899 * @regulator: regulator
1901 * Get regulator driver private data. This call can be used in the consumer
1902 * driver context when non API regulator specific functions need to be called.
1904 void *regulator_get_drvdata(struct regulator *regulator)
1906 return regulator->rdev->reg_data;
1908 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1911 * regulator_set_drvdata - set regulator driver data
1912 * @regulator: regulator
1915 void regulator_set_drvdata(struct regulator *regulator, void *data)
1917 regulator->rdev->reg_data = data;
1919 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
1922 * regulator_get_id - get regulator ID
1923 * @regulator: regulator
1925 int rdev_get_id(struct regulator_dev *rdev)
1927 return rdev->desc->id;
1929 EXPORT_SYMBOL_GPL(rdev_get_id);
1931 struct device *rdev_get_dev(struct regulator_dev *rdev)
1935 EXPORT_SYMBOL_GPL(rdev_get_dev);
1937 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
1939 return reg_init_data->driver_data;
1941 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
1943 static int __init regulator_init(void)
1945 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
1946 return class_register(®ulator_class);
1949 /* init early to allow our consumers to complete system booting */
1950 core_initcall(regulator_init);