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_status_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct regulator_dev *rdev = dev_get_drvdata(dev);
322 status = rdev->desc->ops->get_status(rdev);
327 case REGULATOR_STATUS_OFF:
330 case REGULATOR_STATUS_ON:
333 case REGULATOR_STATUS_ERROR:
336 case REGULATOR_STATUS_FAST:
339 case REGULATOR_STATUS_NORMAL:
342 case REGULATOR_STATUS_IDLE:
345 case REGULATOR_STATUS_STANDBY:
352 return sprintf(buf, "%s\n", label);
354 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
356 static ssize_t regulator_min_uA_show(struct device *dev,
357 struct device_attribute *attr, char *buf)
359 struct regulator_dev *rdev = dev_get_drvdata(dev);
361 if (!rdev->constraints)
362 return sprintf(buf, "constraint not defined\n");
364 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
366 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
368 static ssize_t regulator_max_uA_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
373 if (!rdev->constraints)
374 return sprintf(buf, "constraint not defined\n");
376 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
378 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
380 static ssize_t regulator_min_uV_show(struct device *dev,
381 struct device_attribute *attr, char *buf)
383 struct regulator_dev *rdev = dev_get_drvdata(dev);
385 if (!rdev->constraints)
386 return sprintf(buf, "constraint not defined\n");
388 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
390 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
392 static ssize_t regulator_max_uV_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
395 struct regulator_dev *rdev = dev_get_drvdata(dev);
397 if (!rdev->constraints)
398 return sprintf(buf, "constraint not defined\n");
400 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
402 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
404 static ssize_t regulator_total_uA_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 struct regulator_dev *rdev = dev_get_drvdata(dev);
408 struct regulator *regulator;
411 mutex_lock(&rdev->mutex);
412 list_for_each_entry(regulator, &rdev->consumer_list, list)
413 uA += regulator->uA_load;
414 mutex_unlock(&rdev->mutex);
415 return sprintf(buf, "%d\n", uA);
417 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
419 static ssize_t regulator_num_users_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
422 struct regulator_dev *rdev = dev_get_drvdata(dev);
423 return sprintf(buf, "%d\n", rdev->use_count);
426 static ssize_t regulator_type_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
429 struct regulator_dev *rdev = dev_get_drvdata(dev);
431 switch (rdev->desc->type) {
432 case REGULATOR_VOLTAGE:
433 return sprintf(buf, "voltage\n");
434 case REGULATOR_CURRENT:
435 return sprintf(buf, "current\n");
437 return sprintf(buf, "unknown\n");
440 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct regulator_dev *rdev = dev_get_drvdata(dev);
445 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
447 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
448 regulator_suspend_mem_uV_show, NULL);
450 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 struct regulator_dev *rdev = dev_get_drvdata(dev);
455 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
457 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
458 regulator_suspend_disk_uV_show, NULL);
460 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
463 struct regulator_dev *rdev = dev_get_drvdata(dev);
465 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
467 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
468 regulator_suspend_standby_uV_show, NULL);
470 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
473 struct regulator_dev *rdev = dev_get_drvdata(dev);
475 return regulator_print_opmode(buf,
476 rdev->constraints->state_mem.mode);
478 static DEVICE_ATTR(suspend_mem_mode, 0444,
479 regulator_suspend_mem_mode_show, NULL);
481 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
484 struct regulator_dev *rdev = dev_get_drvdata(dev);
486 return regulator_print_opmode(buf,
487 rdev->constraints->state_disk.mode);
489 static DEVICE_ATTR(suspend_disk_mode, 0444,
490 regulator_suspend_disk_mode_show, NULL);
492 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
495 struct regulator_dev *rdev = dev_get_drvdata(dev);
497 return regulator_print_opmode(buf,
498 rdev->constraints->state_standby.mode);
500 static DEVICE_ATTR(suspend_standby_mode, 0444,
501 regulator_suspend_standby_mode_show, NULL);
503 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
506 struct regulator_dev *rdev = dev_get_drvdata(dev);
508 return regulator_print_state(buf,
509 rdev->constraints->state_mem.enabled);
511 static DEVICE_ATTR(suspend_mem_state, 0444,
512 regulator_suspend_mem_state_show, NULL);
514 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
517 struct regulator_dev *rdev = dev_get_drvdata(dev);
519 return regulator_print_state(buf,
520 rdev->constraints->state_disk.enabled);
522 static DEVICE_ATTR(suspend_disk_state, 0444,
523 regulator_suspend_disk_state_show, NULL);
525 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
528 struct regulator_dev *rdev = dev_get_drvdata(dev);
530 return regulator_print_state(buf,
531 rdev->constraints->state_standby.enabled);
533 static DEVICE_ATTR(suspend_standby_state, 0444,
534 regulator_suspend_standby_state_show, NULL);
538 * These are the only attributes are present for all regulators.
539 * Other attributes are a function of regulator functionality.
541 static struct device_attribute regulator_dev_attrs[] = {
542 __ATTR(name, 0444, regulator_name_show, NULL),
543 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
544 __ATTR(type, 0444, regulator_type_show, NULL),
548 static void regulator_dev_release(struct device *dev)
550 struct regulator_dev *rdev = dev_get_drvdata(dev);
554 static struct class regulator_class = {
556 .dev_release = regulator_dev_release,
557 .dev_attrs = regulator_dev_attrs,
560 /* Calculate the new optimum regulator operating mode based on the new total
561 * consumer load. All locks held by caller */
562 static void drms_uA_update(struct regulator_dev *rdev)
564 struct regulator *sibling;
565 int current_uA = 0, output_uV, input_uV, err;
568 err = regulator_check_drms(rdev);
569 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
570 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
573 /* get output voltage */
574 output_uV = rdev->desc->ops->get_voltage(rdev);
578 /* get input voltage */
579 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
580 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
582 input_uV = rdev->constraints->input_uV;
586 /* calc total requested load */
587 list_for_each_entry(sibling, &rdev->consumer_list, list)
588 current_uA += sibling->uA_load;
590 /* now get the optimum mode for our new total regulator load */
591 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
592 output_uV, current_uA);
594 /* check the new mode is allowed */
595 err = regulator_check_mode(rdev, mode);
597 rdev->desc->ops->set_mode(rdev, mode);
600 static int suspend_set_state(struct regulator_dev *rdev,
601 struct regulator_state *rstate)
605 /* enable & disable are mandatory for suspend control */
606 if (!rdev->desc->ops->set_suspend_enable ||
607 !rdev->desc->ops->set_suspend_disable) {
608 printk(KERN_ERR "%s: no way to set suspend state\n",
614 ret = rdev->desc->ops->set_suspend_enable(rdev);
616 ret = rdev->desc->ops->set_suspend_disable(rdev);
618 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
622 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
623 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
625 printk(KERN_ERR "%s: failed to set voltage\n",
631 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
632 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
634 printk(KERN_ERR "%s: failed to set mode\n", __func__);
641 /* locks held by caller */
642 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
644 if (!rdev->constraints)
648 case PM_SUSPEND_STANDBY:
649 return suspend_set_state(rdev,
650 &rdev->constraints->state_standby);
652 return suspend_set_state(rdev,
653 &rdev->constraints->state_mem);
655 return suspend_set_state(rdev,
656 &rdev->constraints->state_disk);
662 static void print_constraints(struct regulator_dev *rdev)
664 struct regulation_constraints *constraints = rdev->constraints;
668 if (rdev->desc->type == REGULATOR_VOLTAGE) {
669 if (constraints->min_uV == constraints->max_uV)
670 count = sprintf(buf, "%d mV ",
671 constraints->min_uV / 1000);
673 count = sprintf(buf, "%d <--> %d mV ",
674 constraints->min_uV / 1000,
675 constraints->max_uV / 1000);
677 if (constraints->min_uA == constraints->max_uA)
678 count = sprintf(buf, "%d mA ",
679 constraints->min_uA / 1000);
681 count = sprintf(buf, "%d <--> %d mA ",
682 constraints->min_uA / 1000,
683 constraints->max_uA / 1000);
685 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
686 count += sprintf(buf + count, "fast ");
687 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
688 count += sprintf(buf + count, "normal ");
689 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
690 count += sprintf(buf + count, "idle ");
691 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
692 count += sprintf(buf + count, "standby");
694 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
698 * set_machine_constraints - sets regulator constraints
699 * @rdev: regulator source
700 * @constraints: constraints to apply
702 * Allows platform initialisation code to define and constrain
703 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
704 * Constraints *must* be set by platform code in order for some
705 * regulator operations to proceed i.e. set_voltage, set_current_limit,
708 static int set_machine_constraints(struct regulator_dev *rdev,
709 struct regulation_constraints *constraints)
713 struct regulator_ops *ops = rdev->desc->ops;
715 if (constraints->name)
716 name = constraints->name;
717 else if (rdev->desc->name)
718 name = rdev->desc->name;
722 rdev->constraints = constraints;
724 /* do we need to apply the constraint voltage */
725 if (rdev->constraints->apply_uV &&
726 rdev->constraints->min_uV == rdev->constraints->max_uV &&
728 ret = ops->set_voltage(rdev,
729 rdev->constraints->min_uV, rdev->constraints->max_uV);
731 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
733 rdev->constraints->min_uV, name);
734 rdev->constraints = NULL;
739 /* are we enabled at boot time by firmware / bootloader */
740 if (rdev->constraints->boot_on)
743 /* do we need to setup our suspend state */
744 if (constraints->initial_state) {
745 ret = suspend_prepare(rdev, constraints->initial_state);
747 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
749 rdev->constraints = NULL;
754 /* if always_on is set then turn the regulator on if it's not
756 if (constraints->always_on && ops->enable &&
757 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
758 (!ops->is_enabled && !constraints->boot_on))) {
759 ret = ops->enable(rdev);
761 printk(KERN_ERR "%s: failed to enable %s\n",
763 rdev->constraints = NULL;
768 print_constraints(rdev);
774 * set_supply - set regulator supply regulator
775 * @rdev: regulator name
776 * @supply_rdev: supply regulator name
778 * Called by platform initialisation code to set the supply regulator for this
779 * regulator. This ensures that a regulators supply will also be enabled by the
780 * core if it's child is enabled.
782 static int set_supply(struct regulator_dev *rdev,
783 struct regulator_dev *supply_rdev)
787 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
791 "%s: could not add device link %s err %d\n",
792 __func__, supply_rdev->dev.kobj.name, err);
795 rdev->supply = supply_rdev;
796 list_add(&rdev->slist, &supply_rdev->supply_list);
802 * set_consumer_device_supply: Bind a regulator to a symbolic supply
803 * @rdev: regulator source
804 * @consumer_dev: device the supply applies to
805 * @supply: symbolic name for supply
807 * Allows platform initialisation code to map physical regulator
808 * sources to symbolic names for supplies for use by devices. Devices
809 * should use these symbolic names to request regulators, avoiding the
810 * need to provide board-specific regulator names as platform data.
812 static int set_consumer_device_supply(struct regulator_dev *rdev,
813 struct device *consumer_dev, const char *supply)
815 struct regulator_map *node;
820 list_for_each_entry(node, ®ulator_map_list, list) {
821 if (consumer_dev != node->dev)
823 if (strcmp(node->supply, supply) != 0)
826 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
827 dev_name(&node->regulator->dev),
828 node->regulator->desc->name,
830 dev_name(&rdev->dev), rdev->desc->name);
834 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
838 node->regulator = rdev;
839 node->dev = consumer_dev;
840 node->supply = supply;
842 list_add(&node->list, ®ulator_map_list);
846 static void unset_consumer_device_supply(struct regulator_dev *rdev,
847 struct device *consumer_dev)
849 struct regulator_map *node, *n;
851 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
852 if (rdev == node->regulator &&
853 consumer_dev == node->dev) {
854 list_del(&node->list);
861 #define REG_STR_SIZE 32
863 static struct regulator *create_regulator(struct regulator_dev *rdev,
865 const char *supply_name)
867 struct regulator *regulator;
868 char buf[REG_STR_SIZE];
871 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
872 if (regulator == NULL)
875 mutex_lock(&rdev->mutex);
876 regulator->rdev = rdev;
877 list_add(®ulator->list, &rdev->consumer_list);
880 /* create a 'requested_microamps_name' sysfs entry */
881 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
883 if (size >= REG_STR_SIZE)
886 regulator->dev = dev;
887 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
888 if (regulator->dev_attr.attr.name == NULL)
891 regulator->dev_attr.attr.owner = THIS_MODULE;
892 regulator->dev_attr.attr.mode = 0444;
893 regulator->dev_attr.show = device_requested_uA_show;
894 err = device_create_file(dev, ®ulator->dev_attr);
896 printk(KERN_WARNING "%s: could not add regulator_dev"
897 " load sysfs\n", __func__);
901 /* also add a link to the device sysfs entry */
902 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
903 dev->kobj.name, supply_name);
904 if (size >= REG_STR_SIZE)
907 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
908 if (regulator->supply_name == NULL)
911 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
915 "%s: could not add device link %s err %d\n",
916 __func__, dev->kobj.name, err);
917 device_remove_file(dev, ®ulator->dev_attr);
921 mutex_unlock(&rdev->mutex);
924 kfree(regulator->supply_name);
926 device_remove_file(regulator->dev, ®ulator->dev_attr);
928 kfree(regulator->dev_attr.attr.name);
930 list_del(®ulator->list);
932 mutex_unlock(&rdev->mutex);
937 * regulator_get - lookup and obtain a reference to a regulator.
938 * @dev: device for regulator "consumer"
939 * @id: Supply name or regulator ID.
941 * Returns a struct regulator corresponding to the regulator producer,
942 * or IS_ERR() condition containing errno. Use of supply names
943 * configured via regulator_set_device_supply() is strongly
946 struct regulator *regulator_get(struct device *dev, const char *id)
948 struct regulator_dev *rdev;
949 struct regulator_map *map;
950 struct regulator *regulator = ERR_PTR(-ENODEV);
953 printk(KERN_ERR "regulator: get() with no identifier\n");
957 mutex_lock(®ulator_list_mutex);
959 list_for_each_entry(map, ®ulator_map_list, list) {
960 if (dev == map->dev &&
961 strcmp(map->supply, id) == 0) {
962 rdev = map->regulator;
966 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
968 mutex_unlock(®ulator_list_mutex);
972 if (!try_module_get(rdev->owner))
975 regulator = create_regulator(rdev, dev, id);
976 if (regulator == NULL) {
977 regulator = ERR_PTR(-ENOMEM);
978 module_put(rdev->owner);
982 mutex_unlock(®ulator_list_mutex);
985 EXPORT_SYMBOL_GPL(regulator_get);
988 * regulator_put - "free" the regulator source
989 * @regulator: regulator source
991 * Note: drivers must ensure that all regulator_enable calls made on this
992 * regulator source are balanced by regulator_disable calls prior to calling
995 void regulator_put(struct regulator *regulator)
997 struct regulator_dev *rdev;
999 if (regulator == NULL || IS_ERR(regulator))
1002 mutex_lock(®ulator_list_mutex);
1003 rdev = regulator->rdev;
1005 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
1006 regulator->supply_name))
1007 _regulator_disable(rdev);
1009 /* remove any sysfs entries */
1010 if (regulator->dev) {
1011 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1012 kfree(regulator->supply_name);
1013 device_remove_file(regulator->dev, ®ulator->dev_attr);
1014 kfree(regulator->dev_attr.attr.name);
1016 list_del(®ulator->list);
1019 module_put(rdev->owner);
1020 mutex_unlock(®ulator_list_mutex);
1022 EXPORT_SYMBOL_GPL(regulator_put);
1024 /* locks held by regulator_enable() */
1025 static int _regulator_enable(struct regulator_dev *rdev)
1029 if (!rdev->constraints) {
1030 printk(KERN_ERR "%s: %s has no constraints\n",
1031 __func__, rdev->desc->name);
1035 /* do we need to enable the supply regulator first */
1037 ret = _regulator_enable(rdev->supply);
1039 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1040 __func__, rdev->desc->name, ret);
1045 /* check voltage and requested load before enabling */
1046 if (rdev->desc->ops->enable) {
1048 if (rdev->constraints &&
1049 (rdev->constraints->valid_ops_mask &
1050 REGULATOR_CHANGE_DRMS))
1051 drms_uA_update(rdev);
1053 ret = rdev->desc->ops->enable(rdev);
1055 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1056 __func__, rdev->desc->name, ret);
1067 * regulator_enable - enable regulator output
1068 * @regulator: regulator source
1070 * Request that the regulator be enabled with the regulator output at
1071 * the predefined voltage or current value. Calls to regulator_enable()
1072 * must be balanced with calls to regulator_disable().
1074 * NOTE: the output value can be set by other drivers, boot loader or may be
1075 * hardwired in the regulator.
1077 int regulator_enable(struct regulator *regulator)
1079 struct regulator_dev *rdev = regulator->rdev;
1082 mutex_lock(&rdev->mutex);
1083 if (regulator->enabled == 0)
1084 ret = _regulator_enable(rdev);
1085 else if (regulator->enabled < 0)
1088 regulator->enabled++;
1089 mutex_unlock(&rdev->mutex);
1092 EXPORT_SYMBOL_GPL(regulator_enable);
1094 /* locks held by regulator_disable() */
1095 static int _regulator_disable(struct regulator_dev *rdev)
1099 /* are we the last user and permitted to disable ? */
1100 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1102 /* we are last user */
1103 if (rdev->desc->ops->disable) {
1104 ret = rdev->desc->ops->disable(rdev);
1106 printk(KERN_ERR "%s: failed to disable %s\n",
1107 __func__, rdev->desc->name);
1112 /* decrease our supplies ref count and disable if required */
1114 _regulator_disable(rdev->supply);
1116 rdev->use_count = 0;
1117 } else if (rdev->use_count > 1) {
1119 if (rdev->constraints &&
1120 (rdev->constraints->valid_ops_mask &
1121 REGULATOR_CHANGE_DRMS))
1122 drms_uA_update(rdev);
1130 * regulator_disable - disable regulator output
1131 * @regulator: regulator source
1133 * Disable the regulator output voltage or current. Calls to
1134 * regulator_enable() must be balanced with calls to
1135 * regulator_disable().
1137 * NOTE: this will only disable the regulator output if no other consumer
1138 * devices have it enabled, the regulator device supports disabling and
1139 * machine constraints permit this operation.
1141 int regulator_disable(struct regulator *regulator)
1143 struct regulator_dev *rdev = regulator->rdev;
1146 mutex_lock(&rdev->mutex);
1147 if (regulator->enabled == 1) {
1148 ret = _regulator_disable(rdev);
1150 regulator->uA_load = 0;
1151 } else if (WARN(regulator->enabled <= 0,
1152 "unbalanced disables for supply %s\n",
1153 regulator->supply_name))
1156 regulator->enabled--;
1157 mutex_unlock(&rdev->mutex);
1160 EXPORT_SYMBOL_GPL(regulator_disable);
1162 /* locks held by regulator_force_disable() */
1163 static int _regulator_force_disable(struct regulator_dev *rdev)
1168 if (rdev->desc->ops->disable) {
1169 /* ah well, who wants to live forever... */
1170 ret = rdev->desc->ops->disable(rdev);
1172 printk(KERN_ERR "%s: failed to force disable %s\n",
1173 __func__, rdev->desc->name);
1176 /* notify other consumers that power has been forced off */
1177 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1181 /* decrease our supplies ref count and disable if required */
1183 _regulator_disable(rdev->supply);
1185 rdev->use_count = 0;
1190 * regulator_force_disable - force disable regulator output
1191 * @regulator: regulator source
1193 * Forcibly disable the regulator output voltage or current.
1194 * NOTE: this *will* disable the regulator output even if other consumer
1195 * devices have it enabled. This should be used for situations when device
1196 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1198 int regulator_force_disable(struct regulator *regulator)
1202 mutex_lock(®ulator->rdev->mutex);
1203 regulator->enabled = 0;
1204 regulator->uA_load = 0;
1205 ret = _regulator_force_disable(regulator->rdev);
1206 mutex_unlock(®ulator->rdev->mutex);
1209 EXPORT_SYMBOL_GPL(regulator_force_disable);
1211 static int _regulator_is_enabled(struct regulator_dev *rdev)
1215 mutex_lock(&rdev->mutex);
1218 if (!rdev->desc->ops->is_enabled) {
1223 ret = rdev->desc->ops->is_enabled(rdev);
1225 mutex_unlock(&rdev->mutex);
1230 * regulator_is_enabled - is the regulator output enabled
1231 * @regulator: regulator source
1233 * Returns positive if the regulator driver backing the source/client
1234 * has requested that the device be enabled, zero if it hasn't, else a
1235 * negative errno code.
1237 * Note that the device backing this regulator handle can have multiple
1238 * users, so it might be enabled even if regulator_enable() was never
1239 * called for this particular source.
1241 int regulator_is_enabled(struct regulator *regulator)
1243 return _regulator_is_enabled(regulator->rdev);
1245 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1248 * regulator_set_voltage - set regulator output voltage
1249 * @regulator: regulator source
1250 * @min_uV: Minimum required voltage in uV
1251 * @max_uV: Maximum acceptable voltage in uV
1253 * Sets a voltage regulator to the desired output voltage. This can be set
1254 * during any regulator state. IOW, regulator can be disabled or enabled.
1256 * If the regulator is enabled then the voltage will change to the new value
1257 * immediately otherwise if the regulator is disabled the regulator will
1258 * output at the new voltage when enabled.
1260 * NOTE: If the regulator is shared between several devices then the lowest
1261 * request voltage that meets the system constraints will be used.
1262 * Regulator system constraints must be set for this regulator before
1263 * calling this function otherwise this call will fail.
1265 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1267 struct regulator_dev *rdev = regulator->rdev;
1270 mutex_lock(&rdev->mutex);
1273 if (!rdev->desc->ops->set_voltage) {
1278 /* constraints check */
1279 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1282 regulator->min_uV = min_uV;
1283 regulator->max_uV = max_uV;
1284 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1287 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1288 mutex_unlock(&rdev->mutex);
1291 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1293 static int _regulator_get_voltage(struct regulator_dev *rdev)
1296 if (rdev->desc->ops->get_voltage)
1297 return rdev->desc->ops->get_voltage(rdev);
1303 * regulator_get_voltage - get regulator output voltage
1304 * @regulator: regulator source
1306 * This returns the current regulator voltage in uV.
1308 * NOTE: If the regulator is disabled it will return the voltage value. This
1309 * function should not be used to determine regulator state.
1311 int regulator_get_voltage(struct regulator *regulator)
1315 mutex_lock(®ulator->rdev->mutex);
1317 ret = _regulator_get_voltage(regulator->rdev);
1319 mutex_unlock(®ulator->rdev->mutex);
1323 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1326 * regulator_set_current_limit - set regulator output current limit
1327 * @regulator: regulator source
1328 * @min_uA: Minimuum supported current in uA
1329 * @max_uA: Maximum supported current in uA
1331 * Sets current sink to the desired output current. This can be set during
1332 * any regulator state. IOW, regulator can be disabled or enabled.
1334 * If the regulator is enabled then the current will change to the new value
1335 * immediately otherwise if the regulator is disabled the regulator will
1336 * output at the new current when enabled.
1338 * NOTE: Regulator system constraints must be set for this regulator before
1339 * calling this function otherwise this call will fail.
1341 int regulator_set_current_limit(struct regulator *regulator,
1342 int min_uA, int max_uA)
1344 struct regulator_dev *rdev = regulator->rdev;
1347 mutex_lock(&rdev->mutex);
1350 if (!rdev->desc->ops->set_current_limit) {
1355 /* constraints check */
1356 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1360 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1362 mutex_unlock(&rdev->mutex);
1365 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1367 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1371 mutex_lock(&rdev->mutex);
1374 if (!rdev->desc->ops->get_current_limit) {
1379 ret = rdev->desc->ops->get_current_limit(rdev);
1381 mutex_unlock(&rdev->mutex);
1386 * regulator_get_current_limit - get regulator output current
1387 * @regulator: regulator source
1389 * This returns the current supplied by the specified current sink in uA.
1391 * NOTE: If the regulator is disabled it will return the current value. This
1392 * function should not be used to determine regulator state.
1394 int regulator_get_current_limit(struct regulator *regulator)
1396 return _regulator_get_current_limit(regulator->rdev);
1398 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1401 * regulator_set_mode - set regulator operating mode
1402 * @regulator: regulator source
1403 * @mode: operating mode - one of the REGULATOR_MODE constants
1405 * Set regulator operating mode to increase regulator efficiency or improve
1406 * regulation performance.
1408 * NOTE: Regulator system constraints must be set for this regulator before
1409 * calling this function otherwise this call will fail.
1411 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1413 struct regulator_dev *rdev = regulator->rdev;
1416 mutex_lock(&rdev->mutex);
1419 if (!rdev->desc->ops->set_mode) {
1424 /* constraints check */
1425 ret = regulator_check_mode(rdev, mode);
1429 ret = rdev->desc->ops->set_mode(rdev, mode);
1431 mutex_unlock(&rdev->mutex);
1434 EXPORT_SYMBOL_GPL(regulator_set_mode);
1436 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1440 mutex_lock(&rdev->mutex);
1443 if (!rdev->desc->ops->get_mode) {
1448 ret = rdev->desc->ops->get_mode(rdev);
1450 mutex_unlock(&rdev->mutex);
1455 * regulator_get_mode - get regulator operating mode
1456 * @regulator: regulator source
1458 * Get the current regulator operating mode.
1460 unsigned int regulator_get_mode(struct regulator *regulator)
1462 return _regulator_get_mode(regulator->rdev);
1464 EXPORT_SYMBOL_GPL(regulator_get_mode);
1467 * regulator_set_optimum_mode - set regulator optimum operating mode
1468 * @regulator: regulator source
1469 * @uA_load: load current
1471 * Notifies the regulator core of a new device load. This is then used by
1472 * DRMS (if enabled by constraints) to set the most efficient regulator
1473 * operating mode for the new regulator loading.
1475 * Consumer devices notify their supply regulator of the maximum power
1476 * they will require (can be taken from device datasheet in the power
1477 * consumption tables) when they change operational status and hence power
1478 * state. Examples of operational state changes that can affect power
1479 * consumption are :-
1481 * o Device is opened / closed.
1482 * o Device I/O is about to begin or has just finished.
1483 * o Device is idling in between work.
1485 * This information is also exported via sysfs to userspace.
1487 * DRMS will sum the total requested load on the regulator and change
1488 * to the most efficient operating mode if platform constraints allow.
1490 * Returns the new regulator mode or error.
1492 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1494 struct regulator_dev *rdev = regulator->rdev;
1495 struct regulator *consumer;
1496 int ret, output_uV, input_uV, total_uA_load = 0;
1499 mutex_lock(&rdev->mutex);
1501 regulator->uA_load = uA_load;
1502 ret = regulator_check_drms(rdev);
1508 if (!rdev->desc->ops->get_optimum_mode)
1511 /* get output voltage */
1512 output_uV = rdev->desc->ops->get_voltage(rdev);
1513 if (output_uV <= 0) {
1514 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1515 __func__, rdev->desc->name);
1519 /* get input voltage */
1520 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1521 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1523 input_uV = rdev->constraints->input_uV;
1524 if (input_uV <= 0) {
1525 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1526 __func__, rdev->desc->name);
1530 /* calc total requested load for this regulator */
1531 list_for_each_entry(consumer, &rdev->consumer_list, list)
1532 total_uA_load += consumer->uA_load;
1534 mode = rdev->desc->ops->get_optimum_mode(rdev,
1535 input_uV, output_uV,
1537 ret = regulator_check_mode(rdev, mode);
1539 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1540 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1541 total_uA_load, input_uV, output_uV);
1545 ret = rdev->desc->ops->set_mode(rdev, mode);
1547 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1548 __func__, mode, rdev->desc->name);
1553 mutex_unlock(&rdev->mutex);
1556 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1559 * regulator_register_notifier - register regulator event notifier
1560 * @regulator: regulator source
1561 * @nb: notifier block
1563 * Register notifier block to receive regulator events.
1565 int regulator_register_notifier(struct regulator *regulator,
1566 struct notifier_block *nb)
1568 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1571 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1574 * regulator_unregister_notifier - unregister regulator event notifier
1575 * @regulator: regulator source
1576 * @nb: notifier block
1578 * Unregister regulator event notifier block.
1580 int regulator_unregister_notifier(struct regulator *regulator,
1581 struct notifier_block *nb)
1583 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1586 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1588 /* notify regulator consumers and downstream regulator consumers.
1589 * Note mutex must be held by caller.
1591 static void _notifier_call_chain(struct regulator_dev *rdev,
1592 unsigned long event, void *data)
1594 struct regulator_dev *_rdev;
1596 /* call rdev chain first */
1597 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1599 /* now notify regulator we supply */
1600 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1601 mutex_lock(&_rdev->mutex);
1602 _notifier_call_chain(_rdev, event, data);
1603 mutex_unlock(&_rdev->mutex);
1608 * regulator_bulk_get - get multiple regulator consumers
1610 * @dev: Device to supply
1611 * @num_consumers: Number of consumers to register
1612 * @consumers: Configuration of consumers; clients are stored here.
1614 * @return 0 on success, an errno on failure.
1616 * This helper function allows drivers to get several regulator
1617 * consumers in one operation. If any of the regulators cannot be
1618 * acquired then any regulators that were allocated will be freed
1619 * before returning to the caller.
1621 int regulator_bulk_get(struct device *dev, int num_consumers,
1622 struct regulator_bulk_data *consumers)
1627 for (i = 0; i < num_consumers; i++)
1628 consumers[i].consumer = NULL;
1630 for (i = 0; i < num_consumers; i++) {
1631 consumers[i].consumer = regulator_get(dev,
1632 consumers[i].supply);
1633 if (IS_ERR(consumers[i].consumer)) {
1634 dev_err(dev, "Failed to get supply '%s'\n",
1635 consumers[i].supply);
1636 ret = PTR_ERR(consumers[i].consumer);
1637 consumers[i].consumer = NULL;
1645 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1646 regulator_put(consumers[i].consumer);
1650 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1653 * regulator_bulk_enable - enable multiple regulator consumers
1655 * @num_consumers: Number of consumers
1656 * @consumers: Consumer data; clients are stored here.
1657 * @return 0 on success, an errno on failure
1659 * This convenience API allows consumers to enable multiple regulator
1660 * clients in a single API call. If any consumers cannot be enabled
1661 * then any others that were enabled will be disabled again prior to
1664 int regulator_bulk_enable(int num_consumers,
1665 struct regulator_bulk_data *consumers)
1670 for (i = 0; i < num_consumers; i++) {
1671 ret = regulator_enable(consumers[i].consumer);
1679 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1680 for (i = 0; i < num_consumers; i++)
1681 regulator_disable(consumers[i].consumer);
1685 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1688 * regulator_bulk_disable - disable multiple regulator consumers
1690 * @num_consumers: Number of consumers
1691 * @consumers: Consumer data; clients are stored here.
1692 * @return 0 on success, an errno on failure
1694 * This convenience API allows consumers to disable multiple regulator
1695 * clients in a single API call. If any consumers cannot be enabled
1696 * then any others that were disabled will be disabled again prior to
1699 int regulator_bulk_disable(int num_consumers,
1700 struct regulator_bulk_data *consumers)
1705 for (i = 0; i < num_consumers; i++) {
1706 ret = regulator_disable(consumers[i].consumer);
1714 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1715 for (i = 0; i < num_consumers; i++)
1716 regulator_enable(consumers[i].consumer);
1720 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1723 * regulator_bulk_free - free multiple regulator consumers
1725 * @num_consumers: Number of consumers
1726 * @consumers: Consumer data; clients are stored here.
1728 * This convenience API allows consumers to free multiple regulator
1729 * clients in a single API call.
1731 void regulator_bulk_free(int num_consumers,
1732 struct regulator_bulk_data *consumers)
1736 for (i = 0; i < num_consumers; i++) {
1737 regulator_put(consumers[i].consumer);
1738 consumers[i].consumer = NULL;
1741 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1744 * regulator_notifier_call_chain - call regulator event notifier
1745 * @rdev: regulator source
1746 * @event: notifier block
1747 * @data: callback-specific data.
1749 * Called by regulator drivers to notify clients a regulator event has
1750 * occurred. We also notify regulator clients downstream.
1751 * Note lock must be held by caller.
1753 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1754 unsigned long event, void *data)
1756 _notifier_call_chain(rdev, event, data);
1760 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1763 * To avoid cluttering sysfs (and memory) with useless state, only
1764 * create attributes that can be meaningfully displayed.
1766 static int add_regulator_attributes(struct regulator_dev *rdev)
1768 struct device *dev = &rdev->dev;
1769 struct regulator_ops *ops = rdev->desc->ops;
1772 /* some attributes need specific methods to be displayed */
1773 if (ops->get_voltage) {
1774 status = device_create_file(dev, &dev_attr_microvolts);
1778 if (ops->get_current_limit) {
1779 status = device_create_file(dev, &dev_attr_microamps);
1783 if (ops->get_mode) {
1784 status = device_create_file(dev, &dev_attr_opmode);
1788 if (ops->is_enabled) {
1789 status = device_create_file(dev, &dev_attr_state);
1793 if (ops->get_status) {
1794 status = device_create_file(dev, &dev_attr_status);
1799 /* some attributes are type-specific */
1800 if (rdev->desc->type == REGULATOR_CURRENT) {
1801 status = device_create_file(dev, &dev_attr_requested_microamps);
1806 /* all the other attributes exist to support constraints;
1807 * don't show them if there are no constraints, or if the
1808 * relevant supporting methods are missing.
1810 if (!rdev->constraints)
1813 /* constraints need specific supporting methods */
1814 if (ops->set_voltage) {
1815 status = device_create_file(dev, &dev_attr_min_microvolts);
1818 status = device_create_file(dev, &dev_attr_max_microvolts);
1822 if (ops->set_current_limit) {
1823 status = device_create_file(dev, &dev_attr_min_microamps);
1826 status = device_create_file(dev, &dev_attr_max_microamps);
1831 /* suspend mode constraints need multiple supporting methods */
1832 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1835 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1838 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1841 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1845 if (ops->set_suspend_voltage) {
1846 status = device_create_file(dev,
1847 &dev_attr_suspend_standby_microvolts);
1850 status = device_create_file(dev,
1851 &dev_attr_suspend_mem_microvolts);
1854 status = device_create_file(dev,
1855 &dev_attr_suspend_disk_microvolts);
1860 if (ops->set_suspend_mode) {
1861 status = device_create_file(dev,
1862 &dev_attr_suspend_standby_mode);
1865 status = device_create_file(dev,
1866 &dev_attr_suspend_mem_mode);
1869 status = device_create_file(dev,
1870 &dev_attr_suspend_disk_mode);
1879 * regulator_register - register regulator
1880 * @regulator_desc: regulator to register
1881 * @dev: struct device for the regulator
1882 * @init_data: platform provided init data, passed through by driver
1883 * @driver_data: private regulator data
1885 * Called by regulator drivers to register a regulator.
1886 * Returns 0 on success.
1888 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1889 struct device *dev, struct regulator_init_data *init_data,
1892 static atomic_t regulator_no = ATOMIC_INIT(0);
1893 struct regulator_dev *rdev;
1896 if (regulator_desc == NULL)
1897 return ERR_PTR(-EINVAL);
1899 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1900 return ERR_PTR(-EINVAL);
1902 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1903 !regulator_desc->type == REGULATOR_CURRENT)
1904 return ERR_PTR(-EINVAL);
1907 return ERR_PTR(-EINVAL);
1909 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1911 return ERR_PTR(-ENOMEM);
1913 mutex_lock(®ulator_list_mutex);
1915 mutex_init(&rdev->mutex);
1916 rdev->reg_data = driver_data;
1917 rdev->owner = regulator_desc->owner;
1918 rdev->desc = regulator_desc;
1919 INIT_LIST_HEAD(&rdev->consumer_list);
1920 INIT_LIST_HEAD(&rdev->supply_list);
1921 INIT_LIST_HEAD(&rdev->list);
1922 INIT_LIST_HEAD(&rdev->slist);
1923 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1925 /* preform any regulator specific init */
1926 if (init_data->regulator_init) {
1927 ret = init_data->regulator_init(rdev->reg_data);
1932 /* register with sysfs */
1933 rdev->dev.class = ®ulator_class;
1934 rdev->dev.parent = dev;
1935 dev_set_name(&rdev->dev, "regulator.%d",
1936 atomic_inc_return(®ulator_no) - 1);
1937 ret = device_register(&rdev->dev);
1941 dev_set_drvdata(&rdev->dev, rdev);
1943 /* set regulator constraints */
1944 ret = set_machine_constraints(rdev, &init_data->constraints);
1948 /* add attributes supported by this regulator */
1949 ret = add_regulator_attributes(rdev);
1953 /* set supply regulator if it exists */
1954 if (init_data->supply_regulator_dev) {
1955 ret = set_supply(rdev,
1956 dev_get_drvdata(init_data->supply_regulator_dev));
1961 /* add consumers devices */
1962 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1963 ret = set_consumer_device_supply(rdev,
1964 init_data->consumer_supplies[i].dev,
1965 init_data->consumer_supplies[i].supply);
1967 for (--i; i >= 0; i--)
1968 unset_consumer_device_supply(rdev,
1969 init_data->consumer_supplies[i].dev);
1974 list_add(&rdev->list, ®ulator_list);
1976 mutex_unlock(®ulator_list_mutex);
1980 device_unregister(&rdev->dev);
1983 rdev = ERR_PTR(ret);
1986 EXPORT_SYMBOL_GPL(regulator_register);
1989 * regulator_unregister - unregister regulator
1990 * @rdev: regulator to unregister
1992 * Called by regulator drivers to unregister a regulator.
1994 void regulator_unregister(struct regulator_dev *rdev)
1999 mutex_lock(®ulator_list_mutex);
2000 list_del(&rdev->list);
2002 sysfs_remove_link(&rdev->dev.kobj, "supply");
2003 device_unregister(&rdev->dev);
2004 mutex_unlock(®ulator_list_mutex);
2006 EXPORT_SYMBOL_GPL(regulator_unregister);
2009 * regulator_suspend_prepare - prepare regulators for system wide suspend
2010 * @state: system suspend state
2012 * Configure each regulator with it's suspend operating parameters for state.
2013 * This will usually be called by machine suspend code prior to supending.
2015 int regulator_suspend_prepare(suspend_state_t state)
2017 struct regulator_dev *rdev;
2020 /* ON is handled by regulator active state */
2021 if (state == PM_SUSPEND_ON)
2024 mutex_lock(®ulator_list_mutex);
2025 list_for_each_entry(rdev, ®ulator_list, list) {
2027 mutex_lock(&rdev->mutex);
2028 ret = suspend_prepare(rdev, state);
2029 mutex_unlock(&rdev->mutex);
2032 printk(KERN_ERR "%s: failed to prepare %s\n",
2033 __func__, rdev->desc->name);
2038 mutex_unlock(®ulator_list_mutex);
2041 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2044 * rdev_get_drvdata - get rdev regulator driver data
2047 * Get rdev regulator driver private data. This call can be used in the
2048 * regulator driver context.
2050 void *rdev_get_drvdata(struct regulator_dev *rdev)
2052 return rdev->reg_data;
2054 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2057 * regulator_get_drvdata - get regulator driver data
2058 * @regulator: regulator
2060 * Get regulator driver private data. This call can be used in the consumer
2061 * driver context when non API regulator specific functions need to be called.
2063 void *regulator_get_drvdata(struct regulator *regulator)
2065 return regulator->rdev->reg_data;
2067 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2070 * regulator_set_drvdata - set regulator driver data
2071 * @regulator: regulator
2074 void regulator_set_drvdata(struct regulator *regulator, void *data)
2076 regulator->rdev->reg_data = data;
2078 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2081 * regulator_get_id - get regulator ID
2084 int rdev_get_id(struct regulator_dev *rdev)
2086 return rdev->desc->id;
2088 EXPORT_SYMBOL_GPL(rdev_get_id);
2090 struct device *rdev_get_dev(struct regulator_dev *rdev)
2094 EXPORT_SYMBOL_GPL(rdev_get_dev);
2096 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2098 return reg_init_data->driver_data;
2100 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2102 static int __init regulator_init(void)
2104 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2105 return class_register(®ulator_class);
2108 /* init early to allow our consumers to complete system booting */
2109 core_initcall(regulator_init);