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;
38 * struct regulator_map
40 * Used to provide symbolic supply names to devices.
42 struct regulator_map {
43 struct list_head list;
44 const char *dev_name; /* The dev_name() for the consumer */
46 struct regulator_dev *regulator;
52 * One for each consumer device.
56 struct list_head list;
61 struct device_attribute dev_attr;
62 struct regulator_dev *rdev;
65 static int _regulator_is_enabled(struct regulator_dev *rdev);
66 static int _regulator_disable(struct regulator_dev *rdev);
67 static int _regulator_get_voltage(struct regulator_dev *rdev);
68 static int _regulator_get_current_limit(struct regulator_dev *rdev);
69 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
70 static void _notifier_call_chain(struct regulator_dev *rdev,
71 unsigned long event, void *data);
73 static const char *rdev_get_name(struct regulator_dev *rdev)
75 if (rdev->constraints && rdev->constraints->name)
76 return rdev->constraints->name;
77 else if (rdev->desc->name)
78 return rdev->desc->name;
83 /* gets the regulator for a given consumer device */
84 static struct regulator *get_device_regulator(struct device *dev)
86 struct regulator *regulator = NULL;
87 struct regulator_dev *rdev;
89 mutex_lock(®ulator_list_mutex);
90 list_for_each_entry(rdev, ®ulator_list, list) {
91 mutex_lock(&rdev->mutex);
92 list_for_each_entry(regulator, &rdev->consumer_list, list) {
93 if (regulator->dev == dev) {
94 mutex_unlock(&rdev->mutex);
95 mutex_unlock(®ulator_list_mutex);
99 mutex_unlock(&rdev->mutex);
101 mutex_unlock(®ulator_list_mutex);
105 /* Platform voltage constraint check */
106 static int regulator_check_voltage(struct regulator_dev *rdev,
107 int *min_uV, int *max_uV)
109 BUG_ON(*min_uV > *max_uV);
111 if (!rdev->constraints) {
112 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
113 rdev_get_name(rdev));
116 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
117 printk(KERN_ERR "%s: operation not allowed for %s\n",
118 __func__, rdev_get_name(rdev));
122 if (*max_uV > rdev->constraints->max_uV)
123 *max_uV = rdev->constraints->max_uV;
124 if (*min_uV < rdev->constraints->min_uV)
125 *min_uV = rdev->constraints->min_uV;
127 if (*min_uV > *max_uV)
133 /* current constraint check */
134 static int regulator_check_current_limit(struct regulator_dev *rdev,
135 int *min_uA, int *max_uA)
137 BUG_ON(*min_uA > *max_uA);
139 if (!rdev->constraints) {
140 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
141 rdev_get_name(rdev));
144 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
145 printk(KERN_ERR "%s: operation not allowed for %s\n",
146 __func__, rdev_get_name(rdev));
150 if (*max_uA > rdev->constraints->max_uA)
151 *max_uA = rdev->constraints->max_uA;
152 if (*min_uA < rdev->constraints->min_uA)
153 *min_uA = rdev->constraints->min_uA;
155 if (*min_uA > *max_uA)
161 /* operating mode constraint check */
162 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
165 case REGULATOR_MODE_FAST:
166 case REGULATOR_MODE_NORMAL:
167 case REGULATOR_MODE_IDLE:
168 case REGULATOR_MODE_STANDBY:
174 if (!rdev->constraints) {
175 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
176 rdev_get_name(rdev));
179 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
180 printk(KERN_ERR "%s: operation not allowed for %s\n",
181 __func__, rdev_get_name(rdev));
184 if (!(rdev->constraints->valid_modes_mask & mode)) {
185 printk(KERN_ERR "%s: invalid mode %x for %s\n",
186 __func__, mode, rdev_get_name(rdev));
192 /* dynamic regulator mode switching constraint check */
193 static int regulator_check_drms(struct regulator_dev *rdev)
195 if (!rdev->constraints) {
196 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
197 rdev_get_name(rdev));
200 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
201 printk(KERN_ERR "%s: operation not allowed for %s\n",
202 __func__, rdev_get_name(rdev));
208 static ssize_t device_requested_uA_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
211 struct regulator *regulator;
213 regulator = get_device_regulator(dev);
214 if (regulator == NULL)
217 return sprintf(buf, "%d\n", regulator->uA_load);
220 static ssize_t regulator_uV_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
223 struct regulator_dev *rdev = dev_get_drvdata(dev);
226 mutex_lock(&rdev->mutex);
227 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
228 mutex_unlock(&rdev->mutex);
232 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
234 static ssize_t regulator_uA_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct regulator_dev *rdev = dev_get_drvdata(dev);
239 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
241 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
243 static ssize_t regulator_name_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
246 struct regulator_dev *rdev = dev_get_drvdata(dev);
248 return sprintf(buf, "%s\n", rdev_get_name(rdev));
251 static ssize_t regulator_print_opmode(char *buf, int mode)
254 case REGULATOR_MODE_FAST:
255 return sprintf(buf, "fast\n");
256 case REGULATOR_MODE_NORMAL:
257 return sprintf(buf, "normal\n");
258 case REGULATOR_MODE_IDLE:
259 return sprintf(buf, "idle\n");
260 case REGULATOR_MODE_STANDBY:
261 return sprintf(buf, "standby\n");
263 return sprintf(buf, "unknown\n");
266 static ssize_t regulator_opmode_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 struct regulator_dev *rdev = dev_get_drvdata(dev);
271 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
273 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
275 static ssize_t regulator_print_state(char *buf, int state)
278 return sprintf(buf, "enabled\n");
280 return sprintf(buf, "disabled\n");
282 return sprintf(buf, "unknown\n");
285 static ssize_t regulator_state_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct regulator_dev *rdev = dev_get_drvdata(dev);
291 mutex_lock(&rdev->mutex);
292 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
293 mutex_unlock(&rdev->mutex);
297 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
299 static ssize_t regulator_status_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
302 struct regulator_dev *rdev = dev_get_drvdata(dev);
306 status = rdev->desc->ops->get_status(rdev);
311 case REGULATOR_STATUS_OFF:
314 case REGULATOR_STATUS_ON:
317 case REGULATOR_STATUS_ERROR:
320 case REGULATOR_STATUS_FAST:
323 case REGULATOR_STATUS_NORMAL:
326 case REGULATOR_STATUS_IDLE:
329 case REGULATOR_STATUS_STANDBY:
336 return sprintf(buf, "%s\n", label);
338 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
340 static ssize_t regulator_min_uA_show(struct device *dev,
341 struct device_attribute *attr, char *buf)
343 struct regulator_dev *rdev = dev_get_drvdata(dev);
345 if (!rdev->constraints)
346 return sprintf(buf, "constraint not defined\n");
348 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
350 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
352 static ssize_t regulator_max_uA_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
355 struct regulator_dev *rdev = dev_get_drvdata(dev);
357 if (!rdev->constraints)
358 return sprintf(buf, "constraint not defined\n");
360 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
362 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
364 static ssize_t regulator_min_uV_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
367 struct regulator_dev *rdev = dev_get_drvdata(dev);
369 if (!rdev->constraints)
370 return sprintf(buf, "constraint not defined\n");
372 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
374 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
376 static ssize_t regulator_max_uV_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
379 struct regulator_dev *rdev = dev_get_drvdata(dev);
381 if (!rdev->constraints)
382 return sprintf(buf, "constraint not defined\n");
384 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
386 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
388 static ssize_t regulator_total_uA_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct regulator_dev *rdev = dev_get_drvdata(dev);
392 struct regulator *regulator;
395 mutex_lock(&rdev->mutex);
396 list_for_each_entry(regulator, &rdev->consumer_list, list)
397 uA += regulator->uA_load;
398 mutex_unlock(&rdev->mutex);
399 return sprintf(buf, "%d\n", uA);
401 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
403 static ssize_t regulator_num_users_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
406 struct regulator_dev *rdev = dev_get_drvdata(dev);
407 return sprintf(buf, "%d\n", rdev->use_count);
410 static ssize_t regulator_type_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
413 struct regulator_dev *rdev = dev_get_drvdata(dev);
415 switch (rdev->desc->type) {
416 case REGULATOR_VOLTAGE:
417 return sprintf(buf, "voltage\n");
418 case REGULATOR_CURRENT:
419 return sprintf(buf, "current\n");
421 return sprintf(buf, "unknown\n");
424 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
427 struct regulator_dev *rdev = dev_get_drvdata(dev);
429 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
431 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
432 regulator_suspend_mem_uV_show, NULL);
434 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
435 struct device_attribute *attr, char *buf)
437 struct regulator_dev *rdev = dev_get_drvdata(dev);
439 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
441 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
442 regulator_suspend_disk_uV_show, NULL);
444 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct regulator_dev *rdev = dev_get_drvdata(dev);
449 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
451 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
452 regulator_suspend_standby_uV_show, NULL);
454 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 struct regulator_dev *rdev = dev_get_drvdata(dev);
459 return regulator_print_opmode(buf,
460 rdev->constraints->state_mem.mode);
462 static DEVICE_ATTR(suspend_mem_mode, 0444,
463 regulator_suspend_mem_mode_show, NULL);
465 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct regulator_dev *rdev = dev_get_drvdata(dev);
470 return regulator_print_opmode(buf,
471 rdev->constraints->state_disk.mode);
473 static DEVICE_ATTR(suspend_disk_mode, 0444,
474 regulator_suspend_disk_mode_show, NULL);
476 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
479 struct regulator_dev *rdev = dev_get_drvdata(dev);
481 return regulator_print_opmode(buf,
482 rdev->constraints->state_standby.mode);
484 static DEVICE_ATTR(suspend_standby_mode, 0444,
485 regulator_suspend_standby_mode_show, NULL);
487 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
490 struct regulator_dev *rdev = dev_get_drvdata(dev);
492 return regulator_print_state(buf,
493 rdev->constraints->state_mem.enabled);
495 static DEVICE_ATTR(suspend_mem_state, 0444,
496 regulator_suspend_mem_state_show, NULL);
498 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 struct regulator_dev *rdev = dev_get_drvdata(dev);
503 return regulator_print_state(buf,
504 rdev->constraints->state_disk.enabled);
506 static DEVICE_ATTR(suspend_disk_state, 0444,
507 regulator_suspend_disk_state_show, NULL);
509 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
512 struct regulator_dev *rdev = dev_get_drvdata(dev);
514 return regulator_print_state(buf,
515 rdev->constraints->state_standby.enabled);
517 static DEVICE_ATTR(suspend_standby_state, 0444,
518 regulator_suspend_standby_state_show, NULL);
522 * These are the only attributes are present for all regulators.
523 * Other attributes are a function of regulator functionality.
525 static struct device_attribute regulator_dev_attrs[] = {
526 __ATTR(name, 0444, regulator_name_show, NULL),
527 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
528 __ATTR(type, 0444, regulator_type_show, NULL),
532 static void regulator_dev_release(struct device *dev)
534 struct regulator_dev *rdev = dev_get_drvdata(dev);
538 static struct class regulator_class = {
540 .dev_release = regulator_dev_release,
541 .dev_attrs = regulator_dev_attrs,
544 /* Calculate the new optimum regulator operating mode based on the new total
545 * consumer load. All locks held by caller */
546 static void drms_uA_update(struct regulator_dev *rdev)
548 struct regulator *sibling;
549 int current_uA = 0, output_uV, input_uV, err;
552 err = regulator_check_drms(rdev);
553 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
554 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
557 /* get output voltage */
558 output_uV = rdev->desc->ops->get_voltage(rdev);
562 /* get input voltage */
563 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
564 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
566 input_uV = rdev->constraints->input_uV;
570 /* calc total requested load */
571 list_for_each_entry(sibling, &rdev->consumer_list, list)
572 current_uA += sibling->uA_load;
574 /* now get the optimum mode for our new total regulator load */
575 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
576 output_uV, current_uA);
578 /* check the new mode is allowed */
579 err = regulator_check_mode(rdev, mode);
581 rdev->desc->ops->set_mode(rdev, mode);
584 static int suspend_set_state(struct regulator_dev *rdev,
585 struct regulator_state *rstate)
590 can_set_state = rdev->desc->ops->set_suspend_enable &&
591 rdev->desc->ops->set_suspend_disable;
593 /* If we have no suspend mode configration don't set anything;
594 * only warn if the driver actually makes the suspend mode
597 if (!rstate->enabled && !rstate->disabled) {
599 printk(KERN_WARNING "%s: No configuration for %s\n",
600 __func__, rdev_get_name(rdev));
604 if (rstate->enabled && rstate->disabled) {
605 printk(KERN_ERR "%s: invalid configuration for %s\n",
606 __func__, rdev_get_name(rdev));
610 if (!can_set_state) {
611 printk(KERN_ERR "%s: no way to set suspend state\n",
617 ret = rdev->desc->ops->set_suspend_enable(rdev);
619 ret = rdev->desc->ops->set_suspend_disable(rdev);
621 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
625 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
626 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
628 printk(KERN_ERR "%s: failed to set voltage\n",
634 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
635 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
637 printk(KERN_ERR "%s: failed to set mode\n", __func__);
644 /* locks held by caller */
645 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
647 if (!rdev->constraints)
651 case PM_SUSPEND_STANDBY:
652 return suspend_set_state(rdev,
653 &rdev->constraints->state_standby);
655 return suspend_set_state(rdev,
656 &rdev->constraints->state_mem);
658 return suspend_set_state(rdev,
659 &rdev->constraints->state_disk);
665 static void print_constraints(struct regulator_dev *rdev)
667 struct regulation_constraints *constraints = rdev->constraints;
672 if (constraints->min_uV && constraints->max_uV) {
673 if (constraints->min_uV == constraints->max_uV)
674 count += sprintf(buf + count, "%d mV ",
675 constraints->min_uV / 1000);
677 count += sprintf(buf + count, "%d <--> %d mV ",
678 constraints->min_uV / 1000,
679 constraints->max_uV / 1000);
682 if (!constraints->min_uV ||
683 constraints->min_uV != constraints->max_uV) {
684 ret = _regulator_get_voltage(rdev);
686 count += sprintf(buf + count, "at %d mV ", ret / 1000);
689 if (constraints->min_uA && constraints->max_uA) {
690 if (constraints->min_uA == constraints->max_uA)
691 count += sprintf(buf + count, "%d mA ",
692 constraints->min_uA / 1000);
694 count += sprintf(buf + count, "%d <--> %d mA ",
695 constraints->min_uA / 1000,
696 constraints->max_uA / 1000);
699 if (!constraints->min_uA ||
700 constraints->min_uA != constraints->max_uA) {
701 ret = _regulator_get_current_limit(rdev);
703 count += sprintf(buf + count, "at %d mA ", ret / 1000);
706 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
707 count += sprintf(buf + count, "fast ");
708 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
709 count += sprintf(buf + count, "normal ");
710 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
711 count += sprintf(buf + count, "idle ");
712 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
713 count += sprintf(buf + count, "standby");
715 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
718 static int machine_constraints_voltage(struct regulator_dev *rdev,
719 struct regulation_constraints *constraints)
721 struct regulator_ops *ops = rdev->desc->ops;
722 const char *name = rdev_get_name(rdev);
725 /* do we need to apply the constraint voltage */
726 if (rdev->constraints->apply_uV &&
727 rdev->constraints->min_uV == rdev->constraints->max_uV &&
729 ret = ops->set_voltage(rdev,
730 rdev->constraints->min_uV, rdev->constraints->max_uV);
732 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
734 rdev->constraints->min_uV, name);
735 rdev->constraints = NULL;
740 /* constrain machine-level voltage specs to fit
741 * the actual range supported by this regulator.
743 if (ops->list_voltage && rdev->desc->n_voltages) {
744 int count = rdev->desc->n_voltages;
746 int min_uV = INT_MAX;
747 int max_uV = INT_MIN;
748 int cmin = constraints->min_uV;
749 int cmax = constraints->max_uV;
751 /* it's safe to autoconfigure fixed-voltage supplies
752 and the constraints are used by list_voltage. */
753 if (count == 1 && !cmin) {
756 constraints->min_uV = cmin;
757 constraints->max_uV = cmax;
760 /* voltage constraints are optional */
761 if ((cmin == 0) && (cmax == 0))
764 /* else require explicit machine-level constraints */
765 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
766 pr_err("%s: %s '%s' voltage constraints\n",
767 __func__, "invalid", name);
771 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
772 for (i = 0; i < count; i++) {
775 value = ops->list_voltage(rdev, i);
779 /* maybe adjust [min_uV..max_uV] */
780 if (value >= cmin && value < min_uV)
782 if (value <= cmax && value > max_uV)
786 /* final: [min_uV..max_uV] valid iff constraints valid */
787 if (max_uV < min_uV) {
788 pr_err("%s: %s '%s' voltage constraints\n",
789 __func__, "unsupportable", name);
793 /* use regulator's subset of machine constraints */
794 if (constraints->min_uV < min_uV) {
795 pr_debug("%s: override '%s' %s, %d -> %d\n",
796 __func__, name, "min_uV",
797 constraints->min_uV, min_uV);
798 constraints->min_uV = min_uV;
800 if (constraints->max_uV > max_uV) {
801 pr_debug("%s: override '%s' %s, %d -> %d\n",
802 __func__, name, "max_uV",
803 constraints->max_uV, max_uV);
804 constraints->max_uV = max_uV;
812 * set_machine_constraints - sets regulator constraints
813 * @rdev: regulator source
814 * @constraints: constraints to apply
816 * Allows platform initialisation code to define and constrain
817 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
818 * Constraints *must* be set by platform code in order for some
819 * regulator operations to proceed i.e. set_voltage, set_current_limit,
822 static int set_machine_constraints(struct regulator_dev *rdev,
823 struct regulation_constraints *constraints)
827 struct regulator_ops *ops = rdev->desc->ops;
829 rdev->constraints = constraints;
831 name = rdev_get_name(rdev);
833 ret = machine_constraints_voltage(rdev, constraints);
837 /* do we need to setup our suspend state */
838 if (constraints->initial_state) {
839 ret = suspend_prepare(rdev, constraints->initial_state);
841 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
843 rdev->constraints = NULL;
848 if (constraints->initial_mode) {
849 if (!ops->set_mode) {
850 printk(KERN_ERR "%s: no set_mode operation for %s\n",
856 ret = ops->set_mode(rdev, constraints->initial_mode);
859 "%s: failed to set initial mode for %s: %d\n",
860 __func__, name, ret);
865 /* If the constraints say the regulator should be on at this point
866 * and we have control then make sure it is enabled.
868 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
869 ret = ops->enable(rdev);
871 printk(KERN_ERR "%s: failed to enable %s\n",
873 rdev->constraints = NULL;
878 print_constraints(rdev);
884 * set_supply - set regulator supply regulator
885 * @rdev: regulator name
886 * @supply_rdev: supply regulator name
888 * Called by platform initialisation code to set the supply regulator for this
889 * regulator. This ensures that a regulators supply will also be enabled by the
890 * core if it's child is enabled.
892 static int set_supply(struct regulator_dev *rdev,
893 struct regulator_dev *supply_rdev)
897 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
901 "%s: could not add device link %s err %d\n",
902 __func__, supply_rdev->dev.kobj.name, err);
905 rdev->supply = supply_rdev;
906 list_add(&rdev->slist, &supply_rdev->supply_list);
912 * set_consumer_device_supply: Bind a regulator to a symbolic supply
913 * @rdev: regulator source
914 * @consumer_dev: device the supply applies to
915 * @consumer_dev_name: dev_name() string for device supply applies to
916 * @supply: symbolic name for supply
918 * Allows platform initialisation code to map physical regulator
919 * sources to symbolic names for supplies for use by devices. Devices
920 * should use these symbolic names to request regulators, avoiding the
921 * need to provide board-specific regulator names as platform data.
923 * Only one of consumer_dev and consumer_dev_name may be specified.
925 static int set_consumer_device_supply(struct regulator_dev *rdev,
926 struct device *consumer_dev, const char *consumer_dev_name,
929 struct regulator_map *node;
932 if (consumer_dev && consumer_dev_name)
935 if (!consumer_dev_name && consumer_dev)
936 consumer_dev_name = dev_name(consumer_dev);
941 if (consumer_dev_name != NULL)
946 list_for_each_entry(node, ®ulator_map_list, list) {
947 if (node->dev_name && consumer_dev_name) {
948 if (strcmp(node->dev_name, consumer_dev_name) != 0)
950 } else if (node->dev_name || consumer_dev_name) {
954 if (strcmp(node->supply, supply) != 0)
957 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
958 dev_name(&node->regulator->dev),
959 node->regulator->desc->name,
961 dev_name(&rdev->dev), rdev_get_name(rdev));
965 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
969 node->regulator = rdev;
970 node->supply = supply;
973 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
974 if (node->dev_name == NULL) {
980 list_add(&node->list, ®ulator_map_list);
984 static void unset_regulator_supplies(struct regulator_dev *rdev)
986 struct regulator_map *node, *n;
988 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
989 if (rdev == node->regulator) {
990 list_del(&node->list);
991 kfree(node->dev_name);
997 #define REG_STR_SIZE 32
999 static struct regulator *create_regulator(struct regulator_dev *rdev,
1001 const char *supply_name)
1003 struct regulator *regulator;
1004 char buf[REG_STR_SIZE];
1007 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1008 if (regulator == NULL)
1011 mutex_lock(&rdev->mutex);
1012 regulator->rdev = rdev;
1013 list_add(®ulator->list, &rdev->consumer_list);
1016 /* create a 'requested_microamps_name' sysfs entry */
1017 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1019 if (size >= REG_STR_SIZE)
1022 regulator->dev = dev;
1023 sysfs_attr_init(®ulator->dev_attr.attr);
1024 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1025 if (regulator->dev_attr.attr.name == NULL)
1028 regulator->dev_attr.attr.mode = 0444;
1029 regulator->dev_attr.show = device_requested_uA_show;
1030 err = device_create_file(dev, ®ulator->dev_attr);
1032 printk(KERN_WARNING "%s: could not add regulator_dev"
1033 " load sysfs\n", __func__);
1037 /* also add a link to the device sysfs entry */
1038 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1039 dev->kobj.name, supply_name);
1040 if (size >= REG_STR_SIZE)
1043 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1044 if (regulator->supply_name == NULL)
1047 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1051 "%s: could not add device link %s err %d\n",
1052 __func__, dev->kobj.name, err);
1053 device_remove_file(dev, ®ulator->dev_attr);
1057 mutex_unlock(&rdev->mutex);
1060 kfree(regulator->supply_name);
1062 device_remove_file(regulator->dev, ®ulator->dev_attr);
1064 kfree(regulator->dev_attr.attr.name);
1066 list_del(®ulator->list);
1068 mutex_unlock(&rdev->mutex);
1072 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1074 if (!rdev->desc->ops->enable_time)
1076 return rdev->desc->ops->enable_time(rdev);
1079 /* Internal regulator request function */
1080 static struct regulator *_regulator_get(struct device *dev, const char *id,
1083 struct regulator_dev *rdev;
1084 struct regulator_map *map;
1085 struct regulator *regulator = ERR_PTR(-ENODEV);
1086 const char *devname = NULL;
1090 printk(KERN_ERR "regulator: get() with no identifier\n");
1095 devname = dev_name(dev);
1097 mutex_lock(®ulator_list_mutex);
1099 list_for_each_entry(map, ®ulator_map_list, list) {
1100 /* If the mapping has a device set up it must match */
1101 if (map->dev_name &&
1102 (!devname || strcmp(map->dev_name, devname)))
1105 if (strcmp(map->supply, id) == 0) {
1106 rdev = map->regulator;
1111 #ifdef CONFIG_REGULATOR_DUMMY
1113 devname = "deviceless";
1115 /* If the board didn't flag that it was fully constrained then
1116 * substitute in a dummy regulator so consumers can continue.
1118 if (!has_full_constraints) {
1119 pr_warning("%s supply %s not found, using dummy regulator\n",
1121 rdev = dummy_regulator_rdev;
1126 mutex_unlock(®ulator_list_mutex);
1130 if (rdev->exclusive) {
1131 regulator = ERR_PTR(-EPERM);
1135 if (exclusive && rdev->open_count) {
1136 regulator = ERR_PTR(-EBUSY);
1140 if (!try_module_get(rdev->owner))
1143 regulator = create_regulator(rdev, dev, id);
1144 if (regulator == NULL) {
1145 regulator = ERR_PTR(-ENOMEM);
1146 module_put(rdev->owner);
1151 rdev->exclusive = 1;
1153 ret = _regulator_is_enabled(rdev);
1155 rdev->use_count = 1;
1157 rdev->use_count = 0;
1161 mutex_unlock(®ulator_list_mutex);
1167 * regulator_get - lookup and obtain a reference to a regulator.
1168 * @dev: device for regulator "consumer"
1169 * @id: Supply name or regulator ID.
1171 * Returns a struct regulator corresponding to the regulator producer,
1172 * or IS_ERR() condition containing errno.
1174 * Use of supply names configured via regulator_set_device_supply() is
1175 * strongly encouraged. It is recommended that the supply name used
1176 * should match the name used for the supply and/or the relevant
1177 * device pins in the datasheet.
1179 struct regulator *regulator_get(struct device *dev, const char *id)
1181 return _regulator_get(dev, id, 0);
1183 EXPORT_SYMBOL_GPL(regulator_get);
1186 * regulator_get_exclusive - obtain exclusive access to a regulator.
1187 * @dev: device for regulator "consumer"
1188 * @id: Supply name or regulator ID.
1190 * Returns a struct regulator corresponding to the regulator producer,
1191 * or IS_ERR() condition containing errno. Other consumers will be
1192 * unable to obtain this reference is held and the use count for the
1193 * regulator will be initialised to reflect the current state of the
1196 * This is intended for use by consumers which cannot tolerate shared
1197 * use of the regulator such as those which need to force the
1198 * regulator off for correct operation of the hardware they are
1201 * Use of supply names configured via regulator_set_device_supply() is
1202 * strongly encouraged. It is recommended that the supply name used
1203 * should match the name used for the supply and/or the relevant
1204 * device pins in the datasheet.
1206 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1208 return _regulator_get(dev, id, 1);
1210 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1213 * regulator_put - "free" the regulator source
1214 * @regulator: regulator source
1216 * Note: drivers must ensure that all regulator_enable calls made on this
1217 * regulator source are balanced by regulator_disable calls prior to calling
1220 void regulator_put(struct regulator *regulator)
1222 struct regulator_dev *rdev;
1224 if (regulator == NULL || IS_ERR(regulator))
1227 mutex_lock(®ulator_list_mutex);
1228 rdev = regulator->rdev;
1230 /* remove any sysfs entries */
1231 if (regulator->dev) {
1232 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1233 kfree(regulator->supply_name);
1234 device_remove_file(regulator->dev, ®ulator->dev_attr);
1235 kfree(regulator->dev_attr.attr.name);
1237 list_del(®ulator->list);
1241 rdev->exclusive = 0;
1243 module_put(rdev->owner);
1244 mutex_unlock(®ulator_list_mutex);
1246 EXPORT_SYMBOL_GPL(regulator_put);
1248 static int _regulator_can_change_status(struct regulator_dev *rdev)
1250 if (!rdev->constraints)
1253 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1259 /* locks held by regulator_enable() */
1260 static int _regulator_enable(struct regulator_dev *rdev)
1264 /* do we need to enable the supply regulator first */
1266 ret = _regulator_enable(rdev->supply);
1268 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1269 __func__, rdev_get_name(rdev), ret);
1274 /* check voltage and requested load before enabling */
1275 if (rdev->constraints &&
1276 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1277 drms_uA_update(rdev);
1279 if (rdev->use_count == 0) {
1280 /* The regulator may on if it's not switchable or left on */
1281 ret = _regulator_is_enabled(rdev);
1282 if (ret == -EINVAL || ret == 0) {
1283 if (!_regulator_can_change_status(rdev))
1286 if (!rdev->desc->ops->enable)
1289 /* Query before enabling in case configuration
1291 ret = _regulator_get_enable_time(rdev);
1296 "%s: enable_time() failed for %s: %d\n",
1297 __func__, rdev_get_name(rdev),
1302 /* Allow the regulator to ramp; it would be useful
1303 * to extend this for bulk operations so that the
1304 * regulators can ramp together. */
1305 ret = rdev->desc->ops->enable(rdev);
1310 mdelay(delay / 1000);
1314 } else if (ret < 0) {
1315 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
1316 __func__, rdev_get_name(rdev), ret);
1319 /* Fallthrough on positive return values - already enabled */
1328 * regulator_enable - enable regulator output
1329 * @regulator: regulator source
1331 * Request that the regulator be enabled with the regulator output at
1332 * the predefined voltage or current value. Calls to regulator_enable()
1333 * must be balanced with calls to regulator_disable().
1335 * NOTE: the output value can be set by other drivers, boot loader or may be
1336 * hardwired in the regulator.
1338 int regulator_enable(struct regulator *regulator)
1340 struct regulator_dev *rdev = regulator->rdev;
1343 mutex_lock(&rdev->mutex);
1344 ret = _regulator_enable(rdev);
1345 mutex_unlock(&rdev->mutex);
1348 EXPORT_SYMBOL_GPL(regulator_enable);
1350 /* locks held by regulator_disable() */
1351 static int _regulator_disable(struct regulator_dev *rdev)
1355 if (WARN(rdev->use_count <= 0,
1356 "unbalanced disables for %s\n",
1357 rdev_get_name(rdev)))
1360 /* are we the last user and permitted to disable ? */
1361 if (rdev->use_count == 1 &&
1362 (rdev->constraints && !rdev->constraints->always_on)) {
1364 /* we are last user */
1365 if (_regulator_can_change_status(rdev) &&
1366 rdev->desc->ops->disable) {
1367 ret = rdev->desc->ops->disable(rdev);
1369 printk(KERN_ERR "%s: failed to disable %s\n",
1370 __func__, rdev_get_name(rdev));
1374 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1378 /* decrease our supplies ref count and disable if required */
1380 _regulator_disable(rdev->supply);
1382 rdev->use_count = 0;
1383 } else if (rdev->use_count > 1) {
1385 if (rdev->constraints &&
1386 (rdev->constraints->valid_ops_mask &
1387 REGULATOR_CHANGE_DRMS))
1388 drms_uA_update(rdev);
1396 * regulator_disable - disable regulator output
1397 * @regulator: regulator source
1399 * Disable the regulator output voltage or current. Calls to
1400 * regulator_enable() must be balanced with calls to
1401 * regulator_disable().
1403 * NOTE: this will only disable the regulator output if no other consumer
1404 * devices have it enabled, the regulator device supports disabling and
1405 * machine constraints permit this operation.
1407 int regulator_disable(struct regulator *regulator)
1409 struct regulator_dev *rdev = regulator->rdev;
1412 mutex_lock(&rdev->mutex);
1413 ret = _regulator_disable(rdev);
1414 mutex_unlock(&rdev->mutex);
1417 EXPORT_SYMBOL_GPL(regulator_disable);
1419 /* locks held by regulator_force_disable() */
1420 static int _regulator_force_disable(struct regulator_dev *rdev)
1425 if (rdev->desc->ops->disable) {
1426 /* ah well, who wants to live forever... */
1427 ret = rdev->desc->ops->disable(rdev);
1429 printk(KERN_ERR "%s: failed to force disable %s\n",
1430 __func__, rdev_get_name(rdev));
1433 /* notify other consumers that power has been forced off */
1434 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1435 REGULATOR_EVENT_DISABLE, NULL);
1438 /* decrease our supplies ref count and disable if required */
1440 _regulator_disable(rdev->supply);
1442 rdev->use_count = 0;
1447 * regulator_force_disable - force disable regulator output
1448 * @regulator: regulator source
1450 * Forcibly disable the regulator output voltage or current.
1451 * NOTE: this *will* disable the regulator output even if other consumer
1452 * devices have it enabled. This should be used for situations when device
1453 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1455 int regulator_force_disable(struct regulator *regulator)
1459 mutex_lock(®ulator->rdev->mutex);
1460 regulator->uA_load = 0;
1461 ret = _regulator_force_disable(regulator->rdev);
1462 mutex_unlock(®ulator->rdev->mutex);
1465 EXPORT_SYMBOL_GPL(regulator_force_disable);
1467 static int _regulator_is_enabled(struct regulator_dev *rdev)
1469 /* If we don't know then assume that the regulator is always on */
1470 if (!rdev->desc->ops->is_enabled)
1473 return rdev->desc->ops->is_enabled(rdev);
1477 * regulator_is_enabled - is the regulator output enabled
1478 * @regulator: regulator source
1480 * Returns positive if the regulator driver backing the source/client
1481 * has requested that the device be enabled, zero if it hasn't, else a
1482 * negative errno code.
1484 * Note that the device backing this regulator handle can have multiple
1485 * users, so it might be enabled even if regulator_enable() was never
1486 * called for this particular source.
1488 int regulator_is_enabled(struct regulator *regulator)
1492 mutex_lock(®ulator->rdev->mutex);
1493 ret = _regulator_is_enabled(regulator->rdev);
1494 mutex_unlock(®ulator->rdev->mutex);
1498 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1501 * regulator_count_voltages - count regulator_list_voltage() selectors
1502 * @regulator: regulator source
1504 * Returns number of selectors, or negative errno. Selectors are
1505 * numbered starting at zero, and typically correspond to bitfields
1506 * in hardware registers.
1508 int regulator_count_voltages(struct regulator *regulator)
1510 struct regulator_dev *rdev = regulator->rdev;
1512 return rdev->desc->n_voltages ? : -EINVAL;
1514 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1517 * regulator_list_voltage - enumerate supported voltages
1518 * @regulator: regulator source
1519 * @selector: identify voltage to list
1520 * Context: can sleep
1522 * Returns a voltage that can be passed to @regulator_set_voltage(),
1523 * zero if this selector code can't be used on this system, or a
1526 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1528 struct regulator_dev *rdev = regulator->rdev;
1529 struct regulator_ops *ops = rdev->desc->ops;
1532 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1535 mutex_lock(&rdev->mutex);
1536 ret = ops->list_voltage(rdev, selector);
1537 mutex_unlock(&rdev->mutex);
1540 if (ret < rdev->constraints->min_uV)
1542 else if (ret > rdev->constraints->max_uV)
1548 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1551 * regulator_is_supported_voltage - check if a voltage range can be supported
1553 * @regulator: Regulator to check.
1554 * @min_uV: Minimum required voltage in uV.
1555 * @max_uV: Maximum required voltage in uV.
1557 * Returns a boolean or a negative error code.
1559 int regulator_is_supported_voltage(struct regulator *regulator,
1560 int min_uV, int max_uV)
1562 int i, voltages, ret;
1564 ret = regulator_count_voltages(regulator);
1569 for (i = 0; i < voltages; i++) {
1570 ret = regulator_list_voltage(regulator, i);
1572 if (ret >= min_uV && ret <= max_uV)
1580 * regulator_set_voltage - set regulator output voltage
1581 * @regulator: regulator source
1582 * @min_uV: Minimum required voltage in uV
1583 * @max_uV: Maximum acceptable voltage in uV
1585 * Sets a voltage regulator to the desired output voltage. This can be set
1586 * during any regulator state. IOW, regulator can be disabled or enabled.
1588 * If the regulator is enabled then the voltage will change to the new value
1589 * immediately otherwise if the regulator is disabled the regulator will
1590 * output at the new voltage when enabled.
1592 * NOTE: If the regulator is shared between several devices then the lowest
1593 * request voltage that meets the system constraints will be used.
1594 * Regulator system constraints must be set for this regulator before
1595 * calling this function otherwise this call will fail.
1597 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1599 struct regulator_dev *rdev = regulator->rdev;
1602 mutex_lock(&rdev->mutex);
1605 if (!rdev->desc->ops->set_voltage) {
1610 /* constraints check */
1611 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1614 regulator->min_uV = min_uV;
1615 regulator->max_uV = max_uV;
1616 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1619 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1620 mutex_unlock(&rdev->mutex);
1623 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1624 int regulator_set_suspend_voltage(struct regulator *regulator, int uV)
1626 struct regulator_dev *rdev = regulator->rdev;
1629 if (rdev->desc->ops->set_suspend_voltage && uV > 0) {
1630 ret = rdev->desc->ops->set_suspend_voltage(rdev, uV);
1632 printk(KERN_ERR "%s: failed to set voltage\n",
1640 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
1641 static int _regulator_get_voltage(struct regulator_dev *rdev)
1644 if (rdev->desc->ops->get_voltage)
1645 return rdev->desc->ops->get_voltage(rdev);
1651 * regulator_get_voltage - get regulator output voltage
1652 * @regulator: regulator source
1654 * This returns the current regulator voltage in uV.
1656 * NOTE: If the regulator is disabled it will return the voltage value. This
1657 * function should not be used to determine regulator state.
1659 int regulator_get_voltage(struct regulator *regulator)
1663 mutex_lock(®ulator->rdev->mutex);
1665 ret = _regulator_get_voltage(regulator->rdev);
1667 mutex_unlock(®ulator->rdev->mutex);
1671 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1674 * regulator_set_current_limit - set regulator output current limit
1675 * @regulator: regulator source
1676 * @min_uA: Minimuum supported current in uA
1677 * @max_uA: Maximum supported current in uA
1679 * Sets current sink to the desired output current. This can be set during
1680 * any regulator state. IOW, regulator can be disabled or enabled.
1682 * If the regulator is enabled then the current will change to the new value
1683 * immediately otherwise if the regulator is disabled the regulator will
1684 * output at the new current when enabled.
1686 * NOTE: Regulator system constraints must be set for this regulator before
1687 * calling this function otherwise this call will fail.
1689 int regulator_set_current_limit(struct regulator *regulator,
1690 int min_uA, int max_uA)
1692 struct regulator_dev *rdev = regulator->rdev;
1695 mutex_lock(&rdev->mutex);
1698 if (!rdev->desc->ops->set_current_limit) {
1703 /* constraints check */
1704 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1708 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1710 mutex_unlock(&rdev->mutex);
1713 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1715 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1719 mutex_lock(&rdev->mutex);
1722 if (!rdev->desc->ops->get_current_limit) {
1727 ret = rdev->desc->ops->get_current_limit(rdev);
1729 mutex_unlock(&rdev->mutex);
1734 * regulator_get_current_limit - get regulator output current
1735 * @regulator: regulator source
1737 * This returns the current supplied by the specified current sink in uA.
1739 * NOTE: If the regulator is disabled it will return the current value. This
1740 * function should not be used to determine regulator state.
1742 int regulator_get_current_limit(struct regulator *regulator)
1744 return _regulator_get_current_limit(regulator->rdev);
1746 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1749 * regulator_set_mode - set regulator operating mode
1750 * @regulator: regulator source
1751 * @mode: operating mode - one of the REGULATOR_MODE constants
1753 * Set regulator operating mode to increase regulator efficiency or improve
1754 * regulation performance.
1756 * NOTE: Regulator system constraints must be set for this regulator before
1757 * calling this function otherwise this call will fail.
1759 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1761 struct regulator_dev *rdev = regulator->rdev;
1763 int regulator_curr_mode;
1765 mutex_lock(&rdev->mutex);
1768 if (!rdev->desc->ops->set_mode) {
1773 /* return if the same mode is requested */
1774 if (rdev->desc->ops->get_mode) {
1775 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1776 if (regulator_curr_mode == mode) {
1782 /* constraints check */
1783 ret = regulator_check_mode(rdev, mode);
1787 ret = rdev->desc->ops->set_mode(rdev, mode);
1789 mutex_unlock(&rdev->mutex);
1792 EXPORT_SYMBOL_GPL(regulator_set_mode);
1794 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1798 mutex_lock(&rdev->mutex);
1801 if (!rdev->desc->ops->get_mode) {
1806 ret = rdev->desc->ops->get_mode(rdev);
1808 mutex_unlock(&rdev->mutex);
1813 * regulator_get_mode - get regulator operating mode
1814 * @regulator: regulator source
1816 * Get the current regulator operating mode.
1818 unsigned int regulator_get_mode(struct regulator *regulator)
1820 return _regulator_get_mode(regulator->rdev);
1822 EXPORT_SYMBOL_GPL(regulator_get_mode);
1825 * regulator_set_optimum_mode - set regulator optimum operating mode
1826 * @regulator: regulator source
1827 * @uA_load: load current
1829 * Notifies the regulator core of a new device load. This is then used by
1830 * DRMS (if enabled by constraints) to set the most efficient regulator
1831 * operating mode for the new regulator loading.
1833 * Consumer devices notify their supply regulator of the maximum power
1834 * they will require (can be taken from device datasheet in the power
1835 * consumption tables) when they change operational status and hence power
1836 * state. Examples of operational state changes that can affect power
1837 * consumption are :-
1839 * o Device is opened / closed.
1840 * o Device I/O is about to begin or has just finished.
1841 * o Device is idling in between work.
1843 * This information is also exported via sysfs to userspace.
1845 * DRMS will sum the total requested load on the regulator and change
1846 * to the most efficient operating mode if platform constraints allow.
1848 * Returns the new regulator mode or error.
1850 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1852 struct regulator_dev *rdev = regulator->rdev;
1853 struct regulator *consumer;
1854 int ret, output_uV, input_uV, total_uA_load = 0;
1857 mutex_lock(&rdev->mutex);
1859 regulator->uA_load = uA_load;
1860 ret = regulator_check_drms(rdev);
1866 if (!rdev->desc->ops->get_optimum_mode)
1869 /* get output voltage */
1870 output_uV = rdev->desc->ops->get_voltage(rdev);
1871 if (output_uV <= 0) {
1872 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1873 __func__, rdev_get_name(rdev));
1877 /* get input voltage */
1878 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1879 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1881 input_uV = rdev->constraints->input_uV;
1882 if (input_uV <= 0) {
1883 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1884 __func__, rdev_get_name(rdev));
1888 /* calc total requested load for this regulator */
1889 list_for_each_entry(consumer, &rdev->consumer_list, list)
1890 total_uA_load += consumer->uA_load;
1892 mode = rdev->desc->ops->get_optimum_mode(rdev,
1893 input_uV, output_uV,
1895 ret = regulator_check_mode(rdev, mode);
1897 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1898 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
1899 total_uA_load, input_uV, output_uV);
1903 ret = rdev->desc->ops->set_mode(rdev, mode);
1905 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1906 __func__, mode, rdev_get_name(rdev));
1911 mutex_unlock(&rdev->mutex);
1914 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1917 * regulator_register_notifier - register regulator event notifier
1918 * @regulator: regulator source
1919 * @nb: notifier block
1921 * Register notifier block to receive regulator events.
1923 int regulator_register_notifier(struct regulator *regulator,
1924 struct notifier_block *nb)
1926 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1929 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1932 * regulator_unregister_notifier - unregister regulator event notifier
1933 * @regulator: regulator source
1934 * @nb: notifier block
1936 * Unregister regulator event notifier block.
1938 int regulator_unregister_notifier(struct regulator *regulator,
1939 struct notifier_block *nb)
1941 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1944 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1946 /* notify regulator consumers and downstream regulator consumers.
1947 * Note mutex must be held by caller.
1949 static void _notifier_call_chain(struct regulator_dev *rdev,
1950 unsigned long event, void *data)
1952 struct regulator_dev *_rdev;
1954 /* call rdev chain first */
1955 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1957 /* now notify regulator we supply */
1958 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1959 mutex_lock(&_rdev->mutex);
1960 _notifier_call_chain(_rdev, event, data);
1961 mutex_unlock(&_rdev->mutex);
1966 * regulator_bulk_get - get multiple regulator consumers
1968 * @dev: Device to supply
1969 * @num_consumers: Number of consumers to register
1970 * @consumers: Configuration of consumers; clients are stored here.
1972 * @return 0 on success, an errno on failure.
1974 * This helper function allows drivers to get several regulator
1975 * consumers in one operation. If any of the regulators cannot be
1976 * acquired then any regulators that were allocated will be freed
1977 * before returning to the caller.
1979 int regulator_bulk_get(struct device *dev, int num_consumers,
1980 struct regulator_bulk_data *consumers)
1985 for (i = 0; i < num_consumers; i++)
1986 consumers[i].consumer = NULL;
1988 for (i = 0; i < num_consumers; i++) {
1989 consumers[i].consumer = regulator_get(dev,
1990 consumers[i].supply);
1991 if (IS_ERR(consumers[i].consumer)) {
1992 ret = PTR_ERR(consumers[i].consumer);
1993 dev_err(dev, "Failed to get supply '%s': %d\n",
1994 consumers[i].supply, ret);
1995 consumers[i].consumer = NULL;
2003 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2004 regulator_put(consumers[i].consumer);
2008 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2011 * regulator_bulk_enable - enable multiple regulator consumers
2013 * @num_consumers: Number of consumers
2014 * @consumers: Consumer data; clients are stored here.
2015 * @return 0 on success, an errno on failure
2017 * This convenience API allows consumers to enable multiple regulator
2018 * clients in a single API call. If any consumers cannot be enabled
2019 * then any others that were enabled will be disabled again prior to
2022 int regulator_bulk_enable(int num_consumers,
2023 struct regulator_bulk_data *consumers)
2028 for (i = 0; i < num_consumers; i++) {
2029 ret = regulator_enable(consumers[i].consumer);
2037 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
2038 for (--i; i >= 0; --i)
2039 regulator_disable(consumers[i].consumer);
2043 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2046 * regulator_bulk_disable - disable multiple regulator consumers
2048 * @num_consumers: Number of consumers
2049 * @consumers: Consumer data; clients are stored here.
2050 * @return 0 on success, an errno on failure
2052 * This convenience API allows consumers to disable multiple regulator
2053 * clients in a single API call. If any consumers cannot be enabled
2054 * then any others that were disabled will be disabled again prior to
2057 int regulator_bulk_disable(int num_consumers,
2058 struct regulator_bulk_data *consumers)
2063 for (i = 0; i < num_consumers; i++) {
2064 ret = regulator_disable(consumers[i].consumer);
2072 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2074 for (--i; i >= 0; --i)
2075 regulator_enable(consumers[i].consumer);
2079 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2082 * regulator_bulk_free - free multiple regulator consumers
2084 * @num_consumers: Number of consumers
2085 * @consumers: Consumer data; clients are stored here.
2087 * This convenience API allows consumers to free multiple regulator
2088 * clients in a single API call.
2090 void regulator_bulk_free(int num_consumers,
2091 struct regulator_bulk_data *consumers)
2095 for (i = 0; i < num_consumers; i++) {
2096 regulator_put(consumers[i].consumer);
2097 consumers[i].consumer = NULL;
2100 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2103 * regulator_notifier_call_chain - call regulator event notifier
2104 * @rdev: regulator source
2105 * @event: notifier block
2106 * @data: callback-specific data.
2108 * Called by regulator drivers to notify clients a regulator event has
2109 * occurred. We also notify regulator clients downstream.
2110 * Note lock must be held by caller.
2112 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2113 unsigned long event, void *data)
2115 _notifier_call_chain(rdev, event, data);
2119 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2122 * regulator_mode_to_status - convert a regulator mode into a status
2124 * @mode: Mode to convert
2126 * Convert a regulator mode into a status.
2128 int regulator_mode_to_status(unsigned int mode)
2131 case REGULATOR_MODE_FAST:
2132 return REGULATOR_STATUS_FAST;
2133 case REGULATOR_MODE_NORMAL:
2134 return REGULATOR_STATUS_NORMAL;
2135 case REGULATOR_MODE_IDLE:
2136 return REGULATOR_STATUS_IDLE;
2137 case REGULATOR_STATUS_STANDBY:
2138 return REGULATOR_STATUS_STANDBY;
2143 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2146 * To avoid cluttering sysfs (and memory) with useless state, only
2147 * create attributes that can be meaningfully displayed.
2149 static int add_regulator_attributes(struct regulator_dev *rdev)
2151 struct device *dev = &rdev->dev;
2152 struct regulator_ops *ops = rdev->desc->ops;
2155 /* some attributes need specific methods to be displayed */
2156 if (ops->get_voltage) {
2157 status = device_create_file(dev, &dev_attr_microvolts);
2161 if (ops->get_current_limit) {
2162 status = device_create_file(dev, &dev_attr_microamps);
2166 if (ops->get_mode) {
2167 status = device_create_file(dev, &dev_attr_opmode);
2171 if (ops->is_enabled) {
2172 status = device_create_file(dev, &dev_attr_state);
2176 if (ops->get_status) {
2177 status = device_create_file(dev, &dev_attr_status);
2182 /* some attributes are type-specific */
2183 if (rdev->desc->type == REGULATOR_CURRENT) {
2184 status = device_create_file(dev, &dev_attr_requested_microamps);
2189 /* all the other attributes exist to support constraints;
2190 * don't show them if there are no constraints, or if the
2191 * relevant supporting methods are missing.
2193 if (!rdev->constraints)
2196 /* constraints need specific supporting methods */
2197 if (ops->set_voltage) {
2198 status = device_create_file(dev, &dev_attr_min_microvolts);
2201 status = device_create_file(dev, &dev_attr_max_microvolts);
2205 if (ops->set_current_limit) {
2206 status = device_create_file(dev, &dev_attr_min_microamps);
2209 status = device_create_file(dev, &dev_attr_max_microamps);
2214 /* suspend mode constraints need multiple supporting methods */
2215 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2218 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2221 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2224 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2228 if (ops->set_suspend_voltage) {
2229 status = device_create_file(dev,
2230 &dev_attr_suspend_standby_microvolts);
2233 status = device_create_file(dev,
2234 &dev_attr_suspend_mem_microvolts);
2237 status = device_create_file(dev,
2238 &dev_attr_suspend_disk_microvolts);
2243 if (ops->set_suspend_mode) {
2244 status = device_create_file(dev,
2245 &dev_attr_suspend_standby_mode);
2248 status = device_create_file(dev,
2249 &dev_attr_suspend_mem_mode);
2252 status = device_create_file(dev,
2253 &dev_attr_suspend_disk_mode);
2262 * regulator_register - register regulator
2263 * @regulator_desc: regulator to register
2264 * @dev: struct device for the regulator
2265 * @init_data: platform provided init data, passed through by driver
2266 * @driver_data: private regulator data
2268 * Called by regulator drivers to register a regulator.
2269 * Returns 0 on success.
2271 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2272 struct device *dev, struct regulator_init_data *init_data,
2275 static atomic_t regulator_no = ATOMIC_INIT(0);
2276 struct regulator_dev *rdev;
2279 if (regulator_desc == NULL)
2280 return ERR_PTR(-EINVAL);
2282 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2283 return ERR_PTR(-EINVAL);
2285 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2286 regulator_desc->type != REGULATOR_CURRENT)
2287 return ERR_PTR(-EINVAL);
2290 return ERR_PTR(-EINVAL);
2292 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2294 return ERR_PTR(-ENOMEM);
2296 mutex_lock(®ulator_list_mutex);
2298 mutex_init(&rdev->mutex);
2299 rdev->reg_data = driver_data;
2300 rdev->owner = regulator_desc->owner;
2301 rdev->desc = regulator_desc;
2302 INIT_LIST_HEAD(&rdev->consumer_list);
2303 INIT_LIST_HEAD(&rdev->supply_list);
2304 INIT_LIST_HEAD(&rdev->list);
2305 INIT_LIST_HEAD(&rdev->slist);
2306 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2308 /* preform any regulator specific init */
2309 if (init_data->regulator_init) {
2310 ret = init_data->regulator_init(rdev->reg_data);
2315 /* register with sysfs */
2316 rdev->dev.class = ®ulator_class;
2317 rdev->dev.parent = dev;
2318 dev_set_name(&rdev->dev, "regulator.%d",
2319 atomic_inc_return(®ulator_no) - 1);
2320 ret = device_register(&rdev->dev);
2322 put_device(&rdev->dev);
2326 dev_set_drvdata(&rdev->dev, rdev);
2328 /* set regulator constraints */
2329 ret = set_machine_constraints(rdev, &init_data->constraints);
2333 /* add attributes supported by this regulator */
2334 ret = add_regulator_attributes(rdev);
2338 /* set supply regulator if it exists */
2339 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2341 "Supply regulator specified by both name and dev\n");
2345 if (init_data->supply_regulator) {
2346 struct regulator_dev *r;
2349 list_for_each_entry(r, ®ulator_list, list) {
2350 if (strcmp(rdev_get_name(r),
2351 init_data->supply_regulator) == 0) {
2358 dev_err(dev, "Failed to find supply %s\n",
2359 init_data->supply_regulator);
2363 ret = set_supply(rdev, r);
2368 if (init_data->supply_regulator_dev) {
2369 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
2370 ret = set_supply(rdev,
2371 dev_get_drvdata(init_data->supply_regulator_dev));
2376 /* add consumers devices */
2377 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2378 ret = set_consumer_device_supply(rdev,
2379 init_data->consumer_supplies[i].dev,
2380 init_data->consumer_supplies[i].dev_name,
2381 init_data->consumer_supplies[i].supply);
2383 goto unset_supplies;
2386 list_add(&rdev->list, ®ulator_list);
2388 mutex_unlock(®ulator_list_mutex);
2392 unset_regulator_supplies(rdev);
2395 device_unregister(&rdev->dev);
2396 /* device core frees rdev */
2397 rdev = ERR_PTR(ret);
2402 rdev = ERR_PTR(ret);
2405 EXPORT_SYMBOL_GPL(regulator_register);
2408 * regulator_unregister - unregister regulator
2409 * @rdev: regulator to unregister
2411 * Called by regulator drivers to unregister a regulator.
2413 void regulator_unregister(struct regulator_dev *rdev)
2418 mutex_lock(®ulator_list_mutex);
2419 WARN_ON(rdev->open_count);
2420 unset_regulator_supplies(rdev);
2421 list_del(&rdev->list);
2423 sysfs_remove_link(&rdev->dev.kobj, "supply");
2424 device_unregister(&rdev->dev);
2425 mutex_unlock(®ulator_list_mutex);
2427 EXPORT_SYMBOL_GPL(regulator_unregister);
2430 * regulator_suspend_prepare - prepare regulators for system wide suspend
2431 * @state: system suspend state
2433 * Configure each regulator with it's suspend operating parameters for state.
2434 * This will usually be called by machine suspend code prior to supending.
2436 int regulator_suspend_prepare(suspend_state_t state)
2438 struct regulator_dev *rdev;
2441 /* ON is handled by regulator active state */
2442 if (state == PM_SUSPEND_ON)
2445 mutex_lock(®ulator_list_mutex);
2446 list_for_each_entry(rdev, ®ulator_list, list) {
2448 mutex_lock(&rdev->mutex);
2449 ret = suspend_prepare(rdev, state);
2450 mutex_unlock(&rdev->mutex);
2453 printk(KERN_ERR "%s: failed to prepare %s\n",
2454 __func__, rdev_get_name(rdev));
2459 mutex_unlock(®ulator_list_mutex);
2462 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2465 * regulator_has_full_constraints - the system has fully specified constraints
2467 * Calling this function will cause the regulator API to disable all
2468 * regulators which have a zero use count and don't have an always_on
2469 * constraint in a late_initcall.
2471 * The intention is that this will become the default behaviour in a
2472 * future kernel release so users are encouraged to use this facility
2475 void regulator_has_full_constraints(void)
2477 has_full_constraints = 1;
2479 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2482 * rdev_get_drvdata - get rdev regulator driver data
2485 * Get rdev regulator driver private data. This call can be used in the
2486 * regulator driver context.
2488 void *rdev_get_drvdata(struct regulator_dev *rdev)
2490 return rdev->reg_data;
2492 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2495 * regulator_get_drvdata - get regulator driver data
2496 * @regulator: regulator
2498 * Get regulator driver private data. This call can be used in the consumer
2499 * driver context when non API regulator specific functions need to be called.
2501 void *regulator_get_drvdata(struct regulator *regulator)
2503 return regulator->rdev->reg_data;
2505 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2508 * regulator_set_drvdata - set regulator driver data
2509 * @regulator: regulator
2512 void regulator_set_drvdata(struct regulator *regulator, void *data)
2514 regulator->rdev->reg_data = data;
2516 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2519 * regulator_get_id - get regulator ID
2522 int rdev_get_id(struct regulator_dev *rdev)
2524 return rdev->desc->id;
2526 EXPORT_SYMBOL_GPL(rdev_get_id);
2528 struct device *rdev_get_dev(struct regulator_dev *rdev)
2532 EXPORT_SYMBOL_GPL(rdev_get_dev);
2534 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2536 return reg_init_data->driver_data;
2538 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2540 static int __init regulator_init(void)
2544 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2546 ret = class_register(®ulator_class);
2548 regulator_dummy_init();
2553 /* init early to allow our consumers to complete system booting */
2554 core_initcall(regulator_init);
2556 static int __init regulator_init_complete(void)
2558 struct regulator_dev *rdev;
2559 struct regulator_ops *ops;
2560 struct regulation_constraints *c;
2564 mutex_lock(®ulator_list_mutex);
2566 /* If we have a full configuration then disable any regulators
2567 * which are not in use or always_on. This will become the
2568 * default behaviour in the future.
2570 list_for_each_entry(rdev, ®ulator_list, list) {
2571 ops = rdev->desc->ops;
2572 c = rdev->constraints;
2574 name = rdev_get_name(rdev);
2576 if (!ops->disable || (c && c->always_on))
2579 mutex_lock(&rdev->mutex);
2581 if (rdev->use_count)
2584 /* If we can't read the status assume it's on. */
2585 if (ops->is_enabled)
2586 enabled = ops->is_enabled(rdev);
2593 if (has_full_constraints) {
2594 /* We log since this may kill the system if it
2596 printk(KERN_INFO "%s: disabling %s\n",
2598 ret = ops->disable(rdev);
2601 "%s: couldn't disable %s: %d\n",
2602 __func__, name, ret);
2605 /* The intention is that in future we will
2606 * assume that full constraints are provided
2607 * so warn even if we aren't going to do
2611 "%s: incomplete constraints, leaving %s on\n",
2616 mutex_unlock(&rdev->mutex);
2619 mutex_unlock(®ulator_list_mutex);
2623 late_initcall(regulator_init_complete);