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>
30 #define REGULATOR_VERSION "0.5"
32 static DEFINE_MUTEX(regulator_list_mutex);
33 static LIST_HEAD(regulator_list);
34 static LIST_HEAD(regulator_map_list);
35 static int has_full_constraints;
36 static bool board_wants_dummy_regulator;
39 * struct regulator_map
41 * Used to provide symbolic supply names to devices.
43 struct regulator_map {
44 struct list_head list;
45 const char *dev_name; /* The dev_name() for the consumer */
47 struct regulator_dev *regulator;
53 * One for each consumer device.
57 struct list_head list;
62 struct device_attribute dev_attr;
63 struct regulator_dev *rdev;
66 static int _regulator_is_enabled(struct regulator_dev *rdev);
67 static int _regulator_disable(struct regulator_dev *rdev);
68 static int _regulator_get_voltage(struct regulator_dev *rdev);
69 static int _regulator_get_current_limit(struct regulator_dev *rdev);
70 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
71 static void _notifier_call_chain(struct regulator_dev *rdev,
72 unsigned long event, void *data);
74 static const char *rdev_get_name(struct regulator_dev *rdev)
76 if (rdev->constraints && rdev->constraints->name)
77 return rdev->constraints->name;
78 else if (rdev->desc->name)
79 return rdev->desc->name;
84 /* gets the regulator for a given consumer device */
85 static struct regulator *get_device_regulator(struct device *dev)
87 struct regulator *regulator = NULL;
88 struct regulator_dev *rdev;
90 mutex_lock(®ulator_list_mutex);
91 list_for_each_entry(rdev, ®ulator_list, list) {
92 mutex_lock(&rdev->mutex);
93 list_for_each_entry(regulator, &rdev->consumer_list, list) {
94 if (regulator->dev == dev) {
95 mutex_unlock(&rdev->mutex);
96 mutex_unlock(®ulator_list_mutex);
100 mutex_unlock(&rdev->mutex);
102 mutex_unlock(®ulator_list_mutex);
106 /* Platform voltage constraint check */
107 static int regulator_check_voltage(struct regulator_dev *rdev,
108 int *min_uV, int *max_uV)
110 BUG_ON(*min_uV > *max_uV);
112 if (!rdev->constraints) {
113 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
114 rdev_get_name(rdev));
117 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
118 printk(KERN_ERR "%s: operation not allowed for %s\n",
119 __func__, rdev_get_name(rdev));
123 if (*max_uV > rdev->constraints->max_uV)
124 *max_uV = rdev->constraints->max_uV;
125 if (*min_uV < rdev->constraints->min_uV)
126 *min_uV = rdev->constraints->min_uV;
128 if (*min_uV > *max_uV)
134 /* current constraint check */
135 static int regulator_check_current_limit(struct regulator_dev *rdev,
136 int *min_uA, int *max_uA)
138 BUG_ON(*min_uA > *max_uA);
140 if (!rdev->constraints) {
141 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
142 rdev_get_name(rdev));
145 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
146 printk(KERN_ERR "%s: operation not allowed for %s\n",
147 __func__, rdev_get_name(rdev));
151 if (*max_uA > rdev->constraints->max_uA)
152 *max_uA = rdev->constraints->max_uA;
153 if (*min_uA < rdev->constraints->min_uA)
154 *min_uA = rdev->constraints->min_uA;
156 if (*min_uA > *max_uA)
162 /* operating mode constraint check */
163 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
166 case REGULATOR_MODE_FAST:
167 case REGULATOR_MODE_NORMAL:
168 case REGULATOR_MODE_IDLE:
169 case REGULATOR_MODE_STANDBY:
175 if (!rdev->constraints) {
176 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
177 rdev_get_name(rdev));
180 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
181 printk(KERN_ERR "%s: operation not allowed for %s\n",
182 __func__, rdev_get_name(rdev));
185 if (!(rdev->constraints->valid_modes_mask & mode)) {
186 printk(KERN_ERR "%s: invalid mode %x for %s\n",
187 __func__, mode, rdev_get_name(rdev));
193 /* dynamic regulator mode switching constraint check */
194 static int regulator_check_drms(struct regulator_dev *rdev)
196 if (!rdev->constraints) {
197 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
198 rdev_get_name(rdev));
201 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
202 printk(KERN_ERR "%s: operation not allowed for %s\n",
203 __func__, rdev_get_name(rdev));
209 static ssize_t device_requested_uA_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 struct regulator *regulator;
214 regulator = get_device_regulator(dev);
215 if (regulator == NULL)
218 return sprintf(buf, "%d\n", regulator->uA_load);
221 static ssize_t regulator_uV_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
224 struct regulator_dev *rdev = dev_get_drvdata(dev);
227 mutex_lock(&rdev->mutex);
228 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
229 mutex_unlock(&rdev->mutex);
233 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
235 static ssize_t regulator_uA_show(struct device *dev,
236 struct device_attribute *attr, char *buf)
238 struct regulator_dev *rdev = dev_get_drvdata(dev);
240 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
242 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
244 static ssize_t regulator_name_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 struct regulator_dev *rdev = dev_get_drvdata(dev);
249 return sprintf(buf, "%s\n", rdev_get_name(rdev));
252 static ssize_t regulator_print_opmode(char *buf, int mode)
255 case REGULATOR_MODE_FAST:
256 return sprintf(buf, "fast\n");
257 case REGULATOR_MODE_NORMAL:
258 return sprintf(buf, "normal\n");
259 case REGULATOR_MODE_IDLE:
260 return sprintf(buf, "idle\n");
261 case REGULATOR_MODE_STANDBY:
262 return sprintf(buf, "standby\n");
264 return sprintf(buf, "unknown\n");
267 static ssize_t regulator_opmode_show(struct device *dev,
268 struct device_attribute *attr, char *buf)
270 struct regulator_dev *rdev = dev_get_drvdata(dev);
272 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
274 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
276 static ssize_t regulator_print_state(char *buf, int state)
279 return sprintf(buf, "enabled\n");
281 return sprintf(buf, "disabled\n");
283 return sprintf(buf, "unknown\n");
286 static ssize_t regulator_state_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
289 struct regulator_dev *rdev = dev_get_drvdata(dev);
292 mutex_lock(&rdev->mutex);
293 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
294 mutex_unlock(&rdev->mutex);
298 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
300 static ssize_t regulator_status_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
303 struct regulator_dev *rdev = dev_get_drvdata(dev);
307 status = rdev->desc->ops->get_status(rdev);
312 case REGULATOR_STATUS_OFF:
315 case REGULATOR_STATUS_ON:
318 case REGULATOR_STATUS_ERROR:
321 case REGULATOR_STATUS_FAST:
324 case REGULATOR_STATUS_NORMAL:
327 case REGULATOR_STATUS_IDLE:
330 case REGULATOR_STATUS_STANDBY:
337 return sprintf(buf, "%s\n", label);
339 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
341 static ssize_t regulator_min_uA_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
344 struct regulator_dev *rdev = dev_get_drvdata(dev);
346 if (!rdev->constraints)
347 return sprintf(buf, "constraint not defined\n");
349 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
351 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
353 static ssize_t regulator_max_uA_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
356 struct regulator_dev *rdev = dev_get_drvdata(dev);
358 if (!rdev->constraints)
359 return sprintf(buf, "constraint not defined\n");
361 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
363 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
365 static ssize_t regulator_min_uV_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
368 struct regulator_dev *rdev = dev_get_drvdata(dev);
370 if (!rdev->constraints)
371 return sprintf(buf, "constraint not defined\n");
373 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
375 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
377 static ssize_t regulator_max_uV_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
380 struct regulator_dev *rdev = dev_get_drvdata(dev);
382 if (!rdev->constraints)
383 return sprintf(buf, "constraint not defined\n");
385 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
387 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
389 static ssize_t regulator_total_uA_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
392 struct regulator_dev *rdev = dev_get_drvdata(dev);
393 struct regulator *regulator;
396 mutex_lock(&rdev->mutex);
397 list_for_each_entry(regulator, &rdev->consumer_list, list)
398 uA += regulator->uA_load;
399 mutex_unlock(&rdev->mutex);
400 return sprintf(buf, "%d\n", uA);
402 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
404 static ssize_t regulator_num_users_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 struct regulator_dev *rdev = dev_get_drvdata(dev);
408 return sprintf(buf, "%d\n", rdev->use_count);
411 static ssize_t regulator_type_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
414 struct regulator_dev *rdev = dev_get_drvdata(dev);
416 switch (rdev->desc->type) {
417 case REGULATOR_VOLTAGE:
418 return sprintf(buf, "voltage\n");
419 case REGULATOR_CURRENT:
420 return sprintf(buf, "current\n");
422 return sprintf(buf, "unknown\n");
425 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
428 struct regulator_dev *rdev = dev_get_drvdata(dev);
430 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
432 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
433 regulator_suspend_mem_uV_show, NULL);
435 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
436 struct device_attribute *attr, char *buf)
438 struct regulator_dev *rdev = dev_get_drvdata(dev);
440 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
442 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
443 regulator_suspend_disk_uV_show, NULL);
445 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
446 struct device_attribute *attr, char *buf)
448 struct regulator_dev *rdev = dev_get_drvdata(dev);
450 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
452 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
453 regulator_suspend_standby_uV_show, NULL);
455 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
458 struct regulator_dev *rdev = dev_get_drvdata(dev);
460 return regulator_print_opmode(buf,
461 rdev->constraints->state_mem.mode);
463 static DEVICE_ATTR(suspend_mem_mode, 0444,
464 regulator_suspend_mem_mode_show, NULL);
466 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
467 struct device_attribute *attr, char *buf)
469 struct regulator_dev *rdev = dev_get_drvdata(dev);
471 return regulator_print_opmode(buf,
472 rdev->constraints->state_disk.mode);
474 static DEVICE_ATTR(suspend_disk_mode, 0444,
475 regulator_suspend_disk_mode_show, NULL);
477 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
480 struct regulator_dev *rdev = dev_get_drvdata(dev);
482 return regulator_print_opmode(buf,
483 rdev->constraints->state_standby.mode);
485 static DEVICE_ATTR(suspend_standby_mode, 0444,
486 regulator_suspend_standby_mode_show, NULL);
488 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
491 struct regulator_dev *rdev = dev_get_drvdata(dev);
493 return regulator_print_state(buf,
494 rdev->constraints->state_mem.enabled);
496 static DEVICE_ATTR(suspend_mem_state, 0444,
497 regulator_suspend_mem_state_show, NULL);
499 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
502 struct regulator_dev *rdev = dev_get_drvdata(dev);
504 return regulator_print_state(buf,
505 rdev->constraints->state_disk.enabled);
507 static DEVICE_ATTR(suspend_disk_state, 0444,
508 regulator_suspend_disk_state_show, NULL);
510 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
513 struct regulator_dev *rdev = dev_get_drvdata(dev);
515 return regulator_print_state(buf,
516 rdev->constraints->state_standby.enabled);
518 static DEVICE_ATTR(suspend_standby_state, 0444,
519 regulator_suspend_standby_state_show, NULL);
523 * These are the only attributes are present for all regulators.
524 * Other attributes are a function of regulator functionality.
526 static struct device_attribute regulator_dev_attrs[] = {
527 __ATTR(name, 0444, regulator_name_show, NULL),
528 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
529 __ATTR(type, 0444, regulator_type_show, NULL),
533 static void regulator_dev_release(struct device *dev)
535 struct regulator_dev *rdev = dev_get_drvdata(dev);
539 static struct class regulator_class = {
541 .dev_release = regulator_dev_release,
542 .dev_attrs = regulator_dev_attrs,
545 /* Calculate the new optimum regulator operating mode based on the new total
546 * consumer load. All locks held by caller */
547 static void drms_uA_update(struct regulator_dev *rdev)
549 struct regulator *sibling;
550 int current_uA = 0, output_uV, input_uV, err;
553 err = regulator_check_drms(rdev);
554 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
555 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
558 /* get output voltage */
559 output_uV = rdev->desc->ops->get_voltage(rdev);
563 /* get input voltage */
564 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
565 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
567 input_uV = rdev->constraints->input_uV;
571 /* calc total requested load */
572 list_for_each_entry(sibling, &rdev->consumer_list, list)
573 current_uA += sibling->uA_load;
575 /* now get the optimum mode for our new total regulator load */
576 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
577 output_uV, current_uA);
579 /* check the new mode is allowed */
580 err = regulator_check_mode(rdev, mode);
582 rdev->desc->ops->set_mode(rdev, mode);
585 static int suspend_set_state(struct regulator_dev *rdev,
586 struct regulator_state *rstate)
591 can_set_state = rdev->desc->ops->set_suspend_enable &&
592 rdev->desc->ops->set_suspend_disable;
594 /* If we have no suspend mode configration don't set anything;
595 * only warn if the driver actually makes the suspend mode
598 if (!rstate->enabled && !rstate->disabled) {
600 printk(KERN_WARNING "%s: No configuration for %s\n",
601 __func__, rdev_get_name(rdev));
605 if (rstate->enabled && rstate->disabled) {
606 printk(KERN_ERR "%s: invalid configuration for %s\n",
607 __func__, rdev_get_name(rdev));
611 if (!can_set_state) {
612 printk(KERN_ERR "%s: no way to set suspend state\n",
618 ret = rdev->desc->ops->set_suspend_enable(rdev);
620 ret = rdev->desc->ops->set_suspend_disable(rdev);
622 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
626 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
627 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
629 printk(KERN_ERR "%s: failed to set voltage\n",
635 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
636 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
638 printk(KERN_ERR "%s: failed to set mode\n", __func__);
645 /* locks held by caller */
646 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
648 if (!rdev->constraints)
652 case PM_SUSPEND_STANDBY:
653 return suspend_set_state(rdev,
654 &rdev->constraints->state_standby);
656 return suspend_set_state(rdev,
657 &rdev->constraints->state_mem);
659 return suspend_set_state(rdev,
660 &rdev->constraints->state_disk);
666 static void print_constraints(struct regulator_dev *rdev)
668 struct regulation_constraints *constraints = rdev->constraints;
673 if (constraints->min_uV && constraints->max_uV) {
674 if (constraints->min_uV == constraints->max_uV)
675 count += sprintf(buf + count, "%d mV ",
676 constraints->min_uV / 1000);
678 count += sprintf(buf + count, "%d <--> %d mV ",
679 constraints->min_uV / 1000,
680 constraints->max_uV / 1000);
683 if (!constraints->min_uV ||
684 constraints->min_uV != constraints->max_uV) {
685 ret = _regulator_get_voltage(rdev);
687 count += sprintf(buf + count, "at %d mV ", ret / 1000);
690 if (constraints->min_uA && constraints->max_uA) {
691 if (constraints->min_uA == constraints->max_uA)
692 count += sprintf(buf + count, "%d mA ",
693 constraints->min_uA / 1000);
695 count += sprintf(buf + count, "%d <--> %d mA ",
696 constraints->min_uA / 1000,
697 constraints->max_uA / 1000);
700 if (!constraints->min_uA ||
701 constraints->min_uA != constraints->max_uA) {
702 ret = _regulator_get_current_limit(rdev);
704 count += sprintf(buf + count, "at %d mA ", ret / 1000);
707 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
708 count += sprintf(buf + count, "fast ");
709 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
710 count += sprintf(buf + count, "normal ");
711 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
712 count += sprintf(buf + count, "idle ");
713 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
714 count += sprintf(buf + count, "standby");
716 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
719 static int machine_constraints_voltage(struct regulator_dev *rdev,
720 struct regulation_constraints *constraints)
722 struct regulator_ops *ops = rdev->desc->ops;
723 const char *name = rdev_get_name(rdev);
726 /* do we need to apply the constraint voltage */
727 if (rdev->constraints->apply_uV &&
728 rdev->constraints->min_uV == rdev->constraints->max_uV &&
730 ret = ops->set_voltage(rdev,
731 rdev->constraints->min_uV, rdev->constraints->max_uV);
733 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
735 rdev->constraints->min_uV, name);
736 rdev->constraints = NULL;
741 /* constrain machine-level voltage specs to fit
742 * the actual range supported by this regulator.
744 if (ops->list_voltage && rdev->desc->n_voltages) {
745 int count = rdev->desc->n_voltages;
747 int min_uV = INT_MAX;
748 int max_uV = INT_MIN;
749 int cmin = constraints->min_uV;
750 int cmax = constraints->max_uV;
752 /* it's safe to autoconfigure fixed-voltage supplies
753 and the constraints are used by list_voltage. */
754 if (count == 1 && !cmin) {
757 constraints->min_uV = cmin;
758 constraints->max_uV = cmax;
761 /* voltage constraints are optional */
762 if ((cmin == 0) && (cmax == 0))
765 /* else require explicit machine-level constraints */
766 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
767 pr_err("%s: %s '%s' voltage constraints\n",
768 __func__, "invalid", name);
772 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
773 for (i = 0; i < count; i++) {
776 value = ops->list_voltage(rdev, i);
780 /* maybe adjust [min_uV..max_uV] */
781 if (value >= cmin && value < min_uV)
783 if (value <= cmax && value > max_uV)
787 /* final: [min_uV..max_uV] valid iff constraints valid */
788 if (max_uV < min_uV) {
789 pr_err("%s: %s '%s' voltage constraints\n",
790 __func__, "unsupportable", name);
794 /* use regulator's subset of machine constraints */
795 if (constraints->min_uV < min_uV) {
796 pr_debug("%s: override '%s' %s, %d -> %d\n",
797 __func__, name, "min_uV",
798 constraints->min_uV, min_uV);
799 constraints->min_uV = min_uV;
801 if (constraints->max_uV > max_uV) {
802 pr_debug("%s: override '%s' %s, %d -> %d\n",
803 __func__, name, "max_uV",
804 constraints->max_uV, max_uV);
805 constraints->max_uV = max_uV;
813 * set_machine_constraints - sets regulator constraints
814 * @rdev: regulator source
815 * @constraints: constraints to apply
817 * Allows platform initialisation code to define and constrain
818 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
819 * Constraints *must* be set by platform code in order for some
820 * regulator operations to proceed i.e. set_voltage, set_current_limit,
823 static int set_machine_constraints(struct regulator_dev *rdev,
824 struct regulation_constraints *constraints)
828 struct regulator_ops *ops = rdev->desc->ops;
830 rdev->constraints = constraints;
832 name = rdev_get_name(rdev);
834 ret = machine_constraints_voltage(rdev, constraints);
838 /* do we need to setup our suspend state */
839 if (constraints->initial_state) {
840 ret = suspend_prepare(rdev, constraints->initial_state);
842 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
844 rdev->constraints = NULL;
849 if (constraints->initial_mode) {
850 if (!ops->set_mode) {
851 printk(KERN_ERR "%s: no set_mode operation for %s\n",
857 ret = ops->set_mode(rdev, constraints->initial_mode);
860 "%s: failed to set initial mode for %s: %d\n",
861 __func__, name, ret);
866 /* If the constraints say the regulator should be on at this point
867 * and we have control then make sure it is enabled.
869 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
870 ret = ops->enable(rdev);
872 printk(KERN_ERR "%s: failed to enable %s\n",
874 rdev->constraints = NULL;
879 print_constraints(rdev);
885 * set_supply - set regulator supply regulator
886 * @rdev: regulator name
887 * @supply_rdev: supply regulator name
889 * Called by platform initialisation code to set the supply regulator for this
890 * regulator. This ensures that a regulators supply will also be enabled by the
891 * core if it's child is enabled.
893 static int set_supply(struct regulator_dev *rdev,
894 struct regulator_dev *supply_rdev)
898 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
902 "%s: could not add device link %s err %d\n",
903 __func__, supply_rdev->dev.kobj.name, err);
906 rdev->supply = supply_rdev;
907 list_add(&rdev->slist, &supply_rdev->supply_list);
913 * set_consumer_device_supply: Bind a regulator to a symbolic supply
914 * @rdev: regulator source
915 * @consumer_dev: device the supply applies to
916 * @consumer_dev_name: dev_name() string for device supply applies to
917 * @supply: symbolic name for supply
919 * Allows platform initialisation code to map physical regulator
920 * sources to symbolic names for supplies for use by devices. Devices
921 * should use these symbolic names to request regulators, avoiding the
922 * need to provide board-specific regulator names as platform data.
924 * Only one of consumer_dev and consumer_dev_name may be specified.
926 static int set_consumer_device_supply(struct regulator_dev *rdev,
927 struct device *consumer_dev, const char *consumer_dev_name,
930 struct regulator_map *node;
933 if (consumer_dev && consumer_dev_name)
936 if (!consumer_dev_name && consumer_dev)
937 consumer_dev_name = dev_name(consumer_dev);
942 if (consumer_dev_name != NULL)
947 list_for_each_entry(node, ®ulator_map_list, list) {
948 if (node->dev_name && consumer_dev_name) {
949 if (strcmp(node->dev_name, consumer_dev_name) != 0)
951 } else if (node->dev_name || consumer_dev_name) {
955 if (strcmp(node->supply, supply) != 0)
958 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
959 dev_name(&node->regulator->dev),
960 node->regulator->desc->name,
962 dev_name(&rdev->dev), rdev_get_name(rdev));
966 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
970 node->regulator = rdev;
971 node->supply = supply;
974 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
975 if (node->dev_name == NULL) {
981 list_add(&node->list, ®ulator_map_list);
985 static void unset_regulator_supplies(struct regulator_dev *rdev)
987 struct regulator_map *node, *n;
989 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
990 if (rdev == node->regulator) {
991 list_del(&node->list);
992 kfree(node->dev_name);
998 #define REG_STR_SIZE 32
1000 static struct regulator *create_regulator(struct regulator_dev *rdev,
1002 const char *supply_name)
1004 struct regulator *regulator;
1005 char buf[REG_STR_SIZE];
1008 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1009 if (regulator == NULL)
1012 mutex_lock(&rdev->mutex);
1013 regulator->rdev = rdev;
1014 list_add(®ulator->list, &rdev->consumer_list);
1017 /* create a 'requested_microamps_name' sysfs entry */
1018 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1020 if (size >= REG_STR_SIZE)
1023 regulator->dev = dev;
1024 sysfs_attr_init(®ulator->dev_attr.attr);
1025 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1026 if (regulator->dev_attr.attr.name == NULL)
1029 regulator->dev_attr.attr.mode = 0444;
1030 regulator->dev_attr.show = device_requested_uA_show;
1031 err = device_create_file(dev, ®ulator->dev_attr);
1033 printk(KERN_WARNING "%s: could not add regulator_dev"
1034 " load sysfs\n", __func__);
1038 /* also add a link to the device sysfs entry */
1039 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1040 dev->kobj.name, supply_name);
1041 if (size >= REG_STR_SIZE)
1044 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1045 if (regulator->supply_name == NULL)
1048 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1052 "%s: could not add device link %s err %d\n",
1053 __func__, dev->kobj.name, err);
1054 device_remove_file(dev, ®ulator->dev_attr);
1058 mutex_unlock(&rdev->mutex);
1061 kfree(regulator->supply_name);
1063 device_remove_file(regulator->dev, ®ulator->dev_attr);
1065 kfree(regulator->dev_attr.attr.name);
1067 list_del(®ulator->list);
1069 mutex_unlock(&rdev->mutex);
1073 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1075 if (!rdev->desc->ops->enable_time)
1077 return rdev->desc->ops->enable_time(rdev);
1080 /* Internal regulator request function */
1081 static struct regulator *_regulator_get(struct device *dev, const char *id,
1084 struct regulator_dev *rdev;
1085 struct regulator_map *map;
1086 struct regulator *regulator = ERR_PTR(-ENODEV);
1087 const char *devname = NULL;
1091 printk(KERN_ERR "regulator: get() with no identifier\n");
1096 devname = dev_name(dev);
1098 mutex_lock(®ulator_list_mutex);
1100 list_for_each_entry(map, ®ulator_map_list, list) {
1101 /* If the mapping has a device set up it must match */
1102 if (map->dev_name &&
1103 (!devname || strcmp(map->dev_name, devname)))
1106 if (strcmp(map->supply, id) == 0) {
1107 rdev = map->regulator;
1112 if (board_wants_dummy_regulator) {
1113 rdev = dummy_regulator_rdev;
1117 #ifdef CONFIG_REGULATOR_DUMMY
1119 devname = "deviceless";
1121 /* If the board didn't flag that it was fully constrained then
1122 * substitute in a dummy regulator so consumers can continue.
1124 if (!has_full_constraints) {
1125 pr_warning("%s supply %s not found, using dummy regulator\n",
1127 rdev = dummy_regulator_rdev;
1132 mutex_unlock(®ulator_list_mutex);
1136 if (rdev->exclusive) {
1137 regulator = ERR_PTR(-EPERM);
1141 if (exclusive && rdev->open_count) {
1142 regulator = ERR_PTR(-EBUSY);
1146 if (!try_module_get(rdev->owner))
1149 regulator = create_regulator(rdev, dev, id);
1150 if (regulator == NULL) {
1151 regulator = ERR_PTR(-ENOMEM);
1152 module_put(rdev->owner);
1157 rdev->exclusive = 1;
1159 ret = _regulator_is_enabled(rdev);
1161 rdev->use_count = 1;
1163 rdev->use_count = 0;
1167 mutex_unlock(®ulator_list_mutex);
1173 * regulator_get - lookup and obtain a reference to a regulator.
1174 * @dev: device for regulator "consumer"
1175 * @id: Supply name or regulator ID.
1177 * Returns a struct regulator corresponding to the regulator producer,
1178 * or IS_ERR() condition containing errno.
1180 * Use of supply names configured via regulator_set_device_supply() is
1181 * strongly encouraged. It is recommended that the supply name used
1182 * should match the name used for the supply and/or the relevant
1183 * device pins in the datasheet.
1185 struct regulator *regulator_get(struct device *dev, const char *id)
1187 return _regulator_get(dev, id, 0);
1189 EXPORT_SYMBOL_GPL(regulator_get);
1192 * regulator_get_exclusive - obtain exclusive access to a regulator.
1193 * @dev: device for regulator "consumer"
1194 * @id: Supply name or regulator ID.
1196 * Returns a struct regulator corresponding to the regulator producer,
1197 * or IS_ERR() condition containing errno. Other consumers will be
1198 * unable to obtain this reference is held and the use count for the
1199 * regulator will be initialised to reflect the current state of the
1202 * This is intended for use by consumers which cannot tolerate shared
1203 * use of the regulator such as those which need to force the
1204 * regulator off for correct operation of the hardware they are
1207 * Use of supply names configured via regulator_set_device_supply() is
1208 * strongly encouraged. It is recommended that the supply name used
1209 * should match the name used for the supply and/or the relevant
1210 * device pins in the datasheet.
1212 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1214 return _regulator_get(dev, id, 1);
1216 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1219 * regulator_put - "free" the regulator source
1220 * @regulator: regulator source
1222 * Note: drivers must ensure that all regulator_enable calls made on this
1223 * regulator source are balanced by regulator_disable calls prior to calling
1226 void regulator_put(struct regulator *regulator)
1228 struct regulator_dev *rdev;
1230 if (regulator == NULL || IS_ERR(regulator))
1233 mutex_lock(®ulator_list_mutex);
1234 rdev = regulator->rdev;
1236 /* remove any sysfs entries */
1237 if (regulator->dev) {
1238 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1239 kfree(regulator->supply_name);
1240 device_remove_file(regulator->dev, ®ulator->dev_attr);
1241 kfree(regulator->dev_attr.attr.name);
1243 list_del(®ulator->list);
1247 rdev->exclusive = 0;
1249 module_put(rdev->owner);
1250 mutex_unlock(®ulator_list_mutex);
1252 EXPORT_SYMBOL_GPL(regulator_put);
1254 static int _regulator_can_change_status(struct regulator_dev *rdev)
1256 if (!rdev->constraints)
1259 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1265 /* locks held by regulator_enable() */
1266 static int _regulator_enable(struct regulator_dev *rdev)
1270 /* do we need to enable the supply regulator first */
1272 ret = _regulator_enable(rdev->supply);
1274 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1275 __func__, rdev_get_name(rdev), ret);
1280 /* check voltage and requested load before enabling */
1281 if (rdev->constraints &&
1282 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1283 drms_uA_update(rdev);
1285 if (rdev->use_count == 0) {
1286 /* The regulator may on if it's not switchable or left on */
1287 ret = _regulator_is_enabled(rdev);
1288 if (ret == -EINVAL || ret == 0) {
1289 if (!_regulator_can_change_status(rdev))
1292 if (!rdev->desc->ops->enable)
1295 /* Query before enabling in case configuration
1297 ret = _regulator_get_enable_time(rdev);
1302 "%s: enable_time() failed for %s: %d\n",
1303 __func__, rdev_get_name(rdev),
1308 /* Allow the regulator to ramp; it would be useful
1309 * to extend this for bulk operations so that the
1310 * regulators can ramp together. */
1311 ret = rdev->desc->ops->enable(rdev);
1316 mdelay(delay / 1000);
1320 } else if (ret < 0) {
1321 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
1322 __func__, rdev_get_name(rdev), ret);
1325 /* Fallthrough on positive return values - already enabled */
1334 * regulator_enable - enable regulator output
1335 * @regulator: regulator source
1337 * Request that the regulator be enabled with the regulator output at
1338 * the predefined voltage or current value. Calls to regulator_enable()
1339 * must be balanced with calls to regulator_disable().
1341 * NOTE: the output value can be set by other drivers, boot loader or may be
1342 * hardwired in the regulator.
1344 int regulator_enable(struct regulator *regulator)
1346 struct regulator_dev *rdev = regulator->rdev;
1349 mutex_lock(&rdev->mutex);
1350 ret = _regulator_enable(rdev);
1351 mutex_unlock(&rdev->mutex);
1354 EXPORT_SYMBOL_GPL(regulator_enable);
1356 /* locks held by regulator_disable() */
1357 static int _regulator_disable(struct regulator_dev *rdev)
1361 if (WARN(rdev->use_count <= 0,
1362 "unbalanced disables for %s\n",
1363 rdev_get_name(rdev)))
1366 /* are we the last user and permitted to disable ? */
1367 if (rdev->use_count == 1 &&
1368 (rdev->constraints && !rdev->constraints->always_on)) {
1370 /* we are last user */
1371 if (_regulator_can_change_status(rdev) &&
1372 rdev->desc->ops->disable) {
1373 ret = rdev->desc->ops->disable(rdev);
1375 printk(KERN_ERR "%s: failed to disable %s\n",
1376 __func__, rdev_get_name(rdev));
1380 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1384 /* decrease our supplies ref count and disable if required */
1386 _regulator_disable(rdev->supply);
1388 rdev->use_count = 0;
1389 } else if (rdev->use_count > 1) {
1391 if (rdev->constraints &&
1392 (rdev->constraints->valid_ops_mask &
1393 REGULATOR_CHANGE_DRMS))
1394 drms_uA_update(rdev);
1402 * regulator_disable - disable regulator output
1403 * @regulator: regulator source
1405 * Disable the regulator output voltage or current. Calls to
1406 * regulator_enable() must be balanced with calls to
1407 * regulator_disable().
1409 * NOTE: this will only disable the regulator output if no other consumer
1410 * devices have it enabled, the regulator device supports disabling and
1411 * machine constraints permit this operation.
1413 int regulator_disable(struct regulator *regulator)
1415 struct regulator_dev *rdev = regulator->rdev;
1418 mutex_lock(&rdev->mutex);
1419 ret = _regulator_disable(rdev);
1420 mutex_unlock(&rdev->mutex);
1423 EXPORT_SYMBOL_GPL(regulator_disable);
1425 /* locks held by regulator_force_disable() */
1426 static int _regulator_force_disable(struct regulator_dev *rdev)
1431 if (rdev->desc->ops->disable) {
1432 /* ah well, who wants to live forever... */
1433 ret = rdev->desc->ops->disable(rdev);
1435 printk(KERN_ERR "%s: failed to force disable %s\n",
1436 __func__, rdev_get_name(rdev));
1439 /* notify other consumers that power has been forced off */
1440 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1441 REGULATOR_EVENT_DISABLE, NULL);
1444 /* decrease our supplies ref count and disable if required */
1446 _regulator_disable(rdev->supply);
1448 rdev->use_count = 0;
1453 * regulator_force_disable - force disable regulator output
1454 * @regulator: regulator source
1456 * Forcibly disable the regulator output voltage or current.
1457 * NOTE: this *will* disable the regulator output even if other consumer
1458 * devices have it enabled. This should be used for situations when device
1459 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1461 int regulator_force_disable(struct regulator *regulator)
1465 mutex_lock(®ulator->rdev->mutex);
1466 regulator->uA_load = 0;
1467 ret = _regulator_force_disable(regulator->rdev);
1468 mutex_unlock(®ulator->rdev->mutex);
1471 EXPORT_SYMBOL_GPL(regulator_force_disable);
1473 static int _regulator_is_enabled(struct regulator_dev *rdev)
1475 /* If we don't know then assume that the regulator is always on */
1476 if (!rdev->desc->ops->is_enabled)
1479 return rdev->desc->ops->is_enabled(rdev);
1483 * regulator_is_enabled - is the regulator output enabled
1484 * @regulator: regulator source
1486 * Returns positive if the regulator driver backing the source/client
1487 * has requested that the device be enabled, zero if it hasn't, else a
1488 * negative errno code.
1490 * Note that the device backing this regulator handle can have multiple
1491 * users, so it might be enabled even if regulator_enable() was never
1492 * called for this particular source.
1494 int regulator_is_enabled(struct regulator *regulator)
1498 mutex_lock(®ulator->rdev->mutex);
1499 ret = _regulator_is_enabled(regulator->rdev);
1500 mutex_unlock(®ulator->rdev->mutex);
1504 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1507 * regulator_count_voltages - count regulator_list_voltage() selectors
1508 * @regulator: regulator source
1510 * Returns number of selectors, or negative errno. Selectors are
1511 * numbered starting at zero, and typically correspond to bitfields
1512 * in hardware registers.
1514 int regulator_count_voltages(struct regulator *regulator)
1516 struct regulator_dev *rdev = regulator->rdev;
1518 return rdev->desc->n_voltages ? : -EINVAL;
1520 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1523 * regulator_list_voltage - enumerate supported voltages
1524 * @regulator: regulator source
1525 * @selector: identify voltage to list
1526 * Context: can sleep
1528 * Returns a voltage that can be passed to @regulator_set_voltage(),
1529 * zero if this selector code can't be used on this system, or a
1532 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1534 struct regulator_dev *rdev = regulator->rdev;
1535 struct regulator_ops *ops = rdev->desc->ops;
1538 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1541 mutex_lock(&rdev->mutex);
1542 ret = ops->list_voltage(rdev, selector);
1543 mutex_unlock(&rdev->mutex);
1546 if (ret < rdev->constraints->min_uV)
1548 else if (ret > rdev->constraints->max_uV)
1554 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1557 * regulator_is_supported_voltage - check if a voltage range can be supported
1559 * @regulator: Regulator to check.
1560 * @min_uV: Minimum required voltage in uV.
1561 * @max_uV: Maximum required voltage in uV.
1563 * Returns a boolean or a negative error code.
1565 int regulator_is_supported_voltage(struct regulator *regulator,
1566 int min_uV, int max_uV)
1568 int i, voltages, ret;
1570 ret = regulator_count_voltages(regulator);
1575 for (i = 0; i < voltages; i++) {
1576 ret = regulator_list_voltage(regulator, i);
1578 if (ret >= min_uV && ret <= max_uV)
1586 * regulator_set_voltage - set regulator output voltage
1587 * @regulator: regulator source
1588 * @min_uV: Minimum required voltage in uV
1589 * @max_uV: Maximum acceptable voltage in uV
1591 * Sets a voltage regulator to the desired output voltage. This can be set
1592 * during any regulator state. IOW, regulator can be disabled or enabled.
1594 * If the regulator is enabled then the voltage will change to the new value
1595 * immediately otherwise if the regulator is disabled the regulator will
1596 * output at the new voltage when enabled.
1598 * NOTE: If the regulator is shared between several devices then the lowest
1599 * request voltage that meets the system constraints will be used.
1600 * Regulator system constraints must be set for this regulator before
1601 * calling this function otherwise this call will fail.
1603 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1605 struct regulator_dev *rdev = regulator->rdev;
1608 mutex_lock(&rdev->mutex);
1611 if (!rdev->desc->ops->set_voltage) {
1616 /* constraints check */
1617 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1620 regulator->min_uV = min_uV;
1621 regulator->max_uV = max_uV;
1622 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1625 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1626 mutex_unlock(&rdev->mutex);
1629 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1631 static int _regulator_get_voltage(struct regulator_dev *rdev)
1634 if (rdev->desc->ops->get_voltage)
1635 return rdev->desc->ops->get_voltage(rdev);
1641 * regulator_get_voltage - get regulator output voltage
1642 * @regulator: regulator source
1644 * This returns the current regulator voltage in uV.
1646 * NOTE: If the regulator is disabled it will return the voltage value. This
1647 * function should not be used to determine regulator state.
1649 int regulator_get_voltage(struct regulator *regulator)
1653 mutex_lock(®ulator->rdev->mutex);
1655 ret = _regulator_get_voltage(regulator->rdev);
1657 mutex_unlock(®ulator->rdev->mutex);
1661 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1664 * regulator_set_current_limit - set regulator output current limit
1665 * @regulator: regulator source
1666 * @min_uA: Minimuum supported current in uA
1667 * @max_uA: Maximum supported current in uA
1669 * Sets current sink to the desired output current. This can be set during
1670 * any regulator state. IOW, regulator can be disabled or enabled.
1672 * If the regulator is enabled then the current will change to the new value
1673 * immediately otherwise if the regulator is disabled the regulator will
1674 * output at the new current when enabled.
1676 * NOTE: Regulator system constraints must be set for this regulator before
1677 * calling this function otherwise this call will fail.
1679 int regulator_set_current_limit(struct regulator *regulator,
1680 int min_uA, int max_uA)
1682 struct regulator_dev *rdev = regulator->rdev;
1685 mutex_lock(&rdev->mutex);
1688 if (!rdev->desc->ops->set_current_limit) {
1693 /* constraints check */
1694 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1698 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1700 mutex_unlock(&rdev->mutex);
1703 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1705 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1709 mutex_lock(&rdev->mutex);
1712 if (!rdev->desc->ops->get_current_limit) {
1717 ret = rdev->desc->ops->get_current_limit(rdev);
1719 mutex_unlock(&rdev->mutex);
1724 * regulator_get_current_limit - get regulator output current
1725 * @regulator: regulator source
1727 * This returns the current supplied by the specified current sink in uA.
1729 * NOTE: If the regulator is disabled it will return the current value. This
1730 * function should not be used to determine regulator state.
1732 int regulator_get_current_limit(struct regulator *regulator)
1734 return _regulator_get_current_limit(regulator->rdev);
1736 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1739 * regulator_set_mode - set regulator operating mode
1740 * @regulator: regulator source
1741 * @mode: operating mode - one of the REGULATOR_MODE constants
1743 * Set regulator operating mode to increase regulator efficiency or improve
1744 * regulation performance.
1746 * NOTE: Regulator system constraints must be set for this regulator before
1747 * calling this function otherwise this call will fail.
1749 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1751 struct regulator_dev *rdev = regulator->rdev;
1753 int regulator_curr_mode;
1755 mutex_lock(&rdev->mutex);
1758 if (!rdev->desc->ops->set_mode) {
1763 /* return if the same mode is requested */
1764 if (rdev->desc->ops->get_mode) {
1765 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1766 if (regulator_curr_mode == mode) {
1772 /* constraints check */
1773 ret = regulator_check_mode(rdev, mode);
1777 ret = rdev->desc->ops->set_mode(rdev, mode);
1779 mutex_unlock(&rdev->mutex);
1782 EXPORT_SYMBOL_GPL(regulator_set_mode);
1784 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1788 mutex_lock(&rdev->mutex);
1791 if (!rdev->desc->ops->get_mode) {
1796 ret = rdev->desc->ops->get_mode(rdev);
1798 mutex_unlock(&rdev->mutex);
1803 * regulator_get_mode - get regulator operating mode
1804 * @regulator: regulator source
1806 * Get the current regulator operating mode.
1808 unsigned int regulator_get_mode(struct regulator *regulator)
1810 return _regulator_get_mode(regulator->rdev);
1812 EXPORT_SYMBOL_GPL(regulator_get_mode);
1815 * regulator_set_optimum_mode - set regulator optimum operating mode
1816 * @regulator: regulator source
1817 * @uA_load: load current
1819 * Notifies the regulator core of a new device load. This is then used by
1820 * DRMS (if enabled by constraints) to set the most efficient regulator
1821 * operating mode for the new regulator loading.
1823 * Consumer devices notify their supply regulator of the maximum power
1824 * they will require (can be taken from device datasheet in the power
1825 * consumption tables) when they change operational status and hence power
1826 * state. Examples of operational state changes that can affect power
1827 * consumption are :-
1829 * o Device is opened / closed.
1830 * o Device I/O is about to begin or has just finished.
1831 * o Device is idling in between work.
1833 * This information is also exported via sysfs to userspace.
1835 * DRMS will sum the total requested load on the regulator and change
1836 * to the most efficient operating mode if platform constraints allow.
1838 * Returns the new regulator mode or error.
1840 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1842 struct regulator_dev *rdev = regulator->rdev;
1843 struct regulator *consumer;
1844 int ret, output_uV, input_uV, total_uA_load = 0;
1847 mutex_lock(&rdev->mutex);
1849 regulator->uA_load = uA_load;
1850 ret = regulator_check_drms(rdev);
1856 if (!rdev->desc->ops->get_optimum_mode)
1859 /* get output voltage */
1860 output_uV = rdev->desc->ops->get_voltage(rdev);
1861 if (output_uV <= 0) {
1862 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1863 __func__, rdev_get_name(rdev));
1867 /* get input voltage */
1868 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1869 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1871 input_uV = rdev->constraints->input_uV;
1872 if (input_uV <= 0) {
1873 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1874 __func__, rdev_get_name(rdev));
1878 /* calc total requested load for this regulator */
1879 list_for_each_entry(consumer, &rdev->consumer_list, list)
1880 total_uA_load += consumer->uA_load;
1882 mode = rdev->desc->ops->get_optimum_mode(rdev,
1883 input_uV, output_uV,
1885 ret = regulator_check_mode(rdev, mode);
1887 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1888 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
1889 total_uA_load, input_uV, output_uV);
1893 ret = rdev->desc->ops->set_mode(rdev, mode);
1895 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1896 __func__, mode, rdev_get_name(rdev));
1901 mutex_unlock(&rdev->mutex);
1904 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1907 * regulator_register_notifier - register regulator event notifier
1908 * @regulator: regulator source
1909 * @nb: notifier block
1911 * Register notifier block to receive regulator events.
1913 int regulator_register_notifier(struct regulator *regulator,
1914 struct notifier_block *nb)
1916 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1919 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1922 * regulator_unregister_notifier - unregister regulator event notifier
1923 * @regulator: regulator source
1924 * @nb: notifier block
1926 * Unregister regulator event notifier block.
1928 int regulator_unregister_notifier(struct regulator *regulator,
1929 struct notifier_block *nb)
1931 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1934 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1936 /* notify regulator consumers and downstream regulator consumers.
1937 * Note mutex must be held by caller.
1939 static void _notifier_call_chain(struct regulator_dev *rdev,
1940 unsigned long event, void *data)
1942 struct regulator_dev *_rdev;
1944 /* call rdev chain first */
1945 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1947 /* now notify regulator we supply */
1948 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1949 mutex_lock(&_rdev->mutex);
1950 _notifier_call_chain(_rdev, event, data);
1951 mutex_unlock(&_rdev->mutex);
1956 * regulator_bulk_get - get multiple regulator consumers
1958 * @dev: Device to supply
1959 * @num_consumers: Number of consumers to register
1960 * @consumers: Configuration of consumers; clients are stored here.
1962 * @return 0 on success, an errno on failure.
1964 * This helper function allows drivers to get several regulator
1965 * consumers in one operation. If any of the regulators cannot be
1966 * acquired then any regulators that were allocated will be freed
1967 * before returning to the caller.
1969 int regulator_bulk_get(struct device *dev, int num_consumers,
1970 struct regulator_bulk_data *consumers)
1975 for (i = 0; i < num_consumers; i++)
1976 consumers[i].consumer = NULL;
1978 for (i = 0; i < num_consumers; i++) {
1979 consumers[i].consumer = regulator_get(dev,
1980 consumers[i].supply);
1981 if (IS_ERR(consumers[i].consumer)) {
1982 ret = PTR_ERR(consumers[i].consumer);
1983 dev_err(dev, "Failed to get supply '%s': %d\n",
1984 consumers[i].supply, ret);
1985 consumers[i].consumer = NULL;
1993 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1994 regulator_put(consumers[i].consumer);
1998 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2001 * regulator_bulk_enable - enable multiple regulator consumers
2003 * @num_consumers: Number of consumers
2004 * @consumers: Consumer data; clients are stored here.
2005 * @return 0 on success, an errno on failure
2007 * This convenience API allows consumers to enable multiple regulator
2008 * clients in a single API call. If any consumers cannot be enabled
2009 * then any others that were enabled will be disabled again prior to
2012 int regulator_bulk_enable(int num_consumers,
2013 struct regulator_bulk_data *consumers)
2018 for (i = 0; i < num_consumers; i++) {
2019 ret = regulator_enable(consumers[i].consumer);
2027 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
2028 for (--i; i >= 0; --i)
2029 regulator_disable(consumers[i].consumer);
2033 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2036 * regulator_bulk_disable - disable multiple regulator consumers
2038 * @num_consumers: Number of consumers
2039 * @consumers: Consumer data; clients are stored here.
2040 * @return 0 on success, an errno on failure
2042 * This convenience API allows consumers to disable multiple regulator
2043 * clients in a single API call. If any consumers cannot be enabled
2044 * then any others that were disabled will be disabled again prior to
2047 int regulator_bulk_disable(int num_consumers,
2048 struct regulator_bulk_data *consumers)
2053 for (i = 0; i < num_consumers; i++) {
2054 ret = regulator_disable(consumers[i].consumer);
2062 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2064 for (--i; i >= 0; --i)
2065 regulator_enable(consumers[i].consumer);
2069 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2072 * regulator_bulk_free - free multiple regulator consumers
2074 * @num_consumers: Number of consumers
2075 * @consumers: Consumer data; clients are stored here.
2077 * This convenience API allows consumers to free multiple regulator
2078 * clients in a single API call.
2080 void regulator_bulk_free(int num_consumers,
2081 struct regulator_bulk_data *consumers)
2085 for (i = 0; i < num_consumers; i++) {
2086 regulator_put(consumers[i].consumer);
2087 consumers[i].consumer = NULL;
2090 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2093 * regulator_notifier_call_chain - call regulator event notifier
2094 * @rdev: regulator source
2095 * @event: notifier block
2096 * @data: callback-specific data.
2098 * Called by regulator drivers to notify clients a regulator event has
2099 * occurred. We also notify regulator clients downstream.
2100 * Note lock must be held by caller.
2102 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2103 unsigned long event, void *data)
2105 _notifier_call_chain(rdev, event, data);
2109 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2112 * regulator_mode_to_status - convert a regulator mode into a status
2114 * @mode: Mode to convert
2116 * Convert a regulator mode into a status.
2118 int regulator_mode_to_status(unsigned int mode)
2121 case REGULATOR_MODE_FAST:
2122 return REGULATOR_STATUS_FAST;
2123 case REGULATOR_MODE_NORMAL:
2124 return REGULATOR_STATUS_NORMAL;
2125 case REGULATOR_MODE_IDLE:
2126 return REGULATOR_STATUS_IDLE;
2127 case REGULATOR_STATUS_STANDBY:
2128 return REGULATOR_STATUS_STANDBY;
2133 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2136 * To avoid cluttering sysfs (and memory) with useless state, only
2137 * create attributes that can be meaningfully displayed.
2139 static int add_regulator_attributes(struct regulator_dev *rdev)
2141 struct device *dev = &rdev->dev;
2142 struct regulator_ops *ops = rdev->desc->ops;
2145 /* some attributes need specific methods to be displayed */
2146 if (ops->get_voltage) {
2147 status = device_create_file(dev, &dev_attr_microvolts);
2151 if (ops->get_current_limit) {
2152 status = device_create_file(dev, &dev_attr_microamps);
2156 if (ops->get_mode) {
2157 status = device_create_file(dev, &dev_attr_opmode);
2161 if (ops->is_enabled) {
2162 status = device_create_file(dev, &dev_attr_state);
2166 if (ops->get_status) {
2167 status = device_create_file(dev, &dev_attr_status);
2172 /* some attributes are type-specific */
2173 if (rdev->desc->type == REGULATOR_CURRENT) {
2174 status = device_create_file(dev, &dev_attr_requested_microamps);
2179 /* all the other attributes exist to support constraints;
2180 * don't show them if there are no constraints, or if the
2181 * relevant supporting methods are missing.
2183 if (!rdev->constraints)
2186 /* constraints need specific supporting methods */
2187 if (ops->set_voltage) {
2188 status = device_create_file(dev, &dev_attr_min_microvolts);
2191 status = device_create_file(dev, &dev_attr_max_microvolts);
2195 if (ops->set_current_limit) {
2196 status = device_create_file(dev, &dev_attr_min_microamps);
2199 status = device_create_file(dev, &dev_attr_max_microamps);
2204 /* suspend mode constraints need multiple supporting methods */
2205 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2208 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2211 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2214 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2218 if (ops->set_suspend_voltage) {
2219 status = device_create_file(dev,
2220 &dev_attr_suspend_standby_microvolts);
2223 status = device_create_file(dev,
2224 &dev_attr_suspend_mem_microvolts);
2227 status = device_create_file(dev,
2228 &dev_attr_suspend_disk_microvolts);
2233 if (ops->set_suspend_mode) {
2234 status = device_create_file(dev,
2235 &dev_attr_suspend_standby_mode);
2238 status = device_create_file(dev,
2239 &dev_attr_suspend_mem_mode);
2242 status = device_create_file(dev,
2243 &dev_attr_suspend_disk_mode);
2252 * regulator_register - register regulator
2253 * @regulator_desc: regulator to register
2254 * @dev: struct device for the regulator
2255 * @init_data: platform provided init data, passed through by driver
2256 * @driver_data: private regulator data
2258 * Called by regulator drivers to register a regulator.
2259 * Returns 0 on success.
2261 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2262 struct device *dev, struct regulator_init_data *init_data,
2265 static atomic_t regulator_no = ATOMIC_INIT(0);
2266 struct regulator_dev *rdev;
2269 if (regulator_desc == NULL)
2270 return ERR_PTR(-EINVAL);
2272 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2273 return ERR_PTR(-EINVAL);
2275 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2276 regulator_desc->type != REGULATOR_CURRENT)
2277 return ERR_PTR(-EINVAL);
2280 return ERR_PTR(-EINVAL);
2282 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2284 return ERR_PTR(-ENOMEM);
2286 mutex_lock(®ulator_list_mutex);
2288 mutex_init(&rdev->mutex);
2289 rdev->reg_data = driver_data;
2290 rdev->owner = regulator_desc->owner;
2291 rdev->desc = regulator_desc;
2292 INIT_LIST_HEAD(&rdev->consumer_list);
2293 INIT_LIST_HEAD(&rdev->supply_list);
2294 INIT_LIST_HEAD(&rdev->list);
2295 INIT_LIST_HEAD(&rdev->slist);
2296 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2298 /* preform any regulator specific init */
2299 if (init_data->regulator_init) {
2300 ret = init_data->regulator_init(rdev->reg_data);
2305 /* register with sysfs */
2306 rdev->dev.class = ®ulator_class;
2307 rdev->dev.parent = dev;
2308 dev_set_name(&rdev->dev, "regulator.%d",
2309 atomic_inc_return(®ulator_no) - 1);
2310 ret = device_register(&rdev->dev);
2312 put_device(&rdev->dev);
2316 dev_set_drvdata(&rdev->dev, rdev);
2318 /* set regulator constraints */
2319 ret = set_machine_constraints(rdev, &init_data->constraints);
2323 /* add attributes supported by this regulator */
2324 ret = add_regulator_attributes(rdev);
2328 /* set supply regulator if it exists */
2329 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2331 "Supply regulator specified by both name and dev\n");
2335 if (init_data->supply_regulator) {
2336 struct regulator_dev *r;
2339 list_for_each_entry(r, ®ulator_list, list) {
2340 if (strcmp(rdev_get_name(r),
2341 init_data->supply_regulator) == 0) {
2348 dev_err(dev, "Failed to find supply %s\n",
2349 init_data->supply_regulator);
2353 ret = set_supply(rdev, r);
2358 if (init_data->supply_regulator_dev) {
2359 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
2360 ret = set_supply(rdev,
2361 dev_get_drvdata(init_data->supply_regulator_dev));
2366 /* add consumers devices */
2367 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2368 ret = set_consumer_device_supply(rdev,
2369 init_data->consumer_supplies[i].dev,
2370 init_data->consumer_supplies[i].dev_name,
2371 init_data->consumer_supplies[i].supply);
2373 goto unset_supplies;
2376 list_add(&rdev->list, ®ulator_list);
2378 mutex_unlock(®ulator_list_mutex);
2382 unset_regulator_supplies(rdev);
2385 device_unregister(&rdev->dev);
2386 /* device core frees rdev */
2387 rdev = ERR_PTR(ret);
2392 rdev = ERR_PTR(ret);
2395 EXPORT_SYMBOL_GPL(regulator_register);
2398 * regulator_unregister - unregister regulator
2399 * @rdev: regulator to unregister
2401 * Called by regulator drivers to unregister a regulator.
2403 void regulator_unregister(struct regulator_dev *rdev)
2408 mutex_lock(®ulator_list_mutex);
2409 WARN_ON(rdev->open_count);
2410 unset_regulator_supplies(rdev);
2411 list_del(&rdev->list);
2413 sysfs_remove_link(&rdev->dev.kobj, "supply");
2414 device_unregister(&rdev->dev);
2415 mutex_unlock(®ulator_list_mutex);
2417 EXPORT_SYMBOL_GPL(regulator_unregister);
2420 * regulator_suspend_prepare - prepare regulators for system wide suspend
2421 * @state: system suspend state
2423 * Configure each regulator with it's suspend operating parameters for state.
2424 * This will usually be called by machine suspend code prior to supending.
2426 int regulator_suspend_prepare(suspend_state_t state)
2428 struct regulator_dev *rdev;
2431 /* ON is handled by regulator active state */
2432 if (state == PM_SUSPEND_ON)
2435 mutex_lock(®ulator_list_mutex);
2436 list_for_each_entry(rdev, ®ulator_list, list) {
2438 mutex_lock(&rdev->mutex);
2439 ret = suspend_prepare(rdev, state);
2440 mutex_unlock(&rdev->mutex);
2443 printk(KERN_ERR "%s: failed to prepare %s\n",
2444 __func__, rdev_get_name(rdev));
2449 mutex_unlock(®ulator_list_mutex);
2452 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2455 * regulator_has_full_constraints - the system has fully specified constraints
2457 * Calling this function will cause the regulator API to disable all
2458 * regulators which have a zero use count and don't have an always_on
2459 * constraint in a late_initcall.
2461 * The intention is that this will become the default behaviour in a
2462 * future kernel release so users are encouraged to use this facility
2465 void regulator_has_full_constraints(void)
2467 has_full_constraints = 1;
2469 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2472 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2474 * Calling this function will cause the regulator API to provide a
2475 * dummy regulator to consumers if no physical regulator is found,
2476 * allowing most consumers to proceed as though a regulator were
2477 * configured. This allows systems such as those with software
2478 * controllable regulators for the CPU core only to be brought up more
2481 void regulator_use_dummy_regulator(void)
2483 board_wants_dummy_regulator = true;
2485 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2488 * rdev_get_drvdata - get rdev regulator driver data
2491 * Get rdev regulator driver private data. This call can be used in the
2492 * regulator driver context.
2494 void *rdev_get_drvdata(struct regulator_dev *rdev)
2496 return rdev->reg_data;
2498 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2501 * regulator_get_drvdata - get regulator driver data
2502 * @regulator: regulator
2504 * Get regulator driver private data. This call can be used in the consumer
2505 * driver context when non API regulator specific functions need to be called.
2507 void *regulator_get_drvdata(struct regulator *regulator)
2509 return regulator->rdev->reg_data;
2511 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2514 * regulator_set_drvdata - set regulator driver data
2515 * @regulator: regulator
2518 void regulator_set_drvdata(struct regulator *regulator, void *data)
2520 regulator->rdev->reg_data = data;
2522 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2525 * regulator_get_id - get regulator ID
2528 int rdev_get_id(struct regulator_dev *rdev)
2530 return rdev->desc->id;
2532 EXPORT_SYMBOL_GPL(rdev_get_id);
2534 struct device *rdev_get_dev(struct regulator_dev *rdev)
2538 EXPORT_SYMBOL_GPL(rdev_get_dev);
2540 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2542 return reg_init_data->driver_data;
2544 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2546 static int __init regulator_init(void)
2550 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2552 ret = class_register(®ulator_class);
2554 regulator_dummy_init();
2559 /* init early to allow our consumers to complete system booting */
2560 core_initcall(regulator_init);
2562 static int __init regulator_init_complete(void)
2564 struct regulator_dev *rdev;
2565 struct regulator_ops *ops;
2566 struct regulation_constraints *c;
2570 mutex_lock(®ulator_list_mutex);
2572 /* If we have a full configuration then disable any regulators
2573 * which are not in use or always_on. This will become the
2574 * default behaviour in the future.
2576 list_for_each_entry(rdev, ®ulator_list, list) {
2577 ops = rdev->desc->ops;
2578 c = rdev->constraints;
2580 name = rdev_get_name(rdev);
2582 if (!ops->disable || (c && c->always_on))
2585 mutex_lock(&rdev->mutex);
2587 if (rdev->use_count)
2590 /* If we can't read the status assume it's on. */
2591 if (ops->is_enabled)
2592 enabled = ops->is_enabled(rdev);
2599 if (has_full_constraints) {
2600 /* We log since this may kill the system if it
2602 printk(KERN_INFO "%s: disabling %s\n",
2604 ret = ops->disable(rdev);
2607 "%s: couldn't disable %s: %d\n",
2608 __func__, name, ret);
2611 /* The intention is that in future we will
2612 * assume that full constraints are provided
2613 * so warn even if we aren't going to do
2617 "%s: incomplete constraints, leaving %s on\n",
2622 mutex_unlock(&rdev->mutex);
2625 mutex_unlock(®ulator_list_mutex);
2629 late_initcall(regulator_init_complete);