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;
646 if (rdev->desc->type == REGULATOR_VOLTAGE) {
647 if (constraints->min_uV == constraints->max_uV)
648 count = sprintf(buf, "%d mV ",
649 constraints->min_uV / 1000);
651 count = sprintf(buf, "%d <--> %d mV ",
652 constraints->min_uV / 1000,
653 constraints->max_uV / 1000);
655 if (constraints->min_uA == constraints->max_uA)
656 count = sprintf(buf, "%d mA ",
657 constraints->min_uA / 1000);
659 count = sprintf(buf, "%d <--> %d mA ",
660 constraints->min_uA / 1000,
661 constraints->max_uA / 1000);
663 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
664 count += sprintf(buf + count, "fast ");
665 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
666 count += sprintf(buf + count, "normal ");
667 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
668 count += sprintf(buf + count, "idle ");
669 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
670 count += sprintf(buf + count, "standby");
672 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
675 static int machine_constraints_voltage(struct regulator_dev *rdev,
676 const char *name, struct regulation_constraints *constraints)
678 struct regulator_ops *ops = rdev->desc->ops;
680 /* constrain machine-level voltage specs to fit
681 * the actual range supported by this regulator.
683 if (ops->list_voltage && rdev->desc->n_voltages) {
684 int count = rdev->desc->n_voltages;
686 int min_uV = INT_MAX;
687 int max_uV = INT_MIN;
688 int cmin = constraints->min_uV;
689 int cmax = constraints->max_uV;
691 /* it's safe to autoconfigure fixed-voltage supplies
692 and the constraints are used by list_voltage. */
693 if (count == 1 && !cmin) {
696 constraints->min_uV = cmin;
697 constraints->max_uV = cmax;
700 /* voltage constraints are optional */
701 if ((cmin == 0) && (cmax == 0))
704 /* else require explicit machine-level constraints */
705 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
706 pr_err("%s: %s '%s' voltage constraints\n",
707 __func__, "invalid", name);
711 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
712 for (i = 0; i < count; i++) {
715 value = ops->list_voltage(rdev, i);
719 /* maybe adjust [min_uV..max_uV] */
720 if (value >= cmin && value < min_uV)
722 if (value <= cmax && value > max_uV)
726 /* final: [min_uV..max_uV] valid iff constraints valid */
727 if (max_uV < min_uV) {
728 pr_err("%s: %s '%s' voltage constraints\n",
729 __func__, "unsupportable", name);
733 /* use regulator's subset of machine constraints */
734 if (constraints->min_uV < min_uV) {
735 pr_debug("%s: override '%s' %s, %d -> %d\n",
736 __func__, name, "min_uV",
737 constraints->min_uV, min_uV);
738 constraints->min_uV = min_uV;
740 if (constraints->max_uV > max_uV) {
741 pr_debug("%s: override '%s' %s, %d -> %d\n",
742 __func__, name, "max_uV",
743 constraints->max_uV, max_uV);
744 constraints->max_uV = max_uV;
752 * set_machine_constraints - sets regulator constraints
753 * @rdev: regulator source
754 * @constraints: constraints to apply
756 * Allows platform initialisation code to define and constrain
757 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
758 * Constraints *must* be set by platform code in order for some
759 * regulator operations to proceed i.e. set_voltage, set_current_limit,
762 static int set_machine_constraints(struct regulator_dev *rdev,
763 struct regulation_constraints *constraints)
767 struct regulator_ops *ops = rdev->desc->ops;
769 if (constraints->name)
770 name = constraints->name;
771 else if (rdev->desc->name)
772 name = rdev->desc->name;
776 ret = machine_constraints_voltage(rdev, name, constraints);
780 rdev->constraints = constraints;
782 /* do we need to apply the constraint voltage */
783 if (rdev->constraints->apply_uV &&
784 rdev->constraints->min_uV == rdev->constraints->max_uV &&
786 ret = ops->set_voltage(rdev,
787 rdev->constraints->min_uV, rdev->constraints->max_uV);
789 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
791 rdev->constraints->min_uV, name);
792 rdev->constraints = NULL;
797 /* do we need to setup our suspend state */
798 if (constraints->initial_state) {
799 ret = suspend_prepare(rdev, constraints->initial_state);
801 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
803 rdev->constraints = NULL;
808 if (constraints->initial_mode) {
809 if (!ops->set_mode) {
810 printk(KERN_ERR "%s: no set_mode operation for %s\n",
816 ret = ops->set_mode(rdev, constraints->initial_mode);
819 "%s: failed to set initial mode for %s: %d\n",
820 __func__, name, ret);
825 /* If the constraints say the regulator should be on at this point
826 * and we have control then make sure it is enabled.
828 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
829 ret = ops->enable(rdev);
831 printk(KERN_ERR "%s: failed to enable %s\n",
833 rdev->constraints = NULL;
838 print_constraints(rdev);
844 * set_supply - set regulator supply regulator
845 * @rdev: regulator name
846 * @supply_rdev: supply regulator name
848 * Called by platform initialisation code to set the supply regulator for this
849 * regulator. This ensures that a regulators supply will also be enabled by the
850 * core if it's child is enabled.
852 static int set_supply(struct regulator_dev *rdev,
853 struct regulator_dev *supply_rdev)
857 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
861 "%s: could not add device link %s err %d\n",
862 __func__, supply_rdev->dev.kobj.name, err);
865 rdev->supply = supply_rdev;
866 list_add(&rdev->slist, &supply_rdev->supply_list);
872 * set_consumer_device_supply: Bind a regulator to a symbolic supply
873 * @rdev: regulator source
874 * @consumer_dev: device the supply applies to
875 * @consumer_dev_name: dev_name() string for device supply applies to
876 * @supply: symbolic name for supply
878 * Allows platform initialisation code to map physical regulator
879 * sources to symbolic names for supplies for use by devices. Devices
880 * should use these symbolic names to request regulators, avoiding the
881 * need to provide board-specific regulator names as platform data.
883 * Only one of consumer_dev and consumer_dev_name may be specified.
885 static int set_consumer_device_supply(struct regulator_dev *rdev,
886 struct device *consumer_dev, const char *consumer_dev_name,
889 struct regulator_map *node;
892 if (consumer_dev && consumer_dev_name)
895 if (!consumer_dev_name && consumer_dev)
896 consumer_dev_name = dev_name(consumer_dev);
901 if (consumer_dev_name != NULL)
906 list_for_each_entry(node, ®ulator_map_list, list) {
907 if (consumer_dev_name != node->dev_name)
909 if (strcmp(node->supply, supply) != 0)
912 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
913 dev_name(&node->regulator->dev),
914 node->regulator->desc->name,
916 dev_name(&rdev->dev), rdev->desc->name);
920 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
924 node->regulator = rdev;
925 node->supply = supply;
928 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
929 if (node->dev_name == NULL) {
935 list_add(&node->list, ®ulator_map_list);
939 static void unset_consumer_device_supply(struct regulator_dev *rdev,
940 const char *consumer_dev_name, struct device *consumer_dev)
942 struct regulator_map *node, *n;
944 if (consumer_dev && !consumer_dev_name)
945 consumer_dev_name = dev_name(consumer_dev);
947 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
948 if (rdev != node->regulator)
951 if (consumer_dev_name && node->dev_name &&
952 strcmp(consumer_dev_name, node->dev_name))
955 list_del(&node->list);
956 kfree(node->dev_name);
962 static void unset_regulator_supplies(struct regulator_dev *rdev)
964 struct regulator_map *node, *n;
966 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
967 if (rdev == node->regulator) {
968 list_del(&node->list);
969 kfree(node->dev_name);
976 #define REG_STR_SIZE 32
978 static struct regulator *create_regulator(struct regulator_dev *rdev,
980 const char *supply_name)
982 struct regulator *regulator;
983 char buf[REG_STR_SIZE];
986 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
987 if (regulator == NULL)
990 mutex_lock(&rdev->mutex);
991 regulator->rdev = rdev;
992 list_add(®ulator->list, &rdev->consumer_list);
995 /* create a 'requested_microamps_name' sysfs entry */
996 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
998 if (size >= REG_STR_SIZE)
1001 regulator->dev = dev;
1002 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1003 if (regulator->dev_attr.attr.name == NULL)
1006 regulator->dev_attr.attr.owner = THIS_MODULE;
1007 regulator->dev_attr.attr.mode = 0444;
1008 regulator->dev_attr.show = device_requested_uA_show;
1009 err = device_create_file(dev, ®ulator->dev_attr);
1011 printk(KERN_WARNING "%s: could not add regulator_dev"
1012 " load sysfs\n", __func__);
1016 /* also add a link to the device sysfs entry */
1017 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1018 dev->kobj.name, supply_name);
1019 if (size >= REG_STR_SIZE)
1022 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1023 if (regulator->supply_name == NULL)
1026 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1030 "%s: could not add device link %s err %d\n",
1031 __func__, dev->kobj.name, err);
1032 device_remove_file(dev, ®ulator->dev_attr);
1036 mutex_unlock(&rdev->mutex);
1039 kfree(regulator->supply_name);
1041 device_remove_file(regulator->dev, ®ulator->dev_attr);
1043 kfree(regulator->dev_attr.attr.name);
1045 list_del(®ulator->list);
1047 mutex_unlock(&rdev->mutex);
1051 /* Internal regulator request function */
1052 static struct regulator *_regulator_get(struct device *dev, const char *id,
1055 struct regulator_dev *rdev;
1056 struct regulator_map *map;
1057 struct regulator *regulator = ERR_PTR(-ENODEV);
1058 const char *devname = NULL;
1062 printk(KERN_ERR "regulator: get() with no identifier\n");
1067 devname = dev_name(dev);
1069 mutex_lock(®ulator_list_mutex);
1071 list_for_each_entry(map, ®ulator_map_list, list) {
1072 /* If the mapping has a device set up it must match */
1073 if (map->dev_name &&
1074 (!devname || strcmp(map->dev_name, devname)))
1077 if (strcmp(map->supply, id) == 0) {
1078 rdev = map->regulator;
1082 mutex_unlock(®ulator_list_mutex);
1086 if (rdev->exclusive) {
1087 regulator = ERR_PTR(-EPERM);
1091 if (exclusive && rdev->open_count) {
1092 regulator = ERR_PTR(-EBUSY);
1096 if (!try_module_get(rdev->owner))
1099 regulator = create_regulator(rdev, dev, id);
1100 if (regulator == NULL) {
1101 regulator = ERR_PTR(-ENOMEM);
1102 module_put(rdev->owner);
1107 rdev->exclusive = 1;
1109 ret = _regulator_is_enabled(rdev);
1111 rdev->use_count = 1;
1113 rdev->use_count = 0;
1117 mutex_unlock(®ulator_list_mutex);
1123 * regulator_get - lookup and obtain a reference to a regulator.
1124 * @dev: device for regulator "consumer"
1125 * @id: Supply name or regulator ID.
1127 * Returns a struct regulator corresponding to the regulator producer,
1128 * or IS_ERR() condition containing errno.
1130 * Use of supply names configured via regulator_set_device_supply() is
1131 * strongly encouraged. It is recommended that the supply name used
1132 * should match the name used for the supply and/or the relevant
1133 * device pins in the datasheet.
1135 struct regulator *regulator_get(struct device *dev, const char *id)
1137 return _regulator_get(dev, id, 0);
1139 EXPORT_SYMBOL_GPL(regulator_get);
1142 * regulator_get_exclusive - obtain exclusive access 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. Other consumers will be
1148 * unable to obtain this reference is held and the use count for the
1149 * regulator will be initialised to reflect the current state of the
1152 * This is intended for use by consumers which cannot tolerate shared
1153 * use of the regulator such as those which need to force the
1154 * regulator off for correct operation of the hardware they are
1157 * Use of supply names configured via regulator_set_device_supply() is
1158 * strongly encouraged. It is recommended that the supply name used
1159 * should match the name used for the supply and/or the relevant
1160 * device pins in the datasheet.
1162 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1164 return _regulator_get(dev, id, 1);
1166 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1169 * regulator_put - "free" the regulator source
1170 * @regulator: regulator source
1172 * Note: drivers must ensure that all regulator_enable calls made on this
1173 * regulator source are balanced by regulator_disable calls prior to calling
1176 void regulator_put(struct regulator *regulator)
1178 struct regulator_dev *rdev;
1180 if (regulator == NULL || IS_ERR(regulator))
1183 mutex_lock(®ulator_list_mutex);
1184 rdev = regulator->rdev;
1186 /* remove any sysfs entries */
1187 if (regulator->dev) {
1188 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1189 kfree(regulator->supply_name);
1190 device_remove_file(regulator->dev, ®ulator->dev_attr);
1191 kfree(regulator->dev_attr.attr.name);
1193 list_del(®ulator->list);
1197 rdev->exclusive = 0;
1199 module_put(rdev->owner);
1200 mutex_unlock(®ulator_list_mutex);
1202 EXPORT_SYMBOL_GPL(regulator_put);
1204 static int _regulator_can_change_status(struct regulator_dev *rdev)
1206 if (!rdev->constraints)
1209 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1215 /* locks held by regulator_enable() */
1216 static int _regulator_enable(struct regulator_dev *rdev)
1220 /* do we need to enable the supply regulator first */
1222 ret = _regulator_enable(rdev->supply);
1224 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1225 __func__, rdev->desc->name, ret);
1230 /* check voltage and requested load before enabling */
1231 if (rdev->constraints &&
1232 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1233 drms_uA_update(rdev);
1235 if (rdev->use_count == 0) {
1236 /* The regulator may on if it's not switchable or left on */
1237 ret = _regulator_is_enabled(rdev);
1238 if (ret == -EINVAL || ret == 0) {
1239 if (!_regulator_can_change_status(rdev))
1242 if (rdev->desc->ops->enable) {
1243 ret = rdev->desc->ops->enable(rdev);
1249 } else if (ret < 0) {
1250 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
1251 __func__, rdev->desc->name, ret);
1254 /* Fallthrough on positive return values - already enabled */
1263 * regulator_enable - enable regulator output
1264 * @regulator: regulator source
1266 * Request that the regulator be enabled with the regulator output at
1267 * the predefined voltage or current value. Calls to regulator_enable()
1268 * must be balanced with calls to regulator_disable().
1270 * NOTE: the output value can be set by other drivers, boot loader or may be
1271 * hardwired in the regulator.
1273 int regulator_enable(struct regulator *regulator)
1275 struct regulator_dev *rdev = regulator->rdev;
1278 mutex_lock(&rdev->mutex);
1279 ret = _regulator_enable(rdev);
1280 mutex_unlock(&rdev->mutex);
1283 EXPORT_SYMBOL_GPL(regulator_enable);
1285 /* locks held by regulator_disable() */
1286 static int _regulator_disable(struct regulator_dev *rdev)
1290 if (WARN(rdev->use_count <= 0,
1291 "unbalanced disables for %s\n",
1295 /* are we the last user and permitted to disable ? */
1296 if (rdev->use_count == 1 &&
1297 (rdev->constraints && !rdev->constraints->always_on)) {
1299 /* we are last user */
1300 if (_regulator_can_change_status(rdev) &&
1301 rdev->desc->ops->disable) {
1302 ret = rdev->desc->ops->disable(rdev);
1304 printk(KERN_ERR "%s: failed to disable %s\n",
1305 __func__, rdev->desc->name);
1310 /* decrease our supplies ref count and disable if required */
1312 _regulator_disable(rdev->supply);
1314 rdev->use_count = 0;
1315 } else if (rdev->use_count > 1) {
1317 if (rdev->constraints &&
1318 (rdev->constraints->valid_ops_mask &
1319 REGULATOR_CHANGE_DRMS))
1320 drms_uA_update(rdev);
1328 * regulator_disable - disable regulator output
1329 * @regulator: regulator source
1331 * Disable the regulator output voltage or current. Calls to
1332 * regulator_enable() must be balanced with calls to
1333 * regulator_disable().
1335 * NOTE: this will only disable the regulator output if no other consumer
1336 * devices have it enabled, the regulator device supports disabling and
1337 * machine constraints permit this operation.
1339 int regulator_disable(struct regulator *regulator)
1341 struct regulator_dev *rdev = regulator->rdev;
1344 mutex_lock(&rdev->mutex);
1345 ret = _regulator_disable(rdev);
1346 mutex_unlock(&rdev->mutex);
1349 EXPORT_SYMBOL_GPL(regulator_disable);
1351 /* locks held by regulator_force_disable() */
1352 static int _regulator_force_disable(struct regulator_dev *rdev)
1357 if (rdev->desc->ops->disable) {
1358 /* ah well, who wants to live forever... */
1359 ret = rdev->desc->ops->disable(rdev);
1361 printk(KERN_ERR "%s: failed to force disable %s\n",
1362 __func__, rdev->desc->name);
1365 /* notify other consumers that power has been forced off */
1366 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1370 /* decrease our supplies ref count and disable if required */
1372 _regulator_disable(rdev->supply);
1374 rdev->use_count = 0;
1379 * regulator_force_disable - force disable regulator output
1380 * @regulator: regulator source
1382 * Forcibly disable the regulator output voltage or current.
1383 * NOTE: this *will* disable the regulator output even if other consumer
1384 * devices have it enabled. This should be used for situations when device
1385 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1387 int regulator_force_disable(struct regulator *regulator)
1391 mutex_lock(®ulator->rdev->mutex);
1392 regulator->uA_load = 0;
1393 ret = _regulator_force_disable(regulator->rdev);
1394 mutex_unlock(®ulator->rdev->mutex);
1397 EXPORT_SYMBOL_GPL(regulator_force_disable);
1399 static int _regulator_is_enabled(struct regulator_dev *rdev)
1402 if (!rdev->desc->ops->is_enabled)
1405 return rdev->desc->ops->is_enabled(rdev);
1409 * regulator_is_enabled - is the regulator output enabled
1410 * @regulator: regulator source
1412 * Returns positive if the regulator driver backing the source/client
1413 * has requested that the device be enabled, zero if it hasn't, else a
1414 * negative errno code.
1416 * Note that the device backing this regulator handle can have multiple
1417 * users, so it might be enabled even if regulator_enable() was never
1418 * called for this particular source.
1420 int regulator_is_enabled(struct regulator *regulator)
1424 mutex_lock(®ulator->rdev->mutex);
1425 ret = _regulator_is_enabled(regulator->rdev);
1426 mutex_unlock(®ulator->rdev->mutex);
1430 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1433 * regulator_count_voltages - count regulator_list_voltage() selectors
1434 * @regulator: regulator source
1436 * Returns number of selectors, or negative errno. Selectors are
1437 * numbered starting at zero, and typically correspond to bitfields
1438 * in hardware registers.
1440 int regulator_count_voltages(struct regulator *regulator)
1442 struct regulator_dev *rdev = regulator->rdev;
1444 return rdev->desc->n_voltages ? : -EINVAL;
1446 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1449 * regulator_list_voltage - enumerate supported voltages
1450 * @regulator: regulator source
1451 * @selector: identify voltage to list
1452 * Context: can sleep
1454 * Returns a voltage that can be passed to @regulator_set_voltage(),
1455 * zero if this selector code can't be used on this sytem, or a
1458 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1460 struct regulator_dev *rdev = regulator->rdev;
1461 struct regulator_ops *ops = rdev->desc->ops;
1464 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1467 mutex_lock(&rdev->mutex);
1468 ret = ops->list_voltage(rdev, selector);
1469 mutex_unlock(&rdev->mutex);
1472 if (ret < rdev->constraints->min_uV)
1474 else if (ret > rdev->constraints->max_uV)
1480 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1483 * regulator_is_supported_voltage - check if a voltage range can be supported
1485 * @regulator: Regulator to check.
1486 * @min_uV: Minimum required voltage in uV.
1487 * @max_uV: Maximum required voltage in uV.
1489 * Returns a boolean or a negative error code.
1491 int regulator_is_supported_voltage(struct regulator *regulator,
1492 int min_uV, int max_uV)
1494 int i, voltages, ret;
1496 ret = regulator_count_voltages(regulator);
1501 for (i = 0; i < voltages; i++) {
1502 ret = regulator_list_voltage(regulator, i);
1504 if (ret >= min_uV && ret <= max_uV)
1512 * regulator_set_voltage - set regulator output voltage
1513 * @regulator: regulator source
1514 * @min_uV: Minimum required voltage in uV
1515 * @max_uV: Maximum acceptable voltage in uV
1517 * Sets a voltage regulator to the desired output voltage. This can be set
1518 * during any regulator state. IOW, regulator can be disabled or enabled.
1520 * If the regulator is enabled then the voltage will change to the new value
1521 * immediately otherwise if the regulator is disabled the regulator will
1522 * output at the new voltage when enabled.
1524 * NOTE: If the regulator is shared between several devices then the lowest
1525 * request voltage that meets the system constraints will be used.
1526 * Regulator system constraints must be set for this regulator before
1527 * calling this function otherwise this call will fail.
1529 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1531 struct regulator_dev *rdev = regulator->rdev;
1534 mutex_lock(&rdev->mutex);
1537 if (!rdev->desc->ops->set_voltage) {
1542 /* constraints check */
1543 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1546 regulator->min_uV = min_uV;
1547 regulator->max_uV = max_uV;
1548 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1551 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1552 mutex_unlock(&rdev->mutex);
1555 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1557 static int _regulator_get_voltage(struct regulator_dev *rdev)
1560 if (rdev->desc->ops->get_voltage)
1561 return rdev->desc->ops->get_voltage(rdev);
1567 * regulator_get_voltage - get regulator output voltage
1568 * @regulator: regulator source
1570 * This returns the current regulator voltage in uV.
1572 * NOTE: If the regulator is disabled it will return the voltage value. This
1573 * function should not be used to determine regulator state.
1575 int regulator_get_voltage(struct regulator *regulator)
1579 mutex_lock(®ulator->rdev->mutex);
1581 ret = _regulator_get_voltage(regulator->rdev);
1583 mutex_unlock(®ulator->rdev->mutex);
1587 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1590 * regulator_set_current_limit - set regulator output current limit
1591 * @regulator: regulator source
1592 * @min_uA: Minimuum supported current in uA
1593 * @max_uA: Maximum supported current in uA
1595 * Sets current sink to the desired output current. This can be set during
1596 * any regulator state. IOW, regulator can be disabled or enabled.
1598 * If the regulator is enabled then the current will change to the new value
1599 * immediately otherwise if the regulator is disabled the regulator will
1600 * output at the new current when enabled.
1602 * NOTE: Regulator system constraints must be set for this regulator before
1603 * calling this function otherwise this call will fail.
1605 int regulator_set_current_limit(struct regulator *regulator,
1606 int min_uA, int max_uA)
1608 struct regulator_dev *rdev = regulator->rdev;
1611 mutex_lock(&rdev->mutex);
1614 if (!rdev->desc->ops->set_current_limit) {
1619 /* constraints check */
1620 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1624 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1626 mutex_unlock(&rdev->mutex);
1629 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1631 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1635 mutex_lock(&rdev->mutex);
1638 if (!rdev->desc->ops->get_current_limit) {
1643 ret = rdev->desc->ops->get_current_limit(rdev);
1645 mutex_unlock(&rdev->mutex);
1650 * regulator_get_current_limit - get regulator output current
1651 * @regulator: regulator source
1653 * This returns the current supplied by the specified current sink in uA.
1655 * NOTE: If the regulator is disabled it will return the current value. This
1656 * function should not be used to determine regulator state.
1658 int regulator_get_current_limit(struct regulator *regulator)
1660 return _regulator_get_current_limit(regulator->rdev);
1662 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1665 * regulator_set_mode - set regulator operating mode
1666 * @regulator: regulator source
1667 * @mode: operating mode - one of the REGULATOR_MODE constants
1669 * Set regulator operating mode to increase regulator efficiency or improve
1670 * regulation performance.
1672 * NOTE: Regulator system constraints must be set for this regulator before
1673 * calling this function otherwise this call will fail.
1675 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1677 struct regulator_dev *rdev = regulator->rdev;
1680 mutex_lock(&rdev->mutex);
1683 if (!rdev->desc->ops->set_mode) {
1688 /* constraints check */
1689 ret = regulator_check_mode(rdev, mode);
1693 ret = rdev->desc->ops->set_mode(rdev, mode);
1695 mutex_unlock(&rdev->mutex);
1698 EXPORT_SYMBOL_GPL(regulator_set_mode);
1700 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1704 mutex_lock(&rdev->mutex);
1707 if (!rdev->desc->ops->get_mode) {
1712 ret = rdev->desc->ops->get_mode(rdev);
1714 mutex_unlock(&rdev->mutex);
1719 * regulator_get_mode - get regulator operating mode
1720 * @regulator: regulator source
1722 * Get the current regulator operating mode.
1724 unsigned int regulator_get_mode(struct regulator *regulator)
1726 return _regulator_get_mode(regulator->rdev);
1728 EXPORT_SYMBOL_GPL(regulator_get_mode);
1731 * regulator_set_optimum_mode - set regulator optimum operating mode
1732 * @regulator: regulator source
1733 * @uA_load: load current
1735 * Notifies the regulator core of a new device load. This is then used by
1736 * DRMS (if enabled by constraints) to set the most efficient regulator
1737 * operating mode for the new regulator loading.
1739 * Consumer devices notify their supply regulator of the maximum power
1740 * they will require (can be taken from device datasheet in the power
1741 * consumption tables) when they change operational status and hence power
1742 * state. Examples of operational state changes that can affect power
1743 * consumption are :-
1745 * o Device is opened / closed.
1746 * o Device I/O is about to begin or has just finished.
1747 * o Device is idling in between work.
1749 * This information is also exported via sysfs to userspace.
1751 * DRMS will sum the total requested load on the regulator and change
1752 * to the most efficient operating mode if platform constraints allow.
1754 * Returns the new regulator mode or error.
1756 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1758 struct regulator_dev *rdev = regulator->rdev;
1759 struct regulator *consumer;
1760 int ret, output_uV, input_uV, total_uA_load = 0;
1763 mutex_lock(&rdev->mutex);
1765 regulator->uA_load = uA_load;
1766 ret = regulator_check_drms(rdev);
1772 if (!rdev->desc->ops->get_optimum_mode)
1775 /* get output voltage */
1776 output_uV = rdev->desc->ops->get_voltage(rdev);
1777 if (output_uV <= 0) {
1778 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1779 __func__, rdev->desc->name);
1783 /* get input voltage */
1784 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1785 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1787 input_uV = rdev->constraints->input_uV;
1788 if (input_uV <= 0) {
1789 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1790 __func__, rdev->desc->name);
1794 /* calc total requested load for this regulator */
1795 list_for_each_entry(consumer, &rdev->consumer_list, list)
1796 total_uA_load += consumer->uA_load;
1798 mode = rdev->desc->ops->get_optimum_mode(rdev,
1799 input_uV, output_uV,
1801 ret = regulator_check_mode(rdev, mode);
1803 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1804 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1805 total_uA_load, input_uV, output_uV);
1809 ret = rdev->desc->ops->set_mode(rdev, mode);
1811 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1812 __func__, mode, rdev->desc->name);
1817 mutex_unlock(&rdev->mutex);
1820 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1823 * regulator_register_notifier - register regulator event notifier
1824 * @regulator: regulator source
1825 * @nb: notifier block
1827 * Register notifier block to receive regulator events.
1829 int regulator_register_notifier(struct regulator *regulator,
1830 struct notifier_block *nb)
1832 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1835 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1838 * regulator_unregister_notifier - unregister regulator event notifier
1839 * @regulator: regulator source
1840 * @nb: notifier block
1842 * Unregister regulator event notifier block.
1844 int regulator_unregister_notifier(struct regulator *regulator,
1845 struct notifier_block *nb)
1847 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1850 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1852 /* notify regulator consumers and downstream regulator consumers.
1853 * Note mutex must be held by caller.
1855 static void _notifier_call_chain(struct regulator_dev *rdev,
1856 unsigned long event, void *data)
1858 struct regulator_dev *_rdev;
1860 /* call rdev chain first */
1861 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1863 /* now notify regulator we supply */
1864 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1865 mutex_lock(&_rdev->mutex);
1866 _notifier_call_chain(_rdev, event, data);
1867 mutex_unlock(&_rdev->mutex);
1872 * regulator_bulk_get - get multiple regulator consumers
1874 * @dev: Device to supply
1875 * @num_consumers: Number of consumers to register
1876 * @consumers: Configuration of consumers; clients are stored here.
1878 * @return 0 on success, an errno on failure.
1880 * This helper function allows drivers to get several regulator
1881 * consumers in one operation. If any of the regulators cannot be
1882 * acquired then any regulators that were allocated will be freed
1883 * before returning to the caller.
1885 int regulator_bulk_get(struct device *dev, int num_consumers,
1886 struct regulator_bulk_data *consumers)
1891 for (i = 0; i < num_consumers; i++)
1892 consumers[i].consumer = NULL;
1894 for (i = 0; i < num_consumers; i++) {
1895 consumers[i].consumer = regulator_get(dev,
1896 consumers[i].supply);
1897 if (IS_ERR(consumers[i].consumer)) {
1898 ret = PTR_ERR(consumers[i].consumer);
1899 dev_err(dev, "Failed to get supply '%s': %d\n",
1900 consumers[i].supply, ret);
1901 consumers[i].consumer = NULL;
1909 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1910 regulator_put(consumers[i].consumer);
1914 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1917 * regulator_bulk_enable - enable multiple regulator consumers
1919 * @num_consumers: Number of consumers
1920 * @consumers: Consumer data; clients are stored here.
1921 * @return 0 on success, an errno on failure
1923 * This convenience API allows consumers to enable multiple regulator
1924 * clients in a single API call. If any consumers cannot be enabled
1925 * then any others that were enabled will be disabled again prior to
1928 int regulator_bulk_enable(int num_consumers,
1929 struct regulator_bulk_data *consumers)
1934 for (i = 0; i < num_consumers; i++) {
1935 ret = regulator_enable(consumers[i].consumer);
1943 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
1944 for (i = 0; i < num_consumers; i++)
1945 regulator_disable(consumers[i].consumer);
1949 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1952 * regulator_bulk_disable - disable multiple regulator consumers
1954 * @num_consumers: Number of consumers
1955 * @consumers: Consumer data; clients are stored here.
1956 * @return 0 on success, an errno on failure
1958 * This convenience API allows consumers to disable multiple regulator
1959 * clients in a single API call. If any consumers cannot be enabled
1960 * then any others that were disabled will be disabled again prior to
1963 int regulator_bulk_disable(int num_consumers,
1964 struct regulator_bulk_data *consumers)
1969 for (i = 0; i < num_consumers; i++) {
1970 ret = regulator_disable(consumers[i].consumer);
1978 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
1980 for (i = 0; i < num_consumers; i++)
1981 regulator_enable(consumers[i].consumer);
1985 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1988 * regulator_bulk_free - free multiple regulator consumers
1990 * @num_consumers: Number of consumers
1991 * @consumers: Consumer data; clients are stored here.
1993 * This convenience API allows consumers to free multiple regulator
1994 * clients in a single API call.
1996 void regulator_bulk_free(int num_consumers,
1997 struct regulator_bulk_data *consumers)
2001 for (i = 0; i < num_consumers; i++) {
2002 regulator_put(consumers[i].consumer);
2003 consumers[i].consumer = NULL;
2006 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2009 * regulator_notifier_call_chain - call regulator event notifier
2010 * @rdev: regulator source
2011 * @event: notifier block
2012 * @data: callback-specific data.
2014 * Called by regulator drivers to notify clients a regulator event has
2015 * occurred. We also notify regulator clients downstream.
2016 * Note lock must be held by caller.
2018 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2019 unsigned long event, void *data)
2021 _notifier_call_chain(rdev, event, data);
2025 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2028 * regulator_mode_to_status - convert a regulator mode into a status
2030 * @mode: Mode to convert
2032 * Convert a regulator mode into a status.
2034 int regulator_mode_to_status(unsigned int mode)
2037 case REGULATOR_MODE_FAST:
2038 return REGULATOR_STATUS_FAST;
2039 case REGULATOR_MODE_NORMAL:
2040 return REGULATOR_STATUS_NORMAL;
2041 case REGULATOR_MODE_IDLE:
2042 return REGULATOR_STATUS_IDLE;
2043 case REGULATOR_STATUS_STANDBY:
2044 return REGULATOR_STATUS_STANDBY;
2049 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2052 * To avoid cluttering sysfs (and memory) with useless state, only
2053 * create attributes that can be meaningfully displayed.
2055 static int add_regulator_attributes(struct regulator_dev *rdev)
2057 struct device *dev = &rdev->dev;
2058 struct regulator_ops *ops = rdev->desc->ops;
2061 /* some attributes need specific methods to be displayed */
2062 if (ops->get_voltage) {
2063 status = device_create_file(dev, &dev_attr_microvolts);
2067 if (ops->get_current_limit) {
2068 status = device_create_file(dev, &dev_attr_microamps);
2072 if (ops->get_mode) {
2073 status = device_create_file(dev, &dev_attr_opmode);
2077 if (ops->is_enabled) {
2078 status = device_create_file(dev, &dev_attr_state);
2082 if (ops->get_status) {
2083 status = device_create_file(dev, &dev_attr_status);
2088 /* some attributes are type-specific */
2089 if (rdev->desc->type == REGULATOR_CURRENT) {
2090 status = device_create_file(dev, &dev_attr_requested_microamps);
2095 /* all the other attributes exist to support constraints;
2096 * don't show them if there are no constraints, or if the
2097 * relevant supporting methods are missing.
2099 if (!rdev->constraints)
2102 /* constraints need specific supporting methods */
2103 if (ops->set_voltage) {
2104 status = device_create_file(dev, &dev_attr_min_microvolts);
2107 status = device_create_file(dev, &dev_attr_max_microvolts);
2111 if (ops->set_current_limit) {
2112 status = device_create_file(dev, &dev_attr_min_microamps);
2115 status = device_create_file(dev, &dev_attr_max_microamps);
2120 /* suspend mode constraints need multiple supporting methods */
2121 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2124 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2127 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2130 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2134 if (ops->set_suspend_voltage) {
2135 status = device_create_file(dev,
2136 &dev_attr_suspend_standby_microvolts);
2139 status = device_create_file(dev,
2140 &dev_attr_suspend_mem_microvolts);
2143 status = device_create_file(dev,
2144 &dev_attr_suspend_disk_microvolts);
2149 if (ops->set_suspend_mode) {
2150 status = device_create_file(dev,
2151 &dev_attr_suspend_standby_mode);
2154 status = device_create_file(dev,
2155 &dev_attr_suspend_mem_mode);
2158 status = device_create_file(dev,
2159 &dev_attr_suspend_disk_mode);
2168 * regulator_register - register regulator
2169 * @regulator_desc: regulator to register
2170 * @dev: struct device for the regulator
2171 * @init_data: platform provided init data, passed through by driver
2172 * @driver_data: private regulator data
2174 * Called by regulator drivers to register a regulator.
2175 * Returns 0 on success.
2177 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2178 struct device *dev, struct regulator_init_data *init_data,
2181 static atomic_t regulator_no = ATOMIC_INIT(0);
2182 struct regulator_dev *rdev;
2185 if (regulator_desc == NULL)
2186 return ERR_PTR(-EINVAL);
2188 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2189 return ERR_PTR(-EINVAL);
2191 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2192 regulator_desc->type != REGULATOR_CURRENT)
2193 return ERR_PTR(-EINVAL);
2196 return ERR_PTR(-EINVAL);
2198 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2200 return ERR_PTR(-ENOMEM);
2202 mutex_lock(®ulator_list_mutex);
2204 mutex_init(&rdev->mutex);
2205 rdev->reg_data = driver_data;
2206 rdev->owner = regulator_desc->owner;
2207 rdev->desc = regulator_desc;
2208 INIT_LIST_HEAD(&rdev->consumer_list);
2209 INIT_LIST_HEAD(&rdev->supply_list);
2210 INIT_LIST_HEAD(&rdev->list);
2211 INIT_LIST_HEAD(&rdev->slist);
2212 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2214 /* preform any regulator specific init */
2215 if (init_data->regulator_init) {
2216 ret = init_data->regulator_init(rdev->reg_data);
2221 /* register with sysfs */
2222 rdev->dev.class = ®ulator_class;
2223 rdev->dev.parent = dev;
2224 dev_set_name(&rdev->dev, "regulator.%d",
2225 atomic_inc_return(®ulator_no) - 1);
2226 ret = device_register(&rdev->dev);
2230 dev_set_drvdata(&rdev->dev, rdev);
2232 /* set regulator constraints */
2233 ret = set_machine_constraints(rdev, &init_data->constraints);
2237 /* add attributes supported by this regulator */
2238 ret = add_regulator_attributes(rdev);
2242 /* set supply regulator if it exists */
2243 if (init_data->supply_regulator_dev) {
2244 ret = set_supply(rdev,
2245 dev_get_drvdata(init_data->supply_regulator_dev));
2250 /* add consumers devices */
2251 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2252 ret = set_consumer_device_supply(rdev,
2253 init_data->consumer_supplies[i].dev,
2254 init_data->consumer_supplies[i].dev_name,
2255 init_data->consumer_supplies[i].supply);
2257 for (--i; i >= 0; i--)
2258 unset_consumer_device_supply(rdev,
2259 init_data->consumer_supplies[i].dev_name,
2260 init_data->consumer_supplies[i].dev);
2265 list_add(&rdev->list, ®ulator_list);
2267 mutex_unlock(®ulator_list_mutex);
2271 device_unregister(&rdev->dev);
2272 /* device core frees rdev */
2273 rdev = ERR_PTR(ret);
2278 rdev = ERR_PTR(ret);
2281 EXPORT_SYMBOL_GPL(regulator_register);
2284 * regulator_unregister - unregister regulator
2285 * @rdev: regulator to unregister
2287 * Called by regulator drivers to unregister a regulator.
2289 void regulator_unregister(struct regulator_dev *rdev)
2294 mutex_lock(®ulator_list_mutex);
2295 WARN_ON(rdev->open_count);
2296 unset_regulator_supplies(rdev);
2297 list_del(&rdev->list);
2299 sysfs_remove_link(&rdev->dev.kobj, "supply");
2300 device_unregister(&rdev->dev);
2301 mutex_unlock(®ulator_list_mutex);
2303 EXPORT_SYMBOL_GPL(regulator_unregister);
2306 * regulator_suspend_prepare - prepare regulators for system wide suspend
2307 * @state: system suspend state
2309 * Configure each regulator with it's suspend operating parameters for state.
2310 * This will usually be called by machine suspend code prior to supending.
2312 int regulator_suspend_prepare(suspend_state_t state)
2314 struct regulator_dev *rdev;
2317 /* ON is handled by regulator active state */
2318 if (state == PM_SUSPEND_ON)
2321 mutex_lock(®ulator_list_mutex);
2322 list_for_each_entry(rdev, ®ulator_list, list) {
2324 mutex_lock(&rdev->mutex);
2325 ret = suspend_prepare(rdev, state);
2326 mutex_unlock(&rdev->mutex);
2329 printk(KERN_ERR "%s: failed to prepare %s\n",
2330 __func__, rdev->desc->name);
2335 mutex_unlock(®ulator_list_mutex);
2338 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2341 * regulator_has_full_constraints - the system has fully specified constraints
2343 * Calling this function will cause the regulator API to disable all
2344 * regulators which have a zero use count and don't have an always_on
2345 * constraint in a late_initcall.
2347 * The intention is that this will become the default behaviour in a
2348 * future kernel release so users are encouraged to use this facility
2351 void regulator_has_full_constraints(void)
2353 has_full_constraints = 1;
2355 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2358 * rdev_get_drvdata - get rdev regulator driver data
2361 * Get rdev regulator driver private data. This call can be used in the
2362 * regulator driver context.
2364 void *rdev_get_drvdata(struct regulator_dev *rdev)
2366 return rdev->reg_data;
2368 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2371 * regulator_get_drvdata - get regulator driver data
2372 * @regulator: regulator
2374 * Get regulator driver private data. This call can be used in the consumer
2375 * driver context when non API regulator specific functions need to be called.
2377 void *regulator_get_drvdata(struct regulator *regulator)
2379 return regulator->rdev->reg_data;
2381 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2384 * regulator_set_drvdata - set regulator driver data
2385 * @regulator: regulator
2388 void regulator_set_drvdata(struct regulator *regulator, void *data)
2390 regulator->rdev->reg_data = data;
2392 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2395 * regulator_get_id - get regulator ID
2398 int rdev_get_id(struct regulator_dev *rdev)
2400 return rdev->desc->id;
2402 EXPORT_SYMBOL_GPL(rdev_get_id);
2404 struct device *rdev_get_dev(struct regulator_dev *rdev)
2408 EXPORT_SYMBOL_GPL(rdev_get_dev);
2410 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2412 return reg_init_data->driver_data;
2414 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2416 static int __init regulator_init(void)
2418 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2419 return class_register(®ulator_class);
2422 /* init early to allow our consumers to complete system booting */
2423 core_initcall(regulator_init);
2425 static int __init regulator_init_complete(void)
2427 struct regulator_dev *rdev;
2428 struct regulator_ops *ops;
2429 struct regulation_constraints *c;
2433 mutex_lock(®ulator_list_mutex);
2435 /* If we have a full configuration then disable any regulators
2436 * which are not in use or always_on. This will become the
2437 * default behaviour in the future.
2439 list_for_each_entry(rdev, ®ulator_list, list) {
2440 ops = rdev->desc->ops;
2441 c = rdev->constraints;
2445 else if (rdev->desc->name)
2446 name = rdev->desc->name;
2450 if (!ops->disable || (c && c->always_on))
2453 mutex_lock(&rdev->mutex);
2455 if (rdev->use_count)
2458 /* If we can't read the status assume it's on. */
2459 if (ops->is_enabled)
2460 enabled = ops->is_enabled(rdev);
2467 if (has_full_constraints) {
2468 /* We log since this may kill the system if it
2470 printk(KERN_INFO "%s: disabling %s\n",
2472 ret = ops->disable(rdev);
2475 "%s: couldn't disable %s: %d\n",
2476 __func__, name, ret);
2479 /* The intention is that in future we will
2480 * assume that full constraints are provided
2481 * so warn even if we aren't going to do
2485 "%s: incomplete constraints, leaving %s on\n",
2490 mutex_unlock(&rdev->mutex);
2493 mutex_unlock(®ulator_list_mutex);
2497 late_initcall(regulator_init_complete);