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 /* regulator_list_mutex lock 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 mutex_lock(&rdev->mutex);
1411 kfree(regulator->supply_name);
1412 list_del(®ulator->list);
1416 rdev->exclusive = 0;
1417 mutex_unlock(&rdev->mutex);
1419 module_put(rdev->owner);
1423 * regulator_put - "free" the regulator source
1424 * @regulator: regulator source
1426 * Note: drivers must ensure that all regulator_enable calls made on this
1427 * regulator source are balanced by regulator_disable calls prior to calling
1430 void regulator_put(struct regulator *regulator)
1432 mutex_lock(®ulator_list_mutex);
1433 _regulator_put(regulator);
1434 mutex_unlock(®ulator_list_mutex);
1436 EXPORT_SYMBOL_GPL(regulator_put);
1438 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1439 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1440 const struct regulator_config *config)
1442 struct regulator_enable_gpio *pin;
1445 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1446 if (pin->gpio == config->ena_gpio) {
1447 rdev_dbg(rdev, "GPIO %d is already used\n",
1449 goto update_ena_gpio_to_rdev;
1453 ret = gpio_request_one(config->ena_gpio,
1454 GPIOF_DIR_OUT | config->ena_gpio_flags,
1455 rdev_get_name(rdev));
1459 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1461 gpio_free(config->ena_gpio);
1465 pin->gpio = config->ena_gpio;
1466 pin->ena_gpio_invert = config->ena_gpio_invert;
1467 list_add(&pin->list, ®ulator_ena_gpio_list);
1469 update_ena_gpio_to_rdev:
1470 pin->request_count++;
1471 rdev->ena_pin = pin;
1475 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1477 struct regulator_enable_gpio *pin, *n;
1482 /* Free the GPIO only in case of no use */
1483 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1484 if (pin->gpio == rdev->ena_pin->gpio) {
1485 if (pin->request_count <= 1) {
1486 pin->request_count = 0;
1487 gpio_free(pin->gpio);
1488 list_del(&pin->list);
1491 pin->request_count--;
1498 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1499 * @rdev: regulator_dev structure
1500 * @enable: enable GPIO at initial use?
1502 * GPIO is enabled in case of initial use. (enable_count is 0)
1503 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1505 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1507 struct regulator_enable_gpio *pin = rdev->ena_pin;
1513 /* Enable GPIO at initial use */
1514 if (pin->enable_count == 0)
1515 gpio_set_value_cansleep(pin->gpio,
1516 !pin->ena_gpio_invert);
1518 pin->enable_count++;
1520 if (pin->enable_count > 1) {
1521 pin->enable_count--;
1525 /* Disable GPIO if not used */
1526 if (pin->enable_count <= 1) {
1527 gpio_set_value_cansleep(pin->gpio,
1528 pin->ena_gpio_invert);
1529 pin->enable_count = 0;
1536 static int _regulator_do_enable(struct regulator_dev *rdev)
1540 /* Query before enabling in case configuration dependent. */
1541 ret = _regulator_get_enable_time(rdev);
1545 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1549 trace_regulator_enable(rdev_get_name(rdev));
1551 if (rdev->ena_pin) {
1552 if (!rdev->ena_gpio_state) {
1553 ret = regulator_ena_gpio_ctrl(rdev, true);
1556 rdev->ena_gpio_state = 1;
1558 } else if (rdev->desc->ops->enable) {
1559 ret = rdev->desc->ops->enable(rdev);
1566 /* Allow the regulator to ramp; it would be useful to extend
1567 * this for bulk operations so that the regulators can ramp
1569 trace_regulator_enable_delay(rdev_get_name(rdev));
1571 if (delay >= 1000) {
1572 mdelay(delay / 1000);
1573 udelay(delay % 1000);
1578 trace_regulator_enable_complete(rdev_get_name(rdev));
1583 /* locks held by regulator_enable() */
1584 static int _regulator_enable(struct regulator_dev *rdev)
1588 /* check voltage and requested load before enabling */
1589 if (rdev->constraints &&
1590 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1591 drms_uA_update(rdev);
1593 if (rdev->use_count == 0) {
1594 /* The regulator may on if it's not switchable or left on */
1595 ret = _regulator_is_enabled(rdev);
1596 if (ret == -EINVAL || ret == 0) {
1597 if (!_regulator_can_change_status(rdev))
1600 ret = _regulator_do_enable(rdev);
1604 } else if (ret < 0) {
1605 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1608 /* Fallthrough on positive return values - already enabled */
1617 * regulator_enable - enable regulator output
1618 * @regulator: regulator source
1620 * Request that the regulator be enabled with the regulator output at
1621 * the predefined voltage or current value. Calls to regulator_enable()
1622 * must be balanced with calls to regulator_disable().
1624 * NOTE: the output value can be set by other drivers, boot loader or may be
1625 * hardwired in the regulator.
1627 int regulator_enable(struct regulator *regulator)
1629 struct regulator_dev *rdev = regulator->rdev;
1632 if (regulator->always_on)
1636 ret = regulator_enable(rdev->supply);
1641 mutex_lock(&rdev->mutex);
1642 ret = _regulator_enable(rdev);
1643 mutex_unlock(&rdev->mutex);
1645 if (ret != 0 && rdev->supply)
1646 regulator_disable(rdev->supply);
1650 EXPORT_SYMBOL_GPL(regulator_enable);
1652 static int _regulator_do_disable(struct regulator_dev *rdev)
1656 trace_regulator_disable(rdev_get_name(rdev));
1658 if (rdev->ena_pin) {
1659 if (rdev->ena_gpio_state) {
1660 ret = regulator_ena_gpio_ctrl(rdev, false);
1663 rdev->ena_gpio_state = 0;
1666 } else if (rdev->desc->ops->disable) {
1667 ret = rdev->desc->ops->disable(rdev);
1672 trace_regulator_disable_complete(rdev_get_name(rdev));
1677 /* locks held by regulator_disable() */
1678 static int _regulator_disable(struct regulator_dev *rdev)
1682 if (WARN(rdev->use_count <= 0,
1683 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1686 /* are we the last user and permitted to disable ? */
1687 if (rdev->use_count == 1 &&
1688 (rdev->constraints && !rdev->constraints->always_on)) {
1690 /* we are last user */
1691 if (_regulator_can_change_status(rdev)) {
1692 ret = _regulator_do_disable(rdev);
1694 rdev_err(rdev, "failed to disable\n");
1697 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1701 rdev->use_count = 0;
1702 } else if (rdev->use_count > 1) {
1704 if (rdev->constraints &&
1705 (rdev->constraints->valid_ops_mask &
1706 REGULATOR_CHANGE_DRMS))
1707 drms_uA_update(rdev);
1716 * regulator_disable - disable regulator output
1717 * @regulator: regulator source
1719 * Disable the regulator output voltage or current. Calls to
1720 * regulator_enable() must be balanced with calls to
1721 * regulator_disable().
1723 * NOTE: this will only disable the regulator output if no other consumer
1724 * devices have it enabled, the regulator device supports disabling and
1725 * machine constraints permit this operation.
1727 int regulator_disable(struct regulator *regulator)
1729 struct regulator_dev *rdev = regulator->rdev;
1732 if (regulator->always_on)
1735 mutex_lock(&rdev->mutex);
1736 ret = _regulator_disable(rdev);
1737 mutex_unlock(&rdev->mutex);
1739 if (ret == 0 && rdev->supply)
1740 regulator_disable(rdev->supply);
1744 EXPORT_SYMBOL_GPL(regulator_disable);
1746 /* locks held by regulator_force_disable() */
1747 static int _regulator_force_disable(struct regulator_dev *rdev)
1751 ret = _regulator_do_disable(rdev);
1753 rdev_err(rdev, "failed to force disable\n");
1757 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1758 REGULATOR_EVENT_DISABLE, NULL);
1764 * regulator_force_disable - force disable regulator output
1765 * @regulator: regulator source
1767 * Forcibly disable the regulator output voltage or current.
1768 * NOTE: this *will* disable the regulator output even if other consumer
1769 * devices have it enabled. This should be used for situations when device
1770 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1772 int regulator_force_disable(struct regulator *regulator)
1774 struct regulator_dev *rdev = regulator->rdev;
1777 mutex_lock(&rdev->mutex);
1778 regulator->uA_load = 0;
1779 ret = _regulator_force_disable(regulator->rdev);
1780 mutex_unlock(&rdev->mutex);
1783 while (rdev->open_count--)
1784 regulator_disable(rdev->supply);
1788 EXPORT_SYMBOL_GPL(regulator_force_disable);
1790 static void regulator_disable_work(struct work_struct *work)
1792 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1796 mutex_lock(&rdev->mutex);
1798 BUG_ON(!rdev->deferred_disables);
1800 count = rdev->deferred_disables;
1801 rdev->deferred_disables = 0;
1803 for (i = 0; i < count; i++) {
1804 ret = _regulator_disable(rdev);
1806 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1809 mutex_unlock(&rdev->mutex);
1812 for (i = 0; i < count; i++) {
1813 ret = regulator_disable(rdev->supply);
1816 "Supply disable failed: %d\n", ret);
1823 * regulator_disable_deferred - disable regulator output with delay
1824 * @regulator: regulator source
1825 * @ms: miliseconds until the regulator is disabled
1827 * Execute regulator_disable() on the regulator after a delay. This
1828 * is intended for use with devices that require some time to quiesce.
1830 * NOTE: this will only disable the regulator output if no other consumer
1831 * devices have it enabled, the regulator device supports disabling and
1832 * machine constraints permit this operation.
1834 int regulator_disable_deferred(struct regulator *regulator, int ms)
1836 struct regulator_dev *rdev = regulator->rdev;
1839 if (regulator->always_on)
1843 return regulator_disable(regulator);
1845 mutex_lock(&rdev->mutex);
1846 rdev->deferred_disables++;
1847 mutex_unlock(&rdev->mutex);
1849 ret = queue_delayed_work(system_power_efficient_wq,
1850 &rdev->disable_work,
1851 msecs_to_jiffies(ms));
1857 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1860 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1862 * @rdev: regulator to operate on
1864 * Regulators that use regmap for their register I/O can set the
1865 * enable_reg and enable_mask fields in their descriptor and then use
1866 * this as their is_enabled operation, saving some code.
1868 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1873 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1877 if (rdev->desc->enable_is_inverted)
1878 return (val & rdev->desc->enable_mask) == 0;
1880 return (val & rdev->desc->enable_mask) != 0;
1882 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1885 * regulator_enable_regmap - standard enable() for regmap users
1887 * @rdev: regulator to operate on
1889 * Regulators that use regmap for their register I/O can set the
1890 * enable_reg and enable_mask fields in their descriptor and then use
1891 * this as their enable() operation, saving some code.
1893 int regulator_enable_regmap(struct regulator_dev *rdev)
1897 if (rdev->desc->enable_is_inverted)
1900 val = rdev->desc->enable_mask;
1902 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1903 rdev->desc->enable_mask, val);
1905 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1908 * regulator_disable_regmap - standard disable() for regmap users
1910 * @rdev: regulator to operate on
1912 * Regulators that use regmap for their register I/O can set the
1913 * enable_reg and enable_mask fields in their descriptor and then use
1914 * this as their disable() operation, saving some code.
1916 int regulator_disable_regmap(struct regulator_dev *rdev)
1920 if (rdev->desc->enable_is_inverted)
1921 val = rdev->desc->enable_mask;
1925 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1926 rdev->desc->enable_mask, val);
1928 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1930 static int _regulator_is_enabled(struct regulator_dev *rdev)
1932 /* A GPIO control always takes precedence */
1934 return rdev->ena_gpio_state;
1936 /* If we don't know then assume that the regulator is always on */
1937 if (!rdev->desc->ops->is_enabled)
1940 return rdev->desc->ops->is_enabled(rdev);
1944 * regulator_is_enabled - is the regulator output enabled
1945 * @regulator: regulator source
1947 * Returns positive if the regulator driver backing the source/client
1948 * has requested that the device be enabled, zero if it hasn't, else a
1949 * negative errno code.
1951 * Note that the device backing this regulator handle can have multiple
1952 * users, so it might be enabled even if regulator_enable() was never
1953 * called for this particular source.
1955 int regulator_is_enabled(struct regulator *regulator)
1959 if (regulator->always_on)
1962 mutex_lock(®ulator->rdev->mutex);
1963 ret = _regulator_is_enabled(regulator->rdev);
1964 mutex_unlock(®ulator->rdev->mutex);
1968 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1971 * regulator_can_change_voltage - check if regulator can change voltage
1972 * @regulator: regulator source
1974 * Returns positive if the regulator driver backing the source/client
1975 * can change its voltage, false otherwise. Usefull for detecting fixed
1976 * or dummy regulators and disabling voltage change logic in the client
1979 int regulator_can_change_voltage(struct regulator *regulator)
1981 struct regulator_dev *rdev = regulator->rdev;
1983 if (rdev->constraints &&
1984 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1985 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
1988 if (rdev->desc->continuous_voltage_range &&
1989 rdev->constraints->min_uV && rdev->constraints->max_uV &&
1990 rdev->constraints->min_uV != rdev->constraints->max_uV)
1996 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
1999 * regulator_count_voltages - count regulator_list_voltage() selectors
2000 * @regulator: regulator source
2002 * Returns number of selectors, or negative errno. Selectors are
2003 * numbered starting at zero, and typically correspond to bitfields
2004 * in hardware registers.
2006 int regulator_count_voltages(struct regulator *regulator)
2008 struct regulator_dev *rdev = regulator->rdev;
2010 return rdev->desc->n_voltages ? : -EINVAL;
2012 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2015 * regulator_list_voltage_linear - List voltages with simple calculation
2017 * @rdev: Regulator device
2018 * @selector: Selector to convert into a voltage
2020 * Regulators with a simple linear mapping between voltages and
2021 * selectors can set min_uV and uV_step in the regulator descriptor
2022 * and then use this function as their list_voltage() operation,
2024 int regulator_list_voltage_linear(struct regulator_dev *rdev,
2025 unsigned int selector)
2027 if (selector >= rdev->desc->n_voltages)
2029 if (selector < rdev->desc->linear_min_sel)
2032 selector -= rdev->desc->linear_min_sel;
2034 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2036 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2039 * regulator_list_voltage_table - List voltages with table based mapping
2041 * @rdev: Regulator device
2042 * @selector: Selector to convert into a voltage
2044 * Regulators with table based mapping between voltages and
2045 * selectors can set volt_table in the regulator descriptor
2046 * and then use this function as their list_voltage() operation.
2048 int regulator_list_voltage_table(struct regulator_dev *rdev,
2049 unsigned int selector)
2051 if (!rdev->desc->volt_table) {
2052 BUG_ON(!rdev->desc->volt_table);
2056 if (selector >= rdev->desc->n_voltages)
2059 return rdev->desc->volt_table[selector];
2061 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2064 * regulator_list_voltage - enumerate supported voltages
2065 * @regulator: regulator source
2066 * @selector: identify voltage to list
2067 * Context: can sleep
2069 * Returns a voltage that can be passed to @regulator_set_voltage(),
2070 * zero if this selector code can't be used on this system, or a
2073 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2075 struct regulator_dev *rdev = regulator->rdev;
2076 struct regulator_ops *ops = rdev->desc->ops;
2079 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2082 mutex_lock(&rdev->mutex);
2083 ret = ops->list_voltage(rdev, selector);
2084 mutex_unlock(&rdev->mutex);
2087 if (ret < rdev->constraints->min_uV)
2089 else if (ret > rdev->constraints->max_uV)
2095 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2098 * regulator_is_supported_voltage - check if a voltage range can be supported
2100 * @regulator: Regulator to check.
2101 * @min_uV: Minimum required voltage in uV.
2102 * @max_uV: Maximum required voltage in uV.
2104 * Returns a boolean or a negative error code.
2106 int regulator_is_supported_voltage(struct regulator *regulator,
2107 int min_uV, int max_uV)
2109 struct regulator_dev *rdev = regulator->rdev;
2110 int i, voltages, ret;
2112 /* If we can't change voltage check the current voltage */
2113 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2114 ret = regulator_get_voltage(regulator);
2116 return (min_uV <= ret && ret <= max_uV);
2121 /* Any voltage within constrains range is fine? */
2122 if (rdev->desc->continuous_voltage_range)
2123 return min_uV >= rdev->constraints->min_uV &&
2124 max_uV <= rdev->constraints->max_uV;
2126 ret = regulator_count_voltages(regulator);
2131 for (i = 0; i < voltages; i++) {
2132 ret = regulator_list_voltage(regulator, i);
2134 if (ret >= min_uV && ret <= max_uV)
2140 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2143 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2145 * @rdev: regulator to operate on
2147 * Regulators that use regmap for their register I/O can set the
2148 * vsel_reg and vsel_mask fields in their descriptor and then use this
2149 * as their get_voltage_vsel operation, saving some code.
2151 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2156 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2160 val &= rdev->desc->vsel_mask;
2161 val >>= ffs(rdev->desc->vsel_mask) - 1;
2165 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2168 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2170 * @rdev: regulator to operate on
2171 * @sel: Selector to set
2173 * Regulators that use regmap for their register I/O can set the
2174 * vsel_reg and vsel_mask fields in their descriptor and then use this
2175 * as their set_voltage_vsel operation, saving some code.
2177 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2181 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2183 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2184 rdev->desc->vsel_mask, sel);
2188 if (rdev->desc->apply_bit)
2189 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2190 rdev->desc->apply_bit,
2191 rdev->desc->apply_bit);
2194 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2197 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2199 * @rdev: Regulator to operate on
2200 * @min_uV: Lower bound for voltage
2201 * @max_uV: Upper bound for voltage
2203 * Drivers implementing set_voltage_sel() and list_voltage() can use
2204 * this as their map_voltage() operation. It will find a suitable
2205 * voltage by calling list_voltage() until it gets something in bounds
2206 * for the requested voltages.
2208 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2209 int min_uV, int max_uV)
2211 int best_val = INT_MAX;
2215 /* Find the smallest voltage that falls within the specified
2218 for (i = 0; i < rdev->desc->n_voltages; i++) {
2219 ret = rdev->desc->ops->list_voltage(rdev, i);
2223 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2229 if (best_val != INT_MAX)
2234 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2237 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2239 * @rdev: Regulator to operate on
2240 * @min_uV: Lower bound for voltage
2241 * @max_uV: Upper bound for voltage
2243 * Drivers that have ascendant voltage list can use this as their
2244 * map_voltage() operation.
2246 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
2247 int min_uV, int max_uV)
2251 for (i = 0; i < rdev->desc->n_voltages; i++) {
2252 ret = rdev->desc->ops->list_voltage(rdev, i);
2259 if (ret >= min_uV && ret <= max_uV)
2265 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
2268 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2270 * @rdev: Regulator to operate on
2271 * @min_uV: Lower bound for voltage
2272 * @max_uV: Upper bound for voltage
2274 * Drivers providing min_uV and uV_step in their regulator_desc can
2275 * use this as their map_voltage() operation.
2277 int regulator_map_voltage_linear(struct regulator_dev *rdev,
2278 int min_uV, int max_uV)
2282 /* Allow uV_step to be 0 for fixed voltage */
2283 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2284 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2290 if (!rdev->desc->uV_step) {
2291 BUG_ON(!rdev->desc->uV_step);
2295 if (min_uV < rdev->desc->min_uV)
2296 min_uV = rdev->desc->min_uV;
2298 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2302 ret += rdev->desc->linear_min_sel;
2304 /* Map back into a voltage to verify we're still in bounds */
2305 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2306 if (voltage < min_uV || voltage > max_uV)
2311 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2313 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2314 int min_uV, int max_uV,
2317 struct pre_voltage_change_data data;
2320 data.old_uV = _regulator_get_voltage(rdev);
2321 data.min_uV = min_uV;
2322 data.max_uV = max_uV;
2323 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2325 if (ret & NOTIFY_STOP_MASK)
2328 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2332 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2333 (void *)data.old_uV);
2338 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2339 int uV, unsigned selector)
2341 struct pre_voltage_change_data data;
2344 data.old_uV = _regulator_get_voltage(rdev);
2347 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2349 if (ret & NOTIFY_STOP_MASK)
2352 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2356 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2357 (void *)data.old_uV);
2362 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2363 int min_uV, int max_uV)
2368 unsigned int selector;
2369 int old_selector = -1;
2371 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2373 min_uV += rdev->constraints->uV_offset;
2374 max_uV += rdev->constraints->uV_offset;
2377 * If we can't obtain the old selector there is not enough
2378 * info to call set_voltage_time_sel().
2380 if (_regulator_is_enabled(rdev) &&
2381 rdev->desc->ops->set_voltage_time_sel &&
2382 rdev->desc->ops->get_voltage_sel) {
2383 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2384 if (old_selector < 0)
2385 return old_selector;
2388 if (rdev->desc->ops->set_voltage) {
2389 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2393 if (rdev->desc->ops->list_voltage)
2394 best_val = rdev->desc->ops->list_voltage(rdev,
2397 best_val = _regulator_get_voltage(rdev);
2400 } else if (rdev->desc->ops->set_voltage_sel) {
2401 if (rdev->desc->ops->map_voltage) {
2402 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2405 if (rdev->desc->ops->list_voltage ==
2406 regulator_list_voltage_linear)
2407 ret = regulator_map_voltage_linear(rdev,
2410 ret = regulator_map_voltage_iterate(rdev,
2415 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2416 if (min_uV <= best_val && max_uV >= best_val) {
2418 if (old_selector == selector)
2421 ret = _regulator_call_set_voltage_sel(
2422 rdev, best_val, selector);
2431 /* Call set_voltage_time_sel if successfully obtained old_selector */
2432 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2433 old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
2435 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2436 old_selector, selector);
2438 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2443 /* Insert any necessary delays */
2444 if (delay >= 1000) {
2445 mdelay(delay / 1000);
2446 udelay(delay % 1000);
2452 if (ret == 0 && best_val >= 0) {
2453 unsigned long data = best_val;
2455 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2459 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2465 * regulator_set_voltage - set regulator output voltage
2466 * @regulator: regulator source
2467 * @min_uV: Minimum required voltage in uV
2468 * @max_uV: Maximum acceptable voltage in uV
2470 * Sets a voltage regulator to the desired output voltage. This can be set
2471 * during any regulator state. IOW, regulator can be disabled or enabled.
2473 * If the regulator is enabled then the voltage will change to the new value
2474 * immediately otherwise if the regulator is disabled the regulator will
2475 * output at the new voltage when enabled.
2477 * NOTE: If the regulator is shared between several devices then the lowest
2478 * request voltage that meets the system constraints will be used.
2479 * Regulator system constraints must be set for this regulator before
2480 * calling this function otherwise this call will fail.
2482 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2484 struct regulator_dev *rdev = regulator->rdev;
2486 int old_min_uV, old_max_uV;
2488 mutex_lock(&rdev->mutex);
2490 /* If we're setting the same range as last time the change
2491 * should be a noop (some cpufreq implementations use the same
2492 * voltage for multiple frequencies, for example).
2494 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2498 if (!rdev->desc->ops->set_voltage &&
2499 !rdev->desc->ops->set_voltage_sel) {
2504 /* constraints check */
2505 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2509 /* restore original values in case of error */
2510 old_min_uV = regulator->min_uV;
2511 old_max_uV = regulator->max_uV;
2512 regulator->min_uV = min_uV;
2513 regulator->max_uV = max_uV;
2515 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2519 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2524 mutex_unlock(&rdev->mutex);
2527 regulator->min_uV = old_min_uV;
2528 regulator->max_uV = old_max_uV;
2529 mutex_unlock(&rdev->mutex);
2532 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2535 * regulator_set_voltage_time - get raise/fall time
2536 * @regulator: regulator source
2537 * @old_uV: starting voltage in microvolts
2538 * @new_uV: target voltage in microvolts
2540 * Provided with the starting and ending voltage, this function attempts to
2541 * calculate the time in microseconds required to rise or fall to this new
2544 int regulator_set_voltage_time(struct regulator *regulator,
2545 int old_uV, int new_uV)
2547 struct regulator_dev *rdev = regulator->rdev;
2548 struct regulator_ops *ops = rdev->desc->ops;
2554 /* Currently requires operations to do this */
2555 if (!ops->list_voltage || !ops->set_voltage_time_sel
2556 || !rdev->desc->n_voltages)
2559 for (i = 0; i < rdev->desc->n_voltages; i++) {
2560 /* We only look for exact voltage matches here */
2561 voltage = regulator_list_voltage(regulator, i);
2566 if (voltage == old_uV)
2568 if (voltage == new_uV)
2572 if (old_sel < 0 || new_sel < 0)
2575 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2577 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2580 * regulator_set_voltage_time_sel - get raise/fall time
2581 * @rdev: regulator source device
2582 * @old_selector: selector for starting voltage
2583 * @new_selector: selector for target voltage
2585 * Provided with the starting and target voltage selectors, this function
2586 * returns time in microseconds required to rise or fall to this new voltage
2588 * Drivers providing ramp_delay in regulation_constraints can use this as their
2589 * set_voltage_time_sel() operation.
2591 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2592 unsigned int old_selector,
2593 unsigned int new_selector)
2595 unsigned int ramp_delay = 0;
2596 int old_volt, new_volt;
2598 if (rdev->constraints->ramp_delay)
2599 ramp_delay = rdev->constraints->ramp_delay;
2600 else if (rdev->desc->ramp_delay)
2601 ramp_delay = rdev->desc->ramp_delay;
2603 if (ramp_delay == 0) {
2604 rdev_warn(rdev, "ramp_delay not set\n");
2609 if (!rdev->desc->ops->list_voltage)
2612 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2613 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2615 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2617 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2620 * regulator_sync_voltage - re-apply last regulator output voltage
2621 * @regulator: regulator source
2623 * Re-apply the last configured voltage. This is intended to be used
2624 * where some external control source the consumer is cooperating with
2625 * has caused the configured voltage to change.
2627 int regulator_sync_voltage(struct regulator *regulator)
2629 struct regulator_dev *rdev = regulator->rdev;
2630 int ret, min_uV, max_uV;
2632 mutex_lock(&rdev->mutex);
2634 if (!rdev->desc->ops->set_voltage &&
2635 !rdev->desc->ops->set_voltage_sel) {
2640 /* This is only going to work if we've had a voltage configured. */
2641 if (!regulator->min_uV && !regulator->max_uV) {
2646 min_uV = regulator->min_uV;
2647 max_uV = regulator->max_uV;
2649 /* This should be a paranoia check... */
2650 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2654 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2658 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2661 mutex_unlock(&rdev->mutex);
2664 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2666 static int _regulator_get_voltage(struct regulator_dev *rdev)
2670 if (rdev->desc->ops->get_voltage_sel) {
2671 sel = rdev->desc->ops->get_voltage_sel(rdev);
2674 ret = rdev->desc->ops->list_voltage(rdev, sel);
2675 } else if (rdev->desc->ops->get_voltage) {
2676 ret = rdev->desc->ops->get_voltage(rdev);
2677 } else if (rdev->desc->ops->list_voltage) {
2678 ret = rdev->desc->ops->list_voltage(rdev, 0);
2685 return ret - rdev->constraints->uV_offset;
2689 * regulator_get_voltage - get regulator output voltage
2690 * @regulator: regulator source
2692 * This returns the current regulator voltage in uV.
2694 * NOTE: If the regulator is disabled it will return the voltage value. This
2695 * function should not be used to determine regulator state.
2697 int regulator_get_voltage(struct regulator *regulator)
2701 mutex_lock(®ulator->rdev->mutex);
2703 ret = _regulator_get_voltage(regulator->rdev);
2705 mutex_unlock(®ulator->rdev->mutex);
2709 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2712 * regulator_set_current_limit - set regulator output current limit
2713 * @regulator: regulator source
2714 * @min_uA: Minimum supported current in uA
2715 * @max_uA: Maximum supported current in uA
2717 * Sets current sink to the desired output current. This can be set during
2718 * any regulator state. IOW, regulator can be disabled or enabled.
2720 * If the regulator is enabled then the current will change to the new value
2721 * immediately otherwise if the regulator is disabled the regulator will
2722 * output at the new current when enabled.
2724 * NOTE: Regulator system constraints must be set for this regulator before
2725 * calling this function otherwise this call will fail.
2727 int regulator_set_current_limit(struct regulator *regulator,
2728 int min_uA, int max_uA)
2730 struct regulator_dev *rdev = regulator->rdev;
2733 mutex_lock(&rdev->mutex);
2736 if (!rdev->desc->ops->set_current_limit) {
2741 /* constraints check */
2742 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2746 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2748 mutex_unlock(&rdev->mutex);
2751 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2753 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2757 mutex_lock(&rdev->mutex);
2760 if (!rdev->desc->ops->get_current_limit) {
2765 ret = rdev->desc->ops->get_current_limit(rdev);
2767 mutex_unlock(&rdev->mutex);
2772 * regulator_get_current_limit - get regulator output current
2773 * @regulator: regulator source
2775 * This returns the current supplied by the specified current sink in uA.
2777 * NOTE: If the regulator is disabled it will return the current value. This
2778 * function should not be used to determine regulator state.
2780 int regulator_get_current_limit(struct regulator *regulator)
2782 return _regulator_get_current_limit(regulator->rdev);
2784 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2786 int regulator_is_supported_mode(struct regulator *regulator, int *mode)
2788 struct regulator_dev *rdev = regulator->rdev;
2791 mutex_lock(&rdev->mutex);
2793 ret = regulator_mode_constrain(rdev, mode);
2795 mutex_unlock(&rdev->mutex);
2801 * regulator_set_mode - set regulator operating mode
2802 * @regulator: regulator source
2803 * @mode: operating mode - one of the REGULATOR_MODE constants
2805 * Set regulator operating mode to increase regulator efficiency or improve
2806 * regulation performance.
2808 * NOTE: Regulator system constraints must be set for this regulator before
2809 * calling this function otherwise this call will fail.
2811 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2813 struct regulator_dev *rdev = regulator->rdev;
2815 int regulator_curr_mode;
2817 mutex_lock(&rdev->mutex);
2820 if (!rdev->desc->ops->set_mode) {
2825 /* return if the same mode is requested */
2826 if (rdev->desc->ops->get_mode) {
2827 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2828 if (regulator_curr_mode == mode) {
2834 /* constraints check */
2835 ret = regulator_mode_constrain(rdev, &mode);
2839 ret = rdev->desc->ops->set_mode(rdev, mode);
2841 mutex_unlock(&rdev->mutex);
2844 EXPORT_SYMBOL_GPL(regulator_set_mode);
2846 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2850 mutex_lock(&rdev->mutex);
2853 if (!rdev->desc->ops->get_mode) {
2858 ret = rdev->desc->ops->get_mode(rdev);
2860 mutex_unlock(&rdev->mutex);
2865 * regulator_get_mode - get regulator operating mode
2866 * @regulator: regulator source
2868 * Get the current regulator operating mode.
2870 unsigned int regulator_get_mode(struct regulator *regulator)
2872 return _regulator_get_mode(regulator->rdev);
2874 EXPORT_SYMBOL_GPL(regulator_get_mode);
2877 * regulator_set_optimum_mode - set regulator optimum operating mode
2878 * @regulator: regulator source
2879 * @uA_load: load current
2881 * Notifies the regulator core of a new device load. This is then used by
2882 * DRMS (if enabled by constraints) to set the most efficient regulator
2883 * operating mode for the new regulator loading.
2885 * Consumer devices notify their supply regulator of the maximum power
2886 * they will require (can be taken from device datasheet in the power
2887 * consumption tables) when they change operational status and hence power
2888 * state. Examples of operational state changes that can affect power
2889 * consumption are :-
2891 * o Device is opened / closed.
2892 * o Device I/O is about to begin or has just finished.
2893 * o Device is idling in between work.
2895 * This information is also exported via sysfs to userspace.
2897 * DRMS will sum the total requested load on the regulator and change
2898 * to the most efficient operating mode if platform constraints allow.
2900 * Returns the new regulator mode or error.
2902 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2904 struct regulator_dev *rdev = regulator->rdev;
2905 struct regulator *consumer;
2906 int ret, output_uV, input_uV = 0, total_uA_load = 0;
2910 input_uV = regulator_get_voltage(rdev->supply);
2912 mutex_lock(&rdev->mutex);
2915 * first check to see if we can set modes at all, otherwise just
2916 * tell the consumer everything is OK.
2918 regulator->uA_load = uA_load;
2919 ret = regulator_check_drms(rdev);
2925 if (!rdev->desc->ops->get_optimum_mode)
2929 * we can actually do this so any errors are indicators of
2930 * potential real failure.
2934 if (!rdev->desc->ops->set_mode)
2937 /* get output voltage */
2938 output_uV = _regulator_get_voltage(rdev);
2939 if (output_uV <= 0) {
2940 rdev_err(rdev, "invalid output voltage found\n");
2944 /* No supply? Use constraint voltage */
2946 input_uV = rdev->constraints->input_uV;
2947 if (input_uV <= 0) {
2948 rdev_err(rdev, "invalid input voltage found\n");
2952 /* calc total requested load for this regulator */
2953 list_for_each_entry(consumer, &rdev->consumer_list, list)
2954 total_uA_load += consumer->uA_load;
2956 mode = rdev->desc->ops->get_optimum_mode(rdev,
2957 input_uV, output_uV,
2959 ret = regulator_mode_constrain(rdev, &mode);
2961 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2962 total_uA_load, input_uV, output_uV);
2966 ret = rdev->desc->ops->set_mode(rdev, mode);
2968 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2973 mutex_unlock(&rdev->mutex);
2976 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2979 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2981 * @rdev: device to operate on.
2982 * @enable: state to set.
2984 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2989 val = rdev->desc->bypass_mask;
2993 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2994 rdev->desc->bypass_mask, val);
2996 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2999 * regulator_get_bypass_regmap - Default get_bypass() using regmap
3001 * @rdev: device to operate on.
3002 * @enable: current state.
3004 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
3009 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
3013 *enable = val & rdev->desc->bypass_mask;
3017 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
3020 * regulator_allow_bypass - allow the regulator to go into bypass mode
3022 * @regulator: Regulator to configure
3023 * @enable: enable or disable bypass mode
3025 * Allow the regulator to go into bypass mode if all other consumers
3026 * for the regulator also enable bypass mode and the machine
3027 * constraints allow this. Bypass mode means that the regulator is
3028 * simply passing the input directly to the output with no regulation.
3030 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3032 struct regulator_dev *rdev = regulator->rdev;
3035 if (!rdev->desc->ops->set_bypass)
3038 if (rdev->constraints &&
3039 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3042 mutex_lock(&rdev->mutex);
3044 if (enable && !regulator->bypass) {
3045 rdev->bypass_count++;
3047 if (rdev->bypass_count == rdev->open_count) {
3048 ret = rdev->desc->ops->set_bypass(rdev, enable);
3050 rdev->bypass_count--;
3053 } else if (!enable && regulator->bypass) {
3054 rdev->bypass_count--;
3056 if (rdev->bypass_count != rdev->open_count) {
3057 ret = rdev->desc->ops->set_bypass(rdev, enable);
3059 rdev->bypass_count++;
3064 regulator->bypass = enable;
3066 mutex_unlock(&rdev->mutex);
3070 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3073 * regulator_register_notifier - register regulator event notifier
3074 * @regulator: regulator source
3075 * @nb: notifier block
3077 * Register notifier block to receive regulator events.
3079 int regulator_register_notifier(struct regulator *regulator,
3080 struct notifier_block *nb)
3082 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3085 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3088 * regulator_unregister_notifier - unregister regulator event notifier
3089 * @regulator: regulator source
3090 * @nb: notifier block
3092 * Unregister regulator event notifier block.
3094 int regulator_unregister_notifier(struct regulator *regulator,
3095 struct notifier_block *nb)
3097 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3100 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3102 /* notify regulator consumers and downstream regulator consumers.
3103 * Note mutex must be held by caller.
3105 static int _notifier_call_chain(struct regulator_dev *rdev,
3106 unsigned long event, void *data)
3108 /* call rdev chain first */
3109 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3113 * regulator_bulk_get - get multiple regulator consumers
3115 * @dev: Device to supply
3116 * @num_consumers: Number of consumers to register
3117 * @consumers: Configuration of consumers; clients are stored here.
3119 * @return 0 on success, an errno on failure.
3121 * This helper function allows drivers to get several regulator
3122 * consumers in one operation. If any of the regulators cannot be
3123 * acquired then any regulators that were allocated will be freed
3124 * before returning to the caller.
3126 int regulator_bulk_get(struct device *dev, int num_consumers,
3127 struct regulator_bulk_data *consumers)
3132 for (i = 0; i < num_consumers; i++)
3133 consumers[i].consumer = NULL;
3135 for (i = 0; i < num_consumers; i++) {
3136 consumers[i].consumer = regulator_get(dev,
3137 consumers[i].supply);
3138 if (IS_ERR(consumers[i].consumer)) {
3139 ret = PTR_ERR(consumers[i].consumer);
3140 dev_err(dev, "Failed to get supply '%s': %d\n",
3141 consumers[i].supply, ret);
3142 consumers[i].consumer = NULL;
3151 regulator_put(consumers[i].consumer);
3155 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3157 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3159 struct regulator_bulk_data *bulk = data;
3161 bulk->ret = regulator_enable(bulk->consumer);
3165 * regulator_bulk_enable - enable multiple regulator consumers
3167 * @num_consumers: Number of consumers
3168 * @consumers: Consumer data; clients are stored here.
3169 * @return 0 on success, an errno on failure
3171 * This convenience API allows consumers to enable multiple regulator
3172 * clients in a single API call. If any consumers cannot be enabled
3173 * then any others that were enabled will be disabled again prior to
3176 int regulator_bulk_enable(int num_consumers,
3177 struct regulator_bulk_data *consumers)
3179 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3183 for (i = 0; i < num_consumers; i++) {
3184 if (consumers[i].consumer->always_on)
3185 consumers[i].ret = 0;
3187 async_schedule_domain(regulator_bulk_enable_async,
3188 &consumers[i], &async_domain);
3191 async_synchronize_full_domain(&async_domain);
3193 /* If any consumer failed we need to unwind any that succeeded */
3194 for (i = 0; i < num_consumers; i++) {
3195 if (consumers[i].ret != 0) {
3196 ret = consumers[i].ret;
3204 for (i = 0; i < num_consumers; i++) {
3205 if (consumers[i].ret < 0)
3206 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3209 regulator_disable(consumers[i].consumer);
3214 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3217 * regulator_bulk_disable - disable multiple regulator consumers
3219 * @num_consumers: Number of consumers
3220 * @consumers: Consumer data; clients are stored here.
3221 * @return 0 on success, an errno on failure
3223 * This convenience API allows consumers to disable multiple regulator
3224 * clients in a single API call. If any consumers cannot be disabled
3225 * then any others that were disabled will be enabled again prior to
3228 int regulator_bulk_disable(int num_consumers,
3229 struct regulator_bulk_data *consumers)
3234 for (i = num_consumers - 1; i >= 0; --i) {
3235 ret = regulator_disable(consumers[i].consumer);
3243 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3244 for (++i; i < num_consumers; ++i) {
3245 r = regulator_enable(consumers[i].consumer);
3247 pr_err("Failed to reename %s: %d\n",
3248 consumers[i].supply, r);
3253 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3256 * regulator_bulk_force_disable - force disable multiple regulator consumers
3258 * @num_consumers: Number of consumers
3259 * @consumers: Consumer data; clients are stored here.
3260 * @return 0 on success, an errno on failure
3262 * This convenience API allows consumers to forcibly disable multiple regulator
3263 * clients in a single API call.
3264 * NOTE: This should be used for situations when device damage will
3265 * likely occur if the regulators are not disabled (e.g. over temp).
3266 * Although regulator_force_disable function call for some consumers can
3267 * return error numbers, the function is called for all consumers.
3269 int regulator_bulk_force_disable(int num_consumers,
3270 struct regulator_bulk_data *consumers)
3275 for (i = 0; i < num_consumers; i++)
3277 regulator_force_disable(consumers[i].consumer);
3279 for (i = 0; i < num_consumers; i++) {
3280 if (consumers[i].ret != 0) {
3281 ret = consumers[i].ret;
3290 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3293 * regulator_bulk_free - free multiple regulator consumers
3295 * @num_consumers: Number of consumers
3296 * @consumers: Consumer data; clients are stored here.
3298 * This convenience API allows consumers to free multiple regulator
3299 * clients in a single API call.
3301 void regulator_bulk_free(int num_consumers,
3302 struct regulator_bulk_data *consumers)
3306 for (i = 0; i < num_consumers; i++) {
3307 regulator_put(consumers[i].consumer);
3308 consumers[i].consumer = NULL;
3311 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3314 * regulator_notifier_call_chain - call regulator event notifier
3315 * @rdev: regulator source
3316 * @event: notifier block
3317 * @data: callback-specific data.
3319 * Called by regulator drivers to notify clients a regulator event has
3320 * occurred. We also notify regulator clients downstream.
3321 * Note lock must be held by caller.
3323 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3324 unsigned long event, void *data)
3326 _notifier_call_chain(rdev, event, data);
3330 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3333 * regulator_mode_to_status - convert a regulator mode into a status
3335 * @mode: Mode to convert
3337 * Convert a regulator mode into a status.
3339 int regulator_mode_to_status(unsigned int mode)
3342 case REGULATOR_MODE_FAST:
3343 return REGULATOR_STATUS_FAST;
3344 case REGULATOR_MODE_NORMAL:
3345 return REGULATOR_STATUS_NORMAL;
3346 case REGULATOR_MODE_IDLE:
3347 return REGULATOR_STATUS_IDLE;
3348 case REGULATOR_MODE_STANDBY:
3349 return REGULATOR_STATUS_STANDBY;
3351 return REGULATOR_STATUS_UNDEFINED;
3354 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3357 * To avoid cluttering sysfs (and memory) with useless state, only
3358 * create attributes that can be meaningfully displayed.
3360 static int add_regulator_attributes(struct regulator_dev *rdev)
3362 struct device *dev = &rdev->dev;
3363 struct regulator_ops *ops = rdev->desc->ops;
3366 /* some attributes need specific methods to be displayed */
3367 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3368 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3369 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3370 status = device_create_file(dev, &dev_attr_microvolts);
3374 if (ops->get_current_limit) {
3375 status = device_create_file(dev, &dev_attr_microamps);
3379 if (ops->get_mode) {
3380 status = device_create_file(dev, &dev_attr_opmode);
3384 if (rdev->ena_pin || ops->is_enabled) {
3385 status = device_create_file(dev, &dev_attr_state);
3389 if (ops->get_status) {
3390 status = device_create_file(dev, &dev_attr_status);
3394 if (ops->get_bypass) {
3395 status = device_create_file(dev, &dev_attr_bypass);
3400 /* some attributes are type-specific */
3401 if (rdev->desc->type == REGULATOR_CURRENT) {
3402 status = device_create_file(dev, &dev_attr_requested_microamps);
3407 /* all the other attributes exist to support constraints;
3408 * don't show them if there are no constraints, or if the
3409 * relevant supporting methods are missing.
3411 if (!rdev->constraints)
3414 /* constraints need specific supporting methods */
3415 if (ops->set_voltage || ops->set_voltage_sel) {
3416 status = device_create_file(dev, &dev_attr_min_microvolts);
3419 status = device_create_file(dev, &dev_attr_max_microvolts);
3423 if (ops->set_current_limit) {
3424 status = device_create_file(dev, &dev_attr_min_microamps);
3427 status = device_create_file(dev, &dev_attr_max_microamps);
3432 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3435 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3438 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3442 if (ops->set_suspend_voltage) {
3443 status = device_create_file(dev,
3444 &dev_attr_suspend_standby_microvolts);
3447 status = device_create_file(dev,
3448 &dev_attr_suspend_mem_microvolts);
3451 status = device_create_file(dev,
3452 &dev_attr_suspend_disk_microvolts);
3457 if (ops->set_suspend_mode) {
3458 status = device_create_file(dev,
3459 &dev_attr_suspend_standby_mode);
3462 status = device_create_file(dev,
3463 &dev_attr_suspend_mem_mode);
3466 status = device_create_file(dev,
3467 &dev_attr_suspend_disk_mode);
3475 static void rdev_init_debugfs(struct regulator_dev *rdev)
3477 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3478 if (!rdev->debugfs) {
3479 rdev_warn(rdev, "Failed to create debugfs directory\n");
3483 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3485 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3487 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3488 &rdev->bypass_count);
3492 * regulator_register - register regulator
3493 * @regulator_desc: regulator to register
3494 * @config: runtime configuration for regulator
3496 * Called by regulator drivers to register a regulator.
3497 * Returns a valid pointer to struct regulator_dev on success
3498 * or an ERR_PTR() on error.
3500 struct regulator_dev *
3501 regulator_register(const struct regulator_desc *regulator_desc,
3502 const struct regulator_config *config)
3504 const struct regulation_constraints *constraints = NULL;
3505 const struct regulator_init_data *init_data;
3506 static atomic_t regulator_no = ATOMIC_INIT(0);
3507 struct regulator_dev *rdev;
3510 const char *supply = NULL;
3512 if (regulator_desc == NULL || config == NULL)
3513 return ERR_PTR(-EINVAL);
3518 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3519 return ERR_PTR(-EINVAL);
3521 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3522 regulator_desc->type != REGULATOR_CURRENT)
3523 return ERR_PTR(-EINVAL);
3525 /* Only one of each should be implemented */
3526 WARN_ON(regulator_desc->ops->get_voltage &&
3527 regulator_desc->ops->get_voltage_sel);
3528 WARN_ON(regulator_desc->ops->set_voltage &&
3529 regulator_desc->ops->set_voltage_sel);
3531 /* If we're using selectors we must implement list_voltage. */
3532 if (regulator_desc->ops->get_voltage_sel &&
3533 !regulator_desc->ops->list_voltage) {
3534 return ERR_PTR(-EINVAL);
3536 if (regulator_desc->ops->set_voltage_sel &&
3537 !regulator_desc->ops->list_voltage) {
3538 return ERR_PTR(-EINVAL);
3541 init_data = config->init_data;
3543 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3545 return ERR_PTR(-ENOMEM);
3547 mutex_lock(®ulator_list_mutex);
3549 mutex_init(&rdev->mutex);
3550 rdev->reg_data = config->driver_data;
3551 rdev->owner = regulator_desc->owner;
3552 rdev->desc = regulator_desc;
3554 rdev->regmap = config->regmap;
3555 else if (dev_get_regmap(dev, NULL))
3556 rdev->regmap = dev_get_regmap(dev, NULL);
3557 else if (dev->parent)
3558 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3559 INIT_LIST_HEAD(&rdev->consumer_list);
3560 INIT_LIST_HEAD(&rdev->list);
3561 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3562 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3564 /* preform any regulator specific init */
3565 if (init_data && init_data->regulator_init) {
3566 ret = init_data->regulator_init(rdev->reg_data);
3571 /* register with sysfs */
3572 rdev->dev.class = ®ulator_class;
3573 rdev->dev.of_node = config->of_node;
3574 rdev->dev.parent = dev;
3575 dev_set_name(&rdev->dev, "regulator.%d",
3576 atomic_inc_return(®ulator_no) - 1);
3577 ret = device_register(&rdev->dev);
3579 put_device(&rdev->dev);
3583 dev_set_drvdata(&rdev->dev, rdev);
3585 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3586 ret = regulator_ena_gpio_request(rdev, config);
3588 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3589 config->ena_gpio, ret);
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 if (!_regulator_is_enabled(rdev)) {
3761 error = _regulator_do_enable(rdev);
3766 if (!has_full_constraints)
3768 if (!_regulator_is_enabled(rdev))
3771 error = _regulator_do_disable(rdev);
3776 mutex_unlock(&rdev->mutex);
3778 mutex_unlock(®ulator_list_mutex);
3781 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3784 * regulator_has_full_constraints - the system has fully specified constraints
3786 * Calling this function will cause the regulator API to disable all
3787 * regulators which have a zero use count and don't have an always_on
3788 * constraint in a late_initcall.
3790 * The intention is that this will become the default behaviour in a
3791 * future kernel release so users are encouraged to use this facility
3794 void regulator_has_full_constraints(void)
3796 has_full_constraints = 1;
3798 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3801 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3803 * Calling this function will cause the regulator API to provide a
3804 * dummy regulator to consumers if no physical regulator is found,
3805 * allowing most consumers to proceed as though a regulator were
3806 * configured. This allows systems such as those with software
3807 * controllable regulators for the CPU core only to be brought up more
3810 void regulator_use_dummy_regulator(void)
3812 board_wants_dummy_regulator = true;
3814 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3817 * rdev_get_drvdata - get rdev regulator driver data
3820 * Get rdev regulator driver private data. This call can be used in the
3821 * regulator driver context.
3823 void *rdev_get_drvdata(struct regulator_dev *rdev)
3825 return rdev->reg_data;
3827 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3830 * regulator_get_drvdata - get regulator driver data
3831 * @regulator: regulator
3833 * Get regulator driver private data. This call can be used in the consumer
3834 * driver context when non API regulator specific functions need to be called.
3836 void *regulator_get_drvdata(struct regulator *regulator)
3838 return regulator->rdev->reg_data;
3840 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3843 * regulator_set_drvdata - set regulator driver data
3844 * @regulator: regulator
3847 void regulator_set_drvdata(struct regulator *regulator, void *data)
3849 regulator->rdev->reg_data = data;
3851 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3854 * regulator_get_id - get regulator ID
3857 int rdev_get_id(struct regulator_dev *rdev)
3859 return rdev->desc->id;
3861 EXPORT_SYMBOL_GPL(rdev_get_id);
3863 struct device *rdev_get_dev(struct regulator_dev *rdev)
3867 EXPORT_SYMBOL_GPL(rdev_get_dev);
3869 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3871 return reg_init_data->driver_data;
3873 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3875 #ifdef CONFIG_DEBUG_FS
3876 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3877 size_t count, loff_t *ppos)
3879 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3880 ssize_t len, ret = 0;
3881 struct regulator_map *map;
3886 list_for_each_entry(map, ®ulator_map_list, list) {
3887 len = snprintf(buf + ret, PAGE_SIZE - ret,
3889 rdev_get_name(map->regulator), map->dev_name,
3893 if (ret > PAGE_SIZE) {
3899 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3907 static const struct file_operations supply_map_fops = {
3908 #ifdef CONFIG_DEBUG_FS
3909 .read = supply_map_read_file,
3910 .llseek = default_llseek,
3914 static int __init regulator_init(void)
3918 ret = class_register(®ulator_class);
3920 debugfs_root = debugfs_create_dir("regulator", NULL);
3922 pr_warn("regulator: Failed to create debugfs directory\n");
3924 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3927 regulator_dummy_init();
3932 /* init early to allow our consumers to complete system booting */
3933 core_initcall(regulator_init);
3935 static int __init regulator_init_complete(void)
3937 struct regulator_dev *rdev;
3938 struct regulator_ops *ops;
3939 struct regulation_constraints *c;
3943 * Since DT doesn't provide an idiomatic mechanism for
3944 * enabling full constraints and since it's much more natural
3945 * with DT to provide them just assume that a DT enabled
3946 * system has full constraints.
3948 if (of_have_populated_dt())
3949 has_full_constraints = true;
3951 mutex_lock(®ulator_list_mutex);
3953 /* If we have a full configuration then disable any regulators
3954 * which are not in use or always_on. This will become the
3955 * default behaviour in the future.
3957 list_for_each_entry(rdev, ®ulator_list, list) {
3958 ops = rdev->desc->ops;
3959 c = rdev->constraints;
3961 if (c && c->always_on)
3964 mutex_lock(&rdev->mutex);
3966 if (rdev->use_count)
3969 /* If we can't read the status assume it's on. */
3970 if (ops->is_enabled)
3971 enabled = ops->is_enabled(rdev);
3978 if (has_full_constraints) {
3979 /* We log since this may kill the system if it
3981 rdev_info(rdev, "disabling\n");
3982 ret = _regulator_do_disable(rdev);
3984 rdev_err(rdev, "couldn't disable: %d\n", ret);
3987 /* The intention is that in future we will
3988 * assume that full constraints are provided
3989 * so warn even if we aren't going to do
3992 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3996 mutex_unlock(&rdev->mutex);
3999 mutex_unlock(®ulator_list_mutex);
4003 late_initcall(regulator_init_complete);