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 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
783 node->regulator = rdev;
784 node->dev = consumer_dev;
785 node->supply = supply;
787 list_add(&node->list, ®ulator_map_list);
791 static void unset_consumer_device_supply(struct regulator_dev *rdev,
792 struct device *consumer_dev)
794 struct regulator_map *node, *n;
796 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
797 if (rdev == node->regulator &&
798 consumer_dev == node->dev) {
799 list_del(&node->list);
806 #define REG_STR_SIZE 32
808 static struct regulator *create_regulator(struct regulator_dev *rdev,
810 const char *supply_name)
812 struct regulator *regulator;
813 char buf[REG_STR_SIZE];
816 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
817 if (regulator == NULL)
820 mutex_lock(&rdev->mutex);
821 regulator->rdev = rdev;
822 list_add(®ulator->list, &rdev->consumer_list);
825 /* create a 'requested_microamps_name' sysfs entry */
826 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
828 if (size >= REG_STR_SIZE)
831 regulator->dev = dev;
832 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
833 if (regulator->dev_attr.attr.name == NULL)
836 regulator->dev_attr.attr.owner = THIS_MODULE;
837 regulator->dev_attr.attr.mode = 0444;
838 regulator->dev_attr.show = device_requested_uA_show;
839 err = device_create_file(dev, ®ulator->dev_attr);
841 printk(KERN_WARNING "%s: could not add regulator_dev"
842 " load sysfs\n", __func__);
846 /* also add a link to the device sysfs entry */
847 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
848 dev->kobj.name, supply_name);
849 if (size >= REG_STR_SIZE)
852 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
853 if (regulator->supply_name == NULL)
856 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
860 "%s: could not add device link %s err %d\n",
861 __func__, dev->kobj.name, err);
862 device_remove_file(dev, ®ulator->dev_attr);
866 mutex_unlock(&rdev->mutex);
869 kfree(regulator->supply_name);
871 device_remove_file(regulator->dev, ®ulator->dev_attr);
873 kfree(regulator->dev_attr.attr.name);
875 list_del(®ulator->list);
877 mutex_unlock(&rdev->mutex);
882 * regulator_get - lookup and obtain a reference to a regulator.
883 * @dev: device for regulator "consumer"
884 * @id: Supply name or regulator ID.
886 * Returns a struct regulator corresponding to the regulator producer,
887 * or IS_ERR() condition containing errno. Use of supply names
888 * configured via regulator_set_device_supply() is strongly
891 struct regulator *regulator_get(struct device *dev, const char *id)
893 struct regulator_dev *rdev;
894 struct regulator_map *map;
895 struct regulator *regulator = ERR_PTR(-ENODEV);
898 printk(KERN_ERR "regulator: get() with no identifier\n");
902 mutex_lock(®ulator_list_mutex);
904 list_for_each_entry(map, ®ulator_map_list, list) {
905 if (dev == map->dev &&
906 strcmp(map->supply, id) == 0) {
907 rdev = map->regulator;
911 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
913 mutex_unlock(®ulator_list_mutex);
917 if (!try_module_get(rdev->owner))
920 regulator = create_regulator(rdev, dev, id);
921 if (regulator == NULL) {
922 regulator = ERR_PTR(-ENOMEM);
923 module_put(rdev->owner);
927 mutex_unlock(®ulator_list_mutex);
930 EXPORT_SYMBOL_GPL(regulator_get);
933 * regulator_put - "free" the regulator source
934 * @regulator: regulator source
936 * Note: drivers must ensure that all regulator_enable calls made on this
937 * regulator source are balanced by regulator_disable calls prior to calling
940 void regulator_put(struct regulator *regulator)
942 struct regulator_dev *rdev;
944 if (regulator == NULL || IS_ERR(regulator))
947 mutex_lock(®ulator_list_mutex);
948 rdev = regulator->rdev;
950 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
951 regulator->supply_name))
952 _regulator_disable(rdev);
954 /* remove any sysfs entries */
955 if (regulator->dev) {
956 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
957 kfree(regulator->supply_name);
958 device_remove_file(regulator->dev, ®ulator->dev_attr);
959 kfree(regulator->dev_attr.attr.name);
961 list_del(®ulator->list);
964 module_put(rdev->owner);
965 mutex_unlock(®ulator_list_mutex);
967 EXPORT_SYMBOL_GPL(regulator_put);
969 /* locks held by regulator_enable() */
970 static int _regulator_enable(struct regulator_dev *rdev)
974 if (!rdev->constraints) {
975 printk(KERN_ERR "%s: %s has no constraints\n",
976 __func__, rdev->desc->name);
980 /* do we need to enable the supply regulator first */
982 ret = _regulator_enable(rdev->supply);
984 printk(KERN_ERR "%s: failed to enable %s: %d\n",
985 __func__, rdev->desc->name, ret);
990 /* check voltage and requested load before enabling */
991 if (rdev->desc->ops->enable) {
993 if (rdev->constraints &&
994 (rdev->constraints->valid_ops_mask &
995 REGULATOR_CHANGE_DRMS))
996 drms_uA_update(rdev);
998 ret = rdev->desc->ops->enable(rdev);
1000 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1001 __func__, rdev->desc->name, ret);
1012 * regulator_enable - enable regulator output
1013 * @regulator: regulator source
1015 * Request that the regulator be enabled with the regulator output at
1016 * the predefined voltage or current value. Calls to regulator_enable()
1017 * must be balanced with calls to regulator_disable().
1019 * NOTE: the output value can be set by other drivers, boot loader or may be
1020 * hardwired in the regulator.
1022 int regulator_enable(struct regulator *regulator)
1024 struct regulator_dev *rdev = regulator->rdev;
1027 mutex_lock(&rdev->mutex);
1028 if (regulator->enabled == 0)
1029 ret = _regulator_enable(rdev);
1030 else if (regulator->enabled < 0)
1033 regulator->enabled++;
1034 mutex_unlock(&rdev->mutex);
1037 EXPORT_SYMBOL_GPL(regulator_enable);
1039 /* locks held by regulator_disable() */
1040 static int _regulator_disable(struct regulator_dev *rdev)
1044 /* are we the last user and permitted to disable ? */
1045 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1047 /* we are last user */
1048 if (rdev->desc->ops->disable) {
1049 ret = rdev->desc->ops->disable(rdev);
1051 printk(KERN_ERR "%s: failed to disable %s\n",
1052 __func__, rdev->desc->name);
1057 /* decrease our supplies ref count and disable if required */
1059 _regulator_disable(rdev->supply);
1061 rdev->use_count = 0;
1062 } else if (rdev->use_count > 1) {
1064 if (rdev->constraints &&
1065 (rdev->constraints->valid_ops_mask &
1066 REGULATOR_CHANGE_DRMS))
1067 drms_uA_update(rdev);
1075 * regulator_disable - disable regulator output
1076 * @regulator: regulator source
1078 * Disable the regulator output voltage or current. Calls to
1079 * regulator_enable() must be balanced with calls to
1080 * regulator_disable().
1082 * NOTE: this will only disable the regulator output if no other consumer
1083 * devices have it enabled, the regulator device supports disabling and
1084 * machine constraints permit this operation.
1086 int regulator_disable(struct regulator *regulator)
1088 struct regulator_dev *rdev = regulator->rdev;
1091 mutex_lock(&rdev->mutex);
1092 if (regulator->enabled == 1) {
1093 ret = _regulator_disable(rdev);
1095 regulator->uA_load = 0;
1096 } else if (WARN(regulator->enabled <= 0,
1097 "unbalanced disables for supply %s\n",
1098 regulator->supply_name))
1101 regulator->enabled--;
1102 mutex_unlock(&rdev->mutex);
1105 EXPORT_SYMBOL_GPL(regulator_disable);
1107 /* locks held by regulator_force_disable() */
1108 static int _regulator_force_disable(struct regulator_dev *rdev)
1113 if (rdev->desc->ops->disable) {
1114 /* ah well, who wants to live forever... */
1115 ret = rdev->desc->ops->disable(rdev);
1117 printk(KERN_ERR "%s: failed to force disable %s\n",
1118 __func__, rdev->desc->name);
1121 /* notify other consumers that power has been forced off */
1122 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1126 /* decrease our supplies ref count and disable if required */
1128 _regulator_disable(rdev->supply);
1130 rdev->use_count = 0;
1135 * regulator_force_disable - force disable regulator output
1136 * @regulator: regulator source
1138 * Forcibly disable the regulator output voltage or current.
1139 * NOTE: this *will* disable the regulator output even if other consumer
1140 * devices have it enabled. This should be used for situations when device
1141 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1143 int regulator_force_disable(struct regulator *regulator)
1147 mutex_lock(®ulator->rdev->mutex);
1148 regulator->enabled = 0;
1149 regulator->uA_load = 0;
1150 ret = _regulator_force_disable(regulator->rdev);
1151 mutex_unlock(®ulator->rdev->mutex);
1154 EXPORT_SYMBOL_GPL(regulator_force_disable);
1156 static int _regulator_is_enabled(struct regulator_dev *rdev)
1160 mutex_lock(&rdev->mutex);
1163 if (!rdev->desc->ops->is_enabled) {
1168 ret = rdev->desc->ops->is_enabled(rdev);
1170 mutex_unlock(&rdev->mutex);
1175 * regulator_is_enabled - is the regulator output enabled
1176 * @regulator: regulator source
1178 * Returns positive if the regulator driver backing the source/client
1179 * has requested that the device be enabled, zero if it hasn't, else a
1180 * negative errno code.
1182 * Note that the device backing this regulator handle can have multiple
1183 * users, so it might be enabled even if regulator_enable() was never
1184 * called for this particular source.
1186 int regulator_is_enabled(struct regulator *regulator)
1188 return _regulator_is_enabled(regulator->rdev);
1190 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1193 * regulator_set_voltage - set regulator output voltage
1194 * @regulator: regulator source
1195 * @min_uV: Minimum required voltage in uV
1196 * @max_uV: Maximum acceptable voltage in uV
1198 * Sets a voltage regulator to the desired output voltage. This can be set
1199 * during any regulator state. IOW, regulator can be disabled or enabled.
1201 * If the regulator is enabled then the voltage will change to the new value
1202 * immediately otherwise if the regulator is disabled the regulator will
1203 * output at the new voltage when enabled.
1205 * NOTE: If the regulator is shared between several devices then the lowest
1206 * request voltage that meets the system constraints will be used.
1207 * Regulator system constraints must be set for this regulator before
1208 * calling this function otherwise this call will fail.
1210 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1212 struct regulator_dev *rdev = regulator->rdev;
1215 mutex_lock(&rdev->mutex);
1218 if (!rdev->desc->ops->set_voltage) {
1223 /* constraints check */
1224 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1227 regulator->min_uV = min_uV;
1228 regulator->max_uV = max_uV;
1229 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1232 mutex_unlock(&rdev->mutex);
1235 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1237 static int _regulator_get_voltage(struct regulator_dev *rdev)
1240 if (rdev->desc->ops->get_voltage)
1241 return rdev->desc->ops->get_voltage(rdev);
1247 * regulator_get_voltage - get regulator output voltage
1248 * @regulator: regulator source
1250 * This returns the current regulator voltage in uV.
1252 * NOTE: If the regulator is disabled it will return the voltage value. This
1253 * function should not be used to determine regulator state.
1255 int regulator_get_voltage(struct regulator *regulator)
1259 mutex_lock(®ulator->rdev->mutex);
1261 ret = _regulator_get_voltage(regulator->rdev);
1263 mutex_unlock(®ulator->rdev->mutex);
1267 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1270 * regulator_set_current_limit - set regulator output current limit
1271 * @regulator: regulator source
1272 * @min_uA: Minimuum supported current in uA
1273 * @max_uA: Maximum supported current in uA
1275 * Sets current sink to the desired output current. This can be set during
1276 * any regulator state. IOW, regulator can be disabled or enabled.
1278 * If the regulator is enabled then the current will change to the new value
1279 * immediately otherwise if the regulator is disabled the regulator will
1280 * output at the new current when enabled.
1282 * NOTE: Regulator system constraints must be set for this regulator before
1283 * calling this function otherwise this call will fail.
1285 int regulator_set_current_limit(struct regulator *regulator,
1286 int min_uA, int max_uA)
1288 struct regulator_dev *rdev = regulator->rdev;
1291 mutex_lock(&rdev->mutex);
1294 if (!rdev->desc->ops->set_current_limit) {
1299 /* constraints check */
1300 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1304 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1306 mutex_unlock(&rdev->mutex);
1309 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1311 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1315 mutex_lock(&rdev->mutex);
1318 if (!rdev->desc->ops->get_current_limit) {
1323 ret = rdev->desc->ops->get_current_limit(rdev);
1325 mutex_unlock(&rdev->mutex);
1330 * regulator_get_current_limit - get regulator output current
1331 * @regulator: regulator source
1333 * This returns the current supplied by the specified current sink in uA.
1335 * NOTE: If the regulator is disabled it will return the current value. This
1336 * function should not be used to determine regulator state.
1338 int regulator_get_current_limit(struct regulator *regulator)
1340 return _regulator_get_current_limit(regulator->rdev);
1342 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1345 * regulator_set_mode - set regulator operating mode
1346 * @regulator: regulator source
1347 * @mode: operating mode - one of the REGULATOR_MODE constants
1349 * Set regulator operating mode to increase regulator efficiency or improve
1350 * regulation performance.
1352 * NOTE: Regulator system constraints must be set for this regulator before
1353 * calling this function otherwise this call will fail.
1355 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1357 struct regulator_dev *rdev = regulator->rdev;
1360 mutex_lock(&rdev->mutex);
1363 if (!rdev->desc->ops->set_mode) {
1368 /* constraints check */
1369 ret = regulator_check_mode(rdev, mode);
1373 ret = rdev->desc->ops->set_mode(rdev, mode);
1375 mutex_unlock(&rdev->mutex);
1378 EXPORT_SYMBOL_GPL(regulator_set_mode);
1380 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1384 mutex_lock(&rdev->mutex);
1387 if (!rdev->desc->ops->get_mode) {
1392 ret = rdev->desc->ops->get_mode(rdev);
1394 mutex_unlock(&rdev->mutex);
1399 * regulator_get_mode - get regulator operating mode
1400 * @regulator: regulator source
1402 * Get the current regulator operating mode.
1404 unsigned int regulator_get_mode(struct regulator *regulator)
1406 return _regulator_get_mode(regulator->rdev);
1408 EXPORT_SYMBOL_GPL(regulator_get_mode);
1411 * regulator_set_optimum_mode - set regulator optimum operating mode
1412 * @regulator: regulator source
1413 * @uA_load: load current
1415 * Notifies the regulator core of a new device load. This is then used by
1416 * DRMS (if enabled by constraints) to set the most efficient regulator
1417 * operating mode for the new regulator loading.
1419 * Consumer devices notify their supply regulator of the maximum power
1420 * they will require (can be taken from device datasheet in the power
1421 * consumption tables) when they change operational status and hence power
1422 * state. Examples of operational state changes that can affect power
1423 * consumption are :-
1425 * o Device is opened / closed.
1426 * o Device I/O is about to begin or has just finished.
1427 * o Device is idling in between work.
1429 * This information is also exported via sysfs to userspace.
1431 * DRMS will sum the total requested load on the regulator and change
1432 * to the most efficient operating mode if platform constraints allow.
1434 * Returns the new regulator mode or error.
1436 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1438 struct regulator_dev *rdev = regulator->rdev;
1439 struct regulator *consumer;
1440 int ret, output_uV, input_uV, total_uA_load = 0;
1443 mutex_lock(&rdev->mutex);
1445 regulator->uA_load = uA_load;
1446 ret = regulator_check_drms(rdev);
1452 if (!rdev->desc->ops->get_optimum_mode)
1455 /* get output voltage */
1456 output_uV = rdev->desc->ops->get_voltage(rdev);
1457 if (output_uV <= 0) {
1458 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1459 __func__, rdev->desc->name);
1463 /* get input voltage */
1464 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1465 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1467 input_uV = rdev->constraints->input_uV;
1468 if (input_uV <= 0) {
1469 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1470 __func__, rdev->desc->name);
1474 /* calc total requested load for this regulator */
1475 list_for_each_entry(consumer, &rdev->consumer_list, list)
1476 total_uA_load += consumer->uA_load;
1478 mode = rdev->desc->ops->get_optimum_mode(rdev,
1479 input_uV, output_uV,
1481 ret = regulator_check_mode(rdev, mode);
1483 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1484 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1485 total_uA_load, input_uV, output_uV);
1489 ret = rdev->desc->ops->set_mode(rdev, mode);
1491 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1492 __func__, mode, rdev->desc->name);
1497 mutex_unlock(&rdev->mutex);
1500 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1503 * regulator_register_notifier - register regulator event notifier
1504 * @regulator: regulator source
1505 * @nb: notifier block
1507 * Register notifier block to receive regulator events.
1509 int regulator_register_notifier(struct regulator *regulator,
1510 struct notifier_block *nb)
1512 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1515 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1518 * regulator_unregister_notifier - unregister regulator event notifier
1519 * @regulator: regulator source
1520 * @nb: notifier block
1522 * Unregister regulator event notifier block.
1524 int regulator_unregister_notifier(struct regulator *regulator,
1525 struct notifier_block *nb)
1527 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1530 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1532 /* notify regulator consumers and downstream regulator consumers */
1533 static void _notifier_call_chain(struct regulator_dev *rdev,
1534 unsigned long event, void *data)
1536 struct regulator_dev *_rdev;
1538 /* call rdev chain first */
1539 mutex_lock(&rdev->mutex);
1540 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1541 mutex_unlock(&rdev->mutex);
1543 /* now notify regulator we supply */
1544 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1545 _notifier_call_chain(_rdev, event, data);
1549 * regulator_bulk_get - get multiple regulator consumers
1551 * @dev: Device to supply
1552 * @num_consumers: Number of consumers to register
1553 * @consumers: Configuration of consumers; clients are stored here.
1555 * @return 0 on success, an errno on failure.
1557 * This helper function allows drivers to get several regulator
1558 * consumers in one operation. If any of the regulators cannot be
1559 * acquired then any regulators that were allocated will be freed
1560 * before returning to the caller.
1562 int regulator_bulk_get(struct device *dev, int num_consumers,
1563 struct regulator_bulk_data *consumers)
1568 for (i = 0; i < num_consumers; i++)
1569 consumers[i].consumer = NULL;
1571 for (i = 0; i < num_consumers; i++) {
1572 consumers[i].consumer = regulator_get(dev,
1573 consumers[i].supply);
1574 if (IS_ERR(consumers[i].consumer)) {
1575 dev_err(dev, "Failed to get supply '%s'\n",
1576 consumers[i].supply);
1577 ret = PTR_ERR(consumers[i].consumer);
1578 consumers[i].consumer = NULL;
1586 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1587 regulator_put(consumers[i].consumer);
1591 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1594 * regulator_bulk_enable - enable multiple regulator consumers
1596 * @num_consumers: Number of consumers
1597 * @consumers: Consumer data; clients are stored here.
1598 * @return 0 on success, an errno on failure
1600 * This convenience API allows consumers to enable multiple regulator
1601 * clients in a single API call. If any consumers cannot be enabled
1602 * then any others that were enabled will be disabled again prior to
1605 int regulator_bulk_enable(int num_consumers,
1606 struct regulator_bulk_data *consumers)
1611 for (i = 0; i < num_consumers; i++) {
1612 ret = regulator_enable(consumers[i].consumer);
1620 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1621 for (i = 0; i < num_consumers; i++)
1622 regulator_disable(consumers[i].consumer);
1626 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1629 * regulator_bulk_disable - disable multiple regulator consumers
1631 * @num_consumers: Number of consumers
1632 * @consumers: Consumer data; clients are stored here.
1633 * @return 0 on success, an errno on failure
1635 * This convenience API allows consumers to disable multiple regulator
1636 * clients in a single API call. If any consumers cannot be enabled
1637 * then any others that were disabled will be disabled again prior to
1640 int regulator_bulk_disable(int num_consumers,
1641 struct regulator_bulk_data *consumers)
1646 for (i = 0; i < num_consumers; i++) {
1647 ret = regulator_disable(consumers[i].consumer);
1655 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1656 for (i = 0; i < num_consumers; i++)
1657 regulator_enable(consumers[i].consumer);
1661 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1664 * regulator_bulk_free - free multiple regulator consumers
1666 * @num_consumers: Number of consumers
1667 * @consumers: Consumer data; clients are stored here.
1669 * This convenience API allows consumers to free multiple regulator
1670 * clients in a single API call.
1672 void regulator_bulk_free(int num_consumers,
1673 struct regulator_bulk_data *consumers)
1677 for (i = 0; i < num_consumers; i++) {
1678 regulator_put(consumers[i].consumer);
1679 consumers[i].consumer = NULL;
1682 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1685 * regulator_notifier_call_chain - call regulator event notifier
1686 * @rdev: regulator source
1687 * @event: notifier block
1688 * @data: callback-specific data.
1690 * Called by regulator drivers to notify clients a regulator event has
1691 * occurred. We also notify regulator clients downstream.
1693 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1694 unsigned long event, void *data)
1696 _notifier_call_chain(rdev, event, data);
1700 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1703 * To avoid cluttering sysfs (and memory) with useless state, only
1704 * create attributes that can be meaningfully displayed.
1706 static int add_regulator_attributes(struct regulator_dev *rdev)
1708 struct device *dev = &rdev->dev;
1709 struct regulator_ops *ops = rdev->desc->ops;
1712 /* some attributes need specific methods to be displayed */
1713 if (ops->get_voltage) {
1714 status = device_create_file(dev, &dev_attr_microvolts);
1718 if (ops->get_current_limit) {
1719 status = device_create_file(dev, &dev_attr_microamps);
1723 if (ops->get_mode) {
1724 status = device_create_file(dev, &dev_attr_opmode);
1728 if (ops->is_enabled) {
1729 status = device_create_file(dev, &dev_attr_state);
1734 /* some attributes are type-specific */
1735 if (rdev->desc->type == REGULATOR_CURRENT) {
1736 status = device_create_file(dev, &dev_attr_requested_microamps);
1741 /* all the other attributes exist to support constraints;
1742 * don't show them if there are no constraints, or if the
1743 * relevant supporting methods are missing.
1745 if (!rdev->constraints)
1748 /* constraints need specific supporting methods */
1749 if (ops->set_voltage) {
1750 status = device_create_file(dev, &dev_attr_min_microvolts);
1753 status = device_create_file(dev, &dev_attr_max_microvolts);
1757 if (ops->set_current_limit) {
1758 status = device_create_file(dev, &dev_attr_min_microamps);
1761 status = device_create_file(dev, &dev_attr_max_microamps);
1766 /* suspend mode constraints need multiple supporting methods */
1767 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1770 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1773 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1776 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1780 if (ops->set_suspend_voltage) {
1781 status = device_create_file(dev,
1782 &dev_attr_suspend_standby_microvolts);
1785 status = device_create_file(dev,
1786 &dev_attr_suspend_mem_microvolts);
1789 status = device_create_file(dev,
1790 &dev_attr_suspend_disk_microvolts);
1795 if (ops->set_suspend_mode) {
1796 status = device_create_file(dev,
1797 &dev_attr_suspend_standby_mode);
1800 status = device_create_file(dev,
1801 &dev_attr_suspend_mem_mode);
1804 status = device_create_file(dev,
1805 &dev_attr_suspend_disk_mode);
1814 * regulator_register - register regulator
1815 * @regulator_desc: regulator to register
1816 * @dev: struct device for the regulator
1817 * @driver_data: private regulator data
1819 * Called by regulator drivers to register a regulator.
1820 * Returns 0 on success.
1822 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1823 struct device *dev, void *driver_data)
1825 static atomic_t regulator_no = ATOMIC_INIT(0);
1826 struct regulator_dev *rdev;
1827 struct regulator_init_data *init_data = dev->platform_data;
1830 if (regulator_desc == NULL)
1831 return ERR_PTR(-EINVAL);
1833 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1834 return ERR_PTR(-EINVAL);
1836 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1837 !regulator_desc->type == REGULATOR_CURRENT)
1838 return ERR_PTR(-EINVAL);
1841 return ERR_PTR(-EINVAL);
1843 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1845 return ERR_PTR(-ENOMEM);
1847 mutex_lock(®ulator_list_mutex);
1849 mutex_init(&rdev->mutex);
1850 rdev->reg_data = driver_data;
1851 rdev->owner = regulator_desc->owner;
1852 rdev->desc = regulator_desc;
1853 INIT_LIST_HEAD(&rdev->consumer_list);
1854 INIT_LIST_HEAD(&rdev->supply_list);
1855 INIT_LIST_HEAD(&rdev->list);
1856 INIT_LIST_HEAD(&rdev->slist);
1857 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1859 /* preform any regulator specific init */
1860 if (init_data->regulator_init) {
1861 ret = init_data->regulator_init(rdev->reg_data);
1866 /* register with sysfs */
1867 rdev->dev.class = ®ulator_class;
1868 rdev->dev.parent = dev;
1869 dev_set_name(&rdev->dev, "regulator.%d",
1870 atomic_inc_return(®ulator_no) - 1);
1871 ret = device_register(&rdev->dev);
1875 dev_set_drvdata(&rdev->dev, rdev);
1877 /* set regulator constraints */
1878 ret = set_machine_constraints(rdev, &init_data->constraints);
1882 /* add attributes supported by this regulator */
1883 ret = add_regulator_attributes(rdev);
1887 /* set supply regulator if it exists */
1888 if (init_data->supply_regulator_dev) {
1889 ret = set_supply(rdev,
1890 dev_get_drvdata(init_data->supply_regulator_dev));
1895 /* add consumers devices */
1896 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1897 ret = set_consumer_device_supply(rdev,
1898 init_data->consumer_supplies[i].dev,
1899 init_data->consumer_supplies[i].supply);
1901 for (--i; i >= 0; i--)
1902 unset_consumer_device_supply(rdev,
1903 init_data->consumer_supplies[i].dev);
1908 list_add(&rdev->list, ®ulator_list);
1910 mutex_unlock(®ulator_list_mutex);
1914 device_unregister(&rdev->dev);
1917 rdev = ERR_PTR(ret);
1920 EXPORT_SYMBOL_GPL(regulator_register);
1923 * regulator_unregister - unregister regulator
1924 * @rdev: regulator to unregister
1926 * Called by regulator drivers to unregister a regulator.
1928 void regulator_unregister(struct regulator_dev *rdev)
1933 mutex_lock(®ulator_list_mutex);
1934 list_del(&rdev->list);
1936 sysfs_remove_link(&rdev->dev.kobj, "supply");
1937 device_unregister(&rdev->dev);
1938 mutex_unlock(®ulator_list_mutex);
1940 EXPORT_SYMBOL_GPL(regulator_unregister);
1943 * regulator_suspend_prepare - prepare regulators for system wide suspend
1944 * @state: system suspend state
1946 * Configure each regulator with it's suspend operating parameters for state.
1947 * This will usually be called by machine suspend code prior to supending.
1949 int regulator_suspend_prepare(suspend_state_t state)
1951 struct regulator_dev *rdev;
1954 /* ON is handled by regulator active state */
1955 if (state == PM_SUSPEND_ON)
1958 mutex_lock(®ulator_list_mutex);
1959 list_for_each_entry(rdev, ®ulator_list, list) {
1961 mutex_lock(&rdev->mutex);
1962 ret = suspend_prepare(rdev, state);
1963 mutex_unlock(&rdev->mutex);
1966 printk(KERN_ERR "%s: failed to prepare %s\n",
1967 __func__, rdev->desc->name);
1972 mutex_unlock(®ulator_list_mutex);
1975 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1978 * rdev_get_drvdata - get rdev regulator driver data
1981 * Get rdev regulator driver private data. This call can be used in the
1982 * regulator driver context.
1984 void *rdev_get_drvdata(struct regulator_dev *rdev)
1986 return rdev->reg_data;
1988 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1991 * regulator_get_drvdata - get regulator driver data
1992 * @regulator: regulator
1994 * Get regulator driver private data. This call can be used in the consumer
1995 * driver context when non API regulator specific functions need to be called.
1997 void *regulator_get_drvdata(struct regulator *regulator)
1999 return regulator->rdev->reg_data;
2001 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2004 * regulator_set_drvdata - set regulator driver data
2005 * @regulator: regulator
2008 void regulator_set_drvdata(struct regulator *regulator, void *data)
2010 regulator->rdev->reg_data = data;
2012 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2015 * regulator_get_id - get regulator ID
2018 int rdev_get_id(struct regulator_dev *rdev)
2020 return rdev->desc->id;
2022 EXPORT_SYMBOL_GPL(rdev_get_id);
2024 struct device *rdev_get_dev(struct regulator_dev *rdev)
2028 EXPORT_SYMBOL_GPL(rdev_get_dev);
2030 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2032 return reg_init_data->driver_data;
2034 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2036 static int __init regulator_init(void)
2038 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2039 return class_register(®ulator_class);
2042 /* init early to allow our consumers to complete system booting */
2043 core_initcall(regulator_init);