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>
27 #include <linux/gpio/consumer.h>
29 #include <linux/regmap.h>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/module.h>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/regulator.h>
42 #define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50 #define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
53 static DEFINE_MUTEX(regulator_list_mutex);
54 static LIST_HEAD(regulator_list);
55 static LIST_HEAD(regulator_map_list);
56 static LIST_HEAD(regulator_ena_gpio_list);
57 static LIST_HEAD(regulator_supply_alias_list);
58 static bool has_full_constraints;
60 static struct dentry *debugfs_root;
63 * struct regulator_map
65 * Used to provide symbolic supply names to devices.
67 struct regulator_map {
68 struct list_head list;
69 const char *dev_name; /* The dev_name() for the consumer */
71 struct regulator_dev *regulator;
75 * struct regulator_enable_gpio
77 * Management for shared enable GPIO pin
79 struct regulator_enable_gpio {
80 struct list_head list;
81 struct gpio_desc *gpiod;
82 u32 enable_count; /* a number of enabled shared GPIO */
83 u32 request_count; /* a number of requested shared GPIO */
84 unsigned int ena_gpio_invert:1;
88 * struct regulator_supply_alias
90 * Used to map lookups for a supply onto an alternative device.
92 struct regulator_supply_alias {
93 struct list_head list;
94 struct device *src_dev;
95 const char *src_supply;
96 struct device *alias_dev;
97 const char *alias_supply;
100 static int _regulator_is_enabled(struct regulator_dev *rdev);
101 static int _regulator_disable(struct regulator_dev *rdev);
102 static int _regulator_get_voltage(struct regulator_dev *rdev);
103 static int _regulator_get_current_limit(struct regulator_dev *rdev);
104 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
105 static int _notifier_call_chain(struct regulator_dev *rdev,
106 unsigned long event, void *data);
107 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
108 int min_uV, int max_uV);
109 static struct regulator *create_regulator(struct regulator_dev *rdev,
111 const char *supply_name);
112 static void _regulator_put(struct regulator *regulator);
114 static const char *rdev_get_name(struct regulator_dev *rdev)
116 if (rdev->constraints && rdev->constraints->name)
117 return rdev->constraints->name;
118 else if (rdev->desc->name)
119 return rdev->desc->name;
124 static bool have_full_constraints(void)
126 return has_full_constraints || of_have_populated_dt();
130 * of_get_regulator - get a regulator device node based on supply name
131 * @dev: Device pointer for the consumer (of regulator) device
132 * @supply: regulator supply name
134 * Extract the regulator device node corresponding to the supply name.
135 * returns the device node corresponding to the regulator if found, else
138 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
140 struct device_node *regnode = NULL;
141 char prop_name[32]; /* 32 is max size of property name */
143 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
145 snprintf(prop_name, 32, "%s-supply", supply);
146 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
149 dev_dbg(dev, "Looking up %s property in node %s failed",
150 prop_name, dev->of_node->full_name);
156 static int _regulator_can_change_status(struct regulator_dev *rdev)
158 if (!rdev->constraints)
161 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
167 /* Platform voltage constraint check */
168 static int regulator_check_voltage(struct regulator_dev *rdev,
169 int *min_uV, int *max_uV)
171 BUG_ON(*min_uV > *max_uV);
173 if (!rdev->constraints) {
174 rdev_err(rdev, "no constraints\n");
177 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
178 rdev_err(rdev, "operation not allowed\n");
182 if (*max_uV > rdev->constraints->max_uV)
183 *max_uV = rdev->constraints->max_uV;
184 if (*min_uV < rdev->constraints->min_uV)
185 *min_uV = rdev->constraints->min_uV;
187 if (*min_uV > *max_uV) {
188 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
196 /* Make sure we select a voltage that suits the needs of all
197 * regulator consumers
199 static int regulator_check_consumers(struct regulator_dev *rdev,
200 int *min_uV, int *max_uV)
202 struct regulator *regulator;
204 list_for_each_entry(regulator, &rdev->consumer_list, list) {
206 * Assume consumers that didn't say anything are OK
207 * with anything in the constraint range.
209 if (!regulator->min_uV && !regulator->max_uV)
212 if (*max_uV > regulator->max_uV)
213 *max_uV = regulator->max_uV;
214 if (*min_uV < regulator->min_uV)
215 *min_uV = regulator->min_uV;
218 if (*min_uV > *max_uV) {
219 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
227 /* current constraint check */
228 static int regulator_check_current_limit(struct regulator_dev *rdev,
229 int *min_uA, int *max_uA)
231 BUG_ON(*min_uA > *max_uA);
233 if (!rdev->constraints) {
234 rdev_err(rdev, "no constraints\n");
237 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
238 rdev_err(rdev, "operation not allowed\n");
242 if (*max_uA > rdev->constraints->max_uA)
243 *max_uA = rdev->constraints->max_uA;
244 if (*min_uA < rdev->constraints->min_uA)
245 *min_uA = rdev->constraints->min_uA;
247 if (*min_uA > *max_uA) {
248 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
256 /* operating mode constraint check */
257 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
260 case REGULATOR_MODE_FAST:
261 case REGULATOR_MODE_NORMAL:
262 case REGULATOR_MODE_IDLE:
263 case REGULATOR_MODE_STANDBY:
266 rdev_err(rdev, "invalid mode %x specified\n", *mode);
270 if (!rdev->constraints) {
271 rdev_err(rdev, "no constraints\n");
274 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
275 rdev_err(rdev, "operation not allowed\n");
279 /* The modes are bitmasks, the most power hungry modes having
280 * the lowest values. If the requested mode isn't supported
281 * try higher modes. */
283 if (rdev->constraints->valid_modes_mask & *mode)
291 /* dynamic regulator mode switching constraint check */
292 static int regulator_check_drms(struct regulator_dev *rdev)
294 if (!rdev->constraints) {
295 rdev_err(rdev, "no constraints\n");
298 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
299 rdev_err(rdev, "operation not allowed\n");
305 static ssize_t regulator_uV_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct regulator_dev *rdev = dev_get_drvdata(dev);
311 mutex_lock(&rdev->mutex);
312 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
313 mutex_unlock(&rdev->mutex);
317 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
319 static ssize_t regulator_uA_show(struct device *dev,
320 struct device_attribute *attr, char *buf)
322 struct regulator_dev *rdev = dev_get_drvdata(dev);
324 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
326 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
328 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
331 struct regulator_dev *rdev = dev_get_drvdata(dev);
333 return sprintf(buf, "%s\n", rdev_get_name(rdev));
335 static DEVICE_ATTR_RO(name);
337 static ssize_t regulator_print_opmode(char *buf, int mode)
340 case REGULATOR_MODE_FAST:
341 return sprintf(buf, "fast\n");
342 case REGULATOR_MODE_NORMAL:
343 return sprintf(buf, "normal\n");
344 case REGULATOR_MODE_IDLE:
345 return sprintf(buf, "idle\n");
346 case REGULATOR_MODE_STANDBY:
347 return sprintf(buf, "standby\n");
349 return sprintf(buf, "unknown\n");
352 static ssize_t regulator_opmode_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
355 struct regulator_dev *rdev = dev_get_drvdata(dev);
357 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
359 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
361 static ssize_t regulator_print_state(char *buf, int state)
364 return sprintf(buf, "enabled\n");
366 return sprintf(buf, "disabled\n");
368 return sprintf(buf, "unknown\n");
371 static ssize_t regulator_state_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
374 struct regulator_dev *rdev = dev_get_drvdata(dev);
377 mutex_lock(&rdev->mutex);
378 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
379 mutex_unlock(&rdev->mutex);
383 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
385 static ssize_t regulator_status_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
388 struct regulator_dev *rdev = dev_get_drvdata(dev);
392 status = rdev->desc->ops->get_status(rdev);
397 case REGULATOR_STATUS_OFF:
400 case REGULATOR_STATUS_ON:
403 case REGULATOR_STATUS_ERROR:
406 case REGULATOR_STATUS_FAST:
409 case REGULATOR_STATUS_NORMAL:
412 case REGULATOR_STATUS_IDLE:
415 case REGULATOR_STATUS_STANDBY:
418 case REGULATOR_STATUS_BYPASS:
421 case REGULATOR_STATUS_UNDEFINED:
428 return sprintf(buf, "%s\n", label);
430 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
432 static ssize_t regulator_min_uA_show(struct device *dev,
433 struct device_attribute *attr, char *buf)
435 struct regulator_dev *rdev = dev_get_drvdata(dev);
437 if (!rdev->constraints)
438 return sprintf(buf, "constraint not defined\n");
440 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
442 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
444 static ssize_t regulator_max_uA_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct regulator_dev *rdev = dev_get_drvdata(dev);
449 if (!rdev->constraints)
450 return sprintf(buf, "constraint not defined\n");
452 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
454 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
456 static ssize_t regulator_min_uV_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
459 struct regulator_dev *rdev = dev_get_drvdata(dev);
461 if (!rdev->constraints)
462 return sprintf(buf, "constraint not defined\n");
464 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
466 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
468 static ssize_t regulator_max_uV_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 struct regulator_dev *rdev = dev_get_drvdata(dev);
473 if (!rdev->constraints)
474 return sprintf(buf, "constraint not defined\n");
476 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
478 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
480 static ssize_t regulator_total_uA_show(struct device *dev,
481 struct device_attribute *attr, char *buf)
483 struct regulator_dev *rdev = dev_get_drvdata(dev);
484 struct regulator *regulator;
487 mutex_lock(&rdev->mutex);
488 list_for_each_entry(regulator, &rdev->consumer_list, list)
489 uA += regulator->uA_load;
490 mutex_unlock(&rdev->mutex);
491 return sprintf(buf, "%d\n", uA);
493 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
495 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
498 struct regulator_dev *rdev = dev_get_drvdata(dev);
499 return sprintf(buf, "%d\n", rdev->use_count);
501 static DEVICE_ATTR_RO(num_users);
503 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
506 struct regulator_dev *rdev = dev_get_drvdata(dev);
508 switch (rdev->desc->type) {
509 case REGULATOR_VOLTAGE:
510 return sprintf(buf, "voltage\n");
511 case REGULATOR_CURRENT:
512 return sprintf(buf, "current\n");
514 return sprintf(buf, "unknown\n");
516 static DEVICE_ATTR_RO(type);
518 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
519 struct device_attribute *attr, char *buf)
521 struct regulator_dev *rdev = dev_get_drvdata(dev);
523 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
525 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
526 regulator_suspend_mem_uV_show, NULL);
528 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
529 struct device_attribute *attr, char *buf)
531 struct regulator_dev *rdev = dev_get_drvdata(dev);
533 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
535 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
536 regulator_suspend_disk_uV_show, NULL);
538 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
539 struct device_attribute *attr, char *buf)
541 struct regulator_dev *rdev = dev_get_drvdata(dev);
543 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
545 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
546 regulator_suspend_standby_uV_show, NULL);
548 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
549 struct device_attribute *attr, char *buf)
551 struct regulator_dev *rdev = dev_get_drvdata(dev);
553 return regulator_print_opmode(buf,
554 rdev->constraints->state_mem.mode);
556 static DEVICE_ATTR(suspend_mem_mode, 0444,
557 regulator_suspend_mem_mode_show, NULL);
559 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
562 struct regulator_dev *rdev = dev_get_drvdata(dev);
564 return regulator_print_opmode(buf,
565 rdev->constraints->state_disk.mode);
567 static DEVICE_ATTR(suspend_disk_mode, 0444,
568 regulator_suspend_disk_mode_show, NULL);
570 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
573 struct regulator_dev *rdev = dev_get_drvdata(dev);
575 return regulator_print_opmode(buf,
576 rdev->constraints->state_standby.mode);
578 static DEVICE_ATTR(suspend_standby_mode, 0444,
579 regulator_suspend_standby_mode_show, NULL);
581 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
582 struct device_attribute *attr, char *buf)
584 struct regulator_dev *rdev = dev_get_drvdata(dev);
586 return regulator_print_state(buf,
587 rdev->constraints->state_mem.enabled);
589 static DEVICE_ATTR(suspend_mem_state, 0444,
590 regulator_suspend_mem_state_show, NULL);
592 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 struct regulator_dev *rdev = dev_get_drvdata(dev);
597 return regulator_print_state(buf,
598 rdev->constraints->state_disk.enabled);
600 static DEVICE_ATTR(suspend_disk_state, 0444,
601 regulator_suspend_disk_state_show, NULL);
603 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
604 struct device_attribute *attr, char *buf)
606 struct regulator_dev *rdev = dev_get_drvdata(dev);
608 return regulator_print_state(buf,
609 rdev->constraints->state_standby.enabled);
611 static DEVICE_ATTR(suspend_standby_state, 0444,
612 regulator_suspend_standby_state_show, NULL);
614 static ssize_t regulator_bypass_show(struct device *dev,
615 struct device_attribute *attr, char *buf)
617 struct regulator_dev *rdev = dev_get_drvdata(dev);
622 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
631 return sprintf(buf, "%s\n", report);
633 static DEVICE_ATTR(bypass, 0444,
634 regulator_bypass_show, NULL);
636 /* Calculate the new optimum regulator operating mode based on the new total
637 * consumer load. All locks held by caller */
638 static int drms_uA_update(struct regulator_dev *rdev)
640 struct regulator *sibling;
641 int current_uA = 0, output_uV, input_uV, err;
645 * first check to see if we can set modes at all, otherwise just
646 * tell the consumer everything is OK.
648 err = regulator_check_drms(rdev);
652 if (!rdev->desc->ops->get_optimum_mode &&
653 !rdev->desc->ops->set_load)
656 if (!rdev->desc->ops->set_mode &&
657 !rdev->desc->ops->set_load)
660 /* get output voltage */
661 output_uV = _regulator_get_voltage(rdev);
662 if (output_uV <= 0) {
663 rdev_err(rdev, "invalid output voltage found\n");
667 /* get input voltage */
670 input_uV = regulator_get_voltage(rdev->supply);
672 input_uV = rdev->constraints->input_uV;
674 rdev_err(rdev, "invalid input voltage found\n");
678 /* calc total requested load */
679 list_for_each_entry(sibling, &rdev->consumer_list, list)
680 current_uA += sibling->uA_load;
682 current_uA += rdev->constraints->system_load;
684 if (rdev->desc->ops->set_load) {
685 /* set the optimum mode for our new total regulator load */
686 err = rdev->desc->ops->set_load(rdev, current_uA);
688 rdev_err(rdev, "failed to set load %d\n", current_uA);
690 /* now get the optimum mode for our new total regulator load */
691 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
692 output_uV, current_uA);
694 /* check the new mode is allowed */
695 err = regulator_mode_constrain(rdev, &mode);
697 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
698 current_uA, input_uV, output_uV);
702 err = rdev->desc->ops->set_mode(rdev, mode);
704 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
710 static int suspend_set_state(struct regulator_dev *rdev,
711 struct regulator_state *rstate)
715 /* If we have no suspend mode configration don't set anything;
716 * only warn if the driver implements set_suspend_voltage or
717 * set_suspend_mode callback.
719 if (!rstate->enabled && !rstate->disabled) {
720 if (rdev->desc->ops->set_suspend_voltage ||
721 rdev->desc->ops->set_suspend_mode)
722 rdev_warn(rdev, "No configuration\n");
726 if (rstate->enabled && rstate->disabled) {
727 rdev_err(rdev, "invalid configuration\n");
731 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
732 ret = rdev->desc->ops->set_suspend_enable(rdev);
733 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
734 ret = rdev->desc->ops->set_suspend_disable(rdev);
735 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
739 rdev_err(rdev, "failed to enabled/disable\n");
743 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
744 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
746 rdev_err(rdev, "failed to set voltage\n");
751 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
752 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
754 rdev_err(rdev, "failed to set mode\n");
761 /* locks held by caller */
762 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
764 if (!rdev->constraints)
768 case PM_SUSPEND_STANDBY:
769 return suspend_set_state(rdev,
770 &rdev->constraints->state_standby);
772 return suspend_set_state(rdev,
773 &rdev->constraints->state_mem);
775 return suspend_set_state(rdev,
776 &rdev->constraints->state_disk);
782 static void print_constraints(struct regulator_dev *rdev)
784 struct regulation_constraints *constraints = rdev->constraints;
786 size_t len = sizeof(buf) - 1;
790 if (constraints->min_uV && constraints->max_uV) {
791 if (constraints->min_uV == constraints->max_uV)
792 count += scnprintf(buf + count, len - count, "%d mV ",
793 constraints->min_uV / 1000);
795 count += scnprintf(buf + count, len - count,
797 constraints->min_uV / 1000,
798 constraints->max_uV / 1000);
801 if (!constraints->min_uV ||
802 constraints->min_uV != constraints->max_uV) {
803 ret = _regulator_get_voltage(rdev);
805 count += scnprintf(buf + count, len - count,
806 "at %d mV ", ret / 1000);
809 if (constraints->uV_offset)
810 count += scnprintf(buf + count, len - count, "%dmV offset ",
811 constraints->uV_offset / 1000);
813 if (constraints->min_uA && constraints->max_uA) {
814 if (constraints->min_uA == constraints->max_uA)
815 count += scnprintf(buf + count, len - count, "%d mA ",
816 constraints->min_uA / 1000);
818 count += scnprintf(buf + count, len - count,
820 constraints->min_uA / 1000,
821 constraints->max_uA / 1000);
824 if (!constraints->min_uA ||
825 constraints->min_uA != constraints->max_uA) {
826 ret = _regulator_get_current_limit(rdev);
828 count += scnprintf(buf + count, len - count,
829 "at %d mA ", ret / 1000);
832 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
833 count += scnprintf(buf + count, len - count, "fast ");
834 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
835 count += scnprintf(buf + count, len - count, "normal ");
836 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
837 count += scnprintf(buf + count, len - count, "idle ");
838 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
839 count += scnprintf(buf + count, len - count, "standby");
842 scnprintf(buf, len, "no parameters");
844 rdev_dbg(rdev, "%s\n", buf);
846 if ((constraints->min_uV != constraints->max_uV) &&
847 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
849 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
852 static int machine_constraints_voltage(struct regulator_dev *rdev,
853 struct regulation_constraints *constraints)
855 const struct regulator_ops *ops = rdev->desc->ops;
858 /* do we need to apply the constraint voltage */
859 if (rdev->constraints->apply_uV &&
860 rdev->constraints->min_uV == rdev->constraints->max_uV) {
861 int current_uV = _regulator_get_voltage(rdev);
862 if (current_uV < 0) {
864 "failed to get the current voltage(%d)\n",
868 if (current_uV < rdev->constraints->min_uV ||
869 current_uV > rdev->constraints->max_uV) {
870 ret = _regulator_do_set_voltage(
871 rdev, rdev->constraints->min_uV,
872 rdev->constraints->max_uV);
875 "failed to apply %duV constraint(%d)\n",
876 rdev->constraints->min_uV, ret);
882 /* constrain machine-level voltage specs to fit
883 * the actual range supported by this regulator.
885 if (ops->list_voltage && rdev->desc->n_voltages) {
886 int count = rdev->desc->n_voltages;
888 int min_uV = INT_MAX;
889 int max_uV = INT_MIN;
890 int cmin = constraints->min_uV;
891 int cmax = constraints->max_uV;
893 /* it's safe to autoconfigure fixed-voltage supplies
894 and the constraints are used by list_voltage. */
895 if (count == 1 && !cmin) {
898 constraints->min_uV = cmin;
899 constraints->max_uV = cmax;
902 /* voltage constraints are optional */
903 if ((cmin == 0) && (cmax == 0))
906 /* else require explicit machine-level constraints */
907 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
908 rdev_err(rdev, "invalid voltage constraints\n");
912 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
913 for (i = 0; i < count; i++) {
916 value = ops->list_voltage(rdev, i);
920 /* maybe adjust [min_uV..max_uV] */
921 if (value >= cmin && value < min_uV)
923 if (value <= cmax && value > max_uV)
927 /* final: [min_uV..max_uV] valid iff constraints valid */
928 if (max_uV < min_uV) {
930 "unsupportable voltage constraints %u-%uuV\n",
935 /* use regulator's subset of machine constraints */
936 if (constraints->min_uV < min_uV) {
937 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
938 constraints->min_uV, min_uV);
939 constraints->min_uV = min_uV;
941 if (constraints->max_uV > max_uV) {
942 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
943 constraints->max_uV, max_uV);
944 constraints->max_uV = max_uV;
951 static int machine_constraints_current(struct regulator_dev *rdev,
952 struct regulation_constraints *constraints)
954 const struct regulator_ops *ops = rdev->desc->ops;
957 if (!constraints->min_uA && !constraints->max_uA)
960 if (constraints->min_uA > constraints->max_uA) {
961 rdev_err(rdev, "Invalid current constraints\n");
965 if (!ops->set_current_limit || !ops->get_current_limit) {
966 rdev_warn(rdev, "Operation of current configuration missing\n");
970 /* Set regulator current in constraints range */
971 ret = ops->set_current_limit(rdev, constraints->min_uA,
972 constraints->max_uA);
974 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
981 static int _regulator_do_enable(struct regulator_dev *rdev);
984 * set_machine_constraints - sets regulator constraints
985 * @rdev: regulator source
986 * @constraints: constraints to apply
988 * Allows platform initialisation code to define and constrain
989 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
990 * Constraints *must* be set by platform code in order for some
991 * regulator operations to proceed i.e. set_voltage, set_current_limit,
994 static int set_machine_constraints(struct regulator_dev *rdev,
995 const struct regulation_constraints *constraints)
998 const struct regulator_ops *ops = rdev->desc->ops;
1001 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1004 rdev->constraints = kzalloc(sizeof(*constraints),
1006 if (!rdev->constraints)
1009 ret = machine_constraints_voltage(rdev, rdev->constraints);
1013 ret = machine_constraints_current(rdev, rdev->constraints);
1017 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1018 ret = ops->set_input_current_limit(rdev,
1019 rdev->constraints->ilim_uA);
1021 rdev_err(rdev, "failed to set input limit\n");
1026 /* do we need to setup our suspend state */
1027 if (rdev->constraints->initial_state) {
1028 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
1030 rdev_err(rdev, "failed to set suspend state\n");
1035 if (rdev->constraints->initial_mode) {
1036 if (!ops->set_mode) {
1037 rdev_err(rdev, "no set_mode operation\n");
1042 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1044 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1049 /* If the constraints say the regulator should be on at this point
1050 * and we have control then make sure it is enabled.
1052 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1053 ret = _regulator_do_enable(rdev);
1054 if (ret < 0 && ret != -EINVAL) {
1055 rdev_err(rdev, "failed to enable\n");
1060 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1061 && ops->set_ramp_delay) {
1062 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1064 rdev_err(rdev, "failed to set ramp_delay\n");
1069 if (rdev->constraints->pull_down && ops->set_pull_down) {
1070 ret = ops->set_pull_down(rdev);
1072 rdev_err(rdev, "failed to set pull down\n");
1077 if (rdev->constraints->soft_start && ops->set_soft_start) {
1078 ret = ops->set_soft_start(rdev);
1080 rdev_err(rdev, "failed to set soft start\n");
1085 print_constraints(rdev);
1088 kfree(rdev->constraints);
1089 rdev->constraints = NULL;
1094 * set_supply - set regulator supply regulator
1095 * @rdev: regulator name
1096 * @supply_rdev: supply regulator name
1098 * Called by platform initialisation code to set the supply regulator for this
1099 * regulator. This ensures that a regulators supply will also be enabled by the
1100 * core if it's child is enabled.
1102 static int set_supply(struct regulator_dev *rdev,
1103 struct regulator_dev *supply_rdev)
1107 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1109 if (!try_module_get(supply_rdev->owner))
1112 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1113 if (rdev->supply == NULL) {
1117 supply_rdev->open_count++;
1123 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1124 * @rdev: regulator source
1125 * @consumer_dev_name: dev_name() string for device supply applies to
1126 * @supply: symbolic name for supply
1128 * Allows platform initialisation code to map physical regulator
1129 * sources to symbolic names for supplies for use by devices. Devices
1130 * should use these symbolic names to request regulators, avoiding the
1131 * need to provide board-specific regulator names as platform data.
1133 static int set_consumer_device_supply(struct regulator_dev *rdev,
1134 const char *consumer_dev_name,
1137 struct regulator_map *node;
1143 if (consumer_dev_name != NULL)
1148 list_for_each_entry(node, ®ulator_map_list, list) {
1149 if (node->dev_name && consumer_dev_name) {
1150 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1152 } else if (node->dev_name || consumer_dev_name) {
1156 if (strcmp(node->supply, supply) != 0)
1159 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1161 dev_name(&node->regulator->dev),
1162 node->regulator->desc->name,
1164 dev_name(&rdev->dev), rdev_get_name(rdev));
1168 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1172 node->regulator = rdev;
1173 node->supply = supply;
1176 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1177 if (node->dev_name == NULL) {
1183 list_add(&node->list, ®ulator_map_list);
1187 static void unset_regulator_supplies(struct regulator_dev *rdev)
1189 struct regulator_map *node, *n;
1191 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1192 if (rdev == node->regulator) {
1193 list_del(&node->list);
1194 kfree(node->dev_name);
1200 #define REG_STR_SIZE 64
1202 static struct regulator *create_regulator(struct regulator_dev *rdev,
1204 const char *supply_name)
1206 struct regulator *regulator;
1207 char buf[REG_STR_SIZE];
1210 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1211 if (regulator == NULL)
1214 mutex_lock(&rdev->mutex);
1215 regulator->rdev = rdev;
1216 list_add(®ulator->list, &rdev->consumer_list);
1219 regulator->dev = dev;
1221 /* Add a link to the device sysfs entry */
1222 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1223 dev->kobj.name, supply_name);
1224 if (size >= REG_STR_SIZE)
1227 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1228 if (regulator->supply_name == NULL)
1231 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1234 rdev_dbg(rdev, "could not add device link %s err %d\n",
1235 dev->kobj.name, err);
1239 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1240 if (regulator->supply_name == NULL)
1244 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1246 if (!regulator->debugfs) {
1247 rdev_warn(rdev, "Failed to create debugfs directory\n");
1249 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1250 ®ulator->uA_load);
1251 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1252 ®ulator->min_uV);
1253 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1254 ®ulator->max_uV);
1258 * Check now if the regulator is an always on regulator - if
1259 * it is then we don't need to do nearly so much work for
1260 * enable/disable calls.
1262 if (!_regulator_can_change_status(rdev) &&
1263 _regulator_is_enabled(rdev))
1264 regulator->always_on = true;
1266 mutex_unlock(&rdev->mutex);
1269 list_del(®ulator->list);
1271 mutex_unlock(&rdev->mutex);
1275 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1277 if (rdev->constraints && rdev->constraints->enable_time)
1278 return rdev->constraints->enable_time;
1279 if (!rdev->desc->ops->enable_time)
1280 return rdev->desc->enable_time;
1281 return rdev->desc->ops->enable_time(rdev);
1284 static struct regulator_supply_alias *regulator_find_supply_alias(
1285 struct device *dev, const char *supply)
1287 struct regulator_supply_alias *map;
1289 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1290 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1296 static void regulator_supply_alias(struct device **dev, const char **supply)
1298 struct regulator_supply_alias *map;
1300 map = regulator_find_supply_alias(*dev, *supply);
1302 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1303 *supply, map->alias_supply,
1304 dev_name(map->alias_dev));
1305 *dev = map->alias_dev;
1306 *supply = map->alias_supply;
1310 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1314 struct regulator_dev *r;
1315 struct device_node *node;
1316 struct regulator_map *map;
1317 const char *devname = NULL;
1319 regulator_supply_alias(&dev, &supply);
1321 /* first do a dt based lookup */
1322 if (dev && dev->of_node) {
1323 node = of_get_regulator(dev, supply);
1325 list_for_each_entry(r, ®ulator_list, list)
1326 if (r->dev.parent &&
1327 node == r->dev.of_node)
1329 *ret = -EPROBE_DEFER;
1333 * If we couldn't even get the node then it's
1334 * not just that the device didn't register
1335 * yet, there's no node and we'll never
1342 /* if not found, try doing it non-dt way */
1344 devname = dev_name(dev);
1346 list_for_each_entry(r, ®ulator_list, list)
1347 if (strcmp(rdev_get_name(r), supply) == 0)
1350 list_for_each_entry(map, ®ulator_map_list, list) {
1351 /* If the mapping has a device set up it must match */
1352 if (map->dev_name &&
1353 (!devname || strcmp(map->dev_name, devname)))
1356 if (strcmp(map->supply, supply) == 0)
1357 return map->regulator;
1364 static int regulator_resolve_supply(struct regulator_dev *rdev)
1366 struct regulator_dev *r;
1367 struct device *dev = rdev->dev.parent;
1370 /* No supply to resovle? */
1371 if (!rdev->supply_name)
1374 /* Supply already resolved? */
1378 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1379 if (ret == -ENODEV) {
1381 * No supply was specified for this regulator and
1382 * there will never be one.
1388 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1389 rdev->supply_name, rdev->desc->name);
1390 return -EPROBE_DEFER;
1393 /* Recursively resolve the supply of the supply */
1394 ret = regulator_resolve_supply(r);
1398 ret = set_supply(rdev, r);
1402 /* Cascade always-on state to supply */
1403 if (_regulator_is_enabled(rdev)) {
1404 ret = regulator_enable(rdev->supply);
1407 _regulator_put(rdev->supply);
1415 /* Internal regulator request function */
1416 static struct regulator *_regulator_get(struct device *dev, const char *id,
1417 bool exclusive, bool allow_dummy)
1419 struct regulator_dev *rdev;
1420 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1421 const char *devname = NULL;
1425 pr_err("get() with no identifier\n");
1426 return ERR_PTR(-EINVAL);
1430 devname = dev_name(dev);
1432 if (have_full_constraints())
1435 ret = -EPROBE_DEFER;
1437 mutex_lock(®ulator_list_mutex);
1439 rdev = regulator_dev_lookup(dev, id, &ret);
1443 regulator = ERR_PTR(ret);
1446 * If we have return value from dev_lookup fail, we do not expect to
1447 * succeed, so, quit with appropriate error value
1449 if (ret && ret != -ENODEV)
1453 devname = "deviceless";
1456 * Assume that a regulator is physically present and enabled
1457 * even if it isn't hooked up and just provide a dummy.
1459 if (have_full_constraints() && allow_dummy) {
1460 pr_warn("%s supply %s not found, using dummy regulator\n",
1463 rdev = dummy_regulator_rdev;
1465 /* Don't log an error when called from regulator_get_optional() */
1466 } else if (!have_full_constraints() || exclusive) {
1467 dev_warn(dev, "dummy supplies not allowed\n");
1470 mutex_unlock(®ulator_list_mutex);
1474 if (rdev->exclusive) {
1475 regulator = ERR_PTR(-EPERM);
1479 if (exclusive && rdev->open_count) {
1480 regulator = ERR_PTR(-EBUSY);
1484 ret = regulator_resolve_supply(rdev);
1486 regulator = ERR_PTR(ret);
1490 if (!try_module_get(rdev->owner))
1493 regulator = create_regulator(rdev, dev, id);
1494 if (regulator == NULL) {
1495 regulator = ERR_PTR(-ENOMEM);
1496 module_put(rdev->owner);
1502 rdev->exclusive = 1;
1504 ret = _regulator_is_enabled(rdev);
1506 rdev->use_count = 1;
1508 rdev->use_count = 0;
1512 mutex_unlock(®ulator_list_mutex);
1518 * regulator_get - lookup and obtain a reference to a regulator.
1519 * @dev: device for regulator "consumer"
1520 * @id: Supply name or regulator ID.
1522 * Returns a struct regulator corresponding to the regulator producer,
1523 * or IS_ERR() condition containing errno.
1525 * Use of supply names configured via regulator_set_device_supply() is
1526 * strongly encouraged. It is recommended that the supply name used
1527 * should match the name used for the supply and/or the relevant
1528 * device pins in the datasheet.
1530 struct regulator *regulator_get(struct device *dev, const char *id)
1532 return _regulator_get(dev, id, false, true);
1534 EXPORT_SYMBOL_GPL(regulator_get);
1537 * regulator_get_exclusive - obtain exclusive access to a regulator.
1538 * @dev: device for regulator "consumer"
1539 * @id: Supply name or regulator ID.
1541 * Returns a struct regulator corresponding to the regulator producer,
1542 * or IS_ERR() condition containing errno. Other consumers will be
1543 * unable to obtain this regulator while this reference is held and the
1544 * use count for the regulator will be initialised to reflect the current
1545 * state of the regulator.
1547 * This is intended for use by consumers which cannot tolerate shared
1548 * use of the regulator such as those which need to force the
1549 * regulator off for correct operation of the hardware they are
1552 * Use of supply names configured via regulator_set_device_supply() is
1553 * strongly encouraged. It is recommended that the supply name used
1554 * should match the name used for the supply and/or the relevant
1555 * device pins in the datasheet.
1557 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1559 return _regulator_get(dev, id, true, false);
1561 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1564 * regulator_get_optional - obtain optional access to a regulator.
1565 * @dev: device for regulator "consumer"
1566 * @id: Supply name or regulator ID.
1568 * Returns a struct regulator corresponding to the regulator producer,
1569 * or IS_ERR() condition containing errno.
1571 * This is intended for use by consumers for devices which can have
1572 * some supplies unconnected in normal use, such as some MMC devices.
1573 * It can allow the regulator core to provide stub supplies for other
1574 * supplies requested using normal regulator_get() calls without
1575 * disrupting the operation of drivers that can handle absent
1578 * Use of supply names configured via regulator_set_device_supply() is
1579 * strongly encouraged. It is recommended that the supply name used
1580 * should match the name used for the supply and/or the relevant
1581 * device pins in the datasheet.
1583 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1585 return _regulator_get(dev, id, false, false);
1587 EXPORT_SYMBOL_GPL(regulator_get_optional);
1589 /* regulator_list_mutex lock held by regulator_put() */
1590 static void _regulator_put(struct regulator *regulator)
1592 struct regulator_dev *rdev;
1594 if (regulator == NULL || IS_ERR(regulator))
1597 rdev = regulator->rdev;
1599 debugfs_remove_recursive(regulator->debugfs);
1601 /* remove any sysfs entries */
1603 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1604 mutex_lock(&rdev->mutex);
1605 kfree(regulator->supply_name);
1606 list_del(®ulator->list);
1610 rdev->exclusive = 0;
1611 mutex_unlock(&rdev->mutex);
1613 module_put(rdev->owner);
1617 * regulator_put - "free" the regulator source
1618 * @regulator: regulator source
1620 * Note: drivers must ensure that all regulator_enable calls made on this
1621 * regulator source are balanced by regulator_disable calls prior to calling
1624 void regulator_put(struct regulator *regulator)
1626 mutex_lock(®ulator_list_mutex);
1627 _regulator_put(regulator);
1628 mutex_unlock(®ulator_list_mutex);
1630 EXPORT_SYMBOL_GPL(regulator_put);
1633 * regulator_register_supply_alias - Provide device alias for supply lookup
1635 * @dev: device that will be given as the regulator "consumer"
1636 * @id: Supply name or regulator ID
1637 * @alias_dev: device that should be used to lookup the supply
1638 * @alias_id: Supply name or regulator ID that should be used to lookup the
1641 * All lookups for id on dev will instead be conducted for alias_id on
1644 int regulator_register_supply_alias(struct device *dev, const char *id,
1645 struct device *alias_dev,
1646 const char *alias_id)
1648 struct regulator_supply_alias *map;
1650 map = regulator_find_supply_alias(dev, id);
1654 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1659 map->src_supply = id;
1660 map->alias_dev = alias_dev;
1661 map->alias_supply = alias_id;
1663 list_add(&map->list, ®ulator_supply_alias_list);
1665 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1666 id, dev_name(dev), alias_id, dev_name(alias_dev));
1670 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1673 * regulator_unregister_supply_alias - Remove device alias
1675 * @dev: device that will be given as the regulator "consumer"
1676 * @id: Supply name or regulator ID
1678 * Remove a lookup alias if one exists for id on dev.
1680 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1682 struct regulator_supply_alias *map;
1684 map = regulator_find_supply_alias(dev, id);
1686 list_del(&map->list);
1690 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1693 * regulator_bulk_register_supply_alias - register multiple aliases
1695 * @dev: device that will be given as the regulator "consumer"
1696 * @id: List of supply names or regulator IDs
1697 * @alias_dev: device that should be used to lookup the supply
1698 * @alias_id: List of supply names or regulator IDs that should be used to
1700 * @num_id: Number of aliases to register
1702 * @return 0 on success, an errno on failure.
1704 * This helper function allows drivers to register several supply
1705 * aliases in one operation. If any of the aliases cannot be
1706 * registered any aliases that were registered will be removed
1707 * before returning to the caller.
1709 int regulator_bulk_register_supply_alias(struct device *dev,
1710 const char *const *id,
1711 struct device *alias_dev,
1712 const char *const *alias_id,
1718 for (i = 0; i < num_id; ++i) {
1719 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1729 "Failed to create supply alias %s,%s -> %s,%s\n",
1730 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1733 regulator_unregister_supply_alias(dev, id[i]);
1737 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1740 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1742 * @dev: device that will be given as the regulator "consumer"
1743 * @id: List of supply names or regulator IDs
1744 * @num_id: Number of aliases to unregister
1746 * This helper function allows drivers to unregister several supply
1747 * aliases in one operation.
1749 void regulator_bulk_unregister_supply_alias(struct device *dev,
1750 const char *const *id,
1755 for (i = 0; i < num_id; ++i)
1756 regulator_unregister_supply_alias(dev, id[i]);
1758 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1761 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1762 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1763 const struct regulator_config *config)
1765 struct regulator_enable_gpio *pin;
1766 struct gpio_desc *gpiod;
1769 gpiod = gpio_to_desc(config->ena_gpio);
1771 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1772 if (pin->gpiod == gpiod) {
1773 rdev_dbg(rdev, "GPIO %d is already used\n",
1775 goto update_ena_gpio_to_rdev;
1779 ret = gpio_request_one(config->ena_gpio,
1780 GPIOF_DIR_OUT | config->ena_gpio_flags,
1781 rdev_get_name(rdev));
1785 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1787 gpio_free(config->ena_gpio);
1792 pin->ena_gpio_invert = config->ena_gpio_invert;
1793 list_add(&pin->list, ®ulator_ena_gpio_list);
1795 update_ena_gpio_to_rdev:
1796 pin->request_count++;
1797 rdev->ena_pin = pin;
1801 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1803 struct regulator_enable_gpio *pin, *n;
1808 /* Free the GPIO only in case of no use */
1809 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1810 if (pin->gpiod == rdev->ena_pin->gpiod) {
1811 if (pin->request_count <= 1) {
1812 pin->request_count = 0;
1813 gpiod_put(pin->gpiod);
1814 list_del(&pin->list);
1816 rdev->ena_pin = NULL;
1819 pin->request_count--;
1826 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1827 * @rdev: regulator_dev structure
1828 * @enable: enable GPIO at initial use?
1830 * GPIO is enabled in case of initial use. (enable_count is 0)
1831 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1833 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1835 struct regulator_enable_gpio *pin = rdev->ena_pin;
1841 /* Enable GPIO at initial use */
1842 if (pin->enable_count == 0)
1843 gpiod_set_value_cansleep(pin->gpiod,
1844 !pin->ena_gpio_invert);
1846 pin->enable_count++;
1848 if (pin->enable_count > 1) {
1849 pin->enable_count--;
1853 /* Disable GPIO if not used */
1854 if (pin->enable_count <= 1) {
1855 gpiod_set_value_cansleep(pin->gpiod,
1856 pin->ena_gpio_invert);
1857 pin->enable_count = 0;
1865 * _regulator_enable_delay - a delay helper function
1866 * @delay: time to delay in microseconds
1868 * Delay for the requested amount of time as per the guidelines in:
1870 * Documentation/timers/timers-howto.txt
1872 * The assumption here is that regulators will never be enabled in
1873 * atomic context and therefore sleeping functions can be used.
1875 static void _regulator_enable_delay(unsigned int delay)
1877 unsigned int ms = delay / 1000;
1878 unsigned int us = delay % 1000;
1882 * For small enough values, handle super-millisecond
1883 * delays in the usleep_range() call below.
1892 * Give the scheduler some room to coalesce with any other
1893 * wakeup sources. For delays shorter than 10 us, don't even
1894 * bother setting up high-resolution timers and just busy-
1898 usleep_range(us, us + 100);
1903 static int _regulator_do_enable(struct regulator_dev *rdev)
1907 /* Query before enabling in case configuration dependent. */
1908 ret = _regulator_get_enable_time(rdev);
1912 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1916 trace_regulator_enable(rdev_get_name(rdev));
1918 if (rdev->desc->off_on_delay) {
1919 /* if needed, keep a distance of off_on_delay from last time
1920 * this regulator was disabled.
1922 unsigned long start_jiffy = jiffies;
1923 unsigned long intended, max_delay, remaining;
1925 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
1926 intended = rdev->last_off_jiffy + max_delay;
1928 if (time_before(start_jiffy, intended)) {
1929 /* calc remaining jiffies to deal with one-time
1931 * in case of multiple timer wrapping, either it can be
1932 * detected by out-of-range remaining, or it cannot be
1933 * detected and we gets a panelty of
1934 * _regulator_enable_delay().
1936 remaining = intended - start_jiffy;
1937 if (remaining <= max_delay)
1938 _regulator_enable_delay(
1939 jiffies_to_usecs(remaining));
1943 if (rdev->ena_pin) {
1944 if (!rdev->ena_gpio_state) {
1945 ret = regulator_ena_gpio_ctrl(rdev, true);
1948 rdev->ena_gpio_state = 1;
1950 } else if (rdev->desc->ops->enable) {
1951 ret = rdev->desc->ops->enable(rdev);
1958 /* Allow the regulator to ramp; it would be useful to extend
1959 * this for bulk operations so that the regulators can ramp
1961 trace_regulator_enable_delay(rdev_get_name(rdev));
1963 _regulator_enable_delay(delay);
1965 trace_regulator_enable_complete(rdev_get_name(rdev));
1970 /* locks held by regulator_enable() */
1971 static int _regulator_enable(struct regulator_dev *rdev)
1975 /* check voltage and requested load before enabling */
1976 if (rdev->constraints &&
1977 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1978 drms_uA_update(rdev);
1980 if (rdev->use_count == 0) {
1981 /* The regulator may on if it's not switchable or left on */
1982 ret = _regulator_is_enabled(rdev);
1983 if (ret == -EINVAL || ret == 0) {
1984 if (!_regulator_can_change_status(rdev))
1987 ret = _regulator_do_enable(rdev);
1991 } else if (ret < 0) {
1992 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1995 /* Fallthrough on positive return values - already enabled */
2004 * regulator_enable - enable regulator output
2005 * @regulator: regulator source
2007 * Request that the regulator be enabled with the regulator output at
2008 * the predefined voltage or current value. Calls to regulator_enable()
2009 * must be balanced with calls to regulator_disable().
2011 * NOTE: the output value can be set by other drivers, boot loader or may be
2012 * hardwired in the regulator.
2014 int regulator_enable(struct regulator *regulator)
2016 struct regulator_dev *rdev = regulator->rdev;
2019 if (regulator->always_on)
2023 ret = regulator_enable(rdev->supply);
2028 mutex_lock(&rdev->mutex);
2029 ret = _regulator_enable(rdev);
2030 mutex_unlock(&rdev->mutex);
2032 if (ret != 0 && rdev->supply)
2033 regulator_disable(rdev->supply);
2037 EXPORT_SYMBOL_GPL(regulator_enable);
2039 static int _regulator_do_disable(struct regulator_dev *rdev)
2043 trace_regulator_disable(rdev_get_name(rdev));
2045 if (rdev->ena_pin) {
2046 if (rdev->ena_gpio_state) {
2047 ret = regulator_ena_gpio_ctrl(rdev, false);
2050 rdev->ena_gpio_state = 0;
2053 } else if (rdev->desc->ops->disable) {
2054 ret = rdev->desc->ops->disable(rdev);
2059 /* cares about last_off_jiffy only if off_on_delay is required by
2062 if (rdev->desc->off_on_delay)
2063 rdev->last_off_jiffy = jiffies;
2065 trace_regulator_disable_complete(rdev_get_name(rdev));
2070 /* locks held by regulator_disable() */
2071 static int _regulator_disable(struct regulator_dev *rdev)
2075 if (WARN(rdev->use_count <= 0,
2076 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2079 /* are we the last user and permitted to disable ? */
2080 if (rdev->use_count == 1 &&
2081 (rdev->constraints && !rdev->constraints->always_on)) {
2083 /* we are last user */
2084 if (_regulator_can_change_status(rdev)) {
2085 ret = _notifier_call_chain(rdev,
2086 REGULATOR_EVENT_PRE_DISABLE,
2088 if (ret & NOTIFY_STOP_MASK)
2091 ret = _regulator_do_disable(rdev);
2093 rdev_err(rdev, "failed to disable\n");
2094 _notifier_call_chain(rdev,
2095 REGULATOR_EVENT_ABORT_DISABLE,
2099 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2103 rdev->use_count = 0;
2104 } else if (rdev->use_count > 1) {
2106 if (rdev->constraints &&
2107 (rdev->constraints->valid_ops_mask &
2108 REGULATOR_CHANGE_DRMS))
2109 drms_uA_update(rdev);
2118 * regulator_disable - disable regulator output
2119 * @regulator: regulator source
2121 * Disable the regulator output voltage or current. Calls to
2122 * regulator_enable() must be balanced with calls to
2123 * regulator_disable().
2125 * NOTE: this will only disable the regulator output if no other consumer
2126 * devices have it enabled, the regulator device supports disabling and
2127 * machine constraints permit this operation.
2129 int regulator_disable(struct regulator *regulator)
2131 struct regulator_dev *rdev = regulator->rdev;
2134 if (regulator->always_on)
2137 mutex_lock(&rdev->mutex);
2138 ret = _regulator_disable(rdev);
2139 mutex_unlock(&rdev->mutex);
2141 if (ret == 0 && rdev->supply)
2142 regulator_disable(rdev->supply);
2146 EXPORT_SYMBOL_GPL(regulator_disable);
2148 /* locks held by regulator_force_disable() */
2149 static int _regulator_force_disable(struct regulator_dev *rdev)
2153 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2154 REGULATOR_EVENT_PRE_DISABLE, NULL);
2155 if (ret & NOTIFY_STOP_MASK)
2158 ret = _regulator_do_disable(rdev);
2160 rdev_err(rdev, "failed to force disable\n");
2161 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2162 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2166 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2167 REGULATOR_EVENT_DISABLE, NULL);
2173 * regulator_force_disable - force disable regulator output
2174 * @regulator: regulator source
2176 * Forcibly disable the regulator output voltage or current.
2177 * NOTE: this *will* disable the regulator output even if other consumer
2178 * devices have it enabled. This should be used for situations when device
2179 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2181 int regulator_force_disable(struct regulator *regulator)
2183 struct regulator_dev *rdev = regulator->rdev;
2186 mutex_lock(&rdev->mutex);
2187 regulator->uA_load = 0;
2188 ret = _regulator_force_disable(regulator->rdev);
2189 mutex_unlock(&rdev->mutex);
2192 while (rdev->open_count--)
2193 regulator_disable(rdev->supply);
2197 EXPORT_SYMBOL_GPL(regulator_force_disable);
2199 static void regulator_disable_work(struct work_struct *work)
2201 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2205 mutex_lock(&rdev->mutex);
2207 BUG_ON(!rdev->deferred_disables);
2209 count = rdev->deferred_disables;
2210 rdev->deferred_disables = 0;
2212 for (i = 0; i < count; i++) {
2213 ret = _regulator_disable(rdev);
2215 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2218 mutex_unlock(&rdev->mutex);
2221 for (i = 0; i < count; i++) {
2222 ret = regulator_disable(rdev->supply);
2225 "Supply disable failed: %d\n", ret);
2232 * regulator_disable_deferred - disable regulator output with delay
2233 * @regulator: regulator source
2234 * @ms: miliseconds until the regulator is disabled
2236 * Execute regulator_disable() on the regulator after a delay. This
2237 * is intended for use with devices that require some time to quiesce.
2239 * NOTE: this will only disable the regulator output if no other consumer
2240 * devices have it enabled, the regulator device supports disabling and
2241 * machine constraints permit this operation.
2243 int regulator_disable_deferred(struct regulator *regulator, int ms)
2245 struct regulator_dev *rdev = regulator->rdev;
2248 if (regulator->always_on)
2252 return regulator_disable(regulator);
2254 mutex_lock(&rdev->mutex);
2255 rdev->deferred_disables++;
2256 mutex_unlock(&rdev->mutex);
2258 ret = queue_delayed_work(system_power_efficient_wq,
2259 &rdev->disable_work,
2260 msecs_to_jiffies(ms));
2266 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2268 static int _regulator_is_enabled(struct regulator_dev *rdev)
2270 /* A GPIO control always takes precedence */
2272 return rdev->ena_gpio_state;
2274 /* If we don't know then assume that the regulator is always on */
2275 if (!rdev->desc->ops->is_enabled)
2278 return rdev->desc->ops->is_enabled(rdev);
2282 * regulator_is_enabled - is the regulator output enabled
2283 * @regulator: regulator source
2285 * Returns positive if the regulator driver backing the source/client
2286 * has requested that the device be enabled, zero if it hasn't, else a
2287 * negative errno code.
2289 * Note that the device backing this regulator handle can have multiple
2290 * users, so it might be enabled even if regulator_enable() was never
2291 * called for this particular source.
2293 int regulator_is_enabled(struct regulator *regulator)
2297 if (regulator->always_on)
2300 mutex_lock(®ulator->rdev->mutex);
2301 ret = _regulator_is_enabled(regulator->rdev);
2302 mutex_unlock(®ulator->rdev->mutex);
2306 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2309 * regulator_can_change_voltage - check if regulator can change voltage
2310 * @regulator: regulator source
2312 * Returns positive if the regulator driver backing the source/client
2313 * can change its voltage, false otherwise. Useful for detecting fixed
2314 * or dummy regulators and disabling voltage change logic in the client
2317 int regulator_can_change_voltage(struct regulator *regulator)
2319 struct regulator_dev *rdev = regulator->rdev;
2321 if (rdev->constraints &&
2322 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2323 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2326 if (rdev->desc->continuous_voltage_range &&
2327 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2328 rdev->constraints->min_uV != rdev->constraints->max_uV)
2334 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2337 * regulator_count_voltages - count regulator_list_voltage() selectors
2338 * @regulator: regulator source
2340 * Returns number of selectors, or negative errno. Selectors are
2341 * numbered starting at zero, and typically correspond to bitfields
2342 * in hardware registers.
2344 int regulator_count_voltages(struct regulator *regulator)
2346 struct regulator_dev *rdev = regulator->rdev;
2348 if (rdev->desc->n_voltages)
2349 return rdev->desc->n_voltages;
2354 return regulator_count_voltages(rdev->supply);
2356 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2359 * regulator_list_voltage - enumerate supported voltages
2360 * @regulator: regulator source
2361 * @selector: identify voltage to list
2362 * Context: can sleep
2364 * Returns a voltage that can be passed to @regulator_set_voltage(),
2365 * zero if this selector code can't be used on this system, or a
2368 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2370 struct regulator_dev *rdev = regulator->rdev;
2371 const struct regulator_ops *ops = rdev->desc->ops;
2374 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2375 return rdev->desc->fixed_uV;
2377 if (ops->list_voltage) {
2378 if (selector >= rdev->desc->n_voltages)
2380 mutex_lock(&rdev->mutex);
2381 ret = ops->list_voltage(rdev, selector);
2382 mutex_unlock(&rdev->mutex);
2383 } else if (rdev->supply) {
2384 ret = regulator_list_voltage(rdev->supply, selector);
2390 if (ret < rdev->constraints->min_uV)
2392 else if (ret > rdev->constraints->max_uV)
2398 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2401 * regulator_get_regmap - get the regulator's register map
2402 * @regulator: regulator source
2404 * Returns the register map for the given regulator, or an ERR_PTR value
2405 * if the regulator doesn't use regmap.
2407 struct regmap *regulator_get_regmap(struct regulator *regulator)
2409 struct regmap *map = regulator->rdev->regmap;
2411 return map ? map : ERR_PTR(-EOPNOTSUPP);
2415 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2416 * @regulator: regulator source
2417 * @vsel_reg: voltage selector register, output parameter
2418 * @vsel_mask: mask for voltage selector bitfield, output parameter
2420 * Returns the hardware register offset and bitmask used for setting the
2421 * regulator voltage. This might be useful when configuring voltage-scaling
2422 * hardware or firmware that can make I2C requests behind the kernel's back,
2425 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2426 * and 0 is returned, otherwise a negative errno is returned.
2428 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2430 unsigned *vsel_mask)
2432 struct regulator_dev *rdev = regulator->rdev;
2433 const struct regulator_ops *ops = rdev->desc->ops;
2435 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2438 *vsel_reg = rdev->desc->vsel_reg;
2439 *vsel_mask = rdev->desc->vsel_mask;
2443 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2446 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2447 * @regulator: regulator source
2448 * @selector: identify voltage to list
2450 * Converts the selector to a hardware-specific voltage selector that can be
2451 * directly written to the regulator registers. The address of the voltage
2452 * register can be determined by calling @regulator_get_hardware_vsel_register.
2454 * On error a negative errno is returned.
2456 int regulator_list_hardware_vsel(struct regulator *regulator,
2459 struct regulator_dev *rdev = regulator->rdev;
2460 const struct regulator_ops *ops = rdev->desc->ops;
2462 if (selector >= rdev->desc->n_voltages)
2464 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2469 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2472 * regulator_get_linear_step - return the voltage step size between VSEL values
2473 * @regulator: regulator source
2475 * Returns the voltage step size between VSEL values for linear
2476 * regulators, or return 0 if the regulator isn't a linear regulator.
2478 unsigned int regulator_get_linear_step(struct regulator *regulator)
2480 struct regulator_dev *rdev = regulator->rdev;
2482 return rdev->desc->uV_step;
2484 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2487 * regulator_is_supported_voltage - check if a voltage range can be supported
2489 * @regulator: Regulator to check.
2490 * @min_uV: Minimum required voltage in uV.
2491 * @max_uV: Maximum required voltage in uV.
2493 * Returns a boolean or a negative error code.
2495 int regulator_is_supported_voltage(struct regulator *regulator,
2496 int min_uV, int max_uV)
2498 struct regulator_dev *rdev = regulator->rdev;
2499 int i, voltages, ret;
2501 /* If we can't change voltage check the current voltage */
2502 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2503 ret = regulator_get_voltage(regulator);
2505 return min_uV <= ret && ret <= max_uV;
2510 /* Any voltage within constrains range is fine? */
2511 if (rdev->desc->continuous_voltage_range)
2512 return min_uV >= rdev->constraints->min_uV &&
2513 max_uV <= rdev->constraints->max_uV;
2515 ret = regulator_count_voltages(regulator);
2520 for (i = 0; i < voltages; i++) {
2521 ret = regulator_list_voltage(regulator, i);
2523 if (ret >= min_uV && ret <= max_uV)
2529 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2531 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2532 int min_uV, int max_uV,
2535 struct pre_voltage_change_data data;
2538 data.old_uV = _regulator_get_voltage(rdev);
2539 data.min_uV = min_uV;
2540 data.max_uV = max_uV;
2541 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2543 if (ret & NOTIFY_STOP_MASK)
2546 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2550 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2551 (void *)data.old_uV);
2556 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2557 int uV, unsigned selector)
2559 struct pre_voltage_change_data data;
2562 data.old_uV = _regulator_get_voltage(rdev);
2565 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2567 if (ret & NOTIFY_STOP_MASK)
2570 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2574 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2575 (void *)data.old_uV);
2580 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2581 int min_uV, int max_uV)
2586 unsigned int selector;
2587 int old_selector = -1;
2589 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2591 min_uV += rdev->constraints->uV_offset;
2592 max_uV += rdev->constraints->uV_offset;
2595 * If we can't obtain the old selector there is not enough
2596 * info to call set_voltage_time_sel().
2598 if (_regulator_is_enabled(rdev) &&
2599 rdev->desc->ops->set_voltage_time_sel &&
2600 rdev->desc->ops->get_voltage_sel) {
2601 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2602 if (old_selector < 0)
2603 return old_selector;
2606 if (rdev->desc->ops->set_voltage) {
2607 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2611 if (rdev->desc->ops->list_voltage)
2612 best_val = rdev->desc->ops->list_voltage(rdev,
2615 best_val = _regulator_get_voltage(rdev);
2618 } else if (rdev->desc->ops->set_voltage_sel) {
2619 if (rdev->desc->ops->map_voltage) {
2620 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2623 if (rdev->desc->ops->list_voltage ==
2624 regulator_list_voltage_linear)
2625 ret = regulator_map_voltage_linear(rdev,
2627 else if (rdev->desc->ops->list_voltage ==
2628 regulator_list_voltage_linear_range)
2629 ret = regulator_map_voltage_linear_range(rdev,
2632 ret = regulator_map_voltage_iterate(rdev,
2637 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2638 if (min_uV <= best_val && max_uV >= best_val) {
2640 if (old_selector == selector)
2643 ret = _regulator_call_set_voltage_sel(
2644 rdev, best_val, selector);
2653 /* Call set_voltage_time_sel if successfully obtained old_selector */
2654 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2655 && old_selector != selector) {
2657 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2658 old_selector, selector);
2660 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2665 /* Insert any necessary delays */
2666 if (delay >= 1000) {
2667 mdelay(delay / 1000);
2668 udelay(delay % 1000);
2674 if (ret == 0 && best_val >= 0) {
2675 unsigned long data = best_val;
2677 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2681 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2687 * regulator_set_voltage - set regulator output voltage
2688 * @regulator: regulator source
2689 * @min_uV: Minimum required voltage in uV
2690 * @max_uV: Maximum acceptable voltage in uV
2692 * Sets a voltage regulator to the desired output voltage. This can be set
2693 * during any regulator state. IOW, regulator can be disabled or enabled.
2695 * If the regulator is enabled then the voltage will change to the new value
2696 * immediately otherwise if the regulator is disabled the regulator will
2697 * output at the new voltage when enabled.
2699 * NOTE: If the regulator is shared between several devices then the lowest
2700 * request voltage that meets the system constraints will be used.
2701 * Regulator system constraints must be set for this regulator before
2702 * calling this function otherwise this call will fail.
2704 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2706 struct regulator_dev *rdev = regulator->rdev;
2708 int old_min_uV, old_max_uV;
2711 mutex_lock(&rdev->mutex);
2713 /* If we're setting the same range as last time the change
2714 * should be a noop (some cpufreq implementations use the same
2715 * voltage for multiple frequencies, for example).
2717 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2720 /* If we're trying to set a range that overlaps the current voltage,
2721 * return succesfully even though the regulator does not support
2722 * changing the voltage.
2724 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2725 current_uV = _regulator_get_voltage(rdev);
2726 if (min_uV <= current_uV && current_uV <= max_uV) {
2727 regulator->min_uV = min_uV;
2728 regulator->max_uV = max_uV;
2734 if (!rdev->desc->ops->set_voltage &&
2735 !rdev->desc->ops->set_voltage_sel) {
2740 /* constraints check */
2741 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2745 /* restore original values in case of error */
2746 old_min_uV = regulator->min_uV;
2747 old_max_uV = regulator->max_uV;
2748 regulator->min_uV = min_uV;
2749 regulator->max_uV = max_uV;
2751 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2755 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2760 mutex_unlock(&rdev->mutex);
2763 regulator->min_uV = old_min_uV;
2764 regulator->max_uV = old_max_uV;
2765 mutex_unlock(&rdev->mutex);
2768 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2771 * regulator_set_voltage_time - get raise/fall time
2772 * @regulator: regulator source
2773 * @old_uV: starting voltage in microvolts
2774 * @new_uV: target voltage in microvolts
2776 * Provided with the starting and ending voltage, this function attempts to
2777 * calculate the time in microseconds required to rise or fall to this new
2780 int regulator_set_voltage_time(struct regulator *regulator,
2781 int old_uV, int new_uV)
2783 struct regulator_dev *rdev = regulator->rdev;
2784 const struct regulator_ops *ops = rdev->desc->ops;
2790 /* Currently requires operations to do this */
2791 if (!ops->list_voltage || !ops->set_voltage_time_sel
2792 || !rdev->desc->n_voltages)
2795 for (i = 0; i < rdev->desc->n_voltages; i++) {
2796 /* We only look for exact voltage matches here */
2797 voltage = regulator_list_voltage(regulator, i);
2802 if (voltage == old_uV)
2804 if (voltage == new_uV)
2808 if (old_sel < 0 || new_sel < 0)
2811 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2813 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2816 * regulator_set_voltage_time_sel - get raise/fall time
2817 * @rdev: regulator source device
2818 * @old_selector: selector for starting voltage
2819 * @new_selector: selector for target voltage
2821 * Provided with the starting and target voltage selectors, this function
2822 * returns time in microseconds required to rise or fall to this new voltage
2824 * Drivers providing ramp_delay in regulation_constraints can use this as their
2825 * set_voltage_time_sel() operation.
2827 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2828 unsigned int old_selector,
2829 unsigned int new_selector)
2831 unsigned int ramp_delay = 0;
2832 int old_volt, new_volt;
2834 if (rdev->constraints->ramp_delay)
2835 ramp_delay = rdev->constraints->ramp_delay;
2836 else if (rdev->desc->ramp_delay)
2837 ramp_delay = rdev->desc->ramp_delay;
2839 if (ramp_delay == 0) {
2840 rdev_warn(rdev, "ramp_delay not set\n");
2845 if (!rdev->desc->ops->list_voltage)
2848 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2849 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2851 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2853 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2856 * regulator_sync_voltage - re-apply last regulator output voltage
2857 * @regulator: regulator source
2859 * Re-apply the last configured voltage. This is intended to be used
2860 * where some external control source the consumer is cooperating with
2861 * has caused the configured voltage to change.
2863 int regulator_sync_voltage(struct regulator *regulator)
2865 struct regulator_dev *rdev = regulator->rdev;
2866 int ret, min_uV, max_uV;
2868 mutex_lock(&rdev->mutex);
2870 if (!rdev->desc->ops->set_voltage &&
2871 !rdev->desc->ops->set_voltage_sel) {
2876 /* This is only going to work if we've had a voltage configured. */
2877 if (!regulator->min_uV && !regulator->max_uV) {
2882 min_uV = regulator->min_uV;
2883 max_uV = regulator->max_uV;
2885 /* This should be a paranoia check... */
2886 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2890 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2894 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2897 mutex_unlock(&rdev->mutex);
2900 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2902 static int _regulator_get_voltage(struct regulator_dev *rdev)
2906 if (rdev->desc->ops->get_voltage_sel) {
2907 sel = rdev->desc->ops->get_voltage_sel(rdev);
2910 ret = rdev->desc->ops->list_voltage(rdev, sel);
2911 } else if (rdev->desc->ops->get_voltage) {
2912 ret = rdev->desc->ops->get_voltage(rdev);
2913 } else if (rdev->desc->ops->list_voltage) {
2914 ret = rdev->desc->ops->list_voltage(rdev, 0);
2915 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2916 ret = rdev->desc->fixed_uV;
2917 } else if (rdev->supply) {
2918 ret = regulator_get_voltage(rdev->supply);
2925 return ret - rdev->constraints->uV_offset;
2929 * regulator_get_voltage - get regulator output voltage
2930 * @regulator: regulator source
2932 * This returns the current regulator voltage in uV.
2934 * NOTE: If the regulator is disabled it will return the voltage value. This
2935 * function should not be used to determine regulator state.
2937 int regulator_get_voltage(struct regulator *regulator)
2941 mutex_lock(®ulator->rdev->mutex);
2943 ret = _regulator_get_voltage(regulator->rdev);
2945 mutex_unlock(®ulator->rdev->mutex);
2949 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2952 * regulator_set_current_limit - set regulator output current limit
2953 * @regulator: regulator source
2954 * @min_uA: Minimum supported current in uA
2955 * @max_uA: Maximum supported current in uA
2957 * Sets current sink to the desired output current. This can be set during
2958 * any regulator state. IOW, regulator can be disabled or enabled.
2960 * If the regulator is enabled then the current will change to the new value
2961 * immediately otherwise if the regulator is disabled the regulator will
2962 * output at the new current when enabled.
2964 * NOTE: Regulator system constraints must be set for this regulator before
2965 * calling this function otherwise this call will fail.
2967 int regulator_set_current_limit(struct regulator *regulator,
2968 int min_uA, int max_uA)
2970 struct regulator_dev *rdev = regulator->rdev;
2973 mutex_lock(&rdev->mutex);
2976 if (!rdev->desc->ops->set_current_limit) {
2981 /* constraints check */
2982 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2986 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2988 mutex_unlock(&rdev->mutex);
2991 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2993 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2997 mutex_lock(&rdev->mutex);
3000 if (!rdev->desc->ops->get_current_limit) {
3005 ret = rdev->desc->ops->get_current_limit(rdev);
3007 mutex_unlock(&rdev->mutex);
3012 * regulator_get_current_limit - get regulator output current
3013 * @regulator: regulator source
3015 * This returns the current supplied by the specified current sink in uA.
3017 * NOTE: If the regulator is disabled it will return the current value. This
3018 * function should not be used to determine regulator state.
3020 int regulator_get_current_limit(struct regulator *regulator)
3022 return _regulator_get_current_limit(regulator->rdev);
3024 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3027 * regulator_set_mode - set regulator operating mode
3028 * @regulator: regulator source
3029 * @mode: operating mode - one of the REGULATOR_MODE constants
3031 * Set regulator operating mode to increase regulator efficiency or improve
3032 * regulation performance.
3034 * NOTE: Regulator system constraints must be set for this regulator before
3035 * calling this function otherwise this call will fail.
3037 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3039 struct regulator_dev *rdev = regulator->rdev;
3041 int regulator_curr_mode;
3043 mutex_lock(&rdev->mutex);
3046 if (!rdev->desc->ops->set_mode) {
3051 /* return if the same mode is requested */
3052 if (rdev->desc->ops->get_mode) {
3053 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3054 if (regulator_curr_mode == mode) {
3060 /* constraints check */
3061 ret = regulator_mode_constrain(rdev, &mode);
3065 ret = rdev->desc->ops->set_mode(rdev, mode);
3067 mutex_unlock(&rdev->mutex);
3070 EXPORT_SYMBOL_GPL(regulator_set_mode);
3072 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3076 mutex_lock(&rdev->mutex);
3079 if (!rdev->desc->ops->get_mode) {
3084 ret = rdev->desc->ops->get_mode(rdev);
3086 mutex_unlock(&rdev->mutex);
3091 * regulator_get_mode - get regulator operating mode
3092 * @regulator: regulator source
3094 * Get the current regulator operating mode.
3096 unsigned int regulator_get_mode(struct regulator *regulator)
3098 return _regulator_get_mode(regulator->rdev);
3100 EXPORT_SYMBOL_GPL(regulator_get_mode);
3103 * regulator_set_load - set regulator load
3104 * @regulator: regulator source
3105 * @uA_load: load current
3107 * Notifies the regulator core of a new device load. This is then used by
3108 * DRMS (if enabled by constraints) to set the most efficient regulator
3109 * operating mode for the new regulator loading.
3111 * Consumer devices notify their supply regulator of the maximum power
3112 * they will require (can be taken from device datasheet in the power
3113 * consumption tables) when they change operational status and hence power
3114 * state. Examples of operational state changes that can affect power
3115 * consumption are :-
3117 * o Device is opened / closed.
3118 * o Device I/O is about to begin or has just finished.
3119 * o Device is idling in between work.
3121 * This information is also exported via sysfs to userspace.
3123 * DRMS will sum the total requested load on the regulator and change
3124 * to the most efficient operating mode if platform constraints allow.
3126 * On error a negative errno is returned.
3128 int regulator_set_load(struct regulator *regulator, int uA_load)
3130 struct regulator_dev *rdev = regulator->rdev;
3133 mutex_lock(&rdev->mutex);
3134 regulator->uA_load = uA_load;
3135 ret = drms_uA_update(rdev);
3136 mutex_unlock(&rdev->mutex);
3140 EXPORT_SYMBOL_GPL(regulator_set_load);
3143 * regulator_allow_bypass - allow the regulator to go into bypass mode
3145 * @regulator: Regulator to configure
3146 * @enable: enable or disable bypass mode
3148 * Allow the regulator to go into bypass mode if all other consumers
3149 * for the regulator also enable bypass mode and the machine
3150 * constraints allow this. Bypass mode means that the regulator is
3151 * simply passing the input directly to the output with no regulation.
3153 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3155 struct regulator_dev *rdev = regulator->rdev;
3158 if (!rdev->desc->ops->set_bypass)
3161 if (rdev->constraints &&
3162 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3165 mutex_lock(&rdev->mutex);
3167 if (enable && !regulator->bypass) {
3168 rdev->bypass_count++;
3170 if (rdev->bypass_count == rdev->open_count) {
3171 ret = rdev->desc->ops->set_bypass(rdev, enable);
3173 rdev->bypass_count--;
3176 } else if (!enable && regulator->bypass) {
3177 rdev->bypass_count--;
3179 if (rdev->bypass_count != rdev->open_count) {
3180 ret = rdev->desc->ops->set_bypass(rdev, enable);
3182 rdev->bypass_count++;
3187 regulator->bypass = enable;
3189 mutex_unlock(&rdev->mutex);
3193 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3196 * regulator_register_notifier - register regulator event notifier
3197 * @regulator: regulator source
3198 * @nb: notifier block
3200 * Register notifier block to receive regulator events.
3202 int regulator_register_notifier(struct regulator *regulator,
3203 struct notifier_block *nb)
3205 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3208 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3211 * regulator_unregister_notifier - unregister regulator event notifier
3212 * @regulator: regulator source
3213 * @nb: notifier block
3215 * Unregister regulator event notifier block.
3217 int regulator_unregister_notifier(struct regulator *regulator,
3218 struct notifier_block *nb)
3220 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3223 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3225 /* notify regulator consumers and downstream regulator consumers.
3226 * Note mutex must be held by caller.
3228 static int _notifier_call_chain(struct regulator_dev *rdev,
3229 unsigned long event, void *data)
3231 /* call rdev chain first */
3232 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3236 * regulator_bulk_get - get multiple regulator consumers
3238 * @dev: Device to supply
3239 * @num_consumers: Number of consumers to register
3240 * @consumers: Configuration of consumers; clients are stored here.
3242 * @return 0 on success, an errno on failure.
3244 * This helper function allows drivers to get several regulator
3245 * consumers in one operation. If any of the regulators cannot be
3246 * acquired then any regulators that were allocated will be freed
3247 * before returning to the caller.
3249 int regulator_bulk_get(struct device *dev, int num_consumers,
3250 struct regulator_bulk_data *consumers)
3255 for (i = 0; i < num_consumers; i++)
3256 consumers[i].consumer = NULL;
3258 for (i = 0; i < num_consumers; i++) {
3259 consumers[i].consumer = regulator_get(dev,
3260 consumers[i].supply);
3261 if (IS_ERR(consumers[i].consumer)) {
3262 ret = PTR_ERR(consumers[i].consumer);
3263 dev_err(dev, "Failed to get supply '%s': %d\n",
3264 consumers[i].supply, ret);
3265 consumers[i].consumer = NULL;
3274 regulator_put(consumers[i].consumer);
3278 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3280 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3282 struct regulator_bulk_data *bulk = data;
3284 bulk->ret = regulator_enable(bulk->consumer);
3288 * regulator_bulk_enable - enable multiple regulator consumers
3290 * @num_consumers: Number of consumers
3291 * @consumers: Consumer data; clients are stored here.
3292 * @return 0 on success, an errno on failure
3294 * This convenience API allows consumers to enable multiple regulator
3295 * clients in a single API call. If any consumers cannot be enabled
3296 * then any others that were enabled will be disabled again prior to
3299 int regulator_bulk_enable(int num_consumers,
3300 struct regulator_bulk_data *consumers)
3302 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3306 for (i = 0; i < num_consumers; i++) {
3307 if (consumers[i].consumer->always_on)
3308 consumers[i].ret = 0;
3310 async_schedule_domain(regulator_bulk_enable_async,
3311 &consumers[i], &async_domain);
3314 async_synchronize_full_domain(&async_domain);
3316 /* If any consumer failed we need to unwind any that succeeded */
3317 for (i = 0; i < num_consumers; i++) {
3318 if (consumers[i].ret != 0) {
3319 ret = consumers[i].ret;
3327 for (i = 0; i < num_consumers; i++) {
3328 if (consumers[i].ret < 0)
3329 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3332 regulator_disable(consumers[i].consumer);
3337 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3340 * regulator_bulk_disable - disable multiple regulator consumers
3342 * @num_consumers: Number of consumers
3343 * @consumers: Consumer data; clients are stored here.
3344 * @return 0 on success, an errno on failure
3346 * This convenience API allows consumers to disable multiple regulator
3347 * clients in a single API call. If any consumers cannot be disabled
3348 * then any others that were disabled will be enabled again prior to
3351 int regulator_bulk_disable(int num_consumers,
3352 struct regulator_bulk_data *consumers)
3357 for (i = num_consumers - 1; i >= 0; --i) {
3358 ret = regulator_disable(consumers[i].consumer);
3366 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3367 for (++i; i < num_consumers; ++i) {
3368 r = regulator_enable(consumers[i].consumer);
3370 pr_err("Failed to reename %s: %d\n",
3371 consumers[i].supply, r);
3376 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3379 * regulator_bulk_force_disable - force disable multiple regulator consumers
3381 * @num_consumers: Number of consumers
3382 * @consumers: Consumer data; clients are stored here.
3383 * @return 0 on success, an errno on failure
3385 * This convenience API allows consumers to forcibly disable multiple regulator
3386 * clients in a single API call.
3387 * NOTE: This should be used for situations when device damage will
3388 * likely occur if the regulators are not disabled (e.g. over temp).
3389 * Although regulator_force_disable function call for some consumers can
3390 * return error numbers, the function is called for all consumers.
3392 int regulator_bulk_force_disable(int num_consumers,
3393 struct regulator_bulk_data *consumers)
3398 for (i = 0; i < num_consumers; i++)
3400 regulator_force_disable(consumers[i].consumer);
3402 for (i = 0; i < num_consumers; i++) {
3403 if (consumers[i].ret != 0) {
3404 ret = consumers[i].ret;
3413 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3416 * regulator_bulk_free - free multiple regulator consumers
3418 * @num_consumers: Number of consumers
3419 * @consumers: Consumer data; clients are stored here.
3421 * This convenience API allows consumers to free multiple regulator
3422 * clients in a single API call.
3424 void regulator_bulk_free(int num_consumers,
3425 struct regulator_bulk_data *consumers)
3429 for (i = 0; i < num_consumers; i++) {
3430 regulator_put(consumers[i].consumer);
3431 consumers[i].consumer = NULL;
3434 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3437 * regulator_notifier_call_chain - call regulator event notifier
3438 * @rdev: regulator source
3439 * @event: notifier block
3440 * @data: callback-specific data.
3442 * Called by regulator drivers to notify clients a regulator event has
3443 * occurred. We also notify regulator clients downstream.
3444 * Note lock must be held by caller.
3446 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3447 unsigned long event, void *data)
3449 _notifier_call_chain(rdev, event, data);
3453 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3456 * regulator_mode_to_status - convert a regulator mode into a status
3458 * @mode: Mode to convert
3460 * Convert a regulator mode into a status.
3462 int regulator_mode_to_status(unsigned int mode)
3465 case REGULATOR_MODE_FAST:
3466 return REGULATOR_STATUS_FAST;
3467 case REGULATOR_MODE_NORMAL:
3468 return REGULATOR_STATUS_NORMAL;
3469 case REGULATOR_MODE_IDLE:
3470 return REGULATOR_STATUS_IDLE;
3471 case REGULATOR_MODE_STANDBY:
3472 return REGULATOR_STATUS_STANDBY;
3474 return REGULATOR_STATUS_UNDEFINED;
3477 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3479 static struct attribute *regulator_dev_attrs[] = {
3480 &dev_attr_name.attr,
3481 &dev_attr_num_users.attr,
3482 &dev_attr_type.attr,
3483 &dev_attr_microvolts.attr,
3484 &dev_attr_microamps.attr,
3485 &dev_attr_opmode.attr,
3486 &dev_attr_state.attr,
3487 &dev_attr_status.attr,
3488 &dev_attr_bypass.attr,
3489 &dev_attr_requested_microamps.attr,
3490 &dev_attr_min_microvolts.attr,
3491 &dev_attr_max_microvolts.attr,
3492 &dev_attr_min_microamps.attr,
3493 &dev_attr_max_microamps.attr,
3494 &dev_attr_suspend_standby_state.attr,
3495 &dev_attr_suspend_mem_state.attr,
3496 &dev_attr_suspend_disk_state.attr,
3497 &dev_attr_suspend_standby_microvolts.attr,
3498 &dev_attr_suspend_mem_microvolts.attr,
3499 &dev_attr_suspend_disk_microvolts.attr,
3500 &dev_attr_suspend_standby_mode.attr,
3501 &dev_attr_suspend_mem_mode.attr,
3502 &dev_attr_suspend_disk_mode.attr,
3507 * To avoid cluttering sysfs (and memory) with useless state, only
3508 * create attributes that can be meaningfully displayed.
3510 static umode_t regulator_attr_is_visible(struct kobject *kobj,
3511 struct attribute *attr, int idx)
3513 struct device *dev = kobj_to_dev(kobj);
3514 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
3515 const struct regulator_ops *ops = rdev->desc->ops;
3516 umode_t mode = attr->mode;
3518 /* these three are always present */
3519 if (attr == &dev_attr_name.attr ||
3520 attr == &dev_attr_num_users.attr ||
3521 attr == &dev_attr_type.attr)
3524 /* some attributes need specific methods to be displayed */
3525 if (attr == &dev_attr_microvolts.attr) {
3526 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3527 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3528 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3529 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3534 if (attr == &dev_attr_microamps.attr)
3535 return ops->get_current_limit ? mode : 0;
3537 if (attr == &dev_attr_opmode.attr)
3538 return ops->get_mode ? mode : 0;
3540 if (attr == &dev_attr_state.attr)
3541 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3543 if (attr == &dev_attr_status.attr)
3544 return ops->get_status ? mode : 0;
3546 if (attr == &dev_attr_bypass.attr)
3547 return ops->get_bypass ? mode : 0;
3549 /* some attributes are type-specific */
3550 if (attr == &dev_attr_requested_microamps.attr)
3551 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
3553 /* constraints need specific supporting methods */
3554 if (attr == &dev_attr_min_microvolts.attr ||
3555 attr == &dev_attr_max_microvolts.attr)
3556 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
3558 if (attr == &dev_attr_min_microamps.attr ||
3559 attr == &dev_attr_max_microamps.attr)
3560 return ops->set_current_limit ? mode : 0;
3562 if (attr == &dev_attr_suspend_standby_state.attr ||
3563 attr == &dev_attr_suspend_mem_state.attr ||
3564 attr == &dev_attr_suspend_disk_state.attr)
3567 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3568 attr == &dev_attr_suspend_mem_microvolts.attr ||
3569 attr == &dev_attr_suspend_disk_microvolts.attr)
3570 return ops->set_suspend_voltage ? mode : 0;
3572 if (attr == &dev_attr_suspend_standby_mode.attr ||
3573 attr == &dev_attr_suspend_mem_mode.attr ||
3574 attr == &dev_attr_suspend_disk_mode.attr)
3575 return ops->set_suspend_mode ? mode : 0;
3580 static const struct attribute_group regulator_dev_group = {
3581 .attrs = regulator_dev_attrs,
3582 .is_visible = regulator_attr_is_visible,
3585 static const struct attribute_group *regulator_dev_groups[] = {
3586 ®ulator_dev_group,
3590 static void regulator_dev_release(struct device *dev)
3592 struct regulator_dev *rdev = dev_get_drvdata(dev);
3596 static struct class regulator_class = {
3597 .name = "regulator",
3598 .dev_release = regulator_dev_release,
3599 .dev_groups = regulator_dev_groups,
3602 static void rdev_init_debugfs(struct regulator_dev *rdev)
3604 struct device *parent = rdev->dev.parent;
3605 const char *rname = rdev_get_name(rdev);
3606 char name[NAME_MAX];
3608 /* Avoid duplicate debugfs directory names */
3609 if (parent && rname == rdev->desc->name) {
3610 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3615 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
3616 if (!rdev->debugfs) {
3617 rdev_warn(rdev, "Failed to create debugfs directory\n");
3621 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3623 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3625 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3626 &rdev->bypass_count);
3630 * regulator_register - register regulator
3631 * @regulator_desc: regulator to register
3632 * @cfg: runtime configuration for regulator
3634 * Called by regulator drivers to register a regulator.
3635 * Returns a valid pointer to struct regulator_dev on success
3636 * or an ERR_PTR() on error.
3638 struct regulator_dev *
3639 regulator_register(const struct regulator_desc *regulator_desc,
3640 const struct regulator_config *cfg)
3642 const struct regulation_constraints *constraints = NULL;
3643 const struct regulator_init_data *init_data;
3644 struct regulator_config *config = NULL;
3645 static atomic_t regulator_no = ATOMIC_INIT(-1);
3646 struct regulator_dev *rdev;
3650 if (regulator_desc == NULL || cfg == NULL)
3651 return ERR_PTR(-EINVAL);
3656 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3657 return ERR_PTR(-EINVAL);
3659 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3660 regulator_desc->type != REGULATOR_CURRENT)
3661 return ERR_PTR(-EINVAL);
3663 /* Only one of each should be implemented */
3664 WARN_ON(regulator_desc->ops->get_voltage &&
3665 regulator_desc->ops->get_voltage_sel);
3666 WARN_ON(regulator_desc->ops->set_voltage &&
3667 regulator_desc->ops->set_voltage_sel);
3669 /* If we're using selectors we must implement list_voltage. */
3670 if (regulator_desc->ops->get_voltage_sel &&
3671 !regulator_desc->ops->list_voltage) {
3672 return ERR_PTR(-EINVAL);
3674 if (regulator_desc->ops->set_voltage_sel &&
3675 !regulator_desc->ops->list_voltage) {
3676 return ERR_PTR(-EINVAL);
3679 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3681 return ERR_PTR(-ENOMEM);
3684 * Duplicate the config so the driver could override it after
3685 * parsing init data.
3687 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3688 if (config == NULL) {
3690 return ERR_PTR(-ENOMEM);
3693 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
3694 &rdev->dev.of_node);
3696 init_data = config->init_data;
3697 rdev->dev.of_node = of_node_get(config->of_node);
3700 mutex_lock(®ulator_list_mutex);
3702 mutex_init(&rdev->mutex);
3703 rdev->reg_data = config->driver_data;
3704 rdev->owner = regulator_desc->owner;
3705 rdev->desc = regulator_desc;
3707 rdev->regmap = config->regmap;
3708 else if (dev_get_regmap(dev, NULL))
3709 rdev->regmap = dev_get_regmap(dev, NULL);
3710 else if (dev->parent)
3711 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3712 INIT_LIST_HEAD(&rdev->consumer_list);
3713 INIT_LIST_HEAD(&rdev->list);
3714 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3715 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3717 /* preform any regulator specific init */
3718 if (init_data && init_data->regulator_init) {
3719 ret = init_data->regulator_init(rdev->reg_data);
3724 /* register with sysfs */
3725 rdev->dev.class = ®ulator_class;
3726 rdev->dev.parent = dev;
3727 dev_set_name(&rdev->dev, "regulator.%lu",
3728 (unsigned long) atomic_inc_return(®ulator_no));
3729 ret = device_register(&rdev->dev);
3731 put_device(&rdev->dev);
3735 dev_set_drvdata(&rdev->dev, rdev);
3737 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3738 gpio_is_valid(config->ena_gpio)) {
3739 ret = regulator_ena_gpio_request(rdev, config);
3741 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3742 config->ena_gpio, ret);
3747 /* set regulator constraints */
3749 constraints = &init_data->constraints;
3751 ret = set_machine_constraints(rdev, constraints);
3755 if (init_data && init_data->supply_regulator)
3756 rdev->supply_name = init_data->supply_regulator;
3757 else if (regulator_desc->supply_name)
3758 rdev->supply_name = regulator_desc->supply_name;
3760 /* add consumers devices */
3762 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3763 ret = set_consumer_device_supply(rdev,
3764 init_data->consumer_supplies[i].dev_name,
3765 init_data->consumer_supplies[i].supply);
3767 dev_err(dev, "Failed to set supply %s\n",
3768 init_data->consumer_supplies[i].supply);
3769 goto unset_supplies;
3774 list_add(&rdev->list, ®ulator_list);
3776 rdev_init_debugfs(rdev);
3778 mutex_unlock(®ulator_list_mutex);
3783 unset_regulator_supplies(rdev);
3786 regulator_ena_gpio_free(rdev);
3787 kfree(rdev->constraints);
3789 device_unregister(&rdev->dev);
3790 /* device core frees rdev */
3791 rdev = ERR_PTR(ret);
3796 rdev = ERR_PTR(ret);
3799 EXPORT_SYMBOL_GPL(regulator_register);
3802 * regulator_unregister - unregister regulator
3803 * @rdev: regulator to unregister
3805 * Called by regulator drivers to unregister a regulator.
3807 void regulator_unregister(struct regulator_dev *rdev)
3813 while (rdev->use_count--)
3814 regulator_disable(rdev->supply);
3815 regulator_put(rdev->supply);
3817 mutex_lock(®ulator_list_mutex);
3818 debugfs_remove_recursive(rdev->debugfs);
3819 flush_work(&rdev->disable_work.work);
3820 WARN_ON(rdev->open_count);
3821 unset_regulator_supplies(rdev);
3822 list_del(&rdev->list);
3823 kfree(rdev->constraints);
3824 regulator_ena_gpio_free(rdev);
3825 of_node_put(rdev->dev.of_node);
3826 device_unregister(&rdev->dev);
3827 mutex_unlock(®ulator_list_mutex);
3829 EXPORT_SYMBOL_GPL(regulator_unregister);
3832 * regulator_suspend_prepare - prepare regulators for system wide suspend
3833 * @state: system suspend state
3835 * Configure each regulator with it's suspend operating parameters for state.
3836 * This will usually be called by machine suspend code prior to supending.
3838 int regulator_suspend_prepare(suspend_state_t state)
3840 struct regulator_dev *rdev;
3843 /* ON is handled by regulator active state */
3844 if (state == PM_SUSPEND_ON)
3847 mutex_lock(®ulator_list_mutex);
3848 list_for_each_entry(rdev, ®ulator_list, list) {
3850 mutex_lock(&rdev->mutex);
3851 ret = suspend_prepare(rdev, state);
3852 mutex_unlock(&rdev->mutex);
3855 rdev_err(rdev, "failed to prepare\n");
3860 mutex_unlock(®ulator_list_mutex);
3863 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3866 * regulator_suspend_finish - resume regulators from system wide suspend
3868 * Turn on regulators that might be turned off by regulator_suspend_prepare
3869 * and that should be turned on according to the regulators properties.
3871 int regulator_suspend_finish(void)
3873 struct regulator_dev *rdev;
3876 mutex_lock(®ulator_list_mutex);
3877 list_for_each_entry(rdev, ®ulator_list, list) {
3878 mutex_lock(&rdev->mutex);
3879 if (rdev->use_count > 0 || rdev->constraints->always_on) {
3880 if (!_regulator_is_enabled(rdev)) {
3881 error = _regulator_do_enable(rdev);
3886 if (!have_full_constraints())
3888 if (!_regulator_is_enabled(rdev))
3891 error = _regulator_do_disable(rdev);
3896 mutex_unlock(&rdev->mutex);
3898 mutex_unlock(®ulator_list_mutex);
3901 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3904 * regulator_has_full_constraints - the system has fully specified constraints
3906 * Calling this function will cause the regulator API to disable all
3907 * regulators which have a zero use count and don't have an always_on
3908 * constraint in a late_initcall.
3910 * The intention is that this will become the default behaviour in a
3911 * future kernel release so users are encouraged to use this facility
3914 void regulator_has_full_constraints(void)
3916 has_full_constraints = 1;
3918 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3921 * rdev_get_drvdata - get rdev regulator driver data
3924 * Get rdev regulator driver private data. This call can be used in the
3925 * regulator driver context.
3927 void *rdev_get_drvdata(struct regulator_dev *rdev)
3929 return rdev->reg_data;
3931 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3934 * regulator_get_drvdata - get regulator driver data
3935 * @regulator: regulator
3937 * Get regulator driver private data. This call can be used in the consumer
3938 * driver context when non API regulator specific functions need to be called.
3940 void *regulator_get_drvdata(struct regulator *regulator)
3942 return regulator->rdev->reg_data;
3944 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3947 * regulator_set_drvdata - set regulator driver data
3948 * @regulator: regulator
3951 void regulator_set_drvdata(struct regulator *regulator, void *data)
3953 regulator->rdev->reg_data = data;
3955 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3958 * regulator_get_id - get regulator ID
3961 int rdev_get_id(struct regulator_dev *rdev)
3963 return rdev->desc->id;
3965 EXPORT_SYMBOL_GPL(rdev_get_id);
3967 struct device *rdev_get_dev(struct regulator_dev *rdev)
3971 EXPORT_SYMBOL_GPL(rdev_get_dev);
3973 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3975 return reg_init_data->driver_data;
3977 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3979 #ifdef CONFIG_DEBUG_FS
3980 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3981 size_t count, loff_t *ppos)
3983 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3984 ssize_t len, ret = 0;
3985 struct regulator_map *map;
3990 list_for_each_entry(map, ®ulator_map_list, list) {
3991 len = snprintf(buf + ret, PAGE_SIZE - ret,
3993 rdev_get_name(map->regulator), map->dev_name,
3997 if (ret > PAGE_SIZE) {
4003 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4011 static const struct file_operations supply_map_fops = {
4012 #ifdef CONFIG_DEBUG_FS
4013 .read = supply_map_read_file,
4014 .llseek = default_llseek,
4018 #ifdef CONFIG_DEBUG_FS
4019 static void regulator_summary_show_subtree(struct seq_file *s,
4020 struct regulator_dev *rdev,
4023 struct list_head *list = s->private;
4024 struct regulator_dev *child;
4025 struct regulation_constraints *c;
4026 struct regulator *consumer;
4031 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4033 30 - level * 3, rdev_get_name(rdev),
4034 rdev->use_count, rdev->open_count, rdev->bypass_count);
4036 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4037 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4039 c = rdev->constraints;
4041 switch (rdev->desc->type) {
4042 case REGULATOR_VOLTAGE:
4043 seq_printf(s, "%5dmV %5dmV ",
4044 c->min_uV / 1000, c->max_uV / 1000);
4046 case REGULATOR_CURRENT:
4047 seq_printf(s, "%5dmA %5dmA ",
4048 c->min_uA / 1000, c->max_uA / 1000);
4055 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4056 if (consumer->dev->class == ®ulator_class)
4059 seq_printf(s, "%*s%-*s ",
4060 (level + 1) * 3 + 1, "",
4061 30 - (level + 1) * 3, dev_name(consumer->dev));
4063 switch (rdev->desc->type) {
4064 case REGULATOR_VOLTAGE:
4065 seq_printf(s, "%37dmV %5dmV",
4066 consumer->min_uV / 1000,
4067 consumer->max_uV / 1000);
4069 case REGULATOR_CURRENT:
4076 list_for_each_entry(child, list, list) {
4077 /* handle only non-root regulators supplied by current rdev */
4078 if (!child->supply || child->supply->rdev != rdev)
4081 regulator_summary_show_subtree(s, child, level + 1);
4085 static int regulator_summary_show(struct seq_file *s, void *data)
4087 struct list_head *list = s->private;
4088 struct regulator_dev *rdev;
4090 seq_puts(s, " regulator use open bypass voltage current min max\n");
4091 seq_puts(s, "-------------------------------------------------------------------------------\n");
4093 mutex_lock(®ulator_list_mutex);
4095 list_for_each_entry(rdev, list, list) {
4099 regulator_summary_show_subtree(s, rdev, 0);
4102 mutex_unlock(®ulator_list_mutex);
4107 static int regulator_summary_open(struct inode *inode, struct file *file)
4109 return single_open(file, regulator_summary_show, inode->i_private);
4113 static const struct file_operations regulator_summary_fops = {
4114 #ifdef CONFIG_DEBUG_FS
4115 .open = regulator_summary_open,
4117 .llseek = seq_lseek,
4118 .release = single_release,
4122 static int __init regulator_init(void)
4126 ret = class_register(®ulator_class);
4128 debugfs_root = debugfs_create_dir("regulator", NULL);
4130 pr_warn("regulator: Failed to create debugfs directory\n");
4132 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4135 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4136 ®ulator_list, ®ulator_summary_fops);
4138 regulator_dummy_init();
4143 /* init early to allow our consumers to complete system booting */
4144 core_initcall(regulator_init);
4146 static int __init regulator_init_complete(void)
4148 struct regulator_dev *rdev;
4149 const struct regulator_ops *ops;
4150 struct regulation_constraints *c;
4154 * Since DT doesn't provide an idiomatic mechanism for
4155 * enabling full constraints and since it's much more natural
4156 * with DT to provide them just assume that a DT enabled
4157 * system has full constraints.
4159 if (of_have_populated_dt())
4160 has_full_constraints = true;
4162 mutex_lock(®ulator_list_mutex);
4164 /* If we have a full configuration then disable any regulators
4165 * we have permission to change the status for and which are
4166 * not in use or always_on. This is effectively the default
4167 * for DT and ACPI as they have full constraints.
4169 list_for_each_entry(rdev, ®ulator_list, list) {
4170 ops = rdev->desc->ops;
4171 c = rdev->constraints;
4173 if (c && c->always_on)
4176 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4179 mutex_lock(&rdev->mutex);
4181 if (rdev->use_count)
4184 /* If we can't read the status assume it's on. */
4185 if (ops->is_enabled)
4186 enabled = ops->is_enabled(rdev);
4193 if (have_full_constraints()) {
4194 /* We log since this may kill the system if it
4196 rdev_info(rdev, "disabling\n");
4197 ret = _regulator_do_disable(rdev);
4199 rdev_err(rdev, "couldn't disable: %d\n", ret);
4201 /* The intention is that in future we will
4202 * assume that full constraints are provided
4203 * so warn even if we aren't going to do
4206 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4210 mutex_unlock(&rdev->mutex);
4213 mutex_unlock(®ulator_list_mutex);
4217 late_initcall_sync(regulator_init_complete);