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->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);
284 return regulator_print_state(buf, _regulator_is_enabled(rdev));
286 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
288 static ssize_t regulator_status_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
291 struct regulator_dev *rdev = dev_get_drvdata(dev);
295 status = rdev->desc->ops->get_status(rdev);
300 case REGULATOR_STATUS_OFF:
303 case REGULATOR_STATUS_ON:
306 case REGULATOR_STATUS_ERROR:
309 case REGULATOR_STATUS_FAST:
312 case REGULATOR_STATUS_NORMAL:
315 case REGULATOR_STATUS_IDLE:
318 case REGULATOR_STATUS_STANDBY:
325 return sprintf(buf, "%s\n", label);
327 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
329 static ssize_t regulator_min_uA_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
332 struct regulator_dev *rdev = dev_get_drvdata(dev);
334 if (!rdev->constraints)
335 return sprintf(buf, "constraint not defined\n");
337 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
339 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
341 static ssize_t regulator_max_uA_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
344 struct regulator_dev *rdev = dev_get_drvdata(dev);
346 if (!rdev->constraints)
347 return sprintf(buf, "constraint not defined\n");
349 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
351 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
353 static ssize_t regulator_min_uV_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
356 struct regulator_dev *rdev = dev_get_drvdata(dev);
358 if (!rdev->constraints)
359 return sprintf(buf, "constraint not defined\n");
361 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
363 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
365 static ssize_t regulator_max_uV_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
368 struct regulator_dev *rdev = dev_get_drvdata(dev);
370 if (!rdev->constraints)
371 return sprintf(buf, "constraint not defined\n");
373 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
375 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
377 static ssize_t regulator_total_uA_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
380 struct regulator_dev *rdev = dev_get_drvdata(dev);
381 struct regulator *regulator;
384 mutex_lock(&rdev->mutex);
385 list_for_each_entry(regulator, &rdev->consumer_list, list)
386 uA += regulator->uA_load;
387 mutex_unlock(&rdev->mutex);
388 return sprintf(buf, "%d\n", uA);
390 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
392 static ssize_t regulator_num_users_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
395 struct regulator_dev *rdev = dev_get_drvdata(dev);
396 return sprintf(buf, "%d\n", rdev->use_count);
399 static ssize_t regulator_type_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct regulator_dev *rdev = dev_get_drvdata(dev);
404 switch (rdev->desc->type) {
405 case REGULATOR_VOLTAGE:
406 return sprintf(buf, "voltage\n");
407 case REGULATOR_CURRENT:
408 return sprintf(buf, "current\n");
410 return sprintf(buf, "unknown\n");
413 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct regulator_dev *rdev = dev_get_drvdata(dev);
418 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
420 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
421 regulator_suspend_mem_uV_show, NULL);
423 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
426 struct regulator_dev *rdev = dev_get_drvdata(dev);
428 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
430 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
431 regulator_suspend_disk_uV_show, NULL);
433 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
434 struct device_attribute *attr, char *buf)
436 struct regulator_dev *rdev = dev_get_drvdata(dev);
438 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
440 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
441 regulator_suspend_standby_uV_show, NULL);
443 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
446 struct regulator_dev *rdev = dev_get_drvdata(dev);
448 return regulator_print_opmode(buf,
449 rdev->constraints->state_mem.mode);
451 static DEVICE_ATTR(suspend_mem_mode, 0444,
452 regulator_suspend_mem_mode_show, NULL);
454 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 struct regulator_dev *rdev = dev_get_drvdata(dev);
459 return regulator_print_opmode(buf,
460 rdev->constraints->state_disk.mode);
462 static DEVICE_ATTR(suspend_disk_mode, 0444,
463 regulator_suspend_disk_mode_show, NULL);
465 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct regulator_dev *rdev = dev_get_drvdata(dev);
470 return regulator_print_opmode(buf,
471 rdev->constraints->state_standby.mode);
473 static DEVICE_ATTR(suspend_standby_mode, 0444,
474 regulator_suspend_standby_mode_show, NULL);
476 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
479 struct regulator_dev *rdev = dev_get_drvdata(dev);
481 return regulator_print_state(buf,
482 rdev->constraints->state_mem.enabled);
484 static DEVICE_ATTR(suspend_mem_state, 0444,
485 regulator_suspend_mem_state_show, NULL);
487 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
490 struct regulator_dev *rdev = dev_get_drvdata(dev);
492 return regulator_print_state(buf,
493 rdev->constraints->state_disk.enabled);
495 static DEVICE_ATTR(suspend_disk_state, 0444,
496 regulator_suspend_disk_state_show, NULL);
498 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 struct regulator_dev *rdev = dev_get_drvdata(dev);
503 return regulator_print_state(buf,
504 rdev->constraints->state_standby.enabled);
506 static DEVICE_ATTR(suspend_standby_state, 0444,
507 regulator_suspend_standby_state_show, NULL);
511 * These are the only attributes are present for all regulators.
512 * Other attributes are a function of regulator functionality.
514 static struct device_attribute regulator_dev_attrs[] = {
515 __ATTR(name, 0444, regulator_name_show, NULL),
516 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
517 __ATTR(type, 0444, regulator_type_show, NULL),
521 static void regulator_dev_release(struct device *dev)
523 struct regulator_dev *rdev = dev_get_drvdata(dev);
527 static struct class regulator_class = {
529 .dev_release = regulator_dev_release,
530 .dev_attrs = regulator_dev_attrs,
533 /* Calculate the new optimum regulator operating mode based on the new total
534 * consumer load. All locks held by caller */
535 static void drms_uA_update(struct regulator_dev *rdev)
537 struct regulator *sibling;
538 int current_uA = 0, output_uV, input_uV, err;
541 err = regulator_check_drms(rdev);
542 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
543 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
546 /* get output voltage */
547 output_uV = rdev->desc->ops->get_voltage(rdev);
551 /* get input voltage */
552 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
553 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
555 input_uV = rdev->constraints->input_uV;
559 /* calc total requested load */
560 list_for_each_entry(sibling, &rdev->consumer_list, list)
561 current_uA += sibling->uA_load;
563 /* now get the optimum mode for our new total regulator load */
564 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
565 output_uV, current_uA);
567 /* check the new mode is allowed */
568 err = regulator_check_mode(rdev, mode);
570 rdev->desc->ops->set_mode(rdev, mode);
573 static int suspend_set_state(struct regulator_dev *rdev,
574 struct regulator_state *rstate)
578 /* enable & disable are mandatory for suspend control */
579 if (!rdev->desc->ops->set_suspend_enable ||
580 !rdev->desc->ops->set_suspend_disable) {
581 printk(KERN_ERR "%s: no way to set suspend state\n",
587 ret = rdev->desc->ops->set_suspend_enable(rdev);
589 ret = rdev->desc->ops->set_suspend_disable(rdev);
591 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
595 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
596 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
598 printk(KERN_ERR "%s: failed to set voltage\n",
604 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
605 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
607 printk(KERN_ERR "%s: failed to set mode\n", __func__);
614 /* locks held by caller */
615 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
617 if (!rdev->constraints)
621 case PM_SUSPEND_STANDBY:
622 return suspend_set_state(rdev,
623 &rdev->constraints->state_standby);
625 return suspend_set_state(rdev,
626 &rdev->constraints->state_mem);
628 return suspend_set_state(rdev,
629 &rdev->constraints->state_disk);
635 static void print_constraints(struct regulator_dev *rdev)
637 struct regulation_constraints *constraints = rdev->constraints;
641 if (rdev->desc->type == REGULATOR_VOLTAGE) {
642 if (constraints->min_uV == constraints->max_uV)
643 count = sprintf(buf, "%d mV ",
644 constraints->min_uV / 1000);
646 count = sprintf(buf, "%d <--> %d mV ",
647 constraints->min_uV / 1000,
648 constraints->max_uV / 1000);
650 if (constraints->min_uA == constraints->max_uA)
651 count = sprintf(buf, "%d mA ",
652 constraints->min_uA / 1000);
654 count = sprintf(buf, "%d <--> %d mA ",
655 constraints->min_uA / 1000,
656 constraints->max_uA / 1000);
658 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
659 count += sprintf(buf + count, "fast ");
660 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
661 count += sprintf(buf + count, "normal ");
662 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
663 count += sprintf(buf + count, "idle ");
664 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
665 count += sprintf(buf + count, "standby");
667 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
671 * set_machine_constraints - sets regulator constraints
672 * @rdev: regulator source
673 * @constraints: constraints to apply
675 * Allows platform initialisation code to define and constrain
676 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
677 * Constraints *must* be set by platform code in order for some
678 * regulator operations to proceed i.e. set_voltage, set_current_limit,
681 static int set_machine_constraints(struct regulator_dev *rdev,
682 struct regulation_constraints *constraints)
686 struct regulator_ops *ops = rdev->desc->ops;
688 if (constraints->name)
689 name = constraints->name;
690 else if (rdev->desc->name)
691 name = rdev->desc->name;
695 /* constrain machine-level voltage specs to fit
696 * the actual range supported by this regulator.
698 if (ops->list_voltage && rdev->desc->n_voltages) {
699 int count = rdev->desc->n_voltages;
701 int min_uV = INT_MAX;
702 int max_uV = INT_MIN;
703 int cmin = constraints->min_uV;
704 int cmax = constraints->max_uV;
706 /* it's safe to autoconfigure fixed-voltage supplies
707 and the constraints are used by list_voltage. */
708 if (count == 1 && !cmin) {
711 constraints->min_uV = cmin;
712 constraints->max_uV = cmax;
715 /* voltage constraints are optional */
716 if ((cmin == 0) && (cmax == 0))
719 /* else require explicit machine-level constraints */
720 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
721 pr_err("%s: %s '%s' voltage constraints\n",
722 __func__, "invalid", name);
727 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
728 for (i = 0; i < count; i++) {
731 value = ops->list_voltage(rdev, i);
735 /* maybe adjust [min_uV..max_uV] */
736 if (value >= cmin && value < min_uV)
738 if (value <= cmax && value > max_uV)
742 /* final: [min_uV..max_uV] valid iff constraints valid */
743 if (max_uV < min_uV) {
744 pr_err("%s: %s '%s' voltage constraints\n",
745 __func__, "unsupportable", name);
750 /* use regulator's subset of machine constraints */
751 if (constraints->min_uV < min_uV) {
752 pr_debug("%s: override '%s' %s, %d -> %d\n",
753 __func__, name, "min_uV",
754 constraints->min_uV, min_uV);
755 constraints->min_uV = min_uV;
757 if (constraints->max_uV > max_uV) {
758 pr_debug("%s: override '%s' %s, %d -> %d\n",
759 __func__, name, "max_uV",
760 constraints->max_uV, max_uV);
761 constraints->max_uV = max_uV;
765 rdev->constraints = constraints;
767 /* do we need to apply the constraint voltage */
768 if (rdev->constraints->apply_uV &&
769 rdev->constraints->min_uV == rdev->constraints->max_uV &&
771 ret = ops->set_voltage(rdev,
772 rdev->constraints->min_uV, rdev->constraints->max_uV);
774 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
776 rdev->constraints->min_uV, name);
777 rdev->constraints = NULL;
782 /* do we need to setup our suspend state */
783 if (constraints->initial_state) {
784 ret = suspend_prepare(rdev, constraints->initial_state);
786 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
788 rdev->constraints = NULL;
793 if (constraints->initial_mode) {
794 if (!ops->set_mode) {
795 printk(KERN_ERR "%s: no set_mode operation for %s\n",
801 ret = ops->set_mode(rdev, constraints->initial_mode);
804 "%s: failed to set initial mode for %s: %d\n",
805 __func__, name, ret);
810 /* If the constraints say the regulator should be on at this point
811 * and we have control then make sure it is enabled.
813 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
814 ret = ops->enable(rdev);
816 printk(KERN_ERR "%s: failed to enable %s\n",
818 rdev->constraints = NULL;
823 print_constraints(rdev);
829 * set_supply - set regulator supply regulator
830 * @rdev: regulator name
831 * @supply_rdev: supply regulator name
833 * Called by platform initialisation code to set the supply regulator for this
834 * regulator. This ensures that a regulators supply will also be enabled by the
835 * core if it's child is enabled.
837 static int set_supply(struct regulator_dev *rdev,
838 struct regulator_dev *supply_rdev)
842 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
846 "%s: could not add device link %s err %d\n",
847 __func__, supply_rdev->dev.kobj.name, err);
850 rdev->supply = supply_rdev;
851 list_add(&rdev->slist, &supply_rdev->supply_list);
857 * set_consumer_device_supply: Bind a regulator to a symbolic supply
858 * @rdev: regulator source
859 * @consumer_dev: device the supply applies to
860 * @consumer_dev_name: dev_name() string for device supply applies to
861 * @supply: symbolic name for supply
863 * Allows platform initialisation code to map physical regulator
864 * sources to symbolic names for supplies for use by devices. Devices
865 * should use these symbolic names to request regulators, avoiding the
866 * need to provide board-specific regulator names as platform data.
868 * Only one of consumer_dev and consumer_dev_name may be specified.
870 static int set_consumer_device_supply(struct regulator_dev *rdev,
871 struct device *consumer_dev, const char *consumer_dev_name,
874 struct regulator_map *node;
876 if (consumer_dev && consumer_dev_name)
879 if (!consumer_dev_name && consumer_dev)
880 consumer_dev_name = dev_name(consumer_dev);
885 list_for_each_entry(node, ®ulator_map_list, list) {
886 if (consumer_dev_name != node->dev_name)
888 if (strcmp(node->supply, supply) != 0)
891 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
892 dev_name(&node->regulator->dev),
893 node->regulator->desc->name,
895 dev_name(&rdev->dev), rdev->desc->name);
899 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
903 node->regulator = rdev;
904 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
905 node->supply = supply;
907 if (node->dev_name == NULL) {
912 list_add(&node->list, ®ulator_map_list);
916 static void unset_consumer_device_supply(struct regulator_dev *rdev,
917 const char *consumer_dev_name, struct device *consumer_dev)
919 struct regulator_map *node, *n;
921 if (consumer_dev && !consumer_dev_name)
922 consumer_dev_name = dev_name(consumer_dev);
924 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
925 if (rdev != node->regulator)
928 if (consumer_dev_name && node->dev_name &&
929 strcmp(consumer_dev_name, node->dev_name))
932 list_del(&node->list);
933 kfree(node->dev_name);
939 static void unset_regulator_supplies(struct regulator_dev *rdev)
941 struct regulator_map *node, *n;
943 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
944 if (rdev == node->regulator) {
945 list_del(&node->list);
946 kfree(node->dev_name);
953 #define REG_STR_SIZE 32
955 static struct regulator *create_regulator(struct regulator_dev *rdev,
957 const char *supply_name)
959 struct regulator *regulator;
960 char buf[REG_STR_SIZE];
963 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
964 if (regulator == NULL)
967 mutex_lock(&rdev->mutex);
968 regulator->rdev = rdev;
969 list_add(®ulator->list, &rdev->consumer_list);
972 /* create a 'requested_microamps_name' sysfs entry */
973 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
975 if (size >= REG_STR_SIZE)
978 regulator->dev = dev;
979 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
980 if (regulator->dev_attr.attr.name == NULL)
983 regulator->dev_attr.attr.owner = THIS_MODULE;
984 regulator->dev_attr.attr.mode = 0444;
985 regulator->dev_attr.show = device_requested_uA_show;
986 err = device_create_file(dev, ®ulator->dev_attr);
988 printk(KERN_WARNING "%s: could not add regulator_dev"
989 " load sysfs\n", __func__);
993 /* also add a link to the device sysfs entry */
994 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
995 dev->kobj.name, supply_name);
996 if (size >= REG_STR_SIZE)
999 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1000 if (regulator->supply_name == NULL)
1003 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1007 "%s: could not add device link %s err %d\n",
1008 __func__, dev->kobj.name, err);
1009 device_remove_file(dev, ®ulator->dev_attr);
1013 mutex_unlock(&rdev->mutex);
1016 kfree(regulator->supply_name);
1018 device_remove_file(regulator->dev, ®ulator->dev_attr);
1020 kfree(regulator->dev_attr.attr.name);
1022 list_del(®ulator->list);
1024 mutex_unlock(&rdev->mutex);
1028 /* Internal regulator request function */
1029 static struct regulator *_regulator_get(struct device *dev, const char *id,
1032 struct regulator_dev *rdev;
1033 struct regulator_map *map;
1034 struct regulator *regulator = ERR_PTR(-ENODEV);
1035 const char *devname = NULL;
1039 printk(KERN_ERR "regulator: get() with no identifier\n");
1044 devname = dev_name(dev);
1046 mutex_lock(®ulator_list_mutex);
1048 list_for_each_entry(map, ®ulator_map_list, list) {
1049 /* If the mapping has a device set up it must match */
1050 if (map->dev_name &&
1051 (!devname || strcmp(map->dev_name, devname)))
1054 if (strcmp(map->supply, id) == 0) {
1055 rdev = map->regulator;
1059 mutex_unlock(®ulator_list_mutex);
1063 if (rdev->exclusive) {
1064 regulator = ERR_PTR(-EPERM);
1068 if (exclusive && rdev->open_count) {
1069 regulator = ERR_PTR(-EBUSY);
1073 if (!try_module_get(rdev->owner))
1076 regulator = create_regulator(rdev, dev, id);
1077 if (regulator == NULL) {
1078 regulator = ERR_PTR(-ENOMEM);
1079 module_put(rdev->owner);
1084 rdev->exclusive = 1;
1086 ret = _regulator_is_enabled(rdev);
1088 rdev->use_count = 1;
1090 rdev->use_count = 0;
1094 mutex_unlock(®ulator_list_mutex);
1100 * regulator_get - lookup and obtain a reference to a regulator.
1101 * @dev: device for regulator "consumer"
1102 * @id: Supply name or regulator ID.
1104 * Returns a struct regulator corresponding to the regulator producer,
1105 * or IS_ERR() condition containing errno.
1107 * Use of supply names configured via regulator_set_device_supply() is
1108 * strongly encouraged. It is recommended that the supply name used
1109 * should match the name used for the supply and/or the relevant
1110 * device pins in the datasheet.
1112 struct regulator *regulator_get(struct device *dev, const char *id)
1114 return _regulator_get(dev, id, 0);
1116 EXPORT_SYMBOL_GPL(regulator_get);
1119 * regulator_get_exclusive - obtain exclusive access to a regulator.
1120 * @dev: device for regulator "consumer"
1121 * @id: Supply name or regulator ID.
1123 * Returns a struct regulator corresponding to the regulator producer,
1124 * or IS_ERR() condition containing errno. Other consumers will be
1125 * unable to obtain this reference is held and the use count for the
1126 * regulator will be initialised to reflect the current state of the
1129 * This is intended for use by consumers which cannot tolerate shared
1130 * use of the regulator such as those which need to force the
1131 * regulator off for correct operation of the hardware they are
1134 * Use of supply names configured via regulator_set_device_supply() is
1135 * strongly encouraged. It is recommended that the supply name used
1136 * should match the name used for the supply and/or the relevant
1137 * device pins in the datasheet.
1139 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1141 return _regulator_get(dev, id, 1);
1143 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1146 * regulator_put - "free" the regulator source
1147 * @regulator: regulator source
1149 * Note: drivers must ensure that all regulator_enable calls made on this
1150 * regulator source are balanced by regulator_disable calls prior to calling
1153 void regulator_put(struct regulator *regulator)
1155 struct regulator_dev *rdev;
1157 if (regulator == NULL || IS_ERR(regulator))
1160 mutex_lock(®ulator_list_mutex);
1161 rdev = regulator->rdev;
1163 /* remove any sysfs entries */
1164 if (regulator->dev) {
1165 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1166 kfree(regulator->supply_name);
1167 device_remove_file(regulator->dev, ®ulator->dev_attr);
1168 kfree(regulator->dev_attr.attr.name);
1170 list_del(®ulator->list);
1174 rdev->exclusive = 0;
1176 module_put(rdev->owner);
1177 mutex_unlock(®ulator_list_mutex);
1179 EXPORT_SYMBOL_GPL(regulator_put);
1181 /* locks held by regulator_enable() */
1182 static int _regulator_enable(struct regulator_dev *rdev)
1186 if (!rdev->constraints) {
1187 printk(KERN_ERR "%s: %s has no constraints\n",
1188 __func__, rdev->desc->name);
1192 /* do we need to enable the supply regulator first */
1194 ret = _regulator_enable(rdev->supply);
1196 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1197 __func__, rdev->desc->name, ret);
1202 /* check voltage and requested load before enabling */
1203 if (rdev->desc->ops->enable) {
1205 if (rdev->constraints &&
1206 (rdev->constraints->valid_ops_mask &
1207 REGULATOR_CHANGE_DRMS))
1208 drms_uA_update(rdev);
1210 ret = rdev->desc->ops->enable(rdev);
1212 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1213 __func__, rdev->desc->name, ret);
1224 * regulator_enable - enable regulator output
1225 * @regulator: regulator source
1227 * Request that the regulator be enabled with the regulator output at
1228 * the predefined voltage or current value. Calls to regulator_enable()
1229 * must be balanced with calls to regulator_disable().
1231 * NOTE: the output value can be set by other drivers, boot loader or may be
1232 * hardwired in the regulator.
1234 int regulator_enable(struct regulator *regulator)
1236 struct regulator_dev *rdev = regulator->rdev;
1239 mutex_lock(&rdev->mutex);
1240 ret = _regulator_enable(rdev);
1241 mutex_unlock(&rdev->mutex);
1244 EXPORT_SYMBOL_GPL(regulator_enable);
1246 /* locks held by regulator_disable() */
1247 static int _regulator_disable(struct regulator_dev *rdev)
1251 if (WARN(rdev->use_count <= 0,
1252 "unbalanced disables for %s\n",
1256 /* are we the last user and permitted to disable ? */
1257 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1259 /* we are last user */
1260 if (rdev->desc->ops->disable) {
1261 ret = rdev->desc->ops->disable(rdev);
1263 printk(KERN_ERR "%s: failed to disable %s\n",
1264 __func__, rdev->desc->name);
1269 /* decrease our supplies ref count and disable if required */
1271 _regulator_disable(rdev->supply);
1273 rdev->use_count = 0;
1274 } else if (rdev->use_count > 1) {
1276 if (rdev->constraints &&
1277 (rdev->constraints->valid_ops_mask &
1278 REGULATOR_CHANGE_DRMS))
1279 drms_uA_update(rdev);
1287 * regulator_disable - disable regulator output
1288 * @regulator: regulator source
1290 * Disable the regulator output voltage or current. Calls to
1291 * regulator_enable() must be balanced with calls to
1292 * regulator_disable().
1294 * NOTE: this will only disable the regulator output if no other consumer
1295 * devices have it enabled, the regulator device supports disabling and
1296 * machine constraints permit this operation.
1298 int regulator_disable(struct regulator *regulator)
1300 struct regulator_dev *rdev = regulator->rdev;
1303 mutex_lock(&rdev->mutex);
1304 ret = _regulator_disable(rdev);
1305 mutex_unlock(&rdev->mutex);
1308 EXPORT_SYMBOL_GPL(regulator_disable);
1310 /* locks held by regulator_force_disable() */
1311 static int _regulator_force_disable(struct regulator_dev *rdev)
1316 if (rdev->desc->ops->disable) {
1317 /* ah well, who wants to live forever... */
1318 ret = rdev->desc->ops->disable(rdev);
1320 printk(KERN_ERR "%s: failed to force disable %s\n",
1321 __func__, rdev->desc->name);
1324 /* notify other consumers that power has been forced off */
1325 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1329 /* decrease our supplies ref count and disable if required */
1331 _regulator_disable(rdev->supply);
1333 rdev->use_count = 0;
1338 * regulator_force_disable - force disable regulator output
1339 * @regulator: regulator source
1341 * Forcibly disable the regulator output voltage or current.
1342 * NOTE: this *will* disable the regulator output even if other consumer
1343 * devices have it enabled. This should be used for situations when device
1344 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1346 int regulator_force_disable(struct regulator *regulator)
1350 mutex_lock(®ulator->rdev->mutex);
1351 regulator->uA_load = 0;
1352 ret = _regulator_force_disable(regulator->rdev);
1353 mutex_unlock(®ulator->rdev->mutex);
1356 EXPORT_SYMBOL_GPL(regulator_force_disable);
1358 static int _regulator_is_enabled(struct regulator_dev *rdev)
1362 mutex_lock(&rdev->mutex);
1365 if (!rdev->desc->ops->is_enabled) {
1370 ret = rdev->desc->ops->is_enabled(rdev);
1372 mutex_unlock(&rdev->mutex);
1377 * regulator_is_enabled - is the regulator output enabled
1378 * @regulator: regulator source
1380 * Returns positive if the regulator driver backing the source/client
1381 * has requested that the device be enabled, zero if it hasn't, else a
1382 * negative errno code.
1384 * Note that the device backing this regulator handle can have multiple
1385 * users, so it might be enabled even if regulator_enable() was never
1386 * called for this particular source.
1388 int regulator_is_enabled(struct regulator *regulator)
1390 return _regulator_is_enabled(regulator->rdev);
1392 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1395 * regulator_count_voltages - count regulator_list_voltage() selectors
1396 * @regulator: regulator source
1398 * Returns number of selectors, or negative errno. Selectors are
1399 * numbered starting at zero, and typically correspond to bitfields
1400 * in hardware registers.
1402 int regulator_count_voltages(struct regulator *regulator)
1404 struct regulator_dev *rdev = regulator->rdev;
1406 return rdev->desc->n_voltages ? : -EINVAL;
1408 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1411 * regulator_list_voltage - enumerate supported voltages
1412 * @regulator: regulator source
1413 * @selector: identify voltage to list
1414 * Context: can sleep
1416 * Returns a voltage that can be passed to @regulator_set_voltage(),
1417 * zero if this selector code can't be used on this sytem, or a
1420 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1422 struct regulator_dev *rdev = regulator->rdev;
1423 struct regulator_ops *ops = rdev->desc->ops;
1426 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1429 mutex_lock(&rdev->mutex);
1430 ret = ops->list_voltage(rdev, selector);
1431 mutex_unlock(&rdev->mutex);
1434 if (ret < rdev->constraints->min_uV)
1436 else if (ret > rdev->constraints->max_uV)
1442 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1445 * regulator_is_supported_voltage - check if a voltage range can be supported
1447 * @regulator: Regulator to check.
1448 * @min_uV: Minimum required voltage in uV.
1449 * @max_uV: Maximum required voltage in uV.
1451 * Returns a boolean or a negative error code.
1453 int regulator_is_supported_voltage(struct regulator *regulator,
1454 int min_uV, int max_uV)
1456 int i, voltages, ret;
1458 ret = regulator_count_voltages(regulator);
1463 for (i = 0; i < voltages; i++) {
1464 ret = regulator_list_voltage(regulator, i);
1466 if (ret >= min_uV && ret <= max_uV)
1474 * regulator_set_voltage - set regulator output voltage
1475 * @regulator: regulator source
1476 * @min_uV: Minimum required voltage in uV
1477 * @max_uV: Maximum acceptable voltage in uV
1479 * Sets a voltage regulator to the desired output voltage. This can be set
1480 * during any regulator state. IOW, regulator can be disabled or enabled.
1482 * If the regulator is enabled then the voltage will change to the new value
1483 * immediately otherwise if the regulator is disabled the regulator will
1484 * output at the new voltage when enabled.
1486 * NOTE: If the regulator is shared between several devices then the lowest
1487 * request voltage that meets the system constraints will be used.
1488 * Regulator system constraints must be set for this regulator before
1489 * calling this function otherwise this call will fail.
1491 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1493 struct regulator_dev *rdev = regulator->rdev;
1496 mutex_lock(&rdev->mutex);
1499 if (!rdev->desc->ops->set_voltage) {
1504 /* constraints check */
1505 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1508 regulator->min_uV = min_uV;
1509 regulator->max_uV = max_uV;
1510 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1513 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1514 mutex_unlock(&rdev->mutex);
1517 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1519 static int _regulator_get_voltage(struct regulator_dev *rdev)
1522 if (rdev->desc->ops->get_voltage)
1523 return rdev->desc->ops->get_voltage(rdev);
1529 * regulator_get_voltage - get regulator output voltage
1530 * @regulator: regulator source
1532 * This returns the current regulator voltage in uV.
1534 * NOTE: If the regulator is disabled it will return the voltage value. This
1535 * function should not be used to determine regulator state.
1537 int regulator_get_voltage(struct regulator *regulator)
1541 mutex_lock(®ulator->rdev->mutex);
1543 ret = _regulator_get_voltage(regulator->rdev);
1545 mutex_unlock(®ulator->rdev->mutex);
1549 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1552 * regulator_set_current_limit - set regulator output current limit
1553 * @regulator: regulator source
1554 * @min_uA: Minimuum supported current in uA
1555 * @max_uA: Maximum supported current in uA
1557 * Sets current sink to the desired output current. This can be set during
1558 * any regulator state. IOW, regulator can be disabled or enabled.
1560 * If the regulator is enabled then the current will change to the new value
1561 * immediately otherwise if the regulator is disabled the regulator will
1562 * output at the new current when enabled.
1564 * NOTE: Regulator system constraints must be set for this regulator before
1565 * calling this function otherwise this call will fail.
1567 int regulator_set_current_limit(struct regulator *regulator,
1568 int min_uA, int max_uA)
1570 struct regulator_dev *rdev = regulator->rdev;
1573 mutex_lock(&rdev->mutex);
1576 if (!rdev->desc->ops->set_current_limit) {
1581 /* constraints check */
1582 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1586 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1588 mutex_unlock(&rdev->mutex);
1591 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1593 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1597 mutex_lock(&rdev->mutex);
1600 if (!rdev->desc->ops->get_current_limit) {
1605 ret = rdev->desc->ops->get_current_limit(rdev);
1607 mutex_unlock(&rdev->mutex);
1612 * regulator_get_current_limit - get regulator output current
1613 * @regulator: regulator source
1615 * This returns the current supplied by the specified current sink in uA.
1617 * NOTE: If the regulator is disabled it will return the current value. This
1618 * function should not be used to determine regulator state.
1620 int regulator_get_current_limit(struct regulator *regulator)
1622 return _regulator_get_current_limit(regulator->rdev);
1624 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1627 * regulator_set_mode - set regulator operating mode
1628 * @regulator: regulator source
1629 * @mode: operating mode - one of the REGULATOR_MODE constants
1631 * Set regulator operating mode to increase regulator efficiency or improve
1632 * regulation performance.
1634 * NOTE: Regulator system constraints must be set for this regulator before
1635 * calling this function otherwise this call will fail.
1637 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1639 struct regulator_dev *rdev = regulator->rdev;
1642 mutex_lock(&rdev->mutex);
1645 if (!rdev->desc->ops->set_mode) {
1650 /* constraints check */
1651 ret = regulator_check_mode(rdev, mode);
1655 ret = rdev->desc->ops->set_mode(rdev, mode);
1657 mutex_unlock(&rdev->mutex);
1660 EXPORT_SYMBOL_GPL(regulator_set_mode);
1662 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1666 mutex_lock(&rdev->mutex);
1669 if (!rdev->desc->ops->get_mode) {
1674 ret = rdev->desc->ops->get_mode(rdev);
1676 mutex_unlock(&rdev->mutex);
1681 * regulator_get_mode - get regulator operating mode
1682 * @regulator: regulator source
1684 * Get the current regulator operating mode.
1686 unsigned int regulator_get_mode(struct regulator *regulator)
1688 return _regulator_get_mode(regulator->rdev);
1690 EXPORT_SYMBOL_GPL(regulator_get_mode);
1693 * regulator_set_optimum_mode - set regulator optimum operating mode
1694 * @regulator: regulator source
1695 * @uA_load: load current
1697 * Notifies the regulator core of a new device load. This is then used by
1698 * DRMS (if enabled by constraints) to set the most efficient regulator
1699 * operating mode for the new regulator loading.
1701 * Consumer devices notify their supply regulator of the maximum power
1702 * they will require (can be taken from device datasheet in the power
1703 * consumption tables) when they change operational status and hence power
1704 * state. Examples of operational state changes that can affect power
1705 * consumption are :-
1707 * o Device is opened / closed.
1708 * o Device I/O is about to begin or has just finished.
1709 * o Device is idling in between work.
1711 * This information is also exported via sysfs to userspace.
1713 * DRMS will sum the total requested load on the regulator and change
1714 * to the most efficient operating mode if platform constraints allow.
1716 * Returns the new regulator mode or error.
1718 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1720 struct regulator_dev *rdev = regulator->rdev;
1721 struct regulator *consumer;
1722 int ret, output_uV, input_uV, total_uA_load = 0;
1725 mutex_lock(&rdev->mutex);
1727 regulator->uA_load = uA_load;
1728 ret = regulator_check_drms(rdev);
1734 if (!rdev->desc->ops->get_optimum_mode)
1737 /* get output voltage */
1738 output_uV = rdev->desc->ops->get_voltage(rdev);
1739 if (output_uV <= 0) {
1740 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1741 __func__, rdev->desc->name);
1745 /* get input voltage */
1746 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1747 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1749 input_uV = rdev->constraints->input_uV;
1750 if (input_uV <= 0) {
1751 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1752 __func__, rdev->desc->name);
1756 /* calc total requested load for this regulator */
1757 list_for_each_entry(consumer, &rdev->consumer_list, list)
1758 total_uA_load += consumer->uA_load;
1760 mode = rdev->desc->ops->get_optimum_mode(rdev,
1761 input_uV, output_uV,
1763 ret = regulator_check_mode(rdev, mode);
1765 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1766 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1767 total_uA_load, input_uV, output_uV);
1771 ret = rdev->desc->ops->set_mode(rdev, mode);
1773 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1774 __func__, mode, rdev->desc->name);
1779 mutex_unlock(&rdev->mutex);
1782 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1785 * regulator_register_notifier - register regulator event notifier
1786 * @regulator: regulator source
1787 * @nb: notifier block
1789 * Register notifier block to receive regulator events.
1791 int regulator_register_notifier(struct regulator *regulator,
1792 struct notifier_block *nb)
1794 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1797 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1800 * regulator_unregister_notifier - unregister regulator event notifier
1801 * @regulator: regulator source
1802 * @nb: notifier block
1804 * Unregister regulator event notifier block.
1806 int regulator_unregister_notifier(struct regulator *regulator,
1807 struct notifier_block *nb)
1809 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1812 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1814 /* notify regulator consumers and downstream regulator consumers.
1815 * Note mutex must be held by caller.
1817 static void _notifier_call_chain(struct regulator_dev *rdev,
1818 unsigned long event, void *data)
1820 struct regulator_dev *_rdev;
1822 /* call rdev chain first */
1823 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1825 /* now notify regulator we supply */
1826 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1827 mutex_lock(&_rdev->mutex);
1828 _notifier_call_chain(_rdev, event, data);
1829 mutex_unlock(&_rdev->mutex);
1834 * regulator_bulk_get - get multiple regulator consumers
1836 * @dev: Device to supply
1837 * @num_consumers: Number of consumers to register
1838 * @consumers: Configuration of consumers; clients are stored here.
1840 * @return 0 on success, an errno on failure.
1842 * This helper function allows drivers to get several regulator
1843 * consumers in one operation. If any of the regulators cannot be
1844 * acquired then any regulators that were allocated will be freed
1845 * before returning to the caller.
1847 int regulator_bulk_get(struct device *dev, int num_consumers,
1848 struct regulator_bulk_data *consumers)
1853 for (i = 0; i < num_consumers; i++)
1854 consumers[i].consumer = NULL;
1856 for (i = 0; i < num_consumers; i++) {
1857 consumers[i].consumer = regulator_get(dev,
1858 consumers[i].supply);
1859 if (IS_ERR(consumers[i].consumer)) {
1860 dev_err(dev, "Failed to get supply '%s'\n",
1861 consumers[i].supply);
1862 ret = PTR_ERR(consumers[i].consumer);
1863 consumers[i].consumer = NULL;
1871 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1872 regulator_put(consumers[i].consumer);
1876 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1879 * regulator_bulk_enable - enable multiple regulator consumers
1881 * @num_consumers: Number of consumers
1882 * @consumers: Consumer data; clients are stored here.
1883 * @return 0 on success, an errno on failure
1885 * This convenience API allows consumers to enable multiple regulator
1886 * clients in a single API call. If any consumers cannot be enabled
1887 * then any others that were enabled will be disabled again prior to
1890 int regulator_bulk_enable(int num_consumers,
1891 struct regulator_bulk_data *consumers)
1896 for (i = 0; i < num_consumers; i++) {
1897 ret = regulator_enable(consumers[i].consumer);
1905 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1906 for (i = 0; i < num_consumers; i++)
1907 regulator_disable(consumers[i].consumer);
1911 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1914 * regulator_bulk_disable - disable multiple regulator consumers
1916 * @num_consumers: Number of consumers
1917 * @consumers: Consumer data; clients are stored here.
1918 * @return 0 on success, an errno on failure
1920 * This convenience API allows consumers to disable multiple regulator
1921 * clients in a single API call. If any consumers cannot be enabled
1922 * then any others that were disabled will be disabled again prior to
1925 int regulator_bulk_disable(int num_consumers,
1926 struct regulator_bulk_data *consumers)
1931 for (i = 0; i < num_consumers; i++) {
1932 ret = regulator_disable(consumers[i].consumer);
1940 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1941 for (i = 0; i < num_consumers; i++)
1942 regulator_enable(consumers[i].consumer);
1946 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1949 * regulator_bulk_free - free multiple regulator consumers
1951 * @num_consumers: Number of consumers
1952 * @consumers: Consumer data; clients are stored here.
1954 * This convenience API allows consumers to free multiple regulator
1955 * clients in a single API call.
1957 void regulator_bulk_free(int num_consumers,
1958 struct regulator_bulk_data *consumers)
1962 for (i = 0; i < num_consumers; i++) {
1963 regulator_put(consumers[i].consumer);
1964 consumers[i].consumer = NULL;
1967 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1970 * regulator_notifier_call_chain - call regulator event notifier
1971 * @rdev: regulator source
1972 * @event: notifier block
1973 * @data: callback-specific data.
1975 * Called by regulator drivers to notify clients a regulator event has
1976 * occurred. We also notify regulator clients downstream.
1977 * Note lock must be held by caller.
1979 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1980 unsigned long event, void *data)
1982 _notifier_call_chain(rdev, event, data);
1986 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1989 * regulator_mode_to_status - convert a regulator mode into a status
1991 * @mode: Mode to convert
1993 * Convert a regulator mode into a status.
1995 int regulator_mode_to_status(unsigned int mode)
1998 case REGULATOR_MODE_FAST:
1999 return REGULATOR_STATUS_FAST;
2000 case REGULATOR_MODE_NORMAL:
2001 return REGULATOR_STATUS_NORMAL;
2002 case REGULATOR_MODE_IDLE:
2003 return REGULATOR_STATUS_IDLE;
2004 case REGULATOR_STATUS_STANDBY:
2005 return REGULATOR_STATUS_STANDBY;
2010 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2013 * To avoid cluttering sysfs (and memory) with useless state, only
2014 * create attributes that can be meaningfully displayed.
2016 static int add_regulator_attributes(struct regulator_dev *rdev)
2018 struct device *dev = &rdev->dev;
2019 struct regulator_ops *ops = rdev->desc->ops;
2022 /* some attributes need specific methods to be displayed */
2023 if (ops->get_voltage) {
2024 status = device_create_file(dev, &dev_attr_microvolts);
2028 if (ops->get_current_limit) {
2029 status = device_create_file(dev, &dev_attr_microamps);
2033 if (ops->get_mode) {
2034 status = device_create_file(dev, &dev_attr_opmode);
2038 if (ops->is_enabled) {
2039 status = device_create_file(dev, &dev_attr_state);
2043 if (ops->get_status) {
2044 status = device_create_file(dev, &dev_attr_status);
2049 /* some attributes are type-specific */
2050 if (rdev->desc->type == REGULATOR_CURRENT) {
2051 status = device_create_file(dev, &dev_attr_requested_microamps);
2056 /* all the other attributes exist to support constraints;
2057 * don't show them if there are no constraints, or if the
2058 * relevant supporting methods are missing.
2060 if (!rdev->constraints)
2063 /* constraints need specific supporting methods */
2064 if (ops->set_voltage) {
2065 status = device_create_file(dev, &dev_attr_min_microvolts);
2068 status = device_create_file(dev, &dev_attr_max_microvolts);
2072 if (ops->set_current_limit) {
2073 status = device_create_file(dev, &dev_attr_min_microamps);
2076 status = device_create_file(dev, &dev_attr_max_microamps);
2081 /* suspend mode constraints need multiple supporting methods */
2082 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2085 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2088 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2091 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2095 if (ops->set_suspend_voltage) {
2096 status = device_create_file(dev,
2097 &dev_attr_suspend_standby_microvolts);
2100 status = device_create_file(dev,
2101 &dev_attr_suspend_mem_microvolts);
2104 status = device_create_file(dev,
2105 &dev_attr_suspend_disk_microvolts);
2110 if (ops->set_suspend_mode) {
2111 status = device_create_file(dev,
2112 &dev_attr_suspend_standby_mode);
2115 status = device_create_file(dev,
2116 &dev_attr_suspend_mem_mode);
2119 status = device_create_file(dev,
2120 &dev_attr_suspend_disk_mode);
2129 * regulator_register - register regulator
2130 * @regulator_desc: regulator to register
2131 * @dev: struct device for the regulator
2132 * @init_data: platform provided init data, passed through by driver
2133 * @driver_data: private regulator data
2135 * Called by regulator drivers to register a regulator.
2136 * Returns 0 on success.
2138 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2139 struct device *dev, struct regulator_init_data *init_data,
2142 static atomic_t regulator_no = ATOMIC_INIT(0);
2143 struct regulator_dev *rdev;
2146 if (regulator_desc == NULL)
2147 return ERR_PTR(-EINVAL);
2149 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2150 return ERR_PTR(-EINVAL);
2152 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2153 regulator_desc->type != REGULATOR_CURRENT)
2154 return ERR_PTR(-EINVAL);
2157 return ERR_PTR(-EINVAL);
2159 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2161 return ERR_PTR(-ENOMEM);
2163 mutex_lock(®ulator_list_mutex);
2165 mutex_init(&rdev->mutex);
2166 rdev->reg_data = driver_data;
2167 rdev->owner = regulator_desc->owner;
2168 rdev->desc = regulator_desc;
2169 INIT_LIST_HEAD(&rdev->consumer_list);
2170 INIT_LIST_HEAD(&rdev->supply_list);
2171 INIT_LIST_HEAD(&rdev->list);
2172 INIT_LIST_HEAD(&rdev->slist);
2173 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2175 /* preform any regulator specific init */
2176 if (init_data->regulator_init) {
2177 ret = init_data->regulator_init(rdev->reg_data);
2182 /* register with sysfs */
2183 rdev->dev.class = ®ulator_class;
2184 rdev->dev.parent = dev;
2185 dev_set_name(&rdev->dev, "regulator.%d",
2186 atomic_inc_return(®ulator_no) - 1);
2187 ret = device_register(&rdev->dev);
2191 dev_set_drvdata(&rdev->dev, rdev);
2193 /* set regulator constraints */
2194 ret = set_machine_constraints(rdev, &init_data->constraints);
2198 /* add attributes supported by this regulator */
2199 ret = add_regulator_attributes(rdev);
2203 /* set supply regulator if it exists */
2204 if (init_data->supply_regulator_dev) {
2205 ret = set_supply(rdev,
2206 dev_get_drvdata(init_data->supply_regulator_dev));
2211 /* add consumers devices */
2212 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2213 ret = set_consumer_device_supply(rdev,
2214 init_data->consumer_supplies[i].dev,
2215 init_data->consumer_supplies[i].dev_name,
2216 init_data->consumer_supplies[i].supply);
2218 for (--i; i >= 0; i--)
2219 unset_consumer_device_supply(rdev,
2220 init_data->consumer_supplies[i].dev_name,
2221 init_data->consumer_supplies[i].dev);
2226 list_add(&rdev->list, ®ulator_list);
2228 mutex_unlock(®ulator_list_mutex);
2232 device_unregister(&rdev->dev);
2233 /* device core frees rdev */
2234 rdev = ERR_PTR(ret);
2239 rdev = ERR_PTR(ret);
2242 EXPORT_SYMBOL_GPL(regulator_register);
2245 * regulator_unregister - unregister regulator
2246 * @rdev: regulator to unregister
2248 * Called by regulator drivers to unregister a regulator.
2250 void regulator_unregister(struct regulator_dev *rdev)
2255 mutex_lock(®ulator_list_mutex);
2256 WARN_ON(rdev->open_count);
2257 unset_regulator_supplies(rdev);
2258 list_del(&rdev->list);
2260 sysfs_remove_link(&rdev->dev.kobj, "supply");
2261 device_unregister(&rdev->dev);
2262 mutex_unlock(®ulator_list_mutex);
2264 EXPORT_SYMBOL_GPL(regulator_unregister);
2267 * regulator_suspend_prepare - prepare regulators for system wide suspend
2268 * @state: system suspend state
2270 * Configure each regulator with it's suspend operating parameters for state.
2271 * This will usually be called by machine suspend code prior to supending.
2273 int regulator_suspend_prepare(suspend_state_t state)
2275 struct regulator_dev *rdev;
2278 /* ON is handled by regulator active state */
2279 if (state == PM_SUSPEND_ON)
2282 mutex_lock(®ulator_list_mutex);
2283 list_for_each_entry(rdev, ®ulator_list, list) {
2285 mutex_lock(&rdev->mutex);
2286 ret = suspend_prepare(rdev, state);
2287 mutex_unlock(&rdev->mutex);
2290 printk(KERN_ERR "%s: failed to prepare %s\n",
2291 __func__, rdev->desc->name);
2296 mutex_unlock(®ulator_list_mutex);
2299 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2302 * regulator_has_full_constraints - the system has fully specified constraints
2304 * Calling this function will cause the regulator API to disable all
2305 * regulators which have a zero use count and don't have an always_on
2306 * constraint in a late_initcall.
2308 * The intention is that this will become the default behaviour in a
2309 * future kernel release so users are encouraged to use this facility
2312 void regulator_has_full_constraints(void)
2314 has_full_constraints = 1;
2316 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2319 * rdev_get_drvdata - get rdev regulator driver data
2322 * Get rdev regulator driver private data. This call can be used in the
2323 * regulator driver context.
2325 void *rdev_get_drvdata(struct regulator_dev *rdev)
2327 return rdev->reg_data;
2329 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2332 * regulator_get_drvdata - get regulator driver data
2333 * @regulator: regulator
2335 * Get regulator driver private data. This call can be used in the consumer
2336 * driver context when non API regulator specific functions need to be called.
2338 void *regulator_get_drvdata(struct regulator *regulator)
2340 return regulator->rdev->reg_data;
2342 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2345 * regulator_set_drvdata - set regulator driver data
2346 * @regulator: regulator
2349 void regulator_set_drvdata(struct regulator *regulator, void *data)
2351 regulator->rdev->reg_data = data;
2353 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2356 * regulator_get_id - get regulator ID
2359 int rdev_get_id(struct regulator_dev *rdev)
2361 return rdev->desc->id;
2363 EXPORT_SYMBOL_GPL(rdev_get_id);
2365 struct device *rdev_get_dev(struct regulator_dev *rdev)
2369 EXPORT_SYMBOL_GPL(rdev_get_dev);
2371 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2373 return reg_init_data->driver_data;
2375 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2377 static int __init regulator_init(void)
2379 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2380 return class_register(®ulator_class);
2383 /* init early to allow our consumers to complete system booting */
2384 core_initcall(regulator_init);
2386 static int __init regulator_init_complete(void)
2388 struct regulator_dev *rdev;
2389 struct regulator_ops *ops;
2390 struct regulation_constraints *c;
2394 mutex_lock(®ulator_list_mutex);
2396 /* If we have a full configuration then disable any regulators
2397 * which are not in use or always_on. This will become the
2398 * default behaviour in the future.
2400 list_for_each_entry(rdev, ®ulator_list, list) {
2401 ops = rdev->desc->ops;
2402 c = rdev->constraints;
2406 else if (rdev->desc->name)
2407 name = rdev->desc->name;
2411 if (!ops->disable || c->always_on)
2414 mutex_lock(&rdev->mutex);
2416 if (rdev->use_count)
2419 /* If we can't read the status assume it's on. */
2420 if (ops->is_enabled)
2421 enabled = ops->is_enabled(rdev);
2428 if (has_full_constraints) {
2429 /* We log since this may kill the system if it
2431 printk(KERN_INFO "%s: disabling %s\n",
2433 ret = ops->disable(rdev);
2436 "%s: couldn't disable %s: %d\n",
2437 __func__, name, ret);
2440 /* The intention is that in future we will
2441 * assume that full constraints are provided
2442 * so warn even if we aren't going to do
2446 "%s: incomplete constraints, leaving %s on\n",
2451 mutex_unlock(&rdev->mutex);
2454 mutex_unlock(®ulator_list_mutex);
2458 late_initcall(regulator_init_complete);