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_opmode_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 struct regulator_dev *rdev = dev_get_drvdata(dev);
274 int mode = _regulator_get_mode(rdev);
277 case REGULATOR_MODE_FAST:
278 return sprintf(buf, "fast\n");
279 case REGULATOR_MODE_NORMAL:
280 return sprintf(buf, "normal\n");
281 case REGULATOR_MODE_IDLE:
282 return sprintf(buf, "idle\n");
283 case REGULATOR_MODE_STANDBY:
284 return sprintf(buf, "standby\n");
286 return sprintf(buf, "unknown\n");
289 static ssize_t regulator_state_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct regulator_dev *rdev = dev_get_drvdata(dev);
293 int state = _regulator_is_enabled(rdev);
296 return sprintf(buf, "enabled\n");
298 return sprintf(buf, "disabled\n");
300 return sprintf(buf, "unknown\n");
303 static ssize_t regulator_min_uA_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
306 struct regulator_dev *rdev = dev_get_drvdata(dev);
308 if (!rdev->constraints)
309 return sprintf(buf, "constraint not defined\n");
311 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
314 static ssize_t regulator_max_uA_show(struct device *dev,
315 struct device_attribute *attr, char *buf)
317 struct regulator_dev *rdev = dev_get_drvdata(dev);
319 if (!rdev->constraints)
320 return sprintf(buf, "constraint not defined\n");
322 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
325 static ssize_t regulator_min_uV_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
328 struct regulator_dev *rdev = dev_get_drvdata(dev);
330 if (!rdev->constraints)
331 return sprintf(buf, "constraint not defined\n");
333 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
336 static ssize_t regulator_max_uV_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
339 struct regulator_dev *rdev = dev_get_drvdata(dev);
341 if (!rdev->constraints)
342 return sprintf(buf, "constraint not defined\n");
344 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
347 static ssize_t regulator_total_uA_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
350 struct regulator_dev *rdev = dev_get_drvdata(dev);
351 struct regulator *regulator;
354 mutex_lock(&rdev->mutex);
355 list_for_each_entry(regulator, &rdev->consumer_list, list)
356 uA += regulator->uA_load;
357 mutex_unlock(&rdev->mutex);
358 return sprintf(buf, "%d\n", uA);
361 static ssize_t regulator_num_users_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
364 struct regulator_dev *rdev = dev_get_drvdata(dev);
365 return sprintf(buf, "%d\n", rdev->use_count);
368 static ssize_t regulator_type_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
373 switch (rdev->desc->type) {
374 case REGULATOR_VOLTAGE:
375 return sprintf(buf, "voltage\n");
376 case REGULATOR_CURRENT:
377 return sprintf(buf, "current\n");
379 return sprintf(buf, "unknown\n");
382 static ssize_t regulator_suspend_mem_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_mem.uV);
392 static ssize_t regulator_suspend_disk_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_disk.uV);
402 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
405 struct regulator_dev *rdev = dev_get_drvdata(dev);
407 if (!rdev->constraints)
408 return sprintf(buf, "not defined\n");
409 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
412 static ssize_t suspend_opmode_show(struct regulator_dev *rdev,
413 unsigned int mode, char *buf)
416 case REGULATOR_MODE_FAST:
417 return sprintf(buf, "fast\n");
418 case REGULATOR_MODE_NORMAL:
419 return sprintf(buf, "normal\n");
420 case REGULATOR_MODE_IDLE:
421 return sprintf(buf, "idle\n");
422 case REGULATOR_MODE_STANDBY:
423 return sprintf(buf, "standby\n");
425 return sprintf(buf, "unknown\n");
428 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
431 struct regulator_dev *rdev = dev_get_drvdata(dev);
433 if (!rdev->constraints)
434 return sprintf(buf, "not defined\n");
435 return suspend_opmode_show(rdev,
436 rdev->constraints->state_mem.mode, buf);
439 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
442 struct regulator_dev *rdev = dev_get_drvdata(dev);
444 if (!rdev->constraints)
445 return sprintf(buf, "not defined\n");
446 return suspend_opmode_show(rdev,
447 rdev->constraints->state_disk.mode, buf);
450 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 struct regulator_dev *rdev = dev_get_drvdata(dev);
455 if (!rdev->constraints)
456 return sprintf(buf, "not defined\n");
457 return suspend_opmode_show(rdev,
458 rdev->constraints->state_standby.mode, buf);
461 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
464 struct regulator_dev *rdev = dev_get_drvdata(dev);
466 if (!rdev->constraints)
467 return sprintf(buf, "not defined\n");
469 if (rdev->constraints->state_mem.enabled)
470 return sprintf(buf, "enabled\n");
472 return sprintf(buf, "disabled\n");
475 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
478 struct regulator_dev *rdev = dev_get_drvdata(dev);
480 if (!rdev->constraints)
481 return sprintf(buf, "not defined\n");
483 if (rdev->constraints->state_disk.enabled)
484 return sprintf(buf, "enabled\n");
486 return sprintf(buf, "disabled\n");
489 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
492 struct regulator_dev *rdev = dev_get_drvdata(dev);
494 if (!rdev->constraints)
495 return sprintf(buf, "not defined\n");
497 if (rdev->constraints->state_standby.enabled)
498 return sprintf(buf, "enabled\n");
500 return sprintf(buf, "disabled\n");
503 static struct device_attribute regulator_dev_attrs[] = {
504 __ATTR(name, 0444, regulator_name_show, NULL),
505 __ATTR(microvolts, 0444, regulator_uV_show, NULL),
506 __ATTR(microamps, 0444, regulator_uA_show, NULL),
507 __ATTR(opmode, 0444, regulator_opmode_show, NULL),
508 __ATTR(state, 0444, regulator_state_show, NULL),
509 __ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL),
510 __ATTR(min_microamps, 0444, regulator_min_uA_show, NULL),
511 __ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL),
512 __ATTR(max_microamps, 0444, regulator_max_uA_show, NULL),
513 __ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL),
514 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
515 __ATTR(type, 0444, regulator_type_show, NULL),
516 __ATTR(suspend_mem_microvolts, 0444,
517 regulator_suspend_mem_uV_show, NULL),
518 __ATTR(suspend_disk_microvolts, 0444,
519 regulator_suspend_disk_uV_show, NULL),
520 __ATTR(suspend_standby_microvolts, 0444,
521 regulator_suspend_standby_uV_show, NULL),
522 __ATTR(suspend_mem_mode, 0444,
523 regulator_suspend_mem_mode_show, NULL),
524 __ATTR(suspend_disk_mode, 0444,
525 regulator_suspend_disk_mode_show, NULL),
526 __ATTR(suspend_standby_mode, 0444,
527 regulator_suspend_standby_mode_show, NULL),
528 __ATTR(suspend_mem_state, 0444,
529 regulator_suspend_mem_state_show, NULL),
530 __ATTR(suspend_disk_state, 0444,
531 regulator_suspend_disk_state_show, NULL),
532 __ATTR(suspend_standby_state, 0444,
533 regulator_suspend_standby_state_show, NULL),
537 static void regulator_dev_release(struct device *dev)
539 struct regulator_dev *rdev = dev_get_drvdata(dev);
543 static struct class regulator_class = {
545 .dev_release = regulator_dev_release,
546 .dev_attrs = regulator_dev_attrs,
549 /* Calculate the new optimum regulator operating mode based on the new total
550 * consumer load. All locks held by caller */
551 static void drms_uA_update(struct regulator_dev *rdev)
553 struct regulator *sibling;
554 int current_uA = 0, output_uV, input_uV, err;
557 err = regulator_check_drms(rdev);
558 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
559 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
562 /* get output voltage */
563 output_uV = rdev->desc->ops->get_voltage(rdev);
567 /* get input voltage */
568 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
569 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
571 input_uV = rdev->constraints->input_uV;
575 /* calc total requested load */
576 list_for_each_entry(sibling, &rdev->consumer_list, list)
577 current_uA += sibling->uA_load;
579 /* now get the optimum mode for our new total regulator load */
580 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
581 output_uV, current_uA);
583 /* check the new mode is allowed */
584 err = regulator_check_mode(rdev, mode);
586 rdev->desc->ops->set_mode(rdev, mode);
589 static int suspend_set_state(struct regulator_dev *rdev,
590 struct regulator_state *rstate)
594 /* enable & disable are mandatory for suspend control */
595 if (!rdev->desc->ops->set_suspend_enable ||
596 !rdev->desc->ops->set_suspend_disable) {
597 printk(KERN_ERR "%s: no way to set suspend state\n",
603 ret = rdev->desc->ops->set_suspend_enable(rdev);
605 ret = rdev->desc->ops->set_suspend_disable(rdev);
607 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
611 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
612 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
614 printk(KERN_ERR "%s: failed to set voltage\n",
620 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
621 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
623 printk(KERN_ERR "%s: failed to set mode\n", __func__);
630 /* locks held by caller */
631 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
633 if (!rdev->constraints)
637 case PM_SUSPEND_STANDBY:
638 return suspend_set_state(rdev,
639 &rdev->constraints->state_standby);
641 return suspend_set_state(rdev,
642 &rdev->constraints->state_mem);
644 return suspend_set_state(rdev,
645 &rdev->constraints->state_disk);
651 static void print_constraints(struct regulator_dev *rdev)
653 struct regulation_constraints *constraints = rdev->constraints;
657 if (rdev->desc->type == REGULATOR_VOLTAGE) {
658 if (constraints->min_uV == constraints->max_uV)
659 count = sprintf(buf, "%d mV ",
660 constraints->min_uV / 1000);
662 count = sprintf(buf, "%d <--> %d mV ",
663 constraints->min_uV / 1000,
664 constraints->max_uV / 1000);
666 if (constraints->min_uA == constraints->max_uA)
667 count = sprintf(buf, "%d mA ",
668 constraints->min_uA / 1000);
670 count = sprintf(buf, "%d <--> %d mA ",
671 constraints->min_uA / 1000,
672 constraints->max_uA / 1000);
674 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
675 count += sprintf(buf + count, "fast ");
676 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
677 count += sprintf(buf + count, "normal ");
678 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
679 count += sprintf(buf + count, "idle ");
680 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
681 count += sprintf(buf + count, "standby");
683 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
687 * set_machine_constraints - sets regulator constraints
688 * @regulator: regulator source
690 * Allows platform initialisation code to define and constrain
691 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
692 * Constraints *must* be set by platform code in order for some
693 * regulator operations to proceed i.e. set_voltage, set_current_limit,
696 static int set_machine_constraints(struct regulator_dev *rdev,
697 struct regulation_constraints *constraints)
701 struct regulator_ops *ops = rdev->desc->ops;
703 if (constraints->name)
704 name = constraints->name;
705 else if (rdev->desc->name)
706 name = rdev->desc->name;
710 rdev->constraints = constraints;
712 /* do we need to apply the constraint voltage */
713 if (rdev->constraints->apply_uV &&
714 rdev->constraints->min_uV == rdev->constraints->max_uV &&
716 ret = ops->set_voltage(rdev,
717 rdev->constraints->min_uV, rdev->constraints->max_uV);
719 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
721 rdev->constraints->min_uV, name);
722 rdev->constraints = NULL;
727 /* are we enabled at boot time by firmware / bootloader */
728 if (rdev->constraints->boot_on)
731 /* do we need to setup our suspend state */
732 if (constraints->initial_state) {
733 ret = suspend_prepare(rdev, constraints->initial_state);
735 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
737 rdev->constraints = NULL;
742 /* if always_on is set then turn the regulator on if it's not
744 if (constraints->always_on && ops->enable &&
745 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
746 (!ops->is_enabled && !constraints->boot_on))) {
747 ret = ops->enable(rdev);
749 printk(KERN_ERR "%s: failed to enable %s\n",
751 rdev->constraints = NULL;
756 print_constraints(rdev);
762 * set_supply - set regulator supply regulator
763 * @regulator: regulator name
764 * @supply: supply regulator name
766 * Called by platform initialisation code to set the supply regulator for this
767 * regulator. This ensures that a regulators supply will also be enabled by the
768 * core if it's child is enabled.
770 static int set_supply(struct regulator_dev *rdev,
771 struct regulator_dev *supply_rdev)
775 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
779 "%s: could not add device link %s err %d\n",
780 __func__, supply_rdev->dev.kobj.name, err);
783 rdev->supply = supply_rdev;
784 list_add(&rdev->slist, &supply_rdev->supply_list);
790 * set_consumer_device_supply: Bind a regulator to a symbolic supply
791 * @regulator: regulator source
792 * @dev: device the supply applies to
793 * @supply: symbolic name for supply
795 * Allows platform initialisation code to map physical regulator
796 * sources to symbolic names for supplies for use by devices. Devices
797 * should use these symbolic names to request regulators, avoiding the
798 * need to provide board-specific regulator names as platform data.
800 static int set_consumer_device_supply(struct regulator_dev *rdev,
801 struct device *consumer_dev, const char *supply)
803 struct regulator_map *node;
808 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
812 node->regulator = rdev;
813 node->dev = consumer_dev;
814 node->supply = supply;
816 list_add(&node->list, ®ulator_map_list);
820 static void unset_consumer_device_supply(struct regulator_dev *rdev,
821 struct device *consumer_dev)
823 struct regulator_map *node, *n;
825 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
826 if (rdev == node->regulator &&
827 consumer_dev == node->dev) {
828 list_del(&node->list);
835 #define REG_STR_SIZE 32
837 static struct regulator *create_regulator(struct regulator_dev *rdev,
839 const char *supply_name)
841 struct regulator *regulator;
842 char buf[REG_STR_SIZE];
845 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
846 if (regulator == NULL)
849 mutex_lock(&rdev->mutex);
850 regulator->rdev = rdev;
851 list_add(®ulator->list, &rdev->consumer_list);
854 /* create a 'requested_microamps_name' sysfs entry */
855 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
857 if (size >= REG_STR_SIZE)
860 regulator->dev = dev;
861 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
862 if (regulator->dev_attr.attr.name == NULL)
865 regulator->dev_attr.attr.owner = THIS_MODULE;
866 regulator->dev_attr.attr.mode = 0444;
867 regulator->dev_attr.show = device_requested_uA_show;
868 err = device_create_file(dev, ®ulator->dev_attr);
870 printk(KERN_WARNING "%s: could not add regulator_dev"
871 " load sysfs\n", __func__);
875 /* also add a link to the device sysfs entry */
876 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
877 dev->kobj.name, supply_name);
878 if (size >= REG_STR_SIZE)
881 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
882 if (regulator->supply_name == NULL)
885 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
889 "%s: could not add device link %s err %d\n",
890 __func__, dev->kobj.name, err);
891 device_remove_file(dev, ®ulator->dev_attr);
895 mutex_unlock(&rdev->mutex);
898 kfree(regulator->supply_name);
900 device_remove_file(regulator->dev, ®ulator->dev_attr);
902 kfree(regulator->dev_attr.attr.name);
904 list_del(®ulator->list);
906 mutex_unlock(&rdev->mutex);
911 * regulator_get - lookup and obtain a reference to a regulator.
912 * @dev: device for regulator "consumer"
913 * @id: Supply name or regulator ID.
915 * Returns a struct regulator corresponding to the regulator producer,
916 * or IS_ERR() condition containing errno. Use of supply names
917 * configured via regulator_set_device_supply() is strongly
920 struct regulator *regulator_get(struct device *dev, const char *id)
922 struct regulator_dev *rdev;
923 struct regulator_map *map;
924 struct regulator *regulator = ERR_PTR(-ENODEV);
927 printk(KERN_ERR "regulator: get() with no identifier\n");
931 mutex_lock(®ulator_list_mutex);
933 list_for_each_entry(map, ®ulator_map_list, list) {
934 if (dev == map->dev &&
935 strcmp(map->supply, id) == 0) {
936 rdev = map->regulator;
940 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
942 mutex_unlock(®ulator_list_mutex);
946 if (!try_module_get(rdev->owner))
949 regulator = create_regulator(rdev, dev, id);
950 if (regulator == NULL) {
951 regulator = ERR_PTR(-ENOMEM);
952 module_put(rdev->owner);
956 mutex_unlock(®ulator_list_mutex);
959 EXPORT_SYMBOL_GPL(regulator_get);
962 * regulator_put - "free" the regulator source
963 * @regulator: regulator source
965 * Note: drivers must ensure that all regulator_enable calls made on this
966 * regulator source are balanced by regulator_disable calls prior to calling
969 void regulator_put(struct regulator *regulator)
971 struct regulator_dev *rdev;
973 if (regulator == NULL || IS_ERR(regulator))
976 mutex_lock(®ulator_list_mutex);
977 rdev = regulator->rdev;
979 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
980 regulator->supply_name))
981 _regulator_disable(rdev);
983 /* remove any sysfs entries */
984 if (regulator->dev) {
985 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
986 kfree(regulator->supply_name);
987 device_remove_file(regulator->dev, ®ulator->dev_attr);
988 kfree(regulator->dev_attr.attr.name);
990 list_del(®ulator->list);
993 module_put(rdev->owner);
994 mutex_unlock(®ulator_list_mutex);
996 EXPORT_SYMBOL_GPL(regulator_put);
998 /* locks held by regulator_enable() */
999 static int _regulator_enable(struct regulator_dev *rdev)
1003 if (!rdev->constraints) {
1004 printk(KERN_ERR "%s: %s has no constraints\n",
1005 __func__, rdev->desc->name);
1009 /* do we need to enable the supply regulator first */
1011 ret = _regulator_enable(rdev->supply);
1013 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1014 __func__, rdev->desc->name, ret);
1019 /* check voltage and requested load before enabling */
1020 if (rdev->desc->ops->enable) {
1022 if (rdev->constraints &&
1023 (rdev->constraints->valid_ops_mask &
1024 REGULATOR_CHANGE_DRMS))
1025 drms_uA_update(rdev);
1027 ret = rdev->desc->ops->enable(rdev);
1029 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1030 __func__, rdev->desc->name, ret);
1041 * regulator_enable - enable regulator output
1042 * @regulator: regulator source
1044 * Enable the regulator output at the predefined voltage or current value.
1045 * NOTE: the output value can be set by other drivers, boot loader or may be
1046 * hardwired in the regulator.
1047 * NOTE: calls to regulator_enable() must be balanced with calls to
1048 * regulator_disable().
1050 int regulator_enable(struct regulator *regulator)
1052 struct regulator_dev *rdev = regulator->rdev;
1055 mutex_lock(&rdev->mutex);
1056 if (regulator->enabled == 0)
1057 ret = _regulator_enable(rdev);
1058 else if (regulator->enabled < 0)
1061 regulator->enabled++;
1062 mutex_unlock(&rdev->mutex);
1065 EXPORT_SYMBOL_GPL(regulator_enable);
1067 /* locks held by regulator_disable() */
1068 static int _regulator_disable(struct regulator_dev *rdev)
1072 /* are we the last user and permitted to disable ? */
1073 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1075 /* we are last user */
1076 if (rdev->desc->ops->disable) {
1077 ret = rdev->desc->ops->disable(rdev);
1079 printk(KERN_ERR "%s: failed to disable %s\n",
1080 __func__, rdev->desc->name);
1085 /* decrease our supplies ref count and disable if required */
1087 _regulator_disable(rdev->supply);
1089 rdev->use_count = 0;
1090 } else if (rdev->use_count > 1) {
1092 if (rdev->constraints &&
1093 (rdev->constraints->valid_ops_mask &
1094 REGULATOR_CHANGE_DRMS))
1095 drms_uA_update(rdev);
1103 * regulator_disable - disable regulator output
1104 * @regulator: regulator source
1106 * Disable the regulator output voltage or current.
1107 * NOTE: this will only disable the regulator output if no other consumer
1108 * devices have it enabled.
1109 * NOTE: calls to regulator_enable() must be balanced with calls to
1110 * regulator_disable().
1112 int regulator_disable(struct regulator *regulator)
1114 struct regulator_dev *rdev = regulator->rdev;
1117 mutex_lock(&rdev->mutex);
1118 if (regulator->enabled == 1) {
1119 ret = _regulator_disable(rdev);
1121 regulator->uA_load = 0;
1122 } else if (WARN(regulator->enabled <= 0,
1123 "unbalanced disables for supply %s\n",
1124 regulator->supply_name))
1127 regulator->enabled--;
1128 mutex_unlock(&rdev->mutex);
1131 EXPORT_SYMBOL_GPL(regulator_disable);
1133 /* locks held by regulator_force_disable() */
1134 static int _regulator_force_disable(struct regulator_dev *rdev)
1139 if (rdev->desc->ops->disable) {
1140 /* ah well, who wants to live forever... */
1141 ret = rdev->desc->ops->disable(rdev);
1143 printk(KERN_ERR "%s: failed to force disable %s\n",
1144 __func__, rdev->desc->name);
1147 /* notify other consumers that power has been forced off */
1148 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1152 /* decrease our supplies ref count and disable if required */
1154 _regulator_disable(rdev->supply);
1156 rdev->use_count = 0;
1161 * regulator_force_disable - force disable regulator output
1162 * @regulator: regulator source
1164 * Forcibly disable the regulator output voltage or current.
1165 * NOTE: this *will* disable the regulator output even if other consumer
1166 * devices have it enabled. This should be used for situations when device
1167 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1169 int regulator_force_disable(struct regulator *regulator)
1173 mutex_lock(®ulator->rdev->mutex);
1174 regulator->enabled = 0;
1175 regulator->uA_load = 0;
1176 ret = _regulator_force_disable(regulator->rdev);
1177 mutex_unlock(®ulator->rdev->mutex);
1180 EXPORT_SYMBOL_GPL(regulator_force_disable);
1182 static int _regulator_is_enabled(struct regulator_dev *rdev)
1186 mutex_lock(&rdev->mutex);
1189 if (!rdev->desc->ops->is_enabled) {
1194 ret = rdev->desc->ops->is_enabled(rdev);
1196 mutex_unlock(&rdev->mutex);
1201 * regulator_is_enabled - is the regulator output enabled
1202 * @regulator: regulator source
1204 * Returns positive if the regulator driver backing the source/client
1205 * has requested that the device be enabled, zero if it hasn't, else a
1206 * negative errno code.
1208 * Note that the device backing this regulator handle can have multiple
1209 * users, so it might be enabled even if regulator_enable() was never
1210 * called for this particular source.
1212 int regulator_is_enabled(struct regulator *regulator)
1214 return _regulator_is_enabled(regulator->rdev);
1216 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1219 * regulator_set_voltage - set regulator output voltage
1220 * @regulator: regulator source
1221 * @min_uV: Minimum required voltage in uV
1222 * @max_uV: Maximum acceptable voltage in uV
1224 * Sets a voltage regulator to the desired output voltage. This can be set
1225 * during any regulator state. IOW, regulator can be disabled or enabled.
1227 * If the regulator is enabled then the voltage will change to the new value
1228 * immediately otherwise if the regulator is disabled the regulator will
1229 * output at the new voltage when enabled.
1231 * NOTE: If the regulator is shared between several devices then the lowest
1232 * request voltage that meets the system constraints will be used.
1233 * NOTE: Regulator system constraints must be set for this regulator before
1234 * calling this function otherwise this call will fail.
1236 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1238 struct regulator_dev *rdev = regulator->rdev;
1241 mutex_lock(&rdev->mutex);
1244 if (!rdev->desc->ops->set_voltage) {
1249 /* constraints check */
1250 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1253 regulator->min_uV = min_uV;
1254 regulator->max_uV = max_uV;
1255 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1258 mutex_unlock(&rdev->mutex);
1261 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1263 static int _regulator_get_voltage(struct regulator_dev *rdev)
1266 if (rdev->desc->ops->get_voltage)
1267 return rdev->desc->ops->get_voltage(rdev);
1273 * regulator_get_voltage - get regulator output voltage
1274 * @regulator: regulator source
1276 * This returns the current regulator voltage in uV.
1278 * NOTE: If the regulator is disabled it will return the voltage value. This
1279 * function should not be used to determine regulator state.
1281 int regulator_get_voltage(struct regulator *regulator)
1285 mutex_lock(®ulator->rdev->mutex);
1287 ret = _regulator_get_voltage(regulator->rdev);
1289 mutex_unlock(®ulator->rdev->mutex);
1293 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1296 * regulator_set_current_limit - set regulator output current limit
1297 * @regulator: regulator source
1298 * @min_uA: Minimuum supported current in uA
1299 * @max_uA: Maximum supported current in uA
1301 * Sets current sink to the desired output current. This can be set during
1302 * any regulator state. IOW, regulator can be disabled or enabled.
1304 * If the regulator is enabled then the current will change to the new value
1305 * immediately otherwise if the regulator is disabled the regulator will
1306 * output at the new current when enabled.
1308 * NOTE: Regulator system constraints must be set for this regulator before
1309 * calling this function otherwise this call will fail.
1311 int regulator_set_current_limit(struct regulator *regulator,
1312 int min_uA, int max_uA)
1314 struct regulator_dev *rdev = regulator->rdev;
1317 mutex_lock(&rdev->mutex);
1320 if (!rdev->desc->ops->set_current_limit) {
1325 /* constraints check */
1326 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1330 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1332 mutex_unlock(&rdev->mutex);
1335 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1337 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1341 mutex_lock(&rdev->mutex);
1344 if (!rdev->desc->ops->get_current_limit) {
1349 ret = rdev->desc->ops->get_current_limit(rdev);
1351 mutex_unlock(&rdev->mutex);
1356 * regulator_get_current_limit - get regulator output current
1357 * @regulator: regulator source
1359 * This returns the current supplied by the specified current sink in uA.
1361 * NOTE: If the regulator is disabled it will return the current value. This
1362 * function should not be used to determine regulator state.
1364 int regulator_get_current_limit(struct regulator *regulator)
1366 return _regulator_get_current_limit(regulator->rdev);
1368 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1371 * regulator_set_mode - set regulator operating mode
1372 * @regulator: regulator source
1373 * @mode: operating mode - one of the REGULATOR_MODE constants
1375 * Set regulator operating mode to increase regulator efficiency or improve
1376 * regulation performance.
1378 * NOTE: Regulator system constraints must be set for this regulator before
1379 * calling this function otherwise this call will fail.
1381 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1383 struct regulator_dev *rdev = regulator->rdev;
1386 mutex_lock(&rdev->mutex);
1389 if (!rdev->desc->ops->set_mode) {
1394 /* constraints check */
1395 ret = regulator_check_mode(rdev, mode);
1399 ret = rdev->desc->ops->set_mode(rdev, mode);
1401 mutex_unlock(&rdev->mutex);
1404 EXPORT_SYMBOL_GPL(regulator_set_mode);
1406 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1410 mutex_lock(&rdev->mutex);
1413 if (!rdev->desc->ops->get_mode) {
1418 ret = rdev->desc->ops->get_mode(rdev);
1420 mutex_unlock(&rdev->mutex);
1425 * regulator_get_mode - get regulator operating mode
1426 * @regulator: regulator source
1428 * Get the current regulator operating mode.
1430 unsigned int regulator_get_mode(struct regulator *regulator)
1432 return _regulator_get_mode(regulator->rdev);
1434 EXPORT_SYMBOL_GPL(regulator_get_mode);
1437 * regulator_set_optimum_mode - set regulator optimum operating mode
1438 * @regulator: regulator source
1439 * @uA_load: load current
1441 * Notifies the regulator core of a new device load. This is then used by
1442 * DRMS (if enabled by constraints) to set the most efficient regulator
1443 * operating mode for the new regulator loading.
1445 * Consumer devices notify their supply regulator of the maximum power
1446 * they will require (can be taken from device datasheet in the power
1447 * consumption tables) when they change operational status and hence power
1448 * state. Examples of operational state changes that can affect power
1449 * consumption are :-
1451 * o Device is opened / closed.
1452 * o Device I/O is about to begin or has just finished.
1453 * o Device is idling in between work.
1455 * This information is also exported via sysfs to userspace.
1457 * DRMS will sum the total requested load on the regulator and change
1458 * to the most efficient operating mode if platform constraints allow.
1460 * Returns the new regulator mode or error.
1462 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1464 struct regulator_dev *rdev = regulator->rdev;
1465 struct regulator *consumer;
1466 int ret, output_uV, input_uV, total_uA_load = 0;
1469 mutex_lock(&rdev->mutex);
1471 regulator->uA_load = uA_load;
1472 ret = regulator_check_drms(rdev);
1478 if (!rdev->desc->ops->get_optimum_mode)
1481 /* get output voltage */
1482 output_uV = rdev->desc->ops->get_voltage(rdev);
1483 if (output_uV <= 0) {
1484 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1485 __func__, rdev->desc->name);
1489 /* get input voltage */
1490 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1491 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1493 input_uV = rdev->constraints->input_uV;
1494 if (input_uV <= 0) {
1495 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1496 __func__, rdev->desc->name);
1500 /* calc total requested load for this regulator */
1501 list_for_each_entry(consumer, &rdev->consumer_list, list)
1502 total_uA_load += consumer->uA_load;
1504 mode = rdev->desc->ops->get_optimum_mode(rdev,
1505 input_uV, output_uV,
1507 ret = regulator_check_mode(rdev, mode);
1509 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1510 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1511 total_uA_load, input_uV, output_uV);
1515 ret = rdev->desc->ops->set_mode(rdev, mode);
1517 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1518 __func__, mode, rdev->desc->name);
1523 mutex_unlock(&rdev->mutex);
1526 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1529 * regulator_register_notifier - register regulator event notifier
1530 * @regulator: regulator source
1531 * @notifier_block: notifier block
1533 * Register notifier block to receive regulator events.
1535 int regulator_register_notifier(struct regulator *regulator,
1536 struct notifier_block *nb)
1538 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1541 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1544 * regulator_unregister_notifier - unregister regulator event notifier
1545 * @regulator: regulator source
1546 * @notifier_block: notifier block
1548 * Unregister regulator event notifier block.
1550 int regulator_unregister_notifier(struct regulator *regulator,
1551 struct notifier_block *nb)
1553 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1556 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1558 /* notify regulator consumers and downstream regulator consumers */
1559 static void _notifier_call_chain(struct regulator_dev *rdev,
1560 unsigned long event, void *data)
1562 struct regulator_dev *_rdev;
1564 /* call rdev chain first */
1565 mutex_lock(&rdev->mutex);
1566 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1567 mutex_unlock(&rdev->mutex);
1569 /* now notify regulator we supply */
1570 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1571 _notifier_call_chain(_rdev, event, data);
1575 * regulator_bulk_get - get multiple regulator consumers
1577 * @dev: Device to supply
1578 * @num_consumers: Number of consumers to register
1579 * @consumers: Configuration of consumers; clients are stored here.
1581 * @return 0 on success, an errno on failure.
1583 * This helper function allows drivers to get several regulator
1584 * consumers in one operation. If any of the regulators cannot be
1585 * acquired then any regulators that were allocated will be freed
1586 * before returning to the caller.
1588 int regulator_bulk_get(struct device *dev, int num_consumers,
1589 struct regulator_bulk_data *consumers)
1594 for (i = 0; i < num_consumers; i++)
1595 consumers[i].consumer = NULL;
1597 for (i = 0; i < num_consumers; i++) {
1598 consumers[i].consumer = regulator_get(dev,
1599 consumers[i].supply);
1600 if (IS_ERR(consumers[i].consumer)) {
1601 dev_err(dev, "Failed to get supply '%s'\n",
1602 consumers[i].supply);
1603 ret = PTR_ERR(consumers[i].consumer);
1604 consumers[i].consumer = NULL;
1612 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1613 regulator_put(consumers[i].consumer);
1617 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1620 * regulator_bulk_enable - enable multiple regulator consumers
1622 * @num_consumers: Number of consumers
1623 * @consumers: Consumer data; clients are stored here.
1624 * @return 0 on success, an errno on failure
1626 * This convenience API allows consumers to enable multiple regulator
1627 * clients in a single API call. If any consumers cannot be enabled
1628 * then any others that were enabled will be disabled again prior to
1631 int regulator_bulk_enable(int num_consumers,
1632 struct regulator_bulk_data *consumers)
1637 for (i = 0; i < num_consumers; i++) {
1638 ret = regulator_enable(consumers[i].consumer);
1646 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1647 for (i = 0; i < num_consumers; i++)
1648 regulator_disable(consumers[i].consumer);
1652 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1655 * regulator_bulk_disable - disable multiple regulator consumers
1657 * @num_consumers: Number of consumers
1658 * @consumers: Consumer data; clients are stored here.
1659 * @return 0 on success, an errno on failure
1661 * This convenience API allows consumers to disable multiple regulator
1662 * clients in a single API call. If any consumers cannot be enabled
1663 * then any others that were disabled will be disabled again prior to
1666 int regulator_bulk_disable(int num_consumers,
1667 struct regulator_bulk_data *consumers)
1672 for (i = 0; i < num_consumers; i++) {
1673 ret = regulator_disable(consumers[i].consumer);
1681 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1682 for (i = 0; i < num_consumers; i++)
1683 regulator_enable(consumers[i].consumer);
1687 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1690 * regulator_bulk_free - free multiple regulator consumers
1692 * @num_consumers: Number of consumers
1693 * @consumers: Consumer data; clients are stored here.
1695 * This convenience API allows consumers to free multiple regulator
1696 * clients in a single API call.
1698 void regulator_bulk_free(int num_consumers,
1699 struct regulator_bulk_data *consumers)
1703 for (i = 0; i < num_consumers; i++) {
1704 regulator_put(consumers[i].consumer);
1705 consumers[i].consumer = NULL;
1708 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1711 * regulator_notifier_call_chain - call regulator event notifier
1712 * @regulator: regulator source
1713 * @event: notifier block
1716 * Called by regulator drivers to notify clients a regulator event has
1717 * occurred. We also notify regulator clients downstream.
1719 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1720 unsigned long event, void *data)
1722 _notifier_call_chain(rdev, event, data);
1726 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1729 * regulator_register - register regulator
1730 * @regulator: regulator source
1731 * @reg_data: private regulator data
1733 * Called by regulator drivers to register a regulator.
1734 * Returns 0 on success.
1736 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1737 struct device *dev, void *driver_data)
1739 static atomic_t regulator_no = ATOMIC_INIT(0);
1740 struct regulator_dev *rdev;
1741 struct regulator_init_data *init_data = dev->platform_data;
1744 if (regulator_desc == NULL)
1745 return ERR_PTR(-EINVAL);
1747 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1748 return ERR_PTR(-EINVAL);
1750 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1751 !regulator_desc->type == REGULATOR_CURRENT)
1752 return ERR_PTR(-EINVAL);
1755 return ERR_PTR(-EINVAL);
1757 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1759 return ERR_PTR(-ENOMEM);
1761 mutex_lock(®ulator_list_mutex);
1763 mutex_init(&rdev->mutex);
1764 rdev->reg_data = driver_data;
1765 rdev->owner = regulator_desc->owner;
1766 rdev->desc = regulator_desc;
1767 INIT_LIST_HEAD(&rdev->consumer_list);
1768 INIT_LIST_HEAD(&rdev->supply_list);
1769 INIT_LIST_HEAD(&rdev->list);
1770 INIT_LIST_HEAD(&rdev->slist);
1771 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1773 /* preform any regulator specific init */
1774 if (init_data->regulator_init) {
1775 ret = init_data->regulator_init(rdev->reg_data);
1778 rdev = ERR_PTR(ret);
1783 /* set regulator constraints */
1784 ret = set_machine_constraints(rdev, &init_data->constraints);
1787 rdev = ERR_PTR(ret);
1791 /* register with sysfs */
1792 rdev->dev.class = ®ulator_class;
1793 rdev->dev.parent = dev;
1794 dev_set_name(&rdev->dev, "regulator.%d",
1795 atomic_inc_return(®ulator_no) - 1);
1796 ret = device_register(&rdev->dev);
1799 rdev = ERR_PTR(ret);
1803 dev_set_drvdata(&rdev->dev, rdev);
1805 /* set supply regulator if it exists */
1806 if (init_data->supply_regulator_dev) {
1807 ret = set_supply(rdev,
1808 dev_get_drvdata(init_data->supply_regulator_dev));
1810 device_unregister(&rdev->dev);
1812 rdev = ERR_PTR(ret);
1817 /* add consumers devices */
1818 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1819 ret = set_consumer_device_supply(rdev,
1820 init_data->consumer_supplies[i].dev,
1821 init_data->consumer_supplies[i].supply);
1823 for (--i; i >= 0; i--)
1824 unset_consumer_device_supply(rdev,
1825 init_data->consumer_supplies[i].dev);
1826 device_unregister(&rdev->dev);
1828 rdev = ERR_PTR(ret);
1833 list_add(&rdev->list, ®ulator_list);
1835 mutex_unlock(®ulator_list_mutex);
1838 EXPORT_SYMBOL_GPL(regulator_register);
1841 * regulator_unregister - unregister regulator
1842 * @regulator: regulator source
1844 * Called by regulator drivers to unregister a regulator.
1846 void regulator_unregister(struct regulator_dev *rdev)
1851 mutex_lock(®ulator_list_mutex);
1852 list_del(&rdev->list);
1854 sysfs_remove_link(&rdev->dev.kobj, "supply");
1855 device_unregister(&rdev->dev);
1856 mutex_unlock(®ulator_list_mutex);
1858 EXPORT_SYMBOL_GPL(regulator_unregister);
1861 * regulator_suspend_prepare: prepare regulators for system wide suspend
1862 * @state: system suspend state
1864 * Configure each regulator with it's suspend operating parameters for state.
1865 * This will usually be called by machine suspend code prior to supending.
1867 int regulator_suspend_prepare(suspend_state_t state)
1869 struct regulator_dev *rdev;
1872 /* ON is handled by regulator active state */
1873 if (state == PM_SUSPEND_ON)
1876 mutex_lock(®ulator_list_mutex);
1877 list_for_each_entry(rdev, ®ulator_list, list) {
1879 mutex_lock(&rdev->mutex);
1880 ret = suspend_prepare(rdev, state);
1881 mutex_unlock(&rdev->mutex);
1884 printk(KERN_ERR "%s: failed to prepare %s\n",
1885 __func__, rdev->desc->name);
1890 mutex_unlock(®ulator_list_mutex);
1893 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1896 * rdev_get_drvdata - get rdev regulator driver data
1897 * @regulator: regulator
1899 * Get rdev regulator driver private data. This call can be used in the
1900 * regulator driver context.
1902 void *rdev_get_drvdata(struct regulator_dev *rdev)
1904 return rdev->reg_data;
1906 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1909 * regulator_get_drvdata - get regulator driver data
1910 * @regulator: regulator
1912 * Get regulator driver private data. This call can be used in the consumer
1913 * driver context when non API regulator specific functions need to be called.
1915 void *regulator_get_drvdata(struct regulator *regulator)
1917 return regulator->rdev->reg_data;
1919 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1922 * regulator_set_drvdata - set regulator driver data
1923 * @regulator: regulator
1926 void regulator_set_drvdata(struct regulator *regulator, void *data)
1928 regulator->rdev->reg_data = data;
1930 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
1933 * regulator_get_id - get regulator ID
1934 * @regulator: regulator
1936 int rdev_get_id(struct regulator_dev *rdev)
1938 return rdev->desc->id;
1940 EXPORT_SYMBOL_GPL(rdev_get_id);
1942 struct device *rdev_get_dev(struct regulator_dev *rdev)
1946 EXPORT_SYMBOL_GPL(rdev_get_dev);
1948 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
1950 return reg_init_data->driver_data;
1952 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
1954 static int __init regulator_init(void)
1956 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
1957 return class_register(®ulator_class);
1960 /* init early to allow our consumers to complete system booting */
1961 core_initcall(regulator_init);