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);
245 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
247 static ssize_t regulator_uA_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
250 struct regulator_dev *rdev = dev_get_drvdata(dev);
252 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
254 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
256 static ssize_t regulator_name_show(struct device *dev,
257 struct device_attribute *attr, char *buf)
259 struct regulator_dev *rdev = dev_get_drvdata(dev);
262 if (rdev->constraints->name)
263 name = rdev->constraints->name;
264 else if (rdev->desc->name)
265 name = rdev->desc->name;
269 return sprintf(buf, "%s\n", name);
272 static ssize_t regulator_print_opmode(char *buf, int mode)
275 case REGULATOR_MODE_FAST:
276 return sprintf(buf, "fast\n");
277 case REGULATOR_MODE_NORMAL:
278 return sprintf(buf, "normal\n");
279 case REGULATOR_MODE_IDLE:
280 return sprintf(buf, "idle\n");
281 case REGULATOR_MODE_STANDBY:
282 return sprintf(buf, "standby\n");
284 return sprintf(buf, "unknown\n");
287 static ssize_t regulator_opmode_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
290 struct regulator_dev *rdev = dev_get_drvdata(dev);
292 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
294 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
296 static ssize_t regulator_print_state(char *buf, int state)
299 return sprintf(buf, "enabled\n");
301 return sprintf(buf, "disabled\n");
303 return sprintf(buf, "unknown\n");
306 static ssize_t regulator_state_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 struct regulator_dev *rdev = dev_get_drvdata(dev);
311 return regulator_print_state(buf, _regulator_is_enabled(rdev));
313 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
315 static ssize_t regulator_min_uA_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct regulator_dev *rdev = dev_get_drvdata(dev);
320 if (!rdev->constraints)
321 return sprintf(buf, "constraint not defined\n");
323 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
325 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
327 static ssize_t regulator_max_uA_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
330 struct regulator_dev *rdev = dev_get_drvdata(dev);
332 if (!rdev->constraints)
333 return sprintf(buf, "constraint not defined\n");
335 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
337 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
339 static ssize_t regulator_min_uV_show(struct device *dev,
340 struct device_attribute *attr, char *buf)
342 struct regulator_dev *rdev = dev_get_drvdata(dev);
344 if (!rdev->constraints)
345 return sprintf(buf, "constraint not defined\n");
347 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
349 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
351 static ssize_t regulator_max_uV_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
354 struct regulator_dev *rdev = dev_get_drvdata(dev);
356 if (!rdev->constraints)
357 return sprintf(buf, "constraint not defined\n");
359 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
361 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
363 static ssize_t regulator_total_uA_show(struct device *dev,
364 struct device_attribute *attr, char *buf)
366 struct regulator_dev *rdev = dev_get_drvdata(dev);
367 struct regulator *regulator;
370 mutex_lock(&rdev->mutex);
371 list_for_each_entry(regulator, &rdev->consumer_list, list)
372 uA += regulator->uA_load;
373 mutex_unlock(&rdev->mutex);
374 return sprintf(buf, "%d\n", uA);
376 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
378 static ssize_t regulator_num_users_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 struct regulator_dev *rdev = dev_get_drvdata(dev);
382 return sprintf(buf, "%d\n", rdev->use_count);
385 static ssize_t regulator_type_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
388 struct regulator_dev *rdev = dev_get_drvdata(dev);
390 switch (rdev->desc->type) {
391 case REGULATOR_VOLTAGE:
392 return sprintf(buf, "voltage\n");
393 case REGULATOR_CURRENT:
394 return sprintf(buf, "current\n");
396 return sprintf(buf, "unknown\n");
399 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct regulator_dev *rdev = dev_get_drvdata(dev);
404 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
406 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
407 regulator_suspend_mem_uV_show, NULL);
409 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
412 struct regulator_dev *rdev = dev_get_drvdata(dev);
414 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
416 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
417 regulator_suspend_disk_uV_show, NULL);
419 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
422 struct regulator_dev *rdev = dev_get_drvdata(dev);
424 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
426 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
427 regulator_suspend_standby_uV_show, NULL);
429 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 return regulator_print_opmode(buf,
435 rdev->constraints->state_mem.mode);
437 static DEVICE_ATTR(suspend_mem_mode, 0444,
438 regulator_suspend_mem_mode_show, NULL);
440 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct regulator_dev *rdev = dev_get_drvdata(dev);
445 return regulator_print_opmode(buf,
446 rdev->constraints->state_disk.mode);
448 static DEVICE_ATTR(suspend_disk_mode, 0444,
449 regulator_suspend_disk_mode_show, NULL);
451 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
454 struct regulator_dev *rdev = dev_get_drvdata(dev);
456 return regulator_print_opmode(buf,
457 rdev->constraints->state_standby.mode);
459 static DEVICE_ATTR(suspend_standby_mode, 0444,
460 regulator_suspend_standby_mode_show, NULL);
462 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
465 struct regulator_dev *rdev = dev_get_drvdata(dev);
467 return regulator_print_state(buf,
468 rdev->constraints->state_mem.enabled);
470 static DEVICE_ATTR(suspend_mem_state, 0444,
471 regulator_suspend_mem_state_show, NULL);
473 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
476 struct regulator_dev *rdev = dev_get_drvdata(dev);
478 return regulator_print_state(buf,
479 rdev->constraints->state_disk.enabled);
481 static DEVICE_ATTR(suspend_disk_state, 0444,
482 regulator_suspend_disk_state_show, NULL);
484 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
487 struct regulator_dev *rdev = dev_get_drvdata(dev);
489 return regulator_print_state(buf,
490 rdev->constraints->state_standby.enabled);
492 static DEVICE_ATTR(suspend_standby_state, 0444,
493 regulator_suspend_standby_state_show, NULL);
497 * These are the only attributes are present for all regulators.
498 * Other attributes are a function of regulator functionality.
500 static struct device_attribute regulator_dev_attrs[] = {
501 __ATTR(name, 0444, regulator_name_show, NULL),
502 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
503 __ATTR(type, 0444, regulator_type_show, NULL),
507 static void regulator_dev_release(struct device *dev)
509 struct regulator_dev *rdev = dev_get_drvdata(dev);
513 static struct class regulator_class = {
515 .dev_release = regulator_dev_release,
516 .dev_attrs = regulator_dev_attrs,
519 /* Calculate the new optimum regulator operating mode based on the new total
520 * consumer load. All locks held by caller */
521 static void drms_uA_update(struct regulator_dev *rdev)
523 struct regulator *sibling;
524 int current_uA = 0, output_uV, input_uV, err;
527 err = regulator_check_drms(rdev);
528 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
529 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
532 /* get output voltage */
533 output_uV = rdev->desc->ops->get_voltage(rdev);
537 /* get input voltage */
538 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
539 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
541 input_uV = rdev->constraints->input_uV;
545 /* calc total requested load */
546 list_for_each_entry(sibling, &rdev->consumer_list, list)
547 current_uA += sibling->uA_load;
549 /* now get the optimum mode for our new total regulator load */
550 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
551 output_uV, current_uA);
553 /* check the new mode is allowed */
554 err = regulator_check_mode(rdev, mode);
556 rdev->desc->ops->set_mode(rdev, mode);
559 static int suspend_set_state(struct regulator_dev *rdev,
560 struct regulator_state *rstate)
564 /* enable & disable are mandatory for suspend control */
565 if (!rdev->desc->ops->set_suspend_enable ||
566 !rdev->desc->ops->set_suspend_disable) {
567 printk(KERN_ERR "%s: no way to set suspend state\n",
573 ret = rdev->desc->ops->set_suspend_enable(rdev);
575 ret = rdev->desc->ops->set_suspend_disable(rdev);
577 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
581 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
582 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
584 printk(KERN_ERR "%s: failed to set voltage\n",
590 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
591 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
593 printk(KERN_ERR "%s: failed to set mode\n", __func__);
600 /* locks held by caller */
601 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
603 if (!rdev->constraints)
607 case PM_SUSPEND_STANDBY:
608 return suspend_set_state(rdev,
609 &rdev->constraints->state_standby);
611 return suspend_set_state(rdev,
612 &rdev->constraints->state_mem);
614 return suspend_set_state(rdev,
615 &rdev->constraints->state_disk);
621 static void print_constraints(struct regulator_dev *rdev)
623 struct regulation_constraints *constraints = rdev->constraints;
627 if (rdev->desc->type == REGULATOR_VOLTAGE) {
628 if (constraints->min_uV == constraints->max_uV)
629 count = sprintf(buf, "%d mV ",
630 constraints->min_uV / 1000);
632 count = sprintf(buf, "%d <--> %d mV ",
633 constraints->min_uV / 1000,
634 constraints->max_uV / 1000);
636 if (constraints->min_uA == constraints->max_uA)
637 count = sprintf(buf, "%d mA ",
638 constraints->min_uA / 1000);
640 count = sprintf(buf, "%d <--> %d mA ",
641 constraints->min_uA / 1000,
642 constraints->max_uA / 1000);
644 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
645 count += sprintf(buf + count, "fast ");
646 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
647 count += sprintf(buf + count, "normal ");
648 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
649 count += sprintf(buf + count, "idle ");
650 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
651 count += sprintf(buf + count, "standby");
653 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
657 * set_machine_constraints - sets regulator constraints
658 * @rdev: regulator source
659 * @constraints: constraints to apply
661 * Allows platform initialisation code to define and constrain
662 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
663 * Constraints *must* be set by platform code in order for some
664 * regulator operations to proceed i.e. set_voltage, set_current_limit,
667 static int set_machine_constraints(struct regulator_dev *rdev,
668 struct regulation_constraints *constraints)
672 struct regulator_ops *ops = rdev->desc->ops;
674 if (constraints->name)
675 name = constraints->name;
676 else if (rdev->desc->name)
677 name = rdev->desc->name;
681 rdev->constraints = constraints;
683 /* do we need to apply the constraint voltage */
684 if (rdev->constraints->apply_uV &&
685 rdev->constraints->min_uV == rdev->constraints->max_uV &&
687 ret = ops->set_voltage(rdev,
688 rdev->constraints->min_uV, rdev->constraints->max_uV);
690 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
692 rdev->constraints->min_uV, name);
693 rdev->constraints = NULL;
698 /* are we enabled at boot time by firmware / bootloader */
699 if (rdev->constraints->boot_on)
702 /* do we need to setup our suspend state */
703 if (constraints->initial_state) {
704 ret = suspend_prepare(rdev, constraints->initial_state);
706 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
708 rdev->constraints = NULL;
713 /* if always_on is set then turn the regulator on if it's not
715 if (constraints->always_on && ops->enable &&
716 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
717 (!ops->is_enabled && !constraints->boot_on))) {
718 ret = ops->enable(rdev);
720 printk(KERN_ERR "%s: failed to enable %s\n",
722 rdev->constraints = NULL;
727 print_constraints(rdev);
733 * set_supply - set regulator supply regulator
734 * @rdev: regulator name
735 * @supply_rdev: supply regulator name
737 * Called by platform initialisation code to set the supply regulator for this
738 * regulator. This ensures that a regulators supply will also be enabled by the
739 * core if it's child is enabled.
741 static int set_supply(struct regulator_dev *rdev,
742 struct regulator_dev *supply_rdev)
746 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
750 "%s: could not add device link %s err %d\n",
751 __func__, supply_rdev->dev.kobj.name, err);
754 rdev->supply = supply_rdev;
755 list_add(&rdev->slist, &supply_rdev->supply_list);
761 * set_consumer_device_supply: Bind a regulator to a symbolic supply
762 * @rdev: regulator source
763 * @consumer_dev: device the supply applies to
764 * @supply: symbolic name for supply
766 * Allows platform initialisation code to map physical regulator
767 * sources to symbolic names for supplies for use by devices. Devices
768 * should use these symbolic names to request regulators, avoiding the
769 * need to provide board-specific regulator names as platform data.
771 static int set_consumer_device_supply(struct regulator_dev *rdev,
772 struct device *consumer_dev, const char *supply)
774 struct regulator_map *node;
779 list_for_each_entry(node, ®ulator_map_list, list) {
780 if (consumer_dev != node->dev)
782 if (strcmp(node->supply, supply) != 0)
785 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
786 dev_name(&node->regulator->dev),
787 node->regulator->desc->name,
789 dev_name(&rdev->dev), rdev->desc->name);
793 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
797 node->regulator = rdev;
798 node->dev = consumer_dev;
799 node->supply = supply;
801 list_add(&node->list, ®ulator_map_list);
805 static void unset_consumer_device_supply(struct regulator_dev *rdev,
806 struct device *consumer_dev)
808 struct regulator_map *node, *n;
810 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
811 if (rdev == node->regulator &&
812 consumer_dev == node->dev) {
813 list_del(&node->list);
820 #define REG_STR_SIZE 32
822 static struct regulator *create_regulator(struct regulator_dev *rdev,
824 const char *supply_name)
826 struct regulator *regulator;
827 char buf[REG_STR_SIZE];
830 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
831 if (regulator == NULL)
834 mutex_lock(&rdev->mutex);
835 regulator->rdev = rdev;
836 list_add(®ulator->list, &rdev->consumer_list);
839 /* create a 'requested_microamps_name' sysfs entry */
840 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
842 if (size >= REG_STR_SIZE)
845 regulator->dev = dev;
846 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
847 if (regulator->dev_attr.attr.name == NULL)
850 regulator->dev_attr.attr.owner = THIS_MODULE;
851 regulator->dev_attr.attr.mode = 0444;
852 regulator->dev_attr.show = device_requested_uA_show;
853 err = device_create_file(dev, ®ulator->dev_attr);
855 printk(KERN_WARNING "%s: could not add regulator_dev"
856 " load sysfs\n", __func__);
860 /* also add a link to the device sysfs entry */
861 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
862 dev->kobj.name, supply_name);
863 if (size >= REG_STR_SIZE)
866 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
867 if (regulator->supply_name == NULL)
870 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
874 "%s: could not add device link %s err %d\n",
875 __func__, dev->kobj.name, err);
876 device_remove_file(dev, ®ulator->dev_attr);
880 mutex_unlock(&rdev->mutex);
883 kfree(regulator->supply_name);
885 device_remove_file(regulator->dev, ®ulator->dev_attr);
887 kfree(regulator->dev_attr.attr.name);
889 list_del(®ulator->list);
891 mutex_unlock(&rdev->mutex);
896 * regulator_get - lookup and obtain a reference to a regulator.
897 * @dev: device for regulator "consumer"
898 * @id: Supply name or regulator ID.
900 * Returns a struct regulator corresponding to the regulator producer,
901 * or IS_ERR() condition containing errno. Use of supply names
902 * configured via regulator_set_device_supply() is strongly
905 struct regulator *regulator_get(struct device *dev, const char *id)
907 struct regulator_dev *rdev;
908 struct regulator_map *map;
909 struct regulator *regulator = ERR_PTR(-ENODEV);
912 printk(KERN_ERR "regulator: get() with no identifier\n");
916 mutex_lock(®ulator_list_mutex);
918 list_for_each_entry(map, ®ulator_map_list, list) {
919 if (dev == map->dev &&
920 strcmp(map->supply, id) == 0) {
921 rdev = map->regulator;
925 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
927 mutex_unlock(®ulator_list_mutex);
931 if (!try_module_get(rdev->owner))
934 regulator = create_regulator(rdev, dev, id);
935 if (regulator == NULL) {
936 regulator = ERR_PTR(-ENOMEM);
937 module_put(rdev->owner);
941 mutex_unlock(®ulator_list_mutex);
944 EXPORT_SYMBOL_GPL(regulator_get);
947 * regulator_put - "free" the regulator source
948 * @regulator: regulator source
950 * Note: drivers must ensure that all regulator_enable calls made on this
951 * regulator source are balanced by regulator_disable calls prior to calling
954 void regulator_put(struct regulator *regulator)
956 struct regulator_dev *rdev;
958 if (regulator == NULL || IS_ERR(regulator))
961 mutex_lock(®ulator_list_mutex);
962 rdev = regulator->rdev;
964 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
965 regulator->supply_name))
966 _regulator_disable(rdev);
968 /* remove any sysfs entries */
969 if (regulator->dev) {
970 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
971 kfree(regulator->supply_name);
972 device_remove_file(regulator->dev, ®ulator->dev_attr);
973 kfree(regulator->dev_attr.attr.name);
975 list_del(®ulator->list);
978 module_put(rdev->owner);
979 mutex_unlock(®ulator_list_mutex);
981 EXPORT_SYMBOL_GPL(regulator_put);
983 /* locks held by regulator_enable() */
984 static int _regulator_enable(struct regulator_dev *rdev)
988 if (!rdev->constraints) {
989 printk(KERN_ERR "%s: %s has no constraints\n",
990 __func__, rdev->desc->name);
994 /* do we need to enable the supply regulator first */
996 ret = _regulator_enable(rdev->supply);
998 printk(KERN_ERR "%s: failed to enable %s: %d\n",
999 __func__, rdev->desc->name, ret);
1004 /* check voltage and requested load before enabling */
1005 if (rdev->desc->ops->enable) {
1007 if (rdev->constraints &&
1008 (rdev->constraints->valid_ops_mask &
1009 REGULATOR_CHANGE_DRMS))
1010 drms_uA_update(rdev);
1012 ret = rdev->desc->ops->enable(rdev);
1014 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1015 __func__, rdev->desc->name, ret);
1026 * regulator_enable - enable regulator output
1027 * @regulator: regulator source
1029 * Request that the regulator be enabled with the regulator output at
1030 * the predefined voltage or current value. Calls to regulator_enable()
1031 * must be balanced with calls to regulator_disable().
1033 * NOTE: the output value can be set by other drivers, boot loader or may be
1034 * hardwired in the regulator.
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. Calls to
1093 * regulator_enable() must be balanced with calls to
1094 * regulator_disable().
1096 * NOTE: this will only disable the regulator output if no other consumer
1097 * devices have it enabled, the regulator device supports disabling and
1098 * machine constraints permit this operation.
1100 int regulator_disable(struct regulator *regulator)
1102 struct regulator_dev *rdev = regulator->rdev;
1105 mutex_lock(&rdev->mutex);
1106 if (regulator->enabled == 1) {
1107 ret = _regulator_disable(rdev);
1109 regulator->uA_load = 0;
1110 } else if (WARN(regulator->enabled <= 0,
1111 "unbalanced disables for supply %s\n",
1112 regulator->supply_name))
1115 regulator->enabled--;
1116 mutex_unlock(&rdev->mutex);
1119 EXPORT_SYMBOL_GPL(regulator_disable);
1121 /* locks held by regulator_force_disable() */
1122 static int _regulator_force_disable(struct regulator_dev *rdev)
1127 if (rdev->desc->ops->disable) {
1128 /* ah well, who wants to live forever... */
1129 ret = rdev->desc->ops->disable(rdev);
1131 printk(KERN_ERR "%s: failed to force disable %s\n",
1132 __func__, rdev->desc->name);
1135 /* notify other consumers that power has been forced off */
1136 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1140 /* decrease our supplies ref count and disable if required */
1142 _regulator_disable(rdev->supply);
1144 rdev->use_count = 0;
1149 * regulator_force_disable - force disable regulator output
1150 * @regulator: regulator source
1152 * Forcibly disable the regulator output voltage or current.
1153 * NOTE: this *will* disable the regulator output even if other consumer
1154 * devices have it enabled. This should be used for situations when device
1155 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1157 int regulator_force_disable(struct regulator *regulator)
1161 mutex_lock(®ulator->rdev->mutex);
1162 regulator->enabled = 0;
1163 regulator->uA_load = 0;
1164 ret = _regulator_force_disable(regulator->rdev);
1165 mutex_unlock(®ulator->rdev->mutex);
1168 EXPORT_SYMBOL_GPL(regulator_force_disable);
1170 static int _regulator_is_enabled(struct regulator_dev *rdev)
1174 mutex_lock(&rdev->mutex);
1177 if (!rdev->desc->ops->is_enabled) {
1182 ret = rdev->desc->ops->is_enabled(rdev);
1184 mutex_unlock(&rdev->mutex);
1189 * regulator_is_enabled - is the regulator output enabled
1190 * @regulator: regulator source
1192 * Returns positive if the regulator driver backing the source/client
1193 * has requested that the device be enabled, zero if it hasn't, else a
1194 * negative errno code.
1196 * Note that the device backing this regulator handle can have multiple
1197 * users, so it might be enabled even if regulator_enable() was never
1198 * called for this particular source.
1200 int regulator_is_enabled(struct regulator *regulator)
1202 return _regulator_is_enabled(regulator->rdev);
1204 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1207 * regulator_set_voltage - set regulator output voltage
1208 * @regulator: regulator source
1209 * @min_uV: Minimum required voltage in uV
1210 * @max_uV: Maximum acceptable voltage in uV
1212 * Sets a voltage regulator to the desired output voltage. This can be set
1213 * during any regulator state. IOW, regulator can be disabled or enabled.
1215 * If the regulator is enabled then the voltage will change to the new value
1216 * immediately otherwise if the regulator is disabled the regulator will
1217 * output at the new voltage when enabled.
1219 * NOTE: If the regulator is shared between several devices then the lowest
1220 * request voltage that meets the system constraints will be used.
1221 * Regulator system constraints must be set for this regulator before
1222 * calling this function otherwise this call will fail.
1224 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1226 struct regulator_dev *rdev = regulator->rdev;
1229 mutex_lock(&rdev->mutex);
1232 if (!rdev->desc->ops->set_voltage) {
1237 /* constraints check */
1238 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1241 regulator->min_uV = min_uV;
1242 regulator->max_uV = max_uV;
1243 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1246 mutex_unlock(&rdev->mutex);
1249 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1251 static int _regulator_get_voltage(struct regulator_dev *rdev)
1254 if (rdev->desc->ops->get_voltage)
1255 return rdev->desc->ops->get_voltage(rdev);
1261 * regulator_get_voltage - get regulator output voltage
1262 * @regulator: regulator source
1264 * This returns the current regulator voltage in uV.
1266 * NOTE: If the regulator is disabled it will return the voltage value. This
1267 * function should not be used to determine regulator state.
1269 int regulator_get_voltage(struct regulator *regulator)
1273 mutex_lock(®ulator->rdev->mutex);
1275 ret = _regulator_get_voltage(regulator->rdev);
1277 mutex_unlock(®ulator->rdev->mutex);
1281 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1284 * regulator_set_current_limit - set regulator output current limit
1285 * @regulator: regulator source
1286 * @min_uA: Minimuum supported current in uA
1287 * @max_uA: Maximum supported current in uA
1289 * Sets current sink to the desired output current. This can be set during
1290 * any regulator state. IOW, regulator can be disabled or enabled.
1292 * If the regulator is enabled then the current will change to the new value
1293 * immediately otherwise if the regulator is disabled the regulator will
1294 * output at the new current when enabled.
1296 * NOTE: Regulator system constraints must be set for this regulator before
1297 * calling this function otherwise this call will fail.
1299 int regulator_set_current_limit(struct regulator *regulator,
1300 int min_uA, int max_uA)
1302 struct regulator_dev *rdev = regulator->rdev;
1305 mutex_lock(&rdev->mutex);
1308 if (!rdev->desc->ops->set_current_limit) {
1313 /* constraints check */
1314 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1318 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1320 mutex_unlock(&rdev->mutex);
1323 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1325 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1329 mutex_lock(&rdev->mutex);
1332 if (!rdev->desc->ops->get_current_limit) {
1337 ret = rdev->desc->ops->get_current_limit(rdev);
1339 mutex_unlock(&rdev->mutex);
1344 * regulator_get_current_limit - get regulator output current
1345 * @regulator: regulator source
1347 * This returns the current supplied by the specified current sink in uA.
1349 * NOTE: If the regulator is disabled it will return the current value. This
1350 * function should not be used to determine regulator state.
1352 int regulator_get_current_limit(struct regulator *regulator)
1354 return _regulator_get_current_limit(regulator->rdev);
1356 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1359 * regulator_set_mode - set regulator operating mode
1360 * @regulator: regulator source
1361 * @mode: operating mode - one of the REGULATOR_MODE constants
1363 * Set regulator operating mode to increase regulator efficiency or improve
1364 * regulation performance.
1366 * NOTE: Regulator system constraints must be set for this regulator before
1367 * calling this function otherwise this call will fail.
1369 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1371 struct regulator_dev *rdev = regulator->rdev;
1374 mutex_lock(&rdev->mutex);
1377 if (!rdev->desc->ops->set_mode) {
1382 /* constraints check */
1383 ret = regulator_check_mode(rdev, mode);
1387 ret = rdev->desc->ops->set_mode(rdev, mode);
1389 mutex_unlock(&rdev->mutex);
1392 EXPORT_SYMBOL_GPL(regulator_set_mode);
1394 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1398 mutex_lock(&rdev->mutex);
1401 if (!rdev->desc->ops->get_mode) {
1406 ret = rdev->desc->ops->get_mode(rdev);
1408 mutex_unlock(&rdev->mutex);
1413 * regulator_get_mode - get regulator operating mode
1414 * @regulator: regulator source
1416 * Get the current regulator operating mode.
1418 unsigned int regulator_get_mode(struct regulator *regulator)
1420 return _regulator_get_mode(regulator->rdev);
1422 EXPORT_SYMBOL_GPL(regulator_get_mode);
1425 * regulator_set_optimum_mode - set regulator optimum operating mode
1426 * @regulator: regulator source
1427 * @uA_load: load current
1429 * Notifies the regulator core of a new device load. This is then used by
1430 * DRMS (if enabled by constraints) to set the most efficient regulator
1431 * operating mode for the new regulator loading.
1433 * Consumer devices notify their supply regulator of the maximum power
1434 * they will require (can be taken from device datasheet in the power
1435 * consumption tables) when they change operational status and hence power
1436 * state. Examples of operational state changes that can affect power
1437 * consumption are :-
1439 * o Device is opened / closed.
1440 * o Device I/O is about to begin or has just finished.
1441 * o Device is idling in between work.
1443 * This information is also exported via sysfs to userspace.
1445 * DRMS will sum the total requested load on the regulator and change
1446 * to the most efficient operating mode if platform constraints allow.
1448 * Returns the new regulator mode or error.
1450 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1452 struct regulator_dev *rdev = regulator->rdev;
1453 struct regulator *consumer;
1454 int ret, output_uV, input_uV, total_uA_load = 0;
1457 mutex_lock(&rdev->mutex);
1459 regulator->uA_load = uA_load;
1460 ret = regulator_check_drms(rdev);
1466 if (!rdev->desc->ops->get_optimum_mode)
1469 /* get output voltage */
1470 output_uV = rdev->desc->ops->get_voltage(rdev);
1471 if (output_uV <= 0) {
1472 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1473 __func__, rdev->desc->name);
1477 /* get input voltage */
1478 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1479 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1481 input_uV = rdev->constraints->input_uV;
1482 if (input_uV <= 0) {
1483 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1484 __func__, rdev->desc->name);
1488 /* calc total requested load for this regulator */
1489 list_for_each_entry(consumer, &rdev->consumer_list, list)
1490 total_uA_load += consumer->uA_load;
1492 mode = rdev->desc->ops->get_optimum_mode(rdev,
1493 input_uV, output_uV,
1495 ret = regulator_check_mode(rdev, mode);
1497 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1498 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1499 total_uA_load, input_uV, output_uV);
1503 ret = rdev->desc->ops->set_mode(rdev, mode);
1505 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1506 __func__, mode, rdev->desc->name);
1511 mutex_unlock(&rdev->mutex);
1514 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1517 * regulator_register_notifier - register regulator event notifier
1518 * @regulator: regulator source
1519 * @nb: notifier block
1521 * Register notifier block to receive regulator events.
1523 int regulator_register_notifier(struct regulator *regulator,
1524 struct notifier_block *nb)
1526 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1529 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1532 * regulator_unregister_notifier - unregister regulator event notifier
1533 * @regulator: regulator source
1534 * @nb: notifier block
1536 * Unregister regulator event notifier block.
1538 int regulator_unregister_notifier(struct regulator *regulator,
1539 struct notifier_block *nb)
1541 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1544 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1546 /* notify regulator consumers and downstream regulator consumers */
1547 static void _notifier_call_chain(struct regulator_dev *rdev,
1548 unsigned long event, void *data)
1550 struct regulator_dev *_rdev;
1552 /* call rdev chain first */
1553 mutex_lock(&rdev->mutex);
1554 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1555 mutex_unlock(&rdev->mutex);
1557 /* now notify regulator we supply */
1558 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1559 _notifier_call_chain(_rdev, event, data);
1563 * regulator_bulk_get - get multiple regulator consumers
1565 * @dev: Device to supply
1566 * @num_consumers: Number of consumers to register
1567 * @consumers: Configuration of consumers; clients are stored here.
1569 * @return 0 on success, an errno on failure.
1571 * This helper function allows drivers to get several regulator
1572 * consumers in one operation. If any of the regulators cannot be
1573 * acquired then any regulators that were allocated will be freed
1574 * before returning to the caller.
1576 int regulator_bulk_get(struct device *dev, int num_consumers,
1577 struct regulator_bulk_data *consumers)
1582 for (i = 0; i < num_consumers; i++)
1583 consumers[i].consumer = NULL;
1585 for (i = 0; i < num_consumers; i++) {
1586 consumers[i].consumer = regulator_get(dev,
1587 consumers[i].supply);
1588 if (IS_ERR(consumers[i].consumer)) {
1589 dev_err(dev, "Failed to get supply '%s'\n",
1590 consumers[i].supply);
1591 ret = PTR_ERR(consumers[i].consumer);
1592 consumers[i].consumer = NULL;
1600 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1601 regulator_put(consumers[i].consumer);
1605 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1608 * regulator_bulk_enable - enable multiple regulator consumers
1610 * @num_consumers: Number of consumers
1611 * @consumers: Consumer data; clients are stored here.
1612 * @return 0 on success, an errno on failure
1614 * This convenience API allows consumers to enable multiple regulator
1615 * clients in a single API call. If any consumers cannot be enabled
1616 * then any others that were enabled will be disabled again prior to
1619 int regulator_bulk_enable(int num_consumers,
1620 struct regulator_bulk_data *consumers)
1625 for (i = 0; i < num_consumers; i++) {
1626 ret = regulator_enable(consumers[i].consumer);
1634 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1635 for (i = 0; i < num_consumers; i++)
1636 regulator_disable(consumers[i].consumer);
1640 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1643 * regulator_bulk_disable - disable multiple regulator consumers
1645 * @num_consumers: Number of consumers
1646 * @consumers: Consumer data; clients are stored here.
1647 * @return 0 on success, an errno on failure
1649 * This convenience API allows consumers to disable multiple regulator
1650 * clients in a single API call. If any consumers cannot be enabled
1651 * then any others that were disabled will be disabled again prior to
1654 int regulator_bulk_disable(int num_consumers,
1655 struct regulator_bulk_data *consumers)
1660 for (i = 0; i < num_consumers; i++) {
1661 ret = regulator_disable(consumers[i].consumer);
1669 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1670 for (i = 0; i < num_consumers; i++)
1671 regulator_enable(consumers[i].consumer);
1675 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1678 * regulator_bulk_free - free multiple regulator consumers
1680 * @num_consumers: Number of consumers
1681 * @consumers: Consumer data; clients are stored here.
1683 * This convenience API allows consumers to free multiple regulator
1684 * clients in a single API call.
1686 void regulator_bulk_free(int num_consumers,
1687 struct regulator_bulk_data *consumers)
1691 for (i = 0; i < num_consumers; i++) {
1692 regulator_put(consumers[i].consumer);
1693 consumers[i].consumer = NULL;
1696 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1699 * regulator_notifier_call_chain - call regulator event notifier
1700 * @rdev: regulator source
1701 * @event: notifier block
1702 * @data: callback-specific data.
1704 * Called by regulator drivers to notify clients a regulator event has
1705 * occurred. We also notify regulator clients downstream.
1707 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1708 unsigned long event, void *data)
1710 _notifier_call_chain(rdev, event, data);
1714 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1717 * To avoid cluttering sysfs (and memory) with useless state, only
1718 * create attributes that can be meaningfully displayed.
1720 static int add_regulator_attributes(struct regulator_dev *rdev)
1722 struct device *dev = &rdev->dev;
1723 struct regulator_ops *ops = rdev->desc->ops;
1726 /* some attributes need specific methods to be displayed */
1727 if (ops->get_voltage) {
1728 status = device_create_file(dev, &dev_attr_microvolts);
1732 if (ops->get_current_limit) {
1733 status = device_create_file(dev, &dev_attr_microamps);
1737 if (ops->get_mode) {
1738 status = device_create_file(dev, &dev_attr_opmode);
1742 if (ops->is_enabled) {
1743 status = device_create_file(dev, &dev_attr_state);
1748 /* some attributes are type-specific */
1749 if (rdev->desc->type == REGULATOR_CURRENT) {
1750 status = device_create_file(dev, &dev_attr_requested_microamps);
1755 /* all the other attributes exist to support constraints;
1756 * don't show them if there are no constraints, or if the
1757 * relevant supporting methods are missing.
1759 if (!rdev->constraints)
1762 /* constraints need specific supporting methods */
1763 if (ops->set_voltage) {
1764 status = device_create_file(dev, &dev_attr_min_microvolts);
1767 status = device_create_file(dev, &dev_attr_max_microvolts);
1771 if (ops->set_current_limit) {
1772 status = device_create_file(dev, &dev_attr_min_microamps);
1775 status = device_create_file(dev, &dev_attr_max_microamps);
1780 /* suspend mode constraints need multiple supporting methods */
1781 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1784 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1787 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1790 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1794 if (ops->set_suspend_voltage) {
1795 status = device_create_file(dev,
1796 &dev_attr_suspend_standby_microvolts);
1799 status = device_create_file(dev,
1800 &dev_attr_suspend_mem_microvolts);
1803 status = device_create_file(dev,
1804 &dev_attr_suspend_disk_microvolts);
1809 if (ops->set_suspend_mode) {
1810 status = device_create_file(dev,
1811 &dev_attr_suspend_standby_mode);
1814 status = device_create_file(dev,
1815 &dev_attr_suspend_mem_mode);
1818 status = device_create_file(dev,
1819 &dev_attr_suspend_disk_mode);
1828 * regulator_register - register regulator
1829 * @regulator_desc: regulator to register
1830 * @dev: struct device for the regulator
1831 * @driver_data: private regulator data
1833 * Called by regulator drivers to register a regulator.
1834 * Returns 0 on success.
1836 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1837 struct device *dev, void *driver_data)
1839 static atomic_t regulator_no = ATOMIC_INIT(0);
1840 struct regulator_dev *rdev;
1841 struct regulator_init_data *init_data = dev->platform_data;
1844 if (regulator_desc == NULL)
1845 return ERR_PTR(-EINVAL);
1847 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1848 return ERR_PTR(-EINVAL);
1850 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1851 !regulator_desc->type == REGULATOR_CURRENT)
1852 return ERR_PTR(-EINVAL);
1855 return ERR_PTR(-EINVAL);
1857 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1859 return ERR_PTR(-ENOMEM);
1861 mutex_lock(®ulator_list_mutex);
1863 mutex_init(&rdev->mutex);
1864 rdev->reg_data = driver_data;
1865 rdev->owner = regulator_desc->owner;
1866 rdev->desc = regulator_desc;
1867 INIT_LIST_HEAD(&rdev->consumer_list);
1868 INIT_LIST_HEAD(&rdev->supply_list);
1869 INIT_LIST_HEAD(&rdev->list);
1870 INIT_LIST_HEAD(&rdev->slist);
1871 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1873 /* preform any regulator specific init */
1874 if (init_data->regulator_init) {
1875 ret = init_data->regulator_init(rdev->reg_data);
1880 /* register with sysfs */
1881 rdev->dev.class = ®ulator_class;
1882 rdev->dev.parent = dev;
1883 dev_set_name(&rdev->dev, "regulator.%d",
1884 atomic_inc_return(®ulator_no) - 1);
1885 ret = device_register(&rdev->dev);
1889 dev_set_drvdata(&rdev->dev, rdev);
1891 /* set regulator constraints */
1892 ret = set_machine_constraints(rdev, &init_data->constraints);
1896 /* add attributes supported by this regulator */
1897 ret = add_regulator_attributes(rdev);
1901 /* set supply regulator if it exists */
1902 if (init_data->supply_regulator_dev) {
1903 ret = set_supply(rdev,
1904 dev_get_drvdata(init_data->supply_regulator_dev));
1909 /* add consumers devices */
1910 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1911 ret = set_consumer_device_supply(rdev,
1912 init_data->consumer_supplies[i].dev,
1913 init_data->consumer_supplies[i].supply);
1915 for (--i; i >= 0; i--)
1916 unset_consumer_device_supply(rdev,
1917 init_data->consumer_supplies[i].dev);
1922 list_add(&rdev->list, ®ulator_list);
1924 mutex_unlock(®ulator_list_mutex);
1928 device_unregister(&rdev->dev);
1931 rdev = ERR_PTR(ret);
1934 EXPORT_SYMBOL_GPL(regulator_register);
1937 * regulator_unregister - unregister regulator
1938 * @rdev: regulator to unregister
1940 * Called by regulator drivers to unregister a regulator.
1942 void regulator_unregister(struct regulator_dev *rdev)
1947 mutex_lock(®ulator_list_mutex);
1948 list_del(&rdev->list);
1950 sysfs_remove_link(&rdev->dev.kobj, "supply");
1951 device_unregister(&rdev->dev);
1952 mutex_unlock(®ulator_list_mutex);
1954 EXPORT_SYMBOL_GPL(regulator_unregister);
1957 * regulator_suspend_prepare - prepare regulators for system wide suspend
1958 * @state: system suspend state
1960 * Configure each regulator with it's suspend operating parameters for state.
1961 * This will usually be called by machine suspend code prior to supending.
1963 int regulator_suspend_prepare(suspend_state_t state)
1965 struct regulator_dev *rdev;
1968 /* ON is handled by regulator active state */
1969 if (state == PM_SUSPEND_ON)
1972 mutex_lock(®ulator_list_mutex);
1973 list_for_each_entry(rdev, ®ulator_list, list) {
1975 mutex_lock(&rdev->mutex);
1976 ret = suspend_prepare(rdev, state);
1977 mutex_unlock(&rdev->mutex);
1980 printk(KERN_ERR "%s: failed to prepare %s\n",
1981 __func__, rdev->desc->name);
1986 mutex_unlock(®ulator_list_mutex);
1989 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1992 * rdev_get_drvdata - get rdev regulator driver data
1995 * Get rdev regulator driver private data. This call can be used in the
1996 * regulator driver context.
1998 void *rdev_get_drvdata(struct regulator_dev *rdev)
2000 return rdev->reg_data;
2002 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2005 * regulator_get_drvdata - get regulator driver data
2006 * @regulator: regulator
2008 * Get regulator driver private data. This call can be used in the consumer
2009 * driver context when non API regulator specific functions need to be called.
2011 void *regulator_get_drvdata(struct regulator *regulator)
2013 return regulator->rdev->reg_data;
2015 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2018 * regulator_set_drvdata - set regulator driver data
2019 * @regulator: regulator
2022 void regulator_set_drvdata(struct regulator *regulator, void *data)
2024 regulator->rdev->reg_data = data;
2026 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2029 * regulator_get_id - get regulator ID
2032 int rdev_get_id(struct regulator_dev *rdev)
2034 return rdev->desc->id;
2036 EXPORT_SYMBOL_GPL(rdev_get_id);
2038 struct device *rdev_get_dev(struct regulator_dev *rdev)
2042 EXPORT_SYMBOL_GPL(rdev_get_dev);
2044 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2046 return reg_init_data->driver_data;
2048 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2050 static int __init regulator_init(void)
2052 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2053 return class_register(®ulator_class);
2056 /* init early to allow our consumers to complete system booting */
2057 core_initcall(regulator_init);