2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #define pr_fmt(fmt) "%s: " fmt, __func__
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/slab.h>
23 #include <linux/async.h>
24 #include <linux/err.h>
25 #include <linux/mutex.h>
26 #include <linux/suspend.h>
27 #include <linux/delay.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/regulator.h>
34 #include <linux/debugfs.h>
35 #include <linux/seq_file.h>
36 #include <linux/uaccess.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 #ifdef CONFIG_DEBUG_FS
58 static struct dentry *debugfs_root;
62 * struct regulator_map
64 * Used to provide symbolic supply names to devices.
66 struct regulator_map {
67 struct list_head list;
68 const char *dev_name; /* The dev_name() for the consumer */
70 struct regulator_dev *regulator;
76 * One for each consumer device.
80 struct list_head list;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
87 #ifdef CONFIG_DEBUG_FS
88 struct dentry *debugfs;
92 static int _regulator_is_enabled(struct regulator_dev *rdev);
93 static int _regulator_disable(struct regulator_dev *rdev);
94 static int _regulator_get_voltage(struct regulator_dev *rdev);
95 static int _regulator_get_current_limit(struct regulator_dev *rdev);
96 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
97 static void _notifier_call_chain(struct regulator_dev *rdev,
98 unsigned long event, void *data);
99 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
100 int min_uV, int max_uV);
101 static struct regulator *create_regulator(struct regulator_dev *rdev,
103 const char *supply_name);
105 static const char *rdev_get_name(struct regulator_dev *rdev)
107 if (rdev->constraints && rdev->constraints->name)
108 return rdev->constraints->name;
109 else if (rdev->desc->name)
110 return rdev->desc->name;
115 /* gets the regulator for a given consumer device */
116 static struct regulator *get_device_regulator(struct device *dev)
118 struct regulator *regulator = NULL;
119 struct regulator_dev *rdev;
121 mutex_lock(®ulator_list_mutex);
122 list_for_each_entry(rdev, ®ulator_list, list) {
123 mutex_lock(&rdev->mutex);
124 list_for_each_entry(regulator, &rdev->consumer_list, list) {
125 if (regulator->dev == dev) {
126 mutex_unlock(&rdev->mutex);
127 mutex_unlock(®ulator_list_mutex);
131 mutex_unlock(&rdev->mutex);
133 mutex_unlock(®ulator_list_mutex);
137 /* Platform voltage constraint check */
138 static int regulator_check_voltage(struct regulator_dev *rdev,
139 int *min_uV, int *max_uV)
141 BUG_ON(*min_uV > *max_uV);
143 if (!rdev->constraints) {
144 rdev_err(rdev, "no constraints\n");
147 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
148 rdev_err(rdev, "operation not allowed\n");
152 if (*max_uV > rdev->constraints->max_uV)
153 *max_uV = rdev->constraints->max_uV;
154 if (*min_uV < rdev->constraints->min_uV)
155 *min_uV = rdev->constraints->min_uV;
157 if (*min_uV > *max_uV) {
158 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
166 /* Make sure we select a voltage that suits the needs of all
167 * regulator consumers
169 static int regulator_check_consumers(struct regulator_dev *rdev,
170 int *min_uV, int *max_uV)
172 struct regulator *regulator;
174 list_for_each_entry(regulator, &rdev->consumer_list, list) {
176 * Assume consumers that didn't say anything are OK
177 * with anything in the constraint range.
179 if (!regulator->min_uV && !regulator->max_uV)
182 if (*max_uV > regulator->max_uV)
183 *max_uV = regulator->max_uV;
184 if (*min_uV < regulator->min_uV)
185 *min_uV = regulator->min_uV;
188 if (*min_uV > *max_uV)
194 /* current constraint check */
195 static int regulator_check_current_limit(struct regulator_dev *rdev,
196 int *min_uA, int *max_uA)
198 BUG_ON(*min_uA > *max_uA);
200 if (!rdev->constraints) {
201 rdev_err(rdev, "no constraints\n");
204 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
205 rdev_err(rdev, "operation not allowed\n");
209 if (*max_uA > rdev->constraints->max_uA)
210 *max_uA = rdev->constraints->max_uA;
211 if (*min_uA < rdev->constraints->min_uA)
212 *min_uA = rdev->constraints->min_uA;
214 if (*min_uA > *max_uA) {
215 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
223 /* operating mode constraint check */
224 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
227 case REGULATOR_MODE_FAST:
228 case REGULATOR_MODE_NORMAL:
229 case REGULATOR_MODE_IDLE:
230 case REGULATOR_MODE_STANDBY:
233 rdev_err(rdev, "invalid mode %x specified\n", *mode);
237 if (!rdev->constraints) {
238 rdev_err(rdev, "no constraints\n");
241 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
242 rdev_err(rdev, "operation not allowed\n");
246 /* The modes are bitmasks, the most power hungry modes having
247 * the lowest values. If the requested mode isn't supported
248 * try higher modes. */
250 if (rdev->constraints->valid_modes_mask & *mode)
258 /* dynamic regulator mode switching constraint check */
259 static int regulator_check_drms(struct regulator_dev *rdev)
261 if (!rdev->constraints) {
262 rdev_err(rdev, "no constraints\n");
265 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
266 rdev_err(rdev, "operation not allowed\n");
272 static ssize_t device_requested_uA_show(struct device *dev,
273 struct device_attribute *attr, char *buf)
275 struct regulator *regulator;
277 regulator = get_device_regulator(dev);
278 if (regulator == NULL)
281 return sprintf(buf, "%d\n", regulator->uA_load);
284 static ssize_t regulator_uV_show(struct device *dev,
285 struct device_attribute *attr, char *buf)
287 struct regulator_dev *rdev = dev_get_drvdata(dev);
290 mutex_lock(&rdev->mutex);
291 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
292 mutex_unlock(&rdev->mutex);
296 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
298 static ssize_t regulator_uA_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
301 struct regulator_dev *rdev = dev_get_drvdata(dev);
303 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
305 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
307 static ssize_t regulator_name_show(struct device *dev,
308 struct device_attribute *attr, char *buf)
310 struct regulator_dev *rdev = dev_get_drvdata(dev);
312 return sprintf(buf, "%s\n", rdev_get_name(rdev));
315 static ssize_t regulator_print_opmode(char *buf, int mode)
318 case REGULATOR_MODE_FAST:
319 return sprintf(buf, "fast\n");
320 case REGULATOR_MODE_NORMAL:
321 return sprintf(buf, "normal\n");
322 case REGULATOR_MODE_IDLE:
323 return sprintf(buf, "idle\n");
324 case REGULATOR_MODE_STANDBY:
325 return sprintf(buf, "standby\n");
327 return sprintf(buf, "unknown\n");
330 static ssize_t regulator_opmode_show(struct device *dev,
331 struct device_attribute *attr, char *buf)
333 struct regulator_dev *rdev = dev_get_drvdata(dev);
335 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
337 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
339 static ssize_t regulator_print_state(char *buf, int state)
342 return sprintf(buf, "enabled\n");
344 return sprintf(buf, "disabled\n");
346 return sprintf(buf, "unknown\n");
349 static ssize_t regulator_state_show(struct device *dev,
350 struct device_attribute *attr, char *buf)
352 struct regulator_dev *rdev = dev_get_drvdata(dev);
355 mutex_lock(&rdev->mutex);
356 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
357 mutex_unlock(&rdev->mutex);
361 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
363 static ssize_t regulator_status_show(struct device *dev,
364 struct device_attribute *attr, char *buf)
366 struct regulator_dev *rdev = dev_get_drvdata(dev);
370 status = rdev->desc->ops->get_status(rdev);
375 case REGULATOR_STATUS_OFF:
378 case REGULATOR_STATUS_ON:
381 case REGULATOR_STATUS_ERROR:
384 case REGULATOR_STATUS_FAST:
387 case REGULATOR_STATUS_NORMAL:
390 case REGULATOR_STATUS_IDLE:
393 case REGULATOR_STATUS_STANDBY:
400 return sprintf(buf, "%s\n", label);
402 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
404 static ssize_t regulator_min_uA_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 struct regulator_dev *rdev = dev_get_drvdata(dev);
409 if (!rdev->constraints)
410 return sprintf(buf, "constraint not defined\n");
412 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
414 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
416 static ssize_t regulator_max_uA_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
419 struct regulator_dev *rdev = dev_get_drvdata(dev);
421 if (!rdev->constraints)
422 return sprintf(buf, "constraint not defined\n");
424 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
426 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
428 static ssize_t regulator_min_uV_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
431 struct regulator_dev *rdev = dev_get_drvdata(dev);
433 if (!rdev->constraints)
434 return sprintf(buf, "constraint not defined\n");
436 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
438 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
440 static ssize_t regulator_max_uV_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct regulator_dev *rdev = dev_get_drvdata(dev);
445 if (!rdev->constraints)
446 return sprintf(buf, "constraint not defined\n");
448 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
450 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
452 static ssize_t regulator_total_uA_show(struct device *dev,
453 struct device_attribute *attr, char *buf)
455 struct regulator_dev *rdev = dev_get_drvdata(dev);
456 struct regulator *regulator;
459 mutex_lock(&rdev->mutex);
460 list_for_each_entry(regulator, &rdev->consumer_list, list)
461 uA += regulator->uA_load;
462 mutex_unlock(&rdev->mutex);
463 return sprintf(buf, "%d\n", uA);
465 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
467 static ssize_t regulator_num_users_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
470 struct regulator_dev *rdev = dev_get_drvdata(dev);
471 return sprintf(buf, "%d\n", rdev->use_count);
474 static ssize_t regulator_type_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
477 struct regulator_dev *rdev = dev_get_drvdata(dev);
479 switch (rdev->desc->type) {
480 case REGULATOR_VOLTAGE:
481 return sprintf(buf, "voltage\n");
482 case REGULATOR_CURRENT:
483 return sprintf(buf, "current\n");
485 return sprintf(buf, "unknown\n");
488 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
491 struct regulator_dev *rdev = dev_get_drvdata(dev);
493 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
495 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
496 regulator_suspend_mem_uV_show, NULL);
498 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 struct regulator_dev *rdev = dev_get_drvdata(dev);
503 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
505 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
506 regulator_suspend_disk_uV_show, NULL);
508 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
509 struct device_attribute *attr, char *buf)
511 struct regulator_dev *rdev = dev_get_drvdata(dev);
513 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
515 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
516 regulator_suspend_standby_uV_show, NULL);
518 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
519 struct device_attribute *attr, char *buf)
521 struct regulator_dev *rdev = dev_get_drvdata(dev);
523 return regulator_print_opmode(buf,
524 rdev->constraints->state_mem.mode);
526 static DEVICE_ATTR(suspend_mem_mode, 0444,
527 regulator_suspend_mem_mode_show, NULL);
529 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
532 struct regulator_dev *rdev = dev_get_drvdata(dev);
534 return regulator_print_opmode(buf,
535 rdev->constraints->state_disk.mode);
537 static DEVICE_ATTR(suspend_disk_mode, 0444,
538 regulator_suspend_disk_mode_show, NULL);
540 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
543 struct regulator_dev *rdev = dev_get_drvdata(dev);
545 return regulator_print_opmode(buf,
546 rdev->constraints->state_standby.mode);
548 static DEVICE_ATTR(suspend_standby_mode, 0444,
549 regulator_suspend_standby_mode_show, NULL);
551 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
554 struct regulator_dev *rdev = dev_get_drvdata(dev);
556 return regulator_print_state(buf,
557 rdev->constraints->state_mem.enabled);
559 static DEVICE_ATTR(suspend_mem_state, 0444,
560 regulator_suspend_mem_state_show, NULL);
562 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
563 struct device_attribute *attr, char *buf)
565 struct regulator_dev *rdev = dev_get_drvdata(dev);
567 return regulator_print_state(buf,
568 rdev->constraints->state_disk.enabled);
570 static DEVICE_ATTR(suspend_disk_state, 0444,
571 regulator_suspend_disk_state_show, NULL);
573 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
576 struct regulator_dev *rdev = dev_get_drvdata(dev);
578 return regulator_print_state(buf,
579 rdev->constraints->state_standby.enabled);
581 static DEVICE_ATTR(suspend_standby_state, 0444,
582 regulator_suspend_standby_state_show, NULL);
586 * These are the only attributes are present for all regulators.
587 * Other attributes are a function of regulator functionality.
589 static struct device_attribute regulator_dev_attrs[] = {
590 __ATTR(name, 0444, regulator_name_show, NULL),
591 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
592 __ATTR(type, 0444, regulator_type_show, NULL),
596 static void regulator_dev_release(struct device *dev)
598 struct regulator_dev *rdev = dev_get_drvdata(dev);
602 static struct class regulator_class = {
604 .dev_release = regulator_dev_release,
605 .dev_attrs = regulator_dev_attrs,
608 /* Calculate the new optimum regulator operating mode based on the new total
609 * consumer load. All locks held by caller */
610 static void drms_uA_update(struct regulator_dev *rdev)
612 struct regulator *sibling;
613 int current_uA = 0, output_uV, input_uV, err;
616 err = regulator_check_drms(rdev);
617 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
618 (!rdev->desc->ops->get_voltage &&
619 !rdev->desc->ops->get_voltage_sel) ||
620 !rdev->desc->ops->set_mode)
623 /* get output voltage */
624 output_uV = _regulator_get_voltage(rdev);
628 /* get input voltage */
631 input_uV = _regulator_get_voltage(rdev);
633 input_uV = rdev->constraints->input_uV;
637 /* calc total requested load */
638 list_for_each_entry(sibling, &rdev->consumer_list, list)
639 current_uA += sibling->uA_load;
641 /* now get the optimum mode for our new total regulator load */
642 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
643 output_uV, current_uA);
645 /* check the new mode is allowed */
646 err = regulator_mode_constrain(rdev, &mode);
648 rdev->desc->ops->set_mode(rdev, mode);
651 static int suspend_set_state(struct regulator_dev *rdev,
652 struct regulator_state *rstate)
657 can_set_state = rdev->desc->ops->set_suspend_enable &&
658 rdev->desc->ops->set_suspend_disable;
660 /* If we have no suspend mode configration don't set anything;
661 * only warn if the driver actually makes the suspend mode
664 if (!rstate->enabled && !rstate->disabled) {
666 rdev_warn(rdev, "No configuration\n");
670 if (rstate->enabled && rstate->disabled) {
671 rdev_err(rdev, "invalid configuration\n");
675 if (!can_set_state) {
676 rdev_err(rdev, "no way to set suspend state\n");
681 ret = rdev->desc->ops->set_suspend_enable(rdev);
683 ret = rdev->desc->ops->set_suspend_disable(rdev);
685 rdev_err(rdev, "failed to enabled/disable\n");
689 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
690 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
692 rdev_err(rdev, "failed to set voltage\n");
697 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
698 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
700 rdev_err(rdev, "failed to set mode\n");
707 /* locks held by caller */
708 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
710 if (!rdev->constraints)
714 case PM_SUSPEND_STANDBY:
715 return suspend_set_state(rdev,
716 &rdev->constraints->state_standby);
718 return suspend_set_state(rdev,
719 &rdev->constraints->state_mem);
721 return suspend_set_state(rdev,
722 &rdev->constraints->state_disk);
728 static void print_constraints(struct regulator_dev *rdev)
730 struct regulation_constraints *constraints = rdev->constraints;
735 if (constraints->min_uV && constraints->max_uV) {
736 if (constraints->min_uV == constraints->max_uV)
737 count += sprintf(buf + count, "%d mV ",
738 constraints->min_uV / 1000);
740 count += sprintf(buf + count, "%d <--> %d mV ",
741 constraints->min_uV / 1000,
742 constraints->max_uV / 1000);
745 if (!constraints->min_uV ||
746 constraints->min_uV != constraints->max_uV) {
747 ret = _regulator_get_voltage(rdev);
749 count += sprintf(buf + count, "at %d mV ", ret / 1000);
752 if (constraints->uV_offset)
753 count += sprintf(buf, "%dmV offset ",
754 constraints->uV_offset / 1000);
756 if (constraints->min_uA && constraints->max_uA) {
757 if (constraints->min_uA == constraints->max_uA)
758 count += sprintf(buf + count, "%d mA ",
759 constraints->min_uA / 1000);
761 count += sprintf(buf + count, "%d <--> %d mA ",
762 constraints->min_uA / 1000,
763 constraints->max_uA / 1000);
766 if (!constraints->min_uA ||
767 constraints->min_uA != constraints->max_uA) {
768 ret = _regulator_get_current_limit(rdev);
770 count += sprintf(buf + count, "at %d mA ", ret / 1000);
773 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
774 count += sprintf(buf + count, "fast ");
775 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
776 count += sprintf(buf + count, "normal ");
777 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
778 count += sprintf(buf + count, "idle ");
779 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
780 count += sprintf(buf + count, "standby");
782 rdev_info(rdev, "%s\n", buf);
785 static int machine_constraints_voltage(struct regulator_dev *rdev,
786 struct regulation_constraints *constraints)
788 struct regulator_ops *ops = rdev->desc->ops;
791 /* do we need to apply the constraint voltage */
792 if (rdev->constraints->apply_uV &&
793 rdev->constraints->min_uV == rdev->constraints->max_uV) {
794 ret = _regulator_do_set_voltage(rdev,
795 rdev->constraints->min_uV,
796 rdev->constraints->max_uV);
798 rdev_err(rdev, "failed to apply %duV constraint\n",
799 rdev->constraints->min_uV);
804 /* constrain machine-level voltage specs to fit
805 * the actual range supported by this regulator.
807 if (ops->list_voltage && rdev->desc->n_voltages) {
808 int count = rdev->desc->n_voltages;
810 int min_uV = INT_MAX;
811 int max_uV = INT_MIN;
812 int cmin = constraints->min_uV;
813 int cmax = constraints->max_uV;
815 /* it's safe to autoconfigure fixed-voltage supplies
816 and the constraints are used by list_voltage. */
817 if (count == 1 && !cmin) {
820 constraints->min_uV = cmin;
821 constraints->max_uV = cmax;
824 /* voltage constraints are optional */
825 if ((cmin == 0) && (cmax == 0))
828 /* else require explicit machine-level constraints */
829 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
830 rdev_err(rdev, "invalid voltage constraints\n");
834 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
835 for (i = 0; i < count; i++) {
838 value = ops->list_voltage(rdev, i);
842 /* maybe adjust [min_uV..max_uV] */
843 if (value >= cmin && value < min_uV)
845 if (value <= cmax && value > max_uV)
849 /* final: [min_uV..max_uV] valid iff constraints valid */
850 if (max_uV < min_uV) {
851 rdev_err(rdev, "unsupportable voltage constraints\n");
855 /* use regulator's subset of machine constraints */
856 if (constraints->min_uV < min_uV) {
857 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
858 constraints->min_uV, min_uV);
859 constraints->min_uV = min_uV;
861 if (constraints->max_uV > max_uV) {
862 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
863 constraints->max_uV, max_uV);
864 constraints->max_uV = max_uV;
872 * set_machine_constraints - sets regulator constraints
873 * @rdev: regulator source
874 * @constraints: constraints to apply
876 * Allows platform initialisation code to define and constrain
877 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
878 * Constraints *must* be set by platform code in order for some
879 * regulator operations to proceed i.e. set_voltage, set_current_limit,
882 static int set_machine_constraints(struct regulator_dev *rdev,
883 const struct regulation_constraints *constraints)
886 struct regulator_ops *ops = rdev->desc->ops;
888 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
890 if (!rdev->constraints)
893 ret = machine_constraints_voltage(rdev, rdev->constraints);
897 /* do we need to setup our suspend state */
898 if (constraints->initial_state) {
899 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
901 rdev_err(rdev, "failed to set suspend state\n");
906 if (constraints->initial_mode) {
907 if (!ops->set_mode) {
908 rdev_err(rdev, "no set_mode operation\n");
913 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
915 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
920 /* If the constraints say the regulator should be on at this point
921 * and we have control then make sure it is enabled.
923 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
925 ret = ops->enable(rdev);
927 rdev_err(rdev, "failed to enable\n");
932 print_constraints(rdev);
935 kfree(rdev->constraints);
936 rdev->constraints = NULL;
941 * set_supply - set regulator supply regulator
942 * @rdev: regulator name
943 * @supply_rdev: supply regulator name
945 * Called by platform initialisation code to set the supply regulator for this
946 * regulator. This ensures that a regulators supply will also be enabled by the
947 * core if it's child is enabled.
949 static int set_supply(struct regulator_dev *rdev,
950 struct regulator_dev *supply_rdev)
954 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
956 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
957 if (IS_ERR(rdev->supply)) {
958 err = PTR_ERR(rdev->supply);
967 * set_consumer_device_supply - Bind a regulator to a symbolic supply
968 * @rdev: regulator source
969 * @consumer_dev: device the supply applies to
970 * @consumer_dev_name: dev_name() string for device supply applies to
971 * @supply: symbolic name for supply
973 * Allows platform initialisation code to map physical regulator
974 * sources to symbolic names for supplies for use by devices. Devices
975 * should use these symbolic names to request regulators, avoiding the
976 * need to provide board-specific regulator names as platform data.
978 * Only one of consumer_dev and consumer_dev_name may be specified.
980 static int set_consumer_device_supply(struct regulator_dev *rdev,
981 struct device *consumer_dev, const char *consumer_dev_name,
984 struct regulator_map *node;
987 if (consumer_dev && consumer_dev_name)
990 if (!consumer_dev_name && consumer_dev)
991 consumer_dev_name = dev_name(consumer_dev);
996 if (consumer_dev_name != NULL)
1001 list_for_each_entry(node, ®ulator_map_list, list) {
1002 if (node->dev_name && consumer_dev_name) {
1003 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1005 } else if (node->dev_name || consumer_dev_name) {
1009 if (strcmp(node->supply, supply) != 0)
1012 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
1013 dev_name(&node->regulator->dev),
1014 node->regulator->desc->name,
1016 dev_name(&rdev->dev), rdev_get_name(rdev));
1020 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1024 node->regulator = rdev;
1025 node->supply = supply;
1028 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1029 if (node->dev_name == NULL) {
1035 list_add(&node->list, ®ulator_map_list);
1039 static void unset_regulator_supplies(struct regulator_dev *rdev)
1041 struct regulator_map *node, *n;
1043 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1044 if (rdev == node->regulator) {
1045 list_del(&node->list);
1046 kfree(node->dev_name);
1052 #define REG_STR_SIZE 64
1054 static struct regulator *create_regulator(struct regulator_dev *rdev,
1056 const char *supply_name)
1058 struct regulator *regulator;
1059 char buf[REG_STR_SIZE];
1062 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1063 if (regulator == NULL)
1066 mutex_lock(&rdev->mutex);
1067 regulator->rdev = rdev;
1068 list_add(®ulator->list, &rdev->consumer_list);
1071 /* create a 'requested_microamps_name' sysfs entry */
1072 size = scnprintf(buf, REG_STR_SIZE,
1073 "microamps_requested_%s-%s",
1074 dev_name(dev), supply_name);
1075 if (size >= REG_STR_SIZE)
1078 regulator->dev = dev;
1079 sysfs_attr_init(®ulator->dev_attr.attr);
1080 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1081 if (regulator->dev_attr.attr.name == NULL)
1084 regulator->dev_attr.attr.mode = 0444;
1085 regulator->dev_attr.show = device_requested_uA_show;
1086 err = device_create_file(dev, ®ulator->dev_attr);
1088 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1092 /* also add a link to the device sysfs entry */
1093 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1094 dev->kobj.name, supply_name);
1095 if (size >= REG_STR_SIZE)
1098 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1099 if (regulator->supply_name == NULL)
1102 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1105 rdev_warn(rdev, "could not add device link %s err %d\n",
1106 dev->kobj.name, err);
1110 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1111 if (regulator->supply_name == NULL)
1115 #ifdef CONFIG_DEBUG_FS
1116 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1118 if (IS_ERR_OR_NULL(regulator->debugfs)) {
1119 rdev_warn(rdev, "Failed to create debugfs directory\n");
1120 regulator->debugfs = NULL;
1122 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1123 ®ulator->uA_load);
1124 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1125 ®ulator->min_uV);
1126 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1127 ®ulator->max_uV);
1131 mutex_unlock(&rdev->mutex);
1134 kfree(regulator->supply_name);
1136 device_remove_file(regulator->dev, ®ulator->dev_attr);
1138 kfree(regulator->dev_attr.attr.name);
1140 list_del(®ulator->list);
1142 mutex_unlock(&rdev->mutex);
1146 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1148 if (!rdev->desc->ops->enable_time)
1150 return rdev->desc->ops->enable_time(rdev);
1153 /* Internal regulator request function */
1154 static struct regulator *_regulator_get(struct device *dev, const char *id,
1157 struct regulator_dev *rdev;
1158 struct regulator_map *map;
1159 struct regulator *regulator = ERR_PTR(-ENODEV);
1160 const char *devname = NULL;
1164 pr_err("get() with no identifier\n");
1169 devname = dev_name(dev);
1171 mutex_lock(®ulator_list_mutex);
1173 list_for_each_entry(map, ®ulator_map_list, list) {
1174 /* If the mapping has a device set up it must match */
1175 if (map->dev_name &&
1176 (!devname || strcmp(map->dev_name, devname)))
1179 if (strcmp(map->supply, id) == 0) {
1180 rdev = map->regulator;
1185 if (board_wants_dummy_regulator) {
1186 rdev = dummy_regulator_rdev;
1190 #ifdef CONFIG_REGULATOR_DUMMY
1192 devname = "deviceless";
1194 /* If the board didn't flag that it was fully constrained then
1195 * substitute in a dummy regulator so consumers can continue.
1197 if (!has_full_constraints) {
1198 pr_warn("%s supply %s not found, using dummy regulator\n",
1200 rdev = dummy_regulator_rdev;
1205 mutex_unlock(®ulator_list_mutex);
1209 if (rdev->exclusive) {
1210 regulator = ERR_PTR(-EPERM);
1214 if (exclusive && rdev->open_count) {
1215 regulator = ERR_PTR(-EBUSY);
1219 if (!try_module_get(rdev->owner))
1222 regulator = create_regulator(rdev, dev, id);
1223 if (regulator == NULL) {
1224 regulator = ERR_PTR(-ENOMEM);
1225 module_put(rdev->owner);
1230 rdev->exclusive = 1;
1232 ret = _regulator_is_enabled(rdev);
1234 rdev->use_count = 1;
1236 rdev->use_count = 0;
1240 mutex_unlock(®ulator_list_mutex);
1246 * regulator_get - lookup and obtain a reference to a regulator.
1247 * @dev: device for regulator "consumer"
1248 * @id: Supply name or regulator ID.
1250 * Returns a struct regulator corresponding to the regulator producer,
1251 * or IS_ERR() condition containing errno.
1253 * Use of supply names configured via regulator_set_device_supply() is
1254 * strongly encouraged. It is recommended that the supply name used
1255 * should match the name used for the supply and/or the relevant
1256 * device pins in the datasheet.
1258 struct regulator *regulator_get(struct device *dev, const char *id)
1260 return _regulator_get(dev, id, 0);
1262 EXPORT_SYMBOL_GPL(regulator_get);
1265 * regulator_get_exclusive - obtain exclusive access to a regulator.
1266 * @dev: device for regulator "consumer"
1267 * @id: Supply name or regulator ID.
1269 * Returns a struct regulator corresponding to the regulator producer,
1270 * or IS_ERR() condition containing errno. Other consumers will be
1271 * unable to obtain this reference is held and the use count for the
1272 * regulator will be initialised to reflect the current state of the
1275 * This is intended for use by consumers which cannot tolerate shared
1276 * use of the regulator such as those which need to force the
1277 * regulator off for correct operation of the hardware they are
1280 * Use of supply names configured via regulator_set_device_supply() is
1281 * strongly encouraged. It is recommended that the supply name used
1282 * should match the name used for the supply and/or the relevant
1283 * device pins in the datasheet.
1285 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1287 return _regulator_get(dev, id, 1);
1289 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1292 * regulator_put - "free" the regulator source
1293 * @regulator: regulator source
1295 * Note: drivers must ensure that all regulator_enable calls made on this
1296 * regulator source are balanced by regulator_disable calls prior to calling
1299 void regulator_put(struct regulator *regulator)
1301 struct regulator_dev *rdev;
1303 if (regulator == NULL || IS_ERR(regulator))
1306 mutex_lock(®ulator_list_mutex);
1307 rdev = regulator->rdev;
1309 #ifdef CONFIG_DEBUG_FS
1310 debugfs_remove_recursive(regulator->debugfs);
1313 /* remove any sysfs entries */
1314 if (regulator->dev) {
1315 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1316 device_remove_file(regulator->dev, ®ulator->dev_attr);
1317 kfree(regulator->dev_attr.attr.name);
1319 kfree(regulator->supply_name);
1320 list_del(®ulator->list);
1324 rdev->exclusive = 0;
1326 module_put(rdev->owner);
1327 mutex_unlock(®ulator_list_mutex);
1329 EXPORT_SYMBOL_GPL(regulator_put);
1331 static int _regulator_can_change_status(struct regulator_dev *rdev)
1333 if (!rdev->constraints)
1336 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1342 /* locks held by regulator_enable() */
1343 static int _regulator_enable(struct regulator_dev *rdev)
1347 /* check voltage and requested load before enabling */
1348 if (rdev->constraints &&
1349 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1350 drms_uA_update(rdev);
1352 if (rdev->use_count == 0) {
1353 /* The regulator may on if it's not switchable or left on */
1354 ret = _regulator_is_enabled(rdev);
1355 if (ret == -EINVAL || ret == 0) {
1356 if (!_regulator_can_change_status(rdev))
1359 if (!rdev->desc->ops->enable)
1362 /* Query before enabling in case configuration
1364 ret = _regulator_get_enable_time(rdev);
1368 rdev_warn(rdev, "enable_time() failed: %d\n",
1373 trace_regulator_enable(rdev_get_name(rdev));
1374 _notifier_call_chain(
1375 rdev, REGULATOR_EVENT_PRE_ENABLE, NULL);
1377 /* Allow the regulator to ramp; it would be useful
1378 * to extend this for bulk operations so that the
1379 * regulators can ramp together. */
1380 ret = rdev->desc->ops->enable(rdev);
1384 trace_regulator_enable_delay(rdev_get_name(rdev));
1386 if (delay >= 1000) {
1387 mdelay(delay / 1000);
1388 udelay(delay % 1000);
1393 _notifier_call_chain(
1394 rdev, REGULATOR_EVENT_POST_ENABLE, NULL);
1395 trace_regulator_enable_complete(rdev_get_name(rdev));
1397 } else if (ret < 0) {
1398 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1401 /* Fallthrough on positive return values - already enabled */
1410 * regulator_enable - enable regulator output
1411 * @regulator: regulator source
1413 * Request that the regulator be enabled with the regulator output at
1414 * the predefined voltage or current value. Calls to regulator_enable()
1415 * must be balanced with calls to regulator_disable().
1417 * NOTE: the output value can be set by other drivers, boot loader or may be
1418 * hardwired in the regulator.
1420 int regulator_enable(struct regulator *regulator)
1422 struct regulator_dev *rdev = regulator->rdev;
1426 ret = regulator_enable(rdev->supply);
1431 mutex_lock(&rdev->mutex);
1432 ret = _regulator_enable(rdev);
1433 mutex_unlock(&rdev->mutex);
1435 if (ret != 0 && rdev->supply)
1436 regulator_disable(rdev->supply);
1440 EXPORT_SYMBOL_GPL(regulator_enable);
1442 /* locks held by regulator_disable() */
1443 static int _regulator_disable(struct regulator_dev *rdev)
1447 if (WARN(rdev->use_count <= 0,
1448 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1451 /* are we the last user and permitted to disable ? */
1452 if (rdev->use_count == 1 &&
1453 (rdev->constraints && !rdev->constraints->always_on)) {
1455 /* we are last user */
1456 if (_regulator_can_change_status(rdev) &&
1457 rdev->desc->ops->disable) {
1458 trace_regulator_disable(rdev_get_name(rdev));
1460 ret = rdev->desc->ops->disable(rdev);
1462 rdev_err(rdev, "failed to disable\n");
1466 trace_regulator_disable_complete(rdev_get_name(rdev));
1468 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1472 rdev->use_count = 0;
1473 } else if (rdev->use_count > 1) {
1475 if (rdev->constraints &&
1476 (rdev->constraints->valid_ops_mask &
1477 REGULATOR_CHANGE_DRMS))
1478 drms_uA_update(rdev);
1487 * regulator_disable - disable regulator output
1488 * @regulator: regulator source
1490 * Disable the regulator output voltage or current. Calls to
1491 * regulator_enable() must be balanced with calls to
1492 * regulator_disable().
1494 * NOTE: this will only disable the regulator output if no other consumer
1495 * devices have it enabled, the regulator device supports disabling and
1496 * machine constraints permit this operation.
1498 int regulator_disable(struct regulator *regulator)
1500 struct regulator_dev *rdev = regulator->rdev;
1503 mutex_lock(&rdev->mutex);
1504 ret = _regulator_disable(rdev);
1505 mutex_unlock(&rdev->mutex);
1507 if (ret == 0 && rdev->supply)
1508 regulator_disable(rdev->supply);
1512 EXPORT_SYMBOL_GPL(regulator_disable);
1514 /* locks held by regulator_force_disable() */
1515 static int _regulator_force_disable(struct regulator_dev *rdev)
1520 if (rdev->desc->ops->disable) {
1521 /* ah well, who wants to live forever... */
1522 ret = rdev->desc->ops->disable(rdev);
1524 rdev_err(rdev, "failed to force disable\n");
1527 /* notify other consumers that power has been forced off */
1528 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1529 REGULATOR_EVENT_DISABLE, NULL);
1536 * regulator_force_disable - force disable regulator output
1537 * @regulator: regulator source
1539 * Forcibly disable the regulator output voltage or current.
1540 * NOTE: this *will* disable the regulator output even if other consumer
1541 * devices have it enabled. This should be used for situations when device
1542 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1544 int regulator_force_disable(struct regulator *regulator)
1546 struct regulator_dev *rdev = regulator->rdev;
1549 mutex_lock(&rdev->mutex);
1550 regulator->uA_load = 0;
1551 ret = _regulator_force_disable(regulator->rdev);
1552 mutex_unlock(&rdev->mutex);
1555 while (rdev->open_count--)
1556 regulator_disable(rdev->supply);
1560 EXPORT_SYMBOL_GPL(regulator_force_disable);
1562 static int _regulator_is_enabled(struct regulator_dev *rdev)
1564 /* If we don't know then assume that the regulator is always on */
1565 if (!rdev->desc->ops->is_enabled)
1568 return rdev->desc->ops->is_enabled(rdev);
1572 * regulator_is_enabled - is the regulator output enabled
1573 * @regulator: regulator source
1575 * Returns positive if the regulator driver backing the source/client
1576 * has requested that the device be enabled, zero if it hasn't, else a
1577 * negative errno code.
1579 * Note that the device backing this regulator handle can have multiple
1580 * users, so it might be enabled even if regulator_enable() was never
1581 * called for this particular source.
1583 int regulator_is_enabled(struct regulator *regulator)
1587 mutex_lock(®ulator->rdev->mutex);
1588 ret = _regulator_is_enabled(regulator->rdev);
1589 mutex_unlock(®ulator->rdev->mutex);
1593 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1596 * regulator_count_voltages - count regulator_list_voltage() selectors
1597 * @regulator: regulator source
1599 * Returns number of selectors, or negative errno. Selectors are
1600 * numbered starting at zero, and typically correspond to bitfields
1601 * in hardware registers.
1603 int regulator_count_voltages(struct regulator *regulator)
1605 struct regulator_dev *rdev = regulator->rdev;
1607 return rdev->desc->n_voltages ? : -EINVAL;
1609 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1612 * regulator_list_voltage - enumerate supported voltages
1613 * @regulator: regulator source
1614 * @selector: identify voltage to list
1615 * Context: can sleep
1617 * Returns a voltage that can be passed to @regulator_set_voltage(),
1618 * zero if this selector code can't be used on this system, or a
1621 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1623 struct regulator_dev *rdev = regulator->rdev;
1624 struct regulator_ops *ops = rdev->desc->ops;
1627 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1630 mutex_lock(&rdev->mutex);
1631 ret = ops->list_voltage(rdev, selector);
1632 mutex_unlock(&rdev->mutex);
1635 if (ret < rdev->constraints->min_uV)
1637 else if (ret > rdev->constraints->max_uV)
1643 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1646 * regulator_is_supported_voltage - check if a voltage range can be supported
1648 * @regulator: Regulator to check.
1649 * @min_uV: Minimum required voltage in uV.
1650 * @max_uV: Maximum required voltage in uV.
1652 * Returns a boolean or a negative error code.
1654 int regulator_is_supported_voltage(struct regulator *regulator,
1655 int min_uV, int max_uV)
1657 int i, voltages, ret;
1659 ret = regulator_count_voltages(regulator);
1664 for (i = 0; i < voltages; i++) {
1665 ret = regulator_list_voltage(regulator, i);
1667 if (ret >= min_uV && ret <= max_uV)
1674 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1675 int min_uV, int max_uV)
1679 unsigned int selector;
1681 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1683 min_uV += rdev->constraints->uV_offset;
1684 max_uV += rdev->constraints->uV_offset;
1686 if (_regulator_is_enabled(rdev))
1687 _notifier_call_chain(rdev, REGULATOR_EVENT_OUT_PRECHANGE,
1690 if (rdev->desc->ops->set_voltage) {
1691 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1694 if (rdev->desc->ops->list_voltage)
1695 selector = rdev->desc->ops->list_voltage(rdev,
1699 } else if (rdev->desc->ops->set_voltage_sel) {
1700 int best_val = INT_MAX;
1705 /* Find the smallest voltage that falls within the specified
1708 for (i = 0; i < rdev->desc->n_voltages; i++) {
1709 ret = rdev->desc->ops->list_voltage(rdev, i);
1713 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1720 * If we can't obtain the old selector there is not enough
1721 * info to call set_voltage_time_sel().
1723 if (rdev->desc->ops->set_voltage_time_sel &&
1724 rdev->desc->ops->get_voltage_sel) {
1725 unsigned int old_selector = 0;
1727 ret = rdev->desc->ops->get_voltage_sel(rdev);
1731 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1732 old_selector, selector);
1735 if (best_val != INT_MAX) {
1736 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1737 selector = best_val;
1745 /* Insert any necessary delays */
1746 if (delay >= 1000) {
1747 mdelay(delay / 1000);
1748 udelay(delay % 1000);
1754 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1757 if (_regulator_is_enabled(rdev))
1758 _notifier_call_chain(rdev, REGULATOR_EVENT_OUT_POSTCHANGE,
1761 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1767 * regulator_set_voltage - set regulator output voltage
1768 * @regulator: regulator source
1769 * @min_uV: Minimum required voltage in uV
1770 * @max_uV: Maximum acceptable voltage in uV
1772 * Sets a voltage regulator to the desired output voltage. This can be set
1773 * during any regulator state. IOW, regulator can be disabled or enabled.
1775 * If the regulator is enabled then the voltage will change to the new value
1776 * immediately otherwise if the regulator is disabled the regulator will
1777 * output at the new voltage when enabled.
1779 * NOTE: If the regulator is shared between several devices then the lowest
1780 * request voltage that meets the system constraints will be used.
1781 * Regulator system constraints must be set for this regulator before
1782 * calling this function otherwise this call will fail.
1784 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1786 struct regulator_dev *rdev = regulator->rdev;
1789 mutex_lock(&rdev->mutex);
1791 /* If we're setting the same range as last time the change
1792 * should be a noop (some cpufreq implementations use the same
1793 * voltage for multiple frequencies, for example).
1795 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1799 if (!rdev->desc->ops->set_voltage &&
1800 !rdev->desc->ops->set_voltage_sel) {
1805 /* constraints check */
1806 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1809 regulator->min_uV = min_uV;
1810 regulator->max_uV = max_uV;
1812 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1816 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1819 mutex_unlock(&rdev->mutex);
1822 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1825 * regulator_set_voltage_time - get raise/fall time
1826 * @regulator: regulator source
1827 * @old_uV: starting voltage in microvolts
1828 * @new_uV: target voltage in microvolts
1830 * Provided with the starting and ending voltage, this function attempts to
1831 * calculate the time in microseconds required to rise or fall to this new
1834 int regulator_set_voltage_time(struct regulator *regulator,
1835 int old_uV, int new_uV)
1837 struct regulator_dev *rdev = regulator->rdev;
1838 struct regulator_ops *ops = rdev->desc->ops;
1844 /* Currently requires operations to do this */
1845 if (!ops->list_voltage || !ops->set_voltage_time_sel
1846 || !rdev->desc->n_voltages)
1849 for (i = 0; i < rdev->desc->n_voltages; i++) {
1850 /* We only look for exact voltage matches here */
1851 voltage = regulator_list_voltage(regulator, i);
1856 if (voltage == old_uV)
1858 if (voltage == new_uV)
1862 if (old_sel < 0 || new_sel < 0)
1865 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1867 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1870 * regulator_sync_voltage - re-apply last regulator output voltage
1871 * @regulator: regulator source
1873 * Re-apply the last configured voltage. This is intended to be used
1874 * where some external control source the consumer is cooperating with
1875 * has caused the configured voltage to change.
1877 int regulator_sync_voltage(struct regulator *regulator)
1879 struct regulator_dev *rdev = regulator->rdev;
1880 int ret, min_uV, max_uV;
1882 mutex_lock(&rdev->mutex);
1884 if (!rdev->desc->ops->set_voltage &&
1885 !rdev->desc->ops->set_voltage_sel) {
1890 /* This is only going to work if we've had a voltage configured. */
1891 if (!regulator->min_uV && !regulator->max_uV) {
1896 min_uV = regulator->min_uV;
1897 max_uV = regulator->max_uV;
1899 /* This should be a paranoia check... */
1900 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1904 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1908 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1911 mutex_unlock(&rdev->mutex);
1914 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1916 static int _regulator_get_voltage(struct regulator_dev *rdev)
1920 if (rdev->desc->ops->get_voltage_sel) {
1921 sel = rdev->desc->ops->get_voltage_sel(rdev);
1924 ret = rdev->desc->ops->list_voltage(rdev, sel);
1925 } else if (rdev->desc->ops->get_voltage) {
1926 ret = rdev->desc->ops->get_voltage(rdev);
1933 return ret - rdev->constraints->uV_offset;
1937 * regulator_get_voltage - get regulator output voltage
1938 * @regulator: regulator source
1940 * This returns the current regulator voltage in uV.
1942 * NOTE: If the regulator is disabled it will return the voltage value. This
1943 * function should not be used to determine regulator state.
1945 int regulator_get_voltage(struct regulator *regulator)
1949 mutex_lock(®ulator->rdev->mutex);
1951 ret = _regulator_get_voltage(regulator->rdev);
1953 mutex_unlock(®ulator->rdev->mutex);
1957 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1960 * regulator_set_current_limit - set regulator output current limit
1961 * @regulator: regulator source
1962 * @min_uA: Minimuum supported current in uA
1963 * @max_uA: Maximum supported current in uA
1965 * Sets current sink to the desired output current. This can be set during
1966 * any regulator state. IOW, regulator can be disabled or enabled.
1968 * If the regulator is enabled then the current will change to the new value
1969 * immediately otherwise if the regulator is disabled the regulator will
1970 * output at the new current when enabled.
1972 * NOTE: Regulator system constraints must be set for this regulator before
1973 * calling this function otherwise this call will fail.
1975 int regulator_set_current_limit(struct regulator *regulator,
1976 int min_uA, int max_uA)
1978 struct regulator_dev *rdev = regulator->rdev;
1981 mutex_lock(&rdev->mutex);
1984 if (!rdev->desc->ops->set_current_limit) {
1989 /* constraints check */
1990 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1994 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1996 mutex_unlock(&rdev->mutex);
1999 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2001 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2005 mutex_lock(&rdev->mutex);
2008 if (!rdev->desc->ops->get_current_limit) {
2013 ret = rdev->desc->ops->get_current_limit(rdev);
2015 mutex_unlock(&rdev->mutex);
2020 * regulator_get_current_limit - get regulator output current
2021 * @regulator: regulator source
2023 * This returns the current supplied by the specified current sink in uA.
2025 * NOTE: If the regulator is disabled it will return the current value. This
2026 * function should not be used to determine regulator state.
2028 int regulator_get_current_limit(struct regulator *regulator)
2030 return _regulator_get_current_limit(regulator->rdev);
2032 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2035 * regulator_set_mode - set regulator operating mode
2036 * @regulator: regulator source
2037 * @mode: operating mode - one of the REGULATOR_MODE constants
2039 * Set regulator operating mode to increase regulator efficiency or improve
2040 * regulation performance.
2042 * NOTE: Regulator system constraints must be set for this regulator before
2043 * calling this function otherwise this call will fail.
2045 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2047 struct regulator_dev *rdev = regulator->rdev;
2049 int regulator_curr_mode;
2051 mutex_lock(&rdev->mutex);
2054 if (!rdev->desc->ops->set_mode) {
2059 /* return if the same mode is requested */
2060 if (rdev->desc->ops->get_mode) {
2061 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2062 if (regulator_curr_mode == mode) {
2068 /* constraints check */
2069 ret = regulator_mode_constrain(rdev, &mode);
2073 ret = rdev->desc->ops->set_mode(rdev, mode);
2075 mutex_unlock(&rdev->mutex);
2078 EXPORT_SYMBOL_GPL(regulator_set_mode);
2080 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2084 mutex_lock(&rdev->mutex);
2087 if (!rdev->desc->ops->get_mode) {
2092 ret = rdev->desc->ops->get_mode(rdev);
2094 mutex_unlock(&rdev->mutex);
2099 * regulator_get_mode - get regulator operating mode
2100 * @regulator: regulator source
2102 * Get the current regulator operating mode.
2104 unsigned int regulator_get_mode(struct regulator *regulator)
2106 return _regulator_get_mode(regulator->rdev);
2108 EXPORT_SYMBOL_GPL(regulator_get_mode);
2111 * regulator_set_optimum_mode - set regulator optimum operating mode
2112 * @regulator: regulator source
2113 * @uA_load: load current
2115 * Notifies the regulator core of a new device load. This is then used by
2116 * DRMS (if enabled by constraints) to set the most efficient regulator
2117 * operating mode for the new regulator loading.
2119 * Consumer devices notify their supply regulator of the maximum power
2120 * they will require (can be taken from device datasheet in the power
2121 * consumption tables) when they change operational status and hence power
2122 * state. Examples of operational state changes that can affect power
2123 * consumption are :-
2125 * o Device is opened / closed.
2126 * o Device I/O is about to begin or has just finished.
2127 * o Device is idling in between work.
2129 * This information is also exported via sysfs to userspace.
2131 * DRMS will sum the total requested load on the regulator and change
2132 * to the most efficient operating mode if platform constraints allow.
2134 * Returns the new regulator mode or error.
2136 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2138 struct regulator_dev *rdev = regulator->rdev;
2139 struct regulator *consumer;
2140 int ret, output_uV, input_uV, total_uA_load = 0;
2143 mutex_lock(&rdev->mutex);
2146 * first check to see if we can set modes at all, otherwise just
2147 * tell the consumer everything is OK.
2149 regulator->uA_load = uA_load;
2150 ret = regulator_check_drms(rdev);
2156 if (!rdev->desc->ops->get_optimum_mode)
2160 * we can actually do this so any errors are indicators of
2161 * potential real failure.
2165 /* get output voltage */
2166 output_uV = _regulator_get_voltage(rdev);
2167 if (output_uV <= 0) {
2168 rdev_err(rdev, "invalid output voltage found\n");
2172 /* get input voltage */
2175 input_uV = regulator_get_voltage(rdev->supply);
2177 input_uV = rdev->constraints->input_uV;
2178 if (input_uV <= 0) {
2179 rdev_err(rdev, "invalid input voltage found\n");
2183 /* calc total requested load for this regulator */
2184 list_for_each_entry(consumer, &rdev->consumer_list, list)
2185 total_uA_load += consumer->uA_load;
2187 mode = rdev->desc->ops->get_optimum_mode(rdev,
2188 input_uV, output_uV,
2190 ret = regulator_mode_constrain(rdev, &mode);
2192 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2193 total_uA_load, input_uV, output_uV);
2197 ret = rdev->desc->ops->set_mode(rdev, mode);
2199 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2204 mutex_unlock(&rdev->mutex);
2207 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2210 * regulator_register_notifier - register regulator event notifier
2211 * @regulator: regulator source
2212 * @nb: notifier block
2214 * Register notifier block to receive regulator events.
2216 int regulator_register_notifier(struct regulator *regulator,
2217 struct notifier_block *nb)
2219 return blocking_notifier_chain_register(®ulator->rdev->notifier,
2222 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2225 * regulator_unregister_notifier - unregister regulator event notifier
2226 * @regulator: regulator source
2227 * @nb: notifier block
2229 * Unregister regulator event notifier block.
2231 int regulator_unregister_notifier(struct regulator *regulator,
2232 struct notifier_block *nb)
2234 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
2237 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2239 /* notify regulator consumers and downstream regulator consumers.
2240 * Note mutex must be held by caller.
2242 static void _notifier_call_chain(struct regulator_dev *rdev,
2243 unsigned long event, void *data)
2245 /* call rdev chain first */
2246 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
2250 * regulator_bulk_get - get multiple regulator consumers
2252 * @dev: Device to supply
2253 * @num_consumers: Number of consumers to register
2254 * @consumers: Configuration of consumers; clients are stored here.
2256 * @return 0 on success, an errno on failure.
2258 * This helper function allows drivers to get several regulator
2259 * consumers in one operation. If any of the regulators cannot be
2260 * acquired then any regulators that were allocated will be freed
2261 * before returning to the caller.
2263 int regulator_bulk_get(struct device *dev, int num_consumers,
2264 struct regulator_bulk_data *consumers)
2269 for (i = 0; i < num_consumers; i++)
2270 consumers[i].consumer = NULL;
2272 for (i = 0; i < num_consumers; i++) {
2273 consumers[i].consumer = regulator_get(dev,
2274 consumers[i].supply);
2275 if (IS_ERR(consumers[i].consumer)) {
2276 ret = PTR_ERR(consumers[i].consumer);
2277 dev_err(dev, "Failed to get supply '%s': %d\n",
2278 consumers[i].supply, ret);
2279 consumers[i].consumer = NULL;
2287 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2288 regulator_put(consumers[i].consumer);
2292 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2294 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2296 struct regulator_bulk_data *bulk = data;
2298 bulk->ret = regulator_enable(bulk->consumer);
2302 * regulator_bulk_enable - enable multiple regulator consumers
2304 * @num_consumers: Number of consumers
2305 * @consumers: Consumer data; clients are stored here.
2306 * @return 0 on success, an errno on failure
2308 * This convenience API allows consumers to enable multiple regulator
2309 * clients in a single API call. If any consumers cannot be enabled
2310 * then any others that were enabled will be disabled again prior to
2313 int regulator_bulk_enable(int num_consumers,
2314 struct regulator_bulk_data *consumers)
2316 LIST_HEAD(async_domain);
2320 for (i = 0; i < num_consumers; i++)
2321 async_schedule_domain(regulator_bulk_enable_async,
2322 &consumers[i], &async_domain);
2324 async_synchronize_full_domain(&async_domain);
2326 /* If any consumer failed we need to unwind any that succeeded */
2327 for (i = 0; i < num_consumers; i++) {
2328 if (consumers[i].ret != 0) {
2329 ret = consumers[i].ret;
2337 for (i = 0; i < num_consumers; i++)
2338 if (consumers[i].ret == 0)
2339 regulator_disable(consumers[i].consumer);
2341 pr_err("Failed to enable %s: %d\n",
2342 consumers[i].supply, consumers[i].ret);
2346 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2349 * regulator_bulk_disable - disable multiple regulator consumers
2351 * @num_consumers: Number of consumers
2352 * @consumers: Consumer data; clients are stored here.
2353 * @return 0 on success, an errno on failure
2355 * This convenience API allows consumers to disable multiple regulator
2356 * clients in a single API call. If any consumers cannot be enabled
2357 * then any others that were disabled will be disabled again prior to
2360 int regulator_bulk_disable(int num_consumers,
2361 struct regulator_bulk_data *consumers)
2366 for (i = 0; i < num_consumers; i++) {
2367 ret = regulator_disable(consumers[i].consumer);
2375 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2376 for (--i; i >= 0; --i)
2377 regulator_enable(consumers[i].consumer);
2381 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2384 * regulator_bulk_free - free multiple regulator consumers
2386 * @num_consumers: Number of consumers
2387 * @consumers: Consumer data; clients are stored here.
2389 * This convenience API allows consumers to free multiple regulator
2390 * clients in a single API call.
2392 void regulator_bulk_free(int num_consumers,
2393 struct regulator_bulk_data *consumers)
2397 for (i = 0; i < num_consumers; i++) {
2398 regulator_put(consumers[i].consumer);
2399 consumers[i].consumer = NULL;
2402 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2405 * regulator_notifier_call_chain - call regulator event notifier
2406 * @rdev: regulator source
2407 * @event: notifier block
2408 * @data: callback-specific data.
2410 * Called by regulator drivers to notify clients a regulator event has
2411 * occurred. We also notify regulator clients downstream.
2412 * Note lock must be held by caller.
2414 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2415 unsigned long event, void *data)
2417 _notifier_call_chain(rdev, event, data);
2421 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2424 * regulator_mode_to_status - convert a regulator mode into a status
2426 * @mode: Mode to convert
2428 * Convert a regulator mode into a status.
2430 int regulator_mode_to_status(unsigned int mode)
2433 case REGULATOR_MODE_FAST:
2434 return REGULATOR_STATUS_FAST;
2435 case REGULATOR_MODE_NORMAL:
2436 return REGULATOR_STATUS_NORMAL;
2437 case REGULATOR_MODE_IDLE:
2438 return REGULATOR_STATUS_IDLE;
2439 case REGULATOR_STATUS_STANDBY:
2440 return REGULATOR_STATUS_STANDBY;
2445 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2448 * To avoid cluttering sysfs (and memory) with useless state, only
2449 * create attributes that can be meaningfully displayed.
2451 static int add_regulator_attributes(struct regulator_dev *rdev)
2453 struct device *dev = &rdev->dev;
2454 struct regulator_ops *ops = rdev->desc->ops;
2457 /* some attributes need specific methods to be displayed */
2458 if (ops->get_voltage || ops->get_voltage_sel) {
2459 status = device_create_file(dev, &dev_attr_microvolts);
2463 if (ops->get_current_limit) {
2464 status = device_create_file(dev, &dev_attr_microamps);
2468 if (ops->get_mode) {
2469 status = device_create_file(dev, &dev_attr_opmode);
2473 if (ops->is_enabled) {
2474 status = device_create_file(dev, &dev_attr_state);
2478 if (ops->get_status) {
2479 status = device_create_file(dev, &dev_attr_status);
2484 /* some attributes are type-specific */
2485 if (rdev->desc->type == REGULATOR_CURRENT) {
2486 status = device_create_file(dev, &dev_attr_requested_microamps);
2491 /* all the other attributes exist to support constraints;
2492 * don't show them if there are no constraints, or if the
2493 * relevant supporting methods are missing.
2495 if (!rdev->constraints)
2498 /* constraints need specific supporting methods */
2499 if (ops->set_voltage || ops->set_voltage_sel) {
2500 status = device_create_file(dev, &dev_attr_min_microvolts);
2503 status = device_create_file(dev, &dev_attr_max_microvolts);
2507 if (ops->set_current_limit) {
2508 status = device_create_file(dev, &dev_attr_min_microamps);
2511 status = device_create_file(dev, &dev_attr_max_microamps);
2516 /* suspend mode constraints need multiple supporting methods */
2517 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2520 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2523 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2526 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2530 if (ops->set_suspend_voltage) {
2531 status = device_create_file(dev,
2532 &dev_attr_suspend_standby_microvolts);
2535 status = device_create_file(dev,
2536 &dev_attr_suspend_mem_microvolts);
2539 status = device_create_file(dev,
2540 &dev_attr_suspend_disk_microvolts);
2545 if (ops->set_suspend_mode) {
2546 status = device_create_file(dev,
2547 &dev_attr_suspend_standby_mode);
2550 status = device_create_file(dev,
2551 &dev_attr_suspend_mem_mode);
2554 status = device_create_file(dev,
2555 &dev_attr_suspend_disk_mode);
2563 static void rdev_init_debugfs(struct regulator_dev *rdev)
2565 #ifdef CONFIG_DEBUG_FS
2566 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2567 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2568 rdev_warn(rdev, "Failed to create debugfs directory\n");
2569 rdev->debugfs = NULL;
2573 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2575 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2581 * regulator_register - register regulator
2582 * @regulator_desc: regulator to register
2583 * @dev: struct device for the regulator
2584 * @init_data: platform provided init data, passed through by driver
2585 * @driver_data: private regulator data
2587 * Called by regulator drivers to register a regulator.
2588 * Returns 0 on success.
2590 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2591 struct device *dev, const struct regulator_init_data *init_data,
2594 static atomic_t regulator_no = ATOMIC_INIT(0);
2595 struct regulator_dev *rdev;
2598 if (regulator_desc == NULL)
2599 return ERR_PTR(-EINVAL);
2601 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2602 return ERR_PTR(-EINVAL);
2604 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2605 regulator_desc->type != REGULATOR_CURRENT)
2606 return ERR_PTR(-EINVAL);
2609 return ERR_PTR(-EINVAL);
2611 /* Only one of each should be implemented */
2612 WARN_ON(regulator_desc->ops->get_voltage &&
2613 regulator_desc->ops->get_voltage_sel);
2614 WARN_ON(regulator_desc->ops->set_voltage &&
2615 regulator_desc->ops->set_voltage_sel);
2617 /* If we're using selectors we must implement list_voltage. */
2618 if (regulator_desc->ops->get_voltage_sel &&
2619 !regulator_desc->ops->list_voltage) {
2620 return ERR_PTR(-EINVAL);
2622 if (regulator_desc->ops->set_voltage_sel &&
2623 !regulator_desc->ops->list_voltage) {
2624 return ERR_PTR(-EINVAL);
2627 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2629 return ERR_PTR(-ENOMEM);
2631 mutex_lock(®ulator_list_mutex);
2633 mutex_init(&rdev->mutex);
2634 rdev->reg_data = driver_data;
2635 rdev->owner = regulator_desc->owner;
2636 rdev->desc = regulator_desc;
2637 INIT_LIST_HEAD(&rdev->consumer_list);
2638 INIT_LIST_HEAD(&rdev->list);
2639 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2641 /* preform any regulator specific init */
2642 if (init_data->regulator_init) {
2643 ret = init_data->regulator_init(rdev->reg_data);
2648 /* register with sysfs */
2649 rdev->dev.class = ®ulator_class;
2650 rdev->dev.parent = dev;
2651 dev_set_name(&rdev->dev, "regulator.%d",
2652 atomic_inc_return(®ulator_no) - 1);
2653 ret = device_register(&rdev->dev);
2655 put_device(&rdev->dev);
2659 dev_set_drvdata(&rdev->dev, rdev);
2661 /* set regulator constraints */
2662 ret = set_machine_constraints(rdev, &init_data->constraints);
2666 /* add attributes supported by this regulator */
2667 ret = add_regulator_attributes(rdev);
2671 if (init_data->supply_regulator) {
2672 struct regulator_dev *r;
2675 list_for_each_entry(r, ®ulator_list, list) {
2676 if (strcmp(rdev_get_name(r),
2677 init_data->supply_regulator) == 0) {
2684 dev_err(dev, "Failed to find supply %s\n",
2685 init_data->supply_regulator);
2690 ret = set_supply(rdev, r);
2694 /* Enable supply if rail is enabled */
2695 if (rdev->desc->ops->is_enabled &&
2696 rdev->desc->ops->is_enabled(rdev)) {
2697 ret = regulator_enable(rdev->supply);
2703 /* add consumers devices */
2704 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2705 ret = set_consumer_device_supply(rdev,
2706 init_data->consumer_supplies[i].dev,
2707 init_data->consumer_supplies[i].dev_name,
2708 init_data->consumer_supplies[i].supply);
2710 dev_err(dev, "Failed to set supply %s\n",
2711 init_data->consumer_supplies[i].supply);
2712 goto unset_supplies;
2716 list_add(&rdev->list, ®ulator_list);
2718 rdev_init_debugfs(rdev);
2720 mutex_unlock(®ulator_list_mutex);
2724 unset_regulator_supplies(rdev);
2727 kfree(rdev->constraints);
2728 device_unregister(&rdev->dev);
2729 /* device core frees rdev */
2730 rdev = ERR_PTR(ret);
2735 rdev = ERR_PTR(ret);
2738 EXPORT_SYMBOL_GPL(regulator_register);
2741 * regulator_unregister - unregister regulator
2742 * @rdev: regulator to unregister
2744 * Called by regulator drivers to unregister a regulator.
2746 void regulator_unregister(struct regulator_dev *rdev)
2751 mutex_lock(®ulator_list_mutex);
2752 #ifdef CONFIG_DEBUG_FS
2753 debugfs_remove_recursive(rdev->debugfs);
2755 WARN_ON(rdev->open_count);
2756 unset_regulator_supplies(rdev);
2757 list_del(&rdev->list);
2759 regulator_put(rdev->supply);
2760 device_unregister(&rdev->dev);
2761 kfree(rdev->constraints);
2762 mutex_unlock(®ulator_list_mutex);
2764 EXPORT_SYMBOL_GPL(regulator_unregister);
2767 * regulator_suspend_prepare - prepare regulators for system wide suspend
2768 * @state: system suspend state
2770 * Configure each regulator with it's suspend operating parameters for state.
2771 * This will usually be called by machine suspend code prior to supending.
2773 int regulator_suspend_prepare(suspend_state_t state)
2775 struct regulator_dev *rdev;
2778 /* ON is handled by regulator active state */
2779 if (state == PM_SUSPEND_ON)
2782 mutex_lock(®ulator_list_mutex);
2783 list_for_each_entry(rdev, ®ulator_list, list) {
2785 mutex_lock(&rdev->mutex);
2786 ret = suspend_prepare(rdev, state);
2787 mutex_unlock(&rdev->mutex);
2790 rdev_err(rdev, "failed to prepare\n");
2795 mutex_unlock(®ulator_list_mutex);
2798 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2801 * regulator_suspend_finish - resume regulators from system wide suspend
2803 * Turn on regulators that might be turned off by regulator_suspend_prepare
2804 * and that should be turned on according to the regulators properties.
2806 int regulator_suspend_finish(void)
2808 struct regulator_dev *rdev;
2811 mutex_lock(®ulator_list_mutex);
2812 list_for_each_entry(rdev, ®ulator_list, list) {
2813 struct regulator_ops *ops = rdev->desc->ops;
2815 mutex_lock(&rdev->mutex);
2816 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2818 error = ops->enable(rdev);
2822 if (!has_full_constraints)
2826 if (ops->is_enabled && !ops->is_enabled(rdev))
2829 error = ops->disable(rdev);
2834 mutex_unlock(&rdev->mutex);
2836 mutex_unlock(®ulator_list_mutex);
2839 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2842 * regulator_has_full_constraints - the system has fully specified constraints
2844 * Calling this function will cause the regulator API to disable all
2845 * regulators which have a zero use count and don't have an always_on
2846 * constraint in a late_initcall.
2848 * The intention is that this will become the default behaviour in a
2849 * future kernel release so users are encouraged to use this facility
2852 void regulator_has_full_constraints(void)
2854 has_full_constraints = 1;
2856 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2859 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2861 * Calling this function will cause the regulator API to provide a
2862 * dummy regulator to consumers if no physical regulator is found,
2863 * allowing most consumers to proceed as though a regulator were
2864 * configured. This allows systems such as those with software
2865 * controllable regulators for the CPU core only to be brought up more
2868 void regulator_use_dummy_regulator(void)
2870 board_wants_dummy_regulator = true;
2872 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2875 * rdev_get_drvdata - get rdev regulator driver data
2878 * Get rdev regulator driver private data. This call can be used in the
2879 * regulator driver context.
2881 void *rdev_get_drvdata(struct regulator_dev *rdev)
2883 return rdev->reg_data;
2885 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2888 * regulator_get_drvdata - get regulator driver data
2889 * @regulator: regulator
2891 * Get regulator driver private data. This call can be used in the consumer
2892 * driver context when non API regulator specific functions need to be called.
2894 void *regulator_get_drvdata(struct regulator *regulator)
2896 return regulator->rdev->reg_data;
2898 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2901 * regulator_set_drvdata - set regulator driver data
2902 * @regulator: regulator
2905 void regulator_set_drvdata(struct regulator *regulator, void *data)
2907 regulator->rdev->reg_data = data;
2909 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2912 * regulator_get_id - get regulator ID
2915 int rdev_get_id(struct regulator_dev *rdev)
2917 return rdev->desc->id;
2919 EXPORT_SYMBOL_GPL(rdev_get_id);
2921 struct device *rdev_get_dev(struct regulator_dev *rdev)
2925 EXPORT_SYMBOL_GPL(rdev_get_dev);
2927 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2929 return reg_init_data->driver_data;
2931 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2933 static int __init regulator_init(void)
2937 ret = class_register(®ulator_class);
2939 #ifdef CONFIG_DEBUG_FS
2940 debugfs_root = debugfs_create_dir("regulator", NULL);
2941 if (IS_ERR(debugfs_root) || !debugfs_root) {
2942 pr_warn("regulator: Failed to create debugfs directory\n");
2943 debugfs_root = NULL;
2947 regulator_dummy_init();
2952 /* init early to allow our consumers to complete system booting */
2953 core_initcall(regulator_init);
2955 static int __init regulator_init_complete(void)
2957 struct regulator_dev *rdev;
2958 struct regulator_ops *ops;
2959 struct regulation_constraints *c;
2962 mutex_lock(®ulator_list_mutex);
2964 /* If we have a full configuration then disable any regulators
2965 * which are not in use or always_on. This will become the
2966 * default behaviour in the future.
2968 list_for_each_entry(rdev, ®ulator_list, list) {
2969 ops = rdev->desc->ops;
2970 c = rdev->constraints;
2972 if (!ops->disable || (c && c->always_on))
2975 mutex_lock(&rdev->mutex);
2977 if (rdev->use_count)
2980 /* If we can't read the status assume it's on. */
2981 if (ops->is_enabled)
2982 enabled = ops->is_enabled(rdev);
2989 if (has_full_constraints) {
2990 /* We log since this may kill the system if it
2992 rdev_info(rdev, "disabling\n");
2993 ret = ops->disable(rdev);
2995 rdev_err(rdev, "couldn't disable: %d\n", ret);
2998 /* The intention is that in future we will
2999 * assume that full constraints are provided
3000 * so warn even if we aren't going to do
3003 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3007 mutex_unlock(&rdev->mutex);
3010 mutex_unlock(®ulator_list_mutex);
3015 #ifdef CONFIG_DEBUG_FS
3016 static int regulator_syncevent(struct file *file, const char __user *user_buf,
3017 size_t count, loff_t *ppos)
3019 struct regulator_dev *rdev;
3023 memset(buffer, 0, sizeof(buffer));
3024 buf_size = min(count, (sizeof(buffer)-1));
3026 if (copy_from_user(buffer, user_buf, buf_size))
3029 if (!strnicmp("all", buffer, 3)) {
3031 mutex_lock(®ulator_list_mutex);
3033 list_for_each_entry(rdev, ®ulator_list, list) {
3034 mutex_lock(&rdev->mutex);
3036 if (_regulator_is_enabled(rdev))
3037 trace_regulator_enable(rdev_get_name(rdev));
3039 trace_regulator_disable(rdev_get_name(rdev));
3041 trace_regulator_set_voltage(rdev_get_name(rdev),
3042 _regulator_get_voltage(rdev),
3043 _regulator_get_voltage(rdev));
3045 mutex_unlock(&rdev->mutex);
3049 mutex_unlock(®ulator_list_mutex);
3054 static const struct file_operations regulator_syncevent_fops = {
3055 .write = regulator_syncevent,
3058 static int __init regulator_init_debugfs(void)
3060 debugfs_create_file("syncevent_regulators", S_IWUSR, NULL, NULL,
3061 ®ulator_syncevent_fops);
3066 late_initcall(regulator_init_debugfs);
3069 late_initcall(regulator_init_complete);