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 static struct dentry *debugfs_root;
58 * struct regulator_map
60 * Used to provide symbolic supply names to devices.
62 struct regulator_map {
63 struct list_head list;
64 const char *dev_name; /* The dev_name() for the consumer */
66 struct regulator_dev *regulator;
72 * One for each consumer device.
76 struct list_head list;
81 struct device_attribute dev_attr;
82 struct regulator_dev *rdev;
83 struct dentry *debugfs;
86 static int _regulator_is_enabled(struct regulator_dev *rdev);
87 static int _regulator_disable(struct regulator_dev *rdev);
88 static int _regulator_get_voltage(struct regulator_dev *rdev);
89 static int _regulator_get_current_limit(struct regulator_dev *rdev);
90 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
91 static void _notifier_call_chain(struct regulator_dev *rdev,
92 unsigned long event, void *data);
93 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
94 int min_uV, int max_uV);
95 static struct regulator *create_regulator(struct regulator_dev *rdev,
97 const char *supply_name);
99 static const char *rdev_get_name(struct regulator_dev *rdev)
101 if (rdev->constraints && rdev->constraints->name)
102 return rdev->constraints->name;
103 else if (rdev->desc->name)
104 return rdev->desc->name;
109 /* gets the regulator for a given consumer device */
110 static struct regulator *get_device_regulator(struct device *dev)
112 struct regulator *regulator = NULL;
113 struct regulator_dev *rdev;
115 mutex_lock(®ulator_list_mutex);
116 list_for_each_entry(rdev, ®ulator_list, list) {
117 mutex_lock(&rdev->mutex);
118 list_for_each_entry(regulator, &rdev->consumer_list, list) {
119 if (regulator->dev == dev) {
120 mutex_unlock(&rdev->mutex);
121 mutex_unlock(®ulator_list_mutex);
125 mutex_unlock(&rdev->mutex);
127 mutex_unlock(®ulator_list_mutex);
132 * of_get_regulator - get a regulator device node based on supply name
133 * @dev: Device pointer for the consumer (of regulator) device
134 * @supply: regulator supply name
136 * Extract the regulator device node corresponding to the supply name.
137 * retruns the device node corresponding to the regulator if found, else
140 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
142 struct device_node *regnode = NULL;
143 char prop_name[32]; /* 32 is max size of property name */
145 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
147 snprintf(prop_name, 32, "%s-supply", supply);
148 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
151 dev_dbg(dev, "Looking up %s property in node %s failed",
152 prop_name, dev->of_node->full_name);
158 /* Platform voltage constraint check */
159 static int regulator_check_voltage(struct regulator_dev *rdev,
160 int *min_uV, int *max_uV)
162 BUG_ON(*min_uV > *max_uV);
164 if (!rdev->constraints) {
165 rdev_err(rdev, "no constraints\n");
168 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
169 rdev_err(rdev, "operation not allowed\n");
173 if (*max_uV > rdev->constraints->max_uV)
174 *max_uV = rdev->constraints->max_uV;
175 if (*min_uV < rdev->constraints->min_uV)
176 *min_uV = rdev->constraints->min_uV;
178 if (*min_uV > *max_uV) {
179 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
187 /* Make sure we select a voltage that suits the needs of all
188 * regulator consumers
190 static int regulator_check_consumers(struct regulator_dev *rdev,
191 int *min_uV, int *max_uV)
193 struct regulator *regulator;
195 list_for_each_entry(regulator, &rdev->consumer_list, list) {
197 * Assume consumers that didn't say anything are OK
198 * with anything in the constraint range.
200 if (!regulator->min_uV && !regulator->max_uV)
203 if (*max_uV > regulator->max_uV)
204 *max_uV = regulator->max_uV;
205 if (*min_uV < regulator->min_uV)
206 *min_uV = regulator->min_uV;
209 if (*min_uV > *max_uV)
215 /* current constraint check */
216 static int regulator_check_current_limit(struct regulator_dev *rdev,
217 int *min_uA, int *max_uA)
219 BUG_ON(*min_uA > *max_uA);
221 if (!rdev->constraints) {
222 rdev_err(rdev, "no constraints\n");
225 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
226 rdev_err(rdev, "operation not allowed\n");
230 if (*max_uA > rdev->constraints->max_uA)
231 *max_uA = rdev->constraints->max_uA;
232 if (*min_uA < rdev->constraints->min_uA)
233 *min_uA = rdev->constraints->min_uA;
235 if (*min_uA > *max_uA) {
236 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
244 /* operating mode constraint check */
245 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
248 case REGULATOR_MODE_FAST:
249 case REGULATOR_MODE_NORMAL:
250 case REGULATOR_MODE_IDLE:
251 case REGULATOR_MODE_STANDBY:
254 rdev_err(rdev, "invalid mode %x specified\n", *mode);
258 if (!rdev->constraints) {
259 rdev_err(rdev, "no constraints\n");
262 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
263 rdev_err(rdev, "operation not allowed\n");
267 /* The modes are bitmasks, the most power hungry modes having
268 * the lowest values. If the requested mode isn't supported
269 * try higher modes. */
271 if (rdev->constraints->valid_modes_mask & *mode)
279 /* dynamic regulator mode switching constraint check */
280 static int regulator_check_drms(struct regulator_dev *rdev)
282 if (!rdev->constraints) {
283 rdev_err(rdev, "no constraints\n");
286 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
287 rdev_err(rdev, "operation not allowed\n");
293 static ssize_t device_requested_uA_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct regulator *regulator;
298 regulator = get_device_regulator(dev);
299 if (regulator == NULL)
302 return sprintf(buf, "%d\n", regulator->uA_load);
305 static ssize_t regulator_uV_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct regulator_dev *rdev = dev_get_drvdata(dev);
311 mutex_lock(&rdev->mutex);
312 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
313 mutex_unlock(&rdev->mutex);
317 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
319 static ssize_t regulator_uA_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
322 struct regulator_dev *rdev = dev_get_drvdata(dev);
324 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
326 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
328 static ssize_t regulator_name_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
331 struct regulator_dev *rdev = dev_get_drvdata(dev);
333 return sprintf(buf, "%s\n", rdev_get_name(rdev));
336 static ssize_t regulator_print_opmode(char *buf, int mode)
339 case REGULATOR_MODE_FAST:
340 return sprintf(buf, "fast\n");
341 case REGULATOR_MODE_NORMAL:
342 return sprintf(buf, "normal\n");
343 case REGULATOR_MODE_IDLE:
344 return sprintf(buf, "idle\n");
345 case REGULATOR_MODE_STANDBY:
346 return sprintf(buf, "standby\n");
348 return sprintf(buf, "unknown\n");
351 static ssize_t regulator_opmode_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
354 struct regulator_dev *rdev = dev_get_drvdata(dev);
356 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
358 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
360 static ssize_t regulator_print_state(char *buf, int state)
363 return sprintf(buf, "enabled\n");
365 return sprintf(buf, "disabled\n");
367 return sprintf(buf, "unknown\n");
370 static ssize_t regulator_state_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
373 struct regulator_dev *rdev = dev_get_drvdata(dev);
376 mutex_lock(&rdev->mutex);
377 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
378 mutex_unlock(&rdev->mutex);
382 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
384 static ssize_t regulator_status_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
387 struct regulator_dev *rdev = dev_get_drvdata(dev);
391 status = rdev->desc->ops->get_status(rdev);
396 case REGULATOR_STATUS_OFF:
399 case REGULATOR_STATUS_ON:
402 case REGULATOR_STATUS_ERROR:
405 case REGULATOR_STATUS_FAST:
408 case REGULATOR_STATUS_NORMAL:
411 case REGULATOR_STATUS_IDLE:
414 case REGULATOR_STATUS_STANDBY:
421 return sprintf(buf, "%s\n", label);
423 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
425 static ssize_t regulator_min_uA_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
428 struct regulator_dev *rdev = dev_get_drvdata(dev);
430 if (!rdev->constraints)
431 return sprintf(buf, "constraint not defined\n");
433 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
435 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
437 static ssize_t regulator_max_uA_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
440 struct regulator_dev *rdev = dev_get_drvdata(dev);
442 if (!rdev->constraints)
443 return sprintf(buf, "constraint not defined\n");
445 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
447 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
449 static ssize_t regulator_min_uV_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
452 struct regulator_dev *rdev = dev_get_drvdata(dev);
454 if (!rdev->constraints)
455 return sprintf(buf, "constraint not defined\n");
457 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
459 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
461 static ssize_t regulator_max_uV_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
464 struct regulator_dev *rdev = dev_get_drvdata(dev);
466 if (!rdev->constraints)
467 return sprintf(buf, "constraint not defined\n");
469 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
471 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
473 static ssize_t regulator_total_uA_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
476 struct regulator_dev *rdev = dev_get_drvdata(dev);
477 struct regulator *regulator;
480 mutex_lock(&rdev->mutex);
481 list_for_each_entry(regulator, &rdev->consumer_list, list)
482 uA += regulator->uA_load;
483 mutex_unlock(&rdev->mutex);
484 return sprintf(buf, "%d\n", uA);
486 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
488 static ssize_t regulator_num_users_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
491 struct regulator_dev *rdev = dev_get_drvdata(dev);
492 return sprintf(buf, "%d\n", rdev->use_count);
495 static ssize_t regulator_type_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
498 struct regulator_dev *rdev = dev_get_drvdata(dev);
500 switch (rdev->desc->type) {
501 case REGULATOR_VOLTAGE:
502 return sprintf(buf, "voltage\n");
503 case REGULATOR_CURRENT:
504 return sprintf(buf, "current\n");
506 return sprintf(buf, "unknown\n");
509 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
512 struct regulator_dev *rdev = dev_get_drvdata(dev);
514 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
516 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
517 regulator_suspend_mem_uV_show, NULL);
519 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
522 struct regulator_dev *rdev = dev_get_drvdata(dev);
524 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
526 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
527 regulator_suspend_disk_uV_show, NULL);
529 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
532 struct regulator_dev *rdev = dev_get_drvdata(dev);
534 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
536 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
537 regulator_suspend_standby_uV_show, NULL);
539 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
540 struct device_attribute *attr, char *buf)
542 struct regulator_dev *rdev = dev_get_drvdata(dev);
544 return regulator_print_opmode(buf,
545 rdev->constraints->state_mem.mode);
547 static DEVICE_ATTR(suspend_mem_mode, 0444,
548 regulator_suspend_mem_mode_show, NULL);
550 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
551 struct device_attribute *attr, char *buf)
553 struct regulator_dev *rdev = dev_get_drvdata(dev);
555 return regulator_print_opmode(buf,
556 rdev->constraints->state_disk.mode);
558 static DEVICE_ATTR(suspend_disk_mode, 0444,
559 regulator_suspend_disk_mode_show, NULL);
561 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
562 struct device_attribute *attr, char *buf)
564 struct regulator_dev *rdev = dev_get_drvdata(dev);
566 return regulator_print_opmode(buf,
567 rdev->constraints->state_standby.mode);
569 static DEVICE_ATTR(suspend_standby_mode, 0444,
570 regulator_suspend_standby_mode_show, NULL);
572 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
575 struct regulator_dev *rdev = dev_get_drvdata(dev);
577 return regulator_print_state(buf,
578 rdev->constraints->state_mem.enabled);
580 static DEVICE_ATTR(suspend_mem_state, 0444,
581 regulator_suspend_mem_state_show, NULL);
583 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct regulator_dev *rdev = dev_get_drvdata(dev);
588 return regulator_print_state(buf,
589 rdev->constraints->state_disk.enabled);
591 static DEVICE_ATTR(suspend_disk_state, 0444,
592 regulator_suspend_disk_state_show, NULL);
594 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
597 struct regulator_dev *rdev = dev_get_drvdata(dev);
599 return regulator_print_state(buf,
600 rdev->constraints->state_standby.enabled);
602 static DEVICE_ATTR(suspend_standby_state, 0444,
603 regulator_suspend_standby_state_show, NULL);
607 * These are the only attributes are present for all regulators.
608 * Other attributes are a function of regulator functionality.
610 static struct device_attribute regulator_dev_attrs[] = {
611 __ATTR(name, 0444, regulator_name_show, NULL),
612 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
613 __ATTR(type, 0444, regulator_type_show, NULL),
617 static void regulator_dev_release(struct device *dev)
619 struct regulator_dev *rdev = dev_get_drvdata(dev);
623 static struct class regulator_class = {
625 .dev_release = regulator_dev_release,
626 .dev_attrs = regulator_dev_attrs,
629 /* Calculate the new optimum regulator operating mode based on the new total
630 * consumer load. All locks held by caller */
631 static void drms_uA_update(struct regulator_dev *rdev)
633 struct regulator *sibling;
634 int current_uA = 0, output_uV, input_uV, err;
637 err = regulator_check_drms(rdev);
638 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
639 (!rdev->desc->ops->get_voltage &&
640 !rdev->desc->ops->get_voltage_sel) ||
641 !rdev->desc->ops->set_mode)
644 /* get output voltage */
645 output_uV = _regulator_get_voltage(rdev);
649 /* get input voltage */
652 input_uV = _regulator_get_voltage(rdev);
654 input_uV = rdev->constraints->input_uV;
658 /* calc total requested load */
659 list_for_each_entry(sibling, &rdev->consumer_list, list)
660 current_uA += sibling->uA_load;
662 /* now get the optimum mode for our new total regulator load */
663 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
664 output_uV, current_uA);
666 /* check the new mode is allowed */
667 err = regulator_mode_constrain(rdev, &mode);
669 rdev->desc->ops->set_mode(rdev, mode);
672 static int suspend_set_state(struct regulator_dev *rdev,
673 struct regulator_state *rstate)
678 can_set_state = rdev->desc->ops->set_suspend_enable &&
679 rdev->desc->ops->set_suspend_disable;
681 /* If we have no suspend mode configration don't set anything;
682 * only warn if the driver actually makes the suspend mode
685 if (!rstate->enabled && !rstate->disabled) {
687 rdev_warn(rdev, "No configuration\n");
691 if (rstate->enabled && rstate->disabled) {
692 rdev_err(rdev, "invalid configuration\n");
696 if (!can_set_state) {
697 rdev_err(rdev, "no way to set suspend state\n");
702 ret = rdev->desc->ops->set_suspend_enable(rdev);
704 ret = rdev->desc->ops->set_suspend_disable(rdev);
706 rdev_err(rdev, "failed to enabled/disable\n");
710 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
711 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
713 rdev_err(rdev, "failed to set voltage\n");
718 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
719 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
721 rdev_err(rdev, "failed to set mode\n");
728 /* locks held by caller */
729 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
731 if (!rdev->constraints)
735 case PM_SUSPEND_STANDBY:
736 return suspend_set_state(rdev,
737 &rdev->constraints->state_standby);
739 return suspend_set_state(rdev,
740 &rdev->constraints->state_mem);
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_disk);
749 static void print_constraints(struct regulator_dev *rdev)
751 struct regulation_constraints *constraints = rdev->constraints;
756 if (constraints->min_uV && constraints->max_uV) {
757 if (constraints->min_uV == constraints->max_uV)
758 count += sprintf(buf + count, "%d mV ",
759 constraints->min_uV / 1000);
761 count += sprintf(buf + count, "%d <--> %d mV ",
762 constraints->min_uV / 1000,
763 constraints->max_uV / 1000);
766 if (!constraints->min_uV ||
767 constraints->min_uV != constraints->max_uV) {
768 ret = _regulator_get_voltage(rdev);
770 count += sprintf(buf + count, "at %d mV ", ret / 1000);
773 if (constraints->uV_offset)
774 count += sprintf(buf, "%dmV offset ",
775 constraints->uV_offset / 1000);
777 if (constraints->min_uA && constraints->max_uA) {
778 if (constraints->min_uA == constraints->max_uA)
779 count += sprintf(buf + count, "%d mA ",
780 constraints->min_uA / 1000);
782 count += sprintf(buf + count, "%d <--> %d mA ",
783 constraints->min_uA / 1000,
784 constraints->max_uA / 1000);
787 if (!constraints->min_uA ||
788 constraints->min_uA != constraints->max_uA) {
789 ret = _regulator_get_current_limit(rdev);
791 count += sprintf(buf + count, "at %d mA ", ret / 1000);
794 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
795 count += sprintf(buf + count, "fast ");
796 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
797 count += sprintf(buf + count, "normal ");
798 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
799 count += sprintf(buf + count, "idle ");
800 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
801 count += sprintf(buf + count, "standby");
803 rdev_info(rdev, "%s\n", buf);
805 if ((constraints->min_uV != constraints->max_uV) &&
806 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
808 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
811 static int machine_constraints_voltage(struct regulator_dev *rdev,
812 struct regulation_constraints *constraints)
814 struct regulator_ops *ops = rdev->desc->ops;
817 /* do we need to apply the constraint voltage */
818 if (rdev->constraints->apply_uV &&
819 rdev->constraints->min_uV == rdev->constraints->max_uV) {
820 ret = _regulator_do_set_voltage(rdev,
821 rdev->constraints->min_uV,
822 rdev->constraints->max_uV);
824 rdev_err(rdev, "failed to apply %duV constraint\n",
825 rdev->constraints->min_uV);
830 /* constrain machine-level voltage specs to fit
831 * the actual range supported by this regulator.
833 if (ops->list_voltage && rdev->desc->n_voltages) {
834 int count = rdev->desc->n_voltages;
836 int min_uV = INT_MAX;
837 int max_uV = INT_MIN;
838 int cmin = constraints->min_uV;
839 int cmax = constraints->max_uV;
841 /* it's safe to autoconfigure fixed-voltage supplies
842 and the constraints are used by list_voltage. */
843 if (count == 1 && !cmin) {
846 constraints->min_uV = cmin;
847 constraints->max_uV = cmax;
850 /* voltage constraints are optional */
851 if ((cmin == 0) && (cmax == 0))
854 /* else require explicit machine-level constraints */
855 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
856 rdev_err(rdev, "invalid voltage constraints\n");
860 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
861 for (i = 0; i < count; i++) {
864 value = ops->list_voltage(rdev, i);
868 /* maybe adjust [min_uV..max_uV] */
869 if (value >= cmin && value < min_uV)
871 if (value <= cmax && value > max_uV)
875 /* final: [min_uV..max_uV] valid iff constraints valid */
876 if (max_uV < min_uV) {
877 rdev_err(rdev, "unsupportable voltage constraints\n");
881 /* use regulator's subset of machine constraints */
882 if (constraints->min_uV < min_uV) {
883 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
884 constraints->min_uV, min_uV);
885 constraints->min_uV = min_uV;
887 if (constraints->max_uV > max_uV) {
888 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
889 constraints->max_uV, max_uV);
890 constraints->max_uV = max_uV;
898 * set_machine_constraints - sets regulator constraints
899 * @rdev: regulator source
900 * @constraints: constraints to apply
902 * Allows platform initialisation code to define and constrain
903 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
904 * Constraints *must* be set by platform code in order for some
905 * regulator operations to proceed i.e. set_voltage, set_current_limit,
908 static int set_machine_constraints(struct regulator_dev *rdev,
909 const struct regulation_constraints *constraints)
912 struct regulator_ops *ops = rdev->desc->ops;
915 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
918 rdev->constraints = kzalloc(sizeof(*constraints),
920 if (!rdev->constraints)
923 ret = machine_constraints_voltage(rdev, rdev->constraints);
927 /* do we need to setup our suspend state */
928 if (rdev->constraints->initial_state) {
929 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
931 rdev_err(rdev, "failed to set suspend state\n");
936 if (rdev->constraints->initial_mode) {
937 if (!ops->set_mode) {
938 rdev_err(rdev, "no set_mode operation\n");
943 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
945 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
950 /* If the constraints say the regulator should be on at this point
951 * and we have control then make sure it is enabled.
953 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
955 ret = ops->enable(rdev);
957 rdev_err(rdev, "failed to enable\n");
962 print_constraints(rdev);
965 kfree(rdev->constraints);
966 rdev->constraints = NULL;
971 * set_supply - set regulator supply regulator
972 * @rdev: regulator name
973 * @supply_rdev: supply regulator name
975 * Called by platform initialisation code to set the supply regulator for this
976 * regulator. This ensures that a regulators supply will also be enabled by the
977 * core if it's child is enabled.
979 static int set_supply(struct regulator_dev *rdev,
980 struct regulator_dev *supply_rdev)
984 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
986 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
987 if (rdev->supply == NULL) {
996 * set_consumer_device_supply - Bind a regulator to a symbolic supply
997 * @rdev: regulator source
998 * @consumer_dev_name: dev_name() string for device supply applies to
999 * @supply: symbolic name for supply
1001 * Allows platform initialisation code to map physical regulator
1002 * sources to symbolic names for supplies for use by devices. Devices
1003 * should use these symbolic names to request regulators, avoiding the
1004 * need to provide board-specific regulator names as platform data.
1006 static int set_consumer_device_supply(struct regulator_dev *rdev,
1007 const char *consumer_dev_name,
1010 struct regulator_map *node;
1016 if (consumer_dev_name != NULL)
1021 list_for_each_entry(node, ®ulator_map_list, list) {
1022 if (node->dev_name && consumer_dev_name) {
1023 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1025 } else if (node->dev_name || consumer_dev_name) {
1029 if (strcmp(node->supply, supply) != 0)
1032 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1034 dev_name(&node->regulator->dev),
1035 node->regulator->desc->name,
1037 dev_name(&rdev->dev), rdev_get_name(rdev));
1041 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1045 node->regulator = rdev;
1046 node->supply = supply;
1049 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1050 if (node->dev_name == NULL) {
1056 list_add(&node->list, ®ulator_map_list);
1060 static void unset_regulator_supplies(struct regulator_dev *rdev)
1062 struct regulator_map *node, *n;
1064 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1065 if (rdev == node->regulator) {
1066 list_del(&node->list);
1067 kfree(node->dev_name);
1073 #define REG_STR_SIZE 64
1075 static struct regulator *create_regulator(struct regulator_dev *rdev,
1077 const char *supply_name)
1079 struct regulator *regulator;
1080 char buf[REG_STR_SIZE];
1083 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1084 if (regulator == NULL)
1087 mutex_lock(&rdev->mutex);
1088 regulator->rdev = rdev;
1089 list_add(®ulator->list, &rdev->consumer_list);
1092 /* create a 'requested_microamps_name' sysfs entry */
1093 size = scnprintf(buf, REG_STR_SIZE,
1094 "microamps_requested_%s-%s",
1095 dev_name(dev), supply_name);
1096 if (size >= REG_STR_SIZE)
1099 regulator->dev = dev;
1100 sysfs_attr_init(®ulator->dev_attr.attr);
1101 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1102 if (regulator->dev_attr.attr.name == NULL)
1105 regulator->dev_attr.attr.mode = 0444;
1106 regulator->dev_attr.show = device_requested_uA_show;
1107 err = device_create_file(dev, ®ulator->dev_attr);
1109 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1113 /* also add a link to the device sysfs entry */
1114 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1115 dev->kobj.name, supply_name);
1116 if (size >= REG_STR_SIZE)
1119 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1120 if (regulator->supply_name == NULL)
1123 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1126 rdev_warn(rdev, "could not add device link %s err %d\n",
1127 dev->kobj.name, err);
1131 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1132 if (regulator->supply_name == NULL)
1136 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1138 if (!regulator->debugfs) {
1139 rdev_warn(rdev, "Failed to create debugfs directory\n");
1141 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1142 ®ulator->uA_load);
1143 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1144 ®ulator->min_uV);
1145 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1146 ®ulator->max_uV);
1149 mutex_unlock(&rdev->mutex);
1152 kfree(regulator->supply_name);
1154 device_remove_file(regulator->dev, ®ulator->dev_attr);
1156 kfree(regulator->dev_attr.attr.name);
1158 list_del(®ulator->list);
1160 mutex_unlock(&rdev->mutex);
1164 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1166 if (!rdev->desc->ops->enable_time)
1168 return rdev->desc->ops->enable_time(rdev);
1171 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1174 struct regulator_dev *r;
1175 struct device_node *node;
1177 /* first do a dt based lookup */
1178 if (dev && dev->of_node) {
1179 node = of_get_regulator(dev, supply);
1181 list_for_each_entry(r, ®ulator_list, list)
1182 if (r->dev.parent &&
1183 node == r->dev.of_node)
1187 /* if not found, try doing it non-dt way */
1188 list_for_each_entry(r, ®ulator_list, list)
1189 if (strcmp(rdev_get_name(r), supply) == 0)
1195 /* Internal regulator request function */
1196 static struct regulator *_regulator_get(struct device *dev, const char *id,
1199 struct regulator_dev *rdev;
1200 struct regulator_map *map;
1201 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1202 const char *devname = NULL;
1206 pr_err("get() with no identifier\n");
1211 devname = dev_name(dev);
1213 mutex_lock(®ulator_list_mutex);
1215 rdev = regulator_dev_lookup(dev, id);
1219 list_for_each_entry(map, ®ulator_map_list, list) {
1220 /* If the mapping has a device set up it must match */
1221 if (map->dev_name &&
1222 (!devname || strcmp(map->dev_name, devname)))
1225 if (strcmp(map->supply, id) == 0) {
1226 rdev = map->regulator;
1231 if (board_wants_dummy_regulator) {
1232 rdev = dummy_regulator_rdev;
1236 #ifdef CONFIG_REGULATOR_DUMMY
1238 devname = "deviceless";
1240 /* If the board didn't flag that it was fully constrained then
1241 * substitute in a dummy regulator so consumers can continue.
1243 if (!has_full_constraints) {
1244 pr_warn("%s supply %s not found, using dummy regulator\n",
1246 rdev = dummy_regulator_rdev;
1251 mutex_unlock(®ulator_list_mutex);
1255 if (rdev->exclusive) {
1256 regulator = ERR_PTR(-EPERM);
1260 if (exclusive && rdev->open_count) {
1261 regulator = ERR_PTR(-EBUSY);
1265 if (!try_module_get(rdev->owner))
1268 regulator = create_regulator(rdev, dev, id);
1269 if (regulator == NULL) {
1270 regulator = ERR_PTR(-ENOMEM);
1271 module_put(rdev->owner);
1277 rdev->exclusive = 1;
1279 ret = _regulator_is_enabled(rdev);
1281 rdev->use_count = 1;
1283 rdev->use_count = 0;
1287 mutex_unlock(®ulator_list_mutex);
1293 * regulator_get - lookup and obtain a reference to a regulator.
1294 * @dev: device for regulator "consumer"
1295 * @id: Supply name or regulator ID.
1297 * Returns a struct regulator corresponding to the regulator producer,
1298 * or IS_ERR() condition containing errno.
1300 * Use of supply names configured via regulator_set_device_supply() is
1301 * strongly encouraged. It is recommended that the supply name used
1302 * should match the name used for the supply and/or the relevant
1303 * device pins in the datasheet.
1305 struct regulator *regulator_get(struct device *dev, const char *id)
1307 return _regulator_get(dev, id, 0);
1309 EXPORT_SYMBOL_GPL(regulator_get);
1311 static void devm_regulator_release(struct device *dev, void *res)
1313 regulator_put(*(struct regulator **)res);
1317 * devm_regulator_get - Resource managed regulator_get()
1318 * @dev: device for regulator "consumer"
1319 * @id: Supply name or regulator ID.
1321 * Managed regulator_get(). Regulators returned from this function are
1322 * automatically regulator_put() on driver detach. See regulator_get() for more
1325 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1327 struct regulator **ptr, *regulator;
1329 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1331 return ERR_PTR(-ENOMEM);
1333 regulator = regulator_get(dev, id);
1334 if (!IS_ERR(regulator)) {
1336 devres_add(dev, ptr);
1343 EXPORT_SYMBOL_GPL(devm_regulator_get);
1346 * regulator_get_exclusive - obtain exclusive access to a regulator.
1347 * @dev: device for regulator "consumer"
1348 * @id: Supply name or regulator ID.
1350 * Returns a struct regulator corresponding to the regulator producer,
1351 * or IS_ERR() condition containing errno. Other consumers will be
1352 * unable to obtain this reference is held and the use count for the
1353 * regulator will be initialised to reflect the current state of the
1356 * This is intended for use by consumers which cannot tolerate shared
1357 * use of the regulator such as those which need to force the
1358 * regulator off for correct operation of the hardware they are
1361 * Use of supply names configured via regulator_set_device_supply() is
1362 * strongly encouraged. It is recommended that the supply name used
1363 * should match the name used for the supply and/or the relevant
1364 * device pins in the datasheet.
1366 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1368 return _regulator_get(dev, id, 1);
1370 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1373 * regulator_put - "free" the regulator source
1374 * @regulator: regulator source
1376 * Note: drivers must ensure that all regulator_enable calls made on this
1377 * regulator source are balanced by regulator_disable calls prior to calling
1380 void regulator_put(struct regulator *regulator)
1382 struct regulator_dev *rdev;
1384 if (regulator == NULL || IS_ERR(regulator))
1387 mutex_lock(®ulator_list_mutex);
1388 rdev = regulator->rdev;
1390 debugfs_remove_recursive(regulator->debugfs);
1392 /* remove any sysfs entries */
1393 if (regulator->dev) {
1394 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1395 device_remove_file(regulator->dev, ®ulator->dev_attr);
1396 kfree(regulator->dev_attr.attr.name);
1398 kfree(regulator->supply_name);
1399 list_del(®ulator->list);
1403 rdev->exclusive = 0;
1405 module_put(rdev->owner);
1406 mutex_unlock(®ulator_list_mutex);
1408 EXPORT_SYMBOL_GPL(regulator_put);
1410 static int devm_regulator_match(struct device *dev, void *res, void *data)
1412 struct regulator **r = res;
1421 * devm_regulator_put - Resource managed regulator_put()
1422 * @regulator: regulator to free
1424 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1425 * this function will not need to be called and the resource management
1426 * code will ensure that the resource is freed.
1428 void devm_regulator_put(struct regulator *regulator)
1432 rc = devres_destroy(regulator->dev, devm_regulator_release,
1433 devm_regulator_match, regulator);
1436 EXPORT_SYMBOL_GPL(devm_regulator_put);
1438 static int _regulator_can_change_status(struct regulator_dev *rdev)
1440 if (!rdev->constraints)
1443 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1449 /* locks held by regulator_enable() */
1450 static int _regulator_enable(struct regulator_dev *rdev)
1454 /* check voltage and requested load before enabling */
1455 if (rdev->constraints &&
1456 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1457 drms_uA_update(rdev);
1459 if (rdev->use_count == 0) {
1460 /* The regulator may on if it's not switchable or left on */
1461 ret = _regulator_is_enabled(rdev);
1462 if (ret == -EINVAL || ret == 0) {
1463 if (!_regulator_can_change_status(rdev))
1466 if (!rdev->desc->ops->enable)
1469 /* Query before enabling in case configuration
1471 ret = _regulator_get_enable_time(rdev);
1475 rdev_warn(rdev, "enable_time() failed: %d\n",
1480 trace_regulator_enable(rdev_get_name(rdev));
1482 /* Allow the regulator to ramp; it would be useful
1483 * to extend this for bulk operations so that the
1484 * regulators can ramp together. */
1485 ret = rdev->desc->ops->enable(rdev);
1489 trace_regulator_enable_delay(rdev_get_name(rdev));
1491 if (delay >= 1000) {
1492 mdelay(delay / 1000);
1493 udelay(delay % 1000);
1498 trace_regulator_enable_complete(rdev_get_name(rdev));
1500 } else if (ret < 0) {
1501 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1504 /* Fallthrough on positive return values - already enabled */
1513 * regulator_enable - enable regulator output
1514 * @regulator: regulator source
1516 * Request that the regulator be enabled with the regulator output at
1517 * the predefined voltage or current value. Calls to regulator_enable()
1518 * must be balanced with calls to regulator_disable().
1520 * NOTE: the output value can be set by other drivers, boot loader or may be
1521 * hardwired in the regulator.
1523 int regulator_enable(struct regulator *regulator)
1525 struct regulator_dev *rdev = regulator->rdev;
1529 ret = regulator_enable(rdev->supply);
1534 mutex_lock(&rdev->mutex);
1535 ret = _regulator_enable(rdev);
1536 mutex_unlock(&rdev->mutex);
1538 if (ret != 0 && rdev->supply)
1539 regulator_disable(rdev->supply);
1543 EXPORT_SYMBOL_GPL(regulator_enable);
1545 /* locks held by regulator_disable() */
1546 static int _regulator_disable(struct regulator_dev *rdev)
1550 if (WARN(rdev->use_count <= 0,
1551 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1554 /* are we the last user and permitted to disable ? */
1555 if (rdev->use_count == 1 &&
1556 (rdev->constraints && !rdev->constraints->always_on)) {
1558 /* we are last user */
1559 if (_regulator_can_change_status(rdev) &&
1560 rdev->desc->ops->disable) {
1561 trace_regulator_disable(rdev_get_name(rdev));
1563 ret = rdev->desc->ops->disable(rdev);
1565 rdev_err(rdev, "failed to disable\n");
1569 trace_regulator_disable_complete(rdev_get_name(rdev));
1571 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1575 rdev->use_count = 0;
1576 } else if (rdev->use_count > 1) {
1578 if (rdev->constraints &&
1579 (rdev->constraints->valid_ops_mask &
1580 REGULATOR_CHANGE_DRMS))
1581 drms_uA_update(rdev);
1590 * regulator_disable - disable regulator output
1591 * @regulator: regulator source
1593 * Disable the regulator output voltage or current. Calls to
1594 * regulator_enable() must be balanced with calls to
1595 * regulator_disable().
1597 * NOTE: this will only disable the regulator output if no other consumer
1598 * devices have it enabled, the regulator device supports disabling and
1599 * machine constraints permit this operation.
1601 int regulator_disable(struct regulator *regulator)
1603 struct regulator_dev *rdev = regulator->rdev;
1606 mutex_lock(&rdev->mutex);
1607 ret = _regulator_disable(rdev);
1608 mutex_unlock(&rdev->mutex);
1610 if (ret == 0 && rdev->supply)
1611 regulator_disable(rdev->supply);
1615 EXPORT_SYMBOL_GPL(regulator_disable);
1617 /* locks held by regulator_force_disable() */
1618 static int _regulator_force_disable(struct regulator_dev *rdev)
1623 if (rdev->desc->ops->disable) {
1624 /* ah well, who wants to live forever... */
1625 ret = rdev->desc->ops->disable(rdev);
1627 rdev_err(rdev, "failed to force disable\n");
1630 /* notify other consumers that power has been forced off */
1631 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1632 REGULATOR_EVENT_DISABLE, NULL);
1639 * regulator_force_disable - force disable regulator output
1640 * @regulator: regulator source
1642 * Forcibly disable the regulator output voltage or current.
1643 * NOTE: this *will* disable the regulator output even if other consumer
1644 * devices have it enabled. This should be used for situations when device
1645 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1647 int regulator_force_disable(struct regulator *regulator)
1649 struct regulator_dev *rdev = regulator->rdev;
1652 mutex_lock(&rdev->mutex);
1653 regulator->uA_load = 0;
1654 ret = _regulator_force_disable(regulator->rdev);
1655 mutex_unlock(&rdev->mutex);
1658 while (rdev->open_count--)
1659 regulator_disable(rdev->supply);
1663 EXPORT_SYMBOL_GPL(regulator_force_disable);
1665 static void regulator_disable_work(struct work_struct *work)
1667 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1671 mutex_lock(&rdev->mutex);
1673 BUG_ON(!rdev->deferred_disables);
1675 count = rdev->deferred_disables;
1676 rdev->deferred_disables = 0;
1678 for (i = 0; i < count; i++) {
1679 ret = _regulator_disable(rdev);
1681 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1684 mutex_unlock(&rdev->mutex);
1687 for (i = 0; i < count; i++) {
1688 ret = regulator_disable(rdev->supply);
1691 "Supply disable failed: %d\n", ret);
1698 * regulator_disable_deferred - disable regulator output with delay
1699 * @regulator: regulator source
1700 * @ms: miliseconds until the regulator is disabled
1702 * Execute regulator_disable() on the regulator after a delay. This
1703 * is intended for use with devices that require some time to quiesce.
1705 * NOTE: this will only disable the regulator output if no other consumer
1706 * devices have it enabled, the regulator device supports disabling and
1707 * machine constraints permit this operation.
1709 int regulator_disable_deferred(struct regulator *regulator, int ms)
1711 struct regulator_dev *rdev = regulator->rdev;
1714 mutex_lock(&rdev->mutex);
1715 rdev->deferred_disables++;
1716 mutex_unlock(&rdev->mutex);
1718 ret = schedule_delayed_work(&rdev->disable_work,
1719 msecs_to_jiffies(ms));
1725 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1727 static int _regulator_is_enabled(struct regulator_dev *rdev)
1729 /* If we don't know then assume that the regulator is always on */
1730 if (!rdev->desc->ops->is_enabled)
1733 return rdev->desc->ops->is_enabled(rdev);
1737 * regulator_is_enabled - is the regulator output enabled
1738 * @regulator: regulator source
1740 * Returns positive if the regulator driver backing the source/client
1741 * has requested that the device be enabled, zero if it hasn't, else a
1742 * negative errno code.
1744 * Note that the device backing this regulator handle can have multiple
1745 * users, so it might be enabled even if regulator_enable() was never
1746 * called for this particular source.
1748 int regulator_is_enabled(struct regulator *regulator)
1752 mutex_lock(®ulator->rdev->mutex);
1753 ret = _regulator_is_enabled(regulator->rdev);
1754 mutex_unlock(®ulator->rdev->mutex);
1758 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1761 * regulator_count_voltages - count regulator_list_voltage() selectors
1762 * @regulator: regulator source
1764 * Returns number of selectors, or negative errno. Selectors are
1765 * numbered starting at zero, and typically correspond to bitfields
1766 * in hardware registers.
1768 int regulator_count_voltages(struct regulator *regulator)
1770 struct regulator_dev *rdev = regulator->rdev;
1772 return rdev->desc->n_voltages ? : -EINVAL;
1774 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1777 * regulator_list_voltage - enumerate supported voltages
1778 * @regulator: regulator source
1779 * @selector: identify voltage to list
1780 * Context: can sleep
1782 * Returns a voltage that can be passed to @regulator_set_voltage(),
1783 * zero if this selector code can't be used on this system, or a
1786 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1788 struct regulator_dev *rdev = regulator->rdev;
1789 struct regulator_ops *ops = rdev->desc->ops;
1792 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1795 mutex_lock(&rdev->mutex);
1796 ret = ops->list_voltage(rdev, selector);
1797 mutex_unlock(&rdev->mutex);
1800 if (ret < rdev->constraints->min_uV)
1802 else if (ret > rdev->constraints->max_uV)
1808 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1811 * regulator_is_supported_voltage - check if a voltage range can be supported
1813 * @regulator: Regulator to check.
1814 * @min_uV: Minimum required voltage in uV.
1815 * @max_uV: Maximum required voltage in uV.
1817 * Returns a boolean or a negative error code.
1819 int regulator_is_supported_voltage(struct regulator *regulator,
1820 int min_uV, int max_uV)
1822 int i, voltages, ret;
1824 ret = regulator_count_voltages(regulator);
1829 for (i = 0; i < voltages; i++) {
1830 ret = regulator_list_voltage(regulator, i);
1832 if (ret >= min_uV && ret <= max_uV)
1838 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
1840 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1841 int min_uV, int max_uV)
1845 unsigned int selector;
1847 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1849 min_uV += rdev->constraints->uV_offset;
1850 max_uV += rdev->constraints->uV_offset;
1852 if (rdev->desc->ops->set_voltage) {
1853 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1856 if (rdev->desc->ops->list_voltage)
1857 selector = rdev->desc->ops->list_voltage(rdev,
1861 } else if (rdev->desc->ops->set_voltage_sel) {
1862 int best_val = INT_MAX;
1867 /* Find the smallest voltage that falls within the specified
1870 for (i = 0; i < rdev->desc->n_voltages; i++) {
1871 ret = rdev->desc->ops->list_voltage(rdev, i);
1875 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1882 * If we can't obtain the old selector there is not enough
1883 * info to call set_voltage_time_sel().
1885 if (rdev->desc->ops->set_voltage_time_sel &&
1886 rdev->desc->ops->get_voltage_sel) {
1887 unsigned int old_selector = 0;
1889 ret = rdev->desc->ops->get_voltage_sel(rdev);
1893 ret = rdev->desc->ops->set_voltage_time_sel(rdev,
1894 old_selector, selector);
1896 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
1901 if (best_val != INT_MAX) {
1902 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1903 selector = best_val;
1911 /* Insert any necessary delays */
1912 if (delay >= 1000) {
1913 mdelay(delay / 1000);
1914 udelay(delay % 1000);
1920 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1923 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1929 * regulator_set_voltage - set regulator output voltage
1930 * @regulator: regulator source
1931 * @min_uV: Minimum required voltage in uV
1932 * @max_uV: Maximum acceptable voltage in uV
1934 * Sets a voltage regulator to the desired output voltage. This can be set
1935 * during any regulator state. IOW, regulator can be disabled or enabled.
1937 * If the regulator is enabled then the voltage will change to the new value
1938 * immediately otherwise if the regulator is disabled the regulator will
1939 * output at the new voltage when enabled.
1941 * NOTE: If the regulator is shared between several devices then the lowest
1942 * request voltage that meets the system constraints will be used.
1943 * Regulator system constraints must be set for this regulator before
1944 * calling this function otherwise this call will fail.
1946 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1948 struct regulator_dev *rdev = regulator->rdev;
1951 mutex_lock(&rdev->mutex);
1953 /* If we're setting the same range as last time the change
1954 * should be a noop (some cpufreq implementations use the same
1955 * voltage for multiple frequencies, for example).
1957 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1961 if (!rdev->desc->ops->set_voltage &&
1962 !rdev->desc->ops->set_voltage_sel) {
1967 /* constraints check */
1968 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1971 regulator->min_uV = min_uV;
1972 regulator->max_uV = max_uV;
1974 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1978 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1981 mutex_unlock(&rdev->mutex);
1984 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1987 * regulator_set_voltage_time - get raise/fall time
1988 * @regulator: regulator source
1989 * @old_uV: starting voltage in microvolts
1990 * @new_uV: target voltage in microvolts
1992 * Provided with the starting and ending voltage, this function attempts to
1993 * calculate the time in microseconds required to rise or fall to this new
1996 int regulator_set_voltage_time(struct regulator *regulator,
1997 int old_uV, int new_uV)
1999 struct regulator_dev *rdev = regulator->rdev;
2000 struct regulator_ops *ops = rdev->desc->ops;
2006 /* Currently requires operations to do this */
2007 if (!ops->list_voltage || !ops->set_voltage_time_sel
2008 || !rdev->desc->n_voltages)
2011 for (i = 0; i < rdev->desc->n_voltages; i++) {
2012 /* We only look for exact voltage matches here */
2013 voltage = regulator_list_voltage(regulator, i);
2018 if (voltage == old_uV)
2020 if (voltage == new_uV)
2024 if (old_sel < 0 || new_sel < 0)
2027 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2029 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2032 * regulator_sync_voltage - re-apply last regulator output voltage
2033 * @regulator: regulator source
2035 * Re-apply the last configured voltage. This is intended to be used
2036 * where some external control source the consumer is cooperating with
2037 * has caused the configured voltage to change.
2039 int regulator_sync_voltage(struct regulator *regulator)
2041 struct regulator_dev *rdev = regulator->rdev;
2042 int ret, min_uV, max_uV;
2044 mutex_lock(&rdev->mutex);
2046 if (!rdev->desc->ops->set_voltage &&
2047 !rdev->desc->ops->set_voltage_sel) {
2052 /* This is only going to work if we've had a voltage configured. */
2053 if (!regulator->min_uV && !regulator->max_uV) {
2058 min_uV = regulator->min_uV;
2059 max_uV = regulator->max_uV;
2061 /* This should be a paranoia check... */
2062 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2066 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2070 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2073 mutex_unlock(&rdev->mutex);
2076 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2078 static int _regulator_get_voltage(struct regulator_dev *rdev)
2082 if (rdev->desc->ops->get_voltage_sel) {
2083 sel = rdev->desc->ops->get_voltage_sel(rdev);
2086 ret = rdev->desc->ops->list_voltage(rdev, sel);
2087 } else if (rdev->desc->ops->get_voltage) {
2088 ret = rdev->desc->ops->get_voltage(rdev);
2095 return ret - rdev->constraints->uV_offset;
2099 * regulator_get_voltage - get regulator output voltage
2100 * @regulator: regulator source
2102 * This returns the current regulator voltage in uV.
2104 * NOTE: If the regulator is disabled it will return the voltage value. This
2105 * function should not be used to determine regulator state.
2107 int regulator_get_voltage(struct regulator *regulator)
2111 mutex_lock(®ulator->rdev->mutex);
2113 ret = _regulator_get_voltage(regulator->rdev);
2115 mutex_unlock(®ulator->rdev->mutex);
2119 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2122 * regulator_set_current_limit - set regulator output current limit
2123 * @regulator: regulator source
2124 * @min_uA: Minimuum supported current in uA
2125 * @max_uA: Maximum supported current in uA
2127 * Sets current sink to the desired output current. This can be set during
2128 * any regulator state. IOW, regulator can be disabled or enabled.
2130 * If the regulator is enabled then the current will change to the new value
2131 * immediately otherwise if the regulator is disabled the regulator will
2132 * output at the new current when enabled.
2134 * NOTE: Regulator system constraints must be set for this regulator before
2135 * calling this function otherwise this call will fail.
2137 int regulator_set_current_limit(struct regulator *regulator,
2138 int min_uA, int max_uA)
2140 struct regulator_dev *rdev = regulator->rdev;
2143 mutex_lock(&rdev->mutex);
2146 if (!rdev->desc->ops->set_current_limit) {
2151 /* constraints check */
2152 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2156 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2158 mutex_unlock(&rdev->mutex);
2161 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2163 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2167 mutex_lock(&rdev->mutex);
2170 if (!rdev->desc->ops->get_current_limit) {
2175 ret = rdev->desc->ops->get_current_limit(rdev);
2177 mutex_unlock(&rdev->mutex);
2182 * regulator_get_current_limit - get regulator output current
2183 * @regulator: regulator source
2185 * This returns the current supplied by the specified current sink in uA.
2187 * NOTE: If the regulator is disabled it will return the current value. This
2188 * function should not be used to determine regulator state.
2190 int regulator_get_current_limit(struct regulator *regulator)
2192 return _regulator_get_current_limit(regulator->rdev);
2194 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2197 * regulator_set_mode - set regulator operating mode
2198 * @regulator: regulator source
2199 * @mode: operating mode - one of the REGULATOR_MODE constants
2201 * Set regulator operating mode to increase regulator efficiency or improve
2202 * regulation performance.
2204 * NOTE: Regulator system constraints must be set for this regulator before
2205 * calling this function otherwise this call will fail.
2207 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2209 struct regulator_dev *rdev = regulator->rdev;
2211 int regulator_curr_mode;
2213 mutex_lock(&rdev->mutex);
2216 if (!rdev->desc->ops->set_mode) {
2221 /* return if the same mode is requested */
2222 if (rdev->desc->ops->get_mode) {
2223 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2224 if (regulator_curr_mode == mode) {
2230 /* constraints check */
2231 ret = regulator_mode_constrain(rdev, &mode);
2235 ret = rdev->desc->ops->set_mode(rdev, mode);
2237 mutex_unlock(&rdev->mutex);
2240 EXPORT_SYMBOL_GPL(regulator_set_mode);
2242 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2246 mutex_lock(&rdev->mutex);
2249 if (!rdev->desc->ops->get_mode) {
2254 ret = rdev->desc->ops->get_mode(rdev);
2256 mutex_unlock(&rdev->mutex);
2261 * regulator_get_mode - get regulator operating mode
2262 * @regulator: regulator source
2264 * Get the current regulator operating mode.
2266 unsigned int regulator_get_mode(struct regulator *regulator)
2268 return _regulator_get_mode(regulator->rdev);
2270 EXPORT_SYMBOL_GPL(regulator_get_mode);
2273 * regulator_set_optimum_mode - set regulator optimum operating mode
2274 * @regulator: regulator source
2275 * @uA_load: load current
2277 * Notifies the regulator core of a new device load. This is then used by
2278 * DRMS (if enabled by constraints) to set the most efficient regulator
2279 * operating mode for the new regulator loading.
2281 * Consumer devices notify their supply regulator of the maximum power
2282 * they will require (can be taken from device datasheet in the power
2283 * consumption tables) when they change operational status and hence power
2284 * state. Examples of operational state changes that can affect power
2285 * consumption are :-
2287 * o Device is opened / closed.
2288 * o Device I/O is about to begin or has just finished.
2289 * o Device is idling in between work.
2291 * This information is also exported via sysfs to userspace.
2293 * DRMS will sum the total requested load on the regulator and change
2294 * to the most efficient operating mode if platform constraints allow.
2296 * Returns the new regulator mode or error.
2298 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2300 struct regulator_dev *rdev = regulator->rdev;
2301 struct regulator *consumer;
2302 int ret, output_uV, input_uV, total_uA_load = 0;
2305 mutex_lock(&rdev->mutex);
2308 * first check to see if we can set modes at all, otherwise just
2309 * tell the consumer everything is OK.
2311 regulator->uA_load = uA_load;
2312 ret = regulator_check_drms(rdev);
2318 if (!rdev->desc->ops->get_optimum_mode)
2322 * we can actually do this so any errors are indicators of
2323 * potential real failure.
2327 /* get output voltage */
2328 output_uV = _regulator_get_voltage(rdev);
2329 if (output_uV <= 0) {
2330 rdev_err(rdev, "invalid output voltage found\n");
2334 /* get input voltage */
2337 input_uV = regulator_get_voltage(rdev->supply);
2339 input_uV = rdev->constraints->input_uV;
2340 if (input_uV <= 0) {
2341 rdev_err(rdev, "invalid input voltage found\n");
2345 /* calc total requested load for this regulator */
2346 list_for_each_entry(consumer, &rdev->consumer_list, list)
2347 total_uA_load += consumer->uA_load;
2349 mode = rdev->desc->ops->get_optimum_mode(rdev,
2350 input_uV, output_uV,
2352 ret = regulator_mode_constrain(rdev, &mode);
2354 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2355 total_uA_load, input_uV, output_uV);
2359 ret = rdev->desc->ops->set_mode(rdev, mode);
2361 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2366 mutex_unlock(&rdev->mutex);
2369 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2372 * regulator_register_notifier - register regulator event notifier
2373 * @regulator: regulator source
2374 * @nb: notifier block
2376 * Register notifier block to receive regulator events.
2378 int regulator_register_notifier(struct regulator *regulator,
2379 struct notifier_block *nb)
2381 return blocking_notifier_chain_register(®ulator->rdev->notifier,
2384 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2387 * regulator_unregister_notifier - unregister regulator event notifier
2388 * @regulator: regulator source
2389 * @nb: notifier block
2391 * Unregister regulator event notifier block.
2393 int regulator_unregister_notifier(struct regulator *regulator,
2394 struct notifier_block *nb)
2396 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
2399 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2401 /* notify regulator consumers and downstream regulator consumers.
2402 * Note mutex must be held by caller.
2404 static void _notifier_call_chain(struct regulator_dev *rdev,
2405 unsigned long event, void *data)
2407 /* call rdev chain first */
2408 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
2412 * regulator_bulk_get - get multiple regulator consumers
2414 * @dev: Device to supply
2415 * @num_consumers: Number of consumers to register
2416 * @consumers: Configuration of consumers; clients are stored here.
2418 * @return 0 on success, an errno on failure.
2420 * This helper function allows drivers to get several regulator
2421 * consumers in one operation. If any of the regulators cannot be
2422 * acquired then any regulators that were allocated will be freed
2423 * before returning to the caller.
2425 int regulator_bulk_get(struct device *dev, int num_consumers,
2426 struct regulator_bulk_data *consumers)
2431 for (i = 0; i < num_consumers; i++)
2432 consumers[i].consumer = NULL;
2434 for (i = 0; i < num_consumers; i++) {
2435 consumers[i].consumer = regulator_get(dev,
2436 consumers[i].supply);
2437 if (IS_ERR(consumers[i].consumer)) {
2438 ret = PTR_ERR(consumers[i].consumer);
2439 dev_err(dev, "Failed to get supply '%s': %d\n",
2440 consumers[i].supply, ret);
2441 consumers[i].consumer = NULL;
2450 regulator_put(consumers[i].consumer);
2454 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2457 * devm_regulator_bulk_get - managed get multiple regulator consumers
2459 * @dev: Device to supply
2460 * @num_consumers: Number of consumers to register
2461 * @consumers: Configuration of consumers; clients are stored here.
2463 * @return 0 on success, an errno on failure.
2465 * This helper function allows drivers to get several regulator
2466 * consumers in one operation with management, the regulators will
2467 * automatically be freed when the device is unbound. If any of the
2468 * regulators cannot be acquired then any regulators that were
2469 * allocated will be freed before returning to the caller.
2471 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2472 struct regulator_bulk_data *consumers)
2477 for (i = 0; i < num_consumers; i++)
2478 consumers[i].consumer = NULL;
2480 for (i = 0; i < num_consumers; i++) {
2481 consumers[i].consumer = devm_regulator_get(dev,
2482 consumers[i].supply);
2483 if (IS_ERR(consumers[i].consumer)) {
2484 ret = PTR_ERR(consumers[i].consumer);
2485 dev_err(dev, "Failed to get supply '%s': %d\n",
2486 consumers[i].supply, ret);
2487 consumers[i].consumer = NULL;
2495 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2496 devm_regulator_put(consumers[i].consumer);
2500 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2502 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2504 struct regulator_bulk_data *bulk = data;
2506 bulk->ret = regulator_enable(bulk->consumer);
2510 * regulator_bulk_enable - enable multiple regulator consumers
2512 * @num_consumers: Number of consumers
2513 * @consumers: Consumer data; clients are stored here.
2514 * @return 0 on success, an errno on failure
2516 * This convenience API allows consumers to enable multiple regulator
2517 * clients in a single API call. If any consumers cannot be enabled
2518 * then any others that were enabled will be disabled again prior to
2521 int regulator_bulk_enable(int num_consumers,
2522 struct regulator_bulk_data *consumers)
2524 LIST_HEAD(async_domain);
2528 for (i = 0; i < num_consumers; i++)
2529 async_schedule_domain(regulator_bulk_enable_async,
2530 &consumers[i], &async_domain);
2532 async_synchronize_full_domain(&async_domain);
2534 /* If any consumer failed we need to unwind any that succeeded */
2535 for (i = 0; i < num_consumers; i++) {
2536 if (consumers[i].ret != 0) {
2537 ret = consumers[i].ret;
2545 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2547 regulator_disable(consumers[i].consumer);
2551 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2554 * regulator_bulk_disable - disable multiple regulator consumers
2556 * @num_consumers: Number of consumers
2557 * @consumers: Consumer data; clients are stored here.
2558 * @return 0 on success, an errno on failure
2560 * This convenience API allows consumers to disable multiple regulator
2561 * clients in a single API call. If any consumers cannot be disabled
2562 * then any others that were disabled will be enabled again prior to
2565 int regulator_bulk_disable(int num_consumers,
2566 struct regulator_bulk_data *consumers)
2571 for (i = num_consumers - 1; i >= 0; --i) {
2572 ret = regulator_disable(consumers[i].consumer);
2580 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2581 for (++i; i < num_consumers; ++i)
2582 regulator_enable(consumers[i].consumer);
2586 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2589 * regulator_bulk_force_disable - force disable multiple regulator consumers
2591 * @num_consumers: Number of consumers
2592 * @consumers: Consumer data; clients are stored here.
2593 * @return 0 on success, an errno on failure
2595 * This convenience API allows consumers to forcibly disable multiple regulator
2596 * clients in a single API call.
2597 * NOTE: This should be used for situations when device damage will
2598 * likely occur if the regulators are not disabled (e.g. over temp).
2599 * Although regulator_force_disable function call for some consumers can
2600 * return error numbers, the function is called for all consumers.
2602 int regulator_bulk_force_disable(int num_consumers,
2603 struct regulator_bulk_data *consumers)
2608 for (i = 0; i < num_consumers; i++)
2610 regulator_force_disable(consumers[i].consumer);
2612 for (i = 0; i < num_consumers; i++) {
2613 if (consumers[i].ret != 0) {
2614 ret = consumers[i].ret;
2623 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2626 * regulator_bulk_free - free multiple regulator consumers
2628 * @num_consumers: Number of consumers
2629 * @consumers: Consumer data; clients are stored here.
2631 * This convenience API allows consumers to free multiple regulator
2632 * clients in a single API call.
2634 void regulator_bulk_free(int num_consumers,
2635 struct regulator_bulk_data *consumers)
2639 for (i = 0; i < num_consumers; i++) {
2640 regulator_put(consumers[i].consumer);
2641 consumers[i].consumer = NULL;
2644 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2647 * regulator_notifier_call_chain - call regulator event notifier
2648 * @rdev: regulator source
2649 * @event: notifier block
2650 * @data: callback-specific data.
2652 * Called by regulator drivers to notify clients a regulator event has
2653 * occurred. We also notify regulator clients downstream.
2654 * Note lock must be held by caller.
2656 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2657 unsigned long event, void *data)
2659 _notifier_call_chain(rdev, event, data);
2663 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2666 * regulator_mode_to_status - convert a regulator mode into a status
2668 * @mode: Mode to convert
2670 * Convert a regulator mode into a status.
2672 int regulator_mode_to_status(unsigned int mode)
2675 case REGULATOR_MODE_FAST:
2676 return REGULATOR_STATUS_FAST;
2677 case REGULATOR_MODE_NORMAL:
2678 return REGULATOR_STATUS_NORMAL;
2679 case REGULATOR_MODE_IDLE:
2680 return REGULATOR_STATUS_IDLE;
2681 case REGULATOR_STATUS_STANDBY:
2682 return REGULATOR_STATUS_STANDBY;
2687 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2690 * To avoid cluttering sysfs (and memory) with useless state, only
2691 * create attributes that can be meaningfully displayed.
2693 static int add_regulator_attributes(struct regulator_dev *rdev)
2695 struct device *dev = &rdev->dev;
2696 struct regulator_ops *ops = rdev->desc->ops;
2699 /* some attributes need specific methods to be displayed */
2700 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2701 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
2702 status = device_create_file(dev, &dev_attr_microvolts);
2706 if (ops->get_current_limit) {
2707 status = device_create_file(dev, &dev_attr_microamps);
2711 if (ops->get_mode) {
2712 status = device_create_file(dev, &dev_attr_opmode);
2716 if (ops->is_enabled) {
2717 status = device_create_file(dev, &dev_attr_state);
2721 if (ops->get_status) {
2722 status = device_create_file(dev, &dev_attr_status);
2727 /* some attributes are type-specific */
2728 if (rdev->desc->type == REGULATOR_CURRENT) {
2729 status = device_create_file(dev, &dev_attr_requested_microamps);
2734 /* all the other attributes exist to support constraints;
2735 * don't show them if there are no constraints, or if the
2736 * relevant supporting methods are missing.
2738 if (!rdev->constraints)
2741 /* constraints need specific supporting methods */
2742 if (ops->set_voltage || ops->set_voltage_sel) {
2743 status = device_create_file(dev, &dev_attr_min_microvolts);
2746 status = device_create_file(dev, &dev_attr_max_microvolts);
2750 if (ops->set_current_limit) {
2751 status = device_create_file(dev, &dev_attr_min_microamps);
2754 status = device_create_file(dev, &dev_attr_max_microamps);
2759 /* suspend mode constraints need multiple supporting methods */
2760 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2763 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2766 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2769 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2773 if (ops->set_suspend_voltage) {
2774 status = device_create_file(dev,
2775 &dev_attr_suspend_standby_microvolts);
2778 status = device_create_file(dev,
2779 &dev_attr_suspend_mem_microvolts);
2782 status = device_create_file(dev,
2783 &dev_attr_suspend_disk_microvolts);
2788 if (ops->set_suspend_mode) {
2789 status = device_create_file(dev,
2790 &dev_attr_suspend_standby_mode);
2793 status = device_create_file(dev,
2794 &dev_attr_suspend_mem_mode);
2797 status = device_create_file(dev,
2798 &dev_attr_suspend_disk_mode);
2806 static void rdev_init_debugfs(struct regulator_dev *rdev)
2808 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2809 if (!rdev->debugfs) {
2810 rdev_warn(rdev, "Failed to create debugfs directory\n");
2814 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2816 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2821 * regulator_register - register regulator
2822 * @regulator_desc: regulator to register
2823 * @dev: struct device for the regulator
2824 * @init_data: platform provided init data, passed through by driver
2825 * @driver_data: private regulator data
2826 * @of_node: OpenFirmware node to parse for device tree bindings (may be
2829 * Called by regulator drivers to register a regulator.
2830 * Returns 0 on success.
2832 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2833 struct device *dev, const struct regulator_init_data *init_data,
2834 void *driver_data, struct device_node *of_node)
2836 const struct regulation_constraints *constraints = NULL;
2837 static atomic_t regulator_no = ATOMIC_INIT(0);
2838 struct regulator_dev *rdev;
2840 const char *supply = NULL;
2842 if (regulator_desc == NULL)
2843 return ERR_PTR(-EINVAL);
2845 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2846 return ERR_PTR(-EINVAL);
2848 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2849 regulator_desc->type != REGULATOR_CURRENT)
2850 return ERR_PTR(-EINVAL);
2852 /* Only one of each should be implemented */
2853 WARN_ON(regulator_desc->ops->get_voltage &&
2854 regulator_desc->ops->get_voltage_sel);
2855 WARN_ON(regulator_desc->ops->set_voltage &&
2856 regulator_desc->ops->set_voltage_sel);
2858 /* If we're using selectors we must implement list_voltage. */
2859 if (regulator_desc->ops->get_voltage_sel &&
2860 !regulator_desc->ops->list_voltage) {
2861 return ERR_PTR(-EINVAL);
2863 if (regulator_desc->ops->set_voltage_sel &&
2864 !regulator_desc->ops->list_voltage) {
2865 return ERR_PTR(-EINVAL);
2868 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2870 return ERR_PTR(-ENOMEM);
2872 mutex_lock(®ulator_list_mutex);
2874 mutex_init(&rdev->mutex);
2875 rdev->reg_data = driver_data;
2876 rdev->owner = regulator_desc->owner;
2877 rdev->desc = regulator_desc;
2878 INIT_LIST_HEAD(&rdev->consumer_list);
2879 INIT_LIST_HEAD(&rdev->list);
2880 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2881 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
2883 /* preform any regulator specific init */
2884 if (init_data && init_data->regulator_init) {
2885 ret = init_data->regulator_init(rdev->reg_data);
2890 /* register with sysfs */
2891 rdev->dev.class = ®ulator_class;
2892 rdev->dev.of_node = of_node;
2893 rdev->dev.parent = dev;
2894 dev_set_name(&rdev->dev, "regulator.%d",
2895 atomic_inc_return(®ulator_no) - 1);
2896 ret = device_register(&rdev->dev);
2898 put_device(&rdev->dev);
2902 dev_set_drvdata(&rdev->dev, rdev);
2904 /* set regulator constraints */
2906 constraints = &init_data->constraints;
2908 ret = set_machine_constraints(rdev, constraints);
2912 /* add attributes supported by this regulator */
2913 ret = add_regulator_attributes(rdev);
2917 if (init_data && init_data->supply_regulator)
2918 supply = init_data->supply_regulator;
2919 else if (regulator_desc->supply_name)
2920 supply = regulator_desc->supply_name;
2923 struct regulator_dev *r;
2925 r = regulator_dev_lookup(dev, supply);
2928 dev_err(dev, "Failed to find supply %s\n", supply);
2929 ret = -EPROBE_DEFER;
2933 ret = set_supply(rdev, r);
2937 /* Enable supply if rail is enabled */
2938 if (rdev->desc->ops->is_enabled &&
2939 rdev->desc->ops->is_enabled(rdev)) {
2940 ret = regulator_enable(rdev->supply);
2946 /* add consumers devices */
2948 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2949 ret = set_consumer_device_supply(rdev,
2950 init_data->consumer_supplies[i].dev_name,
2951 init_data->consumer_supplies[i].supply);
2953 dev_err(dev, "Failed to set supply %s\n",
2954 init_data->consumer_supplies[i].supply);
2955 goto unset_supplies;
2960 list_add(&rdev->list, ®ulator_list);
2962 rdev_init_debugfs(rdev);
2964 mutex_unlock(®ulator_list_mutex);
2968 unset_regulator_supplies(rdev);
2971 kfree(rdev->constraints);
2972 device_unregister(&rdev->dev);
2973 /* device core frees rdev */
2974 rdev = ERR_PTR(ret);
2979 rdev = ERR_PTR(ret);
2982 EXPORT_SYMBOL_GPL(regulator_register);
2985 * regulator_unregister - unregister regulator
2986 * @rdev: regulator to unregister
2988 * Called by regulator drivers to unregister a regulator.
2990 void regulator_unregister(struct regulator_dev *rdev)
2996 regulator_put(rdev->supply);
2997 mutex_lock(®ulator_list_mutex);
2998 debugfs_remove_recursive(rdev->debugfs);
2999 flush_work_sync(&rdev->disable_work.work);
3000 WARN_ON(rdev->open_count);
3001 unset_regulator_supplies(rdev);
3002 list_del(&rdev->list);
3003 kfree(rdev->constraints);
3004 device_unregister(&rdev->dev);
3005 mutex_unlock(®ulator_list_mutex);
3007 EXPORT_SYMBOL_GPL(regulator_unregister);
3010 * regulator_suspend_prepare - prepare regulators for system wide suspend
3011 * @state: system suspend state
3013 * Configure each regulator with it's suspend operating parameters for state.
3014 * This will usually be called by machine suspend code prior to supending.
3016 int regulator_suspend_prepare(suspend_state_t state)
3018 struct regulator_dev *rdev;
3021 /* ON is handled by regulator active state */
3022 if (state == PM_SUSPEND_ON)
3025 mutex_lock(®ulator_list_mutex);
3026 list_for_each_entry(rdev, ®ulator_list, list) {
3028 mutex_lock(&rdev->mutex);
3029 ret = suspend_prepare(rdev, state);
3030 mutex_unlock(&rdev->mutex);
3033 rdev_err(rdev, "failed to prepare\n");
3038 mutex_unlock(®ulator_list_mutex);
3041 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3044 * regulator_suspend_finish - resume regulators from system wide suspend
3046 * Turn on regulators that might be turned off by regulator_suspend_prepare
3047 * and that should be turned on according to the regulators properties.
3049 int regulator_suspend_finish(void)
3051 struct regulator_dev *rdev;
3054 mutex_lock(®ulator_list_mutex);
3055 list_for_each_entry(rdev, ®ulator_list, list) {
3056 struct regulator_ops *ops = rdev->desc->ops;
3058 mutex_lock(&rdev->mutex);
3059 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3061 error = ops->enable(rdev);
3065 if (!has_full_constraints)
3069 if (ops->is_enabled && !ops->is_enabled(rdev))
3072 error = ops->disable(rdev);
3077 mutex_unlock(&rdev->mutex);
3079 mutex_unlock(®ulator_list_mutex);
3082 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3085 * regulator_has_full_constraints - the system has fully specified constraints
3087 * Calling this function will cause the regulator API to disable all
3088 * regulators which have a zero use count and don't have an always_on
3089 * constraint in a late_initcall.
3091 * The intention is that this will become the default behaviour in a
3092 * future kernel release so users are encouraged to use this facility
3095 void regulator_has_full_constraints(void)
3097 has_full_constraints = 1;
3099 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3102 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3104 * Calling this function will cause the regulator API to provide a
3105 * dummy regulator to consumers if no physical regulator is found,
3106 * allowing most consumers to proceed as though a regulator were
3107 * configured. This allows systems such as those with software
3108 * controllable regulators for the CPU core only to be brought up more
3111 void regulator_use_dummy_regulator(void)
3113 board_wants_dummy_regulator = true;
3115 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3118 * rdev_get_drvdata - get rdev regulator driver data
3121 * Get rdev regulator driver private data. This call can be used in the
3122 * regulator driver context.
3124 void *rdev_get_drvdata(struct regulator_dev *rdev)
3126 return rdev->reg_data;
3128 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3131 * regulator_get_drvdata - get regulator driver data
3132 * @regulator: regulator
3134 * Get regulator driver private data. This call can be used in the consumer
3135 * driver context when non API regulator specific functions need to be called.
3137 void *regulator_get_drvdata(struct regulator *regulator)
3139 return regulator->rdev->reg_data;
3141 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3144 * regulator_set_drvdata - set regulator driver data
3145 * @regulator: regulator
3148 void regulator_set_drvdata(struct regulator *regulator, void *data)
3150 regulator->rdev->reg_data = data;
3152 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3155 * regulator_get_id - get regulator ID
3158 int rdev_get_id(struct regulator_dev *rdev)
3160 return rdev->desc->id;
3162 EXPORT_SYMBOL_GPL(rdev_get_id);
3164 struct device *rdev_get_dev(struct regulator_dev *rdev)
3168 EXPORT_SYMBOL_GPL(rdev_get_dev);
3170 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3172 return reg_init_data->driver_data;
3174 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3176 #ifdef CONFIG_DEBUG_FS
3177 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3178 size_t count, loff_t *ppos)
3180 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3181 ssize_t len, ret = 0;
3182 struct regulator_map *map;
3187 list_for_each_entry(map, ®ulator_map_list, list) {
3188 len = snprintf(buf + ret, PAGE_SIZE - ret,
3190 rdev_get_name(map->regulator), map->dev_name,
3194 if (ret > PAGE_SIZE) {
3200 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3208 static const struct file_operations supply_map_fops = {
3209 #ifdef CONFIG_DEBUG_FS
3210 .read = supply_map_read_file,
3211 .llseek = default_llseek,
3215 static int __init regulator_init(void)
3219 ret = class_register(®ulator_class);
3221 debugfs_root = debugfs_create_dir("regulator", NULL);
3223 pr_warn("regulator: Failed to create debugfs directory\n");
3225 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3228 regulator_dummy_init();
3233 /* init early to allow our consumers to complete system booting */
3234 core_initcall(regulator_init);
3236 static int __init regulator_init_complete(void)
3238 struct regulator_dev *rdev;
3239 struct regulator_ops *ops;
3240 struct regulation_constraints *c;
3243 mutex_lock(®ulator_list_mutex);
3245 /* If we have a full configuration then disable any regulators
3246 * which are not in use or always_on. This will become the
3247 * default behaviour in the future.
3249 list_for_each_entry(rdev, ®ulator_list, list) {
3250 ops = rdev->desc->ops;
3251 c = rdev->constraints;
3253 if (!ops->disable || (c && c->always_on))
3256 mutex_lock(&rdev->mutex);
3258 if (rdev->use_count)
3261 /* If we can't read the status assume it's on. */
3262 if (ops->is_enabled)
3263 enabled = ops->is_enabled(rdev);
3270 if (has_full_constraints) {
3271 /* We log since this may kill the system if it
3273 rdev_info(rdev, "disabling\n");
3274 ret = ops->disable(rdev);
3276 rdev_err(rdev, "couldn't disable: %d\n", ret);
3279 /* The intention is that in future we will
3280 * assume that full constraints are provided
3281 * so warn even if we aren't going to do
3284 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3288 mutex_unlock(&rdev->mutex);
3291 mutex_unlock(®ulator_list_mutex);
3295 late_initcall(regulator_init_complete);