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)
178 case REGULATOR_MODE_FAST:
179 case REGULATOR_MODE_NORMAL:
180 case REGULATOR_MODE_IDLE:
181 case REGULATOR_MODE_STANDBY:
187 if (!rdev->constraints) {
188 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
192 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
193 printk(KERN_ERR "%s: operation not allowed for %s\n",
194 __func__, rdev->desc->name);
197 if (!(rdev->constraints->valid_modes_mask & mode)) {
198 printk(KERN_ERR "%s: invalid mode %x for %s\n",
199 __func__, mode, rdev->desc->name);
205 /* dynamic regulator mode switching constraint check */
206 static int regulator_check_drms(struct regulator_dev *rdev)
208 if (!rdev->constraints) {
209 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
213 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
214 printk(KERN_ERR "%s: operation not allowed for %s\n",
215 __func__, rdev->desc->name);
221 static ssize_t device_requested_uA_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
224 struct regulator *regulator;
226 regulator = get_device_regulator(dev);
227 if (regulator == NULL)
230 return sprintf(buf, "%d\n", regulator->uA_load);
233 static ssize_t regulator_uV_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
236 struct regulator_dev *rdev = dev_get_drvdata(dev);
239 mutex_lock(&rdev->mutex);
240 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
241 mutex_unlock(&rdev->mutex);
246 static ssize_t regulator_uA_show(struct device *dev,
247 struct device_attribute *attr, char *buf)
249 struct regulator_dev *rdev = dev_get_drvdata(dev);
251 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
254 static ssize_t regulator_name_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
257 struct regulator_dev *rdev = dev_get_drvdata(dev);
260 if (rdev->constraints->name)
261 name = rdev->constraints->name;
262 else if (rdev->desc->name)
263 name = rdev->desc->name;
267 return sprintf(buf, "%s\n", name);
270 static ssize_t regulator_print_opmode(char *buf, int mode)
273 case REGULATOR_MODE_FAST:
274 return sprintf(buf, "fast\n");
275 case REGULATOR_MODE_NORMAL:
276 return sprintf(buf, "normal\n");
277 case REGULATOR_MODE_IDLE:
278 return sprintf(buf, "idle\n");
279 case REGULATOR_MODE_STANDBY:
280 return sprintf(buf, "standby\n");
282 return sprintf(buf, "unknown\n");
285 static ssize_t regulator_opmode_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct regulator_dev *rdev = dev_get_drvdata(dev);
290 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
293 static ssize_t regulator_print_state(char *buf, int state)
296 return sprintf(buf, "enabled\n");
298 return sprintf(buf, "disabled\n");
300 return sprintf(buf, "unknown\n");
303 static ssize_t regulator_state_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
306 struct regulator_dev *rdev = dev_get_drvdata(dev);
308 return regulator_print_state(buf, _regulator_is_enabled(rdev));
311 static ssize_t regulator_min_uA_show(struct device *dev,
312 struct device_attribute *attr, char *buf)
314 struct regulator_dev *rdev = dev_get_drvdata(dev);
316 if (!rdev->constraints)
317 return sprintf(buf, "constraint not defined\n");
319 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
322 static ssize_t regulator_max_uA_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 struct regulator_dev *rdev = dev_get_drvdata(dev);
327 if (!rdev->constraints)
328 return sprintf(buf, "constraint not defined\n");
330 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
333 static ssize_t regulator_min_uV_show(struct device *dev,
334 struct device_attribute *attr, char *buf)
336 struct regulator_dev *rdev = dev_get_drvdata(dev);
338 if (!rdev->constraints)
339 return sprintf(buf, "constraint not defined\n");
341 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
344 static ssize_t regulator_max_uV_show(struct device *dev,
345 struct device_attribute *attr, char *buf)
347 struct regulator_dev *rdev = dev_get_drvdata(dev);
349 if (!rdev->constraints)
350 return sprintf(buf, "constraint not defined\n");
352 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
355 static ssize_t regulator_total_uA_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
358 struct regulator_dev *rdev = dev_get_drvdata(dev);
359 struct regulator *regulator;
362 mutex_lock(&rdev->mutex);
363 list_for_each_entry(regulator, &rdev->consumer_list, list)
364 uA += regulator->uA_load;
365 mutex_unlock(&rdev->mutex);
366 return sprintf(buf, "%d\n", uA);
369 static ssize_t regulator_num_users_show(struct device *dev,
370 struct device_attribute *attr, char *buf)
372 struct regulator_dev *rdev = dev_get_drvdata(dev);
373 return sprintf(buf, "%d\n", rdev->use_count);
376 static ssize_t regulator_type_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
379 struct regulator_dev *rdev = dev_get_drvdata(dev);
381 switch (rdev->desc->type) {
382 case REGULATOR_VOLTAGE:
383 return sprintf(buf, "voltage\n");
384 case REGULATOR_CURRENT:
385 return sprintf(buf, "current\n");
387 return sprintf(buf, "unknown\n");
390 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
393 struct regulator_dev *rdev = dev_get_drvdata(dev);
395 if (!rdev->constraints)
396 return sprintf(buf, "not defined\n");
397 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
400 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
401 struct device_attribute *attr, char *buf)
403 struct regulator_dev *rdev = dev_get_drvdata(dev);
405 if (!rdev->constraints)
406 return sprintf(buf, "not defined\n");
407 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
410 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
413 struct regulator_dev *rdev = dev_get_drvdata(dev);
415 if (!rdev->constraints)
416 return sprintf(buf, "not defined\n");
417 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
420 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 struct regulator_dev *rdev = dev_get_drvdata(dev);
425 if (!rdev->constraints)
426 return sprintf(buf, "not defined\n");
427 return regulator_print_opmode(buf,
428 rdev->constraints->state_mem.mode);
431 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
434 struct regulator_dev *rdev = dev_get_drvdata(dev);
436 if (!rdev->constraints)
437 return sprintf(buf, "not defined\n");
438 return regulator_print_opmode(buf,
439 rdev->constraints->state_disk.mode);
442 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
445 struct regulator_dev *rdev = dev_get_drvdata(dev);
447 if (!rdev->constraints)
448 return sprintf(buf, "not defined\n");
449 return regulator_print_opmode(buf,
450 rdev->constraints->state_standby.mode);
453 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
456 struct regulator_dev *rdev = dev_get_drvdata(dev);
458 if (!rdev->constraints)
459 return sprintf(buf, "not defined\n");
461 return regulator_print_state(buf,
462 rdev->constraints->state_mem.enabled);
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 return regulator_print_state(buf,
474 rdev->constraints->state_disk.enabled);
477 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
480 struct regulator_dev *rdev = dev_get_drvdata(dev);
482 if (!rdev->constraints)
483 return sprintf(buf, "not defined\n");
485 return regulator_print_state(buf,
486 rdev->constraints->state_standby.enabled);
489 static struct device_attribute regulator_dev_attrs[] = {
490 __ATTR(name, 0444, regulator_name_show, NULL),
491 __ATTR(microvolts, 0444, regulator_uV_show, NULL),
492 __ATTR(microamps, 0444, regulator_uA_show, NULL),
493 __ATTR(opmode, 0444, regulator_opmode_show, NULL),
494 __ATTR(state, 0444, regulator_state_show, NULL),
495 __ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL),
496 __ATTR(min_microamps, 0444, regulator_min_uA_show, NULL),
497 __ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL),
498 __ATTR(max_microamps, 0444, regulator_max_uA_show, NULL),
499 __ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL),
500 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
501 __ATTR(type, 0444, regulator_type_show, NULL),
502 __ATTR(suspend_mem_microvolts, 0444,
503 regulator_suspend_mem_uV_show, NULL),
504 __ATTR(suspend_disk_microvolts, 0444,
505 regulator_suspend_disk_uV_show, NULL),
506 __ATTR(suspend_standby_microvolts, 0444,
507 regulator_suspend_standby_uV_show, NULL),
508 __ATTR(suspend_mem_mode, 0444,
509 regulator_suspend_mem_mode_show, NULL),
510 __ATTR(suspend_disk_mode, 0444,
511 regulator_suspend_disk_mode_show, NULL),
512 __ATTR(suspend_standby_mode, 0444,
513 regulator_suspend_standby_mode_show, NULL),
514 __ATTR(suspend_mem_state, 0444,
515 regulator_suspend_mem_state_show, NULL),
516 __ATTR(suspend_disk_state, 0444,
517 regulator_suspend_disk_state_show, NULL),
518 __ATTR(suspend_standby_state, 0444,
519 regulator_suspend_standby_state_show, NULL),
523 static void regulator_dev_release(struct device *dev)
525 struct regulator_dev *rdev = dev_get_drvdata(dev);
529 static struct class regulator_class = {
531 .dev_release = regulator_dev_release,
532 .dev_attrs = regulator_dev_attrs,
535 /* Calculate the new optimum regulator operating mode based on the new total
536 * consumer load. All locks held by caller */
537 static void drms_uA_update(struct regulator_dev *rdev)
539 struct regulator *sibling;
540 int current_uA = 0, output_uV, input_uV, err;
543 err = regulator_check_drms(rdev);
544 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
545 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
548 /* get output voltage */
549 output_uV = rdev->desc->ops->get_voltage(rdev);
553 /* get input voltage */
554 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
555 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
557 input_uV = rdev->constraints->input_uV;
561 /* calc total requested load */
562 list_for_each_entry(sibling, &rdev->consumer_list, list)
563 current_uA += sibling->uA_load;
565 /* now get the optimum mode for our new total regulator load */
566 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
567 output_uV, current_uA);
569 /* check the new mode is allowed */
570 err = regulator_check_mode(rdev, mode);
572 rdev->desc->ops->set_mode(rdev, mode);
575 static int suspend_set_state(struct regulator_dev *rdev,
576 struct regulator_state *rstate)
580 /* enable & disable are mandatory for suspend control */
581 if (!rdev->desc->ops->set_suspend_enable ||
582 !rdev->desc->ops->set_suspend_disable) {
583 printk(KERN_ERR "%s: no way to set suspend state\n",
589 ret = rdev->desc->ops->set_suspend_enable(rdev);
591 ret = rdev->desc->ops->set_suspend_disable(rdev);
593 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
597 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
598 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
600 printk(KERN_ERR "%s: failed to set voltage\n",
606 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
607 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
609 printk(KERN_ERR "%s: failed to set mode\n", __func__);
616 /* locks held by caller */
617 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
619 if (!rdev->constraints)
623 case PM_SUSPEND_STANDBY:
624 return suspend_set_state(rdev,
625 &rdev->constraints->state_standby);
627 return suspend_set_state(rdev,
628 &rdev->constraints->state_mem);
630 return suspend_set_state(rdev,
631 &rdev->constraints->state_disk);
637 static void print_constraints(struct regulator_dev *rdev)
639 struct regulation_constraints *constraints = rdev->constraints;
643 if (rdev->desc->type == REGULATOR_VOLTAGE) {
644 if (constraints->min_uV == constraints->max_uV)
645 count = sprintf(buf, "%d mV ",
646 constraints->min_uV / 1000);
648 count = sprintf(buf, "%d <--> %d mV ",
649 constraints->min_uV / 1000,
650 constraints->max_uV / 1000);
652 if (constraints->min_uA == constraints->max_uA)
653 count = sprintf(buf, "%d mA ",
654 constraints->min_uA / 1000);
656 count = sprintf(buf, "%d <--> %d mA ",
657 constraints->min_uA / 1000,
658 constraints->max_uA / 1000);
660 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
661 count += sprintf(buf + count, "fast ");
662 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
663 count += sprintf(buf + count, "normal ");
664 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
665 count += sprintf(buf + count, "idle ");
666 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
667 count += sprintf(buf + count, "standby");
669 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
673 * set_machine_constraints - sets regulator constraints
674 * @regulator: regulator source
676 * Allows platform initialisation code to define and constrain
677 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
678 * Constraints *must* be set by platform code in order for some
679 * regulator operations to proceed i.e. set_voltage, set_current_limit,
682 static int set_machine_constraints(struct regulator_dev *rdev,
683 struct regulation_constraints *constraints)
687 struct regulator_ops *ops = rdev->desc->ops;
689 if (constraints->name)
690 name = constraints->name;
691 else if (rdev->desc->name)
692 name = rdev->desc->name;
696 rdev->constraints = constraints;
698 /* do we need to apply the constraint voltage */
699 if (rdev->constraints->apply_uV &&
700 rdev->constraints->min_uV == rdev->constraints->max_uV &&
702 ret = ops->set_voltage(rdev,
703 rdev->constraints->min_uV, rdev->constraints->max_uV);
705 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
707 rdev->constraints->min_uV, name);
708 rdev->constraints = NULL;
713 /* are we enabled at boot time by firmware / bootloader */
714 if (rdev->constraints->boot_on)
717 /* do we need to setup our suspend state */
718 if (constraints->initial_state) {
719 ret = suspend_prepare(rdev, constraints->initial_state);
721 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
723 rdev->constraints = NULL;
728 /* if always_on is set then turn the regulator on if it's not
730 if (constraints->always_on && ops->enable &&
731 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
732 (!ops->is_enabled && !constraints->boot_on))) {
733 ret = ops->enable(rdev);
735 printk(KERN_ERR "%s: failed to enable %s\n",
737 rdev->constraints = NULL;
742 print_constraints(rdev);
748 * set_supply - set regulator supply regulator
749 * @regulator: regulator name
750 * @supply: supply regulator name
752 * Called by platform initialisation code to set the supply regulator for this
753 * regulator. This ensures that a regulators supply will also be enabled by the
754 * core if it's child is enabled.
756 static int set_supply(struct regulator_dev *rdev,
757 struct regulator_dev *supply_rdev)
761 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
765 "%s: could not add device link %s err %d\n",
766 __func__, supply_rdev->dev.kobj.name, err);
769 rdev->supply = supply_rdev;
770 list_add(&rdev->slist, &supply_rdev->supply_list);
776 * set_consumer_device_supply: Bind a regulator to a symbolic supply
777 * @regulator: regulator source
778 * @dev: device the supply applies to
779 * @supply: symbolic name for supply
781 * Allows platform initialisation code to map physical regulator
782 * sources to symbolic names for supplies for use by devices. Devices
783 * should use these symbolic names to request regulators, avoiding the
784 * need to provide board-specific regulator names as platform data.
786 static int set_consumer_device_supply(struct regulator_dev *rdev,
787 struct device *consumer_dev, const char *supply)
789 struct regulator_map *node;
794 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
798 node->regulator = rdev;
799 node->dev = consumer_dev;
800 node->supply = supply;
802 list_add(&node->list, ®ulator_map_list);
806 static void unset_consumer_device_supply(struct regulator_dev *rdev,
807 struct device *consumer_dev)
809 struct regulator_map *node, *n;
811 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
812 if (rdev == node->regulator &&
813 consumer_dev == node->dev) {
814 list_del(&node->list);
821 #define REG_STR_SIZE 32
823 static struct regulator *create_regulator(struct regulator_dev *rdev,
825 const char *supply_name)
827 struct regulator *regulator;
828 char buf[REG_STR_SIZE];
831 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
832 if (regulator == NULL)
835 mutex_lock(&rdev->mutex);
836 regulator->rdev = rdev;
837 list_add(®ulator->list, &rdev->consumer_list);
840 /* create a 'requested_microamps_name' sysfs entry */
841 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
843 if (size >= REG_STR_SIZE)
846 regulator->dev = dev;
847 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
848 if (regulator->dev_attr.attr.name == NULL)
851 regulator->dev_attr.attr.owner = THIS_MODULE;
852 regulator->dev_attr.attr.mode = 0444;
853 regulator->dev_attr.show = device_requested_uA_show;
854 err = device_create_file(dev, ®ulator->dev_attr);
856 printk(KERN_WARNING "%s: could not add regulator_dev"
857 " load sysfs\n", __func__);
861 /* also add a link to the device sysfs entry */
862 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
863 dev->kobj.name, supply_name);
864 if (size >= REG_STR_SIZE)
867 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
868 if (regulator->supply_name == NULL)
871 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
875 "%s: could not add device link %s err %d\n",
876 __func__, dev->kobj.name, err);
877 device_remove_file(dev, ®ulator->dev_attr);
881 mutex_unlock(&rdev->mutex);
884 kfree(regulator->supply_name);
886 device_remove_file(regulator->dev, ®ulator->dev_attr);
888 kfree(regulator->dev_attr.attr.name);
890 list_del(®ulator->list);
892 mutex_unlock(&rdev->mutex);
897 * regulator_get - lookup and obtain a reference to a regulator.
898 * @dev: device for regulator "consumer"
899 * @id: Supply name or regulator ID.
901 * Returns a struct regulator corresponding to the regulator producer,
902 * or IS_ERR() condition containing errno. Use of supply names
903 * configured via regulator_set_device_supply() is strongly
906 struct regulator *regulator_get(struct device *dev, const char *id)
908 struct regulator_dev *rdev;
909 struct regulator_map *map;
910 struct regulator *regulator = ERR_PTR(-ENODEV);
913 printk(KERN_ERR "regulator: get() with no identifier\n");
917 mutex_lock(®ulator_list_mutex);
919 list_for_each_entry(map, ®ulator_map_list, list) {
920 if (dev == map->dev &&
921 strcmp(map->supply, id) == 0) {
922 rdev = map->regulator;
926 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
928 mutex_unlock(®ulator_list_mutex);
932 if (!try_module_get(rdev->owner))
935 regulator = create_regulator(rdev, dev, id);
936 if (regulator == NULL) {
937 regulator = ERR_PTR(-ENOMEM);
938 module_put(rdev->owner);
942 mutex_unlock(®ulator_list_mutex);
945 EXPORT_SYMBOL_GPL(regulator_get);
948 * regulator_put - "free" the regulator source
949 * @regulator: regulator source
951 * Note: drivers must ensure that all regulator_enable calls made on this
952 * regulator source are balanced by regulator_disable calls prior to calling
955 void regulator_put(struct regulator *regulator)
957 struct regulator_dev *rdev;
959 if (regulator == NULL || IS_ERR(regulator))
962 mutex_lock(®ulator_list_mutex);
963 rdev = regulator->rdev;
965 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
966 regulator->supply_name))
967 _regulator_disable(rdev);
969 /* remove any sysfs entries */
970 if (regulator->dev) {
971 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
972 kfree(regulator->supply_name);
973 device_remove_file(regulator->dev, ®ulator->dev_attr);
974 kfree(regulator->dev_attr.attr.name);
976 list_del(®ulator->list);
979 module_put(rdev->owner);
980 mutex_unlock(®ulator_list_mutex);
982 EXPORT_SYMBOL_GPL(regulator_put);
984 /* locks held by regulator_enable() */
985 static int _regulator_enable(struct regulator_dev *rdev)
989 if (!rdev->constraints) {
990 printk(KERN_ERR "%s: %s has no constraints\n",
991 __func__, rdev->desc->name);
995 /* do we need to enable the supply regulator first */
997 ret = _regulator_enable(rdev->supply);
999 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1000 __func__, rdev->desc->name, ret);
1005 /* check voltage and requested load before enabling */
1006 if (rdev->desc->ops->enable) {
1008 if (rdev->constraints &&
1009 (rdev->constraints->valid_ops_mask &
1010 REGULATOR_CHANGE_DRMS))
1011 drms_uA_update(rdev);
1013 ret = rdev->desc->ops->enable(rdev);
1015 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1016 __func__, rdev->desc->name, ret);
1027 * regulator_enable - enable regulator output
1028 * @regulator: regulator source
1030 * Enable the regulator output at the predefined voltage or current value.
1031 * NOTE: the output value can be set by other drivers, boot loader or may be
1032 * hardwired in the regulator.
1033 * NOTE: calls to regulator_enable() must be balanced with calls to
1034 * regulator_disable().
1036 int regulator_enable(struct regulator *regulator)
1038 struct regulator_dev *rdev = regulator->rdev;
1041 mutex_lock(&rdev->mutex);
1042 if (regulator->enabled == 0)
1043 ret = _regulator_enable(rdev);
1044 else if (regulator->enabled < 0)
1047 regulator->enabled++;
1048 mutex_unlock(&rdev->mutex);
1051 EXPORT_SYMBOL_GPL(regulator_enable);
1053 /* locks held by regulator_disable() */
1054 static int _regulator_disable(struct regulator_dev *rdev)
1058 /* are we the last user and permitted to disable ? */
1059 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1061 /* we are last user */
1062 if (rdev->desc->ops->disable) {
1063 ret = rdev->desc->ops->disable(rdev);
1065 printk(KERN_ERR "%s: failed to disable %s\n",
1066 __func__, rdev->desc->name);
1071 /* decrease our supplies ref count and disable if required */
1073 _regulator_disable(rdev->supply);
1075 rdev->use_count = 0;
1076 } else if (rdev->use_count > 1) {
1078 if (rdev->constraints &&
1079 (rdev->constraints->valid_ops_mask &
1080 REGULATOR_CHANGE_DRMS))
1081 drms_uA_update(rdev);
1089 * regulator_disable - disable regulator output
1090 * @regulator: regulator source
1092 * Disable the regulator output voltage or current.
1093 * NOTE: this will only disable the regulator output if no other consumer
1094 * devices have it enabled.
1095 * NOTE: calls to regulator_enable() must be balanced with calls to
1096 * regulator_disable().
1098 int regulator_disable(struct regulator *regulator)
1100 struct regulator_dev *rdev = regulator->rdev;
1103 mutex_lock(&rdev->mutex);
1104 if (regulator->enabled == 1) {
1105 ret = _regulator_disable(rdev);
1107 regulator->uA_load = 0;
1108 } else if (WARN(regulator->enabled <= 0,
1109 "unbalanced disables for supply %s\n",
1110 regulator->supply_name))
1113 regulator->enabled--;
1114 mutex_unlock(&rdev->mutex);
1117 EXPORT_SYMBOL_GPL(regulator_disable);
1119 /* locks held by regulator_force_disable() */
1120 static int _regulator_force_disable(struct regulator_dev *rdev)
1125 if (rdev->desc->ops->disable) {
1126 /* ah well, who wants to live forever... */
1127 ret = rdev->desc->ops->disable(rdev);
1129 printk(KERN_ERR "%s: failed to force disable %s\n",
1130 __func__, rdev->desc->name);
1133 /* notify other consumers that power has been forced off */
1134 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1138 /* decrease our supplies ref count and disable if required */
1140 _regulator_disable(rdev->supply);
1142 rdev->use_count = 0;
1147 * regulator_force_disable - force disable regulator output
1148 * @regulator: regulator source
1150 * Forcibly disable the regulator output voltage or current.
1151 * NOTE: this *will* disable the regulator output even if other consumer
1152 * devices have it enabled. This should be used for situations when device
1153 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1155 int regulator_force_disable(struct regulator *regulator)
1159 mutex_lock(®ulator->rdev->mutex);
1160 regulator->enabled = 0;
1161 regulator->uA_load = 0;
1162 ret = _regulator_force_disable(regulator->rdev);
1163 mutex_unlock(®ulator->rdev->mutex);
1166 EXPORT_SYMBOL_GPL(regulator_force_disable);
1168 static int _regulator_is_enabled(struct regulator_dev *rdev)
1172 mutex_lock(&rdev->mutex);
1175 if (!rdev->desc->ops->is_enabled) {
1180 ret = rdev->desc->ops->is_enabled(rdev);
1182 mutex_unlock(&rdev->mutex);
1187 * regulator_is_enabled - is the regulator output enabled
1188 * @regulator: regulator source
1190 * Returns positive if the regulator driver backing the source/client
1191 * has requested that the device be enabled, zero if it hasn't, else a
1192 * negative errno code.
1194 * Note that the device backing this regulator handle can have multiple
1195 * users, so it might be enabled even if regulator_enable() was never
1196 * called for this particular source.
1198 int regulator_is_enabled(struct regulator *regulator)
1200 return _regulator_is_enabled(regulator->rdev);
1202 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1205 * regulator_set_voltage - set regulator output voltage
1206 * @regulator: regulator source
1207 * @min_uV: Minimum required voltage in uV
1208 * @max_uV: Maximum acceptable voltage in uV
1210 * Sets a voltage regulator to the desired output voltage. This can be set
1211 * during any regulator state. IOW, regulator can be disabled or enabled.
1213 * If the regulator is enabled then the voltage will change to the new value
1214 * immediately otherwise if the regulator is disabled the regulator will
1215 * output at the new voltage when enabled.
1217 * NOTE: If the regulator is shared between several devices then the lowest
1218 * request voltage that meets the system constraints will be used.
1219 * NOTE: Regulator system constraints must be set for this regulator before
1220 * calling this function otherwise this call will fail.
1222 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1224 struct regulator_dev *rdev = regulator->rdev;
1227 mutex_lock(&rdev->mutex);
1230 if (!rdev->desc->ops->set_voltage) {
1235 /* constraints check */
1236 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1239 regulator->min_uV = min_uV;
1240 regulator->max_uV = max_uV;
1241 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1244 mutex_unlock(&rdev->mutex);
1247 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1249 static int _regulator_get_voltage(struct regulator_dev *rdev)
1252 if (rdev->desc->ops->get_voltage)
1253 return rdev->desc->ops->get_voltage(rdev);
1259 * regulator_get_voltage - get regulator output voltage
1260 * @regulator: regulator source
1262 * This returns the current regulator voltage in uV.
1264 * NOTE: If the regulator is disabled it will return the voltage value. This
1265 * function should not be used to determine regulator state.
1267 int regulator_get_voltage(struct regulator *regulator)
1271 mutex_lock(®ulator->rdev->mutex);
1273 ret = _regulator_get_voltage(regulator->rdev);
1275 mutex_unlock(®ulator->rdev->mutex);
1279 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1282 * regulator_set_current_limit - set regulator output current limit
1283 * @regulator: regulator source
1284 * @min_uA: Minimuum supported current in uA
1285 * @max_uA: Maximum supported current in uA
1287 * Sets current sink to the desired output current. This can be set during
1288 * any regulator state. IOW, regulator can be disabled or enabled.
1290 * If the regulator is enabled then the current will change to the new value
1291 * immediately otherwise if the regulator is disabled the regulator will
1292 * output at the new current when enabled.
1294 * NOTE: Regulator system constraints must be set for this regulator before
1295 * calling this function otherwise this call will fail.
1297 int regulator_set_current_limit(struct regulator *regulator,
1298 int min_uA, int max_uA)
1300 struct regulator_dev *rdev = regulator->rdev;
1303 mutex_lock(&rdev->mutex);
1306 if (!rdev->desc->ops->set_current_limit) {
1311 /* constraints check */
1312 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1316 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1318 mutex_unlock(&rdev->mutex);
1321 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1323 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1327 mutex_lock(&rdev->mutex);
1330 if (!rdev->desc->ops->get_current_limit) {
1335 ret = rdev->desc->ops->get_current_limit(rdev);
1337 mutex_unlock(&rdev->mutex);
1342 * regulator_get_current_limit - get regulator output current
1343 * @regulator: regulator source
1345 * This returns the current supplied by the specified current sink in uA.
1347 * NOTE: If the regulator is disabled it will return the current value. This
1348 * function should not be used to determine regulator state.
1350 int regulator_get_current_limit(struct regulator *regulator)
1352 return _regulator_get_current_limit(regulator->rdev);
1354 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1357 * regulator_set_mode - set regulator operating mode
1358 * @regulator: regulator source
1359 * @mode: operating mode - one of the REGULATOR_MODE constants
1361 * Set regulator operating mode to increase regulator efficiency or improve
1362 * regulation performance.
1364 * NOTE: Regulator system constraints must be set for this regulator before
1365 * calling this function otherwise this call will fail.
1367 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1369 struct regulator_dev *rdev = regulator->rdev;
1372 mutex_lock(&rdev->mutex);
1375 if (!rdev->desc->ops->set_mode) {
1380 /* constraints check */
1381 ret = regulator_check_mode(rdev, mode);
1385 ret = rdev->desc->ops->set_mode(rdev, mode);
1387 mutex_unlock(&rdev->mutex);
1390 EXPORT_SYMBOL_GPL(regulator_set_mode);
1392 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1396 mutex_lock(&rdev->mutex);
1399 if (!rdev->desc->ops->get_mode) {
1404 ret = rdev->desc->ops->get_mode(rdev);
1406 mutex_unlock(&rdev->mutex);
1411 * regulator_get_mode - get regulator operating mode
1412 * @regulator: regulator source
1414 * Get the current regulator operating mode.
1416 unsigned int regulator_get_mode(struct regulator *regulator)
1418 return _regulator_get_mode(regulator->rdev);
1420 EXPORT_SYMBOL_GPL(regulator_get_mode);
1423 * regulator_set_optimum_mode - set regulator optimum operating mode
1424 * @regulator: regulator source
1425 * @uA_load: load current
1427 * Notifies the regulator core of a new device load. This is then used by
1428 * DRMS (if enabled by constraints) to set the most efficient regulator
1429 * operating mode for the new regulator loading.
1431 * Consumer devices notify their supply regulator of the maximum power
1432 * they will require (can be taken from device datasheet in the power
1433 * consumption tables) when they change operational status and hence power
1434 * state. Examples of operational state changes that can affect power
1435 * consumption are :-
1437 * o Device is opened / closed.
1438 * o Device I/O is about to begin or has just finished.
1439 * o Device is idling in between work.
1441 * This information is also exported via sysfs to userspace.
1443 * DRMS will sum the total requested load on the regulator and change
1444 * to the most efficient operating mode if platform constraints allow.
1446 * Returns the new regulator mode or error.
1448 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1450 struct regulator_dev *rdev = regulator->rdev;
1451 struct regulator *consumer;
1452 int ret, output_uV, input_uV, total_uA_load = 0;
1455 mutex_lock(&rdev->mutex);
1457 regulator->uA_load = uA_load;
1458 ret = regulator_check_drms(rdev);
1464 if (!rdev->desc->ops->get_optimum_mode)
1467 /* get output voltage */
1468 output_uV = rdev->desc->ops->get_voltage(rdev);
1469 if (output_uV <= 0) {
1470 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1471 __func__, rdev->desc->name);
1475 /* get input voltage */
1476 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1477 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1479 input_uV = rdev->constraints->input_uV;
1480 if (input_uV <= 0) {
1481 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1482 __func__, rdev->desc->name);
1486 /* calc total requested load for this regulator */
1487 list_for_each_entry(consumer, &rdev->consumer_list, list)
1488 total_uA_load += consumer->uA_load;
1490 mode = rdev->desc->ops->get_optimum_mode(rdev,
1491 input_uV, output_uV,
1493 ret = regulator_check_mode(rdev, mode);
1495 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1496 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1497 total_uA_load, input_uV, output_uV);
1501 ret = rdev->desc->ops->set_mode(rdev, mode);
1503 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1504 __func__, mode, rdev->desc->name);
1509 mutex_unlock(&rdev->mutex);
1512 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1515 * regulator_register_notifier - register regulator event notifier
1516 * @regulator: regulator source
1517 * @notifier_block: notifier block
1519 * Register notifier block to receive regulator events.
1521 int regulator_register_notifier(struct regulator *regulator,
1522 struct notifier_block *nb)
1524 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1527 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1530 * regulator_unregister_notifier - unregister regulator event notifier
1531 * @regulator: regulator source
1532 * @notifier_block: notifier block
1534 * Unregister regulator event notifier block.
1536 int regulator_unregister_notifier(struct regulator *regulator,
1537 struct notifier_block *nb)
1539 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1542 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1544 /* notify regulator consumers and downstream regulator consumers */
1545 static void _notifier_call_chain(struct regulator_dev *rdev,
1546 unsigned long event, void *data)
1548 struct regulator_dev *_rdev;
1550 /* call rdev chain first */
1551 mutex_lock(&rdev->mutex);
1552 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1553 mutex_unlock(&rdev->mutex);
1555 /* now notify regulator we supply */
1556 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1557 _notifier_call_chain(_rdev, event, data);
1561 * regulator_bulk_get - get multiple regulator consumers
1563 * @dev: Device to supply
1564 * @num_consumers: Number of consumers to register
1565 * @consumers: Configuration of consumers; clients are stored here.
1567 * @return 0 on success, an errno on failure.
1569 * This helper function allows drivers to get several regulator
1570 * consumers in one operation. If any of the regulators cannot be
1571 * acquired then any regulators that were allocated will be freed
1572 * before returning to the caller.
1574 int regulator_bulk_get(struct device *dev, int num_consumers,
1575 struct regulator_bulk_data *consumers)
1580 for (i = 0; i < num_consumers; i++)
1581 consumers[i].consumer = NULL;
1583 for (i = 0; i < num_consumers; i++) {
1584 consumers[i].consumer = regulator_get(dev,
1585 consumers[i].supply);
1586 if (IS_ERR(consumers[i].consumer)) {
1587 dev_err(dev, "Failed to get supply '%s'\n",
1588 consumers[i].supply);
1589 ret = PTR_ERR(consumers[i].consumer);
1590 consumers[i].consumer = NULL;
1598 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1599 regulator_put(consumers[i].consumer);
1603 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1606 * regulator_bulk_enable - enable multiple regulator consumers
1608 * @num_consumers: Number of consumers
1609 * @consumers: Consumer data; clients are stored here.
1610 * @return 0 on success, an errno on failure
1612 * This convenience API allows consumers to enable multiple regulator
1613 * clients in a single API call. If any consumers cannot be enabled
1614 * then any others that were enabled will be disabled again prior to
1617 int regulator_bulk_enable(int num_consumers,
1618 struct regulator_bulk_data *consumers)
1623 for (i = 0; i < num_consumers; i++) {
1624 ret = regulator_enable(consumers[i].consumer);
1632 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1633 for (i = 0; i < num_consumers; i++)
1634 regulator_disable(consumers[i].consumer);
1638 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1641 * regulator_bulk_disable - disable multiple regulator consumers
1643 * @num_consumers: Number of consumers
1644 * @consumers: Consumer data; clients are stored here.
1645 * @return 0 on success, an errno on failure
1647 * This convenience API allows consumers to disable multiple regulator
1648 * clients in a single API call. If any consumers cannot be enabled
1649 * then any others that were disabled will be disabled again prior to
1652 int regulator_bulk_disable(int num_consumers,
1653 struct regulator_bulk_data *consumers)
1658 for (i = 0; i < num_consumers; i++) {
1659 ret = regulator_disable(consumers[i].consumer);
1667 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1668 for (i = 0; i < num_consumers; i++)
1669 regulator_enable(consumers[i].consumer);
1673 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1676 * regulator_bulk_free - free multiple regulator consumers
1678 * @num_consumers: Number of consumers
1679 * @consumers: Consumer data; clients are stored here.
1681 * This convenience API allows consumers to free multiple regulator
1682 * clients in a single API call.
1684 void regulator_bulk_free(int num_consumers,
1685 struct regulator_bulk_data *consumers)
1689 for (i = 0; i < num_consumers; i++) {
1690 regulator_put(consumers[i].consumer);
1691 consumers[i].consumer = NULL;
1694 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1697 * regulator_notifier_call_chain - call regulator event notifier
1698 * @regulator: regulator source
1699 * @event: notifier block
1702 * Called by regulator drivers to notify clients a regulator event has
1703 * occurred. We also notify regulator clients downstream.
1705 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1706 unsigned long event, void *data)
1708 _notifier_call_chain(rdev, event, data);
1712 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1715 * regulator_register - register regulator
1716 * @regulator: regulator source
1717 * @reg_data: private regulator data
1719 * Called by regulator drivers to register a regulator.
1720 * Returns 0 on success.
1722 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1723 struct device *dev, void *driver_data)
1725 static atomic_t regulator_no = ATOMIC_INIT(0);
1726 struct regulator_dev *rdev;
1727 struct regulator_init_data *init_data = dev->platform_data;
1730 if (regulator_desc == NULL)
1731 return ERR_PTR(-EINVAL);
1733 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1734 return ERR_PTR(-EINVAL);
1736 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1737 !regulator_desc->type == REGULATOR_CURRENT)
1738 return ERR_PTR(-EINVAL);
1741 return ERR_PTR(-EINVAL);
1743 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1745 return ERR_PTR(-ENOMEM);
1747 mutex_lock(®ulator_list_mutex);
1749 mutex_init(&rdev->mutex);
1750 rdev->reg_data = driver_data;
1751 rdev->owner = regulator_desc->owner;
1752 rdev->desc = regulator_desc;
1753 INIT_LIST_HEAD(&rdev->consumer_list);
1754 INIT_LIST_HEAD(&rdev->supply_list);
1755 INIT_LIST_HEAD(&rdev->list);
1756 INIT_LIST_HEAD(&rdev->slist);
1757 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1759 /* preform any regulator specific init */
1760 if (init_data->regulator_init) {
1761 ret = init_data->regulator_init(rdev->reg_data);
1766 /* set regulator constraints */
1767 ret = set_machine_constraints(rdev, &init_data->constraints);
1771 /* register with sysfs */
1772 rdev->dev.class = ®ulator_class;
1773 rdev->dev.parent = dev;
1774 dev_set_name(&rdev->dev, "regulator.%d",
1775 atomic_inc_return(®ulator_no) - 1);
1776 ret = device_register(&rdev->dev);
1780 dev_set_drvdata(&rdev->dev, rdev);
1782 /* set supply regulator if it exists */
1783 if (init_data->supply_regulator_dev) {
1784 ret = set_supply(rdev,
1785 dev_get_drvdata(init_data->supply_regulator_dev));
1790 /* add consumers devices */
1791 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1792 ret = set_consumer_device_supply(rdev,
1793 init_data->consumer_supplies[i].dev,
1794 init_data->consumer_supplies[i].supply);
1796 for (--i; i >= 0; i--)
1797 unset_consumer_device_supply(rdev,
1798 init_data->consumer_supplies[i].dev);
1803 list_add(&rdev->list, ®ulator_list);
1805 mutex_unlock(®ulator_list_mutex);
1809 device_unregister(&rdev->dev);
1812 rdev = ERR_PTR(ret);
1815 EXPORT_SYMBOL_GPL(regulator_register);
1818 * regulator_unregister - unregister regulator
1819 * @regulator: regulator source
1821 * Called by regulator drivers to unregister a regulator.
1823 void regulator_unregister(struct regulator_dev *rdev)
1828 mutex_lock(®ulator_list_mutex);
1829 list_del(&rdev->list);
1831 sysfs_remove_link(&rdev->dev.kobj, "supply");
1832 device_unregister(&rdev->dev);
1833 mutex_unlock(®ulator_list_mutex);
1835 EXPORT_SYMBOL_GPL(regulator_unregister);
1838 * regulator_suspend_prepare: prepare regulators for system wide suspend
1839 * @state: system suspend state
1841 * Configure each regulator with it's suspend operating parameters for state.
1842 * This will usually be called by machine suspend code prior to supending.
1844 int regulator_suspend_prepare(suspend_state_t state)
1846 struct regulator_dev *rdev;
1849 /* ON is handled by regulator active state */
1850 if (state == PM_SUSPEND_ON)
1853 mutex_lock(®ulator_list_mutex);
1854 list_for_each_entry(rdev, ®ulator_list, list) {
1856 mutex_lock(&rdev->mutex);
1857 ret = suspend_prepare(rdev, state);
1858 mutex_unlock(&rdev->mutex);
1861 printk(KERN_ERR "%s: failed to prepare %s\n",
1862 __func__, rdev->desc->name);
1867 mutex_unlock(®ulator_list_mutex);
1870 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1873 * rdev_get_drvdata - get rdev regulator driver data
1874 * @regulator: regulator
1876 * Get rdev regulator driver private data. This call can be used in the
1877 * regulator driver context.
1879 void *rdev_get_drvdata(struct regulator_dev *rdev)
1881 return rdev->reg_data;
1883 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1886 * regulator_get_drvdata - get regulator driver data
1887 * @regulator: regulator
1889 * Get regulator driver private data. This call can be used in the consumer
1890 * driver context when non API regulator specific functions need to be called.
1892 void *regulator_get_drvdata(struct regulator *regulator)
1894 return regulator->rdev->reg_data;
1896 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1899 * regulator_set_drvdata - set regulator driver data
1900 * @regulator: regulator
1903 void regulator_set_drvdata(struct regulator *regulator, void *data)
1905 regulator->rdev->reg_data = data;
1907 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
1910 * regulator_get_id - get regulator ID
1911 * @regulator: regulator
1913 int rdev_get_id(struct regulator_dev *rdev)
1915 return rdev->desc->id;
1917 EXPORT_SYMBOL_GPL(rdev_get_id);
1919 struct device *rdev_get_dev(struct regulator_dev *rdev)
1923 EXPORT_SYMBOL_GPL(rdev_get_dev);
1925 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
1927 return reg_init_data->driver_data;
1929 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
1931 static int __init regulator_init(void)
1933 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
1934 return class_register(®ulator_class);
1937 /* init early to allow our consumers to complete system booting */
1938 core_initcall(regulator_init);