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>
27 #include <linux/regulator/of_regulator.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/module.h>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/regulator.h>
38 #define rdev_crit(rdev, fmt, ...) \
39 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_err(rdev, fmt, ...) \
41 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_warn(rdev, fmt, ...) \
43 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_info(rdev, fmt, ...) \
45 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_dbg(rdev, fmt, ...) \
47 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 static DEFINE_MUTEX(regulator_list_mutex);
50 static LIST_HEAD(regulator_list);
51 static LIST_HEAD(regulator_map_list);
52 static bool has_full_constraints;
53 static bool board_wants_dummy_regulator;
55 #ifdef CONFIG_DEBUG_FS
56 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;
83 struct device_attribute dev_attr;
84 struct regulator_dev *rdev;
85 #ifdef CONFIG_DEBUG_FS
86 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;
113 /* gets the regulator for a given consumer device */
114 static struct regulator *get_device_regulator(struct device *dev)
116 struct regulator *regulator = NULL;
117 struct regulator_dev *rdev;
119 mutex_lock(®ulator_list_mutex);
120 list_for_each_entry(rdev, ®ulator_list, list) {
121 mutex_lock(&rdev->mutex);
122 list_for_each_entry(regulator, &rdev->consumer_list, list) {
123 if (regulator->dev == dev) {
124 mutex_unlock(&rdev->mutex);
125 mutex_unlock(®ulator_list_mutex);
129 mutex_unlock(&rdev->mutex);
131 mutex_unlock(®ulator_list_mutex);
136 * of_get_regulator - get a regulator device node based on supply name
137 * @dev: Device pointer for the consumer (of regulator) device
138 * @supply: regulator supply name
140 * Extract the regulator device node corresponding to the supply name.
141 * retruns the device node corresponding to the regulator if found, else
144 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
146 struct device_node *regnode = NULL;
147 char prop_name[32]; /* 32 is max size of property name */
149 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
151 snprintf(prop_name, 32, "%s-supply", supply);
152 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
155 dev_warn(dev, "%s property in node %s references invalid phandle",
156 prop_name, dev->of_node->full_name);
162 /* Platform voltage constraint check */
163 static int regulator_check_voltage(struct regulator_dev *rdev,
164 int *min_uV, int *max_uV)
166 BUG_ON(*min_uV > *max_uV);
168 if (!rdev->constraints) {
169 rdev_err(rdev, "no constraints\n");
172 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
173 rdev_err(rdev, "operation not allowed\n");
177 if (*max_uV > rdev->constraints->max_uV)
178 *max_uV = rdev->constraints->max_uV;
179 if (*min_uV < rdev->constraints->min_uV)
180 *min_uV = rdev->constraints->min_uV;
182 if (*min_uV > *max_uV) {
183 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
191 /* Make sure we select a voltage that suits the needs of all
192 * regulator consumers
194 static int regulator_check_consumers(struct regulator_dev *rdev,
195 int *min_uV, int *max_uV)
197 struct regulator *regulator;
199 list_for_each_entry(regulator, &rdev->consumer_list, list) {
201 * Assume consumers that didn't say anything are OK
202 * with anything in the constraint range.
204 if (!regulator->min_uV && !regulator->max_uV)
207 if (*max_uV > regulator->max_uV)
208 *max_uV = regulator->max_uV;
209 if (*min_uV < regulator->min_uV)
210 *min_uV = regulator->min_uV;
213 if (*min_uV > *max_uV)
219 /* current constraint check */
220 static int regulator_check_current_limit(struct regulator_dev *rdev,
221 int *min_uA, int *max_uA)
223 BUG_ON(*min_uA > *max_uA);
225 if (!rdev->constraints) {
226 rdev_err(rdev, "no constraints\n");
229 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
230 rdev_err(rdev, "operation not allowed\n");
234 if (*max_uA > rdev->constraints->max_uA)
235 *max_uA = rdev->constraints->max_uA;
236 if (*min_uA < rdev->constraints->min_uA)
237 *min_uA = rdev->constraints->min_uA;
239 if (*min_uA > *max_uA) {
240 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
248 /* operating mode constraint check */
249 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
252 case REGULATOR_MODE_FAST:
253 case REGULATOR_MODE_NORMAL:
254 case REGULATOR_MODE_IDLE:
255 case REGULATOR_MODE_STANDBY:
258 rdev_err(rdev, "invalid mode %x specified\n", *mode);
262 if (!rdev->constraints) {
263 rdev_err(rdev, "no constraints\n");
266 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
267 rdev_err(rdev, "operation not allowed\n");
271 /* The modes are bitmasks, the most power hungry modes having
272 * the lowest values. If the requested mode isn't supported
273 * try higher modes. */
275 if (rdev->constraints->valid_modes_mask & *mode)
283 /* dynamic regulator mode switching constraint check */
284 static int regulator_check_drms(struct regulator_dev *rdev)
286 if (!rdev->constraints) {
287 rdev_err(rdev, "no constraints\n");
290 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
291 rdev_err(rdev, "operation not allowed\n");
297 static ssize_t device_requested_uA_show(struct device *dev,
298 struct device_attribute *attr, char *buf)
300 struct regulator *regulator;
302 regulator = get_device_regulator(dev);
303 if (regulator == NULL)
306 return sprintf(buf, "%d\n", regulator->uA_load);
309 static ssize_t regulator_uV_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 struct regulator_dev *rdev = dev_get_drvdata(dev);
315 mutex_lock(&rdev->mutex);
316 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
317 mutex_unlock(&rdev->mutex);
321 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
323 static ssize_t regulator_uA_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 struct regulator_dev *rdev = dev_get_drvdata(dev);
328 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
330 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
332 static ssize_t regulator_name_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
335 struct regulator_dev *rdev = dev_get_drvdata(dev);
337 return sprintf(buf, "%s\n", rdev_get_name(rdev));
340 static ssize_t regulator_print_opmode(char *buf, int mode)
343 case REGULATOR_MODE_FAST:
344 return sprintf(buf, "fast\n");
345 case REGULATOR_MODE_NORMAL:
346 return sprintf(buf, "normal\n");
347 case REGULATOR_MODE_IDLE:
348 return sprintf(buf, "idle\n");
349 case REGULATOR_MODE_STANDBY:
350 return sprintf(buf, "standby\n");
352 return sprintf(buf, "unknown\n");
355 static ssize_t regulator_opmode_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
358 struct regulator_dev *rdev = dev_get_drvdata(dev);
360 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
362 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
364 static ssize_t regulator_print_state(char *buf, int state)
367 return sprintf(buf, "enabled\n");
369 return sprintf(buf, "disabled\n");
371 return sprintf(buf, "unknown\n");
374 static ssize_t regulator_state_show(struct device *dev,
375 struct device_attribute *attr, char *buf)
377 struct regulator_dev *rdev = dev_get_drvdata(dev);
380 mutex_lock(&rdev->mutex);
381 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
382 mutex_unlock(&rdev->mutex);
386 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
388 static ssize_t regulator_status_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct regulator_dev *rdev = dev_get_drvdata(dev);
395 status = rdev->desc->ops->get_status(rdev);
400 case REGULATOR_STATUS_OFF:
403 case REGULATOR_STATUS_ON:
406 case REGULATOR_STATUS_ERROR:
409 case REGULATOR_STATUS_FAST:
412 case REGULATOR_STATUS_NORMAL:
415 case REGULATOR_STATUS_IDLE:
418 case REGULATOR_STATUS_STANDBY:
425 return sprintf(buf, "%s\n", label);
427 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
429 static ssize_t regulator_min_uA_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 if (!rdev->constraints)
435 return sprintf(buf, "constraint not defined\n");
437 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
439 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
441 static ssize_t regulator_max_uA_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
444 struct regulator_dev *rdev = dev_get_drvdata(dev);
446 if (!rdev->constraints)
447 return sprintf(buf, "constraint not defined\n");
449 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
451 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
453 static ssize_t regulator_min_uV_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
456 struct regulator_dev *rdev = dev_get_drvdata(dev);
458 if (!rdev->constraints)
459 return sprintf(buf, "constraint not defined\n");
461 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
463 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
465 static ssize_t regulator_max_uV_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct regulator_dev *rdev = dev_get_drvdata(dev);
470 if (!rdev->constraints)
471 return sprintf(buf, "constraint not defined\n");
473 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
475 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
477 static ssize_t regulator_total_uA_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
480 struct regulator_dev *rdev = dev_get_drvdata(dev);
481 struct regulator *regulator;
484 mutex_lock(&rdev->mutex);
485 list_for_each_entry(regulator, &rdev->consumer_list, list)
486 uA += regulator->uA_load;
487 mutex_unlock(&rdev->mutex);
488 return sprintf(buf, "%d\n", uA);
490 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
492 static ssize_t regulator_num_users_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
495 struct regulator_dev *rdev = dev_get_drvdata(dev);
496 return sprintf(buf, "%d\n", rdev->use_count);
499 static ssize_t regulator_type_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
502 struct regulator_dev *rdev = dev_get_drvdata(dev);
504 switch (rdev->desc->type) {
505 case REGULATOR_VOLTAGE:
506 return sprintf(buf, "voltage\n");
507 case REGULATOR_CURRENT:
508 return sprintf(buf, "current\n");
510 return sprintf(buf, "unknown\n");
513 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
516 struct regulator_dev *rdev = dev_get_drvdata(dev);
518 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
520 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
521 regulator_suspend_mem_uV_show, NULL);
523 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
526 struct regulator_dev *rdev = dev_get_drvdata(dev);
528 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
530 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
531 regulator_suspend_disk_uV_show, NULL);
533 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
536 struct regulator_dev *rdev = dev_get_drvdata(dev);
538 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
540 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
541 regulator_suspend_standby_uV_show, NULL);
543 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
546 struct regulator_dev *rdev = dev_get_drvdata(dev);
548 return regulator_print_opmode(buf,
549 rdev->constraints->state_mem.mode);
551 static DEVICE_ATTR(suspend_mem_mode, 0444,
552 regulator_suspend_mem_mode_show, NULL);
554 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
557 struct regulator_dev *rdev = dev_get_drvdata(dev);
559 return regulator_print_opmode(buf,
560 rdev->constraints->state_disk.mode);
562 static DEVICE_ATTR(suspend_disk_mode, 0444,
563 regulator_suspend_disk_mode_show, NULL);
565 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
568 struct regulator_dev *rdev = dev_get_drvdata(dev);
570 return regulator_print_opmode(buf,
571 rdev->constraints->state_standby.mode);
573 static DEVICE_ATTR(suspend_standby_mode, 0444,
574 regulator_suspend_standby_mode_show, NULL);
576 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
577 struct device_attribute *attr, char *buf)
579 struct regulator_dev *rdev = dev_get_drvdata(dev);
581 return regulator_print_state(buf,
582 rdev->constraints->state_mem.enabled);
584 static DEVICE_ATTR(suspend_mem_state, 0444,
585 regulator_suspend_mem_state_show, NULL);
587 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
590 struct regulator_dev *rdev = dev_get_drvdata(dev);
592 return regulator_print_state(buf,
593 rdev->constraints->state_disk.enabled);
595 static DEVICE_ATTR(suspend_disk_state, 0444,
596 regulator_suspend_disk_state_show, NULL);
598 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
601 struct regulator_dev *rdev = dev_get_drvdata(dev);
603 return regulator_print_state(buf,
604 rdev->constraints->state_standby.enabled);
606 static DEVICE_ATTR(suspend_standby_state, 0444,
607 regulator_suspend_standby_state_show, NULL);
611 * These are the only attributes are present for all regulators.
612 * Other attributes are a function of regulator functionality.
614 static struct device_attribute regulator_dev_attrs[] = {
615 __ATTR(name, 0444, regulator_name_show, NULL),
616 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
617 __ATTR(type, 0444, regulator_type_show, NULL),
621 static void regulator_dev_release(struct device *dev)
623 struct regulator_dev *rdev = dev_get_drvdata(dev);
627 static struct class regulator_class = {
629 .dev_release = regulator_dev_release,
630 .dev_attrs = regulator_dev_attrs,
633 /* Calculate the new optimum regulator operating mode based on the new total
634 * consumer load. All locks held by caller */
635 static void drms_uA_update(struct regulator_dev *rdev)
637 struct regulator *sibling;
638 int current_uA = 0, output_uV, input_uV, err;
641 err = regulator_check_drms(rdev);
642 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
643 (!rdev->desc->ops->get_voltage &&
644 !rdev->desc->ops->get_voltage_sel) ||
645 !rdev->desc->ops->set_mode)
648 /* get output voltage */
649 output_uV = _regulator_get_voltage(rdev);
653 /* get input voltage */
656 input_uV = _regulator_get_voltage(rdev);
658 input_uV = rdev->constraints->input_uV;
662 /* calc total requested load */
663 list_for_each_entry(sibling, &rdev->consumer_list, list)
664 current_uA += sibling->uA_load;
666 /* now get the optimum mode for our new total regulator load */
667 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
668 output_uV, current_uA);
670 /* check the new mode is allowed */
671 err = regulator_mode_constrain(rdev, &mode);
673 rdev->desc->ops->set_mode(rdev, mode);
676 static int suspend_set_state(struct regulator_dev *rdev,
677 struct regulator_state *rstate)
682 can_set_state = rdev->desc->ops->set_suspend_enable &&
683 rdev->desc->ops->set_suspend_disable;
685 /* If we have no suspend mode configration don't set anything;
686 * only warn if the driver actually makes the suspend mode
689 if (!rstate->enabled && !rstate->disabled) {
691 rdev_warn(rdev, "No configuration\n");
695 if (rstate->enabled && rstate->disabled) {
696 rdev_err(rdev, "invalid configuration\n");
700 if (!can_set_state) {
701 rdev_err(rdev, "no way to set suspend state\n");
706 ret = rdev->desc->ops->set_suspend_enable(rdev);
708 ret = rdev->desc->ops->set_suspend_disable(rdev);
710 rdev_err(rdev, "failed to enabled/disable\n");
714 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
715 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
717 rdev_err(rdev, "failed to set voltage\n");
722 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
723 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
725 rdev_err(rdev, "failed to set mode\n");
732 /* locks held by caller */
733 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
735 if (!rdev->constraints)
739 case PM_SUSPEND_STANDBY:
740 return suspend_set_state(rdev,
741 &rdev->constraints->state_standby);
743 return suspend_set_state(rdev,
744 &rdev->constraints->state_mem);
746 return suspend_set_state(rdev,
747 &rdev->constraints->state_disk);
753 static void print_constraints(struct regulator_dev *rdev)
755 struct regulation_constraints *constraints = rdev->constraints;
760 if (constraints->min_uV && constraints->max_uV) {
761 if (constraints->min_uV == constraints->max_uV)
762 count += sprintf(buf + count, "%d mV ",
763 constraints->min_uV / 1000);
765 count += sprintf(buf + count, "%d <--> %d mV ",
766 constraints->min_uV / 1000,
767 constraints->max_uV / 1000);
770 if (!constraints->min_uV ||
771 constraints->min_uV != constraints->max_uV) {
772 ret = _regulator_get_voltage(rdev);
774 count += sprintf(buf + count, "at %d mV ", ret / 1000);
777 if (constraints->uV_offset)
778 count += sprintf(buf, "%dmV offset ",
779 constraints->uV_offset / 1000);
781 if (constraints->min_uA && constraints->max_uA) {
782 if (constraints->min_uA == constraints->max_uA)
783 count += sprintf(buf + count, "%d mA ",
784 constraints->min_uA / 1000);
786 count += sprintf(buf + count, "%d <--> %d mA ",
787 constraints->min_uA / 1000,
788 constraints->max_uA / 1000);
791 if (!constraints->min_uA ||
792 constraints->min_uA != constraints->max_uA) {
793 ret = _regulator_get_current_limit(rdev);
795 count += sprintf(buf + count, "at %d mA ", ret / 1000);
798 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
799 count += sprintf(buf + count, "fast ");
800 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
801 count += sprintf(buf + count, "normal ");
802 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
803 count += sprintf(buf + count, "idle ");
804 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
805 count += sprintf(buf + count, "standby");
807 rdev_info(rdev, "%s\n", buf);
809 if ((constraints->min_uV != constraints->max_uV) &&
810 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
812 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
815 static int machine_constraints_voltage(struct regulator_dev *rdev,
816 struct regulation_constraints *constraints)
818 struct regulator_ops *ops = rdev->desc->ops;
821 /* do we need to apply the constraint voltage */
822 if (rdev->constraints->apply_uV &&
823 rdev->constraints->min_uV == rdev->constraints->max_uV) {
824 ret = _regulator_do_set_voltage(rdev,
825 rdev->constraints->min_uV,
826 rdev->constraints->max_uV);
828 rdev_err(rdev, "failed to apply %duV constraint\n",
829 rdev->constraints->min_uV);
834 /* constrain machine-level voltage specs to fit
835 * the actual range supported by this regulator.
837 if (ops->list_voltage && rdev->desc->n_voltages) {
838 int count = rdev->desc->n_voltages;
840 int min_uV = INT_MAX;
841 int max_uV = INT_MIN;
842 int cmin = constraints->min_uV;
843 int cmax = constraints->max_uV;
845 /* it's safe to autoconfigure fixed-voltage supplies
846 and the constraints are used by list_voltage. */
847 if (count == 1 && !cmin) {
850 constraints->min_uV = cmin;
851 constraints->max_uV = cmax;
854 /* voltage constraints are optional */
855 if ((cmin == 0) && (cmax == 0))
858 /* else require explicit machine-level constraints */
859 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
860 rdev_err(rdev, "invalid voltage constraints\n");
864 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
865 for (i = 0; i < count; i++) {
868 value = ops->list_voltage(rdev, i);
872 /* maybe adjust [min_uV..max_uV] */
873 if (value >= cmin && value < min_uV)
875 if (value <= cmax && value > max_uV)
879 /* final: [min_uV..max_uV] valid iff constraints valid */
880 if (max_uV < min_uV) {
881 rdev_err(rdev, "unsupportable voltage constraints\n");
885 /* use regulator's subset of machine constraints */
886 if (constraints->min_uV < min_uV) {
887 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
888 constraints->min_uV, min_uV);
889 constraints->min_uV = min_uV;
891 if (constraints->max_uV > max_uV) {
892 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
893 constraints->max_uV, max_uV);
894 constraints->max_uV = max_uV;
902 * set_machine_constraints - sets regulator constraints
903 * @rdev: regulator source
904 * @constraints: constraints to apply
906 * Allows platform initialisation code to define and constrain
907 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
908 * Constraints *must* be set by platform code in order for some
909 * regulator operations to proceed i.e. set_voltage, set_current_limit,
912 static int set_machine_constraints(struct regulator_dev *rdev,
913 const struct regulation_constraints *constraints)
916 struct regulator_ops *ops = rdev->desc->ops;
919 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
922 rdev->constraints = kzalloc(sizeof(*constraints),
924 if (!rdev->constraints)
927 ret = machine_constraints_voltage(rdev, rdev->constraints);
931 /* do we need to setup our suspend state */
932 if (rdev->constraints->initial_state) {
933 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
935 rdev_err(rdev, "failed to set suspend state\n");
940 if (rdev->constraints->initial_mode) {
941 if (!ops->set_mode) {
942 rdev_err(rdev, "no set_mode operation\n");
947 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
949 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
954 /* If the constraints say the regulator should be on at this point
955 * and we have control then make sure it is enabled.
957 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
959 ret = ops->enable(rdev);
961 rdev_err(rdev, "failed to enable\n");
966 print_constraints(rdev);
969 kfree(rdev->constraints);
970 rdev->constraints = NULL;
975 * set_supply - set regulator supply regulator
976 * @rdev: regulator name
977 * @supply_rdev: supply regulator name
979 * Called by platform initialisation code to set the supply regulator for this
980 * regulator. This ensures that a regulators supply will also be enabled by the
981 * core if it's child is enabled.
983 static int set_supply(struct regulator_dev *rdev,
984 struct regulator_dev *supply_rdev)
988 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
990 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
991 if (rdev->supply == NULL) {
1000 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1001 * @rdev: regulator source
1002 * @consumer_dev: device the supply applies to
1003 * @consumer_dev_name: dev_name() string for device supply applies to
1004 * @supply: symbolic name for supply
1006 * Allows platform initialisation code to map physical regulator
1007 * sources to symbolic names for supplies for use by devices. Devices
1008 * should use these symbolic names to request regulators, avoiding the
1009 * need to provide board-specific regulator names as platform data.
1011 * Only one of consumer_dev and consumer_dev_name may be specified.
1013 static int set_consumer_device_supply(struct regulator_dev *rdev,
1014 struct device *consumer_dev, const char *consumer_dev_name,
1017 struct regulator_map *node;
1020 if (consumer_dev && consumer_dev_name)
1023 if (!consumer_dev_name && consumer_dev)
1024 consumer_dev_name = dev_name(consumer_dev);
1029 if (consumer_dev_name != NULL)
1034 list_for_each_entry(node, ®ulator_map_list, list) {
1035 if (node->dev_name && consumer_dev_name) {
1036 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1038 } else if (node->dev_name || consumer_dev_name) {
1042 if (strcmp(node->supply, supply) != 0)
1045 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
1046 dev_name(&node->regulator->dev),
1047 node->regulator->desc->name,
1049 dev_name(&rdev->dev), rdev_get_name(rdev));
1053 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1057 node->regulator = rdev;
1058 node->supply = supply;
1061 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1062 if (node->dev_name == NULL) {
1068 list_add(&node->list, ®ulator_map_list);
1072 static void unset_regulator_supplies(struct regulator_dev *rdev)
1074 struct regulator_map *node, *n;
1076 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1077 if (rdev == node->regulator) {
1078 list_del(&node->list);
1079 kfree(node->dev_name);
1085 #define REG_STR_SIZE 64
1087 static struct regulator *create_regulator(struct regulator_dev *rdev,
1089 const char *supply_name)
1091 struct regulator *regulator;
1092 char buf[REG_STR_SIZE];
1095 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1096 if (regulator == NULL)
1099 mutex_lock(&rdev->mutex);
1100 regulator->rdev = rdev;
1101 list_add(®ulator->list, &rdev->consumer_list);
1104 /* create a 'requested_microamps_name' sysfs entry */
1105 size = scnprintf(buf, REG_STR_SIZE,
1106 "microamps_requested_%s-%s",
1107 dev_name(dev), supply_name);
1108 if (size >= REG_STR_SIZE)
1111 regulator->dev = dev;
1112 sysfs_attr_init(®ulator->dev_attr.attr);
1113 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1114 if (regulator->dev_attr.attr.name == NULL)
1117 regulator->dev_attr.attr.mode = 0444;
1118 regulator->dev_attr.show = device_requested_uA_show;
1119 err = device_create_file(dev, ®ulator->dev_attr);
1121 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1125 /* also add a link to the device sysfs entry */
1126 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1127 dev->kobj.name, supply_name);
1128 if (size >= REG_STR_SIZE)
1131 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1132 if (regulator->supply_name == NULL)
1135 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1138 rdev_warn(rdev, "could not add device link %s err %d\n",
1139 dev->kobj.name, err);
1143 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1144 if (regulator->supply_name == NULL)
1148 #ifdef CONFIG_DEBUG_FS
1149 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1151 if (IS_ERR_OR_NULL(regulator->debugfs)) {
1152 rdev_warn(rdev, "Failed to create debugfs directory\n");
1153 regulator->debugfs = NULL;
1155 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1156 ®ulator->uA_load);
1157 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1158 ®ulator->min_uV);
1159 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1160 ®ulator->max_uV);
1164 mutex_unlock(&rdev->mutex);
1167 kfree(regulator->supply_name);
1169 device_remove_file(regulator->dev, ®ulator->dev_attr);
1171 kfree(regulator->dev_attr.attr.name);
1173 list_del(®ulator->list);
1175 mutex_unlock(&rdev->mutex);
1179 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1181 if (!rdev->desc->ops->enable_time)
1183 return rdev->desc->ops->enable_time(rdev);
1186 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1189 struct regulator_dev *r;
1190 struct device_node *node;
1192 /* first do a dt based lookup */
1193 if (dev && dev->of_node) {
1194 node = of_get_regulator(dev, supply);
1196 list_for_each_entry(r, ®ulator_list, list)
1197 if (r->dev.parent &&
1198 node == r->dev.of_node)
1202 /* if not found, try doing it non-dt way */
1203 list_for_each_entry(r, ®ulator_list, list)
1204 if (strcmp(rdev_get_name(r), supply) == 0)
1210 /* Internal regulator request function */
1211 static struct regulator *_regulator_get(struct device *dev, const char *id,
1214 struct regulator_dev *rdev;
1215 struct regulator_map *map;
1216 struct regulator *regulator = ERR_PTR(-ENODEV);
1217 const char *devname = NULL;
1221 pr_err("get() with no identifier\n");
1226 devname = dev_name(dev);
1228 mutex_lock(®ulator_list_mutex);
1230 rdev = regulator_dev_lookup(dev, id);
1234 list_for_each_entry(map, ®ulator_map_list, list) {
1235 /* If the mapping has a device set up it must match */
1236 if (map->dev_name &&
1237 (!devname || strcmp(map->dev_name, devname)))
1240 if (strcmp(map->supply, id) == 0) {
1241 rdev = map->regulator;
1246 if (board_wants_dummy_regulator) {
1247 rdev = dummy_regulator_rdev;
1251 #ifdef CONFIG_REGULATOR_DUMMY
1253 devname = "deviceless";
1255 /* If the board didn't flag that it was fully constrained then
1256 * substitute in a dummy regulator so consumers can continue.
1258 if (!has_full_constraints) {
1259 pr_warn("%s supply %s not found, using dummy regulator\n",
1261 rdev = dummy_regulator_rdev;
1266 mutex_unlock(®ulator_list_mutex);
1270 if (rdev->exclusive) {
1271 regulator = ERR_PTR(-EPERM);
1275 if (exclusive && rdev->open_count) {
1276 regulator = ERR_PTR(-EBUSY);
1280 if (!try_module_get(rdev->owner))
1283 regulator = create_regulator(rdev, dev, id);
1284 if (regulator == NULL) {
1285 regulator = ERR_PTR(-ENOMEM);
1286 module_put(rdev->owner);
1292 rdev->exclusive = 1;
1294 ret = _regulator_is_enabled(rdev);
1296 rdev->use_count = 1;
1298 rdev->use_count = 0;
1302 mutex_unlock(®ulator_list_mutex);
1308 * regulator_get - lookup and obtain a reference to a regulator.
1309 * @dev: device for regulator "consumer"
1310 * @id: Supply name or regulator ID.
1312 * Returns a struct regulator corresponding to the regulator producer,
1313 * or IS_ERR() condition containing errno.
1315 * Use of supply names configured via regulator_set_device_supply() is
1316 * strongly encouraged. It is recommended that the supply name used
1317 * should match the name used for the supply and/or the relevant
1318 * device pins in the datasheet.
1320 struct regulator *regulator_get(struct device *dev, const char *id)
1322 return _regulator_get(dev, id, 0);
1324 EXPORT_SYMBOL_GPL(regulator_get);
1327 * regulator_get_exclusive - obtain exclusive access to a regulator.
1328 * @dev: device for regulator "consumer"
1329 * @id: Supply name or regulator ID.
1331 * Returns a struct regulator corresponding to the regulator producer,
1332 * or IS_ERR() condition containing errno. Other consumers will be
1333 * unable to obtain this reference is held and the use count for the
1334 * regulator will be initialised to reflect the current state of the
1337 * This is intended for use by consumers which cannot tolerate shared
1338 * use of the regulator such as those which need to force the
1339 * regulator off for correct operation of the hardware they are
1342 * Use of supply names configured via regulator_set_device_supply() is
1343 * strongly encouraged. It is recommended that the supply name used
1344 * should match the name used for the supply and/or the relevant
1345 * device pins in the datasheet.
1347 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1349 return _regulator_get(dev, id, 1);
1351 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1354 * regulator_put - "free" the regulator source
1355 * @regulator: regulator source
1357 * Note: drivers must ensure that all regulator_enable calls made on this
1358 * regulator source are balanced by regulator_disable calls prior to calling
1361 void regulator_put(struct regulator *regulator)
1363 struct regulator_dev *rdev;
1365 if (regulator == NULL || IS_ERR(regulator))
1368 mutex_lock(®ulator_list_mutex);
1369 rdev = regulator->rdev;
1371 #ifdef CONFIG_DEBUG_FS
1372 debugfs_remove_recursive(regulator->debugfs);
1375 /* remove any sysfs entries */
1376 if (regulator->dev) {
1377 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1378 device_remove_file(regulator->dev, ®ulator->dev_attr);
1379 kfree(regulator->dev_attr.attr.name);
1381 kfree(regulator->supply_name);
1382 list_del(®ulator->list);
1386 rdev->exclusive = 0;
1388 module_put(rdev->owner);
1389 mutex_unlock(®ulator_list_mutex);
1391 EXPORT_SYMBOL_GPL(regulator_put);
1393 static int _regulator_can_change_status(struct regulator_dev *rdev)
1395 if (!rdev->constraints)
1398 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1404 /* locks held by regulator_enable() */
1405 static int _regulator_enable(struct regulator_dev *rdev)
1409 /* check voltage and requested load before enabling */
1410 if (rdev->constraints &&
1411 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1412 drms_uA_update(rdev);
1414 if (rdev->use_count == 0) {
1415 /* The regulator may on if it's not switchable or left on */
1416 ret = _regulator_is_enabled(rdev);
1417 if (ret == -EINVAL || ret == 0) {
1418 if (!_regulator_can_change_status(rdev))
1421 if (!rdev->desc->ops->enable)
1424 /* Query before enabling in case configuration
1426 ret = _regulator_get_enable_time(rdev);
1430 rdev_warn(rdev, "enable_time() failed: %d\n",
1435 trace_regulator_enable(rdev_get_name(rdev));
1437 /* Allow the regulator to ramp; it would be useful
1438 * to extend this for bulk operations so that the
1439 * regulators can ramp together. */
1440 ret = rdev->desc->ops->enable(rdev);
1444 trace_regulator_enable_delay(rdev_get_name(rdev));
1446 if (delay >= 1000) {
1447 mdelay(delay / 1000);
1448 udelay(delay % 1000);
1453 trace_regulator_enable_complete(rdev_get_name(rdev));
1455 } else if (ret < 0) {
1456 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1459 /* Fallthrough on positive return values - already enabled */
1468 * regulator_enable - enable regulator output
1469 * @regulator: regulator source
1471 * Request that the regulator be enabled with the regulator output at
1472 * the predefined voltage or current value. Calls to regulator_enable()
1473 * must be balanced with calls to regulator_disable().
1475 * NOTE: the output value can be set by other drivers, boot loader or may be
1476 * hardwired in the regulator.
1478 int regulator_enable(struct regulator *regulator)
1480 struct regulator_dev *rdev = regulator->rdev;
1484 ret = regulator_enable(rdev->supply);
1489 mutex_lock(&rdev->mutex);
1490 ret = _regulator_enable(rdev);
1491 mutex_unlock(&rdev->mutex);
1493 if (ret != 0 && rdev->supply)
1494 regulator_disable(rdev->supply);
1498 EXPORT_SYMBOL_GPL(regulator_enable);
1500 /* locks held by regulator_disable() */
1501 static int _regulator_disable(struct regulator_dev *rdev)
1505 if (WARN(rdev->use_count <= 0,
1506 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1509 /* are we the last user and permitted to disable ? */
1510 if (rdev->use_count == 1 &&
1511 (rdev->constraints && !rdev->constraints->always_on)) {
1513 /* we are last user */
1514 if (_regulator_can_change_status(rdev) &&
1515 rdev->desc->ops->disable) {
1516 trace_regulator_disable(rdev_get_name(rdev));
1518 ret = rdev->desc->ops->disable(rdev);
1520 rdev_err(rdev, "failed to disable\n");
1524 trace_regulator_disable_complete(rdev_get_name(rdev));
1526 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1530 rdev->use_count = 0;
1531 } else if (rdev->use_count > 1) {
1533 if (rdev->constraints &&
1534 (rdev->constraints->valid_ops_mask &
1535 REGULATOR_CHANGE_DRMS))
1536 drms_uA_update(rdev);
1545 * regulator_disable - disable regulator output
1546 * @regulator: regulator source
1548 * Disable the regulator output voltage or current. Calls to
1549 * regulator_enable() must be balanced with calls to
1550 * regulator_disable().
1552 * NOTE: this will only disable the regulator output if no other consumer
1553 * devices have it enabled, the regulator device supports disabling and
1554 * machine constraints permit this operation.
1556 int regulator_disable(struct regulator *regulator)
1558 struct regulator_dev *rdev = regulator->rdev;
1561 mutex_lock(&rdev->mutex);
1562 ret = _regulator_disable(rdev);
1563 mutex_unlock(&rdev->mutex);
1565 if (ret == 0 && rdev->supply)
1566 regulator_disable(rdev->supply);
1570 EXPORT_SYMBOL_GPL(regulator_disable);
1572 /* locks held by regulator_force_disable() */
1573 static int _regulator_force_disable(struct regulator_dev *rdev)
1578 if (rdev->desc->ops->disable) {
1579 /* ah well, who wants to live forever... */
1580 ret = rdev->desc->ops->disable(rdev);
1582 rdev_err(rdev, "failed to force disable\n");
1585 /* notify other consumers that power has been forced off */
1586 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1587 REGULATOR_EVENT_DISABLE, NULL);
1594 * regulator_force_disable - force disable regulator output
1595 * @regulator: regulator source
1597 * Forcibly disable the regulator output voltage or current.
1598 * NOTE: this *will* disable the regulator output even if other consumer
1599 * devices have it enabled. This should be used for situations when device
1600 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1602 int regulator_force_disable(struct regulator *regulator)
1604 struct regulator_dev *rdev = regulator->rdev;
1607 mutex_lock(&rdev->mutex);
1608 regulator->uA_load = 0;
1609 ret = _regulator_force_disable(regulator->rdev);
1610 mutex_unlock(&rdev->mutex);
1613 while (rdev->open_count--)
1614 regulator_disable(rdev->supply);
1618 EXPORT_SYMBOL_GPL(regulator_force_disable);
1620 static void regulator_disable_work(struct work_struct *work)
1622 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1626 mutex_lock(&rdev->mutex);
1628 BUG_ON(!rdev->deferred_disables);
1630 count = rdev->deferred_disables;
1631 rdev->deferred_disables = 0;
1633 for (i = 0; i < count; i++) {
1634 ret = _regulator_disable(rdev);
1636 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1639 mutex_unlock(&rdev->mutex);
1642 for (i = 0; i < count; i++) {
1643 ret = regulator_disable(rdev->supply);
1646 "Supply disable failed: %d\n", ret);
1653 * regulator_disable_deferred - disable regulator output with delay
1654 * @regulator: regulator source
1655 * @ms: miliseconds until the regulator is disabled
1657 * Execute regulator_disable() on the regulator after a delay. This
1658 * is intended for use with devices that require some time to quiesce.
1660 * NOTE: this will only disable the regulator output if no other consumer
1661 * devices have it enabled, the regulator device supports disabling and
1662 * machine constraints permit this operation.
1664 int regulator_disable_deferred(struct regulator *regulator, int ms)
1666 struct regulator_dev *rdev = regulator->rdev;
1669 mutex_lock(&rdev->mutex);
1670 rdev->deferred_disables++;
1671 mutex_unlock(&rdev->mutex);
1673 ret = schedule_delayed_work(&rdev->disable_work,
1674 msecs_to_jiffies(ms));
1680 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1682 static int _regulator_is_enabled(struct regulator_dev *rdev)
1684 /* If we don't know then assume that the regulator is always on */
1685 if (!rdev->desc->ops->is_enabled)
1688 return rdev->desc->ops->is_enabled(rdev);
1692 * regulator_is_enabled - is the regulator output enabled
1693 * @regulator: regulator source
1695 * Returns positive if the regulator driver backing the source/client
1696 * has requested that the device be enabled, zero if it hasn't, else a
1697 * negative errno code.
1699 * Note that the device backing this regulator handle can have multiple
1700 * users, so it might be enabled even if regulator_enable() was never
1701 * called for this particular source.
1703 int regulator_is_enabled(struct regulator *regulator)
1707 mutex_lock(®ulator->rdev->mutex);
1708 ret = _regulator_is_enabled(regulator->rdev);
1709 mutex_unlock(®ulator->rdev->mutex);
1713 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1716 * regulator_count_voltages - count regulator_list_voltage() selectors
1717 * @regulator: regulator source
1719 * Returns number of selectors, or negative errno. Selectors are
1720 * numbered starting at zero, and typically correspond to bitfields
1721 * in hardware registers.
1723 int regulator_count_voltages(struct regulator *regulator)
1725 struct regulator_dev *rdev = regulator->rdev;
1727 return rdev->desc->n_voltages ? : -EINVAL;
1729 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1732 * regulator_list_voltage - enumerate supported voltages
1733 * @regulator: regulator source
1734 * @selector: identify voltage to list
1735 * Context: can sleep
1737 * Returns a voltage that can be passed to @regulator_set_voltage(),
1738 * zero if this selector code can't be used on this system, or a
1741 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1743 struct regulator_dev *rdev = regulator->rdev;
1744 struct regulator_ops *ops = rdev->desc->ops;
1747 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1750 mutex_lock(&rdev->mutex);
1751 ret = ops->list_voltage(rdev, selector);
1752 mutex_unlock(&rdev->mutex);
1755 if (ret < rdev->constraints->min_uV)
1757 else if (ret > rdev->constraints->max_uV)
1763 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1766 * regulator_is_supported_voltage - check if a voltage range can be supported
1768 * @regulator: Regulator to check.
1769 * @min_uV: Minimum required voltage in uV.
1770 * @max_uV: Maximum required voltage in uV.
1772 * Returns a boolean or a negative error code.
1774 int regulator_is_supported_voltage(struct regulator *regulator,
1775 int min_uV, int max_uV)
1777 int i, voltages, ret;
1779 ret = regulator_count_voltages(regulator);
1784 for (i = 0; i < voltages; i++) {
1785 ret = regulator_list_voltage(regulator, i);
1787 if (ret >= min_uV && ret <= max_uV)
1793 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
1795 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1796 int min_uV, int max_uV)
1800 unsigned int selector;
1802 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1804 min_uV += rdev->constraints->uV_offset;
1805 max_uV += rdev->constraints->uV_offset;
1807 if (rdev->desc->ops->set_voltage) {
1808 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1811 if (rdev->desc->ops->list_voltage)
1812 selector = rdev->desc->ops->list_voltage(rdev,
1816 } else if (rdev->desc->ops->set_voltage_sel) {
1817 int best_val = INT_MAX;
1822 /* Find the smallest voltage that falls within the specified
1825 for (i = 0; i < rdev->desc->n_voltages; i++) {
1826 ret = rdev->desc->ops->list_voltage(rdev, i);
1830 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1837 * If we can't obtain the old selector there is not enough
1838 * info to call set_voltage_time_sel().
1840 if (rdev->desc->ops->set_voltage_time_sel &&
1841 rdev->desc->ops->get_voltage_sel) {
1842 unsigned int old_selector = 0;
1844 ret = rdev->desc->ops->get_voltage_sel(rdev);
1848 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1849 old_selector, selector);
1852 if (best_val != INT_MAX) {
1853 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1854 selector = best_val;
1862 /* Insert any necessary delays */
1863 if (delay >= 1000) {
1864 mdelay(delay / 1000);
1865 udelay(delay % 1000);
1871 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1874 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1880 * regulator_set_voltage - set regulator output voltage
1881 * @regulator: regulator source
1882 * @min_uV: Minimum required voltage in uV
1883 * @max_uV: Maximum acceptable voltage in uV
1885 * Sets a voltage regulator to the desired output voltage. This can be set
1886 * during any regulator state. IOW, regulator can be disabled or enabled.
1888 * If the regulator is enabled then the voltage will change to the new value
1889 * immediately otherwise if the regulator is disabled the regulator will
1890 * output at the new voltage when enabled.
1892 * NOTE: If the regulator is shared between several devices then the lowest
1893 * request voltage that meets the system constraints will be used.
1894 * Regulator system constraints must be set for this regulator before
1895 * calling this function otherwise this call will fail.
1897 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1899 struct regulator_dev *rdev = regulator->rdev;
1902 mutex_lock(&rdev->mutex);
1904 /* If we're setting the same range as last time the change
1905 * should be a noop (some cpufreq implementations use the same
1906 * voltage for multiple frequencies, for example).
1908 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1912 if (!rdev->desc->ops->set_voltage &&
1913 !rdev->desc->ops->set_voltage_sel) {
1918 /* constraints check */
1919 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1922 regulator->min_uV = min_uV;
1923 regulator->max_uV = max_uV;
1925 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1929 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1932 mutex_unlock(&rdev->mutex);
1935 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1938 * regulator_set_voltage_time - get raise/fall time
1939 * @regulator: regulator source
1940 * @old_uV: starting voltage in microvolts
1941 * @new_uV: target voltage in microvolts
1943 * Provided with the starting and ending voltage, this function attempts to
1944 * calculate the time in microseconds required to rise or fall to this new
1947 int regulator_set_voltage_time(struct regulator *regulator,
1948 int old_uV, int new_uV)
1950 struct regulator_dev *rdev = regulator->rdev;
1951 struct regulator_ops *ops = rdev->desc->ops;
1957 /* Currently requires operations to do this */
1958 if (!ops->list_voltage || !ops->set_voltage_time_sel
1959 || !rdev->desc->n_voltages)
1962 for (i = 0; i < rdev->desc->n_voltages; i++) {
1963 /* We only look for exact voltage matches here */
1964 voltage = regulator_list_voltage(regulator, i);
1969 if (voltage == old_uV)
1971 if (voltage == new_uV)
1975 if (old_sel < 0 || new_sel < 0)
1978 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1980 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1983 * regulator_sync_voltage - re-apply last regulator output voltage
1984 * @regulator: regulator source
1986 * Re-apply the last configured voltage. This is intended to be used
1987 * where some external control source the consumer is cooperating with
1988 * has caused the configured voltage to change.
1990 int regulator_sync_voltage(struct regulator *regulator)
1992 struct regulator_dev *rdev = regulator->rdev;
1993 int ret, min_uV, max_uV;
1995 mutex_lock(&rdev->mutex);
1997 if (!rdev->desc->ops->set_voltage &&
1998 !rdev->desc->ops->set_voltage_sel) {
2003 /* This is only going to work if we've had a voltage configured. */
2004 if (!regulator->min_uV && !regulator->max_uV) {
2009 min_uV = regulator->min_uV;
2010 max_uV = regulator->max_uV;
2012 /* This should be a paranoia check... */
2013 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2017 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2021 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2024 mutex_unlock(&rdev->mutex);
2027 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2029 static int _regulator_get_voltage(struct regulator_dev *rdev)
2033 if (rdev->desc->ops->get_voltage_sel) {
2034 sel = rdev->desc->ops->get_voltage_sel(rdev);
2037 ret = rdev->desc->ops->list_voltage(rdev, sel);
2038 } else if (rdev->desc->ops->get_voltage) {
2039 ret = rdev->desc->ops->get_voltage(rdev);
2046 return ret - rdev->constraints->uV_offset;
2050 * regulator_get_voltage - get regulator output voltage
2051 * @regulator: regulator source
2053 * This returns the current regulator voltage in uV.
2055 * NOTE: If the regulator is disabled it will return the voltage value. This
2056 * function should not be used to determine regulator state.
2058 int regulator_get_voltage(struct regulator *regulator)
2062 mutex_lock(®ulator->rdev->mutex);
2064 ret = _regulator_get_voltage(regulator->rdev);
2066 mutex_unlock(®ulator->rdev->mutex);
2070 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2073 * regulator_set_current_limit - set regulator output current limit
2074 * @regulator: regulator source
2075 * @min_uA: Minimuum supported current in uA
2076 * @max_uA: Maximum supported current in uA
2078 * Sets current sink to the desired output current. This can be set during
2079 * any regulator state. IOW, regulator can be disabled or enabled.
2081 * If the regulator is enabled then the current will change to the new value
2082 * immediately otherwise if the regulator is disabled the regulator will
2083 * output at the new current when enabled.
2085 * NOTE: Regulator system constraints must be set for this regulator before
2086 * calling this function otherwise this call will fail.
2088 int regulator_set_current_limit(struct regulator *regulator,
2089 int min_uA, int max_uA)
2091 struct regulator_dev *rdev = regulator->rdev;
2094 mutex_lock(&rdev->mutex);
2097 if (!rdev->desc->ops->set_current_limit) {
2102 /* constraints check */
2103 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2107 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2109 mutex_unlock(&rdev->mutex);
2112 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2114 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2118 mutex_lock(&rdev->mutex);
2121 if (!rdev->desc->ops->get_current_limit) {
2126 ret = rdev->desc->ops->get_current_limit(rdev);
2128 mutex_unlock(&rdev->mutex);
2133 * regulator_get_current_limit - get regulator output current
2134 * @regulator: regulator source
2136 * This returns the current supplied by the specified current sink in uA.
2138 * NOTE: If the regulator is disabled it will return the current value. This
2139 * function should not be used to determine regulator state.
2141 int regulator_get_current_limit(struct regulator *regulator)
2143 return _regulator_get_current_limit(regulator->rdev);
2145 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2148 * regulator_set_mode - set regulator operating mode
2149 * @regulator: regulator source
2150 * @mode: operating mode - one of the REGULATOR_MODE constants
2152 * Set regulator operating mode to increase regulator efficiency or improve
2153 * regulation performance.
2155 * NOTE: Regulator system constraints must be set for this regulator before
2156 * calling this function otherwise this call will fail.
2158 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2160 struct regulator_dev *rdev = regulator->rdev;
2162 int regulator_curr_mode;
2164 mutex_lock(&rdev->mutex);
2167 if (!rdev->desc->ops->set_mode) {
2172 /* return if the same mode is requested */
2173 if (rdev->desc->ops->get_mode) {
2174 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2175 if (regulator_curr_mode == mode) {
2181 /* constraints check */
2182 ret = regulator_mode_constrain(rdev, &mode);
2186 ret = rdev->desc->ops->set_mode(rdev, mode);
2188 mutex_unlock(&rdev->mutex);
2191 EXPORT_SYMBOL_GPL(regulator_set_mode);
2193 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2197 mutex_lock(&rdev->mutex);
2200 if (!rdev->desc->ops->get_mode) {
2205 ret = rdev->desc->ops->get_mode(rdev);
2207 mutex_unlock(&rdev->mutex);
2212 * regulator_get_mode - get regulator operating mode
2213 * @regulator: regulator source
2215 * Get the current regulator operating mode.
2217 unsigned int regulator_get_mode(struct regulator *regulator)
2219 return _regulator_get_mode(regulator->rdev);
2221 EXPORT_SYMBOL_GPL(regulator_get_mode);
2224 * regulator_set_optimum_mode - set regulator optimum operating mode
2225 * @regulator: regulator source
2226 * @uA_load: load current
2228 * Notifies the regulator core of a new device load. This is then used by
2229 * DRMS (if enabled by constraints) to set the most efficient regulator
2230 * operating mode for the new regulator loading.
2232 * Consumer devices notify their supply regulator of the maximum power
2233 * they will require (can be taken from device datasheet in the power
2234 * consumption tables) when they change operational status and hence power
2235 * state. Examples of operational state changes that can affect power
2236 * consumption are :-
2238 * o Device is opened / closed.
2239 * o Device I/O is about to begin or has just finished.
2240 * o Device is idling in between work.
2242 * This information is also exported via sysfs to userspace.
2244 * DRMS will sum the total requested load on the regulator and change
2245 * to the most efficient operating mode if platform constraints allow.
2247 * Returns the new regulator mode or error.
2249 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2251 struct regulator_dev *rdev = regulator->rdev;
2252 struct regulator *consumer;
2253 int ret, output_uV, input_uV, total_uA_load = 0;
2256 mutex_lock(&rdev->mutex);
2259 * first check to see if we can set modes at all, otherwise just
2260 * tell the consumer everything is OK.
2262 regulator->uA_load = uA_load;
2263 ret = regulator_check_drms(rdev);
2269 if (!rdev->desc->ops->get_optimum_mode)
2273 * we can actually do this so any errors are indicators of
2274 * potential real failure.
2278 /* get output voltage */
2279 output_uV = _regulator_get_voltage(rdev);
2280 if (output_uV <= 0) {
2281 rdev_err(rdev, "invalid output voltage found\n");
2285 /* get input voltage */
2288 input_uV = regulator_get_voltage(rdev->supply);
2290 input_uV = rdev->constraints->input_uV;
2291 if (input_uV <= 0) {
2292 rdev_err(rdev, "invalid input voltage found\n");
2296 /* calc total requested load for this regulator */
2297 list_for_each_entry(consumer, &rdev->consumer_list, list)
2298 total_uA_load += consumer->uA_load;
2300 mode = rdev->desc->ops->get_optimum_mode(rdev,
2301 input_uV, output_uV,
2303 ret = regulator_mode_constrain(rdev, &mode);
2305 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2306 total_uA_load, input_uV, output_uV);
2310 ret = rdev->desc->ops->set_mode(rdev, mode);
2312 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2317 mutex_unlock(&rdev->mutex);
2320 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2323 * regulator_register_notifier - register regulator event notifier
2324 * @regulator: regulator source
2325 * @nb: notifier block
2327 * Register notifier block to receive regulator events.
2329 int regulator_register_notifier(struct regulator *regulator,
2330 struct notifier_block *nb)
2332 return blocking_notifier_chain_register(®ulator->rdev->notifier,
2335 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2338 * regulator_unregister_notifier - unregister regulator event notifier
2339 * @regulator: regulator source
2340 * @nb: notifier block
2342 * Unregister regulator event notifier block.
2344 int regulator_unregister_notifier(struct regulator *regulator,
2345 struct notifier_block *nb)
2347 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
2350 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2352 /* notify regulator consumers and downstream regulator consumers.
2353 * Note mutex must be held by caller.
2355 static void _notifier_call_chain(struct regulator_dev *rdev,
2356 unsigned long event, void *data)
2358 /* call rdev chain first */
2359 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
2363 * regulator_bulk_get - get multiple regulator consumers
2365 * @dev: Device to supply
2366 * @num_consumers: Number of consumers to register
2367 * @consumers: Configuration of consumers; clients are stored here.
2369 * @return 0 on success, an errno on failure.
2371 * This helper function allows drivers to get several regulator
2372 * consumers in one operation. If any of the regulators cannot be
2373 * acquired then any regulators that were allocated will be freed
2374 * before returning to the caller.
2376 int regulator_bulk_get(struct device *dev, int num_consumers,
2377 struct regulator_bulk_data *consumers)
2382 for (i = 0; i < num_consumers; i++)
2383 consumers[i].consumer = NULL;
2385 for (i = 0; i < num_consumers; i++) {
2386 consumers[i].consumer = regulator_get(dev,
2387 consumers[i].supply);
2388 if (IS_ERR(consumers[i].consumer)) {
2389 ret = PTR_ERR(consumers[i].consumer);
2390 dev_err(dev, "Failed to get supply '%s': %d\n",
2391 consumers[i].supply, ret);
2392 consumers[i].consumer = NULL;
2401 regulator_put(consumers[i].consumer);
2405 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2407 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2409 struct regulator_bulk_data *bulk = data;
2411 bulk->ret = regulator_enable(bulk->consumer);
2415 * regulator_bulk_enable - enable multiple regulator consumers
2417 * @num_consumers: Number of consumers
2418 * @consumers: Consumer data; clients are stored here.
2419 * @return 0 on success, an errno on failure
2421 * This convenience API allows consumers to enable multiple regulator
2422 * clients in a single API call. If any consumers cannot be enabled
2423 * then any others that were enabled will be disabled again prior to
2426 int regulator_bulk_enable(int num_consumers,
2427 struct regulator_bulk_data *consumers)
2429 LIST_HEAD(async_domain);
2433 for (i = 0; i < num_consumers; i++)
2434 async_schedule_domain(regulator_bulk_enable_async,
2435 &consumers[i], &async_domain);
2437 async_synchronize_full_domain(&async_domain);
2439 /* If any consumer failed we need to unwind any that succeeded */
2440 for (i = 0; i < num_consumers; i++) {
2441 if (consumers[i].ret != 0) {
2442 ret = consumers[i].ret;
2450 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2452 regulator_disable(consumers[i].consumer);
2456 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2459 * regulator_bulk_disable - disable multiple regulator consumers
2461 * @num_consumers: Number of consumers
2462 * @consumers: Consumer data; clients are stored here.
2463 * @return 0 on success, an errno on failure
2465 * This convenience API allows consumers to disable multiple regulator
2466 * clients in a single API call. If any consumers cannot be disabled
2467 * then any others that were disabled will be enabled again prior to
2470 int regulator_bulk_disable(int num_consumers,
2471 struct regulator_bulk_data *consumers)
2476 for (i = num_consumers - 1; i >= 0; --i) {
2477 ret = regulator_disable(consumers[i].consumer);
2485 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2486 for (++i; i < num_consumers; ++i)
2487 regulator_enable(consumers[i].consumer);
2491 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2494 * regulator_bulk_force_disable - force disable multiple regulator consumers
2496 * @num_consumers: Number of consumers
2497 * @consumers: Consumer data; clients are stored here.
2498 * @return 0 on success, an errno on failure
2500 * This convenience API allows consumers to forcibly disable multiple regulator
2501 * clients in a single API call.
2502 * NOTE: This should be used for situations when device damage will
2503 * likely occur if the regulators are not disabled (e.g. over temp).
2504 * Although regulator_force_disable function call for some consumers can
2505 * return error numbers, the function is called for all consumers.
2507 int regulator_bulk_force_disable(int num_consumers,
2508 struct regulator_bulk_data *consumers)
2513 for (i = 0; i < num_consumers; i++)
2515 regulator_force_disable(consumers[i].consumer);
2517 for (i = 0; i < num_consumers; i++) {
2518 if (consumers[i].ret != 0) {
2519 ret = consumers[i].ret;
2528 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2531 * regulator_bulk_free - free multiple regulator consumers
2533 * @num_consumers: Number of consumers
2534 * @consumers: Consumer data; clients are stored here.
2536 * This convenience API allows consumers to free multiple regulator
2537 * clients in a single API call.
2539 void regulator_bulk_free(int num_consumers,
2540 struct regulator_bulk_data *consumers)
2544 for (i = 0; i < num_consumers; i++) {
2545 regulator_put(consumers[i].consumer);
2546 consumers[i].consumer = NULL;
2549 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2552 * regulator_notifier_call_chain - call regulator event notifier
2553 * @rdev: regulator source
2554 * @event: notifier block
2555 * @data: callback-specific data.
2557 * Called by regulator drivers to notify clients a regulator event has
2558 * occurred. We also notify regulator clients downstream.
2559 * Note lock must be held by caller.
2561 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2562 unsigned long event, void *data)
2564 _notifier_call_chain(rdev, event, data);
2568 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2571 * regulator_mode_to_status - convert a regulator mode into a status
2573 * @mode: Mode to convert
2575 * Convert a regulator mode into a status.
2577 int regulator_mode_to_status(unsigned int mode)
2580 case REGULATOR_MODE_FAST:
2581 return REGULATOR_STATUS_FAST;
2582 case REGULATOR_MODE_NORMAL:
2583 return REGULATOR_STATUS_NORMAL;
2584 case REGULATOR_MODE_IDLE:
2585 return REGULATOR_STATUS_IDLE;
2586 case REGULATOR_STATUS_STANDBY:
2587 return REGULATOR_STATUS_STANDBY;
2592 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2595 * To avoid cluttering sysfs (and memory) with useless state, only
2596 * create attributes that can be meaningfully displayed.
2598 static int add_regulator_attributes(struct regulator_dev *rdev)
2600 struct device *dev = &rdev->dev;
2601 struct regulator_ops *ops = rdev->desc->ops;
2604 /* some attributes need specific methods to be displayed */
2605 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2606 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
2607 status = device_create_file(dev, &dev_attr_microvolts);
2611 if (ops->get_current_limit) {
2612 status = device_create_file(dev, &dev_attr_microamps);
2616 if (ops->get_mode) {
2617 status = device_create_file(dev, &dev_attr_opmode);
2621 if (ops->is_enabled) {
2622 status = device_create_file(dev, &dev_attr_state);
2626 if (ops->get_status) {
2627 status = device_create_file(dev, &dev_attr_status);
2632 /* some attributes are type-specific */
2633 if (rdev->desc->type == REGULATOR_CURRENT) {
2634 status = device_create_file(dev, &dev_attr_requested_microamps);
2639 /* all the other attributes exist to support constraints;
2640 * don't show them if there are no constraints, or if the
2641 * relevant supporting methods are missing.
2643 if (!rdev->constraints)
2646 /* constraints need specific supporting methods */
2647 if (ops->set_voltage || ops->set_voltage_sel) {
2648 status = device_create_file(dev, &dev_attr_min_microvolts);
2651 status = device_create_file(dev, &dev_attr_max_microvolts);
2655 if (ops->set_current_limit) {
2656 status = device_create_file(dev, &dev_attr_min_microamps);
2659 status = device_create_file(dev, &dev_attr_max_microamps);
2664 /* suspend mode constraints need multiple supporting methods */
2665 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2668 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2671 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2674 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2678 if (ops->set_suspend_voltage) {
2679 status = device_create_file(dev,
2680 &dev_attr_suspend_standby_microvolts);
2683 status = device_create_file(dev,
2684 &dev_attr_suspend_mem_microvolts);
2687 status = device_create_file(dev,
2688 &dev_attr_suspend_disk_microvolts);
2693 if (ops->set_suspend_mode) {
2694 status = device_create_file(dev,
2695 &dev_attr_suspend_standby_mode);
2698 status = device_create_file(dev,
2699 &dev_attr_suspend_mem_mode);
2702 status = device_create_file(dev,
2703 &dev_attr_suspend_disk_mode);
2711 static void rdev_init_debugfs(struct regulator_dev *rdev)
2713 #ifdef CONFIG_DEBUG_FS
2714 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2715 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2716 rdev_warn(rdev, "Failed to create debugfs directory\n");
2717 rdev->debugfs = NULL;
2721 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2723 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2729 * regulator_register - register regulator
2730 * @regulator_desc: regulator to register
2731 * @dev: struct device for the regulator
2732 * @init_data: platform provided init data, passed through by driver
2733 * @driver_data: private regulator data
2735 * Called by regulator drivers to register a regulator.
2736 * Returns 0 on success.
2738 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2739 struct device *dev, const struct regulator_init_data *init_data,
2740 void *driver_data, struct device_node *of_node)
2742 const struct regulation_constraints *constraints = NULL;
2743 static atomic_t regulator_no = ATOMIC_INIT(0);
2744 struct regulator_dev *rdev;
2746 const char *supply = NULL;
2748 if (regulator_desc == NULL)
2749 return ERR_PTR(-EINVAL);
2751 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2752 return ERR_PTR(-EINVAL);
2754 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2755 regulator_desc->type != REGULATOR_CURRENT)
2756 return ERR_PTR(-EINVAL);
2758 /* Only one of each should be implemented */
2759 WARN_ON(regulator_desc->ops->get_voltage &&
2760 regulator_desc->ops->get_voltage_sel);
2761 WARN_ON(regulator_desc->ops->set_voltage &&
2762 regulator_desc->ops->set_voltage_sel);
2764 /* If we're using selectors we must implement list_voltage. */
2765 if (regulator_desc->ops->get_voltage_sel &&
2766 !regulator_desc->ops->list_voltage) {
2767 return ERR_PTR(-EINVAL);
2769 if (regulator_desc->ops->set_voltage_sel &&
2770 !regulator_desc->ops->list_voltage) {
2771 return ERR_PTR(-EINVAL);
2774 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2776 return ERR_PTR(-ENOMEM);
2778 mutex_lock(®ulator_list_mutex);
2780 mutex_init(&rdev->mutex);
2781 rdev->reg_data = driver_data;
2782 rdev->owner = regulator_desc->owner;
2783 rdev->desc = regulator_desc;
2784 INIT_LIST_HEAD(&rdev->consumer_list);
2785 INIT_LIST_HEAD(&rdev->list);
2786 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2787 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
2789 /* preform any regulator specific init */
2790 if (init_data && init_data->regulator_init) {
2791 ret = init_data->regulator_init(rdev->reg_data);
2796 /* register with sysfs */
2797 rdev->dev.class = ®ulator_class;
2798 rdev->dev.of_node = of_node;
2799 rdev->dev.parent = dev;
2800 dev_set_name(&rdev->dev, "regulator.%d",
2801 atomic_inc_return(®ulator_no) - 1);
2802 ret = device_register(&rdev->dev);
2804 put_device(&rdev->dev);
2808 dev_set_drvdata(&rdev->dev, rdev);
2810 /* set regulator constraints */
2812 constraints = &init_data->constraints;
2814 ret = set_machine_constraints(rdev, constraints);
2818 /* add attributes supported by this regulator */
2819 ret = add_regulator_attributes(rdev);
2823 if (init_data && init_data->supply_regulator)
2824 supply = init_data->supply_regulator;
2825 else if (regulator_desc->supply_name)
2826 supply = regulator_desc->supply_name;
2829 struct regulator_dev *r;
2831 r = regulator_dev_lookup(dev, supply);
2834 dev_err(dev, "Failed to find supply %s\n", supply);
2839 ret = set_supply(rdev, r);
2843 /* Enable supply if rail is enabled */
2844 if (rdev->desc->ops->is_enabled &&
2845 rdev->desc->ops->is_enabled(rdev)) {
2846 ret = regulator_enable(rdev->supply);
2852 /* add consumers devices */
2854 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2855 ret = set_consumer_device_supply(rdev,
2856 init_data->consumer_supplies[i].dev,
2857 init_data->consumer_supplies[i].dev_name,
2858 init_data->consumer_supplies[i].supply);
2860 dev_err(dev, "Failed to set supply %s\n",
2861 init_data->consumer_supplies[i].supply);
2862 goto unset_supplies;
2867 list_add(&rdev->list, ®ulator_list);
2869 rdev_init_debugfs(rdev);
2871 mutex_unlock(®ulator_list_mutex);
2875 unset_regulator_supplies(rdev);
2878 kfree(rdev->constraints);
2879 device_unregister(&rdev->dev);
2880 /* device core frees rdev */
2881 rdev = ERR_PTR(ret);
2886 rdev = ERR_PTR(ret);
2889 EXPORT_SYMBOL_GPL(regulator_register);
2892 * regulator_unregister - unregister regulator
2893 * @rdev: regulator to unregister
2895 * Called by regulator drivers to unregister a regulator.
2897 void regulator_unregister(struct regulator_dev *rdev)
2902 mutex_lock(®ulator_list_mutex);
2903 #ifdef CONFIG_DEBUG_FS
2904 debugfs_remove_recursive(rdev->debugfs);
2906 flush_work_sync(&rdev->disable_work.work);
2907 WARN_ON(rdev->open_count);
2908 unset_regulator_supplies(rdev);
2909 list_del(&rdev->list);
2911 regulator_put(rdev->supply);
2912 kfree(rdev->constraints);
2913 device_unregister(&rdev->dev);
2914 mutex_unlock(®ulator_list_mutex);
2916 EXPORT_SYMBOL_GPL(regulator_unregister);
2919 * regulator_suspend_prepare - prepare regulators for system wide suspend
2920 * @state: system suspend state
2922 * Configure each regulator with it's suspend operating parameters for state.
2923 * This will usually be called by machine suspend code prior to supending.
2925 int regulator_suspend_prepare(suspend_state_t state)
2927 struct regulator_dev *rdev;
2930 /* ON is handled by regulator active state */
2931 if (state == PM_SUSPEND_ON)
2934 mutex_lock(®ulator_list_mutex);
2935 list_for_each_entry(rdev, ®ulator_list, list) {
2937 mutex_lock(&rdev->mutex);
2938 ret = suspend_prepare(rdev, state);
2939 mutex_unlock(&rdev->mutex);
2942 rdev_err(rdev, "failed to prepare\n");
2947 mutex_unlock(®ulator_list_mutex);
2950 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2953 * regulator_suspend_finish - resume regulators from system wide suspend
2955 * Turn on regulators that might be turned off by regulator_suspend_prepare
2956 * and that should be turned on according to the regulators properties.
2958 int regulator_suspend_finish(void)
2960 struct regulator_dev *rdev;
2963 mutex_lock(®ulator_list_mutex);
2964 list_for_each_entry(rdev, ®ulator_list, list) {
2965 struct regulator_ops *ops = rdev->desc->ops;
2967 mutex_lock(&rdev->mutex);
2968 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2970 error = ops->enable(rdev);
2974 if (!has_full_constraints)
2978 if (ops->is_enabled && !ops->is_enabled(rdev))
2981 error = ops->disable(rdev);
2986 mutex_unlock(&rdev->mutex);
2988 mutex_unlock(®ulator_list_mutex);
2991 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2994 * regulator_has_full_constraints - the system has fully specified constraints
2996 * Calling this function will cause the regulator API to disable all
2997 * regulators which have a zero use count and don't have an always_on
2998 * constraint in a late_initcall.
3000 * The intention is that this will become the default behaviour in a
3001 * future kernel release so users are encouraged to use this facility
3004 void regulator_has_full_constraints(void)
3006 has_full_constraints = 1;
3008 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3011 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3013 * Calling this function will cause the regulator API to provide a
3014 * dummy regulator to consumers if no physical regulator is found,
3015 * allowing most consumers to proceed as though a regulator were
3016 * configured. This allows systems such as those with software
3017 * controllable regulators for the CPU core only to be brought up more
3020 void regulator_use_dummy_regulator(void)
3022 board_wants_dummy_regulator = true;
3024 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3027 * rdev_get_drvdata - get rdev regulator driver data
3030 * Get rdev regulator driver private data. This call can be used in the
3031 * regulator driver context.
3033 void *rdev_get_drvdata(struct regulator_dev *rdev)
3035 return rdev->reg_data;
3037 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3040 * regulator_get_drvdata - get regulator driver data
3041 * @regulator: regulator
3043 * Get regulator driver private data. This call can be used in the consumer
3044 * driver context when non API regulator specific functions need to be called.
3046 void *regulator_get_drvdata(struct regulator *regulator)
3048 return regulator->rdev->reg_data;
3050 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3053 * regulator_set_drvdata - set regulator driver data
3054 * @regulator: regulator
3057 void regulator_set_drvdata(struct regulator *regulator, void *data)
3059 regulator->rdev->reg_data = data;
3061 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3064 * regulator_get_id - get regulator ID
3067 int rdev_get_id(struct regulator_dev *rdev)
3069 return rdev->desc->id;
3071 EXPORT_SYMBOL_GPL(rdev_get_id);
3073 struct device *rdev_get_dev(struct regulator_dev *rdev)
3077 EXPORT_SYMBOL_GPL(rdev_get_dev);
3079 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3081 return reg_init_data->driver_data;
3083 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3085 #ifdef CONFIG_DEBUG_FS
3086 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3087 size_t count, loff_t *ppos)
3089 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3090 ssize_t len, ret = 0;
3091 struct regulator_map *map;
3096 list_for_each_entry(map, ®ulator_map_list, list) {
3097 len = snprintf(buf + ret, PAGE_SIZE - ret,
3099 rdev_get_name(map->regulator), map->dev_name,
3103 if (ret > PAGE_SIZE) {
3109 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3116 static const struct file_operations supply_map_fops = {
3117 .read = supply_map_read_file,
3118 .llseek = default_llseek,
3122 static int __init regulator_init(void)
3126 ret = class_register(®ulator_class);
3128 #ifdef CONFIG_DEBUG_FS
3129 debugfs_root = debugfs_create_dir("regulator", NULL);
3130 if (IS_ERR(debugfs_root) || !debugfs_root) {
3131 pr_warn("regulator: Failed to create debugfs directory\n");
3132 debugfs_root = NULL;
3135 if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root,
3136 NULL, &supply_map_fops)))
3137 pr_warn("regulator: Failed to create supplies debugfs\n");
3140 regulator_dummy_init();
3145 /* init early to allow our consumers to complete system booting */
3146 core_initcall(regulator_init);
3148 static int __init regulator_init_complete(void)
3150 struct regulator_dev *rdev;
3151 struct regulator_ops *ops;
3152 struct regulation_constraints *c;
3155 mutex_lock(®ulator_list_mutex);
3157 /* If we have a full configuration then disable any regulators
3158 * which are not in use or always_on. This will become the
3159 * default behaviour in the future.
3161 list_for_each_entry(rdev, ®ulator_list, list) {
3162 ops = rdev->desc->ops;
3163 c = rdev->constraints;
3165 if (!ops->disable || (c && c->always_on))
3168 mutex_lock(&rdev->mutex);
3170 if (rdev->use_count)
3173 /* If we can't read the status assume it's on. */
3174 if (ops->is_enabled)
3175 enabled = ops->is_enabled(rdev);
3182 if (has_full_constraints) {
3183 /* We log since this may kill the system if it
3185 rdev_info(rdev, "disabling\n");
3186 ret = ops->disable(rdev);
3188 rdev_err(rdev, "couldn't disable: %d\n", ret);
3191 /* The intention is that in future we will
3192 * assume that full constraints are provided
3193 * so warn even if we aren't going to do
3196 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3200 mutex_unlock(&rdev->mutex);
3203 mutex_unlock(®ulator_list_mutex);
3207 late_initcall(regulator_init_complete);