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 #define pr_fmt(fmt) "%s: " fmt, __func__
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/slab.h>
23 #include <linux/err.h>
24 #include <linux/mutex.h>
25 #include <linux/suspend.h>
26 #include <linux/delay.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/regulator/driver.h>
29 #include <linux/regulator/machine.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/regulator.h>
36 #define rdev_err(rdev, fmt, ...) \
37 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_warn(rdev, fmt, ...) \
39 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_info(rdev, fmt, ...) \
41 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_dbg(rdev, fmt, ...) \
43 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45 static DEFINE_MUTEX(regulator_list_mutex);
46 static LIST_HEAD(regulator_list);
47 static LIST_HEAD(regulator_map_list);
48 static bool has_full_constraints;
49 static bool board_wants_dummy_regulator;
51 #ifdef CONFIG_DEBUG_FS
52 static struct dentry *debugfs_root;
56 * struct regulator_map
58 * Used to provide symbolic supply names to devices.
60 struct regulator_map {
61 struct list_head list;
62 const char *dev_name; /* The dev_name() for the consumer */
64 struct regulator_dev *regulator;
70 * One for each consumer device.
74 struct list_head list;
79 struct device_attribute dev_attr;
80 struct regulator_dev *rdev;
83 static int _regulator_is_enabled(struct regulator_dev *rdev);
84 static int _regulator_disable(struct regulator_dev *rdev,
85 struct regulator_dev **supply_rdev_ptr);
86 static int _regulator_get_voltage(struct regulator_dev *rdev);
87 static int _regulator_get_current_limit(struct regulator_dev *rdev);
88 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
89 static void _notifier_call_chain(struct regulator_dev *rdev,
90 unsigned long event, void *data);
91 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
92 int min_uV, int max_uV);
94 static const char *rdev_get_name(struct regulator_dev *rdev)
96 if (rdev->constraints && rdev->constraints->name)
97 return rdev->constraints->name;
98 else if (rdev->desc->name)
99 return rdev->desc->name;
104 /* gets the regulator for a given consumer device */
105 static struct regulator *get_device_regulator(struct device *dev)
107 struct regulator *regulator = NULL;
108 struct regulator_dev *rdev;
110 mutex_lock(®ulator_list_mutex);
111 list_for_each_entry(rdev, ®ulator_list, list) {
112 mutex_lock(&rdev->mutex);
113 list_for_each_entry(regulator, &rdev->consumer_list, list) {
114 if (regulator->dev == dev) {
115 mutex_unlock(&rdev->mutex);
116 mutex_unlock(®ulator_list_mutex);
120 mutex_unlock(&rdev->mutex);
122 mutex_unlock(®ulator_list_mutex);
126 /* Platform voltage constraint check */
127 static int regulator_check_voltage(struct regulator_dev *rdev,
128 int *min_uV, int *max_uV)
130 BUG_ON(*min_uV > *max_uV);
132 if (!rdev->constraints) {
133 rdev_err(rdev, "no constraints\n");
136 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
137 rdev_err(rdev, "operation not allowed\n");
141 if (*max_uV > rdev->constraints->max_uV)
142 *max_uV = rdev->constraints->max_uV;
143 if (*min_uV < rdev->constraints->min_uV)
144 *min_uV = rdev->constraints->min_uV;
146 if (*min_uV > *max_uV)
152 /* Make sure we select a voltage that suits the needs of all
153 * regulator consumers
155 static int regulator_check_consumers(struct regulator_dev *rdev,
156 int *min_uV, int *max_uV)
158 struct regulator *regulator;
160 list_for_each_entry(regulator, &rdev->consumer_list, list) {
161 if (*max_uV > regulator->max_uV)
162 *max_uV = regulator->max_uV;
163 if (*min_uV < regulator->min_uV)
164 *min_uV = regulator->min_uV;
167 if (*min_uV > *max_uV)
173 /* current constraint check */
174 static int regulator_check_current_limit(struct regulator_dev *rdev,
175 int *min_uA, int *max_uA)
177 BUG_ON(*min_uA > *max_uA);
179 if (!rdev->constraints) {
180 rdev_err(rdev, "no constraints\n");
183 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
184 rdev_err(rdev, "operation not allowed\n");
188 if (*max_uA > rdev->constraints->max_uA)
189 *max_uA = rdev->constraints->max_uA;
190 if (*min_uA < rdev->constraints->min_uA)
191 *min_uA = rdev->constraints->min_uA;
193 if (*min_uA > *max_uA)
199 /* operating mode constraint check */
200 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
203 case REGULATOR_MODE_FAST:
204 case REGULATOR_MODE_NORMAL:
205 case REGULATOR_MODE_IDLE:
206 case REGULATOR_MODE_STANDBY:
212 if (!rdev->constraints) {
213 rdev_err(rdev, "no constraints\n");
216 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
217 rdev_err(rdev, "operation not allowed\n");
221 /* The modes are bitmasks, the most power hungry modes having
222 * the lowest values. If the requested mode isn't supported
223 * try higher modes. */
225 if (rdev->constraints->valid_modes_mask & *mode)
233 /* dynamic regulator mode switching constraint check */
234 static int regulator_check_drms(struct regulator_dev *rdev)
236 if (!rdev->constraints) {
237 rdev_err(rdev, "no constraints\n");
240 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
241 rdev_err(rdev, "operation not allowed\n");
247 static ssize_t device_requested_uA_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
250 struct regulator *regulator;
252 regulator = get_device_regulator(dev);
253 if (regulator == NULL)
256 return sprintf(buf, "%d\n", regulator->uA_load);
259 static ssize_t regulator_uV_show(struct device *dev,
260 struct device_attribute *attr, char *buf)
262 struct regulator_dev *rdev = dev_get_drvdata(dev);
265 mutex_lock(&rdev->mutex);
266 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
267 mutex_unlock(&rdev->mutex);
271 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
273 static ssize_t regulator_uA_show(struct device *dev,
274 struct device_attribute *attr, char *buf)
276 struct regulator_dev *rdev = dev_get_drvdata(dev);
278 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
280 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
282 static ssize_t regulator_name_show(struct device *dev,
283 struct device_attribute *attr, char *buf)
285 struct regulator_dev *rdev = dev_get_drvdata(dev);
287 return sprintf(buf, "%s\n", rdev_get_name(rdev));
290 static ssize_t regulator_print_opmode(char *buf, int mode)
293 case REGULATOR_MODE_FAST:
294 return sprintf(buf, "fast\n");
295 case REGULATOR_MODE_NORMAL:
296 return sprintf(buf, "normal\n");
297 case REGULATOR_MODE_IDLE:
298 return sprintf(buf, "idle\n");
299 case REGULATOR_MODE_STANDBY:
300 return sprintf(buf, "standby\n");
302 return sprintf(buf, "unknown\n");
305 static ssize_t regulator_opmode_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct regulator_dev *rdev = dev_get_drvdata(dev);
310 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
312 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
314 static ssize_t regulator_print_state(char *buf, int state)
317 return sprintf(buf, "enabled\n");
319 return sprintf(buf, "disabled\n");
321 return sprintf(buf, "unknown\n");
324 static ssize_t regulator_state_show(struct device *dev,
325 struct device_attribute *attr, char *buf)
327 struct regulator_dev *rdev = dev_get_drvdata(dev);
330 mutex_lock(&rdev->mutex);
331 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
332 mutex_unlock(&rdev->mutex);
336 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
338 static ssize_t regulator_status_show(struct device *dev,
339 struct device_attribute *attr, char *buf)
341 struct regulator_dev *rdev = dev_get_drvdata(dev);
345 status = rdev->desc->ops->get_status(rdev);
350 case REGULATOR_STATUS_OFF:
353 case REGULATOR_STATUS_ON:
356 case REGULATOR_STATUS_ERROR:
359 case REGULATOR_STATUS_FAST:
362 case REGULATOR_STATUS_NORMAL:
365 case REGULATOR_STATUS_IDLE:
368 case REGULATOR_STATUS_STANDBY:
375 return sprintf(buf, "%s\n", label);
377 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
379 static ssize_t regulator_min_uA_show(struct device *dev,
380 struct device_attribute *attr, char *buf)
382 struct regulator_dev *rdev = dev_get_drvdata(dev);
384 if (!rdev->constraints)
385 return sprintf(buf, "constraint not defined\n");
387 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
389 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
391 static ssize_t regulator_max_uA_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
394 struct regulator_dev *rdev = dev_get_drvdata(dev);
396 if (!rdev->constraints)
397 return sprintf(buf, "constraint not defined\n");
399 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
401 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
403 static ssize_t regulator_min_uV_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
406 struct regulator_dev *rdev = dev_get_drvdata(dev);
408 if (!rdev->constraints)
409 return sprintf(buf, "constraint not defined\n");
411 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
413 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
415 static ssize_t regulator_max_uV_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
418 struct regulator_dev *rdev = dev_get_drvdata(dev);
420 if (!rdev->constraints)
421 return sprintf(buf, "constraint not defined\n");
423 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
425 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
427 static ssize_t regulator_total_uA_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
430 struct regulator_dev *rdev = dev_get_drvdata(dev);
431 struct regulator *regulator;
434 mutex_lock(&rdev->mutex);
435 list_for_each_entry(regulator, &rdev->consumer_list, list)
436 uA += regulator->uA_load;
437 mutex_unlock(&rdev->mutex);
438 return sprintf(buf, "%d\n", uA);
440 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
442 static ssize_t regulator_num_users_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
445 struct regulator_dev *rdev = dev_get_drvdata(dev);
446 return sprintf(buf, "%d\n", rdev->use_count);
449 static ssize_t regulator_type_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
452 struct regulator_dev *rdev = dev_get_drvdata(dev);
454 switch (rdev->desc->type) {
455 case REGULATOR_VOLTAGE:
456 return sprintf(buf, "voltage\n");
457 case REGULATOR_CURRENT:
458 return sprintf(buf, "current\n");
460 return sprintf(buf, "unknown\n");
463 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
466 struct regulator_dev *rdev = dev_get_drvdata(dev);
468 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
470 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
471 regulator_suspend_mem_uV_show, NULL);
473 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
476 struct regulator_dev *rdev = dev_get_drvdata(dev);
478 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
480 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
481 regulator_suspend_disk_uV_show, NULL);
483 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
486 struct regulator_dev *rdev = dev_get_drvdata(dev);
488 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
490 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
491 regulator_suspend_standby_uV_show, NULL);
493 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
496 struct regulator_dev *rdev = dev_get_drvdata(dev);
498 return regulator_print_opmode(buf,
499 rdev->constraints->state_mem.mode);
501 static DEVICE_ATTR(suspend_mem_mode, 0444,
502 regulator_suspend_mem_mode_show, NULL);
504 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
507 struct regulator_dev *rdev = dev_get_drvdata(dev);
509 return regulator_print_opmode(buf,
510 rdev->constraints->state_disk.mode);
512 static DEVICE_ATTR(suspend_disk_mode, 0444,
513 regulator_suspend_disk_mode_show, NULL);
515 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 struct regulator_dev *rdev = dev_get_drvdata(dev);
520 return regulator_print_opmode(buf,
521 rdev->constraints->state_standby.mode);
523 static DEVICE_ATTR(suspend_standby_mode, 0444,
524 regulator_suspend_standby_mode_show, NULL);
526 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
529 struct regulator_dev *rdev = dev_get_drvdata(dev);
531 return regulator_print_state(buf,
532 rdev->constraints->state_mem.enabled);
534 static DEVICE_ATTR(suspend_mem_state, 0444,
535 regulator_suspend_mem_state_show, NULL);
537 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
540 struct regulator_dev *rdev = dev_get_drvdata(dev);
542 return regulator_print_state(buf,
543 rdev->constraints->state_disk.enabled);
545 static DEVICE_ATTR(suspend_disk_state, 0444,
546 regulator_suspend_disk_state_show, NULL);
548 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
549 struct device_attribute *attr, char *buf)
551 struct regulator_dev *rdev = dev_get_drvdata(dev);
553 return regulator_print_state(buf,
554 rdev->constraints->state_standby.enabled);
556 static DEVICE_ATTR(suspend_standby_state, 0444,
557 regulator_suspend_standby_state_show, NULL);
561 * These are the only attributes are present for all regulators.
562 * Other attributes are a function of regulator functionality.
564 static struct device_attribute regulator_dev_attrs[] = {
565 __ATTR(name, 0444, regulator_name_show, NULL),
566 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
567 __ATTR(type, 0444, regulator_type_show, NULL),
571 static void regulator_dev_release(struct device *dev)
573 struct regulator_dev *rdev = dev_get_drvdata(dev);
577 static struct class regulator_class = {
579 .dev_release = regulator_dev_release,
580 .dev_attrs = regulator_dev_attrs,
583 /* Calculate the new optimum regulator operating mode based on the new total
584 * consumer load. All locks held by caller */
585 static void drms_uA_update(struct regulator_dev *rdev)
587 struct regulator *sibling;
588 int current_uA = 0, output_uV, input_uV, err;
591 err = regulator_check_drms(rdev);
592 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
593 (!rdev->desc->ops->get_voltage &&
594 !rdev->desc->ops->get_voltage_sel) ||
595 !rdev->desc->ops->set_mode)
598 /* get output voltage */
599 output_uV = _regulator_get_voltage(rdev);
603 /* get input voltage */
606 input_uV = _regulator_get_voltage(rdev);
608 input_uV = rdev->constraints->input_uV;
612 /* calc total requested load */
613 list_for_each_entry(sibling, &rdev->consumer_list, list)
614 current_uA += sibling->uA_load;
616 /* now get the optimum mode for our new total regulator load */
617 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
618 output_uV, current_uA);
620 /* check the new mode is allowed */
621 err = regulator_mode_constrain(rdev, &mode);
623 rdev->desc->ops->set_mode(rdev, mode);
626 static int suspend_set_state(struct regulator_dev *rdev,
627 struct regulator_state *rstate)
632 can_set_state = rdev->desc->ops->set_suspend_enable &&
633 rdev->desc->ops->set_suspend_disable;
635 /* If we have no suspend mode configration don't set anything;
636 * only warn if the driver actually makes the suspend mode
639 if (!rstate->enabled && !rstate->disabled) {
641 rdev_warn(rdev, "No configuration\n");
645 if (rstate->enabled && rstate->disabled) {
646 rdev_err(rdev, "invalid configuration\n");
650 if (!can_set_state) {
651 rdev_err(rdev, "no way to set suspend state\n");
656 ret = rdev->desc->ops->set_suspend_enable(rdev);
658 ret = rdev->desc->ops->set_suspend_disable(rdev);
660 rdev_err(rdev, "failed to enabled/disable\n");
664 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
665 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
667 rdev_err(rdev, "failed to set voltage\n");
672 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
673 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
675 rdev_err(rdev, "failed to set mode\n");
682 /* locks held by caller */
683 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
685 if (!rdev->constraints)
689 case PM_SUSPEND_STANDBY:
690 return suspend_set_state(rdev,
691 &rdev->constraints->state_standby);
693 return suspend_set_state(rdev,
694 &rdev->constraints->state_mem);
696 return suspend_set_state(rdev,
697 &rdev->constraints->state_disk);
703 static void print_constraints(struct regulator_dev *rdev)
705 struct regulation_constraints *constraints = rdev->constraints;
710 if (constraints->min_uV && constraints->max_uV) {
711 if (constraints->min_uV == constraints->max_uV)
712 count += sprintf(buf + count, "%d mV ",
713 constraints->min_uV / 1000);
715 count += sprintf(buf + count, "%d <--> %d mV ",
716 constraints->min_uV / 1000,
717 constraints->max_uV / 1000);
720 if (!constraints->min_uV ||
721 constraints->min_uV != constraints->max_uV) {
722 ret = _regulator_get_voltage(rdev);
724 count += sprintf(buf + count, "at %d mV ", ret / 1000);
727 if (constraints->min_uA && constraints->max_uA) {
728 if (constraints->min_uA == constraints->max_uA)
729 count += sprintf(buf + count, "%d mA ",
730 constraints->min_uA / 1000);
732 count += sprintf(buf + count, "%d <--> %d mA ",
733 constraints->min_uA / 1000,
734 constraints->max_uA / 1000);
737 if (!constraints->min_uA ||
738 constraints->min_uA != constraints->max_uA) {
739 ret = _regulator_get_current_limit(rdev);
741 count += sprintf(buf + count, "at %d mA ", ret / 1000);
744 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
745 count += sprintf(buf + count, "fast ");
746 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
747 count += sprintf(buf + count, "normal ");
748 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
749 count += sprintf(buf + count, "idle ");
750 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
751 count += sprintf(buf + count, "standby");
753 rdev_info(rdev, "%s\n", buf);
756 static int machine_constraints_voltage(struct regulator_dev *rdev,
757 struct regulation_constraints *constraints)
759 struct regulator_ops *ops = rdev->desc->ops;
762 /* do we need to apply the constraint voltage */
763 if (rdev->constraints->apply_uV &&
764 rdev->constraints->min_uV == rdev->constraints->max_uV) {
765 ret = _regulator_do_set_voltage(rdev,
766 rdev->constraints->min_uV,
767 rdev->constraints->max_uV);
769 rdev_err(rdev, "failed to apply %duV constraint\n",
770 rdev->constraints->min_uV);
771 rdev->constraints = NULL;
776 /* constrain machine-level voltage specs to fit
777 * the actual range supported by this regulator.
779 if (ops->list_voltage && rdev->desc->n_voltages) {
780 int count = rdev->desc->n_voltages;
782 int min_uV = INT_MAX;
783 int max_uV = INT_MIN;
784 int cmin = constraints->min_uV;
785 int cmax = constraints->max_uV;
787 /* it's safe to autoconfigure fixed-voltage supplies
788 and the constraints are used by list_voltage. */
789 if (count == 1 && !cmin) {
792 constraints->min_uV = cmin;
793 constraints->max_uV = cmax;
796 /* voltage constraints are optional */
797 if ((cmin == 0) && (cmax == 0))
800 /* else require explicit machine-level constraints */
801 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
802 rdev_err(rdev, "invalid voltage constraints\n");
806 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
807 for (i = 0; i < count; i++) {
810 value = ops->list_voltage(rdev, i);
814 /* maybe adjust [min_uV..max_uV] */
815 if (value >= cmin && value < min_uV)
817 if (value <= cmax && value > max_uV)
821 /* final: [min_uV..max_uV] valid iff constraints valid */
822 if (max_uV < min_uV) {
823 rdev_err(rdev, "unsupportable voltage constraints\n");
827 /* use regulator's subset of machine constraints */
828 if (constraints->min_uV < min_uV) {
829 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
830 constraints->min_uV, min_uV);
831 constraints->min_uV = min_uV;
833 if (constraints->max_uV > max_uV) {
834 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
835 constraints->max_uV, max_uV);
836 constraints->max_uV = max_uV;
844 * set_machine_constraints - sets regulator constraints
845 * @rdev: regulator source
846 * @constraints: constraints to apply
848 * Allows platform initialisation code to define and constrain
849 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
850 * Constraints *must* be set by platform code in order for some
851 * regulator operations to proceed i.e. set_voltage, set_current_limit,
854 static int set_machine_constraints(struct regulator_dev *rdev,
855 const struct regulation_constraints *constraints)
858 struct regulator_ops *ops = rdev->desc->ops;
860 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
862 if (!rdev->constraints)
865 ret = machine_constraints_voltage(rdev, rdev->constraints);
869 /* do we need to setup our suspend state */
870 if (constraints->initial_state) {
871 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
873 rdev_err(rdev, "failed to set suspend state\n");
874 rdev->constraints = NULL;
879 if (constraints->initial_mode) {
880 if (!ops->set_mode) {
881 rdev_err(rdev, "no set_mode operation\n");
886 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
888 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
893 /* If the constraints say the regulator should be on at this point
894 * and we have control then make sure it is enabled.
896 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
898 ret = ops->enable(rdev);
900 rdev_err(rdev, "failed to enable\n");
901 rdev->constraints = NULL;
906 print_constraints(rdev);
912 * set_supply - set regulator supply regulator
913 * @rdev: regulator name
914 * @supply_rdev: supply regulator name
916 * Called by platform initialisation code to set the supply regulator for this
917 * regulator. This ensures that a regulators supply will also be enabled by the
918 * core if it's child is enabled.
920 static int set_supply(struct regulator_dev *rdev,
921 struct regulator_dev *supply_rdev)
925 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
928 rdev_err(rdev, "could not add device link %s err %d\n",
929 supply_rdev->dev.kobj.name, err);
932 rdev->supply = supply_rdev;
933 list_add(&rdev->slist, &supply_rdev->supply_list);
939 * set_consumer_device_supply - Bind a regulator to a symbolic supply
940 * @rdev: regulator source
941 * @consumer_dev: device the supply applies to
942 * @consumer_dev_name: dev_name() string for device supply applies to
943 * @supply: symbolic name for supply
945 * Allows platform initialisation code to map physical regulator
946 * sources to symbolic names for supplies for use by devices. Devices
947 * should use these symbolic names to request regulators, avoiding the
948 * need to provide board-specific regulator names as platform data.
950 * Only one of consumer_dev and consumer_dev_name may be specified.
952 static int set_consumer_device_supply(struct regulator_dev *rdev,
953 struct device *consumer_dev, const char *consumer_dev_name,
956 struct regulator_map *node;
959 if (consumer_dev && consumer_dev_name)
962 if (!consumer_dev_name && consumer_dev)
963 consumer_dev_name = dev_name(consumer_dev);
968 if (consumer_dev_name != NULL)
973 list_for_each_entry(node, ®ulator_map_list, list) {
974 if (node->dev_name && consumer_dev_name) {
975 if (strcmp(node->dev_name, consumer_dev_name) != 0)
977 } else if (node->dev_name || consumer_dev_name) {
981 if (strcmp(node->supply, supply) != 0)
984 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
985 dev_name(&node->regulator->dev),
986 node->regulator->desc->name,
988 dev_name(&rdev->dev), rdev_get_name(rdev));
992 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
996 node->regulator = rdev;
997 node->supply = supply;
1000 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1001 if (node->dev_name == NULL) {
1007 list_add(&node->list, ®ulator_map_list);
1011 static void unset_regulator_supplies(struct regulator_dev *rdev)
1013 struct regulator_map *node, *n;
1015 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1016 if (rdev == node->regulator) {
1017 list_del(&node->list);
1018 kfree(node->dev_name);
1024 #define REG_STR_SIZE 32
1026 static struct regulator *create_regulator(struct regulator_dev *rdev,
1028 const char *supply_name)
1030 struct regulator *regulator;
1031 char buf[REG_STR_SIZE];
1034 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1035 if (regulator == NULL)
1038 mutex_lock(&rdev->mutex);
1039 regulator->rdev = rdev;
1040 list_add(®ulator->list, &rdev->consumer_list);
1043 /* create a 'requested_microamps_name' sysfs entry */
1044 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1046 if (size >= REG_STR_SIZE)
1049 regulator->dev = dev;
1050 sysfs_attr_init(®ulator->dev_attr.attr);
1051 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1052 if (regulator->dev_attr.attr.name == NULL)
1055 regulator->dev_attr.attr.mode = 0444;
1056 regulator->dev_attr.show = device_requested_uA_show;
1057 err = device_create_file(dev, ®ulator->dev_attr);
1059 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1063 /* also add a link to the device sysfs entry */
1064 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1065 dev->kobj.name, supply_name);
1066 if (size >= REG_STR_SIZE)
1069 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1070 if (regulator->supply_name == NULL)
1073 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1076 rdev_warn(rdev, "could not add device link %s err %d\n",
1077 dev->kobj.name, err);
1081 mutex_unlock(&rdev->mutex);
1084 kfree(regulator->supply_name);
1086 device_remove_file(regulator->dev, ®ulator->dev_attr);
1088 kfree(regulator->dev_attr.attr.name);
1090 list_del(®ulator->list);
1092 mutex_unlock(&rdev->mutex);
1096 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1098 if (!rdev->desc->ops->enable_time)
1100 return rdev->desc->ops->enable_time(rdev);
1103 /* Internal regulator request function */
1104 static struct regulator *_regulator_get(struct device *dev, const char *id,
1107 struct regulator_dev *rdev;
1108 struct regulator_map *map;
1109 struct regulator *regulator = ERR_PTR(-ENODEV);
1110 const char *devname = NULL;
1114 pr_err("get() with no identifier\n");
1119 devname = dev_name(dev);
1121 mutex_lock(®ulator_list_mutex);
1123 list_for_each_entry(map, ®ulator_map_list, list) {
1124 /* If the mapping has a device set up it must match */
1125 if (map->dev_name &&
1126 (!devname || strcmp(map->dev_name, devname)))
1129 if (strcmp(map->supply, id) == 0) {
1130 rdev = map->regulator;
1135 if (board_wants_dummy_regulator) {
1136 rdev = dummy_regulator_rdev;
1140 #ifdef CONFIG_REGULATOR_DUMMY
1142 devname = "deviceless";
1144 /* If the board didn't flag that it was fully constrained then
1145 * substitute in a dummy regulator so consumers can continue.
1147 if (!has_full_constraints) {
1148 pr_warn("%s supply %s not found, using dummy regulator\n",
1150 rdev = dummy_regulator_rdev;
1155 mutex_unlock(®ulator_list_mutex);
1159 if (rdev->exclusive) {
1160 regulator = ERR_PTR(-EPERM);
1164 if (exclusive && rdev->open_count) {
1165 regulator = ERR_PTR(-EBUSY);
1169 if (!try_module_get(rdev->owner))
1172 regulator = create_regulator(rdev, dev, id);
1173 if (regulator == NULL) {
1174 regulator = ERR_PTR(-ENOMEM);
1175 module_put(rdev->owner);
1180 rdev->exclusive = 1;
1182 ret = _regulator_is_enabled(rdev);
1184 rdev->use_count = 1;
1186 rdev->use_count = 0;
1190 mutex_unlock(®ulator_list_mutex);
1196 * regulator_get - lookup and obtain a reference to a regulator.
1197 * @dev: device for regulator "consumer"
1198 * @id: Supply name or regulator ID.
1200 * Returns a struct regulator corresponding to the regulator producer,
1201 * or IS_ERR() condition containing errno.
1203 * Use of supply names configured via regulator_set_device_supply() is
1204 * strongly encouraged. It is recommended that the supply name used
1205 * should match the name used for the supply and/or the relevant
1206 * device pins in the datasheet.
1208 struct regulator *regulator_get(struct device *dev, const char *id)
1210 return _regulator_get(dev, id, 0);
1212 EXPORT_SYMBOL_GPL(regulator_get);
1215 * regulator_get_exclusive - obtain exclusive access to a regulator.
1216 * @dev: device for regulator "consumer"
1217 * @id: Supply name or regulator ID.
1219 * Returns a struct regulator corresponding to the regulator producer,
1220 * or IS_ERR() condition containing errno. Other consumers will be
1221 * unable to obtain this reference is held and the use count for the
1222 * regulator will be initialised to reflect the current state of the
1225 * This is intended for use by consumers which cannot tolerate shared
1226 * use of the regulator such as those which need to force the
1227 * regulator off for correct operation of the hardware they are
1230 * Use of supply names configured via regulator_set_device_supply() is
1231 * strongly encouraged. It is recommended that the supply name used
1232 * should match the name used for the supply and/or the relevant
1233 * device pins in the datasheet.
1235 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1237 return _regulator_get(dev, id, 1);
1239 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1242 * regulator_put - "free" the regulator source
1243 * @regulator: regulator source
1245 * Note: drivers must ensure that all regulator_enable calls made on this
1246 * regulator source are balanced by regulator_disable calls prior to calling
1249 void regulator_put(struct regulator *regulator)
1251 struct regulator_dev *rdev;
1253 if (regulator == NULL || IS_ERR(regulator))
1256 mutex_lock(®ulator_list_mutex);
1257 rdev = regulator->rdev;
1259 /* remove any sysfs entries */
1260 if (regulator->dev) {
1261 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1262 kfree(regulator->supply_name);
1263 device_remove_file(regulator->dev, ®ulator->dev_attr);
1264 kfree(regulator->dev_attr.attr.name);
1266 list_del(®ulator->list);
1270 rdev->exclusive = 0;
1272 module_put(rdev->owner);
1273 mutex_unlock(®ulator_list_mutex);
1275 EXPORT_SYMBOL_GPL(regulator_put);
1277 static int _regulator_can_change_status(struct regulator_dev *rdev)
1279 if (!rdev->constraints)
1282 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1288 /* locks held by regulator_enable() */
1289 static int _regulator_enable(struct regulator_dev *rdev)
1293 if (rdev->use_count == 0) {
1294 /* do we need to enable the supply regulator first */
1296 mutex_lock(&rdev->supply->mutex);
1297 ret = _regulator_enable(rdev->supply);
1298 mutex_unlock(&rdev->supply->mutex);
1300 rdev_err(rdev, "failed to enable: %d\n", ret);
1306 /* check voltage and requested load before enabling */
1307 if (rdev->constraints &&
1308 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1309 drms_uA_update(rdev);
1311 if (rdev->use_count == 0) {
1312 /* The regulator may on if it's not switchable or left on */
1313 ret = _regulator_is_enabled(rdev);
1314 if (ret == -EINVAL || ret == 0) {
1315 if (!_regulator_can_change_status(rdev))
1318 if (!rdev->desc->ops->enable)
1321 /* Query before enabling in case configuration
1323 ret = _regulator_get_enable_time(rdev);
1327 rdev_warn(rdev, "enable_time() failed: %d\n",
1332 trace_regulator_enable(rdev_get_name(rdev));
1334 /* Allow the regulator to ramp; it would be useful
1335 * to extend this for bulk operations so that the
1336 * regulators can ramp together. */
1337 ret = rdev->desc->ops->enable(rdev);
1341 trace_regulator_enable_delay(rdev_get_name(rdev));
1343 if (delay >= 1000) {
1344 mdelay(delay / 1000);
1345 udelay(delay % 1000);
1350 trace_regulator_enable_complete(rdev_get_name(rdev));
1352 } else if (ret < 0) {
1353 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1356 /* Fallthrough on positive return values - already enabled */
1365 * regulator_enable - enable regulator output
1366 * @regulator: regulator source
1368 * Request that the regulator be enabled with the regulator output at
1369 * the predefined voltage or current value. Calls to regulator_enable()
1370 * must be balanced with calls to regulator_disable().
1372 * NOTE: the output value can be set by other drivers, boot loader or may be
1373 * hardwired in the regulator.
1375 int regulator_enable(struct regulator *regulator)
1377 struct regulator_dev *rdev = regulator->rdev;
1380 mutex_lock(&rdev->mutex);
1381 ret = _regulator_enable(rdev);
1382 mutex_unlock(&rdev->mutex);
1385 EXPORT_SYMBOL_GPL(regulator_enable);
1387 /* locks held by regulator_disable() */
1388 static int _regulator_disable(struct regulator_dev *rdev,
1389 struct regulator_dev **supply_rdev_ptr)
1392 *supply_rdev_ptr = NULL;
1394 if (WARN(rdev->use_count <= 0,
1395 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1398 /* are we the last user and permitted to disable ? */
1399 if (rdev->use_count == 1 &&
1400 (rdev->constraints && !rdev->constraints->always_on)) {
1402 /* we are last user */
1403 if (_regulator_can_change_status(rdev) &&
1404 rdev->desc->ops->disable) {
1405 trace_regulator_disable(rdev_get_name(rdev));
1407 ret = rdev->desc->ops->disable(rdev);
1409 rdev_err(rdev, "failed to disable\n");
1413 trace_regulator_disable_complete(rdev_get_name(rdev));
1415 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1419 /* decrease our supplies ref count and disable if required */
1420 *supply_rdev_ptr = rdev->supply;
1422 rdev->use_count = 0;
1423 } else if (rdev->use_count > 1) {
1425 if (rdev->constraints &&
1426 (rdev->constraints->valid_ops_mask &
1427 REGULATOR_CHANGE_DRMS))
1428 drms_uA_update(rdev);
1436 * regulator_disable - disable regulator output
1437 * @regulator: regulator source
1439 * Disable the regulator output voltage or current. Calls to
1440 * regulator_enable() must be balanced with calls to
1441 * regulator_disable().
1443 * NOTE: this will only disable the regulator output if no other consumer
1444 * devices have it enabled, the regulator device supports disabling and
1445 * machine constraints permit this operation.
1447 int regulator_disable(struct regulator *regulator)
1449 struct regulator_dev *rdev = regulator->rdev;
1450 struct regulator_dev *supply_rdev = NULL;
1453 mutex_lock(&rdev->mutex);
1454 ret = _regulator_disable(rdev, &supply_rdev);
1455 mutex_unlock(&rdev->mutex);
1457 /* decrease our supplies ref count and disable if required */
1458 while (supply_rdev != NULL) {
1461 mutex_lock(&rdev->mutex);
1462 _regulator_disable(rdev, &supply_rdev);
1463 mutex_unlock(&rdev->mutex);
1468 EXPORT_SYMBOL_GPL(regulator_disable);
1470 /* locks held by regulator_force_disable() */
1471 static int _regulator_force_disable(struct regulator_dev *rdev,
1472 struct regulator_dev **supply_rdev_ptr)
1477 if (rdev->desc->ops->disable) {
1478 /* ah well, who wants to live forever... */
1479 ret = rdev->desc->ops->disable(rdev);
1481 rdev_err(rdev, "failed to force disable\n");
1484 /* notify other consumers that power has been forced off */
1485 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1486 REGULATOR_EVENT_DISABLE, NULL);
1489 /* decrease our supplies ref count and disable if required */
1490 *supply_rdev_ptr = rdev->supply;
1492 rdev->use_count = 0;
1497 * regulator_force_disable - force disable regulator output
1498 * @regulator: regulator source
1500 * Forcibly disable the regulator output voltage or current.
1501 * NOTE: this *will* disable the regulator output even if other consumer
1502 * devices have it enabled. This should be used for situations when device
1503 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1505 int regulator_force_disable(struct regulator *regulator)
1507 struct regulator_dev *supply_rdev = NULL;
1510 mutex_lock(®ulator->rdev->mutex);
1511 regulator->uA_load = 0;
1512 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
1513 mutex_unlock(®ulator->rdev->mutex);
1516 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1520 EXPORT_SYMBOL_GPL(regulator_force_disable);
1522 static int _regulator_is_enabled(struct regulator_dev *rdev)
1524 /* If we don't know then assume that the regulator is always on */
1525 if (!rdev->desc->ops->is_enabled)
1528 return rdev->desc->ops->is_enabled(rdev);
1532 * regulator_is_enabled - is the regulator output enabled
1533 * @regulator: regulator source
1535 * Returns positive if the regulator driver backing the source/client
1536 * has requested that the device be enabled, zero if it hasn't, else a
1537 * negative errno code.
1539 * Note that the device backing this regulator handle can have multiple
1540 * users, so it might be enabled even if regulator_enable() was never
1541 * called for this particular source.
1543 int regulator_is_enabled(struct regulator *regulator)
1547 mutex_lock(®ulator->rdev->mutex);
1548 ret = _regulator_is_enabled(regulator->rdev);
1549 mutex_unlock(®ulator->rdev->mutex);
1553 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1556 * regulator_count_voltages - count regulator_list_voltage() selectors
1557 * @regulator: regulator source
1559 * Returns number of selectors, or negative errno. Selectors are
1560 * numbered starting at zero, and typically correspond to bitfields
1561 * in hardware registers.
1563 int regulator_count_voltages(struct regulator *regulator)
1565 struct regulator_dev *rdev = regulator->rdev;
1567 return rdev->desc->n_voltages ? : -EINVAL;
1569 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1572 * regulator_list_voltage - enumerate supported voltages
1573 * @regulator: regulator source
1574 * @selector: identify voltage to list
1575 * Context: can sleep
1577 * Returns a voltage that can be passed to @regulator_set_voltage(),
1578 * zero if this selector code can't be used on this system, or a
1581 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1583 struct regulator_dev *rdev = regulator->rdev;
1584 struct regulator_ops *ops = rdev->desc->ops;
1587 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1590 mutex_lock(&rdev->mutex);
1591 ret = ops->list_voltage(rdev, selector);
1592 mutex_unlock(&rdev->mutex);
1595 if (ret < rdev->constraints->min_uV)
1597 else if (ret > rdev->constraints->max_uV)
1603 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1606 * regulator_is_supported_voltage - check if a voltage range can be supported
1608 * @regulator: Regulator to check.
1609 * @min_uV: Minimum required voltage in uV.
1610 * @max_uV: Maximum required voltage in uV.
1612 * Returns a boolean or a negative error code.
1614 int regulator_is_supported_voltage(struct regulator *regulator,
1615 int min_uV, int max_uV)
1617 int i, voltages, ret;
1619 ret = regulator_count_voltages(regulator);
1624 for (i = 0; i < voltages; i++) {
1625 ret = regulator_list_voltage(regulator, i);
1627 if (ret >= min_uV && ret <= max_uV)
1634 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1635 int min_uV, int max_uV)
1639 unsigned int selector;
1641 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1643 if (rdev->desc->ops->set_voltage) {
1644 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1647 if (rdev->desc->ops->list_voltage)
1648 selector = rdev->desc->ops->list_voltage(rdev,
1652 } else if (rdev->desc->ops->set_voltage_sel) {
1653 int best_val = INT_MAX;
1658 /* Find the smallest voltage that falls within the specified
1661 for (i = 0; i < rdev->desc->n_voltages; i++) {
1662 ret = rdev->desc->ops->list_voltage(rdev, i);
1666 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1673 * If we can't obtain the old selector there is not enough
1674 * info to call set_voltage_time_sel().
1676 if (rdev->desc->ops->set_voltage_time_sel &&
1677 rdev->desc->ops->get_voltage_sel) {
1678 unsigned int old_selector = 0;
1680 ret = rdev->desc->ops->get_voltage_sel(rdev);
1684 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1685 old_selector, selector);
1688 if (best_val != INT_MAX) {
1689 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1690 selector = best_val;
1698 /* Insert any necessary delays */
1699 if (delay >= 1000) {
1700 mdelay(delay / 1000);
1701 udelay(delay % 1000);
1707 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1710 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1716 * regulator_set_voltage - set regulator output voltage
1717 * @regulator: regulator source
1718 * @min_uV: Minimum required voltage in uV
1719 * @max_uV: Maximum acceptable voltage in uV
1721 * Sets a voltage regulator to the desired output voltage. This can be set
1722 * during any regulator state. IOW, regulator can be disabled or enabled.
1724 * If the regulator is enabled then the voltage will change to the new value
1725 * immediately otherwise if the regulator is disabled the regulator will
1726 * output at the new voltage when enabled.
1728 * NOTE: If the regulator is shared between several devices then the lowest
1729 * request voltage that meets the system constraints will be used.
1730 * Regulator system constraints must be set for this regulator before
1731 * calling this function otherwise this call will fail.
1733 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1735 struct regulator_dev *rdev = regulator->rdev;
1738 mutex_lock(&rdev->mutex);
1740 /* If we're setting the same range as last time the change
1741 * should be a noop (some cpufreq implementations use the same
1742 * voltage for multiple frequencies, for example).
1744 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1748 if (!rdev->desc->ops->set_voltage &&
1749 !rdev->desc->ops->set_voltage_sel) {
1754 /* constraints check */
1755 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1758 regulator->min_uV = min_uV;
1759 regulator->max_uV = max_uV;
1761 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1765 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1768 mutex_unlock(&rdev->mutex);
1771 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1774 * regulator_set_voltage_time - get raise/fall time
1775 * @regulator: regulator source
1776 * @old_uV: starting voltage in microvolts
1777 * @new_uV: target voltage in microvolts
1779 * Provided with the starting and ending voltage, this function attempts to
1780 * calculate the time in microseconds required to rise or fall to this new
1783 int regulator_set_voltage_time(struct regulator *regulator,
1784 int old_uV, int new_uV)
1786 struct regulator_dev *rdev = regulator->rdev;
1787 struct regulator_ops *ops = rdev->desc->ops;
1793 /* Currently requires operations to do this */
1794 if (!ops->list_voltage || !ops->set_voltage_time_sel
1795 || !rdev->desc->n_voltages)
1798 for (i = 0; i < rdev->desc->n_voltages; i++) {
1799 /* We only look for exact voltage matches here */
1800 voltage = regulator_list_voltage(regulator, i);
1805 if (voltage == old_uV)
1807 if (voltage == new_uV)
1811 if (old_sel < 0 || new_sel < 0)
1814 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1816 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1819 * regulator_sync_voltage - re-apply last regulator output voltage
1820 * @regulator: regulator source
1822 * Re-apply the last configured voltage. This is intended to be used
1823 * where some external control source the consumer is cooperating with
1824 * has caused the configured voltage to change.
1826 int regulator_sync_voltage(struct regulator *regulator)
1828 struct regulator_dev *rdev = regulator->rdev;
1829 int ret, min_uV, max_uV;
1831 mutex_lock(&rdev->mutex);
1833 if (!rdev->desc->ops->set_voltage &&
1834 !rdev->desc->ops->set_voltage_sel) {
1839 /* This is only going to work if we've had a voltage configured. */
1840 if (!regulator->min_uV && !regulator->max_uV) {
1845 min_uV = regulator->min_uV;
1846 max_uV = regulator->max_uV;
1848 /* This should be a paranoia check... */
1849 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1853 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1857 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1860 mutex_unlock(&rdev->mutex);
1863 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1865 static int _regulator_get_voltage(struct regulator_dev *rdev)
1869 if (rdev->desc->ops->get_voltage_sel) {
1870 sel = rdev->desc->ops->get_voltage_sel(rdev);
1873 return rdev->desc->ops->list_voltage(rdev, sel);
1875 if (rdev->desc->ops->get_voltage)
1876 return rdev->desc->ops->get_voltage(rdev);
1882 * regulator_get_voltage - get regulator output voltage
1883 * @regulator: regulator source
1885 * This returns the current regulator voltage in uV.
1887 * NOTE: If the regulator is disabled it will return the voltage value. This
1888 * function should not be used to determine regulator state.
1890 int regulator_get_voltage(struct regulator *regulator)
1894 mutex_lock(®ulator->rdev->mutex);
1896 ret = _regulator_get_voltage(regulator->rdev);
1898 mutex_unlock(®ulator->rdev->mutex);
1902 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1905 * regulator_set_current_limit - set regulator output current limit
1906 * @regulator: regulator source
1907 * @min_uA: Minimuum supported current in uA
1908 * @max_uA: Maximum supported current in uA
1910 * Sets current sink to the desired output current. This can be set during
1911 * any regulator state. IOW, regulator can be disabled or enabled.
1913 * If the regulator is enabled then the current will change to the new value
1914 * immediately otherwise if the regulator is disabled the regulator will
1915 * output at the new current when enabled.
1917 * NOTE: Regulator system constraints must be set for this regulator before
1918 * calling this function otherwise this call will fail.
1920 int regulator_set_current_limit(struct regulator *regulator,
1921 int min_uA, int max_uA)
1923 struct regulator_dev *rdev = regulator->rdev;
1926 mutex_lock(&rdev->mutex);
1929 if (!rdev->desc->ops->set_current_limit) {
1934 /* constraints check */
1935 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1939 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1941 mutex_unlock(&rdev->mutex);
1944 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1946 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1950 mutex_lock(&rdev->mutex);
1953 if (!rdev->desc->ops->get_current_limit) {
1958 ret = rdev->desc->ops->get_current_limit(rdev);
1960 mutex_unlock(&rdev->mutex);
1965 * regulator_get_current_limit - get regulator output current
1966 * @regulator: regulator source
1968 * This returns the current supplied by the specified current sink in uA.
1970 * NOTE: If the regulator is disabled it will return the current value. This
1971 * function should not be used to determine regulator state.
1973 int regulator_get_current_limit(struct regulator *regulator)
1975 return _regulator_get_current_limit(regulator->rdev);
1977 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1980 * regulator_set_mode - set regulator operating mode
1981 * @regulator: regulator source
1982 * @mode: operating mode - one of the REGULATOR_MODE constants
1984 * Set regulator operating mode to increase regulator efficiency or improve
1985 * regulation performance.
1987 * NOTE: Regulator system constraints must be set for this regulator before
1988 * calling this function otherwise this call will fail.
1990 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1992 struct regulator_dev *rdev = regulator->rdev;
1994 int regulator_curr_mode;
1996 mutex_lock(&rdev->mutex);
1999 if (!rdev->desc->ops->set_mode) {
2004 /* return if the same mode is requested */
2005 if (rdev->desc->ops->get_mode) {
2006 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2007 if (regulator_curr_mode == mode) {
2013 /* constraints check */
2014 ret = regulator_mode_constrain(rdev, mode);
2018 ret = rdev->desc->ops->set_mode(rdev, mode);
2020 mutex_unlock(&rdev->mutex);
2023 EXPORT_SYMBOL_GPL(regulator_set_mode);
2025 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2029 mutex_lock(&rdev->mutex);
2032 if (!rdev->desc->ops->get_mode) {
2037 ret = rdev->desc->ops->get_mode(rdev);
2039 mutex_unlock(&rdev->mutex);
2044 * regulator_get_mode - get regulator operating mode
2045 * @regulator: regulator source
2047 * Get the current regulator operating mode.
2049 unsigned int regulator_get_mode(struct regulator *regulator)
2051 return _regulator_get_mode(regulator->rdev);
2053 EXPORT_SYMBOL_GPL(regulator_get_mode);
2056 * regulator_set_optimum_mode - set regulator optimum operating mode
2057 * @regulator: regulator source
2058 * @uA_load: load current
2060 * Notifies the regulator core of a new device load. This is then used by
2061 * DRMS (if enabled by constraints) to set the most efficient regulator
2062 * operating mode for the new regulator loading.
2064 * Consumer devices notify their supply regulator of the maximum power
2065 * they will require (can be taken from device datasheet in the power
2066 * consumption tables) when they change operational status and hence power
2067 * state. Examples of operational state changes that can affect power
2068 * consumption are :-
2070 * o Device is opened / closed.
2071 * o Device I/O is about to begin or has just finished.
2072 * o Device is idling in between work.
2074 * This information is also exported via sysfs to userspace.
2076 * DRMS will sum the total requested load on the regulator and change
2077 * to the most efficient operating mode if platform constraints allow.
2079 * Returns the new regulator mode or error.
2081 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2083 struct regulator_dev *rdev = regulator->rdev;
2084 struct regulator *consumer;
2085 int ret, output_uV, input_uV, total_uA_load = 0;
2088 mutex_lock(&rdev->mutex);
2090 regulator->uA_load = uA_load;
2091 ret = regulator_check_drms(rdev);
2097 if (!rdev->desc->ops->get_optimum_mode)
2100 /* get output voltage */
2101 output_uV = _regulator_get_voltage(rdev);
2102 if (output_uV <= 0) {
2103 rdev_err(rdev, "invalid output voltage found\n");
2107 /* get input voltage */
2110 input_uV = _regulator_get_voltage(rdev->supply);
2112 input_uV = rdev->constraints->input_uV;
2113 if (input_uV <= 0) {
2114 rdev_err(rdev, "invalid input voltage found\n");
2118 /* calc total requested load for this regulator */
2119 list_for_each_entry(consumer, &rdev->consumer_list, list)
2120 total_uA_load += consumer->uA_load;
2122 mode = rdev->desc->ops->get_optimum_mode(rdev,
2123 input_uV, output_uV,
2125 ret = regulator_mode_constrain(rdev, &mode);
2127 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2128 total_uA_load, input_uV, output_uV);
2132 ret = rdev->desc->ops->set_mode(rdev, mode);
2134 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2139 mutex_unlock(&rdev->mutex);
2142 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2145 * regulator_register_notifier - register regulator event notifier
2146 * @regulator: regulator source
2147 * @nb: notifier block
2149 * Register notifier block to receive regulator events.
2151 int regulator_register_notifier(struct regulator *regulator,
2152 struct notifier_block *nb)
2154 return blocking_notifier_chain_register(®ulator->rdev->notifier,
2157 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2160 * regulator_unregister_notifier - unregister regulator event notifier
2161 * @regulator: regulator source
2162 * @nb: notifier block
2164 * Unregister regulator event notifier block.
2166 int regulator_unregister_notifier(struct regulator *regulator,
2167 struct notifier_block *nb)
2169 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
2172 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2174 /* notify regulator consumers and downstream regulator consumers.
2175 * Note mutex must be held by caller.
2177 static void _notifier_call_chain(struct regulator_dev *rdev,
2178 unsigned long event, void *data)
2180 struct regulator_dev *_rdev;
2182 /* call rdev chain first */
2183 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
2185 /* now notify regulator we supply */
2186 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
2187 mutex_lock(&_rdev->mutex);
2188 _notifier_call_chain(_rdev, event, data);
2189 mutex_unlock(&_rdev->mutex);
2194 * regulator_bulk_get - get multiple regulator consumers
2196 * @dev: Device to supply
2197 * @num_consumers: Number of consumers to register
2198 * @consumers: Configuration of consumers; clients are stored here.
2200 * @return 0 on success, an errno on failure.
2202 * This helper function allows drivers to get several regulator
2203 * consumers in one operation. If any of the regulators cannot be
2204 * acquired then any regulators that were allocated will be freed
2205 * before returning to the caller.
2207 int regulator_bulk_get(struct device *dev, int num_consumers,
2208 struct regulator_bulk_data *consumers)
2213 for (i = 0; i < num_consumers; i++)
2214 consumers[i].consumer = NULL;
2216 for (i = 0; i < num_consumers; i++) {
2217 consumers[i].consumer = regulator_get(dev,
2218 consumers[i].supply);
2219 if (IS_ERR(consumers[i].consumer)) {
2220 ret = PTR_ERR(consumers[i].consumer);
2221 dev_err(dev, "Failed to get supply '%s': %d\n",
2222 consumers[i].supply, ret);
2223 consumers[i].consumer = NULL;
2231 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2232 regulator_put(consumers[i].consumer);
2236 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2239 * regulator_bulk_enable - enable multiple regulator consumers
2241 * @num_consumers: Number of consumers
2242 * @consumers: Consumer data; clients are stored here.
2243 * @return 0 on success, an errno on failure
2245 * This convenience API allows consumers to enable multiple regulator
2246 * clients in a single API call. If any consumers cannot be enabled
2247 * then any others that were enabled will be disabled again prior to
2250 int regulator_bulk_enable(int num_consumers,
2251 struct regulator_bulk_data *consumers)
2256 for (i = 0; i < num_consumers; i++) {
2257 ret = regulator_enable(consumers[i].consumer);
2265 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2266 for (--i; i >= 0; --i)
2267 regulator_disable(consumers[i].consumer);
2271 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2274 * regulator_bulk_disable - disable multiple regulator consumers
2276 * @num_consumers: Number of consumers
2277 * @consumers: Consumer data; clients are stored here.
2278 * @return 0 on success, an errno on failure
2280 * This convenience API allows consumers to disable multiple regulator
2281 * clients in a single API call. If any consumers cannot be enabled
2282 * then any others that were disabled will be disabled again prior to
2285 int regulator_bulk_disable(int num_consumers,
2286 struct regulator_bulk_data *consumers)
2291 for (i = 0; i < num_consumers; i++) {
2292 ret = regulator_disable(consumers[i].consumer);
2300 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2301 for (--i; i >= 0; --i)
2302 regulator_enable(consumers[i].consumer);
2306 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2309 * regulator_bulk_free - free multiple regulator consumers
2311 * @num_consumers: Number of consumers
2312 * @consumers: Consumer data; clients are stored here.
2314 * This convenience API allows consumers to free multiple regulator
2315 * clients in a single API call.
2317 void regulator_bulk_free(int num_consumers,
2318 struct regulator_bulk_data *consumers)
2322 for (i = 0; i < num_consumers; i++) {
2323 regulator_put(consumers[i].consumer);
2324 consumers[i].consumer = NULL;
2327 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2330 * regulator_notifier_call_chain - call regulator event notifier
2331 * @rdev: regulator source
2332 * @event: notifier block
2333 * @data: callback-specific data.
2335 * Called by regulator drivers to notify clients a regulator event has
2336 * occurred. We also notify regulator clients downstream.
2337 * Note lock must be held by caller.
2339 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2340 unsigned long event, void *data)
2342 _notifier_call_chain(rdev, event, data);
2346 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2349 * regulator_mode_to_status - convert a regulator mode into a status
2351 * @mode: Mode to convert
2353 * Convert a regulator mode into a status.
2355 int regulator_mode_to_status(unsigned int mode)
2358 case REGULATOR_MODE_FAST:
2359 return REGULATOR_STATUS_FAST;
2360 case REGULATOR_MODE_NORMAL:
2361 return REGULATOR_STATUS_NORMAL;
2362 case REGULATOR_MODE_IDLE:
2363 return REGULATOR_STATUS_IDLE;
2364 case REGULATOR_STATUS_STANDBY:
2365 return REGULATOR_STATUS_STANDBY;
2370 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2373 * To avoid cluttering sysfs (and memory) with useless state, only
2374 * create attributes that can be meaningfully displayed.
2376 static int add_regulator_attributes(struct regulator_dev *rdev)
2378 struct device *dev = &rdev->dev;
2379 struct regulator_ops *ops = rdev->desc->ops;
2382 /* some attributes need specific methods to be displayed */
2383 if (ops->get_voltage || ops->get_voltage_sel) {
2384 status = device_create_file(dev, &dev_attr_microvolts);
2388 if (ops->get_current_limit) {
2389 status = device_create_file(dev, &dev_attr_microamps);
2393 if (ops->get_mode) {
2394 status = device_create_file(dev, &dev_attr_opmode);
2398 if (ops->is_enabled) {
2399 status = device_create_file(dev, &dev_attr_state);
2403 if (ops->get_status) {
2404 status = device_create_file(dev, &dev_attr_status);
2409 /* some attributes are type-specific */
2410 if (rdev->desc->type == REGULATOR_CURRENT) {
2411 status = device_create_file(dev, &dev_attr_requested_microamps);
2416 /* all the other attributes exist to support constraints;
2417 * don't show them if there are no constraints, or if the
2418 * relevant supporting methods are missing.
2420 if (!rdev->constraints)
2423 /* constraints need specific supporting methods */
2424 if (ops->set_voltage || ops->set_voltage_sel) {
2425 status = device_create_file(dev, &dev_attr_min_microvolts);
2428 status = device_create_file(dev, &dev_attr_max_microvolts);
2432 if (ops->set_current_limit) {
2433 status = device_create_file(dev, &dev_attr_min_microamps);
2436 status = device_create_file(dev, &dev_attr_max_microamps);
2441 /* suspend mode constraints need multiple supporting methods */
2442 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2445 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2448 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2451 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2455 if (ops->set_suspend_voltage) {
2456 status = device_create_file(dev,
2457 &dev_attr_suspend_standby_microvolts);
2460 status = device_create_file(dev,
2461 &dev_attr_suspend_mem_microvolts);
2464 status = device_create_file(dev,
2465 &dev_attr_suspend_disk_microvolts);
2470 if (ops->set_suspend_mode) {
2471 status = device_create_file(dev,
2472 &dev_attr_suspend_standby_mode);
2475 status = device_create_file(dev,
2476 &dev_attr_suspend_mem_mode);
2479 status = device_create_file(dev,
2480 &dev_attr_suspend_disk_mode);
2488 static void rdev_init_debugfs(struct regulator_dev *rdev)
2490 #ifdef CONFIG_DEBUG_FS
2491 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2492 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2493 rdev_warn(rdev, "Failed to create debugfs directory\n");
2494 rdev->debugfs = NULL;
2498 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2500 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2506 * regulator_register - register regulator
2507 * @regulator_desc: regulator to register
2508 * @dev: struct device for the regulator
2509 * @init_data: platform provided init data, passed through by driver
2510 * @driver_data: private regulator data
2512 * Called by regulator drivers to register a regulator.
2513 * Returns 0 on success.
2515 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2516 struct device *dev, const struct regulator_init_data *init_data,
2519 static atomic_t regulator_no = ATOMIC_INIT(0);
2520 struct regulator_dev *rdev;
2523 if (regulator_desc == NULL)
2524 return ERR_PTR(-EINVAL);
2526 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2527 return ERR_PTR(-EINVAL);
2529 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2530 regulator_desc->type != REGULATOR_CURRENT)
2531 return ERR_PTR(-EINVAL);
2534 return ERR_PTR(-EINVAL);
2536 /* Only one of each should be implemented */
2537 WARN_ON(regulator_desc->ops->get_voltage &&
2538 regulator_desc->ops->get_voltage_sel);
2539 WARN_ON(regulator_desc->ops->set_voltage &&
2540 regulator_desc->ops->set_voltage_sel);
2542 /* If we're using selectors we must implement list_voltage. */
2543 if (regulator_desc->ops->get_voltage_sel &&
2544 !regulator_desc->ops->list_voltage) {
2545 return ERR_PTR(-EINVAL);
2547 if (regulator_desc->ops->set_voltage_sel &&
2548 !regulator_desc->ops->list_voltage) {
2549 return ERR_PTR(-EINVAL);
2552 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2554 return ERR_PTR(-ENOMEM);
2556 mutex_lock(®ulator_list_mutex);
2558 mutex_init(&rdev->mutex);
2559 rdev->reg_data = driver_data;
2560 rdev->owner = regulator_desc->owner;
2561 rdev->desc = regulator_desc;
2562 INIT_LIST_HEAD(&rdev->consumer_list);
2563 INIT_LIST_HEAD(&rdev->supply_list);
2564 INIT_LIST_HEAD(&rdev->list);
2565 INIT_LIST_HEAD(&rdev->slist);
2566 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2568 /* preform any regulator specific init */
2569 if (init_data->regulator_init) {
2570 ret = init_data->regulator_init(rdev->reg_data);
2575 /* register with sysfs */
2576 rdev->dev.class = ®ulator_class;
2577 rdev->dev.parent = dev;
2578 dev_set_name(&rdev->dev, "regulator.%d",
2579 atomic_inc_return(®ulator_no) - 1);
2580 ret = device_register(&rdev->dev);
2582 put_device(&rdev->dev);
2586 dev_set_drvdata(&rdev->dev, rdev);
2588 /* set regulator constraints */
2589 ret = set_machine_constraints(rdev, &init_data->constraints);
2593 /* add attributes supported by this regulator */
2594 ret = add_regulator_attributes(rdev);
2598 /* set supply regulator if it exists */
2599 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2601 "Supply regulator specified by both name and dev\n");
2606 if (init_data->supply_regulator) {
2607 struct regulator_dev *r;
2610 list_for_each_entry(r, ®ulator_list, list) {
2611 if (strcmp(rdev_get_name(r),
2612 init_data->supply_regulator) == 0) {
2619 dev_err(dev, "Failed to find supply %s\n",
2620 init_data->supply_regulator);
2625 ret = set_supply(rdev, r);
2630 if (init_data->supply_regulator_dev) {
2631 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
2632 ret = set_supply(rdev,
2633 dev_get_drvdata(init_data->supply_regulator_dev));
2638 /* add consumers devices */
2639 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2640 ret = set_consumer_device_supply(rdev,
2641 init_data->consumer_supplies[i].dev,
2642 init_data->consumer_supplies[i].dev_name,
2643 init_data->consumer_supplies[i].supply);
2645 dev_err(dev, "Failed to set supply %s\n",
2646 init_data->consumer_supplies[i].supply);
2647 goto unset_supplies;
2651 list_add(&rdev->list, ®ulator_list);
2653 rdev_init_debugfs(rdev);
2655 mutex_unlock(®ulator_list_mutex);
2659 unset_regulator_supplies(rdev);
2662 device_unregister(&rdev->dev);
2663 /* device core frees rdev */
2664 rdev = ERR_PTR(ret);
2669 rdev = ERR_PTR(ret);
2672 EXPORT_SYMBOL_GPL(regulator_register);
2675 * regulator_unregister - unregister regulator
2676 * @rdev: regulator to unregister
2678 * Called by regulator drivers to unregister a regulator.
2680 void regulator_unregister(struct regulator_dev *rdev)
2685 mutex_lock(®ulator_list_mutex);
2686 #ifdef CONFIG_DEBUG_FS
2687 debugfs_remove_recursive(rdev->debugfs);
2689 WARN_ON(rdev->open_count);
2690 unset_regulator_supplies(rdev);
2691 list_del(&rdev->list);
2693 sysfs_remove_link(&rdev->dev.kobj, "supply");
2694 device_unregister(&rdev->dev);
2695 kfree(rdev->constraints);
2696 mutex_unlock(®ulator_list_mutex);
2698 EXPORT_SYMBOL_GPL(regulator_unregister);
2701 * regulator_suspend_prepare - prepare regulators for system wide suspend
2702 * @state: system suspend state
2704 * Configure each regulator with it's suspend operating parameters for state.
2705 * This will usually be called by machine suspend code prior to supending.
2707 int regulator_suspend_prepare(suspend_state_t state)
2709 struct regulator_dev *rdev;
2712 /* ON is handled by regulator active state */
2713 if (state == PM_SUSPEND_ON)
2716 mutex_lock(®ulator_list_mutex);
2717 list_for_each_entry(rdev, ®ulator_list, list) {
2719 mutex_lock(&rdev->mutex);
2720 ret = suspend_prepare(rdev, state);
2721 mutex_unlock(&rdev->mutex);
2724 rdev_err(rdev, "failed to prepare\n");
2729 mutex_unlock(®ulator_list_mutex);
2732 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2735 * regulator_suspend_finish - resume regulators from system wide suspend
2737 * Turn on regulators that might be turned off by regulator_suspend_prepare
2738 * and that should be turned on according to the regulators properties.
2740 int regulator_suspend_finish(void)
2742 struct regulator_dev *rdev;
2745 mutex_lock(®ulator_list_mutex);
2746 list_for_each_entry(rdev, ®ulator_list, list) {
2747 struct regulator_ops *ops = rdev->desc->ops;
2749 mutex_lock(&rdev->mutex);
2750 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2752 error = ops->enable(rdev);
2756 if (!has_full_constraints)
2760 if (ops->is_enabled && !ops->is_enabled(rdev))
2763 error = ops->disable(rdev);
2768 mutex_unlock(&rdev->mutex);
2770 mutex_unlock(®ulator_list_mutex);
2773 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2776 * regulator_has_full_constraints - the system has fully specified constraints
2778 * Calling this function will cause the regulator API to disable all
2779 * regulators which have a zero use count and don't have an always_on
2780 * constraint in a late_initcall.
2782 * The intention is that this will become the default behaviour in a
2783 * future kernel release so users are encouraged to use this facility
2786 void regulator_has_full_constraints(void)
2788 has_full_constraints = 1;
2790 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2793 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2795 * Calling this function will cause the regulator API to provide a
2796 * dummy regulator to consumers if no physical regulator is found,
2797 * allowing most consumers to proceed as though a regulator were
2798 * configured. This allows systems such as those with software
2799 * controllable regulators for the CPU core only to be brought up more
2802 void regulator_use_dummy_regulator(void)
2804 board_wants_dummy_regulator = true;
2806 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2809 * rdev_get_drvdata - get rdev regulator driver data
2812 * Get rdev regulator driver private data. This call can be used in the
2813 * regulator driver context.
2815 void *rdev_get_drvdata(struct regulator_dev *rdev)
2817 return rdev->reg_data;
2819 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2822 * regulator_get_drvdata - get regulator driver data
2823 * @regulator: regulator
2825 * Get regulator driver private data. This call can be used in the consumer
2826 * driver context when non API regulator specific functions need to be called.
2828 void *regulator_get_drvdata(struct regulator *regulator)
2830 return regulator->rdev->reg_data;
2832 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2835 * regulator_set_drvdata - set regulator driver data
2836 * @regulator: regulator
2839 void regulator_set_drvdata(struct regulator *regulator, void *data)
2841 regulator->rdev->reg_data = data;
2843 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2846 * regulator_get_id - get regulator ID
2849 int rdev_get_id(struct regulator_dev *rdev)
2851 return rdev->desc->id;
2853 EXPORT_SYMBOL_GPL(rdev_get_id);
2855 struct device *rdev_get_dev(struct regulator_dev *rdev)
2859 EXPORT_SYMBOL_GPL(rdev_get_dev);
2861 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2863 return reg_init_data->driver_data;
2865 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2867 static int __init regulator_init(void)
2871 ret = class_register(®ulator_class);
2873 #ifdef CONFIG_DEBUG_FS
2874 debugfs_root = debugfs_create_dir("regulator", NULL);
2875 if (IS_ERR(debugfs_root) || !debugfs_root) {
2876 pr_warn("regulator: Failed to create debugfs directory\n");
2877 debugfs_root = NULL;
2881 regulator_dummy_init();
2886 /* init early to allow our consumers to complete system booting */
2887 core_initcall(regulator_init);
2889 static int __init regulator_init_complete(void)
2891 struct regulator_dev *rdev;
2892 struct regulator_ops *ops;
2893 struct regulation_constraints *c;
2896 mutex_lock(®ulator_list_mutex);
2898 /* If we have a full configuration then disable any regulators
2899 * which are not in use or always_on. This will become the
2900 * default behaviour in the future.
2902 list_for_each_entry(rdev, ®ulator_list, list) {
2903 ops = rdev->desc->ops;
2904 c = rdev->constraints;
2906 if (!ops->disable || (c && c->always_on))
2909 mutex_lock(&rdev->mutex);
2911 if (rdev->use_count)
2914 /* If we can't read the status assume it's on. */
2915 if (ops->is_enabled)
2916 enabled = ops->is_enabled(rdev);
2923 if (has_full_constraints) {
2924 /* We log since this may kill the system if it
2926 rdev_info(rdev, "disabling\n");
2927 ret = ops->disable(rdev);
2929 rdev_err(rdev, "couldn't disable: %d\n", ret);
2932 /* The intention is that in future we will
2933 * assume that full constraints are provided
2934 * so warn even if we aren't going to do
2937 rdev_warn(rdev, "incomplete constraints, leaving on\n");
2941 mutex_unlock(&rdev->mutex);
2944 mutex_unlock(®ulator_list_mutex);
2948 late_initcall(regulator_init_complete);