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;
62 * struct regulator_map
64 * Used to provide symbolic supply names to devices.
66 struct regulator_map {
67 struct list_head list;
68 const char *dev_name; /* The dev_name() for the consumer */
70 struct regulator_dev *regulator;
74 * struct regulator_enable_gpio
76 * Management for shared enable GPIO pin
78 struct regulator_enable_gpio {
79 struct list_head list;
80 struct gpio_desc *gpiod;
81 u32 enable_count; /* a number of enabled shared GPIO */
82 u32 request_count; /* a number of requested shared GPIO */
83 unsigned int ena_gpio_invert:1;
87 * struct regulator_supply_alias
89 * Used to map lookups for a supply onto an alternative device.
91 struct regulator_supply_alias {
92 struct list_head list;
93 struct device *src_dev;
94 const char *src_supply;
95 struct device *alias_dev;
96 const char *alias_supply;
99 static int _regulator_is_enabled(struct regulator_dev *rdev);
100 static int _regulator_disable(struct regulator_dev *rdev);
101 static int _regulator_get_voltage(struct regulator_dev *rdev);
102 static int _regulator_get_current_limit(struct regulator_dev *rdev);
103 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
104 static int _notifier_call_chain(struct regulator_dev *rdev,
105 unsigned long event, void *data);
106 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
107 int min_uV, int max_uV);
108 static int regulator_balance_voltage(struct regulator_dev *rdev,
109 suspend_state_t state);
110 static int regulator_set_voltage_rdev(struct regulator_dev *rdev,
111 int min_uV, int max_uV,
112 suspend_state_t state);
113 static struct regulator *create_regulator(struct regulator_dev *rdev,
115 const char *supply_name);
116 static void _regulator_put(struct regulator *regulator);
118 static const char *rdev_get_name(struct regulator_dev *rdev)
120 if (rdev->constraints && rdev->constraints->name)
121 return rdev->constraints->name;
122 else if (rdev->desc->name)
123 return rdev->desc->name;
128 static bool have_full_constraints(void)
130 return has_full_constraints || of_have_populated_dt();
133 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
135 if (!rdev->constraints) {
136 rdev_err(rdev, "no constraints\n");
140 if (rdev->constraints->valid_ops_mask & ops)
146 static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
148 if (rdev && rdev->supply)
149 return rdev->supply->rdev;
155 * regulator_lock_nested - lock a single regulator
156 * @rdev: regulator source
157 * @subclass: mutex subclass used for lockdep
159 * This function can be called many times by one task on
160 * a single regulator and its mutex will be locked only
161 * once. If a task, which is calling this function is other
162 * than the one, which initially locked the mutex, it will
165 static void regulator_lock_nested(struct regulator_dev *rdev,
166 unsigned int subclass)
168 if (!mutex_trylock(&rdev->mutex)) {
169 if (rdev->mutex_owner == current) {
173 mutex_lock_nested(&rdev->mutex, subclass);
177 rdev->mutex_owner = current;
180 static inline void regulator_lock(struct regulator_dev *rdev)
182 regulator_lock_nested(rdev, 0);
186 * regulator_unlock - unlock a single regulator
187 * @rdev: regulator_source
189 * This function unlocks the mutex when the
190 * reference counter reaches 0.
192 static void regulator_unlock(struct regulator_dev *rdev)
194 if (rdev->ref_cnt != 0) {
197 if (!rdev->ref_cnt) {
198 rdev->mutex_owner = NULL;
199 mutex_unlock(&rdev->mutex);
204 static int regulator_lock_recursive(struct regulator_dev *rdev,
205 unsigned int subclass)
207 struct regulator_dev *c_rdev;
210 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
211 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
216 regulator_lock_nested(c_rdev, subclass++);
220 regulator_lock_recursive(c_rdev->supply->rdev,
228 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
230 * @rdev: regulator source
232 * Unlock all regulators related with rdev by coupling or suppling.
234 static void regulator_unlock_dependent(struct regulator_dev *rdev)
236 struct regulator_dev *c_rdev;
239 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
240 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
245 regulator_unlock(c_rdev);
248 regulator_unlock_dependent(c_rdev->supply->rdev);
253 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
254 * @rdev: regulator source
256 * This function as a wrapper on regulator_lock_recursive(), which locks
257 * all regulators related with rdev by coupling or suppling.
259 static inline void regulator_lock_dependent(struct regulator_dev *rdev)
261 regulator_lock_recursive(rdev, 0);
265 * of_get_regulator - get a regulator device node based on supply name
266 * @dev: Device pointer for the consumer (of regulator) device
267 * @supply: regulator supply name
269 * Extract the regulator device node corresponding to the supply name.
270 * returns the device node corresponding to the regulator if found, else
273 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
275 struct device_node *regnode = NULL;
276 char prop_name[32]; /* 32 is max size of property name */
278 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
280 snprintf(prop_name, 32, "%s-supply", supply);
281 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
284 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
285 prop_name, dev->of_node);
291 /* Platform voltage constraint check */
292 static int regulator_check_voltage(struct regulator_dev *rdev,
293 int *min_uV, int *max_uV)
295 BUG_ON(*min_uV > *max_uV);
297 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
298 rdev_err(rdev, "voltage operation not allowed\n");
302 if (*max_uV > rdev->constraints->max_uV)
303 *max_uV = rdev->constraints->max_uV;
304 if (*min_uV < rdev->constraints->min_uV)
305 *min_uV = rdev->constraints->min_uV;
307 if (*min_uV > *max_uV) {
308 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
316 /* return 0 if the state is valid */
317 static int regulator_check_states(suspend_state_t state)
319 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
322 /* Make sure we select a voltage that suits the needs of all
323 * regulator consumers
325 static int regulator_check_consumers(struct regulator_dev *rdev,
326 int *min_uV, int *max_uV,
327 suspend_state_t state)
329 struct regulator *regulator;
330 struct regulator_voltage *voltage;
332 list_for_each_entry(regulator, &rdev->consumer_list, list) {
333 voltage = ®ulator->voltage[state];
335 * Assume consumers that didn't say anything are OK
336 * with anything in the constraint range.
338 if (!voltage->min_uV && !voltage->max_uV)
341 if (*max_uV > voltage->max_uV)
342 *max_uV = voltage->max_uV;
343 if (*min_uV < voltage->min_uV)
344 *min_uV = voltage->min_uV;
347 if (*min_uV > *max_uV) {
348 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
356 /* current constraint check */
357 static int regulator_check_current_limit(struct regulator_dev *rdev,
358 int *min_uA, int *max_uA)
360 BUG_ON(*min_uA > *max_uA);
362 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
363 rdev_err(rdev, "current operation not allowed\n");
367 if (*max_uA > rdev->constraints->max_uA)
368 *max_uA = rdev->constraints->max_uA;
369 if (*min_uA < rdev->constraints->min_uA)
370 *min_uA = rdev->constraints->min_uA;
372 if (*min_uA > *max_uA) {
373 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
381 /* operating mode constraint check */
382 static int regulator_mode_constrain(struct regulator_dev *rdev,
386 case REGULATOR_MODE_FAST:
387 case REGULATOR_MODE_NORMAL:
388 case REGULATOR_MODE_IDLE:
389 case REGULATOR_MODE_STANDBY:
392 rdev_err(rdev, "invalid mode %x specified\n", *mode);
396 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
397 rdev_err(rdev, "mode operation not allowed\n");
401 /* The modes are bitmasks, the most power hungry modes having
402 * the lowest values. If the requested mode isn't supported
403 * try higher modes. */
405 if (rdev->constraints->valid_modes_mask & *mode)
413 static inline struct regulator_state *
414 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
416 if (rdev->constraints == NULL)
420 case PM_SUSPEND_STANDBY:
421 return &rdev->constraints->state_standby;
423 return &rdev->constraints->state_mem;
425 return &rdev->constraints->state_disk;
431 static ssize_t regulator_uV_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
434 struct regulator_dev *rdev = dev_get_drvdata(dev);
437 regulator_lock(rdev);
438 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
439 regulator_unlock(rdev);
443 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
445 static ssize_t regulator_uA_show(struct device *dev,
446 struct device_attribute *attr, char *buf)
448 struct regulator_dev *rdev = dev_get_drvdata(dev);
450 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
452 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
454 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
457 struct regulator_dev *rdev = dev_get_drvdata(dev);
459 return sprintf(buf, "%s\n", rdev_get_name(rdev));
461 static DEVICE_ATTR_RO(name);
463 static const char *regulator_opmode_to_str(int mode)
466 case REGULATOR_MODE_FAST:
468 case REGULATOR_MODE_NORMAL:
470 case REGULATOR_MODE_IDLE:
472 case REGULATOR_MODE_STANDBY:
478 static ssize_t regulator_print_opmode(char *buf, int mode)
480 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
483 static ssize_t regulator_opmode_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
486 struct regulator_dev *rdev = dev_get_drvdata(dev);
488 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
490 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
492 static ssize_t regulator_print_state(char *buf, int state)
495 return sprintf(buf, "enabled\n");
497 return sprintf(buf, "disabled\n");
499 return sprintf(buf, "unknown\n");
502 static ssize_t regulator_state_show(struct device *dev,
503 struct device_attribute *attr, char *buf)
505 struct regulator_dev *rdev = dev_get_drvdata(dev);
508 regulator_lock(rdev);
509 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
510 regulator_unlock(rdev);
514 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
516 static ssize_t regulator_status_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
519 struct regulator_dev *rdev = dev_get_drvdata(dev);
523 status = rdev->desc->ops->get_status(rdev);
528 case REGULATOR_STATUS_OFF:
531 case REGULATOR_STATUS_ON:
534 case REGULATOR_STATUS_ERROR:
537 case REGULATOR_STATUS_FAST:
540 case REGULATOR_STATUS_NORMAL:
543 case REGULATOR_STATUS_IDLE:
546 case REGULATOR_STATUS_STANDBY:
549 case REGULATOR_STATUS_BYPASS:
552 case REGULATOR_STATUS_UNDEFINED:
559 return sprintf(buf, "%s\n", label);
561 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
563 static ssize_t regulator_min_uA_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
566 struct regulator_dev *rdev = dev_get_drvdata(dev);
568 if (!rdev->constraints)
569 return sprintf(buf, "constraint not defined\n");
571 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
573 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
575 static ssize_t regulator_max_uA_show(struct device *dev,
576 struct device_attribute *attr, char *buf)
578 struct regulator_dev *rdev = dev_get_drvdata(dev);
580 if (!rdev->constraints)
581 return sprintf(buf, "constraint not defined\n");
583 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
585 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
587 static ssize_t regulator_min_uV_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
590 struct regulator_dev *rdev = dev_get_drvdata(dev);
592 if (!rdev->constraints)
593 return sprintf(buf, "constraint not defined\n");
595 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
597 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
599 static ssize_t regulator_max_uV_show(struct device *dev,
600 struct device_attribute *attr, char *buf)
602 struct regulator_dev *rdev = dev_get_drvdata(dev);
604 if (!rdev->constraints)
605 return sprintf(buf, "constraint not defined\n");
607 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
609 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
611 static ssize_t regulator_total_uA_show(struct device *dev,
612 struct device_attribute *attr, char *buf)
614 struct regulator_dev *rdev = dev_get_drvdata(dev);
615 struct regulator *regulator;
618 regulator_lock(rdev);
619 list_for_each_entry(regulator, &rdev->consumer_list, list)
620 uA += regulator->uA_load;
621 regulator_unlock(rdev);
622 return sprintf(buf, "%d\n", uA);
624 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
626 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
629 struct regulator_dev *rdev = dev_get_drvdata(dev);
630 return sprintf(buf, "%d\n", rdev->use_count);
632 static DEVICE_ATTR_RO(num_users);
634 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
637 struct regulator_dev *rdev = dev_get_drvdata(dev);
639 switch (rdev->desc->type) {
640 case REGULATOR_VOLTAGE:
641 return sprintf(buf, "voltage\n");
642 case REGULATOR_CURRENT:
643 return sprintf(buf, "current\n");
645 return sprintf(buf, "unknown\n");
647 static DEVICE_ATTR_RO(type);
649 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
650 struct device_attribute *attr, char *buf)
652 struct regulator_dev *rdev = dev_get_drvdata(dev);
654 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
656 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
657 regulator_suspend_mem_uV_show, NULL);
659 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 struct regulator_dev *rdev = dev_get_drvdata(dev);
664 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
666 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
667 regulator_suspend_disk_uV_show, NULL);
669 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
670 struct device_attribute *attr, char *buf)
672 struct regulator_dev *rdev = dev_get_drvdata(dev);
674 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
676 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
677 regulator_suspend_standby_uV_show, NULL);
679 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
680 struct device_attribute *attr, char *buf)
682 struct regulator_dev *rdev = dev_get_drvdata(dev);
684 return regulator_print_opmode(buf,
685 rdev->constraints->state_mem.mode);
687 static DEVICE_ATTR(suspend_mem_mode, 0444,
688 regulator_suspend_mem_mode_show, NULL);
690 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
691 struct device_attribute *attr, char *buf)
693 struct regulator_dev *rdev = dev_get_drvdata(dev);
695 return regulator_print_opmode(buf,
696 rdev->constraints->state_disk.mode);
698 static DEVICE_ATTR(suspend_disk_mode, 0444,
699 regulator_suspend_disk_mode_show, NULL);
701 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
704 struct regulator_dev *rdev = dev_get_drvdata(dev);
706 return regulator_print_opmode(buf,
707 rdev->constraints->state_standby.mode);
709 static DEVICE_ATTR(suspend_standby_mode, 0444,
710 regulator_suspend_standby_mode_show, NULL);
712 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
713 struct device_attribute *attr, char *buf)
715 struct regulator_dev *rdev = dev_get_drvdata(dev);
717 return regulator_print_state(buf,
718 rdev->constraints->state_mem.enabled);
720 static DEVICE_ATTR(suspend_mem_state, 0444,
721 regulator_suspend_mem_state_show, NULL);
723 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
724 struct device_attribute *attr, char *buf)
726 struct regulator_dev *rdev = dev_get_drvdata(dev);
728 return regulator_print_state(buf,
729 rdev->constraints->state_disk.enabled);
731 static DEVICE_ATTR(suspend_disk_state, 0444,
732 regulator_suspend_disk_state_show, NULL);
734 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
735 struct device_attribute *attr, char *buf)
737 struct regulator_dev *rdev = dev_get_drvdata(dev);
739 return regulator_print_state(buf,
740 rdev->constraints->state_standby.enabled);
742 static DEVICE_ATTR(suspend_standby_state, 0444,
743 regulator_suspend_standby_state_show, NULL);
745 static ssize_t regulator_bypass_show(struct device *dev,
746 struct device_attribute *attr, char *buf)
748 struct regulator_dev *rdev = dev_get_drvdata(dev);
753 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
762 return sprintf(buf, "%s\n", report);
764 static DEVICE_ATTR(bypass, 0444,
765 regulator_bypass_show, NULL);
767 /* Calculate the new optimum regulator operating mode based on the new total
768 * consumer load. All locks held by caller */
769 static int drms_uA_update(struct regulator_dev *rdev)
771 struct regulator *sibling;
772 int current_uA = 0, output_uV, input_uV, err;
775 lockdep_assert_held_once(&rdev->mutex);
778 * first check to see if we can set modes at all, otherwise just
779 * tell the consumer everything is OK.
781 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
784 if (!rdev->desc->ops->get_optimum_mode &&
785 !rdev->desc->ops->set_load)
788 if (!rdev->desc->ops->set_mode &&
789 !rdev->desc->ops->set_load)
792 /* calc total requested load */
793 list_for_each_entry(sibling, &rdev->consumer_list, list)
794 current_uA += sibling->uA_load;
796 current_uA += rdev->constraints->system_load;
798 if (rdev->desc->ops->set_load) {
799 /* set the optimum mode for our new total regulator load */
800 err = rdev->desc->ops->set_load(rdev, current_uA);
802 rdev_err(rdev, "failed to set load %d\n", current_uA);
804 /* get output voltage */
805 output_uV = _regulator_get_voltage(rdev);
806 if (output_uV <= 0) {
807 rdev_err(rdev, "invalid output voltage found\n");
811 /* get input voltage */
814 input_uV = regulator_get_voltage(rdev->supply);
816 input_uV = rdev->constraints->input_uV;
818 rdev_err(rdev, "invalid input voltage found\n");
822 /* now get the optimum mode for our new total regulator load */
823 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
824 output_uV, current_uA);
826 /* check the new mode is allowed */
827 err = regulator_mode_constrain(rdev, &mode);
829 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
830 current_uA, input_uV, output_uV);
834 err = rdev->desc->ops->set_mode(rdev, mode);
836 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
842 static int suspend_set_state(struct regulator_dev *rdev,
843 suspend_state_t state)
846 struct regulator_state *rstate;
848 rstate = regulator_get_suspend_state(rdev, state);
852 /* If we have no suspend mode configration don't set anything;
853 * only warn if the driver implements set_suspend_voltage or
854 * set_suspend_mode callback.
856 if (rstate->enabled != ENABLE_IN_SUSPEND &&
857 rstate->enabled != DISABLE_IN_SUSPEND) {
858 if (rdev->desc->ops->set_suspend_voltage ||
859 rdev->desc->ops->set_suspend_mode)
860 rdev_warn(rdev, "No configuration\n");
864 if (rstate->enabled == ENABLE_IN_SUSPEND &&
865 rdev->desc->ops->set_suspend_enable)
866 ret = rdev->desc->ops->set_suspend_enable(rdev);
867 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
868 rdev->desc->ops->set_suspend_disable)
869 ret = rdev->desc->ops->set_suspend_disable(rdev);
870 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
874 rdev_err(rdev, "failed to enabled/disable\n");
878 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
879 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
881 rdev_err(rdev, "failed to set voltage\n");
886 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
887 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
889 rdev_err(rdev, "failed to set mode\n");
897 static void print_constraints(struct regulator_dev *rdev)
899 struct regulation_constraints *constraints = rdev->constraints;
901 size_t len = sizeof(buf) - 1;
905 if (constraints->min_uV && constraints->max_uV) {
906 if (constraints->min_uV == constraints->max_uV)
907 count += scnprintf(buf + count, len - count, "%d mV ",
908 constraints->min_uV / 1000);
910 count += scnprintf(buf + count, len - count,
912 constraints->min_uV / 1000,
913 constraints->max_uV / 1000);
916 if (!constraints->min_uV ||
917 constraints->min_uV != constraints->max_uV) {
918 ret = _regulator_get_voltage(rdev);
920 count += scnprintf(buf + count, len - count,
921 "at %d mV ", ret / 1000);
924 if (constraints->uV_offset)
925 count += scnprintf(buf + count, len - count, "%dmV offset ",
926 constraints->uV_offset / 1000);
928 if (constraints->min_uA && constraints->max_uA) {
929 if (constraints->min_uA == constraints->max_uA)
930 count += scnprintf(buf + count, len - count, "%d mA ",
931 constraints->min_uA / 1000);
933 count += scnprintf(buf + count, len - count,
935 constraints->min_uA / 1000,
936 constraints->max_uA / 1000);
939 if (!constraints->min_uA ||
940 constraints->min_uA != constraints->max_uA) {
941 ret = _regulator_get_current_limit(rdev);
943 count += scnprintf(buf + count, len - count,
944 "at %d mA ", ret / 1000);
947 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
948 count += scnprintf(buf + count, len - count, "fast ");
949 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
950 count += scnprintf(buf + count, len - count, "normal ");
951 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
952 count += scnprintf(buf + count, len - count, "idle ");
953 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
954 count += scnprintf(buf + count, len - count, "standby");
957 scnprintf(buf, len, "no parameters");
959 rdev_dbg(rdev, "%s\n", buf);
961 if ((constraints->min_uV != constraints->max_uV) &&
962 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
964 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
967 static int machine_constraints_voltage(struct regulator_dev *rdev,
968 struct regulation_constraints *constraints)
970 const struct regulator_ops *ops = rdev->desc->ops;
973 /* do we need to apply the constraint voltage */
974 if (rdev->constraints->apply_uV &&
975 rdev->constraints->min_uV && rdev->constraints->max_uV) {
976 int target_min, target_max;
977 int current_uV = _regulator_get_voltage(rdev);
979 if (current_uV == -ENOTRECOVERABLE) {
980 /* This regulator can't be read and must be initted */
981 rdev_info(rdev, "Setting %d-%duV\n",
982 rdev->constraints->min_uV,
983 rdev->constraints->max_uV);
984 _regulator_do_set_voltage(rdev,
985 rdev->constraints->min_uV,
986 rdev->constraints->max_uV);
987 current_uV = _regulator_get_voltage(rdev);
990 if (current_uV < 0) {
992 "failed to get the current voltage(%d)\n",
998 * If we're below the minimum voltage move up to the
999 * minimum voltage, if we're above the maximum voltage
1000 * then move down to the maximum.
1002 target_min = current_uV;
1003 target_max = current_uV;
1005 if (current_uV < rdev->constraints->min_uV) {
1006 target_min = rdev->constraints->min_uV;
1007 target_max = rdev->constraints->min_uV;
1010 if (current_uV > rdev->constraints->max_uV) {
1011 target_min = rdev->constraints->max_uV;
1012 target_max = rdev->constraints->max_uV;
1015 if (target_min != current_uV || target_max != current_uV) {
1016 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1017 current_uV, target_min, target_max);
1018 ret = _regulator_do_set_voltage(
1019 rdev, target_min, target_max);
1022 "failed to apply %d-%duV constraint(%d)\n",
1023 target_min, target_max, ret);
1029 /* constrain machine-level voltage specs to fit
1030 * the actual range supported by this regulator.
1032 if (ops->list_voltage && rdev->desc->n_voltages) {
1033 int count = rdev->desc->n_voltages;
1035 int min_uV = INT_MAX;
1036 int max_uV = INT_MIN;
1037 int cmin = constraints->min_uV;
1038 int cmax = constraints->max_uV;
1040 /* it's safe to autoconfigure fixed-voltage supplies
1041 and the constraints are used by list_voltage. */
1042 if (count == 1 && !cmin) {
1045 constraints->min_uV = cmin;
1046 constraints->max_uV = cmax;
1049 /* voltage constraints are optional */
1050 if ((cmin == 0) && (cmax == 0))
1053 /* else require explicit machine-level constraints */
1054 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1055 rdev_err(rdev, "invalid voltage constraints\n");
1059 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1060 for (i = 0; i < count; i++) {
1063 value = ops->list_voltage(rdev, i);
1067 /* maybe adjust [min_uV..max_uV] */
1068 if (value >= cmin && value < min_uV)
1070 if (value <= cmax && value > max_uV)
1074 /* final: [min_uV..max_uV] valid iff constraints valid */
1075 if (max_uV < min_uV) {
1077 "unsupportable voltage constraints %u-%uuV\n",
1082 /* use regulator's subset of machine constraints */
1083 if (constraints->min_uV < min_uV) {
1084 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1085 constraints->min_uV, min_uV);
1086 constraints->min_uV = min_uV;
1088 if (constraints->max_uV > max_uV) {
1089 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1090 constraints->max_uV, max_uV);
1091 constraints->max_uV = max_uV;
1098 static int machine_constraints_current(struct regulator_dev *rdev,
1099 struct regulation_constraints *constraints)
1101 const struct regulator_ops *ops = rdev->desc->ops;
1104 if (!constraints->min_uA && !constraints->max_uA)
1107 if (constraints->min_uA > constraints->max_uA) {
1108 rdev_err(rdev, "Invalid current constraints\n");
1112 if (!ops->set_current_limit || !ops->get_current_limit) {
1113 rdev_warn(rdev, "Operation of current configuration missing\n");
1117 /* Set regulator current in constraints range */
1118 ret = ops->set_current_limit(rdev, constraints->min_uA,
1119 constraints->max_uA);
1121 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1128 static int _regulator_do_enable(struct regulator_dev *rdev);
1131 * set_machine_constraints - sets regulator constraints
1132 * @rdev: regulator source
1133 * @constraints: constraints to apply
1135 * Allows platform initialisation code to define and constrain
1136 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1137 * Constraints *must* be set by platform code in order for some
1138 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1141 static int set_machine_constraints(struct regulator_dev *rdev,
1142 const struct regulation_constraints *constraints)
1145 const struct regulator_ops *ops = rdev->desc->ops;
1148 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1151 rdev->constraints = kzalloc(sizeof(*constraints),
1153 if (!rdev->constraints)
1156 ret = machine_constraints_voltage(rdev, rdev->constraints);
1160 ret = machine_constraints_current(rdev, rdev->constraints);
1164 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1165 ret = ops->set_input_current_limit(rdev,
1166 rdev->constraints->ilim_uA);
1168 rdev_err(rdev, "failed to set input limit\n");
1173 /* do we need to setup our suspend state */
1174 if (rdev->constraints->initial_state) {
1175 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1177 rdev_err(rdev, "failed to set suspend state\n");
1182 if (rdev->constraints->initial_mode) {
1183 if (!ops->set_mode) {
1184 rdev_err(rdev, "no set_mode operation\n");
1188 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1190 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1195 /* If the constraints say the regulator should be on at this point
1196 * and we have control then make sure it is enabled.
1198 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1199 ret = _regulator_do_enable(rdev);
1200 if (ret < 0 && ret != -EINVAL) {
1201 rdev_err(rdev, "failed to enable\n");
1206 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1207 && ops->set_ramp_delay) {
1208 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1210 rdev_err(rdev, "failed to set ramp_delay\n");
1215 if (rdev->constraints->pull_down && ops->set_pull_down) {
1216 ret = ops->set_pull_down(rdev);
1218 rdev_err(rdev, "failed to set pull down\n");
1223 if (rdev->constraints->soft_start && ops->set_soft_start) {
1224 ret = ops->set_soft_start(rdev);
1226 rdev_err(rdev, "failed to set soft start\n");
1231 if (rdev->constraints->over_current_protection
1232 && ops->set_over_current_protection) {
1233 ret = ops->set_over_current_protection(rdev);
1235 rdev_err(rdev, "failed to set over current protection\n");
1240 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1241 bool ad_state = (rdev->constraints->active_discharge ==
1242 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1244 ret = ops->set_active_discharge(rdev, ad_state);
1246 rdev_err(rdev, "failed to set active discharge\n");
1251 print_constraints(rdev);
1256 * set_supply - set regulator supply regulator
1257 * @rdev: regulator name
1258 * @supply_rdev: supply regulator name
1260 * Called by platform initialisation code to set the supply regulator for this
1261 * regulator. This ensures that a regulators supply will also be enabled by the
1262 * core if it's child is enabled.
1264 static int set_supply(struct regulator_dev *rdev,
1265 struct regulator_dev *supply_rdev)
1269 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1271 if (!try_module_get(supply_rdev->owner))
1274 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1275 if (rdev->supply == NULL) {
1279 supply_rdev->open_count++;
1285 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1286 * @rdev: regulator source
1287 * @consumer_dev_name: dev_name() string for device supply applies to
1288 * @supply: symbolic name for supply
1290 * Allows platform initialisation code to map physical regulator
1291 * sources to symbolic names for supplies for use by devices. Devices
1292 * should use these symbolic names to request regulators, avoiding the
1293 * need to provide board-specific regulator names as platform data.
1295 static int set_consumer_device_supply(struct regulator_dev *rdev,
1296 const char *consumer_dev_name,
1299 struct regulator_map *node;
1305 if (consumer_dev_name != NULL)
1310 list_for_each_entry(node, ®ulator_map_list, list) {
1311 if (node->dev_name && consumer_dev_name) {
1312 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1314 } else if (node->dev_name || consumer_dev_name) {
1318 if (strcmp(node->supply, supply) != 0)
1321 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1323 dev_name(&node->regulator->dev),
1324 node->regulator->desc->name,
1326 dev_name(&rdev->dev), rdev_get_name(rdev));
1330 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1334 node->regulator = rdev;
1335 node->supply = supply;
1338 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1339 if (node->dev_name == NULL) {
1345 list_add(&node->list, ®ulator_map_list);
1349 static void unset_regulator_supplies(struct regulator_dev *rdev)
1351 struct regulator_map *node, *n;
1353 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1354 if (rdev == node->regulator) {
1355 list_del(&node->list);
1356 kfree(node->dev_name);
1362 #ifdef CONFIG_DEBUG_FS
1363 static ssize_t constraint_flags_read_file(struct file *file,
1364 char __user *user_buf,
1365 size_t count, loff_t *ppos)
1367 const struct regulator *regulator = file->private_data;
1368 const struct regulation_constraints *c = regulator->rdev->constraints;
1375 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1379 ret = snprintf(buf, PAGE_SIZE,
1383 "ramp_disable: %u\n"
1386 "over_current_protection: %u\n",
1393 c->over_current_protection);
1395 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1403 static const struct file_operations constraint_flags_fops = {
1404 #ifdef CONFIG_DEBUG_FS
1405 .open = simple_open,
1406 .read = constraint_flags_read_file,
1407 .llseek = default_llseek,
1411 #define REG_STR_SIZE 64
1413 static struct regulator *create_regulator(struct regulator_dev *rdev,
1415 const char *supply_name)
1417 struct regulator *regulator;
1418 char buf[REG_STR_SIZE];
1421 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1422 if (regulator == NULL)
1425 regulator_lock(rdev);
1426 regulator->rdev = rdev;
1427 list_add(®ulator->list, &rdev->consumer_list);
1430 regulator->dev = dev;
1432 /* Add a link to the device sysfs entry */
1433 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1434 dev->kobj.name, supply_name);
1435 if (size >= REG_STR_SIZE)
1438 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1439 if (regulator->supply_name == NULL)
1442 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1445 rdev_dbg(rdev, "could not add device link %s err %d\n",
1446 dev->kobj.name, err);
1450 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1451 if (regulator->supply_name == NULL)
1455 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1457 if (!regulator->debugfs) {
1458 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1460 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1461 ®ulator->uA_load);
1462 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1463 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1464 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1465 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1466 debugfs_create_file("constraint_flags", 0444,
1467 regulator->debugfs, regulator,
1468 &constraint_flags_fops);
1472 * Check now if the regulator is an always on regulator - if
1473 * it is then we don't need to do nearly so much work for
1474 * enable/disable calls.
1476 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1477 _regulator_is_enabled(rdev))
1478 regulator->always_on = true;
1480 regulator_unlock(rdev);
1483 list_del(®ulator->list);
1485 regulator_unlock(rdev);
1489 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1491 if (rdev->constraints && rdev->constraints->enable_time)
1492 return rdev->constraints->enable_time;
1493 if (!rdev->desc->ops->enable_time)
1494 return rdev->desc->enable_time;
1495 return rdev->desc->ops->enable_time(rdev);
1498 static struct regulator_supply_alias *regulator_find_supply_alias(
1499 struct device *dev, const char *supply)
1501 struct regulator_supply_alias *map;
1503 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1504 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1510 static void regulator_supply_alias(struct device **dev, const char **supply)
1512 struct regulator_supply_alias *map;
1514 map = regulator_find_supply_alias(*dev, *supply);
1516 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1517 *supply, map->alias_supply,
1518 dev_name(map->alias_dev));
1519 *dev = map->alias_dev;
1520 *supply = map->alias_supply;
1524 static int regulator_match(struct device *dev, const void *data)
1526 struct regulator_dev *r = dev_to_rdev(dev);
1528 return strcmp(rdev_get_name(r), data) == 0;
1531 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1535 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1537 return dev ? dev_to_rdev(dev) : NULL;
1541 * regulator_dev_lookup - lookup a regulator device.
1542 * @dev: device for regulator "consumer".
1543 * @supply: Supply name or regulator ID.
1545 * If successful, returns a struct regulator_dev that corresponds to the name
1546 * @supply and with the embedded struct device refcount incremented by one.
1547 * The refcount must be dropped by calling put_device().
1548 * On failure one of the following ERR-PTR-encoded values is returned:
1549 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1552 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1555 struct regulator_dev *r = NULL;
1556 struct device_node *node;
1557 struct regulator_map *map;
1558 const char *devname = NULL;
1560 regulator_supply_alias(&dev, &supply);
1562 /* first do a dt based lookup */
1563 if (dev && dev->of_node) {
1564 node = of_get_regulator(dev, supply);
1566 r = of_find_regulator_by_node(node);
1571 * We have a node, but there is no device.
1572 * assume it has not registered yet.
1574 return ERR_PTR(-EPROBE_DEFER);
1578 /* if not found, try doing it non-dt way */
1580 devname = dev_name(dev);
1582 mutex_lock(®ulator_list_mutex);
1583 list_for_each_entry(map, ®ulator_map_list, list) {
1584 /* If the mapping has a device set up it must match */
1585 if (map->dev_name &&
1586 (!devname || strcmp(map->dev_name, devname)))
1589 if (strcmp(map->supply, supply) == 0 &&
1590 get_device(&map->regulator->dev)) {
1595 mutex_unlock(®ulator_list_mutex);
1600 r = regulator_lookup_by_name(supply);
1604 return ERR_PTR(-ENODEV);
1607 static int regulator_resolve_supply(struct regulator_dev *rdev)
1609 struct regulator_dev *r;
1610 struct device *dev = rdev->dev.parent;
1613 /* No supply to resovle? */
1614 if (!rdev->supply_name)
1617 /* Supply already resolved? */
1621 r = regulator_dev_lookup(dev, rdev->supply_name);
1625 /* Did the lookup explicitly defer for us? */
1626 if (ret == -EPROBE_DEFER)
1629 if (have_full_constraints()) {
1630 r = dummy_regulator_rdev;
1631 get_device(&r->dev);
1633 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1634 rdev->supply_name, rdev->desc->name);
1635 return -EPROBE_DEFER;
1640 * If the supply's parent device is not the same as the
1641 * regulator's parent device, then ensure the parent device
1642 * is bound before we resolve the supply, in case the parent
1643 * device get probe deferred and unregisters the supply.
1645 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1646 if (!device_is_bound(r->dev.parent)) {
1647 put_device(&r->dev);
1648 return -EPROBE_DEFER;
1652 /* Recursively resolve the supply of the supply */
1653 ret = regulator_resolve_supply(r);
1655 put_device(&r->dev);
1659 ret = set_supply(rdev, r);
1661 put_device(&r->dev);
1665 /* Cascade always-on state to supply */
1666 if (_regulator_is_enabled(rdev)) {
1667 ret = regulator_enable(rdev->supply);
1669 _regulator_put(rdev->supply);
1670 rdev->supply = NULL;
1678 /* Internal regulator request function */
1679 struct regulator *_regulator_get(struct device *dev, const char *id,
1680 enum regulator_get_type get_type)
1682 struct regulator_dev *rdev;
1683 struct regulator *regulator;
1684 const char *devname = dev ? dev_name(dev) : "deviceless";
1687 if (get_type >= MAX_GET_TYPE) {
1688 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1689 return ERR_PTR(-EINVAL);
1693 pr_err("get() with no identifier\n");
1694 return ERR_PTR(-EINVAL);
1697 rdev = regulator_dev_lookup(dev, id);
1699 ret = PTR_ERR(rdev);
1702 * If regulator_dev_lookup() fails with error other
1703 * than -ENODEV our job here is done, we simply return it.
1706 return ERR_PTR(ret);
1708 if (!have_full_constraints()) {
1710 "incomplete constraints, dummy supplies not allowed\n");
1711 return ERR_PTR(-ENODEV);
1717 * Assume that a regulator is physically present and
1718 * enabled, even if it isn't hooked up, and just
1722 "%s supply %s not found, using dummy regulator\n",
1724 rdev = dummy_regulator_rdev;
1725 get_device(&rdev->dev);
1730 "dummy supplies not allowed for exclusive requests\n");
1734 return ERR_PTR(-ENODEV);
1738 if (rdev->exclusive) {
1739 regulator = ERR_PTR(-EPERM);
1740 put_device(&rdev->dev);
1744 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1745 regulator = ERR_PTR(-EBUSY);
1746 put_device(&rdev->dev);
1750 ret = regulator_resolve_supply(rdev);
1752 regulator = ERR_PTR(ret);
1753 put_device(&rdev->dev);
1757 if (!try_module_get(rdev->owner)) {
1758 regulator = ERR_PTR(-EPROBE_DEFER);
1759 put_device(&rdev->dev);
1763 regulator = create_regulator(rdev, dev, id);
1764 if (regulator == NULL) {
1765 regulator = ERR_PTR(-ENOMEM);
1766 put_device(&rdev->dev);
1767 module_put(rdev->owner);
1772 if (get_type == EXCLUSIVE_GET) {
1773 rdev->exclusive = 1;
1775 ret = _regulator_is_enabled(rdev);
1777 rdev->use_count = 1;
1779 rdev->use_count = 0;
1782 device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1788 * regulator_get - lookup and obtain a reference to a regulator.
1789 * @dev: device for regulator "consumer"
1790 * @id: Supply name or regulator ID.
1792 * Returns a struct regulator corresponding to the regulator producer,
1793 * or IS_ERR() condition containing errno.
1795 * Use of supply names configured via regulator_set_device_supply() is
1796 * strongly encouraged. It is recommended that the supply name used
1797 * should match the name used for the supply and/or the relevant
1798 * device pins in the datasheet.
1800 struct regulator *regulator_get(struct device *dev, const char *id)
1802 return _regulator_get(dev, id, NORMAL_GET);
1804 EXPORT_SYMBOL_GPL(regulator_get);
1807 * regulator_get_exclusive - obtain exclusive access to a regulator.
1808 * @dev: device for regulator "consumer"
1809 * @id: Supply name or regulator ID.
1811 * Returns a struct regulator corresponding to the regulator producer,
1812 * or IS_ERR() condition containing errno. Other consumers will be
1813 * unable to obtain this regulator while this reference is held and the
1814 * use count for the regulator will be initialised to reflect the current
1815 * state of the regulator.
1817 * This is intended for use by consumers which cannot tolerate shared
1818 * use of the regulator such as those which need to force the
1819 * regulator off for correct operation of the hardware they are
1822 * Use of supply names configured via regulator_set_device_supply() is
1823 * strongly encouraged. It is recommended that the supply name used
1824 * should match the name used for the supply and/or the relevant
1825 * device pins in the datasheet.
1827 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1829 return _regulator_get(dev, id, EXCLUSIVE_GET);
1831 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1834 * regulator_get_optional - obtain optional access to a regulator.
1835 * @dev: device for regulator "consumer"
1836 * @id: Supply name or regulator ID.
1838 * Returns a struct regulator corresponding to the regulator producer,
1839 * or IS_ERR() condition containing errno.
1841 * This is intended for use by consumers for devices which can have
1842 * some supplies unconnected in normal use, such as some MMC devices.
1843 * It can allow the regulator core to provide stub supplies for other
1844 * supplies requested using normal regulator_get() calls without
1845 * disrupting the operation of drivers that can handle absent
1848 * Use of supply names configured via regulator_set_device_supply() is
1849 * strongly encouraged. It is recommended that the supply name used
1850 * should match the name used for the supply and/or the relevant
1851 * device pins in the datasheet.
1853 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1855 return _regulator_get(dev, id, OPTIONAL_GET);
1857 EXPORT_SYMBOL_GPL(regulator_get_optional);
1859 /* regulator_list_mutex lock held by regulator_put() */
1860 static void _regulator_put(struct regulator *regulator)
1862 struct regulator_dev *rdev;
1864 if (IS_ERR_OR_NULL(regulator))
1867 lockdep_assert_held_once(®ulator_list_mutex);
1869 rdev = regulator->rdev;
1871 debugfs_remove_recursive(regulator->debugfs);
1873 if (regulator->dev) {
1875 struct regulator *r;
1877 list_for_each_entry(r, &rdev->consumer_list, list)
1878 if (r->dev == regulator->dev)
1882 device_link_remove(regulator->dev, &rdev->dev);
1884 /* remove any sysfs entries */
1885 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1888 regulator_lock(rdev);
1889 list_del(®ulator->list);
1892 rdev->exclusive = 0;
1893 put_device(&rdev->dev);
1894 regulator_unlock(rdev);
1896 kfree_const(regulator->supply_name);
1899 module_put(rdev->owner);
1903 * regulator_put - "free" the regulator source
1904 * @regulator: regulator source
1906 * Note: drivers must ensure that all regulator_enable calls made on this
1907 * regulator source are balanced by regulator_disable calls prior to calling
1910 void regulator_put(struct regulator *regulator)
1912 mutex_lock(®ulator_list_mutex);
1913 _regulator_put(regulator);
1914 mutex_unlock(®ulator_list_mutex);
1916 EXPORT_SYMBOL_GPL(regulator_put);
1919 * regulator_register_supply_alias - Provide device alias for supply lookup
1921 * @dev: device that will be given as the regulator "consumer"
1922 * @id: Supply name or regulator ID
1923 * @alias_dev: device that should be used to lookup the supply
1924 * @alias_id: Supply name or regulator ID that should be used to lookup the
1927 * All lookups for id on dev will instead be conducted for alias_id on
1930 int regulator_register_supply_alias(struct device *dev, const char *id,
1931 struct device *alias_dev,
1932 const char *alias_id)
1934 struct regulator_supply_alias *map;
1936 map = regulator_find_supply_alias(dev, id);
1940 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1945 map->src_supply = id;
1946 map->alias_dev = alias_dev;
1947 map->alias_supply = alias_id;
1949 list_add(&map->list, ®ulator_supply_alias_list);
1951 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1952 id, dev_name(dev), alias_id, dev_name(alias_dev));
1956 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1959 * regulator_unregister_supply_alias - Remove device alias
1961 * @dev: device that will be given as the regulator "consumer"
1962 * @id: Supply name or regulator ID
1964 * Remove a lookup alias if one exists for id on dev.
1966 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1968 struct regulator_supply_alias *map;
1970 map = regulator_find_supply_alias(dev, id);
1972 list_del(&map->list);
1976 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1979 * regulator_bulk_register_supply_alias - register multiple aliases
1981 * @dev: device that will be given as the regulator "consumer"
1982 * @id: List of supply names or regulator IDs
1983 * @alias_dev: device that should be used to lookup the supply
1984 * @alias_id: List of supply names or regulator IDs that should be used to
1986 * @num_id: Number of aliases to register
1988 * @return 0 on success, an errno on failure.
1990 * This helper function allows drivers to register several supply
1991 * aliases in one operation. If any of the aliases cannot be
1992 * registered any aliases that were registered will be removed
1993 * before returning to the caller.
1995 int regulator_bulk_register_supply_alias(struct device *dev,
1996 const char *const *id,
1997 struct device *alias_dev,
1998 const char *const *alias_id,
2004 for (i = 0; i < num_id; ++i) {
2005 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2015 "Failed to create supply alias %s,%s -> %s,%s\n",
2016 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2019 regulator_unregister_supply_alias(dev, id[i]);
2023 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2026 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2028 * @dev: device that will be given as the regulator "consumer"
2029 * @id: List of supply names or regulator IDs
2030 * @num_id: Number of aliases to unregister
2032 * This helper function allows drivers to unregister several supply
2033 * aliases in one operation.
2035 void regulator_bulk_unregister_supply_alias(struct device *dev,
2036 const char *const *id,
2041 for (i = 0; i < num_id; ++i)
2042 regulator_unregister_supply_alias(dev, id[i]);
2044 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2047 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2048 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2049 const struct regulator_config *config)
2051 struct regulator_enable_gpio *pin;
2052 struct gpio_desc *gpiod;
2055 if (config->ena_gpiod)
2056 gpiod = config->ena_gpiod;
2058 gpiod = gpio_to_desc(config->ena_gpio);
2060 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2061 if (pin->gpiod == gpiod) {
2062 rdev_dbg(rdev, "GPIO %d is already used\n",
2064 goto update_ena_gpio_to_rdev;
2068 if (!config->ena_gpiod) {
2069 ret = gpio_request_one(config->ena_gpio,
2070 GPIOF_DIR_OUT | config->ena_gpio_flags,
2071 rdev_get_name(rdev));
2076 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2078 if (!config->ena_gpiod)
2079 gpio_free(config->ena_gpio);
2084 pin->ena_gpio_invert = config->ena_gpio_invert;
2085 list_add(&pin->list, ®ulator_ena_gpio_list);
2087 update_ena_gpio_to_rdev:
2088 pin->request_count++;
2089 rdev->ena_pin = pin;
2093 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2095 struct regulator_enable_gpio *pin, *n;
2100 /* Free the GPIO only in case of no use */
2101 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2102 if (pin->gpiod == rdev->ena_pin->gpiod) {
2103 if (pin->request_count <= 1) {
2104 pin->request_count = 0;
2105 gpiod_put(pin->gpiod);
2106 list_del(&pin->list);
2108 rdev->ena_pin = NULL;
2111 pin->request_count--;
2118 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2119 * @rdev: regulator_dev structure
2120 * @enable: enable GPIO at initial use?
2122 * GPIO is enabled in case of initial use. (enable_count is 0)
2123 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2125 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2127 struct regulator_enable_gpio *pin = rdev->ena_pin;
2133 /* Enable GPIO at initial use */
2134 if (pin->enable_count == 0)
2135 gpiod_set_value_cansleep(pin->gpiod,
2136 !pin->ena_gpio_invert);
2138 pin->enable_count++;
2140 if (pin->enable_count > 1) {
2141 pin->enable_count--;
2145 /* Disable GPIO if not used */
2146 if (pin->enable_count <= 1) {
2147 gpiod_set_value_cansleep(pin->gpiod,
2148 pin->ena_gpio_invert);
2149 pin->enable_count = 0;
2157 * _regulator_enable_delay - a delay helper function
2158 * @delay: time to delay in microseconds
2160 * Delay for the requested amount of time as per the guidelines in:
2162 * Documentation/timers/timers-howto.txt
2164 * The assumption here is that regulators will never be enabled in
2165 * atomic context and therefore sleeping functions can be used.
2167 static void _regulator_enable_delay(unsigned int delay)
2169 unsigned int ms = delay / 1000;
2170 unsigned int us = delay % 1000;
2174 * For small enough values, handle super-millisecond
2175 * delays in the usleep_range() call below.
2184 * Give the scheduler some room to coalesce with any other
2185 * wakeup sources. For delays shorter than 10 us, don't even
2186 * bother setting up high-resolution timers and just busy-
2190 usleep_range(us, us + 100);
2195 static int _regulator_do_enable(struct regulator_dev *rdev)
2199 /* Query before enabling in case configuration dependent. */
2200 ret = _regulator_get_enable_time(rdev);
2204 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2208 trace_regulator_enable(rdev_get_name(rdev));
2210 if (rdev->desc->off_on_delay) {
2211 /* if needed, keep a distance of off_on_delay from last time
2212 * this regulator was disabled.
2214 unsigned long start_jiffy = jiffies;
2215 unsigned long intended, max_delay, remaining;
2217 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2218 intended = rdev->last_off_jiffy + max_delay;
2220 if (time_before(start_jiffy, intended)) {
2221 /* calc remaining jiffies to deal with one-time
2223 * in case of multiple timer wrapping, either it can be
2224 * detected by out-of-range remaining, or it cannot be
2225 * detected and we gets a panelty of
2226 * _regulator_enable_delay().
2228 remaining = intended - start_jiffy;
2229 if (remaining <= max_delay)
2230 _regulator_enable_delay(
2231 jiffies_to_usecs(remaining));
2235 if (rdev->ena_pin) {
2236 if (!rdev->ena_gpio_state) {
2237 ret = regulator_ena_gpio_ctrl(rdev, true);
2240 rdev->ena_gpio_state = 1;
2242 } else if (rdev->desc->ops->enable) {
2243 ret = rdev->desc->ops->enable(rdev);
2250 /* Allow the regulator to ramp; it would be useful to extend
2251 * this for bulk operations so that the regulators can ramp
2253 trace_regulator_enable_delay(rdev_get_name(rdev));
2255 _regulator_enable_delay(delay);
2257 trace_regulator_enable_complete(rdev_get_name(rdev));
2262 /* locks held by regulator_enable() */
2263 static int _regulator_enable(struct regulator_dev *rdev)
2267 lockdep_assert_held_once(&rdev->mutex);
2269 /* check voltage and requested load before enabling */
2270 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2271 drms_uA_update(rdev);
2273 if (rdev->use_count == 0) {
2274 /* The regulator may on if it's not switchable or left on */
2275 ret = _regulator_is_enabled(rdev);
2276 if (ret == -EINVAL || ret == 0) {
2277 if (!regulator_ops_is_valid(rdev,
2278 REGULATOR_CHANGE_STATUS))
2281 ret = _regulator_do_enable(rdev);
2285 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2287 } else if (ret < 0) {
2288 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2291 /* Fallthrough on positive return values - already enabled */
2300 * regulator_enable - enable regulator output
2301 * @regulator: regulator source
2303 * Request that the regulator be enabled with the regulator output at
2304 * the predefined voltage or current value. Calls to regulator_enable()
2305 * must be balanced with calls to regulator_disable().
2307 * NOTE: the output value can be set by other drivers, boot loader or may be
2308 * hardwired in the regulator.
2310 int regulator_enable(struct regulator *regulator)
2312 struct regulator_dev *rdev = regulator->rdev;
2315 if (regulator->always_on)
2319 ret = regulator_enable(rdev->supply);
2324 regulator_lock_dependent(rdev);
2325 /* balance only if there are regulators coupled */
2326 if (rdev->coupling_desc.n_coupled > 1) {
2327 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2331 ret = _regulator_enable(rdev);
2333 regulator_unlock_dependent(rdev);
2335 if (ret != 0 && rdev->supply)
2336 regulator_disable(rdev->supply);
2340 EXPORT_SYMBOL_GPL(regulator_enable);
2342 static int _regulator_do_disable(struct regulator_dev *rdev)
2346 trace_regulator_disable(rdev_get_name(rdev));
2348 if (rdev->ena_pin) {
2349 if (rdev->ena_gpio_state) {
2350 ret = regulator_ena_gpio_ctrl(rdev, false);
2353 rdev->ena_gpio_state = 0;
2356 } else if (rdev->desc->ops->disable) {
2357 ret = rdev->desc->ops->disable(rdev);
2362 /* cares about last_off_jiffy only if off_on_delay is required by
2365 if (rdev->desc->off_on_delay)
2366 rdev->last_off_jiffy = jiffies;
2368 trace_regulator_disable_complete(rdev_get_name(rdev));
2373 /* locks held by regulator_disable() */
2374 static int _regulator_disable(struct regulator_dev *rdev)
2378 lockdep_assert_held_once(&rdev->mutex);
2380 if (WARN(rdev->use_count <= 0,
2381 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2384 /* are we the last user and permitted to disable ? */
2385 if (rdev->use_count == 1 &&
2386 (rdev->constraints && !rdev->constraints->always_on)) {
2388 /* we are last user */
2389 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2390 ret = _notifier_call_chain(rdev,
2391 REGULATOR_EVENT_PRE_DISABLE,
2393 if (ret & NOTIFY_STOP_MASK)
2396 ret = _regulator_do_disable(rdev);
2398 rdev_err(rdev, "failed to disable\n");
2399 _notifier_call_chain(rdev,
2400 REGULATOR_EVENT_ABORT_DISABLE,
2404 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2408 rdev->use_count = 0;
2409 } else if (rdev->use_count > 1) {
2410 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2411 drms_uA_update(rdev);
2420 * regulator_disable - disable regulator output
2421 * @regulator: regulator source
2423 * Disable the regulator output voltage or current. Calls to
2424 * regulator_enable() must be balanced with calls to
2425 * regulator_disable().
2427 * NOTE: this will only disable the regulator output if no other consumer
2428 * devices have it enabled, the regulator device supports disabling and
2429 * machine constraints permit this operation.
2431 int regulator_disable(struct regulator *regulator)
2433 struct regulator_dev *rdev = regulator->rdev;
2436 if (regulator->always_on)
2439 regulator_lock_dependent(rdev);
2440 ret = _regulator_disable(rdev);
2441 if (rdev->coupling_desc.n_coupled > 1)
2442 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2443 regulator_unlock_dependent(rdev);
2445 if (ret == 0 && rdev->supply)
2446 regulator_disable(rdev->supply);
2450 EXPORT_SYMBOL_GPL(regulator_disable);
2452 /* locks held by regulator_force_disable() */
2453 static int _regulator_force_disable(struct regulator_dev *rdev)
2457 lockdep_assert_held_once(&rdev->mutex);
2459 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2460 REGULATOR_EVENT_PRE_DISABLE, NULL);
2461 if (ret & NOTIFY_STOP_MASK)
2464 ret = _regulator_do_disable(rdev);
2466 rdev_err(rdev, "failed to force disable\n");
2467 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2468 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2472 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2473 REGULATOR_EVENT_DISABLE, NULL);
2479 * regulator_force_disable - force disable regulator output
2480 * @regulator: regulator source
2482 * Forcibly disable the regulator output voltage or current.
2483 * NOTE: this *will* disable the regulator output even if other consumer
2484 * devices have it enabled. This should be used for situations when device
2485 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2487 int regulator_force_disable(struct regulator *regulator)
2489 struct regulator_dev *rdev = regulator->rdev;
2492 regulator_lock_dependent(rdev);
2493 regulator->uA_load = 0;
2494 ret = _regulator_force_disable(regulator->rdev);
2495 if (rdev->coupling_desc.n_coupled > 1)
2496 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2497 regulator_unlock_dependent(rdev);
2500 while (rdev->open_count--)
2501 regulator_disable(rdev->supply);
2505 EXPORT_SYMBOL_GPL(regulator_force_disable);
2507 static void regulator_disable_work(struct work_struct *work)
2509 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2513 regulator_lock(rdev);
2515 BUG_ON(!rdev->deferred_disables);
2517 count = rdev->deferred_disables;
2518 rdev->deferred_disables = 0;
2521 * Workqueue functions queue the new work instance while the previous
2522 * work instance is being processed. Cancel the queued work instance
2523 * as the work instance under processing does the job of the queued
2526 cancel_delayed_work(&rdev->disable_work);
2528 for (i = 0; i < count; i++) {
2529 ret = _regulator_disable(rdev);
2531 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2534 regulator_unlock(rdev);
2537 for (i = 0; i < count; i++) {
2538 ret = regulator_disable(rdev->supply);
2541 "Supply disable failed: %d\n", ret);
2548 * regulator_disable_deferred - disable regulator output with delay
2549 * @regulator: regulator source
2550 * @ms: miliseconds until the regulator is disabled
2552 * Execute regulator_disable() on the regulator after a delay. This
2553 * is intended for use with devices that require some time to quiesce.
2555 * NOTE: this will only disable the regulator output if no other consumer
2556 * devices have it enabled, the regulator device supports disabling and
2557 * machine constraints permit this operation.
2559 int regulator_disable_deferred(struct regulator *regulator, int ms)
2561 struct regulator_dev *rdev = regulator->rdev;
2563 if (regulator->always_on)
2567 return regulator_disable(regulator);
2569 regulator_lock(rdev);
2570 rdev->deferred_disables++;
2571 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2572 msecs_to_jiffies(ms));
2573 regulator_unlock(rdev);
2577 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2579 static int _regulator_is_enabled(struct regulator_dev *rdev)
2581 /* A GPIO control always takes precedence */
2583 return rdev->ena_gpio_state;
2585 /* If we don't know then assume that the regulator is always on */
2586 if (!rdev->desc->ops->is_enabled)
2589 return rdev->desc->ops->is_enabled(rdev);
2592 static int _regulator_list_voltage(struct regulator_dev *rdev,
2593 unsigned selector, int lock)
2595 const struct regulator_ops *ops = rdev->desc->ops;
2598 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2599 return rdev->desc->fixed_uV;
2601 if (ops->list_voltage) {
2602 if (selector >= rdev->desc->n_voltages)
2605 regulator_lock(rdev);
2606 ret = ops->list_voltage(rdev, selector);
2608 regulator_unlock(rdev);
2609 } else if (rdev->is_switch && rdev->supply) {
2610 ret = _regulator_list_voltage(rdev->supply->rdev,
2617 if (ret < rdev->constraints->min_uV)
2619 else if (ret > rdev->constraints->max_uV)
2627 * regulator_is_enabled - is the regulator output enabled
2628 * @regulator: regulator source
2630 * Returns positive if the regulator driver backing the source/client
2631 * has requested that the device be enabled, zero if it hasn't, else a
2632 * negative errno code.
2634 * Note that the device backing this regulator handle can have multiple
2635 * users, so it might be enabled even if regulator_enable() was never
2636 * called for this particular source.
2638 int regulator_is_enabled(struct regulator *regulator)
2642 if (regulator->always_on)
2645 regulator_lock_dependent(regulator->rdev);
2646 ret = _regulator_is_enabled(regulator->rdev);
2647 regulator_unlock_dependent(regulator->rdev);
2651 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2654 * regulator_count_voltages - count regulator_list_voltage() selectors
2655 * @regulator: regulator source
2657 * Returns number of selectors, or negative errno. Selectors are
2658 * numbered starting at zero, and typically correspond to bitfields
2659 * in hardware registers.
2661 int regulator_count_voltages(struct regulator *regulator)
2663 struct regulator_dev *rdev = regulator->rdev;
2665 if (rdev->desc->n_voltages)
2666 return rdev->desc->n_voltages;
2668 if (!rdev->is_switch || !rdev->supply)
2671 return regulator_count_voltages(rdev->supply);
2673 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2676 * regulator_list_voltage - enumerate supported voltages
2677 * @regulator: regulator source
2678 * @selector: identify voltage to list
2679 * Context: can sleep
2681 * Returns a voltage that can be passed to @regulator_set_voltage(),
2682 * zero if this selector code can't be used on this system, or a
2685 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2687 return _regulator_list_voltage(regulator->rdev, selector, 1);
2689 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2692 * regulator_get_regmap - get the regulator's register map
2693 * @regulator: regulator source
2695 * Returns the register map for the given regulator, or an ERR_PTR value
2696 * if the regulator doesn't use regmap.
2698 struct regmap *regulator_get_regmap(struct regulator *regulator)
2700 struct regmap *map = regulator->rdev->regmap;
2702 return map ? map : ERR_PTR(-EOPNOTSUPP);
2706 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2707 * @regulator: regulator source
2708 * @vsel_reg: voltage selector register, output parameter
2709 * @vsel_mask: mask for voltage selector bitfield, output parameter
2711 * Returns the hardware register offset and bitmask used for setting the
2712 * regulator voltage. This might be useful when configuring voltage-scaling
2713 * hardware or firmware that can make I2C requests behind the kernel's back,
2716 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2717 * and 0 is returned, otherwise a negative errno is returned.
2719 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2721 unsigned *vsel_mask)
2723 struct regulator_dev *rdev = regulator->rdev;
2724 const struct regulator_ops *ops = rdev->desc->ops;
2726 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2729 *vsel_reg = rdev->desc->vsel_reg;
2730 *vsel_mask = rdev->desc->vsel_mask;
2734 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2737 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2738 * @regulator: regulator source
2739 * @selector: identify voltage to list
2741 * Converts the selector to a hardware-specific voltage selector that can be
2742 * directly written to the regulator registers. The address of the voltage
2743 * register can be determined by calling @regulator_get_hardware_vsel_register.
2745 * On error a negative errno is returned.
2747 int regulator_list_hardware_vsel(struct regulator *regulator,
2750 struct regulator_dev *rdev = regulator->rdev;
2751 const struct regulator_ops *ops = rdev->desc->ops;
2753 if (selector >= rdev->desc->n_voltages)
2755 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2760 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2763 * regulator_get_linear_step - return the voltage step size between VSEL values
2764 * @regulator: regulator source
2766 * Returns the voltage step size between VSEL values for linear
2767 * regulators, or return 0 if the regulator isn't a linear regulator.
2769 unsigned int regulator_get_linear_step(struct regulator *regulator)
2771 struct regulator_dev *rdev = regulator->rdev;
2773 return rdev->desc->uV_step;
2775 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2778 * regulator_is_supported_voltage - check if a voltage range can be supported
2780 * @regulator: Regulator to check.
2781 * @min_uV: Minimum required voltage in uV.
2782 * @max_uV: Maximum required voltage in uV.
2784 * Returns a boolean or a negative error code.
2786 int regulator_is_supported_voltage(struct regulator *regulator,
2787 int min_uV, int max_uV)
2789 struct regulator_dev *rdev = regulator->rdev;
2790 int i, voltages, ret;
2792 /* If we can't change voltage check the current voltage */
2793 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2794 ret = regulator_get_voltage(regulator);
2796 return min_uV <= ret && ret <= max_uV;
2801 /* Any voltage within constrains range is fine? */
2802 if (rdev->desc->continuous_voltage_range)
2803 return min_uV >= rdev->constraints->min_uV &&
2804 max_uV <= rdev->constraints->max_uV;
2806 ret = regulator_count_voltages(regulator);
2811 for (i = 0; i < voltages; i++) {
2812 ret = regulator_list_voltage(regulator, i);
2814 if (ret >= min_uV && ret <= max_uV)
2820 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2822 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2825 const struct regulator_desc *desc = rdev->desc;
2827 if (desc->ops->map_voltage)
2828 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2830 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2831 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2833 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2834 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2836 if (desc->ops->list_voltage ==
2837 regulator_list_voltage_pickable_linear_range)
2838 return regulator_map_voltage_pickable_linear_range(rdev,
2841 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2844 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2845 int min_uV, int max_uV,
2848 struct pre_voltage_change_data data;
2851 data.old_uV = _regulator_get_voltage(rdev);
2852 data.min_uV = min_uV;
2853 data.max_uV = max_uV;
2854 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2856 if (ret & NOTIFY_STOP_MASK)
2859 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2863 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2864 (void *)data.old_uV);
2869 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2870 int uV, unsigned selector)
2872 struct pre_voltage_change_data data;
2875 data.old_uV = _regulator_get_voltage(rdev);
2878 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2880 if (ret & NOTIFY_STOP_MASK)
2883 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2887 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2888 (void *)data.old_uV);
2893 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2894 int old_uV, int new_uV)
2896 unsigned int ramp_delay = 0;
2898 if (rdev->constraints->ramp_delay)
2899 ramp_delay = rdev->constraints->ramp_delay;
2900 else if (rdev->desc->ramp_delay)
2901 ramp_delay = rdev->desc->ramp_delay;
2902 else if (rdev->constraints->settling_time)
2903 return rdev->constraints->settling_time;
2904 else if (rdev->constraints->settling_time_up &&
2906 return rdev->constraints->settling_time_up;
2907 else if (rdev->constraints->settling_time_down &&
2909 return rdev->constraints->settling_time_down;
2911 if (ramp_delay == 0) {
2912 rdev_dbg(rdev, "ramp_delay not set\n");
2916 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2919 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2920 int min_uV, int max_uV)
2925 unsigned int selector;
2926 int old_selector = -1;
2927 const struct regulator_ops *ops = rdev->desc->ops;
2928 int old_uV = _regulator_get_voltage(rdev);
2930 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2932 min_uV += rdev->constraints->uV_offset;
2933 max_uV += rdev->constraints->uV_offset;
2936 * If we can't obtain the old selector there is not enough
2937 * info to call set_voltage_time_sel().
2939 if (_regulator_is_enabled(rdev) &&
2940 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2941 old_selector = ops->get_voltage_sel(rdev);
2942 if (old_selector < 0)
2943 return old_selector;
2946 if (ops->set_voltage) {
2947 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2951 if (ops->list_voltage)
2952 best_val = ops->list_voltage(rdev,
2955 best_val = _regulator_get_voltage(rdev);
2958 } else if (ops->set_voltage_sel) {
2959 ret = regulator_map_voltage(rdev, min_uV, max_uV);
2961 best_val = ops->list_voltage(rdev, ret);
2962 if (min_uV <= best_val && max_uV >= best_val) {
2964 if (old_selector == selector)
2967 ret = _regulator_call_set_voltage_sel(
2968 rdev, best_val, selector);
2980 if (ops->set_voltage_time_sel) {
2982 * Call set_voltage_time_sel if successfully obtained
2985 if (old_selector >= 0 && old_selector != selector)
2986 delay = ops->set_voltage_time_sel(rdev, old_selector,
2989 if (old_uV != best_val) {
2990 if (ops->set_voltage_time)
2991 delay = ops->set_voltage_time(rdev, old_uV,
2994 delay = _regulator_set_voltage_time(rdev,
3001 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3005 /* Insert any necessary delays */
3006 if (delay >= 1000) {
3007 mdelay(delay / 1000);
3008 udelay(delay % 1000);
3013 if (best_val >= 0) {
3014 unsigned long data = best_val;
3016 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3021 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3026 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3027 int min_uV, int max_uV, suspend_state_t state)
3029 struct regulator_state *rstate;
3032 rstate = regulator_get_suspend_state(rdev, state);
3036 if (min_uV < rstate->min_uV)
3037 min_uV = rstate->min_uV;
3038 if (max_uV > rstate->max_uV)
3039 max_uV = rstate->max_uV;
3041 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3045 uV = rdev->desc->ops->list_voltage(rdev, sel);
3046 if (uV >= min_uV && uV <= max_uV)
3052 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3053 int min_uV, int max_uV,
3054 suspend_state_t state)
3056 struct regulator_dev *rdev = regulator->rdev;
3057 struct regulator_voltage *voltage = ®ulator->voltage[state];
3059 int old_min_uV, old_max_uV;
3062 /* If we're setting the same range as last time the change
3063 * should be a noop (some cpufreq implementations use the same
3064 * voltage for multiple frequencies, for example).
3066 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3069 /* If we're trying to set a range that overlaps the current voltage,
3070 * return successfully even though the regulator does not support
3071 * changing the voltage.
3073 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3074 current_uV = _regulator_get_voltage(rdev);
3075 if (min_uV <= current_uV && current_uV <= max_uV) {
3076 voltage->min_uV = min_uV;
3077 voltage->max_uV = max_uV;
3083 if (!rdev->desc->ops->set_voltage &&
3084 !rdev->desc->ops->set_voltage_sel) {
3089 /* constraints check */
3090 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3094 /* restore original values in case of error */
3095 old_min_uV = voltage->min_uV;
3096 old_max_uV = voltage->max_uV;
3097 voltage->min_uV = min_uV;
3098 voltage->max_uV = max_uV;
3100 /* for not coupled regulators this will just set the voltage */
3101 ret = regulator_balance_voltage(rdev, state);
3108 voltage->min_uV = old_min_uV;
3109 voltage->max_uV = old_max_uV;
3114 static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3115 int max_uV, suspend_state_t state)
3117 int best_supply_uV = 0;
3118 int supply_change_uV = 0;
3122 regulator_ops_is_valid(rdev->supply->rdev,
3123 REGULATOR_CHANGE_VOLTAGE) &&
3124 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3125 rdev->desc->ops->get_voltage_sel))) {
3126 int current_supply_uV;
3129 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3135 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3136 if (best_supply_uV < 0) {
3137 ret = best_supply_uV;
3141 best_supply_uV += rdev->desc->min_dropout_uV;
3143 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3144 if (current_supply_uV < 0) {
3145 ret = current_supply_uV;
3149 supply_change_uV = best_supply_uV - current_supply_uV;
3152 if (supply_change_uV > 0) {
3153 ret = regulator_set_voltage_unlocked(rdev->supply,
3154 best_supply_uV, INT_MAX, state);
3156 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3162 if (state == PM_SUSPEND_ON)
3163 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3165 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3170 if (supply_change_uV < 0) {
3171 ret = regulator_set_voltage_unlocked(rdev->supply,
3172 best_supply_uV, INT_MAX, state);
3174 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3176 /* No need to fail here */
3184 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3186 int *min_uV, int *max_uV,
3187 suspend_state_t state,
3190 struct coupling_desc *c_desc = &rdev->coupling_desc;
3191 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3192 struct regulation_constraints *constraints = rdev->constraints;
3193 int max_spread = constraints->max_spread;
3194 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3195 int max_current_uV = 0, min_current_uV = INT_MAX;
3196 int highest_min_uV = 0, target_uV, possible_uV;
3203 * If there are no coupled regulators, simply set the voltage
3204 * demanded by consumers.
3206 if (n_coupled == 1) {
3208 * If consumers don't provide any demands, set voltage
3211 desired_min_uV = constraints->min_uV;
3212 desired_max_uV = constraints->max_uV;
3214 ret = regulator_check_consumers(rdev,
3216 &desired_max_uV, state);
3220 possible_uV = desired_min_uV;
3226 /* Find highest min desired voltage */
3227 for (i = 0; i < n_coupled; i++) {
3229 int tmp_max = INT_MAX;
3231 lockdep_assert_held_once(&c_rdevs[i]->mutex);
3233 ret = regulator_check_consumers(c_rdevs[i],
3239 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3243 highest_min_uV = max(highest_min_uV, tmp_min);
3246 desired_min_uV = tmp_min;
3247 desired_max_uV = tmp_max;
3252 * Let target_uV be equal to the desired one if possible.
3253 * If not, set it to minimum voltage, allowed by other coupled
3256 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3259 * Find min and max voltages, which currently aren't violating
3262 for (i = 1; i < n_coupled; i++) {
3265 if (!_regulator_is_enabled(c_rdevs[i]))
3268 tmp_act = _regulator_get_voltage(c_rdevs[i]);
3272 min_current_uV = min(tmp_act, min_current_uV);
3273 max_current_uV = max(tmp_act, max_current_uV);
3276 /* There aren't any other regulators enabled */
3277 if (max_current_uV == 0) {
3278 possible_uV = target_uV;
3281 * Correct target voltage, so as it currently isn't
3282 * violating max_spread
3284 possible_uV = max(target_uV, max_current_uV - max_spread);
3285 possible_uV = min(possible_uV, min_current_uV + max_spread);
3288 if (possible_uV > desired_max_uV)
3291 done = (possible_uV == target_uV);
3292 desired_min_uV = possible_uV;
3295 /* Set current_uV if wasn't done earlier in the code and if necessary */
3296 if (n_coupled > 1 && *current_uV == -1) {
3298 if (_regulator_is_enabled(rdev)) {
3299 ret = _regulator_get_voltage(rdev);
3305 *current_uV = desired_min_uV;
3309 *min_uV = desired_min_uV;
3310 *max_uV = desired_max_uV;
3315 static int regulator_balance_voltage(struct regulator_dev *rdev,
3316 suspend_state_t state)
3318 struct regulator_dev **c_rdevs;
3319 struct regulator_dev *best_rdev;
3320 struct coupling_desc *c_desc = &rdev->coupling_desc;
3321 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3322 bool best_c_rdev_done, c_rdev_done[MAX_COUPLED];
3323 unsigned int delta, best_delta;
3325 c_rdevs = c_desc->coupled_rdevs;
3326 n_coupled = c_desc->n_coupled;
3329 * If system is in a state other than PM_SUSPEND_ON, don't check
3330 * other coupled regulators.
3332 if (state != PM_SUSPEND_ON)
3335 if (c_desc->n_resolved < n_coupled) {
3336 rdev_err(rdev, "Not all coupled regulators registered\n");
3340 for (i = 0; i < n_coupled; i++)
3341 c_rdev_done[i] = false;
3344 * Find the best possible voltage change on each loop. Leave the loop
3345 * if there isn't any possible change.
3348 best_c_rdev_done = false;
3356 * Find highest difference between optimal voltage
3357 * and current voltage.
3359 for (i = 0; i < n_coupled; i++) {
3361 * optimal_uV is the best voltage that can be set for
3362 * i-th regulator at the moment without violating
3363 * max_spread constraint in order to balance
3364 * the coupled voltages.
3366 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3371 ret = regulator_get_optimal_voltage(c_rdevs[i],
3379 delta = abs(optimal_uV - current_uV);
3381 if (delta && best_delta <= delta) {
3382 best_c_rdev_done = ret;
3384 best_rdev = c_rdevs[i];
3385 best_min_uV = optimal_uV;
3386 best_max_uV = optimal_max_uV;
3391 /* Nothing to change, return successfully */
3397 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3398 best_max_uV, state);
3403 c_rdev_done[best_c_rdev] = best_c_rdev_done;
3405 } while (n_coupled > 1);
3412 * regulator_set_voltage - set regulator output voltage
3413 * @regulator: regulator source
3414 * @min_uV: Minimum required voltage in uV
3415 * @max_uV: Maximum acceptable voltage in uV
3417 * Sets a voltage regulator to the desired output voltage. This can be set
3418 * during any regulator state. IOW, regulator can be disabled or enabled.
3420 * If the regulator is enabled then the voltage will change to the new value
3421 * immediately otherwise if the regulator is disabled the regulator will
3422 * output at the new voltage when enabled.
3424 * NOTE: If the regulator is shared between several devices then the lowest
3425 * request voltage that meets the system constraints will be used.
3426 * Regulator system constraints must be set for this regulator before
3427 * calling this function otherwise this call will fail.
3429 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3433 regulator_lock_dependent(regulator->rdev);
3435 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3438 regulator_unlock_dependent(regulator->rdev);
3442 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3444 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3445 suspend_state_t state, bool en)
3447 struct regulator_state *rstate;
3449 rstate = regulator_get_suspend_state(rdev, state);
3453 if (!rstate->changeable)
3456 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3461 int regulator_suspend_enable(struct regulator_dev *rdev,
3462 suspend_state_t state)
3464 return regulator_suspend_toggle(rdev, state, true);
3466 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3468 int regulator_suspend_disable(struct regulator_dev *rdev,
3469 suspend_state_t state)
3471 struct regulator *regulator;
3472 struct regulator_voltage *voltage;
3475 * if any consumer wants this regulator device keeping on in
3476 * suspend states, don't set it as disabled.
3478 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3479 voltage = ®ulator->voltage[state];
3480 if (voltage->min_uV || voltage->max_uV)
3484 return regulator_suspend_toggle(rdev, state, false);
3486 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3488 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3489 int min_uV, int max_uV,
3490 suspend_state_t state)
3492 struct regulator_dev *rdev = regulator->rdev;
3493 struct regulator_state *rstate;
3495 rstate = regulator_get_suspend_state(rdev, state);
3499 if (rstate->min_uV == rstate->max_uV) {
3500 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3504 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3507 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3508 int max_uV, suspend_state_t state)
3512 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3513 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3516 regulator_lock_dependent(regulator->rdev);
3518 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3521 regulator_unlock_dependent(regulator->rdev);
3525 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3528 * regulator_set_voltage_time - get raise/fall time
3529 * @regulator: regulator source
3530 * @old_uV: starting voltage in microvolts
3531 * @new_uV: target voltage in microvolts
3533 * Provided with the starting and ending voltage, this function attempts to
3534 * calculate the time in microseconds required to rise or fall to this new
3537 int regulator_set_voltage_time(struct regulator *regulator,
3538 int old_uV, int new_uV)
3540 struct regulator_dev *rdev = regulator->rdev;
3541 const struct regulator_ops *ops = rdev->desc->ops;
3547 if (ops->set_voltage_time)
3548 return ops->set_voltage_time(rdev, old_uV, new_uV);
3549 else if (!ops->set_voltage_time_sel)
3550 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3552 /* Currently requires operations to do this */
3553 if (!ops->list_voltage || !rdev->desc->n_voltages)
3556 for (i = 0; i < rdev->desc->n_voltages; i++) {
3557 /* We only look for exact voltage matches here */
3558 voltage = regulator_list_voltage(regulator, i);
3563 if (voltage == old_uV)
3565 if (voltage == new_uV)
3569 if (old_sel < 0 || new_sel < 0)
3572 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3574 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3577 * regulator_set_voltage_time_sel - get raise/fall time
3578 * @rdev: regulator source device
3579 * @old_selector: selector for starting voltage
3580 * @new_selector: selector for target voltage
3582 * Provided with the starting and target voltage selectors, this function
3583 * returns time in microseconds required to rise or fall to this new voltage
3585 * Drivers providing ramp_delay in regulation_constraints can use this as their
3586 * set_voltage_time_sel() operation.
3588 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3589 unsigned int old_selector,
3590 unsigned int new_selector)
3592 int old_volt, new_volt;
3595 if (!rdev->desc->ops->list_voltage)
3598 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3599 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3601 if (rdev->desc->ops->set_voltage_time)
3602 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3605 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3607 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3610 * regulator_sync_voltage - re-apply last regulator output voltage
3611 * @regulator: regulator source
3613 * Re-apply the last configured voltage. This is intended to be used
3614 * where some external control source the consumer is cooperating with
3615 * has caused the configured voltage to change.
3617 int regulator_sync_voltage(struct regulator *regulator)
3619 struct regulator_dev *rdev = regulator->rdev;
3620 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
3621 int ret, min_uV, max_uV;
3623 regulator_lock(rdev);
3625 if (!rdev->desc->ops->set_voltage &&
3626 !rdev->desc->ops->set_voltage_sel) {
3631 /* This is only going to work if we've had a voltage configured. */
3632 if (!voltage->min_uV && !voltage->max_uV) {
3637 min_uV = voltage->min_uV;
3638 max_uV = voltage->max_uV;
3640 /* This should be a paranoia check... */
3641 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3645 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3649 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3652 regulator_unlock(rdev);
3655 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3657 static int _regulator_get_voltage(struct regulator_dev *rdev)
3662 if (rdev->desc->ops->get_bypass) {
3663 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3667 /* if bypassed the regulator must have a supply */
3668 if (!rdev->supply) {
3670 "bypassed regulator has no supply!\n");
3671 return -EPROBE_DEFER;
3674 return _regulator_get_voltage(rdev->supply->rdev);
3678 if (rdev->desc->ops->get_voltage_sel) {
3679 sel = rdev->desc->ops->get_voltage_sel(rdev);
3682 ret = rdev->desc->ops->list_voltage(rdev, sel);
3683 } else if (rdev->desc->ops->get_voltage) {
3684 ret = rdev->desc->ops->get_voltage(rdev);
3685 } else if (rdev->desc->ops->list_voltage) {
3686 ret = rdev->desc->ops->list_voltage(rdev, 0);
3687 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3688 ret = rdev->desc->fixed_uV;
3689 } else if (rdev->supply) {
3690 ret = _regulator_get_voltage(rdev->supply->rdev);
3697 return ret - rdev->constraints->uV_offset;
3701 * regulator_get_voltage - get regulator output voltage
3702 * @regulator: regulator source
3704 * This returns the current regulator voltage in uV.
3706 * NOTE: If the regulator is disabled it will return the voltage value. This
3707 * function should not be used to determine regulator state.
3709 int regulator_get_voltage(struct regulator *regulator)
3713 regulator_lock_dependent(regulator->rdev);
3715 ret = _regulator_get_voltage(regulator->rdev);
3717 regulator_unlock_dependent(regulator->rdev);
3721 EXPORT_SYMBOL_GPL(regulator_get_voltage);
3724 * regulator_set_current_limit - set regulator output current limit
3725 * @regulator: regulator source
3726 * @min_uA: Minimum supported current in uA
3727 * @max_uA: Maximum supported current in uA
3729 * Sets current sink to the desired output current. This can be set during
3730 * any regulator state. IOW, regulator can be disabled or enabled.
3732 * If the regulator is enabled then the current will change to the new value
3733 * immediately otherwise if the regulator is disabled the regulator will
3734 * output at the new current when enabled.
3736 * NOTE: Regulator system constraints must be set for this regulator before
3737 * calling this function otherwise this call will fail.
3739 int regulator_set_current_limit(struct regulator *regulator,
3740 int min_uA, int max_uA)
3742 struct regulator_dev *rdev = regulator->rdev;
3745 regulator_lock(rdev);
3748 if (!rdev->desc->ops->set_current_limit) {
3753 /* constraints check */
3754 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3758 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3760 regulator_unlock(rdev);
3763 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3765 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
3768 if (!rdev->desc->ops->get_current_limit)
3771 return rdev->desc->ops->get_current_limit(rdev);
3774 static int _regulator_get_current_limit(struct regulator_dev *rdev)
3778 regulator_lock(rdev);
3779 ret = _regulator_get_current_limit_unlocked(rdev);
3780 regulator_unlock(rdev);
3786 * regulator_get_current_limit - get regulator output current
3787 * @regulator: regulator source
3789 * This returns the current supplied by the specified current sink in uA.
3791 * NOTE: If the regulator is disabled it will return the current value. This
3792 * function should not be used to determine regulator state.
3794 int regulator_get_current_limit(struct regulator *regulator)
3796 return _regulator_get_current_limit(regulator->rdev);
3798 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3801 * regulator_set_mode - set regulator operating mode
3802 * @regulator: regulator source
3803 * @mode: operating mode - one of the REGULATOR_MODE constants
3805 * Set regulator operating mode to increase regulator efficiency or improve
3806 * regulation performance.
3808 * NOTE: Regulator system constraints must be set for this regulator before
3809 * calling this function otherwise this call will fail.
3811 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3813 struct regulator_dev *rdev = regulator->rdev;
3815 int regulator_curr_mode;
3817 regulator_lock(rdev);
3820 if (!rdev->desc->ops->set_mode) {
3825 /* return if the same mode is requested */
3826 if (rdev->desc->ops->get_mode) {
3827 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3828 if (regulator_curr_mode == mode) {
3834 /* constraints check */
3835 ret = regulator_mode_constrain(rdev, &mode);
3839 ret = rdev->desc->ops->set_mode(rdev, mode);
3841 regulator_unlock(rdev);
3844 EXPORT_SYMBOL_GPL(regulator_set_mode);
3846 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
3849 if (!rdev->desc->ops->get_mode)
3852 return rdev->desc->ops->get_mode(rdev);
3855 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3859 regulator_lock(rdev);
3860 ret = _regulator_get_mode_unlocked(rdev);
3861 regulator_unlock(rdev);
3867 * regulator_get_mode - get regulator operating mode
3868 * @regulator: regulator source
3870 * Get the current regulator operating mode.
3872 unsigned int regulator_get_mode(struct regulator *regulator)
3874 return _regulator_get_mode(regulator->rdev);
3876 EXPORT_SYMBOL_GPL(regulator_get_mode);
3878 static int _regulator_get_error_flags(struct regulator_dev *rdev,
3879 unsigned int *flags)
3883 regulator_lock(rdev);
3886 if (!rdev->desc->ops->get_error_flags) {
3891 ret = rdev->desc->ops->get_error_flags(rdev, flags);
3893 regulator_unlock(rdev);
3898 * regulator_get_error_flags - get regulator error information
3899 * @regulator: regulator source
3900 * @flags: pointer to store error flags
3902 * Get the current regulator error information.
3904 int regulator_get_error_flags(struct regulator *regulator,
3905 unsigned int *flags)
3907 return _regulator_get_error_flags(regulator->rdev, flags);
3909 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
3912 * regulator_set_load - set regulator load
3913 * @regulator: regulator source
3914 * @uA_load: load current
3916 * Notifies the regulator core of a new device load. This is then used by
3917 * DRMS (if enabled by constraints) to set the most efficient regulator
3918 * operating mode for the new regulator loading.
3920 * Consumer devices notify their supply regulator of the maximum power
3921 * they will require (can be taken from device datasheet in the power
3922 * consumption tables) when they change operational status and hence power
3923 * state. Examples of operational state changes that can affect power
3924 * consumption are :-
3926 * o Device is opened / closed.
3927 * o Device I/O is about to begin or has just finished.
3928 * o Device is idling in between work.
3930 * This information is also exported via sysfs to userspace.
3932 * DRMS will sum the total requested load on the regulator and change
3933 * to the most efficient operating mode if platform constraints allow.
3935 * On error a negative errno is returned.
3937 int regulator_set_load(struct regulator *regulator, int uA_load)
3939 struct regulator_dev *rdev = regulator->rdev;
3942 regulator_lock(rdev);
3943 regulator->uA_load = uA_load;
3944 ret = drms_uA_update(rdev);
3945 regulator_unlock(rdev);
3949 EXPORT_SYMBOL_GPL(regulator_set_load);
3952 * regulator_allow_bypass - allow the regulator to go into bypass mode
3954 * @regulator: Regulator to configure
3955 * @enable: enable or disable bypass mode
3957 * Allow the regulator to go into bypass mode if all other consumers
3958 * for the regulator also enable bypass mode and the machine
3959 * constraints allow this. Bypass mode means that the regulator is
3960 * simply passing the input directly to the output with no regulation.
3962 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3964 struct regulator_dev *rdev = regulator->rdev;
3967 if (!rdev->desc->ops->set_bypass)
3970 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
3973 regulator_lock(rdev);
3975 if (enable && !regulator->bypass) {
3976 rdev->bypass_count++;
3978 if (rdev->bypass_count == rdev->open_count) {
3979 ret = rdev->desc->ops->set_bypass(rdev, enable);
3981 rdev->bypass_count--;
3984 } else if (!enable && regulator->bypass) {
3985 rdev->bypass_count--;
3987 if (rdev->bypass_count != rdev->open_count) {
3988 ret = rdev->desc->ops->set_bypass(rdev, enable);
3990 rdev->bypass_count++;
3995 regulator->bypass = enable;
3997 regulator_unlock(rdev);
4001 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4004 * regulator_register_notifier - register regulator event notifier
4005 * @regulator: regulator source
4006 * @nb: notifier block
4008 * Register notifier block to receive regulator events.
4010 int regulator_register_notifier(struct regulator *regulator,
4011 struct notifier_block *nb)
4013 return blocking_notifier_chain_register(®ulator->rdev->notifier,
4016 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4019 * regulator_unregister_notifier - unregister regulator event notifier
4020 * @regulator: regulator source
4021 * @nb: notifier block
4023 * Unregister regulator event notifier block.
4025 int regulator_unregister_notifier(struct regulator *regulator,
4026 struct notifier_block *nb)
4028 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
4031 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4033 /* notify regulator consumers and downstream regulator consumers.
4034 * Note mutex must be held by caller.
4036 static int _notifier_call_chain(struct regulator_dev *rdev,
4037 unsigned long event, void *data)
4039 /* call rdev chain first */
4040 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4044 * regulator_bulk_get - get multiple regulator consumers
4046 * @dev: Device to supply
4047 * @num_consumers: Number of consumers to register
4048 * @consumers: Configuration of consumers; clients are stored here.
4050 * @return 0 on success, an errno on failure.
4052 * This helper function allows drivers to get several regulator
4053 * consumers in one operation. If any of the regulators cannot be
4054 * acquired then any regulators that were allocated will be freed
4055 * before returning to the caller.
4057 int regulator_bulk_get(struct device *dev, int num_consumers,
4058 struct regulator_bulk_data *consumers)
4063 for (i = 0; i < num_consumers; i++)
4064 consumers[i].consumer = NULL;
4066 for (i = 0; i < num_consumers; i++) {
4067 consumers[i].consumer = regulator_get(dev,
4068 consumers[i].supply);
4069 if (IS_ERR(consumers[i].consumer)) {
4070 ret = PTR_ERR(consumers[i].consumer);
4071 dev_err(dev, "Failed to get supply '%s': %d\n",
4072 consumers[i].supply, ret);
4073 consumers[i].consumer = NULL;
4082 regulator_put(consumers[i].consumer);
4086 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4088 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4090 struct regulator_bulk_data *bulk = data;
4092 bulk->ret = regulator_enable(bulk->consumer);
4096 * regulator_bulk_enable - enable multiple regulator consumers
4098 * @num_consumers: Number of consumers
4099 * @consumers: Consumer data; clients are stored here.
4100 * @return 0 on success, an errno on failure
4102 * This convenience API allows consumers to enable multiple regulator
4103 * clients in a single API call. If any consumers cannot be enabled
4104 * then any others that were enabled will be disabled again prior to
4107 int regulator_bulk_enable(int num_consumers,
4108 struct regulator_bulk_data *consumers)
4110 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4114 for (i = 0; i < num_consumers; i++) {
4115 if (consumers[i].consumer->always_on)
4116 consumers[i].ret = 0;
4118 async_schedule_domain(regulator_bulk_enable_async,
4119 &consumers[i], &async_domain);
4122 async_synchronize_full_domain(&async_domain);
4124 /* If any consumer failed we need to unwind any that succeeded */
4125 for (i = 0; i < num_consumers; i++) {
4126 if (consumers[i].ret != 0) {
4127 ret = consumers[i].ret;
4135 for (i = 0; i < num_consumers; i++) {
4136 if (consumers[i].ret < 0)
4137 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4140 regulator_disable(consumers[i].consumer);
4145 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4148 * regulator_bulk_disable - disable multiple regulator consumers
4150 * @num_consumers: Number of consumers
4151 * @consumers: Consumer data; clients are stored here.
4152 * @return 0 on success, an errno on failure
4154 * This convenience API allows consumers to disable multiple regulator
4155 * clients in a single API call. If any consumers cannot be disabled
4156 * then any others that were disabled will be enabled again prior to
4159 int regulator_bulk_disable(int num_consumers,
4160 struct regulator_bulk_data *consumers)
4165 for (i = num_consumers - 1; i >= 0; --i) {
4166 ret = regulator_disable(consumers[i].consumer);
4174 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4175 for (++i; i < num_consumers; ++i) {
4176 r = regulator_enable(consumers[i].consumer);
4178 pr_err("Failed to re-enable %s: %d\n",
4179 consumers[i].supply, r);
4184 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4187 * regulator_bulk_force_disable - force disable multiple regulator consumers
4189 * @num_consumers: Number of consumers
4190 * @consumers: Consumer data; clients are stored here.
4191 * @return 0 on success, an errno on failure
4193 * This convenience API allows consumers to forcibly disable multiple regulator
4194 * clients in a single API call.
4195 * NOTE: This should be used for situations when device damage will
4196 * likely occur if the regulators are not disabled (e.g. over temp).
4197 * Although regulator_force_disable function call for some consumers can
4198 * return error numbers, the function is called for all consumers.
4200 int regulator_bulk_force_disable(int num_consumers,
4201 struct regulator_bulk_data *consumers)
4206 for (i = 0; i < num_consumers; i++) {
4208 regulator_force_disable(consumers[i].consumer);
4210 /* Store first error for reporting */
4211 if (consumers[i].ret && !ret)
4212 ret = consumers[i].ret;
4217 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4220 * regulator_bulk_free - free multiple regulator consumers
4222 * @num_consumers: Number of consumers
4223 * @consumers: Consumer data; clients are stored here.
4225 * This convenience API allows consumers to free multiple regulator
4226 * clients in a single API call.
4228 void regulator_bulk_free(int num_consumers,
4229 struct regulator_bulk_data *consumers)
4233 for (i = 0; i < num_consumers; i++) {
4234 regulator_put(consumers[i].consumer);
4235 consumers[i].consumer = NULL;
4238 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4241 * regulator_notifier_call_chain - call regulator event notifier
4242 * @rdev: regulator source
4243 * @event: notifier block
4244 * @data: callback-specific data.
4246 * Called by regulator drivers to notify clients a regulator event has
4247 * occurred. We also notify regulator clients downstream.
4248 * Note lock must be held by caller.
4250 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4251 unsigned long event, void *data)
4253 lockdep_assert_held_once(&rdev->mutex);
4255 _notifier_call_chain(rdev, event, data);
4259 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4262 * regulator_mode_to_status - convert a regulator mode into a status
4264 * @mode: Mode to convert
4266 * Convert a regulator mode into a status.
4268 int regulator_mode_to_status(unsigned int mode)
4271 case REGULATOR_MODE_FAST:
4272 return REGULATOR_STATUS_FAST;
4273 case REGULATOR_MODE_NORMAL:
4274 return REGULATOR_STATUS_NORMAL;
4275 case REGULATOR_MODE_IDLE:
4276 return REGULATOR_STATUS_IDLE;
4277 case REGULATOR_MODE_STANDBY:
4278 return REGULATOR_STATUS_STANDBY;
4280 return REGULATOR_STATUS_UNDEFINED;
4283 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4285 static struct attribute *regulator_dev_attrs[] = {
4286 &dev_attr_name.attr,
4287 &dev_attr_num_users.attr,
4288 &dev_attr_type.attr,
4289 &dev_attr_microvolts.attr,
4290 &dev_attr_microamps.attr,
4291 &dev_attr_opmode.attr,
4292 &dev_attr_state.attr,
4293 &dev_attr_status.attr,
4294 &dev_attr_bypass.attr,
4295 &dev_attr_requested_microamps.attr,
4296 &dev_attr_min_microvolts.attr,
4297 &dev_attr_max_microvolts.attr,
4298 &dev_attr_min_microamps.attr,
4299 &dev_attr_max_microamps.attr,
4300 &dev_attr_suspend_standby_state.attr,
4301 &dev_attr_suspend_mem_state.attr,
4302 &dev_attr_suspend_disk_state.attr,
4303 &dev_attr_suspend_standby_microvolts.attr,
4304 &dev_attr_suspend_mem_microvolts.attr,
4305 &dev_attr_suspend_disk_microvolts.attr,
4306 &dev_attr_suspend_standby_mode.attr,
4307 &dev_attr_suspend_mem_mode.attr,
4308 &dev_attr_suspend_disk_mode.attr,
4313 * To avoid cluttering sysfs (and memory) with useless state, only
4314 * create attributes that can be meaningfully displayed.
4316 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4317 struct attribute *attr, int idx)
4319 struct device *dev = kobj_to_dev(kobj);
4320 struct regulator_dev *rdev = dev_to_rdev(dev);
4321 const struct regulator_ops *ops = rdev->desc->ops;
4322 umode_t mode = attr->mode;
4324 /* these three are always present */
4325 if (attr == &dev_attr_name.attr ||
4326 attr == &dev_attr_num_users.attr ||
4327 attr == &dev_attr_type.attr)
4330 /* some attributes need specific methods to be displayed */
4331 if (attr == &dev_attr_microvolts.attr) {
4332 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4333 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4334 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4335 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4340 if (attr == &dev_attr_microamps.attr)
4341 return ops->get_current_limit ? mode : 0;
4343 if (attr == &dev_attr_opmode.attr)
4344 return ops->get_mode ? mode : 0;
4346 if (attr == &dev_attr_state.attr)
4347 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4349 if (attr == &dev_attr_status.attr)
4350 return ops->get_status ? mode : 0;
4352 if (attr == &dev_attr_bypass.attr)
4353 return ops->get_bypass ? mode : 0;
4355 /* some attributes are type-specific */
4356 if (attr == &dev_attr_requested_microamps.attr)
4357 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
4359 /* constraints need specific supporting methods */
4360 if (attr == &dev_attr_min_microvolts.attr ||
4361 attr == &dev_attr_max_microvolts.attr)
4362 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4364 if (attr == &dev_attr_min_microamps.attr ||
4365 attr == &dev_attr_max_microamps.attr)
4366 return ops->set_current_limit ? mode : 0;
4368 if (attr == &dev_attr_suspend_standby_state.attr ||
4369 attr == &dev_attr_suspend_mem_state.attr ||
4370 attr == &dev_attr_suspend_disk_state.attr)
4373 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4374 attr == &dev_attr_suspend_mem_microvolts.attr ||
4375 attr == &dev_attr_suspend_disk_microvolts.attr)
4376 return ops->set_suspend_voltage ? mode : 0;
4378 if (attr == &dev_attr_suspend_standby_mode.attr ||
4379 attr == &dev_attr_suspend_mem_mode.attr ||
4380 attr == &dev_attr_suspend_disk_mode.attr)
4381 return ops->set_suspend_mode ? mode : 0;
4386 static const struct attribute_group regulator_dev_group = {
4387 .attrs = regulator_dev_attrs,
4388 .is_visible = regulator_attr_is_visible,
4391 static const struct attribute_group *regulator_dev_groups[] = {
4392 ®ulator_dev_group,
4396 static void regulator_dev_release(struct device *dev)
4398 struct regulator_dev *rdev = dev_get_drvdata(dev);
4400 kfree(rdev->constraints);
4401 of_node_put(rdev->dev.of_node);
4405 static void rdev_init_debugfs(struct regulator_dev *rdev)
4407 struct device *parent = rdev->dev.parent;
4408 const char *rname = rdev_get_name(rdev);
4409 char name[NAME_MAX];
4411 /* Avoid duplicate debugfs directory names */
4412 if (parent && rname == rdev->desc->name) {
4413 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4418 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4419 if (!rdev->debugfs) {
4420 rdev_warn(rdev, "Failed to create debugfs directory\n");
4424 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4426 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4428 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4429 &rdev->bypass_count);
4432 static int regulator_register_resolve_supply(struct device *dev, void *data)
4434 struct regulator_dev *rdev = dev_to_rdev(dev);
4436 if (regulator_resolve_supply(rdev))
4437 rdev_dbg(rdev, "unable to resolve supply\n");
4442 static int regulator_fill_coupling_array(struct regulator_dev *rdev)
4444 struct coupling_desc *c_desc = &rdev->coupling_desc;
4445 int n_coupled = c_desc->n_coupled;
4446 struct regulator_dev *c_rdev;
4449 for (i = 1; i < n_coupled; i++) {
4450 /* already resolved */
4451 if (c_desc->coupled_rdevs[i])
4454 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4457 c_desc->coupled_rdevs[i] = c_rdev;
4458 c_desc->n_resolved++;
4462 if (rdev->coupling_desc.n_resolved < n_coupled)
4468 static int regulator_register_fill_coupling_array(struct device *dev,
4471 struct regulator_dev *rdev = dev_to_rdev(dev);
4473 if (!IS_ENABLED(CONFIG_OF))
4476 if (regulator_fill_coupling_array(rdev))
4477 rdev_dbg(rdev, "unable to resolve coupling\n");
4482 static int regulator_resolve_coupling(struct regulator_dev *rdev)
4486 if (!IS_ENABLED(CONFIG_OF))
4489 n_phandles = of_get_n_coupled(rdev);
4491 if (n_phandles + 1 > MAX_COUPLED) {
4492 rdev_err(rdev, "too many regulators coupled\n");
4497 * Every regulator should always have coupling descriptor filled with
4498 * at least pointer to itself.
4500 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4501 rdev->coupling_desc.n_coupled = n_phandles + 1;
4502 rdev->coupling_desc.n_resolved++;
4504 /* regulator isn't coupled */
4505 if (n_phandles == 0)
4508 /* regulator, which can't change its voltage, can't be coupled */
4509 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
4510 rdev_err(rdev, "voltage operation not allowed\n");
4514 if (rdev->constraints->max_spread <= 0) {
4515 rdev_err(rdev, "wrong max_spread value\n");
4519 if (!of_check_coupling_data(rdev))
4523 * After everything has been checked, try to fill rdevs array
4524 * with pointers to regulators parsed from device tree. If some
4525 * regulators are not registered yet, retry in late init call
4527 regulator_fill_coupling_array(rdev);
4533 * regulator_register - register regulator
4534 * @regulator_desc: regulator to register
4535 * @cfg: runtime configuration for regulator
4537 * Called by regulator drivers to register a regulator.
4538 * Returns a valid pointer to struct regulator_dev on success
4539 * or an ERR_PTR() on error.
4541 struct regulator_dev *
4542 regulator_register(const struct regulator_desc *regulator_desc,
4543 const struct regulator_config *cfg)
4545 const struct regulation_constraints *constraints = NULL;
4546 const struct regulator_init_data *init_data;
4547 struct regulator_config *config = NULL;
4548 static atomic_t regulator_no = ATOMIC_INIT(-1);
4549 struct regulator_dev *rdev;
4553 if (regulator_desc == NULL || cfg == NULL)
4554 return ERR_PTR(-EINVAL);
4559 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4560 return ERR_PTR(-EINVAL);
4562 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4563 regulator_desc->type != REGULATOR_CURRENT)
4564 return ERR_PTR(-EINVAL);
4566 /* Only one of each should be implemented */
4567 WARN_ON(regulator_desc->ops->get_voltage &&
4568 regulator_desc->ops->get_voltage_sel);
4569 WARN_ON(regulator_desc->ops->set_voltage &&
4570 regulator_desc->ops->set_voltage_sel);
4572 /* If we're using selectors we must implement list_voltage. */
4573 if (regulator_desc->ops->get_voltage_sel &&
4574 !regulator_desc->ops->list_voltage) {
4575 return ERR_PTR(-EINVAL);
4577 if (regulator_desc->ops->set_voltage_sel &&
4578 !regulator_desc->ops->list_voltage) {
4579 return ERR_PTR(-EINVAL);
4582 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4584 return ERR_PTR(-ENOMEM);
4587 * Duplicate the config so the driver could override it after
4588 * parsing init data.
4590 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4591 if (config == NULL) {
4593 return ERR_PTR(-ENOMEM);
4596 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4597 &rdev->dev.of_node);
4599 init_data = config->init_data;
4600 rdev->dev.of_node = of_node_get(config->of_node);
4603 mutex_init(&rdev->mutex);
4604 rdev->reg_data = config->driver_data;
4605 rdev->owner = regulator_desc->owner;
4606 rdev->desc = regulator_desc;
4608 rdev->regmap = config->regmap;
4609 else if (dev_get_regmap(dev, NULL))
4610 rdev->regmap = dev_get_regmap(dev, NULL);
4611 else if (dev->parent)
4612 rdev->regmap = dev_get_regmap(dev->parent, NULL);
4613 INIT_LIST_HEAD(&rdev->consumer_list);
4614 INIT_LIST_HEAD(&rdev->list);
4615 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4616 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4618 /* preform any regulator specific init */
4619 if (init_data && init_data->regulator_init) {
4620 ret = init_data->regulator_init(rdev->reg_data);
4625 if (config->ena_gpiod ||
4626 ((config->ena_gpio || config->ena_gpio_initialized) &&
4627 gpio_is_valid(config->ena_gpio))) {
4628 mutex_lock(®ulator_list_mutex);
4629 ret = regulator_ena_gpio_request(rdev, config);
4630 mutex_unlock(®ulator_list_mutex);
4632 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4633 config->ena_gpio, ret);
4638 /* register with sysfs */
4639 rdev->dev.class = ®ulator_class;
4640 rdev->dev.parent = dev;
4641 dev_set_name(&rdev->dev, "regulator.%lu",
4642 (unsigned long) atomic_inc_return(®ulator_no));
4644 /* set regulator constraints */
4646 constraints = &init_data->constraints;
4648 if (init_data && init_data->supply_regulator)
4649 rdev->supply_name = init_data->supply_regulator;
4650 else if (regulator_desc->supply_name)
4651 rdev->supply_name = regulator_desc->supply_name;
4654 * Attempt to resolve the regulator supply, if specified,
4655 * but don't return an error if we fail because we will try
4656 * to resolve it again later as more regulators are added.
4658 if (regulator_resolve_supply(rdev))
4659 rdev_dbg(rdev, "unable to resolve supply\n");
4661 ret = set_machine_constraints(rdev, constraints);
4665 mutex_lock(®ulator_list_mutex);
4666 ret = regulator_resolve_coupling(rdev);
4667 mutex_unlock(®ulator_list_mutex);
4672 /* add consumers devices */
4674 mutex_lock(®ulator_list_mutex);
4675 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4676 ret = set_consumer_device_supply(rdev,
4677 init_data->consumer_supplies[i].dev_name,
4678 init_data->consumer_supplies[i].supply);
4680 mutex_unlock(®ulator_list_mutex);
4681 dev_err(dev, "Failed to set supply %s\n",
4682 init_data->consumer_supplies[i].supply);
4683 goto unset_supplies;
4686 mutex_unlock(®ulator_list_mutex);
4689 if (!rdev->desc->ops->get_voltage &&
4690 !rdev->desc->ops->list_voltage &&
4691 !rdev->desc->fixed_uV)
4692 rdev->is_switch = true;
4694 dev_set_drvdata(&rdev->dev, rdev);
4695 ret = device_register(&rdev->dev);
4697 put_device(&rdev->dev);
4698 goto unset_supplies;
4701 rdev_init_debugfs(rdev);
4703 /* try to resolve regulators supply since a new one was registered */
4704 class_for_each_device(®ulator_class, NULL, NULL,
4705 regulator_register_resolve_supply);
4710 mutex_lock(®ulator_list_mutex);
4711 unset_regulator_supplies(rdev);
4712 mutex_unlock(®ulator_list_mutex);
4714 kfree(rdev->constraints);
4715 mutex_lock(®ulator_list_mutex);
4716 regulator_ena_gpio_free(rdev);
4717 mutex_unlock(®ulator_list_mutex);
4721 return ERR_PTR(ret);
4723 EXPORT_SYMBOL_GPL(regulator_register);
4726 * regulator_unregister - unregister regulator
4727 * @rdev: regulator to unregister
4729 * Called by regulator drivers to unregister a regulator.
4731 void regulator_unregister(struct regulator_dev *rdev)
4737 while (rdev->use_count--)
4738 regulator_disable(rdev->supply);
4739 regulator_put(rdev->supply);
4741 mutex_lock(®ulator_list_mutex);
4742 debugfs_remove_recursive(rdev->debugfs);
4743 flush_work(&rdev->disable_work.work);
4744 WARN_ON(rdev->open_count);
4745 unset_regulator_supplies(rdev);
4746 list_del(&rdev->list);
4747 regulator_ena_gpio_free(rdev);
4748 mutex_unlock(®ulator_list_mutex);
4749 device_unregister(&rdev->dev);
4751 EXPORT_SYMBOL_GPL(regulator_unregister);
4753 #ifdef CONFIG_SUSPEND
4755 * regulator_suspend - prepare regulators for system wide suspend
4756 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
4758 * Configure each regulator with it's suspend operating parameters for state.
4760 static int regulator_suspend(struct device *dev)
4762 struct regulator_dev *rdev = dev_to_rdev(dev);
4763 suspend_state_t state = pm_suspend_target_state;
4766 regulator_lock(rdev);
4767 ret = suspend_set_state(rdev, state);
4768 regulator_unlock(rdev);
4773 static int regulator_resume(struct device *dev)
4775 suspend_state_t state = pm_suspend_target_state;
4776 struct regulator_dev *rdev = dev_to_rdev(dev);
4777 struct regulator_state *rstate;
4780 rstate = regulator_get_suspend_state(rdev, state);
4784 regulator_lock(rdev);
4786 if (rdev->desc->ops->resume &&
4787 (rstate->enabled == ENABLE_IN_SUSPEND ||
4788 rstate->enabled == DISABLE_IN_SUSPEND))
4789 ret = rdev->desc->ops->resume(rdev);
4791 regulator_unlock(rdev);
4795 #else /* !CONFIG_SUSPEND */
4797 #define regulator_suspend NULL
4798 #define regulator_resume NULL
4800 #endif /* !CONFIG_SUSPEND */
4803 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
4804 .suspend = regulator_suspend,
4805 .resume = regulator_resume,
4809 struct class regulator_class = {
4810 .name = "regulator",
4811 .dev_release = regulator_dev_release,
4812 .dev_groups = regulator_dev_groups,
4814 .pm = ®ulator_pm_ops,
4818 * regulator_has_full_constraints - the system has fully specified constraints
4820 * Calling this function will cause the regulator API to disable all
4821 * regulators which have a zero use count and don't have an always_on
4822 * constraint in a late_initcall.
4824 * The intention is that this will become the default behaviour in a
4825 * future kernel release so users are encouraged to use this facility
4828 void regulator_has_full_constraints(void)
4830 has_full_constraints = 1;
4832 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4835 * rdev_get_drvdata - get rdev regulator driver data
4838 * Get rdev regulator driver private data. This call can be used in the
4839 * regulator driver context.
4841 void *rdev_get_drvdata(struct regulator_dev *rdev)
4843 return rdev->reg_data;
4845 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4848 * regulator_get_drvdata - get regulator driver data
4849 * @regulator: regulator
4851 * Get regulator driver private data. This call can be used in the consumer
4852 * driver context when non API regulator specific functions need to be called.
4854 void *regulator_get_drvdata(struct regulator *regulator)
4856 return regulator->rdev->reg_data;
4858 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4861 * regulator_set_drvdata - set regulator driver data
4862 * @regulator: regulator
4865 void regulator_set_drvdata(struct regulator *regulator, void *data)
4867 regulator->rdev->reg_data = data;
4869 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4872 * regulator_get_id - get regulator ID
4875 int rdev_get_id(struct regulator_dev *rdev)
4877 return rdev->desc->id;
4879 EXPORT_SYMBOL_GPL(rdev_get_id);
4881 struct device *rdev_get_dev(struct regulator_dev *rdev)
4885 EXPORT_SYMBOL_GPL(rdev_get_dev);
4887 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4889 return reg_init_data->driver_data;
4891 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4893 #ifdef CONFIG_DEBUG_FS
4894 static int supply_map_show(struct seq_file *sf, void *data)
4896 struct regulator_map *map;
4898 list_for_each_entry(map, ®ulator_map_list, list) {
4899 seq_printf(sf, "%s -> %s.%s\n",
4900 rdev_get_name(map->regulator), map->dev_name,
4907 static int supply_map_open(struct inode *inode, struct file *file)
4909 return single_open(file, supply_map_show, inode->i_private);
4913 static const struct file_operations supply_map_fops = {
4914 #ifdef CONFIG_DEBUG_FS
4915 .open = supply_map_open,
4917 .llseek = seq_lseek,
4918 .release = single_release,
4922 #ifdef CONFIG_DEBUG_FS
4923 struct summary_data {
4925 struct regulator_dev *parent;
4929 static void regulator_summary_show_subtree(struct seq_file *s,
4930 struct regulator_dev *rdev,
4933 static int regulator_summary_show_children(struct device *dev, void *data)
4935 struct regulator_dev *rdev = dev_to_rdev(dev);
4936 struct summary_data *summary_data = data;
4938 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4939 regulator_summary_show_subtree(summary_data->s, rdev,
4940 summary_data->level + 1);
4945 static void regulator_summary_show_subtree(struct seq_file *s,
4946 struct regulator_dev *rdev,
4949 struct regulation_constraints *c;
4950 struct regulator *consumer;
4951 struct summary_data summary_data;
4952 unsigned int opmode;
4957 regulator_lock_nested(rdev, level);
4959 opmode = _regulator_get_mode_unlocked(rdev);
4960 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
4962 30 - level * 3, rdev_get_name(rdev),
4963 rdev->use_count, rdev->open_count, rdev->bypass_count,
4964 regulator_opmode_to_str(opmode));
4966 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4967 seq_printf(s, "%5dmA ",
4968 _regulator_get_current_limit_unlocked(rdev) / 1000);
4970 c = rdev->constraints;
4972 switch (rdev->desc->type) {
4973 case REGULATOR_VOLTAGE:
4974 seq_printf(s, "%5dmV %5dmV ",
4975 c->min_uV / 1000, c->max_uV / 1000);
4977 case REGULATOR_CURRENT:
4978 seq_printf(s, "%5dmA %5dmA ",
4979 c->min_uA / 1000, c->max_uA / 1000);
4986 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4987 if (consumer->dev && consumer->dev->class == ®ulator_class)
4990 seq_printf(s, "%*s%-*s ",
4991 (level + 1) * 3 + 1, "",
4992 30 - (level + 1) * 3,
4993 consumer->dev ? dev_name(consumer->dev) : "deviceless");
4995 switch (rdev->desc->type) {
4996 case REGULATOR_VOLTAGE:
4997 seq_printf(s, "%37dmA %5dmV %5dmV",
4998 consumer->uA_load / 1000,
4999 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5000 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5002 case REGULATOR_CURRENT:
5010 summary_data.level = level;
5011 summary_data.parent = rdev;
5013 class_for_each_device(®ulator_class, NULL, &summary_data,
5014 regulator_summary_show_children);
5016 regulator_unlock(rdev);
5019 static int regulator_summary_show_roots(struct device *dev, void *data)
5021 struct regulator_dev *rdev = dev_to_rdev(dev);
5022 struct seq_file *s = data;
5025 regulator_summary_show_subtree(s, rdev, 0);
5030 static int regulator_summary_show(struct seq_file *s, void *data)
5032 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5033 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5035 class_for_each_device(®ulator_class, NULL, s,
5036 regulator_summary_show_roots);
5041 static int regulator_summary_open(struct inode *inode, struct file *file)
5043 return single_open(file, regulator_summary_show, inode->i_private);
5047 static const struct file_operations regulator_summary_fops = {
5048 #ifdef CONFIG_DEBUG_FS
5049 .open = regulator_summary_open,
5051 .llseek = seq_lseek,
5052 .release = single_release,
5056 static int __init regulator_init(void)
5060 ret = class_register(®ulator_class);
5062 debugfs_root = debugfs_create_dir("regulator", NULL);
5064 pr_warn("regulator: Failed to create debugfs directory\n");
5066 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5069 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5070 NULL, ®ulator_summary_fops);
5072 regulator_dummy_init();
5077 /* init early to allow our consumers to complete system booting */
5078 core_initcall(regulator_init);
5080 static int __init regulator_late_cleanup(struct device *dev, void *data)
5082 struct regulator_dev *rdev = dev_to_rdev(dev);
5083 const struct regulator_ops *ops = rdev->desc->ops;
5084 struct regulation_constraints *c = rdev->constraints;
5087 if (c && c->always_on)
5090 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5093 regulator_lock(rdev);
5095 if (rdev->use_count)
5098 /* If we can't read the status assume it's on. */
5099 if (ops->is_enabled)
5100 enabled = ops->is_enabled(rdev);
5107 if (have_full_constraints()) {
5108 /* We log since this may kill the system if it goes
5110 rdev_info(rdev, "disabling\n");
5111 ret = _regulator_do_disable(rdev);
5113 rdev_err(rdev, "couldn't disable: %d\n", ret);
5115 /* The intention is that in future we will
5116 * assume that full constraints are provided
5117 * so warn even if we aren't going to do
5120 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5124 regulator_unlock(rdev);
5129 static int __init regulator_init_complete(void)
5132 * Since DT doesn't provide an idiomatic mechanism for
5133 * enabling full constraints and since it's much more natural
5134 * with DT to provide them just assume that a DT enabled
5135 * system has full constraints.
5137 if (of_have_populated_dt())
5138 has_full_constraints = true;
5141 * Regulators may had failed to resolve their input supplies
5142 * when were registered, either because the input supply was
5143 * not registered yet or because its parent device was not
5144 * bound yet. So attempt to resolve the input supplies for
5145 * pending regulators before trying to disable unused ones.
5147 class_for_each_device(®ulator_class, NULL, NULL,
5148 regulator_register_resolve_supply);
5150 /* If we have a full configuration then disable any regulators
5151 * we have permission to change the status for and which are
5152 * not in use or always_on. This is effectively the default
5153 * for DT and ACPI as they have full constraints.
5155 class_for_each_device(®ulator_class, NULL, NULL,
5156 regulator_late_cleanup);
5158 class_for_each_device(®ulator_class, NULL, NULL,
5159 regulator_register_fill_coupling_array);
5163 late_initcall_sync(regulator_init_complete);