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 struct regulator *create_regulator(struct regulator_dev *rdev,
110 const char *supply_name);
111 static void _regulator_put(struct regulator *regulator);
113 static const char *rdev_get_name(struct regulator_dev *rdev)
115 if (rdev->constraints && rdev->constraints->name)
116 return rdev->constraints->name;
117 else if (rdev->desc->name)
118 return rdev->desc->name;
123 static bool have_full_constraints(void)
125 return has_full_constraints || of_have_populated_dt();
128 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
130 if (!rdev->constraints) {
131 rdev_err(rdev, "no constraints\n");
135 if (rdev->constraints->valid_ops_mask & ops)
141 static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
143 if (rdev && rdev->supply)
144 return rdev->supply->rdev;
150 * regulator_lock_nested - lock a single regulator
151 * @rdev: regulator source
152 * @subclass: mutex subclass used for lockdep
154 * This function can be called many times by one task on
155 * a single regulator and its mutex will be locked only
156 * once. If a task, which is calling this function is other
157 * than the one, which initially locked the mutex, it will
160 static void regulator_lock_nested(struct regulator_dev *rdev,
161 unsigned int subclass)
163 if (!mutex_trylock(&rdev->mutex)) {
164 if (rdev->mutex_owner == current) {
168 mutex_lock_nested(&rdev->mutex, subclass);
172 rdev->mutex_owner = current;
175 static inline void regulator_lock(struct regulator_dev *rdev)
177 regulator_lock_nested(rdev, 0);
181 * regulator_unlock - unlock a single regulator
182 * @rdev: regulator_source
184 * This function unlocks the mutex when the
185 * reference counter reaches 0.
187 static void regulator_unlock(struct regulator_dev *rdev)
189 if (rdev->ref_cnt != 0) {
192 if (!rdev->ref_cnt) {
193 rdev->mutex_owner = NULL;
194 mutex_unlock(&rdev->mutex);
200 * regulator_lock_supply - lock a regulator and its supplies
201 * @rdev: regulator source
203 static void regulator_lock_supply(struct regulator_dev *rdev)
207 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
208 regulator_lock_nested(rdev, i);
212 * regulator_unlock_supply - unlock a regulator and its supplies
213 * @rdev: regulator source
215 static void regulator_unlock_supply(struct regulator_dev *rdev)
217 struct regulator *supply;
220 regulator_unlock(rdev);
221 supply = rdev->supply;
231 * of_get_regulator - get a regulator device node based on supply name
232 * @dev: Device pointer for the consumer (of regulator) device
233 * @supply: regulator supply name
235 * Extract the regulator device node corresponding to the supply name.
236 * returns the device node corresponding to the regulator if found, else
239 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
241 struct device_node *regnode = NULL;
242 char prop_name[32]; /* 32 is max size of property name */
244 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
246 snprintf(prop_name, 32, "%s-supply", supply);
247 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
250 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
251 prop_name, dev->of_node);
257 /* Platform voltage constraint check */
258 static int regulator_check_voltage(struct regulator_dev *rdev,
259 int *min_uV, int *max_uV)
261 BUG_ON(*min_uV > *max_uV);
263 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
264 rdev_err(rdev, "voltage operation not allowed\n");
268 if (*max_uV > rdev->constraints->max_uV)
269 *max_uV = rdev->constraints->max_uV;
270 if (*min_uV < rdev->constraints->min_uV)
271 *min_uV = rdev->constraints->min_uV;
273 if (*min_uV > *max_uV) {
274 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
282 /* return 0 if the state is valid */
283 static int regulator_check_states(suspend_state_t state)
285 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
288 /* Make sure we select a voltage that suits the needs of all
289 * regulator consumers
291 static int regulator_check_consumers(struct regulator_dev *rdev,
292 int *min_uV, int *max_uV,
293 suspend_state_t state)
295 struct regulator *regulator;
296 struct regulator_voltage *voltage;
298 list_for_each_entry(regulator, &rdev->consumer_list, list) {
299 voltage = ®ulator->voltage[state];
301 * Assume consumers that didn't say anything are OK
302 * with anything in the constraint range.
304 if (!voltage->min_uV && !voltage->max_uV)
307 if (*max_uV > voltage->max_uV)
308 *max_uV = voltage->max_uV;
309 if (*min_uV < voltage->min_uV)
310 *min_uV = voltage->min_uV;
313 if (*min_uV > *max_uV) {
314 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
322 /* current constraint check */
323 static int regulator_check_current_limit(struct regulator_dev *rdev,
324 int *min_uA, int *max_uA)
326 BUG_ON(*min_uA > *max_uA);
328 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
329 rdev_err(rdev, "current operation not allowed\n");
333 if (*max_uA > rdev->constraints->max_uA)
334 *max_uA = rdev->constraints->max_uA;
335 if (*min_uA < rdev->constraints->min_uA)
336 *min_uA = rdev->constraints->min_uA;
338 if (*min_uA > *max_uA) {
339 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
347 /* operating mode constraint check */
348 static int regulator_mode_constrain(struct regulator_dev *rdev,
352 case REGULATOR_MODE_FAST:
353 case REGULATOR_MODE_NORMAL:
354 case REGULATOR_MODE_IDLE:
355 case REGULATOR_MODE_STANDBY:
358 rdev_err(rdev, "invalid mode %x specified\n", *mode);
362 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
363 rdev_err(rdev, "mode operation not allowed\n");
367 /* The modes are bitmasks, the most power hungry modes having
368 * the lowest values. If the requested mode isn't supported
369 * try higher modes. */
371 if (rdev->constraints->valid_modes_mask & *mode)
379 static inline struct regulator_state *
380 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
382 if (rdev->constraints == NULL)
386 case PM_SUSPEND_STANDBY:
387 return &rdev->constraints->state_standby;
389 return &rdev->constraints->state_mem;
391 return &rdev->constraints->state_disk;
397 static ssize_t regulator_uV_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
400 struct regulator_dev *rdev = dev_get_drvdata(dev);
403 regulator_lock(rdev);
404 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
405 regulator_unlock(rdev);
409 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
411 static ssize_t regulator_uA_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
414 struct regulator_dev *rdev = dev_get_drvdata(dev);
416 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
418 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
420 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
423 struct regulator_dev *rdev = dev_get_drvdata(dev);
425 return sprintf(buf, "%s\n", rdev_get_name(rdev));
427 static DEVICE_ATTR_RO(name);
429 static const char *regulator_opmode_to_str(int mode)
432 case REGULATOR_MODE_FAST:
434 case REGULATOR_MODE_NORMAL:
436 case REGULATOR_MODE_IDLE:
438 case REGULATOR_MODE_STANDBY:
444 static ssize_t regulator_print_opmode(char *buf, int mode)
446 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
449 static ssize_t regulator_opmode_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
452 struct regulator_dev *rdev = dev_get_drvdata(dev);
454 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
456 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
458 static ssize_t regulator_print_state(char *buf, int state)
461 return sprintf(buf, "enabled\n");
463 return sprintf(buf, "disabled\n");
465 return sprintf(buf, "unknown\n");
468 static ssize_t regulator_state_show(struct device *dev,
469 struct device_attribute *attr, char *buf)
471 struct regulator_dev *rdev = dev_get_drvdata(dev);
474 regulator_lock(rdev);
475 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
476 regulator_unlock(rdev);
480 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
482 static ssize_t regulator_status_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
485 struct regulator_dev *rdev = dev_get_drvdata(dev);
489 status = rdev->desc->ops->get_status(rdev);
494 case REGULATOR_STATUS_OFF:
497 case REGULATOR_STATUS_ON:
500 case REGULATOR_STATUS_ERROR:
503 case REGULATOR_STATUS_FAST:
506 case REGULATOR_STATUS_NORMAL:
509 case REGULATOR_STATUS_IDLE:
512 case REGULATOR_STATUS_STANDBY:
515 case REGULATOR_STATUS_BYPASS:
518 case REGULATOR_STATUS_UNDEFINED:
525 return sprintf(buf, "%s\n", label);
527 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
529 static ssize_t regulator_min_uA_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
532 struct regulator_dev *rdev = dev_get_drvdata(dev);
534 if (!rdev->constraints)
535 return sprintf(buf, "constraint not defined\n");
537 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
539 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
541 static ssize_t regulator_max_uA_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
544 struct regulator_dev *rdev = dev_get_drvdata(dev);
546 if (!rdev->constraints)
547 return sprintf(buf, "constraint not defined\n");
549 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
551 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
553 static ssize_t regulator_min_uV_show(struct device *dev,
554 struct device_attribute *attr, char *buf)
556 struct regulator_dev *rdev = dev_get_drvdata(dev);
558 if (!rdev->constraints)
559 return sprintf(buf, "constraint not defined\n");
561 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
563 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
565 static ssize_t regulator_max_uV_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
568 struct regulator_dev *rdev = dev_get_drvdata(dev);
570 if (!rdev->constraints)
571 return sprintf(buf, "constraint not defined\n");
573 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
575 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
577 static ssize_t regulator_total_uA_show(struct device *dev,
578 struct device_attribute *attr, char *buf)
580 struct regulator_dev *rdev = dev_get_drvdata(dev);
581 struct regulator *regulator;
584 regulator_lock(rdev);
585 list_for_each_entry(regulator, &rdev->consumer_list, list)
586 uA += regulator->uA_load;
587 regulator_unlock(rdev);
588 return sprintf(buf, "%d\n", uA);
590 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
592 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
595 struct regulator_dev *rdev = dev_get_drvdata(dev);
596 return sprintf(buf, "%d\n", rdev->use_count);
598 static DEVICE_ATTR_RO(num_users);
600 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
603 struct regulator_dev *rdev = dev_get_drvdata(dev);
605 switch (rdev->desc->type) {
606 case REGULATOR_VOLTAGE:
607 return sprintf(buf, "voltage\n");
608 case REGULATOR_CURRENT:
609 return sprintf(buf, "current\n");
611 return sprintf(buf, "unknown\n");
613 static DEVICE_ATTR_RO(type);
615 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
618 struct regulator_dev *rdev = dev_get_drvdata(dev);
620 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
622 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
623 regulator_suspend_mem_uV_show, NULL);
625 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
626 struct device_attribute *attr, char *buf)
628 struct regulator_dev *rdev = dev_get_drvdata(dev);
630 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
632 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
633 regulator_suspend_disk_uV_show, NULL);
635 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
636 struct device_attribute *attr, char *buf)
638 struct regulator_dev *rdev = dev_get_drvdata(dev);
640 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
642 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
643 regulator_suspend_standby_uV_show, NULL);
645 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
646 struct device_attribute *attr, char *buf)
648 struct regulator_dev *rdev = dev_get_drvdata(dev);
650 return regulator_print_opmode(buf,
651 rdev->constraints->state_mem.mode);
653 static DEVICE_ATTR(suspend_mem_mode, 0444,
654 regulator_suspend_mem_mode_show, NULL);
656 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
657 struct device_attribute *attr, char *buf)
659 struct regulator_dev *rdev = dev_get_drvdata(dev);
661 return regulator_print_opmode(buf,
662 rdev->constraints->state_disk.mode);
664 static DEVICE_ATTR(suspend_disk_mode, 0444,
665 regulator_suspend_disk_mode_show, NULL);
667 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
668 struct device_attribute *attr, char *buf)
670 struct regulator_dev *rdev = dev_get_drvdata(dev);
672 return regulator_print_opmode(buf,
673 rdev->constraints->state_standby.mode);
675 static DEVICE_ATTR(suspend_standby_mode, 0444,
676 regulator_suspend_standby_mode_show, NULL);
678 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
679 struct device_attribute *attr, char *buf)
681 struct regulator_dev *rdev = dev_get_drvdata(dev);
683 return regulator_print_state(buf,
684 rdev->constraints->state_mem.enabled);
686 static DEVICE_ATTR(suspend_mem_state, 0444,
687 regulator_suspend_mem_state_show, NULL);
689 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
690 struct device_attribute *attr, char *buf)
692 struct regulator_dev *rdev = dev_get_drvdata(dev);
694 return regulator_print_state(buf,
695 rdev->constraints->state_disk.enabled);
697 static DEVICE_ATTR(suspend_disk_state, 0444,
698 regulator_suspend_disk_state_show, NULL);
700 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
701 struct device_attribute *attr, char *buf)
703 struct regulator_dev *rdev = dev_get_drvdata(dev);
705 return regulator_print_state(buf,
706 rdev->constraints->state_standby.enabled);
708 static DEVICE_ATTR(suspend_standby_state, 0444,
709 regulator_suspend_standby_state_show, NULL);
711 static ssize_t regulator_bypass_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
714 struct regulator_dev *rdev = dev_get_drvdata(dev);
719 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
728 return sprintf(buf, "%s\n", report);
730 static DEVICE_ATTR(bypass, 0444,
731 regulator_bypass_show, NULL);
733 /* Calculate the new optimum regulator operating mode based on the new total
734 * consumer load. All locks held by caller */
735 static int drms_uA_update(struct regulator_dev *rdev)
737 struct regulator *sibling;
738 int current_uA = 0, output_uV, input_uV, err;
741 lockdep_assert_held_once(&rdev->mutex);
744 * first check to see if we can set modes at all, otherwise just
745 * tell the consumer everything is OK.
747 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
750 if (!rdev->desc->ops->get_optimum_mode &&
751 !rdev->desc->ops->set_load)
754 if (!rdev->desc->ops->set_mode &&
755 !rdev->desc->ops->set_load)
758 /* calc total requested load */
759 list_for_each_entry(sibling, &rdev->consumer_list, list)
760 current_uA += sibling->uA_load;
762 current_uA += rdev->constraints->system_load;
764 if (rdev->desc->ops->set_load) {
765 /* set the optimum mode for our new total regulator load */
766 err = rdev->desc->ops->set_load(rdev, current_uA);
768 rdev_err(rdev, "failed to set load %d\n", current_uA);
770 /* get output voltage */
771 output_uV = _regulator_get_voltage(rdev);
772 if (output_uV <= 0) {
773 rdev_err(rdev, "invalid output voltage found\n");
777 /* get input voltage */
780 input_uV = regulator_get_voltage(rdev->supply);
782 input_uV = rdev->constraints->input_uV;
784 rdev_err(rdev, "invalid input voltage found\n");
788 /* now get the optimum mode for our new total regulator load */
789 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
790 output_uV, current_uA);
792 /* check the new mode is allowed */
793 err = regulator_mode_constrain(rdev, &mode);
795 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
796 current_uA, input_uV, output_uV);
800 err = rdev->desc->ops->set_mode(rdev, mode);
802 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
808 static int suspend_set_state(struct regulator_dev *rdev,
809 suspend_state_t state)
812 struct regulator_state *rstate;
814 rstate = regulator_get_suspend_state(rdev, state);
818 /* If we have no suspend mode configration don't set anything;
819 * only warn if the driver implements set_suspend_voltage or
820 * set_suspend_mode callback.
822 if (rstate->enabled != ENABLE_IN_SUSPEND &&
823 rstate->enabled != DISABLE_IN_SUSPEND) {
824 if (rdev->desc->ops->set_suspend_voltage ||
825 rdev->desc->ops->set_suspend_mode)
826 rdev_warn(rdev, "No configuration\n");
830 if (rstate->enabled == ENABLE_IN_SUSPEND &&
831 rdev->desc->ops->set_suspend_enable)
832 ret = rdev->desc->ops->set_suspend_enable(rdev);
833 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
834 rdev->desc->ops->set_suspend_disable)
835 ret = rdev->desc->ops->set_suspend_disable(rdev);
836 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
840 rdev_err(rdev, "failed to enabled/disable\n");
844 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
845 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
847 rdev_err(rdev, "failed to set voltage\n");
852 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
853 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
855 rdev_err(rdev, "failed to set mode\n");
863 static void print_constraints(struct regulator_dev *rdev)
865 struct regulation_constraints *constraints = rdev->constraints;
867 size_t len = sizeof(buf) - 1;
871 if (constraints->min_uV && constraints->max_uV) {
872 if (constraints->min_uV == constraints->max_uV)
873 count += scnprintf(buf + count, len - count, "%d mV ",
874 constraints->min_uV / 1000);
876 count += scnprintf(buf + count, len - count,
878 constraints->min_uV / 1000,
879 constraints->max_uV / 1000);
882 if (!constraints->min_uV ||
883 constraints->min_uV != constraints->max_uV) {
884 ret = _regulator_get_voltage(rdev);
886 count += scnprintf(buf + count, len - count,
887 "at %d mV ", ret / 1000);
890 if (constraints->uV_offset)
891 count += scnprintf(buf + count, len - count, "%dmV offset ",
892 constraints->uV_offset / 1000);
894 if (constraints->min_uA && constraints->max_uA) {
895 if (constraints->min_uA == constraints->max_uA)
896 count += scnprintf(buf + count, len - count, "%d mA ",
897 constraints->min_uA / 1000);
899 count += scnprintf(buf + count, len - count,
901 constraints->min_uA / 1000,
902 constraints->max_uA / 1000);
905 if (!constraints->min_uA ||
906 constraints->min_uA != constraints->max_uA) {
907 ret = _regulator_get_current_limit(rdev);
909 count += scnprintf(buf + count, len - count,
910 "at %d mA ", ret / 1000);
913 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
914 count += scnprintf(buf + count, len - count, "fast ");
915 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
916 count += scnprintf(buf + count, len - count, "normal ");
917 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
918 count += scnprintf(buf + count, len - count, "idle ");
919 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
920 count += scnprintf(buf + count, len - count, "standby");
923 scnprintf(buf, len, "no parameters");
925 rdev_dbg(rdev, "%s\n", buf);
927 if ((constraints->min_uV != constraints->max_uV) &&
928 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
930 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
933 static int machine_constraints_voltage(struct regulator_dev *rdev,
934 struct regulation_constraints *constraints)
936 const struct regulator_ops *ops = rdev->desc->ops;
939 /* do we need to apply the constraint voltage */
940 if (rdev->constraints->apply_uV &&
941 rdev->constraints->min_uV && rdev->constraints->max_uV) {
942 int target_min, target_max;
943 int current_uV = _regulator_get_voltage(rdev);
945 if (current_uV == -ENOTRECOVERABLE) {
946 /* This regulator can't be read and must be initted */
947 rdev_info(rdev, "Setting %d-%duV\n",
948 rdev->constraints->min_uV,
949 rdev->constraints->max_uV);
950 _regulator_do_set_voltage(rdev,
951 rdev->constraints->min_uV,
952 rdev->constraints->max_uV);
953 current_uV = _regulator_get_voltage(rdev);
956 if (current_uV < 0) {
958 "failed to get the current voltage(%d)\n",
964 * If we're below the minimum voltage move up to the
965 * minimum voltage, if we're above the maximum voltage
966 * then move down to the maximum.
968 target_min = current_uV;
969 target_max = current_uV;
971 if (current_uV < rdev->constraints->min_uV) {
972 target_min = rdev->constraints->min_uV;
973 target_max = rdev->constraints->min_uV;
976 if (current_uV > rdev->constraints->max_uV) {
977 target_min = rdev->constraints->max_uV;
978 target_max = rdev->constraints->max_uV;
981 if (target_min != current_uV || target_max != current_uV) {
982 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
983 current_uV, target_min, target_max);
984 ret = _regulator_do_set_voltage(
985 rdev, target_min, target_max);
988 "failed to apply %d-%duV constraint(%d)\n",
989 target_min, target_max, ret);
995 /* constrain machine-level voltage specs to fit
996 * the actual range supported by this regulator.
998 if (ops->list_voltage && rdev->desc->n_voltages) {
999 int count = rdev->desc->n_voltages;
1001 int min_uV = INT_MAX;
1002 int max_uV = INT_MIN;
1003 int cmin = constraints->min_uV;
1004 int cmax = constraints->max_uV;
1006 /* it's safe to autoconfigure fixed-voltage supplies
1007 and the constraints are used by list_voltage. */
1008 if (count == 1 && !cmin) {
1011 constraints->min_uV = cmin;
1012 constraints->max_uV = cmax;
1015 /* voltage constraints are optional */
1016 if ((cmin == 0) && (cmax == 0))
1019 /* else require explicit machine-level constraints */
1020 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1021 rdev_err(rdev, "invalid voltage constraints\n");
1025 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1026 for (i = 0; i < count; i++) {
1029 value = ops->list_voltage(rdev, i);
1033 /* maybe adjust [min_uV..max_uV] */
1034 if (value >= cmin && value < min_uV)
1036 if (value <= cmax && value > max_uV)
1040 /* final: [min_uV..max_uV] valid iff constraints valid */
1041 if (max_uV < min_uV) {
1043 "unsupportable voltage constraints %u-%uuV\n",
1048 /* use regulator's subset of machine constraints */
1049 if (constraints->min_uV < min_uV) {
1050 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1051 constraints->min_uV, min_uV);
1052 constraints->min_uV = min_uV;
1054 if (constraints->max_uV > max_uV) {
1055 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1056 constraints->max_uV, max_uV);
1057 constraints->max_uV = max_uV;
1064 static int machine_constraints_current(struct regulator_dev *rdev,
1065 struct regulation_constraints *constraints)
1067 const struct regulator_ops *ops = rdev->desc->ops;
1070 if (!constraints->min_uA && !constraints->max_uA)
1073 if (constraints->min_uA > constraints->max_uA) {
1074 rdev_err(rdev, "Invalid current constraints\n");
1078 if (!ops->set_current_limit || !ops->get_current_limit) {
1079 rdev_warn(rdev, "Operation of current configuration missing\n");
1083 /* Set regulator current in constraints range */
1084 ret = ops->set_current_limit(rdev, constraints->min_uA,
1085 constraints->max_uA);
1087 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1094 static int _regulator_do_enable(struct regulator_dev *rdev);
1097 * set_machine_constraints - sets regulator constraints
1098 * @rdev: regulator source
1099 * @constraints: constraints to apply
1101 * Allows platform initialisation code to define and constrain
1102 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1103 * Constraints *must* be set by platform code in order for some
1104 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1107 static int set_machine_constraints(struct regulator_dev *rdev,
1108 const struct regulation_constraints *constraints)
1111 const struct regulator_ops *ops = rdev->desc->ops;
1114 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1117 rdev->constraints = kzalloc(sizeof(*constraints),
1119 if (!rdev->constraints)
1122 ret = machine_constraints_voltage(rdev, rdev->constraints);
1126 ret = machine_constraints_current(rdev, rdev->constraints);
1130 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1131 ret = ops->set_input_current_limit(rdev,
1132 rdev->constraints->ilim_uA);
1134 rdev_err(rdev, "failed to set input limit\n");
1139 /* do we need to setup our suspend state */
1140 if (rdev->constraints->initial_state) {
1141 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1143 rdev_err(rdev, "failed to set suspend state\n");
1148 if (rdev->constraints->initial_mode) {
1149 if (!ops->set_mode) {
1150 rdev_err(rdev, "no set_mode operation\n");
1154 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1156 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1161 /* If the constraints say the regulator should be on at this point
1162 * and we have control then make sure it is enabled.
1164 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1165 ret = _regulator_do_enable(rdev);
1166 if (ret < 0 && ret != -EINVAL) {
1167 rdev_err(rdev, "failed to enable\n");
1172 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1173 && ops->set_ramp_delay) {
1174 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1176 rdev_err(rdev, "failed to set ramp_delay\n");
1181 if (rdev->constraints->pull_down && ops->set_pull_down) {
1182 ret = ops->set_pull_down(rdev);
1184 rdev_err(rdev, "failed to set pull down\n");
1189 if (rdev->constraints->soft_start && ops->set_soft_start) {
1190 ret = ops->set_soft_start(rdev);
1192 rdev_err(rdev, "failed to set soft start\n");
1197 if (rdev->constraints->over_current_protection
1198 && ops->set_over_current_protection) {
1199 ret = ops->set_over_current_protection(rdev);
1201 rdev_err(rdev, "failed to set over current protection\n");
1206 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1207 bool ad_state = (rdev->constraints->active_discharge ==
1208 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1210 ret = ops->set_active_discharge(rdev, ad_state);
1212 rdev_err(rdev, "failed to set active discharge\n");
1217 print_constraints(rdev);
1222 * set_supply - set regulator supply regulator
1223 * @rdev: regulator name
1224 * @supply_rdev: supply regulator name
1226 * Called by platform initialisation code to set the supply regulator for this
1227 * regulator. This ensures that a regulators supply will also be enabled by the
1228 * core if it's child is enabled.
1230 static int set_supply(struct regulator_dev *rdev,
1231 struct regulator_dev *supply_rdev)
1235 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1237 if (!try_module_get(supply_rdev->owner))
1240 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1241 if (rdev->supply == NULL) {
1245 supply_rdev->open_count++;
1251 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1252 * @rdev: regulator source
1253 * @consumer_dev_name: dev_name() string for device supply applies to
1254 * @supply: symbolic name for supply
1256 * Allows platform initialisation code to map physical regulator
1257 * sources to symbolic names for supplies for use by devices. Devices
1258 * should use these symbolic names to request regulators, avoiding the
1259 * need to provide board-specific regulator names as platform data.
1261 static int set_consumer_device_supply(struct regulator_dev *rdev,
1262 const char *consumer_dev_name,
1265 struct regulator_map *node;
1271 if (consumer_dev_name != NULL)
1276 list_for_each_entry(node, ®ulator_map_list, list) {
1277 if (node->dev_name && consumer_dev_name) {
1278 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1280 } else if (node->dev_name || consumer_dev_name) {
1284 if (strcmp(node->supply, supply) != 0)
1287 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1289 dev_name(&node->regulator->dev),
1290 node->regulator->desc->name,
1292 dev_name(&rdev->dev), rdev_get_name(rdev));
1296 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1300 node->regulator = rdev;
1301 node->supply = supply;
1304 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1305 if (node->dev_name == NULL) {
1311 list_add(&node->list, ®ulator_map_list);
1315 static void unset_regulator_supplies(struct regulator_dev *rdev)
1317 struct regulator_map *node, *n;
1319 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1320 if (rdev == node->regulator) {
1321 list_del(&node->list);
1322 kfree(node->dev_name);
1328 #ifdef CONFIG_DEBUG_FS
1329 static ssize_t constraint_flags_read_file(struct file *file,
1330 char __user *user_buf,
1331 size_t count, loff_t *ppos)
1333 const struct regulator *regulator = file->private_data;
1334 const struct regulation_constraints *c = regulator->rdev->constraints;
1341 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1345 ret = snprintf(buf, PAGE_SIZE,
1349 "ramp_disable: %u\n"
1352 "over_current_protection: %u\n",
1359 c->over_current_protection);
1361 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1369 static const struct file_operations constraint_flags_fops = {
1370 #ifdef CONFIG_DEBUG_FS
1371 .open = simple_open,
1372 .read = constraint_flags_read_file,
1373 .llseek = default_llseek,
1377 #define REG_STR_SIZE 64
1379 static struct regulator *create_regulator(struct regulator_dev *rdev,
1381 const char *supply_name)
1383 struct regulator *regulator;
1384 char buf[REG_STR_SIZE];
1387 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1388 if (regulator == NULL)
1391 regulator_lock(rdev);
1392 regulator->rdev = rdev;
1393 list_add(®ulator->list, &rdev->consumer_list);
1396 regulator->dev = dev;
1398 /* Add a link to the device sysfs entry */
1399 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1400 dev->kobj.name, supply_name);
1401 if (size >= REG_STR_SIZE)
1404 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1405 if (regulator->supply_name == NULL)
1408 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1411 rdev_dbg(rdev, "could not add device link %s err %d\n",
1412 dev->kobj.name, err);
1416 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1417 if (regulator->supply_name == NULL)
1421 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1423 if (!regulator->debugfs) {
1424 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1426 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1427 ®ulator->uA_load);
1428 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1429 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1430 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1431 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1432 debugfs_create_file("constraint_flags", 0444,
1433 regulator->debugfs, regulator,
1434 &constraint_flags_fops);
1438 * Check now if the regulator is an always on regulator - if
1439 * it is then we don't need to do nearly so much work for
1440 * enable/disable calls.
1442 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1443 _regulator_is_enabled(rdev))
1444 regulator->always_on = true;
1446 regulator_unlock(rdev);
1449 list_del(®ulator->list);
1451 regulator_unlock(rdev);
1455 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1457 if (rdev->constraints && rdev->constraints->enable_time)
1458 return rdev->constraints->enable_time;
1459 if (!rdev->desc->ops->enable_time)
1460 return rdev->desc->enable_time;
1461 return rdev->desc->ops->enable_time(rdev);
1464 static struct regulator_supply_alias *regulator_find_supply_alias(
1465 struct device *dev, const char *supply)
1467 struct regulator_supply_alias *map;
1469 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1470 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1476 static void regulator_supply_alias(struct device **dev, const char **supply)
1478 struct regulator_supply_alias *map;
1480 map = regulator_find_supply_alias(*dev, *supply);
1482 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1483 *supply, map->alias_supply,
1484 dev_name(map->alias_dev));
1485 *dev = map->alias_dev;
1486 *supply = map->alias_supply;
1490 static int regulator_match(struct device *dev, const void *data)
1492 struct regulator_dev *r = dev_to_rdev(dev);
1494 return strcmp(rdev_get_name(r), data) == 0;
1497 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1501 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1503 return dev ? dev_to_rdev(dev) : NULL;
1507 * regulator_dev_lookup - lookup a regulator device.
1508 * @dev: device for regulator "consumer".
1509 * @supply: Supply name or regulator ID.
1511 * If successful, returns a struct regulator_dev that corresponds to the name
1512 * @supply and with the embedded struct device refcount incremented by one.
1513 * The refcount must be dropped by calling put_device().
1514 * On failure one of the following ERR-PTR-encoded values is returned:
1515 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1518 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1521 struct regulator_dev *r = NULL;
1522 struct device_node *node;
1523 struct regulator_map *map;
1524 const char *devname = NULL;
1526 regulator_supply_alias(&dev, &supply);
1528 /* first do a dt based lookup */
1529 if (dev && dev->of_node) {
1530 node = of_get_regulator(dev, supply);
1532 r = of_find_regulator_by_node(node);
1537 * We have a node, but there is no device.
1538 * assume it has not registered yet.
1540 return ERR_PTR(-EPROBE_DEFER);
1544 /* if not found, try doing it non-dt way */
1546 devname = dev_name(dev);
1548 mutex_lock(®ulator_list_mutex);
1549 list_for_each_entry(map, ®ulator_map_list, list) {
1550 /* If the mapping has a device set up it must match */
1551 if (map->dev_name &&
1552 (!devname || strcmp(map->dev_name, devname)))
1555 if (strcmp(map->supply, supply) == 0 &&
1556 get_device(&map->regulator->dev)) {
1561 mutex_unlock(®ulator_list_mutex);
1566 r = regulator_lookup_by_name(supply);
1570 return ERR_PTR(-ENODEV);
1573 static int regulator_resolve_supply(struct regulator_dev *rdev)
1575 struct regulator_dev *r;
1576 struct device *dev = rdev->dev.parent;
1579 /* No supply to resovle? */
1580 if (!rdev->supply_name)
1583 /* Supply already resolved? */
1587 r = regulator_dev_lookup(dev, rdev->supply_name);
1591 /* Did the lookup explicitly defer for us? */
1592 if (ret == -EPROBE_DEFER)
1595 if (have_full_constraints()) {
1596 r = dummy_regulator_rdev;
1597 get_device(&r->dev);
1599 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1600 rdev->supply_name, rdev->desc->name);
1601 return -EPROBE_DEFER;
1606 * If the supply's parent device is not the same as the
1607 * regulator's parent device, then ensure the parent device
1608 * is bound before we resolve the supply, in case the parent
1609 * device get probe deferred and unregisters the supply.
1611 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1612 if (!device_is_bound(r->dev.parent)) {
1613 put_device(&r->dev);
1614 return -EPROBE_DEFER;
1618 /* Recursively resolve the supply of the supply */
1619 ret = regulator_resolve_supply(r);
1621 put_device(&r->dev);
1625 ret = set_supply(rdev, r);
1627 put_device(&r->dev);
1631 /* Cascade always-on state to supply */
1632 if (_regulator_is_enabled(rdev)) {
1633 ret = regulator_enable(rdev->supply);
1635 _regulator_put(rdev->supply);
1636 rdev->supply = NULL;
1644 /* Internal regulator request function */
1645 struct regulator *_regulator_get(struct device *dev, const char *id,
1646 enum regulator_get_type get_type)
1648 struct regulator_dev *rdev;
1649 struct regulator *regulator;
1650 const char *devname = dev ? dev_name(dev) : "deviceless";
1653 if (get_type >= MAX_GET_TYPE) {
1654 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1655 return ERR_PTR(-EINVAL);
1659 pr_err("get() with no identifier\n");
1660 return ERR_PTR(-EINVAL);
1663 rdev = regulator_dev_lookup(dev, id);
1665 ret = PTR_ERR(rdev);
1668 * If regulator_dev_lookup() fails with error other
1669 * than -ENODEV our job here is done, we simply return it.
1672 return ERR_PTR(ret);
1674 if (!have_full_constraints()) {
1676 "incomplete constraints, dummy supplies not allowed\n");
1677 return ERR_PTR(-ENODEV);
1683 * Assume that a regulator is physically present and
1684 * enabled, even if it isn't hooked up, and just
1688 "%s supply %s not found, using dummy regulator\n",
1690 rdev = dummy_regulator_rdev;
1691 get_device(&rdev->dev);
1696 "dummy supplies not allowed for exclusive requests\n");
1700 return ERR_PTR(-ENODEV);
1704 if (rdev->exclusive) {
1705 regulator = ERR_PTR(-EPERM);
1706 put_device(&rdev->dev);
1710 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1711 regulator = ERR_PTR(-EBUSY);
1712 put_device(&rdev->dev);
1716 ret = regulator_resolve_supply(rdev);
1718 regulator = ERR_PTR(ret);
1719 put_device(&rdev->dev);
1723 if (!try_module_get(rdev->owner)) {
1724 regulator = ERR_PTR(-EPROBE_DEFER);
1725 put_device(&rdev->dev);
1729 regulator = create_regulator(rdev, dev, id);
1730 if (regulator == NULL) {
1731 regulator = ERR_PTR(-ENOMEM);
1732 put_device(&rdev->dev);
1733 module_put(rdev->owner);
1738 if (get_type == EXCLUSIVE_GET) {
1739 rdev->exclusive = 1;
1741 ret = _regulator_is_enabled(rdev);
1743 rdev->use_count = 1;
1745 rdev->use_count = 0;
1748 device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1754 * regulator_get - lookup and obtain a reference to a regulator.
1755 * @dev: device for regulator "consumer"
1756 * @id: Supply name or regulator ID.
1758 * Returns a struct regulator corresponding to the regulator producer,
1759 * or IS_ERR() condition containing errno.
1761 * Use of supply names configured via regulator_set_device_supply() is
1762 * strongly encouraged. It is recommended that the supply name used
1763 * should match the name used for the supply and/or the relevant
1764 * device pins in the datasheet.
1766 struct regulator *regulator_get(struct device *dev, const char *id)
1768 return _regulator_get(dev, id, NORMAL_GET);
1770 EXPORT_SYMBOL_GPL(regulator_get);
1773 * regulator_get_exclusive - obtain exclusive access to a regulator.
1774 * @dev: device for regulator "consumer"
1775 * @id: Supply name or regulator ID.
1777 * Returns a struct regulator corresponding to the regulator producer,
1778 * or IS_ERR() condition containing errno. Other consumers will be
1779 * unable to obtain this regulator while this reference is held and the
1780 * use count for the regulator will be initialised to reflect the current
1781 * state of the regulator.
1783 * This is intended for use by consumers which cannot tolerate shared
1784 * use of the regulator such as those which need to force the
1785 * regulator off for correct operation of the hardware they are
1788 * Use of supply names configured via regulator_set_device_supply() is
1789 * strongly encouraged. It is recommended that the supply name used
1790 * should match the name used for the supply and/or the relevant
1791 * device pins in the datasheet.
1793 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1795 return _regulator_get(dev, id, EXCLUSIVE_GET);
1797 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1800 * regulator_get_optional - obtain optional access to a regulator.
1801 * @dev: device for regulator "consumer"
1802 * @id: Supply name or regulator ID.
1804 * Returns a struct regulator corresponding to the regulator producer,
1805 * or IS_ERR() condition containing errno.
1807 * This is intended for use by consumers for devices which can have
1808 * some supplies unconnected in normal use, such as some MMC devices.
1809 * It can allow the regulator core to provide stub supplies for other
1810 * supplies requested using normal regulator_get() calls without
1811 * disrupting the operation of drivers that can handle absent
1814 * Use of supply names configured via regulator_set_device_supply() is
1815 * strongly encouraged. It is recommended that the supply name used
1816 * should match the name used for the supply and/or the relevant
1817 * device pins in the datasheet.
1819 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1821 return _regulator_get(dev, id, OPTIONAL_GET);
1823 EXPORT_SYMBOL_GPL(regulator_get_optional);
1825 /* regulator_list_mutex lock held by regulator_put() */
1826 static void _regulator_put(struct regulator *regulator)
1828 struct regulator_dev *rdev;
1830 if (IS_ERR_OR_NULL(regulator))
1833 lockdep_assert_held_once(®ulator_list_mutex);
1835 rdev = regulator->rdev;
1837 debugfs_remove_recursive(regulator->debugfs);
1839 if (regulator->dev) {
1841 struct regulator *r;
1843 list_for_each_entry(r, &rdev->consumer_list, list)
1844 if (r->dev == regulator->dev)
1848 device_link_remove(regulator->dev, &rdev->dev);
1850 /* remove any sysfs entries */
1851 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1854 regulator_lock(rdev);
1855 list_del(®ulator->list);
1858 rdev->exclusive = 0;
1859 put_device(&rdev->dev);
1860 regulator_unlock(rdev);
1862 kfree_const(regulator->supply_name);
1865 module_put(rdev->owner);
1869 * regulator_put - "free" the regulator source
1870 * @regulator: regulator source
1872 * Note: drivers must ensure that all regulator_enable calls made on this
1873 * regulator source are balanced by regulator_disable calls prior to calling
1876 void regulator_put(struct regulator *regulator)
1878 mutex_lock(®ulator_list_mutex);
1879 _regulator_put(regulator);
1880 mutex_unlock(®ulator_list_mutex);
1882 EXPORT_SYMBOL_GPL(regulator_put);
1885 * regulator_register_supply_alias - Provide device alias for supply lookup
1887 * @dev: device that will be given as the regulator "consumer"
1888 * @id: Supply name or regulator ID
1889 * @alias_dev: device that should be used to lookup the supply
1890 * @alias_id: Supply name or regulator ID that should be used to lookup the
1893 * All lookups for id on dev will instead be conducted for alias_id on
1896 int regulator_register_supply_alias(struct device *dev, const char *id,
1897 struct device *alias_dev,
1898 const char *alias_id)
1900 struct regulator_supply_alias *map;
1902 map = regulator_find_supply_alias(dev, id);
1906 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1911 map->src_supply = id;
1912 map->alias_dev = alias_dev;
1913 map->alias_supply = alias_id;
1915 list_add(&map->list, ®ulator_supply_alias_list);
1917 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1918 id, dev_name(dev), alias_id, dev_name(alias_dev));
1922 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1925 * regulator_unregister_supply_alias - Remove device alias
1927 * @dev: device that will be given as the regulator "consumer"
1928 * @id: Supply name or regulator ID
1930 * Remove a lookup alias if one exists for id on dev.
1932 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1934 struct regulator_supply_alias *map;
1936 map = regulator_find_supply_alias(dev, id);
1938 list_del(&map->list);
1942 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1945 * regulator_bulk_register_supply_alias - register multiple aliases
1947 * @dev: device that will be given as the regulator "consumer"
1948 * @id: List of supply names or regulator IDs
1949 * @alias_dev: device that should be used to lookup the supply
1950 * @alias_id: List of supply names or regulator IDs that should be used to
1952 * @num_id: Number of aliases to register
1954 * @return 0 on success, an errno on failure.
1956 * This helper function allows drivers to register several supply
1957 * aliases in one operation. If any of the aliases cannot be
1958 * registered any aliases that were registered will be removed
1959 * before returning to the caller.
1961 int regulator_bulk_register_supply_alias(struct device *dev,
1962 const char *const *id,
1963 struct device *alias_dev,
1964 const char *const *alias_id,
1970 for (i = 0; i < num_id; ++i) {
1971 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1981 "Failed to create supply alias %s,%s -> %s,%s\n",
1982 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1985 regulator_unregister_supply_alias(dev, id[i]);
1989 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1992 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1994 * @dev: device that will be given as the regulator "consumer"
1995 * @id: List of supply names or regulator IDs
1996 * @num_id: Number of aliases to unregister
1998 * This helper function allows drivers to unregister several supply
1999 * aliases in one operation.
2001 void regulator_bulk_unregister_supply_alias(struct device *dev,
2002 const char *const *id,
2007 for (i = 0; i < num_id; ++i)
2008 regulator_unregister_supply_alias(dev, id[i]);
2010 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2013 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2014 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2015 const struct regulator_config *config)
2017 struct regulator_enable_gpio *pin;
2018 struct gpio_desc *gpiod;
2021 if (config->ena_gpiod)
2022 gpiod = config->ena_gpiod;
2024 gpiod = gpio_to_desc(config->ena_gpio);
2026 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
2027 if (pin->gpiod == gpiod) {
2028 rdev_dbg(rdev, "GPIO %d is already used\n",
2030 goto update_ena_gpio_to_rdev;
2034 if (!config->ena_gpiod) {
2035 ret = gpio_request_one(config->ena_gpio,
2036 GPIOF_DIR_OUT | config->ena_gpio_flags,
2037 rdev_get_name(rdev));
2042 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2044 if (!config->ena_gpiod)
2045 gpio_free(config->ena_gpio);
2050 pin->ena_gpio_invert = config->ena_gpio_invert;
2051 list_add(&pin->list, ®ulator_ena_gpio_list);
2053 update_ena_gpio_to_rdev:
2054 pin->request_count++;
2055 rdev->ena_pin = pin;
2059 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2061 struct regulator_enable_gpio *pin, *n;
2066 /* Free the GPIO only in case of no use */
2067 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
2068 if (pin->gpiod == rdev->ena_pin->gpiod) {
2069 if (pin->request_count <= 1) {
2070 pin->request_count = 0;
2071 gpiod_put(pin->gpiod);
2072 list_del(&pin->list);
2074 rdev->ena_pin = NULL;
2077 pin->request_count--;
2084 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2085 * @rdev: regulator_dev structure
2086 * @enable: enable GPIO at initial use?
2088 * GPIO is enabled in case of initial use. (enable_count is 0)
2089 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2091 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2093 struct regulator_enable_gpio *pin = rdev->ena_pin;
2099 /* Enable GPIO at initial use */
2100 if (pin->enable_count == 0)
2101 gpiod_set_value_cansleep(pin->gpiod,
2102 !pin->ena_gpio_invert);
2104 pin->enable_count++;
2106 if (pin->enable_count > 1) {
2107 pin->enable_count--;
2111 /* Disable GPIO if not used */
2112 if (pin->enable_count <= 1) {
2113 gpiod_set_value_cansleep(pin->gpiod,
2114 pin->ena_gpio_invert);
2115 pin->enable_count = 0;
2123 * _regulator_enable_delay - a delay helper function
2124 * @delay: time to delay in microseconds
2126 * Delay for the requested amount of time as per the guidelines in:
2128 * Documentation/timers/timers-howto.txt
2130 * The assumption here is that regulators will never be enabled in
2131 * atomic context and therefore sleeping functions can be used.
2133 static void _regulator_enable_delay(unsigned int delay)
2135 unsigned int ms = delay / 1000;
2136 unsigned int us = delay % 1000;
2140 * For small enough values, handle super-millisecond
2141 * delays in the usleep_range() call below.
2150 * Give the scheduler some room to coalesce with any other
2151 * wakeup sources. For delays shorter than 10 us, don't even
2152 * bother setting up high-resolution timers and just busy-
2156 usleep_range(us, us + 100);
2161 static int _regulator_do_enable(struct regulator_dev *rdev)
2165 /* Query before enabling in case configuration dependent. */
2166 ret = _regulator_get_enable_time(rdev);
2170 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2174 trace_regulator_enable(rdev_get_name(rdev));
2176 if (rdev->desc->off_on_delay) {
2177 /* if needed, keep a distance of off_on_delay from last time
2178 * this regulator was disabled.
2180 unsigned long start_jiffy = jiffies;
2181 unsigned long intended, max_delay, remaining;
2183 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2184 intended = rdev->last_off_jiffy + max_delay;
2186 if (time_before(start_jiffy, intended)) {
2187 /* calc remaining jiffies to deal with one-time
2189 * in case of multiple timer wrapping, either it can be
2190 * detected by out-of-range remaining, or it cannot be
2191 * detected and we gets a panelty of
2192 * _regulator_enable_delay().
2194 remaining = intended - start_jiffy;
2195 if (remaining <= max_delay)
2196 _regulator_enable_delay(
2197 jiffies_to_usecs(remaining));
2201 if (rdev->ena_pin) {
2202 if (!rdev->ena_gpio_state) {
2203 ret = regulator_ena_gpio_ctrl(rdev, true);
2206 rdev->ena_gpio_state = 1;
2208 } else if (rdev->desc->ops->enable) {
2209 ret = rdev->desc->ops->enable(rdev);
2216 /* Allow the regulator to ramp; it would be useful to extend
2217 * this for bulk operations so that the regulators can ramp
2219 trace_regulator_enable_delay(rdev_get_name(rdev));
2221 _regulator_enable_delay(delay);
2223 trace_regulator_enable_complete(rdev_get_name(rdev));
2228 /* locks held by regulator_enable() */
2229 static int _regulator_enable(struct regulator_dev *rdev)
2233 lockdep_assert_held_once(&rdev->mutex);
2235 /* check voltage and requested load before enabling */
2236 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2237 drms_uA_update(rdev);
2239 if (rdev->use_count == 0) {
2240 /* The regulator may on if it's not switchable or left on */
2241 ret = _regulator_is_enabled(rdev);
2242 if (ret == -EINVAL || ret == 0) {
2243 if (!regulator_ops_is_valid(rdev,
2244 REGULATOR_CHANGE_STATUS))
2247 ret = _regulator_do_enable(rdev);
2251 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2253 } else if (ret < 0) {
2254 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2257 /* Fallthrough on positive return values - already enabled */
2266 * regulator_enable - enable regulator output
2267 * @regulator: regulator source
2269 * Request that the regulator be enabled with the regulator output at
2270 * the predefined voltage or current value. Calls to regulator_enable()
2271 * must be balanced with calls to regulator_disable().
2273 * NOTE: the output value can be set by other drivers, boot loader or may be
2274 * hardwired in the regulator.
2276 int regulator_enable(struct regulator *regulator)
2278 struct regulator_dev *rdev = regulator->rdev;
2281 if (regulator->always_on)
2285 ret = regulator_enable(rdev->supply);
2290 mutex_lock(&rdev->mutex);
2291 ret = _regulator_enable(rdev);
2292 mutex_unlock(&rdev->mutex);
2294 if (ret != 0 && rdev->supply)
2295 regulator_disable(rdev->supply);
2299 EXPORT_SYMBOL_GPL(regulator_enable);
2301 static int _regulator_do_disable(struct regulator_dev *rdev)
2305 trace_regulator_disable(rdev_get_name(rdev));
2307 if (rdev->ena_pin) {
2308 if (rdev->ena_gpio_state) {
2309 ret = regulator_ena_gpio_ctrl(rdev, false);
2312 rdev->ena_gpio_state = 0;
2315 } else if (rdev->desc->ops->disable) {
2316 ret = rdev->desc->ops->disable(rdev);
2321 /* cares about last_off_jiffy only if off_on_delay is required by
2324 if (rdev->desc->off_on_delay)
2325 rdev->last_off_jiffy = jiffies;
2327 trace_regulator_disable_complete(rdev_get_name(rdev));
2332 /* locks held by regulator_disable() */
2333 static int _regulator_disable(struct regulator_dev *rdev)
2337 lockdep_assert_held_once(&rdev->mutex);
2339 if (WARN(rdev->use_count <= 0,
2340 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2343 /* are we the last user and permitted to disable ? */
2344 if (rdev->use_count == 1 &&
2345 (rdev->constraints && !rdev->constraints->always_on)) {
2347 /* we are last user */
2348 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2349 ret = _notifier_call_chain(rdev,
2350 REGULATOR_EVENT_PRE_DISABLE,
2352 if (ret & NOTIFY_STOP_MASK)
2355 ret = _regulator_do_disable(rdev);
2357 rdev_err(rdev, "failed to disable\n");
2358 _notifier_call_chain(rdev,
2359 REGULATOR_EVENT_ABORT_DISABLE,
2363 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2367 rdev->use_count = 0;
2368 } else if (rdev->use_count > 1) {
2369 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2370 drms_uA_update(rdev);
2379 * regulator_disable - disable regulator output
2380 * @regulator: regulator source
2382 * Disable the regulator output voltage or current. Calls to
2383 * regulator_enable() must be balanced with calls to
2384 * regulator_disable().
2386 * NOTE: this will only disable the regulator output if no other consumer
2387 * devices have it enabled, the regulator device supports disabling and
2388 * machine constraints permit this operation.
2390 int regulator_disable(struct regulator *regulator)
2392 struct regulator_dev *rdev = regulator->rdev;
2395 if (regulator->always_on)
2398 mutex_lock(&rdev->mutex);
2399 ret = _regulator_disable(rdev);
2400 mutex_unlock(&rdev->mutex);
2402 if (ret == 0 && rdev->supply)
2403 regulator_disable(rdev->supply);
2407 EXPORT_SYMBOL_GPL(regulator_disable);
2409 /* locks held by regulator_force_disable() */
2410 static int _regulator_force_disable(struct regulator_dev *rdev)
2414 lockdep_assert_held_once(&rdev->mutex);
2416 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2417 REGULATOR_EVENT_PRE_DISABLE, NULL);
2418 if (ret & NOTIFY_STOP_MASK)
2421 ret = _regulator_do_disable(rdev);
2423 rdev_err(rdev, "failed to force disable\n");
2424 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2425 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2429 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2430 REGULATOR_EVENT_DISABLE, NULL);
2436 * regulator_force_disable - force disable regulator output
2437 * @regulator: regulator source
2439 * Forcibly disable the regulator output voltage or current.
2440 * NOTE: this *will* disable the regulator output even if other consumer
2441 * devices have it enabled. This should be used for situations when device
2442 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2444 int regulator_force_disable(struct regulator *regulator)
2446 struct regulator_dev *rdev = regulator->rdev;
2449 mutex_lock(&rdev->mutex);
2450 regulator->uA_load = 0;
2451 ret = _regulator_force_disable(regulator->rdev);
2452 mutex_unlock(&rdev->mutex);
2455 while (rdev->open_count--)
2456 regulator_disable(rdev->supply);
2460 EXPORT_SYMBOL_GPL(regulator_force_disable);
2462 static void regulator_disable_work(struct work_struct *work)
2464 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2468 regulator_lock(rdev);
2470 BUG_ON(!rdev->deferred_disables);
2472 count = rdev->deferred_disables;
2473 rdev->deferred_disables = 0;
2476 * Workqueue functions queue the new work instance while the previous
2477 * work instance is being processed. Cancel the queued work instance
2478 * as the work instance under processing does the job of the queued
2481 cancel_delayed_work(&rdev->disable_work);
2483 for (i = 0; i < count; i++) {
2484 ret = _regulator_disable(rdev);
2486 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2489 regulator_unlock(rdev);
2492 for (i = 0; i < count; i++) {
2493 ret = regulator_disable(rdev->supply);
2496 "Supply disable failed: %d\n", ret);
2503 * regulator_disable_deferred - disable regulator output with delay
2504 * @regulator: regulator source
2505 * @ms: miliseconds until the regulator is disabled
2507 * Execute regulator_disable() on the regulator after a delay. This
2508 * is intended for use with devices that require some time to quiesce.
2510 * NOTE: this will only disable the regulator output if no other consumer
2511 * devices have it enabled, the regulator device supports disabling and
2512 * machine constraints permit this operation.
2514 int regulator_disable_deferred(struct regulator *regulator, int ms)
2516 struct regulator_dev *rdev = regulator->rdev;
2518 if (regulator->always_on)
2522 return regulator_disable(regulator);
2524 regulator_lock(rdev);
2525 rdev->deferred_disables++;
2526 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2527 msecs_to_jiffies(ms));
2528 regulator_unlock(rdev);
2532 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2534 static int _regulator_is_enabled(struct regulator_dev *rdev)
2536 /* A GPIO control always takes precedence */
2538 return rdev->ena_gpio_state;
2540 /* If we don't know then assume that the regulator is always on */
2541 if (!rdev->desc->ops->is_enabled)
2544 return rdev->desc->ops->is_enabled(rdev);
2547 static int _regulator_list_voltage(struct regulator_dev *rdev,
2548 unsigned selector, int lock)
2550 const struct regulator_ops *ops = rdev->desc->ops;
2553 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2554 return rdev->desc->fixed_uV;
2556 if (ops->list_voltage) {
2557 if (selector >= rdev->desc->n_voltages)
2560 regulator_lock(rdev);
2561 ret = ops->list_voltage(rdev, selector);
2563 regulator_unlock(rdev);
2564 } else if (rdev->is_switch && rdev->supply) {
2565 ret = _regulator_list_voltage(rdev->supply->rdev,
2572 if (ret < rdev->constraints->min_uV)
2574 else if (ret > rdev->constraints->max_uV)
2582 * regulator_is_enabled - is the regulator output enabled
2583 * @regulator: regulator source
2585 * Returns positive if the regulator driver backing the source/client
2586 * has requested that the device be enabled, zero if it hasn't, else a
2587 * negative errno code.
2589 * Note that the device backing this regulator handle can have multiple
2590 * users, so it might be enabled even if regulator_enable() was never
2591 * called for this particular source.
2593 int regulator_is_enabled(struct regulator *regulator)
2597 if (regulator->always_on)
2600 mutex_lock(®ulator->rdev->mutex);
2601 ret = _regulator_is_enabled(regulator->rdev);
2602 mutex_unlock(®ulator->rdev->mutex);
2606 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2609 * regulator_count_voltages - count regulator_list_voltage() selectors
2610 * @regulator: regulator source
2612 * Returns number of selectors, or negative errno. Selectors are
2613 * numbered starting at zero, and typically correspond to bitfields
2614 * in hardware registers.
2616 int regulator_count_voltages(struct regulator *regulator)
2618 struct regulator_dev *rdev = regulator->rdev;
2620 if (rdev->desc->n_voltages)
2621 return rdev->desc->n_voltages;
2623 if (!rdev->is_switch || !rdev->supply)
2626 return regulator_count_voltages(rdev->supply);
2628 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2631 * regulator_list_voltage - enumerate supported voltages
2632 * @regulator: regulator source
2633 * @selector: identify voltage to list
2634 * Context: can sleep
2636 * Returns a voltage that can be passed to @regulator_set_voltage(),
2637 * zero if this selector code can't be used on this system, or a
2640 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2642 return _regulator_list_voltage(regulator->rdev, selector, 1);
2644 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2647 * regulator_get_regmap - get the regulator's register map
2648 * @regulator: regulator source
2650 * Returns the register map for the given regulator, or an ERR_PTR value
2651 * if the regulator doesn't use regmap.
2653 struct regmap *regulator_get_regmap(struct regulator *regulator)
2655 struct regmap *map = regulator->rdev->regmap;
2657 return map ? map : ERR_PTR(-EOPNOTSUPP);
2661 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2662 * @regulator: regulator source
2663 * @vsel_reg: voltage selector register, output parameter
2664 * @vsel_mask: mask for voltage selector bitfield, output parameter
2666 * Returns the hardware register offset and bitmask used for setting the
2667 * regulator voltage. This might be useful when configuring voltage-scaling
2668 * hardware or firmware that can make I2C requests behind the kernel's back,
2671 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2672 * and 0 is returned, otherwise a negative errno is returned.
2674 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2676 unsigned *vsel_mask)
2678 struct regulator_dev *rdev = regulator->rdev;
2679 const struct regulator_ops *ops = rdev->desc->ops;
2681 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2684 *vsel_reg = rdev->desc->vsel_reg;
2685 *vsel_mask = rdev->desc->vsel_mask;
2689 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2692 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2693 * @regulator: regulator source
2694 * @selector: identify voltage to list
2696 * Converts the selector to a hardware-specific voltage selector that can be
2697 * directly written to the regulator registers. The address of the voltage
2698 * register can be determined by calling @regulator_get_hardware_vsel_register.
2700 * On error a negative errno is returned.
2702 int regulator_list_hardware_vsel(struct regulator *regulator,
2705 struct regulator_dev *rdev = regulator->rdev;
2706 const struct regulator_ops *ops = rdev->desc->ops;
2708 if (selector >= rdev->desc->n_voltages)
2710 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2715 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2718 * regulator_get_linear_step - return the voltage step size between VSEL values
2719 * @regulator: regulator source
2721 * Returns the voltage step size between VSEL values for linear
2722 * regulators, or return 0 if the regulator isn't a linear regulator.
2724 unsigned int regulator_get_linear_step(struct regulator *regulator)
2726 struct regulator_dev *rdev = regulator->rdev;
2728 return rdev->desc->uV_step;
2730 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2733 * regulator_is_supported_voltage - check if a voltage range can be supported
2735 * @regulator: Regulator to check.
2736 * @min_uV: Minimum required voltage in uV.
2737 * @max_uV: Maximum required voltage in uV.
2739 * Returns a boolean or a negative error code.
2741 int regulator_is_supported_voltage(struct regulator *regulator,
2742 int min_uV, int max_uV)
2744 struct regulator_dev *rdev = regulator->rdev;
2745 int i, voltages, ret;
2747 /* If we can't change voltage check the current voltage */
2748 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2749 ret = regulator_get_voltage(regulator);
2751 return min_uV <= ret && ret <= max_uV;
2756 /* Any voltage within constrains range is fine? */
2757 if (rdev->desc->continuous_voltage_range)
2758 return min_uV >= rdev->constraints->min_uV &&
2759 max_uV <= rdev->constraints->max_uV;
2761 ret = regulator_count_voltages(regulator);
2766 for (i = 0; i < voltages; i++) {
2767 ret = regulator_list_voltage(regulator, i);
2769 if (ret >= min_uV && ret <= max_uV)
2775 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2777 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2780 const struct regulator_desc *desc = rdev->desc;
2782 if (desc->ops->map_voltage)
2783 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2785 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2786 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2788 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2789 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2791 if (desc->ops->list_voltage ==
2792 regulator_list_voltage_pickable_linear_range)
2793 return regulator_map_voltage_pickable_linear_range(rdev,
2796 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2799 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2800 int min_uV, int max_uV,
2803 struct pre_voltage_change_data data;
2806 data.old_uV = _regulator_get_voltage(rdev);
2807 data.min_uV = min_uV;
2808 data.max_uV = max_uV;
2809 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2811 if (ret & NOTIFY_STOP_MASK)
2814 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2818 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2819 (void *)data.old_uV);
2824 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2825 int uV, unsigned selector)
2827 struct pre_voltage_change_data data;
2830 data.old_uV = _regulator_get_voltage(rdev);
2833 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2835 if (ret & NOTIFY_STOP_MASK)
2838 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2842 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2843 (void *)data.old_uV);
2848 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2849 int old_uV, int new_uV)
2851 unsigned int ramp_delay = 0;
2853 if (rdev->constraints->ramp_delay)
2854 ramp_delay = rdev->constraints->ramp_delay;
2855 else if (rdev->desc->ramp_delay)
2856 ramp_delay = rdev->desc->ramp_delay;
2857 else if (rdev->constraints->settling_time)
2858 return rdev->constraints->settling_time;
2859 else if (rdev->constraints->settling_time_up &&
2861 return rdev->constraints->settling_time_up;
2862 else if (rdev->constraints->settling_time_down &&
2864 return rdev->constraints->settling_time_down;
2866 if (ramp_delay == 0) {
2867 rdev_dbg(rdev, "ramp_delay not set\n");
2871 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2874 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2875 int min_uV, int max_uV)
2880 unsigned int selector;
2881 int old_selector = -1;
2882 const struct regulator_ops *ops = rdev->desc->ops;
2883 int old_uV = _regulator_get_voltage(rdev);
2885 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2887 min_uV += rdev->constraints->uV_offset;
2888 max_uV += rdev->constraints->uV_offset;
2891 * If we can't obtain the old selector there is not enough
2892 * info to call set_voltage_time_sel().
2894 if (_regulator_is_enabled(rdev) &&
2895 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2896 old_selector = ops->get_voltage_sel(rdev);
2897 if (old_selector < 0)
2898 return old_selector;
2901 if (ops->set_voltage) {
2902 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2906 if (ops->list_voltage)
2907 best_val = ops->list_voltage(rdev,
2910 best_val = _regulator_get_voltage(rdev);
2913 } else if (ops->set_voltage_sel) {
2914 ret = regulator_map_voltage(rdev, min_uV, max_uV);
2916 best_val = ops->list_voltage(rdev, ret);
2917 if (min_uV <= best_val && max_uV >= best_val) {
2919 if (old_selector == selector)
2922 ret = _regulator_call_set_voltage_sel(
2923 rdev, best_val, selector);
2935 if (ops->set_voltage_time_sel) {
2937 * Call set_voltage_time_sel if successfully obtained
2940 if (old_selector >= 0 && old_selector != selector)
2941 delay = ops->set_voltage_time_sel(rdev, old_selector,
2944 if (old_uV != best_val) {
2945 if (ops->set_voltage_time)
2946 delay = ops->set_voltage_time(rdev, old_uV,
2949 delay = _regulator_set_voltage_time(rdev,
2956 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2960 /* Insert any necessary delays */
2961 if (delay >= 1000) {
2962 mdelay(delay / 1000);
2963 udelay(delay % 1000);
2968 if (best_val >= 0) {
2969 unsigned long data = best_val;
2971 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2976 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2981 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
2982 int min_uV, int max_uV, suspend_state_t state)
2984 struct regulator_state *rstate;
2987 rstate = regulator_get_suspend_state(rdev, state);
2991 if (min_uV < rstate->min_uV)
2992 min_uV = rstate->min_uV;
2993 if (max_uV > rstate->max_uV)
2994 max_uV = rstate->max_uV;
2996 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3000 uV = rdev->desc->ops->list_voltage(rdev, sel);
3001 if (uV >= min_uV && uV <= max_uV)
3007 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3008 int min_uV, int max_uV,
3009 suspend_state_t state)
3011 struct regulator_dev *rdev = regulator->rdev;
3012 struct regulator_voltage *voltage = ®ulator->voltage[state];
3014 int old_min_uV, old_max_uV;
3016 int best_supply_uV = 0;
3017 int supply_change_uV = 0;
3019 /* If we're setting the same range as last time the change
3020 * should be a noop (some cpufreq implementations use the same
3021 * voltage for multiple frequencies, for example).
3023 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3026 /* If we're trying to set a range that overlaps the current voltage,
3027 * return successfully even though the regulator does not support
3028 * changing the voltage.
3030 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3031 current_uV = _regulator_get_voltage(rdev);
3032 if (min_uV <= current_uV && current_uV <= max_uV) {
3033 voltage->min_uV = min_uV;
3034 voltage->max_uV = max_uV;
3040 if (!rdev->desc->ops->set_voltage &&
3041 !rdev->desc->ops->set_voltage_sel) {
3046 /* constraints check */
3047 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3051 /* restore original values in case of error */
3052 old_min_uV = voltage->min_uV;
3053 old_max_uV = voltage->max_uV;
3054 voltage->min_uV = min_uV;
3055 voltage->max_uV = max_uV;
3057 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
3062 regulator_ops_is_valid(rdev->supply->rdev,
3063 REGULATOR_CHANGE_VOLTAGE) &&
3064 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3065 rdev->desc->ops->get_voltage_sel))) {
3066 int current_supply_uV;
3069 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3075 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3076 if (best_supply_uV < 0) {
3077 ret = best_supply_uV;
3081 best_supply_uV += rdev->desc->min_dropout_uV;
3083 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3084 if (current_supply_uV < 0) {
3085 ret = current_supply_uV;
3089 supply_change_uV = best_supply_uV - current_supply_uV;
3092 if (supply_change_uV > 0) {
3093 ret = regulator_set_voltage_unlocked(rdev->supply,
3094 best_supply_uV, INT_MAX, state);
3096 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3102 if (state == PM_SUSPEND_ON)
3103 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3105 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3110 if (supply_change_uV < 0) {
3111 ret = regulator_set_voltage_unlocked(rdev->supply,
3112 best_supply_uV, INT_MAX, state);
3114 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3116 /* No need to fail here */
3123 voltage->min_uV = old_min_uV;
3124 voltage->max_uV = old_max_uV;
3130 * regulator_set_voltage - set regulator output voltage
3131 * @regulator: regulator source
3132 * @min_uV: Minimum required voltage in uV
3133 * @max_uV: Maximum acceptable voltage in uV
3135 * Sets a voltage regulator to the desired output voltage. This can be set
3136 * during any regulator state. IOW, regulator can be disabled or enabled.
3138 * If the regulator is enabled then the voltage will change to the new value
3139 * immediately otherwise if the regulator is disabled the regulator will
3140 * output at the new voltage when enabled.
3142 * NOTE: If the regulator is shared between several devices then the lowest
3143 * request voltage that meets the system constraints will be used.
3144 * Regulator system constraints must be set for this regulator before
3145 * calling this function otherwise this call will fail.
3147 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3151 regulator_lock_supply(regulator->rdev);
3153 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3156 regulator_unlock_supply(regulator->rdev);
3160 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3162 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3163 suspend_state_t state, bool en)
3165 struct regulator_state *rstate;
3167 rstate = regulator_get_suspend_state(rdev, state);
3171 if (!rstate->changeable)
3174 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3179 int regulator_suspend_enable(struct regulator_dev *rdev,
3180 suspend_state_t state)
3182 return regulator_suspend_toggle(rdev, state, true);
3184 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3186 int regulator_suspend_disable(struct regulator_dev *rdev,
3187 suspend_state_t state)
3189 struct regulator *regulator;
3190 struct regulator_voltage *voltage;
3193 * if any consumer wants this regulator device keeping on in
3194 * suspend states, don't set it as disabled.
3196 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3197 voltage = ®ulator->voltage[state];
3198 if (voltage->min_uV || voltage->max_uV)
3202 return regulator_suspend_toggle(rdev, state, false);
3204 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3206 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3207 int min_uV, int max_uV,
3208 suspend_state_t state)
3210 struct regulator_dev *rdev = regulator->rdev;
3211 struct regulator_state *rstate;
3213 rstate = regulator_get_suspend_state(rdev, state);
3217 if (rstate->min_uV == rstate->max_uV) {
3218 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3222 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3225 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3226 int max_uV, suspend_state_t state)
3230 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3231 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3234 regulator_lock_supply(regulator->rdev);
3236 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3239 regulator_unlock_supply(regulator->rdev);
3243 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3246 * regulator_set_voltage_time - get raise/fall time
3247 * @regulator: regulator source
3248 * @old_uV: starting voltage in microvolts
3249 * @new_uV: target voltage in microvolts
3251 * Provided with the starting and ending voltage, this function attempts to
3252 * calculate the time in microseconds required to rise or fall to this new
3255 int regulator_set_voltage_time(struct regulator *regulator,
3256 int old_uV, int new_uV)
3258 struct regulator_dev *rdev = regulator->rdev;
3259 const struct regulator_ops *ops = rdev->desc->ops;
3265 if (ops->set_voltage_time)
3266 return ops->set_voltage_time(rdev, old_uV, new_uV);
3267 else if (!ops->set_voltage_time_sel)
3268 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3270 /* Currently requires operations to do this */
3271 if (!ops->list_voltage || !rdev->desc->n_voltages)
3274 for (i = 0; i < rdev->desc->n_voltages; i++) {
3275 /* We only look for exact voltage matches here */
3276 voltage = regulator_list_voltage(regulator, i);
3281 if (voltage == old_uV)
3283 if (voltage == new_uV)
3287 if (old_sel < 0 || new_sel < 0)
3290 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3292 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3295 * regulator_set_voltage_time_sel - get raise/fall time
3296 * @rdev: regulator source device
3297 * @old_selector: selector for starting voltage
3298 * @new_selector: selector for target voltage
3300 * Provided with the starting and target voltage selectors, this function
3301 * returns time in microseconds required to rise or fall to this new voltage
3303 * Drivers providing ramp_delay in regulation_constraints can use this as their
3304 * set_voltage_time_sel() operation.
3306 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3307 unsigned int old_selector,
3308 unsigned int new_selector)
3310 int old_volt, new_volt;
3313 if (!rdev->desc->ops->list_voltage)
3316 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3317 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3319 if (rdev->desc->ops->set_voltage_time)
3320 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3323 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3325 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3328 * regulator_sync_voltage - re-apply last regulator output voltage
3329 * @regulator: regulator source
3331 * Re-apply the last configured voltage. This is intended to be used
3332 * where some external control source the consumer is cooperating with
3333 * has caused the configured voltage to change.
3335 int regulator_sync_voltage(struct regulator *regulator)
3337 struct regulator_dev *rdev = regulator->rdev;
3338 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
3339 int ret, min_uV, max_uV;
3341 regulator_lock(rdev);
3343 if (!rdev->desc->ops->set_voltage &&
3344 !rdev->desc->ops->set_voltage_sel) {
3349 /* This is only going to work if we've had a voltage configured. */
3350 if (!voltage->min_uV && !voltage->max_uV) {
3355 min_uV = voltage->min_uV;
3356 max_uV = voltage->max_uV;
3358 /* This should be a paranoia check... */
3359 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3363 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3367 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3370 regulator_unlock(rdev);
3373 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3375 static int _regulator_get_voltage(struct regulator_dev *rdev)
3380 if (rdev->desc->ops->get_bypass) {
3381 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3385 /* if bypassed the regulator must have a supply */
3386 if (!rdev->supply) {
3388 "bypassed regulator has no supply!\n");
3389 return -EPROBE_DEFER;
3392 return _regulator_get_voltage(rdev->supply->rdev);
3396 if (rdev->desc->ops->get_voltage_sel) {
3397 sel = rdev->desc->ops->get_voltage_sel(rdev);
3400 ret = rdev->desc->ops->list_voltage(rdev, sel);
3401 } else if (rdev->desc->ops->get_voltage) {
3402 ret = rdev->desc->ops->get_voltage(rdev);
3403 } else if (rdev->desc->ops->list_voltage) {
3404 ret = rdev->desc->ops->list_voltage(rdev, 0);
3405 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3406 ret = rdev->desc->fixed_uV;
3407 } else if (rdev->supply) {
3408 ret = _regulator_get_voltage(rdev->supply->rdev);
3415 return ret - rdev->constraints->uV_offset;
3419 * regulator_get_voltage - get regulator output voltage
3420 * @regulator: regulator source
3422 * This returns the current regulator voltage in uV.
3424 * NOTE: If the regulator is disabled it will return the voltage value. This
3425 * function should not be used to determine regulator state.
3427 int regulator_get_voltage(struct regulator *regulator)
3431 regulator_lock_supply(regulator->rdev);
3433 ret = _regulator_get_voltage(regulator->rdev);
3435 regulator_unlock_supply(regulator->rdev);
3439 EXPORT_SYMBOL_GPL(regulator_get_voltage);
3442 * regulator_set_current_limit - set regulator output current limit
3443 * @regulator: regulator source
3444 * @min_uA: Minimum supported current in uA
3445 * @max_uA: Maximum supported current in uA
3447 * Sets current sink to the desired output current. This can be set during
3448 * any regulator state. IOW, regulator can be disabled or enabled.
3450 * If the regulator is enabled then the current will change to the new value
3451 * immediately otherwise if the regulator is disabled the regulator will
3452 * output at the new current when enabled.
3454 * NOTE: Regulator system constraints must be set for this regulator before
3455 * calling this function otherwise this call will fail.
3457 int regulator_set_current_limit(struct regulator *regulator,
3458 int min_uA, int max_uA)
3460 struct regulator_dev *rdev = regulator->rdev;
3463 regulator_lock(rdev);
3466 if (!rdev->desc->ops->set_current_limit) {
3471 /* constraints check */
3472 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3476 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3478 regulator_unlock(rdev);
3481 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3483 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
3486 if (!rdev->desc->ops->get_current_limit)
3489 return rdev->desc->ops->get_current_limit(rdev);
3492 static int _regulator_get_current_limit(struct regulator_dev *rdev)
3496 regulator_lock(rdev);
3497 ret = _regulator_get_current_limit_unlocked(rdev);
3498 regulator_unlock(rdev);
3504 * regulator_get_current_limit - get regulator output current
3505 * @regulator: regulator source
3507 * This returns the current supplied by the specified current sink in uA.
3509 * NOTE: If the regulator is disabled it will return the current value. This
3510 * function should not be used to determine regulator state.
3512 int regulator_get_current_limit(struct regulator *regulator)
3514 return _regulator_get_current_limit(regulator->rdev);
3516 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3519 * regulator_set_mode - set regulator operating mode
3520 * @regulator: regulator source
3521 * @mode: operating mode - one of the REGULATOR_MODE constants
3523 * Set regulator operating mode to increase regulator efficiency or improve
3524 * regulation performance.
3526 * NOTE: Regulator system constraints must be set for this regulator before
3527 * calling this function otherwise this call will fail.
3529 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3531 struct regulator_dev *rdev = regulator->rdev;
3533 int regulator_curr_mode;
3535 regulator_lock(rdev);
3538 if (!rdev->desc->ops->set_mode) {
3543 /* return if the same mode is requested */
3544 if (rdev->desc->ops->get_mode) {
3545 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3546 if (regulator_curr_mode == mode) {
3552 /* constraints check */
3553 ret = regulator_mode_constrain(rdev, &mode);
3557 ret = rdev->desc->ops->set_mode(rdev, mode);
3559 regulator_unlock(rdev);
3562 EXPORT_SYMBOL_GPL(regulator_set_mode);
3564 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
3567 if (!rdev->desc->ops->get_mode)
3570 return rdev->desc->ops->get_mode(rdev);
3573 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3577 regulator_lock(rdev);
3578 ret = _regulator_get_mode_unlocked(rdev);
3579 regulator_unlock(rdev);
3585 * regulator_get_mode - get regulator operating mode
3586 * @regulator: regulator source
3588 * Get the current regulator operating mode.
3590 unsigned int regulator_get_mode(struct regulator *regulator)
3592 return _regulator_get_mode(regulator->rdev);
3594 EXPORT_SYMBOL_GPL(regulator_get_mode);
3596 static int _regulator_get_error_flags(struct regulator_dev *rdev,
3597 unsigned int *flags)
3601 regulator_lock(rdev);
3604 if (!rdev->desc->ops->get_error_flags) {
3609 ret = rdev->desc->ops->get_error_flags(rdev, flags);
3611 regulator_unlock(rdev);
3616 * regulator_get_error_flags - get regulator error information
3617 * @regulator: regulator source
3618 * @flags: pointer to store error flags
3620 * Get the current regulator error information.
3622 int regulator_get_error_flags(struct regulator *regulator,
3623 unsigned int *flags)
3625 return _regulator_get_error_flags(regulator->rdev, flags);
3627 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
3630 * regulator_set_load - set regulator load
3631 * @regulator: regulator source
3632 * @uA_load: load current
3634 * Notifies the regulator core of a new device load. This is then used by
3635 * DRMS (if enabled by constraints) to set the most efficient regulator
3636 * operating mode for the new regulator loading.
3638 * Consumer devices notify their supply regulator of the maximum power
3639 * they will require (can be taken from device datasheet in the power
3640 * consumption tables) when they change operational status and hence power
3641 * state. Examples of operational state changes that can affect power
3642 * consumption are :-
3644 * o Device is opened / closed.
3645 * o Device I/O is about to begin or has just finished.
3646 * o Device is idling in between work.
3648 * This information is also exported via sysfs to userspace.
3650 * DRMS will sum the total requested load on the regulator and change
3651 * to the most efficient operating mode if platform constraints allow.
3653 * On error a negative errno is returned.
3655 int regulator_set_load(struct regulator *regulator, int uA_load)
3657 struct regulator_dev *rdev = regulator->rdev;
3660 regulator_lock(rdev);
3661 regulator->uA_load = uA_load;
3662 ret = drms_uA_update(rdev);
3663 regulator_unlock(rdev);
3667 EXPORT_SYMBOL_GPL(regulator_set_load);
3670 * regulator_allow_bypass - allow the regulator to go into bypass mode
3672 * @regulator: Regulator to configure
3673 * @enable: enable or disable bypass mode
3675 * Allow the regulator to go into bypass mode if all other consumers
3676 * for the regulator also enable bypass mode and the machine
3677 * constraints allow this. Bypass mode means that the regulator is
3678 * simply passing the input directly to the output with no regulation.
3680 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3682 struct regulator_dev *rdev = regulator->rdev;
3685 if (!rdev->desc->ops->set_bypass)
3688 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
3691 regulator_lock(rdev);
3693 if (enable && !regulator->bypass) {
3694 rdev->bypass_count++;
3696 if (rdev->bypass_count == rdev->open_count) {
3697 ret = rdev->desc->ops->set_bypass(rdev, enable);
3699 rdev->bypass_count--;
3702 } else if (!enable && regulator->bypass) {
3703 rdev->bypass_count--;
3705 if (rdev->bypass_count != rdev->open_count) {
3706 ret = rdev->desc->ops->set_bypass(rdev, enable);
3708 rdev->bypass_count++;
3713 regulator->bypass = enable;
3715 regulator_unlock(rdev);
3719 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3722 * regulator_register_notifier - register regulator event notifier
3723 * @regulator: regulator source
3724 * @nb: notifier block
3726 * Register notifier block to receive regulator events.
3728 int regulator_register_notifier(struct regulator *regulator,
3729 struct notifier_block *nb)
3731 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3734 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3737 * regulator_unregister_notifier - unregister regulator event notifier
3738 * @regulator: regulator source
3739 * @nb: notifier block
3741 * Unregister regulator event notifier block.
3743 int regulator_unregister_notifier(struct regulator *regulator,
3744 struct notifier_block *nb)
3746 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3749 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3751 /* notify regulator consumers and downstream regulator consumers.
3752 * Note mutex must be held by caller.
3754 static int _notifier_call_chain(struct regulator_dev *rdev,
3755 unsigned long event, void *data)
3757 /* call rdev chain first */
3758 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3762 * regulator_bulk_get - get multiple regulator consumers
3764 * @dev: Device to supply
3765 * @num_consumers: Number of consumers to register
3766 * @consumers: Configuration of consumers; clients are stored here.
3768 * @return 0 on success, an errno on failure.
3770 * This helper function allows drivers to get several regulator
3771 * consumers in one operation. If any of the regulators cannot be
3772 * acquired then any regulators that were allocated will be freed
3773 * before returning to the caller.
3775 int regulator_bulk_get(struct device *dev, int num_consumers,
3776 struct regulator_bulk_data *consumers)
3781 for (i = 0; i < num_consumers; i++)
3782 consumers[i].consumer = NULL;
3784 for (i = 0; i < num_consumers; i++) {
3785 consumers[i].consumer = regulator_get(dev,
3786 consumers[i].supply);
3787 if (IS_ERR(consumers[i].consumer)) {
3788 ret = PTR_ERR(consumers[i].consumer);
3789 dev_err(dev, "Failed to get supply '%s': %d\n",
3790 consumers[i].supply, ret);
3791 consumers[i].consumer = NULL;
3800 regulator_put(consumers[i].consumer);
3804 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3806 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3808 struct regulator_bulk_data *bulk = data;
3810 bulk->ret = regulator_enable(bulk->consumer);
3814 * regulator_bulk_enable - enable multiple regulator consumers
3816 * @num_consumers: Number of consumers
3817 * @consumers: Consumer data; clients are stored here.
3818 * @return 0 on success, an errno on failure
3820 * This convenience API allows consumers to enable multiple regulator
3821 * clients in a single API call. If any consumers cannot be enabled
3822 * then any others that were enabled will be disabled again prior to
3825 int regulator_bulk_enable(int num_consumers,
3826 struct regulator_bulk_data *consumers)
3828 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3832 for (i = 0; i < num_consumers; i++) {
3833 if (consumers[i].consumer->always_on)
3834 consumers[i].ret = 0;
3836 async_schedule_domain(regulator_bulk_enable_async,
3837 &consumers[i], &async_domain);
3840 async_synchronize_full_domain(&async_domain);
3842 /* If any consumer failed we need to unwind any that succeeded */
3843 for (i = 0; i < num_consumers; i++) {
3844 if (consumers[i].ret != 0) {
3845 ret = consumers[i].ret;
3853 for (i = 0; i < num_consumers; i++) {
3854 if (consumers[i].ret < 0)
3855 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3858 regulator_disable(consumers[i].consumer);
3863 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3866 * regulator_bulk_disable - disable multiple regulator consumers
3868 * @num_consumers: Number of consumers
3869 * @consumers: Consumer data; clients are stored here.
3870 * @return 0 on success, an errno on failure
3872 * This convenience API allows consumers to disable multiple regulator
3873 * clients in a single API call. If any consumers cannot be disabled
3874 * then any others that were disabled will be enabled again prior to
3877 int regulator_bulk_disable(int num_consumers,
3878 struct regulator_bulk_data *consumers)
3883 for (i = num_consumers - 1; i >= 0; --i) {
3884 ret = regulator_disable(consumers[i].consumer);
3892 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3893 for (++i; i < num_consumers; ++i) {
3894 r = regulator_enable(consumers[i].consumer);
3896 pr_err("Failed to re-enable %s: %d\n",
3897 consumers[i].supply, r);
3902 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3905 * regulator_bulk_force_disable - force disable multiple regulator consumers
3907 * @num_consumers: Number of consumers
3908 * @consumers: Consumer data; clients are stored here.
3909 * @return 0 on success, an errno on failure
3911 * This convenience API allows consumers to forcibly disable multiple regulator
3912 * clients in a single API call.
3913 * NOTE: This should be used for situations when device damage will
3914 * likely occur if the regulators are not disabled (e.g. over temp).
3915 * Although regulator_force_disable function call for some consumers can
3916 * return error numbers, the function is called for all consumers.
3918 int regulator_bulk_force_disable(int num_consumers,
3919 struct regulator_bulk_data *consumers)
3924 for (i = 0; i < num_consumers; i++) {
3926 regulator_force_disable(consumers[i].consumer);
3928 /* Store first error for reporting */
3929 if (consumers[i].ret && !ret)
3930 ret = consumers[i].ret;
3935 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3938 * regulator_bulk_free - free multiple regulator consumers
3940 * @num_consumers: Number of consumers
3941 * @consumers: Consumer data; clients are stored here.
3943 * This convenience API allows consumers to free multiple regulator
3944 * clients in a single API call.
3946 void regulator_bulk_free(int num_consumers,
3947 struct regulator_bulk_data *consumers)
3951 for (i = 0; i < num_consumers; i++) {
3952 regulator_put(consumers[i].consumer);
3953 consumers[i].consumer = NULL;
3956 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3959 * regulator_notifier_call_chain - call regulator event notifier
3960 * @rdev: regulator source
3961 * @event: notifier block
3962 * @data: callback-specific data.
3964 * Called by regulator drivers to notify clients a regulator event has
3965 * occurred. We also notify regulator clients downstream.
3966 * Note lock must be held by caller.
3968 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3969 unsigned long event, void *data)
3971 lockdep_assert_held_once(&rdev->mutex);
3973 _notifier_call_chain(rdev, event, data);
3977 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3980 * regulator_mode_to_status - convert a regulator mode into a status
3982 * @mode: Mode to convert
3984 * Convert a regulator mode into a status.
3986 int regulator_mode_to_status(unsigned int mode)
3989 case REGULATOR_MODE_FAST:
3990 return REGULATOR_STATUS_FAST;
3991 case REGULATOR_MODE_NORMAL:
3992 return REGULATOR_STATUS_NORMAL;
3993 case REGULATOR_MODE_IDLE:
3994 return REGULATOR_STATUS_IDLE;
3995 case REGULATOR_MODE_STANDBY:
3996 return REGULATOR_STATUS_STANDBY;
3998 return REGULATOR_STATUS_UNDEFINED;
4001 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4003 static struct attribute *regulator_dev_attrs[] = {
4004 &dev_attr_name.attr,
4005 &dev_attr_num_users.attr,
4006 &dev_attr_type.attr,
4007 &dev_attr_microvolts.attr,
4008 &dev_attr_microamps.attr,
4009 &dev_attr_opmode.attr,
4010 &dev_attr_state.attr,
4011 &dev_attr_status.attr,
4012 &dev_attr_bypass.attr,
4013 &dev_attr_requested_microamps.attr,
4014 &dev_attr_min_microvolts.attr,
4015 &dev_attr_max_microvolts.attr,
4016 &dev_attr_min_microamps.attr,
4017 &dev_attr_max_microamps.attr,
4018 &dev_attr_suspend_standby_state.attr,
4019 &dev_attr_suspend_mem_state.attr,
4020 &dev_attr_suspend_disk_state.attr,
4021 &dev_attr_suspend_standby_microvolts.attr,
4022 &dev_attr_suspend_mem_microvolts.attr,
4023 &dev_attr_suspend_disk_microvolts.attr,
4024 &dev_attr_suspend_standby_mode.attr,
4025 &dev_attr_suspend_mem_mode.attr,
4026 &dev_attr_suspend_disk_mode.attr,
4031 * To avoid cluttering sysfs (and memory) with useless state, only
4032 * create attributes that can be meaningfully displayed.
4034 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4035 struct attribute *attr, int idx)
4037 struct device *dev = kobj_to_dev(kobj);
4038 struct regulator_dev *rdev = dev_to_rdev(dev);
4039 const struct regulator_ops *ops = rdev->desc->ops;
4040 umode_t mode = attr->mode;
4042 /* these three are always present */
4043 if (attr == &dev_attr_name.attr ||
4044 attr == &dev_attr_num_users.attr ||
4045 attr == &dev_attr_type.attr)
4048 /* some attributes need specific methods to be displayed */
4049 if (attr == &dev_attr_microvolts.attr) {
4050 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4051 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4052 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4053 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4058 if (attr == &dev_attr_microamps.attr)
4059 return ops->get_current_limit ? mode : 0;
4061 if (attr == &dev_attr_opmode.attr)
4062 return ops->get_mode ? mode : 0;
4064 if (attr == &dev_attr_state.attr)
4065 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4067 if (attr == &dev_attr_status.attr)
4068 return ops->get_status ? mode : 0;
4070 if (attr == &dev_attr_bypass.attr)
4071 return ops->get_bypass ? mode : 0;
4073 /* some attributes are type-specific */
4074 if (attr == &dev_attr_requested_microamps.attr)
4075 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
4077 /* constraints need specific supporting methods */
4078 if (attr == &dev_attr_min_microvolts.attr ||
4079 attr == &dev_attr_max_microvolts.attr)
4080 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4082 if (attr == &dev_attr_min_microamps.attr ||
4083 attr == &dev_attr_max_microamps.attr)
4084 return ops->set_current_limit ? mode : 0;
4086 if (attr == &dev_attr_suspend_standby_state.attr ||
4087 attr == &dev_attr_suspend_mem_state.attr ||
4088 attr == &dev_attr_suspend_disk_state.attr)
4091 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4092 attr == &dev_attr_suspend_mem_microvolts.attr ||
4093 attr == &dev_attr_suspend_disk_microvolts.attr)
4094 return ops->set_suspend_voltage ? mode : 0;
4096 if (attr == &dev_attr_suspend_standby_mode.attr ||
4097 attr == &dev_attr_suspend_mem_mode.attr ||
4098 attr == &dev_attr_suspend_disk_mode.attr)
4099 return ops->set_suspend_mode ? mode : 0;
4104 static const struct attribute_group regulator_dev_group = {
4105 .attrs = regulator_dev_attrs,
4106 .is_visible = regulator_attr_is_visible,
4109 static const struct attribute_group *regulator_dev_groups[] = {
4110 ®ulator_dev_group,
4114 static void regulator_dev_release(struct device *dev)
4116 struct regulator_dev *rdev = dev_get_drvdata(dev);
4118 kfree(rdev->constraints);
4119 of_node_put(rdev->dev.of_node);
4123 static void rdev_init_debugfs(struct regulator_dev *rdev)
4125 struct device *parent = rdev->dev.parent;
4126 const char *rname = rdev_get_name(rdev);
4127 char name[NAME_MAX];
4129 /* Avoid duplicate debugfs directory names */
4130 if (parent && rname == rdev->desc->name) {
4131 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4136 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4137 if (!rdev->debugfs) {
4138 rdev_warn(rdev, "Failed to create debugfs directory\n");
4142 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4144 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4146 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4147 &rdev->bypass_count);
4150 static int regulator_register_resolve_supply(struct device *dev, void *data)
4152 struct regulator_dev *rdev = dev_to_rdev(dev);
4154 if (regulator_resolve_supply(rdev))
4155 rdev_dbg(rdev, "unable to resolve supply\n");
4160 static int regulator_fill_coupling_array(struct regulator_dev *rdev)
4162 struct coupling_desc *c_desc = &rdev->coupling_desc;
4163 int n_coupled = c_desc->n_coupled;
4164 struct regulator_dev *c_rdev;
4167 for (i = 1; i < n_coupled; i++) {
4168 /* already resolved */
4169 if (c_desc->coupled_rdevs[i])
4172 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4175 c_desc->coupled_rdevs[i] = c_rdev;
4176 c_desc->n_resolved++;
4180 if (rdev->coupling_desc.n_resolved < n_coupled)
4186 static int regulator_register_fill_coupling_array(struct device *dev,
4189 struct regulator_dev *rdev = dev_to_rdev(dev);
4191 if (!IS_ENABLED(CONFIG_OF))
4194 if (regulator_fill_coupling_array(rdev))
4195 rdev_dbg(rdev, "unable to resolve coupling\n");
4200 static int regulator_resolve_coupling(struct regulator_dev *rdev)
4204 if (!IS_ENABLED(CONFIG_OF))
4207 n_phandles = of_get_n_coupled(rdev);
4209 if (n_phandles + 1 > MAX_COUPLED) {
4210 rdev_err(rdev, "too many regulators coupled\n");
4215 * Every regulator should always have coupling descriptor filled with
4216 * at least pointer to itself.
4218 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4219 rdev->coupling_desc.n_coupled = n_phandles + 1;
4220 rdev->coupling_desc.n_resolved++;
4222 /* regulator isn't coupled */
4223 if (n_phandles == 0)
4226 /* regulator, which can't change its voltage, can't be coupled */
4227 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
4228 rdev_err(rdev, "voltage operation not allowed\n");
4232 if (rdev->constraints->max_spread <= 0) {
4233 rdev_err(rdev, "wrong max_spread value\n");
4237 if (!of_check_coupling_data(rdev))
4241 * After everything has been checked, try to fill rdevs array
4242 * with pointers to regulators parsed from device tree. If some
4243 * regulators are not registered yet, retry in late init call
4245 regulator_fill_coupling_array(rdev);
4251 * regulator_register - register regulator
4252 * @regulator_desc: regulator to register
4253 * @cfg: runtime configuration for regulator
4255 * Called by regulator drivers to register a regulator.
4256 * Returns a valid pointer to struct regulator_dev on success
4257 * or an ERR_PTR() on error.
4259 struct regulator_dev *
4260 regulator_register(const struct regulator_desc *regulator_desc,
4261 const struct regulator_config *cfg)
4263 const struct regulation_constraints *constraints = NULL;
4264 const struct regulator_init_data *init_data;
4265 struct regulator_config *config = NULL;
4266 static atomic_t regulator_no = ATOMIC_INIT(-1);
4267 struct regulator_dev *rdev;
4271 if (regulator_desc == NULL || cfg == NULL)
4272 return ERR_PTR(-EINVAL);
4277 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4278 return ERR_PTR(-EINVAL);
4280 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4281 regulator_desc->type != REGULATOR_CURRENT)
4282 return ERR_PTR(-EINVAL);
4284 /* Only one of each should be implemented */
4285 WARN_ON(regulator_desc->ops->get_voltage &&
4286 regulator_desc->ops->get_voltage_sel);
4287 WARN_ON(regulator_desc->ops->set_voltage &&
4288 regulator_desc->ops->set_voltage_sel);
4290 /* If we're using selectors we must implement list_voltage. */
4291 if (regulator_desc->ops->get_voltage_sel &&
4292 !regulator_desc->ops->list_voltage) {
4293 return ERR_PTR(-EINVAL);
4295 if (regulator_desc->ops->set_voltage_sel &&
4296 !regulator_desc->ops->list_voltage) {
4297 return ERR_PTR(-EINVAL);
4300 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4302 return ERR_PTR(-ENOMEM);
4305 * Duplicate the config so the driver could override it after
4306 * parsing init data.
4308 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4309 if (config == NULL) {
4311 return ERR_PTR(-ENOMEM);
4314 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4315 &rdev->dev.of_node);
4317 init_data = config->init_data;
4318 rdev->dev.of_node = of_node_get(config->of_node);
4321 mutex_init(&rdev->mutex);
4322 rdev->reg_data = config->driver_data;
4323 rdev->owner = regulator_desc->owner;
4324 rdev->desc = regulator_desc;
4326 rdev->regmap = config->regmap;
4327 else if (dev_get_regmap(dev, NULL))
4328 rdev->regmap = dev_get_regmap(dev, NULL);
4329 else if (dev->parent)
4330 rdev->regmap = dev_get_regmap(dev->parent, NULL);
4331 INIT_LIST_HEAD(&rdev->consumer_list);
4332 INIT_LIST_HEAD(&rdev->list);
4333 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4334 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4336 /* preform any regulator specific init */
4337 if (init_data && init_data->regulator_init) {
4338 ret = init_data->regulator_init(rdev->reg_data);
4343 if (config->ena_gpiod ||
4344 ((config->ena_gpio || config->ena_gpio_initialized) &&
4345 gpio_is_valid(config->ena_gpio))) {
4346 mutex_lock(®ulator_list_mutex);
4347 ret = regulator_ena_gpio_request(rdev, config);
4348 mutex_unlock(®ulator_list_mutex);
4350 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4351 config->ena_gpio, ret);
4356 /* register with sysfs */
4357 rdev->dev.class = ®ulator_class;
4358 rdev->dev.parent = dev;
4359 dev_set_name(&rdev->dev, "regulator.%lu",
4360 (unsigned long) atomic_inc_return(®ulator_no));
4362 /* set regulator constraints */
4364 constraints = &init_data->constraints;
4366 if (init_data && init_data->supply_regulator)
4367 rdev->supply_name = init_data->supply_regulator;
4368 else if (regulator_desc->supply_name)
4369 rdev->supply_name = regulator_desc->supply_name;
4372 * Attempt to resolve the regulator supply, if specified,
4373 * but don't return an error if we fail because we will try
4374 * to resolve it again later as more regulators are added.
4376 if (regulator_resolve_supply(rdev))
4377 rdev_dbg(rdev, "unable to resolve supply\n");
4379 ret = set_machine_constraints(rdev, constraints);
4383 mutex_lock(®ulator_list_mutex);
4384 ret = regulator_resolve_coupling(rdev);
4385 mutex_unlock(®ulator_list_mutex);
4390 /* add consumers devices */
4392 mutex_lock(®ulator_list_mutex);
4393 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4394 ret = set_consumer_device_supply(rdev,
4395 init_data->consumer_supplies[i].dev_name,
4396 init_data->consumer_supplies[i].supply);
4398 mutex_unlock(®ulator_list_mutex);
4399 dev_err(dev, "Failed to set supply %s\n",
4400 init_data->consumer_supplies[i].supply);
4401 goto unset_supplies;
4404 mutex_unlock(®ulator_list_mutex);
4407 if (!rdev->desc->ops->get_voltage &&
4408 !rdev->desc->ops->list_voltage &&
4409 !rdev->desc->fixed_uV)
4410 rdev->is_switch = true;
4412 dev_set_drvdata(&rdev->dev, rdev);
4413 ret = device_register(&rdev->dev);
4415 put_device(&rdev->dev);
4416 goto unset_supplies;
4419 rdev_init_debugfs(rdev);
4421 /* try to resolve regulators supply since a new one was registered */
4422 class_for_each_device(®ulator_class, NULL, NULL,
4423 regulator_register_resolve_supply);
4428 mutex_lock(®ulator_list_mutex);
4429 unset_regulator_supplies(rdev);
4430 mutex_unlock(®ulator_list_mutex);
4432 kfree(rdev->constraints);
4433 mutex_lock(®ulator_list_mutex);
4434 regulator_ena_gpio_free(rdev);
4435 mutex_unlock(®ulator_list_mutex);
4439 return ERR_PTR(ret);
4441 EXPORT_SYMBOL_GPL(regulator_register);
4444 * regulator_unregister - unregister regulator
4445 * @rdev: regulator to unregister
4447 * Called by regulator drivers to unregister a regulator.
4449 void regulator_unregister(struct regulator_dev *rdev)
4455 while (rdev->use_count--)
4456 regulator_disable(rdev->supply);
4457 regulator_put(rdev->supply);
4459 mutex_lock(®ulator_list_mutex);
4460 debugfs_remove_recursive(rdev->debugfs);
4461 flush_work(&rdev->disable_work.work);
4462 WARN_ON(rdev->open_count);
4463 unset_regulator_supplies(rdev);
4464 list_del(&rdev->list);
4465 regulator_ena_gpio_free(rdev);
4466 mutex_unlock(®ulator_list_mutex);
4467 device_unregister(&rdev->dev);
4469 EXPORT_SYMBOL_GPL(regulator_unregister);
4471 #ifdef CONFIG_SUSPEND
4473 * regulator_suspend - prepare regulators for system wide suspend
4474 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
4476 * Configure each regulator with it's suspend operating parameters for state.
4478 static int regulator_suspend(struct device *dev)
4480 struct regulator_dev *rdev = dev_to_rdev(dev);
4481 suspend_state_t state = pm_suspend_target_state;
4484 regulator_lock(rdev);
4485 ret = suspend_set_state(rdev, state);
4486 regulator_unlock(rdev);
4491 static int regulator_resume(struct device *dev)
4493 suspend_state_t state = pm_suspend_target_state;
4494 struct regulator_dev *rdev = dev_to_rdev(dev);
4495 struct regulator_state *rstate;
4498 rstate = regulator_get_suspend_state(rdev, state);
4502 regulator_lock(rdev);
4504 if (rdev->desc->ops->resume &&
4505 (rstate->enabled == ENABLE_IN_SUSPEND ||
4506 rstate->enabled == DISABLE_IN_SUSPEND))
4507 ret = rdev->desc->ops->resume(rdev);
4509 regulator_unlock(rdev);
4513 #else /* !CONFIG_SUSPEND */
4515 #define regulator_suspend NULL
4516 #define regulator_resume NULL
4518 #endif /* !CONFIG_SUSPEND */
4521 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
4522 .suspend = regulator_suspend,
4523 .resume = regulator_resume,
4527 struct class regulator_class = {
4528 .name = "regulator",
4529 .dev_release = regulator_dev_release,
4530 .dev_groups = regulator_dev_groups,
4532 .pm = ®ulator_pm_ops,
4536 * regulator_has_full_constraints - the system has fully specified constraints
4538 * Calling this function will cause the regulator API to disable all
4539 * regulators which have a zero use count and don't have an always_on
4540 * constraint in a late_initcall.
4542 * The intention is that this will become the default behaviour in a
4543 * future kernel release so users are encouraged to use this facility
4546 void regulator_has_full_constraints(void)
4548 has_full_constraints = 1;
4550 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4553 * rdev_get_drvdata - get rdev regulator driver data
4556 * Get rdev regulator driver private data. This call can be used in the
4557 * regulator driver context.
4559 void *rdev_get_drvdata(struct regulator_dev *rdev)
4561 return rdev->reg_data;
4563 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4566 * regulator_get_drvdata - get regulator driver data
4567 * @regulator: regulator
4569 * Get regulator driver private data. This call can be used in the consumer
4570 * driver context when non API regulator specific functions need to be called.
4572 void *regulator_get_drvdata(struct regulator *regulator)
4574 return regulator->rdev->reg_data;
4576 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4579 * regulator_set_drvdata - set regulator driver data
4580 * @regulator: regulator
4583 void regulator_set_drvdata(struct regulator *regulator, void *data)
4585 regulator->rdev->reg_data = data;
4587 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4590 * regulator_get_id - get regulator ID
4593 int rdev_get_id(struct regulator_dev *rdev)
4595 return rdev->desc->id;
4597 EXPORT_SYMBOL_GPL(rdev_get_id);
4599 struct device *rdev_get_dev(struct regulator_dev *rdev)
4603 EXPORT_SYMBOL_GPL(rdev_get_dev);
4605 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4607 return reg_init_data->driver_data;
4609 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4611 #ifdef CONFIG_DEBUG_FS
4612 static int supply_map_show(struct seq_file *sf, void *data)
4614 struct regulator_map *map;
4616 list_for_each_entry(map, ®ulator_map_list, list) {
4617 seq_printf(sf, "%s -> %s.%s\n",
4618 rdev_get_name(map->regulator), map->dev_name,
4625 static int supply_map_open(struct inode *inode, struct file *file)
4627 return single_open(file, supply_map_show, inode->i_private);
4631 static const struct file_operations supply_map_fops = {
4632 #ifdef CONFIG_DEBUG_FS
4633 .open = supply_map_open,
4635 .llseek = seq_lseek,
4636 .release = single_release,
4640 #ifdef CONFIG_DEBUG_FS
4641 struct summary_data {
4643 struct regulator_dev *parent;
4647 static void regulator_summary_show_subtree(struct seq_file *s,
4648 struct regulator_dev *rdev,
4651 static int regulator_summary_show_children(struct device *dev, void *data)
4653 struct regulator_dev *rdev = dev_to_rdev(dev);
4654 struct summary_data *summary_data = data;
4656 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4657 regulator_summary_show_subtree(summary_data->s, rdev,
4658 summary_data->level + 1);
4663 static void regulator_summary_show_subtree(struct seq_file *s,
4664 struct regulator_dev *rdev,
4667 struct regulation_constraints *c;
4668 struct regulator *consumer;
4669 struct summary_data summary_data;
4670 unsigned int opmode;
4675 regulator_lock_nested(rdev, level);
4677 opmode = _regulator_get_mode_unlocked(rdev);
4678 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
4680 30 - level * 3, rdev_get_name(rdev),
4681 rdev->use_count, rdev->open_count, rdev->bypass_count,
4682 regulator_opmode_to_str(opmode));
4684 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4685 seq_printf(s, "%5dmA ",
4686 _regulator_get_current_limit_unlocked(rdev) / 1000);
4688 c = rdev->constraints;
4690 switch (rdev->desc->type) {
4691 case REGULATOR_VOLTAGE:
4692 seq_printf(s, "%5dmV %5dmV ",
4693 c->min_uV / 1000, c->max_uV / 1000);
4695 case REGULATOR_CURRENT:
4696 seq_printf(s, "%5dmA %5dmA ",
4697 c->min_uA / 1000, c->max_uA / 1000);
4704 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4705 if (consumer->dev && consumer->dev->class == ®ulator_class)
4708 seq_printf(s, "%*s%-*s ",
4709 (level + 1) * 3 + 1, "",
4710 30 - (level + 1) * 3,
4711 consumer->dev ? dev_name(consumer->dev) : "deviceless");
4713 switch (rdev->desc->type) {
4714 case REGULATOR_VOLTAGE:
4715 seq_printf(s, "%37dmA %5dmV %5dmV",
4716 consumer->uA_load / 1000,
4717 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
4718 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
4720 case REGULATOR_CURRENT:
4728 summary_data.level = level;
4729 summary_data.parent = rdev;
4731 class_for_each_device(®ulator_class, NULL, &summary_data,
4732 regulator_summary_show_children);
4734 regulator_unlock(rdev);
4737 static int regulator_summary_show_roots(struct device *dev, void *data)
4739 struct regulator_dev *rdev = dev_to_rdev(dev);
4740 struct seq_file *s = data;
4743 regulator_summary_show_subtree(s, rdev, 0);
4748 static int regulator_summary_show(struct seq_file *s, void *data)
4750 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
4751 seq_puts(s, "---------------------------------------------------------------------------------------\n");
4753 class_for_each_device(®ulator_class, NULL, s,
4754 regulator_summary_show_roots);
4759 static int regulator_summary_open(struct inode *inode, struct file *file)
4761 return single_open(file, regulator_summary_show, inode->i_private);
4765 static const struct file_operations regulator_summary_fops = {
4766 #ifdef CONFIG_DEBUG_FS
4767 .open = regulator_summary_open,
4769 .llseek = seq_lseek,
4770 .release = single_release,
4774 static int __init regulator_init(void)
4778 ret = class_register(®ulator_class);
4780 debugfs_root = debugfs_create_dir("regulator", NULL);
4782 pr_warn("regulator: Failed to create debugfs directory\n");
4784 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4787 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4788 NULL, ®ulator_summary_fops);
4790 regulator_dummy_init();
4795 /* init early to allow our consumers to complete system booting */
4796 core_initcall(regulator_init);
4798 static int __init regulator_late_cleanup(struct device *dev, void *data)
4800 struct regulator_dev *rdev = dev_to_rdev(dev);
4801 const struct regulator_ops *ops = rdev->desc->ops;
4802 struct regulation_constraints *c = rdev->constraints;
4805 if (c && c->always_on)
4808 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
4811 regulator_lock(rdev);
4813 if (rdev->use_count)
4816 /* If we can't read the status assume it's on. */
4817 if (ops->is_enabled)
4818 enabled = ops->is_enabled(rdev);
4825 if (have_full_constraints()) {
4826 /* We log since this may kill the system if it goes
4828 rdev_info(rdev, "disabling\n");
4829 ret = _regulator_do_disable(rdev);
4831 rdev_err(rdev, "couldn't disable: %d\n", ret);
4833 /* The intention is that in future we will
4834 * assume that full constraints are provided
4835 * so warn even if we aren't going to do
4838 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4842 regulator_unlock(rdev);
4847 static int __init regulator_init_complete(void)
4850 * Since DT doesn't provide an idiomatic mechanism for
4851 * enabling full constraints and since it's much more natural
4852 * with DT to provide them just assume that a DT enabled
4853 * system has full constraints.
4855 if (of_have_populated_dt())
4856 has_full_constraints = true;
4859 * Regulators may had failed to resolve their input supplies
4860 * when were registered, either because the input supply was
4861 * not registered yet or because its parent device was not
4862 * bound yet. So attempt to resolve the input supplies for
4863 * pending regulators before trying to disable unused ones.
4865 class_for_each_device(®ulator_class, NULL, NULL,
4866 regulator_register_resolve_supply);
4868 /* If we have a full configuration then disable any regulators
4869 * we have permission to change the status for and which are
4870 * not in use or always_on. This is effectively the default
4871 * for DT and ACPI as they have full constraints.
4873 class_for_each_device(®ulator_class, NULL, NULL,
4874 regulator_late_cleanup);
4876 class_for_each_device(®ulator_class, NULL, NULL,
4877 regulator_register_fill_coupling_array);
4881 late_initcall_sync(regulator_init_complete);