2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
28 #include <linux/regmap.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/module.h>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/regulator.h>
41 #define rdev_crit(rdev, fmt, ...) \
42 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43 #define rdev_err(rdev, fmt, ...) \
44 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45 #define rdev_warn(rdev, fmt, ...) \
46 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 #define rdev_info(rdev, fmt, ...) \
48 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 #define rdev_dbg(rdev, fmt, ...) \
50 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52 static DEFINE_MUTEX(regulator_list_mutex);
53 static LIST_HEAD(regulator_list);
54 static LIST_HEAD(regulator_map_list);
55 static LIST_HEAD(regulator_ena_gpio_list);
56 static bool has_full_constraints;
57 static bool board_wants_dummy_regulator;
59 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;
74 * struct regulator_enable_gpio
76 * Management for shared enable GPIO pin
78 struct regulator_enable_gpio {
79 struct list_head list;
81 u32 enable_count; /* a number of enabled shared GPIO */
82 u32 request_count; /* a number of requested shared GPIO */
83 unsigned int ena_gpio_invert:1;
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 int _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;
110 * of_get_regulator - get a regulator device node based on supply name
111 * @dev: Device pointer for the consumer (of regulator) device
112 * @supply: regulator supply name
114 * Extract the regulator device node corresponding to the supply name.
115 * returns the device node corresponding to the regulator if found, else
118 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
120 struct device_node *regnode = NULL;
121 char prop_name[32]; /* 32 is max size of property name */
123 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
125 snprintf(prop_name, 32, "%s-supply", supply);
126 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
129 dev_dbg(dev, "Looking up %s property in node %s failed",
130 prop_name, dev->of_node->full_name);
136 static int _regulator_can_change_status(struct regulator_dev *rdev)
138 if (!rdev->constraints)
141 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
147 /* Platform voltage constraint check */
148 static int regulator_check_voltage(struct regulator_dev *rdev,
149 int *min_uV, int *max_uV)
151 BUG_ON(*min_uV > *max_uV);
153 if (!rdev->constraints) {
154 rdev_err(rdev, "no constraints\n");
157 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
158 rdev_err(rdev, "operation not allowed\n");
162 if (*max_uV > rdev->constraints->max_uV)
163 *max_uV = rdev->constraints->max_uV;
164 if (*min_uV < rdev->constraints->min_uV)
165 *min_uV = rdev->constraints->min_uV;
167 if (*min_uV > *max_uV) {
168 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
176 /* Make sure we select a voltage that suits the needs of all
177 * regulator consumers
179 static int regulator_check_consumers(struct regulator_dev *rdev,
180 int *min_uV, int *max_uV)
182 struct regulator *regulator;
184 list_for_each_entry(regulator, &rdev->consumer_list, list) {
186 * Assume consumers that didn't say anything are OK
187 * with anything in the constraint range.
189 if (!regulator->min_uV && !regulator->max_uV)
192 if (*max_uV > regulator->max_uV)
193 *max_uV = regulator->max_uV;
194 if (*min_uV < regulator->min_uV)
195 *min_uV = regulator->min_uV;
198 if (*min_uV > *max_uV) {
199 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
207 /* current constraint check */
208 static int regulator_check_current_limit(struct regulator_dev *rdev,
209 int *min_uA, int *max_uA)
211 BUG_ON(*min_uA > *max_uA);
213 if (!rdev->constraints) {
214 rdev_err(rdev, "no constraints\n");
217 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
218 rdev_err(rdev, "operation not allowed\n");
222 if (*max_uA > rdev->constraints->max_uA)
223 *max_uA = rdev->constraints->max_uA;
224 if (*min_uA < rdev->constraints->min_uA)
225 *min_uA = rdev->constraints->min_uA;
227 if (*min_uA > *max_uA) {
228 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
236 /* operating mode constraint check */
237 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
240 case REGULATOR_MODE_FAST:
241 case REGULATOR_MODE_NORMAL:
242 case REGULATOR_MODE_IDLE:
243 case REGULATOR_MODE_STANDBY:
246 rdev_err(rdev, "invalid mode %x specified\n", *mode);
250 if (!rdev->constraints) {
251 rdev_err(rdev, "no constraints\n");
254 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
255 rdev_err(rdev, "operation not allowed\n");
259 /* The modes are bitmasks, the most power hungry modes having
260 * the lowest values. If the requested mode isn't supported
261 * try higher modes. */
263 if (rdev->constraints->valid_modes_mask & *mode)
271 /* dynamic regulator mode switching constraint check */
272 static int regulator_check_drms(struct regulator_dev *rdev)
274 if (!rdev->constraints) {
275 rdev_err(rdev, "no constraints\n");
278 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
279 rdev_err(rdev, "operation not allowed\n");
285 static ssize_t regulator_uV_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct regulator_dev *rdev = dev_get_drvdata(dev);
291 mutex_lock(&rdev->mutex);
292 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
293 mutex_unlock(&rdev->mutex);
297 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
299 static ssize_t regulator_uA_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
302 struct regulator_dev *rdev = dev_get_drvdata(dev);
304 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
306 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
308 static ssize_t regulator_name_show(struct device *dev,
309 struct device_attribute *attr, char *buf)
311 struct regulator_dev *rdev = dev_get_drvdata(dev);
313 return sprintf(buf, "%s\n", rdev_get_name(rdev));
316 static ssize_t regulator_print_opmode(char *buf, int mode)
319 case REGULATOR_MODE_FAST:
320 return sprintf(buf, "fast\n");
321 case REGULATOR_MODE_NORMAL:
322 return sprintf(buf, "normal\n");
323 case REGULATOR_MODE_IDLE:
324 return sprintf(buf, "idle\n");
325 case REGULATOR_MODE_STANDBY:
326 return sprintf(buf, "standby\n");
328 return sprintf(buf, "unknown\n");
331 static ssize_t regulator_opmode_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
334 struct regulator_dev *rdev = dev_get_drvdata(dev);
336 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
338 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
340 static ssize_t regulator_print_state(char *buf, int state)
343 return sprintf(buf, "enabled\n");
345 return sprintf(buf, "disabled\n");
347 return sprintf(buf, "unknown\n");
350 static ssize_t regulator_state_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
353 struct regulator_dev *rdev = dev_get_drvdata(dev);
356 mutex_lock(&rdev->mutex);
357 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
358 mutex_unlock(&rdev->mutex);
362 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
364 static ssize_t regulator_status_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
367 struct regulator_dev *rdev = dev_get_drvdata(dev);
371 status = rdev->desc->ops->get_status(rdev);
376 case REGULATOR_STATUS_OFF:
379 case REGULATOR_STATUS_ON:
382 case REGULATOR_STATUS_ERROR:
385 case REGULATOR_STATUS_FAST:
388 case REGULATOR_STATUS_NORMAL:
391 case REGULATOR_STATUS_IDLE:
394 case REGULATOR_STATUS_STANDBY:
397 case REGULATOR_STATUS_BYPASS:
400 case REGULATOR_STATUS_UNDEFINED:
407 return sprintf(buf, "%s\n", label);
409 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
411 static ssize_t regulator_min_uA_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
414 struct regulator_dev *rdev = dev_get_drvdata(dev);
416 if (!rdev->constraints)
417 return sprintf(buf, "constraint not defined\n");
419 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
421 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
423 static ssize_t regulator_max_uA_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
426 struct regulator_dev *rdev = dev_get_drvdata(dev);
428 if (!rdev->constraints)
429 return sprintf(buf, "constraint not defined\n");
431 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
433 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
435 static ssize_t regulator_min_uV_show(struct device *dev,
436 struct device_attribute *attr, char *buf)
438 struct regulator_dev *rdev = dev_get_drvdata(dev);
440 if (!rdev->constraints)
441 return sprintf(buf, "constraint not defined\n");
443 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
445 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
447 static ssize_t regulator_max_uV_show(struct device *dev,
448 struct device_attribute *attr, char *buf)
450 struct regulator_dev *rdev = dev_get_drvdata(dev);
452 if (!rdev->constraints)
453 return sprintf(buf, "constraint not defined\n");
455 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
457 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
459 static ssize_t regulator_total_uA_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
462 struct regulator_dev *rdev = dev_get_drvdata(dev);
463 struct regulator *regulator;
466 mutex_lock(&rdev->mutex);
467 list_for_each_entry(regulator, &rdev->consumer_list, list)
468 uA += regulator->uA_load;
469 mutex_unlock(&rdev->mutex);
470 return sprintf(buf, "%d\n", uA);
472 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
474 static ssize_t regulator_num_users_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
477 struct regulator_dev *rdev = dev_get_drvdata(dev);
478 return sprintf(buf, "%d\n", rdev->use_count);
481 static ssize_t regulator_type_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
484 struct regulator_dev *rdev = dev_get_drvdata(dev);
486 switch (rdev->desc->type) {
487 case REGULATOR_VOLTAGE:
488 return sprintf(buf, "voltage\n");
489 case REGULATOR_CURRENT:
490 return sprintf(buf, "current\n");
492 return sprintf(buf, "unknown\n");
495 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
498 struct regulator_dev *rdev = dev_get_drvdata(dev);
500 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
502 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
503 regulator_suspend_mem_uV_show, NULL);
505 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
508 struct regulator_dev *rdev = dev_get_drvdata(dev);
510 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
512 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
513 regulator_suspend_disk_uV_show, NULL);
515 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 struct regulator_dev *rdev = dev_get_drvdata(dev);
520 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
522 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
523 regulator_suspend_standby_uV_show, NULL);
525 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
528 struct regulator_dev *rdev = dev_get_drvdata(dev);
530 return regulator_print_opmode(buf,
531 rdev->constraints->state_mem.mode);
533 static DEVICE_ATTR(suspend_mem_mode, 0444,
534 regulator_suspend_mem_mode_show, NULL);
536 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
537 struct device_attribute *attr, char *buf)
539 struct regulator_dev *rdev = dev_get_drvdata(dev);
541 return regulator_print_opmode(buf,
542 rdev->constraints->state_disk.mode);
544 static DEVICE_ATTR(suspend_disk_mode, 0444,
545 regulator_suspend_disk_mode_show, NULL);
547 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
548 struct device_attribute *attr, char *buf)
550 struct regulator_dev *rdev = dev_get_drvdata(dev);
552 return regulator_print_opmode(buf,
553 rdev->constraints->state_standby.mode);
555 static DEVICE_ATTR(suspend_standby_mode, 0444,
556 regulator_suspend_standby_mode_show, NULL);
558 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
561 struct regulator_dev *rdev = dev_get_drvdata(dev);
563 return regulator_print_state(buf,
564 rdev->constraints->state_mem.enabled);
566 static DEVICE_ATTR(suspend_mem_state, 0444,
567 regulator_suspend_mem_state_show, NULL);
569 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
570 struct device_attribute *attr, char *buf)
572 struct regulator_dev *rdev = dev_get_drvdata(dev);
574 return regulator_print_state(buf,
575 rdev->constraints->state_disk.enabled);
577 static DEVICE_ATTR(suspend_disk_state, 0444,
578 regulator_suspend_disk_state_show, NULL);
580 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
581 struct device_attribute *attr, char *buf)
583 struct regulator_dev *rdev = dev_get_drvdata(dev);
585 return regulator_print_state(buf,
586 rdev->constraints->state_standby.enabled);
588 static DEVICE_ATTR(suspend_standby_state, 0444,
589 regulator_suspend_standby_state_show, NULL);
591 static ssize_t regulator_bypass_show(struct device *dev,
592 struct device_attribute *attr, char *buf)
594 struct regulator_dev *rdev = dev_get_drvdata(dev);
599 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
608 return sprintf(buf, "%s\n", report);
610 static DEVICE_ATTR(bypass, 0444,
611 regulator_bypass_show, NULL);
614 * These are the only attributes are present for all regulators.
615 * Other attributes are a function of regulator functionality.
617 static struct device_attribute regulator_dev_attrs[] = {
618 __ATTR(name, 0444, regulator_name_show, NULL),
619 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
620 __ATTR(type, 0444, regulator_type_show, NULL),
624 static void regulator_dev_release(struct device *dev)
626 struct regulator_dev *rdev = dev_get_drvdata(dev);
630 static struct class regulator_class = {
632 .dev_release = regulator_dev_release,
633 .dev_attrs = regulator_dev_attrs,
636 /* Calculate the new optimum regulator operating mode based on the new total
637 * consumer load. All locks held by caller */
638 static void drms_uA_update(struct regulator_dev *rdev)
640 struct regulator *sibling;
641 int current_uA = 0, output_uV, input_uV, err;
644 err = regulator_check_drms(rdev);
645 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
646 (!rdev->desc->ops->get_voltage &&
647 !rdev->desc->ops->get_voltage_sel) ||
648 !rdev->desc->ops->set_mode)
651 /* get output voltage */
652 output_uV = _regulator_get_voltage(rdev);
656 /* get input voltage */
659 input_uV = regulator_get_voltage(rdev->supply);
661 input_uV = rdev->constraints->input_uV;
665 /* calc total requested load */
666 list_for_each_entry(sibling, &rdev->consumer_list, list)
667 current_uA += sibling->uA_load;
669 /* now get the optimum mode for our new total regulator load */
670 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
671 output_uV, current_uA);
673 /* check the new mode is allowed */
674 err = regulator_mode_constrain(rdev, &mode);
676 rdev->desc->ops->set_mode(rdev, mode);
679 static int suspend_set_state(struct regulator_dev *rdev,
680 struct regulator_state *rstate)
684 /* If we have no suspend mode configration don't set anything;
685 * only warn if the driver implements set_suspend_voltage or
686 * set_suspend_mode callback.
688 if (!rstate->enabled && !rstate->disabled) {
689 if (rdev->desc->ops->set_suspend_voltage ||
690 rdev->desc->ops->set_suspend_mode)
691 rdev_warn(rdev, "No configuration\n");
695 if (rstate->enabled && rstate->disabled) {
696 rdev_err(rdev, "invalid configuration\n");
700 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
701 ret = rdev->desc->ops->set_suspend_enable(rdev);
702 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
703 ret = rdev->desc->ops->set_suspend_disable(rdev);
704 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
708 rdev_err(rdev, "failed to enabled/disable\n");
712 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
713 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
715 rdev_err(rdev, "failed to set voltage\n");
720 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
721 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
723 rdev_err(rdev, "failed to set mode\n");
730 /* locks held by caller */
731 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
733 if (!rdev->constraints)
737 case PM_SUSPEND_STANDBY:
738 return suspend_set_state(rdev,
739 &rdev->constraints->state_standby);
741 return suspend_set_state(rdev,
742 &rdev->constraints->state_mem);
744 return suspend_set_state(rdev,
745 &rdev->constraints->state_disk);
751 static void print_constraints(struct regulator_dev *rdev)
753 struct regulation_constraints *constraints = rdev->constraints;
758 if (constraints->min_uV && constraints->max_uV) {
759 if (constraints->min_uV == constraints->max_uV)
760 count += sprintf(buf + count, "%d mV ",
761 constraints->min_uV / 1000);
763 count += sprintf(buf + count, "%d <--> %d mV ",
764 constraints->min_uV / 1000,
765 constraints->max_uV / 1000);
768 if (!constraints->min_uV ||
769 constraints->min_uV != constraints->max_uV) {
770 ret = _regulator_get_voltage(rdev);
772 count += sprintf(buf + count, "at %d mV ", ret / 1000);
775 if (constraints->uV_offset)
776 count += sprintf(buf, "%dmV offset ",
777 constraints->uV_offset / 1000);
779 if (constraints->min_uA && constraints->max_uA) {
780 if (constraints->min_uA == constraints->max_uA)
781 count += sprintf(buf + count, "%d mA ",
782 constraints->min_uA / 1000);
784 count += sprintf(buf + count, "%d <--> %d mA ",
785 constraints->min_uA / 1000,
786 constraints->max_uA / 1000);
789 if (!constraints->min_uA ||
790 constraints->min_uA != constraints->max_uA) {
791 ret = _regulator_get_current_limit(rdev);
793 count += sprintf(buf + count, "at %d mA ", ret / 1000);
796 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
797 count += sprintf(buf + count, "fast ");
798 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
799 count += sprintf(buf + count, "normal ");
800 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
801 count += sprintf(buf + count, "idle ");
802 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
803 count += sprintf(buf + count, "standby");
806 sprintf(buf, "no parameters");
808 rdev_info(rdev, "%s\n", buf);
810 if ((constraints->min_uV != constraints->max_uV) &&
811 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
813 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
816 static int machine_constraints_voltage(struct regulator_dev *rdev,
817 struct regulation_constraints *constraints)
819 struct regulator_ops *ops = rdev->desc->ops;
822 /* do we need to apply the constraint voltage */
823 if (rdev->constraints->apply_uV &&
824 rdev->constraints->min_uV == rdev->constraints->max_uV) {
825 ret = _regulator_do_set_voltage(rdev,
826 rdev->constraints->min_uV,
827 rdev->constraints->max_uV);
829 rdev_err(rdev, "failed to apply %duV constraint\n",
830 rdev->constraints->min_uV);
835 /* constrain machine-level voltage specs to fit
836 * the actual range supported by this regulator.
838 if (ops->list_voltage && rdev->desc->n_voltages) {
839 int count = rdev->desc->n_voltages;
841 int min_uV = INT_MAX;
842 int max_uV = INT_MIN;
843 int cmin = constraints->min_uV;
844 int cmax = constraints->max_uV;
846 /* it's safe to autoconfigure fixed-voltage supplies
847 and the constraints are used by list_voltage. */
848 if (count == 1 && !cmin) {
851 constraints->min_uV = cmin;
852 constraints->max_uV = cmax;
855 /* voltage constraints are optional */
856 if ((cmin == 0) && (cmax == 0))
859 /* else require explicit machine-level constraints */
860 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
861 rdev_err(rdev, "invalid voltage constraints\n");
865 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
866 for (i = 0; i < count; i++) {
869 value = ops->list_voltage(rdev, i);
873 /* maybe adjust [min_uV..max_uV] */
874 if (value >= cmin && value < min_uV)
876 if (value <= cmax && value > max_uV)
880 /* final: [min_uV..max_uV] valid iff constraints valid */
881 if (max_uV < min_uV) {
883 "unsupportable voltage constraints %u-%uuV\n",
888 /* use regulator's subset of machine constraints */
889 if (constraints->min_uV < min_uV) {
890 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
891 constraints->min_uV, min_uV);
892 constraints->min_uV = min_uV;
894 if (constraints->max_uV > max_uV) {
895 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
896 constraints->max_uV, max_uV);
897 constraints->max_uV = max_uV;
904 static int _regulator_do_enable(struct regulator_dev *rdev);
907 * set_machine_constraints - sets regulator constraints
908 * @rdev: regulator source
909 * @constraints: constraints to apply
911 * Allows platform initialisation code to define and constrain
912 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
913 * Constraints *must* be set by platform code in order for some
914 * regulator operations to proceed i.e. set_voltage, set_current_limit,
917 static int set_machine_constraints(struct regulator_dev *rdev,
918 const struct regulation_constraints *constraints)
921 struct regulator_ops *ops = rdev->desc->ops;
924 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
927 rdev->constraints = kzalloc(sizeof(*constraints),
929 if (!rdev->constraints)
932 ret = machine_constraints_voltage(rdev, rdev->constraints);
936 /* do we need to setup our suspend state */
937 if (rdev->constraints->initial_state) {
938 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
940 rdev_err(rdev, "failed to set suspend state\n");
945 if (rdev->constraints->initial_mode) {
946 if (!ops->set_mode) {
947 rdev_err(rdev, "no set_mode operation\n");
952 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
954 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
959 /* If the constraints say the regulator should be on at this point
960 * and we have control then make sure it is enabled.
962 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
963 ret = _regulator_do_enable(rdev);
964 if (ret < 0 && ret != -EINVAL) {
965 rdev_err(rdev, "failed to enable\n");
970 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
971 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
973 rdev_err(rdev, "failed to set ramp_delay\n");
978 print_constraints(rdev);
981 kfree(rdev->constraints);
982 rdev->constraints = NULL;
987 * set_supply - set regulator supply regulator
988 * @rdev: regulator name
989 * @supply_rdev: supply regulator name
991 * Called by platform initialisation code to set the supply regulator for this
992 * regulator. This ensures that a regulators supply will also be enabled by the
993 * core if it's child is enabled.
995 static int set_supply(struct regulator_dev *rdev,
996 struct regulator_dev *supply_rdev)
1000 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1002 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1003 if (rdev->supply == NULL) {
1007 supply_rdev->open_count++;
1013 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1014 * @rdev: regulator source
1015 * @consumer_dev_name: dev_name() string for device supply applies to
1016 * @supply: symbolic name for supply
1018 * Allows platform initialisation code to map physical regulator
1019 * sources to symbolic names for supplies for use by devices. Devices
1020 * should use these symbolic names to request regulators, avoiding the
1021 * need to provide board-specific regulator names as platform data.
1023 static int set_consumer_device_supply(struct regulator_dev *rdev,
1024 const char *consumer_dev_name,
1027 struct regulator_map *node;
1033 if (consumer_dev_name != NULL)
1038 list_for_each_entry(node, ®ulator_map_list, list) {
1039 if (node->dev_name && consumer_dev_name) {
1040 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1042 } else if (node->dev_name || consumer_dev_name) {
1046 if (strcmp(node->supply, supply) != 0)
1049 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1051 dev_name(&node->regulator->dev),
1052 node->regulator->desc->name,
1054 dev_name(&rdev->dev), rdev_get_name(rdev));
1058 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1062 node->regulator = rdev;
1063 node->supply = supply;
1066 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1067 if (node->dev_name == NULL) {
1073 list_add(&node->list, ®ulator_map_list);
1077 static void unset_regulator_supplies(struct regulator_dev *rdev)
1079 struct regulator_map *node, *n;
1081 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1082 if (rdev == node->regulator) {
1083 list_del(&node->list);
1084 kfree(node->dev_name);
1090 #define REG_STR_SIZE 64
1092 static struct regulator *create_regulator(struct regulator_dev *rdev,
1094 const char *supply_name)
1096 struct regulator *regulator;
1097 char buf[REG_STR_SIZE];
1100 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1101 if (regulator == NULL)
1104 mutex_lock(&rdev->mutex);
1105 regulator->rdev = rdev;
1106 list_add(®ulator->list, &rdev->consumer_list);
1109 regulator->dev = dev;
1111 /* Add a link to the device sysfs entry */
1112 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1113 dev->kobj.name, supply_name);
1114 if (size >= REG_STR_SIZE)
1117 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1118 if (regulator->supply_name == NULL)
1121 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1124 rdev_warn(rdev, "could not add device link %s err %d\n",
1125 dev->kobj.name, err);
1129 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1130 if (regulator->supply_name == NULL)
1134 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1136 if (!regulator->debugfs) {
1137 rdev_warn(rdev, "Failed to create debugfs directory\n");
1139 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1140 ®ulator->uA_load);
1141 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1142 ®ulator->min_uV);
1143 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1144 ®ulator->max_uV);
1148 * Check now if the regulator is an always on regulator - if
1149 * it is then we don't need to do nearly so much work for
1150 * enable/disable calls.
1152 if (!_regulator_can_change_status(rdev) &&
1153 _regulator_is_enabled(rdev))
1154 regulator->always_on = true;
1156 mutex_unlock(&rdev->mutex);
1159 list_del(®ulator->list);
1161 mutex_unlock(&rdev->mutex);
1165 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1167 if (!rdev->desc->ops->enable_time)
1168 return rdev->desc->enable_time;
1169 return rdev->desc->ops->enable_time(rdev);
1172 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1176 struct regulator_dev *r;
1177 struct device_node *node;
1178 struct regulator_map *map;
1179 const char *devname = NULL;
1181 /* first do a dt based lookup */
1182 if (dev && dev->of_node) {
1183 node = of_get_regulator(dev, supply);
1185 list_for_each_entry(r, ®ulator_list, list)
1186 if (r->dev.parent &&
1187 node == r->dev.of_node)
1191 * If we couldn't even get the node then it's
1192 * not just that the device didn't register
1193 * yet, there's no node and we'll never
1200 /* if not found, try doing it non-dt way */
1202 devname = dev_name(dev);
1204 list_for_each_entry(r, ®ulator_list, list)
1205 if (strcmp(rdev_get_name(r), supply) == 0)
1208 list_for_each_entry(map, ®ulator_map_list, list) {
1209 /* If the mapping has a device set up it must match */
1210 if (map->dev_name &&
1211 (!devname || strcmp(map->dev_name, devname)))
1214 if (strcmp(map->supply, supply) == 0)
1215 return map->regulator;
1222 /* Internal regulator request function */
1223 static struct regulator *_regulator_get(struct device *dev, const char *id,
1226 struct regulator_dev *rdev;
1227 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1228 const char *devname = NULL;
1232 pr_err("get() with no identifier\n");
1237 devname = dev_name(dev);
1239 mutex_lock(®ulator_list_mutex);
1241 rdev = regulator_dev_lookup(dev, id, &ret);
1246 * If we have return value from dev_lookup fail, we do not expect to
1247 * succeed, so, quit with appropriate error value
1250 regulator = ERR_PTR(ret);
1254 if (board_wants_dummy_regulator) {
1255 rdev = dummy_regulator_rdev;
1259 #ifdef CONFIG_REGULATOR_DUMMY
1261 devname = "deviceless";
1263 /* If the board didn't flag that it was fully constrained then
1264 * substitute in a dummy regulator so consumers can continue.
1266 if (!has_full_constraints) {
1267 pr_warn("%s supply %s not found, using dummy regulator\n",
1269 rdev = dummy_regulator_rdev;
1274 mutex_unlock(®ulator_list_mutex);
1278 if (rdev->exclusive) {
1279 regulator = ERR_PTR(-EPERM);
1283 if (exclusive && rdev->open_count) {
1284 regulator = ERR_PTR(-EBUSY);
1288 if (!try_module_get(rdev->owner))
1291 regulator = create_regulator(rdev, dev, id);
1292 if (regulator == NULL) {
1293 regulator = ERR_PTR(-ENOMEM);
1294 module_put(rdev->owner);
1300 rdev->exclusive = 1;
1302 ret = _regulator_is_enabled(rdev);
1304 rdev->use_count = 1;
1306 rdev->use_count = 0;
1310 mutex_unlock(®ulator_list_mutex);
1316 * regulator_get - lookup and obtain a reference to a regulator.
1317 * @dev: device for regulator "consumer"
1318 * @id: Supply name or regulator ID.
1320 * Returns a struct regulator corresponding to the regulator producer,
1321 * or IS_ERR() condition containing errno.
1323 * Use of supply names configured via regulator_set_device_supply() is
1324 * strongly encouraged. It is recommended that the supply name used
1325 * should match the name used for the supply and/or the relevant
1326 * device pins in the datasheet.
1328 struct regulator *regulator_get(struct device *dev, const char *id)
1330 return _regulator_get(dev, id, 0);
1332 EXPORT_SYMBOL_GPL(regulator_get);
1334 static void devm_regulator_release(struct device *dev, void *res)
1336 regulator_put(*(struct regulator **)res);
1340 * regulator_get_exclusive - obtain exclusive access to a regulator.
1341 * @dev: device for regulator "consumer"
1342 * @id: Supply name or regulator ID.
1344 * Returns a struct regulator corresponding to the regulator producer,
1345 * or IS_ERR() condition containing errno. Other consumers will be
1346 * unable to obtain this reference is held and the use count for the
1347 * regulator will be initialised to reflect the current state of the
1350 * This is intended for use by consumers which cannot tolerate shared
1351 * use of the regulator such as those which need to force the
1352 * regulator off for correct operation of the hardware they are
1355 * Use of supply names configured via regulator_set_device_supply() is
1356 * strongly encouraged. It is recommended that the supply name used
1357 * should match the name used for the supply and/or the relevant
1358 * device pins in the datasheet.
1360 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1362 return _regulator_get(dev, id, 1);
1364 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1367 * regulator_get_optional - obtain optional access to a regulator.
1368 * @dev: device for regulator "consumer"
1369 * @id: Supply name or regulator ID.
1371 * Returns a struct regulator corresponding to the regulator producer,
1372 * or IS_ERR() condition containing errno. Other consumers will be
1373 * unable to obtain this reference is held and the use count for the
1374 * regulator will be initialised to reflect the current state of the
1377 * This is intended for use by consumers for devices which can have
1378 * some supplies unconnected in normal use, such as some MMC devices.
1379 * It can allow the regulator core to provide stub supplies for other
1380 * supplies requested using normal regulator_get() calls without
1381 * disrupting the operation of drivers that can handle absent
1384 * Use of supply names configured via regulator_set_device_supply() is
1385 * strongly encouraged. It is recommended that the supply name used
1386 * should match the name used for the supply and/or the relevant
1387 * device pins in the datasheet.
1389 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1391 return _regulator_get(dev, id, 0);
1393 EXPORT_SYMBOL_GPL(regulator_get_optional);
1395 /* Locks held by regulator_put() */
1396 static void _regulator_put(struct regulator *regulator)
1398 struct regulator_dev *rdev;
1400 if (regulator == NULL || IS_ERR(regulator))
1403 rdev = regulator->rdev;
1405 debugfs_remove_recursive(regulator->debugfs);
1407 /* remove any sysfs entries */
1409 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1410 kfree(regulator->supply_name);
1411 list_del(®ulator->list);
1415 rdev->exclusive = 0;
1417 module_put(rdev->owner);
1421 * regulator_put - "free" the regulator source
1422 * @regulator: regulator source
1424 * Note: drivers must ensure that all regulator_enable calls made on this
1425 * regulator source are balanced by regulator_disable calls prior to calling
1428 void regulator_put(struct regulator *regulator)
1430 mutex_lock(®ulator_list_mutex);
1431 _regulator_put(regulator);
1432 mutex_unlock(®ulator_list_mutex);
1434 EXPORT_SYMBOL_GPL(regulator_put);
1436 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1437 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1438 const struct regulator_config *config)
1440 struct regulator_enable_gpio *pin;
1443 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1444 if (pin->gpio == config->ena_gpio) {
1445 rdev_dbg(rdev, "GPIO %d is already used\n",
1447 goto update_ena_gpio_to_rdev;
1451 ret = gpio_request_one(config->ena_gpio,
1452 GPIOF_DIR_OUT | config->ena_gpio_flags,
1453 rdev_get_name(rdev));
1457 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1459 gpio_free(config->ena_gpio);
1463 pin->gpio = config->ena_gpio;
1464 pin->ena_gpio_invert = config->ena_gpio_invert;
1465 list_add(&pin->list, ®ulator_ena_gpio_list);
1467 update_ena_gpio_to_rdev:
1468 pin->request_count++;
1469 rdev->ena_pin = pin;
1473 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1475 struct regulator_enable_gpio *pin, *n;
1480 /* Free the GPIO only in case of no use */
1481 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1482 if (pin->gpio == rdev->ena_pin->gpio) {
1483 if (pin->request_count <= 1) {
1484 pin->request_count = 0;
1485 gpio_free(pin->gpio);
1486 list_del(&pin->list);
1489 pin->request_count--;
1496 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1497 * @rdev: regulator_dev structure
1498 * @enable: enable GPIO at initial use?
1500 * GPIO is enabled in case of initial use. (enable_count is 0)
1501 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1503 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1505 struct regulator_enable_gpio *pin = rdev->ena_pin;
1511 /* Enable GPIO at initial use */
1512 if (pin->enable_count == 0)
1513 gpio_set_value_cansleep(pin->gpio,
1514 !pin->ena_gpio_invert);
1516 pin->enable_count++;
1518 if (pin->enable_count > 1) {
1519 pin->enable_count--;
1523 /* Disable GPIO if not used */
1524 if (pin->enable_count <= 1) {
1525 gpio_set_value_cansleep(pin->gpio,
1526 pin->ena_gpio_invert);
1527 pin->enable_count = 0;
1534 static int _regulator_do_enable(struct regulator_dev *rdev)
1538 /* Query before enabling in case configuration dependent. */
1539 ret = _regulator_get_enable_time(rdev);
1543 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1547 trace_regulator_enable(rdev_get_name(rdev));
1549 if (rdev->ena_pin) {
1550 ret = regulator_ena_gpio_ctrl(rdev, true);
1553 rdev->ena_gpio_state = 1;
1554 } else if (rdev->desc->ops->enable) {
1555 ret = rdev->desc->ops->enable(rdev);
1562 /* Allow the regulator to ramp; it would be useful to extend
1563 * this for bulk operations so that the regulators can ramp
1565 trace_regulator_enable_delay(rdev_get_name(rdev));
1567 if (delay >= 1000) {
1568 mdelay(delay / 1000);
1569 udelay(delay % 1000);
1574 trace_regulator_enable_complete(rdev_get_name(rdev));
1579 /* locks held by regulator_enable() */
1580 static int _regulator_enable(struct regulator_dev *rdev)
1584 /* check voltage and requested load before enabling */
1585 if (rdev->constraints &&
1586 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1587 drms_uA_update(rdev);
1589 if (rdev->use_count == 0) {
1590 /* The regulator may on if it's not switchable or left on */
1591 ret = _regulator_is_enabled(rdev);
1592 if (ret == -EINVAL || ret == 0) {
1593 if (!_regulator_can_change_status(rdev))
1596 ret = _regulator_do_enable(rdev);
1600 } else if (ret < 0) {
1601 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1604 /* Fallthrough on positive return values - already enabled */
1613 * regulator_enable - enable regulator output
1614 * @regulator: regulator source
1616 * Request that the regulator be enabled with the regulator output at
1617 * the predefined voltage or current value. Calls to regulator_enable()
1618 * must be balanced with calls to regulator_disable().
1620 * NOTE: the output value can be set by other drivers, boot loader or may be
1621 * hardwired in the regulator.
1623 int regulator_enable(struct regulator *regulator)
1625 struct regulator_dev *rdev = regulator->rdev;
1628 if (regulator->always_on)
1632 ret = regulator_enable(rdev->supply);
1637 mutex_lock(&rdev->mutex);
1638 ret = _regulator_enable(rdev);
1639 mutex_unlock(&rdev->mutex);
1641 if (ret != 0 && rdev->supply)
1642 regulator_disable(rdev->supply);
1646 EXPORT_SYMBOL_GPL(regulator_enable);
1648 static int _regulator_do_disable(struct regulator_dev *rdev)
1652 trace_regulator_disable(rdev_get_name(rdev));
1654 if (rdev->ena_pin) {
1655 ret = regulator_ena_gpio_ctrl(rdev, false);
1658 rdev->ena_gpio_state = 0;
1660 } else if (rdev->desc->ops->disable) {
1661 ret = rdev->desc->ops->disable(rdev);
1666 trace_regulator_disable_complete(rdev_get_name(rdev));
1671 /* locks held by regulator_disable() */
1672 static int _regulator_disable(struct regulator_dev *rdev)
1676 if (WARN(rdev->use_count <= 0,
1677 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1680 /* are we the last user and permitted to disable ? */
1681 if (rdev->use_count == 1 &&
1682 (rdev->constraints && !rdev->constraints->always_on)) {
1684 /* we are last user */
1685 if (_regulator_can_change_status(rdev)) {
1686 ret = _regulator_do_disable(rdev);
1688 rdev_err(rdev, "failed to disable\n");
1691 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1695 rdev->use_count = 0;
1696 } else if (rdev->use_count > 1) {
1698 if (rdev->constraints &&
1699 (rdev->constraints->valid_ops_mask &
1700 REGULATOR_CHANGE_DRMS))
1701 drms_uA_update(rdev);
1710 * regulator_disable - disable regulator output
1711 * @regulator: regulator source
1713 * Disable the regulator output voltage or current. Calls to
1714 * regulator_enable() must be balanced with calls to
1715 * regulator_disable().
1717 * NOTE: this will only disable the regulator output if no other consumer
1718 * devices have it enabled, the regulator device supports disabling and
1719 * machine constraints permit this operation.
1721 int regulator_disable(struct regulator *regulator)
1723 struct regulator_dev *rdev = regulator->rdev;
1726 if (regulator->always_on)
1729 mutex_lock(&rdev->mutex);
1730 ret = _regulator_disable(rdev);
1731 mutex_unlock(&rdev->mutex);
1733 if (ret == 0 && rdev->supply)
1734 regulator_disable(rdev->supply);
1738 EXPORT_SYMBOL_GPL(regulator_disable);
1740 /* locks held by regulator_force_disable() */
1741 static int _regulator_force_disable(struct regulator_dev *rdev)
1745 ret = _regulator_do_disable(rdev);
1747 rdev_err(rdev, "failed to force disable\n");
1751 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1752 REGULATOR_EVENT_DISABLE, NULL);
1758 * regulator_force_disable - force disable regulator output
1759 * @regulator: regulator source
1761 * Forcibly disable the regulator output voltage or current.
1762 * NOTE: this *will* disable the regulator output even if other consumer
1763 * devices have it enabled. This should be used for situations when device
1764 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1766 int regulator_force_disable(struct regulator *regulator)
1768 struct regulator_dev *rdev = regulator->rdev;
1771 mutex_lock(&rdev->mutex);
1772 regulator->uA_load = 0;
1773 ret = _regulator_force_disable(regulator->rdev);
1774 mutex_unlock(&rdev->mutex);
1777 while (rdev->open_count--)
1778 regulator_disable(rdev->supply);
1782 EXPORT_SYMBOL_GPL(regulator_force_disable);
1784 static void regulator_disable_work(struct work_struct *work)
1786 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1790 mutex_lock(&rdev->mutex);
1792 BUG_ON(!rdev->deferred_disables);
1794 count = rdev->deferred_disables;
1795 rdev->deferred_disables = 0;
1797 for (i = 0; i < count; i++) {
1798 ret = _regulator_disable(rdev);
1800 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1803 mutex_unlock(&rdev->mutex);
1806 for (i = 0; i < count; i++) {
1807 ret = regulator_disable(rdev->supply);
1810 "Supply disable failed: %d\n", ret);
1817 * regulator_disable_deferred - disable regulator output with delay
1818 * @regulator: regulator source
1819 * @ms: miliseconds until the regulator is disabled
1821 * Execute regulator_disable() on the regulator after a delay. This
1822 * is intended for use with devices that require some time to quiesce.
1824 * NOTE: this will only disable the regulator output if no other consumer
1825 * devices have it enabled, the regulator device supports disabling and
1826 * machine constraints permit this operation.
1828 int regulator_disable_deferred(struct regulator *regulator, int ms)
1830 struct regulator_dev *rdev = regulator->rdev;
1833 if (regulator->always_on)
1837 return regulator_disable(regulator);
1839 mutex_lock(&rdev->mutex);
1840 rdev->deferred_disables++;
1841 mutex_unlock(&rdev->mutex);
1843 ret = queue_delayed_work(system_power_efficient_wq,
1844 &rdev->disable_work,
1845 msecs_to_jiffies(ms));
1851 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1854 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1856 * @rdev: regulator to operate on
1858 * Regulators that use regmap for their register I/O can set the
1859 * enable_reg and enable_mask fields in their descriptor and then use
1860 * this as their is_enabled operation, saving some code.
1862 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1867 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1871 if (rdev->desc->enable_is_inverted)
1872 return (val & rdev->desc->enable_mask) == 0;
1874 return (val & rdev->desc->enable_mask) != 0;
1876 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1879 * regulator_enable_regmap - standard enable() for regmap users
1881 * @rdev: regulator to operate on
1883 * Regulators that use regmap for their register I/O can set the
1884 * enable_reg and enable_mask fields in their descriptor and then use
1885 * this as their enable() operation, saving some code.
1887 int regulator_enable_regmap(struct regulator_dev *rdev)
1891 if (rdev->desc->enable_is_inverted)
1894 val = rdev->desc->enable_mask;
1896 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1897 rdev->desc->enable_mask, val);
1899 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1902 * regulator_disable_regmap - standard disable() for regmap users
1904 * @rdev: regulator to operate on
1906 * Regulators that use regmap for their register I/O can set the
1907 * enable_reg and enable_mask fields in their descriptor and then use
1908 * this as their disable() operation, saving some code.
1910 int regulator_disable_regmap(struct regulator_dev *rdev)
1914 if (rdev->desc->enable_is_inverted)
1915 val = rdev->desc->enable_mask;
1919 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1920 rdev->desc->enable_mask, val);
1922 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1924 static int _regulator_is_enabled(struct regulator_dev *rdev)
1926 /* A GPIO control always takes precedence */
1928 return rdev->ena_gpio_state;
1930 /* If we don't know then assume that the regulator is always on */
1931 if (!rdev->desc->ops->is_enabled)
1934 return rdev->desc->ops->is_enabled(rdev);
1938 * regulator_is_enabled - is the regulator output enabled
1939 * @regulator: regulator source
1941 * Returns positive if the regulator driver backing the source/client
1942 * has requested that the device be enabled, zero if it hasn't, else a
1943 * negative errno code.
1945 * Note that the device backing this regulator handle can have multiple
1946 * users, so it might be enabled even if regulator_enable() was never
1947 * called for this particular source.
1949 int regulator_is_enabled(struct regulator *regulator)
1953 if (regulator->always_on)
1956 mutex_lock(®ulator->rdev->mutex);
1957 ret = _regulator_is_enabled(regulator->rdev);
1958 mutex_unlock(®ulator->rdev->mutex);
1962 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1965 * regulator_can_change_voltage - check if regulator can change voltage
1966 * @regulator: regulator source
1968 * Returns positive if the regulator driver backing the source/client
1969 * can change its voltage, false otherwise. Usefull for detecting fixed
1970 * or dummy regulators and disabling voltage change logic in the client
1973 int regulator_can_change_voltage(struct regulator *regulator)
1975 struct regulator_dev *rdev = regulator->rdev;
1977 if (rdev->constraints &&
1978 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1979 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
1982 if (rdev->desc->continuous_voltage_range &&
1983 rdev->constraints->min_uV && rdev->constraints->max_uV &&
1984 rdev->constraints->min_uV != rdev->constraints->max_uV)
1990 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
1993 * regulator_count_voltages - count regulator_list_voltage() selectors
1994 * @regulator: regulator source
1996 * Returns number of selectors, or negative errno. Selectors are
1997 * numbered starting at zero, and typically correspond to bitfields
1998 * in hardware registers.
2000 int regulator_count_voltages(struct regulator *regulator)
2002 struct regulator_dev *rdev = regulator->rdev;
2004 return rdev->desc->n_voltages ? : -EINVAL;
2006 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2009 * regulator_list_voltage_linear - List voltages with simple calculation
2011 * @rdev: Regulator device
2012 * @selector: Selector to convert into a voltage
2014 * Regulators with a simple linear mapping between voltages and
2015 * selectors can set min_uV and uV_step in the regulator descriptor
2016 * and then use this function as their list_voltage() operation,
2018 int regulator_list_voltage_linear(struct regulator_dev *rdev,
2019 unsigned int selector)
2021 if (selector >= rdev->desc->n_voltages)
2023 if (selector < rdev->desc->linear_min_sel)
2026 selector -= rdev->desc->linear_min_sel;
2028 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2030 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2033 * regulator_list_voltage_table - List voltages with table based mapping
2035 * @rdev: Regulator device
2036 * @selector: Selector to convert into a voltage
2038 * Regulators with table based mapping between voltages and
2039 * selectors can set volt_table in the regulator descriptor
2040 * and then use this function as their list_voltage() operation.
2042 int regulator_list_voltage_table(struct regulator_dev *rdev,
2043 unsigned int selector)
2045 if (!rdev->desc->volt_table) {
2046 BUG_ON(!rdev->desc->volt_table);
2050 if (selector >= rdev->desc->n_voltages)
2053 return rdev->desc->volt_table[selector];
2055 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2058 * regulator_list_voltage - enumerate supported voltages
2059 * @regulator: regulator source
2060 * @selector: identify voltage to list
2061 * Context: can sleep
2063 * Returns a voltage that can be passed to @regulator_set_voltage(),
2064 * zero if this selector code can't be used on this system, or a
2067 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2069 struct regulator_dev *rdev = regulator->rdev;
2070 struct regulator_ops *ops = rdev->desc->ops;
2073 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2076 mutex_lock(&rdev->mutex);
2077 ret = ops->list_voltage(rdev, selector);
2078 mutex_unlock(&rdev->mutex);
2081 if (ret < rdev->constraints->min_uV)
2083 else if (ret > rdev->constraints->max_uV)
2089 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2092 * regulator_is_supported_voltage - check if a voltage range can be supported
2094 * @regulator: Regulator to check.
2095 * @min_uV: Minimum required voltage in uV.
2096 * @max_uV: Maximum required voltage in uV.
2098 * Returns a boolean or a negative error code.
2100 int regulator_is_supported_voltage(struct regulator *regulator,
2101 int min_uV, int max_uV)
2103 struct regulator_dev *rdev = regulator->rdev;
2104 int i, voltages, ret;
2106 /* If we can't change voltage check the current voltage */
2107 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2108 ret = regulator_get_voltage(regulator);
2110 return (min_uV <= ret && ret <= max_uV);
2115 /* Any voltage within constrains range is fine? */
2116 if (rdev->desc->continuous_voltage_range)
2117 return min_uV >= rdev->constraints->min_uV &&
2118 max_uV <= rdev->constraints->max_uV;
2120 ret = regulator_count_voltages(regulator);
2125 for (i = 0; i < voltages; i++) {
2126 ret = regulator_list_voltage(regulator, i);
2128 if (ret >= min_uV && ret <= max_uV)
2134 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2137 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2139 * @rdev: regulator to operate on
2141 * Regulators that use regmap for their register I/O can set the
2142 * vsel_reg and vsel_mask fields in their descriptor and then use this
2143 * as their get_voltage_vsel operation, saving some code.
2145 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2150 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2154 val &= rdev->desc->vsel_mask;
2155 val >>= ffs(rdev->desc->vsel_mask) - 1;
2159 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2162 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2164 * @rdev: regulator to operate on
2165 * @sel: Selector to set
2167 * Regulators that use regmap for their register I/O can set the
2168 * vsel_reg and vsel_mask fields in their descriptor and then use this
2169 * as their set_voltage_vsel operation, saving some code.
2171 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2175 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2177 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2178 rdev->desc->vsel_mask, sel);
2182 if (rdev->desc->apply_bit)
2183 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2184 rdev->desc->apply_bit,
2185 rdev->desc->apply_bit);
2188 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2191 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2193 * @rdev: Regulator to operate on
2194 * @min_uV: Lower bound for voltage
2195 * @max_uV: Upper bound for voltage
2197 * Drivers implementing set_voltage_sel() and list_voltage() can use
2198 * this as their map_voltage() operation. It will find a suitable
2199 * voltage by calling list_voltage() until it gets something in bounds
2200 * for the requested voltages.
2202 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2203 int min_uV, int max_uV)
2205 int best_val = INT_MAX;
2209 /* Find the smallest voltage that falls within the specified
2212 for (i = 0; i < rdev->desc->n_voltages; i++) {
2213 ret = rdev->desc->ops->list_voltage(rdev, i);
2217 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2223 if (best_val != INT_MAX)
2228 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2231 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2233 * @rdev: Regulator to operate on
2234 * @min_uV: Lower bound for voltage
2235 * @max_uV: Upper bound for voltage
2237 * Drivers that have ascendant voltage list can use this as their
2238 * map_voltage() operation.
2240 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
2241 int min_uV, int max_uV)
2245 for (i = 0; i < rdev->desc->n_voltages; i++) {
2246 ret = rdev->desc->ops->list_voltage(rdev, i);
2253 if (ret >= min_uV && ret <= max_uV)
2259 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
2262 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2264 * @rdev: Regulator to operate on
2265 * @min_uV: Lower bound for voltage
2266 * @max_uV: Upper bound for voltage
2268 * Drivers providing min_uV and uV_step in their regulator_desc can
2269 * use this as their map_voltage() operation.
2271 int regulator_map_voltage_linear(struct regulator_dev *rdev,
2272 int min_uV, int max_uV)
2276 /* Allow uV_step to be 0 for fixed voltage */
2277 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2278 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2284 if (!rdev->desc->uV_step) {
2285 BUG_ON(!rdev->desc->uV_step);
2289 if (min_uV < rdev->desc->min_uV)
2290 min_uV = rdev->desc->min_uV;
2292 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2296 ret += rdev->desc->linear_min_sel;
2298 /* Map back into a voltage to verify we're still in bounds */
2299 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2300 if (voltage < min_uV || voltage > max_uV)
2305 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2307 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2308 int min_uV, int max_uV,
2311 struct pre_voltage_change_data data;
2314 data.old_uV = _regulator_get_voltage(rdev);
2315 data.min_uV = min_uV;
2316 data.max_uV = max_uV;
2317 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2319 if (ret & NOTIFY_STOP_MASK)
2322 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2326 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2327 (void *)data.old_uV);
2332 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2333 int uV, unsigned selector)
2335 struct pre_voltage_change_data data;
2338 data.old_uV = _regulator_get_voltage(rdev);
2341 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2343 if (ret & NOTIFY_STOP_MASK)
2346 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2350 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2351 (void *)data.old_uV);
2356 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2357 int min_uV, int max_uV)
2362 unsigned int selector;
2363 int old_selector = -1;
2365 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2367 min_uV += rdev->constraints->uV_offset;
2368 max_uV += rdev->constraints->uV_offset;
2371 * If we can't obtain the old selector there is not enough
2372 * info to call set_voltage_time_sel().
2374 if (_regulator_is_enabled(rdev) &&
2375 rdev->desc->ops->set_voltage_time_sel &&
2376 rdev->desc->ops->get_voltage_sel) {
2377 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2378 if (old_selector < 0)
2379 return old_selector;
2382 if (rdev->desc->ops->set_voltage) {
2383 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2387 if (rdev->desc->ops->list_voltage)
2388 best_val = rdev->desc->ops->list_voltage(rdev,
2391 best_val = _regulator_get_voltage(rdev);
2394 } else if (rdev->desc->ops->set_voltage_sel) {
2395 if (rdev->desc->ops->map_voltage) {
2396 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2399 if (rdev->desc->ops->list_voltage ==
2400 regulator_list_voltage_linear)
2401 ret = regulator_map_voltage_linear(rdev,
2404 ret = regulator_map_voltage_iterate(rdev,
2409 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2410 if (min_uV <= best_val && max_uV >= best_val) {
2412 if (old_selector == selector)
2415 ret = _regulator_call_set_voltage_sel(
2416 rdev, best_val, selector);
2425 /* Call set_voltage_time_sel if successfully obtained old_selector */
2426 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2427 old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
2429 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2430 old_selector, selector);
2432 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2437 /* Insert any necessary delays */
2438 if (delay >= 1000) {
2439 mdelay(delay / 1000);
2440 udelay(delay % 1000);
2446 if (ret == 0 && best_val >= 0) {
2447 unsigned long data = best_val;
2449 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2453 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2459 * regulator_set_voltage - set regulator output voltage
2460 * @regulator: regulator source
2461 * @min_uV: Minimum required voltage in uV
2462 * @max_uV: Maximum acceptable voltage in uV
2464 * Sets a voltage regulator to the desired output voltage. This can be set
2465 * during any regulator state. IOW, regulator can be disabled or enabled.
2467 * If the regulator is enabled then the voltage will change to the new value
2468 * immediately otherwise if the regulator is disabled the regulator will
2469 * output at the new voltage when enabled.
2471 * NOTE: If the regulator is shared between several devices then the lowest
2472 * request voltage that meets the system constraints will be used.
2473 * Regulator system constraints must be set for this regulator before
2474 * calling this function otherwise this call will fail.
2476 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2478 struct regulator_dev *rdev = regulator->rdev;
2480 int old_min_uV, old_max_uV;
2482 mutex_lock(&rdev->mutex);
2484 /* If we're setting the same range as last time the change
2485 * should be a noop (some cpufreq implementations use the same
2486 * voltage for multiple frequencies, for example).
2488 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2492 if (!rdev->desc->ops->set_voltage &&
2493 !rdev->desc->ops->set_voltage_sel) {
2498 /* constraints check */
2499 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2503 /* restore original values in case of error */
2504 old_min_uV = regulator->min_uV;
2505 old_max_uV = regulator->max_uV;
2506 regulator->min_uV = min_uV;
2507 regulator->max_uV = max_uV;
2509 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2513 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2518 mutex_unlock(&rdev->mutex);
2521 regulator->min_uV = old_min_uV;
2522 regulator->max_uV = old_max_uV;
2523 mutex_unlock(&rdev->mutex);
2526 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2529 * regulator_set_voltage_time - get raise/fall time
2530 * @regulator: regulator source
2531 * @old_uV: starting voltage in microvolts
2532 * @new_uV: target voltage in microvolts
2534 * Provided with the starting and ending voltage, this function attempts to
2535 * calculate the time in microseconds required to rise or fall to this new
2538 int regulator_set_voltage_time(struct regulator *regulator,
2539 int old_uV, int new_uV)
2541 struct regulator_dev *rdev = regulator->rdev;
2542 struct regulator_ops *ops = rdev->desc->ops;
2548 /* Currently requires operations to do this */
2549 if (!ops->list_voltage || !ops->set_voltage_time_sel
2550 || !rdev->desc->n_voltages)
2553 for (i = 0; i < rdev->desc->n_voltages; i++) {
2554 /* We only look for exact voltage matches here */
2555 voltage = regulator_list_voltage(regulator, i);
2560 if (voltage == old_uV)
2562 if (voltage == new_uV)
2566 if (old_sel < 0 || new_sel < 0)
2569 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2571 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2574 * regulator_set_voltage_time_sel - get raise/fall time
2575 * @rdev: regulator source device
2576 * @old_selector: selector for starting voltage
2577 * @new_selector: selector for target voltage
2579 * Provided with the starting and target voltage selectors, this function
2580 * returns time in microseconds required to rise or fall to this new voltage
2582 * Drivers providing ramp_delay in regulation_constraints can use this as their
2583 * set_voltage_time_sel() operation.
2585 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2586 unsigned int old_selector,
2587 unsigned int new_selector)
2589 unsigned int ramp_delay = 0;
2590 int old_volt, new_volt;
2592 if (rdev->constraints->ramp_delay)
2593 ramp_delay = rdev->constraints->ramp_delay;
2594 else if (rdev->desc->ramp_delay)
2595 ramp_delay = rdev->desc->ramp_delay;
2597 if (ramp_delay == 0) {
2598 rdev_warn(rdev, "ramp_delay not set\n");
2603 if (!rdev->desc->ops->list_voltage)
2606 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2607 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2609 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2611 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2614 * regulator_sync_voltage - re-apply last regulator output voltage
2615 * @regulator: regulator source
2617 * Re-apply the last configured voltage. This is intended to be used
2618 * where some external control source the consumer is cooperating with
2619 * has caused the configured voltage to change.
2621 int regulator_sync_voltage(struct regulator *regulator)
2623 struct regulator_dev *rdev = regulator->rdev;
2624 int ret, min_uV, max_uV;
2626 mutex_lock(&rdev->mutex);
2628 if (!rdev->desc->ops->set_voltage &&
2629 !rdev->desc->ops->set_voltage_sel) {
2634 /* This is only going to work if we've had a voltage configured. */
2635 if (!regulator->min_uV && !regulator->max_uV) {
2640 min_uV = regulator->min_uV;
2641 max_uV = regulator->max_uV;
2643 /* This should be a paranoia check... */
2644 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2648 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2652 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2655 mutex_unlock(&rdev->mutex);
2658 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2660 static int _regulator_get_voltage(struct regulator_dev *rdev)
2664 if (rdev->desc->ops->get_voltage_sel) {
2665 sel = rdev->desc->ops->get_voltage_sel(rdev);
2668 ret = rdev->desc->ops->list_voltage(rdev, sel);
2669 } else if (rdev->desc->ops->get_voltage) {
2670 ret = rdev->desc->ops->get_voltage(rdev);
2671 } else if (rdev->desc->ops->list_voltage) {
2672 ret = rdev->desc->ops->list_voltage(rdev, 0);
2679 return ret - rdev->constraints->uV_offset;
2683 * regulator_get_voltage - get regulator output voltage
2684 * @regulator: regulator source
2686 * This returns the current regulator voltage in uV.
2688 * NOTE: If the regulator is disabled it will return the voltage value. This
2689 * function should not be used to determine regulator state.
2691 int regulator_get_voltage(struct regulator *regulator)
2695 mutex_lock(®ulator->rdev->mutex);
2697 ret = _regulator_get_voltage(regulator->rdev);
2699 mutex_unlock(®ulator->rdev->mutex);
2703 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2706 * regulator_set_current_limit - set regulator output current limit
2707 * @regulator: regulator source
2708 * @min_uA: Minimum supported current in uA
2709 * @max_uA: Maximum supported current in uA
2711 * Sets current sink to the desired output current. This can be set during
2712 * any regulator state. IOW, regulator can be disabled or enabled.
2714 * If the regulator is enabled then the current will change to the new value
2715 * immediately otherwise if the regulator is disabled the regulator will
2716 * output at the new current when enabled.
2718 * NOTE: Regulator system constraints must be set for this regulator before
2719 * calling this function otherwise this call will fail.
2721 int regulator_set_current_limit(struct regulator *regulator,
2722 int min_uA, int max_uA)
2724 struct regulator_dev *rdev = regulator->rdev;
2727 mutex_lock(&rdev->mutex);
2730 if (!rdev->desc->ops->set_current_limit) {
2735 /* constraints check */
2736 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2740 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2742 mutex_unlock(&rdev->mutex);
2745 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2747 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2751 mutex_lock(&rdev->mutex);
2754 if (!rdev->desc->ops->get_current_limit) {
2759 ret = rdev->desc->ops->get_current_limit(rdev);
2761 mutex_unlock(&rdev->mutex);
2766 * regulator_get_current_limit - get regulator output current
2767 * @regulator: regulator source
2769 * This returns the current supplied by the specified current sink in uA.
2771 * NOTE: If the regulator is disabled it will return the current value. This
2772 * function should not be used to determine regulator state.
2774 int regulator_get_current_limit(struct regulator *regulator)
2776 return _regulator_get_current_limit(regulator->rdev);
2778 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2780 int regulator_is_supported_mode(struct regulator *regulator, int *mode)
2782 struct regulator_dev *rdev = regulator->rdev;
2785 mutex_lock(&rdev->mutex);
2787 ret = regulator_mode_constrain(rdev, mode);
2789 mutex_unlock(&rdev->mutex);
2795 * regulator_set_mode - set regulator operating mode
2796 * @regulator: regulator source
2797 * @mode: operating mode - one of the REGULATOR_MODE constants
2799 * Set regulator operating mode to increase regulator efficiency or improve
2800 * regulation performance.
2802 * NOTE: Regulator system constraints must be set for this regulator before
2803 * calling this function otherwise this call will fail.
2805 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2807 struct regulator_dev *rdev = regulator->rdev;
2809 int regulator_curr_mode;
2811 mutex_lock(&rdev->mutex);
2814 if (!rdev->desc->ops->set_mode) {
2819 /* return if the same mode is requested */
2820 if (rdev->desc->ops->get_mode) {
2821 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2822 if (regulator_curr_mode == mode) {
2828 /* constraints check */
2829 ret = regulator_mode_constrain(rdev, &mode);
2833 ret = rdev->desc->ops->set_mode(rdev, mode);
2835 mutex_unlock(&rdev->mutex);
2838 EXPORT_SYMBOL_GPL(regulator_set_mode);
2840 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2844 mutex_lock(&rdev->mutex);
2847 if (!rdev->desc->ops->get_mode) {
2852 ret = rdev->desc->ops->get_mode(rdev);
2854 mutex_unlock(&rdev->mutex);
2859 * regulator_get_mode - get regulator operating mode
2860 * @regulator: regulator source
2862 * Get the current regulator operating mode.
2864 unsigned int regulator_get_mode(struct regulator *regulator)
2866 return _regulator_get_mode(regulator->rdev);
2868 EXPORT_SYMBOL_GPL(regulator_get_mode);
2871 * regulator_set_optimum_mode - set regulator optimum operating mode
2872 * @regulator: regulator source
2873 * @uA_load: load current
2875 * Notifies the regulator core of a new device load. This is then used by
2876 * DRMS (if enabled by constraints) to set the most efficient regulator
2877 * operating mode for the new regulator loading.
2879 * Consumer devices notify their supply regulator of the maximum power
2880 * they will require (can be taken from device datasheet in the power
2881 * consumption tables) when they change operational status and hence power
2882 * state. Examples of operational state changes that can affect power
2883 * consumption are :-
2885 * o Device is opened / closed.
2886 * o Device I/O is about to begin or has just finished.
2887 * o Device is idling in between work.
2889 * This information is also exported via sysfs to userspace.
2891 * DRMS will sum the total requested load on the regulator and change
2892 * to the most efficient operating mode if platform constraints allow.
2894 * Returns the new regulator mode or error.
2896 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2898 struct regulator_dev *rdev = regulator->rdev;
2899 struct regulator *consumer;
2900 int ret, output_uV, input_uV = 0, total_uA_load = 0;
2904 input_uV = regulator_get_voltage(rdev->supply);
2906 mutex_lock(&rdev->mutex);
2909 * first check to see if we can set modes at all, otherwise just
2910 * tell the consumer everything is OK.
2912 regulator->uA_load = uA_load;
2913 ret = regulator_check_drms(rdev);
2919 if (!rdev->desc->ops->get_optimum_mode)
2923 * we can actually do this so any errors are indicators of
2924 * potential real failure.
2928 if (!rdev->desc->ops->set_mode)
2931 /* get output voltage */
2932 output_uV = _regulator_get_voltage(rdev);
2933 if (output_uV <= 0) {
2934 rdev_err(rdev, "invalid output voltage found\n");
2938 /* No supply? Use constraint voltage */
2940 input_uV = rdev->constraints->input_uV;
2941 if (input_uV <= 0) {
2942 rdev_err(rdev, "invalid input voltage found\n");
2946 /* calc total requested load for this regulator */
2947 list_for_each_entry(consumer, &rdev->consumer_list, list)
2948 total_uA_load += consumer->uA_load;
2950 mode = rdev->desc->ops->get_optimum_mode(rdev,
2951 input_uV, output_uV,
2953 ret = regulator_mode_constrain(rdev, &mode);
2955 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2956 total_uA_load, input_uV, output_uV);
2960 ret = rdev->desc->ops->set_mode(rdev, mode);
2962 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2967 mutex_unlock(&rdev->mutex);
2970 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2973 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2975 * @rdev: device to operate on.
2976 * @enable: state to set.
2978 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2983 val = rdev->desc->bypass_mask;
2987 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2988 rdev->desc->bypass_mask, val);
2990 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2993 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2995 * @rdev: device to operate on.
2996 * @enable: current state.
2998 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
3003 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
3007 *enable = val & rdev->desc->bypass_mask;
3011 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
3014 * regulator_allow_bypass - allow the regulator to go into bypass mode
3016 * @regulator: Regulator to configure
3017 * @enable: enable or disable bypass mode
3019 * Allow the regulator to go into bypass mode if all other consumers
3020 * for the regulator also enable bypass mode and the machine
3021 * constraints allow this. Bypass mode means that the regulator is
3022 * simply passing the input directly to the output with no regulation.
3024 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3026 struct regulator_dev *rdev = regulator->rdev;
3029 if (!rdev->desc->ops->set_bypass)
3032 if (rdev->constraints &&
3033 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3036 mutex_lock(&rdev->mutex);
3038 if (enable && !regulator->bypass) {
3039 rdev->bypass_count++;
3041 if (rdev->bypass_count == rdev->open_count) {
3042 ret = rdev->desc->ops->set_bypass(rdev, enable);
3044 rdev->bypass_count--;
3047 } else if (!enable && regulator->bypass) {
3048 rdev->bypass_count--;
3050 if (rdev->bypass_count != rdev->open_count) {
3051 ret = rdev->desc->ops->set_bypass(rdev, enable);
3053 rdev->bypass_count++;
3058 regulator->bypass = enable;
3060 mutex_unlock(&rdev->mutex);
3064 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3067 * regulator_register_notifier - register regulator event notifier
3068 * @regulator: regulator source
3069 * @nb: notifier block
3071 * Register notifier block to receive regulator events.
3073 int regulator_register_notifier(struct regulator *regulator,
3074 struct notifier_block *nb)
3076 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3079 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3082 * regulator_unregister_notifier - unregister regulator event notifier
3083 * @regulator: regulator source
3084 * @nb: notifier block
3086 * Unregister regulator event notifier block.
3088 int regulator_unregister_notifier(struct regulator *regulator,
3089 struct notifier_block *nb)
3091 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3094 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3096 /* notify regulator consumers and downstream regulator consumers.
3097 * Note mutex must be held by caller.
3099 static int _notifier_call_chain(struct regulator_dev *rdev,
3100 unsigned long event, void *data)
3102 /* call rdev chain first */
3103 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3107 * regulator_bulk_get - get multiple regulator consumers
3109 * @dev: Device to supply
3110 * @num_consumers: Number of consumers to register
3111 * @consumers: Configuration of consumers; clients are stored here.
3113 * @return 0 on success, an errno on failure.
3115 * This helper function allows drivers to get several regulator
3116 * consumers in one operation. If any of the regulators cannot be
3117 * acquired then any regulators that were allocated will be freed
3118 * before returning to the caller.
3120 int regulator_bulk_get(struct device *dev, int num_consumers,
3121 struct regulator_bulk_data *consumers)
3126 for (i = 0; i < num_consumers; i++)
3127 consumers[i].consumer = NULL;
3129 for (i = 0; i < num_consumers; i++) {
3130 consumers[i].consumer = regulator_get(dev,
3131 consumers[i].supply);
3132 if (IS_ERR(consumers[i].consumer)) {
3133 ret = PTR_ERR(consumers[i].consumer);
3134 dev_err(dev, "Failed to get supply '%s': %d\n",
3135 consumers[i].supply, ret);
3136 consumers[i].consumer = NULL;
3145 regulator_put(consumers[i].consumer);
3149 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3151 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3153 struct regulator_bulk_data *bulk = data;
3155 bulk->ret = regulator_enable(bulk->consumer);
3159 * regulator_bulk_enable - enable multiple regulator consumers
3161 * @num_consumers: Number of consumers
3162 * @consumers: Consumer data; clients are stored here.
3163 * @return 0 on success, an errno on failure
3165 * This convenience API allows consumers to enable multiple regulator
3166 * clients in a single API call. If any consumers cannot be enabled
3167 * then any others that were enabled will be disabled again prior to
3170 int regulator_bulk_enable(int num_consumers,
3171 struct regulator_bulk_data *consumers)
3173 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3177 for (i = 0; i < num_consumers; i++) {
3178 if (consumers[i].consumer->always_on)
3179 consumers[i].ret = 0;
3181 async_schedule_domain(regulator_bulk_enable_async,
3182 &consumers[i], &async_domain);
3185 async_synchronize_full_domain(&async_domain);
3187 /* If any consumer failed we need to unwind any that succeeded */
3188 for (i = 0; i < num_consumers; i++) {
3189 if (consumers[i].ret != 0) {
3190 ret = consumers[i].ret;
3198 for (i = 0; i < num_consumers; i++) {
3199 if (consumers[i].ret < 0)
3200 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3203 regulator_disable(consumers[i].consumer);
3208 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3211 * regulator_bulk_disable - disable multiple regulator consumers
3213 * @num_consumers: Number of consumers
3214 * @consumers: Consumer data; clients are stored here.
3215 * @return 0 on success, an errno on failure
3217 * This convenience API allows consumers to disable multiple regulator
3218 * clients in a single API call. If any consumers cannot be disabled
3219 * then any others that were disabled will be enabled again prior to
3222 int regulator_bulk_disable(int num_consumers,
3223 struct regulator_bulk_data *consumers)
3228 for (i = num_consumers - 1; i >= 0; --i) {
3229 ret = regulator_disable(consumers[i].consumer);
3237 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3238 for (++i; i < num_consumers; ++i) {
3239 r = regulator_enable(consumers[i].consumer);
3241 pr_err("Failed to reename %s: %d\n",
3242 consumers[i].supply, r);
3247 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3250 * regulator_bulk_force_disable - force disable multiple regulator consumers
3252 * @num_consumers: Number of consumers
3253 * @consumers: Consumer data; clients are stored here.
3254 * @return 0 on success, an errno on failure
3256 * This convenience API allows consumers to forcibly disable multiple regulator
3257 * clients in a single API call.
3258 * NOTE: This should be used for situations when device damage will
3259 * likely occur if the regulators are not disabled (e.g. over temp).
3260 * Although regulator_force_disable function call for some consumers can
3261 * return error numbers, the function is called for all consumers.
3263 int regulator_bulk_force_disable(int num_consumers,
3264 struct regulator_bulk_data *consumers)
3269 for (i = 0; i < num_consumers; i++)
3271 regulator_force_disable(consumers[i].consumer);
3273 for (i = 0; i < num_consumers; i++) {
3274 if (consumers[i].ret != 0) {
3275 ret = consumers[i].ret;
3284 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3287 * regulator_bulk_free - free multiple regulator consumers
3289 * @num_consumers: Number of consumers
3290 * @consumers: Consumer data; clients are stored here.
3292 * This convenience API allows consumers to free multiple regulator
3293 * clients in a single API call.
3295 void regulator_bulk_free(int num_consumers,
3296 struct regulator_bulk_data *consumers)
3300 for (i = 0; i < num_consumers; i++) {
3301 regulator_put(consumers[i].consumer);
3302 consumers[i].consumer = NULL;
3305 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3308 * regulator_notifier_call_chain - call regulator event notifier
3309 * @rdev: regulator source
3310 * @event: notifier block
3311 * @data: callback-specific data.
3313 * Called by regulator drivers to notify clients a regulator event has
3314 * occurred. We also notify regulator clients downstream.
3315 * Note lock must be held by caller.
3317 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3318 unsigned long event, void *data)
3320 _notifier_call_chain(rdev, event, data);
3324 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3327 * regulator_mode_to_status - convert a regulator mode into a status
3329 * @mode: Mode to convert
3331 * Convert a regulator mode into a status.
3333 int regulator_mode_to_status(unsigned int mode)
3336 case REGULATOR_MODE_FAST:
3337 return REGULATOR_STATUS_FAST;
3338 case REGULATOR_MODE_NORMAL:
3339 return REGULATOR_STATUS_NORMAL;
3340 case REGULATOR_MODE_IDLE:
3341 return REGULATOR_STATUS_IDLE;
3342 case REGULATOR_MODE_STANDBY:
3343 return REGULATOR_STATUS_STANDBY;
3345 return REGULATOR_STATUS_UNDEFINED;
3348 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3351 * To avoid cluttering sysfs (and memory) with useless state, only
3352 * create attributes that can be meaningfully displayed.
3354 static int add_regulator_attributes(struct regulator_dev *rdev)
3356 struct device *dev = &rdev->dev;
3357 struct regulator_ops *ops = rdev->desc->ops;
3360 /* some attributes need specific methods to be displayed */
3361 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3362 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3363 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3364 status = device_create_file(dev, &dev_attr_microvolts);
3368 if (ops->get_current_limit) {
3369 status = device_create_file(dev, &dev_attr_microamps);
3373 if (ops->get_mode) {
3374 status = device_create_file(dev, &dev_attr_opmode);
3378 if (rdev->ena_pin || ops->is_enabled) {
3379 status = device_create_file(dev, &dev_attr_state);
3383 if (ops->get_status) {
3384 status = device_create_file(dev, &dev_attr_status);
3388 if (ops->get_bypass) {
3389 status = device_create_file(dev, &dev_attr_bypass);
3394 /* some attributes are type-specific */
3395 if (rdev->desc->type == REGULATOR_CURRENT) {
3396 status = device_create_file(dev, &dev_attr_requested_microamps);
3401 /* all the other attributes exist to support constraints;
3402 * don't show them if there are no constraints, or if the
3403 * relevant supporting methods are missing.
3405 if (!rdev->constraints)
3408 /* constraints need specific supporting methods */
3409 if (ops->set_voltage || ops->set_voltage_sel) {
3410 status = device_create_file(dev, &dev_attr_min_microvolts);
3413 status = device_create_file(dev, &dev_attr_max_microvolts);
3417 if (ops->set_current_limit) {
3418 status = device_create_file(dev, &dev_attr_min_microamps);
3421 status = device_create_file(dev, &dev_attr_max_microamps);
3426 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3429 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3432 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3436 if (ops->set_suspend_voltage) {
3437 status = device_create_file(dev,
3438 &dev_attr_suspend_standby_microvolts);
3441 status = device_create_file(dev,
3442 &dev_attr_suspend_mem_microvolts);
3445 status = device_create_file(dev,
3446 &dev_attr_suspend_disk_microvolts);
3451 if (ops->set_suspend_mode) {
3452 status = device_create_file(dev,
3453 &dev_attr_suspend_standby_mode);
3456 status = device_create_file(dev,
3457 &dev_attr_suspend_mem_mode);
3460 status = device_create_file(dev,
3461 &dev_attr_suspend_disk_mode);
3469 static void rdev_init_debugfs(struct regulator_dev *rdev)
3471 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3472 if (!rdev->debugfs) {
3473 rdev_warn(rdev, "Failed to create debugfs directory\n");
3477 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3479 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3481 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3482 &rdev->bypass_count);
3486 * regulator_register - register regulator
3487 * @regulator_desc: regulator to register
3488 * @config: runtime configuration for regulator
3490 * Called by regulator drivers to register a regulator.
3491 * Returns a valid pointer to struct regulator_dev on success
3492 * or an ERR_PTR() on error.
3494 struct regulator_dev *
3495 regulator_register(const struct regulator_desc *regulator_desc,
3496 const struct regulator_config *config)
3498 const struct regulation_constraints *constraints = NULL;
3499 const struct regulator_init_data *init_data;
3500 static atomic_t regulator_no = ATOMIC_INIT(0);
3501 struct regulator_dev *rdev;
3504 const char *supply = NULL;
3506 if (regulator_desc == NULL || config == NULL)
3507 return ERR_PTR(-EINVAL);
3512 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3513 return ERR_PTR(-EINVAL);
3515 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3516 regulator_desc->type != REGULATOR_CURRENT)
3517 return ERR_PTR(-EINVAL);
3519 /* Only one of each should be implemented */
3520 WARN_ON(regulator_desc->ops->get_voltage &&
3521 regulator_desc->ops->get_voltage_sel);
3522 WARN_ON(regulator_desc->ops->set_voltage &&
3523 regulator_desc->ops->set_voltage_sel);
3525 /* If we're using selectors we must implement list_voltage. */
3526 if (regulator_desc->ops->get_voltage_sel &&
3527 !regulator_desc->ops->list_voltage) {
3528 return ERR_PTR(-EINVAL);
3530 if (regulator_desc->ops->set_voltage_sel &&
3531 !regulator_desc->ops->list_voltage) {
3532 return ERR_PTR(-EINVAL);
3535 init_data = config->init_data;
3537 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3539 return ERR_PTR(-ENOMEM);
3541 mutex_lock(®ulator_list_mutex);
3543 mutex_init(&rdev->mutex);
3544 rdev->reg_data = config->driver_data;
3545 rdev->owner = regulator_desc->owner;
3546 rdev->desc = regulator_desc;
3548 rdev->regmap = config->regmap;
3549 else if (dev_get_regmap(dev, NULL))
3550 rdev->regmap = dev_get_regmap(dev, NULL);
3551 else if (dev->parent)
3552 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3553 INIT_LIST_HEAD(&rdev->consumer_list);
3554 INIT_LIST_HEAD(&rdev->list);
3555 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3556 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3558 /* preform any regulator specific init */
3559 if (init_data && init_data->regulator_init) {
3560 ret = init_data->regulator_init(rdev->reg_data);
3565 /* register with sysfs */
3566 rdev->dev.class = ®ulator_class;
3567 rdev->dev.of_node = config->of_node;
3568 rdev->dev.parent = dev;
3569 dev_set_name(&rdev->dev, "regulator.%d",
3570 atomic_inc_return(®ulator_no) - 1);
3571 ret = device_register(&rdev->dev);
3573 put_device(&rdev->dev);
3577 dev_set_drvdata(&rdev->dev, rdev);
3579 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3580 ret = regulator_ena_gpio_request(rdev, config);
3582 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3583 config->ena_gpio, ret);
3587 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3588 rdev->ena_gpio_state = 1;
3590 if (config->ena_gpio_invert)
3591 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3594 /* set regulator constraints */
3596 constraints = &init_data->constraints;
3598 ret = set_machine_constraints(rdev, constraints);
3602 /* add attributes supported by this regulator */
3603 ret = add_regulator_attributes(rdev);
3607 if (init_data && init_data->supply_regulator)
3608 supply = init_data->supply_regulator;
3609 else if (regulator_desc->supply_name)
3610 supply = regulator_desc->supply_name;
3613 struct regulator_dev *r;
3615 r = regulator_dev_lookup(dev, supply, &ret);
3617 if (ret == -ENODEV) {
3619 * No supply was specified for this regulator and
3620 * there will never be one.
3625 dev_err(dev, "Failed to find supply %s\n", supply);
3626 ret = -EPROBE_DEFER;
3630 ret = set_supply(rdev, r);
3634 /* Enable supply if rail is enabled */
3635 if (_regulator_is_enabled(rdev)) {
3636 ret = regulator_enable(rdev->supply);
3643 /* add consumers devices */
3645 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3646 ret = set_consumer_device_supply(rdev,
3647 init_data->consumer_supplies[i].dev_name,
3648 init_data->consumer_supplies[i].supply);
3650 dev_err(dev, "Failed to set supply %s\n",
3651 init_data->consumer_supplies[i].supply);
3652 goto unset_supplies;
3657 list_add(&rdev->list, ®ulator_list);
3659 rdev_init_debugfs(rdev);
3661 mutex_unlock(®ulator_list_mutex);
3665 unset_regulator_supplies(rdev);
3669 _regulator_put(rdev->supply);
3670 regulator_ena_gpio_free(rdev);
3671 kfree(rdev->constraints);
3673 device_unregister(&rdev->dev);
3674 /* device core frees rdev */
3675 rdev = ERR_PTR(ret);
3680 rdev = ERR_PTR(ret);
3683 EXPORT_SYMBOL_GPL(regulator_register);
3686 * regulator_unregister - unregister regulator
3687 * @rdev: regulator to unregister
3689 * Called by regulator drivers to unregister a regulator.
3691 void regulator_unregister(struct regulator_dev *rdev)
3697 regulator_put(rdev->supply);
3698 mutex_lock(®ulator_list_mutex);
3699 debugfs_remove_recursive(rdev->debugfs);
3700 flush_work(&rdev->disable_work.work);
3701 WARN_ON(rdev->open_count);
3702 unset_regulator_supplies(rdev);
3703 list_del(&rdev->list);
3704 kfree(rdev->constraints);
3705 regulator_ena_gpio_free(rdev);
3706 device_unregister(&rdev->dev);
3707 mutex_unlock(®ulator_list_mutex);
3709 EXPORT_SYMBOL_GPL(regulator_unregister);
3712 * regulator_suspend_prepare - prepare regulators for system wide suspend
3713 * @state: system suspend state
3715 * Configure each regulator with it's suspend operating parameters for state.
3716 * This will usually be called by machine suspend code prior to supending.
3718 int regulator_suspend_prepare(suspend_state_t state)
3720 struct regulator_dev *rdev;
3723 /* ON is handled by regulator active state */
3724 if (state == PM_SUSPEND_ON)
3727 mutex_lock(®ulator_list_mutex);
3728 list_for_each_entry(rdev, ®ulator_list, list) {
3730 mutex_lock(&rdev->mutex);
3731 ret = suspend_prepare(rdev, state);
3732 mutex_unlock(&rdev->mutex);
3735 rdev_err(rdev, "failed to prepare\n");
3740 mutex_unlock(®ulator_list_mutex);
3743 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3746 * regulator_suspend_finish - resume regulators from system wide suspend
3748 * Turn on regulators that might be turned off by regulator_suspend_prepare
3749 * and that should be turned on according to the regulators properties.
3751 int regulator_suspend_finish(void)
3753 struct regulator_dev *rdev;
3756 mutex_lock(®ulator_list_mutex);
3757 list_for_each_entry(rdev, ®ulator_list, list) {
3758 mutex_lock(&rdev->mutex);
3759 if (rdev->use_count > 0 || rdev->constraints->always_on) {
3760 error = _regulator_do_enable(rdev);
3764 if (!has_full_constraints)
3766 if (!_regulator_is_enabled(rdev))
3769 error = _regulator_do_disable(rdev);
3774 mutex_unlock(&rdev->mutex);
3776 mutex_unlock(®ulator_list_mutex);
3779 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3782 * regulator_has_full_constraints - the system has fully specified constraints
3784 * Calling this function will cause the regulator API to disable all
3785 * regulators which have a zero use count and don't have an always_on
3786 * constraint in a late_initcall.
3788 * The intention is that this will become the default behaviour in a
3789 * future kernel release so users are encouraged to use this facility
3792 void regulator_has_full_constraints(void)
3794 has_full_constraints = 1;
3796 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3799 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3801 * Calling this function will cause the regulator API to provide a
3802 * dummy regulator to consumers if no physical regulator is found,
3803 * allowing most consumers to proceed as though a regulator were
3804 * configured. This allows systems such as those with software
3805 * controllable regulators for the CPU core only to be brought up more
3808 void regulator_use_dummy_regulator(void)
3810 board_wants_dummy_regulator = true;
3812 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3815 * rdev_get_drvdata - get rdev regulator driver data
3818 * Get rdev regulator driver private data. This call can be used in the
3819 * regulator driver context.
3821 void *rdev_get_drvdata(struct regulator_dev *rdev)
3823 return rdev->reg_data;
3825 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3828 * regulator_get_drvdata - get regulator driver data
3829 * @regulator: regulator
3831 * Get regulator driver private data. This call can be used in the consumer
3832 * driver context when non API regulator specific functions need to be called.
3834 void *regulator_get_drvdata(struct regulator *regulator)
3836 return regulator->rdev->reg_data;
3838 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3841 * regulator_set_drvdata - set regulator driver data
3842 * @regulator: regulator
3845 void regulator_set_drvdata(struct regulator *regulator, void *data)
3847 regulator->rdev->reg_data = data;
3849 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3852 * regulator_get_id - get regulator ID
3855 int rdev_get_id(struct regulator_dev *rdev)
3857 return rdev->desc->id;
3859 EXPORT_SYMBOL_GPL(rdev_get_id);
3861 struct device *rdev_get_dev(struct regulator_dev *rdev)
3865 EXPORT_SYMBOL_GPL(rdev_get_dev);
3867 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3869 return reg_init_data->driver_data;
3871 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3873 #ifdef CONFIG_DEBUG_FS
3874 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3875 size_t count, loff_t *ppos)
3877 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3878 ssize_t len, ret = 0;
3879 struct regulator_map *map;
3884 list_for_each_entry(map, ®ulator_map_list, list) {
3885 len = snprintf(buf + ret, PAGE_SIZE - ret,
3887 rdev_get_name(map->regulator), map->dev_name,
3891 if (ret > PAGE_SIZE) {
3897 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3905 static const struct file_operations supply_map_fops = {
3906 #ifdef CONFIG_DEBUG_FS
3907 .read = supply_map_read_file,
3908 .llseek = default_llseek,
3912 static int __init regulator_init(void)
3916 ret = class_register(®ulator_class);
3918 debugfs_root = debugfs_create_dir("regulator", NULL);
3920 pr_warn("regulator: Failed to create debugfs directory\n");
3922 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3925 regulator_dummy_init();
3930 /* init early to allow our consumers to complete system booting */
3931 core_initcall(regulator_init);
3933 static int __init regulator_init_complete(void)
3935 struct regulator_dev *rdev;
3936 struct regulator_ops *ops;
3937 struct regulation_constraints *c;
3941 * Since DT doesn't provide an idiomatic mechanism for
3942 * enabling full constraints and since it's much more natural
3943 * with DT to provide them just assume that a DT enabled
3944 * system has full constraints.
3946 if (of_have_populated_dt())
3947 has_full_constraints = true;
3949 mutex_lock(®ulator_list_mutex);
3951 /* If we have a full configuration then disable any regulators
3952 * which are not in use or always_on. This will become the
3953 * default behaviour in the future.
3955 list_for_each_entry(rdev, ®ulator_list, list) {
3956 ops = rdev->desc->ops;
3957 c = rdev->constraints;
3959 if (c && c->always_on)
3962 mutex_lock(&rdev->mutex);
3964 if (rdev->use_count)
3967 /* If we can't read the status assume it's on. */
3968 if (ops->is_enabled)
3969 enabled = ops->is_enabled(rdev);
3976 if (has_full_constraints) {
3977 /* We log since this may kill the system if it
3979 rdev_info(rdev, "disabling\n");
3980 ret = _regulator_do_disable(rdev);
3982 rdev_err(rdev, "couldn't disable: %d\n", ret);
3985 /* The intention is that in future we will
3986 * assume that full constraints are provided
3987 * so warn even if we aren't going to do
3990 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3994 mutex_unlock(&rdev->mutex);
3997 mutex_unlock(®ulator_list_mutex);
4001 late_initcall(regulator_init_complete);