2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/mutex.h>
21 #include <linux/suspend.h>
22 #include <linux/regulator/consumer.h>
23 #include <linux/regulator/driver.h>
24 #include <linux/regulator/machine.h>
26 #define REGULATOR_VERSION "0.5"
28 static DEFINE_MUTEX(regulator_list_mutex);
29 static LIST_HEAD(regulator_list);
30 static LIST_HEAD(regulator_map_list);
31 static int has_full_constraints;
34 * struct regulator_map
36 * Used to provide symbolic supply names to devices.
38 struct regulator_map {
39 struct list_head list;
40 const char *dev_name; /* The dev_name() for the consumer */
42 struct regulator_dev *regulator;
48 * One for each consumer device.
52 struct list_head list;
57 struct device_attribute dev_attr;
58 struct regulator_dev *rdev;
61 static int _regulator_is_enabled(struct regulator_dev *rdev);
62 static int _regulator_disable(struct regulator_dev *rdev);
63 static int _regulator_get_voltage(struct regulator_dev *rdev);
64 static int _regulator_get_current_limit(struct regulator_dev *rdev);
65 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
66 static void _notifier_call_chain(struct regulator_dev *rdev,
67 unsigned long event, void *data);
69 /* gets the regulator for a given consumer device */
70 static struct regulator *get_device_regulator(struct device *dev)
72 struct regulator *regulator = NULL;
73 struct regulator_dev *rdev;
75 mutex_lock(®ulator_list_mutex);
76 list_for_each_entry(rdev, ®ulator_list, list) {
77 mutex_lock(&rdev->mutex);
78 list_for_each_entry(regulator, &rdev->consumer_list, list) {
79 if (regulator->dev == dev) {
80 mutex_unlock(&rdev->mutex);
81 mutex_unlock(®ulator_list_mutex);
85 mutex_unlock(&rdev->mutex);
87 mutex_unlock(®ulator_list_mutex);
91 /* Platform voltage constraint check */
92 static int regulator_check_voltage(struct regulator_dev *rdev,
93 int *min_uV, int *max_uV)
95 BUG_ON(*min_uV > *max_uV);
97 if (!rdev->constraints) {
98 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
102 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
103 printk(KERN_ERR "%s: operation not allowed for %s\n",
104 __func__, rdev->desc->name);
108 if (*max_uV > rdev->constraints->max_uV)
109 *max_uV = rdev->constraints->max_uV;
110 if (*min_uV < rdev->constraints->min_uV)
111 *min_uV = rdev->constraints->min_uV;
113 if (*min_uV > *max_uV)
119 /* current constraint check */
120 static int regulator_check_current_limit(struct regulator_dev *rdev,
121 int *min_uA, int *max_uA)
123 BUG_ON(*min_uA > *max_uA);
125 if (!rdev->constraints) {
126 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
130 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
131 printk(KERN_ERR "%s: operation not allowed for %s\n",
132 __func__, rdev->desc->name);
136 if (*max_uA > rdev->constraints->max_uA)
137 *max_uA = rdev->constraints->max_uA;
138 if (*min_uA < rdev->constraints->min_uA)
139 *min_uA = rdev->constraints->min_uA;
141 if (*min_uA > *max_uA)
147 /* operating mode constraint check */
148 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
151 case REGULATOR_MODE_FAST:
152 case REGULATOR_MODE_NORMAL:
153 case REGULATOR_MODE_IDLE:
154 case REGULATOR_MODE_STANDBY:
160 if (!rdev->constraints) {
161 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
165 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
166 printk(KERN_ERR "%s: operation not allowed for %s\n",
167 __func__, rdev->desc->name);
170 if (!(rdev->constraints->valid_modes_mask & mode)) {
171 printk(KERN_ERR "%s: invalid mode %x for %s\n",
172 __func__, mode, rdev->desc->name);
178 /* dynamic regulator mode switching constraint check */
179 static int regulator_check_drms(struct regulator_dev *rdev)
181 if (!rdev->constraints) {
182 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
186 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
187 printk(KERN_ERR "%s: operation not allowed for %s\n",
188 __func__, rdev->desc->name);
194 static ssize_t device_requested_uA_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
197 struct regulator *regulator;
199 regulator = get_device_regulator(dev);
200 if (regulator == NULL)
203 return sprintf(buf, "%d\n", regulator->uA_load);
206 static ssize_t regulator_uV_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
209 struct regulator_dev *rdev = dev_get_drvdata(dev);
212 mutex_lock(&rdev->mutex);
213 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
214 mutex_unlock(&rdev->mutex);
218 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
220 static ssize_t regulator_uA_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
223 struct regulator_dev *rdev = dev_get_drvdata(dev);
225 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
227 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
229 static ssize_t regulator_name_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
232 struct regulator_dev *rdev = dev_get_drvdata(dev);
235 if (rdev->constraints && rdev->constraints->name)
236 name = rdev->constraints->name;
237 else if (rdev->desc->name)
238 name = rdev->desc->name;
242 return sprintf(buf, "%s\n", name);
245 static ssize_t regulator_print_opmode(char *buf, int mode)
248 case REGULATOR_MODE_FAST:
249 return sprintf(buf, "fast\n");
250 case REGULATOR_MODE_NORMAL:
251 return sprintf(buf, "normal\n");
252 case REGULATOR_MODE_IDLE:
253 return sprintf(buf, "idle\n");
254 case REGULATOR_MODE_STANDBY:
255 return sprintf(buf, "standby\n");
257 return sprintf(buf, "unknown\n");
260 static ssize_t regulator_opmode_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
263 struct regulator_dev *rdev = dev_get_drvdata(dev);
265 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
267 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
269 static ssize_t regulator_print_state(char *buf, int state)
272 return sprintf(buf, "enabled\n");
274 return sprintf(buf, "disabled\n");
276 return sprintf(buf, "unknown\n");
279 static ssize_t regulator_state_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
282 struct regulator_dev *rdev = dev_get_drvdata(dev);
285 mutex_lock(&rdev->mutex);
286 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
287 mutex_unlock(&rdev->mutex);
291 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
293 static ssize_t regulator_status_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct regulator_dev *rdev = dev_get_drvdata(dev);
300 status = rdev->desc->ops->get_status(rdev);
305 case REGULATOR_STATUS_OFF:
308 case REGULATOR_STATUS_ON:
311 case REGULATOR_STATUS_ERROR:
314 case REGULATOR_STATUS_FAST:
317 case REGULATOR_STATUS_NORMAL:
320 case REGULATOR_STATUS_IDLE:
323 case REGULATOR_STATUS_STANDBY:
330 return sprintf(buf, "%s\n", label);
332 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
334 static ssize_t regulator_min_uA_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
337 struct regulator_dev *rdev = dev_get_drvdata(dev);
339 if (!rdev->constraints)
340 return sprintf(buf, "constraint not defined\n");
342 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
344 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
346 static ssize_t regulator_max_uA_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
349 struct regulator_dev *rdev = dev_get_drvdata(dev);
351 if (!rdev->constraints)
352 return sprintf(buf, "constraint not defined\n");
354 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
356 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
358 static ssize_t regulator_min_uV_show(struct device *dev,
359 struct device_attribute *attr, char *buf)
361 struct regulator_dev *rdev = dev_get_drvdata(dev);
363 if (!rdev->constraints)
364 return sprintf(buf, "constraint not defined\n");
366 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
368 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
370 static ssize_t regulator_max_uV_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
373 struct regulator_dev *rdev = dev_get_drvdata(dev);
375 if (!rdev->constraints)
376 return sprintf(buf, "constraint not defined\n");
378 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
380 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
382 static ssize_t regulator_total_uA_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
385 struct regulator_dev *rdev = dev_get_drvdata(dev);
386 struct regulator *regulator;
389 mutex_lock(&rdev->mutex);
390 list_for_each_entry(regulator, &rdev->consumer_list, list)
391 uA += regulator->uA_load;
392 mutex_unlock(&rdev->mutex);
393 return sprintf(buf, "%d\n", uA);
395 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
397 static ssize_t regulator_num_users_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
400 struct regulator_dev *rdev = dev_get_drvdata(dev);
401 return sprintf(buf, "%d\n", rdev->use_count);
404 static ssize_t regulator_type_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 struct regulator_dev *rdev = dev_get_drvdata(dev);
409 switch (rdev->desc->type) {
410 case REGULATOR_VOLTAGE:
411 return sprintf(buf, "voltage\n");
412 case REGULATOR_CURRENT:
413 return sprintf(buf, "current\n");
415 return sprintf(buf, "unknown\n");
418 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
419 struct device_attribute *attr, char *buf)
421 struct regulator_dev *rdev = dev_get_drvdata(dev);
423 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
425 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
426 regulator_suspend_mem_uV_show, NULL);
428 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
431 struct regulator_dev *rdev = dev_get_drvdata(dev);
433 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
435 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
436 regulator_suspend_disk_uV_show, NULL);
438 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct regulator_dev *rdev = dev_get_drvdata(dev);
443 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
445 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
446 regulator_suspend_standby_uV_show, NULL);
448 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
451 struct regulator_dev *rdev = dev_get_drvdata(dev);
453 return regulator_print_opmode(buf,
454 rdev->constraints->state_mem.mode);
456 static DEVICE_ATTR(suspend_mem_mode, 0444,
457 regulator_suspend_mem_mode_show, NULL);
459 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
462 struct regulator_dev *rdev = dev_get_drvdata(dev);
464 return regulator_print_opmode(buf,
465 rdev->constraints->state_disk.mode);
467 static DEVICE_ATTR(suspend_disk_mode, 0444,
468 regulator_suspend_disk_mode_show, NULL);
470 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
473 struct regulator_dev *rdev = dev_get_drvdata(dev);
475 return regulator_print_opmode(buf,
476 rdev->constraints->state_standby.mode);
478 static DEVICE_ATTR(suspend_standby_mode, 0444,
479 regulator_suspend_standby_mode_show, NULL);
481 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
484 struct regulator_dev *rdev = dev_get_drvdata(dev);
486 return regulator_print_state(buf,
487 rdev->constraints->state_mem.enabled);
489 static DEVICE_ATTR(suspend_mem_state, 0444,
490 regulator_suspend_mem_state_show, NULL);
492 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
495 struct regulator_dev *rdev = dev_get_drvdata(dev);
497 return regulator_print_state(buf,
498 rdev->constraints->state_disk.enabled);
500 static DEVICE_ATTR(suspend_disk_state, 0444,
501 regulator_suspend_disk_state_show, NULL);
503 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
506 struct regulator_dev *rdev = dev_get_drvdata(dev);
508 return regulator_print_state(buf,
509 rdev->constraints->state_standby.enabled);
511 static DEVICE_ATTR(suspend_standby_state, 0444,
512 regulator_suspend_standby_state_show, NULL);
516 * These are the only attributes are present for all regulators.
517 * Other attributes are a function of regulator functionality.
519 static struct device_attribute regulator_dev_attrs[] = {
520 __ATTR(name, 0444, regulator_name_show, NULL),
521 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
522 __ATTR(type, 0444, regulator_type_show, NULL),
526 static void regulator_dev_release(struct device *dev)
528 struct regulator_dev *rdev = dev_get_drvdata(dev);
532 static struct class regulator_class = {
534 .dev_release = regulator_dev_release,
535 .dev_attrs = regulator_dev_attrs,
538 /* Calculate the new optimum regulator operating mode based on the new total
539 * consumer load. All locks held by caller */
540 static void drms_uA_update(struct regulator_dev *rdev)
542 struct regulator *sibling;
543 int current_uA = 0, output_uV, input_uV, err;
546 err = regulator_check_drms(rdev);
547 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
548 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
551 /* get output voltage */
552 output_uV = rdev->desc->ops->get_voltage(rdev);
556 /* get input voltage */
557 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
558 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
560 input_uV = rdev->constraints->input_uV;
564 /* calc total requested load */
565 list_for_each_entry(sibling, &rdev->consumer_list, list)
566 current_uA += sibling->uA_load;
568 /* now get the optimum mode for our new total regulator load */
569 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
570 output_uV, current_uA);
572 /* check the new mode is allowed */
573 err = regulator_check_mode(rdev, mode);
575 rdev->desc->ops->set_mode(rdev, mode);
578 static int suspend_set_state(struct regulator_dev *rdev,
579 struct regulator_state *rstate)
583 /* enable & disable are mandatory for suspend control */
584 if (!rdev->desc->ops->set_suspend_enable ||
585 !rdev->desc->ops->set_suspend_disable) {
586 printk(KERN_ERR "%s: no way to set suspend state\n",
592 ret = rdev->desc->ops->set_suspend_enable(rdev);
594 ret = rdev->desc->ops->set_suspend_disable(rdev);
596 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
600 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
601 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
603 printk(KERN_ERR "%s: failed to set voltage\n",
609 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
610 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
612 printk(KERN_ERR "%s: failed to set mode\n", __func__);
619 /* locks held by caller */
620 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
622 if (!rdev->constraints)
626 case PM_SUSPEND_STANDBY:
627 return suspend_set_state(rdev,
628 &rdev->constraints->state_standby);
630 return suspend_set_state(rdev,
631 &rdev->constraints->state_mem);
633 return suspend_set_state(rdev,
634 &rdev->constraints->state_disk);
640 static void print_constraints(struct regulator_dev *rdev)
642 struct regulation_constraints *constraints = rdev->constraints;
647 if (constraints->min_uV && constraints->max_uV) {
648 if (constraints->min_uV == constraints->max_uV)
649 count += sprintf(buf + count, "%d mV ",
650 constraints->min_uV / 1000);
652 count += sprintf(buf + count, "%d <--> %d mV ",
653 constraints->min_uV / 1000,
654 constraints->max_uV / 1000);
657 if (!constraints->min_uV ||
658 constraints->min_uV != constraints->max_uV) {
659 ret = _regulator_get_voltage(rdev);
661 count += sprintf(buf + count, "at %d mV ", ret / 1000);
664 if (constraints->min_uA && constraints->max_uA) {
665 if (constraints->min_uA == constraints->max_uA)
666 count += sprintf(buf + count, "%d mA ",
667 constraints->min_uA / 1000);
669 count += sprintf(buf + count, "%d <--> %d mA ",
670 constraints->min_uA / 1000,
671 constraints->max_uA / 1000);
674 if (!constraints->min_uA ||
675 constraints->min_uA != constraints->max_uA) {
676 ret = _regulator_get_current_limit(rdev);
678 count += sprintf(buf + count, "at %d uA ", ret / 1000);
681 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
682 count += sprintf(buf + count, "fast ");
683 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
684 count += sprintf(buf + count, "normal ");
685 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
686 count += sprintf(buf + count, "idle ");
687 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
688 count += sprintf(buf + count, "standby");
690 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
693 static int machine_constraints_voltage(struct regulator_dev *rdev,
694 const char *name, struct regulation_constraints *constraints)
696 struct regulator_ops *ops = rdev->desc->ops;
699 /* do we need to apply the constraint voltage */
700 if (rdev->constraints->apply_uV &&
701 rdev->constraints->min_uV == rdev->constraints->max_uV &&
703 ret = ops->set_voltage(rdev,
704 rdev->constraints->min_uV, rdev->constraints->max_uV);
706 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
708 rdev->constraints->min_uV, name);
709 rdev->constraints = NULL;
714 /* constrain machine-level voltage specs to fit
715 * the actual range supported by this regulator.
717 if (ops->list_voltage && rdev->desc->n_voltages) {
718 int count = rdev->desc->n_voltages;
720 int min_uV = INT_MAX;
721 int max_uV = INT_MIN;
722 int cmin = constraints->min_uV;
723 int cmax = constraints->max_uV;
725 /* it's safe to autoconfigure fixed-voltage supplies
726 and the constraints are used by list_voltage. */
727 if (count == 1 && !cmin) {
730 constraints->min_uV = cmin;
731 constraints->max_uV = cmax;
734 /* voltage constraints are optional */
735 if ((cmin == 0) && (cmax == 0))
738 /* else require explicit machine-level constraints */
739 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
740 pr_err("%s: %s '%s' voltage constraints\n",
741 __func__, "invalid", name);
745 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
746 for (i = 0; i < count; i++) {
749 value = ops->list_voltage(rdev, i);
753 /* maybe adjust [min_uV..max_uV] */
754 if (value >= cmin && value < min_uV)
756 if (value <= cmax && value > max_uV)
760 /* final: [min_uV..max_uV] valid iff constraints valid */
761 if (max_uV < min_uV) {
762 pr_err("%s: %s '%s' voltage constraints\n",
763 __func__, "unsupportable", name);
767 /* use regulator's subset of machine constraints */
768 if (constraints->min_uV < min_uV) {
769 pr_debug("%s: override '%s' %s, %d -> %d\n",
770 __func__, name, "min_uV",
771 constraints->min_uV, min_uV);
772 constraints->min_uV = min_uV;
774 if (constraints->max_uV > max_uV) {
775 pr_debug("%s: override '%s' %s, %d -> %d\n",
776 __func__, name, "max_uV",
777 constraints->max_uV, max_uV);
778 constraints->max_uV = max_uV;
786 * set_machine_constraints - sets regulator constraints
787 * @rdev: regulator source
788 * @constraints: constraints to apply
790 * Allows platform initialisation code to define and constrain
791 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
792 * Constraints *must* be set by platform code in order for some
793 * regulator operations to proceed i.e. set_voltage, set_current_limit,
796 static int set_machine_constraints(struct regulator_dev *rdev,
797 struct regulation_constraints *constraints)
801 struct regulator_ops *ops = rdev->desc->ops;
803 if (constraints->name)
804 name = constraints->name;
805 else if (rdev->desc->name)
806 name = rdev->desc->name;
810 rdev->constraints = constraints;
812 ret = machine_constraints_voltage(rdev, name, constraints);
816 /* do we need to setup our suspend state */
817 if (constraints->initial_state) {
818 ret = suspend_prepare(rdev, constraints->initial_state);
820 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
822 rdev->constraints = NULL;
827 if (constraints->initial_mode) {
828 if (!ops->set_mode) {
829 printk(KERN_ERR "%s: no set_mode operation for %s\n",
835 ret = ops->set_mode(rdev, constraints->initial_mode);
838 "%s: failed to set initial mode for %s: %d\n",
839 __func__, name, ret);
844 /* If the constraints say the regulator should be on at this point
845 * and we have control then make sure it is enabled.
847 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
848 ret = ops->enable(rdev);
850 printk(KERN_ERR "%s: failed to enable %s\n",
852 rdev->constraints = NULL;
857 print_constraints(rdev);
863 * set_supply - set regulator supply regulator
864 * @rdev: regulator name
865 * @supply_rdev: supply regulator name
867 * Called by platform initialisation code to set the supply regulator for this
868 * regulator. This ensures that a regulators supply will also be enabled by the
869 * core if it's child is enabled.
871 static int set_supply(struct regulator_dev *rdev,
872 struct regulator_dev *supply_rdev)
876 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
880 "%s: could not add device link %s err %d\n",
881 __func__, supply_rdev->dev.kobj.name, err);
884 rdev->supply = supply_rdev;
885 list_add(&rdev->slist, &supply_rdev->supply_list);
891 * set_consumer_device_supply: Bind a regulator to a symbolic supply
892 * @rdev: regulator source
893 * @consumer_dev: device the supply applies to
894 * @consumer_dev_name: dev_name() string for device supply applies to
895 * @supply: symbolic name for supply
897 * Allows platform initialisation code to map physical regulator
898 * sources to symbolic names for supplies for use by devices. Devices
899 * should use these symbolic names to request regulators, avoiding the
900 * need to provide board-specific regulator names as platform data.
902 * Only one of consumer_dev and consumer_dev_name may be specified.
904 static int set_consumer_device_supply(struct regulator_dev *rdev,
905 struct device *consumer_dev, const char *consumer_dev_name,
908 struct regulator_map *node;
911 if (consumer_dev && consumer_dev_name)
914 if (!consumer_dev_name && consumer_dev)
915 consumer_dev_name = dev_name(consumer_dev);
920 if (consumer_dev_name != NULL)
925 list_for_each_entry(node, ®ulator_map_list, list) {
926 if (consumer_dev_name != node->dev_name)
928 if (strcmp(node->supply, supply) != 0)
931 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
932 dev_name(&node->regulator->dev),
933 node->regulator->desc->name,
935 dev_name(&rdev->dev), rdev->desc->name);
939 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
943 node->regulator = rdev;
944 node->supply = supply;
947 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
948 if (node->dev_name == NULL) {
954 list_add(&node->list, ®ulator_map_list);
958 static void unset_consumer_device_supply(struct regulator_dev *rdev,
959 const char *consumer_dev_name, struct device *consumer_dev)
961 struct regulator_map *node, *n;
963 if (consumer_dev && !consumer_dev_name)
964 consumer_dev_name = dev_name(consumer_dev);
966 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
967 if (rdev != node->regulator)
970 if (consumer_dev_name && node->dev_name &&
971 strcmp(consumer_dev_name, node->dev_name))
974 list_del(&node->list);
975 kfree(node->dev_name);
981 static void unset_regulator_supplies(struct regulator_dev *rdev)
983 struct regulator_map *node, *n;
985 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
986 if (rdev == node->regulator) {
987 list_del(&node->list);
988 kfree(node->dev_name);
995 #define REG_STR_SIZE 32
997 static struct regulator *create_regulator(struct regulator_dev *rdev,
999 const char *supply_name)
1001 struct regulator *regulator;
1002 char buf[REG_STR_SIZE];
1005 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1006 if (regulator == NULL)
1009 mutex_lock(&rdev->mutex);
1010 regulator->rdev = rdev;
1011 list_add(®ulator->list, &rdev->consumer_list);
1014 /* create a 'requested_microamps_name' sysfs entry */
1015 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1017 if (size >= REG_STR_SIZE)
1020 regulator->dev = dev;
1021 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1022 if (regulator->dev_attr.attr.name == NULL)
1025 regulator->dev_attr.attr.owner = THIS_MODULE;
1026 regulator->dev_attr.attr.mode = 0444;
1027 regulator->dev_attr.show = device_requested_uA_show;
1028 err = device_create_file(dev, ®ulator->dev_attr);
1030 printk(KERN_WARNING "%s: could not add regulator_dev"
1031 " load sysfs\n", __func__);
1035 /* also add a link to the device sysfs entry */
1036 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1037 dev->kobj.name, supply_name);
1038 if (size >= REG_STR_SIZE)
1041 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1042 if (regulator->supply_name == NULL)
1045 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1049 "%s: could not add device link %s err %d\n",
1050 __func__, dev->kobj.name, err);
1051 device_remove_file(dev, ®ulator->dev_attr);
1055 mutex_unlock(&rdev->mutex);
1058 kfree(regulator->supply_name);
1060 device_remove_file(regulator->dev, ®ulator->dev_attr);
1062 kfree(regulator->dev_attr.attr.name);
1064 list_del(®ulator->list);
1066 mutex_unlock(&rdev->mutex);
1070 /* Internal regulator request function */
1071 static struct regulator *_regulator_get(struct device *dev, const char *id,
1074 struct regulator_dev *rdev;
1075 struct regulator_map *map;
1076 struct regulator *regulator = ERR_PTR(-ENODEV);
1077 const char *devname = NULL;
1081 printk(KERN_ERR "regulator: get() with no identifier\n");
1086 devname = dev_name(dev);
1088 mutex_lock(®ulator_list_mutex);
1090 list_for_each_entry(map, ®ulator_map_list, list) {
1091 /* If the mapping has a device set up it must match */
1092 if (map->dev_name &&
1093 (!devname || strcmp(map->dev_name, devname)))
1096 if (strcmp(map->supply, id) == 0) {
1097 rdev = map->regulator;
1101 mutex_unlock(®ulator_list_mutex);
1105 if (rdev->exclusive) {
1106 regulator = ERR_PTR(-EPERM);
1110 if (exclusive && rdev->open_count) {
1111 regulator = ERR_PTR(-EBUSY);
1115 if (!try_module_get(rdev->owner))
1118 regulator = create_regulator(rdev, dev, id);
1119 if (regulator == NULL) {
1120 regulator = ERR_PTR(-ENOMEM);
1121 module_put(rdev->owner);
1126 rdev->exclusive = 1;
1128 ret = _regulator_is_enabled(rdev);
1130 rdev->use_count = 1;
1132 rdev->use_count = 0;
1136 mutex_unlock(®ulator_list_mutex);
1142 * regulator_get - lookup and obtain a reference to a regulator.
1143 * @dev: device for regulator "consumer"
1144 * @id: Supply name or regulator ID.
1146 * Returns a struct regulator corresponding to the regulator producer,
1147 * or IS_ERR() condition containing errno.
1149 * Use of supply names configured via regulator_set_device_supply() is
1150 * strongly encouraged. It is recommended that the supply name used
1151 * should match the name used for the supply and/or the relevant
1152 * device pins in the datasheet.
1154 struct regulator *regulator_get(struct device *dev, const char *id)
1156 return _regulator_get(dev, id, 0);
1158 EXPORT_SYMBOL_GPL(regulator_get);
1161 * regulator_get_exclusive - obtain exclusive access to a regulator.
1162 * @dev: device for regulator "consumer"
1163 * @id: Supply name or regulator ID.
1165 * Returns a struct regulator corresponding to the regulator producer,
1166 * or IS_ERR() condition containing errno. Other consumers will be
1167 * unable to obtain this reference is held and the use count for the
1168 * regulator will be initialised to reflect the current state of the
1171 * This is intended for use by consumers which cannot tolerate shared
1172 * use of the regulator such as those which need to force the
1173 * regulator off for correct operation of the hardware they are
1176 * Use of supply names configured via regulator_set_device_supply() is
1177 * strongly encouraged. It is recommended that the supply name used
1178 * should match the name used for the supply and/or the relevant
1179 * device pins in the datasheet.
1181 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1183 return _regulator_get(dev, id, 1);
1185 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1188 * regulator_put - "free" the regulator source
1189 * @regulator: regulator source
1191 * Note: drivers must ensure that all regulator_enable calls made on this
1192 * regulator source are balanced by regulator_disable calls prior to calling
1195 void regulator_put(struct regulator *regulator)
1197 struct regulator_dev *rdev;
1199 if (regulator == NULL || IS_ERR(regulator))
1202 mutex_lock(®ulator_list_mutex);
1203 rdev = regulator->rdev;
1205 /* remove any sysfs entries */
1206 if (regulator->dev) {
1207 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1208 kfree(regulator->supply_name);
1209 device_remove_file(regulator->dev, ®ulator->dev_attr);
1210 kfree(regulator->dev_attr.attr.name);
1212 list_del(®ulator->list);
1216 rdev->exclusive = 0;
1218 module_put(rdev->owner);
1219 mutex_unlock(®ulator_list_mutex);
1221 EXPORT_SYMBOL_GPL(regulator_put);
1223 static int _regulator_can_change_status(struct regulator_dev *rdev)
1225 if (!rdev->constraints)
1228 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1234 /* locks held by regulator_enable() */
1235 static int _regulator_enable(struct regulator_dev *rdev)
1239 /* do we need to enable the supply regulator first */
1241 ret = _regulator_enable(rdev->supply);
1243 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1244 __func__, rdev->desc->name, ret);
1249 /* check voltage and requested load before enabling */
1250 if (rdev->constraints &&
1251 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1252 drms_uA_update(rdev);
1254 if (rdev->use_count == 0) {
1255 /* The regulator may on if it's not switchable or left on */
1256 ret = _regulator_is_enabled(rdev);
1257 if (ret == -EINVAL || ret == 0) {
1258 if (!_regulator_can_change_status(rdev))
1261 if (rdev->desc->ops->enable) {
1262 ret = rdev->desc->ops->enable(rdev);
1268 } else if (ret < 0) {
1269 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
1270 __func__, rdev->desc->name, ret);
1273 /* Fallthrough on positive return values - already enabled */
1282 * regulator_enable - enable regulator output
1283 * @regulator: regulator source
1285 * Request that the regulator be enabled with the regulator output at
1286 * the predefined voltage or current value. Calls to regulator_enable()
1287 * must be balanced with calls to regulator_disable().
1289 * NOTE: the output value can be set by other drivers, boot loader or may be
1290 * hardwired in the regulator.
1292 int regulator_enable(struct regulator *regulator)
1294 struct regulator_dev *rdev = regulator->rdev;
1297 mutex_lock(&rdev->mutex);
1298 ret = _regulator_enable(rdev);
1299 mutex_unlock(&rdev->mutex);
1302 EXPORT_SYMBOL_GPL(regulator_enable);
1304 /* locks held by regulator_disable() */
1305 static int _regulator_disable(struct regulator_dev *rdev)
1309 if (WARN(rdev->use_count <= 0,
1310 "unbalanced disables for %s\n",
1314 /* are we the last user and permitted to disable ? */
1315 if (rdev->use_count == 1 &&
1316 (rdev->constraints && !rdev->constraints->always_on)) {
1318 /* we are last user */
1319 if (_regulator_can_change_status(rdev) &&
1320 rdev->desc->ops->disable) {
1321 ret = rdev->desc->ops->disable(rdev);
1323 printk(KERN_ERR "%s: failed to disable %s\n",
1324 __func__, rdev->desc->name);
1329 /* decrease our supplies ref count and disable if required */
1331 _regulator_disable(rdev->supply);
1333 rdev->use_count = 0;
1334 } else if (rdev->use_count > 1) {
1336 if (rdev->constraints &&
1337 (rdev->constraints->valid_ops_mask &
1338 REGULATOR_CHANGE_DRMS))
1339 drms_uA_update(rdev);
1347 * regulator_disable - disable regulator output
1348 * @regulator: regulator source
1350 * Disable the regulator output voltage or current. Calls to
1351 * regulator_enable() must be balanced with calls to
1352 * regulator_disable().
1354 * NOTE: this will only disable the regulator output if no other consumer
1355 * devices have it enabled, the regulator device supports disabling and
1356 * machine constraints permit this operation.
1358 int regulator_disable(struct regulator *regulator)
1360 struct regulator_dev *rdev = regulator->rdev;
1363 mutex_lock(&rdev->mutex);
1364 ret = _regulator_disable(rdev);
1365 mutex_unlock(&rdev->mutex);
1368 EXPORT_SYMBOL_GPL(regulator_disable);
1370 /* locks held by regulator_force_disable() */
1371 static int _regulator_force_disable(struct regulator_dev *rdev)
1376 if (rdev->desc->ops->disable) {
1377 /* ah well, who wants to live forever... */
1378 ret = rdev->desc->ops->disable(rdev);
1380 printk(KERN_ERR "%s: failed to force disable %s\n",
1381 __func__, rdev->desc->name);
1384 /* notify other consumers that power has been forced off */
1385 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1389 /* decrease our supplies ref count and disable if required */
1391 _regulator_disable(rdev->supply);
1393 rdev->use_count = 0;
1398 * regulator_force_disable - force disable regulator output
1399 * @regulator: regulator source
1401 * Forcibly disable the regulator output voltage or current.
1402 * NOTE: this *will* disable the regulator output even if other consumer
1403 * devices have it enabled. This should be used for situations when device
1404 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1406 int regulator_force_disable(struct regulator *regulator)
1410 mutex_lock(®ulator->rdev->mutex);
1411 regulator->uA_load = 0;
1412 ret = _regulator_force_disable(regulator->rdev);
1413 mutex_unlock(®ulator->rdev->mutex);
1416 EXPORT_SYMBOL_GPL(regulator_force_disable);
1418 static int _regulator_is_enabled(struct regulator_dev *rdev)
1421 if (!rdev->desc->ops->is_enabled)
1424 return rdev->desc->ops->is_enabled(rdev);
1428 * regulator_is_enabled - is the regulator output enabled
1429 * @regulator: regulator source
1431 * Returns positive if the regulator driver backing the source/client
1432 * has requested that the device be enabled, zero if it hasn't, else a
1433 * negative errno code.
1435 * Note that the device backing this regulator handle can have multiple
1436 * users, so it might be enabled even if regulator_enable() was never
1437 * called for this particular source.
1439 int regulator_is_enabled(struct regulator *regulator)
1443 mutex_lock(®ulator->rdev->mutex);
1444 ret = _regulator_is_enabled(regulator->rdev);
1445 mutex_unlock(®ulator->rdev->mutex);
1449 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1452 * regulator_count_voltages - count regulator_list_voltage() selectors
1453 * @regulator: regulator source
1455 * Returns number of selectors, or negative errno. Selectors are
1456 * numbered starting at zero, and typically correspond to bitfields
1457 * in hardware registers.
1459 int regulator_count_voltages(struct regulator *regulator)
1461 struct regulator_dev *rdev = regulator->rdev;
1463 return rdev->desc->n_voltages ? : -EINVAL;
1465 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1468 * regulator_list_voltage - enumerate supported voltages
1469 * @regulator: regulator source
1470 * @selector: identify voltage to list
1471 * Context: can sleep
1473 * Returns a voltage that can be passed to @regulator_set_voltage(),
1474 * zero if this selector code can't be used on this sytem, or a
1477 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1479 struct regulator_dev *rdev = regulator->rdev;
1480 struct regulator_ops *ops = rdev->desc->ops;
1483 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1486 mutex_lock(&rdev->mutex);
1487 ret = ops->list_voltage(rdev, selector);
1488 mutex_unlock(&rdev->mutex);
1491 if (ret < rdev->constraints->min_uV)
1493 else if (ret > rdev->constraints->max_uV)
1499 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1502 * regulator_is_supported_voltage - check if a voltage range can be supported
1504 * @regulator: Regulator to check.
1505 * @min_uV: Minimum required voltage in uV.
1506 * @max_uV: Maximum required voltage in uV.
1508 * Returns a boolean or a negative error code.
1510 int regulator_is_supported_voltage(struct regulator *regulator,
1511 int min_uV, int max_uV)
1513 int i, voltages, ret;
1515 ret = regulator_count_voltages(regulator);
1520 for (i = 0; i < voltages; i++) {
1521 ret = regulator_list_voltage(regulator, i);
1523 if (ret >= min_uV && ret <= max_uV)
1531 * regulator_set_voltage - set regulator output voltage
1532 * @regulator: regulator source
1533 * @min_uV: Minimum required voltage in uV
1534 * @max_uV: Maximum acceptable voltage in uV
1536 * Sets a voltage regulator to the desired output voltage. This can be set
1537 * during any regulator state. IOW, regulator can be disabled or enabled.
1539 * If the regulator is enabled then the voltage will change to the new value
1540 * immediately otherwise if the regulator is disabled the regulator will
1541 * output at the new voltage when enabled.
1543 * NOTE: If the regulator is shared between several devices then the lowest
1544 * request voltage that meets the system constraints will be used.
1545 * Regulator system constraints must be set for this regulator before
1546 * calling this function otherwise this call will fail.
1548 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1550 struct regulator_dev *rdev = regulator->rdev;
1553 mutex_lock(&rdev->mutex);
1556 if (!rdev->desc->ops->set_voltage) {
1561 /* constraints check */
1562 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1565 regulator->min_uV = min_uV;
1566 regulator->max_uV = max_uV;
1567 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1570 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1571 mutex_unlock(&rdev->mutex);
1574 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1576 static int _regulator_get_voltage(struct regulator_dev *rdev)
1579 if (rdev->desc->ops->get_voltage)
1580 return rdev->desc->ops->get_voltage(rdev);
1586 * regulator_get_voltage - get regulator output voltage
1587 * @regulator: regulator source
1589 * This returns the current regulator voltage in uV.
1591 * NOTE: If the regulator is disabled it will return the voltage value. This
1592 * function should not be used to determine regulator state.
1594 int regulator_get_voltage(struct regulator *regulator)
1598 mutex_lock(®ulator->rdev->mutex);
1600 ret = _regulator_get_voltage(regulator->rdev);
1602 mutex_unlock(®ulator->rdev->mutex);
1606 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1609 * regulator_set_current_limit - set regulator output current limit
1610 * @regulator: regulator source
1611 * @min_uA: Minimuum supported current in uA
1612 * @max_uA: Maximum supported current in uA
1614 * Sets current sink to the desired output current. This can be set during
1615 * any regulator state. IOW, regulator can be disabled or enabled.
1617 * If the regulator is enabled then the current will change to the new value
1618 * immediately otherwise if the regulator is disabled the regulator will
1619 * output at the new current when enabled.
1621 * NOTE: Regulator system constraints must be set for this regulator before
1622 * calling this function otherwise this call will fail.
1624 int regulator_set_current_limit(struct regulator *regulator,
1625 int min_uA, int max_uA)
1627 struct regulator_dev *rdev = regulator->rdev;
1630 mutex_lock(&rdev->mutex);
1633 if (!rdev->desc->ops->set_current_limit) {
1638 /* constraints check */
1639 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1643 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1645 mutex_unlock(&rdev->mutex);
1648 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1650 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1654 mutex_lock(&rdev->mutex);
1657 if (!rdev->desc->ops->get_current_limit) {
1662 ret = rdev->desc->ops->get_current_limit(rdev);
1664 mutex_unlock(&rdev->mutex);
1669 * regulator_get_current_limit - get regulator output current
1670 * @regulator: regulator source
1672 * This returns the current supplied by the specified current sink in uA.
1674 * NOTE: If the regulator is disabled it will return the current value. This
1675 * function should not be used to determine regulator state.
1677 int regulator_get_current_limit(struct regulator *regulator)
1679 return _regulator_get_current_limit(regulator->rdev);
1681 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1684 * regulator_set_mode - set regulator operating mode
1685 * @regulator: regulator source
1686 * @mode: operating mode - one of the REGULATOR_MODE constants
1688 * Set regulator operating mode to increase regulator efficiency or improve
1689 * regulation performance.
1691 * NOTE: Regulator system constraints must be set for this regulator before
1692 * calling this function otherwise this call will fail.
1694 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1696 struct regulator_dev *rdev = regulator->rdev;
1699 mutex_lock(&rdev->mutex);
1702 if (!rdev->desc->ops->set_mode) {
1707 /* constraints check */
1708 ret = regulator_check_mode(rdev, mode);
1712 ret = rdev->desc->ops->set_mode(rdev, mode);
1714 mutex_unlock(&rdev->mutex);
1717 EXPORT_SYMBOL_GPL(regulator_set_mode);
1719 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1723 mutex_lock(&rdev->mutex);
1726 if (!rdev->desc->ops->get_mode) {
1731 ret = rdev->desc->ops->get_mode(rdev);
1733 mutex_unlock(&rdev->mutex);
1738 * regulator_get_mode - get regulator operating mode
1739 * @regulator: regulator source
1741 * Get the current regulator operating mode.
1743 unsigned int regulator_get_mode(struct regulator *regulator)
1745 return _regulator_get_mode(regulator->rdev);
1747 EXPORT_SYMBOL_GPL(regulator_get_mode);
1750 * regulator_set_optimum_mode - set regulator optimum operating mode
1751 * @regulator: regulator source
1752 * @uA_load: load current
1754 * Notifies the regulator core of a new device load. This is then used by
1755 * DRMS (if enabled by constraints) to set the most efficient regulator
1756 * operating mode for the new regulator loading.
1758 * Consumer devices notify their supply regulator of the maximum power
1759 * they will require (can be taken from device datasheet in the power
1760 * consumption tables) when they change operational status and hence power
1761 * state. Examples of operational state changes that can affect power
1762 * consumption are :-
1764 * o Device is opened / closed.
1765 * o Device I/O is about to begin or has just finished.
1766 * o Device is idling in between work.
1768 * This information is also exported via sysfs to userspace.
1770 * DRMS will sum the total requested load on the regulator and change
1771 * to the most efficient operating mode if platform constraints allow.
1773 * Returns the new regulator mode or error.
1775 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1777 struct regulator_dev *rdev = regulator->rdev;
1778 struct regulator *consumer;
1779 int ret, output_uV, input_uV, total_uA_load = 0;
1782 mutex_lock(&rdev->mutex);
1784 regulator->uA_load = uA_load;
1785 ret = regulator_check_drms(rdev);
1791 if (!rdev->desc->ops->get_optimum_mode)
1794 /* get output voltage */
1795 output_uV = rdev->desc->ops->get_voltage(rdev);
1796 if (output_uV <= 0) {
1797 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1798 __func__, rdev->desc->name);
1802 /* get input voltage */
1803 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1804 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1806 input_uV = rdev->constraints->input_uV;
1807 if (input_uV <= 0) {
1808 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1809 __func__, rdev->desc->name);
1813 /* calc total requested load for this regulator */
1814 list_for_each_entry(consumer, &rdev->consumer_list, list)
1815 total_uA_load += consumer->uA_load;
1817 mode = rdev->desc->ops->get_optimum_mode(rdev,
1818 input_uV, output_uV,
1820 ret = regulator_check_mode(rdev, mode);
1822 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1823 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1824 total_uA_load, input_uV, output_uV);
1828 ret = rdev->desc->ops->set_mode(rdev, mode);
1830 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1831 __func__, mode, rdev->desc->name);
1836 mutex_unlock(&rdev->mutex);
1839 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1842 * regulator_register_notifier - register regulator event notifier
1843 * @regulator: regulator source
1844 * @nb: notifier block
1846 * Register notifier block to receive regulator events.
1848 int regulator_register_notifier(struct regulator *regulator,
1849 struct notifier_block *nb)
1851 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1854 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1857 * regulator_unregister_notifier - unregister regulator event notifier
1858 * @regulator: regulator source
1859 * @nb: notifier block
1861 * Unregister regulator event notifier block.
1863 int regulator_unregister_notifier(struct regulator *regulator,
1864 struct notifier_block *nb)
1866 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1869 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1871 /* notify regulator consumers and downstream regulator consumers.
1872 * Note mutex must be held by caller.
1874 static void _notifier_call_chain(struct regulator_dev *rdev,
1875 unsigned long event, void *data)
1877 struct regulator_dev *_rdev;
1879 /* call rdev chain first */
1880 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1882 /* now notify regulator we supply */
1883 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1884 mutex_lock(&_rdev->mutex);
1885 _notifier_call_chain(_rdev, event, data);
1886 mutex_unlock(&_rdev->mutex);
1891 * regulator_bulk_get - get multiple regulator consumers
1893 * @dev: Device to supply
1894 * @num_consumers: Number of consumers to register
1895 * @consumers: Configuration of consumers; clients are stored here.
1897 * @return 0 on success, an errno on failure.
1899 * This helper function allows drivers to get several regulator
1900 * consumers in one operation. If any of the regulators cannot be
1901 * acquired then any regulators that were allocated will be freed
1902 * before returning to the caller.
1904 int regulator_bulk_get(struct device *dev, int num_consumers,
1905 struct regulator_bulk_data *consumers)
1910 for (i = 0; i < num_consumers; i++)
1911 consumers[i].consumer = NULL;
1913 for (i = 0; i < num_consumers; i++) {
1914 consumers[i].consumer = regulator_get(dev,
1915 consumers[i].supply);
1916 if (IS_ERR(consumers[i].consumer)) {
1917 ret = PTR_ERR(consumers[i].consumer);
1918 dev_err(dev, "Failed to get supply '%s': %d\n",
1919 consumers[i].supply, ret);
1920 consumers[i].consumer = NULL;
1928 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1929 regulator_put(consumers[i].consumer);
1933 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1936 * regulator_bulk_enable - enable multiple regulator consumers
1938 * @num_consumers: Number of consumers
1939 * @consumers: Consumer data; clients are stored here.
1940 * @return 0 on success, an errno on failure
1942 * This convenience API allows consumers to enable multiple regulator
1943 * clients in a single API call. If any consumers cannot be enabled
1944 * then any others that were enabled will be disabled again prior to
1947 int regulator_bulk_enable(int num_consumers,
1948 struct regulator_bulk_data *consumers)
1953 for (i = 0; i < num_consumers; i++) {
1954 ret = regulator_enable(consumers[i].consumer);
1962 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
1963 for (i = 0; i < num_consumers; i++)
1964 regulator_disable(consumers[i].consumer);
1968 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1971 * regulator_bulk_disable - disable multiple regulator consumers
1973 * @num_consumers: Number of consumers
1974 * @consumers: Consumer data; clients are stored here.
1975 * @return 0 on success, an errno on failure
1977 * This convenience API allows consumers to disable multiple regulator
1978 * clients in a single API call. If any consumers cannot be enabled
1979 * then any others that were disabled will be disabled again prior to
1982 int regulator_bulk_disable(int num_consumers,
1983 struct regulator_bulk_data *consumers)
1988 for (i = 0; i < num_consumers; i++) {
1989 ret = regulator_disable(consumers[i].consumer);
1997 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
1999 for (i = 0; i < num_consumers; i++)
2000 regulator_enable(consumers[i].consumer);
2004 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2007 * regulator_bulk_free - free multiple regulator consumers
2009 * @num_consumers: Number of consumers
2010 * @consumers: Consumer data; clients are stored here.
2012 * This convenience API allows consumers to free multiple regulator
2013 * clients in a single API call.
2015 void regulator_bulk_free(int num_consumers,
2016 struct regulator_bulk_data *consumers)
2020 for (i = 0; i < num_consumers; i++) {
2021 regulator_put(consumers[i].consumer);
2022 consumers[i].consumer = NULL;
2025 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2028 * regulator_notifier_call_chain - call regulator event notifier
2029 * @rdev: regulator source
2030 * @event: notifier block
2031 * @data: callback-specific data.
2033 * Called by regulator drivers to notify clients a regulator event has
2034 * occurred. We also notify regulator clients downstream.
2035 * Note lock must be held by caller.
2037 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2038 unsigned long event, void *data)
2040 _notifier_call_chain(rdev, event, data);
2044 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2047 * regulator_mode_to_status - convert a regulator mode into a status
2049 * @mode: Mode to convert
2051 * Convert a regulator mode into a status.
2053 int regulator_mode_to_status(unsigned int mode)
2056 case REGULATOR_MODE_FAST:
2057 return REGULATOR_STATUS_FAST;
2058 case REGULATOR_MODE_NORMAL:
2059 return REGULATOR_STATUS_NORMAL;
2060 case REGULATOR_MODE_IDLE:
2061 return REGULATOR_STATUS_IDLE;
2062 case REGULATOR_STATUS_STANDBY:
2063 return REGULATOR_STATUS_STANDBY;
2068 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2071 * To avoid cluttering sysfs (and memory) with useless state, only
2072 * create attributes that can be meaningfully displayed.
2074 static int add_regulator_attributes(struct regulator_dev *rdev)
2076 struct device *dev = &rdev->dev;
2077 struct regulator_ops *ops = rdev->desc->ops;
2080 /* some attributes need specific methods to be displayed */
2081 if (ops->get_voltage) {
2082 status = device_create_file(dev, &dev_attr_microvolts);
2086 if (ops->get_current_limit) {
2087 status = device_create_file(dev, &dev_attr_microamps);
2091 if (ops->get_mode) {
2092 status = device_create_file(dev, &dev_attr_opmode);
2096 if (ops->is_enabled) {
2097 status = device_create_file(dev, &dev_attr_state);
2101 if (ops->get_status) {
2102 status = device_create_file(dev, &dev_attr_status);
2107 /* some attributes are type-specific */
2108 if (rdev->desc->type == REGULATOR_CURRENT) {
2109 status = device_create_file(dev, &dev_attr_requested_microamps);
2114 /* all the other attributes exist to support constraints;
2115 * don't show them if there are no constraints, or if the
2116 * relevant supporting methods are missing.
2118 if (!rdev->constraints)
2121 /* constraints need specific supporting methods */
2122 if (ops->set_voltage) {
2123 status = device_create_file(dev, &dev_attr_min_microvolts);
2126 status = device_create_file(dev, &dev_attr_max_microvolts);
2130 if (ops->set_current_limit) {
2131 status = device_create_file(dev, &dev_attr_min_microamps);
2134 status = device_create_file(dev, &dev_attr_max_microamps);
2139 /* suspend mode constraints need multiple supporting methods */
2140 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2143 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2146 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2149 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2153 if (ops->set_suspend_voltage) {
2154 status = device_create_file(dev,
2155 &dev_attr_suspend_standby_microvolts);
2158 status = device_create_file(dev,
2159 &dev_attr_suspend_mem_microvolts);
2162 status = device_create_file(dev,
2163 &dev_attr_suspend_disk_microvolts);
2168 if (ops->set_suspend_mode) {
2169 status = device_create_file(dev,
2170 &dev_attr_suspend_standby_mode);
2173 status = device_create_file(dev,
2174 &dev_attr_suspend_mem_mode);
2177 status = device_create_file(dev,
2178 &dev_attr_suspend_disk_mode);
2187 * regulator_register - register regulator
2188 * @regulator_desc: regulator to register
2189 * @dev: struct device for the regulator
2190 * @init_data: platform provided init data, passed through by driver
2191 * @driver_data: private regulator data
2193 * Called by regulator drivers to register a regulator.
2194 * Returns 0 on success.
2196 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2197 struct device *dev, struct regulator_init_data *init_data,
2200 static atomic_t regulator_no = ATOMIC_INIT(0);
2201 struct regulator_dev *rdev;
2204 if (regulator_desc == NULL)
2205 return ERR_PTR(-EINVAL);
2207 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2208 return ERR_PTR(-EINVAL);
2210 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2211 regulator_desc->type != REGULATOR_CURRENT)
2212 return ERR_PTR(-EINVAL);
2215 return ERR_PTR(-EINVAL);
2217 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2219 return ERR_PTR(-ENOMEM);
2221 mutex_lock(®ulator_list_mutex);
2223 mutex_init(&rdev->mutex);
2224 rdev->reg_data = driver_data;
2225 rdev->owner = regulator_desc->owner;
2226 rdev->desc = regulator_desc;
2227 INIT_LIST_HEAD(&rdev->consumer_list);
2228 INIT_LIST_HEAD(&rdev->supply_list);
2229 INIT_LIST_HEAD(&rdev->list);
2230 INIT_LIST_HEAD(&rdev->slist);
2231 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2233 /* preform any regulator specific init */
2234 if (init_data->regulator_init) {
2235 ret = init_data->regulator_init(rdev->reg_data);
2240 /* register with sysfs */
2241 rdev->dev.class = ®ulator_class;
2242 rdev->dev.parent = dev;
2243 dev_set_name(&rdev->dev, "regulator.%d",
2244 atomic_inc_return(®ulator_no) - 1);
2245 ret = device_register(&rdev->dev);
2249 dev_set_drvdata(&rdev->dev, rdev);
2251 /* set regulator constraints */
2252 ret = set_machine_constraints(rdev, &init_data->constraints);
2256 /* add attributes supported by this regulator */
2257 ret = add_regulator_attributes(rdev);
2261 /* set supply regulator if it exists */
2262 if (init_data->supply_regulator_dev) {
2263 ret = set_supply(rdev,
2264 dev_get_drvdata(init_data->supply_regulator_dev));
2269 /* add consumers devices */
2270 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2271 ret = set_consumer_device_supply(rdev,
2272 init_data->consumer_supplies[i].dev,
2273 init_data->consumer_supplies[i].dev_name,
2274 init_data->consumer_supplies[i].supply);
2276 for (--i; i >= 0; i--)
2277 unset_consumer_device_supply(rdev,
2278 init_data->consumer_supplies[i].dev_name,
2279 init_data->consumer_supplies[i].dev);
2284 list_add(&rdev->list, ®ulator_list);
2286 mutex_unlock(®ulator_list_mutex);
2290 device_unregister(&rdev->dev);
2291 /* device core frees rdev */
2292 rdev = ERR_PTR(ret);
2297 rdev = ERR_PTR(ret);
2300 EXPORT_SYMBOL_GPL(regulator_register);
2303 * regulator_unregister - unregister regulator
2304 * @rdev: regulator to unregister
2306 * Called by regulator drivers to unregister a regulator.
2308 void regulator_unregister(struct regulator_dev *rdev)
2313 mutex_lock(®ulator_list_mutex);
2314 WARN_ON(rdev->open_count);
2315 unset_regulator_supplies(rdev);
2316 list_del(&rdev->list);
2318 sysfs_remove_link(&rdev->dev.kobj, "supply");
2319 device_unregister(&rdev->dev);
2320 mutex_unlock(®ulator_list_mutex);
2322 EXPORT_SYMBOL_GPL(regulator_unregister);
2325 * regulator_suspend_prepare - prepare regulators for system wide suspend
2326 * @state: system suspend state
2328 * Configure each regulator with it's suspend operating parameters for state.
2329 * This will usually be called by machine suspend code prior to supending.
2331 int regulator_suspend_prepare(suspend_state_t state)
2333 struct regulator_dev *rdev;
2336 /* ON is handled by regulator active state */
2337 if (state == PM_SUSPEND_ON)
2340 mutex_lock(®ulator_list_mutex);
2341 list_for_each_entry(rdev, ®ulator_list, list) {
2343 mutex_lock(&rdev->mutex);
2344 ret = suspend_prepare(rdev, state);
2345 mutex_unlock(&rdev->mutex);
2348 printk(KERN_ERR "%s: failed to prepare %s\n",
2349 __func__, rdev->desc->name);
2354 mutex_unlock(®ulator_list_mutex);
2357 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2360 * regulator_has_full_constraints - the system has fully specified constraints
2362 * Calling this function will cause the regulator API to disable all
2363 * regulators which have a zero use count and don't have an always_on
2364 * constraint in a late_initcall.
2366 * The intention is that this will become the default behaviour in a
2367 * future kernel release so users are encouraged to use this facility
2370 void regulator_has_full_constraints(void)
2372 has_full_constraints = 1;
2374 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2377 * rdev_get_drvdata - get rdev regulator driver data
2380 * Get rdev regulator driver private data. This call can be used in the
2381 * regulator driver context.
2383 void *rdev_get_drvdata(struct regulator_dev *rdev)
2385 return rdev->reg_data;
2387 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2390 * regulator_get_drvdata - get regulator driver data
2391 * @regulator: regulator
2393 * Get regulator driver private data. This call can be used in the consumer
2394 * driver context when non API regulator specific functions need to be called.
2396 void *regulator_get_drvdata(struct regulator *regulator)
2398 return regulator->rdev->reg_data;
2400 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2403 * regulator_set_drvdata - set regulator driver data
2404 * @regulator: regulator
2407 void regulator_set_drvdata(struct regulator *regulator, void *data)
2409 regulator->rdev->reg_data = data;
2411 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2414 * regulator_get_id - get regulator ID
2417 int rdev_get_id(struct regulator_dev *rdev)
2419 return rdev->desc->id;
2421 EXPORT_SYMBOL_GPL(rdev_get_id);
2423 struct device *rdev_get_dev(struct regulator_dev *rdev)
2427 EXPORT_SYMBOL_GPL(rdev_get_dev);
2429 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2431 return reg_init_data->driver_data;
2433 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2435 static int __init regulator_init(void)
2437 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2438 return class_register(®ulator_class);
2441 /* init early to allow our consumers to complete system booting */
2442 core_initcall(regulator_init);
2444 static int __init regulator_init_complete(void)
2446 struct regulator_dev *rdev;
2447 struct regulator_ops *ops;
2448 struct regulation_constraints *c;
2452 mutex_lock(®ulator_list_mutex);
2454 /* If we have a full configuration then disable any regulators
2455 * which are not in use or always_on. This will become the
2456 * default behaviour in the future.
2458 list_for_each_entry(rdev, ®ulator_list, list) {
2459 ops = rdev->desc->ops;
2460 c = rdev->constraints;
2464 else if (rdev->desc->name)
2465 name = rdev->desc->name;
2469 if (!ops->disable || (c && c->always_on))
2472 mutex_lock(&rdev->mutex);
2474 if (rdev->use_count)
2477 /* If we can't read the status assume it's on. */
2478 if (ops->is_enabled)
2479 enabled = ops->is_enabled(rdev);
2486 if (has_full_constraints) {
2487 /* We log since this may kill the system if it
2489 printk(KERN_INFO "%s: disabling %s\n",
2491 ret = ops->disable(rdev);
2494 "%s: couldn't disable %s: %d\n",
2495 __func__, name, ret);
2498 /* The intention is that in future we will
2499 * assume that full constraints are provided
2500 * so warn even if we aren't going to do
2504 "%s: incomplete constraints, leaving %s on\n",
2509 mutex_unlock(&rdev->mutex);
2512 mutex_unlock(®ulator_list_mutex);
2516 late_initcall(regulator_init_complete);