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 * Enable the regulator output at the predefined voltage or current value.
1016 * NOTE: the output value can be set by other drivers, boot loader or may be
1017 * hardwired in the regulator. Calls to regulator_enable() must be balanced
1018 * with calls to regulator_disable().
1020 int regulator_enable(struct regulator *regulator)
1022 struct regulator_dev *rdev = regulator->rdev;
1025 mutex_lock(&rdev->mutex);
1026 if (regulator->enabled == 0)
1027 ret = _regulator_enable(rdev);
1028 else if (regulator->enabled < 0)
1031 regulator->enabled++;
1032 mutex_unlock(&rdev->mutex);
1035 EXPORT_SYMBOL_GPL(regulator_enable);
1037 /* locks held by regulator_disable() */
1038 static int _regulator_disable(struct regulator_dev *rdev)
1042 /* are we the last user and permitted to disable ? */
1043 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1045 /* we are last user */
1046 if (rdev->desc->ops->disable) {
1047 ret = rdev->desc->ops->disable(rdev);
1049 printk(KERN_ERR "%s: failed to disable %s\n",
1050 __func__, rdev->desc->name);
1055 /* decrease our supplies ref count and disable if required */
1057 _regulator_disable(rdev->supply);
1059 rdev->use_count = 0;
1060 } else if (rdev->use_count > 1) {
1062 if (rdev->constraints &&
1063 (rdev->constraints->valid_ops_mask &
1064 REGULATOR_CHANGE_DRMS))
1065 drms_uA_update(rdev);
1073 * regulator_disable - disable regulator output
1074 * @regulator: regulator source
1076 * Disable the regulator output voltage or current.
1078 * NOTE: this will only disable the regulator output if no other consumer
1079 * devices have it enabled. Calls to regulator_enable() must be balanced with
1080 * calls to regulator_disable().
1082 int regulator_disable(struct regulator *regulator)
1084 struct regulator_dev *rdev = regulator->rdev;
1087 mutex_lock(&rdev->mutex);
1088 if (regulator->enabled == 1) {
1089 ret = _regulator_disable(rdev);
1091 regulator->uA_load = 0;
1092 } else if (WARN(regulator->enabled <= 0,
1093 "unbalanced disables for supply %s\n",
1094 regulator->supply_name))
1097 regulator->enabled--;
1098 mutex_unlock(&rdev->mutex);
1101 EXPORT_SYMBOL_GPL(regulator_disable);
1103 /* locks held by regulator_force_disable() */
1104 static int _regulator_force_disable(struct regulator_dev *rdev)
1109 if (rdev->desc->ops->disable) {
1110 /* ah well, who wants to live forever... */
1111 ret = rdev->desc->ops->disable(rdev);
1113 printk(KERN_ERR "%s: failed to force disable %s\n",
1114 __func__, rdev->desc->name);
1117 /* notify other consumers that power has been forced off */
1118 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1122 /* decrease our supplies ref count and disable if required */
1124 _regulator_disable(rdev->supply);
1126 rdev->use_count = 0;
1131 * regulator_force_disable - force disable regulator output
1132 * @regulator: regulator source
1134 * Forcibly disable the regulator output voltage or current.
1135 * NOTE: this *will* disable the regulator output even if other consumer
1136 * devices have it enabled. This should be used for situations when device
1137 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1139 int regulator_force_disable(struct regulator *regulator)
1143 mutex_lock(®ulator->rdev->mutex);
1144 regulator->enabled = 0;
1145 regulator->uA_load = 0;
1146 ret = _regulator_force_disable(regulator->rdev);
1147 mutex_unlock(®ulator->rdev->mutex);
1150 EXPORT_SYMBOL_GPL(regulator_force_disable);
1152 static int _regulator_is_enabled(struct regulator_dev *rdev)
1156 mutex_lock(&rdev->mutex);
1159 if (!rdev->desc->ops->is_enabled) {
1164 ret = rdev->desc->ops->is_enabled(rdev);
1166 mutex_unlock(&rdev->mutex);
1171 * regulator_is_enabled - is the regulator output enabled
1172 * @regulator: regulator source
1174 * Returns positive if the regulator driver backing the source/client
1175 * has requested that the device be enabled, zero if it hasn't, else a
1176 * negative errno code.
1178 * Note that the device backing this regulator handle can have multiple
1179 * users, so it might be enabled even if regulator_enable() was never
1180 * called for this particular source.
1182 int regulator_is_enabled(struct regulator *regulator)
1184 return _regulator_is_enabled(regulator->rdev);
1186 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1189 * regulator_set_voltage - set regulator output voltage
1190 * @regulator: regulator source
1191 * @min_uV: Minimum required voltage in uV
1192 * @max_uV: Maximum acceptable voltage in uV
1194 * Sets a voltage regulator to the desired output voltage. This can be set
1195 * during any regulator state. IOW, regulator can be disabled or enabled.
1197 * If the regulator is enabled then the voltage will change to the new value
1198 * immediately otherwise if the regulator is disabled the regulator will
1199 * output at the new voltage when enabled.
1201 * NOTE: If the regulator is shared between several devices then the lowest
1202 * request voltage that meets the system constraints will be used.
1203 * Regulator system constraints must be set for this regulator before
1204 * calling this function otherwise this call will fail.
1206 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1208 struct regulator_dev *rdev = regulator->rdev;
1211 mutex_lock(&rdev->mutex);
1214 if (!rdev->desc->ops->set_voltage) {
1219 /* constraints check */
1220 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1223 regulator->min_uV = min_uV;
1224 regulator->max_uV = max_uV;
1225 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1228 mutex_unlock(&rdev->mutex);
1231 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1233 static int _regulator_get_voltage(struct regulator_dev *rdev)
1236 if (rdev->desc->ops->get_voltage)
1237 return rdev->desc->ops->get_voltage(rdev);
1243 * regulator_get_voltage - get regulator output voltage
1244 * @regulator: regulator source
1246 * This returns the current regulator voltage in uV.
1248 * NOTE: If the regulator is disabled it will return the voltage value. This
1249 * function should not be used to determine regulator state.
1251 int regulator_get_voltage(struct regulator *regulator)
1255 mutex_lock(®ulator->rdev->mutex);
1257 ret = _regulator_get_voltage(regulator->rdev);
1259 mutex_unlock(®ulator->rdev->mutex);
1263 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1266 * regulator_set_current_limit - set regulator output current limit
1267 * @regulator: regulator source
1268 * @min_uA: Minimuum supported current in uA
1269 * @max_uA: Maximum supported current in uA
1271 * Sets current sink to the desired output current. This can be set during
1272 * any regulator state. IOW, regulator can be disabled or enabled.
1274 * If the regulator is enabled then the current will change to the new value
1275 * immediately otherwise if the regulator is disabled the regulator will
1276 * output at the new current when enabled.
1278 * NOTE: Regulator system constraints must be set for this regulator before
1279 * calling this function otherwise this call will fail.
1281 int regulator_set_current_limit(struct regulator *regulator,
1282 int min_uA, int max_uA)
1284 struct regulator_dev *rdev = regulator->rdev;
1287 mutex_lock(&rdev->mutex);
1290 if (!rdev->desc->ops->set_current_limit) {
1295 /* constraints check */
1296 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1300 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1302 mutex_unlock(&rdev->mutex);
1305 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1307 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1311 mutex_lock(&rdev->mutex);
1314 if (!rdev->desc->ops->get_current_limit) {
1319 ret = rdev->desc->ops->get_current_limit(rdev);
1321 mutex_unlock(&rdev->mutex);
1326 * regulator_get_current_limit - get regulator output current
1327 * @regulator: regulator source
1329 * This returns the current supplied by the specified current sink in uA.
1331 * NOTE: If the regulator is disabled it will return the current value. This
1332 * function should not be used to determine regulator state.
1334 int regulator_get_current_limit(struct regulator *regulator)
1336 return _regulator_get_current_limit(regulator->rdev);
1338 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1341 * regulator_set_mode - set regulator operating mode
1342 * @regulator: regulator source
1343 * @mode: operating mode - one of the REGULATOR_MODE constants
1345 * Set regulator operating mode to increase regulator efficiency or improve
1346 * regulation performance.
1348 * NOTE: Regulator system constraints must be set for this regulator before
1349 * calling this function otherwise this call will fail.
1351 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1353 struct regulator_dev *rdev = regulator->rdev;
1356 mutex_lock(&rdev->mutex);
1359 if (!rdev->desc->ops->set_mode) {
1364 /* constraints check */
1365 ret = regulator_check_mode(rdev, mode);
1369 ret = rdev->desc->ops->set_mode(rdev, mode);
1371 mutex_unlock(&rdev->mutex);
1374 EXPORT_SYMBOL_GPL(regulator_set_mode);
1376 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1380 mutex_lock(&rdev->mutex);
1383 if (!rdev->desc->ops->get_mode) {
1388 ret = rdev->desc->ops->get_mode(rdev);
1390 mutex_unlock(&rdev->mutex);
1395 * regulator_get_mode - get regulator operating mode
1396 * @regulator: regulator source
1398 * Get the current regulator operating mode.
1400 unsigned int regulator_get_mode(struct regulator *regulator)
1402 return _regulator_get_mode(regulator->rdev);
1404 EXPORT_SYMBOL_GPL(regulator_get_mode);
1407 * regulator_set_optimum_mode - set regulator optimum operating mode
1408 * @regulator: regulator source
1409 * @uA_load: load current
1411 * Notifies the regulator core of a new device load. This is then used by
1412 * DRMS (if enabled by constraints) to set the most efficient regulator
1413 * operating mode for the new regulator loading.
1415 * Consumer devices notify their supply regulator of the maximum power
1416 * they will require (can be taken from device datasheet in the power
1417 * consumption tables) when they change operational status and hence power
1418 * state. Examples of operational state changes that can affect power
1419 * consumption are :-
1421 * o Device is opened / closed.
1422 * o Device I/O is about to begin or has just finished.
1423 * o Device is idling in between work.
1425 * This information is also exported via sysfs to userspace.
1427 * DRMS will sum the total requested load on the regulator and change
1428 * to the most efficient operating mode if platform constraints allow.
1430 * Returns the new regulator mode or error.
1432 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1434 struct regulator_dev *rdev = regulator->rdev;
1435 struct regulator *consumer;
1436 int ret, output_uV, input_uV, total_uA_load = 0;
1439 mutex_lock(&rdev->mutex);
1441 regulator->uA_load = uA_load;
1442 ret = regulator_check_drms(rdev);
1448 if (!rdev->desc->ops->get_optimum_mode)
1451 /* get output voltage */
1452 output_uV = rdev->desc->ops->get_voltage(rdev);
1453 if (output_uV <= 0) {
1454 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1455 __func__, rdev->desc->name);
1459 /* get input voltage */
1460 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1461 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1463 input_uV = rdev->constraints->input_uV;
1464 if (input_uV <= 0) {
1465 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1466 __func__, rdev->desc->name);
1470 /* calc total requested load for this regulator */
1471 list_for_each_entry(consumer, &rdev->consumer_list, list)
1472 total_uA_load += consumer->uA_load;
1474 mode = rdev->desc->ops->get_optimum_mode(rdev,
1475 input_uV, output_uV,
1477 ret = regulator_check_mode(rdev, mode);
1479 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1480 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1481 total_uA_load, input_uV, output_uV);
1485 ret = rdev->desc->ops->set_mode(rdev, mode);
1487 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1488 __func__, mode, rdev->desc->name);
1493 mutex_unlock(&rdev->mutex);
1496 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1499 * regulator_register_notifier - register regulator event notifier
1500 * @regulator: regulator source
1501 * @nb: notifier block
1503 * Register notifier block to receive regulator events.
1505 int regulator_register_notifier(struct regulator *regulator,
1506 struct notifier_block *nb)
1508 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1511 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1514 * regulator_unregister_notifier - unregister regulator event notifier
1515 * @regulator: regulator source
1516 * @nb: notifier block
1518 * Unregister regulator event notifier block.
1520 int regulator_unregister_notifier(struct regulator *regulator,
1521 struct notifier_block *nb)
1523 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1526 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1528 /* notify regulator consumers and downstream regulator consumers */
1529 static void _notifier_call_chain(struct regulator_dev *rdev,
1530 unsigned long event, void *data)
1532 struct regulator_dev *_rdev;
1534 /* call rdev chain first */
1535 mutex_lock(&rdev->mutex);
1536 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1537 mutex_unlock(&rdev->mutex);
1539 /* now notify regulator we supply */
1540 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1541 _notifier_call_chain(_rdev, event, data);
1545 * regulator_bulk_get - get multiple regulator consumers
1547 * @dev: Device to supply
1548 * @num_consumers: Number of consumers to register
1549 * @consumers: Configuration of consumers; clients are stored here.
1551 * @return 0 on success, an errno on failure.
1553 * This helper function allows drivers to get several regulator
1554 * consumers in one operation. If any of the regulators cannot be
1555 * acquired then any regulators that were allocated will be freed
1556 * before returning to the caller.
1558 int regulator_bulk_get(struct device *dev, int num_consumers,
1559 struct regulator_bulk_data *consumers)
1564 for (i = 0; i < num_consumers; i++)
1565 consumers[i].consumer = NULL;
1567 for (i = 0; i < num_consumers; i++) {
1568 consumers[i].consumer = regulator_get(dev,
1569 consumers[i].supply);
1570 if (IS_ERR(consumers[i].consumer)) {
1571 dev_err(dev, "Failed to get supply '%s'\n",
1572 consumers[i].supply);
1573 ret = PTR_ERR(consumers[i].consumer);
1574 consumers[i].consumer = NULL;
1582 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1583 regulator_put(consumers[i].consumer);
1587 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1590 * regulator_bulk_enable - enable multiple regulator consumers
1592 * @num_consumers: Number of consumers
1593 * @consumers: Consumer data; clients are stored here.
1594 * @return 0 on success, an errno on failure
1596 * This convenience API allows consumers to enable multiple regulator
1597 * clients in a single API call. If any consumers cannot be enabled
1598 * then any others that were enabled will be disabled again prior to
1601 int regulator_bulk_enable(int num_consumers,
1602 struct regulator_bulk_data *consumers)
1607 for (i = 0; i < num_consumers; i++) {
1608 ret = regulator_enable(consumers[i].consumer);
1616 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1617 for (i = 0; i < num_consumers; i++)
1618 regulator_disable(consumers[i].consumer);
1622 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1625 * regulator_bulk_disable - disable multiple regulator consumers
1627 * @num_consumers: Number of consumers
1628 * @consumers: Consumer data; clients are stored here.
1629 * @return 0 on success, an errno on failure
1631 * This convenience API allows consumers to disable multiple regulator
1632 * clients in a single API call. If any consumers cannot be enabled
1633 * then any others that were disabled will be disabled again prior to
1636 int regulator_bulk_disable(int num_consumers,
1637 struct regulator_bulk_data *consumers)
1642 for (i = 0; i < num_consumers; i++) {
1643 ret = regulator_disable(consumers[i].consumer);
1651 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1652 for (i = 0; i < num_consumers; i++)
1653 regulator_enable(consumers[i].consumer);
1657 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1660 * regulator_bulk_free - free multiple regulator consumers
1662 * @num_consumers: Number of consumers
1663 * @consumers: Consumer data; clients are stored here.
1665 * This convenience API allows consumers to free multiple regulator
1666 * clients in a single API call.
1668 void regulator_bulk_free(int num_consumers,
1669 struct regulator_bulk_data *consumers)
1673 for (i = 0; i < num_consumers; i++) {
1674 regulator_put(consumers[i].consumer);
1675 consumers[i].consumer = NULL;
1678 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1681 * regulator_notifier_call_chain - call regulator event notifier
1682 * @rdev: regulator source
1683 * @event: notifier block
1684 * @data: callback-specific data.
1686 * Called by regulator drivers to notify clients a regulator event has
1687 * occurred. We also notify regulator clients downstream.
1689 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1690 unsigned long event, void *data)
1692 _notifier_call_chain(rdev, event, data);
1696 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1699 * To avoid cluttering sysfs (and memory) with useless state, only
1700 * create attributes that can be meaningfully displayed.
1702 static int add_regulator_attributes(struct regulator_dev *rdev)
1704 struct device *dev = &rdev->dev;
1705 struct regulator_ops *ops = rdev->desc->ops;
1708 /* some attributes need specific methods to be displayed */
1709 if (ops->get_voltage) {
1710 status = device_create_file(dev, &dev_attr_microvolts);
1714 if (ops->get_current_limit) {
1715 status = device_create_file(dev, &dev_attr_microamps);
1719 if (ops->get_mode) {
1720 status = device_create_file(dev, &dev_attr_opmode);
1724 if (ops->is_enabled) {
1725 status = device_create_file(dev, &dev_attr_state);
1730 /* some attributes are type-specific */
1731 if (rdev->desc->type == REGULATOR_CURRENT) {
1732 status = device_create_file(dev, &dev_attr_requested_microamps);
1737 /* all the other attributes exist to support constraints;
1738 * don't show them if there are no constraints, or if the
1739 * relevant supporting methods are missing.
1741 if (!rdev->constraints)
1744 /* constraints need specific supporting methods */
1745 if (ops->set_voltage) {
1746 status = device_create_file(dev, &dev_attr_min_microvolts);
1749 status = device_create_file(dev, &dev_attr_max_microvolts);
1753 if (ops->set_current_limit) {
1754 status = device_create_file(dev, &dev_attr_min_microamps);
1757 status = device_create_file(dev, &dev_attr_max_microamps);
1762 /* suspend mode constraints need multiple supporting methods */
1763 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1766 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1769 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1772 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1776 if (ops->set_suspend_voltage) {
1777 status = device_create_file(dev,
1778 &dev_attr_suspend_standby_microvolts);
1781 status = device_create_file(dev,
1782 &dev_attr_suspend_mem_microvolts);
1785 status = device_create_file(dev,
1786 &dev_attr_suspend_disk_microvolts);
1791 if (ops->set_suspend_mode) {
1792 status = device_create_file(dev,
1793 &dev_attr_suspend_standby_mode);
1796 status = device_create_file(dev,
1797 &dev_attr_suspend_mem_mode);
1800 status = device_create_file(dev,
1801 &dev_attr_suspend_disk_mode);
1810 * regulator_register - register regulator
1811 * @regulator_desc: regulator to register
1812 * @dev: struct device for the regulator
1813 * @driver_data: private regulator data
1815 * Called by regulator drivers to register a regulator.
1816 * Returns 0 on success.
1818 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1819 struct device *dev, void *driver_data)
1821 static atomic_t regulator_no = ATOMIC_INIT(0);
1822 struct regulator_dev *rdev;
1823 struct regulator_init_data *init_data = dev->platform_data;
1826 if (regulator_desc == NULL)
1827 return ERR_PTR(-EINVAL);
1829 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1830 return ERR_PTR(-EINVAL);
1832 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1833 !regulator_desc->type == REGULATOR_CURRENT)
1834 return ERR_PTR(-EINVAL);
1837 return ERR_PTR(-EINVAL);
1839 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1841 return ERR_PTR(-ENOMEM);
1843 mutex_lock(®ulator_list_mutex);
1845 mutex_init(&rdev->mutex);
1846 rdev->reg_data = driver_data;
1847 rdev->owner = regulator_desc->owner;
1848 rdev->desc = regulator_desc;
1849 INIT_LIST_HEAD(&rdev->consumer_list);
1850 INIT_LIST_HEAD(&rdev->supply_list);
1851 INIT_LIST_HEAD(&rdev->list);
1852 INIT_LIST_HEAD(&rdev->slist);
1853 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1855 /* preform any regulator specific init */
1856 if (init_data->regulator_init) {
1857 ret = init_data->regulator_init(rdev->reg_data);
1862 /* register with sysfs */
1863 rdev->dev.class = ®ulator_class;
1864 rdev->dev.parent = dev;
1865 dev_set_name(&rdev->dev, "regulator.%d",
1866 atomic_inc_return(®ulator_no) - 1);
1867 ret = device_register(&rdev->dev);
1871 dev_set_drvdata(&rdev->dev, rdev);
1873 /* set regulator constraints */
1874 ret = set_machine_constraints(rdev, &init_data->constraints);
1878 /* add attributes supported by this regulator */
1879 ret = add_regulator_attributes(rdev);
1883 /* set supply regulator if it exists */
1884 if (init_data->supply_regulator_dev) {
1885 ret = set_supply(rdev,
1886 dev_get_drvdata(init_data->supply_regulator_dev));
1891 /* add consumers devices */
1892 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1893 ret = set_consumer_device_supply(rdev,
1894 init_data->consumer_supplies[i].dev,
1895 init_data->consumer_supplies[i].supply);
1897 for (--i; i >= 0; i--)
1898 unset_consumer_device_supply(rdev,
1899 init_data->consumer_supplies[i].dev);
1904 list_add(&rdev->list, ®ulator_list);
1906 mutex_unlock(®ulator_list_mutex);
1910 device_unregister(&rdev->dev);
1913 rdev = ERR_PTR(ret);
1916 EXPORT_SYMBOL_GPL(regulator_register);
1919 * regulator_unregister - unregister regulator
1920 * @rdev: regulator to unregister
1922 * Called by regulator drivers to unregister a regulator.
1924 void regulator_unregister(struct regulator_dev *rdev)
1929 mutex_lock(®ulator_list_mutex);
1930 list_del(&rdev->list);
1932 sysfs_remove_link(&rdev->dev.kobj, "supply");
1933 device_unregister(&rdev->dev);
1934 mutex_unlock(®ulator_list_mutex);
1936 EXPORT_SYMBOL_GPL(regulator_unregister);
1939 * regulator_suspend_prepare: prepare regulators for system wide suspend
1940 * @state: system suspend state
1942 * Configure each regulator with it's suspend operating parameters for state.
1943 * This will usually be called by machine suspend code prior to supending.
1945 int regulator_suspend_prepare(suspend_state_t state)
1947 struct regulator_dev *rdev;
1950 /* ON is handled by regulator active state */
1951 if (state == PM_SUSPEND_ON)
1954 mutex_lock(®ulator_list_mutex);
1955 list_for_each_entry(rdev, ®ulator_list, list) {
1957 mutex_lock(&rdev->mutex);
1958 ret = suspend_prepare(rdev, state);
1959 mutex_unlock(&rdev->mutex);
1962 printk(KERN_ERR "%s: failed to prepare %s\n",
1963 __func__, rdev->desc->name);
1968 mutex_unlock(®ulator_list_mutex);
1971 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1974 * rdev_get_drvdata - get rdev regulator driver data
1977 * Get rdev regulator driver private data. This call can be used in the
1978 * regulator driver context.
1980 void *rdev_get_drvdata(struct regulator_dev *rdev)
1982 return rdev->reg_data;
1984 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1987 * regulator_get_drvdata - get regulator driver data
1988 * @regulator: regulator
1990 * Get regulator driver private data. This call can be used in the consumer
1991 * driver context when non API regulator specific functions need to be called.
1993 void *regulator_get_drvdata(struct regulator *regulator)
1995 return regulator->rdev->reg_data;
1997 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2000 * regulator_set_drvdata - set regulator driver data
2001 * @regulator: regulator
2004 void regulator_set_drvdata(struct regulator *regulator, void *data)
2006 regulator->rdev->reg_data = data;
2008 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2011 * regulator_get_id - get regulator ID
2014 int rdev_get_id(struct regulator_dev *rdev)
2016 return rdev->desc->id;
2018 EXPORT_SYMBOL_GPL(rdev_get_id);
2020 struct device *rdev_get_dev(struct regulator_dev *rdev)
2024 EXPORT_SYMBOL_GPL(rdev_get_dev);
2026 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2028 return reg_init_data->driver_data;
2030 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2032 static int __init regulator_init(void)
2034 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2035 return class_register(®ulator_class);
2038 /* init early to allow our consumers to complete system booting */
2039 core_initcall(regulator_init);