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/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
28 #include <linux/regmap.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/module.h>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/regulator.h>
40 #define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51 static DEFINE_MUTEX(regulator_list_mutex);
52 static LIST_HEAD(regulator_list);
53 static LIST_HEAD(regulator_map_list);
54 static bool has_full_constraints;
55 static bool board_wants_dummy_regulator;
57 static struct dentry *debugfs_root;
60 * struct regulator_map
62 * Used to provide symbolic supply names to devices.
64 struct regulator_map {
65 struct list_head list;
66 const char *dev_name; /* The dev_name() for the consumer */
68 struct regulator_dev *regulator;
74 * One for each consumer device.
78 struct list_head list;
79 unsigned int always_on:1;
80 unsigned int bypass:1;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
87 struct dentry *debugfs;
90 static int _regulator_is_enabled(struct regulator_dev *rdev);
91 static int _regulator_disable(struct regulator_dev *rdev);
92 static int _regulator_get_voltage(struct regulator_dev *rdev);
93 static int _regulator_get_current_limit(struct regulator_dev *rdev);
94 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
95 static void _notifier_call_chain(struct regulator_dev *rdev,
96 unsigned long event, void *data);
97 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
98 int min_uV, int max_uV);
99 static struct regulator *create_regulator(struct regulator_dev *rdev,
101 const char *supply_name);
103 static const char *rdev_get_name(struct regulator_dev *rdev)
105 if (rdev->constraints && rdev->constraints->name)
106 return rdev->constraints->name;
107 else if (rdev->desc->name)
108 return rdev->desc->name;
114 * of_get_regulator - get a regulator device node based on supply name
115 * @dev: Device pointer for the consumer (of regulator) device
116 * @supply: regulator supply name
118 * Extract the regulator device node corresponding to the supply name.
119 * retruns the device node corresponding to the regulator if found, else
122 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
124 struct device_node *regnode = NULL;
125 char prop_name[32]; /* 32 is max size of property name */
127 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
129 snprintf(prop_name, 32, "%s-supply", supply);
130 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
133 dev_dbg(dev, "Looking up %s property in node %s failed",
134 prop_name, dev->of_node->full_name);
140 static int _regulator_can_change_status(struct regulator_dev *rdev)
142 if (!rdev->constraints)
145 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
151 /* Platform voltage constraint check */
152 static int regulator_check_voltage(struct regulator_dev *rdev,
153 int *min_uV, int *max_uV)
155 BUG_ON(*min_uV > *max_uV);
157 if (!rdev->constraints) {
158 rdev_err(rdev, "no constraints\n");
161 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
162 rdev_err(rdev, "operation not allowed\n");
166 if (*max_uV > rdev->constraints->max_uV)
167 *max_uV = rdev->constraints->max_uV;
168 if (*min_uV < rdev->constraints->min_uV)
169 *min_uV = rdev->constraints->min_uV;
171 if (*min_uV > *max_uV) {
172 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
180 /* Make sure we select a voltage that suits the needs of all
181 * regulator consumers
183 static int regulator_check_consumers(struct regulator_dev *rdev,
184 int *min_uV, int *max_uV)
186 struct regulator *regulator;
188 list_for_each_entry(regulator, &rdev->consumer_list, list) {
190 * Assume consumers that didn't say anything are OK
191 * with anything in the constraint range.
193 if (!regulator->min_uV && !regulator->max_uV)
196 if (*max_uV > regulator->max_uV)
197 *max_uV = regulator->max_uV;
198 if (*min_uV < regulator->min_uV)
199 *min_uV = regulator->min_uV;
202 if (*min_uV > *max_uV)
208 /* current constraint check */
209 static int regulator_check_current_limit(struct regulator_dev *rdev,
210 int *min_uA, int *max_uA)
212 BUG_ON(*min_uA > *max_uA);
214 if (!rdev->constraints) {
215 rdev_err(rdev, "no constraints\n");
218 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
219 rdev_err(rdev, "operation not allowed\n");
223 if (*max_uA > rdev->constraints->max_uA)
224 *max_uA = rdev->constraints->max_uA;
225 if (*min_uA < rdev->constraints->min_uA)
226 *min_uA = rdev->constraints->min_uA;
228 if (*min_uA > *max_uA) {
229 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
237 /* operating mode constraint check */
238 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
241 case REGULATOR_MODE_FAST:
242 case REGULATOR_MODE_NORMAL:
243 case REGULATOR_MODE_IDLE:
244 case REGULATOR_MODE_STANDBY:
247 rdev_err(rdev, "invalid mode %x specified\n", *mode);
251 if (!rdev->constraints) {
252 rdev_err(rdev, "no constraints\n");
255 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
256 rdev_err(rdev, "operation not allowed\n");
260 /* The modes are bitmasks, the most power hungry modes having
261 * the lowest values. If the requested mode isn't supported
262 * try higher modes. */
264 if (rdev->constraints->valid_modes_mask & *mode)
272 /* dynamic regulator mode switching constraint check */
273 static int regulator_check_drms(struct regulator_dev *rdev)
275 if (!rdev->constraints) {
276 rdev_err(rdev, "no constraints\n");
279 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
280 rdev_err(rdev, "operation not allowed\n");
286 static ssize_t regulator_uV_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
289 struct regulator_dev *rdev = dev_get_drvdata(dev);
292 mutex_lock(&rdev->mutex);
293 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
294 mutex_unlock(&rdev->mutex);
298 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
300 static ssize_t regulator_uA_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
303 struct regulator_dev *rdev = dev_get_drvdata(dev);
305 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
307 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
309 static ssize_t regulator_name_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 struct regulator_dev *rdev = dev_get_drvdata(dev);
314 return sprintf(buf, "%s\n", rdev_get_name(rdev));
317 static ssize_t regulator_print_opmode(char *buf, int mode)
320 case REGULATOR_MODE_FAST:
321 return sprintf(buf, "fast\n");
322 case REGULATOR_MODE_NORMAL:
323 return sprintf(buf, "normal\n");
324 case REGULATOR_MODE_IDLE:
325 return sprintf(buf, "idle\n");
326 case REGULATOR_MODE_STANDBY:
327 return sprintf(buf, "standby\n");
329 return sprintf(buf, "unknown\n");
332 static ssize_t regulator_opmode_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
335 struct regulator_dev *rdev = dev_get_drvdata(dev);
337 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
339 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
341 static ssize_t regulator_print_state(char *buf, int state)
344 return sprintf(buf, "enabled\n");
346 return sprintf(buf, "disabled\n");
348 return sprintf(buf, "unknown\n");
351 static ssize_t regulator_state_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
354 struct regulator_dev *rdev = dev_get_drvdata(dev);
357 mutex_lock(&rdev->mutex);
358 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
359 mutex_unlock(&rdev->mutex);
363 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
365 static ssize_t regulator_status_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
368 struct regulator_dev *rdev = dev_get_drvdata(dev);
372 status = rdev->desc->ops->get_status(rdev);
377 case REGULATOR_STATUS_OFF:
380 case REGULATOR_STATUS_ON:
383 case REGULATOR_STATUS_ERROR:
386 case REGULATOR_STATUS_FAST:
389 case REGULATOR_STATUS_NORMAL:
392 case REGULATOR_STATUS_IDLE:
395 case REGULATOR_STATUS_STANDBY:
398 case REGULATOR_STATUS_BYPASS:
401 case REGULATOR_STATUS_UNDEFINED:
408 return sprintf(buf, "%s\n", label);
410 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
412 static ssize_t regulator_min_uA_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
415 struct regulator_dev *rdev = dev_get_drvdata(dev);
417 if (!rdev->constraints)
418 return sprintf(buf, "constraint not defined\n");
420 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
422 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
424 static ssize_t regulator_max_uA_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
427 struct regulator_dev *rdev = dev_get_drvdata(dev);
429 if (!rdev->constraints)
430 return sprintf(buf, "constraint not defined\n");
432 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
434 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
436 static ssize_t regulator_min_uV_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
439 struct regulator_dev *rdev = dev_get_drvdata(dev);
441 if (!rdev->constraints)
442 return sprintf(buf, "constraint not defined\n");
444 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
446 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
448 static ssize_t regulator_max_uV_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
451 struct regulator_dev *rdev = dev_get_drvdata(dev);
453 if (!rdev->constraints)
454 return sprintf(buf, "constraint not defined\n");
456 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
458 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
460 static ssize_t regulator_total_uA_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
463 struct regulator_dev *rdev = dev_get_drvdata(dev);
464 struct regulator *regulator;
467 mutex_lock(&rdev->mutex);
468 list_for_each_entry(regulator, &rdev->consumer_list, list)
469 uA += regulator->uA_load;
470 mutex_unlock(&rdev->mutex);
471 return sprintf(buf, "%d\n", uA);
473 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
475 static ssize_t regulator_num_users_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
478 struct regulator_dev *rdev = dev_get_drvdata(dev);
479 return sprintf(buf, "%d\n", rdev->use_count);
482 static ssize_t regulator_type_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
485 struct regulator_dev *rdev = dev_get_drvdata(dev);
487 switch (rdev->desc->type) {
488 case REGULATOR_VOLTAGE:
489 return sprintf(buf, "voltage\n");
490 case REGULATOR_CURRENT:
491 return sprintf(buf, "current\n");
493 return sprintf(buf, "unknown\n");
496 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
499 struct regulator_dev *rdev = dev_get_drvdata(dev);
501 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
503 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
504 regulator_suspend_mem_uV_show, NULL);
506 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
507 struct device_attribute *attr, char *buf)
509 struct regulator_dev *rdev = dev_get_drvdata(dev);
511 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
513 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
514 regulator_suspend_disk_uV_show, NULL);
516 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
519 struct regulator_dev *rdev = dev_get_drvdata(dev);
521 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
523 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
524 regulator_suspend_standby_uV_show, NULL);
526 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
529 struct regulator_dev *rdev = dev_get_drvdata(dev);
531 return regulator_print_opmode(buf,
532 rdev->constraints->state_mem.mode);
534 static DEVICE_ATTR(suspend_mem_mode, 0444,
535 regulator_suspend_mem_mode_show, NULL);
537 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
540 struct regulator_dev *rdev = dev_get_drvdata(dev);
542 return regulator_print_opmode(buf,
543 rdev->constraints->state_disk.mode);
545 static DEVICE_ATTR(suspend_disk_mode, 0444,
546 regulator_suspend_disk_mode_show, NULL);
548 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
549 struct device_attribute *attr, char *buf)
551 struct regulator_dev *rdev = dev_get_drvdata(dev);
553 return regulator_print_opmode(buf,
554 rdev->constraints->state_standby.mode);
556 static DEVICE_ATTR(suspend_standby_mode, 0444,
557 regulator_suspend_standby_mode_show, NULL);
559 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
562 struct regulator_dev *rdev = dev_get_drvdata(dev);
564 return regulator_print_state(buf,
565 rdev->constraints->state_mem.enabled);
567 static DEVICE_ATTR(suspend_mem_state, 0444,
568 regulator_suspend_mem_state_show, NULL);
570 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
573 struct regulator_dev *rdev = dev_get_drvdata(dev);
575 return regulator_print_state(buf,
576 rdev->constraints->state_disk.enabled);
578 static DEVICE_ATTR(suspend_disk_state, 0444,
579 regulator_suspend_disk_state_show, NULL);
581 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
582 struct device_attribute *attr, char *buf)
584 struct regulator_dev *rdev = dev_get_drvdata(dev);
586 return regulator_print_state(buf,
587 rdev->constraints->state_standby.enabled);
589 static DEVICE_ATTR(suspend_standby_state, 0444,
590 regulator_suspend_standby_state_show, NULL);
592 static ssize_t regulator_bypass_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 struct regulator_dev *rdev = dev_get_drvdata(dev);
600 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
609 return sprintf(buf, "%s\n", report);
611 static DEVICE_ATTR(bypass, 0444,
612 regulator_bypass_show, NULL);
615 * These are the only attributes are present for all regulators.
616 * Other attributes are a function of regulator functionality.
618 static struct device_attribute regulator_dev_attrs[] = {
619 __ATTR(name, 0444, regulator_name_show, NULL),
620 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
621 __ATTR(type, 0444, regulator_type_show, NULL),
625 static void regulator_dev_release(struct device *dev)
627 struct regulator_dev *rdev = dev_get_drvdata(dev);
631 static struct class regulator_class = {
633 .dev_release = regulator_dev_release,
634 .dev_attrs = regulator_dev_attrs,
637 /* Calculate the new optimum regulator operating mode based on the new total
638 * consumer load. All locks held by caller */
639 static void drms_uA_update(struct regulator_dev *rdev)
641 struct regulator *sibling;
642 int current_uA = 0, output_uV, input_uV, err;
645 err = regulator_check_drms(rdev);
646 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
647 (!rdev->desc->ops->get_voltage &&
648 !rdev->desc->ops->get_voltage_sel) ||
649 !rdev->desc->ops->set_mode)
652 /* get output voltage */
653 output_uV = _regulator_get_voltage(rdev);
657 /* get input voltage */
660 input_uV = regulator_get_voltage(rdev->supply);
662 input_uV = rdev->constraints->input_uV;
666 /* calc total requested load */
667 list_for_each_entry(sibling, &rdev->consumer_list, list)
668 current_uA += sibling->uA_load;
670 /* now get the optimum mode for our new total regulator load */
671 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
672 output_uV, current_uA);
674 /* check the new mode is allowed */
675 err = regulator_mode_constrain(rdev, &mode);
677 rdev->desc->ops->set_mode(rdev, mode);
680 static int suspend_set_state(struct regulator_dev *rdev,
681 struct regulator_state *rstate)
685 /* If we have no suspend mode configration don't set anything;
686 * only warn if the driver implements set_suspend_voltage or
687 * set_suspend_mode callback.
689 if (!rstate->enabled && !rstate->disabled) {
690 if (rdev->desc->ops->set_suspend_voltage ||
691 rdev->desc->ops->set_suspend_mode)
692 rdev_warn(rdev, "No configuration\n");
696 if (rstate->enabled && rstate->disabled) {
697 rdev_err(rdev, "invalid configuration\n");
701 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
702 ret = rdev->desc->ops->set_suspend_enable(rdev);
703 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
704 ret = rdev->desc->ops->set_suspend_disable(rdev);
705 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
709 rdev_err(rdev, "failed to enabled/disable\n");
713 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
714 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
716 rdev_err(rdev, "failed to set voltage\n");
721 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
722 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
724 rdev_err(rdev, "failed to set mode\n");
731 /* locks held by caller */
732 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
734 if (!rdev->constraints)
738 case PM_SUSPEND_STANDBY:
739 return suspend_set_state(rdev,
740 &rdev->constraints->state_standby);
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_mem);
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_disk);
752 static void print_constraints(struct regulator_dev *rdev)
754 struct regulation_constraints *constraints = rdev->constraints;
759 if (constraints->min_uV && constraints->max_uV) {
760 if (constraints->min_uV == constraints->max_uV)
761 count += sprintf(buf + count, "%d mV ",
762 constraints->min_uV / 1000);
764 count += sprintf(buf + count, "%d <--> %d mV ",
765 constraints->min_uV / 1000,
766 constraints->max_uV / 1000);
769 if (!constraints->min_uV ||
770 constraints->min_uV != constraints->max_uV) {
771 ret = _regulator_get_voltage(rdev);
773 count += sprintf(buf + count, "at %d mV ", ret / 1000);
776 if (constraints->uV_offset)
777 count += sprintf(buf, "%dmV offset ",
778 constraints->uV_offset / 1000);
780 if (constraints->min_uA && constraints->max_uA) {
781 if (constraints->min_uA == constraints->max_uA)
782 count += sprintf(buf + count, "%d mA ",
783 constraints->min_uA / 1000);
785 count += sprintf(buf + count, "%d <--> %d mA ",
786 constraints->min_uA / 1000,
787 constraints->max_uA / 1000);
790 if (!constraints->min_uA ||
791 constraints->min_uA != constraints->max_uA) {
792 ret = _regulator_get_current_limit(rdev);
794 count += sprintf(buf + count, "at %d mA ", ret / 1000);
797 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
798 count += sprintf(buf + count, "fast ");
799 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
800 count += sprintf(buf + count, "normal ");
801 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
802 count += sprintf(buf + count, "idle ");
803 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
804 count += sprintf(buf + count, "standby");
807 sprintf(buf, "no parameters");
809 rdev_info(rdev, "%s\n", buf);
811 if ((constraints->min_uV != constraints->max_uV) &&
812 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
814 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
817 static int machine_constraints_voltage(struct regulator_dev *rdev,
818 struct regulation_constraints *constraints)
820 struct regulator_ops *ops = rdev->desc->ops;
823 /* do we need to apply the constraint voltage */
824 if (rdev->constraints->apply_uV &&
825 rdev->constraints->min_uV == rdev->constraints->max_uV) {
826 ret = _regulator_do_set_voltage(rdev,
827 rdev->constraints->min_uV,
828 rdev->constraints->max_uV);
830 rdev_err(rdev, "failed to apply %duV constraint\n",
831 rdev->constraints->min_uV);
836 /* constrain machine-level voltage specs to fit
837 * the actual range supported by this regulator.
839 if (ops->list_voltage && rdev->desc->n_voltages) {
840 int count = rdev->desc->n_voltages;
842 int min_uV = INT_MAX;
843 int max_uV = INT_MIN;
844 int cmin = constraints->min_uV;
845 int cmax = constraints->max_uV;
847 /* it's safe to autoconfigure fixed-voltage supplies
848 and the constraints are used by list_voltage. */
849 if (count == 1 && !cmin) {
852 constraints->min_uV = cmin;
853 constraints->max_uV = cmax;
856 /* voltage constraints are optional */
857 if ((cmin == 0) && (cmax == 0))
860 /* else require explicit machine-level constraints */
861 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
862 rdev_err(rdev, "invalid voltage constraints\n");
866 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
867 for (i = 0; i < count; i++) {
870 value = ops->list_voltage(rdev, i);
874 /* maybe adjust [min_uV..max_uV] */
875 if (value >= cmin && value < min_uV)
877 if (value <= cmax && value > max_uV)
881 /* final: [min_uV..max_uV] valid iff constraints valid */
882 if (max_uV < min_uV) {
883 rdev_err(rdev, "unsupportable voltage constraints\n");
887 /* use regulator's subset of machine constraints */
888 if (constraints->min_uV < min_uV) {
889 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
890 constraints->min_uV, min_uV);
891 constraints->min_uV = min_uV;
893 if (constraints->max_uV > max_uV) {
894 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
895 constraints->max_uV, max_uV);
896 constraints->max_uV = max_uV;
904 * set_machine_constraints - sets regulator constraints
905 * @rdev: regulator source
906 * @constraints: constraints to apply
908 * Allows platform initialisation code to define and constrain
909 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
910 * Constraints *must* be set by platform code in order for some
911 * regulator operations to proceed i.e. set_voltage, set_current_limit,
914 static int set_machine_constraints(struct regulator_dev *rdev,
915 const struct regulation_constraints *constraints)
918 struct regulator_ops *ops = rdev->desc->ops;
921 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
924 rdev->constraints = kzalloc(sizeof(*constraints),
926 if (!rdev->constraints)
929 ret = machine_constraints_voltage(rdev, rdev->constraints);
933 /* do we need to setup our suspend state */
934 if (rdev->constraints->initial_state) {
935 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
937 rdev_err(rdev, "failed to set suspend state\n");
942 if (rdev->constraints->initial_mode) {
943 if (!ops->set_mode) {
944 rdev_err(rdev, "no set_mode operation\n");
949 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
951 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
956 /* If the constraints say the regulator should be on at this point
957 * and we have control then make sure it is enabled.
959 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
961 ret = ops->enable(rdev);
963 rdev_err(rdev, "failed to enable\n");
968 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
969 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
971 rdev_err(rdev, "failed to set ramp_delay\n");
976 print_constraints(rdev);
979 kfree(rdev->constraints);
980 rdev->constraints = NULL;
985 * set_supply - set regulator supply regulator
986 * @rdev: regulator name
987 * @supply_rdev: supply regulator name
989 * Called by platform initialisation code to set the supply regulator for this
990 * regulator. This ensures that a regulators supply will also be enabled by the
991 * core if it's child is enabled.
993 static int set_supply(struct regulator_dev *rdev,
994 struct regulator_dev *supply_rdev)
998 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1000 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1001 if (rdev->supply == NULL) {
1005 supply_rdev->open_count++;
1011 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1012 * @rdev: regulator source
1013 * @consumer_dev_name: dev_name() string for device supply applies to
1014 * @supply: symbolic name for supply
1016 * Allows platform initialisation code to map physical regulator
1017 * sources to symbolic names for supplies for use by devices. Devices
1018 * should use these symbolic names to request regulators, avoiding the
1019 * need to provide board-specific regulator names as platform data.
1021 static int set_consumer_device_supply(struct regulator_dev *rdev,
1022 const char *consumer_dev_name,
1025 struct regulator_map *node;
1031 if (consumer_dev_name != NULL)
1036 list_for_each_entry(node, ®ulator_map_list, list) {
1037 if (node->dev_name && consumer_dev_name) {
1038 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1040 } else if (node->dev_name || consumer_dev_name) {
1044 if (strcmp(node->supply, supply) != 0)
1047 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1049 dev_name(&node->regulator->dev),
1050 node->regulator->desc->name,
1052 dev_name(&rdev->dev), rdev_get_name(rdev));
1056 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1060 node->regulator = rdev;
1061 node->supply = supply;
1064 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1065 if (node->dev_name == NULL) {
1071 list_add(&node->list, ®ulator_map_list);
1075 static void unset_regulator_supplies(struct regulator_dev *rdev)
1077 struct regulator_map *node, *n;
1079 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1080 if (rdev == node->regulator) {
1081 list_del(&node->list);
1082 kfree(node->dev_name);
1088 #define REG_STR_SIZE 64
1090 static struct regulator *create_regulator(struct regulator_dev *rdev,
1092 const char *supply_name)
1094 struct regulator *regulator;
1095 char buf[REG_STR_SIZE];
1098 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1099 if (regulator == NULL)
1102 mutex_lock(&rdev->mutex);
1103 regulator->rdev = rdev;
1104 list_add(®ulator->list, &rdev->consumer_list);
1107 regulator->dev = dev;
1109 /* Add a link to the device sysfs entry */
1110 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1111 dev->kobj.name, supply_name);
1112 if (size >= REG_STR_SIZE)
1115 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1116 if (regulator->supply_name == NULL)
1119 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1122 rdev_warn(rdev, "could not add device link %s err %d\n",
1123 dev->kobj.name, err);
1127 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1128 if (regulator->supply_name == NULL)
1132 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1134 if (!regulator->debugfs) {
1135 rdev_warn(rdev, "Failed to create debugfs directory\n");
1137 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1138 ®ulator->uA_load);
1139 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1140 ®ulator->min_uV);
1141 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1142 ®ulator->max_uV);
1146 * Check now if the regulator is an always on regulator - if
1147 * it is then we don't need to do nearly so much work for
1148 * enable/disable calls.
1150 if (!_regulator_can_change_status(rdev) &&
1151 _regulator_is_enabled(rdev))
1152 regulator->always_on = true;
1154 mutex_unlock(&rdev->mutex);
1157 list_del(®ulator->list);
1159 mutex_unlock(&rdev->mutex);
1163 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1165 if (!rdev->desc->ops->enable_time)
1166 return rdev->desc->enable_time;
1167 return rdev->desc->ops->enable_time(rdev);
1170 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1174 struct regulator_dev *r;
1175 struct device_node *node;
1176 struct regulator_map *map;
1177 const char *devname = NULL;
1179 /* first do a dt based lookup */
1180 if (dev && dev->of_node) {
1181 node = of_get_regulator(dev, supply);
1183 list_for_each_entry(r, ®ulator_list, list)
1184 if (r->dev.parent &&
1185 node == r->dev.of_node)
1189 * If we couldn't even get the node then it's
1190 * not just that the device didn't register
1191 * yet, there's no node and we'll never
1198 /* if not found, try doing it non-dt way */
1200 devname = dev_name(dev);
1202 list_for_each_entry(r, ®ulator_list, list)
1203 if (strcmp(rdev_get_name(r), supply) == 0)
1206 list_for_each_entry(map, ®ulator_map_list, list) {
1207 /* If the mapping has a device set up it must match */
1208 if (map->dev_name &&
1209 (!devname || strcmp(map->dev_name, devname)))
1212 if (strcmp(map->supply, supply) == 0)
1213 return map->regulator;
1220 /* Internal regulator request function */
1221 static struct regulator *_regulator_get(struct device *dev, const char *id,
1224 struct regulator_dev *rdev;
1225 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1226 const char *devname = NULL;
1230 pr_err("get() with no identifier\n");
1235 devname = dev_name(dev);
1237 mutex_lock(®ulator_list_mutex);
1239 rdev = regulator_dev_lookup(dev, id, &ret);
1243 if (board_wants_dummy_regulator) {
1244 rdev = dummy_regulator_rdev;
1248 #ifdef CONFIG_REGULATOR_DUMMY
1250 devname = "deviceless";
1252 /* If the board didn't flag that it was fully constrained then
1253 * substitute in a dummy regulator so consumers can continue.
1255 if (!has_full_constraints) {
1256 pr_warn("%s supply %s not found, using dummy regulator\n",
1258 rdev = dummy_regulator_rdev;
1263 mutex_unlock(®ulator_list_mutex);
1267 if (rdev->exclusive) {
1268 regulator = ERR_PTR(-EPERM);
1272 if (exclusive && rdev->open_count) {
1273 regulator = ERR_PTR(-EBUSY);
1277 if (!try_module_get(rdev->owner))
1280 regulator = create_regulator(rdev, dev, id);
1281 if (regulator == NULL) {
1282 regulator = ERR_PTR(-ENOMEM);
1283 module_put(rdev->owner);
1289 rdev->exclusive = 1;
1291 ret = _regulator_is_enabled(rdev);
1293 rdev->use_count = 1;
1295 rdev->use_count = 0;
1299 mutex_unlock(®ulator_list_mutex);
1305 * regulator_get - lookup and obtain a reference to a regulator.
1306 * @dev: device for regulator "consumer"
1307 * @id: Supply name or regulator ID.
1309 * Returns a struct regulator corresponding to the regulator producer,
1310 * or IS_ERR() condition containing errno.
1312 * Use of supply names configured via regulator_set_device_supply() is
1313 * strongly encouraged. It is recommended that the supply name used
1314 * should match the name used for the supply and/or the relevant
1315 * device pins in the datasheet.
1317 struct regulator *regulator_get(struct device *dev, const char *id)
1319 return _regulator_get(dev, id, 0);
1321 EXPORT_SYMBOL_GPL(regulator_get);
1323 static void devm_regulator_release(struct device *dev, void *res)
1325 regulator_put(*(struct regulator **)res);
1329 * devm_regulator_get - Resource managed regulator_get()
1330 * @dev: device for regulator "consumer"
1331 * @id: Supply name or regulator ID.
1333 * Managed regulator_get(). Regulators returned from this function are
1334 * automatically regulator_put() on driver detach. See regulator_get() for more
1337 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1339 struct regulator **ptr, *regulator;
1341 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1343 return ERR_PTR(-ENOMEM);
1345 regulator = regulator_get(dev, id);
1346 if (!IS_ERR(regulator)) {
1348 devres_add(dev, ptr);
1355 EXPORT_SYMBOL_GPL(devm_regulator_get);
1358 * regulator_get_exclusive - obtain exclusive access to a regulator.
1359 * @dev: device for regulator "consumer"
1360 * @id: Supply name or regulator ID.
1362 * Returns a struct regulator corresponding to the regulator producer,
1363 * or IS_ERR() condition containing errno. Other consumers will be
1364 * unable to obtain this reference is held and the use count for the
1365 * regulator will be initialised to reflect the current state of the
1368 * This is intended for use by consumers which cannot tolerate shared
1369 * use of the regulator such as those which need to force the
1370 * regulator off for correct operation of the hardware they are
1373 * Use of supply names configured via regulator_set_device_supply() is
1374 * strongly encouraged. It is recommended that the supply name used
1375 * should match the name used for the supply and/or the relevant
1376 * device pins in the datasheet.
1378 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1380 return _regulator_get(dev, id, 1);
1382 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1384 /* Locks held by regulator_put() */
1385 static void _regulator_put(struct regulator *regulator)
1387 struct regulator_dev *rdev;
1389 if (regulator == NULL || IS_ERR(regulator))
1392 rdev = regulator->rdev;
1394 debugfs_remove_recursive(regulator->debugfs);
1396 /* remove any sysfs entries */
1398 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1399 kfree(regulator->supply_name);
1400 list_del(®ulator->list);
1404 rdev->exclusive = 0;
1406 module_put(rdev->owner);
1410 * regulator_put - "free" the regulator source
1411 * @regulator: regulator source
1413 * Note: drivers must ensure that all regulator_enable calls made on this
1414 * regulator source are balanced by regulator_disable calls prior to calling
1417 void regulator_put(struct regulator *regulator)
1419 mutex_lock(®ulator_list_mutex);
1420 _regulator_put(regulator);
1421 mutex_unlock(®ulator_list_mutex);
1423 EXPORT_SYMBOL_GPL(regulator_put);
1425 static int devm_regulator_match(struct device *dev, void *res, void *data)
1427 struct regulator **r = res;
1436 * devm_regulator_put - Resource managed regulator_put()
1437 * @regulator: regulator to free
1439 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1440 * this function will not need to be called and the resource management
1441 * code will ensure that the resource is freed.
1443 void devm_regulator_put(struct regulator *regulator)
1447 rc = devres_release(regulator->dev, devm_regulator_release,
1448 devm_regulator_match, regulator);
1452 EXPORT_SYMBOL_GPL(devm_regulator_put);
1454 static int _regulator_do_enable(struct regulator_dev *rdev)
1458 /* Query before enabling in case configuration dependent. */
1459 ret = _regulator_get_enable_time(rdev);
1463 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1467 trace_regulator_enable(rdev_get_name(rdev));
1469 if (rdev->ena_gpio) {
1470 gpio_set_value_cansleep(rdev->ena_gpio,
1471 !rdev->ena_gpio_invert);
1472 rdev->ena_gpio_state = 1;
1473 } else if (rdev->desc->ops->enable) {
1474 ret = rdev->desc->ops->enable(rdev);
1481 /* Allow the regulator to ramp; it would be useful to extend
1482 * this for bulk operations so that the regulators can ramp
1484 trace_regulator_enable_delay(rdev_get_name(rdev));
1486 if (delay >= 1000) {
1487 mdelay(delay / 1000);
1488 udelay(delay % 1000);
1493 trace_regulator_enable_complete(rdev_get_name(rdev));
1498 /* locks held by regulator_enable() */
1499 static int _regulator_enable(struct regulator_dev *rdev)
1503 /* check voltage and requested load before enabling */
1504 if (rdev->constraints &&
1505 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1506 drms_uA_update(rdev);
1508 if (rdev->use_count == 0) {
1509 /* The regulator may on if it's not switchable or left on */
1510 ret = _regulator_is_enabled(rdev);
1511 if (ret == -EINVAL || ret == 0) {
1512 if (!_regulator_can_change_status(rdev))
1515 ret = _regulator_do_enable(rdev);
1519 } else if (ret < 0) {
1520 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1523 /* Fallthrough on positive return values - already enabled */
1532 * regulator_enable - enable regulator output
1533 * @regulator: regulator source
1535 * Request that the regulator be enabled with the regulator output at
1536 * the predefined voltage or current value. Calls to regulator_enable()
1537 * must be balanced with calls to regulator_disable().
1539 * NOTE: the output value can be set by other drivers, boot loader or may be
1540 * hardwired in the regulator.
1542 int regulator_enable(struct regulator *regulator)
1544 struct regulator_dev *rdev = regulator->rdev;
1547 if (regulator->always_on)
1551 ret = regulator_enable(rdev->supply);
1556 mutex_lock(&rdev->mutex);
1557 ret = _regulator_enable(rdev);
1558 mutex_unlock(&rdev->mutex);
1560 if (ret != 0 && rdev->supply)
1561 regulator_disable(rdev->supply);
1565 EXPORT_SYMBOL_GPL(regulator_enable);
1567 static int _regulator_do_disable(struct regulator_dev *rdev)
1571 trace_regulator_disable(rdev_get_name(rdev));
1573 if (rdev->ena_gpio) {
1574 gpio_set_value_cansleep(rdev->ena_gpio,
1575 rdev->ena_gpio_invert);
1576 rdev->ena_gpio_state = 0;
1578 } else if (rdev->desc->ops->disable) {
1579 ret = rdev->desc->ops->disable(rdev);
1584 trace_regulator_disable_complete(rdev_get_name(rdev));
1586 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1591 /* locks held by regulator_disable() */
1592 static int _regulator_disable(struct regulator_dev *rdev)
1596 if (WARN(rdev->use_count <= 0,
1597 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1600 /* are we the last user and permitted to disable ? */
1601 if (rdev->use_count == 1 &&
1602 (rdev->constraints && !rdev->constraints->always_on)) {
1604 /* we are last user */
1605 if (_regulator_can_change_status(rdev)) {
1606 ret = _regulator_do_disable(rdev);
1608 rdev_err(rdev, "failed to disable\n");
1613 rdev->use_count = 0;
1614 } else if (rdev->use_count > 1) {
1616 if (rdev->constraints &&
1617 (rdev->constraints->valid_ops_mask &
1618 REGULATOR_CHANGE_DRMS))
1619 drms_uA_update(rdev);
1628 * regulator_disable - disable regulator output
1629 * @regulator: regulator source
1631 * Disable the regulator output voltage or current. Calls to
1632 * regulator_enable() must be balanced with calls to
1633 * regulator_disable().
1635 * NOTE: this will only disable the regulator output if no other consumer
1636 * devices have it enabled, the regulator device supports disabling and
1637 * machine constraints permit this operation.
1639 int regulator_disable(struct regulator *regulator)
1641 struct regulator_dev *rdev = regulator->rdev;
1644 if (regulator->always_on)
1647 mutex_lock(&rdev->mutex);
1648 ret = _regulator_disable(rdev);
1649 mutex_unlock(&rdev->mutex);
1651 if (ret == 0 && rdev->supply)
1652 regulator_disable(rdev->supply);
1656 EXPORT_SYMBOL_GPL(regulator_disable);
1658 /* locks held by regulator_force_disable() */
1659 static int _regulator_force_disable(struct regulator_dev *rdev)
1664 if (rdev->desc->ops->disable) {
1665 /* ah well, who wants to live forever... */
1666 ret = rdev->desc->ops->disable(rdev);
1668 rdev_err(rdev, "failed to force disable\n");
1671 /* notify other consumers that power has been forced off */
1672 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1673 REGULATOR_EVENT_DISABLE, NULL);
1680 * regulator_force_disable - force disable regulator output
1681 * @regulator: regulator source
1683 * Forcibly disable the regulator output voltage or current.
1684 * NOTE: this *will* disable the regulator output even if other consumer
1685 * devices have it enabled. This should be used for situations when device
1686 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1688 int regulator_force_disable(struct regulator *regulator)
1690 struct regulator_dev *rdev = regulator->rdev;
1693 mutex_lock(&rdev->mutex);
1694 regulator->uA_load = 0;
1695 ret = _regulator_force_disable(regulator->rdev);
1696 mutex_unlock(&rdev->mutex);
1699 while (rdev->open_count--)
1700 regulator_disable(rdev->supply);
1704 EXPORT_SYMBOL_GPL(regulator_force_disable);
1706 static void regulator_disable_work(struct work_struct *work)
1708 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1712 mutex_lock(&rdev->mutex);
1714 BUG_ON(!rdev->deferred_disables);
1716 count = rdev->deferred_disables;
1717 rdev->deferred_disables = 0;
1719 for (i = 0; i < count; i++) {
1720 ret = _regulator_disable(rdev);
1722 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1725 mutex_unlock(&rdev->mutex);
1728 for (i = 0; i < count; i++) {
1729 ret = regulator_disable(rdev->supply);
1732 "Supply disable failed: %d\n", ret);
1739 * regulator_disable_deferred - disable regulator output with delay
1740 * @regulator: regulator source
1741 * @ms: miliseconds until the regulator is disabled
1743 * Execute regulator_disable() on the regulator after a delay. This
1744 * is intended for use with devices that require some time to quiesce.
1746 * NOTE: this will only disable the regulator output if no other consumer
1747 * devices have it enabled, the regulator device supports disabling and
1748 * machine constraints permit this operation.
1750 int regulator_disable_deferred(struct regulator *regulator, int ms)
1752 struct regulator_dev *rdev = regulator->rdev;
1755 if (regulator->always_on)
1759 return regulator_disable(regulator);
1761 mutex_lock(&rdev->mutex);
1762 rdev->deferred_disables++;
1763 mutex_unlock(&rdev->mutex);
1765 ret = schedule_delayed_work(&rdev->disable_work,
1766 msecs_to_jiffies(ms));
1772 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1775 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1777 * @rdev: regulator to operate on
1779 * Regulators that use regmap for their register I/O can set the
1780 * enable_reg and enable_mask fields in their descriptor and then use
1781 * this as their is_enabled operation, saving some code.
1783 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1788 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1792 return (val & rdev->desc->enable_mask) != 0;
1794 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1797 * regulator_enable_regmap - standard enable() for regmap users
1799 * @rdev: regulator to operate on
1801 * Regulators that use regmap for their register I/O can set the
1802 * enable_reg and enable_mask fields in their descriptor and then use
1803 * this as their enable() operation, saving some code.
1805 int regulator_enable_regmap(struct regulator_dev *rdev)
1807 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1808 rdev->desc->enable_mask,
1809 rdev->desc->enable_mask);
1811 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1814 * regulator_disable_regmap - standard disable() for regmap users
1816 * @rdev: regulator to operate on
1818 * Regulators that use regmap for their register I/O can set the
1819 * enable_reg and enable_mask fields in their descriptor and then use
1820 * this as their disable() operation, saving some code.
1822 int regulator_disable_regmap(struct regulator_dev *rdev)
1824 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1825 rdev->desc->enable_mask, 0);
1827 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1829 static int _regulator_is_enabled(struct regulator_dev *rdev)
1831 /* A GPIO control always takes precedence */
1833 return rdev->ena_gpio_state;
1835 /* If we don't know then assume that the regulator is always on */
1836 if (!rdev->desc->ops->is_enabled)
1839 return rdev->desc->ops->is_enabled(rdev);
1843 * regulator_is_enabled - is the regulator output enabled
1844 * @regulator: regulator source
1846 * Returns positive if the regulator driver backing the source/client
1847 * has requested that the device be enabled, zero if it hasn't, else a
1848 * negative errno code.
1850 * Note that the device backing this regulator handle can have multiple
1851 * users, so it might be enabled even if regulator_enable() was never
1852 * called for this particular source.
1854 int regulator_is_enabled(struct regulator *regulator)
1858 if (regulator->always_on)
1861 mutex_lock(®ulator->rdev->mutex);
1862 ret = _regulator_is_enabled(regulator->rdev);
1863 mutex_unlock(®ulator->rdev->mutex);
1867 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1870 * regulator_can_change_voltage - check if regulator can change voltage
1871 * @regulator: regulator source
1873 * Returns positive if the regulator driver backing the source/client
1874 * can change its voltage, false otherwise. Usefull for detecting fixed
1875 * or dummy regulators and disabling voltage change logic in the client
1878 int regulator_can_change_voltage(struct regulator *regulator)
1880 struct regulator_dev *rdev = regulator->rdev;
1882 if (rdev->constraints &&
1883 rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE &&
1884 rdev->desc->n_voltages > 1)
1889 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
1892 * regulator_count_voltages - count regulator_list_voltage() selectors
1893 * @regulator: regulator source
1895 * Returns number of selectors, or negative errno. Selectors are
1896 * numbered starting at zero, and typically correspond to bitfields
1897 * in hardware registers.
1899 int regulator_count_voltages(struct regulator *regulator)
1901 struct regulator_dev *rdev = regulator->rdev;
1903 return rdev->desc->n_voltages ? : -EINVAL;
1905 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1908 * regulator_list_voltage_linear - List voltages with simple calculation
1910 * @rdev: Regulator device
1911 * @selector: Selector to convert into a voltage
1913 * Regulators with a simple linear mapping between voltages and
1914 * selectors can set min_uV and uV_step in the regulator descriptor
1915 * and then use this function as their list_voltage() operation,
1917 int regulator_list_voltage_linear(struct regulator_dev *rdev,
1918 unsigned int selector)
1920 if (selector >= rdev->desc->n_voltages)
1923 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1925 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1928 * regulator_list_voltage_table - List voltages with table based mapping
1930 * @rdev: Regulator device
1931 * @selector: Selector to convert into a voltage
1933 * Regulators with table based mapping between voltages and
1934 * selectors can set volt_table in the regulator descriptor
1935 * and then use this function as their list_voltage() operation.
1937 int regulator_list_voltage_table(struct regulator_dev *rdev,
1938 unsigned int selector)
1940 if (!rdev->desc->volt_table) {
1941 BUG_ON(!rdev->desc->volt_table);
1945 if (selector >= rdev->desc->n_voltages)
1948 return rdev->desc->volt_table[selector];
1950 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
1953 * regulator_list_voltage - enumerate supported voltages
1954 * @regulator: regulator source
1955 * @selector: identify voltage to list
1956 * Context: can sleep
1958 * Returns a voltage that can be passed to @regulator_set_voltage(),
1959 * zero if this selector code can't be used on this system, or a
1962 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1964 struct regulator_dev *rdev = regulator->rdev;
1965 struct regulator_ops *ops = rdev->desc->ops;
1968 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1971 mutex_lock(&rdev->mutex);
1972 ret = ops->list_voltage(rdev, selector);
1973 mutex_unlock(&rdev->mutex);
1976 if (ret < rdev->constraints->min_uV)
1978 else if (ret > rdev->constraints->max_uV)
1984 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1987 * regulator_is_supported_voltage - check if a voltage range can be supported
1989 * @regulator: Regulator to check.
1990 * @min_uV: Minimum required voltage in uV.
1991 * @max_uV: Maximum required voltage in uV.
1993 * Returns a boolean or a negative error code.
1995 int regulator_is_supported_voltage(struct regulator *regulator,
1996 int min_uV, int max_uV)
1998 struct regulator_dev *rdev = regulator->rdev;
1999 int i, voltages, ret;
2001 /* If we can't change voltage check the current voltage */
2002 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2003 ret = regulator_get_voltage(regulator);
2005 return (min_uV <= ret && ret <= max_uV);
2010 ret = regulator_count_voltages(regulator);
2015 for (i = 0; i < voltages; i++) {
2016 ret = regulator_list_voltage(regulator, i);
2018 if (ret >= min_uV && ret <= max_uV)
2024 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2027 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2029 * @rdev: regulator to operate on
2031 * Regulators that use regmap for their register I/O can set the
2032 * vsel_reg and vsel_mask fields in their descriptor and then use this
2033 * as their get_voltage_vsel operation, saving some code.
2035 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2040 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2044 val &= rdev->desc->vsel_mask;
2045 val >>= ffs(rdev->desc->vsel_mask) - 1;
2049 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2052 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2054 * @rdev: regulator to operate on
2055 * @sel: Selector to set
2057 * Regulators that use regmap for their register I/O can set the
2058 * vsel_reg and vsel_mask fields in their descriptor and then use this
2059 * as their set_voltage_vsel operation, saving some code.
2061 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2063 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2065 return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2066 rdev->desc->vsel_mask, sel);
2068 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2071 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2073 * @rdev: Regulator to operate on
2074 * @min_uV: Lower bound for voltage
2075 * @max_uV: Upper bound for voltage
2077 * Drivers implementing set_voltage_sel() and list_voltage() can use
2078 * this as their map_voltage() operation. It will find a suitable
2079 * voltage by calling list_voltage() until it gets something in bounds
2080 * for the requested voltages.
2082 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2083 int min_uV, int max_uV)
2085 int best_val = INT_MAX;
2089 /* Find the smallest voltage that falls within the specified
2092 for (i = 0; i < rdev->desc->n_voltages; i++) {
2093 ret = rdev->desc->ops->list_voltage(rdev, i);
2097 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2103 if (best_val != INT_MAX)
2108 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2111 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2113 * @rdev: Regulator to operate on
2114 * @min_uV: Lower bound for voltage
2115 * @max_uV: Upper bound for voltage
2117 * Drivers providing min_uV and uV_step in their regulator_desc can
2118 * use this as their map_voltage() operation.
2120 int regulator_map_voltage_linear(struct regulator_dev *rdev,
2121 int min_uV, int max_uV)
2125 /* Allow uV_step to be 0 for fixed voltage */
2126 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2127 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2133 if (!rdev->desc->uV_step) {
2134 BUG_ON(!rdev->desc->uV_step);
2138 if (min_uV < rdev->desc->min_uV)
2139 min_uV = rdev->desc->min_uV;
2141 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2145 /* Map back into a voltage to verify we're still in bounds */
2146 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2147 if (voltage < min_uV || voltage > max_uV)
2152 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2154 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2155 int min_uV, int max_uV)
2160 unsigned int selector;
2161 int old_selector = -1;
2163 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2165 min_uV += rdev->constraints->uV_offset;
2166 max_uV += rdev->constraints->uV_offset;
2169 * If we can't obtain the old selector there is not enough
2170 * info to call set_voltage_time_sel().
2172 if (_regulator_is_enabled(rdev) &&
2173 rdev->desc->ops->set_voltage_time_sel &&
2174 rdev->desc->ops->get_voltage_sel) {
2175 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2176 if (old_selector < 0)
2177 return old_selector;
2180 if (rdev->desc->ops->set_voltage) {
2181 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2185 if (rdev->desc->ops->list_voltage)
2186 best_val = rdev->desc->ops->list_voltage(rdev,
2189 best_val = _regulator_get_voltage(rdev);
2192 } else if (rdev->desc->ops->set_voltage_sel) {
2193 if (rdev->desc->ops->map_voltage) {
2194 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2197 if (rdev->desc->ops->list_voltage ==
2198 regulator_list_voltage_linear)
2199 ret = regulator_map_voltage_linear(rdev,
2202 ret = regulator_map_voltage_iterate(rdev,
2207 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2208 if (min_uV <= best_val && max_uV >= best_val) {
2210 ret = rdev->desc->ops->set_voltage_sel(rdev,
2220 /* Call set_voltage_time_sel if successfully obtained old_selector */
2221 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2222 rdev->desc->ops->set_voltage_time_sel) {
2224 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2225 old_selector, selector);
2227 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2232 /* Insert any necessary delays */
2233 if (delay >= 1000) {
2234 mdelay(delay / 1000);
2235 udelay(delay % 1000);
2241 if (ret == 0 && best_val >= 0) {
2242 unsigned long data = best_val;
2244 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2248 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2254 * regulator_set_voltage - set regulator output voltage
2255 * @regulator: regulator source
2256 * @min_uV: Minimum required voltage in uV
2257 * @max_uV: Maximum acceptable voltage in uV
2259 * Sets a voltage regulator to the desired output voltage. This can be set
2260 * during any regulator state. IOW, regulator can be disabled or enabled.
2262 * If the regulator is enabled then the voltage will change to the new value
2263 * immediately otherwise if the regulator is disabled the regulator will
2264 * output at the new voltage when enabled.
2266 * NOTE: If the regulator is shared between several devices then the lowest
2267 * request voltage that meets the system constraints will be used.
2268 * Regulator system constraints must be set for this regulator before
2269 * calling this function otherwise this call will fail.
2271 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2273 struct regulator_dev *rdev = regulator->rdev;
2276 mutex_lock(&rdev->mutex);
2278 /* If we're setting the same range as last time the change
2279 * should be a noop (some cpufreq implementations use the same
2280 * voltage for multiple frequencies, for example).
2282 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2286 if (!rdev->desc->ops->set_voltage &&
2287 !rdev->desc->ops->set_voltage_sel) {
2292 /* constraints check */
2293 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2296 regulator->min_uV = min_uV;
2297 regulator->max_uV = max_uV;
2299 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2303 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2306 mutex_unlock(&rdev->mutex);
2309 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2312 * regulator_set_voltage_time - get raise/fall time
2313 * @regulator: regulator source
2314 * @old_uV: starting voltage in microvolts
2315 * @new_uV: target voltage in microvolts
2317 * Provided with the starting and ending voltage, this function attempts to
2318 * calculate the time in microseconds required to rise or fall to this new
2321 int regulator_set_voltage_time(struct regulator *regulator,
2322 int old_uV, int new_uV)
2324 struct regulator_dev *rdev = regulator->rdev;
2325 struct regulator_ops *ops = rdev->desc->ops;
2331 /* Currently requires operations to do this */
2332 if (!ops->list_voltage || !ops->set_voltage_time_sel
2333 || !rdev->desc->n_voltages)
2336 for (i = 0; i < rdev->desc->n_voltages; i++) {
2337 /* We only look for exact voltage matches here */
2338 voltage = regulator_list_voltage(regulator, i);
2343 if (voltage == old_uV)
2345 if (voltage == new_uV)
2349 if (old_sel < 0 || new_sel < 0)
2352 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2354 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2357 * regulator_set_voltage_time_sel - get raise/fall time
2358 * @rdev: regulator source device
2359 * @old_selector: selector for starting voltage
2360 * @new_selector: selector for target voltage
2362 * Provided with the starting and target voltage selectors, this function
2363 * returns time in microseconds required to rise or fall to this new voltage
2365 * Drivers providing ramp_delay in regulation_constraints can use this as their
2366 * set_voltage_time_sel() operation.
2368 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2369 unsigned int old_selector,
2370 unsigned int new_selector)
2372 unsigned int ramp_delay = 0;
2373 int old_volt, new_volt;
2375 if (rdev->constraints->ramp_delay)
2376 ramp_delay = rdev->constraints->ramp_delay;
2377 else if (rdev->desc->ramp_delay)
2378 ramp_delay = rdev->desc->ramp_delay;
2380 if (ramp_delay == 0) {
2381 rdev_warn(rdev, "ramp_delay not set\n");
2386 if (!rdev->desc->ops->list_voltage)
2389 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2390 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2392 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2394 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2397 * regulator_sync_voltage - re-apply last regulator output voltage
2398 * @regulator: regulator source
2400 * Re-apply the last configured voltage. This is intended to be used
2401 * where some external control source the consumer is cooperating with
2402 * has caused the configured voltage to change.
2404 int regulator_sync_voltage(struct regulator *regulator)
2406 struct regulator_dev *rdev = regulator->rdev;
2407 int ret, min_uV, max_uV;
2409 mutex_lock(&rdev->mutex);
2411 if (!rdev->desc->ops->set_voltage &&
2412 !rdev->desc->ops->set_voltage_sel) {
2417 /* This is only going to work if we've had a voltage configured. */
2418 if (!regulator->min_uV && !regulator->max_uV) {
2423 min_uV = regulator->min_uV;
2424 max_uV = regulator->max_uV;
2426 /* This should be a paranoia check... */
2427 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2431 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2435 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2438 mutex_unlock(&rdev->mutex);
2441 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2443 static int _regulator_get_voltage(struct regulator_dev *rdev)
2447 if (rdev->desc->ops->get_voltage_sel) {
2448 sel = rdev->desc->ops->get_voltage_sel(rdev);
2451 ret = rdev->desc->ops->list_voltage(rdev, sel);
2452 } else if (rdev->desc->ops->get_voltage) {
2453 ret = rdev->desc->ops->get_voltage(rdev);
2454 } else if (rdev->desc->ops->list_voltage) {
2455 ret = rdev->desc->ops->list_voltage(rdev, 0);
2462 return ret - rdev->constraints->uV_offset;
2466 * regulator_get_voltage - get regulator output voltage
2467 * @regulator: regulator source
2469 * This returns the current regulator voltage in uV.
2471 * NOTE: If the regulator is disabled it will return the voltage value. This
2472 * function should not be used to determine regulator state.
2474 int regulator_get_voltage(struct regulator *regulator)
2478 mutex_lock(®ulator->rdev->mutex);
2480 ret = _regulator_get_voltage(regulator->rdev);
2482 mutex_unlock(®ulator->rdev->mutex);
2486 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2489 * regulator_set_current_limit - set regulator output current limit
2490 * @regulator: regulator source
2491 * @min_uA: Minimuum supported current in uA
2492 * @max_uA: Maximum supported current in uA
2494 * Sets current sink to the desired output current. This can be set during
2495 * any regulator state. IOW, regulator can be disabled or enabled.
2497 * If the regulator is enabled then the current will change to the new value
2498 * immediately otherwise if the regulator is disabled the regulator will
2499 * output at the new current when enabled.
2501 * NOTE: Regulator system constraints must be set for this regulator before
2502 * calling this function otherwise this call will fail.
2504 int regulator_set_current_limit(struct regulator *regulator,
2505 int min_uA, int max_uA)
2507 struct regulator_dev *rdev = regulator->rdev;
2510 mutex_lock(&rdev->mutex);
2513 if (!rdev->desc->ops->set_current_limit) {
2518 /* constraints check */
2519 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2523 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2525 mutex_unlock(&rdev->mutex);
2528 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2530 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2534 mutex_lock(&rdev->mutex);
2537 if (!rdev->desc->ops->get_current_limit) {
2542 ret = rdev->desc->ops->get_current_limit(rdev);
2544 mutex_unlock(&rdev->mutex);
2549 * regulator_get_current_limit - get regulator output current
2550 * @regulator: regulator source
2552 * This returns the current supplied by the specified current sink in uA.
2554 * NOTE: If the regulator is disabled it will return the current value. This
2555 * function should not be used to determine regulator state.
2557 int regulator_get_current_limit(struct regulator *regulator)
2559 return _regulator_get_current_limit(regulator->rdev);
2561 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2564 * regulator_set_mode - set regulator operating mode
2565 * @regulator: regulator source
2566 * @mode: operating mode - one of the REGULATOR_MODE constants
2568 * Set regulator operating mode to increase regulator efficiency or improve
2569 * regulation performance.
2571 * NOTE: Regulator system constraints must be set for this regulator before
2572 * calling this function otherwise this call will fail.
2574 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2576 struct regulator_dev *rdev = regulator->rdev;
2578 int regulator_curr_mode;
2580 mutex_lock(&rdev->mutex);
2583 if (!rdev->desc->ops->set_mode) {
2588 /* return if the same mode is requested */
2589 if (rdev->desc->ops->get_mode) {
2590 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2591 if (regulator_curr_mode == mode) {
2597 /* constraints check */
2598 ret = regulator_mode_constrain(rdev, &mode);
2602 ret = rdev->desc->ops->set_mode(rdev, mode);
2604 mutex_unlock(&rdev->mutex);
2607 EXPORT_SYMBOL_GPL(regulator_set_mode);
2609 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2613 mutex_lock(&rdev->mutex);
2616 if (!rdev->desc->ops->get_mode) {
2621 ret = rdev->desc->ops->get_mode(rdev);
2623 mutex_unlock(&rdev->mutex);
2628 * regulator_get_mode - get regulator operating mode
2629 * @regulator: regulator source
2631 * Get the current regulator operating mode.
2633 unsigned int regulator_get_mode(struct regulator *regulator)
2635 return _regulator_get_mode(regulator->rdev);
2637 EXPORT_SYMBOL_GPL(regulator_get_mode);
2640 * regulator_set_optimum_mode - set regulator optimum operating mode
2641 * @regulator: regulator source
2642 * @uA_load: load current
2644 * Notifies the regulator core of a new device load. This is then used by
2645 * DRMS (if enabled by constraints) to set the most efficient regulator
2646 * operating mode for the new regulator loading.
2648 * Consumer devices notify their supply regulator of the maximum power
2649 * they will require (can be taken from device datasheet in the power
2650 * consumption tables) when they change operational status and hence power
2651 * state. Examples of operational state changes that can affect power
2652 * consumption are :-
2654 * o Device is opened / closed.
2655 * o Device I/O is about to begin or has just finished.
2656 * o Device is idling in between work.
2658 * This information is also exported via sysfs to userspace.
2660 * DRMS will sum the total requested load on the regulator and change
2661 * to the most efficient operating mode if platform constraints allow.
2663 * Returns the new regulator mode or error.
2665 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2667 struct regulator_dev *rdev = regulator->rdev;
2668 struct regulator *consumer;
2669 int ret, output_uV, input_uV = 0, total_uA_load = 0;
2673 input_uV = regulator_get_voltage(rdev->supply);
2675 mutex_lock(&rdev->mutex);
2678 * first check to see if we can set modes at all, otherwise just
2679 * tell the consumer everything is OK.
2681 regulator->uA_load = uA_load;
2682 ret = regulator_check_drms(rdev);
2688 if (!rdev->desc->ops->get_optimum_mode)
2692 * we can actually do this so any errors are indicators of
2693 * potential real failure.
2697 if (!rdev->desc->ops->set_mode)
2700 /* get output voltage */
2701 output_uV = _regulator_get_voltage(rdev);
2702 if (output_uV <= 0) {
2703 rdev_err(rdev, "invalid output voltage found\n");
2707 /* No supply? Use constraint voltage */
2709 input_uV = rdev->constraints->input_uV;
2710 if (input_uV <= 0) {
2711 rdev_err(rdev, "invalid input voltage found\n");
2715 /* calc total requested load for this regulator */
2716 list_for_each_entry(consumer, &rdev->consumer_list, list)
2717 total_uA_load += consumer->uA_load;
2719 mode = rdev->desc->ops->get_optimum_mode(rdev,
2720 input_uV, output_uV,
2722 ret = regulator_mode_constrain(rdev, &mode);
2724 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2725 total_uA_load, input_uV, output_uV);
2729 ret = rdev->desc->ops->set_mode(rdev, mode);
2731 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2736 mutex_unlock(&rdev->mutex);
2739 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2742 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2744 * @rdev: device to operate on.
2745 * @enable: state to set.
2747 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2752 val = rdev->desc->bypass_mask;
2756 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2757 rdev->desc->bypass_mask, val);
2759 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2762 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2764 * @rdev: device to operate on.
2765 * @enable: current state.
2767 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2772 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2776 *enable = val & rdev->desc->bypass_mask;
2780 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2783 * regulator_allow_bypass - allow the regulator to go into bypass mode
2785 * @regulator: Regulator to configure
2786 * @allow: enable or disable bypass mode
2788 * Allow the regulator to go into bypass mode if all other consumers
2789 * for the regulator also enable bypass mode and the machine
2790 * constraints allow this. Bypass mode means that the regulator is
2791 * simply passing the input directly to the output with no regulation.
2793 int regulator_allow_bypass(struct regulator *regulator, bool enable)
2795 struct regulator_dev *rdev = regulator->rdev;
2798 if (!rdev->desc->ops->set_bypass)
2801 if (rdev->constraints &&
2802 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2805 mutex_lock(&rdev->mutex);
2807 if (enable && !regulator->bypass) {
2808 rdev->bypass_count++;
2810 if (rdev->bypass_count == rdev->open_count) {
2811 ret = rdev->desc->ops->set_bypass(rdev, enable);
2813 rdev->bypass_count--;
2816 } else if (!enable && regulator->bypass) {
2817 rdev->bypass_count--;
2819 if (rdev->bypass_count != rdev->open_count) {
2820 ret = rdev->desc->ops->set_bypass(rdev, enable);
2822 rdev->bypass_count++;
2827 regulator->bypass = enable;
2829 mutex_unlock(&rdev->mutex);
2833 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2836 * regulator_register_notifier - register regulator event notifier
2837 * @regulator: regulator source
2838 * @nb: notifier block
2840 * Register notifier block to receive regulator events.
2842 int regulator_register_notifier(struct regulator *regulator,
2843 struct notifier_block *nb)
2845 return blocking_notifier_chain_register(®ulator->rdev->notifier,
2848 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2851 * regulator_unregister_notifier - unregister regulator event notifier
2852 * @regulator: regulator source
2853 * @nb: notifier block
2855 * Unregister regulator event notifier block.
2857 int regulator_unregister_notifier(struct regulator *regulator,
2858 struct notifier_block *nb)
2860 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
2863 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2865 /* notify regulator consumers and downstream regulator consumers.
2866 * Note mutex must be held by caller.
2868 static void _notifier_call_chain(struct regulator_dev *rdev,
2869 unsigned long event, void *data)
2871 /* call rdev chain first */
2872 blocking_notifier_call_chain(&rdev->notifier, event, data);
2876 * regulator_bulk_get - get multiple regulator consumers
2878 * @dev: Device to supply
2879 * @num_consumers: Number of consumers to register
2880 * @consumers: Configuration of consumers; clients are stored here.
2882 * @return 0 on success, an errno on failure.
2884 * This helper function allows drivers to get several regulator
2885 * consumers in one operation. If any of the regulators cannot be
2886 * acquired then any regulators that were allocated will be freed
2887 * before returning to the caller.
2889 int regulator_bulk_get(struct device *dev, int num_consumers,
2890 struct regulator_bulk_data *consumers)
2895 for (i = 0; i < num_consumers; i++)
2896 consumers[i].consumer = NULL;
2898 for (i = 0; i < num_consumers; i++) {
2899 consumers[i].consumer = regulator_get(dev,
2900 consumers[i].supply);
2901 if (IS_ERR(consumers[i].consumer)) {
2902 ret = PTR_ERR(consumers[i].consumer);
2903 dev_err(dev, "Failed to get supply '%s': %d\n",
2904 consumers[i].supply, ret);
2905 consumers[i].consumer = NULL;
2914 regulator_put(consumers[i].consumer);
2918 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2921 * devm_regulator_bulk_get - managed get multiple regulator consumers
2923 * @dev: Device to supply
2924 * @num_consumers: Number of consumers to register
2925 * @consumers: Configuration of consumers; clients are stored here.
2927 * @return 0 on success, an errno on failure.
2929 * This helper function allows drivers to get several regulator
2930 * consumers in one operation with management, the regulators will
2931 * automatically be freed when the device is unbound. If any of the
2932 * regulators cannot be acquired then any regulators that were
2933 * allocated will be freed before returning to the caller.
2935 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2936 struct regulator_bulk_data *consumers)
2941 for (i = 0; i < num_consumers; i++)
2942 consumers[i].consumer = NULL;
2944 for (i = 0; i < num_consumers; i++) {
2945 consumers[i].consumer = devm_regulator_get(dev,
2946 consumers[i].supply);
2947 if (IS_ERR(consumers[i].consumer)) {
2948 ret = PTR_ERR(consumers[i].consumer);
2949 dev_err(dev, "Failed to get supply '%s': %d\n",
2950 consumers[i].supply, ret);
2951 consumers[i].consumer = NULL;
2959 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2960 devm_regulator_put(consumers[i].consumer);
2964 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2966 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2968 struct regulator_bulk_data *bulk = data;
2970 bulk->ret = regulator_enable(bulk->consumer);
2974 * regulator_bulk_enable - enable multiple regulator consumers
2976 * @num_consumers: Number of consumers
2977 * @consumers: Consumer data; clients are stored here.
2978 * @return 0 on success, an errno on failure
2980 * This convenience API allows consumers to enable multiple regulator
2981 * clients in a single API call. If any consumers cannot be enabled
2982 * then any others that were enabled will be disabled again prior to
2985 int regulator_bulk_enable(int num_consumers,
2986 struct regulator_bulk_data *consumers)
2988 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
2992 for (i = 0; i < num_consumers; i++) {
2993 if (consumers[i].consumer->always_on)
2994 consumers[i].ret = 0;
2996 async_schedule_domain(regulator_bulk_enable_async,
2997 &consumers[i], &async_domain);
3000 async_synchronize_full_domain(&async_domain);
3002 /* If any consumer failed we need to unwind any that succeeded */
3003 for (i = 0; i < num_consumers; i++) {
3004 if (consumers[i].ret != 0) {
3005 ret = consumers[i].ret;
3013 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
3015 regulator_disable(consumers[i].consumer);
3019 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3022 * regulator_bulk_disable - disable multiple regulator consumers
3024 * @num_consumers: Number of consumers
3025 * @consumers: Consumer data; clients are stored here.
3026 * @return 0 on success, an errno on failure
3028 * This convenience API allows consumers to disable multiple regulator
3029 * clients in a single API call. If any consumers cannot be disabled
3030 * then any others that were disabled will be enabled again prior to
3033 int regulator_bulk_disable(int num_consumers,
3034 struct regulator_bulk_data *consumers)
3039 for (i = num_consumers - 1; i >= 0; --i) {
3040 ret = regulator_disable(consumers[i].consumer);
3048 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3049 for (++i; i < num_consumers; ++i) {
3050 r = regulator_enable(consumers[i].consumer);
3052 pr_err("Failed to reename %s: %d\n",
3053 consumers[i].supply, r);
3058 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3061 * regulator_bulk_force_disable - force disable multiple regulator consumers
3063 * @num_consumers: Number of consumers
3064 * @consumers: Consumer data; clients are stored here.
3065 * @return 0 on success, an errno on failure
3067 * This convenience API allows consumers to forcibly disable multiple regulator
3068 * clients in a single API call.
3069 * NOTE: This should be used for situations when device damage will
3070 * likely occur if the regulators are not disabled (e.g. over temp).
3071 * Although regulator_force_disable function call for some consumers can
3072 * return error numbers, the function is called for all consumers.
3074 int regulator_bulk_force_disable(int num_consumers,
3075 struct regulator_bulk_data *consumers)
3080 for (i = 0; i < num_consumers; i++)
3082 regulator_force_disable(consumers[i].consumer);
3084 for (i = 0; i < num_consumers; i++) {
3085 if (consumers[i].ret != 0) {
3086 ret = consumers[i].ret;
3095 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3098 * regulator_bulk_free - free multiple regulator consumers
3100 * @num_consumers: Number of consumers
3101 * @consumers: Consumer data; clients are stored here.
3103 * This convenience API allows consumers to free multiple regulator
3104 * clients in a single API call.
3106 void regulator_bulk_free(int num_consumers,
3107 struct regulator_bulk_data *consumers)
3111 for (i = 0; i < num_consumers; i++) {
3112 regulator_put(consumers[i].consumer);
3113 consumers[i].consumer = NULL;
3116 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3119 * regulator_notifier_call_chain - call regulator event notifier
3120 * @rdev: regulator source
3121 * @event: notifier block
3122 * @data: callback-specific data.
3124 * Called by regulator drivers to notify clients a regulator event has
3125 * occurred. We also notify regulator clients downstream.
3126 * Note lock must be held by caller.
3128 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3129 unsigned long event, void *data)
3131 _notifier_call_chain(rdev, event, data);
3135 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3138 * regulator_mode_to_status - convert a regulator mode into a status
3140 * @mode: Mode to convert
3142 * Convert a regulator mode into a status.
3144 int regulator_mode_to_status(unsigned int mode)
3147 case REGULATOR_MODE_FAST:
3148 return REGULATOR_STATUS_FAST;
3149 case REGULATOR_MODE_NORMAL:
3150 return REGULATOR_STATUS_NORMAL;
3151 case REGULATOR_MODE_IDLE:
3152 return REGULATOR_STATUS_IDLE;
3153 case REGULATOR_MODE_STANDBY:
3154 return REGULATOR_STATUS_STANDBY;
3156 return REGULATOR_STATUS_UNDEFINED;
3159 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3162 * To avoid cluttering sysfs (and memory) with useless state, only
3163 * create attributes that can be meaningfully displayed.
3165 static int add_regulator_attributes(struct regulator_dev *rdev)
3167 struct device *dev = &rdev->dev;
3168 struct regulator_ops *ops = rdev->desc->ops;
3171 /* some attributes need specific methods to be displayed */
3172 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3173 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3174 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3175 status = device_create_file(dev, &dev_attr_microvolts);
3179 if (ops->get_current_limit) {
3180 status = device_create_file(dev, &dev_attr_microamps);
3184 if (ops->get_mode) {
3185 status = device_create_file(dev, &dev_attr_opmode);
3189 if (ops->is_enabled) {
3190 status = device_create_file(dev, &dev_attr_state);
3194 if (ops->get_status) {
3195 status = device_create_file(dev, &dev_attr_status);
3199 if (ops->get_bypass) {
3200 status = device_create_file(dev, &dev_attr_bypass);
3205 /* some attributes are type-specific */
3206 if (rdev->desc->type == REGULATOR_CURRENT) {
3207 status = device_create_file(dev, &dev_attr_requested_microamps);
3212 /* all the other attributes exist to support constraints;
3213 * don't show them if there are no constraints, or if the
3214 * relevant supporting methods are missing.
3216 if (!rdev->constraints)
3219 /* constraints need specific supporting methods */
3220 if (ops->set_voltage || ops->set_voltage_sel) {
3221 status = device_create_file(dev, &dev_attr_min_microvolts);
3224 status = device_create_file(dev, &dev_attr_max_microvolts);
3228 if (ops->set_current_limit) {
3229 status = device_create_file(dev, &dev_attr_min_microamps);
3232 status = device_create_file(dev, &dev_attr_max_microamps);
3237 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3240 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3243 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3247 if (ops->set_suspend_voltage) {
3248 status = device_create_file(dev,
3249 &dev_attr_suspend_standby_microvolts);
3252 status = device_create_file(dev,
3253 &dev_attr_suspend_mem_microvolts);
3256 status = device_create_file(dev,
3257 &dev_attr_suspend_disk_microvolts);
3262 if (ops->set_suspend_mode) {
3263 status = device_create_file(dev,
3264 &dev_attr_suspend_standby_mode);
3267 status = device_create_file(dev,
3268 &dev_attr_suspend_mem_mode);
3271 status = device_create_file(dev,
3272 &dev_attr_suspend_disk_mode);
3280 static void rdev_init_debugfs(struct regulator_dev *rdev)
3282 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3283 if (!rdev->debugfs) {
3284 rdev_warn(rdev, "Failed to create debugfs directory\n");
3288 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3290 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3292 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3293 &rdev->bypass_count);
3297 * regulator_register - register regulator
3298 * @regulator_desc: regulator to register
3299 * @config: runtime configuration for regulator
3301 * Called by regulator drivers to register a regulator.
3302 * Returns 0 on success.
3304 struct regulator_dev *
3305 regulator_register(const struct regulator_desc *regulator_desc,
3306 const struct regulator_config *config)
3308 const struct regulation_constraints *constraints = NULL;
3309 const struct regulator_init_data *init_data;
3310 static atomic_t regulator_no = ATOMIC_INIT(0);
3311 struct regulator_dev *rdev;
3314 const char *supply = NULL;
3316 if (regulator_desc == NULL || config == NULL)
3317 return ERR_PTR(-EINVAL);
3322 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3323 return ERR_PTR(-EINVAL);
3325 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3326 regulator_desc->type != REGULATOR_CURRENT)
3327 return ERR_PTR(-EINVAL);
3329 /* Only one of each should be implemented */
3330 WARN_ON(regulator_desc->ops->get_voltage &&
3331 regulator_desc->ops->get_voltage_sel);
3332 WARN_ON(regulator_desc->ops->set_voltage &&
3333 regulator_desc->ops->set_voltage_sel);
3335 /* If we're using selectors we must implement list_voltage. */
3336 if (regulator_desc->ops->get_voltage_sel &&
3337 !regulator_desc->ops->list_voltage) {
3338 return ERR_PTR(-EINVAL);
3340 if (regulator_desc->ops->set_voltage_sel &&
3341 !regulator_desc->ops->list_voltage) {
3342 return ERR_PTR(-EINVAL);
3345 init_data = config->init_data;
3347 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3349 return ERR_PTR(-ENOMEM);
3351 mutex_lock(®ulator_list_mutex);
3353 mutex_init(&rdev->mutex);
3354 rdev->reg_data = config->driver_data;
3355 rdev->owner = regulator_desc->owner;
3356 rdev->desc = regulator_desc;
3358 rdev->regmap = config->regmap;
3359 else if (dev_get_regmap(dev, NULL))
3360 rdev->regmap = dev_get_regmap(dev, NULL);
3361 else if (dev->parent)
3362 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3363 INIT_LIST_HEAD(&rdev->consumer_list);
3364 INIT_LIST_HEAD(&rdev->list);
3365 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3366 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3368 /* preform any regulator specific init */
3369 if (init_data && init_data->regulator_init) {
3370 ret = init_data->regulator_init(rdev->reg_data);
3375 /* register with sysfs */
3376 rdev->dev.class = ®ulator_class;
3377 rdev->dev.of_node = config->of_node;
3378 rdev->dev.parent = dev;
3379 dev_set_name(&rdev->dev, "regulator.%d",
3380 atomic_inc_return(®ulator_no) - 1);
3381 ret = device_register(&rdev->dev);
3383 put_device(&rdev->dev);
3387 dev_set_drvdata(&rdev->dev, rdev);
3389 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3390 ret = gpio_request_one(config->ena_gpio,
3391 GPIOF_DIR_OUT | config->ena_gpio_flags,
3392 rdev_get_name(rdev));
3394 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3395 config->ena_gpio, ret);
3399 rdev->ena_gpio = config->ena_gpio;
3400 rdev->ena_gpio_invert = config->ena_gpio_invert;
3402 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3403 rdev->ena_gpio_state = 1;
3405 if (rdev->ena_gpio_invert)
3406 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3409 /* set regulator constraints */
3411 constraints = &init_data->constraints;
3413 ret = set_machine_constraints(rdev, constraints);
3417 /* add attributes supported by this regulator */
3418 ret = add_regulator_attributes(rdev);
3422 if (init_data && init_data->supply_regulator)
3423 supply = init_data->supply_regulator;
3424 else if (regulator_desc->supply_name)
3425 supply = regulator_desc->supply_name;
3428 struct regulator_dev *r;
3430 r = regulator_dev_lookup(dev, supply, &ret);
3433 dev_err(dev, "Failed to find supply %s\n", supply);
3434 ret = -EPROBE_DEFER;
3438 ret = set_supply(rdev, r);
3442 /* Enable supply if rail is enabled */
3443 if (_regulator_is_enabled(rdev)) {
3444 ret = regulator_enable(rdev->supply);
3450 /* add consumers devices */
3452 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3453 ret = set_consumer_device_supply(rdev,
3454 init_data->consumer_supplies[i].dev_name,
3455 init_data->consumer_supplies[i].supply);
3457 dev_err(dev, "Failed to set supply %s\n",
3458 init_data->consumer_supplies[i].supply);
3459 goto unset_supplies;
3464 list_add(&rdev->list, ®ulator_list);
3466 rdev_init_debugfs(rdev);
3468 mutex_unlock(®ulator_list_mutex);
3472 unset_regulator_supplies(rdev);
3476 _regulator_put(rdev->supply);
3478 gpio_free(rdev->ena_gpio);
3479 kfree(rdev->constraints);
3481 device_unregister(&rdev->dev);
3482 /* device core frees rdev */
3483 rdev = ERR_PTR(ret);
3488 rdev = ERR_PTR(ret);
3491 EXPORT_SYMBOL_GPL(regulator_register);
3494 * regulator_unregister - unregister regulator
3495 * @rdev: regulator to unregister
3497 * Called by regulator drivers to unregister a regulator.
3499 void regulator_unregister(struct regulator_dev *rdev)
3505 regulator_put(rdev->supply);
3506 mutex_lock(®ulator_list_mutex);
3507 debugfs_remove_recursive(rdev->debugfs);
3508 flush_work(&rdev->disable_work.work);
3509 WARN_ON(rdev->open_count);
3510 unset_regulator_supplies(rdev);
3511 list_del(&rdev->list);
3512 kfree(rdev->constraints);
3514 gpio_free(rdev->ena_gpio);
3515 device_unregister(&rdev->dev);
3516 mutex_unlock(®ulator_list_mutex);
3518 EXPORT_SYMBOL_GPL(regulator_unregister);
3521 * regulator_suspend_prepare - prepare regulators for system wide suspend
3522 * @state: system suspend state
3524 * Configure each regulator with it's suspend operating parameters for state.
3525 * This will usually be called by machine suspend code prior to supending.
3527 int regulator_suspend_prepare(suspend_state_t state)
3529 struct regulator_dev *rdev;
3532 /* ON is handled by regulator active state */
3533 if (state == PM_SUSPEND_ON)
3536 mutex_lock(®ulator_list_mutex);
3537 list_for_each_entry(rdev, ®ulator_list, list) {
3539 mutex_lock(&rdev->mutex);
3540 ret = suspend_prepare(rdev, state);
3541 mutex_unlock(&rdev->mutex);
3544 rdev_err(rdev, "failed to prepare\n");
3549 mutex_unlock(®ulator_list_mutex);
3552 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3555 * regulator_suspend_finish - resume regulators from system wide suspend
3557 * Turn on regulators that might be turned off by regulator_suspend_prepare
3558 * and that should be turned on according to the regulators properties.
3560 int regulator_suspend_finish(void)
3562 struct regulator_dev *rdev;
3565 mutex_lock(®ulator_list_mutex);
3566 list_for_each_entry(rdev, ®ulator_list, list) {
3567 struct regulator_ops *ops = rdev->desc->ops;
3569 mutex_lock(&rdev->mutex);
3570 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3572 error = ops->enable(rdev);
3576 if (!has_full_constraints)
3580 if (!_regulator_is_enabled(rdev))
3583 error = ops->disable(rdev);
3588 mutex_unlock(&rdev->mutex);
3590 mutex_unlock(®ulator_list_mutex);
3593 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3596 * regulator_has_full_constraints - the system has fully specified constraints
3598 * Calling this function will cause the regulator API to disable all
3599 * regulators which have a zero use count and don't have an always_on
3600 * constraint in a late_initcall.
3602 * The intention is that this will become the default behaviour in a
3603 * future kernel release so users are encouraged to use this facility
3606 void regulator_has_full_constraints(void)
3608 has_full_constraints = 1;
3610 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3613 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3615 * Calling this function will cause the regulator API to provide a
3616 * dummy regulator to consumers if no physical regulator is found,
3617 * allowing most consumers to proceed as though a regulator were
3618 * configured. This allows systems such as those with software
3619 * controllable regulators for the CPU core only to be brought up more
3622 void regulator_use_dummy_regulator(void)
3624 board_wants_dummy_regulator = true;
3626 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3629 * rdev_get_drvdata - get rdev regulator driver data
3632 * Get rdev regulator driver private data. This call can be used in the
3633 * regulator driver context.
3635 void *rdev_get_drvdata(struct regulator_dev *rdev)
3637 return rdev->reg_data;
3639 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3642 * regulator_get_drvdata - get regulator driver data
3643 * @regulator: regulator
3645 * Get regulator driver private data. This call can be used in the consumer
3646 * driver context when non API regulator specific functions need to be called.
3648 void *regulator_get_drvdata(struct regulator *regulator)
3650 return regulator->rdev->reg_data;
3652 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3655 * regulator_set_drvdata - set regulator driver data
3656 * @regulator: regulator
3659 void regulator_set_drvdata(struct regulator *regulator, void *data)
3661 regulator->rdev->reg_data = data;
3663 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3666 * regulator_get_id - get regulator ID
3669 int rdev_get_id(struct regulator_dev *rdev)
3671 return rdev->desc->id;
3673 EXPORT_SYMBOL_GPL(rdev_get_id);
3675 struct device *rdev_get_dev(struct regulator_dev *rdev)
3679 EXPORT_SYMBOL_GPL(rdev_get_dev);
3681 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3683 return reg_init_data->driver_data;
3685 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3687 #ifdef CONFIG_DEBUG_FS
3688 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3689 size_t count, loff_t *ppos)
3691 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3692 ssize_t len, ret = 0;
3693 struct regulator_map *map;
3698 list_for_each_entry(map, ®ulator_map_list, list) {
3699 len = snprintf(buf + ret, PAGE_SIZE - ret,
3701 rdev_get_name(map->regulator), map->dev_name,
3705 if (ret > PAGE_SIZE) {
3711 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3719 static const struct file_operations supply_map_fops = {
3720 #ifdef CONFIG_DEBUG_FS
3721 .read = supply_map_read_file,
3722 .llseek = default_llseek,
3726 static int __init regulator_init(void)
3730 ret = class_register(®ulator_class);
3732 debugfs_root = debugfs_create_dir("regulator", NULL);
3734 pr_warn("regulator: Failed to create debugfs directory\n");
3736 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3739 regulator_dummy_init();
3744 /* init early to allow our consumers to complete system booting */
3745 core_initcall(regulator_init);
3747 static int __init regulator_init_complete(void)
3749 struct regulator_dev *rdev;
3750 struct regulator_ops *ops;
3751 struct regulation_constraints *c;
3755 * Since DT doesn't provide an idiomatic mechanism for
3756 * enabling full constraints and since it's much more natural
3757 * with DT to provide them just assume that a DT enabled
3758 * system has full constraints.
3760 if (of_have_populated_dt())
3761 has_full_constraints = true;
3763 mutex_lock(®ulator_list_mutex);
3765 /* If we have a full configuration then disable any regulators
3766 * which are not in use or always_on. This will become the
3767 * default behaviour in the future.
3769 list_for_each_entry(rdev, ®ulator_list, list) {
3770 ops = rdev->desc->ops;
3771 c = rdev->constraints;
3773 if (!ops->disable || (c && c->always_on))
3776 mutex_lock(&rdev->mutex);
3778 if (rdev->use_count)
3781 /* If we can't read the status assume it's on. */
3782 if (ops->is_enabled)
3783 enabled = ops->is_enabled(rdev);
3790 if (has_full_constraints) {
3791 /* We log since this may kill the system if it
3793 rdev_info(rdev, "disabling\n");
3794 ret = ops->disable(rdev);
3796 rdev_err(rdev, "couldn't disable: %d\n", ret);
3799 /* The intention is that in future we will
3800 * assume that full constraints are provided
3801 * so warn even if we aren't going to do
3804 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3808 mutex_unlock(&rdev->mutex);
3811 mutex_unlock(®ulator_list_mutex);
3815 late_initcall(regulator_init_complete);