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_map_list);
55 static LIST_HEAD(regulator_ena_gpio_list);
56 static LIST_HEAD(regulator_supply_alias_list);
57 static bool has_full_constraints;
59 static struct dentry *debugfs_root;
61 static struct class regulator_class;
64 * struct regulator_map
66 * Used to provide symbolic supply names to devices.
68 struct regulator_map {
69 struct list_head list;
70 const char *dev_name; /* The dev_name() for the consumer */
72 struct regulator_dev *regulator;
76 * struct regulator_enable_gpio
78 * Management for shared enable GPIO pin
80 struct regulator_enable_gpio {
81 struct list_head list;
82 struct gpio_desc *gpiod;
83 u32 enable_count; /* a number of enabled shared GPIO */
84 u32 request_count; /* a number of requested shared GPIO */
85 unsigned int ena_gpio_invert:1;
89 * struct regulator_supply_alias
91 * Used to map lookups for a supply onto an alternative device.
93 struct regulator_supply_alias {
94 struct list_head list;
95 struct device *src_dev;
96 const char *src_supply;
97 struct device *alias_dev;
98 const char *alias_supply;
101 static int _regulator_is_enabled(struct regulator_dev *rdev);
102 static int _regulator_disable(struct regulator_dev *rdev);
103 static int _regulator_get_voltage(struct regulator_dev *rdev);
104 static int _regulator_get_current_limit(struct regulator_dev *rdev);
105 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
106 static int _notifier_call_chain(struct regulator_dev *rdev,
107 unsigned long event, void *data);
108 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
109 int min_uV, int max_uV);
110 static struct regulator *create_regulator(struct regulator_dev *rdev,
112 const char *supply_name);
113 static void _regulator_put(struct regulator *regulator);
115 static struct regulator_dev *dev_to_rdev(struct device *dev)
117 return container_of(dev, struct regulator_dev, dev);
120 static const char *rdev_get_name(struct regulator_dev *rdev)
122 if (rdev->constraints && rdev->constraints->name)
123 return rdev->constraints->name;
124 else if (rdev->desc->name)
125 return rdev->desc->name;
130 static bool have_full_constraints(void)
132 return has_full_constraints || of_have_populated_dt();
136 * of_get_regulator - get a regulator device node based on supply name
137 * @dev: Device pointer for the consumer (of regulator) device
138 * @supply: regulator supply name
140 * Extract the regulator device node corresponding to the supply name.
141 * returns the device node corresponding to the regulator if found, else
144 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
146 struct device_node *regnode = NULL;
147 char prop_name[32]; /* 32 is max size of property name */
149 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
151 snprintf(prop_name, 32, "%s-supply", supply);
152 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
155 dev_dbg(dev, "Looking up %s property in node %s failed",
156 prop_name, dev->of_node->full_name);
162 static int _regulator_can_change_status(struct regulator_dev *rdev)
164 if (!rdev->constraints)
167 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
173 /* Platform voltage constraint check */
174 static int regulator_check_voltage(struct regulator_dev *rdev,
175 int *min_uV, int *max_uV)
177 BUG_ON(*min_uV > *max_uV);
179 if (!rdev->constraints) {
180 rdev_err(rdev, "no constraints\n");
183 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
184 rdev_err(rdev, "voltage operation not allowed\n");
188 if (*max_uV > rdev->constraints->max_uV)
189 *max_uV = rdev->constraints->max_uV;
190 if (*min_uV < rdev->constraints->min_uV)
191 *min_uV = rdev->constraints->min_uV;
193 if (*min_uV > *max_uV) {
194 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
202 /* Make sure we select a voltage that suits the needs of all
203 * regulator consumers
205 static int regulator_check_consumers(struct regulator_dev *rdev,
206 int *min_uV, int *max_uV)
208 struct regulator *regulator;
210 list_for_each_entry(regulator, &rdev->consumer_list, list) {
212 * Assume consumers that didn't say anything are OK
213 * with anything in the constraint range.
215 if (!regulator->min_uV && !regulator->max_uV)
218 if (*max_uV > regulator->max_uV)
219 *max_uV = regulator->max_uV;
220 if (*min_uV < regulator->min_uV)
221 *min_uV = regulator->min_uV;
224 if (*min_uV > *max_uV) {
225 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
233 /* current constraint check */
234 static int regulator_check_current_limit(struct regulator_dev *rdev,
235 int *min_uA, int *max_uA)
237 BUG_ON(*min_uA > *max_uA);
239 if (!rdev->constraints) {
240 rdev_err(rdev, "no constraints\n");
243 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
244 rdev_err(rdev, "current operation not allowed\n");
248 if (*max_uA > rdev->constraints->max_uA)
249 *max_uA = rdev->constraints->max_uA;
250 if (*min_uA < rdev->constraints->min_uA)
251 *min_uA = rdev->constraints->min_uA;
253 if (*min_uA > *max_uA) {
254 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
262 /* operating mode constraint check */
263 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
266 case REGULATOR_MODE_FAST:
267 case REGULATOR_MODE_NORMAL:
268 case REGULATOR_MODE_IDLE:
269 case REGULATOR_MODE_STANDBY:
272 rdev_err(rdev, "invalid mode %x specified\n", *mode);
276 if (!rdev->constraints) {
277 rdev_err(rdev, "no constraints\n");
280 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
281 rdev_err(rdev, "mode operation not allowed\n");
285 /* The modes are bitmasks, the most power hungry modes having
286 * the lowest values. If the requested mode isn't supported
287 * try higher modes. */
289 if (rdev->constraints->valid_modes_mask & *mode)
297 /* dynamic regulator mode switching constraint check */
298 static int regulator_check_drms(struct regulator_dev *rdev)
300 if (!rdev->constraints) {
301 rdev_err(rdev, "no constraints\n");
304 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
305 rdev_dbg(rdev, "drms operation not allowed\n");
311 static ssize_t regulator_uV_show(struct device *dev,
312 struct device_attribute *attr, char *buf)
314 struct regulator_dev *rdev = dev_get_drvdata(dev);
317 mutex_lock(&rdev->mutex);
318 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
319 mutex_unlock(&rdev->mutex);
323 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
325 static ssize_t regulator_uA_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
328 struct regulator_dev *rdev = dev_get_drvdata(dev);
330 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
332 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
334 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
337 struct regulator_dev *rdev = dev_get_drvdata(dev);
339 return sprintf(buf, "%s\n", rdev_get_name(rdev));
341 static DEVICE_ATTR_RO(name);
343 static ssize_t regulator_print_opmode(char *buf, int mode)
346 case REGULATOR_MODE_FAST:
347 return sprintf(buf, "fast\n");
348 case REGULATOR_MODE_NORMAL:
349 return sprintf(buf, "normal\n");
350 case REGULATOR_MODE_IDLE:
351 return sprintf(buf, "idle\n");
352 case REGULATOR_MODE_STANDBY:
353 return sprintf(buf, "standby\n");
355 return sprintf(buf, "unknown\n");
358 static ssize_t regulator_opmode_show(struct device *dev,
359 struct device_attribute *attr, char *buf)
361 struct regulator_dev *rdev = dev_get_drvdata(dev);
363 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
365 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
367 static ssize_t regulator_print_state(char *buf, int state)
370 return sprintf(buf, "enabled\n");
372 return sprintf(buf, "disabled\n");
374 return sprintf(buf, "unknown\n");
377 static ssize_t regulator_state_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
380 struct regulator_dev *rdev = dev_get_drvdata(dev);
383 mutex_lock(&rdev->mutex);
384 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
385 mutex_unlock(&rdev->mutex);
389 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
391 static ssize_t regulator_status_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
394 struct regulator_dev *rdev = dev_get_drvdata(dev);
398 status = rdev->desc->ops->get_status(rdev);
403 case REGULATOR_STATUS_OFF:
406 case REGULATOR_STATUS_ON:
409 case REGULATOR_STATUS_ERROR:
412 case REGULATOR_STATUS_FAST:
415 case REGULATOR_STATUS_NORMAL:
418 case REGULATOR_STATUS_IDLE:
421 case REGULATOR_STATUS_STANDBY:
424 case REGULATOR_STATUS_BYPASS:
427 case REGULATOR_STATUS_UNDEFINED:
434 return sprintf(buf, "%s\n", label);
436 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
438 static ssize_t regulator_min_uA_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct regulator_dev *rdev = dev_get_drvdata(dev);
443 if (!rdev->constraints)
444 return sprintf(buf, "constraint not defined\n");
446 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
448 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
450 static ssize_t regulator_max_uA_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 struct regulator_dev *rdev = dev_get_drvdata(dev);
455 if (!rdev->constraints)
456 return sprintf(buf, "constraint not defined\n");
458 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
460 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
462 static ssize_t regulator_min_uV_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
465 struct regulator_dev *rdev = dev_get_drvdata(dev);
467 if (!rdev->constraints)
468 return sprintf(buf, "constraint not defined\n");
470 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
472 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
474 static ssize_t regulator_max_uV_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
477 struct regulator_dev *rdev = dev_get_drvdata(dev);
479 if (!rdev->constraints)
480 return sprintf(buf, "constraint not defined\n");
482 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
484 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
486 static ssize_t regulator_total_uA_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
489 struct regulator_dev *rdev = dev_get_drvdata(dev);
490 struct regulator *regulator;
493 mutex_lock(&rdev->mutex);
494 list_for_each_entry(regulator, &rdev->consumer_list, list)
495 uA += regulator->uA_load;
496 mutex_unlock(&rdev->mutex);
497 return sprintf(buf, "%d\n", uA);
499 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
501 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
504 struct regulator_dev *rdev = dev_get_drvdata(dev);
505 return sprintf(buf, "%d\n", rdev->use_count);
507 static DEVICE_ATTR_RO(num_users);
509 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
512 struct regulator_dev *rdev = dev_get_drvdata(dev);
514 switch (rdev->desc->type) {
515 case REGULATOR_VOLTAGE:
516 return sprintf(buf, "voltage\n");
517 case REGULATOR_CURRENT:
518 return sprintf(buf, "current\n");
520 return sprintf(buf, "unknown\n");
522 static DEVICE_ATTR_RO(type);
524 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
527 struct regulator_dev *rdev = dev_get_drvdata(dev);
529 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
531 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
532 regulator_suspend_mem_uV_show, NULL);
534 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
535 struct device_attribute *attr, char *buf)
537 struct regulator_dev *rdev = dev_get_drvdata(dev);
539 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
541 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
542 regulator_suspend_disk_uV_show, NULL);
544 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
545 struct device_attribute *attr, char *buf)
547 struct regulator_dev *rdev = dev_get_drvdata(dev);
549 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
551 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
552 regulator_suspend_standby_uV_show, NULL);
554 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
557 struct regulator_dev *rdev = dev_get_drvdata(dev);
559 return regulator_print_opmode(buf,
560 rdev->constraints->state_mem.mode);
562 static DEVICE_ATTR(suspend_mem_mode, 0444,
563 regulator_suspend_mem_mode_show, NULL);
565 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
568 struct regulator_dev *rdev = dev_get_drvdata(dev);
570 return regulator_print_opmode(buf,
571 rdev->constraints->state_disk.mode);
573 static DEVICE_ATTR(suspend_disk_mode, 0444,
574 regulator_suspend_disk_mode_show, NULL);
576 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
577 struct device_attribute *attr, char *buf)
579 struct regulator_dev *rdev = dev_get_drvdata(dev);
581 return regulator_print_opmode(buf,
582 rdev->constraints->state_standby.mode);
584 static DEVICE_ATTR(suspend_standby_mode, 0444,
585 regulator_suspend_standby_mode_show, NULL);
587 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
590 struct regulator_dev *rdev = dev_get_drvdata(dev);
592 return regulator_print_state(buf,
593 rdev->constraints->state_mem.enabled);
595 static DEVICE_ATTR(suspend_mem_state, 0444,
596 regulator_suspend_mem_state_show, NULL);
598 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
601 struct regulator_dev *rdev = dev_get_drvdata(dev);
603 return regulator_print_state(buf,
604 rdev->constraints->state_disk.enabled);
606 static DEVICE_ATTR(suspend_disk_state, 0444,
607 regulator_suspend_disk_state_show, NULL);
609 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
610 struct device_attribute *attr, char *buf)
612 struct regulator_dev *rdev = dev_get_drvdata(dev);
614 return regulator_print_state(buf,
615 rdev->constraints->state_standby.enabled);
617 static DEVICE_ATTR(suspend_standby_state, 0444,
618 regulator_suspend_standby_state_show, NULL);
620 static ssize_t regulator_bypass_show(struct device *dev,
621 struct device_attribute *attr, char *buf)
623 struct regulator_dev *rdev = dev_get_drvdata(dev);
628 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
637 return sprintf(buf, "%s\n", report);
639 static DEVICE_ATTR(bypass, 0444,
640 regulator_bypass_show, NULL);
642 /* Calculate the new optimum regulator operating mode based on the new total
643 * consumer load. All locks held by caller */
644 static int drms_uA_update(struct regulator_dev *rdev)
646 struct regulator *sibling;
647 int current_uA = 0, output_uV, input_uV, err;
650 lockdep_assert_held_once(&rdev->mutex);
653 * first check to see if we can set modes at all, otherwise just
654 * tell the consumer everything is OK.
656 err = regulator_check_drms(rdev);
660 if (!rdev->desc->ops->get_optimum_mode &&
661 !rdev->desc->ops->set_load)
664 if (!rdev->desc->ops->set_mode &&
665 !rdev->desc->ops->set_load)
668 /* get output voltage */
669 output_uV = _regulator_get_voltage(rdev);
670 if (output_uV <= 0) {
671 rdev_err(rdev, "invalid output voltage found\n");
675 /* get input voltage */
678 input_uV = regulator_get_voltage(rdev->supply);
680 input_uV = rdev->constraints->input_uV;
682 rdev_err(rdev, "invalid input voltage found\n");
686 /* calc total requested load */
687 list_for_each_entry(sibling, &rdev->consumer_list, list)
688 current_uA += sibling->uA_load;
690 current_uA += rdev->constraints->system_load;
692 if (rdev->desc->ops->set_load) {
693 /* set the optimum mode for our new total regulator load */
694 err = rdev->desc->ops->set_load(rdev, current_uA);
696 rdev_err(rdev, "failed to set load %d\n", current_uA);
698 /* now get the optimum mode for our new total regulator load */
699 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
700 output_uV, current_uA);
702 /* check the new mode is allowed */
703 err = regulator_mode_constrain(rdev, &mode);
705 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
706 current_uA, input_uV, output_uV);
710 err = rdev->desc->ops->set_mode(rdev, mode);
712 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
718 static int suspend_set_state(struct regulator_dev *rdev,
719 struct regulator_state *rstate)
723 /* If we have no suspend mode configration don't set anything;
724 * only warn if the driver implements set_suspend_voltage or
725 * set_suspend_mode callback.
727 if (!rstate->enabled && !rstate->disabled) {
728 if (rdev->desc->ops->set_suspend_voltage ||
729 rdev->desc->ops->set_suspend_mode)
730 rdev_warn(rdev, "No configuration\n");
734 if (rstate->enabled && rstate->disabled) {
735 rdev_err(rdev, "invalid configuration\n");
739 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
740 ret = rdev->desc->ops->set_suspend_enable(rdev);
741 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
742 ret = rdev->desc->ops->set_suspend_disable(rdev);
743 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
747 rdev_err(rdev, "failed to enabled/disable\n");
751 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
752 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
754 rdev_err(rdev, "failed to set voltage\n");
759 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
760 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
762 rdev_err(rdev, "failed to set mode\n");
769 /* locks held by caller */
770 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
772 lockdep_assert_held_once(&rdev->mutex);
774 if (!rdev->constraints)
778 case PM_SUSPEND_STANDBY:
779 return suspend_set_state(rdev,
780 &rdev->constraints->state_standby);
782 return suspend_set_state(rdev,
783 &rdev->constraints->state_mem);
785 return suspend_set_state(rdev,
786 &rdev->constraints->state_disk);
792 static void print_constraints(struct regulator_dev *rdev)
794 struct regulation_constraints *constraints = rdev->constraints;
796 size_t len = sizeof(buf) - 1;
800 if (constraints->min_uV && constraints->max_uV) {
801 if (constraints->min_uV == constraints->max_uV)
802 count += scnprintf(buf + count, len - count, "%d mV ",
803 constraints->min_uV / 1000);
805 count += scnprintf(buf + count, len - count,
807 constraints->min_uV / 1000,
808 constraints->max_uV / 1000);
811 if (!constraints->min_uV ||
812 constraints->min_uV != constraints->max_uV) {
813 ret = _regulator_get_voltage(rdev);
815 count += scnprintf(buf + count, len - count,
816 "at %d mV ", ret / 1000);
819 if (constraints->uV_offset)
820 count += scnprintf(buf + count, len - count, "%dmV offset ",
821 constraints->uV_offset / 1000);
823 if (constraints->min_uA && constraints->max_uA) {
824 if (constraints->min_uA == constraints->max_uA)
825 count += scnprintf(buf + count, len - count, "%d mA ",
826 constraints->min_uA / 1000);
828 count += scnprintf(buf + count, len - count,
830 constraints->min_uA / 1000,
831 constraints->max_uA / 1000);
834 if (!constraints->min_uA ||
835 constraints->min_uA != constraints->max_uA) {
836 ret = _regulator_get_current_limit(rdev);
838 count += scnprintf(buf + count, len - count,
839 "at %d mA ", ret / 1000);
842 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
843 count += scnprintf(buf + count, len - count, "fast ");
844 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
845 count += scnprintf(buf + count, len - count, "normal ");
846 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
847 count += scnprintf(buf + count, len - count, "idle ");
848 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
849 count += scnprintf(buf + count, len - count, "standby");
852 scnprintf(buf, len, "no parameters");
854 rdev_dbg(rdev, "%s\n", buf);
856 if ((constraints->min_uV != constraints->max_uV) &&
857 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
859 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
862 static int machine_constraints_voltage(struct regulator_dev *rdev,
863 struct regulation_constraints *constraints)
865 const struct regulator_ops *ops = rdev->desc->ops;
868 /* do we need to apply the constraint voltage */
869 if (rdev->constraints->apply_uV &&
870 rdev->constraints->min_uV == rdev->constraints->max_uV) {
871 int current_uV = _regulator_get_voltage(rdev);
872 if (current_uV < 0) {
874 "failed to get the current voltage(%d)\n",
878 if (current_uV < rdev->constraints->min_uV ||
879 current_uV > rdev->constraints->max_uV) {
880 ret = _regulator_do_set_voltage(
881 rdev, rdev->constraints->min_uV,
882 rdev->constraints->max_uV);
885 "failed to apply %duV constraint(%d)\n",
886 rdev->constraints->min_uV, ret);
892 /* constrain machine-level voltage specs to fit
893 * the actual range supported by this regulator.
895 if (ops->list_voltage && rdev->desc->n_voltages) {
896 int count = rdev->desc->n_voltages;
898 int min_uV = INT_MAX;
899 int max_uV = INT_MIN;
900 int cmin = constraints->min_uV;
901 int cmax = constraints->max_uV;
903 /* it's safe to autoconfigure fixed-voltage supplies
904 and the constraints are used by list_voltage. */
905 if (count == 1 && !cmin) {
908 constraints->min_uV = cmin;
909 constraints->max_uV = cmax;
912 /* voltage constraints are optional */
913 if ((cmin == 0) && (cmax == 0))
916 /* else require explicit machine-level constraints */
917 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
918 rdev_err(rdev, "invalid voltage constraints\n");
922 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
923 for (i = 0; i < count; i++) {
926 value = ops->list_voltage(rdev, i);
930 /* maybe adjust [min_uV..max_uV] */
931 if (value >= cmin && value < min_uV)
933 if (value <= cmax && value > max_uV)
937 /* final: [min_uV..max_uV] valid iff constraints valid */
938 if (max_uV < min_uV) {
940 "unsupportable voltage constraints %u-%uuV\n",
945 /* use regulator's subset of machine constraints */
946 if (constraints->min_uV < min_uV) {
947 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
948 constraints->min_uV, min_uV);
949 constraints->min_uV = min_uV;
951 if (constraints->max_uV > max_uV) {
952 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
953 constraints->max_uV, max_uV);
954 constraints->max_uV = max_uV;
961 static int machine_constraints_current(struct regulator_dev *rdev,
962 struct regulation_constraints *constraints)
964 const struct regulator_ops *ops = rdev->desc->ops;
967 if (!constraints->min_uA && !constraints->max_uA)
970 if (constraints->min_uA > constraints->max_uA) {
971 rdev_err(rdev, "Invalid current constraints\n");
975 if (!ops->set_current_limit || !ops->get_current_limit) {
976 rdev_warn(rdev, "Operation of current configuration missing\n");
980 /* Set regulator current in constraints range */
981 ret = ops->set_current_limit(rdev, constraints->min_uA,
982 constraints->max_uA);
984 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
991 static int _regulator_do_enable(struct regulator_dev *rdev);
994 * set_machine_constraints - sets regulator constraints
995 * @rdev: regulator source
996 * @constraints: constraints to apply
998 * Allows platform initialisation code to define and constrain
999 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1000 * Constraints *must* be set by platform code in order for some
1001 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1004 static int set_machine_constraints(struct regulator_dev *rdev,
1005 const struct regulation_constraints *constraints)
1008 const struct regulator_ops *ops = rdev->desc->ops;
1011 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1014 rdev->constraints = kzalloc(sizeof(*constraints),
1016 if (!rdev->constraints)
1019 ret = machine_constraints_voltage(rdev, rdev->constraints);
1023 ret = machine_constraints_current(rdev, rdev->constraints);
1027 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1028 ret = ops->set_input_current_limit(rdev,
1029 rdev->constraints->ilim_uA);
1031 rdev_err(rdev, "failed to set input limit\n");
1036 /* do we need to setup our suspend state */
1037 if (rdev->constraints->initial_state) {
1038 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
1040 rdev_err(rdev, "failed to set suspend state\n");
1045 if (rdev->constraints->initial_mode) {
1046 if (!ops->set_mode) {
1047 rdev_err(rdev, "no set_mode operation\n");
1052 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1054 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1059 /* If the constraints say the regulator should be on at this point
1060 * and we have control then make sure it is enabled.
1062 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1063 ret = _regulator_do_enable(rdev);
1064 if (ret < 0 && ret != -EINVAL) {
1065 rdev_err(rdev, "failed to enable\n");
1070 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1071 && ops->set_ramp_delay) {
1072 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1074 rdev_err(rdev, "failed to set ramp_delay\n");
1079 if (rdev->constraints->pull_down && ops->set_pull_down) {
1080 ret = ops->set_pull_down(rdev);
1082 rdev_err(rdev, "failed to set pull down\n");
1087 if (rdev->constraints->soft_start && ops->set_soft_start) {
1088 ret = ops->set_soft_start(rdev);
1090 rdev_err(rdev, "failed to set soft start\n");
1095 if (rdev->constraints->over_current_protection
1096 && ops->set_over_current_protection) {
1097 ret = ops->set_over_current_protection(rdev);
1099 rdev_err(rdev, "failed to set over current protection\n");
1104 print_constraints(rdev);
1107 kfree(rdev->constraints);
1108 rdev->constraints = NULL;
1113 * set_supply - set regulator supply regulator
1114 * @rdev: regulator name
1115 * @supply_rdev: supply regulator name
1117 * Called by platform initialisation code to set the supply regulator for this
1118 * regulator. This ensures that a regulators supply will also be enabled by the
1119 * core if it's child is enabled.
1121 static int set_supply(struct regulator_dev *rdev,
1122 struct regulator_dev *supply_rdev)
1126 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1128 if (!try_module_get(supply_rdev->owner))
1131 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1132 if (rdev->supply == NULL) {
1136 supply_rdev->open_count++;
1142 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1143 * @rdev: regulator source
1144 * @consumer_dev_name: dev_name() string for device supply applies to
1145 * @supply: symbolic name for supply
1147 * Allows platform initialisation code to map physical regulator
1148 * sources to symbolic names for supplies for use by devices. Devices
1149 * should use these symbolic names to request regulators, avoiding the
1150 * need to provide board-specific regulator names as platform data.
1152 static int set_consumer_device_supply(struct regulator_dev *rdev,
1153 const char *consumer_dev_name,
1156 struct regulator_map *node;
1162 if (consumer_dev_name != NULL)
1167 list_for_each_entry(node, ®ulator_map_list, list) {
1168 if (node->dev_name && consumer_dev_name) {
1169 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1171 } else if (node->dev_name || consumer_dev_name) {
1175 if (strcmp(node->supply, supply) != 0)
1178 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1180 dev_name(&node->regulator->dev),
1181 node->regulator->desc->name,
1183 dev_name(&rdev->dev), rdev_get_name(rdev));
1187 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1191 node->regulator = rdev;
1192 node->supply = supply;
1195 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1196 if (node->dev_name == NULL) {
1202 list_add(&node->list, ®ulator_map_list);
1206 static void unset_regulator_supplies(struct regulator_dev *rdev)
1208 struct regulator_map *node, *n;
1210 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1211 if (rdev == node->regulator) {
1212 list_del(&node->list);
1213 kfree(node->dev_name);
1219 #define REG_STR_SIZE 64
1221 static struct regulator *create_regulator(struct regulator_dev *rdev,
1223 const char *supply_name)
1225 struct regulator *regulator;
1226 char buf[REG_STR_SIZE];
1229 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1230 if (regulator == NULL)
1233 mutex_lock(&rdev->mutex);
1234 regulator->rdev = rdev;
1235 list_add(®ulator->list, &rdev->consumer_list);
1238 regulator->dev = dev;
1240 /* Add a link to the device sysfs entry */
1241 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1242 dev->kobj.name, supply_name);
1243 if (size >= REG_STR_SIZE)
1246 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1247 if (regulator->supply_name == NULL)
1250 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1253 rdev_dbg(rdev, "could not add device link %s err %d\n",
1254 dev->kobj.name, err);
1258 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1259 if (regulator->supply_name == NULL)
1263 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1265 if (!regulator->debugfs) {
1266 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1268 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1269 ®ulator->uA_load);
1270 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1271 ®ulator->min_uV);
1272 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1273 ®ulator->max_uV);
1277 * Check now if the regulator is an always on regulator - if
1278 * it is then we don't need to do nearly so much work for
1279 * enable/disable calls.
1281 if (!_regulator_can_change_status(rdev) &&
1282 _regulator_is_enabled(rdev))
1283 regulator->always_on = true;
1285 mutex_unlock(&rdev->mutex);
1288 list_del(®ulator->list);
1290 mutex_unlock(&rdev->mutex);
1294 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1296 if (rdev->constraints && rdev->constraints->enable_time)
1297 return rdev->constraints->enable_time;
1298 if (!rdev->desc->ops->enable_time)
1299 return rdev->desc->enable_time;
1300 return rdev->desc->ops->enable_time(rdev);
1303 static struct regulator_supply_alias *regulator_find_supply_alias(
1304 struct device *dev, const char *supply)
1306 struct regulator_supply_alias *map;
1308 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1309 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1315 static void regulator_supply_alias(struct device **dev, const char **supply)
1317 struct regulator_supply_alias *map;
1319 map = regulator_find_supply_alias(*dev, *supply);
1321 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1322 *supply, map->alias_supply,
1323 dev_name(map->alias_dev));
1324 *dev = map->alias_dev;
1325 *supply = map->alias_supply;
1329 static int of_node_match(struct device *dev, const void *data)
1331 return dev->of_node == data;
1334 static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
1338 dev = class_find_device(®ulator_class, NULL, np, of_node_match);
1340 return dev ? dev_to_rdev(dev) : NULL;
1343 static int regulator_match(struct device *dev, const void *data)
1345 struct regulator_dev *r = dev_to_rdev(dev);
1347 return strcmp(rdev_get_name(r), data) == 0;
1350 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1354 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1356 return dev ? dev_to_rdev(dev) : NULL;
1360 * regulator_dev_lookup - lookup a regulator device.
1361 * @dev: device for regulator "consumer".
1362 * @supply: Supply name or regulator ID.
1363 * @ret: 0 on success, -ENODEV if lookup fails permanently, -EPROBE_DEFER if
1364 * lookup could succeed in the future.
1366 * If successful, returns a struct regulator_dev that corresponds to the name
1367 * @supply and with the embedded struct device refcount incremented by one,
1368 * or NULL on failure. The refcount must be dropped by calling put_device().
1370 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1374 struct regulator_dev *r;
1375 struct device_node *node;
1376 struct regulator_map *map;
1377 const char *devname = NULL;
1379 regulator_supply_alias(&dev, &supply);
1381 /* first do a dt based lookup */
1382 if (dev && dev->of_node) {
1383 node = of_get_regulator(dev, supply);
1385 r = of_find_regulator_by_node(node);
1388 *ret = -EPROBE_DEFER;
1392 * If we couldn't even get the node then it's
1393 * not just that the device didn't register
1394 * yet, there's no node and we'll never
1401 /* if not found, try doing it non-dt way */
1403 devname = dev_name(dev);
1405 r = regulator_lookup_by_name(supply);
1409 mutex_lock(®ulator_list_mutex);
1410 list_for_each_entry(map, ®ulator_map_list, list) {
1411 /* If the mapping has a device set up it must match */
1412 if (map->dev_name &&
1413 (!devname || strcmp(map->dev_name, devname)))
1416 if (strcmp(map->supply, supply) == 0 &&
1417 get_device(&map->regulator->dev)) {
1418 mutex_unlock(®ulator_list_mutex);
1419 return map->regulator;
1422 mutex_unlock(®ulator_list_mutex);
1427 static int regulator_resolve_supply(struct regulator_dev *rdev)
1429 struct regulator_dev *r;
1430 struct device *dev = rdev->dev.parent;
1433 /* No supply to resovle? */
1434 if (!rdev->supply_name)
1437 /* Supply already resolved? */
1441 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1443 if (ret == -ENODEV) {
1445 * No supply was specified for this regulator and
1446 * there will never be one.
1451 /* Did the lookup explicitly defer for us? */
1452 if (ret == -EPROBE_DEFER)
1455 if (have_full_constraints()) {
1456 r = dummy_regulator_rdev;
1457 get_device(&r->dev);
1459 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1460 rdev->supply_name, rdev->desc->name);
1461 return -EPROBE_DEFER;
1465 /* Recursively resolve the supply of the supply */
1466 ret = regulator_resolve_supply(r);
1468 put_device(&r->dev);
1472 ret = set_supply(rdev, r);
1474 put_device(&r->dev);
1478 /* Cascade always-on state to supply */
1479 if (_regulator_is_enabled(rdev) && rdev->supply) {
1480 ret = regulator_enable(rdev->supply);
1482 _regulator_put(rdev->supply);
1490 /* Internal regulator request function */
1491 static struct regulator *_regulator_get(struct device *dev, const char *id,
1492 bool exclusive, bool allow_dummy)
1494 struct regulator_dev *rdev;
1495 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1496 const char *devname = NULL;
1500 pr_err("get() with no identifier\n");
1501 return ERR_PTR(-EINVAL);
1505 devname = dev_name(dev);
1507 if (have_full_constraints())
1510 ret = -EPROBE_DEFER;
1512 rdev = regulator_dev_lookup(dev, id, &ret);
1516 regulator = ERR_PTR(ret);
1519 * If we have return value from dev_lookup fail, we do not expect to
1520 * succeed, so, quit with appropriate error value
1522 if (ret && ret != -ENODEV)
1526 devname = "deviceless";
1529 * Assume that a regulator is physically present and enabled
1530 * even if it isn't hooked up and just provide a dummy.
1532 if (have_full_constraints() && allow_dummy) {
1533 pr_warn("%s supply %s not found, using dummy regulator\n",
1536 rdev = dummy_regulator_rdev;
1537 get_device(&rdev->dev);
1539 /* Don't log an error when called from regulator_get_optional() */
1540 } else if (!have_full_constraints() || exclusive) {
1541 dev_warn(dev, "dummy supplies not allowed\n");
1547 if (rdev->exclusive) {
1548 regulator = ERR_PTR(-EPERM);
1549 put_device(&rdev->dev);
1553 if (exclusive && rdev->open_count) {
1554 regulator = ERR_PTR(-EBUSY);
1555 put_device(&rdev->dev);
1559 ret = regulator_resolve_supply(rdev);
1561 regulator = ERR_PTR(ret);
1562 put_device(&rdev->dev);
1566 if (!try_module_get(rdev->owner)) {
1567 put_device(&rdev->dev);
1571 regulator = create_regulator(rdev, dev, id);
1572 if (regulator == NULL) {
1573 regulator = ERR_PTR(-ENOMEM);
1574 put_device(&rdev->dev);
1575 module_put(rdev->owner);
1581 rdev->exclusive = 1;
1583 ret = _regulator_is_enabled(rdev);
1585 rdev->use_count = 1;
1587 rdev->use_count = 0;
1594 * regulator_get - lookup and obtain a reference to a regulator.
1595 * @dev: device for regulator "consumer"
1596 * @id: Supply name or regulator ID.
1598 * Returns a struct regulator corresponding to the regulator producer,
1599 * or IS_ERR() condition containing errno.
1601 * Use of supply names configured via regulator_set_device_supply() is
1602 * strongly encouraged. It is recommended that the supply name used
1603 * should match the name used for the supply and/or the relevant
1604 * device pins in the datasheet.
1606 struct regulator *regulator_get(struct device *dev, const char *id)
1608 return _regulator_get(dev, id, false, true);
1610 EXPORT_SYMBOL_GPL(regulator_get);
1613 * regulator_get_exclusive - obtain exclusive access to a regulator.
1614 * @dev: device for regulator "consumer"
1615 * @id: Supply name or regulator ID.
1617 * Returns a struct regulator corresponding to the regulator producer,
1618 * or IS_ERR() condition containing errno. Other consumers will be
1619 * unable to obtain this regulator while this reference is held and the
1620 * use count for the regulator will be initialised to reflect the current
1621 * state of the regulator.
1623 * This is intended for use by consumers which cannot tolerate shared
1624 * use of the regulator such as those which need to force the
1625 * regulator off for correct operation of the hardware they are
1628 * Use of supply names configured via regulator_set_device_supply() is
1629 * strongly encouraged. It is recommended that the supply name used
1630 * should match the name used for the supply and/or the relevant
1631 * device pins in the datasheet.
1633 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1635 return _regulator_get(dev, id, true, false);
1637 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1640 * regulator_get_optional - obtain optional access to a regulator.
1641 * @dev: device for regulator "consumer"
1642 * @id: Supply name or regulator ID.
1644 * Returns a struct regulator corresponding to the regulator producer,
1645 * or IS_ERR() condition containing errno.
1647 * This is intended for use by consumers for devices which can have
1648 * some supplies unconnected in normal use, such as some MMC devices.
1649 * It can allow the regulator core to provide stub supplies for other
1650 * supplies requested using normal regulator_get() calls without
1651 * disrupting the operation of drivers that can handle absent
1654 * Use of supply names configured via regulator_set_device_supply() is
1655 * strongly encouraged. It is recommended that the supply name used
1656 * should match the name used for the supply and/or the relevant
1657 * device pins in the datasheet.
1659 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1661 return _regulator_get(dev, id, false, false);
1663 EXPORT_SYMBOL_GPL(regulator_get_optional);
1665 /* regulator_list_mutex lock held by regulator_put() */
1666 static void _regulator_put(struct regulator *regulator)
1668 struct regulator_dev *rdev;
1670 if (IS_ERR_OR_NULL(regulator))
1673 lockdep_assert_held_once(®ulator_list_mutex);
1675 rdev = regulator->rdev;
1677 debugfs_remove_recursive(regulator->debugfs);
1679 /* remove any sysfs entries */
1681 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1682 mutex_lock(&rdev->mutex);
1683 list_del(®ulator->list);
1686 rdev->exclusive = 0;
1687 put_device(&rdev->dev);
1688 mutex_unlock(&rdev->mutex);
1690 kfree(regulator->supply_name);
1693 module_put(rdev->owner);
1697 * regulator_put - "free" the regulator source
1698 * @regulator: regulator source
1700 * Note: drivers must ensure that all regulator_enable calls made on this
1701 * regulator source are balanced by regulator_disable calls prior to calling
1704 void regulator_put(struct regulator *regulator)
1706 mutex_lock(®ulator_list_mutex);
1707 _regulator_put(regulator);
1708 mutex_unlock(®ulator_list_mutex);
1710 EXPORT_SYMBOL_GPL(regulator_put);
1713 * regulator_register_supply_alias - Provide device alias for supply lookup
1715 * @dev: device that will be given as the regulator "consumer"
1716 * @id: Supply name or regulator ID
1717 * @alias_dev: device that should be used to lookup the supply
1718 * @alias_id: Supply name or regulator ID that should be used to lookup the
1721 * All lookups for id on dev will instead be conducted for alias_id on
1724 int regulator_register_supply_alias(struct device *dev, const char *id,
1725 struct device *alias_dev,
1726 const char *alias_id)
1728 struct regulator_supply_alias *map;
1730 map = regulator_find_supply_alias(dev, id);
1734 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1739 map->src_supply = id;
1740 map->alias_dev = alias_dev;
1741 map->alias_supply = alias_id;
1743 list_add(&map->list, ®ulator_supply_alias_list);
1745 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1746 id, dev_name(dev), alias_id, dev_name(alias_dev));
1750 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1753 * regulator_unregister_supply_alias - Remove device alias
1755 * @dev: device that will be given as the regulator "consumer"
1756 * @id: Supply name or regulator ID
1758 * Remove a lookup alias if one exists for id on dev.
1760 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1762 struct regulator_supply_alias *map;
1764 map = regulator_find_supply_alias(dev, id);
1766 list_del(&map->list);
1770 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1773 * regulator_bulk_register_supply_alias - register multiple aliases
1775 * @dev: device that will be given as the regulator "consumer"
1776 * @id: List of supply names or regulator IDs
1777 * @alias_dev: device that should be used to lookup the supply
1778 * @alias_id: List of supply names or regulator IDs that should be used to
1780 * @num_id: Number of aliases to register
1782 * @return 0 on success, an errno on failure.
1784 * This helper function allows drivers to register several supply
1785 * aliases in one operation. If any of the aliases cannot be
1786 * registered any aliases that were registered will be removed
1787 * before returning to the caller.
1789 int regulator_bulk_register_supply_alias(struct device *dev,
1790 const char *const *id,
1791 struct device *alias_dev,
1792 const char *const *alias_id,
1798 for (i = 0; i < num_id; ++i) {
1799 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1809 "Failed to create supply alias %s,%s -> %s,%s\n",
1810 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1813 regulator_unregister_supply_alias(dev, id[i]);
1817 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1820 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1822 * @dev: device that will be given as the regulator "consumer"
1823 * @id: List of supply names or regulator IDs
1824 * @num_id: Number of aliases to unregister
1826 * This helper function allows drivers to unregister several supply
1827 * aliases in one operation.
1829 void regulator_bulk_unregister_supply_alias(struct device *dev,
1830 const char *const *id,
1835 for (i = 0; i < num_id; ++i)
1836 regulator_unregister_supply_alias(dev, id[i]);
1838 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1841 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1842 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1843 const struct regulator_config *config)
1845 struct regulator_enable_gpio *pin;
1846 struct gpio_desc *gpiod;
1849 gpiod = gpio_to_desc(config->ena_gpio);
1851 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1852 if (pin->gpiod == gpiod) {
1853 rdev_dbg(rdev, "GPIO %d is already used\n",
1855 goto update_ena_gpio_to_rdev;
1859 ret = gpio_request_one(config->ena_gpio,
1860 GPIOF_DIR_OUT | config->ena_gpio_flags,
1861 rdev_get_name(rdev));
1865 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1867 gpio_free(config->ena_gpio);
1872 pin->ena_gpio_invert = config->ena_gpio_invert;
1873 list_add(&pin->list, ®ulator_ena_gpio_list);
1875 update_ena_gpio_to_rdev:
1876 pin->request_count++;
1877 rdev->ena_pin = pin;
1881 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1883 struct regulator_enable_gpio *pin, *n;
1888 /* Free the GPIO only in case of no use */
1889 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1890 if (pin->gpiod == rdev->ena_pin->gpiod) {
1891 if (pin->request_count <= 1) {
1892 pin->request_count = 0;
1893 gpiod_put(pin->gpiod);
1894 list_del(&pin->list);
1896 rdev->ena_pin = NULL;
1899 pin->request_count--;
1906 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1907 * @rdev: regulator_dev structure
1908 * @enable: enable GPIO at initial use?
1910 * GPIO is enabled in case of initial use. (enable_count is 0)
1911 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1913 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1915 struct regulator_enable_gpio *pin = rdev->ena_pin;
1921 /* Enable GPIO at initial use */
1922 if (pin->enable_count == 0)
1923 gpiod_set_value_cansleep(pin->gpiod,
1924 !pin->ena_gpio_invert);
1926 pin->enable_count++;
1928 if (pin->enable_count > 1) {
1929 pin->enable_count--;
1933 /* Disable GPIO if not used */
1934 if (pin->enable_count <= 1) {
1935 gpiod_set_value_cansleep(pin->gpiod,
1936 pin->ena_gpio_invert);
1937 pin->enable_count = 0;
1945 * _regulator_enable_delay - a delay helper function
1946 * @delay: time to delay in microseconds
1948 * Delay for the requested amount of time as per the guidelines in:
1950 * Documentation/timers/timers-howto.txt
1952 * The assumption here is that regulators will never be enabled in
1953 * atomic context and therefore sleeping functions can be used.
1955 static void _regulator_enable_delay(unsigned int delay)
1957 unsigned int ms = delay / 1000;
1958 unsigned int us = delay % 1000;
1962 * For small enough values, handle super-millisecond
1963 * delays in the usleep_range() call below.
1972 * Give the scheduler some room to coalesce with any other
1973 * wakeup sources. For delays shorter than 10 us, don't even
1974 * bother setting up high-resolution timers and just busy-
1978 usleep_range(us, us + 100);
1983 static int _regulator_do_enable(struct regulator_dev *rdev)
1987 /* Query before enabling in case configuration dependent. */
1988 ret = _regulator_get_enable_time(rdev);
1992 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1996 trace_regulator_enable(rdev_get_name(rdev));
1998 if (rdev->desc->off_on_delay) {
1999 /* if needed, keep a distance of off_on_delay from last time
2000 * this regulator was disabled.
2002 unsigned long start_jiffy = jiffies;
2003 unsigned long intended, max_delay, remaining;
2005 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2006 intended = rdev->last_off_jiffy + max_delay;
2008 if (time_before(start_jiffy, intended)) {
2009 /* calc remaining jiffies to deal with one-time
2011 * in case of multiple timer wrapping, either it can be
2012 * detected by out-of-range remaining, or it cannot be
2013 * detected and we gets a panelty of
2014 * _regulator_enable_delay().
2016 remaining = intended - start_jiffy;
2017 if (remaining <= max_delay)
2018 _regulator_enable_delay(
2019 jiffies_to_usecs(remaining));
2023 if (rdev->ena_pin) {
2024 if (!rdev->ena_gpio_state) {
2025 ret = regulator_ena_gpio_ctrl(rdev, true);
2028 rdev->ena_gpio_state = 1;
2030 } else if (rdev->desc->ops->enable) {
2031 ret = rdev->desc->ops->enable(rdev);
2038 /* Allow the regulator to ramp; it would be useful to extend
2039 * this for bulk operations so that the regulators can ramp
2041 trace_regulator_enable_delay(rdev_get_name(rdev));
2043 _regulator_enable_delay(delay);
2045 trace_regulator_enable_complete(rdev_get_name(rdev));
2050 /* locks held by regulator_enable() */
2051 static int _regulator_enable(struct regulator_dev *rdev)
2055 lockdep_assert_held_once(&rdev->mutex);
2057 /* check voltage and requested load before enabling */
2058 if (rdev->constraints &&
2059 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
2060 drms_uA_update(rdev);
2062 if (rdev->use_count == 0) {
2063 /* The regulator may on if it's not switchable or left on */
2064 ret = _regulator_is_enabled(rdev);
2065 if (ret == -EINVAL || ret == 0) {
2066 if (!_regulator_can_change_status(rdev))
2069 ret = _regulator_do_enable(rdev);
2073 } else if (ret < 0) {
2074 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2077 /* Fallthrough on positive return values - already enabled */
2086 * regulator_enable - enable regulator output
2087 * @regulator: regulator source
2089 * Request that the regulator be enabled with the regulator output at
2090 * the predefined voltage or current value. Calls to regulator_enable()
2091 * must be balanced with calls to regulator_disable().
2093 * NOTE: the output value can be set by other drivers, boot loader or may be
2094 * hardwired in the regulator.
2096 int regulator_enable(struct regulator *regulator)
2098 struct regulator_dev *rdev = regulator->rdev;
2101 if (regulator->always_on)
2105 ret = regulator_enable(rdev->supply);
2110 mutex_lock(&rdev->mutex);
2111 ret = _regulator_enable(rdev);
2112 mutex_unlock(&rdev->mutex);
2114 if (ret != 0 && rdev->supply)
2115 regulator_disable(rdev->supply);
2119 EXPORT_SYMBOL_GPL(regulator_enable);
2121 static int _regulator_do_disable(struct regulator_dev *rdev)
2125 trace_regulator_disable(rdev_get_name(rdev));
2127 if (rdev->ena_pin) {
2128 if (rdev->ena_gpio_state) {
2129 ret = regulator_ena_gpio_ctrl(rdev, false);
2132 rdev->ena_gpio_state = 0;
2135 } else if (rdev->desc->ops->disable) {
2136 ret = rdev->desc->ops->disable(rdev);
2141 /* cares about last_off_jiffy only if off_on_delay is required by
2144 if (rdev->desc->off_on_delay)
2145 rdev->last_off_jiffy = jiffies;
2147 trace_regulator_disable_complete(rdev_get_name(rdev));
2152 /* locks held by regulator_disable() */
2153 static int _regulator_disable(struct regulator_dev *rdev)
2157 lockdep_assert_held_once(&rdev->mutex);
2159 if (WARN(rdev->use_count <= 0,
2160 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2163 /* are we the last user and permitted to disable ? */
2164 if (rdev->use_count == 1 &&
2165 (rdev->constraints && !rdev->constraints->always_on)) {
2167 /* we are last user */
2168 if (_regulator_can_change_status(rdev)) {
2169 ret = _notifier_call_chain(rdev,
2170 REGULATOR_EVENT_PRE_DISABLE,
2172 if (ret & NOTIFY_STOP_MASK)
2175 ret = _regulator_do_disable(rdev);
2177 rdev_err(rdev, "failed to disable\n");
2178 _notifier_call_chain(rdev,
2179 REGULATOR_EVENT_ABORT_DISABLE,
2183 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2187 rdev->use_count = 0;
2188 } else if (rdev->use_count > 1) {
2190 if (rdev->constraints &&
2191 (rdev->constraints->valid_ops_mask &
2192 REGULATOR_CHANGE_DRMS))
2193 drms_uA_update(rdev);
2202 * regulator_disable - disable regulator output
2203 * @regulator: regulator source
2205 * Disable the regulator output voltage or current. Calls to
2206 * regulator_enable() must be balanced with calls to
2207 * regulator_disable().
2209 * NOTE: this will only disable the regulator output if no other consumer
2210 * devices have it enabled, the regulator device supports disabling and
2211 * machine constraints permit this operation.
2213 int regulator_disable(struct regulator *regulator)
2215 struct regulator_dev *rdev = regulator->rdev;
2218 if (regulator->always_on)
2221 mutex_lock(&rdev->mutex);
2222 ret = _regulator_disable(rdev);
2223 mutex_unlock(&rdev->mutex);
2225 if (ret == 0 && rdev->supply)
2226 regulator_disable(rdev->supply);
2230 EXPORT_SYMBOL_GPL(regulator_disable);
2232 /* locks held by regulator_force_disable() */
2233 static int _regulator_force_disable(struct regulator_dev *rdev)
2237 lockdep_assert_held_once(&rdev->mutex);
2239 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2240 REGULATOR_EVENT_PRE_DISABLE, NULL);
2241 if (ret & NOTIFY_STOP_MASK)
2244 ret = _regulator_do_disable(rdev);
2246 rdev_err(rdev, "failed to force disable\n");
2247 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2248 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2252 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2253 REGULATOR_EVENT_DISABLE, NULL);
2259 * regulator_force_disable - force disable regulator output
2260 * @regulator: regulator source
2262 * Forcibly disable the regulator output voltage or current.
2263 * NOTE: this *will* disable the regulator output even if other consumer
2264 * devices have it enabled. This should be used for situations when device
2265 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2267 int regulator_force_disable(struct regulator *regulator)
2269 struct regulator_dev *rdev = regulator->rdev;
2272 mutex_lock(&rdev->mutex);
2273 regulator->uA_load = 0;
2274 ret = _regulator_force_disable(regulator->rdev);
2275 mutex_unlock(&rdev->mutex);
2278 while (rdev->open_count--)
2279 regulator_disable(rdev->supply);
2283 EXPORT_SYMBOL_GPL(regulator_force_disable);
2285 static void regulator_disable_work(struct work_struct *work)
2287 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2291 mutex_lock(&rdev->mutex);
2293 BUG_ON(!rdev->deferred_disables);
2295 count = rdev->deferred_disables;
2296 rdev->deferred_disables = 0;
2298 for (i = 0; i < count; i++) {
2299 ret = _regulator_disable(rdev);
2301 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2304 mutex_unlock(&rdev->mutex);
2307 for (i = 0; i < count; i++) {
2308 ret = regulator_disable(rdev->supply);
2311 "Supply disable failed: %d\n", ret);
2318 * regulator_disable_deferred - disable regulator output with delay
2319 * @regulator: regulator source
2320 * @ms: miliseconds until the regulator is disabled
2322 * Execute regulator_disable() on the regulator after a delay. This
2323 * is intended for use with devices that require some time to quiesce.
2325 * NOTE: this will only disable the regulator output if no other consumer
2326 * devices have it enabled, the regulator device supports disabling and
2327 * machine constraints permit this operation.
2329 int regulator_disable_deferred(struct regulator *regulator, int ms)
2331 struct regulator_dev *rdev = regulator->rdev;
2334 if (regulator->always_on)
2338 return regulator_disable(regulator);
2340 mutex_lock(&rdev->mutex);
2341 rdev->deferred_disables++;
2342 mutex_unlock(&rdev->mutex);
2344 ret = queue_delayed_work(system_power_efficient_wq,
2345 &rdev->disable_work,
2346 msecs_to_jiffies(ms));
2352 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2354 static int _regulator_is_enabled(struct regulator_dev *rdev)
2356 /* A GPIO control always takes precedence */
2358 return rdev->ena_gpio_state;
2360 /* If we don't know then assume that the regulator is always on */
2361 if (!rdev->desc->ops->is_enabled)
2364 return rdev->desc->ops->is_enabled(rdev);
2368 * regulator_is_enabled - is the regulator output enabled
2369 * @regulator: regulator source
2371 * Returns positive if the regulator driver backing the source/client
2372 * has requested that the device be enabled, zero if it hasn't, else a
2373 * negative errno code.
2375 * Note that the device backing this regulator handle can have multiple
2376 * users, so it might be enabled even if regulator_enable() was never
2377 * called for this particular source.
2379 int regulator_is_enabled(struct regulator *regulator)
2383 if (regulator->always_on)
2386 mutex_lock(®ulator->rdev->mutex);
2387 ret = _regulator_is_enabled(regulator->rdev);
2388 mutex_unlock(®ulator->rdev->mutex);
2392 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2395 * regulator_can_change_voltage - check if regulator can change voltage
2396 * @regulator: regulator source
2398 * Returns positive if the regulator driver backing the source/client
2399 * can change its voltage, false otherwise. Useful for detecting fixed
2400 * or dummy regulators and disabling voltage change logic in the client
2403 int regulator_can_change_voltage(struct regulator *regulator)
2405 struct regulator_dev *rdev = regulator->rdev;
2407 if (rdev->constraints &&
2408 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2409 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2412 if (rdev->desc->continuous_voltage_range &&
2413 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2414 rdev->constraints->min_uV != rdev->constraints->max_uV)
2420 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2423 * regulator_count_voltages - count regulator_list_voltage() selectors
2424 * @regulator: regulator source
2426 * Returns number of selectors, or negative errno. Selectors are
2427 * numbered starting at zero, and typically correspond to bitfields
2428 * in hardware registers.
2430 int regulator_count_voltages(struct regulator *regulator)
2432 struct regulator_dev *rdev = regulator->rdev;
2434 if (rdev->desc->n_voltages)
2435 return rdev->desc->n_voltages;
2440 return regulator_count_voltages(rdev->supply);
2442 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2445 * regulator_list_voltage - enumerate supported voltages
2446 * @regulator: regulator source
2447 * @selector: identify voltage to list
2448 * Context: can sleep
2450 * Returns a voltage that can be passed to @regulator_set_voltage(),
2451 * zero if this selector code can't be used on this system, or a
2454 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2456 struct regulator_dev *rdev = regulator->rdev;
2457 const struct regulator_ops *ops = rdev->desc->ops;
2460 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2461 return rdev->desc->fixed_uV;
2463 if (ops->list_voltage) {
2464 if (selector >= rdev->desc->n_voltages)
2466 mutex_lock(&rdev->mutex);
2467 ret = ops->list_voltage(rdev, selector);
2468 mutex_unlock(&rdev->mutex);
2469 } else if (rdev->supply) {
2470 ret = regulator_list_voltage(rdev->supply, selector);
2476 if (ret < rdev->constraints->min_uV)
2478 else if (ret > rdev->constraints->max_uV)
2484 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2487 * regulator_get_regmap - get the regulator's register map
2488 * @regulator: regulator source
2490 * Returns the register map for the given regulator, or an ERR_PTR value
2491 * if the regulator doesn't use regmap.
2493 struct regmap *regulator_get_regmap(struct regulator *regulator)
2495 struct regmap *map = regulator->rdev->regmap;
2497 return map ? map : ERR_PTR(-EOPNOTSUPP);
2501 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2502 * @regulator: regulator source
2503 * @vsel_reg: voltage selector register, output parameter
2504 * @vsel_mask: mask for voltage selector bitfield, output parameter
2506 * Returns the hardware register offset and bitmask used for setting the
2507 * regulator voltage. This might be useful when configuring voltage-scaling
2508 * hardware or firmware that can make I2C requests behind the kernel's back,
2511 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2512 * and 0 is returned, otherwise a negative errno is returned.
2514 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2516 unsigned *vsel_mask)
2518 struct regulator_dev *rdev = regulator->rdev;
2519 const struct regulator_ops *ops = rdev->desc->ops;
2521 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2524 *vsel_reg = rdev->desc->vsel_reg;
2525 *vsel_mask = rdev->desc->vsel_mask;
2529 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2532 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2533 * @regulator: regulator source
2534 * @selector: identify voltage to list
2536 * Converts the selector to a hardware-specific voltage selector that can be
2537 * directly written to the regulator registers. The address of the voltage
2538 * register can be determined by calling @regulator_get_hardware_vsel_register.
2540 * On error a negative errno is returned.
2542 int regulator_list_hardware_vsel(struct regulator *regulator,
2545 struct regulator_dev *rdev = regulator->rdev;
2546 const struct regulator_ops *ops = rdev->desc->ops;
2548 if (selector >= rdev->desc->n_voltages)
2550 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2555 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2558 * regulator_get_linear_step - return the voltage step size between VSEL values
2559 * @regulator: regulator source
2561 * Returns the voltage step size between VSEL values for linear
2562 * regulators, or return 0 if the regulator isn't a linear regulator.
2564 unsigned int regulator_get_linear_step(struct regulator *regulator)
2566 struct regulator_dev *rdev = regulator->rdev;
2568 return rdev->desc->uV_step;
2570 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2573 * regulator_is_supported_voltage - check if a voltage range can be supported
2575 * @regulator: Regulator to check.
2576 * @min_uV: Minimum required voltage in uV.
2577 * @max_uV: Maximum required voltage in uV.
2579 * Returns a boolean or a negative error code.
2581 int regulator_is_supported_voltage(struct regulator *regulator,
2582 int min_uV, int max_uV)
2584 struct regulator_dev *rdev = regulator->rdev;
2585 int i, voltages, ret;
2587 /* If we can't change voltage check the current voltage */
2588 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2589 ret = regulator_get_voltage(regulator);
2591 return min_uV <= ret && ret <= max_uV;
2596 /* Any voltage within constrains range is fine? */
2597 if (rdev->desc->continuous_voltage_range)
2598 return min_uV >= rdev->constraints->min_uV &&
2599 max_uV <= rdev->constraints->max_uV;
2601 ret = regulator_count_voltages(regulator);
2606 for (i = 0; i < voltages; i++) {
2607 ret = regulator_list_voltage(regulator, i);
2609 if (ret >= min_uV && ret <= max_uV)
2615 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2617 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2618 int min_uV, int max_uV,
2621 struct pre_voltage_change_data data;
2624 data.old_uV = _regulator_get_voltage(rdev);
2625 data.min_uV = min_uV;
2626 data.max_uV = max_uV;
2627 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2629 if (ret & NOTIFY_STOP_MASK)
2632 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2636 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2637 (void *)data.old_uV);
2642 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2643 int uV, unsigned selector)
2645 struct pre_voltage_change_data data;
2648 data.old_uV = _regulator_get_voltage(rdev);
2651 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2653 if (ret & NOTIFY_STOP_MASK)
2656 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2660 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2661 (void *)data.old_uV);
2666 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2667 int min_uV, int max_uV)
2672 unsigned int selector;
2673 int old_selector = -1;
2675 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2677 min_uV += rdev->constraints->uV_offset;
2678 max_uV += rdev->constraints->uV_offset;
2681 * If we can't obtain the old selector there is not enough
2682 * info to call set_voltage_time_sel().
2684 if (_regulator_is_enabled(rdev) &&
2685 rdev->desc->ops->set_voltage_time_sel &&
2686 rdev->desc->ops->get_voltage_sel) {
2687 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2688 if (old_selector < 0)
2689 return old_selector;
2692 if (rdev->desc->ops->set_voltage) {
2693 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2697 if (rdev->desc->ops->list_voltage)
2698 best_val = rdev->desc->ops->list_voltage(rdev,
2701 best_val = _regulator_get_voltage(rdev);
2704 } else if (rdev->desc->ops->set_voltage_sel) {
2705 if (rdev->desc->ops->map_voltage) {
2706 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2709 if (rdev->desc->ops->list_voltage ==
2710 regulator_list_voltage_linear)
2711 ret = regulator_map_voltage_linear(rdev,
2713 else if (rdev->desc->ops->list_voltage ==
2714 regulator_list_voltage_linear_range)
2715 ret = regulator_map_voltage_linear_range(rdev,
2718 ret = regulator_map_voltage_iterate(rdev,
2723 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2724 if (min_uV <= best_val && max_uV >= best_val) {
2726 if (old_selector == selector)
2729 ret = _regulator_call_set_voltage_sel(
2730 rdev, best_val, selector);
2739 /* Call set_voltage_time_sel if successfully obtained old_selector */
2740 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2741 && old_selector != selector) {
2743 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2744 old_selector, selector);
2746 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2751 /* Insert any necessary delays */
2752 if (delay >= 1000) {
2753 mdelay(delay / 1000);
2754 udelay(delay % 1000);
2760 if (ret == 0 && best_val >= 0) {
2761 unsigned long data = best_val;
2763 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2767 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2773 * regulator_set_voltage - set regulator output voltage
2774 * @regulator: regulator source
2775 * @min_uV: Minimum required voltage in uV
2776 * @max_uV: Maximum acceptable voltage in uV
2778 * Sets a voltage regulator to the desired output voltage. This can be set
2779 * during any regulator state. IOW, regulator can be disabled or enabled.
2781 * If the regulator is enabled then the voltage will change to the new value
2782 * immediately otherwise if the regulator is disabled the regulator will
2783 * output at the new voltage when enabled.
2785 * NOTE: If the regulator is shared between several devices then the lowest
2786 * request voltage that meets the system constraints will be used.
2787 * Regulator system constraints must be set for this regulator before
2788 * calling this function otherwise this call will fail.
2790 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2792 struct regulator_dev *rdev = regulator->rdev;
2794 int old_min_uV, old_max_uV;
2797 mutex_lock(&rdev->mutex);
2799 /* If we're setting the same range as last time the change
2800 * should be a noop (some cpufreq implementations use the same
2801 * voltage for multiple frequencies, for example).
2803 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2806 /* If we're trying to set a range that overlaps the current voltage,
2807 * return successfully even though the regulator does not support
2808 * changing the voltage.
2810 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2811 current_uV = _regulator_get_voltage(rdev);
2812 if (min_uV <= current_uV && current_uV <= max_uV) {
2813 regulator->min_uV = min_uV;
2814 regulator->max_uV = max_uV;
2820 if (!rdev->desc->ops->set_voltage &&
2821 !rdev->desc->ops->set_voltage_sel) {
2826 /* constraints check */
2827 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2831 /* restore original values in case of error */
2832 old_min_uV = regulator->min_uV;
2833 old_max_uV = regulator->max_uV;
2834 regulator->min_uV = min_uV;
2835 regulator->max_uV = max_uV;
2837 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2841 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2846 mutex_unlock(&rdev->mutex);
2849 regulator->min_uV = old_min_uV;
2850 regulator->max_uV = old_max_uV;
2851 mutex_unlock(&rdev->mutex);
2854 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2857 * regulator_set_voltage_time - get raise/fall time
2858 * @regulator: regulator source
2859 * @old_uV: starting voltage in microvolts
2860 * @new_uV: target voltage in microvolts
2862 * Provided with the starting and ending voltage, this function attempts to
2863 * calculate the time in microseconds required to rise or fall to this new
2866 int regulator_set_voltage_time(struct regulator *regulator,
2867 int old_uV, int new_uV)
2869 struct regulator_dev *rdev = regulator->rdev;
2870 const struct regulator_ops *ops = rdev->desc->ops;
2876 /* Currently requires operations to do this */
2877 if (!ops->list_voltage || !ops->set_voltage_time_sel
2878 || !rdev->desc->n_voltages)
2881 for (i = 0; i < rdev->desc->n_voltages; i++) {
2882 /* We only look for exact voltage matches here */
2883 voltage = regulator_list_voltage(regulator, i);
2888 if (voltage == old_uV)
2890 if (voltage == new_uV)
2894 if (old_sel < 0 || new_sel < 0)
2897 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2899 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2902 * regulator_set_voltage_time_sel - get raise/fall time
2903 * @rdev: regulator source device
2904 * @old_selector: selector for starting voltage
2905 * @new_selector: selector for target voltage
2907 * Provided with the starting and target voltage selectors, this function
2908 * returns time in microseconds required to rise or fall to this new voltage
2910 * Drivers providing ramp_delay in regulation_constraints can use this as their
2911 * set_voltage_time_sel() operation.
2913 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2914 unsigned int old_selector,
2915 unsigned int new_selector)
2917 unsigned int ramp_delay = 0;
2918 int old_volt, new_volt;
2920 if (rdev->constraints->ramp_delay)
2921 ramp_delay = rdev->constraints->ramp_delay;
2922 else if (rdev->desc->ramp_delay)
2923 ramp_delay = rdev->desc->ramp_delay;
2925 if (ramp_delay == 0) {
2926 rdev_warn(rdev, "ramp_delay not set\n");
2931 if (!rdev->desc->ops->list_voltage)
2934 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2935 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2937 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2939 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2942 * regulator_sync_voltage - re-apply last regulator output voltage
2943 * @regulator: regulator source
2945 * Re-apply the last configured voltage. This is intended to be used
2946 * where some external control source the consumer is cooperating with
2947 * has caused the configured voltage to change.
2949 int regulator_sync_voltage(struct regulator *regulator)
2951 struct regulator_dev *rdev = regulator->rdev;
2952 int ret, min_uV, max_uV;
2954 mutex_lock(&rdev->mutex);
2956 if (!rdev->desc->ops->set_voltage &&
2957 !rdev->desc->ops->set_voltage_sel) {
2962 /* This is only going to work if we've had a voltage configured. */
2963 if (!regulator->min_uV && !regulator->max_uV) {
2968 min_uV = regulator->min_uV;
2969 max_uV = regulator->max_uV;
2971 /* This should be a paranoia check... */
2972 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2976 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2980 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2983 mutex_unlock(&rdev->mutex);
2986 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2988 static int _regulator_get_voltage(struct regulator_dev *rdev)
2992 if (rdev->desc->ops->get_voltage_sel) {
2993 sel = rdev->desc->ops->get_voltage_sel(rdev);
2996 ret = rdev->desc->ops->list_voltage(rdev, sel);
2997 } else if (rdev->desc->ops->get_voltage) {
2998 ret = rdev->desc->ops->get_voltage(rdev);
2999 } else if (rdev->desc->ops->list_voltage) {
3000 ret = rdev->desc->ops->list_voltage(rdev, 0);
3001 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3002 ret = rdev->desc->fixed_uV;
3003 } else if (rdev->supply) {
3004 ret = regulator_get_voltage(rdev->supply);
3011 return ret - rdev->constraints->uV_offset;
3015 * regulator_get_voltage - get regulator output voltage
3016 * @regulator: regulator source
3018 * This returns the current regulator voltage in uV.
3020 * NOTE: If the regulator is disabled it will return the voltage value. This
3021 * function should not be used to determine regulator state.
3023 int regulator_get_voltage(struct regulator *regulator)
3027 mutex_lock(®ulator->rdev->mutex);
3029 ret = _regulator_get_voltage(regulator->rdev);
3031 mutex_unlock(®ulator->rdev->mutex);
3035 EXPORT_SYMBOL_GPL(regulator_get_voltage);
3038 * regulator_set_current_limit - set regulator output current limit
3039 * @regulator: regulator source
3040 * @min_uA: Minimum supported current in uA
3041 * @max_uA: Maximum supported current in uA
3043 * Sets current sink to the desired output current. This can be set during
3044 * any regulator state. IOW, regulator can be disabled or enabled.
3046 * If the regulator is enabled then the current will change to the new value
3047 * immediately otherwise if the regulator is disabled the regulator will
3048 * output at the new current when enabled.
3050 * NOTE: Regulator system constraints must be set for this regulator before
3051 * calling this function otherwise this call will fail.
3053 int regulator_set_current_limit(struct regulator *regulator,
3054 int min_uA, int max_uA)
3056 struct regulator_dev *rdev = regulator->rdev;
3059 mutex_lock(&rdev->mutex);
3062 if (!rdev->desc->ops->set_current_limit) {
3067 /* constraints check */
3068 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3072 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3074 mutex_unlock(&rdev->mutex);
3077 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3079 static int _regulator_get_current_limit(struct regulator_dev *rdev)
3083 mutex_lock(&rdev->mutex);
3086 if (!rdev->desc->ops->get_current_limit) {
3091 ret = rdev->desc->ops->get_current_limit(rdev);
3093 mutex_unlock(&rdev->mutex);
3098 * regulator_get_current_limit - get regulator output current
3099 * @regulator: regulator source
3101 * This returns the current supplied by the specified current sink in uA.
3103 * NOTE: If the regulator is disabled it will return the current value. This
3104 * function should not be used to determine regulator state.
3106 int regulator_get_current_limit(struct regulator *regulator)
3108 return _regulator_get_current_limit(regulator->rdev);
3110 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3113 * regulator_set_mode - set regulator operating mode
3114 * @regulator: regulator source
3115 * @mode: operating mode - one of the REGULATOR_MODE constants
3117 * Set regulator operating mode to increase regulator efficiency or improve
3118 * regulation performance.
3120 * NOTE: Regulator system constraints must be set for this regulator before
3121 * calling this function otherwise this call will fail.
3123 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3125 struct regulator_dev *rdev = regulator->rdev;
3127 int regulator_curr_mode;
3129 mutex_lock(&rdev->mutex);
3132 if (!rdev->desc->ops->set_mode) {
3137 /* return if the same mode is requested */
3138 if (rdev->desc->ops->get_mode) {
3139 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3140 if (regulator_curr_mode == mode) {
3146 /* constraints check */
3147 ret = regulator_mode_constrain(rdev, &mode);
3151 ret = rdev->desc->ops->set_mode(rdev, mode);
3153 mutex_unlock(&rdev->mutex);
3156 EXPORT_SYMBOL_GPL(regulator_set_mode);
3158 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3162 mutex_lock(&rdev->mutex);
3165 if (!rdev->desc->ops->get_mode) {
3170 ret = rdev->desc->ops->get_mode(rdev);
3172 mutex_unlock(&rdev->mutex);
3177 * regulator_get_mode - get regulator operating mode
3178 * @regulator: regulator source
3180 * Get the current regulator operating mode.
3182 unsigned int regulator_get_mode(struct regulator *regulator)
3184 return _regulator_get_mode(regulator->rdev);
3186 EXPORT_SYMBOL_GPL(regulator_get_mode);
3189 * regulator_set_load - set regulator load
3190 * @regulator: regulator source
3191 * @uA_load: load current
3193 * Notifies the regulator core of a new device load. This is then used by
3194 * DRMS (if enabled by constraints) to set the most efficient regulator
3195 * operating mode for the new regulator loading.
3197 * Consumer devices notify their supply regulator of the maximum power
3198 * they will require (can be taken from device datasheet in the power
3199 * consumption tables) when they change operational status and hence power
3200 * state. Examples of operational state changes that can affect power
3201 * consumption are :-
3203 * o Device is opened / closed.
3204 * o Device I/O is about to begin or has just finished.
3205 * o Device is idling in between work.
3207 * This information is also exported via sysfs to userspace.
3209 * DRMS will sum the total requested load on the regulator and change
3210 * to the most efficient operating mode if platform constraints allow.
3212 * On error a negative errno is returned.
3214 int regulator_set_load(struct regulator *regulator, int uA_load)
3216 struct regulator_dev *rdev = regulator->rdev;
3219 mutex_lock(&rdev->mutex);
3220 regulator->uA_load = uA_load;
3221 ret = drms_uA_update(rdev);
3222 mutex_unlock(&rdev->mutex);
3226 EXPORT_SYMBOL_GPL(regulator_set_load);
3229 * regulator_allow_bypass - allow the regulator to go into bypass mode
3231 * @regulator: Regulator to configure
3232 * @enable: enable or disable bypass mode
3234 * Allow the regulator to go into bypass mode if all other consumers
3235 * for the regulator also enable bypass mode and the machine
3236 * constraints allow this. Bypass mode means that the regulator is
3237 * simply passing the input directly to the output with no regulation.
3239 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3241 struct regulator_dev *rdev = regulator->rdev;
3244 if (!rdev->desc->ops->set_bypass)
3247 if (rdev->constraints &&
3248 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3251 mutex_lock(&rdev->mutex);
3253 if (enable && !regulator->bypass) {
3254 rdev->bypass_count++;
3256 if (rdev->bypass_count == rdev->open_count) {
3257 ret = rdev->desc->ops->set_bypass(rdev, enable);
3259 rdev->bypass_count--;
3262 } else if (!enable && regulator->bypass) {
3263 rdev->bypass_count--;
3265 if (rdev->bypass_count != rdev->open_count) {
3266 ret = rdev->desc->ops->set_bypass(rdev, enable);
3268 rdev->bypass_count++;
3273 regulator->bypass = enable;
3275 mutex_unlock(&rdev->mutex);
3279 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3282 * regulator_register_notifier - register regulator event notifier
3283 * @regulator: regulator source
3284 * @nb: notifier block
3286 * Register notifier block to receive regulator events.
3288 int regulator_register_notifier(struct regulator *regulator,
3289 struct notifier_block *nb)
3291 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3294 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3297 * regulator_unregister_notifier - unregister regulator event notifier
3298 * @regulator: regulator source
3299 * @nb: notifier block
3301 * Unregister regulator event notifier block.
3303 int regulator_unregister_notifier(struct regulator *regulator,
3304 struct notifier_block *nb)
3306 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3309 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3311 /* notify regulator consumers and downstream regulator consumers.
3312 * Note mutex must be held by caller.
3314 static int _notifier_call_chain(struct regulator_dev *rdev,
3315 unsigned long event, void *data)
3317 /* call rdev chain first */
3318 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3322 * regulator_bulk_get - get multiple regulator consumers
3324 * @dev: Device to supply
3325 * @num_consumers: Number of consumers to register
3326 * @consumers: Configuration of consumers; clients are stored here.
3328 * @return 0 on success, an errno on failure.
3330 * This helper function allows drivers to get several regulator
3331 * consumers in one operation. If any of the regulators cannot be
3332 * acquired then any regulators that were allocated will be freed
3333 * before returning to the caller.
3335 int regulator_bulk_get(struct device *dev, int num_consumers,
3336 struct regulator_bulk_data *consumers)
3341 for (i = 0; i < num_consumers; i++)
3342 consumers[i].consumer = NULL;
3344 for (i = 0; i < num_consumers; i++) {
3345 consumers[i].consumer = regulator_get(dev,
3346 consumers[i].supply);
3347 if (IS_ERR(consumers[i].consumer)) {
3348 ret = PTR_ERR(consumers[i].consumer);
3349 dev_err(dev, "Failed to get supply '%s': %d\n",
3350 consumers[i].supply, ret);
3351 consumers[i].consumer = NULL;
3360 regulator_put(consumers[i].consumer);
3364 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3366 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3368 struct regulator_bulk_data *bulk = data;
3370 bulk->ret = regulator_enable(bulk->consumer);
3374 * regulator_bulk_enable - enable multiple regulator consumers
3376 * @num_consumers: Number of consumers
3377 * @consumers: Consumer data; clients are stored here.
3378 * @return 0 on success, an errno on failure
3380 * This convenience API allows consumers to enable multiple regulator
3381 * clients in a single API call. If any consumers cannot be enabled
3382 * then any others that were enabled will be disabled again prior to
3385 int regulator_bulk_enable(int num_consumers,
3386 struct regulator_bulk_data *consumers)
3388 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3392 for (i = 0; i < num_consumers; i++) {
3393 if (consumers[i].consumer->always_on)
3394 consumers[i].ret = 0;
3396 async_schedule_domain(regulator_bulk_enable_async,
3397 &consumers[i], &async_domain);
3400 async_synchronize_full_domain(&async_domain);
3402 /* If any consumer failed we need to unwind any that succeeded */
3403 for (i = 0; i < num_consumers; i++) {
3404 if (consumers[i].ret != 0) {
3405 ret = consumers[i].ret;
3413 for (i = 0; i < num_consumers; i++) {
3414 if (consumers[i].ret < 0)
3415 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3418 regulator_disable(consumers[i].consumer);
3423 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3426 * regulator_bulk_disable - disable multiple regulator consumers
3428 * @num_consumers: Number of consumers
3429 * @consumers: Consumer data; clients are stored here.
3430 * @return 0 on success, an errno on failure
3432 * This convenience API allows consumers to disable multiple regulator
3433 * clients in a single API call. If any consumers cannot be disabled
3434 * then any others that were disabled will be enabled again prior to
3437 int regulator_bulk_disable(int num_consumers,
3438 struct regulator_bulk_data *consumers)
3443 for (i = num_consumers - 1; i >= 0; --i) {
3444 ret = regulator_disable(consumers[i].consumer);
3452 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3453 for (++i; i < num_consumers; ++i) {
3454 r = regulator_enable(consumers[i].consumer);
3456 pr_err("Failed to reename %s: %d\n",
3457 consumers[i].supply, r);
3462 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3465 * regulator_bulk_force_disable - force disable multiple regulator consumers
3467 * @num_consumers: Number of consumers
3468 * @consumers: Consumer data; clients are stored here.
3469 * @return 0 on success, an errno on failure
3471 * This convenience API allows consumers to forcibly disable multiple regulator
3472 * clients in a single API call.
3473 * NOTE: This should be used for situations when device damage will
3474 * likely occur if the regulators are not disabled (e.g. over temp).
3475 * Although regulator_force_disable function call for some consumers can
3476 * return error numbers, the function is called for all consumers.
3478 int regulator_bulk_force_disable(int num_consumers,
3479 struct regulator_bulk_data *consumers)
3484 for (i = 0; i < num_consumers; i++)
3486 regulator_force_disable(consumers[i].consumer);
3488 for (i = 0; i < num_consumers; i++) {
3489 if (consumers[i].ret != 0) {
3490 ret = consumers[i].ret;
3499 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3502 * regulator_bulk_free - free multiple regulator consumers
3504 * @num_consumers: Number of consumers
3505 * @consumers: Consumer data; clients are stored here.
3507 * This convenience API allows consumers to free multiple regulator
3508 * clients in a single API call.
3510 void regulator_bulk_free(int num_consumers,
3511 struct regulator_bulk_data *consumers)
3515 for (i = 0; i < num_consumers; i++) {
3516 regulator_put(consumers[i].consumer);
3517 consumers[i].consumer = NULL;
3520 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3523 * regulator_notifier_call_chain - call regulator event notifier
3524 * @rdev: regulator source
3525 * @event: notifier block
3526 * @data: callback-specific data.
3528 * Called by regulator drivers to notify clients a regulator event has
3529 * occurred. We also notify regulator clients downstream.
3530 * Note lock must be held by caller.
3532 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3533 unsigned long event, void *data)
3535 lockdep_assert_held_once(&rdev->mutex);
3537 _notifier_call_chain(rdev, event, data);
3541 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3544 * regulator_mode_to_status - convert a regulator mode into a status
3546 * @mode: Mode to convert
3548 * Convert a regulator mode into a status.
3550 int regulator_mode_to_status(unsigned int mode)
3553 case REGULATOR_MODE_FAST:
3554 return REGULATOR_STATUS_FAST;
3555 case REGULATOR_MODE_NORMAL:
3556 return REGULATOR_STATUS_NORMAL;
3557 case REGULATOR_MODE_IDLE:
3558 return REGULATOR_STATUS_IDLE;
3559 case REGULATOR_MODE_STANDBY:
3560 return REGULATOR_STATUS_STANDBY;
3562 return REGULATOR_STATUS_UNDEFINED;
3565 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3567 static struct attribute *regulator_dev_attrs[] = {
3568 &dev_attr_name.attr,
3569 &dev_attr_num_users.attr,
3570 &dev_attr_type.attr,
3571 &dev_attr_microvolts.attr,
3572 &dev_attr_microamps.attr,
3573 &dev_attr_opmode.attr,
3574 &dev_attr_state.attr,
3575 &dev_attr_status.attr,
3576 &dev_attr_bypass.attr,
3577 &dev_attr_requested_microamps.attr,
3578 &dev_attr_min_microvolts.attr,
3579 &dev_attr_max_microvolts.attr,
3580 &dev_attr_min_microamps.attr,
3581 &dev_attr_max_microamps.attr,
3582 &dev_attr_suspend_standby_state.attr,
3583 &dev_attr_suspend_mem_state.attr,
3584 &dev_attr_suspend_disk_state.attr,
3585 &dev_attr_suspend_standby_microvolts.attr,
3586 &dev_attr_suspend_mem_microvolts.attr,
3587 &dev_attr_suspend_disk_microvolts.attr,
3588 &dev_attr_suspend_standby_mode.attr,
3589 &dev_attr_suspend_mem_mode.attr,
3590 &dev_attr_suspend_disk_mode.attr,
3595 * To avoid cluttering sysfs (and memory) with useless state, only
3596 * create attributes that can be meaningfully displayed.
3598 static umode_t regulator_attr_is_visible(struct kobject *kobj,
3599 struct attribute *attr, int idx)
3601 struct device *dev = kobj_to_dev(kobj);
3602 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
3603 const struct regulator_ops *ops = rdev->desc->ops;
3604 umode_t mode = attr->mode;
3606 /* these three are always present */
3607 if (attr == &dev_attr_name.attr ||
3608 attr == &dev_attr_num_users.attr ||
3609 attr == &dev_attr_type.attr)
3612 /* some attributes need specific methods to be displayed */
3613 if (attr == &dev_attr_microvolts.attr) {
3614 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3615 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3616 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3617 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3622 if (attr == &dev_attr_microamps.attr)
3623 return ops->get_current_limit ? mode : 0;
3625 if (attr == &dev_attr_opmode.attr)
3626 return ops->get_mode ? mode : 0;
3628 if (attr == &dev_attr_state.attr)
3629 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3631 if (attr == &dev_attr_status.attr)
3632 return ops->get_status ? mode : 0;
3634 if (attr == &dev_attr_bypass.attr)
3635 return ops->get_bypass ? mode : 0;
3637 /* some attributes are type-specific */
3638 if (attr == &dev_attr_requested_microamps.attr)
3639 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
3641 /* constraints need specific supporting methods */
3642 if (attr == &dev_attr_min_microvolts.attr ||
3643 attr == &dev_attr_max_microvolts.attr)
3644 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
3646 if (attr == &dev_attr_min_microamps.attr ||
3647 attr == &dev_attr_max_microamps.attr)
3648 return ops->set_current_limit ? mode : 0;
3650 if (attr == &dev_attr_suspend_standby_state.attr ||
3651 attr == &dev_attr_suspend_mem_state.attr ||
3652 attr == &dev_attr_suspend_disk_state.attr)
3655 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3656 attr == &dev_attr_suspend_mem_microvolts.attr ||
3657 attr == &dev_attr_suspend_disk_microvolts.attr)
3658 return ops->set_suspend_voltage ? mode : 0;
3660 if (attr == &dev_attr_suspend_standby_mode.attr ||
3661 attr == &dev_attr_suspend_mem_mode.attr ||
3662 attr == &dev_attr_suspend_disk_mode.attr)
3663 return ops->set_suspend_mode ? mode : 0;
3668 static const struct attribute_group regulator_dev_group = {
3669 .attrs = regulator_dev_attrs,
3670 .is_visible = regulator_attr_is_visible,
3673 static const struct attribute_group *regulator_dev_groups[] = {
3674 ®ulator_dev_group,
3678 static void regulator_dev_release(struct device *dev)
3680 struct regulator_dev *rdev = dev_get_drvdata(dev);
3682 kfree(rdev->constraints);
3683 of_node_put(rdev->dev.of_node);
3687 static struct class regulator_class = {
3688 .name = "regulator",
3689 .dev_release = regulator_dev_release,
3690 .dev_groups = regulator_dev_groups,
3693 static void rdev_init_debugfs(struct regulator_dev *rdev)
3695 struct device *parent = rdev->dev.parent;
3696 const char *rname = rdev_get_name(rdev);
3697 char name[NAME_MAX];
3699 /* Avoid duplicate debugfs directory names */
3700 if (parent && rname == rdev->desc->name) {
3701 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3706 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
3707 if (!rdev->debugfs) {
3708 rdev_warn(rdev, "Failed to create debugfs directory\n");
3712 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3714 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3716 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3717 &rdev->bypass_count);
3721 * regulator_register - register regulator
3722 * @regulator_desc: regulator to register
3723 * @cfg: runtime configuration for regulator
3725 * Called by regulator drivers to register a regulator.
3726 * Returns a valid pointer to struct regulator_dev on success
3727 * or an ERR_PTR() on error.
3729 struct regulator_dev *
3730 regulator_register(const struct regulator_desc *regulator_desc,
3731 const struct regulator_config *cfg)
3733 const struct regulation_constraints *constraints = NULL;
3734 const struct regulator_init_data *init_data;
3735 struct regulator_config *config = NULL;
3736 static atomic_t regulator_no = ATOMIC_INIT(-1);
3737 struct regulator_dev *rdev;
3741 if (regulator_desc == NULL || cfg == NULL)
3742 return ERR_PTR(-EINVAL);
3747 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3748 return ERR_PTR(-EINVAL);
3750 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3751 regulator_desc->type != REGULATOR_CURRENT)
3752 return ERR_PTR(-EINVAL);
3754 /* Only one of each should be implemented */
3755 WARN_ON(regulator_desc->ops->get_voltage &&
3756 regulator_desc->ops->get_voltage_sel);
3757 WARN_ON(regulator_desc->ops->set_voltage &&
3758 regulator_desc->ops->set_voltage_sel);
3760 /* If we're using selectors we must implement list_voltage. */
3761 if (regulator_desc->ops->get_voltage_sel &&
3762 !regulator_desc->ops->list_voltage) {
3763 return ERR_PTR(-EINVAL);
3765 if (regulator_desc->ops->set_voltage_sel &&
3766 !regulator_desc->ops->list_voltage) {
3767 return ERR_PTR(-EINVAL);
3770 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3772 return ERR_PTR(-ENOMEM);
3775 * Duplicate the config so the driver could override it after
3776 * parsing init data.
3778 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3779 if (config == NULL) {
3781 return ERR_PTR(-ENOMEM);
3784 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
3785 &rdev->dev.of_node);
3787 init_data = config->init_data;
3788 rdev->dev.of_node = of_node_get(config->of_node);
3791 mutex_lock(®ulator_list_mutex);
3793 mutex_init(&rdev->mutex);
3794 rdev->reg_data = config->driver_data;
3795 rdev->owner = regulator_desc->owner;
3796 rdev->desc = regulator_desc;
3798 rdev->regmap = config->regmap;
3799 else if (dev_get_regmap(dev, NULL))
3800 rdev->regmap = dev_get_regmap(dev, NULL);
3801 else if (dev->parent)
3802 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3803 INIT_LIST_HEAD(&rdev->consumer_list);
3804 INIT_LIST_HEAD(&rdev->list);
3805 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3806 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3808 /* preform any regulator specific init */
3809 if (init_data && init_data->regulator_init) {
3810 ret = init_data->regulator_init(rdev->reg_data);
3815 /* register with sysfs */
3816 rdev->dev.class = ®ulator_class;
3817 rdev->dev.parent = dev;
3818 dev_set_name(&rdev->dev, "regulator.%lu",
3819 (unsigned long) atomic_inc_return(®ulator_no));
3820 ret = device_register(&rdev->dev);
3822 put_device(&rdev->dev);
3826 dev_set_drvdata(&rdev->dev, rdev);
3828 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3829 gpio_is_valid(config->ena_gpio)) {
3830 ret = regulator_ena_gpio_request(rdev, config);
3832 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3833 config->ena_gpio, ret);
3838 /* set regulator constraints */
3840 constraints = &init_data->constraints;
3842 ret = set_machine_constraints(rdev, constraints);
3846 if (init_data && init_data->supply_regulator)
3847 rdev->supply_name = init_data->supply_regulator;
3848 else if (regulator_desc->supply_name)
3849 rdev->supply_name = regulator_desc->supply_name;
3851 /* add consumers devices */
3853 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3854 ret = set_consumer_device_supply(rdev,
3855 init_data->consumer_supplies[i].dev_name,
3856 init_data->consumer_supplies[i].supply);
3858 dev_err(dev, "Failed to set supply %s\n",
3859 init_data->consumer_supplies[i].supply);
3860 goto unset_supplies;
3865 rdev_init_debugfs(rdev);
3867 mutex_unlock(®ulator_list_mutex);
3872 unset_regulator_supplies(rdev);
3875 regulator_ena_gpio_free(rdev);
3876 kfree(rdev->constraints);
3878 device_unregister(&rdev->dev);
3879 /* device core frees rdev */
3880 rdev = ERR_PTR(ret);
3885 rdev = ERR_PTR(ret);
3888 EXPORT_SYMBOL_GPL(regulator_register);
3891 * regulator_unregister - unregister regulator
3892 * @rdev: regulator to unregister
3894 * Called by regulator drivers to unregister a regulator.
3896 void regulator_unregister(struct regulator_dev *rdev)
3902 while (rdev->use_count--)
3903 regulator_disable(rdev->supply);
3904 regulator_put(rdev->supply);
3906 mutex_lock(®ulator_list_mutex);
3907 debugfs_remove_recursive(rdev->debugfs);
3908 flush_work(&rdev->disable_work.work);
3909 WARN_ON(rdev->open_count);
3910 unset_regulator_supplies(rdev);
3911 list_del(&rdev->list);
3912 mutex_unlock(®ulator_list_mutex);
3913 regulator_ena_gpio_free(rdev);
3914 device_unregister(&rdev->dev);
3916 EXPORT_SYMBOL_GPL(regulator_unregister);
3918 static int _regulator_suspend_prepare(struct device *dev, void *data)
3920 struct regulator_dev *rdev = dev_to_rdev(dev);
3921 const suspend_state_t *state = data;
3924 mutex_lock(&rdev->mutex);
3925 ret = suspend_prepare(rdev, *state);
3926 mutex_unlock(&rdev->mutex);
3932 * regulator_suspend_prepare - prepare regulators for system wide suspend
3933 * @state: system suspend state
3935 * Configure each regulator with it's suspend operating parameters for state.
3936 * This will usually be called by machine suspend code prior to supending.
3938 int regulator_suspend_prepare(suspend_state_t state)
3940 /* ON is handled by regulator active state */
3941 if (state == PM_SUSPEND_ON)
3944 return class_for_each_device(®ulator_class, NULL, &state,
3945 _regulator_suspend_prepare);
3947 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3949 static int _regulator_suspend_finish(struct device *dev, void *data)
3951 struct regulator_dev *rdev = dev_to_rdev(dev);
3954 mutex_lock(&rdev->mutex);
3955 if (rdev->use_count > 0 || rdev->constraints->always_on) {
3956 if (!_regulator_is_enabled(rdev)) {
3957 ret = _regulator_do_enable(rdev);
3960 "Failed to resume regulator %d\n",
3964 if (!have_full_constraints())
3966 if (!_regulator_is_enabled(rdev))
3969 ret = _regulator_do_disable(rdev);
3971 dev_err(dev, "Failed to suspend regulator %d\n", ret);
3974 mutex_unlock(&rdev->mutex);
3976 /* Keep processing regulators in spite of any errors */
3981 * regulator_suspend_finish - resume regulators from system wide suspend
3983 * Turn on regulators that might be turned off by regulator_suspend_prepare
3984 * and that should be turned on according to the regulators properties.
3986 int regulator_suspend_finish(void)
3988 return class_for_each_device(®ulator_class, NULL, NULL,
3989 _regulator_suspend_finish);
3991 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3994 * regulator_has_full_constraints - the system has fully specified constraints
3996 * Calling this function will cause the regulator API to disable all
3997 * regulators which have a zero use count and don't have an always_on
3998 * constraint in a late_initcall.
4000 * The intention is that this will become the default behaviour in a
4001 * future kernel release so users are encouraged to use this facility
4004 void regulator_has_full_constraints(void)
4006 has_full_constraints = 1;
4008 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4011 * rdev_get_drvdata - get rdev regulator driver data
4014 * Get rdev regulator driver private data. This call can be used in the
4015 * regulator driver context.
4017 void *rdev_get_drvdata(struct regulator_dev *rdev)
4019 return rdev->reg_data;
4021 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4024 * regulator_get_drvdata - get regulator driver data
4025 * @regulator: regulator
4027 * Get regulator driver private data. This call can be used in the consumer
4028 * driver context when non API regulator specific functions need to be called.
4030 void *regulator_get_drvdata(struct regulator *regulator)
4032 return regulator->rdev->reg_data;
4034 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4037 * regulator_set_drvdata - set regulator driver data
4038 * @regulator: regulator
4041 void regulator_set_drvdata(struct regulator *regulator, void *data)
4043 regulator->rdev->reg_data = data;
4045 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4048 * regulator_get_id - get regulator ID
4051 int rdev_get_id(struct regulator_dev *rdev)
4053 return rdev->desc->id;
4055 EXPORT_SYMBOL_GPL(rdev_get_id);
4057 struct device *rdev_get_dev(struct regulator_dev *rdev)
4061 EXPORT_SYMBOL_GPL(rdev_get_dev);
4063 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4065 return reg_init_data->driver_data;
4067 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4069 #ifdef CONFIG_DEBUG_FS
4070 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4071 size_t count, loff_t *ppos)
4073 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4074 ssize_t len, ret = 0;
4075 struct regulator_map *map;
4080 list_for_each_entry(map, ®ulator_map_list, list) {
4081 len = snprintf(buf + ret, PAGE_SIZE - ret,
4083 rdev_get_name(map->regulator), map->dev_name,
4087 if (ret > PAGE_SIZE) {
4093 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4101 static const struct file_operations supply_map_fops = {
4102 #ifdef CONFIG_DEBUG_FS
4103 .read = supply_map_read_file,
4104 .llseek = default_llseek,
4108 #ifdef CONFIG_DEBUG_FS
4109 struct summary_data {
4111 struct regulator_dev *parent;
4115 static void regulator_summary_show_subtree(struct seq_file *s,
4116 struct regulator_dev *rdev,
4119 static int regulator_summary_show_children(struct device *dev, void *data)
4121 struct regulator_dev *rdev = dev_to_rdev(dev);
4122 struct summary_data *summary_data = data;
4124 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4125 regulator_summary_show_subtree(summary_data->s, rdev,
4126 summary_data->level + 1);
4131 static void regulator_summary_show_subtree(struct seq_file *s,
4132 struct regulator_dev *rdev,
4135 struct regulation_constraints *c;
4136 struct regulator *consumer;
4137 struct summary_data summary_data;
4142 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4144 30 - level * 3, rdev_get_name(rdev),
4145 rdev->use_count, rdev->open_count, rdev->bypass_count);
4147 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4148 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4150 c = rdev->constraints;
4152 switch (rdev->desc->type) {
4153 case REGULATOR_VOLTAGE:
4154 seq_printf(s, "%5dmV %5dmV ",
4155 c->min_uV / 1000, c->max_uV / 1000);
4157 case REGULATOR_CURRENT:
4158 seq_printf(s, "%5dmA %5dmA ",
4159 c->min_uA / 1000, c->max_uA / 1000);
4166 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4167 if (consumer->dev->class == ®ulator_class)
4170 seq_printf(s, "%*s%-*s ",
4171 (level + 1) * 3 + 1, "",
4172 30 - (level + 1) * 3, dev_name(consumer->dev));
4174 switch (rdev->desc->type) {
4175 case REGULATOR_VOLTAGE:
4176 seq_printf(s, "%37dmV %5dmV",
4177 consumer->min_uV / 1000,
4178 consumer->max_uV / 1000);
4180 case REGULATOR_CURRENT:
4188 summary_data.level = level;
4189 summary_data.parent = rdev;
4191 class_for_each_device(®ulator_class, NULL, &summary_data,
4192 regulator_summary_show_children);
4195 static int regulator_summary_show_roots(struct device *dev, void *data)
4197 struct regulator_dev *rdev = dev_to_rdev(dev);
4198 struct seq_file *s = data;
4201 regulator_summary_show_subtree(s, rdev, 0);
4206 static int regulator_summary_show(struct seq_file *s, void *data)
4208 seq_puts(s, " regulator use open bypass voltage current min max\n");
4209 seq_puts(s, "-------------------------------------------------------------------------------\n");
4211 class_for_each_device(®ulator_class, NULL, s,
4212 regulator_summary_show_roots);
4217 static int regulator_summary_open(struct inode *inode, struct file *file)
4219 return single_open(file, regulator_summary_show, inode->i_private);
4223 static const struct file_operations regulator_summary_fops = {
4224 #ifdef CONFIG_DEBUG_FS
4225 .open = regulator_summary_open,
4227 .llseek = seq_lseek,
4228 .release = single_release,
4232 static int __init regulator_init(void)
4236 ret = class_register(®ulator_class);
4238 debugfs_root = debugfs_create_dir("regulator", NULL);
4240 pr_warn("regulator: Failed to create debugfs directory\n");
4242 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4245 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4246 NULL, ®ulator_summary_fops);
4248 regulator_dummy_init();
4253 /* init early to allow our consumers to complete system booting */
4254 core_initcall(regulator_init);
4256 static int __init regulator_late_cleanup(struct device *dev, void *data)
4258 struct regulator_dev *rdev = dev_to_rdev(dev);
4259 const struct regulator_ops *ops = rdev->desc->ops;
4260 struct regulation_constraints *c = rdev->constraints;
4263 if (c && c->always_on)
4266 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4269 mutex_lock(&rdev->mutex);
4271 if (rdev->use_count)
4274 /* If we can't read the status assume it's on. */
4275 if (ops->is_enabled)
4276 enabled = ops->is_enabled(rdev);
4283 if (have_full_constraints()) {
4284 /* We log since this may kill the system if it goes
4286 rdev_info(rdev, "disabling\n");
4287 ret = _regulator_do_disable(rdev);
4289 rdev_err(rdev, "couldn't disable: %d\n", ret);
4291 /* The intention is that in future we will
4292 * assume that full constraints are provided
4293 * so warn even if we aren't going to do
4296 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4300 mutex_unlock(&rdev->mutex);
4305 static int __init regulator_init_complete(void)
4308 * Since DT doesn't provide an idiomatic mechanism for
4309 * enabling full constraints and since it's much more natural
4310 * with DT to provide them just assume that a DT enabled
4311 * system has full constraints.
4313 if (of_have_populated_dt())
4314 has_full_constraints = true;
4316 /* If we have a full configuration then disable any regulators
4317 * we have permission to change the status for and which are
4318 * not in use or always_on. This is effectively the default
4319 * for DT and ACPI as they have full constraints.
4321 class_for_each_device(®ulator_class, NULL, NULL,
4322 regulator_late_cleanup);
4326 late_initcall_sync(regulator_init_complete);