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);
31 static int has_full_constraints;
34 * struct regulator_map
36 * Used to provide symbolic supply names to devices.
38 struct regulator_map {
39 struct list_head list;
40 const char *dev_name; /* The dev_name() for the consumer */
42 struct regulator_dev *regulator;
48 * One for each consumer device.
52 struct list_head list;
57 struct device_attribute dev_attr;
58 struct regulator_dev *rdev;
61 static int _regulator_is_enabled(struct regulator_dev *rdev);
62 static int _regulator_disable(struct regulator_dev *rdev);
63 static int _regulator_get_voltage(struct regulator_dev *rdev);
64 static int _regulator_get_current_limit(struct regulator_dev *rdev);
65 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
66 static void _notifier_call_chain(struct regulator_dev *rdev,
67 unsigned long event, void *data);
69 static const char *rdev_get_name(struct regulator_dev *rdev)
71 if (rdev->constraints && rdev->constraints->name)
72 return rdev->constraints->name;
73 else if (rdev->desc->name)
74 return rdev->desc->name;
79 /* gets the regulator for a given consumer device */
80 static struct regulator *get_device_regulator(struct device *dev)
82 struct regulator *regulator = NULL;
83 struct regulator_dev *rdev;
85 mutex_lock(®ulator_list_mutex);
86 list_for_each_entry(rdev, ®ulator_list, list) {
87 mutex_lock(&rdev->mutex);
88 list_for_each_entry(regulator, &rdev->consumer_list, list) {
89 if (regulator->dev == dev) {
90 mutex_unlock(&rdev->mutex);
91 mutex_unlock(®ulator_list_mutex);
95 mutex_unlock(&rdev->mutex);
97 mutex_unlock(®ulator_list_mutex);
101 /* Platform voltage constraint check */
102 static int regulator_check_voltage(struct regulator_dev *rdev,
103 int *min_uV, int *max_uV)
105 BUG_ON(*min_uV > *max_uV);
107 if (!rdev->constraints) {
108 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
109 rdev_get_name(rdev));
112 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
113 printk(KERN_ERR "%s: operation not allowed for %s\n",
114 __func__, rdev_get_name(rdev));
118 if (*max_uV > rdev->constraints->max_uV)
119 *max_uV = rdev->constraints->max_uV;
120 if (*min_uV < rdev->constraints->min_uV)
121 *min_uV = rdev->constraints->min_uV;
123 if (*min_uV > *max_uV)
129 /* current constraint check */
130 static int regulator_check_current_limit(struct regulator_dev *rdev,
131 int *min_uA, int *max_uA)
133 BUG_ON(*min_uA > *max_uA);
135 if (!rdev->constraints) {
136 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
137 rdev_get_name(rdev));
140 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
141 printk(KERN_ERR "%s: operation not allowed for %s\n",
142 __func__, rdev_get_name(rdev));
146 if (*max_uA > rdev->constraints->max_uA)
147 *max_uA = rdev->constraints->max_uA;
148 if (*min_uA < rdev->constraints->min_uA)
149 *min_uA = rdev->constraints->min_uA;
151 if (*min_uA > *max_uA)
157 /* operating mode constraint check */
158 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
161 case REGULATOR_MODE_FAST:
162 case REGULATOR_MODE_NORMAL:
163 case REGULATOR_MODE_IDLE:
164 case REGULATOR_MODE_STANDBY:
170 if (!rdev->constraints) {
171 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
172 rdev_get_name(rdev));
175 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
176 printk(KERN_ERR "%s: operation not allowed for %s\n",
177 __func__, rdev_get_name(rdev));
180 if (!(rdev->constraints->valid_modes_mask & mode)) {
181 printk(KERN_ERR "%s: invalid mode %x for %s\n",
182 __func__, mode, rdev_get_name(rdev));
188 /* dynamic regulator mode switching constraint check */
189 static int regulator_check_drms(struct regulator_dev *rdev)
191 if (!rdev->constraints) {
192 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
193 rdev_get_name(rdev));
196 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
197 printk(KERN_ERR "%s: operation not allowed for %s\n",
198 __func__, rdev_get_name(rdev));
204 static ssize_t device_requested_uA_show(struct device *dev,
205 struct device_attribute *attr, char *buf)
207 struct regulator *regulator;
209 regulator = get_device_regulator(dev);
210 if (regulator == NULL)
213 return sprintf(buf, "%d\n", regulator->uA_load);
216 static ssize_t regulator_uV_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
219 struct regulator_dev *rdev = dev_get_drvdata(dev);
222 mutex_lock(&rdev->mutex);
223 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
224 mutex_unlock(&rdev->mutex);
228 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
230 static ssize_t regulator_uA_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
233 struct regulator_dev *rdev = dev_get_drvdata(dev);
235 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
237 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
239 static ssize_t regulator_name_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 struct regulator_dev *rdev = dev_get_drvdata(dev);
244 return sprintf(buf, "%s\n", rdev_get_name(rdev));
247 static ssize_t regulator_print_opmode(char *buf, int mode)
250 case REGULATOR_MODE_FAST:
251 return sprintf(buf, "fast\n");
252 case REGULATOR_MODE_NORMAL:
253 return sprintf(buf, "normal\n");
254 case REGULATOR_MODE_IDLE:
255 return sprintf(buf, "idle\n");
256 case REGULATOR_MODE_STANDBY:
257 return sprintf(buf, "standby\n");
259 return sprintf(buf, "unknown\n");
262 static ssize_t regulator_opmode_show(struct device *dev,
263 struct device_attribute *attr, char *buf)
265 struct regulator_dev *rdev = dev_get_drvdata(dev);
267 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
269 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
271 static ssize_t regulator_print_state(char *buf, int state)
274 return sprintf(buf, "enabled\n");
276 return sprintf(buf, "disabled\n");
278 return sprintf(buf, "unknown\n");
281 static ssize_t regulator_state_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
284 struct regulator_dev *rdev = dev_get_drvdata(dev);
287 mutex_lock(&rdev->mutex);
288 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
289 mutex_unlock(&rdev->mutex);
293 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
295 static ssize_t regulator_status_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
298 struct regulator_dev *rdev = dev_get_drvdata(dev);
302 status = rdev->desc->ops->get_status(rdev);
307 case REGULATOR_STATUS_OFF:
310 case REGULATOR_STATUS_ON:
313 case REGULATOR_STATUS_ERROR:
316 case REGULATOR_STATUS_FAST:
319 case REGULATOR_STATUS_NORMAL:
322 case REGULATOR_STATUS_IDLE:
325 case REGULATOR_STATUS_STANDBY:
332 return sprintf(buf, "%s\n", label);
334 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
336 static ssize_t regulator_min_uA_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
339 struct regulator_dev *rdev = dev_get_drvdata(dev);
341 if (!rdev->constraints)
342 return sprintf(buf, "constraint not defined\n");
344 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
346 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
348 static ssize_t regulator_max_uA_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
351 struct regulator_dev *rdev = dev_get_drvdata(dev);
353 if (!rdev->constraints)
354 return sprintf(buf, "constraint not defined\n");
356 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
358 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
360 static ssize_t regulator_min_uV_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
363 struct regulator_dev *rdev = dev_get_drvdata(dev);
365 if (!rdev->constraints)
366 return sprintf(buf, "constraint not defined\n");
368 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
370 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
372 static ssize_t regulator_max_uV_show(struct device *dev,
373 struct device_attribute *attr, char *buf)
375 struct regulator_dev *rdev = dev_get_drvdata(dev);
377 if (!rdev->constraints)
378 return sprintf(buf, "constraint not defined\n");
380 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
382 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
384 static ssize_t regulator_total_uA_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
387 struct regulator_dev *rdev = dev_get_drvdata(dev);
388 struct regulator *regulator;
391 mutex_lock(&rdev->mutex);
392 list_for_each_entry(regulator, &rdev->consumer_list, list)
393 uA += regulator->uA_load;
394 mutex_unlock(&rdev->mutex);
395 return sprintf(buf, "%d\n", uA);
397 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
399 static ssize_t regulator_num_users_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct regulator_dev *rdev = dev_get_drvdata(dev);
403 return sprintf(buf, "%d\n", rdev->use_count);
406 static ssize_t regulator_type_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
409 struct regulator_dev *rdev = dev_get_drvdata(dev);
411 switch (rdev->desc->type) {
412 case REGULATOR_VOLTAGE:
413 return sprintf(buf, "voltage\n");
414 case REGULATOR_CURRENT:
415 return sprintf(buf, "current\n");
417 return sprintf(buf, "unknown\n");
420 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
423 struct regulator_dev *rdev = dev_get_drvdata(dev);
425 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
427 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
428 regulator_suspend_mem_uV_show, NULL);
430 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
433 struct regulator_dev *rdev = dev_get_drvdata(dev);
435 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
437 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
438 regulator_suspend_disk_uV_show, NULL);
440 static ssize_t regulator_suspend_standby_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_standby.uV);
447 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
448 regulator_suspend_standby_uV_show, NULL);
450 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 struct regulator_dev *rdev = dev_get_drvdata(dev);
455 return regulator_print_opmode(buf,
456 rdev->constraints->state_mem.mode);
458 static DEVICE_ATTR(suspend_mem_mode, 0444,
459 regulator_suspend_mem_mode_show, NULL);
461 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
464 struct regulator_dev *rdev = dev_get_drvdata(dev);
466 return regulator_print_opmode(buf,
467 rdev->constraints->state_disk.mode);
469 static DEVICE_ATTR(suspend_disk_mode, 0444,
470 regulator_suspend_disk_mode_show, NULL);
472 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
475 struct regulator_dev *rdev = dev_get_drvdata(dev);
477 return regulator_print_opmode(buf,
478 rdev->constraints->state_standby.mode);
480 static DEVICE_ATTR(suspend_standby_mode, 0444,
481 regulator_suspend_standby_mode_show, NULL);
483 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
486 struct regulator_dev *rdev = dev_get_drvdata(dev);
488 return regulator_print_state(buf,
489 rdev->constraints->state_mem.enabled);
491 static DEVICE_ATTR(suspend_mem_state, 0444,
492 regulator_suspend_mem_state_show, NULL);
494 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
497 struct regulator_dev *rdev = dev_get_drvdata(dev);
499 return regulator_print_state(buf,
500 rdev->constraints->state_disk.enabled);
502 static DEVICE_ATTR(suspend_disk_state, 0444,
503 regulator_suspend_disk_state_show, NULL);
505 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
508 struct regulator_dev *rdev = dev_get_drvdata(dev);
510 return regulator_print_state(buf,
511 rdev->constraints->state_standby.enabled);
513 static DEVICE_ATTR(suspend_standby_state, 0444,
514 regulator_suspend_standby_state_show, NULL);
518 * These are the only attributes are present for all regulators.
519 * Other attributes are a function of regulator functionality.
521 static struct device_attribute regulator_dev_attrs[] = {
522 __ATTR(name, 0444, regulator_name_show, NULL),
523 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
524 __ATTR(type, 0444, regulator_type_show, NULL),
528 static void regulator_dev_release(struct device *dev)
530 struct regulator_dev *rdev = dev_get_drvdata(dev);
534 static struct class regulator_class = {
536 .dev_release = regulator_dev_release,
537 .dev_attrs = regulator_dev_attrs,
540 /* Calculate the new optimum regulator operating mode based on the new total
541 * consumer load. All locks held by caller */
542 static void drms_uA_update(struct regulator_dev *rdev)
544 struct regulator *sibling;
545 int current_uA = 0, output_uV, input_uV, err;
548 err = regulator_check_drms(rdev);
549 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
550 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
553 /* get output voltage */
554 output_uV = rdev->desc->ops->get_voltage(rdev);
558 /* get input voltage */
559 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
560 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
562 input_uV = rdev->constraints->input_uV;
566 /* calc total requested load */
567 list_for_each_entry(sibling, &rdev->consumer_list, list)
568 current_uA += sibling->uA_load;
570 /* now get the optimum mode for our new total regulator load */
571 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
572 output_uV, current_uA);
574 /* check the new mode is allowed */
575 err = regulator_check_mode(rdev, mode);
577 rdev->desc->ops->set_mode(rdev, mode);
580 static int suspend_set_state(struct regulator_dev *rdev,
581 struct regulator_state *rstate)
585 /* enable & disable are mandatory for suspend control */
586 if (!rdev->desc->ops->set_suspend_enable ||
587 !rdev->desc->ops->set_suspend_disable) {
588 printk(KERN_ERR "%s: no way to set suspend state\n",
594 ret = rdev->desc->ops->set_suspend_enable(rdev);
596 ret = rdev->desc->ops->set_suspend_disable(rdev);
598 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
602 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
603 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
605 printk(KERN_ERR "%s: failed to set voltage\n",
611 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
612 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
614 printk(KERN_ERR "%s: failed to set mode\n", __func__);
621 /* locks held by caller */
622 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
624 if (!rdev->constraints)
628 case PM_SUSPEND_STANDBY:
629 return suspend_set_state(rdev,
630 &rdev->constraints->state_standby);
632 return suspend_set_state(rdev,
633 &rdev->constraints->state_mem);
635 return suspend_set_state(rdev,
636 &rdev->constraints->state_disk);
642 static void print_constraints(struct regulator_dev *rdev)
644 struct regulation_constraints *constraints = rdev->constraints;
649 if (constraints->min_uV && constraints->max_uV) {
650 if (constraints->min_uV == constraints->max_uV)
651 count += sprintf(buf + count, "%d mV ",
652 constraints->min_uV / 1000);
654 count += sprintf(buf + count, "%d <--> %d mV ",
655 constraints->min_uV / 1000,
656 constraints->max_uV / 1000);
659 if (!constraints->min_uV ||
660 constraints->min_uV != constraints->max_uV) {
661 ret = _regulator_get_voltage(rdev);
663 count += sprintf(buf + count, "at %d mV ", ret / 1000);
666 if (constraints->min_uA && constraints->max_uA) {
667 if (constraints->min_uA == constraints->max_uA)
668 count += sprintf(buf + count, "%d mA ",
669 constraints->min_uA / 1000);
671 count += sprintf(buf + count, "%d <--> %d mA ",
672 constraints->min_uA / 1000,
673 constraints->max_uA / 1000);
676 if (!constraints->min_uA ||
677 constraints->min_uA != constraints->max_uA) {
678 ret = _regulator_get_current_limit(rdev);
680 count += sprintf(buf + count, "at %d uA ", ret / 1000);
683 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
684 count += sprintf(buf + count, "fast ");
685 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
686 count += sprintf(buf + count, "normal ");
687 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
688 count += sprintf(buf + count, "idle ");
689 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
690 count += sprintf(buf + count, "standby");
692 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
695 static int machine_constraints_voltage(struct regulator_dev *rdev,
696 struct regulation_constraints *constraints)
698 struct regulator_ops *ops = rdev->desc->ops;
699 const char *name = rdev_get_name(rdev);
702 /* do we need to apply the constraint voltage */
703 if (rdev->constraints->apply_uV &&
704 rdev->constraints->min_uV == rdev->constraints->max_uV &&
706 ret = ops->set_voltage(rdev,
707 rdev->constraints->min_uV, rdev->constraints->max_uV);
709 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
711 rdev->constraints->min_uV, name);
712 rdev->constraints = NULL;
717 /* constrain machine-level voltage specs to fit
718 * the actual range supported by this regulator.
720 if (ops->list_voltage && rdev->desc->n_voltages) {
721 int count = rdev->desc->n_voltages;
723 int min_uV = INT_MAX;
724 int max_uV = INT_MIN;
725 int cmin = constraints->min_uV;
726 int cmax = constraints->max_uV;
728 /* it's safe to autoconfigure fixed-voltage supplies
729 and the constraints are used by list_voltage. */
730 if (count == 1 && !cmin) {
733 constraints->min_uV = cmin;
734 constraints->max_uV = cmax;
737 /* voltage constraints are optional */
738 if ((cmin == 0) && (cmax == 0))
741 /* else require explicit machine-level constraints */
742 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
743 pr_err("%s: %s '%s' voltage constraints\n",
744 __func__, "invalid", name);
748 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
749 for (i = 0; i < count; i++) {
752 value = ops->list_voltage(rdev, i);
756 /* maybe adjust [min_uV..max_uV] */
757 if (value >= cmin && value < min_uV)
759 if (value <= cmax && value > max_uV)
763 /* final: [min_uV..max_uV] valid iff constraints valid */
764 if (max_uV < min_uV) {
765 pr_err("%s: %s '%s' voltage constraints\n",
766 __func__, "unsupportable", name);
770 /* use regulator's subset of machine constraints */
771 if (constraints->min_uV < min_uV) {
772 pr_debug("%s: override '%s' %s, %d -> %d\n",
773 __func__, name, "min_uV",
774 constraints->min_uV, min_uV);
775 constraints->min_uV = min_uV;
777 if (constraints->max_uV > max_uV) {
778 pr_debug("%s: override '%s' %s, %d -> %d\n",
779 __func__, name, "max_uV",
780 constraints->max_uV, max_uV);
781 constraints->max_uV = max_uV;
789 * set_machine_constraints - sets regulator constraints
790 * @rdev: regulator source
791 * @constraints: constraints to apply
793 * Allows platform initialisation code to define and constrain
794 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
795 * Constraints *must* be set by platform code in order for some
796 * regulator operations to proceed i.e. set_voltage, set_current_limit,
799 static int set_machine_constraints(struct regulator_dev *rdev,
800 struct regulation_constraints *constraints)
804 struct regulator_ops *ops = rdev->desc->ops;
806 rdev->constraints = constraints;
808 name = rdev_get_name(rdev);
810 ret = machine_constraints_voltage(rdev, constraints);
814 /* do we need to setup our suspend state */
815 if (constraints->initial_state) {
816 ret = suspend_prepare(rdev, constraints->initial_state);
818 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
820 rdev->constraints = NULL;
825 if (constraints->initial_mode) {
826 if (!ops->set_mode) {
827 printk(KERN_ERR "%s: no set_mode operation for %s\n",
833 ret = ops->set_mode(rdev, constraints->initial_mode);
836 "%s: failed to set initial mode for %s: %d\n",
837 __func__, name, ret);
842 /* If the constraints say the regulator should be on at this point
843 * and we have control then make sure it is enabled.
845 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
846 ret = ops->enable(rdev);
848 printk(KERN_ERR "%s: failed to enable %s\n",
850 rdev->constraints = NULL;
855 print_constraints(rdev);
861 * set_supply - set regulator supply regulator
862 * @rdev: regulator name
863 * @supply_rdev: supply regulator name
865 * Called by platform initialisation code to set the supply regulator for this
866 * regulator. This ensures that a regulators supply will also be enabled by the
867 * core if it's child is enabled.
869 static int set_supply(struct regulator_dev *rdev,
870 struct regulator_dev *supply_rdev)
874 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
878 "%s: could not add device link %s err %d\n",
879 __func__, supply_rdev->dev.kobj.name, err);
882 rdev->supply = supply_rdev;
883 list_add(&rdev->slist, &supply_rdev->supply_list);
889 * set_consumer_device_supply: Bind a regulator to a symbolic supply
890 * @rdev: regulator source
891 * @consumer_dev: device the supply applies to
892 * @consumer_dev_name: dev_name() string for device supply applies to
893 * @supply: symbolic name for supply
895 * Allows platform initialisation code to map physical regulator
896 * sources to symbolic names for supplies for use by devices. Devices
897 * should use these symbolic names to request regulators, avoiding the
898 * need to provide board-specific regulator names as platform data.
900 * Only one of consumer_dev and consumer_dev_name may be specified.
902 static int set_consumer_device_supply(struct regulator_dev *rdev,
903 struct device *consumer_dev, const char *consumer_dev_name,
906 struct regulator_map *node;
909 if (consumer_dev && consumer_dev_name)
912 if (!consumer_dev_name && consumer_dev)
913 consumer_dev_name = dev_name(consumer_dev);
918 if (consumer_dev_name != NULL)
923 list_for_each_entry(node, ®ulator_map_list, list) {
924 if (consumer_dev_name != node->dev_name)
926 if (strcmp(node->supply, supply) != 0)
929 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
930 dev_name(&node->regulator->dev),
931 node->regulator->desc->name,
933 dev_name(&rdev->dev), rdev_get_name(rdev));
937 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
941 node->regulator = rdev;
942 node->supply = supply;
945 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
946 if (node->dev_name == NULL) {
952 list_add(&node->list, ®ulator_map_list);
956 static void unset_consumer_device_supply(struct regulator_dev *rdev,
957 const char *consumer_dev_name, struct device *consumer_dev)
959 struct regulator_map *node, *n;
961 if (consumer_dev && !consumer_dev_name)
962 consumer_dev_name = dev_name(consumer_dev);
964 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
965 if (rdev != node->regulator)
968 if (consumer_dev_name && node->dev_name &&
969 strcmp(consumer_dev_name, node->dev_name))
972 list_del(&node->list);
973 kfree(node->dev_name);
979 static void unset_regulator_supplies(struct regulator_dev *rdev)
981 struct regulator_map *node, *n;
983 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
984 if (rdev == node->regulator) {
985 list_del(&node->list);
986 kfree(node->dev_name);
993 #define REG_STR_SIZE 32
995 static struct regulator *create_regulator(struct regulator_dev *rdev,
997 const char *supply_name)
999 struct regulator *regulator;
1000 char buf[REG_STR_SIZE];
1003 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1004 if (regulator == NULL)
1007 mutex_lock(&rdev->mutex);
1008 regulator->rdev = rdev;
1009 list_add(®ulator->list, &rdev->consumer_list);
1012 /* create a 'requested_microamps_name' sysfs entry */
1013 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1015 if (size >= REG_STR_SIZE)
1018 regulator->dev = dev;
1019 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1020 if (regulator->dev_attr.attr.name == NULL)
1023 regulator->dev_attr.attr.owner = THIS_MODULE;
1024 regulator->dev_attr.attr.mode = 0444;
1025 regulator->dev_attr.show = device_requested_uA_show;
1026 err = device_create_file(dev, ®ulator->dev_attr);
1028 printk(KERN_WARNING "%s: could not add regulator_dev"
1029 " load sysfs\n", __func__);
1033 /* also add a link to the device sysfs entry */
1034 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1035 dev->kobj.name, supply_name);
1036 if (size >= REG_STR_SIZE)
1039 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1040 if (regulator->supply_name == NULL)
1043 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1047 "%s: could not add device link %s err %d\n",
1048 __func__, dev->kobj.name, err);
1049 device_remove_file(dev, ®ulator->dev_attr);
1053 mutex_unlock(&rdev->mutex);
1056 kfree(regulator->supply_name);
1058 device_remove_file(regulator->dev, ®ulator->dev_attr);
1060 kfree(regulator->dev_attr.attr.name);
1062 list_del(®ulator->list);
1064 mutex_unlock(&rdev->mutex);
1068 /* Internal regulator request function */
1069 static struct regulator *_regulator_get(struct device *dev, const char *id,
1072 struct regulator_dev *rdev;
1073 struct regulator_map *map;
1074 struct regulator *regulator = ERR_PTR(-ENODEV);
1075 const char *devname = NULL;
1079 printk(KERN_ERR "regulator: get() with no identifier\n");
1084 devname = dev_name(dev);
1086 mutex_lock(®ulator_list_mutex);
1088 list_for_each_entry(map, ®ulator_map_list, list) {
1089 /* If the mapping has a device set up it must match */
1090 if (map->dev_name &&
1091 (!devname || strcmp(map->dev_name, devname)))
1094 if (strcmp(map->supply, id) == 0) {
1095 rdev = map->regulator;
1099 mutex_unlock(®ulator_list_mutex);
1103 if (rdev->exclusive) {
1104 regulator = ERR_PTR(-EPERM);
1108 if (exclusive && rdev->open_count) {
1109 regulator = ERR_PTR(-EBUSY);
1113 if (!try_module_get(rdev->owner))
1116 regulator = create_regulator(rdev, dev, id);
1117 if (regulator == NULL) {
1118 regulator = ERR_PTR(-ENOMEM);
1119 module_put(rdev->owner);
1124 rdev->exclusive = 1;
1126 ret = _regulator_is_enabled(rdev);
1128 rdev->use_count = 1;
1130 rdev->use_count = 0;
1134 mutex_unlock(®ulator_list_mutex);
1140 * regulator_get - lookup and obtain a reference to a regulator.
1141 * @dev: device for regulator "consumer"
1142 * @id: Supply name or regulator ID.
1144 * Returns a struct regulator corresponding to the regulator producer,
1145 * or IS_ERR() condition containing errno.
1147 * Use of supply names configured via regulator_set_device_supply() is
1148 * strongly encouraged. It is recommended that the supply name used
1149 * should match the name used for the supply and/or the relevant
1150 * device pins in the datasheet.
1152 struct regulator *regulator_get(struct device *dev, const char *id)
1154 return _regulator_get(dev, id, 0);
1156 EXPORT_SYMBOL_GPL(regulator_get);
1159 * regulator_get_exclusive - obtain exclusive access to a regulator.
1160 * @dev: device for regulator "consumer"
1161 * @id: Supply name or regulator ID.
1163 * Returns a struct regulator corresponding to the regulator producer,
1164 * or IS_ERR() condition containing errno. Other consumers will be
1165 * unable to obtain this reference is held and the use count for the
1166 * regulator will be initialised to reflect the current state of the
1169 * This is intended for use by consumers which cannot tolerate shared
1170 * use of the regulator such as those which need to force the
1171 * regulator off for correct operation of the hardware they are
1174 * Use of supply names configured via regulator_set_device_supply() is
1175 * strongly encouraged. It is recommended that the supply name used
1176 * should match the name used for the supply and/or the relevant
1177 * device pins in the datasheet.
1179 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1181 return _regulator_get(dev, id, 1);
1183 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1186 * regulator_put - "free" the regulator source
1187 * @regulator: regulator source
1189 * Note: drivers must ensure that all regulator_enable calls made on this
1190 * regulator source are balanced by regulator_disable calls prior to calling
1193 void regulator_put(struct regulator *regulator)
1195 struct regulator_dev *rdev;
1197 if (regulator == NULL || IS_ERR(regulator))
1200 mutex_lock(®ulator_list_mutex);
1201 rdev = regulator->rdev;
1203 /* remove any sysfs entries */
1204 if (regulator->dev) {
1205 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1206 kfree(regulator->supply_name);
1207 device_remove_file(regulator->dev, ®ulator->dev_attr);
1208 kfree(regulator->dev_attr.attr.name);
1210 list_del(®ulator->list);
1214 rdev->exclusive = 0;
1216 module_put(rdev->owner);
1217 mutex_unlock(®ulator_list_mutex);
1219 EXPORT_SYMBOL_GPL(regulator_put);
1221 static int _regulator_can_change_status(struct regulator_dev *rdev)
1223 if (!rdev->constraints)
1226 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1232 /* locks held by regulator_enable() */
1233 static int _regulator_enable(struct regulator_dev *rdev)
1237 /* do we need to enable the supply regulator first */
1239 ret = _regulator_enable(rdev->supply);
1241 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1242 __func__, rdev_get_name(rdev), ret);
1247 /* check voltage and requested load before enabling */
1248 if (rdev->constraints &&
1249 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1250 drms_uA_update(rdev);
1252 if (rdev->use_count == 0) {
1253 /* The regulator may on if it's not switchable or left on */
1254 ret = _regulator_is_enabled(rdev);
1255 if (ret == -EINVAL || ret == 0) {
1256 if (!_regulator_can_change_status(rdev))
1259 if (rdev->desc->ops->enable) {
1260 ret = rdev->desc->ops->enable(rdev);
1266 } else if (ret < 0) {
1267 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
1268 __func__, rdev_get_name(rdev), ret);
1271 /* Fallthrough on positive return values - already enabled */
1280 * regulator_enable - enable regulator output
1281 * @regulator: regulator source
1283 * Request that the regulator be enabled with the regulator output at
1284 * the predefined voltage or current value. Calls to regulator_enable()
1285 * must be balanced with calls to regulator_disable().
1287 * NOTE: the output value can be set by other drivers, boot loader or may be
1288 * hardwired in the regulator.
1290 int regulator_enable(struct regulator *regulator)
1292 struct regulator_dev *rdev = regulator->rdev;
1295 mutex_lock(&rdev->mutex);
1296 ret = _regulator_enable(rdev);
1297 mutex_unlock(&rdev->mutex);
1300 EXPORT_SYMBOL_GPL(regulator_enable);
1302 /* locks held by regulator_disable() */
1303 static int _regulator_disable(struct regulator_dev *rdev)
1307 if (WARN(rdev->use_count <= 0,
1308 "unbalanced disables for %s\n",
1309 rdev_get_name(rdev)))
1312 /* are we the last user and permitted to disable ? */
1313 if (rdev->use_count == 1 &&
1314 (rdev->constraints && !rdev->constraints->always_on)) {
1316 /* we are last user */
1317 if (_regulator_can_change_status(rdev) &&
1318 rdev->desc->ops->disable) {
1319 ret = rdev->desc->ops->disable(rdev);
1321 printk(KERN_ERR "%s: failed to disable %s\n",
1322 __func__, rdev_get_name(rdev));
1327 /* decrease our supplies ref count and disable if required */
1329 _regulator_disable(rdev->supply);
1331 rdev->use_count = 0;
1332 } else if (rdev->use_count > 1) {
1334 if (rdev->constraints &&
1335 (rdev->constraints->valid_ops_mask &
1336 REGULATOR_CHANGE_DRMS))
1337 drms_uA_update(rdev);
1345 * regulator_disable - disable regulator output
1346 * @regulator: regulator source
1348 * Disable the regulator output voltage or current. Calls to
1349 * regulator_enable() must be balanced with calls to
1350 * regulator_disable().
1352 * NOTE: this will only disable the regulator output if no other consumer
1353 * devices have it enabled, the regulator device supports disabling and
1354 * machine constraints permit this operation.
1356 int regulator_disable(struct regulator *regulator)
1358 struct regulator_dev *rdev = regulator->rdev;
1361 mutex_lock(&rdev->mutex);
1362 ret = _regulator_disable(rdev);
1363 mutex_unlock(&rdev->mutex);
1366 EXPORT_SYMBOL_GPL(regulator_disable);
1368 /* locks held by regulator_force_disable() */
1369 static int _regulator_force_disable(struct regulator_dev *rdev)
1374 if (rdev->desc->ops->disable) {
1375 /* ah well, who wants to live forever... */
1376 ret = rdev->desc->ops->disable(rdev);
1378 printk(KERN_ERR "%s: failed to force disable %s\n",
1379 __func__, rdev_get_name(rdev));
1382 /* notify other consumers that power has been forced off */
1383 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1387 /* decrease our supplies ref count and disable if required */
1389 _regulator_disable(rdev->supply);
1391 rdev->use_count = 0;
1396 * regulator_force_disable - force disable regulator output
1397 * @regulator: regulator source
1399 * Forcibly disable the regulator output voltage or current.
1400 * NOTE: this *will* disable the regulator output even if other consumer
1401 * devices have it enabled. This should be used for situations when device
1402 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1404 int regulator_force_disable(struct regulator *regulator)
1408 mutex_lock(®ulator->rdev->mutex);
1409 regulator->uA_load = 0;
1410 ret = _regulator_force_disable(regulator->rdev);
1411 mutex_unlock(®ulator->rdev->mutex);
1414 EXPORT_SYMBOL_GPL(regulator_force_disable);
1416 static int _regulator_is_enabled(struct regulator_dev *rdev)
1419 if (!rdev->desc->ops->is_enabled)
1422 return rdev->desc->ops->is_enabled(rdev);
1426 * regulator_is_enabled - is the regulator output enabled
1427 * @regulator: regulator source
1429 * Returns positive if the regulator driver backing the source/client
1430 * has requested that the device be enabled, zero if it hasn't, else a
1431 * negative errno code.
1433 * Note that the device backing this regulator handle can have multiple
1434 * users, so it might be enabled even if regulator_enable() was never
1435 * called for this particular source.
1437 int regulator_is_enabled(struct regulator *regulator)
1441 mutex_lock(®ulator->rdev->mutex);
1442 ret = _regulator_is_enabled(regulator->rdev);
1443 mutex_unlock(®ulator->rdev->mutex);
1447 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1450 * regulator_count_voltages - count regulator_list_voltage() selectors
1451 * @regulator: regulator source
1453 * Returns number of selectors, or negative errno. Selectors are
1454 * numbered starting at zero, and typically correspond to bitfields
1455 * in hardware registers.
1457 int regulator_count_voltages(struct regulator *regulator)
1459 struct regulator_dev *rdev = regulator->rdev;
1461 return rdev->desc->n_voltages ? : -EINVAL;
1463 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1466 * regulator_list_voltage - enumerate supported voltages
1467 * @regulator: regulator source
1468 * @selector: identify voltage to list
1469 * Context: can sleep
1471 * Returns a voltage that can be passed to @regulator_set_voltage(),
1472 * zero if this selector code can't be used on this sytem, or a
1475 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1477 struct regulator_dev *rdev = regulator->rdev;
1478 struct regulator_ops *ops = rdev->desc->ops;
1481 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1484 mutex_lock(&rdev->mutex);
1485 ret = ops->list_voltage(rdev, selector);
1486 mutex_unlock(&rdev->mutex);
1489 if (ret < rdev->constraints->min_uV)
1491 else if (ret > rdev->constraints->max_uV)
1497 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1500 * regulator_is_supported_voltage - check if a voltage range can be supported
1502 * @regulator: Regulator to check.
1503 * @min_uV: Minimum required voltage in uV.
1504 * @max_uV: Maximum required voltage in uV.
1506 * Returns a boolean or a negative error code.
1508 int regulator_is_supported_voltage(struct regulator *regulator,
1509 int min_uV, int max_uV)
1511 int i, voltages, ret;
1513 ret = regulator_count_voltages(regulator);
1518 for (i = 0; i < voltages; i++) {
1519 ret = regulator_list_voltage(regulator, i);
1521 if (ret >= min_uV && ret <= max_uV)
1529 * regulator_set_voltage - set regulator output voltage
1530 * @regulator: regulator source
1531 * @min_uV: Minimum required voltage in uV
1532 * @max_uV: Maximum acceptable voltage in uV
1534 * Sets a voltage regulator to the desired output voltage. This can be set
1535 * during any regulator state. IOW, regulator can be disabled or enabled.
1537 * If the regulator is enabled then the voltage will change to the new value
1538 * immediately otherwise if the regulator is disabled the regulator will
1539 * output at the new voltage when enabled.
1541 * NOTE: If the regulator is shared between several devices then the lowest
1542 * request voltage that meets the system constraints will be used.
1543 * Regulator system constraints must be set for this regulator before
1544 * calling this function otherwise this call will fail.
1546 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1548 struct regulator_dev *rdev = regulator->rdev;
1551 mutex_lock(&rdev->mutex);
1554 if (!rdev->desc->ops->set_voltage) {
1559 /* constraints check */
1560 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1563 regulator->min_uV = min_uV;
1564 regulator->max_uV = max_uV;
1565 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1568 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1569 mutex_unlock(&rdev->mutex);
1572 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1574 static int _regulator_get_voltage(struct regulator_dev *rdev)
1577 if (rdev->desc->ops->get_voltage)
1578 return rdev->desc->ops->get_voltage(rdev);
1584 * regulator_get_voltage - get regulator output voltage
1585 * @regulator: regulator source
1587 * This returns the current regulator voltage in uV.
1589 * NOTE: If the regulator is disabled it will return the voltage value. This
1590 * function should not be used to determine regulator state.
1592 int regulator_get_voltage(struct regulator *regulator)
1596 mutex_lock(®ulator->rdev->mutex);
1598 ret = _regulator_get_voltage(regulator->rdev);
1600 mutex_unlock(®ulator->rdev->mutex);
1604 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1607 * regulator_set_current_limit - set regulator output current limit
1608 * @regulator: regulator source
1609 * @min_uA: Minimuum supported current in uA
1610 * @max_uA: Maximum supported current in uA
1612 * Sets current sink to the desired output current. This can be set during
1613 * any regulator state. IOW, regulator can be disabled or enabled.
1615 * If the regulator is enabled then the current will change to the new value
1616 * immediately otherwise if the regulator is disabled the regulator will
1617 * output at the new current when enabled.
1619 * NOTE: Regulator system constraints must be set for this regulator before
1620 * calling this function otherwise this call will fail.
1622 int regulator_set_current_limit(struct regulator *regulator,
1623 int min_uA, int max_uA)
1625 struct regulator_dev *rdev = regulator->rdev;
1628 mutex_lock(&rdev->mutex);
1631 if (!rdev->desc->ops->set_current_limit) {
1636 /* constraints check */
1637 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1641 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1643 mutex_unlock(&rdev->mutex);
1646 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1648 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1652 mutex_lock(&rdev->mutex);
1655 if (!rdev->desc->ops->get_current_limit) {
1660 ret = rdev->desc->ops->get_current_limit(rdev);
1662 mutex_unlock(&rdev->mutex);
1667 * regulator_get_current_limit - get regulator output current
1668 * @regulator: regulator source
1670 * This returns the current supplied by the specified current sink in uA.
1672 * NOTE: If the regulator is disabled it will return the current value. This
1673 * function should not be used to determine regulator state.
1675 int regulator_get_current_limit(struct regulator *regulator)
1677 return _regulator_get_current_limit(regulator->rdev);
1679 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1682 * regulator_set_mode - set regulator operating mode
1683 * @regulator: regulator source
1684 * @mode: operating mode - one of the REGULATOR_MODE constants
1686 * Set regulator operating mode to increase regulator efficiency or improve
1687 * regulation performance.
1689 * NOTE: Regulator system constraints must be set for this regulator before
1690 * calling this function otherwise this call will fail.
1692 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1694 struct regulator_dev *rdev = regulator->rdev;
1697 mutex_lock(&rdev->mutex);
1700 if (!rdev->desc->ops->set_mode) {
1705 /* constraints check */
1706 ret = regulator_check_mode(rdev, mode);
1710 ret = rdev->desc->ops->set_mode(rdev, mode);
1712 mutex_unlock(&rdev->mutex);
1715 EXPORT_SYMBOL_GPL(regulator_set_mode);
1717 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1721 mutex_lock(&rdev->mutex);
1724 if (!rdev->desc->ops->get_mode) {
1729 ret = rdev->desc->ops->get_mode(rdev);
1731 mutex_unlock(&rdev->mutex);
1736 * regulator_get_mode - get regulator operating mode
1737 * @regulator: regulator source
1739 * Get the current regulator operating mode.
1741 unsigned int regulator_get_mode(struct regulator *regulator)
1743 return _regulator_get_mode(regulator->rdev);
1745 EXPORT_SYMBOL_GPL(regulator_get_mode);
1748 * regulator_set_optimum_mode - set regulator optimum operating mode
1749 * @regulator: regulator source
1750 * @uA_load: load current
1752 * Notifies the regulator core of a new device load. This is then used by
1753 * DRMS (if enabled by constraints) to set the most efficient regulator
1754 * operating mode for the new regulator loading.
1756 * Consumer devices notify their supply regulator of the maximum power
1757 * they will require (can be taken from device datasheet in the power
1758 * consumption tables) when they change operational status and hence power
1759 * state. Examples of operational state changes that can affect power
1760 * consumption are :-
1762 * o Device is opened / closed.
1763 * o Device I/O is about to begin or has just finished.
1764 * o Device is idling in between work.
1766 * This information is also exported via sysfs to userspace.
1768 * DRMS will sum the total requested load on the regulator and change
1769 * to the most efficient operating mode if platform constraints allow.
1771 * Returns the new regulator mode or error.
1773 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1775 struct regulator_dev *rdev = regulator->rdev;
1776 struct regulator *consumer;
1777 int ret, output_uV, input_uV, total_uA_load = 0;
1780 mutex_lock(&rdev->mutex);
1782 regulator->uA_load = uA_load;
1783 ret = regulator_check_drms(rdev);
1789 if (!rdev->desc->ops->get_optimum_mode)
1792 /* get output voltage */
1793 output_uV = rdev->desc->ops->get_voltage(rdev);
1794 if (output_uV <= 0) {
1795 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1796 __func__, rdev_get_name(rdev));
1800 /* get input voltage */
1801 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1802 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1804 input_uV = rdev->constraints->input_uV;
1805 if (input_uV <= 0) {
1806 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1807 __func__, rdev_get_name(rdev));
1811 /* calc total requested load for this regulator */
1812 list_for_each_entry(consumer, &rdev->consumer_list, list)
1813 total_uA_load += consumer->uA_load;
1815 mode = rdev->desc->ops->get_optimum_mode(rdev,
1816 input_uV, output_uV,
1818 ret = regulator_check_mode(rdev, mode);
1820 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1821 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
1822 total_uA_load, input_uV, output_uV);
1826 ret = rdev->desc->ops->set_mode(rdev, mode);
1828 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1829 __func__, mode, rdev_get_name(rdev));
1834 mutex_unlock(&rdev->mutex);
1837 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1840 * regulator_register_notifier - register regulator event notifier
1841 * @regulator: regulator source
1842 * @nb: notifier block
1844 * Register notifier block to receive regulator events.
1846 int regulator_register_notifier(struct regulator *regulator,
1847 struct notifier_block *nb)
1849 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1852 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1855 * regulator_unregister_notifier - unregister regulator event notifier
1856 * @regulator: regulator source
1857 * @nb: notifier block
1859 * Unregister regulator event notifier block.
1861 int regulator_unregister_notifier(struct regulator *regulator,
1862 struct notifier_block *nb)
1864 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1867 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1869 /* notify regulator consumers and downstream regulator consumers.
1870 * Note mutex must be held by caller.
1872 static void _notifier_call_chain(struct regulator_dev *rdev,
1873 unsigned long event, void *data)
1875 struct regulator_dev *_rdev;
1877 /* call rdev chain first */
1878 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1880 /* now notify regulator we supply */
1881 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1882 mutex_lock(&_rdev->mutex);
1883 _notifier_call_chain(_rdev, event, data);
1884 mutex_unlock(&_rdev->mutex);
1889 * regulator_bulk_get - get multiple regulator consumers
1891 * @dev: Device to supply
1892 * @num_consumers: Number of consumers to register
1893 * @consumers: Configuration of consumers; clients are stored here.
1895 * @return 0 on success, an errno on failure.
1897 * This helper function allows drivers to get several regulator
1898 * consumers in one operation. If any of the regulators cannot be
1899 * acquired then any regulators that were allocated will be freed
1900 * before returning to the caller.
1902 int regulator_bulk_get(struct device *dev, int num_consumers,
1903 struct regulator_bulk_data *consumers)
1908 for (i = 0; i < num_consumers; i++)
1909 consumers[i].consumer = NULL;
1911 for (i = 0; i < num_consumers; i++) {
1912 consumers[i].consumer = regulator_get(dev,
1913 consumers[i].supply);
1914 if (IS_ERR(consumers[i].consumer)) {
1915 ret = PTR_ERR(consumers[i].consumer);
1916 dev_err(dev, "Failed to get supply '%s': %d\n",
1917 consumers[i].supply, ret);
1918 consumers[i].consumer = NULL;
1926 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1927 regulator_put(consumers[i].consumer);
1931 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1934 * regulator_bulk_enable - enable multiple regulator consumers
1936 * @num_consumers: Number of consumers
1937 * @consumers: Consumer data; clients are stored here.
1938 * @return 0 on success, an errno on failure
1940 * This convenience API allows consumers to enable multiple regulator
1941 * clients in a single API call. If any consumers cannot be enabled
1942 * then any others that were enabled will be disabled again prior to
1945 int regulator_bulk_enable(int num_consumers,
1946 struct regulator_bulk_data *consumers)
1951 for (i = 0; i < num_consumers; i++) {
1952 ret = regulator_enable(consumers[i].consumer);
1960 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
1961 for (i = 0; i < num_consumers; i++)
1962 regulator_disable(consumers[i].consumer);
1966 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1969 * regulator_bulk_disable - disable multiple regulator consumers
1971 * @num_consumers: Number of consumers
1972 * @consumers: Consumer data; clients are stored here.
1973 * @return 0 on success, an errno on failure
1975 * This convenience API allows consumers to disable multiple regulator
1976 * clients in a single API call. If any consumers cannot be enabled
1977 * then any others that were disabled will be disabled again prior to
1980 int regulator_bulk_disable(int num_consumers,
1981 struct regulator_bulk_data *consumers)
1986 for (i = 0; i < num_consumers; i++) {
1987 ret = regulator_disable(consumers[i].consumer);
1995 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
1997 for (i = 0; i < num_consumers; i++)
1998 regulator_enable(consumers[i].consumer);
2002 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2005 * regulator_bulk_free - free multiple regulator consumers
2007 * @num_consumers: Number of consumers
2008 * @consumers: Consumer data; clients are stored here.
2010 * This convenience API allows consumers to free multiple regulator
2011 * clients in a single API call.
2013 void regulator_bulk_free(int num_consumers,
2014 struct regulator_bulk_data *consumers)
2018 for (i = 0; i < num_consumers; i++) {
2019 regulator_put(consumers[i].consumer);
2020 consumers[i].consumer = NULL;
2023 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2026 * regulator_notifier_call_chain - call regulator event notifier
2027 * @rdev: regulator source
2028 * @event: notifier block
2029 * @data: callback-specific data.
2031 * Called by regulator drivers to notify clients a regulator event has
2032 * occurred. We also notify regulator clients downstream.
2033 * Note lock must be held by caller.
2035 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2036 unsigned long event, void *data)
2038 _notifier_call_chain(rdev, event, data);
2042 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2045 * regulator_mode_to_status - convert a regulator mode into a status
2047 * @mode: Mode to convert
2049 * Convert a regulator mode into a status.
2051 int regulator_mode_to_status(unsigned int mode)
2054 case REGULATOR_MODE_FAST:
2055 return REGULATOR_STATUS_FAST;
2056 case REGULATOR_MODE_NORMAL:
2057 return REGULATOR_STATUS_NORMAL;
2058 case REGULATOR_MODE_IDLE:
2059 return REGULATOR_STATUS_IDLE;
2060 case REGULATOR_STATUS_STANDBY:
2061 return REGULATOR_STATUS_STANDBY;
2066 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2069 * To avoid cluttering sysfs (and memory) with useless state, only
2070 * create attributes that can be meaningfully displayed.
2072 static int add_regulator_attributes(struct regulator_dev *rdev)
2074 struct device *dev = &rdev->dev;
2075 struct regulator_ops *ops = rdev->desc->ops;
2078 /* some attributes need specific methods to be displayed */
2079 if (ops->get_voltage) {
2080 status = device_create_file(dev, &dev_attr_microvolts);
2084 if (ops->get_current_limit) {
2085 status = device_create_file(dev, &dev_attr_microamps);
2089 if (ops->get_mode) {
2090 status = device_create_file(dev, &dev_attr_opmode);
2094 if (ops->is_enabled) {
2095 status = device_create_file(dev, &dev_attr_state);
2099 if (ops->get_status) {
2100 status = device_create_file(dev, &dev_attr_status);
2105 /* some attributes are type-specific */
2106 if (rdev->desc->type == REGULATOR_CURRENT) {
2107 status = device_create_file(dev, &dev_attr_requested_microamps);
2112 /* all the other attributes exist to support constraints;
2113 * don't show them if there are no constraints, or if the
2114 * relevant supporting methods are missing.
2116 if (!rdev->constraints)
2119 /* constraints need specific supporting methods */
2120 if (ops->set_voltage) {
2121 status = device_create_file(dev, &dev_attr_min_microvolts);
2124 status = device_create_file(dev, &dev_attr_max_microvolts);
2128 if (ops->set_current_limit) {
2129 status = device_create_file(dev, &dev_attr_min_microamps);
2132 status = device_create_file(dev, &dev_attr_max_microamps);
2137 /* suspend mode constraints need multiple supporting methods */
2138 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2141 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2144 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2147 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2151 if (ops->set_suspend_voltage) {
2152 status = device_create_file(dev,
2153 &dev_attr_suspend_standby_microvolts);
2156 status = device_create_file(dev,
2157 &dev_attr_suspend_mem_microvolts);
2160 status = device_create_file(dev,
2161 &dev_attr_suspend_disk_microvolts);
2166 if (ops->set_suspend_mode) {
2167 status = device_create_file(dev,
2168 &dev_attr_suspend_standby_mode);
2171 status = device_create_file(dev,
2172 &dev_attr_suspend_mem_mode);
2175 status = device_create_file(dev,
2176 &dev_attr_suspend_disk_mode);
2185 * regulator_register - register regulator
2186 * @regulator_desc: regulator to register
2187 * @dev: struct device for the regulator
2188 * @init_data: platform provided init data, passed through by driver
2189 * @driver_data: private regulator data
2191 * Called by regulator drivers to register a regulator.
2192 * Returns 0 on success.
2194 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2195 struct device *dev, struct regulator_init_data *init_data,
2198 static atomic_t regulator_no = ATOMIC_INIT(0);
2199 struct regulator_dev *rdev;
2202 if (regulator_desc == NULL)
2203 return ERR_PTR(-EINVAL);
2205 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2206 return ERR_PTR(-EINVAL);
2208 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2209 regulator_desc->type != REGULATOR_CURRENT)
2210 return ERR_PTR(-EINVAL);
2213 return ERR_PTR(-EINVAL);
2215 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2217 return ERR_PTR(-ENOMEM);
2219 mutex_lock(®ulator_list_mutex);
2221 mutex_init(&rdev->mutex);
2222 rdev->reg_data = driver_data;
2223 rdev->owner = regulator_desc->owner;
2224 rdev->desc = regulator_desc;
2225 INIT_LIST_HEAD(&rdev->consumer_list);
2226 INIT_LIST_HEAD(&rdev->supply_list);
2227 INIT_LIST_HEAD(&rdev->list);
2228 INIT_LIST_HEAD(&rdev->slist);
2229 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2231 /* preform any regulator specific init */
2232 if (init_data->regulator_init) {
2233 ret = init_data->regulator_init(rdev->reg_data);
2238 /* register with sysfs */
2239 rdev->dev.class = ®ulator_class;
2240 rdev->dev.parent = dev;
2241 dev_set_name(&rdev->dev, "regulator.%d",
2242 atomic_inc_return(®ulator_no) - 1);
2243 ret = device_register(&rdev->dev);
2247 dev_set_drvdata(&rdev->dev, rdev);
2249 /* set regulator constraints */
2250 ret = set_machine_constraints(rdev, &init_data->constraints);
2254 /* add attributes supported by this regulator */
2255 ret = add_regulator_attributes(rdev);
2259 /* set supply regulator if it exists */
2260 if (init_data->supply_regulator_dev) {
2261 ret = set_supply(rdev,
2262 dev_get_drvdata(init_data->supply_regulator_dev));
2267 /* add consumers devices */
2268 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2269 ret = set_consumer_device_supply(rdev,
2270 init_data->consumer_supplies[i].dev,
2271 init_data->consumer_supplies[i].dev_name,
2272 init_data->consumer_supplies[i].supply);
2274 for (--i; i >= 0; i--)
2275 unset_consumer_device_supply(rdev,
2276 init_data->consumer_supplies[i].dev_name,
2277 init_data->consumer_supplies[i].dev);
2282 list_add(&rdev->list, ®ulator_list);
2284 mutex_unlock(®ulator_list_mutex);
2288 device_unregister(&rdev->dev);
2289 /* device core frees rdev */
2290 rdev = ERR_PTR(ret);
2295 rdev = ERR_PTR(ret);
2298 EXPORT_SYMBOL_GPL(regulator_register);
2301 * regulator_unregister - unregister regulator
2302 * @rdev: regulator to unregister
2304 * Called by regulator drivers to unregister a regulator.
2306 void regulator_unregister(struct regulator_dev *rdev)
2311 mutex_lock(®ulator_list_mutex);
2312 WARN_ON(rdev->open_count);
2313 unset_regulator_supplies(rdev);
2314 list_del(&rdev->list);
2316 sysfs_remove_link(&rdev->dev.kobj, "supply");
2317 device_unregister(&rdev->dev);
2318 mutex_unlock(®ulator_list_mutex);
2320 EXPORT_SYMBOL_GPL(regulator_unregister);
2323 * regulator_suspend_prepare - prepare regulators for system wide suspend
2324 * @state: system suspend state
2326 * Configure each regulator with it's suspend operating parameters for state.
2327 * This will usually be called by machine suspend code prior to supending.
2329 int regulator_suspend_prepare(suspend_state_t state)
2331 struct regulator_dev *rdev;
2334 /* ON is handled by regulator active state */
2335 if (state == PM_SUSPEND_ON)
2338 mutex_lock(®ulator_list_mutex);
2339 list_for_each_entry(rdev, ®ulator_list, list) {
2341 mutex_lock(&rdev->mutex);
2342 ret = suspend_prepare(rdev, state);
2343 mutex_unlock(&rdev->mutex);
2346 printk(KERN_ERR "%s: failed to prepare %s\n",
2347 __func__, rdev_get_name(rdev));
2352 mutex_unlock(®ulator_list_mutex);
2355 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2358 * regulator_has_full_constraints - the system has fully specified constraints
2360 * Calling this function will cause the regulator API to disable all
2361 * regulators which have a zero use count and don't have an always_on
2362 * constraint in a late_initcall.
2364 * The intention is that this will become the default behaviour in a
2365 * future kernel release so users are encouraged to use this facility
2368 void regulator_has_full_constraints(void)
2370 has_full_constraints = 1;
2372 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2375 * rdev_get_drvdata - get rdev regulator driver data
2378 * Get rdev regulator driver private data. This call can be used in the
2379 * regulator driver context.
2381 void *rdev_get_drvdata(struct regulator_dev *rdev)
2383 return rdev->reg_data;
2385 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2388 * regulator_get_drvdata - get regulator driver data
2389 * @regulator: regulator
2391 * Get regulator driver private data. This call can be used in the consumer
2392 * driver context when non API regulator specific functions need to be called.
2394 void *regulator_get_drvdata(struct regulator *regulator)
2396 return regulator->rdev->reg_data;
2398 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2401 * regulator_set_drvdata - set regulator driver data
2402 * @regulator: regulator
2405 void regulator_set_drvdata(struct regulator *regulator, void *data)
2407 regulator->rdev->reg_data = data;
2409 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2412 * regulator_get_id - get regulator ID
2415 int rdev_get_id(struct regulator_dev *rdev)
2417 return rdev->desc->id;
2419 EXPORT_SYMBOL_GPL(rdev_get_id);
2421 struct device *rdev_get_dev(struct regulator_dev *rdev)
2425 EXPORT_SYMBOL_GPL(rdev_get_dev);
2427 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2429 return reg_init_data->driver_data;
2431 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2433 static int __init regulator_init(void)
2435 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2436 return class_register(®ulator_class);
2439 /* init early to allow our consumers to complete system booting */
2440 core_initcall(regulator_init);
2442 static int __init regulator_init_complete(void)
2444 struct regulator_dev *rdev;
2445 struct regulator_ops *ops;
2446 struct regulation_constraints *c;
2450 mutex_lock(®ulator_list_mutex);
2452 /* If we have a full configuration then disable any regulators
2453 * which are not in use or always_on. This will become the
2454 * default behaviour in the future.
2456 list_for_each_entry(rdev, ®ulator_list, list) {
2457 ops = rdev->desc->ops;
2458 c = rdev->constraints;
2460 name = rdev_get_name(rdev);
2462 if (!ops->disable || (c && c->always_on))
2465 mutex_lock(&rdev->mutex);
2467 if (rdev->use_count)
2470 /* If we can't read the status assume it's on. */
2471 if (ops->is_enabled)
2472 enabled = ops->is_enabled(rdev);
2479 if (has_full_constraints) {
2480 /* We log since this may kill the system if it
2482 printk(KERN_INFO "%s: disabling %s\n",
2484 ret = ops->disable(rdev);
2487 "%s: couldn't disable %s: %d\n",
2488 __func__, name, ret);
2491 /* The intention is that in future we will
2492 * assume that full constraints are provided
2493 * so warn even if we aren't going to do
2497 "%s: incomplete constraints, leaving %s on\n",
2502 mutex_unlock(&rdev->mutex);
2505 mutex_unlock(®ulator_list_mutex);
2509 late_initcall(regulator_init_complete);