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/slab.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include <linux/suspend.h>
23 #include <linux/delay.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/regulator.h>
33 #define REGULATOR_VERSION "0.5"
35 static DEFINE_MUTEX(regulator_list_mutex);
36 static LIST_HEAD(regulator_list);
37 static LIST_HEAD(regulator_map_list);
38 static int has_full_constraints;
39 static bool board_wants_dummy_regulator;
42 * struct regulator_map
44 * Used to provide symbolic supply names to devices.
46 struct regulator_map {
47 struct list_head list;
48 const char *dev_name; /* The dev_name() for the consumer */
50 struct regulator_dev *regulator;
56 * One for each consumer device.
60 struct list_head list;
65 struct device_attribute dev_attr;
66 struct regulator_dev *rdev;
69 static int _regulator_is_enabled(struct regulator_dev *rdev);
70 static int _regulator_disable(struct regulator_dev *rdev,
71 struct regulator_dev **supply_rdev_ptr);
72 static int _regulator_get_voltage(struct regulator_dev *rdev);
73 static int _regulator_get_current_limit(struct regulator_dev *rdev);
74 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
75 static void _notifier_call_chain(struct regulator_dev *rdev,
76 unsigned long event, void *data);
78 static const char *rdev_get_name(struct regulator_dev *rdev)
80 if (rdev->constraints && rdev->constraints->name)
81 return rdev->constraints->name;
82 else if (rdev->desc->name)
83 return rdev->desc->name;
88 /* gets the regulator for a given consumer device */
89 static struct regulator *get_device_regulator(struct device *dev)
91 struct regulator *regulator = NULL;
92 struct regulator_dev *rdev;
94 mutex_lock(®ulator_list_mutex);
95 list_for_each_entry(rdev, ®ulator_list, list) {
96 mutex_lock(&rdev->mutex);
97 list_for_each_entry(regulator, &rdev->consumer_list, list) {
98 if (regulator->dev == dev) {
99 mutex_unlock(&rdev->mutex);
100 mutex_unlock(®ulator_list_mutex);
104 mutex_unlock(&rdev->mutex);
106 mutex_unlock(®ulator_list_mutex);
110 /* Platform voltage constraint check */
111 static int regulator_check_voltage(struct regulator_dev *rdev,
112 int *min_uV, int *max_uV)
114 BUG_ON(*min_uV > *max_uV);
116 if (!rdev->constraints) {
117 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
118 rdev_get_name(rdev));
121 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
122 printk(KERN_ERR "%s: operation not allowed for %s\n",
123 __func__, rdev_get_name(rdev));
127 if (*max_uV > rdev->constraints->max_uV)
128 *max_uV = rdev->constraints->max_uV;
129 if (*min_uV < rdev->constraints->min_uV)
130 *min_uV = rdev->constraints->min_uV;
132 if (*min_uV > *max_uV)
138 /* current constraint check */
139 static int regulator_check_current_limit(struct regulator_dev *rdev,
140 int *min_uA, int *max_uA)
142 BUG_ON(*min_uA > *max_uA);
144 if (!rdev->constraints) {
145 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
146 rdev_get_name(rdev));
149 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
150 printk(KERN_ERR "%s: operation not allowed for %s\n",
151 __func__, rdev_get_name(rdev));
155 if (*max_uA > rdev->constraints->max_uA)
156 *max_uA = rdev->constraints->max_uA;
157 if (*min_uA < rdev->constraints->min_uA)
158 *min_uA = rdev->constraints->min_uA;
160 if (*min_uA > *max_uA)
166 /* operating mode constraint check */
167 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
170 case REGULATOR_MODE_FAST:
171 case REGULATOR_MODE_NORMAL:
172 case REGULATOR_MODE_IDLE:
173 case REGULATOR_MODE_STANDBY:
179 if (!rdev->constraints) {
180 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
181 rdev_get_name(rdev));
184 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
185 printk(KERN_ERR "%s: operation not allowed for %s\n",
186 __func__, rdev_get_name(rdev));
189 if (!(rdev->constraints->valid_modes_mask & mode)) {
190 printk(KERN_ERR "%s: invalid mode %x for %s\n",
191 __func__, mode, rdev_get_name(rdev));
197 /* dynamic regulator mode switching constraint check */
198 static int regulator_check_drms(struct regulator_dev *rdev)
200 if (!rdev->constraints) {
201 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
202 rdev_get_name(rdev));
205 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
206 printk(KERN_ERR "%s: operation not allowed for %s\n",
207 __func__, rdev_get_name(rdev));
213 static ssize_t device_requested_uA_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
216 struct regulator *regulator;
218 regulator = get_device_regulator(dev);
219 if (regulator == NULL)
222 return sprintf(buf, "%d\n", regulator->uA_load);
225 static ssize_t regulator_uV_show(struct device *dev,
226 struct device_attribute *attr, char *buf)
228 struct regulator_dev *rdev = dev_get_drvdata(dev);
231 mutex_lock(&rdev->mutex);
232 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
233 mutex_unlock(&rdev->mutex);
237 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
239 static ssize_t regulator_uA_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
242 struct regulator_dev *rdev = dev_get_drvdata(dev);
244 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
246 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
248 static ssize_t regulator_name_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
251 struct regulator_dev *rdev = dev_get_drvdata(dev);
253 return sprintf(buf, "%s\n", rdev_get_name(rdev));
256 static ssize_t regulator_print_opmode(char *buf, int mode)
259 case REGULATOR_MODE_FAST:
260 return sprintf(buf, "fast\n");
261 case REGULATOR_MODE_NORMAL:
262 return sprintf(buf, "normal\n");
263 case REGULATOR_MODE_IDLE:
264 return sprintf(buf, "idle\n");
265 case REGULATOR_MODE_STANDBY:
266 return sprintf(buf, "standby\n");
268 return sprintf(buf, "unknown\n");
271 static ssize_t regulator_opmode_show(struct device *dev,
272 struct device_attribute *attr, char *buf)
274 struct regulator_dev *rdev = dev_get_drvdata(dev);
276 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
278 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
280 static ssize_t regulator_print_state(char *buf, int state)
283 return sprintf(buf, "enabled\n");
285 return sprintf(buf, "disabled\n");
287 return sprintf(buf, "unknown\n");
290 static ssize_t regulator_state_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
293 struct regulator_dev *rdev = dev_get_drvdata(dev);
296 mutex_lock(&rdev->mutex);
297 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
298 mutex_unlock(&rdev->mutex);
302 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
304 static ssize_t regulator_status_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 struct regulator_dev *rdev = dev_get_drvdata(dev);
311 status = rdev->desc->ops->get_status(rdev);
316 case REGULATOR_STATUS_OFF:
319 case REGULATOR_STATUS_ON:
322 case REGULATOR_STATUS_ERROR:
325 case REGULATOR_STATUS_FAST:
328 case REGULATOR_STATUS_NORMAL:
331 case REGULATOR_STATUS_IDLE:
334 case REGULATOR_STATUS_STANDBY:
341 return sprintf(buf, "%s\n", label);
343 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
345 static ssize_t regulator_min_uA_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
348 struct regulator_dev *rdev = dev_get_drvdata(dev);
350 if (!rdev->constraints)
351 return sprintf(buf, "constraint not defined\n");
353 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
355 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
357 static ssize_t regulator_max_uA_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
360 struct regulator_dev *rdev = dev_get_drvdata(dev);
362 if (!rdev->constraints)
363 return sprintf(buf, "constraint not defined\n");
365 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
367 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
369 static ssize_t regulator_min_uV_show(struct device *dev,
370 struct device_attribute *attr, char *buf)
372 struct regulator_dev *rdev = dev_get_drvdata(dev);
374 if (!rdev->constraints)
375 return sprintf(buf, "constraint not defined\n");
377 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
379 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
381 static ssize_t regulator_max_uV_show(struct device *dev,
382 struct device_attribute *attr, char *buf)
384 struct regulator_dev *rdev = dev_get_drvdata(dev);
386 if (!rdev->constraints)
387 return sprintf(buf, "constraint not defined\n");
389 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
391 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
393 static ssize_t regulator_total_uA_show(struct device *dev,
394 struct device_attribute *attr, char *buf)
396 struct regulator_dev *rdev = dev_get_drvdata(dev);
397 struct regulator *regulator;
400 mutex_lock(&rdev->mutex);
401 list_for_each_entry(regulator, &rdev->consumer_list, list)
402 uA += regulator->uA_load;
403 mutex_unlock(&rdev->mutex);
404 return sprintf(buf, "%d\n", uA);
406 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
408 static ssize_t regulator_num_users_show(struct device *dev,
409 struct device_attribute *attr, char *buf)
411 struct regulator_dev *rdev = dev_get_drvdata(dev);
412 return sprintf(buf, "%d\n", rdev->use_count);
415 static ssize_t regulator_type_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
418 struct regulator_dev *rdev = dev_get_drvdata(dev);
420 switch (rdev->desc->type) {
421 case REGULATOR_VOLTAGE:
422 return sprintf(buf, "voltage\n");
423 case REGULATOR_CURRENT:
424 return sprintf(buf, "current\n");
426 return sprintf(buf, "unknown\n");
429 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
436 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
437 regulator_suspend_mem_uV_show, NULL);
439 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
442 struct regulator_dev *rdev = dev_get_drvdata(dev);
444 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
446 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
447 regulator_suspend_disk_uV_show, NULL);
449 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
452 struct regulator_dev *rdev = dev_get_drvdata(dev);
454 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
456 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
457 regulator_suspend_standby_uV_show, NULL);
459 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
462 struct regulator_dev *rdev = dev_get_drvdata(dev);
464 return regulator_print_opmode(buf,
465 rdev->constraints->state_mem.mode);
467 static DEVICE_ATTR(suspend_mem_mode, 0444,
468 regulator_suspend_mem_mode_show, NULL);
470 static ssize_t regulator_suspend_disk_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_disk.mode);
478 static DEVICE_ATTR(suspend_disk_mode, 0444,
479 regulator_suspend_disk_mode_show, NULL);
481 static ssize_t regulator_suspend_standby_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_standby.mode);
489 static DEVICE_ATTR(suspend_standby_mode, 0444,
490 regulator_suspend_standby_mode_show, NULL);
492 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
495 struct regulator_dev *rdev = dev_get_drvdata(dev);
497 return regulator_print_state(buf,
498 rdev->constraints->state_mem.enabled);
500 static DEVICE_ATTR(suspend_mem_state, 0444,
501 regulator_suspend_mem_state_show, NULL);
503 static ssize_t regulator_suspend_disk_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_disk.enabled);
511 static DEVICE_ATTR(suspend_disk_state, 0444,
512 regulator_suspend_disk_state_show, NULL);
514 static ssize_t regulator_suspend_standby_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_standby.enabled);
522 static DEVICE_ATTR(suspend_standby_state, 0444,
523 regulator_suspend_standby_state_show, NULL);
527 * These are the only attributes are present for all regulators.
528 * Other attributes are a function of regulator functionality.
530 static struct device_attribute regulator_dev_attrs[] = {
531 __ATTR(name, 0444, regulator_name_show, NULL),
532 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
533 __ATTR(type, 0444, regulator_type_show, NULL),
537 static void regulator_dev_release(struct device *dev)
539 struct regulator_dev *rdev = dev_get_drvdata(dev);
543 static struct class regulator_class = {
545 .dev_release = regulator_dev_release,
546 .dev_attrs = regulator_dev_attrs,
549 /* Calculate the new optimum regulator operating mode based on the new total
550 * consumer load. All locks held by caller */
551 static void drms_uA_update(struct regulator_dev *rdev)
553 struct regulator *sibling;
554 int current_uA = 0, output_uV, input_uV, err;
557 err = regulator_check_drms(rdev);
558 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
559 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
562 /* get output voltage */
563 output_uV = rdev->desc->ops->get_voltage(rdev);
567 /* get input voltage */
568 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
569 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
571 input_uV = rdev->constraints->input_uV;
575 /* calc total requested load */
576 list_for_each_entry(sibling, &rdev->consumer_list, list)
577 current_uA += sibling->uA_load;
579 /* now get the optimum mode for our new total regulator load */
580 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
581 output_uV, current_uA);
583 /* check the new mode is allowed */
584 err = regulator_check_mode(rdev, mode);
586 rdev->desc->ops->set_mode(rdev, mode);
589 static int suspend_set_state(struct regulator_dev *rdev,
590 struct regulator_state *rstate)
595 can_set_state = rdev->desc->ops->set_suspend_enable &&
596 rdev->desc->ops->set_suspend_disable;
598 /* If we have no suspend mode configration don't set anything;
599 * only warn if the driver actually makes the suspend mode
602 if (!rstate->enabled && !rstate->disabled) {
604 printk(KERN_WARNING "%s: No configuration for %s\n",
605 __func__, rdev_get_name(rdev));
609 if (rstate->enabled && rstate->disabled) {
610 printk(KERN_ERR "%s: invalid configuration for %s\n",
611 __func__, rdev_get_name(rdev));
615 if (!can_set_state) {
616 printk(KERN_ERR "%s: no way to set suspend state\n",
622 ret = rdev->desc->ops->set_suspend_enable(rdev);
624 ret = rdev->desc->ops->set_suspend_disable(rdev);
626 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
630 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
631 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
633 printk(KERN_ERR "%s: failed to set voltage\n",
639 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
640 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
642 printk(KERN_ERR "%s: failed to set mode\n", __func__);
649 /* locks held by caller */
650 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
652 if (!rdev->constraints)
656 case PM_SUSPEND_STANDBY:
657 return suspend_set_state(rdev,
658 &rdev->constraints->state_standby);
660 return suspend_set_state(rdev,
661 &rdev->constraints->state_mem);
663 return suspend_set_state(rdev,
664 &rdev->constraints->state_disk);
670 static void print_constraints(struct regulator_dev *rdev)
672 struct regulation_constraints *constraints = rdev->constraints;
677 if (constraints->min_uV && constraints->max_uV) {
678 if (constraints->min_uV == constraints->max_uV)
679 count += sprintf(buf + count, "%d mV ",
680 constraints->min_uV / 1000);
682 count += sprintf(buf + count, "%d <--> %d mV ",
683 constraints->min_uV / 1000,
684 constraints->max_uV / 1000);
687 if (!constraints->min_uV ||
688 constraints->min_uV != constraints->max_uV) {
689 ret = _regulator_get_voltage(rdev);
691 count += sprintf(buf + count, "at %d mV ", ret / 1000);
694 if (constraints->min_uA && constraints->max_uA) {
695 if (constraints->min_uA == constraints->max_uA)
696 count += sprintf(buf + count, "%d mA ",
697 constraints->min_uA / 1000);
699 count += sprintf(buf + count, "%d <--> %d mA ",
700 constraints->min_uA / 1000,
701 constraints->max_uA / 1000);
704 if (!constraints->min_uA ||
705 constraints->min_uA != constraints->max_uA) {
706 ret = _regulator_get_current_limit(rdev);
708 count += sprintf(buf + count, "at %d mA ", ret / 1000);
711 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
712 count += sprintf(buf + count, "fast ");
713 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
714 count += sprintf(buf + count, "normal ");
715 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
716 count += sprintf(buf + count, "idle ");
717 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
718 count += sprintf(buf + count, "standby");
720 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
723 static int machine_constraints_voltage(struct regulator_dev *rdev,
724 struct regulation_constraints *constraints)
726 struct regulator_ops *ops = rdev->desc->ops;
727 const char *name = rdev_get_name(rdev);
731 /* do we need to apply the constraint voltage */
732 if (rdev->constraints->apply_uV &&
733 rdev->constraints->min_uV == rdev->constraints->max_uV &&
735 ret = ops->set_voltage(rdev,
736 rdev->constraints->min_uV,
737 rdev->constraints->max_uV,
740 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
742 rdev->constraints->min_uV, name);
743 rdev->constraints = NULL;
748 /* constrain machine-level voltage specs to fit
749 * the actual range supported by this regulator.
751 if (ops->list_voltage && rdev->desc->n_voltages) {
752 int count = rdev->desc->n_voltages;
754 int min_uV = INT_MAX;
755 int max_uV = INT_MIN;
756 int cmin = constraints->min_uV;
757 int cmax = constraints->max_uV;
759 /* it's safe to autoconfigure fixed-voltage supplies
760 and the constraints are used by list_voltage. */
761 if (count == 1 && !cmin) {
764 constraints->min_uV = cmin;
765 constraints->max_uV = cmax;
768 /* voltage constraints are optional */
769 if ((cmin == 0) && (cmax == 0))
772 /* else require explicit machine-level constraints */
773 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
774 pr_err("%s: %s '%s' voltage constraints\n",
775 __func__, "invalid", name);
779 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
780 for (i = 0; i < count; i++) {
783 value = ops->list_voltage(rdev, i);
787 /* maybe adjust [min_uV..max_uV] */
788 if (value >= cmin && value < min_uV)
790 if (value <= cmax && value > max_uV)
794 /* final: [min_uV..max_uV] valid iff constraints valid */
795 if (max_uV < min_uV) {
796 pr_err("%s: %s '%s' voltage constraints\n",
797 __func__, "unsupportable", name);
801 /* use regulator's subset of machine constraints */
802 if (constraints->min_uV < min_uV) {
803 pr_debug("%s: override '%s' %s, %d -> %d\n",
804 __func__, name, "min_uV",
805 constraints->min_uV, min_uV);
806 constraints->min_uV = min_uV;
808 if (constraints->max_uV > max_uV) {
809 pr_debug("%s: override '%s' %s, %d -> %d\n",
810 __func__, name, "max_uV",
811 constraints->max_uV, max_uV);
812 constraints->max_uV = max_uV;
820 * set_machine_constraints - sets regulator constraints
821 * @rdev: regulator source
822 * @constraints: constraints to apply
824 * Allows platform initialisation code to define and constrain
825 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
826 * Constraints *must* be set by platform code in order for some
827 * regulator operations to proceed i.e. set_voltage, set_current_limit,
830 static int set_machine_constraints(struct regulator_dev *rdev,
831 struct regulation_constraints *constraints)
835 struct regulator_ops *ops = rdev->desc->ops;
837 rdev->constraints = constraints;
839 name = rdev_get_name(rdev);
841 ret = machine_constraints_voltage(rdev, constraints);
845 /* do we need to setup our suspend state */
846 if (constraints->initial_state) {
847 ret = suspend_prepare(rdev, constraints->initial_state);
849 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
851 rdev->constraints = NULL;
856 if (constraints->initial_mode) {
857 if (!ops->set_mode) {
858 printk(KERN_ERR "%s: no set_mode operation for %s\n",
864 ret = ops->set_mode(rdev, constraints->initial_mode);
867 "%s: failed to set initial mode for %s: %d\n",
868 __func__, name, ret);
873 /* If the constraints say the regulator should be on at this point
874 * and we have control then make sure it is enabled.
876 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
877 ret = ops->enable(rdev);
879 printk(KERN_ERR "%s: failed to enable %s\n",
881 rdev->constraints = NULL;
886 print_constraints(rdev);
892 * set_supply - set regulator supply regulator
893 * @rdev: regulator name
894 * @supply_rdev: supply regulator name
896 * Called by platform initialisation code to set the supply regulator for this
897 * regulator. This ensures that a regulators supply will also be enabled by the
898 * core if it's child is enabled.
900 static int set_supply(struct regulator_dev *rdev,
901 struct regulator_dev *supply_rdev)
905 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
909 "%s: could not add device link %s err %d\n",
910 __func__, supply_rdev->dev.kobj.name, err);
913 rdev->supply = supply_rdev;
914 list_add(&rdev->slist, &supply_rdev->supply_list);
920 * set_consumer_device_supply - Bind a regulator to a symbolic supply
921 * @rdev: regulator source
922 * @consumer_dev: device the supply applies to
923 * @consumer_dev_name: dev_name() string for device supply applies to
924 * @supply: symbolic name for supply
926 * Allows platform initialisation code to map physical regulator
927 * sources to symbolic names for supplies for use by devices. Devices
928 * should use these symbolic names to request regulators, avoiding the
929 * need to provide board-specific regulator names as platform data.
931 * Only one of consumer_dev and consumer_dev_name may be specified.
933 static int set_consumer_device_supply(struct regulator_dev *rdev,
934 struct device *consumer_dev, const char *consumer_dev_name,
937 struct regulator_map *node;
940 if (consumer_dev && consumer_dev_name)
943 if (!consumer_dev_name && consumer_dev)
944 consumer_dev_name = dev_name(consumer_dev);
949 if (consumer_dev_name != NULL)
954 list_for_each_entry(node, ®ulator_map_list, list) {
955 if (node->dev_name && consumer_dev_name) {
956 if (strcmp(node->dev_name, consumer_dev_name) != 0)
958 } else if (node->dev_name || consumer_dev_name) {
962 if (strcmp(node->supply, supply) != 0)
965 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
966 dev_name(&node->regulator->dev),
967 node->regulator->desc->name,
969 dev_name(&rdev->dev), rdev_get_name(rdev));
973 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
977 node->regulator = rdev;
978 node->supply = supply;
981 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
982 if (node->dev_name == NULL) {
988 list_add(&node->list, ®ulator_map_list);
992 static void unset_regulator_supplies(struct regulator_dev *rdev)
994 struct regulator_map *node, *n;
996 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
997 if (rdev == node->regulator) {
998 list_del(&node->list);
999 kfree(node->dev_name);
1005 #define REG_STR_SIZE 32
1007 static struct regulator *create_regulator(struct regulator_dev *rdev,
1009 const char *supply_name)
1011 struct regulator *regulator;
1012 char buf[REG_STR_SIZE];
1015 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1016 if (regulator == NULL)
1019 mutex_lock(&rdev->mutex);
1020 regulator->rdev = rdev;
1021 list_add(®ulator->list, &rdev->consumer_list);
1024 /* create a 'requested_microamps_name' sysfs entry */
1025 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1027 if (size >= REG_STR_SIZE)
1030 regulator->dev = dev;
1031 sysfs_attr_init(®ulator->dev_attr.attr);
1032 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1033 if (regulator->dev_attr.attr.name == NULL)
1036 regulator->dev_attr.attr.mode = 0444;
1037 regulator->dev_attr.show = device_requested_uA_show;
1038 err = device_create_file(dev, ®ulator->dev_attr);
1040 printk(KERN_WARNING "%s: could not add regulator_dev"
1041 " load sysfs\n", __func__);
1045 /* also add a link to the device sysfs entry */
1046 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1047 dev->kobj.name, supply_name);
1048 if (size >= REG_STR_SIZE)
1051 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1052 if (regulator->supply_name == NULL)
1055 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1059 "%s: could not add device link %s err %d\n",
1060 __func__, dev->kobj.name, err);
1064 mutex_unlock(&rdev->mutex);
1067 kfree(regulator->supply_name);
1069 device_remove_file(regulator->dev, ®ulator->dev_attr);
1071 kfree(regulator->dev_attr.attr.name);
1073 list_del(®ulator->list);
1075 mutex_unlock(&rdev->mutex);
1079 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1081 if (!rdev->desc->ops->enable_time)
1083 return rdev->desc->ops->enable_time(rdev);
1086 /* Internal regulator request function */
1087 static struct regulator *_regulator_get(struct device *dev, const char *id,
1090 struct regulator_dev *rdev;
1091 struct regulator_map *map;
1092 struct regulator *regulator = ERR_PTR(-ENODEV);
1093 const char *devname = NULL;
1097 printk(KERN_ERR "regulator: get() with no identifier\n");
1102 devname = dev_name(dev);
1104 mutex_lock(®ulator_list_mutex);
1106 list_for_each_entry(map, ®ulator_map_list, list) {
1107 /* If the mapping has a device set up it must match */
1108 if (map->dev_name &&
1109 (!devname || strcmp(map->dev_name, devname)))
1112 if (strcmp(map->supply, id) == 0) {
1113 rdev = map->regulator;
1118 if (board_wants_dummy_regulator) {
1119 rdev = dummy_regulator_rdev;
1123 #ifdef CONFIG_REGULATOR_DUMMY
1125 devname = "deviceless";
1127 /* If the board didn't flag that it was fully constrained then
1128 * substitute in a dummy regulator so consumers can continue.
1130 if (!has_full_constraints) {
1131 pr_warning("%s supply %s not found, using dummy regulator\n",
1133 rdev = dummy_regulator_rdev;
1138 mutex_unlock(®ulator_list_mutex);
1142 if (rdev->exclusive) {
1143 regulator = ERR_PTR(-EPERM);
1147 if (exclusive && rdev->open_count) {
1148 regulator = ERR_PTR(-EBUSY);
1152 if (!try_module_get(rdev->owner))
1155 regulator = create_regulator(rdev, dev, id);
1156 if (regulator == NULL) {
1157 regulator = ERR_PTR(-ENOMEM);
1158 module_put(rdev->owner);
1163 rdev->exclusive = 1;
1165 ret = _regulator_is_enabled(rdev);
1167 rdev->use_count = 1;
1169 rdev->use_count = 0;
1173 mutex_unlock(®ulator_list_mutex);
1179 * regulator_get - lookup and obtain a reference to a regulator.
1180 * @dev: device for regulator "consumer"
1181 * @id: Supply name or regulator ID.
1183 * Returns a struct regulator corresponding to the regulator producer,
1184 * or IS_ERR() condition containing errno.
1186 * Use of supply names configured via regulator_set_device_supply() is
1187 * strongly encouraged. It is recommended that the supply name used
1188 * should match the name used for the supply and/or the relevant
1189 * device pins in the datasheet.
1191 struct regulator *regulator_get(struct device *dev, const char *id)
1193 return _regulator_get(dev, id, 0);
1195 EXPORT_SYMBOL_GPL(regulator_get);
1198 * regulator_get_exclusive - obtain exclusive access to a regulator.
1199 * @dev: device for regulator "consumer"
1200 * @id: Supply name or regulator ID.
1202 * Returns a struct regulator corresponding to the regulator producer,
1203 * or IS_ERR() condition containing errno. Other consumers will be
1204 * unable to obtain this reference is held and the use count for the
1205 * regulator will be initialised to reflect the current state of the
1208 * This is intended for use by consumers which cannot tolerate shared
1209 * use of the regulator such as those which need to force the
1210 * regulator off for correct operation of the hardware they are
1213 * Use of supply names configured via regulator_set_device_supply() is
1214 * strongly encouraged. It is recommended that the supply name used
1215 * should match the name used for the supply and/or the relevant
1216 * device pins in the datasheet.
1218 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1220 return _regulator_get(dev, id, 1);
1222 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1225 * regulator_put - "free" the regulator source
1226 * @regulator: regulator source
1228 * Note: drivers must ensure that all regulator_enable calls made on this
1229 * regulator source are balanced by regulator_disable calls prior to calling
1232 void regulator_put(struct regulator *regulator)
1234 struct regulator_dev *rdev;
1236 if (regulator == NULL || IS_ERR(regulator))
1239 mutex_lock(®ulator_list_mutex);
1240 rdev = regulator->rdev;
1242 /* remove any sysfs entries */
1243 if (regulator->dev) {
1244 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1245 kfree(regulator->supply_name);
1246 device_remove_file(regulator->dev, ®ulator->dev_attr);
1247 kfree(regulator->dev_attr.attr.name);
1249 list_del(®ulator->list);
1253 rdev->exclusive = 0;
1255 module_put(rdev->owner);
1256 mutex_unlock(®ulator_list_mutex);
1258 EXPORT_SYMBOL_GPL(regulator_put);
1260 static int _regulator_can_change_status(struct regulator_dev *rdev)
1262 if (!rdev->constraints)
1265 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1271 /* locks held by regulator_enable() */
1272 static int _regulator_enable(struct regulator_dev *rdev)
1276 /* do we need to enable the supply regulator first */
1278 mutex_lock(&rdev->supply->mutex);
1279 ret = _regulator_enable(rdev->supply);
1280 mutex_unlock(&rdev->supply->mutex);
1282 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1283 __func__, rdev_get_name(rdev), ret);
1288 /* check voltage and requested load before enabling */
1289 if (rdev->constraints &&
1290 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1291 drms_uA_update(rdev);
1293 if (rdev->use_count == 0) {
1294 /* The regulator may on if it's not switchable or left on */
1295 ret = _regulator_is_enabled(rdev);
1296 if (ret == -EINVAL || ret == 0) {
1297 if (!_regulator_can_change_status(rdev))
1300 if (!rdev->desc->ops->enable)
1303 /* Query before enabling in case configuration
1305 ret = _regulator_get_enable_time(rdev);
1310 "%s: enable_time() failed for %s: %d\n",
1311 __func__, rdev_get_name(rdev),
1316 trace_regulator_enable(rdev_get_name(rdev));
1318 /* Allow the regulator to ramp; it would be useful
1319 * to extend this for bulk operations so that the
1320 * regulators can ramp together. */
1321 ret = rdev->desc->ops->enable(rdev);
1325 trace_regulator_enable_delay(rdev_get_name(rdev));
1327 if (delay >= 1000) {
1328 mdelay(delay / 1000);
1329 udelay(delay % 1000);
1334 trace_regulator_enable_complete(rdev_get_name(rdev));
1336 } else if (ret < 0) {
1337 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
1338 __func__, rdev_get_name(rdev), ret);
1341 /* Fallthrough on positive return values - already enabled */
1350 * regulator_enable - enable regulator output
1351 * @regulator: regulator source
1353 * Request that the regulator be enabled with the regulator output at
1354 * the predefined voltage or current value. Calls to regulator_enable()
1355 * must be balanced with calls to regulator_disable().
1357 * NOTE: the output value can be set by other drivers, boot loader or may be
1358 * hardwired in the regulator.
1360 int regulator_enable(struct regulator *regulator)
1362 struct regulator_dev *rdev = regulator->rdev;
1365 mutex_lock(&rdev->mutex);
1366 ret = _regulator_enable(rdev);
1367 mutex_unlock(&rdev->mutex);
1370 EXPORT_SYMBOL_GPL(regulator_enable);
1372 /* locks held by regulator_disable() */
1373 static int _regulator_disable(struct regulator_dev *rdev,
1374 struct regulator_dev **supply_rdev_ptr)
1377 *supply_rdev_ptr = NULL;
1379 if (WARN(rdev->use_count <= 0,
1380 "unbalanced disables for %s\n",
1381 rdev_get_name(rdev)))
1384 /* are we the last user and permitted to disable ? */
1385 if (rdev->use_count == 1 &&
1386 (rdev->constraints && !rdev->constraints->always_on)) {
1388 /* we are last user */
1389 if (_regulator_can_change_status(rdev) &&
1390 rdev->desc->ops->disable) {
1391 trace_regulator_disable(rdev_get_name(rdev));
1393 ret = rdev->desc->ops->disable(rdev);
1395 printk(KERN_ERR "%s: failed to disable %s\n",
1396 __func__, rdev_get_name(rdev));
1400 trace_regulator_disable_complete(rdev_get_name(rdev));
1402 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1406 /* decrease our supplies ref count and disable if required */
1407 *supply_rdev_ptr = rdev->supply;
1409 rdev->use_count = 0;
1410 } else if (rdev->use_count > 1) {
1412 if (rdev->constraints &&
1413 (rdev->constraints->valid_ops_mask &
1414 REGULATOR_CHANGE_DRMS))
1415 drms_uA_update(rdev);
1423 * regulator_disable - disable regulator output
1424 * @regulator: regulator source
1426 * Disable the regulator output voltage or current. Calls to
1427 * regulator_enable() must be balanced with calls to
1428 * regulator_disable().
1430 * NOTE: this will only disable the regulator output if no other consumer
1431 * devices have it enabled, the regulator device supports disabling and
1432 * machine constraints permit this operation.
1434 int regulator_disable(struct regulator *regulator)
1436 struct regulator_dev *rdev = regulator->rdev;
1437 struct regulator_dev *supply_rdev = NULL;
1440 mutex_lock(&rdev->mutex);
1441 ret = _regulator_disable(rdev, &supply_rdev);
1442 mutex_unlock(&rdev->mutex);
1444 /* decrease our supplies ref count and disable if required */
1445 while (supply_rdev != NULL) {
1448 mutex_lock(&rdev->mutex);
1449 _regulator_disable(rdev, &supply_rdev);
1450 mutex_unlock(&rdev->mutex);
1455 EXPORT_SYMBOL_GPL(regulator_disable);
1457 /* locks held by regulator_force_disable() */
1458 static int _regulator_force_disable(struct regulator_dev *rdev,
1459 struct regulator_dev **supply_rdev_ptr)
1464 if (rdev->desc->ops->disable) {
1465 /* ah well, who wants to live forever... */
1466 ret = rdev->desc->ops->disable(rdev);
1468 printk(KERN_ERR "%s: failed to force disable %s\n",
1469 __func__, rdev_get_name(rdev));
1472 /* notify other consumers that power has been forced off */
1473 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1474 REGULATOR_EVENT_DISABLE, NULL);
1477 /* decrease our supplies ref count and disable if required */
1478 *supply_rdev_ptr = rdev->supply;
1480 rdev->use_count = 0;
1485 * regulator_force_disable - force disable regulator output
1486 * @regulator: regulator source
1488 * Forcibly disable the regulator output voltage or current.
1489 * NOTE: this *will* disable the regulator output even if other consumer
1490 * devices have it enabled. This should be used for situations when device
1491 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1493 int regulator_force_disable(struct regulator *regulator)
1495 struct regulator_dev *supply_rdev = NULL;
1498 mutex_lock(®ulator->rdev->mutex);
1499 regulator->uA_load = 0;
1500 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
1501 mutex_unlock(®ulator->rdev->mutex);
1504 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1508 EXPORT_SYMBOL_GPL(regulator_force_disable);
1510 static int _regulator_is_enabled(struct regulator_dev *rdev)
1512 /* If we don't know then assume that the regulator is always on */
1513 if (!rdev->desc->ops->is_enabled)
1516 return rdev->desc->ops->is_enabled(rdev);
1520 * regulator_is_enabled - is the regulator output enabled
1521 * @regulator: regulator source
1523 * Returns positive if the regulator driver backing the source/client
1524 * has requested that the device be enabled, zero if it hasn't, else a
1525 * negative errno code.
1527 * Note that the device backing this regulator handle can have multiple
1528 * users, so it might be enabled even if regulator_enable() was never
1529 * called for this particular source.
1531 int regulator_is_enabled(struct regulator *regulator)
1535 mutex_lock(®ulator->rdev->mutex);
1536 ret = _regulator_is_enabled(regulator->rdev);
1537 mutex_unlock(®ulator->rdev->mutex);
1541 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1544 * regulator_count_voltages - count regulator_list_voltage() selectors
1545 * @regulator: regulator source
1547 * Returns number of selectors, or negative errno. Selectors are
1548 * numbered starting at zero, and typically correspond to bitfields
1549 * in hardware registers.
1551 int regulator_count_voltages(struct regulator *regulator)
1553 struct regulator_dev *rdev = regulator->rdev;
1555 return rdev->desc->n_voltages ? : -EINVAL;
1557 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1560 * regulator_list_voltage - enumerate supported voltages
1561 * @regulator: regulator source
1562 * @selector: identify voltage to list
1563 * Context: can sleep
1565 * Returns a voltage that can be passed to @regulator_set_voltage(),
1566 * zero if this selector code can't be used on this system, or a
1569 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1571 struct regulator_dev *rdev = regulator->rdev;
1572 struct regulator_ops *ops = rdev->desc->ops;
1575 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1578 mutex_lock(&rdev->mutex);
1579 ret = ops->list_voltage(rdev, selector);
1580 mutex_unlock(&rdev->mutex);
1583 if (ret < rdev->constraints->min_uV)
1585 else if (ret > rdev->constraints->max_uV)
1591 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1594 * regulator_is_supported_voltage - check if a voltage range can be supported
1596 * @regulator: Regulator to check.
1597 * @min_uV: Minimum required voltage in uV.
1598 * @max_uV: Maximum required voltage in uV.
1600 * Returns a boolean or a negative error code.
1602 int regulator_is_supported_voltage(struct regulator *regulator,
1603 int min_uV, int max_uV)
1605 int i, voltages, ret;
1607 ret = regulator_count_voltages(regulator);
1612 for (i = 0; i < voltages; i++) {
1613 ret = regulator_list_voltage(regulator, i);
1615 if (ret >= min_uV && ret <= max_uV)
1623 * regulator_set_voltage - set regulator output voltage
1624 * @regulator: regulator source
1625 * @min_uV: Minimum required voltage in uV
1626 * @max_uV: Maximum acceptable voltage in uV
1628 * Sets a voltage regulator to the desired output voltage. This can be set
1629 * during any regulator state. IOW, regulator can be disabled or enabled.
1631 * If the regulator is enabled then the voltage will change to the new value
1632 * immediately otherwise if the regulator is disabled the regulator will
1633 * output at the new voltage when enabled.
1635 * NOTE: If the regulator is shared between several devices then the lowest
1636 * request voltage that meets the system constraints will be used.
1637 * Regulator system constraints must be set for this regulator before
1638 * calling this function otherwise this call will fail.
1640 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1642 struct regulator_dev *rdev = regulator->rdev;
1646 mutex_lock(&rdev->mutex);
1649 if (!rdev->desc->ops->set_voltage) {
1654 /* constraints check */
1655 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1658 regulator->min_uV = min_uV;
1659 regulator->max_uV = max_uV;
1661 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1663 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, &selector);
1665 if (rdev->desc->ops->list_voltage)
1666 selector = rdev->desc->ops->list_voltage(rdev, selector);
1670 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1673 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1674 mutex_unlock(&rdev->mutex);
1677 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1679 static int _regulator_get_voltage(struct regulator_dev *rdev)
1682 if (rdev->desc->ops->get_voltage)
1683 return rdev->desc->ops->get_voltage(rdev);
1689 * regulator_get_voltage - get regulator output voltage
1690 * @regulator: regulator source
1692 * This returns the current regulator voltage in uV.
1694 * NOTE: If the regulator is disabled it will return the voltage value. This
1695 * function should not be used to determine regulator state.
1697 int regulator_get_voltage(struct regulator *regulator)
1701 mutex_lock(®ulator->rdev->mutex);
1703 ret = _regulator_get_voltage(regulator->rdev);
1705 mutex_unlock(®ulator->rdev->mutex);
1709 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1712 * regulator_set_current_limit - set regulator output current limit
1713 * @regulator: regulator source
1714 * @min_uA: Minimuum supported current in uA
1715 * @max_uA: Maximum supported current in uA
1717 * Sets current sink to the desired output current. This can be set during
1718 * any regulator state. IOW, regulator can be disabled or enabled.
1720 * If the regulator is enabled then the current will change to the new value
1721 * immediately otherwise if the regulator is disabled the regulator will
1722 * output at the new current when enabled.
1724 * NOTE: Regulator system constraints must be set for this regulator before
1725 * calling this function otherwise this call will fail.
1727 int regulator_set_current_limit(struct regulator *regulator,
1728 int min_uA, int max_uA)
1730 struct regulator_dev *rdev = regulator->rdev;
1733 mutex_lock(&rdev->mutex);
1736 if (!rdev->desc->ops->set_current_limit) {
1741 /* constraints check */
1742 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1746 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1748 mutex_unlock(&rdev->mutex);
1751 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1753 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1757 mutex_lock(&rdev->mutex);
1760 if (!rdev->desc->ops->get_current_limit) {
1765 ret = rdev->desc->ops->get_current_limit(rdev);
1767 mutex_unlock(&rdev->mutex);
1772 * regulator_get_current_limit - get regulator output current
1773 * @regulator: regulator source
1775 * This returns the current supplied by the specified current sink in uA.
1777 * NOTE: If the regulator is disabled it will return the current value. This
1778 * function should not be used to determine regulator state.
1780 int regulator_get_current_limit(struct regulator *regulator)
1782 return _regulator_get_current_limit(regulator->rdev);
1784 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1787 * regulator_set_mode - set regulator operating mode
1788 * @regulator: regulator source
1789 * @mode: operating mode - one of the REGULATOR_MODE constants
1791 * Set regulator operating mode to increase regulator efficiency or improve
1792 * regulation performance.
1794 * NOTE: Regulator system constraints must be set for this regulator before
1795 * calling this function otherwise this call will fail.
1797 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1799 struct regulator_dev *rdev = regulator->rdev;
1801 int regulator_curr_mode;
1803 mutex_lock(&rdev->mutex);
1806 if (!rdev->desc->ops->set_mode) {
1811 /* return if the same mode is requested */
1812 if (rdev->desc->ops->get_mode) {
1813 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1814 if (regulator_curr_mode == mode) {
1820 /* constraints check */
1821 ret = regulator_check_mode(rdev, mode);
1825 ret = rdev->desc->ops->set_mode(rdev, mode);
1827 mutex_unlock(&rdev->mutex);
1830 EXPORT_SYMBOL_GPL(regulator_set_mode);
1832 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1836 mutex_lock(&rdev->mutex);
1839 if (!rdev->desc->ops->get_mode) {
1844 ret = rdev->desc->ops->get_mode(rdev);
1846 mutex_unlock(&rdev->mutex);
1851 * regulator_get_mode - get regulator operating mode
1852 * @regulator: regulator source
1854 * Get the current regulator operating mode.
1856 unsigned int regulator_get_mode(struct regulator *regulator)
1858 return _regulator_get_mode(regulator->rdev);
1860 EXPORT_SYMBOL_GPL(regulator_get_mode);
1863 * regulator_set_optimum_mode - set regulator optimum operating mode
1864 * @regulator: regulator source
1865 * @uA_load: load current
1867 * Notifies the regulator core of a new device load. This is then used by
1868 * DRMS (if enabled by constraints) to set the most efficient regulator
1869 * operating mode for the new regulator loading.
1871 * Consumer devices notify their supply regulator of the maximum power
1872 * they will require (can be taken from device datasheet in the power
1873 * consumption tables) when they change operational status and hence power
1874 * state. Examples of operational state changes that can affect power
1875 * consumption are :-
1877 * o Device is opened / closed.
1878 * o Device I/O is about to begin or has just finished.
1879 * o Device is idling in between work.
1881 * This information is also exported via sysfs to userspace.
1883 * DRMS will sum the total requested load on the regulator and change
1884 * to the most efficient operating mode if platform constraints allow.
1886 * Returns the new regulator mode or error.
1888 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1890 struct regulator_dev *rdev = regulator->rdev;
1891 struct regulator *consumer;
1892 int ret, output_uV, input_uV, total_uA_load = 0;
1895 mutex_lock(&rdev->mutex);
1897 regulator->uA_load = uA_load;
1898 ret = regulator_check_drms(rdev);
1904 if (!rdev->desc->ops->get_optimum_mode)
1907 /* get output voltage */
1908 output_uV = rdev->desc->ops->get_voltage(rdev);
1909 if (output_uV <= 0) {
1910 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1911 __func__, rdev_get_name(rdev));
1915 /* get input voltage */
1916 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1917 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1919 input_uV = rdev->constraints->input_uV;
1920 if (input_uV <= 0) {
1921 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1922 __func__, rdev_get_name(rdev));
1926 /* calc total requested load for this regulator */
1927 list_for_each_entry(consumer, &rdev->consumer_list, list)
1928 total_uA_load += consumer->uA_load;
1930 mode = rdev->desc->ops->get_optimum_mode(rdev,
1931 input_uV, output_uV,
1933 ret = regulator_check_mode(rdev, mode);
1935 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1936 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
1937 total_uA_load, input_uV, output_uV);
1941 ret = rdev->desc->ops->set_mode(rdev, mode);
1943 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1944 __func__, mode, rdev_get_name(rdev));
1949 mutex_unlock(&rdev->mutex);
1952 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1955 * regulator_register_notifier - register regulator event notifier
1956 * @regulator: regulator source
1957 * @nb: notifier block
1959 * Register notifier block to receive regulator events.
1961 int regulator_register_notifier(struct regulator *regulator,
1962 struct notifier_block *nb)
1964 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1967 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1970 * regulator_unregister_notifier - unregister regulator event notifier
1971 * @regulator: regulator source
1972 * @nb: notifier block
1974 * Unregister regulator event notifier block.
1976 int regulator_unregister_notifier(struct regulator *regulator,
1977 struct notifier_block *nb)
1979 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1982 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1984 /* notify regulator consumers and downstream regulator consumers.
1985 * Note mutex must be held by caller.
1987 static void _notifier_call_chain(struct regulator_dev *rdev,
1988 unsigned long event, void *data)
1990 struct regulator_dev *_rdev;
1992 /* call rdev chain first */
1993 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1995 /* now notify regulator we supply */
1996 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1997 mutex_lock(&_rdev->mutex);
1998 _notifier_call_chain(_rdev, event, data);
1999 mutex_unlock(&_rdev->mutex);
2004 * regulator_bulk_get - get multiple regulator consumers
2006 * @dev: Device to supply
2007 * @num_consumers: Number of consumers to register
2008 * @consumers: Configuration of consumers; clients are stored here.
2010 * @return 0 on success, an errno on failure.
2012 * This helper function allows drivers to get several regulator
2013 * consumers in one operation. If any of the regulators cannot be
2014 * acquired then any regulators that were allocated will be freed
2015 * before returning to the caller.
2017 int regulator_bulk_get(struct device *dev, int num_consumers,
2018 struct regulator_bulk_data *consumers)
2023 for (i = 0; i < num_consumers; i++)
2024 consumers[i].consumer = NULL;
2026 for (i = 0; i < num_consumers; i++) {
2027 consumers[i].consumer = regulator_get(dev,
2028 consumers[i].supply);
2029 if (IS_ERR(consumers[i].consumer)) {
2030 ret = PTR_ERR(consumers[i].consumer);
2031 dev_err(dev, "Failed to get supply '%s': %d\n",
2032 consumers[i].supply, ret);
2033 consumers[i].consumer = NULL;
2041 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2042 regulator_put(consumers[i].consumer);
2046 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2049 * regulator_bulk_enable - enable multiple regulator consumers
2051 * @num_consumers: Number of consumers
2052 * @consumers: Consumer data; clients are stored here.
2053 * @return 0 on success, an errno on failure
2055 * This convenience API allows consumers to enable multiple regulator
2056 * clients in a single API call. If any consumers cannot be enabled
2057 * then any others that were enabled will be disabled again prior to
2060 int regulator_bulk_enable(int num_consumers,
2061 struct regulator_bulk_data *consumers)
2066 for (i = 0; i < num_consumers; i++) {
2067 ret = regulator_enable(consumers[i].consumer);
2075 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
2076 for (--i; i >= 0; --i)
2077 regulator_disable(consumers[i].consumer);
2081 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2084 * regulator_bulk_disable - disable multiple regulator consumers
2086 * @num_consumers: Number of consumers
2087 * @consumers: Consumer data; clients are stored here.
2088 * @return 0 on success, an errno on failure
2090 * This convenience API allows consumers to disable multiple regulator
2091 * clients in a single API call. If any consumers cannot be enabled
2092 * then any others that were disabled will be disabled again prior to
2095 int regulator_bulk_disable(int num_consumers,
2096 struct regulator_bulk_data *consumers)
2101 for (i = 0; i < num_consumers; i++) {
2102 ret = regulator_disable(consumers[i].consumer);
2110 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2112 for (--i; i >= 0; --i)
2113 regulator_enable(consumers[i].consumer);
2117 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2120 * regulator_bulk_free - free multiple regulator consumers
2122 * @num_consumers: Number of consumers
2123 * @consumers: Consumer data; clients are stored here.
2125 * This convenience API allows consumers to free multiple regulator
2126 * clients in a single API call.
2128 void regulator_bulk_free(int num_consumers,
2129 struct regulator_bulk_data *consumers)
2133 for (i = 0; i < num_consumers; i++) {
2134 regulator_put(consumers[i].consumer);
2135 consumers[i].consumer = NULL;
2138 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2141 * regulator_notifier_call_chain - call regulator event notifier
2142 * @rdev: regulator source
2143 * @event: notifier block
2144 * @data: callback-specific data.
2146 * Called by regulator drivers to notify clients a regulator event has
2147 * occurred. We also notify regulator clients downstream.
2148 * Note lock must be held by caller.
2150 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2151 unsigned long event, void *data)
2153 _notifier_call_chain(rdev, event, data);
2157 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2160 * regulator_mode_to_status - convert a regulator mode into a status
2162 * @mode: Mode to convert
2164 * Convert a regulator mode into a status.
2166 int regulator_mode_to_status(unsigned int mode)
2169 case REGULATOR_MODE_FAST:
2170 return REGULATOR_STATUS_FAST;
2171 case REGULATOR_MODE_NORMAL:
2172 return REGULATOR_STATUS_NORMAL;
2173 case REGULATOR_MODE_IDLE:
2174 return REGULATOR_STATUS_IDLE;
2175 case REGULATOR_STATUS_STANDBY:
2176 return REGULATOR_STATUS_STANDBY;
2181 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2184 * To avoid cluttering sysfs (and memory) with useless state, only
2185 * create attributes that can be meaningfully displayed.
2187 static int add_regulator_attributes(struct regulator_dev *rdev)
2189 struct device *dev = &rdev->dev;
2190 struct regulator_ops *ops = rdev->desc->ops;
2193 /* some attributes need specific methods to be displayed */
2194 if (ops->get_voltage) {
2195 status = device_create_file(dev, &dev_attr_microvolts);
2199 if (ops->get_current_limit) {
2200 status = device_create_file(dev, &dev_attr_microamps);
2204 if (ops->get_mode) {
2205 status = device_create_file(dev, &dev_attr_opmode);
2209 if (ops->is_enabled) {
2210 status = device_create_file(dev, &dev_attr_state);
2214 if (ops->get_status) {
2215 status = device_create_file(dev, &dev_attr_status);
2220 /* some attributes are type-specific */
2221 if (rdev->desc->type == REGULATOR_CURRENT) {
2222 status = device_create_file(dev, &dev_attr_requested_microamps);
2227 /* all the other attributes exist to support constraints;
2228 * don't show them if there are no constraints, or if the
2229 * relevant supporting methods are missing.
2231 if (!rdev->constraints)
2234 /* constraints need specific supporting methods */
2235 if (ops->set_voltage) {
2236 status = device_create_file(dev, &dev_attr_min_microvolts);
2239 status = device_create_file(dev, &dev_attr_max_microvolts);
2243 if (ops->set_current_limit) {
2244 status = device_create_file(dev, &dev_attr_min_microamps);
2247 status = device_create_file(dev, &dev_attr_max_microamps);
2252 /* suspend mode constraints need multiple supporting methods */
2253 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2256 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2259 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2262 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2266 if (ops->set_suspend_voltage) {
2267 status = device_create_file(dev,
2268 &dev_attr_suspend_standby_microvolts);
2271 status = device_create_file(dev,
2272 &dev_attr_suspend_mem_microvolts);
2275 status = device_create_file(dev,
2276 &dev_attr_suspend_disk_microvolts);
2281 if (ops->set_suspend_mode) {
2282 status = device_create_file(dev,
2283 &dev_attr_suspend_standby_mode);
2286 status = device_create_file(dev,
2287 &dev_attr_suspend_mem_mode);
2290 status = device_create_file(dev,
2291 &dev_attr_suspend_disk_mode);
2300 * regulator_register - register regulator
2301 * @regulator_desc: regulator to register
2302 * @dev: struct device for the regulator
2303 * @init_data: platform provided init data, passed through by driver
2304 * @driver_data: private regulator data
2306 * Called by regulator drivers to register a regulator.
2307 * Returns 0 on success.
2309 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2310 struct device *dev, struct regulator_init_data *init_data,
2313 static atomic_t regulator_no = ATOMIC_INIT(0);
2314 struct regulator_dev *rdev;
2317 if (regulator_desc == NULL)
2318 return ERR_PTR(-EINVAL);
2320 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2321 return ERR_PTR(-EINVAL);
2323 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2324 regulator_desc->type != REGULATOR_CURRENT)
2325 return ERR_PTR(-EINVAL);
2328 return ERR_PTR(-EINVAL);
2330 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2332 return ERR_PTR(-ENOMEM);
2334 mutex_lock(®ulator_list_mutex);
2336 mutex_init(&rdev->mutex);
2337 rdev->reg_data = driver_data;
2338 rdev->owner = regulator_desc->owner;
2339 rdev->desc = regulator_desc;
2340 INIT_LIST_HEAD(&rdev->consumer_list);
2341 INIT_LIST_HEAD(&rdev->supply_list);
2342 INIT_LIST_HEAD(&rdev->list);
2343 INIT_LIST_HEAD(&rdev->slist);
2344 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2346 /* preform any regulator specific init */
2347 if (init_data->regulator_init) {
2348 ret = init_data->regulator_init(rdev->reg_data);
2353 /* register with sysfs */
2354 rdev->dev.class = ®ulator_class;
2355 rdev->dev.parent = dev;
2356 dev_set_name(&rdev->dev, "regulator.%d",
2357 atomic_inc_return(®ulator_no) - 1);
2358 ret = device_register(&rdev->dev);
2360 put_device(&rdev->dev);
2364 dev_set_drvdata(&rdev->dev, rdev);
2366 /* set regulator constraints */
2367 ret = set_machine_constraints(rdev, &init_data->constraints);
2371 /* add attributes supported by this regulator */
2372 ret = add_regulator_attributes(rdev);
2376 /* set supply regulator if it exists */
2377 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2379 "Supply regulator specified by both name and dev\n");
2384 if (init_data->supply_regulator) {
2385 struct regulator_dev *r;
2388 list_for_each_entry(r, ®ulator_list, list) {
2389 if (strcmp(rdev_get_name(r),
2390 init_data->supply_regulator) == 0) {
2397 dev_err(dev, "Failed to find supply %s\n",
2398 init_data->supply_regulator);
2403 ret = set_supply(rdev, r);
2408 if (init_data->supply_regulator_dev) {
2409 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
2410 ret = set_supply(rdev,
2411 dev_get_drvdata(init_data->supply_regulator_dev));
2416 /* add consumers devices */
2417 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2418 ret = set_consumer_device_supply(rdev,
2419 init_data->consumer_supplies[i].dev,
2420 init_data->consumer_supplies[i].dev_name,
2421 init_data->consumer_supplies[i].supply);
2423 goto unset_supplies;
2426 list_add(&rdev->list, ®ulator_list);
2428 mutex_unlock(®ulator_list_mutex);
2432 unset_regulator_supplies(rdev);
2435 device_unregister(&rdev->dev);
2436 /* device core frees rdev */
2437 rdev = ERR_PTR(ret);
2442 rdev = ERR_PTR(ret);
2445 EXPORT_SYMBOL_GPL(regulator_register);
2448 * regulator_unregister - unregister regulator
2449 * @rdev: regulator to unregister
2451 * Called by regulator drivers to unregister a regulator.
2453 void regulator_unregister(struct regulator_dev *rdev)
2458 mutex_lock(®ulator_list_mutex);
2459 WARN_ON(rdev->open_count);
2460 unset_regulator_supplies(rdev);
2461 list_del(&rdev->list);
2463 sysfs_remove_link(&rdev->dev.kobj, "supply");
2464 device_unregister(&rdev->dev);
2465 mutex_unlock(®ulator_list_mutex);
2467 EXPORT_SYMBOL_GPL(regulator_unregister);
2470 * regulator_suspend_prepare - prepare regulators for system wide suspend
2471 * @state: system suspend state
2473 * Configure each regulator with it's suspend operating parameters for state.
2474 * This will usually be called by machine suspend code prior to supending.
2476 int regulator_suspend_prepare(suspend_state_t state)
2478 struct regulator_dev *rdev;
2481 /* ON is handled by regulator active state */
2482 if (state == PM_SUSPEND_ON)
2485 mutex_lock(®ulator_list_mutex);
2486 list_for_each_entry(rdev, ®ulator_list, list) {
2488 mutex_lock(&rdev->mutex);
2489 ret = suspend_prepare(rdev, state);
2490 mutex_unlock(&rdev->mutex);
2493 printk(KERN_ERR "%s: failed to prepare %s\n",
2494 __func__, rdev_get_name(rdev));
2499 mutex_unlock(®ulator_list_mutex);
2502 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2505 * regulator_has_full_constraints - the system has fully specified constraints
2507 * Calling this function will cause the regulator API to disable all
2508 * regulators which have a zero use count and don't have an always_on
2509 * constraint in a late_initcall.
2511 * The intention is that this will become the default behaviour in a
2512 * future kernel release so users are encouraged to use this facility
2515 void regulator_has_full_constraints(void)
2517 has_full_constraints = 1;
2519 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2522 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2524 * Calling this function will cause the regulator API to provide a
2525 * dummy regulator to consumers if no physical regulator is found,
2526 * allowing most consumers to proceed as though a regulator were
2527 * configured. This allows systems such as those with software
2528 * controllable regulators for the CPU core only to be brought up more
2531 void regulator_use_dummy_regulator(void)
2533 board_wants_dummy_regulator = true;
2535 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2538 * rdev_get_drvdata - get rdev regulator driver data
2541 * Get rdev regulator driver private data. This call can be used in the
2542 * regulator driver context.
2544 void *rdev_get_drvdata(struct regulator_dev *rdev)
2546 return rdev->reg_data;
2548 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2551 * regulator_get_drvdata - get regulator driver data
2552 * @regulator: regulator
2554 * Get regulator driver private data. This call can be used in the consumer
2555 * driver context when non API regulator specific functions need to be called.
2557 void *regulator_get_drvdata(struct regulator *regulator)
2559 return regulator->rdev->reg_data;
2561 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2564 * regulator_set_drvdata - set regulator driver data
2565 * @regulator: regulator
2568 void regulator_set_drvdata(struct regulator *regulator, void *data)
2570 regulator->rdev->reg_data = data;
2572 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2575 * regulator_get_id - get regulator ID
2578 int rdev_get_id(struct regulator_dev *rdev)
2580 return rdev->desc->id;
2582 EXPORT_SYMBOL_GPL(rdev_get_id);
2584 struct device *rdev_get_dev(struct regulator_dev *rdev)
2588 EXPORT_SYMBOL_GPL(rdev_get_dev);
2590 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2592 return reg_init_data->driver_data;
2594 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2596 static int __init regulator_init(void)
2600 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2602 ret = class_register(®ulator_class);
2604 regulator_dummy_init();
2609 /* init early to allow our consumers to complete system booting */
2610 core_initcall(regulator_init);
2612 static int __init regulator_init_complete(void)
2614 struct regulator_dev *rdev;
2615 struct regulator_ops *ops;
2616 struct regulation_constraints *c;
2620 mutex_lock(®ulator_list_mutex);
2622 /* If we have a full configuration then disable any regulators
2623 * which are not in use or always_on. This will become the
2624 * default behaviour in the future.
2626 list_for_each_entry(rdev, ®ulator_list, list) {
2627 ops = rdev->desc->ops;
2628 c = rdev->constraints;
2630 name = rdev_get_name(rdev);
2632 if (!ops->disable || (c && c->always_on))
2635 mutex_lock(&rdev->mutex);
2637 if (rdev->use_count)
2640 /* If we can't read the status assume it's on. */
2641 if (ops->is_enabled)
2642 enabled = ops->is_enabled(rdev);
2649 if (has_full_constraints) {
2650 /* We log since this may kill the system if it
2652 printk(KERN_INFO "%s: disabling %s\n",
2654 ret = ops->disable(rdev);
2657 "%s: couldn't disable %s: %d\n",
2658 __func__, name, ret);
2661 /* The intention is that in future we will
2662 * assume that full constraints are provided
2663 * so warn even if we aren't going to do
2667 "%s: incomplete constraints, leaving %s on\n",
2672 mutex_unlock(&rdev->mutex);
2675 mutex_unlock(®ulator_list_mutex);
2679 late_initcall(regulator_init_complete);