| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | * core.c  --  Voltage/Current Regulator framework. | 
|  | 3 | * | 
|  | 4 | * Copyright 2007, 2008 Wolfson Microelectronics PLC. | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 5 | * Copyright 2008 SlimLogic Ltd. | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 6 | * | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 7 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 8 | * | 
|  | 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. | 
|  | 13 | * | 
|  | 14 | */ | 
|  | 15 |  | 
|  | 16 | #include <linux/kernel.h> | 
|  | 17 | #include <linux/init.h> | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 18 | #include <linux/debugfs.h> | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 19 | #include <linux/device.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> | 
| Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 21 | #include <linux/async.h> | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 22 | #include <linux/err.h> | 
|  | 23 | #include <linux/mutex.h> | 
|  | 24 | #include <linux/suspend.h> | 
| Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 25 | #include <linux/delay.h> | 
| Mark Brown | 65f7350 | 2012-06-27 14:14:38 +0100 | [diff] [blame] | 26 | #include <linux/gpio.h> | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 27 | #include <linux/gpio/consumer.h> | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 28 | #include <linux/of.h> | 
| Mark Brown | 65b19ce | 2012-04-15 11:16:05 +0100 | [diff] [blame] | 29 | #include <linux/regmap.h> | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 30 | #include <linux/regulator/of_regulator.h> | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 31 | #include <linux/regulator/consumer.h> | 
|  | 32 | #include <linux/regulator/driver.h> | 
|  | 33 | #include <linux/regulator/machine.h> | 
| Paul Gortmaker | 65602c3 | 2011-07-17 16:28:23 -0400 | [diff] [blame] | 34 | #include <linux/module.h> | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 35 |  | 
| Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 36 | #define CREATE_TRACE_POINTS | 
|  | 37 | #include <trace/events/regulator.h> | 
|  | 38 |  | 
| Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 39 | #include "dummy.h" | 
| Mark Brown | 0cdfcc0 | 2013-09-11 13:15:40 +0100 | [diff] [blame] | 40 | #include "internal.h" | 
| Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 41 |  | 
| Mark Brown | 7d51a0d | 2011-06-09 16:06:37 +0100 | [diff] [blame] | 42 | #define rdev_crit(rdev, fmt, ...)					\ | 
|  | 43 | pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 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__) | 
|  | 52 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 53 | static DEFINE_MUTEX(regulator_list_mutex); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 54 | static LIST_HEAD(regulator_map_list); | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 55 | static LIST_HEAD(regulator_ena_gpio_list); | 
| Charles Keepax | a06ccd9 | 2013-10-15 20:14:20 +0100 | [diff] [blame] | 56 | static LIST_HEAD(regulator_supply_alias_list); | 
| Mark Brown | 21cf891 | 2010-12-21 23:30:07 +0000 | [diff] [blame] | 57 | static bool has_full_constraints; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 58 |  | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 59 | static struct dentry *debugfs_root; | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 60 |  | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 61 | static struct class regulator_class; | 
|  | 62 |  | 
| Mark Brown | 8dc5390 | 2008-12-31 12:52:40 +0000 | [diff] [blame] | 63 | /* | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 64 | * struct regulator_map | 
|  | 65 | * | 
|  | 66 | * Used to provide symbolic supply names to devices. | 
|  | 67 | */ | 
|  | 68 | struct regulator_map { | 
|  | 69 | struct list_head list; | 
| Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 70 | const char *dev_name;   /* The dev_name() for the consumer */ | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 71 | const char *supply; | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 72 | struct regulator_dev *regulator; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 73 | }; | 
|  | 74 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 75 | /* | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 76 | * struct regulator_enable_gpio | 
|  | 77 | * | 
|  | 78 | * Management for shared enable GPIO pin | 
|  | 79 | */ | 
|  | 80 | struct regulator_enable_gpio { | 
|  | 81 | struct list_head list; | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 82 | struct gpio_desc *gpiod; | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 83 | u32 enable_count;	/* a number of enabled shared GPIO */ | 
|  | 84 | u32 request_count;	/* a number of requested shared GPIO */ | 
|  | 85 | unsigned int ena_gpio_invert:1; | 
|  | 86 | }; | 
|  | 87 |  | 
| Charles Keepax | a06ccd9 | 2013-10-15 20:14:20 +0100 | [diff] [blame] | 88 | /* | 
|  | 89 | * struct regulator_supply_alias | 
|  | 90 | * | 
|  | 91 | * Used to map lookups for a supply onto an alternative device. | 
|  | 92 | */ | 
|  | 93 | struct regulator_supply_alias { | 
|  | 94 | struct list_head list; | 
|  | 95 | struct device *src_dev; | 
|  | 96 | const char *src_supply; | 
|  | 97 | struct device *alias_dev; | 
|  | 98 | const char *alias_supply; | 
|  | 99 | }; | 
|  | 100 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 101 | static int _regulator_is_enabled(struct regulator_dev *rdev); | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 102 | static int _regulator_disable(struct regulator_dev *rdev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 103 | static int _regulator_get_voltage(struct regulator_dev *rdev); | 
|  | 104 | static int _regulator_get_current_limit(struct regulator_dev *rdev); | 
|  | 105 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev); | 
| Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 106 | static int _notifier_call_chain(struct regulator_dev *rdev, | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 107 | unsigned long event, void *data); | 
| Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 108 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, | 
|  | 109 | int min_uV, int max_uV); | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 110 | static struct regulator *create_regulator(struct regulator_dev *rdev, | 
|  | 111 | struct device *dev, | 
|  | 112 | const char *supply_name); | 
| Javier Martinez Canillas | 36a1f1b | 2015-07-15 16:10:29 +0200 | [diff] [blame] | 113 | static void _regulator_put(struct regulator *regulator); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 114 |  | 
| Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 115 | static struct regulator_dev *dev_to_rdev(struct device *dev) | 
|  | 116 | { | 
|  | 117 | return container_of(dev, struct regulator_dev, dev); | 
|  | 118 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 119 |  | 
| Mark Brown | 1083c39 | 2009-10-22 16:31:32 +0100 | [diff] [blame] | 120 | static const char *rdev_get_name(struct regulator_dev *rdev) | 
|  | 121 | { | 
|  | 122 | if (rdev->constraints && rdev->constraints->name) | 
|  | 123 | return rdev->constraints->name; | 
|  | 124 | else if (rdev->desc->name) | 
|  | 125 | return rdev->desc->name; | 
|  | 126 | else | 
|  | 127 | return ""; | 
|  | 128 | } | 
|  | 129 |  | 
| Mark Brown | 87b2841 | 2013-11-27 16:13:10 +0000 | [diff] [blame] | 130 | static bool have_full_constraints(void) | 
|  | 131 | { | 
| Mark Brown | 75bc964 | 2013-11-27 16:22:53 +0000 | [diff] [blame] | 132 | return has_full_constraints || of_have_populated_dt(); | 
| Mark Brown | 87b2841 | 2013-11-27 16:13:10 +0000 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 135 | static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops) | 
|  | 136 | { | 
|  | 137 | if (!rdev->constraints) { | 
|  | 138 | rdev_err(rdev, "no constraints\n"); | 
|  | 139 | return false; | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | if (rdev->constraints->valid_ops_mask & ops) | 
|  | 143 | return true; | 
|  | 144 |  | 
|  | 145 | return false; | 
|  | 146 | } | 
|  | 147 |  | 
| Thierry Reding | 70a7fb8 | 2015-12-02 16:54:50 +0100 | [diff] [blame] | 148 | static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev) | 
|  | 149 | { | 
|  | 150 | if (rdev && rdev->supply) | 
|  | 151 | return rdev->supply->rdev; | 
|  | 152 |  | 
|  | 153 | return NULL; | 
|  | 154 | } | 
|  | 155 |  | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 156 | /** | 
| Sascha Hauer | 9f01cd4 | 2015-09-30 16:05:42 +0200 | [diff] [blame] | 157 | * regulator_lock_supply - lock a regulator and its supplies | 
|  | 158 | * @rdev:         regulator source | 
|  | 159 | */ | 
|  | 160 | static void regulator_lock_supply(struct regulator_dev *rdev) | 
|  | 161 | { | 
| Arnd Bergmann | fa731ac | 2015-11-20 15:24:39 +0100 | [diff] [blame] | 162 | int i; | 
| Sascha Hauer | 9f01cd4 | 2015-09-30 16:05:42 +0200 | [diff] [blame] | 163 |  | 
| Thierry Reding | 70a7fb8 | 2015-12-02 16:54:50 +0100 | [diff] [blame] | 164 | for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++) | 
| Arnd Bergmann | fa731ac | 2015-11-20 15:24:39 +0100 | [diff] [blame] | 165 | mutex_lock_nested(&rdev->mutex, i); | 
| Sascha Hauer | 9f01cd4 | 2015-09-30 16:05:42 +0200 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
|  | 168 | /** | 
|  | 169 | * regulator_unlock_supply - unlock a regulator and its supplies | 
|  | 170 | * @rdev:         regulator source | 
|  | 171 | */ | 
|  | 172 | static void regulator_unlock_supply(struct regulator_dev *rdev) | 
|  | 173 | { | 
|  | 174 | struct regulator *supply; | 
|  | 175 |  | 
|  | 176 | while (1) { | 
|  | 177 | mutex_unlock(&rdev->mutex); | 
|  | 178 | supply = rdev->supply; | 
|  | 179 |  | 
|  | 180 | if (!rdev->supply) | 
|  | 181 | return; | 
|  | 182 |  | 
|  | 183 | rdev = supply->rdev; | 
|  | 184 | } | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | /** | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 188 | * of_get_regulator - get a regulator device node based on supply name | 
|  | 189 | * @dev: Device pointer for the consumer (of regulator) device | 
|  | 190 | * @supply: regulator supply name | 
|  | 191 | * | 
|  | 192 | * Extract the regulator device node corresponding to the supply name. | 
| Maxime Ripard | 167d41d | 2013-03-23 11:00:41 +0100 | [diff] [blame] | 193 | * returns the device node corresponding to the regulator if found, else | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 194 | * returns NULL. | 
|  | 195 | */ | 
|  | 196 | static struct device_node *of_get_regulator(struct device *dev, const char *supply) | 
|  | 197 | { | 
|  | 198 | struct device_node *regnode = NULL; | 
|  | 199 | char prop_name[32]; /* 32 is max size of property name */ | 
|  | 200 |  | 
|  | 201 | dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); | 
|  | 202 |  | 
|  | 203 | snprintf(prop_name, 32, "%s-supply", supply); | 
|  | 204 | regnode = of_parse_phandle(dev->of_node, prop_name, 0); | 
|  | 205 |  | 
|  | 206 | if (!regnode) { | 
| Rob Herring | 7799167 | 2017-07-18 16:43:26 -0500 | [diff] [blame] | 207 | dev_dbg(dev, "Looking up %s property in node %pOF failed\n", | 
|  | 208 | prop_name, dev->of_node); | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 209 | return NULL; | 
|  | 210 | } | 
|  | 211 | return regnode; | 
|  | 212 | } | 
|  | 213 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 214 | /* Platform voltage constraint check */ | 
|  | 215 | static int regulator_check_voltage(struct regulator_dev *rdev, | 
|  | 216 | int *min_uV, int *max_uV) | 
|  | 217 | { | 
|  | 218 | BUG_ON(*min_uV > *max_uV); | 
|  | 219 |  | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 220 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { | 
| Stephen Boyd | 7ebcf26 | 2015-09-16 12:10:26 -0700 | [diff] [blame] | 221 | rdev_err(rdev, "voltage operation not allowed\n"); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 222 | return -EPERM; | 
|  | 223 | } | 
|  | 224 |  | 
|  | 225 | if (*max_uV > rdev->constraints->max_uV) | 
|  | 226 | *max_uV = rdev->constraints->max_uV; | 
|  | 227 | if (*min_uV < rdev->constraints->min_uV) | 
|  | 228 | *min_uV = rdev->constraints->min_uV; | 
|  | 229 |  | 
| Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 230 | if (*min_uV > *max_uV) { | 
|  | 231 | rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", | 
| Mark Brown | 54abd33 | 2011-07-21 15:07:37 +0100 | [diff] [blame] | 232 | *min_uV, *max_uV); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 233 | return -EINVAL; | 
| Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 234 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 235 |  | 
|  | 236 | return 0; | 
|  | 237 | } | 
|  | 238 |  | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 239 | /* return 0 if the state is valid */ | 
|  | 240 | static int regulator_check_states(suspend_state_t state) | 
|  | 241 | { | 
|  | 242 | return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE); | 
|  | 243 | } | 
|  | 244 |  | 
| Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 245 | /* Make sure we select a voltage that suits the needs of all | 
|  | 246 | * regulator consumers | 
|  | 247 | */ | 
|  | 248 | static int regulator_check_consumers(struct regulator_dev *rdev, | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 249 | int *min_uV, int *max_uV, | 
|  | 250 | suspend_state_t state) | 
| Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 251 | { | 
|  | 252 | struct regulator *regulator; | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 253 | struct regulator_voltage *voltage; | 
| Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 254 |  | 
|  | 255 | list_for_each_entry(regulator, &rdev->consumer_list, list) { | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 256 | voltage = ®ulator->voltage[state]; | 
| Mark Brown | 4aa922c | 2011-05-14 13:42:34 -0700 | [diff] [blame] | 257 | /* | 
|  | 258 | * Assume consumers that didn't say anything are OK | 
|  | 259 | * with anything in the constraint range. | 
|  | 260 | */ | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 261 | if (!voltage->min_uV && !voltage->max_uV) | 
| Mark Brown | 4aa922c | 2011-05-14 13:42:34 -0700 | [diff] [blame] | 262 | continue; | 
|  | 263 |  | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 264 | if (*max_uV > voltage->max_uV) | 
|  | 265 | *max_uV = voltage->max_uV; | 
|  | 266 | if (*min_uV < voltage->min_uV) | 
|  | 267 | *min_uV = voltage->min_uV; | 
| Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 268 | } | 
|  | 269 |  | 
| Mark Brown | dd8004a | 2012-11-28 17:09:27 +0000 | [diff] [blame] | 270 | if (*min_uV > *max_uV) { | 
| Russ Dill | 9c7b4e8 | 2013-02-14 04:46:33 -0800 | [diff] [blame] | 271 | rdev_err(rdev, "Restricting voltage, %u-%uuV\n", | 
|  | 272 | *min_uV, *max_uV); | 
| Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 273 | return -EINVAL; | 
| Mark Brown | dd8004a | 2012-11-28 17:09:27 +0000 | [diff] [blame] | 274 | } | 
| Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 275 |  | 
|  | 276 | return 0; | 
|  | 277 | } | 
|  | 278 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 279 | /* current constraint check */ | 
|  | 280 | static int regulator_check_current_limit(struct regulator_dev *rdev, | 
|  | 281 | int *min_uA, int *max_uA) | 
|  | 282 | { | 
|  | 283 | BUG_ON(*min_uA > *max_uA); | 
|  | 284 |  | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 285 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) { | 
| Stephen Boyd | 7ebcf26 | 2015-09-16 12:10:26 -0700 | [diff] [blame] | 286 | rdev_err(rdev, "current operation not allowed\n"); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 287 | return -EPERM; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | if (*max_uA > rdev->constraints->max_uA) | 
|  | 291 | *max_uA = rdev->constraints->max_uA; | 
|  | 292 | if (*min_uA < rdev->constraints->min_uA) | 
|  | 293 | *min_uA = rdev->constraints->min_uA; | 
|  | 294 |  | 
| Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 295 | if (*min_uA > *max_uA) { | 
|  | 296 | rdev_err(rdev, "unsupportable current range: %d-%duA\n", | 
| Mark Brown | 54abd33 | 2011-07-21 15:07:37 +0100 | [diff] [blame] | 297 | *min_uA, *max_uA); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 298 | return -EINVAL; | 
| Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 299 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 300 |  | 
|  | 301 | return 0; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | /* operating mode constraint check */ | 
| Charles Keepax | 109c75a | 2016-11-29 11:50:03 +0000 | [diff] [blame] | 305 | static int regulator_mode_constrain(struct regulator_dev *rdev, | 
|  | 306 | unsigned int *mode) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 307 | { | 
| Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 308 | switch (*mode) { | 
| David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 309 | case REGULATOR_MODE_FAST: | 
|  | 310 | case REGULATOR_MODE_NORMAL: | 
|  | 311 | case REGULATOR_MODE_IDLE: | 
|  | 312 | case REGULATOR_MODE_STANDBY: | 
|  | 313 | break; | 
|  | 314 | default: | 
| Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 315 | rdev_err(rdev, "invalid mode %x specified\n", *mode); | 
| David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 316 | return -EINVAL; | 
|  | 317 | } | 
|  | 318 |  | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 319 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) { | 
| Stephen Boyd | 7ebcf26 | 2015-09-16 12:10:26 -0700 | [diff] [blame] | 320 | rdev_err(rdev, "mode operation not allowed\n"); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 321 | return -EPERM; | 
|  | 322 | } | 
| Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 323 |  | 
|  | 324 | /* The modes are bitmasks, the most power hungry modes having | 
|  | 325 | * the lowest values. If the requested mode isn't supported | 
|  | 326 | * try higher modes. */ | 
|  | 327 | while (*mode) { | 
|  | 328 | if (rdev->constraints->valid_modes_mask & *mode) | 
|  | 329 | return 0; | 
|  | 330 | *mode /= 2; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 331 | } | 
| Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 332 |  | 
|  | 333 | return -EINVAL; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 334 | } | 
|  | 335 |  | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 336 | static inline struct regulator_state * | 
|  | 337 | regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state) | 
|  | 338 | { | 
|  | 339 | if (rdev->constraints == NULL) | 
|  | 340 | return NULL; | 
|  | 341 |  | 
|  | 342 | switch (state) { | 
|  | 343 | case PM_SUSPEND_STANDBY: | 
|  | 344 | return &rdev->constraints->state_standby; | 
|  | 345 | case PM_SUSPEND_MEM: | 
|  | 346 | return &rdev->constraints->state_mem; | 
|  | 347 | case PM_SUSPEND_MAX: | 
|  | 348 | return &rdev->constraints->state_disk; | 
|  | 349 | default: | 
|  | 350 | return NULL; | 
|  | 351 | } | 
|  | 352 | } | 
|  | 353 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 354 | static ssize_t regulator_uV_show(struct device *dev, | 
|  | 355 | struct device_attribute *attr, char *buf) | 
|  | 356 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 357 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 358 | ssize_t ret; | 
|  | 359 |  | 
|  | 360 | mutex_lock(&rdev->mutex); | 
|  | 361 | ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); | 
|  | 362 | mutex_unlock(&rdev->mutex); | 
|  | 363 |  | 
|  | 364 | return ret; | 
|  | 365 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 366 | static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 367 |  | 
|  | 368 | static ssize_t regulator_uA_show(struct device *dev, | 
|  | 369 | struct device_attribute *attr, char *buf) | 
|  | 370 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 371 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 372 |  | 
|  | 373 | return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); | 
|  | 374 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 375 | static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 376 |  | 
| Greg Kroah-Hartman | 587cea2 | 2013-07-24 15:05:21 -0700 | [diff] [blame] | 377 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, | 
|  | 378 | char *buf) | 
| Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 379 | { | 
|  | 380 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 381 |  | 
| Mark Brown | 1083c39 | 2009-10-22 16:31:32 +0100 | [diff] [blame] | 382 | return sprintf(buf, "%s\n", rdev_get_name(rdev)); | 
| Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 383 | } | 
| Greg Kroah-Hartman | 587cea2 | 2013-07-24 15:05:21 -0700 | [diff] [blame] | 384 | static DEVICE_ATTR_RO(name); | 
| Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 385 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 386 | static ssize_t regulator_print_opmode(char *buf, int mode) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 387 | { | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 388 | switch (mode) { | 
|  | 389 | case REGULATOR_MODE_FAST: | 
|  | 390 | return sprintf(buf, "fast\n"); | 
|  | 391 | case REGULATOR_MODE_NORMAL: | 
|  | 392 | return sprintf(buf, "normal\n"); | 
|  | 393 | case REGULATOR_MODE_IDLE: | 
|  | 394 | return sprintf(buf, "idle\n"); | 
|  | 395 | case REGULATOR_MODE_STANDBY: | 
|  | 396 | return sprintf(buf, "standby\n"); | 
|  | 397 | } | 
|  | 398 | return sprintf(buf, "unknown\n"); | 
|  | 399 | } | 
|  | 400 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 401 | static ssize_t regulator_opmode_show(struct device *dev, | 
|  | 402 | struct device_attribute *attr, char *buf) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 403 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 404 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 405 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 406 | return regulator_print_opmode(buf, _regulator_get_mode(rdev)); | 
|  | 407 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 408 | static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 409 |  | 
|  | 410 | static ssize_t regulator_print_state(char *buf, int state) | 
|  | 411 | { | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 412 | if (state > 0) | 
|  | 413 | return sprintf(buf, "enabled\n"); | 
|  | 414 | else if (state == 0) | 
|  | 415 | return sprintf(buf, "disabled\n"); | 
|  | 416 | else | 
|  | 417 | return sprintf(buf, "unknown\n"); | 
|  | 418 | } | 
|  | 419 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 420 | static ssize_t regulator_state_show(struct device *dev, | 
|  | 421 | struct device_attribute *attr, char *buf) | 
|  | 422 | { | 
|  | 423 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 424 | ssize_t ret; | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 425 |  | 
| Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 426 | mutex_lock(&rdev->mutex); | 
|  | 427 | ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); | 
|  | 428 | mutex_unlock(&rdev->mutex); | 
|  | 429 |  | 
|  | 430 | return ret; | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 431 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 432 | static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 433 |  | 
| David Brownell | 853116a | 2009-01-14 23:03:17 -0800 | [diff] [blame] | 434 | static ssize_t regulator_status_show(struct device *dev, | 
|  | 435 | struct device_attribute *attr, char *buf) | 
|  | 436 | { | 
|  | 437 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
|  | 438 | int status; | 
|  | 439 | char *label; | 
|  | 440 |  | 
|  | 441 | status = rdev->desc->ops->get_status(rdev); | 
|  | 442 | if (status < 0) | 
|  | 443 | return status; | 
|  | 444 |  | 
|  | 445 | switch (status) { | 
|  | 446 | case REGULATOR_STATUS_OFF: | 
|  | 447 | label = "off"; | 
|  | 448 | break; | 
|  | 449 | case REGULATOR_STATUS_ON: | 
|  | 450 | label = "on"; | 
|  | 451 | break; | 
|  | 452 | case REGULATOR_STATUS_ERROR: | 
|  | 453 | label = "error"; | 
|  | 454 | break; | 
|  | 455 | case REGULATOR_STATUS_FAST: | 
|  | 456 | label = "fast"; | 
|  | 457 | break; | 
|  | 458 | case REGULATOR_STATUS_NORMAL: | 
|  | 459 | label = "normal"; | 
|  | 460 | break; | 
|  | 461 | case REGULATOR_STATUS_IDLE: | 
|  | 462 | label = "idle"; | 
|  | 463 | break; | 
|  | 464 | case REGULATOR_STATUS_STANDBY: | 
|  | 465 | label = "standby"; | 
|  | 466 | break; | 
| Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 467 | case REGULATOR_STATUS_BYPASS: | 
|  | 468 | label = "bypass"; | 
|  | 469 | break; | 
| Krystian Garbaciak | 1beaf76 | 2012-07-12 13:53:35 +0100 | [diff] [blame] | 470 | case REGULATOR_STATUS_UNDEFINED: | 
|  | 471 | label = "undefined"; | 
|  | 472 | break; | 
| David Brownell | 853116a | 2009-01-14 23:03:17 -0800 | [diff] [blame] | 473 | default: | 
|  | 474 | return -ERANGE; | 
|  | 475 | } | 
|  | 476 |  | 
|  | 477 | return sprintf(buf, "%s\n", label); | 
|  | 478 | } | 
|  | 479 | static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); | 
|  | 480 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 481 | static ssize_t regulator_min_uA_show(struct device *dev, | 
|  | 482 | struct device_attribute *attr, char *buf) | 
|  | 483 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 484 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 485 |  | 
|  | 486 | if (!rdev->constraints) | 
|  | 487 | return sprintf(buf, "constraint not defined\n"); | 
|  | 488 |  | 
|  | 489 | return sprintf(buf, "%d\n", rdev->constraints->min_uA); | 
|  | 490 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 491 | static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 492 |  | 
|  | 493 | static ssize_t regulator_max_uA_show(struct device *dev, | 
|  | 494 | struct device_attribute *attr, char *buf) | 
|  | 495 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 496 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 497 |  | 
|  | 498 | if (!rdev->constraints) | 
|  | 499 | return sprintf(buf, "constraint not defined\n"); | 
|  | 500 |  | 
|  | 501 | return sprintf(buf, "%d\n", rdev->constraints->max_uA); | 
|  | 502 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 503 | static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 504 |  | 
|  | 505 | static ssize_t regulator_min_uV_show(struct device *dev, | 
|  | 506 | struct device_attribute *attr, char *buf) | 
|  | 507 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 508 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 509 |  | 
|  | 510 | if (!rdev->constraints) | 
|  | 511 | return sprintf(buf, "constraint not defined\n"); | 
|  | 512 |  | 
|  | 513 | return sprintf(buf, "%d\n", rdev->constraints->min_uV); | 
|  | 514 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 515 | static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 516 |  | 
|  | 517 | static ssize_t regulator_max_uV_show(struct device *dev, | 
|  | 518 | struct device_attribute *attr, char *buf) | 
|  | 519 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 520 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 521 |  | 
|  | 522 | if (!rdev->constraints) | 
|  | 523 | return sprintf(buf, "constraint not defined\n"); | 
|  | 524 |  | 
|  | 525 | return sprintf(buf, "%d\n", rdev->constraints->max_uV); | 
|  | 526 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 527 | static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 528 |  | 
|  | 529 | static ssize_t regulator_total_uA_show(struct device *dev, | 
|  | 530 | struct device_attribute *attr, char *buf) | 
|  | 531 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 532 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 533 | struct regulator *regulator; | 
|  | 534 | int uA = 0; | 
|  | 535 |  | 
|  | 536 | mutex_lock(&rdev->mutex); | 
|  | 537 | list_for_each_entry(regulator, &rdev->consumer_list, list) | 
| Stefan Roese | fa2984d | 2009-11-27 15:56:34 +0100 | [diff] [blame] | 538 | uA += regulator->uA_load; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 539 | mutex_unlock(&rdev->mutex); | 
|  | 540 | return sprintf(buf, "%d\n", uA); | 
|  | 541 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 542 | static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 543 |  | 
| Greg Kroah-Hartman | 587cea2 | 2013-07-24 15:05:21 -0700 | [diff] [blame] | 544 | static ssize_t num_users_show(struct device *dev, struct device_attribute *attr, | 
|  | 545 | char *buf) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 546 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 547 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 548 | return sprintf(buf, "%d\n", rdev->use_count); | 
|  | 549 | } | 
| Greg Kroah-Hartman | 587cea2 | 2013-07-24 15:05:21 -0700 | [diff] [blame] | 550 | static DEVICE_ATTR_RO(num_users); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 551 |  | 
| Greg Kroah-Hartman | 587cea2 | 2013-07-24 15:05:21 -0700 | [diff] [blame] | 552 | static ssize_t type_show(struct device *dev, struct device_attribute *attr, | 
|  | 553 | char *buf) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 554 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 555 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 556 |  | 
|  | 557 | switch (rdev->desc->type) { | 
|  | 558 | case REGULATOR_VOLTAGE: | 
|  | 559 | return sprintf(buf, "voltage\n"); | 
|  | 560 | case REGULATOR_CURRENT: | 
|  | 561 | return sprintf(buf, "current\n"); | 
|  | 562 | } | 
|  | 563 | return sprintf(buf, "unknown\n"); | 
|  | 564 | } | 
| Greg Kroah-Hartman | 587cea2 | 2013-07-24 15:05:21 -0700 | [diff] [blame] | 565 | static DEVICE_ATTR_RO(type); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 566 |  | 
|  | 567 | static ssize_t regulator_suspend_mem_uV_show(struct device *dev, | 
|  | 568 | struct device_attribute *attr, char *buf) | 
|  | 569 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 570 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 571 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 572 | return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); | 
|  | 573 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 574 | static DEVICE_ATTR(suspend_mem_microvolts, 0444, | 
|  | 575 | regulator_suspend_mem_uV_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 576 |  | 
|  | 577 | static ssize_t regulator_suspend_disk_uV_show(struct device *dev, | 
|  | 578 | struct device_attribute *attr, char *buf) | 
|  | 579 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 580 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 581 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 582 | return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); | 
|  | 583 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 584 | static DEVICE_ATTR(suspend_disk_microvolts, 0444, | 
|  | 585 | regulator_suspend_disk_uV_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 586 |  | 
|  | 587 | static ssize_t regulator_suspend_standby_uV_show(struct device *dev, | 
|  | 588 | struct device_attribute *attr, char *buf) | 
|  | 589 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 590 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 591 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 592 | return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); | 
|  | 593 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 594 | static DEVICE_ATTR(suspend_standby_microvolts, 0444, | 
|  | 595 | regulator_suspend_standby_uV_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 596 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 597 | static ssize_t regulator_suspend_mem_mode_show(struct device *dev, | 
|  | 598 | struct device_attribute *attr, char *buf) | 
|  | 599 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 600 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 601 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 602 | return regulator_print_opmode(buf, | 
|  | 603 | rdev->constraints->state_mem.mode); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 604 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 605 | static DEVICE_ATTR(suspend_mem_mode, 0444, | 
|  | 606 | regulator_suspend_mem_mode_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 607 |  | 
|  | 608 | static ssize_t regulator_suspend_disk_mode_show(struct device *dev, | 
|  | 609 | struct device_attribute *attr, char *buf) | 
|  | 610 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 611 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 612 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 613 | return regulator_print_opmode(buf, | 
|  | 614 | rdev->constraints->state_disk.mode); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 615 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 616 | static DEVICE_ATTR(suspend_disk_mode, 0444, | 
|  | 617 | regulator_suspend_disk_mode_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 618 |  | 
|  | 619 | static ssize_t regulator_suspend_standby_mode_show(struct device *dev, | 
|  | 620 | struct device_attribute *attr, char *buf) | 
|  | 621 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 622 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 623 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 624 | return regulator_print_opmode(buf, | 
|  | 625 | rdev->constraints->state_standby.mode); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 626 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 627 | static DEVICE_ATTR(suspend_standby_mode, 0444, | 
|  | 628 | regulator_suspend_standby_mode_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 629 |  | 
|  | 630 | static ssize_t regulator_suspend_mem_state_show(struct device *dev, | 
|  | 631 | struct device_attribute *attr, char *buf) | 
|  | 632 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 633 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 634 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 635 | return regulator_print_state(buf, | 
|  | 636 | rdev->constraints->state_mem.enabled); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 637 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 638 | static DEVICE_ATTR(suspend_mem_state, 0444, | 
|  | 639 | regulator_suspend_mem_state_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 640 |  | 
|  | 641 | static ssize_t regulator_suspend_disk_state_show(struct device *dev, | 
|  | 642 | struct device_attribute *attr, char *buf) | 
|  | 643 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 644 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 645 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 646 | return regulator_print_state(buf, | 
|  | 647 | rdev->constraints->state_disk.enabled); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 648 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 649 | static DEVICE_ATTR(suspend_disk_state, 0444, | 
|  | 650 | regulator_suspend_disk_state_show, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 651 |  | 
|  | 652 | static ssize_t regulator_suspend_standby_state_show(struct device *dev, | 
|  | 653 | struct device_attribute *attr, char *buf) | 
|  | 654 | { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 655 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 656 |  | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 657 | return regulator_print_state(buf, | 
|  | 658 | rdev->constraints->state_standby.enabled); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 659 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 660 | static DEVICE_ATTR(suspend_standby_state, 0444, | 
|  | 661 | regulator_suspend_standby_state_show, NULL); | 
| Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 662 |  | 
| Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 663 | static ssize_t regulator_bypass_show(struct device *dev, | 
|  | 664 | struct device_attribute *attr, char *buf) | 
|  | 665 | { | 
|  | 666 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
|  | 667 | const char *report; | 
|  | 668 | bool bypass; | 
|  | 669 | int ret; | 
|  | 670 |  | 
|  | 671 | ret = rdev->desc->ops->get_bypass(rdev, &bypass); | 
|  | 672 |  | 
|  | 673 | if (ret != 0) | 
|  | 674 | report = "unknown"; | 
|  | 675 | else if (bypass) | 
|  | 676 | report = "enabled"; | 
|  | 677 | else | 
|  | 678 | report = "disabled"; | 
|  | 679 |  | 
|  | 680 | return sprintf(buf, "%s\n", report); | 
|  | 681 | } | 
|  | 682 | static DEVICE_ATTR(bypass, 0444, | 
|  | 683 | regulator_bypass_show, NULL); | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 684 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 685 | /* Calculate the new optimum regulator operating mode based on the new total | 
|  | 686 | * consumer load. All locks held by caller */ | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 687 | static int drms_uA_update(struct regulator_dev *rdev) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 688 | { | 
|  | 689 | struct regulator *sibling; | 
|  | 690 | int current_uA = 0, output_uV, input_uV, err; | 
|  | 691 | unsigned int mode; | 
|  | 692 |  | 
| Krzysztof Kozlowski | 70cfef2 | 2015-06-29 09:04:43 +0900 | [diff] [blame] | 693 | lockdep_assert_held_once(&rdev->mutex); | 
|  | 694 |  | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 695 | /* | 
|  | 696 | * first check to see if we can set modes at all, otherwise just | 
|  | 697 | * tell the consumer everything is OK. | 
|  | 698 | */ | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 699 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 700 | return 0; | 
|  | 701 |  | 
| Bjorn Andersson | 8f4490e | 2015-02-11 19:39:12 -0800 | [diff] [blame] | 702 | if (!rdev->desc->ops->get_optimum_mode && | 
|  | 703 | !rdev->desc->ops->set_load) | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 704 | return 0; | 
|  | 705 |  | 
| Bjorn Andersson | 8f4490e | 2015-02-11 19:39:12 -0800 | [diff] [blame] | 706 | if (!rdev->desc->ops->set_mode && | 
|  | 707 | !rdev->desc->ops->set_load) | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 708 | return -EINVAL; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 709 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 710 | /* calc total requested load */ | 
|  | 711 | list_for_each_entry(sibling, &rdev->consumer_list, list) | 
| Stefan Roese | fa2984d | 2009-11-27 15:56:34 +0100 | [diff] [blame] | 712 | current_uA += sibling->uA_load; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 713 |  | 
| Stephen Boyd | 22a10bc | 2015-06-11 17:37:03 -0700 | [diff] [blame] | 714 | current_uA += rdev->constraints->system_load; | 
|  | 715 |  | 
| Bjorn Andersson | 8f4490e | 2015-02-11 19:39:12 -0800 | [diff] [blame] | 716 | if (rdev->desc->ops->set_load) { | 
|  | 717 | /* set the optimum mode for our new total regulator load */ | 
|  | 718 | err = rdev->desc->ops->set_load(rdev, current_uA); | 
|  | 719 | if (err < 0) | 
|  | 720 | rdev_err(rdev, "failed to set load %d\n", current_uA); | 
|  | 721 | } else { | 
| Joonwoo Park | 5777661 | 2016-09-19 14:46:54 -0700 | [diff] [blame] | 722 | /* get output voltage */ | 
|  | 723 | output_uV = _regulator_get_voltage(rdev); | 
|  | 724 | if (output_uV <= 0) { | 
|  | 725 | rdev_err(rdev, "invalid output voltage found\n"); | 
|  | 726 | return -EINVAL; | 
|  | 727 | } | 
|  | 728 |  | 
|  | 729 | /* get input voltage */ | 
|  | 730 | input_uV = 0; | 
|  | 731 | if (rdev->supply) | 
|  | 732 | input_uV = regulator_get_voltage(rdev->supply); | 
|  | 733 | if (input_uV <= 0) | 
|  | 734 | input_uV = rdev->constraints->input_uV; | 
|  | 735 | if (input_uV <= 0) { | 
|  | 736 | rdev_err(rdev, "invalid input voltage found\n"); | 
|  | 737 | return -EINVAL; | 
|  | 738 | } | 
|  | 739 |  | 
| Bjorn Andersson | 8f4490e | 2015-02-11 19:39:12 -0800 | [diff] [blame] | 740 | /* now get the optimum mode for our new total regulator load */ | 
|  | 741 | mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, | 
|  | 742 | output_uV, current_uA); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 743 |  | 
| Bjorn Andersson | 8f4490e | 2015-02-11 19:39:12 -0800 | [diff] [blame] | 744 | /* check the new mode is allowed */ | 
|  | 745 | err = regulator_mode_constrain(rdev, &mode); | 
|  | 746 | if (err < 0) { | 
|  | 747 | rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", | 
|  | 748 | current_uA, input_uV, output_uV); | 
|  | 749 | return err; | 
|  | 750 | } | 
|  | 751 |  | 
|  | 752 | err = rdev->desc->ops->set_mode(rdev, mode); | 
|  | 753 | if (err < 0) | 
|  | 754 | rdev_err(rdev, "failed to set optimum mode %x\n", mode); | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 755 | } | 
|  | 756 |  | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 757 | return err; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 758 | } | 
|  | 759 |  | 
|  | 760 | static int suspend_set_state(struct regulator_dev *rdev, | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 761 | suspend_state_t state) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 762 | { | 
|  | 763 | int ret = 0; | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 764 | struct regulator_state *rstate; | 
|  | 765 |  | 
|  | 766 | rstate = regulator_get_suspend_state(rdev, state); | 
|  | 767 | if (rstate == NULL) | 
|  | 768 | return -EINVAL; | 
| Mark Brown | 638f85c | 2009-10-22 16:31:33 +0100 | [diff] [blame] | 769 |  | 
|  | 770 | /* If we have no suspend mode configration don't set anything; | 
| Axel Lin | 8ac0e95 | 2012-04-14 09:14:34 +0800 | [diff] [blame] | 771 | * only warn if the driver implements set_suspend_voltage or | 
|  | 772 | * set_suspend_mode callback. | 
| Mark Brown | 638f85c | 2009-10-22 16:31:33 +0100 | [diff] [blame] | 773 | */ | 
| Chunyan Zhang | 72069f9 | 2018-01-26 21:08:45 +0800 | [diff] [blame] | 774 | if (rstate->enabled != ENABLE_IN_SUSPEND && | 
|  | 775 | rstate->enabled != DISABLE_IN_SUSPEND) { | 
| Axel Lin | 8ac0e95 | 2012-04-14 09:14:34 +0800 | [diff] [blame] | 776 | if (rdev->desc->ops->set_suspend_voltage || | 
|  | 777 | rdev->desc->ops->set_suspend_mode) | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 778 | rdev_warn(rdev, "No configuration\n"); | 
| Mark Brown | 638f85c | 2009-10-22 16:31:33 +0100 | [diff] [blame] | 779 | return 0; | 
|  | 780 | } | 
|  | 781 |  | 
| Chunyan Zhang | 72069f9 | 2018-01-26 21:08:45 +0800 | [diff] [blame] | 782 | if (rstate->enabled == ENABLE_IN_SUSPEND && | 
|  | 783 | rdev->desc->ops->set_suspend_enable) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 784 | ret = rdev->desc->ops->set_suspend_enable(rdev); | 
| Chunyan Zhang | 72069f9 | 2018-01-26 21:08:45 +0800 | [diff] [blame] | 785 | else if (rstate->enabled == DISABLE_IN_SUSPEND && | 
|  | 786 | rdev->desc->ops->set_suspend_disable) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 787 | ret = rdev->desc->ops->set_suspend_disable(rdev); | 
| Axel Lin | 8ac0e95 | 2012-04-14 09:14:34 +0800 | [diff] [blame] | 788 | else /* OK if set_suspend_enable or set_suspend_disable is NULL */ | 
|  | 789 | ret = 0; | 
|  | 790 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 791 | if (ret < 0) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 792 | rdev_err(rdev, "failed to enabled/disable\n"); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 793 | return ret; | 
|  | 794 | } | 
|  | 795 |  | 
|  | 796 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { | 
|  | 797 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); | 
|  | 798 | if (ret < 0) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 799 | rdev_err(rdev, "failed to set voltage\n"); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 800 | return ret; | 
|  | 801 | } | 
|  | 802 | } | 
|  | 803 |  | 
|  | 804 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { | 
|  | 805 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); | 
|  | 806 | if (ret < 0) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 807 | rdev_err(rdev, "failed to set mode\n"); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 808 | return ret; | 
|  | 809 | } | 
|  | 810 | } | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 811 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 812 | return ret; | 
|  | 813 | } | 
|  | 814 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 815 | static void print_constraints(struct regulator_dev *rdev) | 
|  | 816 | { | 
|  | 817 | struct regulation_constraints *constraints = rdev->constraints; | 
| Stefan Wahren | a7068e3 | 2015-06-09 20:09:42 +0000 | [diff] [blame] | 818 | char buf[160] = ""; | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 819 | size_t len = sizeof(buf) - 1; | 
| Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 820 | int count = 0; | 
|  | 821 | int ret; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 822 |  | 
| Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 823 | if (constraints->min_uV && constraints->max_uV) { | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 824 | if (constraints->min_uV == constraints->max_uV) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 825 | count += scnprintf(buf + count, len - count, "%d mV ", | 
|  | 826 | constraints->min_uV / 1000); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 827 | else | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 828 | count += scnprintf(buf + count, len - count, | 
|  | 829 | "%d <--> %d mV ", | 
|  | 830 | constraints->min_uV / 1000, | 
|  | 831 | constraints->max_uV / 1000); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 832 | } | 
| Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 833 |  | 
|  | 834 | if (!constraints->min_uV || | 
|  | 835 | constraints->min_uV != constraints->max_uV) { | 
|  | 836 | ret = _regulator_get_voltage(rdev); | 
|  | 837 | if (ret > 0) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 838 | count += scnprintf(buf + count, len - count, | 
|  | 839 | "at %d mV ", ret / 1000); | 
| Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 840 | } | 
|  | 841 |  | 
| Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 842 | if (constraints->uV_offset) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 843 | count += scnprintf(buf + count, len - count, "%dmV offset ", | 
|  | 844 | constraints->uV_offset / 1000); | 
| Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 845 |  | 
| Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 846 | if (constraints->min_uA && constraints->max_uA) { | 
|  | 847 | if (constraints->min_uA == constraints->max_uA) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 848 | count += scnprintf(buf + count, len - count, "%d mA ", | 
|  | 849 | constraints->min_uA / 1000); | 
| Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 850 | else | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 851 | count += scnprintf(buf + count, len - count, | 
|  | 852 | "%d <--> %d mA ", | 
|  | 853 | constraints->min_uA / 1000, | 
|  | 854 | constraints->max_uA / 1000); | 
| Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 855 | } | 
|  | 856 |  | 
|  | 857 | if (!constraints->min_uA || | 
|  | 858 | constraints->min_uA != constraints->max_uA) { | 
|  | 859 | ret = _regulator_get_current_limit(rdev); | 
|  | 860 | if (ret > 0) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 861 | count += scnprintf(buf + count, len - count, | 
|  | 862 | "at %d mA ", ret / 1000); | 
| Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 863 | } | 
|  | 864 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 865 | if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 866 | count += scnprintf(buf + count, len - count, "fast "); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 867 | if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 868 | count += scnprintf(buf + count, len - count, "normal "); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 869 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 870 | count += scnprintf(buf + count, len - count, "idle "); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 871 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 872 | count += scnprintf(buf + count, len - count, "standby"); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 873 |  | 
| Uwe Kleine-König | 215b8b0 | 2012-08-07 21:01:37 +0200 | [diff] [blame] | 874 | if (!count) | 
| Stefan Wahren | 5751a99 | 2015-06-10 06:13:15 +0000 | [diff] [blame] | 875 | scnprintf(buf, len, "no parameters"); | 
| Uwe Kleine-König | 215b8b0 | 2012-08-07 21:01:37 +0200 | [diff] [blame] | 876 |  | 
| Mark Brown | 194dbae | 2014-10-31 19:11:59 +0000 | [diff] [blame] | 877 | rdev_dbg(rdev, "%s\n", buf); | 
| Mark Brown | 4a68292 | 2012-02-09 13:26:13 +0000 | [diff] [blame] | 878 |  | 
|  | 879 | if ((constraints->min_uV != constraints->max_uV) && | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 880 | !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) | 
| Mark Brown | 4a68292 | 2012-02-09 13:26:13 +0000 | [diff] [blame] | 881 | rdev_warn(rdev, | 
|  | 882 | "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n"); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 883 | } | 
|  | 884 |  | 
| Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 885 | static int machine_constraints_voltage(struct regulator_dev *rdev, | 
| Mark Brown | 1083c39 | 2009-10-22 16:31:32 +0100 | [diff] [blame] | 886 | struct regulation_constraints *constraints) | 
| Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 887 | { | 
| Guodong Xu | 272e231 | 2014-08-13 19:33:38 +0800 | [diff] [blame] | 888 | const struct regulator_ops *ops = rdev->desc->ops; | 
| Mark Brown | af5866c | 2009-10-22 16:31:30 +0100 | [diff] [blame] | 889 | int ret; | 
|  | 890 |  | 
|  | 891 | /* do we need to apply the constraint voltage */ | 
|  | 892 | if (rdev->constraints->apply_uV && | 
| Mark Brown | fa93fd4 | 2016-03-21 18:12:52 +0000 | [diff] [blame] | 893 | rdev->constraints->min_uV && rdev->constraints->max_uV) { | 
|  | 894 | int target_min, target_max; | 
| Alban Bedel | 064d5cd | 2014-05-20 12:12:16 +0200 | [diff] [blame] | 895 | int current_uV = _regulator_get_voltage(rdev); | 
|  | 896 | if (current_uV < 0) { | 
| Nishanth Menon | 69d5883 | 2014-06-04 14:53:25 -0500 | [diff] [blame] | 897 | rdev_err(rdev, | 
|  | 898 | "failed to get the current voltage(%d)\n", | 
|  | 899 | current_uV); | 
| Alban Bedel | 064d5cd | 2014-05-20 12:12:16 +0200 | [diff] [blame] | 900 | return current_uV; | 
|  | 901 | } | 
| Mark Brown | fa93fd4 | 2016-03-21 18:12:52 +0000 | [diff] [blame] | 902 |  | 
|  | 903 | /* | 
|  | 904 | * If we're below the minimum voltage move up to the | 
|  | 905 | * minimum voltage, if we're above the maximum voltage | 
|  | 906 | * then move down to the maximum. | 
|  | 907 | */ | 
|  | 908 | target_min = current_uV; | 
|  | 909 | target_max = current_uV; | 
|  | 910 |  | 
|  | 911 | if (current_uV < rdev->constraints->min_uV) { | 
|  | 912 | target_min = rdev->constraints->min_uV; | 
|  | 913 | target_max = rdev->constraints->min_uV; | 
|  | 914 | } | 
|  | 915 |  | 
|  | 916 | if (current_uV > rdev->constraints->max_uV) { | 
|  | 917 | target_min = rdev->constraints->max_uV; | 
|  | 918 | target_max = rdev->constraints->max_uV; | 
|  | 919 | } | 
|  | 920 |  | 
|  | 921 | if (target_min != current_uV || target_max != current_uV) { | 
| Mark Brown | 45a91e8 | 2016-03-29 16:33:42 -0700 | [diff] [blame] | 922 | rdev_info(rdev, "Bringing %duV into %d-%duV\n", | 
|  | 923 | current_uV, target_min, target_max); | 
| Alban Bedel | 064d5cd | 2014-05-20 12:12:16 +0200 | [diff] [blame] | 924 | ret = _regulator_do_set_voltage( | 
| Mark Brown | fa93fd4 | 2016-03-21 18:12:52 +0000 | [diff] [blame] | 925 | rdev, target_min, target_max); | 
| Alban Bedel | 064d5cd | 2014-05-20 12:12:16 +0200 | [diff] [blame] | 926 | if (ret < 0) { | 
|  | 927 | rdev_err(rdev, | 
| Mark Brown | fa93fd4 | 2016-03-21 18:12:52 +0000 | [diff] [blame] | 928 | "failed to apply %d-%duV constraint(%d)\n", | 
|  | 929 | target_min, target_max, ret); | 
| Alban Bedel | 064d5cd | 2014-05-20 12:12:16 +0200 | [diff] [blame] | 930 | return ret; | 
|  | 931 | } | 
| Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 932 | } | 
| Mark Brown | af5866c | 2009-10-22 16:31:30 +0100 | [diff] [blame] | 933 | } | 
| Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 934 |  | 
|  | 935 | /* constrain machine-level voltage specs to fit | 
|  | 936 | * the actual range supported by this regulator. | 
|  | 937 | */ | 
|  | 938 | if (ops->list_voltage && rdev->desc->n_voltages) { | 
|  | 939 | int	count = rdev->desc->n_voltages; | 
|  | 940 | int	i; | 
|  | 941 | int	min_uV = INT_MAX; | 
|  | 942 | int	max_uV = INT_MIN; | 
|  | 943 | int	cmin = constraints->min_uV; | 
|  | 944 | int	cmax = constraints->max_uV; | 
|  | 945 |  | 
|  | 946 | /* it's safe to autoconfigure fixed-voltage supplies | 
|  | 947 | and the constraints are used by list_voltage. */ | 
|  | 948 | if (count == 1 && !cmin) { | 
|  | 949 | cmin = 1; | 
|  | 950 | cmax = INT_MAX; | 
|  | 951 | constraints->min_uV = cmin; | 
|  | 952 | constraints->max_uV = cmax; | 
|  | 953 | } | 
|  | 954 |  | 
|  | 955 | /* voltage constraints are optional */ | 
|  | 956 | if ((cmin == 0) && (cmax == 0)) | 
|  | 957 | return 0; | 
|  | 958 |  | 
|  | 959 | /* else require explicit machine-level constraints */ | 
|  | 960 | if (cmin <= 0 || cmax <= 0 || cmax < cmin) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 961 | rdev_err(rdev, "invalid voltage constraints\n"); | 
| Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 962 | return -EINVAL; | 
|  | 963 | } | 
|  | 964 |  | 
|  | 965 | /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ | 
|  | 966 | for (i = 0; i < count; i++) { | 
|  | 967 | int	value; | 
|  | 968 |  | 
|  | 969 | value = ops->list_voltage(rdev, i); | 
|  | 970 | if (value <= 0) | 
|  | 971 | continue; | 
|  | 972 |  | 
|  | 973 | /* maybe adjust [min_uV..max_uV] */ | 
|  | 974 | if (value >= cmin && value < min_uV) | 
|  | 975 | min_uV = value; | 
|  | 976 | if (value <= cmax && value > max_uV) | 
|  | 977 | max_uV = value; | 
|  | 978 | } | 
|  | 979 |  | 
|  | 980 | /* final: [min_uV..max_uV] valid iff constraints valid */ | 
|  | 981 | if (max_uV < min_uV) { | 
| Mark Brown | fff15be | 2012-11-27 18:48:56 +0000 | [diff] [blame] | 982 | rdev_err(rdev, | 
|  | 983 | "unsupportable voltage constraints %u-%uuV\n", | 
|  | 984 | min_uV, max_uV); | 
| Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 985 | return -EINVAL; | 
|  | 986 | } | 
|  | 987 |  | 
|  | 988 | /* use regulator's subset of machine constraints */ | 
|  | 989 | if (constraints->min_uV < min_uV) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 990 | rdev_dbg(rdev, "override min_uV, %d -> %d\n", | 
|  | 991 | constraints->min_uV, min_uV); | 
| Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 992 | constraints->min_uV = min_uV; | 
|  | 993 | } | 
|  | 994 | if (constraints->max_uV > max_uV) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 995 | rdev_dbg(rdev, "override max_uV, %d -> %d\n", | 
|  | 996 | constraints->max_uV, max_uV); | 
| Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 997 | constraints->max_uV = max_uV; | 
|  | 998 | } | 
|  | 999 | } | 
|  | 1000 |  | 
|  | 1001 | return 0; | 
|  | 1002 | } | 
|  | 1003 |  | 
| Laxman Dewangan | f8c1700 | 2013-09-20 13:13:02 +0530 | [diff] [blame] | 1004 | static int machine_constraints_current(struct regulator_dev *rdev, | 
|  | 1005 | struct regulation_constraints *constraints) | 
|  | 1006 | { | 
| Guodong Xu | 272e231 | 2014-08-13 19:33:38 +0800 | [diff] [blame] | 1007 | const struct regulator_ops *ops = rdev->desc->ops; | 
| Laxman Dewangan | f8c1700 | 2013-09-20 13:13:02 +0530 | [diff] [blame] | 1008 | int ret; | 
|  | 1009 |  | 
|  | 1010 | if (!constraints->min_uA && !constraints->max_uA) | 
|  | 1011 | return 0; | 
|  | 1012 |  | 
|  | 1013 | if (constraints->min_uA > constraints->max_uA) { | 
|  | 1014 | rdev_err(rdev, "Invalid current constraints\n"); | 
|  | 1015 | return -EINVAL; | 
|  | 1016 | } | 
|  | 1017 |  | 
|  | 1018 | if (!ops->set_current_limit || !ops->get_current_limit) { | 
|  | 1019 | rdev_warn(rdev, "Operation of current configuration missing\n"); | 
|  | 1020 | return 0; | 
|  | 1021 | } | 
|  | 1022 |  | 
|  | 1023 | /* Set regulator current in constraints range */ | 
|  | 1024 | ret = ops->set_current_limit(rdev, constraints->min_uA, | 
|  | 1025 | constraints->max_uA); | 
|  | 1026 | if (ret < 0) { | 
|  | 1027 | rdev_err(rdev, "Failed to set current constraint, %d\n", ret); | 
|  | 1028 | return ret; | 
|  | 1029 | } | 
|  | 1030 |  | 
|  | 1031 | return 0; | 
|  | 1032 | } | 
|  | 1033 |  | 
| Markus Pargmann | 30c2197 | 2014-02-20 17:36:03 +0100 | [diff] [blame] | 1034 | static int _regulator_do_enable(struct regulator_dev *rdev); | 
|  | 1035 |  | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1036 | /** | 
|  | 1037 | * set_machine_constraints - sets regulator constraints | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1038 | * @rdev: regulator source | 
| Mark Brown | c8e7e46 | 2008-12-31 12:52:42 +0000 | [diff] [blame] | 1039 | * @constraints: constraints to apply | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1040 | * | 
|  | 1041 | * Allows platform initialisation code to define and constrain | 
|  | 1042 | * regulator circuits e.g. valid voltage/current ranges, etc.  NOTE: | 
|  | 1043 | * Constraints *must* be set by platform code in order for some | 
|  | 1044 | * regulator operations to proceed i.e. set_voltage, set_current_limit, | 
|  | 1045 | * set_mode. | 
|  | 1046 | */ | 
|  | 1047 | static int set_machine_constraints(struct regulator_dev *rdev, | 
| Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 1048 | const struct regulation_constraints *constraints) | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1049 | { | 
|  | 1050 | int ret = 0; | 
| Guodong Xu | 272e231 | 2014-08-13 19:33:38 +0800 | [diff] [blame] | 1051 | const struct regulator_ops *ops = rdev->desc->ops; | 
| Mark Brown | e06f5b4 | 2008-09-09 16:21:19 +0100 | [diff] [blame] | 1052 |  | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 1053 | if (constraints) | 
|  | 1054 | rdev->constraints = kmemdup(constraints, sizeof(*constraints), | 
|  | 1055 | GFP_KERNEL); | 
|  | 1056 | else | 
|  | 1057 | rdev->constraints = kzalloc(sizeof(*constraints), | 
|  | 1058 | GFP_KERNEL); | 
| Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 1059 | if (!rdev->constraints) | 
|  | 1060 | return -ENOMEM; | 
| Mark Brown | af5866c | 2009-10-22 16:31:30 +0100 | [diff] [blame] | 1061 |  | 
| Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 1062 | ret = machine_constraints_voltage(rdev, rdev->constraints); | 
| Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 1063 | if (ret != 0) | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1064 | return ret; | 
| David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 1065 |  | 
| Laxman Dewangan | f8c1700 | 2013-09-20 13:13:02 +0530 | [diff] [blame] | 1066 | ret = machine_constraints_current(rdev, rdev->constraints); | 
|  | 1067 | if (ret != 0) | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1068 | return ret; | 
| Laxman Dewangan | f8c1700 | 2013-09-20 13:13:02 +0530 | [diff] [blame] | 1069 |  | 
| Stephen Boyd | 36e4f83 | 2015-06-11 17:37:06 -0700 | [diff] [blame] | 1070 | if (rdev->constraints->ilim_uA && ops->set_input_current_limit) { | 
|  | 1071 | ret = ops->set_input_current_limit(rdev, | 
|  | 1072 | rdev->constraints->ilim_uA); | 
|  | 1073 | if (ret < 0) { | 
|  | 1074 | rdev_err(rdev, "failed to set input limit\n"); | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1075 | return ret; | 
| Stephen Boyd | 36e4f83 | 2015-06-11 17:37:06 -0700 | [diff] [blame] | 1076 | } | 
|  | 1077 | } | 
|  | 1078 |  | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1079 | /* do we need to setup our suspend state */ | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 1080 | if (rdev->constraints->initial_state) { | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 1081 | ret = suspend_set_state(rdev, rdev->constraints->initial_state); | 
| Mark Brown | e06f5b4 | 2008-09-09 16:21:19 +0100 | [diff] [blame] | 1082 | if (ret < 0) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1083 | rdev_err(rdev, "failed to set suspend state\n"); | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1084 | return ret; | 
| Mark Brown | e06f5b4 | 2008-09-09 16:21:19 +0100 | [diff] [blame] | 1085 | } | 
|  | 1086 | } | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1087 |  | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 1088 | if (rdev->constraints->initial_mode) { | 
| Mark Brown | a308466 | 2009-02-26 19:24:19 +0000 | [diff] [blame] | 1089 | if (!ops->set_mode) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1090 | rdev_err(rdev, "no set_mode operation\n"); | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1091 | return -EINVAL; | 
| Mark Brown | a308466 | 2009-02-26 19:24:19 +0000 | [diff] [blame] | 1092 | } | 
|  | 1093 |  | 
| Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 1094 | ret = ops->set_mode(rdev, rdev->constraints->initial_mode); | 
| Mark Brown | a308466 | 2009-02-26 19:24:19 +0000 | [diff] [blame] | 1095 | if (ret < 0) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1096 | rdev_err(rdev, "failed to set initial mode: %d\n", ret); | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1097 | return ret; | 
| Mark Brown | a308466 | 2009-02-26 19:24:19 +0000 | [diff] [blame] | 1098 | } | 
|  | 1099 | } | 
|  | 1100 |  | 
| Mark Brown | cacf90f | 2009-03-02 16:32:46 +0000 | [diff] [blame] | 1101 | /* If the constraints say the regulator should be on at this point | 
|  | 1102 | * and we have control then make sure it is enabled. | 
|  | 1103 | */ | 
| Markus Pargmann | 30c2197 | 2014-02-20 17:36:03 +0100 | [diff] [blame] | 1104 | if (rdev->constraints->always_on || rdev->constraints->boot_on) { | 
|  | 1105 | ret = _regulator_do_enable(rdev); | 
|  | 1106 | if (ret < 0 && ret != -EINVAL) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1107 | rdev_err(rdev, "failed to enable\n"); | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1108 | return ret; | 
| Mark Brown | e5fda26 | 2008-09-09 16:21:20 +0100 | [diff] [blame] | 1109 | } | 
|  | 1110 | } | 
|  | 1111 |  | 
| Yadwinder Singh Brar | 1653ccf | 2013-06-29 18:21:15 +0530 | [diff] [blame] | 1112 | if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) | 
|  | 1113 | && ops->set_ramp_delay) { | 
| Yadwinder Singh Brar | 6f0b2c6 | 2012-06-11 17:41:08 +0530 | [diff] [blame] | 1114 | ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); | 
|  | 1115 | if (ret < 0) { | 
|  | 1116 | rdev_err(rdev, "failed to set ramp_delay\n"); | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1117 | return ret; | 
| Yadwinder Singh Brar | 6f0b2c6 | 2012-06-11 17:41:08 +0530 | [diff] [blame] | 1118 | } | 
|  | 1119 | } | 
|  | 1120 |  | 
| Stephen Boyd | 23c779b | 2015-06-11 17:37:04 -0700 | [diff] [blame] | 1121 | if (rdev->constraints->pull_down && ops->set_pull_down) { | 
|  | 1122 | ret = ops->set_pull_down(rdev); | 
|  | 1123 | if (ret < 0) { | 
|  | 1124 | rdev_err(rdev, "failed to set pull down\n"); | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1125 | return ret; | 
| Stephen Boyd | 23c779b | 2015-06-11 17:37:04 -0700 | [diff] [blame] | 1126 | } | 
|  | 1127 | } | 
|  | 1128 |  | 
| Stephen Boyd | 57f66b7 | 2015-06-11 17:37:05 -0700 | [diff] [blame] | 1129 | if (rdev->constraints->soft_start && ops->set_soft_start) { | 
|  | 1130 | ret = ops->set_soft_start(rdev); | 
|  | 1131 | if (ret < 0) { | 
|  | 1132 | rdev_err(rdev, "failed to set soft start\n"); | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1133 | return ret; | 
| Stephen Boyd | 57f66b7 | 2015-06-11 17:37:05 -0700 | [diff] [blame] | 1134 | } | 
|  | 1135 | } | 
|  | 1136 |  | 
| Stephen Boyd | 3a003ba | 2015-07-17 14:41:54 -0700 | [diff] [blame] | 1137 | if (rdev->constraints->over_current_protection | 
|  | 1138 | && ops->set_over_current_protection) { | 
|  | 1139 | ret = ops->set_over_current_protection(rdev); | 
|  | 1140 | if (ret < 0) { | 
|  | 1141 | rdev_err(rdev, "failed to set over current protection\n"); | 
| Charles Keepax | 6333ef4 | 2016-01-26 16:38:59 +0000 | [diff] [blame] | 1142 | return ret; | 
| Stephen Boyd | 3a003ba | 2015-07-17 14:41:54 -0700 | [diff] [blame] | 1143 | } | 
|  | 1144 | } | 
|  | 1145 |  | 
| Laxman Dewangan | 670666b | 2016-03-02 16:24:46 +0530 | [diff] [blame] | 1146 | if (rdev->constraints->active_discharge && ops->set_active_discharge) { | 
|  | 1147 | bool ad_state = (rdev->constraints->active_discharge == | 
|  | 1148 | REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false; | 
|  | 1149 |  | 
|  | 1150 | ret = ops->set_active_discharge(rdev, ad_state); | 
|  | 1151 | if (ret < 0) { | 
|  | 1152 | rdev_err(rdev, "failed to set active discharge\n"); | 
|  | 1153 | return ret; | 
|  | 1154 | } | 
|  | 1155 | } | 
|  | 1156 |  | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1157 | print_constraints(rdev); | 
| Axel Lin | 1a6958e7 | 2011-07-15 10:50:43 +0800 | [diff] [blame] | 1158 | return 0; | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1159 | } | 
|  | 1160 |  | 
|  | 1161 | /** | 
|  | 1162 | * set_supply - set regulator supply regulator | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1163 | * @rdev: regulator name | 
|  | 1164 | * @supply_rdev: supply regulator name | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1165 | * | 
|  | 1166 | * Called by platform initialisation code to set the supply regulator for this | 
|  | 1167 | * regulator. This ensures that a regulators supply will also be enabled by the | 
|  | 1168 | * core if it's child is enabled. | 
|  | 1169 | */ | 
|  | 1170 | static int set_supply(struct regulator_dev *rdev, | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1171 | struct regulator_dev *supply_rdev) | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1172 | { | 
|  | 1173 | int err; | 
|  | 1174 |  | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1175 | rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev)); | 
|  | 1176 |  | 
| Javier Martinez Canillas | e2c09ae | 2015-07-15 16:10:28 +0200 | [diff] [blame] | 1177 | if (!try_module_get(supply_rdev->owner)) | 
|  | 1178 | return -ENODEV; | 
|  | 1179 |  | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1180 | rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); | 
| Axel Lin | 32c78de | 2011-12-29 17:03:20 +0800 | [diff] [blame] | 1181 | if (rdev->supply == NULL) { | 
|  | 1182 | err = -ENOMEM; | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1183 | return err; | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1184 | } | 
| Laxman Dewangan | 57ad526a | 2012-07-23 20:35:46 +0530 | [diff] [blame] | 1185 | supply_rdev->open_count++; | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1186 |  | 
|  | 1187 | return 0; | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1188 | } | 
|  | 1189 |  | 
|  | 1190 | /** | 
| Randy Dunlap | 06c63f9 | 2010-11-18 15:02:26 -0800 | [diff] [blame] | 1191 | * set_consumer_device_supply - Bind a regulator to a symbolic supply | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1192 | * @rdev:         regulator source | 
| Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 1193 | * @consumer_dev_name: dev_name() string for device supply applies to | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1194 | * @supply:       symbolic name for supply | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1195 | * | 
|  | 1196 | * Allows platform initialisation code to map physical regulator | 
|  | 1197 | * sources to symbolic names for supplies for use by devices.  Devices | 
|  | 1198 | * should use these symbolic names to request regulators, avoiding the | 
|  | 1199 | * need to provide board-specific regulator names as platform data. | 
|  | 1200 | */ | 
|  | 1201 | static int set_consumer_device_supply(struct regulator_dev *rdev, | 
| Mark Brown | 737f360 | 2012-02-02 00:10:51 +0000 | [diff] [blame] | 1202 | const char *consumer_dev_name, | 
|  | 1203 | const char *supply) | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1204 | { | 
|  | 1205 | struct regulator_map *node; | 
| Mark Brown | 9ed2099 | 2009-07-21 16:00:26 +0100 | [diff] [blame] | 1206 | int has_dev; | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1207 |  | 
|  | 1208 | if (supply == NULL) | 
|  | 1209 | return -EINVAL; | 
|  | 1210 |  | 
| Mark Brown | 9ed2099 | 2009-07-21 16:00:26 +0100 | [diff] [blame] | 1211 | if (consumer_dev_name != NULL) | 
|  | 1212 | has_dev = 1; | 
|  | 1213 | else | 
|  | 1214 | has_dev = 0; | 
|  | 1215 |  | 
| David Brownell | 6001e13 | 2008-12-31 12:54:19 +0000 | [diff] [blame] | 1216 | list_for_each_entry(node, ®ulator_map_list, list) { | 
| Jani Nikula | 23b5cc2 | 2010-04-29 10:55:09 +0300 | [diff] [blame] | 1217 | if (node->dev_name && consumer_dev_name) { | 
|  | 1218 | if (strcmp(node->dev_name, consumer_dev_name) != 0) | 
|  | 1219 | continue; | 
|  | 1220 | } else if (node->dev_name || consumer_dev_name) { | 
| David Brownell | 6001e13 | 2008-12-31 12:54:19 +0000 | [diff] [blame] | 1221 | continue; | 
| Jani Nikula | 23b5cc2 | 2010-04-29 10:55:09 +0300 | [diff] [blame] | 1222 | } | 
|  | 1223 |  | 
| David Brownell | 6001e13 | 2008-12-31 12:54:19 +0000 | [diff] [blame] | 1224 | if (strcmp(node->supply, supply) != 0) | 
|  | 1225 | continue; | 
|  | 1226 |  | 
| Mark Brown | 737f360 | 2012-02-02 00:10:51 +0000 | [diff] [blame] | 1227 | pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n", | 
|  | 1228 | consumer_dev_name, | 
|  | 1229 | dev_name(&node->regulator->dev), | 
|  | 1230 | node->regulator->desc->name, | 
|  | 1231 | supply, | 
|  | 1232 | dev_name(&rdev->dev), rdev_get_name(rdev)); | 
| David Brownell | 6001e13 | 2008-12-31 12:54:19 +0000 | [diff] [blame] | 1233 | return -EBUSY; | 
|  | 1234 | } | 
|  | 1235 |  | 
| Mark Brown | 9ed2099 | 2009-07-21 16:00:26 +0100 | [diff] [blame] | 1236 | node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1237 | if (node == NULL) | 
|  | 1238 | return -ENOMEM; | 
|  | 1239 |  | 
|  | 1240 | node->regulator = rdev; | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1241 | node->supply = supply; | 
|  | 1242 |  | 
| Mark Brown | 9ed2099 | 2009-07-21 16:00:26 +0100 | [diff] [blame] | 1243 | if (has_dev) { | 
|  | 1244 | node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); | 
|  | 1245 | if (node->dev_name == NULL) { | 
|  | 1246 | kfree(node); | 
|  | 1247 | return -ENOMEM; | 
|  | 1248 | } | 
| Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 1249 | } | 
|  | 1250 |  | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1251 | list_add(&node->list, ®ulator_map_list); | 
|  | 1252 | return 0; | 
|  | 1253 | } | 
|  | 1254 |  | 
| Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 1255 | static void unset_regulator_supplies(struct regulator_dev *rdev) | 
|  | 1256 | { | 
|  | 1257 | struct regulator_map *node, *n; | 
|  | 1258 |  | 
|  | 1259 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { | 
|  | 1260 | if (rdev == node->regulator) { | 
|  | 1261 | list_del(&node->list); | 
| Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 1262 | kfree(node->dev_name); | 
| Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 1263 | kfree(node); | 
| Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 1264 | } | 
|  | 1265 | } | 
|  | 1266 | } | 
|  | 1267 |  | 
| Richard Fitzgerald | 2d80a91 | 2016-04-21 17:23:21 +0100 | [diff] [blame] | 1268 | #ifdef CONFIG_DEBUG_FS | 
|  | 1269 | static ssize_t constraint_flags_read_file(struct file *file, | 
|  | 1270 | char __user *user_buf, | 
|  | 1271 | size_t count, loff_t *ppos) | 
|  | 1272 | { | 
|  | 1273 | const struct regulator *regulator = file->private_data; | 
|  | 1274 | const struct regulation_constraints *c = regulator->rdev->constraints; | 
|  | 1275 | char *buf; | 
|  | 1276 | ssize_t ret; | 
|  | 1277 |  | 
|  | 1278 | if (!c) | 
|  | 1279 | return 0; | 
|  | 1280 |  | 
|  | 1281 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | 
|  | 1282 | if (!buf) | 
|  | 1283 | return -ENOMEM; | 
|  | 1284 |  | 
|  | 1285 | ret = snprintf(buf, PAGE_SIZE, | 
|  | 1286 | "always_on: %u\n" | 
|  | 1287 | "boot_on: %u\n" | 
|  | 1288 | "apply_uV: %u\n" | 
|  | 1289 | "ramp_disable: %u\n" | 
|  | 1290 | "soft_start: %u\n" | 
|  | 1291 | "pull_down: %u\n" | 
|  | 1292 | "over_current_protection: %u\n", | 
|  | 1293 | c->always_on, | 
|  | 1294 | c->boot_on, | 
|  | 1295 | c->apply_uV, | 
|  | 1296 | c->ramp_disable, | 
|  | 1297 | c->soft_start, | 
|  | 1298 | c->pull_down, | 
|  | 1299 | c->over_current_protection); | 
|  | 1300 |  | 
|  | 1301 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); | 
|  | 1302 | kfree(buf); | 
|  | 1303 |  | 
|  | 1304 | return ret; | 
|  | 1305 | } | 
|  | 1306 |  | 
|  | 1307 | #endif | 
|  | 1308 |  | 
|  | 1309 | static const struct file_operations constraint_flags_fops = { | 
|  | 1310 | #ifdef CONFIG_DEBUG_FS | 
|  | 1311 | .open = simple_open, | 
|  | 1312 | .read = constraint_flags_read_file, | 
|  | 1313 | .llseek = default_llseek, | 
|  | 1314 | #endif | 
|  | 1315 | }; | 
|  | 1316 |  | 
| Mark Brown | f5726ae | 2011-06-09 16:22:20 +0100 | [diff] [blame] | 1317 | #define REG_STR_SIZE	64 | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1318 |  | 
|  | 1319 | static struct regulator *create_regulator(struct regulator_dev *rdev, | 
|  | 1320 | struct device *dev, | 
|  | 1321 | const char *supply_name) | 
|  | 1322 | { | 
|  | 1323 | struct regulator *regulator; | 
|  | 1324 | char buf[REG_STR_SIZE]; | 
|  | 1325 | int err, size; | 
|  | 1326 |  | 
|  | 1327 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); | 
|  | 1328 | if (regulator == NULL) | 
|  | 1329 | return NULL; | 
|  | 1330 |  | 
|  | 1331 | mutex_lock(&rdev->mutex); | 
|  | 1332 | regulator->rdev = rdev; | 
|  | 1333 | list_add(®ulator->list, &rdev->consumer_list); | 
|  | 1334 |  | 
|  | 1335 | if (dev) { | 
| Shawn Guo | e2c98ea | 2012-07-05 14:19:42 +0800 | [diff] [blame] | 1336 | regulator->dev = dev; | 
|  | 1337 |  | 
| Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1338 | /* Add a link to the device sysfs entry */ | 
| Bartosz Golaszewski | b7cd1b1 | 2017-03-06 17:34:48 +0100 | [diff] [blame] | 1339 | size = snprintf(buf, REG_STR_SIZE, "%s-%s", | 
|  | 1340 | dev->kobj.name, supply_name); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1341 | if (size >= REG_STR_SIZE) | 
| Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1342 | goto overflow_err; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1343 |  | 
|  | 1344 | regulator->supply_name = kstrdup(buf, GFP_KERNEL); | 
|  | 1345 | if (regulator->supply_name == NULL) | 
| Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1346 | goto overflow_err; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1347 |  | 
| Stephen Boyd | ff268b5 | 2015-06-01 18:47:54 -0700 | [diff] [blame] | 1348 | err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj, | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1349 | buf); | 
|  | 1350 | if (err) { | 
| Stephen Boyd | ff268b5 | 2015-06-01 18:47:54 -0700 | [diff] [blame] | 1351 | rdev_dbg(rdev, "could not add device link %s err %d\n", | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1352 | dev->kobj.name, err); | 
| Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1353 | /* non-fatal */ | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1354 | } | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1355 | } else { | 
| Stephen Boyd | 0630b61 | 2017-03-16 18:07:14 -0700 | [diff] [blame] | 1356 | regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL); | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1357 | if (regulator->supply_name == NULL) | 
| Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1358 | goto overflow_err; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1359 | } | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1360 |  | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1361 | regulator->debugfs = debugfs_create_dir(regulator->supply_name, | 
|  | 1362 | rdev->debugfs); | 
| Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 1363 | if (!regulator->debugfs) { | 
| Stephen Boyd | ad3a942 | 2015-06-01 18:47:55 -0700 | [diff] [blame] | 1364 | rdev_dbg(rdev, "Failed to create debugfs directory\n"); | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1365 | } else { | 
|  | 1366 | debugfs_create_u32("uA_load", 0444, regulator->debugfs, | 
|  | 1367 | ®ulator->uA_load); | 
|  | 1368 | debugfs_create_u32("min_uV", 0444, regulator->debugfs, | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 1369 | ®ulator->voltage[PM_SUSPEND_ON].min_uV); | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1370 | debugfs_create_u32("max_uV", 0444, regulator->debugfs, | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 1371 | ®ulator->voltage[PM_SUSPEND_ON].max_uV); | 
| Richard Fitzgerald | 2d80a91 | 2016-04-21 17:23:21 +0100 | [diff] [blame] | 1372 | debugfs_create_file("constraint_flags", 0444, | 
|  | 1373 | regulator->debugfs, regulator, | 
|  | 1374 | &constraint_flags_fops); | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1375 | } | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1376 |  | 
| Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 1377 | /* | 
|  | 1378 | * Check now if the regulator is an always on regulator - if | 
|  | 1379 | * it is then we don't need to do nearly so much work for | 
|  | 1380 | * enable/disable calls. | 
|  | 1381 | */ | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 1382 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) && | 
| Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 1383 | _regulator_is_enabled(rdev)) | 
|  | 1384 | regulator->always_on = true; | 
|  | 1385 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1386 | mutex_unlock(&rdev->mutex); | 
|  | 1387 | return regulator; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1388 | overflow_err: | 
|  | 1389 | list_del(®ulator->list); | 
|  | 1390 | kfree(regulator); | 
|  | 1391 | mutex_unlock(&rdev->mutex); | 
|  | 1392 | return NULL; | 
|  | 1393 | } | 
|  | 1394 |  | 
| Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1395 | static int _regulator_get_enable_time(struct regulator_dev *rdev) | 
|  | 1396 | { | 
| Laxman Dewangan | 00c877c | 2013-09-18 18:18:02 +0530 | [diff] [blame] | 1397 | if (rdev->constraints && rdev->constraints->enable_time) | 
|  | 1398 | return rdev->constraints->enable_time; | 
| Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1399 | if (!rdev->desc->ops->enable_time) | 
| Mark Brown | 79511ed | 2012-06-27 14:23:10 +0100 | [diff] [blame] | 1400 | return rdev->desc->enable_time; | 
| Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1401 | return rdev->desc->ops->enable_time(rdev); | 
|  | 1402 | } | 
|  | 1403 |  | 
| Charles Keepax | a06ccd9 | 2013-10-15 20:14:20 +0100 | [diff] [blame] | 1404 | static struct regulator_supply_alias *regulator_find_supply_alias( | 
|  | 1405 | struct device *dev, const char *supply) | 
|  | 1406 | { | 
|  | 1407 | struct regulator_supply_alias *map; | 
|  | 1408 |  | 
|  | 1409 | list_for_each_entry(map, ®ulator_supply_alias_list, list) | 
|  | 1410 | if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0) | 
|  | 1411 | return map; | 
|  | 1412 |  | 
|  | 1413 | return NULL; | 
|  | 1414 | } | 
|  | 1415 |  | 
|  | 1416 | static void regulator_supply_alias(struct device **dev, const char **supply) | 
|  | 1417 | { | 
|  | 1418 | struct regulator_supply_alias *map; | 
|  | 1419 |  | 
|  | 1420 | map = regulator_find_supply_alias(*dev, *supply); | 
|  | 1421 | if (map) { | 
|  | 1422 | dev_dbg(*dev, "Mapping supply %s to %s,%s\n", | 
|  | 1423 | *supply, map->alias_supply, | 
|  | 1424 | dev_name(map->alias_dev)); | 
|  | 1425 | *dev = map->alias_dev; | 
|  | 1426 | *supply = map->alias_supply; | 
|  | 1427 | } | 
|  | 1428 | } | 
|  | 1429 |  | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1430 | static int of_node_match(struct device *dev, const void *data) | 
|  | 1431 | { | 
|  | 1432 | return dev->of_node == data; | 
|  | 1433 | } | 
|  | 1434 |  | 
|  | 1435 | static struct regulator_dev *of_find_regulator_by_node(struct device_node *np) | 
|  | 1436 | { | 
|  | 1437 | struct device *dev; | 
|  | 1438 |  | 
|  | 1439 | dev = class_find_device(®ulator_class, NULL, np, of_node_match); | 
|  | 1440 |  | 
|  | 1441 | return dev ? dev_to_rdev(dev) : NULL; | 
|  | 1442 | } | 
|  | 1443 |  | 
|  | 1444 | static int regulator_match(struct device *dev, const void *data) | 
|  | 1445 | { | 
|  | 1446 | struct regulator_dev *r = dev_to_rdev(dev); | 
|  | 1447 |  | 
|  | 1448 | return strcmp(rdev_get_name(r), data) == 0; | 
|  | 1449 | } | 
|  | 1450 |  | 
|  | 1451 | static struct regulator_dev *regulator_lookup_by_name(const char *name) | 
|  | 1452 | { | 
|  | 1453 | struct device *dev; | 
|  | 1454 |  | 
|  | 1455 | dev = class_find_device(®ulator_class, NULL, name, regulator_match); | 
|  | 1456 |  | 
|  | 1457 | return dev ? dev_to_rdev(dev) : NULL; | 
|  | 1458 | } | 
|  | 1459 |  | 
|  | 1460 | /** | 
|  | 1461 | * regulator_dev_lookup - lookup a regulator device. | 
|  | 1462 | * @dev: device for regulator "consumer". | 
|  | 1463 | * @supply: Supply name or regulator ID. | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1464 | * | 
|  | 1465 | * If successful, returns a struct regulator_dev that corresponds to the name | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1466 | * @supply and with the embedded struct device refcount incremented by one. | 
|  | 1467 | * The refcount must be dropped by calling put_device(). | 
|  | 1468 | * On failure one of the following ERR-PTR-encoded values is returned: | 
|  | 1469 | * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed | 
|  | 1470 | * in the future. | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1471 | */ | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1472 | static struct regulator_dev *regulator_dev_lookup(struct device *dev, | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1473 | const char *supply) | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1474 | { | 
| Charles Keepax | 0621719 | 2017-06-13 16:12:47 +0100 | [diff] [blame] | 1475 | struct regulator_dev *r = NULL; | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1476 | struct device_node *node; | 
| Mark Brown | 576ca436 | 2012-03-30 12:24:37 +0100 | [diff] [blame] | 1477 | struct regulator_map *map; | 
|  | 1478 | const char *devname = NULL; | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1479 |  | 
| Charles Keepax | a06ccd9 | 2013-10-15 20:14:20 +0100 | [diff] [blame] | 1480 | regulator_supply_alias(&dev, &supply); | 
|  | 1481 |  | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1482 | /* first do a dt based lookup */ | 
|  | 1483 | if (dev && dev->of_node) { | 
|  | 1484 | node = of_get_regulator(dev, supply); | 
| Mark Brown | 6d191a5 | 2012-03-29 14:19:02 +0100 | [diff] [blame] | 1485 | if (node) { | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1486 | r = of_find_regulator_by_node(node); | 
|  | 1487 | if (r) | 
|  | 1488 | return r; | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1489 |  | 
| Mark Brown | 6d191a5 | 2012-03-29 14:19:02 +0100 | [diff] [blame] | 1490 | /* | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1491 | * We have a node, but there is no device. | 
|  | 1492 | * assume it has not registered yet. | 
| Mark Brown | 6d191a5 | 2012-03-29 14:19:02 +0100 | [diff] [blame] | 1493 | */ | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1494 | return ERR_PTR(-EPROBE_DEFER); | 
| Mark Brown | 6d191a5 | 2012-03-29 14:19:02 +0100 | [diff] [blame] | 1495 | } | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1496 | } | 
|  | 1497 |  | 
|  | 1498 | /* if not found, try doing it non-dt way */ | 
| Mark Brown | 576ca436 | 2012-03-30 12:24:37 +0100 | [diff] [blame] | 1499 | if (dev) | 
|  | 1500 | devname = dev_name(dev); | 
|  | 1501 |  | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1502 | mutex_lock(®ulator_list_mutex); | 
| Mark Brown | 576ca436 | 2012-03-30 12:24:37 +0100 | [diff] [blame] | 1503 | list_for_each_entry(map, ®ulator_map_list, list) { | 
|  | 1504 | /* If the mapping has a device set up it must match */ | 
|  | 1505 | if (map->dev_name && | 
|  | 1506 | (!devname || strcmp(map->dev_name, devname))) | 
|  | 1507 | continue; | 
|  | 1508 |  | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1509 | if (strcmp(map->supply, supply) == 0 && | 
|  | 1510 | get_device(&map->regulator->dev)) { | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1511 | r = map->regulator; | 
|  | 1512 | break; | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1513 | } | 
| Mark Brown | 576ca436 | 2012-03-30 12:24:37 +0100 | [diff] [blame] | 1514 | } | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1515 | mutex_unlock(®ulator_list_mutex); | 
| Mark Brown | 576ca436 | 2012-03-30 12:24:37 +0100 | [diff] [blame] | 1516 |  | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1517 | if (r) | 
|  | 1518 | return r; | 
|  | 1519 |  | 
| Charles Keepax | 0621719 | 2017-06-13 16:12:47 +0100 | [diff] [blame] | 1520 | r = regulator_lookup_by_name(supply); | 
|  | 1521 | if (r) | 
|  | 1522 | return r; | 
|  | 1523 |  | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1524 | return ERR_PTR(-ENODEV); | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1525 | } | 
|  | 1526 |  | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1527 | static int regulator_resolve_supply(struct regulator_dev *rdev) | 
|  | 1528 | { | 
|  | 1529 | struct regulator_dev *r; | 
|  | 1530 | struct device *dev = rdev->dev.parent; | 
|  | 1531 | int ret; | 
|  | 1532 |  | 
|  | 1533 | /* No supply to resovle? */ | 
|  | 1534 | if (!rdev->supply_name) | 
|  | 1535 | return 0; | 
|  | 1536 |  | 
|  | 1537 | /* Supply already resolved? */ | 
|  | 1538 | if (rdev->supply) | 
|  | 1539 | return 0; | 
|  | 1540 |  | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1541 | r = regulator_dev_lookup(dev, rdev->supply_name); | 
|  | 1542 | if (IS_ERR(r)) { | 
|  | 1543 | ret = PTR_ERR(r); | 
|  | 1544 |  | 
| Mark Brown | 0642312 | 2015-10-01 10:59:48 +0100 | [diff] [blame] | 1545 | /* Did the lookup explicitly defer for us? */ | 
|  | 1546 | if (ret == -EPROBE_DEFER) | 
|  | 1547 | return ret; | 
|  | 1548 |  | 
| Mark Brown | 9f7e25e | 2015-07-14 11:17:26 +0100 | [diff] [blame] | 1549 | if (have_full_constraints()) { | 
|  | 1550 | r = dummy_regulator_rdev; | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1551 | get_device(&r->dev); | 
| Mark Brown | 9f7e25e | 2015-07-14 11:17:26 +0100 | [diff] [blame] | 1552 | } else { | 
|  | 1553 | dev_err(dev, "Failed to resolve %s-supply for %s\n", | 
|  | 1554 | rdev->supply_name, rdev->desc->name); | 
|  | 1555 | return -EPROBE_DEFER; | 
|  | 1556 | } | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1557 | } | 
|  | 1558 |  | 
| Jon Hunter | 66d228a | 2017-01-11 17:44:25 +0000 | [diff] [blame] | 1559 | /* | 
|  | 1560 | * If the supply's parent device is not the same as the | 
|  | 1561 | * regulator's parent device, then ensure the parent device | 
|  | 1562 | * is bound before we resolve the supply, in case the parent | 
|  | 1563 | * device get probe deferred and unregisters the supply. | 
|  | 1564 | */ | 
|  | 1565 | if (r->dev.parent && r->dev.parent != rdev->dev.parent) { | 
|  | 1566 | if (!device_is_bound(r->dev.parent)) { | 
|  | 1567 | put_device(&r->dev); | 
|  | 1568 | return -EPROBE_DEFER; | 
|  | 1569 | } | 
|  | 1570 | } | 
|  | 1571 |  | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1572 | /* Recursively resolve the supply of the supply */ | 
|  | 1573 | ret = regulator_resolve_supply(r); | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1574 | if (ret < 0) { | 
|  | 1575 | put_device(&r->dev); | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1576 | return ret; | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1577 | } | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1578 |  | 
|  | 1579 | ret = set_supply(rdev, r); | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1580 | if (ret < 0) { | 
|  | 1581 | put_device(&r->dev); | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1582 | return ret; | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1583 | } | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1584 |  | 
|  | 1585 | /* Cascade always-on state to supply */ | 
| Javier Martinez Canillas | 95a293c | 2016-03-20 23:29:45 -0300 | [diff] [blame] | 1586 | if (_regulator_is_enabled(rdev)) { | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1587 | ret = regulator_enable(rdev->supply); | 
| Javier Martinez Canillas | 36a1f1b | 2015-07-15 16:10:29 +0200 | [diff] [blame] | 1588 | if (ret < 0) { | 
| Sudip Mukherjee | 9f8df6a | 2015-09-02 16:14:06 +0530 | [diff] [blame] | 1589 | _regulator_put(rdev->supply); | 
| Jon Hunter | 8e5356a | 2016-04-21 17:11:58 +0100 | [diff] [blame] | 1590 | rdev->supply = NULL; | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1591 | return ret; | 
| Javier Martinez Canillas | 36a1f1b | 2015-07-15 16:10:29 +0200 | [diff] [blame] | 1592 | } | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1593 | } | 
|  | 1594 |  | 
|  | 1595 | return 0; | 
|  | 1596 | } | 
|  | 1597 |  | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1598 | /* Internal regulator request function */ | 
| Dmitry Torokhov | a8bd42a | 2017-02-03 13:56:02 -0800 | [diff] [blame] | 1599 | struct regulator *_regulator_get(struct device *dev, const char *id, | 
|  | 1600 | enum regulator_get_type get_type) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1601 | { | 
|  | 1602 | struct regulator_dev *rdev; | 
| Dmitry Torokhov | 7d245af | 2017-02-03 13:56:00 -0800 | [diff] [blame] | 1603 | struct regulator *regulator; | 
| Dmitry Torokhov | a4d7641 | 2017-02-06 19:56:14 -0800 | [diff] [blame] | 1604 | const char *devname = dev ? dev_name(dev) : "deviceless"; | 
| Mark Brown | 317b568 | 2014-01-27 17:34:07 +0000 | [diff] [blame] | 1605 | int ret; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1606 |  | 
| Dmitry Torokhov | a8bd42a | 2017-02-03 13:56:02 -0800 | [diff] [blame] | 1607 | if (get_type >= MAX_GET_TYPE) { | 
|  | 1608 | dev_err(dev, "invalid type %d in %s\n", get_type, __func__); | 
|  | 1609 | return ERR_PTR(-EINVAL); | 
|  | 1610 | } | 
|  | 1611 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1612 | if (id == NULL) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1613 | pr_err("get() with no identifier\n"); | 
| Mark Brown | 043c998 | 2013-09-20 12:32:18 +0100 | [diff] [blame] | 1614 | return ERR_PTR(-EINVAL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1615 | } | 
|  | 1616 |  | 
| Dmitry Torokhov | 163478d | 2017-02-04 10:19:21 -0800 | [diff] [blame] | 1617 | rdev = regulator_dev_lookup(dev, id); | 
| Dmitry Torokhov | a4d7641 | 2017-02-06 19:56:14 -0800 | [diff] [blame] | 1618 | if (IS_ERR(rdev)) { | 
|  | 1619 | ret = PTR_ERR(rdev); | 
| Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 1620 |  | 
| Dmitry Torokhov | a4d7641 | 2017-02-06 19:56:14 -0800 | [diff] [blame] | 1621 | /* | 
|  | 1622 | * If regulator_dev_lookup() fails with error other | 
|  | 1623 | * than -ENODEV our job here is done, we simply return it. | 
|  | 1624 | */ | 
|  | 1625 | if (ret != -ENODEV) | 
|  | 1626 | return ERR_PTR(ret); | 
| Mark Brown | 317b568 | 2014-01-27 17:34:07 +0000 | [diff] [blame] | 1627 |  | 
| Dmitry Torokhov | a4d7641 | 2017-02-06 19:56:14 -0800 | [diff] [blame] | 1628 | if (!have_full_constraints()) { | 
|  | 1629 | dev_warn(dev, | 
|  | 1630 | "incomplete constraints, dummy supplies not allowed\n"); | 
|  | 1631 | return ERR_PTR(-ENODEV); | 
|  | 1632 | } | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1633 |  | 
| Dmitry Torokhov | a4d7641 | 2017-02-06 19:56:14 -0800 | [diff] [blame] | 1634 | switch (get_type) { | 
|  | 1635 | case NORMAL_GET: | 
|  | 1636 | /* | 
|  | 1637 | * Assume that a regulator is physically present and | 
|  | 1638 | * enabled, even if it isn't hooked up, and just | 
|  | 1639 | * provide a dummy. | 
|  | 1640 | */ | 
|  | 1641 | dev_warn(dev, | 
|  | 1642 | "%s supply %s not found, using dummy regulator\n", | 
|  | 1643 | devname, id); | 
|  | 1644 | rdev = dummy_regulator_rdev; | 
|  | 1645 | get_device(&rdev->dev); | 
|  | 1646 | break; | 
| Mark Brown | ef60abb | 2013-09-23 16:12:52 +0100 | [diff] [blame] | 1647 |  | 
| Dmitry Torokhov | a4d7641 | 2017-02-06 19:56:14 -0800 | [diff] [blame] | 1648 | case EXCLUSIVE_GET: | 
|  | 1649 | dev_warn(dev, | 
|  | 1650 | "dummy supplies not allowed for exclusive requests\n"); | 
|  | 1651 | /* fall through */ | 
| Nishanth Menon | 1e4b545 | 2013-04-16 16:45:16 -0500 | [diff] [blame] | 1652 |  | 
| Dmitry Torokhov | a4d7641 | 2017-02-06 19:56:14 -0800 | [diff] [blame] | 1653 | default: | 
|  | 1654 | return ERR_PTR(-ENODEV); | 
|  | 1655 | } | 
| Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 1656 | } | 
| Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 1657 |  | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1658 | if (rdev->exclusive) { | 
|  | 1659 | regulator = ERR_PTR(-EPERM); | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1660 | put_device(&rdev->dev); | 
|  | 1661 | return regulator; | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1662 | } | 
|  | 1663 |  | 
| Dmitry Torokhov | a8bd42a | 2017-02-03 13:56:02 -0800 | [diff] [blame] | 1664 | if (get_type == EXCLUSIVE_GET && rdev->open_count) { | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1665 | regulator = ERR_PTR(-EBUSY); | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1666 | put_device(&rdev->dev); | 
|  | 1667 | return regulator; | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1668 | } | 
|  | 1669 |  | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1670 | ret = regulator_resolve_supply(rdev); | 
|  | 1671 | if (ret < 0) { | 
|  | 1672 | regulator = ERR_PTR(ret); | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1673 | put_device(&rdev->dev); | 
|  | 1674 | return regulator; | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 1675 | } | 
|  | 1676 |  | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1677 | if (!try_module_get(rdev->owner)) { | 
| Dmitry Torokhov | 7d245af | 2017-02-03 13:56:00 -0800 | [diff] [blame] | 1678 | regulator = ERR_PTR(-EPROBE_DEFER); | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1679 | put_device(&rdev->dev); | 
|  | 1680 | return regulator; | 
|  | 1681 | } | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1682 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1683 | regulator = create_regulator(rdev, dev, id); | 
|  | 1684 | if (regulator == NULL) { | 
|  | 1685 | regulator = ERR_PTR(-ENOMEM); | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1686 | put_device(&rdev->dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1687 | module_put(rdev->owner); | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1688 | return regulator; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1689 | } | 
|  | 1690 |  | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1691 | rdev->open_count++; | 
| Dmitry Torokhov | a8bd42a | 2017-02-03 13:56:02 -0800 | [diff] [blame] | 1692 | if (get_type == EXCLUSIVE_GET) { | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1693 | rdev->exclusive = 1; | 
|  | 1694 |  | 
|  | 1695 | ret = _regulator_is_enabled(rdev); | 
|  | 1696 | if (ret > 0) | 
|  | 1697 | rdev->use_count = 1; | 
|  | 1698 | else | 
|  | 1699 | rdev->use_count = 0; | 
|  | 1700 | } | 
|  | 1701 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1702 | return regulator; | 
|  | 1703 | } | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1704 |  | 
|  | 1705 | /** | 
|  | 1706 | * regulator_get - lookup and obtain a reference to a regulator. | 
|  | 1707 | * @dev: device for regulator "consumer" | 
|  | 1708 | * @id: Supply name or regulator ID. | 
|  | 1709 | * | 
|  | 1710 | * Returns a struct regulator corresponding to the regulator producer, | 
|  | 1711 | * or IS_ERR() condition containing errno. | 
|  | 1712 | * | 
|  | 1713 | * Use of supply names configured via regulator_set_device_supply() is | 
|  | 1714 | * strongly encouraged.  It is recommended that the supply name used | 
|  | 1715 | * should match the name used for the supply and/or the relevant | 
|  | 1716 | * device pins in the datasheet. | 
|  | 1717 | */ | 
|  | 1718 | struct regulator *regulator_get(struct device *dev, const char *id) | 
|  | 1719 | { | 
| Dmitry Torokhov | a8bd42a | 2017-02-03 13:56:02 -0800 | [diff] [blame] | 1720 | return _regulator_get(dev, id, NORMAL_GET); | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1721 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1722 | EXPORT_SYMBOL_GPL(regulator_get); | 
|  | 1723 |  | 
| Stephen Boyd | 070b907 | 2012-01-16 19:39:58 -0800 | [diff] [blame] | 1724 | /** | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1725 | * regulator_get_exclusive - obtain exclusive access to a regulator. | 
|  | 1726 | * @dev: device for regulator "consumer" | 
|  | 1727 | * @id: Supply name or regulator ID. | 
|  | 1728 | * | 
|  | 1729 | * Returns a struct regulator corresponding to the regulator producer, | 
|  | 1730 | * or IS_ERR() condition containing errno.  Other consumers will be | 
| Stephen Boyd | 69c3f72 | 2014-05-28 12:41:28 -0700 | [diff] [blame] | 1731 | * unable to obtain this regulator while this reference is held and the | 
|  | 1732 | * use count for the regulator will be initialised to reflect the current | 
|  | 1733 | * state of the regulator. | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1734 | * | 
|  | 1735 | * This is intended for use by consumers which cannot tolerate shared | 
|  | 1736 | * use of the regulator such as those which need to force the | 
|  | 1737 | * regulator off for correct operation of the hardware they are | 
|  | 1738 | * controlling. | 
|  | 1739 | * | 
|  | 1740 | * Use of supply names configured via regulator_set_device_supply() is | 
|  | 1741 | * strongly encouraged.  It is recommended that the supply name used | 
|  | 1742 | * should match the name used for the supply and/or the relevant | 
|  | 1743 | * device pins in the datasheet. | 
|  | 1744 | */ | 
|  | 1745 | struct regulator *regulator_get_exclusive(struct device *dev, const char *id) | 
|  | 1746 | { | 
| Dmitry Torokhov | a8bd42a | 2017-02-03 13:56:02 -0800 | [diff] [blame] | 1747 | return _regulator_get(dev, id, EXCLUSIVE_GET); | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1748 | } | 
|  | 1749 | EXPORT_SYMBOL_GPL(regulator_get_exclusive); | 
|  | 1750 |  | 
| Mark Brown | de1dd9f | 2013-07-29 21:42:42 +0100 | [diff] [blame] | 1751 | /** | 
|  | 1752 | * regulator_get_optional - obtain optional access to a regulator. | 
|  | 1753 | * @dev: device for regulator "consumer" | 
|  | 1754 | * @id: Supply name or regulator ID. | 
|  | 1755 | * | 
|  | 1756 | * Returns a struct regulator corresponding to the regulator producer, | 
| Stephen Boyd | 69c3f72 | 2014-05-28 12:41:28 -0700 | [diff] [blame] | 1757 | * or IS_ERR() condition containing errno. | 
| Mark Brown | de1dd9f | 2013-07-29 21:42:42 +0100 | [diff] [blame] | 1758 | * | 
|  | 1759 | * This is intended for use by consumers for devices which can have | 
|  | 1760 | * some supplies unconnected in normal use, such as some MMC devices. | 
|  | 1761 | * It can allow the regulator core to provide stub supplies for other | 
|  | 1762 | * supplies requested using normal regulator_get() calls without | 
|  | 1763 | * disrupting the operation of drivers that can handle absent | 
|  | 1764 | * supplies. | 
|  | 1765 | * | 
|  | 1766 | * Use of supply names configured via regulator_set_device_supply() is | 
|  | 1767 | * strongly encouraged.  It is recommended that the supply name used | 
|  | 1768 | * should match the name used for the supply and/or the relevant | 
|  | 1769 | * device pins in the datasheet. | 
|  | 1770 | */ | 
|  | 1771 | struct regulator *regulator_get_optional(struct device *dev, const char *id) | 
|  | 1772 | { | 
| Dmitry Torokhov | a8bd42a | 2017-02-03 13:56:02 -0800 | [diff] [blame] | 1773 | return _regulator_get(dev, id, OPTIONAL_GET); | 
| Mark Brown | de1dd9f | 2013-07-29 21:42:42 +0100 | [diff] [blame] | 1774 | } | 
|  | 1775 | EXPORT_SYMBOL_GPL(regulator_get_optional); | 
|  | 1776 |  | 
| Ashay Jaiswal | 83b0302 | 2015-01-08 18:54:25 +0530 | [diff] [blame] | 1777 | /* regulator_list_mutex lock held by regulator_put() */ | 
| Charles Keepax | 23ff2f0 | 2012-11-14 09:39:31 +0000 | [diff] [blame] | 1778 | static void _regulator_put(struct regulator *regulator) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1779 | { | 
|  | 1780 | struct regulator_dev *rdev; | 
|  | 1781 |  | 
| Viresh Kumar | 9357684 | 2015-08-17 12:30:58 +0530 | [diff] [blame] | 1782 | if (IS_ERR_OR_NULL(regulator)) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1783 | return; | 
|  | 1784 |  | 
| Krzysztof Kozlowski | 70cfef2 | 2015-06-29 09:04:43 +0900 | [diff] [blame] | 1785 | lockdep_assert_held_once(®ulator_list_mutex); | 
|  | 1786 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1787 | rdev = regulator->rdev; | 
|  | 1788 |  | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1789 | debugfs_remove_recursive(regulator->debugfs); | 
| Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1790 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1791 | /* remove any sysfs entries */ | 
| Shawn Guo | e2c98ea | 2012-07-05 14:19:42 +0800 | [diff] [blame] | 1792 | if (regulator->dev) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1793 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); | 
| Ashay Jaiswal | 83b0302 | 2015-01-08 18:54:25 +0530 | [diff] [blame] | 1794 | mutex_lock(&rdev->mutex); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1795 | list_del(®ulator->list); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1796 |  | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1797 | rdev->open_count--; | 
|  | 1798 | rdev->exclusive = 0; | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 1799 | put_device(&rdev->dev); | 
| Ashay Jaiswal | 83b0302 | 2015-01-08 18:54:25 +0530 | [diff] [blame] | 1800 | mutex_unlock(&rdev->mutex); | 
| Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1801 |  | 
| Stephen Boyd | 0630b61 | 2017-03-16 18:07:14 -0700 | [diff] [blame] | 1802 | kfree_const(regulator->supply_name); | 
| Mark Brown | 1768514 | 2015-08-07 21:19:26 +0100 | [diff] [blame] | 1803 | kfree(regulator); | 
|  | 1804 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1805 | module_put(rdev->owner); | 
| Charles Keepax | 23ff2f0 | 2012-11-14 09:39:31 +0000 | [diff] [blame] | 1806 | } | 
|  | 1807 |  | 
|  | 1808 | /** | 
|  | 1809 | * regulator_put - "free" the regulator source | 
|  | 1810 | * @regulator: regulator source | 
|  | 1811 | * | 
|  | 1812 | * Note: drivers must ensure that all regulator_enable calls made on this | 
|  | 1813 | * regulator source are balanced by regulator_disable calls prior to calling | 
|  | 1814 | * this function. | 
|  | 1815 | */ | 
|  | 1816 | void regulator_put(struct regulator *regulator) | 
|  | 1817 | { | 
|  | 1818 | mutex_lock(®ulator_list_mutex); | 
|  | 1819 | _regulator_put(regulator); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1820 | mutex_unlock(®ulator_list_mutex); | 
|  | 1821 | } | 
|  | 1822 | EXPORT_SYMBOL_GPL(regulator_put); | 
|  | 1823 |  | 
| Charles Keepax | a06ccd9 | 2013-10-15 20:14:20 +0100 | [diff] [blame] | 1824 | /** | 
|  | 1825 | * regulator_register_supply_alias - Provide device alias for supply lookup | 
|  | 1826 | * | 
|  | 1827 | * @dev: device that will be given as the regulator "consumer" | 
|  | 1828 | * @id: Supply name or regulator ID | 
|  | 1829 | * @alias_dev: device that should be used to lookup the supply | 
|  | 1830 | * @alias_id: Supply name or regulator ID that should be used to lookup the | 
|  | 1831 | * supply | 
|  | 1832 | * | 
|  | 1833 | * All lookups for id on dev will instead be conducted for alias_id on | 
|  | 1834 | * alias_dev. | 
|  | 1835 | */ | 
|  | 1836 | int regulator_register_supply_alias(struct device *dev, const char *id, | 
|  | 1837 | struct device *alias_dev, | 
|  | 1838 | const char *alias_id) | 
|  | 1839 | { | 
|  | 1840 | struct regulator_supply_alias *map; | 
|  | 1841 |  | 
|  | 1842 | map = regulator_find_supply_alias(dev, id); | 
|  | 1843 | if (map) | 
|  | 1844 | return -EEXIST; | 
|  | 1845 |  | 
|  | 1846 | map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL); | 
|  | 1847 | if (!map) | 
|  | 1848 | return -ENOMEM; | 
|  | 1849 |  | 
|  | 1850 | map->src_dev = dev; | 
|  | 1851 | map->src_supply = id; | 
|  | 1852 | map->alias_dev = alias_dev; | 
|  | 1853 | map->alias_supply = alias_id; | 
|  | 1854 |  | 
|  | 1855 | list_add(&map->list, ®ulator_supply_alias_list); | 
|  | 1856 |  | 
|  | 1857 | pr_info("Adding alias for supply %s,%s -> %s,%s\n", | 
|  | 1858 | id, dev_name(dev), alias_id, dev_name(alias_dev)); | 
|  | 1859 |  | 
|  | 1860 | return 0; | 
|  | 1861 | } | 
|  | 1862 | EXPORT_SYMBOL_GPL(regulator_register_supply_alias); | 
|  | 1863 |  | 
|  | 1864 | /** | 
|  | 1865 | * regulator_unregister_supply_alias - Remove device alias | 
|  | 1866 | * | 
|  | 1867 | * @dev: device that will be given as the regulator "consumer" | 
|  | 1868 | * @id: Supply name or regulator ID | 
|  | 1869 | * | 
|  | 1870 | * Remove a lookup alias if one exists for id on dev. | 
|  | 1871 | */ | 
|  | 1872 | void regulator_unregister_supply_alias(struct device *dev, const char *id) | 
|  | 1873 | { | 
|  | 1874 | struct regulator_supply_alias *map; | 
|  | 1875 |  | 
|  | 1876 | map = regulator_find_supply_alias(dev, id); | 
|  | 1877 | if (map) { | 
|  | 1878 | list_del(&map->list); | 
|  | 1879 | kfree(map); | 
|  | 1880 | } | 
|  | 1881 | } | 
|  | 1882 | EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias); | 
|  | 1883 |  | 
|  | 1884 | /** | 
|  | 1885 | * regulator_bulk_register_supply_alias - register multiple aliases | 
|  | 1886 | * | 
|  | 1887 | * @dev: device that will be given as the regulator "consumer" | 
|  | 1888 | * @id: List of supply names or regulator IDs | 
|  | 1889 | * @alias_dev: device that should be used to lookup the supply | 
|  | 1890 | * @alias_id: List of supply names or regulator IDs that should be used to | 
|  | 1891 | * lookup the supply | 
|  | 1892 | * @num_id: Number of aliases to register | 
|  | 1893 | * | 
|  | 1894 | * @return 0 on success, an errno on failure. | 
|  | 1895 | * | 
|  | 1896 | * This helper function allows drivers to register several supply | 
|  | 1897 | * aliases in one operation.  If any of the aliases cannot be | 
|  | 1898 | * registered any aliases that were registered will be removed | 
|  | 1899 | * before returning to the caller. | 
|  | 1900 | */ | 
| Lee Jones | 9f8c0fe | 2014-05-23 16:44:10 +0100 | [diff] [blame] | 1901 | int regulator_bulk_register_supply_alias(struct device *dev, | 
|  | 1902 | const char *const *id, | 
| Charles Keepax | a06ccd9 | 2013-10-15 20:14:20 +0100 | [diff] [blame] | 1903 | struct device *alias_dev, | 
| Lee Jones | 9f8c0fe | 2014-05-23 16:44:10 +0100 | [diff] [blame] | 1904 | const char *const *alias_id, | 
| Charles Keepax | a06ccd9 | 2013-10-15 20:14:20 +0100 | [diff] [blame] | 1905 | int num_id) | 
|  | 1906 | { | 
|  | 1907 | int i; | 
|  | 1908 | int ret; | 
|  | 1909 |  | 
|  | 1910 | for (i = 0; i < num_id; ++i) { | 
|  | 1911 | ret = regulator_register_supply_alias(dev, id[i], alias_dev, | 
|  | 1912 | alias_id[i]); | 
|  | 1913 | if (ret < 0) | 
|  | 1914 | goto err; | 
|  | 1915 | } | 
|  | 1916 |  | 
|  | 1917 | return 0; | 
|  | 1918 |  | 
|  | 1919 | err: | 
|  | 1920 | dev_err(dev, | 
|  | 1921 | "Failed to create supply alias %s,%s -> %s,%s\n", | 
|  | 1922 | id[i], dev_name(dev), alias_id[i], dev_name(alias_dev)); | 
|  | 1923 |  | 
|  | 1924 | while (--i >= 0) | 
|  | 1925 | regulator_unregister_supply_alias(dev, id[i]); | 
|  | 1926 |  | 
|  | 1927 | return ret; | 
|  | 1928 | } | 
|  | 1929 | EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias); | 
|  | 1930 |  | 
|  | 1931 | /** | 
|  | 1932 | * regulator_bulk_unregister_supply_alias - unregister multiple aliases | 
|  | 1933 | * | 
|  | 1934 | * @dev: device that will be given as the regulator "consumer" | 
|  | 1935 | * @id: List of supply names or regulator IDs | 
|  | 1936 | * @num_id: Number of aliases to unregister | 
|  | 1937 | * | 
|  | 1938 | * This helper function allows drivers to unregister several supply | 
|  | 1939 | * aliases in one operation. | 
|  | 1940 | */ | 
|  | 1941 | void regulator_bulk_unregister_supply_alias(struct device *dev, | 
| Lee Jones | 9f8c0fe | 2014-05-23 16:44:10 +0100 | [diff] [blame] | 1942 | const char *const *id, | 
| Charles Keepax | a06ccd9 | 2013-10-15 20:14:20 +0100 | [diff] [blame] | 1943 | int num_id) | 
|  | 1944 | { | 
|  | 1945 | int i; | 
|  | 1946 |  | 
|  | 1947 | for (i = 0; i < num_id; ++i) | 
|  | 1948 | regulator_unregister_supply_alias(dev, id[i]); | 
|  | 1949 | } | 
|  | 1950 | EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias); | 
|  | 1951 |  | 
|  | 1952 |  | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 1953 | /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */ | 
|  | 1954 | static int regulator_ena_gpio_request(struct regulator_dev *rdev, | 
|  | 1955 | const struct regulator_config *config) | 
|  | 1956 | { | 
|  | 1957 | struct regulator_enable_gpio *pin; | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 1958 | struct gpio_desc *gpiod; | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 1959 | int ret; | 
|  | 1960 |  | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 1961 | gpiod = gpio_to_desc(config->ena_gpio); | 
|  | 1962 |  | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 1963 | list_for_each_entry(pin, ®ulator_ena_gpio_list, list) { | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 1964 | if (pin->gpiod == gpiod) { | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 1965 | rdev_dbg(rdev, "GPIO %d is already used\n", | 
|  | 1966 | config->ena_gpio); | 
|  | 1967 | goto update_ena_gpio_to_rdev; | 
|  | 1968 | } | 
|  | 1969 | } | 
|  | 1970 |  | 
|  | 1971 | ret = gpio_request_one(config->ena_gpio, | 
|  | 1972 | GPIOF_DIR_OUT | config->ena_gpio_flags, | 
|  | 1973 | rdev_get_name(rdev)); | 
|  | 1974 | if (ret) | 
|  | 1975 | return ret; | 
|  | 1976 |  | 
|  | 1977 | pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL); | 
|  | 1978 | if (pin == NULL) { | 
|  | 1979 | gpio_free(config->ena_gpio); | 
|  | 1980 | return -ENOMEM; | 
|  | 1981 | } | 
|  | 1982 |  | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 1983 | pin->gpiod = gpiod; | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 1984 | pin->ena_gpio_invert = config->ena_gpio_invert; | 
|  | 1985 | list_add(&pin->list, ®ulator_ena_gpio_list); | 
|  | 1986 |  | 
|  | 1987 | update_ena_gpio_to_rdev: | 
|  | 1988 | pin->request_count++; | 
|  | 1989 | rdev->ena_pin = pin; | 
|  | 1990 | return 0; | 
|  | 1991 | } | 
|  | 1992 |  | 
|  | 1993 | static void regulator_ena_gpio_free(struct regulator_dev *rdev) | 
|  | 1994 | { | 
|  | 1995 | struct regulator_enable_gpio *pin, *n; | 
|  | 1996 |  | 
|  | 1997 | if (!rdev->ena_pin) | 
|  | 1998 | return; | 
|  | 1999 |  | 
|  | 2000 | /* Free the GPIO only in case of no use */ | 
|  | 2001 | list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) { | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 2002 | if (pin->gpiod == rdev->ena_pin->gpiod) { | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 2003 | if (pin->request_count <= 1) { | 
|  | 2004 | pin->request_count = 0; | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 2005 | gpiod_put(pin->gpiod); | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 2006 | list_del(&pin->list); | 
|  | 2007 | kfree(pin); | 
| Seung-Woo Kim | 60a2362 | 2014-12-04 19:17:17 +0900 | [diff] [blame] | 2008 | rdev->ena_pin = NULL; | 
|  | 2009 | return; | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 2010 | } else { | 
|  | 2011 | pin->request_count--; | 
|  | 2012 | } | 
|  | 2013 | } | 
|  | 2014 | } | 
|  | 2015 | } | 
|  | 2016 |  | 
| Kim, Milo | 967cfb1 | 2013-02-18 06:50:48 +0000 | [diff] [blame] | 2017 | /** | 
| Robert P. J. Day | 31d6eeb | 2013-05-02 10:19:11 -0400 | [diff] [blame] | 2018 | * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control | 
|  | 2019 | * @rdev: regulator_dev structure | 
|  | 2020 | * @enable: enable GPIO at initial use? | 
|  | 2021 | * | 
| Kim, Milo | 967cfb1 | 2013-02-18 06:50:48 +0000 | [diff] [blame] | 2022 | * GPIO is enabled in case of initial use. (enable_count is 0) | 
|  | 2023 | * GPIO is disabled when it is not shared any more. (enable_count <= 1) | 
|  | 2024 | */ | 
|  | 2025 | static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable) | 
|  | 2026 | { | 
|  | 2027 | struct regulator_enable_gpio *pin = rdev->ena_pin; | 
|  | 2028 |  | 
|  | 2029 | if (!pin) | 
|  | 2030 | return -EINVAL; | 
|  | 2031 |  | 
|  | 2032 | if (enable) { | 
|  | 2033 | /* Enable GPIO at initial use */ | 
|  | 2034 | if (pin->enable_count == 0) | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 2035 | gpiod_set_value_cansleep(pin->gpiod, | 
|  | 2036 | !pin->ena_gpio_invert); | 
| Kim, Milo | 967cfb1 | 2013-02-18 06:50:48 +0000 | [diff] [blame] | 2037 |  | 
|  | 2038 | pin->enable_count++; | 
|  | 2039 | } else { | 
|  | 2040 | if (pin->enable_count > 1) { | 
|  | 2041 | pin->enable_count--; | 
|  | 2042 | return 0; | 
|  | 2043 | } | 
|  | 2044 |  | 
|  | 2045 | /* Disable GPIO if not used */ | 
|  | 2046 | if (pin->enable_count <= 1) { | 
| Russell King | 778b28b | 2014-06-29 17:55:54 +0100 | [diff] [blame] | 2047 | gpiod_set_value_cansleep(pin->gpiod, | 
|  | 2048 | pin->ena_gpio_invert); | 
| Kim, Milo | 967cfb1 | 2013-02-18 06:50:48 +0000 | [diff] [blame] | 2049 | pin->enable_count = 0; | 
|  | 2050 | } | 
|  | 2051 | } | 
|  | 2052 |  | 
|  | 2053 | return 0; | 
|  | 2054 | } | 
|  | 2055 |  | 
| Guodong Xu | 79fd114 | 2014-08-13 19:33:39 +0800 | [diff] [blame] | 2056 | /** | 
|  | 2057 | * _regulator_enable_delay - a delay helper function | 
|  | 2058 | * @delay: time to delay in microseconds | 
|  | 2059 | * | 
|  | 2060 | * Delay for the requested amount of time as per the guidelines in: | 
|  | 2061 | * | 
|  | 2062 | *     Documentation/timers/timers-howto.txt | 
|  | 2063 | * | 
|  | 2064 | * The assumption here is that regulators will never be enabled in | 
|  | 2065 | * atomic context and therefore sleeping functions can be used. | 
|  | 2066 | */ | 
|  | 2067 | static void _regulator_enable_delay(unsigned int delay) | 
|  | 2068 | { | 
|  | 2069 | unsigned int ms = delay / 1000; | 
|  | 2070 | unsigned int us = delay % 1000; | 
|  | 2071 |  | 
|  | 2072 | if (ms > 0) { | 
|  | 2073 | /* | 
|  | 2074 | * For small enough values, handle super-millisecond | 
|  | 2075 | * delays in the usleep_range() call below. | 
|  | 2076 | */ | 
|  | 2077 | if (ms < 20) | 
|  | 2078 | us += ms * 1000; | 
|  | 2079 | else | 
|  | 2080 | msleep(ms); | 
|  | 2081 | } | 
|  | 2082 |  | 
|  | 2083 | /* | 
|  | 2084 | * Give the scheduler some room to coalesce with any other | 
|  | 2085 | * wakeup sources. For delays shorter than 10 us, don't even | 
|  | 2086 | * bother setting up high-resolution timers and just busy- | 
|  | 2087 | * loop. | 
|  | 2088 | */ | 
|  | 2089 | if (us >= 10) | 
|  | 2090 | usleep_range(us, us + 100); | 
|  | 2091 | else | 
|  | 2092 | udelay(us); | 
|  | 2093 | } | 
|  | 2094 |  | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2095 | static int _regulator_do_enable(struct regulator_dev *rdev) | 
|  | 2096 | { | 
|  | 2097 | int ret, delay; | 
|  | 2098 |  | 
|  | 2099 | /* Query before enabling in case configuration dependent.  */ | 
|  | 2100 | ret = _regulator_get_enable_time(rdev); | 
|  | 2101 | if (ret >= 0) { | 
|  | 2102 | delay = ret; | 
|  | 2103 | } else { | 
|  | 2104 | rdev_warn(rdev, "enable_time() failed: %d\n", ret); | 
|  | 2105 | delay = 0; | 
|  | 2106 | } | 
|  | 2107 |  | 
|  | 2108 | trace_regulator_enable(rdev_get_name(rdev)); | 
|  | 2109 |  | 
| Guodong Xu | 871f565 | 2014-08-13 19:33:40 +0800 | [diff] [blame] | 2110 | if (rdev->desc->off_on_delay) { | 
|  | 2111 | /* if needed, keep a distance of off_on_delay from last time | 
|  | 2112 | * this regulator was disabled. | 
|  | 2113 | */ | 
|  | 2114 | unsigned long start_jiffy = jiffies; | 
|  | 2115 | unsigned long intended, max_delay, remaining; | 
|  | 2116 |  | 
|  | 2117 | max_delay = usecs_to_jiffies(rdev->desc->off_on_delay); | 
|  | 2118 | intended = rdev->last_off_jiffy + max_delay; | 
|  | 2119 |  | 
|  | 2120 | if (time_before(start_jiffy, intended)) { | 
|  | 2121 | /* calc remaining jiffies to deal with one-time | 
|  | 2122 | * timer wrapping. | 
|  | 2123 | * in case of multiple timer wrapping, either it can be | 
|  | 2124 | * detected by out-of-range remaining, or it cannot be | 
|  | 2125 | * detected and we gets a panelty of | 
|  | 2126 | * _regulator_enable_delay(). | 
|  | 2127 | */ | 
|  | 2128 | remaining = intended - start_jiffy; | 
|  | 2129 | if (remaining <= max_delay) | 
|  | 2130 | _regulator_enable_delay( | 
|  | 2131 | jiffies_to_usecs(remaining)); | 
|  | 2132 | } | 
|  | 2133 | } | 
|  | 2134 |  | 
| Kim, Milo | 967cfb1 | 2013-02-18 06:50:48 +0000 | [diff] [blame] | 2135 | if (rdev->ena_pin) { | 
| Doug Anderson | 29d62ec | 2015-03-03 15:20:47 -0800 | [diff] [blame] | 2136 | if (!rdev->ena_gpio_state) { | 
|  | 2137 | ret = regulator_ena_gpio_ctrl(rdev, true); | 
|  | 2138 | if (ret < 0) | 
|  | 2139 | return ret; | 
|  | 2140 | rdev->ena_gpio_state = 1; | 
|  | 2141 | } | 
| Mark Brown | 65f7350 | 2012-06-27 14:14:38 +0100 | [diff] [blame] | 2142 | } else if (rdev->desc->ops->enable) { | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2143 | ret = rdev->desc->ops->enable(rdev); | 
|  | 2144 | if (ret < 0) | 
|  | 2145 | return ret; | 
|  | 2146 | } else { | 
|  | 2147 | return -EINVAL; | 
|  | 2148 | } | 
|  | 2149 |  | 
|  | 2150 | /* Allow the regulator to ramp; it would be useful to extend | 
|  | 2151 | * this for bulk operations so that the regulators can ramp | 
|  | 2152 | * together.  */ | 
|  | 2153 | trace_regulator_enable_delay(rdev_get_name(rdev)); | 
|  | 2154 |  | 
| Guodong Xu | 79fd114 | 2014-08-13 19:33:39 +0800 | [diff] [blame] | 2155 | _regulator_enable_delay(delay); | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2156 |  | 
|  | 2157 | trace_regulator_enable_complete(rdev_get_name(rdev)); | 
|  | 2158 |  | 
|  | 2159 | return 0; | 
|  | 2160 | } | 
|  | 2161 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2162 | /* locks held by regulator_enable() */ | 
|  | 2163 | static int _regulator_enable(struct regulator_dev *rdev) | 
|  | 2164 | { | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2165 | int ret; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2166 |  | 
| Krzysztof Kozlowski | 70cfef2 | 2015-06-29 09:04:43 +0900 | [diff] [blame] | 2167 | lockdep_assert_held_once(&rdev->mutex); | 
|  | 2168 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2169 | /* check voltage and requested load before enabling */ | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 2170 | if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) | 
| Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 2171 | drms_uA_update(rdev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2172 |  | 
| Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 2173 | if (rdev->use_count == 0) { | 
|  | 2174 | /* The regulator may on if it's not switchable or left on */ | 
|  | 2175 | ret = _regulator_is_enabled(rdev); | 
|  | 2176 | if (ret == -EINVAL || ret == 0) { | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 2177 | if (!regulator_ops_is_valid(rdev, | 
|  | 2178 | REGULATOR_CHANGE_STATUS)) | 
| Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 2179 | return -EPERM; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2180 |  | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2181 | ret = _regulator_do_enable(rdev); | 
| Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 2182 | if (ret < 0) | 
|  | 2183 | return ret; | 
|  | 2184 |  | 
| Harald Geyer | 264b88c | 2017-02-23 17:06:52 +0000 | [diff] [blame] | 2185 | _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE, | 
|  | 2186 | NULL); | 
| Linus Walleij | a7433cf | 2009-08-26 12:54:04 +0200 | [diff] [blame] | 2187 | } else if (ret < 0) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2188 | rdev_err(rdev, "is_enabled() failed: %d\n", ret); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2189 | return ret; | 
|  | 2190 | } | 
| Linus Walleij | a7433cf | 2009-08-26 12:54:04 +0200 | [diff] [blame] | 2191 | /* Fallthrough on positive return values - already enabled */ | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2192 | } | 
|  | 2193 |  | 
| Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 2194 | rdev->use_count++; | 
|  | 2195 |  | 
|  | 2196 | return 0; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2197 | } | 
|  | 2198 |  | 
|  | 2199 | /** | 
|  | 2200 | * regulator_enable - enable regulator output | 
|  | 2201 | * @regulator: regulator source | 
|  | 2202 | * | 
| Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 2203 | * Request that the regulator be enabled with the regulator output at | 
|  | 2204 | * the predefined voltage or current value.  Calls to regulator_enable() | 
|  | 2205 | * must be balanced with calls to regulator_disable(). | 
|  | 2206 | * | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2207 | * NOTE: the output value can be set by other drivers, boot loader or may be | 
| Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 2208 | * hardwired in the regulator. | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2209 | */ | 
|  | 2210 | int regulator_enable(struct regulator *regulator) | 
|  | 2211 | { | 
| David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 2212 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 2213 | int ret = 0; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2214 |  | 
| Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 2215 | if (regulator->always_on) | 
|  | 2216 | return 0; | 
|  | 2217 |  | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2218 | if (rdev->supply) { | 
|  | 2219 | ret = regulator_enable(rdev->supply); | 
|  | 2220 | if (ret != 0) | 
|  | 2221 | return ret; | 
|  | 2222 | } | 
|  | 2223 |  | 
| David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 2224 | mutex_lock(&rdev->mutex); | 
| David Brownell | cd94b50 | 2009-03-11 16:43:34 -0800 | [diff] [blame] | 2225 | ret = _regulator_enable(rdev); | 
| David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 2226 | mutex_unlock(&rdev->mutex); | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2227 |  | 
| Heiko Stübner | d1685e4 | 2011-10-14 18:00:29 +0200 | [diff] [blame] | 2228 | if (ret != 0 && rdev->supply) | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2229 | regulator_disable(rdev->supply); | 
|  | 2230 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2231 | return ret; | 
|  | 2232 | } | 
|  | 2233 | EXPORT_SYMBOL_GPL(regulator_enable); | 
|  | 2234 |  | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2235 | static int _regulator_do_disable(struct regulator_dev *rdev) | 
|  | 2236 | { | 
|  | 2237 | int ret; | 
|  | 2238 |  | 
|  | 2239 | trace_regulator_disable(rdev_get_name(rdev)); | 
|  | 2240 |  | 
| Kim, Milo | 967cfb1 | 2013-02-18 06:50:48 +0000 | [diff] [blame] | 2241 | if (rdev->ena_pin) { | 
| Doug Anderson | 29d62ec | 2015-03-03 15:20:47 -0800 | [diff] [blame] | 2242 | if (rdev->ena_gpio_state) { | 
|  | 2243 | ret = regulator_ena_gpio_ctrl(rdev, false); | 
|  | 2244 | if (ret < 0) | 
|  | 2245 | return ret; | 
|  | 2246 | rdev->ena_gpio_state = 0; | 
|  | 2247 | } | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2248 |  | 
|  | 2249 | } else if (rdev->desc->ops->disable) { | 
|  | 2250 | ret = rdev->desc->ops->disable(rdev); | 
|  | 2251 | if (ret != 0) | 
|  | 2252 | return ret; | 
|  | 2253 | } | 
|  | 2254 |  | 
| Guodong Xu | 871f565 | 2014-08-13 19:33:40 +0800 | [diff] [blame] | 2255 | /* cares about last_off_jiffy only if off_on_delay is required by | 
|  | 2256 | * device. | 
|  | 2257 | */ | 
|  | 2258 | if (rdev->desc->off_on_delay) | 
|  | 2259 | rdev->last_off_jiffy = jiffies; | 
|  | 2260 |  | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2261 | trace_regulator_disable_complete(rdev_get_name(rdev)); | 
|  | 2262 |  | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2263 | return 0; | 
|  | 2264 | } | 
|  | 2265 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2266 | /* locks held by regulator_disable() */ | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2267 | static int _regulator_disable(struct regulator_dev *rdev) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2268 | { | 
|  | 2269 | int ret = 0; | 
|  | 2270 |  | 
| Krzysztof Kozlowski | 70cfef2 | 2015-06-29 09:04:43 +0900 | [diff] [blame] | 2271 | lockdep_assert_held_once(&rdev->mutex); | 
|  | 2272 |  | 
| David Brownell | cd94b50 | 2009-03-11 16:43:34 -0800 | [diff] [blame] | 2273 | if (WARN(rdev->use_count <= 0, | 
| Joe Perches | 43e7ee3 | 2010-12-06 14:05:19 -0800 | [diff] [blame] | 2274 | "unbalanced disables for %s\n", rdev_get_name(rdev))) | 
| David Brownell | cd94b50 | 2009-03-11 16:43:34 -0800 | [diff] [blame] | 2275 | return -EIO; | 
|  | 2276 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2277 | /* are we the last user and permitted to disable ? */ | 
| Mark Brown | 60ef66f | 2009-10-13 13:06:50 +0100 | [diff] [blame] | 2278 | if (rdev->use_count == 1 && | 
|  | 2279 | (rdev->constraints && !rdev->constraints->always_on)) { | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2280 |  | 
|  | 2281 | /* we are last user */ | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 2282 | if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) { | 
| Richard Fitzgerald | a1c8a55 | 2014-11-24 14:10:52 +0000 | [diff] [blame] | 2283 | ret = _notifier_call_chain(rdev, | 
|  | 2284 | REGULATOR_EVENT_PRE_DISABLE, | 
|  | 2285 | NULL); | 
|  | 2286 | if (ret & NOTIFY_STOP_MASK) | 
|  | 2287 | return -EINVAL; | 
|  | 2288 |  | 
| Mark Brown | 5c5659d | 2012-06-27 13:21:26 +0100 | [diff] [blame] | 2289 | ret = _regulator_do_disable(rdev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2290 | if (ret < 0) { | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2291 | rdev_err(rdev, "failed to disable\n"); | 
| Richard Fitzgerald | a1c8a55 | 2014-11-24 14:10:52 +0000 | [diff] [blame] | 2292 | _notifier_call_chain(rdev, | 
|  | 2293 | REGULATOR_EVENT_ABORT_DISABLE, | 
|  | 2294 | NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2295 | return ret; | 
|  | 2296 | } | 
| Markus Pargmann | 66fda75 | 2014-02-20 17:36:04 +0100 | [diff] [blame] | 2297 | _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, | 
|  | 2298 | NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2299 | } | 
|  | 2300 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2301 | rdev->use_count = 0; | 
|  | 2302 | } else if (rdev->use_count > 1) { | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 2303 | if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2304 | drms_uA_update(rdev); | 
|  | 2305 |  | 
|  | 2306 | rdev->use_count--; | 
|  | 2307 | } | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2308 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2309 | return ret; | 
|  | 2310 | } | 
|  | 2311 |  | 
|  | 2312 | /** | 
|  | 2313 | * regulator_disable - disable regulator output | 
|  | 2314 | * @regulator: regulator source | 
|  | 2315 | * | 
| Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 2316 | * Disable the regulator output voltage or current.  Calls to | 
|  | 2317 | * regulator_enable() must be balanced with calls to | 
|  | 2318 | * regulator_disable(). | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2319 | * | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2320 | * NOTE: this will only disable the regulator output if no other consumer | 
| Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 2321 | * devices have it enabled, the regulator device supports disabling and | 
|  | 2322 | * machine constraints permit this operation. | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2323 | */ | 
|  | 2324 | int regulator_disable(struct regulator *regulator) | 
|  | 2325 | { | 
| David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 2326 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 2327 | int ret = 0; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2328 |  | 
| Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 2329 | if (regulator->always_on) | 
|  | 2330 | return 0; | 
|  | 2331 |  | 
| David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 2332 | mutex_lock(&rdev->mutex); | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2333 | ret = _regulator_disable(rdev); | 
| David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 2334 | mutex_unlock(&rdev->mutex); | 
| Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 2335 |  | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2336 | if (ret == 0 && rdev->supply) | 
|  | 2337 | regulator_disable(rdev->supply); | 
| Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 2338 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2339 | return ret; | 
|  | 2340 | } | 
|  | 2341 | EXPORT_SYMBOL_GPL(regulator_disable); | 
|  | 2342 |  | 
|  | 2343 | /* locks held by regulator_force_disable() */ | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2344 | static int _regulator_force_disable(struct regulator_dev *rdev) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2345 | { | 
|  | 2346 | int ret = 0; | 
|  | 2347 |  | 
| Krzysztof Kozlowski | 70cfef2 | 2015-06-29 09:04:43 +0900 | [diff] [blame] | 2348 | lockdep_assert_held_once(&rdev->mutex); | 
|  | 2349 |  | 
| Richard Fitzgerald | a1c8a55 | 2014-11-24 14:10:52 +0000 | [diff] [blame] | 2350 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | | 
|  | 2351 | REGULATOR_EVENT_PRE_DISABLE, NULL); | 
|  | 2352 | if (ret & NOTIFY_STOP_MASK) | 
|  | 2353 | return -EINVAL; | 
|  | 2354 |  | 
| Markus Pargmann | 66fda75 | 2014-02-20 17:36:04 +0100 | [diff] [blame] | 2355 | ret = _regulator_do_disable(rdev); | 
|  | 2356 | if (ret < 0) { | 
|  | 2357 | rdev_err(rdev, "failed to force disable\n"); | 
| Richard Fitzgerald | a1c8a55 | 2014-11-24 14:10:52 +0000 | [diff] [blame] | 2358 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | | 
|  | 2359 | REGULATOR_EVENT_ABORT_DISABLE, NULL); | 
| Markus Pargmann | 66fda75 | 2014-02-20 17:36:04 +0100 | [diff] [blame] | 2360 | return ret; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2361 | } | 
|  | 2362 |  | 
| Markus Pargmann | 66fda75 | 2014-02-20 17:36:04 +0100 | [diff] [blame] | 2363 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | | 
|  | 2364 | REGULATOR_EVENT_DISABLE, NULL); | 
|  | 2365 |  | 
|  | 2366 | return 0; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2367 | } | 
|  | 2368 |  | 
|  | 2369 | /** | 
|  | 2370 | * regulator_force_disable - force disable regulator output | 
|  | 2371 | * @regulator: regulator source | 
|  | 2372 | * | 
|  | 2373 | * Forcibly disable the regulator output voltage or current. | 
|  | 2374 | * NOTE: this *will* disable the regulator output even if other consumer | 
|  | 2375 | * devices have it enabled. This should be used for situations when device | 
|  | 2376 | * damage will likely occur if the regulator is not disabled (e.g. over temp). | 
|  | 2377 | */ | 
|  | 2378 | int regulator_force_disable(struct regulator *regulator) | 
|  | 2379 | { | 
| Mark Brown | 82d1583 | 2011-05-09 11:41:02 +0200 | [diff] [blame] | 2380 | struct regulator_dev *rdev = regulator->rdev; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2381 | int ret; | 
|  | 2382 |  | 
| Mark Brown | 82d1583 | 2011-05-09 11:41:02 +0200 | [diff] [blame] | 2383 | mutex_lock(&rdev->mutex); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2384 | regulator->uA_load = 0; | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2385 | ret = _regulator_force_disable(regulator->rdev); | 
| Mark Brown | 82d1583 | 2011-05-09 11:41:02 +0200 | [diff] [blame] | 2386 | mutex_unlock(&rdev->mutex); | 
| Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 2387 |  | 
| Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2388 | if (rdev->supply) | 
|  | 2389 | while (rdev->open_count--) | 
|  | 2390 | regulator_disable(rdev->supply); | 
| Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 2391 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2392 | return ret; | 
|  | 2393 | } | 
|  | 2394 | EXPORT_SYMBOL_GPL(regulator_force_disable); | 
|  | 2395 |  | 
| Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 2396 | static void regulator_disable_work(struct work_struct *work) | 
|  | 2397 | { | 
|  | 2398 | struct regulator_dev *rdev = container_of(work, struct regulator_dev, | 
|  | 2399 | disable_work.work); | 
|  | 2400 | int count, i, ret; | 
|  | 2401 |  | 
|  | 2402 | mutex_lock(&rdev->mutex); | 
|  | 2403 |  | 
|  | 2404 | BUG_ON(!rdev->deferred_disables); | 
|  | 2405 |  | 
|  | 2406 | count = rdev->deferred_disables; | 
|  | 2407 | rdev->deferred_disables = 0; | 
|  | 2408 |  | 
| Tirupathi Reddy | c9ccaa0 | 2017-07-12 17:08:13 +0530 | [diff] [blame] | 2409 | /* | 
|  | 2410 | * Workqueue functions queue the new work instance while the previous | 
|  | 2411 | * work instance is being processed. Cancel the queued work instance | 
|  | 2412 | * as the work instance under processing does the job of the queued | 
|  | 2413 | * work instance. | 
|  | 2414 | */ | 
|  | 2415 | cancel_delayed_work(&rdev->disable_work); | 
|  | 2416 |  | 
| Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 2417 | for (i = 0; i < count; i++) { | 
|  | 2418 | ret = _regulator_disable(rdev); | 
|  | 2419 | if (ret != 0) | 
|  | 2420 | rdev_err(rdev, "Deferred disable failed: %d\n", ret); | 
|  | 2421 | } | 
|  | 2422 |  | 
|  | 2423 | mutex_unlock(&rdev->mutex); | 
|  | 2424 |  | 
|  | 2425 | if (rdev->supply) { | 
|  | 2426 | for (i = 0; i < count; i++) { | 
|  | 2427 | ret = regulator_disable(rdev->supply); | 
|  | 2428 | if (ret != 0) { | 
|  | 2429 | rdev_err(rdev, | 
|  | 2430 | "Supply disable failed: %d\n", ret); | 
|  | 2431 | } | 
|  | 2432 | } | 
|  | 2433 | } | 
|  | 2434 | } | 
|  | 2435 |  | 
|  | 2436 | /** | 
|  | 2437 | * regulator_disable_deferred - disable regulator output with delay | 
|  | 2438 | * @regulator: regulator source | 
|  | 2439 | * @ms: miliseconds until the regulator is disabled | 
|  | 2440 | * | 
|  | 2441 | * Execute regulator_disable() on the regulator after a delay.  This | 
|  | 2442 | * is intended for use with devices that require some time to quiesce. | 
|  | 2443 | * | 
|  | 2444 | * NOTE: this will only disable the regulator output if no other consumer | 
|  | 2445 | * devices have it enabled, the regulator device supports disabling and | 
|  | 2446 | * machine constraints permit this operation. | 
|  | 2447 | */ | 
|  | 2448 | int regulator_disable_deferred(struct regulator *regulator, int ms) | 
|  | 2449 | { | 
|  | 2450 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 2451 |  | 
| Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 2452 | if (regulator->always_on) | 
|  | 2453 | return 0; | 
|  | 2454 |  | 
| Mark Brown | 2b5a24a | 2012-09-07 11:00:53 +0800 | [diff] [blame] | 2455 | if (!ms) | 
|  | 2456 | return regulator_disable(regulator); | 
|  | 2457 |  | 
| Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 2458 | mutex_lock(&rdev->mutex); | 
|  | 2459 | rdev->deferred_disables++; | 
| Tirupathi Reddy | c9ccaa0 | 2017-07-12 17:08:13 +0530 | [diff] [blame] | 2460 | mod_delayed_work(system_power_efficient_wq, &rdev->disable_work, | 
|  | 2461 | msecs_to_jiffies(ms)); | 
| Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 2462 | mutex_unlock(&rdev->mutex); | 
|  | 2463 |  | 
| Dan Carpenter | 70dc6da | 2016-01-07 13:03:08 +0300 | [diff] [blame] | 2464 | return 0; | 
| Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 2465 | } | 
|  | 2466 | EXPORT_SYMBOL_GPL(regulator_disable_deferred); | 
|  | 2467 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2468 | static int _regulator_is_enabled(struct regulator_dev *rdev) | 
|  | 2469 | { | 
| Mark Brown | 65f7350 | 2012-06-27 14:14:38 +0100 | [diff] [blame] | 2470 | /* A GPIO control always takes precedence */ | 
| Kim, Milo | 7b74d14 | 2013-02-18 06:50:55 +0000 | [diff] [blame] | 2471 | if (rdev->ena_pin) | 
| Mark Brown | 65f7350 | 2012-06-27 14:14:38 +0100 | [diff] [blame] | 2472 | return rdev->ena_gpio_state; | 
|  | 2473 |  | 
| Mark Brown | 9a7f6a4 | 2010-02-11 17:22:45 +0000 | [diff] [blame] | 2474 | /* If we don't know then assume that the regulator is always on */ | 
| Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 2475 | if (!rdev->desc->ops->is_enabled) | 
| Mark Brown | 9a7f6a4 | 2010-02-11 17:22:45 +0000 | [diff] [blame] | 2476 | return 1; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2477 |  | 
| Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 2478 | return rdev->desc->ops->is_enabled(rdev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2479 | } | 
|  | 2480 |  | 
| Sascha Hauer | 3a40cfc | 2015-09-30 16:05:43 +0200 | [diff] [blame] | 2481 | static int _regulator_list_voltage(struct regulator *regulator, | 
|  | 2482 | unsigned selector, int lock) | 
|  | 2483 | { | 
|  | 2484 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 2485 | const struct regulator_ops *ops = rdev->desc->ops; | 
|  | 2486 | int ret; | 
|  | 2487 |  | 
|  | 2488 | if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) | 
|  | 2489 | return rdev->desc->fixed_uV; | 
|  | 2490 |  | 
|  | 2491 | if (ops->list_voltage) { | 
|  | 2492 | if (selector >= rdev->desc->n_voltages) | 
|  | 2493 | return -EINVAL; | 
|  | 2494 | if (lock) | 
|  | 2495 | mutex_lock(&rdev->mutex); | 
|  | 2496 | ret = ops->list_voltage(rdev, selector); | 
|  | 2497 | if (lock) | 
|  | 2498 | mutex_unlock(&rdev->mutex); | 
| Matthias Kaehlcke | fd08604 | 2017-03-27 16:54:12 -0700 | [diff] [blame] | 2499 | } else if (rdev->is_switch && rdev->supply) { | 
| Sascha Hauer | 3a40cfc | 2015-09-30 16:05:43 +0200 | [diff] [blame] | 2500 | ret = _regulator_list_voltage(rdev->supply, selector, lock); | 
|  | 2501 | } else { | 
|  | 2502 | return -EINVAL; | 
|  | 2503 | } | 
|  | 2504 |  | 
|  | 2505 | if (ret > 0) { | 
|  | 2506 | if (ret < rdev->constraints->min_uV) | 
|  | 2507 | ret = 0; | 
|  | 2508 | else if (ret > rdev->constraints->max_uV) | 
|  | 2509 | ret = 0; | 
|  | 2510 | } | 
|  | 2511 |  | 
|  | 2512 | return ret; | 
|  | 2513 | } | 
|  | 2514 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2515 | /** | 
|  | 2516 | * regulator_is_enabled - is the regulator output enabled | 
|  | 2517 | * @regulator: regulator source | 
|  | 2518 | * | 
| David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 2519 | * Returns positive if the regulator driver backing the source/client | 
|  | 2520 | * has requested that the device be enabled, zero if it hasn't, else a | 
|  | 2521 | * negative errno code. | 
|  | 2522 | * | 
|  | 2523 | * Note that the device backing this regulator handle can have multiple | 
|  | 2524 | * users, so it might be enabled even if regulator_enable() was never | 
|  | 2525 | * called for this particular source. | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2526 | */ | 
|  | 2527 | int regulator_is_enabled(struct regulator *regulator) | 
|  | 2528 | { | 
| Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 2529 | int ret; | 
|  | 2530 |  | 
| Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 2531 | if (regulator->always_on) | 
|  | 2532 | return 1; | 
|  | 2533 |  | 
| Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 2534 | mutex_lock(®ulator->rdev->mutex); | 
|  | 2535 | ret = _regulator_is_enabled(regulator->rdev); | 
|  | 2536 | mutex_unlock(®ulator->rdev->mutex); | 
|  | 2537 |  | 
|  | 2538 | return ret; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2539 | } | 
|  | 2540 | EXPORT_SYMBOL_GPL(regulator_is_enabled); | 
|  | 2541 |  | 
|  | 2542 | /** | 
| David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 2543 | * regulator_count_voltages - count regulator_list_voltage() selectors | 
|  | 2544 | * @regulator: regulator source | 
|  | 2545 | * | 
|  | 2546 | * Returns number of selectors, or negative errno.  Selectors are | 
|  | 2547 | * numbered starting at zero, and typically correspond to bitfields | 
|  | 2548 | * in hardware registers. | 
|  | 2549 | */ | 
|  | 2550 | int regulator_count_voltages(struct regulator *regulator) | 
|  | 2551 | { | 
|  | 2552 | struct regulator_dev	*rdev = regulator->rdev; | 
|  | 2553 |  | 
| Javier Martinez Canillas | 26988ef | 2014-07-29 18:28:56 +0200 | [diff] [blame] | 2554 | if (rdev->desc->n_voltages) | 
|  | 2555 | return rdev->desc->n_voltages; | 
|  | 2556 |  | 
| Matthias Kaehlcke | fd08604 | 2017-03-27 16:54:12 -0700 | [diff] [blame] | 2557 | if (!rdev->is_switch || !rdev->supply) | 
| Javier Martinez Canillas | 26988ef | 2014-07-29 18:28:56 +0200 | [diff] [blame] | 2558 | return -EINVAL; | 
|  | 2559 |  | 
|  | 2560 | return regulator_count_voltages(rdev->supply); | 
| David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 2561 | } | 
|  | 2562 | EXPORT_SYMBOL_GPL(regulator_count_voltages); | 
|  | 2563 |  | 
|  | 2564 | /** | 
|  | 2565 | * regulator_list_voltage - enumerate supported voltages | 
|  | 2566 | * @regulator: regulator source | 
|  | 2567 | * @selector: identify voltage to list | 
|  | 2568 | * Context: can sleep | 
|  | 2569 | * | 
|  | 2570 | * Returns a voltage that can be passed to @regulator_set_voltage(), | 
| Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 2571 | * zero if this selector code can't be used on this system, or a | 
| David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 2572 | * negative errno. | 
|  | 2573 | */ | 
|  | 2574 | int regulator_list_voltage(struct regulator *regulator, unsigned selector) | 
|  | 2575 | { | 
| Sascha Hauer | 3a40cfc | 2015-09-30 16:05:43 +0200 | [diff] [blame] | 2576 | return _regulator_list_voltage(regulator, selector, 1); | 
| David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 2577 | } | 
|  | 2578 | EXPORT_SYMBOL_GPL(regulator_list_voltage); | 
|  | 2579 |  | 
|  | 2580 | /** | 
| Tuomas Tynkkynen | 04eca28 | 2014-07-21 18:38:48 +0300 | [diff] [blame] | 2581 | * regulator_get_regmap - get the regulator's register map | 
|  | 2582 | * @regulator: regulator source | 
|  | 2583 | * | 
|  | 2584 | * Returns the register map for the given regulator, or an ERR_PTR value | 
|  | 2585 | * if the regulator doesn't use regmap. | 
|  | 2586 | */ | 
|  | 2587 | struct regmap *regulator_get_regmap(struct regulator *regulator) | 
|  | 2588 | { | 
|  | 2589 | struct regmap *map = regulator->rdev->regmap; | 
|  | 2590 |  | 
|  | 2591 | return map ? map : ERR_PTR(-EOPNOTSUPP); | 
|  | 2592 | } | 
|  | 2593 |  | 
|  | 2594 | /** | 
|  | 2595 | * regulator_get_hardware_vsel_register - get the HW voltage selector register | 
|  | 2596 | * @regulator: regulator source | 
|  | 2597 | * @vsel_reg: voltage selector register, output parameter | 
|  | 2598 | * @vsel_mask: mask for voltage selector bitfield, output parameter | 
|  | 2599 | * | 
|  | 2600 | * Returns the hardware register offset and bitmask used for setting the | 
|  | 2601 | * regulator voltage. This might be useful when configuring voltage-scaling | 
|  | 2602 | * hardware or firmware that can make I2C requests behind the kernel's back, | 
|  | 2603 | * for example. | 
|  | 2604 | * | 
|  | 2605 | * On success, the output parameters @vsel_reg and @vsel_mask are filled in | 
|  | 2606 | * and 0 is returned, otherwise a negative errno is returned. | 
|  | 2607 | */ | 
|  | 2608 | int regulator_get_hardware_vsel_register(struct regulator *regulator, | 
|  | 2609 | unsigned *vsel_reg, | 
|  | 2610 | unsigned *vsel_mask) | 
|  | 2611 | { | 
| Guodong Xu | 39f5460 | 2014-08-19 18:07:41 +0800 | [diff] [blame] | 2612 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 2613 | const struct regulator_ops *ops = rdev->desc->ops; | 
| Tuomas Tynkkynen | 04eca28 | 2014-07-21 18:38:48 +0300 | [diff] [blame] | 2614 |  | 
|  | 2615 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) | 
|  | 2616 | return -EOPNOTSUPP; | 
|  | 2617 |  | 
|  | 2618 | *vsel_reg = rdev->desc->vsel_reg; | 
|  | 2619 | *vsel_mask = rdev->desc->vsel_mask; | 
|  | 2620 |  | 
|  | 2621 | return 0; | 
|  | 2622 | } | 
|  | 2623 | EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register); | 
|  | 2624 |  | 
|  | 2625 | /** | 
|  | 2626 | * regulator_list_hardware_vsel - get the HW-specific register value for a selector | 
|  | 2627 | * @regulator: regulator source | 
|  | 2628 | * @selector: identify voltage to list | 
|  | 2629 | * | 
|  | 2630 | * Converts the selector to a hardware-specific voltage selector that can be | 
|  | 2631 | * directly written to the regulator registers. The address of the voltage | 
|  | 2632 | * register can be determined by calling @regulator_get_hardware_vsel_register. | 
|  | 2633 | * | 
|  | 2634 | * On error a negative errno is returned. | 
|  | 2635 | */ | 
|  | 2636 | int regulator_list_hardware_vsel(struct regulator *regulator, | 
|  | 2637 | unsigned selector) | 
|  | 2638 | { | 
| Guodong Xu | 39f5460 | 2014-08-19 18:07:41 +0800 | [diff] [blame] | 2639 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 2640 | const struct regulator_ops *ops = rdev->desc->ops; | 
| Tuomas Tynkkynen | 04eca28 | 2014-07-21 18:38:48 +0300 | [diff] [blame] | 2641 |  | 
|  | 2642 | if (selector >= rdev->desc->n_voltages) | 
|  | 2643 | return -EINVAL; | 
|  | 2644 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) | 
|  | 2645 | return -EOPNOTSUPP; | 
|  | 2646 |  | 
|  | 2647 | return selector; | 
|  | 2648 | } | 
|  | 2649 | EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel); | 
|  | 2650 |  | 
|  | 2651 | /** | 
| Paul Walmsley | 2a668a8 | 2013-06-07 08:06:56 +0000 | [diff] [blame] | 2652 | * regulator_get_linear_step - return the voltage step size between VSEL values | 
|  | 2653 | * @regulator: regulator source | 
|  | 2654 | * | 
|  | 2655 | * Returns the voltage step size between VSEL values for linear | 
|  | 2656 | * regulators, or return 0 if the regulator isn't a linear regulator. | 
|  | 2657 | */ | 
|  | 2658 | unsigned int regulator_get_linear_step(struct regulator *regulator) | 
|  | 2659 | { | 
|  | 2660 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 2661 |  | 
|  | 2662 | return rdev->desc->uV_step; | 
|  | 2663 | } | 
|  | 2664 | EXPORT_SYMBOL_GPL(regulator_get_linear_step); | 
|  | 2665 |  | 
|  | 2666 | /** | 
| Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 2667 | * regulator_is_supported_voltage - check if a voltage range can be supported | 
|  | 2668 | * | 
|  | 2669 | * @regulator: Regulator to check. | 
|  | 2670 | * @min_uV: Minimum required voltage in uV. | 
|  | 2671 | * @max_uV: Maximum required voltage in uV. | 
|  | 2672 | * | 
|  | 2673 | * Returns a boolean or a negative error code. | 
|  | 2674 | */ | 
|  | 2675 | int regulator_is_supported_voltage(struct regulator *regulator, | 
|  | 2676 | int min_uV, int max_uV) | 
|  | 2677 | { | 
| Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 2678 | struct regulator_dev *rdev = regulator->rdev; | 
| Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 2679 | int i, voltages, ret; | 
|  | 2680 |  | 
| Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 2681 | /* If we can't change voltage check the current voltage */ | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 2682 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { | 
| Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 2683 | ret = regulator_get_voltage(regulator); | 
|  | 2684 | if (ret >= 0) | 
| Jingoo Han | 0d25d09 | 2014-01-08 10:02:16 +0900 | [diff] [blame] | 2685 | return min_uV <= ret && ret <= max_uV; | 
| Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 2686 | else | 
|  | 2687 | return ret; | 
|  | 2688 | } | 
|  | 2689 |  | 
| Pawel Moll | bd7a2b6 | 2012-09-24 18:56:53 +0100 | [diff] [blame] | 2690 | /* Any voltage within constrains range is fine? */ | 
|  | 2691 | if (rdev->desc->continuous_voltage_range) | 
|  | 2692 | return min_uV >= rdev->constraints->min_uV && | 
|  | 2693 | max_uV <= rdev->constraints->max_uV; | 
|  | 2694 |  | 
| Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 2695 | ret = regulator_count_voltages(regulator); | 
|  | 2696 | if (ret < 0) | 
|  | 2697 | return ret; | 
|  | 2698 | voltages = ret; | 
|  | 2699 |  | 
|  | 2700 | for (i = 0; i < voltages; i++) { | 
|  | 2701 | ret = regulator_list_voltage(regulator, i); | 
|  | 2702 |  | 
|  | 2703 | if (ret >= min_uV && ret <= max_uV) | 
|  | 2704 | return 1; | 
|  | 2705 | } | 
|  | 2706 |  | 
|  | 2707 | return 0; | 
|  | 2708 | } | 
| Mark Brown | a398eaa | 2011-12-28 12:48:45 +0000 | [diff] [blame] | 2709 | EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); | 
| Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 2710 |  | 
| Sascha Hauer | a204f41 | 2015-10-20 14:37:27 +0200 | [diff] [blame] | 2711 | static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV, | 
|  | 2712 | int max_uV) | 
|  | 2713 | { | 
|  | 2714 | const struct regulator_desc *desc = rdev->desc; | 
|  | 2715 |  | 
|  | 2716 | if (desc->ops->map_voltage) | 
|  | 2717 | return desc->ops->map_voltage(rdev, min_uV, max_uV); | 
|  | 2718 |  | 
|  | 2719 | if (desc->ops->list_voltage == regulator_list_voltage_linear) | 
|  | 2720 | return regulator_map_voltage_linear(rdev, min_uV, max_uV); | 
|  | 2721 |  | 
|  | 2722 | if (desc->ops->list_voltage == regulator_list_voltage_linear_range) | 
|  | 2723 | return regulator_map_voltage_linear_range(rdev, min_uV, max_uV); | 
|  | 2724 |  | 
|  | 2725 | return regulator_map_voltage_iterate(rdev, min_uV, max_uV); | 
|  | 2726 | } | 
|  | 2727 |  | 
| Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 2728 | static int _regulator_call_set_voltage(struct regulator_dev *rdev, | 
|  | 2729 | int min_uV, int max_uV, | 
|  | 2730 | unsigned *selector) | 
|  | 2731 | { | 
|  | 2732 | struct pre_voltage_change_data data; | 
|  | 2733 | int ret; | 
|  | 2734 |  | 
|  | 2735 | data.old_uV = _regulator_get_voltage(rdev); | 
|  | 2736 | data.min_uV = min_uV; | 
|  | 2737 | data.max_uV = max_uV; | 
|  | 2738 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, | 
|  | 2739 | &data); | 
|  | 2740 | if (ret & NOTIFY_STOP_MASK) | 
|  | 2741 | return -EINVAL; | 
|  | 2742 |  | 
|  | 2743 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector); | 
|  | 2744 | if (ret >= 0) | 
|  | 2745 | return ret; | 
|  | 2746 |  | 
|  | 2747 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, | 
|  | 2748 | (void *)data.old_uV); | 
|  | 2749 |  | 
|  | 2750 | return ret; | 
|  | 2751 | } | 
|  | 2752 |  | 
|  | 2753 | static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev, | 
|  | 2754 | int uV, unsigned selector) | 
|  | 2755 | { | 
|  | 2756 | struct pre_voltage_change_data data; | 
|  | 2757 | int ret; | 
|  | 2758 |  | 
|  | 2759 | data.old_uV = _regulator_get_voltage(rdev); | 
|  | 2760 | data.min_uV = uV; | 
|  | 2761 | data.max_uV = uV; | 
|  | 2762 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, | 
|  | 2763 | &data); | 
|  | 2764 | if (ret & NOTIFY_STOP_MASK) | 
|  | 2765 | return -EINVAL; | 
|  | 2766 |  | 
|  | 2767 | ret = rdev->desc->ops->set_voltage_sel(rdev, selector); | 
|  | 2768 | if (ret >= 0) | 
|  | 2769 | return ret; | 
|  | 2770 |  | 
|  | 2771 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, | 
|  | 2772 | (void *)data.old_uV); | 
|  | 2773 |  | 
|  | 2774 | return ret; | 
|  | 2775 | } | 
|  | 2776 |  | 
| Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2777 | static int _regulator_set_voltage_time(struct regulator_dev *rdev, | 
|  | 2778 | int old_uV, int new_uV) | 
|  | 2779 | { | 
|  | 2780 | unsigned int ramp_delay = 0; | 
|  | 2781 |  | 
|  | 2782 | if (rdev->constraints->ramp_delay) | 
|  | 2783 | ramp_delay = rdev->constraints->ramp_delay; | 
|  | 2784 | else if (rdev->desc->ramp_delay) | 
|  | 2785 | ramp_delay = rdev->desc->ramp_delay; | 
| Laxman Dewangan | d6c1dc3 | 2017-04-04 18:59:50 +0530 | [diff] [blame] | 2786 | else if (rdev->constraints->settling_time) | 
|  | 2787 | return rdev->constraints->settling_time; | 
| Matthias Kaehlcke | 3ffad46 | 2017-05-16 11:43:43 -0700 | [diff] [blame] | 2788 | else if (rdev->constraints->settling_time_up && | 
|  | 2789 | (new_uV > old_uV)) | 
|  | 2790 | return rdev->constraints->settling_time_up; | 
|  | 2791 | else if (rdev->constraints->settling_time_down && | 
|  | 2792 | (new_uV < old_uV)) | 
|  | 2793 | return rdev->constraints->settling_time_down; | 
| Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2794 |  | 
|  | 2795 | if (ramp_delay == 0) { | 
| H. Nikolaus Schaller | ba14fa1 | 2016-10-27 14:31:39 +0200 | [diff] [blame] | 2796 | rdev_dbg(rdev, "ramp_delay not set\n"); | 
| Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2797 | return 0; | 
|  | 2798 | } | 
|  | 2799 |  | 
|  | 2800 | return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); | 
|  | 2801 | } | 
|  | 2802 |  | 
| Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2803 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, | 
|  | 2804 | int min_uV, int max_uV) | 
|  | 2805 | { | 
|  | 2806 | int ret; | 
| Linus Walleij | 77af1b2 | 2011-03-17 13:24:36 +0100 | [diff] [blame] | 2807 | int delay = 0; | 
| Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2808 | int best_val = 0; | 
| Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2809 | unsigned int selector; | 
| Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2810 | int old_selector = -1; | 
| Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2811 | const struct regulator_ops *ops = rdev->desc->ops; | 
| Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2812 | int old_uV = _regulator_get_voltage(rdev); | 
| Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2813 |  | 
|  | 2814 | trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); | 
|  | 2815 |  | 
| Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2816 | min_uV += rdev->constraints->uV_offset; | 
|  | 2817 | max_uV += rdev->constraints->uV_offset; | 
|  | 2818 |  | 
| Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2819 | /* | 
|  | 2820 | * If we can't obtain the old selector there is not enough | 
|  | 2821 | * info to call set_voltage_time_sel(). | 
|  | 2822 | */ | 
| Axel Lin | 8b7485e | 2012-05-21 09:37:52 +0800 | [diff] [blame] | 2823 | if (_regulator_is_enabled(rdev) && | 
| Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2824 | ops->set_voltage_time_sel && ops->get_voltage_sel) { | 
|  | 2825 | old_selector = ops->get_voltage_sel(rdev); | 
| Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2826 | if (old_selector < 0) | 
|  | 2827 | return old_selector; | 
|  | 2828 | } | 
|  | 2829 |  | 
| Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2830 | if (ops->set_voltage) { | 
| Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 2831 | ret = _regulator_call_set_voltage(rdev, min_uV, max_uV, | 
|  | 2832 | &selector); | 
| Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2833 |  | 
|  | 2834 | if (ret >= 0) { | 
| Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2835 | if (ops->list_voltage) | 
|  | 2836 | best_val = ops->list_voltage(rdev, | 
|  | 2837 | selector); | 
| Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2838 | else | 
|  | 2839 | best_val = _regulator_get_voltage(rdev); | 
|  | 2840 | } | 
|  | 2841 |  | 
| Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2842 | } else if (ops->set_voltage_sel) { | 
| Sascha Hauer | a204f41 | 2015-10-20 14:37:27 +0200 | [diff] [blame] | 2843 | ret = regulator_map_voltage(rdev, min_uV, max_uV); | 
| Mark Brown | e843fc4 | 2012-05-09 21:16:06 +0100 | [diff] [blame] | 2844 | if (ret >= 0) { | 
| Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2845 | best_val = ops->list_voltage(rdev, ret); | 
| Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2846 | if (min_uV <= best_val && max_uV >= best_val) { | 
|  | 2847 | selector = ret; | 
| Axel Lin | c66a566 | 2013-02-06 11:09:48 +0800 | [diff] [blame] | 2848 | if (old_selector == selector) | 
|  | 2849 | ret = 0; | 
|  | 2850 | else | 
| Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 2851 | ret = _regulator_call_set_voltage_sel( | 
|  | 2852 | rdev, best_val, selector); | 
| Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2853 | } else { | 
|  | 2854 | ret = -EINVAL; | 
|  | 2855 | } | 
| Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2856 | } | 
| Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2857 | } else { | 
|  | 2858 | ret = -EINVAL; | 
|  | 2859 | } | 
|  | 2860 |  | 
| Matthias Kaehlcke | 31dfe68 | 2016-09-14 09:52:06 -0700 | [diff] [blame] | 2861 | if (ret) | 
|  | 2862 | goto out; | 
| Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2863 |  | 
| Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2864 | if (ops->set_voltage_time_sel) { | 
|  | 2865 | /* | 
|  | 2866 | * Call set_voltage_time_sel if successfully obtained | 
|  | 2867 | * old_selector | 
|  | 2868 | */ | 
|  | 2869 | if (old_selector >= 0 && old_selector != selector) | 
|  | 2870 | delay = ops->set_voltage_time_sel(rdev, old_selector, | 
|  | 2871 | selector); | 
|  | 2872 | } else { | 
|  | 2873 | if (old_uV != best_val) { | 
|  | 2874 | if (ops->set_voltage_time) | 
|  | 2875 | delay = ops->set_voltage_time(rdev, old_uV, | 
|  | 2876 | best_val); | 
|  | 2877 | else | 
|  | 2878 | delay = _regulator_set_voltage_time(rdev, | 
|  | 2879 | old_uV, | 
|  | 2880 | best_val); | 
| Philip Rakity | 8b96de3 | 2012-06-14 15:07:56 -0700 | [diff] [blame] | 2881 | } | 
| Linus Walleij | 77af1b2 | 2011-03-17 13:24:36 +0100 | [diff] [blame] | 2882 | } | 
|  | 2883 |  | 
| Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2884 | if (delay < 0) { | 
|  | 2885 | rdev_warn(rdev, "failed to get delay: %d\n", delay); | 
|  | 2886 | delay = 0; | 
|  | 2887 | } | 
|  | 2888 |  | 
|  | 2889 | /* Insert any necessary delays */ | 
|  | 2890 | if (delay >= 1000) { | 
|  | 2891 | mdelay(delay / 1000); | 
|  | 2892 | udelay(delay % 1000); | 
|  | 2893 | } else if (delay) { | 
|  | 2894 | udelay(delay); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2895 | } | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2896 |  | 
| Matthias Kaehlcke | 31dfe68 | 2016-09-14 09:52:06 -0700 | [diff] [blame] | 2897 | if (best_val >= 0) { | 
| Axel Lin | 2f6c797 | 2012-08-06 23:44:19 +0800 | [diff] [blame] | 2898 | unsigned long data = best_val; | 
|  | 2899 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2900 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, | 
| Axel Lin | 2f6c797 | 2012-08-06 23:44:19 +0800 | [diff] [blame] | 2901 | (void *)data); | 
|  | 2902 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2903 |  | 
| Matthias Kaehlcke | 31dfe68 | 2016-09-14 09:52:06 -0700 | [diff] [blame] | 2904 | out: | 
| Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2905 | trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2906 |  | 
|  | 2907 | return ret; | 
|  | 2908 | } | 
|  | 2909 |  | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 2910 | static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev, | 
|  | 2911 | int min_uV, int max_uV, suspend_state_t state) | 
|  | 2912 | { | 
|  | 2913 | struct regulator_state *rstate; | 
|  | 2914 | int uV, sel; | 
|  | 2915 |  | 
|  | 2916 | rstate = regulator_get_suspend_state(rdev, state); | 
|  | 2917 | if (rstate == NULL) | 
|  | 2918 | return -EINVAL; | 
|  | 2919 |  | 
|  | 2920 | if (min_uV < rstate->min_uV) | 
|  | 2921 | min_uV = rstate->min_uV; | 
|  | 2922 | if (max_uV > rstate->max_uV) | 
|  | 2923 | max_uV = rstate->max_uV; | 
|  | 2924 |  | 
|  | 2925 | sel = regulator_map_voltage(rdev, min_uV, max_uV); | 
|  | 2926 | if (sel < 0) | 
|  | 2927 | return sel; | 
|  | 2928 |  | 
|  | 2929 | uV = rdev->desc->ops->list_voltage(rdev, sel); | 
|  | 2930 | if (uV >= min_uV && uV <= max_uV) | 
|  | 2931 | rstate->uV = uV; | 
|  | 2932 |  | 
|  | 2933 | return 0; | 
|  | 2934 | } | 
|  | 2935 |  | 
| Sascha Hauer | a9f226b | 2015-10-13 12:45:25 +0200 | [diff] [blame] | 2936 | static int regulator_set_voltage_unlocked(struct regulator *regulator, | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 2937 | int min_uV, int max_uV, | 
|  | 2938 | suspend_state_t state) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2939 | { | 
|  | 2940 | struct regulator_dev *rdev = regulator->rdev; | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 2941 | struct regulator_voltage *voltage = ®ulator->voltage[state]; | 
| Mark Brown | 95a3c23 | 2010-12-16 15:49:37 +0000 | [diff] [blame] | 2942 | int ret = 0; | 
| Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 2943 | int old_min_uV, old_max_uV; | 
| Bjorn Andersson | c00dc35 | 2014-02-05 12:30:26 -0800 | [diff] [blame] | 2944 | int current_uV; | 
| Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 2945 | int best_supply_uV = 0; | 
|  | 2946 | int supply_change_uV = 0; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2947 |  | 
| Mark Brown | 95a3c23 | 2010-12-16 15:49:37 +0000 | [diff] [blame] | 2948 | /* If we're setting the same range as last time the change | 
|  | 2949 | * should be a noop (some cpufreq implementations use the same | 
|  | 2950 | * voltage for multiple frequencies, for example). | 
|  | 2951 | */ | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 2952 | if (voltage->min_uV == min_uV && voltage->max_uV == max_uV) | 
| Mark Brown | 95a3c23 | 2010-12-16 15:49:37 +0000 | [diff] [blame] | 2953 | goto out; | 
|  | 2954 |  | 
| Bjorn Andersson | c00dc35 | 2014-02-05 12:30:26 -0800 | [diff] [blame] | 2955 | /* If we're trying to set a range that overlaps the current voltage, | 
| Viresh Kumar | d3fb980 | 2015-08-13 18:09:49 +0530 | [diff] [blame] | 2956 | * return successfully even though the regulator does not support | 
| Bjorn Andersson | c00dc35 | 2014-02-05 12:30:26 -0800 | [diff] [blame] | 2957 | * changing the voltage. | 
|  | 2958 | */ | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 2959 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { | 
| Bjorn Andersson | c00dc35 | 2014-02-05 12:30:26 -0800 | [diff] [blame] | 2960 | current_uV = _regulator_get_voltage(rdev); | 
|  | 2961 | if (min_uV <= current_uV && current_uV <= max_uV) { | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 2962 | voltage->min_uV = min_uV; | 
|  | 2963 | voltage->max_uV = max_uV; | 
| Bjorn Andersson | c00dc35 | 2014-02-05 12:30:26 -0800 | [diff] [blame] | 2964 | goto out; | 
|  | 2965 | } | 
|  | 2966 | } | 
|  | 2967 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2968 | /* sanity check */ | 
| Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2969 | if (!rdev->desc->ops->set_voltage && | 
|  | 2970 | !rdev->desc->ops->set_voltage_sel) { | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2971 | ret = -EINVAL; | 
|  | 2972 | goto out; | 
|  | 2973 | } | 
|  | 2974 |  | 
|  | 2975 | /* constraints check */ | 
|  | 2976 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | 
|  | 2977 | if (ret < 0) | 
|  | 2978 | goto out; | 
| Jingoo Han | 0d25d09 | 2014-01-08 10:02:16 +0900 | [diff] [blame] | 2979 |  | 
| Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 2980 | /* restore original values in case of error */ | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 2981 | old_min_uV = voltage->min_uV; | 
|  | 2982 | old_max_uV = voltage->max_uV; | 
|  | 2983 | voltage->min_uV = min_uV; | 
|  | 2984 | voltage->max_uV = max_uV; | 
| Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 2985 |  | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 2986 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state); | 
| Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 2987 | if (ret < 0) | 
| Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 2988 | goto out2; | 
| Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 2989 |  | 
| Mark Brown | 43fc99f | 2017-04-13 18:36:59 +0100 | [diff] [blame] | 2990 | if (rdev->supply && | 
|  | 2991 | regulator_ops_is_valid(rdev->supply->rdev, | 
|  | 2992 | REGULATOR_CHANGE_VOLTAGE) && | 
| Tirupathi Reddy | 2c2874b | 2017-05-25 15:33:17 +0530 | [diff] [blame] | 2993 | (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage || | 
|  | 2994 | rdev->desc->ops->get_voltage_sel))) { | 
| Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 2995 | int current_supply_uV; | 
|  | 2996 | int selector; | 
|  | 2997 |  | 
|  | 2998 | selector = regulator_map_voltage(rdev, min_uV, max_uV); | 
|  | 2999 | if (selector < 0) { | 
|  | 3000 | ret = selector; | 
|  | 3001 | goto out2; | 
|  | 3002 | } | 
|  | 3003 |  | 
|  | 3004 | best_supply_uV = _regulator_list_voltage(regulator, selector, 0); | 
|  | 3005 | if (best_supply_uV < 0) { | 
|  | 3006 | ret = best_supply_uV; | 
|  | 3007 | goto out2; | 
|  | 3008 | } | 
|  | 3009 |  | 
|  | 3010 | best_supply_uV += rdev->desc->min_dropout_uV; | 
|  | 3011 |  | 
|  | 3012 | current_supply_uV = _regulator_get_voltage(rdev->supply->rdev); | 
|  | 3013 | if (current_supply_uV < 0) { | 
|  | 3014 | ret = current_supply_uV; | 
|  | 3015 | goto out2; | 
|  | 3016 | } | 
|  | 3017 |  | 
|  | 3018 | supply_change_uV = best_supply_uV - current_supply_uV; | 
|  | 3019 | } | 
|  | 3020 |  | 
|  | 3021 | if (supply_change_uV > 0) { | 
|  | 3022 | ret = regulator_set_voltage_unlocked(rdev->supply, | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 3023 | best_supply_uV, INT_MAX, state); | 
| Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 3024 | if (ret) { | 
|  | 3025 | dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n", | 
|  | 3026 | ret); | 
|  | 3027 | goto out2; | 
|  | 3028 | } | 
|  | 3029 | } | 
|  | 3030 |  | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 3031 | if (state == PM_SUSPEND_ON) | 
|  | 3032 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); | 
|  | 3033 | else | 
|  | 3034 | ret = _regulator_do_set_suspend_voltage(rdev, min_uV, | 
|  | 3035 | max_uV, state); | 
| Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 3036 | if (ret < 0) | 
|  | 3037 | goto out2; | 
| Jingoo Han | 0d25d09 | 2014-01-08 10:02:16 +0900 | [diff] [blame] | 3038 |  | 
| Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 3039 | if (supply_change_uV < 0) { | 
|  | 3040 | ret = regulator_set_voltage_unlocked(rdev->supply, | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 3041 | best_supply_uV, INT_MAX, state); | 
| Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 3042 | if (ret) | 
|  | 3043 | dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n", | 
|  | 3044 | ret); | 
|  | 3045 | /* No need to fail here */ | 
|  | 3046 | ret = 0; | 
|  | 3047 | } | 
|  | 3048 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3049 | out: | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3050 | return ret; | 
| Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 3051 | out2: | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 3052 | voltage->min_uV = old_min_uV; | 
|  | 3053 | voltage->max_uV = old_max_uV; | 
| Sascha Hauer | a9f226b | 2015-10-13 12:45:25 +0200 | [diff] [blame] | 3054 |  | 
|  | 3055 | return ret; | 
|  | 3056 | } | 
|  | 3057 |  | 
|  | 3058 | /** | 
|  | 3059 | * regulator_set_voltage - set regulator output voltage | 
|  | 3060 | * @regulator: regulator source | 
|  | 3061 | * @min_uV: Minimum required voltage in uV | 
|  | 3062 | * @max_uV: Maximum acceptable voltage in uV | 
|  | 3063 | * | 
|  | 3064 | * Sets a voltage regulator to the desired output voltage. This can be set | 
|  | 3065 | * during any regulator state. IOW, regulator can be disabled or enabled. | 
|  | 3066 | * | 
|  | 3067 | * If the regulator is enabled then the voltage will change to the new value | 
|  | 3068 | * immediately otherwise if the regulator is disabled the regulator will | 
|  | 3069 | * output at the new voltage when enabled. | 
|  | 3070 | * | 
|  | 3071 | * NOTE: If the regulator is shared between several devices then the lowest | 
|  | 3072 | * request voltage that meets the system constraints will be used. | 
|  | 3073 | * Regulator system constraints must be set for this regulator before | 
|  | 3074 | * calling this function otherwise this call will fail. | 
|  | 3075 | */ | 
|  | 3076 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) | 
|  | 3077 | { | 
|  | 3078 | int ret = 0; | 
|  | 3079 |  | 
| Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 3080 | regulator_lock_supply(regulator->rdev); | 
| Sascha Hauer | a9f226b | 2015-10-13 12:45:25 +0200 | [diff] [blame] | 3081 |  | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 3082 | ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV, | 
|  | 3083 | PM_SUSPEND_ON); | 
| Sascha Hauer | a9f226b | 2015-10-13 12:45:25 +0200 | [diff] [blame] | 3084 |  | 
| Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 3085 | regulator_unlock_supply(regulator->rdev); | 
| Sascha Hauer | a9f226b | 2015-10-13 12:45:25 +0200 | [diff] [blame] | 3086 |  | 
| Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 3087 | return ret; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3088 | } | 
|  | 3089 | EXPORT_SYMBOL_GPL(regulator_set_voltage); | 
|  | 3090 |  | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 3091 | static inline int regulator_suspend_toggle(struct regulator_dev *rdev, | 
|  | 3092 | suspend_state_t state, bool en) | 
|  | 3093 | { | 
|  | 3094 | struct regulator_state *rstate; | 
|  | 3095 |  | 
|  | 3096 | rstate = regulator_get_suspend_state(rdev, state); | 
|  | 3097 | if (rstate == NULL) | 
|  | 3098 | return -EINVAL; | 
|  | 3099 |  | 
|  | 3100 | if (!rstate->changeable) | 
|  | 3101 | return -EPERM; | 
|  | 3102 |  | 
|  | 3103 | rstate->enabled = en; | 
|  | 3104 |  | 
|  | 3105 | return 0; | 
|  | 3106 | } | 
|  | 3107 |  | 
|  | 3108 | int regulator_suspend_enable(struct regulator_dev *rdev, | 
|  | 3109 | suspend_state_t state) | 
|  | 3110 | { | 
|  | 3111 | return regulator_suspend_toggle(rdev, state, true); | 
|  | 3112 | } | 
|  | 3113 | EXPORT_SYMBOL_GPL(regulator_suspend_enable); | 
|  | 3114 |  | 
|  | 3115 | int regulator_suspend_disable(struct regulator_dev *rdev, | 
|  | 3116 | suspend_state_t state) | 
|  | 3117 | { | 
|  | 3118 | struct regulator *regulator; | 
|  | 3119 | struct regulator_voltage *voltage; | 
|  | 3120 |  | 
|  | 3121 | /* | 
|  | 3122 | * if any consumer wants this regulator device keeping on in | 
|  | 3123 | * suspend states, don't set it as disabled. | 
|  | 3124 | */ | 
|  | 3125 | list_for_each_entry(regulator, &rdev->consumer_list, list) { | 
|  | 3126 | voltage = ®ulator->voltage[state]; | 
|  | 3127 | if (voltage->min_uV || voltage->max_uV) | 
|  | 3128 | return 0; | 
|  | 3129 | } | 
|  | 3130 |  | 
|  | 3131 | return regulator_suspend_toggle(rdev, state, false); | 
|  | 3132 | } | 
|  | 3133 | EXPORT_SYMBOL_GPL(regulator_suspend_disable); | 
|  | 3134 |  | 
|  | 3135 | static int _regulator_set_suspend_voltage(struct regulator *regulator, | 
|  | 3136 | int min_uV, int max_uV, | 
|  | 3137 | suspend_state_t state) | 
|  | 3138 | { | 
|  | 3139 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 3140 | struct regulator_state *rstate; | 
|  | 3141 |  | 
|  | 3142 | rstate = regulator_get_suspend_state(rdev, state); | 
|  | 3143 | if (rstate == NULL) | 
|  | 3144 | return -EINVAL; | 
|  | 3145 |  | 
|  | 3146 | if (rstate->min_uV == rstate->max_uV) { | 
|  | 3147 | rdev_err(rdev, "The suspend voltage can't be changed!\n"); | 
|  | 3148 | return -EPERM; | 
|  | 3149 | } | 
|  | 3150 |  | 
|  | 3151 | return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state); | 
|  | 3152 | } | 
|  | 3153 |  | 
|  | 3154 | int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, | 
|  | 3155 | int max_uV, suspend_state_t state) | 
|  | 3156 | { | 
|  | 3157 | int ret = 0; | 
|  | 3158 |  | 
|  | 3159 | /* PM_SUSPEND_ON is handled by regulator_set_voltage() */ | 
|  | 3160 | if (regulator_check_states(state) || state == PM_SUSPEND_ON) | 
|  | 3161 | return -EINVAL; | 
|  | 3162 |  | 
|  | 3163 | regulator_lock_supply(regulator->rdev); | 
|  | 3164 |  | 
|  | 3165 | ret = _regulator_set_suspend_voltage(regulator, min_uV, | 
|  | 3166 | max_uV, state); | 
|  | 3167 |  | 
|  | 3168 | regulator_unlock_supply(regulator->rdev); | 
|  | 3169 |  | 
|  | 3170 | return ret; | 
|  | 3171 | } | 
|  | 3172 | EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage); | 
|  | 3173 |  | 
| Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 3174 | /** | 
| Linus Walleij | 88cd222b | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 3175 | * regulator_set_voltage_time - get raise/fall time | 
|  | 3176 | * @regulator: regulator source | 
|  | 3177 | * @old_uV: starting voltage in microvolts | 
|  | 3178 | * @new_uV: target voltage in microvolts | 
|  | 3179 | * | 
|  | 3180 | * Provided with the starting and ending voltage, this function attempts to | 
|  | 3181 | * calculate the time in microseconds required to rise or fall to this new | 
|  | 3182 | * voltage. | 
|  | 3183 | */ | 
|  | 3184 | int regulator_set_voltage_time(struct regulator *regulator, | 
|  | 3185 | int old_uV, int new_uV) | 
|  | 3186 | { | 
| Guodong Xu | 272e231 | 2014-08-13 19:33:38 +0800 | [diff] [blame] | 3187 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 3188 | const struct regulator_ops *ops = rdev->desc->ops; | 
| Linus Walleij | 88cd222b | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 3189 | int old_sel = -1; | 
|  | 3190 | int new_sel = -1; | 
|  | 3191 | int voltage; | 
|  | 3192 | int i; | 
|  | 3193 |  | 
| Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 3194 | if (ops->set_voltage_time) | 
|  | 3195 | return ops->set_voltage_time(rdev, old_uV, new_uV); | 
|  | 3196 | else if (!ops->set_voltage_time_sel) | 
|  | 3197 | return _regulator_set_voltage_time(rdev, old_uV, new_uV); | 
|  | 3198 |  | 
| Linus Walleij | 88cd222b | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 3199 | /* Currently requires operations to do this */ | 
| Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 3200 | if (!ops->list_voltage || !rdev->desc->n_voltages) | 
| Linus Walleij | 88cd222b | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 3201 | return -EINVAL; | 
|  | 3202 |  | 
|  | 3203 | for (i = 0; i < rdev->desc->n_voltages; i++) { | 
|  | 3204 | /* We only look for exact voltage matches here */ | 
|  | 3205 | voltage = regulator_list_voltage(regulator, i); | 
|  | 3206 | if (voltage < 0) | 
|  | 3207 | return -EINVAL; | 
|  | 3208 | if (voltage == 0) | 
|  | 3209 | continue; | 
|  | 3210 | if (voltage == old_uV) | 
|  | 3211 | old_sel = i; | 
|  | 3212 | if (voltage == new_uV) | 
|  | 3213 | new_sel = i; | 
|  | 3214 | } | 
|  | 3215 |  | 
|  | 3216 | if (old_sel < 0 || new_sel < 0) | 
|  | 3217 | return -EINVAL; | 
|  | 3218 |  | 
|  | 3219 | return ops->set_voltage_time_sel(rdev, old_sel, new_sel); | 
|  | 3220 | } | 
|  | 3221 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time); | 
|  | 3222 |  | 
|  | 3223 | /** | 
| Randy Dunlap | 296c656 | 2012-08-18 17:44:14 -0700 | [diff] [blame] | 3224 | * regulator_set_voltage_time_sel - get raise/fall time | 
|  | 3225 | * @rdev: regulator source device | 
| Yadwinder Singh Brar | 98a175b | 2012-06-09 16:40:38 +0530 | [diff] [blame] | 3226 | * @old_selector: selector for starting voltage | 
|  | 3227 | * @new_selector: selector for target voltage | 
|  | 3228 | * | 
|  | 3229 | * Provided with the starting and target voltage selectors, this function | 
|  | 3230 | * returns time in microseconds required to rise or fall to this new voltage | 
|  | 3231 | * | 
| Axel Lin | f11d08c | 2012-06-19 09:38:45 +0800 | [diff] [blame] | 3232 | * Drivers providing ramp_delay in regulation_constraints can use this as their | 
| Axel Lin | 398715a | 2012-06-18 10:11:28 +0800 | [diff] [blame] | 3233 | * set_voltage_time_sel() operation. | 
| Yadwinder Singh Brar | 98a175b | 2012-06-09 16:40:38 +0530 | [diff] [blame] | 3234 | */ | 
|  | 3235 | int regulator_set_voltage_time_sel(struct regulator_dev *rdev, | 
|  | 3236 | unsigned int old_selector, | 
|  | 3237 | unsigned int new_selector) | 
|  | 3238 | { | 
| Axel Lin | f11d08c | 2012-06-19 09:38:45 +0800 | [diff] [blame] | 3239 | int old_volt, new_volt; | 
| Axel Lin | 398715a | 2012-06-18 10:11:28 +0800 | [diff] [blame] | 3240 |  | 
| Axel Lin | f11d08c | 2012-06-19 09:38:45 +0800 | [diff] [blame] | 3241 | /* sanity check */ | 
|  | 3242 | if (!rdev->desc->ops->list_voltage) | 
|  | 3243 | return -EINVAL; | 
| Axel Lin | 398715a | 2012-06-18 10:11:28 +0800 | [diff] [blame] | 3244 |  | 
| Axel Lin | f11d08c | 2012-06-19 09:38:45 +0800 | [diff] [blame] | 3245 | old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); | 
|  | 3246 | new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); | 
|  | 3247 |  | 
| Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 3248 | if (rdev->desc->ops->set_voltage_time) | 
|  | 3249 | return rdev->desc->ops->set_voltage_time(rdev, old_volt, | 
|  | 3250 | new_volt); | 
|  | 3251 | else | 
|  | 3252 | return _regulator_set_voltage_time(rdev, old_volt, new_volt); | 
| Yadwinder Singh Brar | 98a175b | 2012-06-09 16:40:38 +0530 | [diff] [blame] | 3253 | } | 
| Mark Brown | b19dbf7 | 2012-06-23 11:34:20 +0100 | [diff] [blame] | 3254 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel); | 
| Yadwinder Singh Brar | 98a175b | 2012-06-09 16:40:38 +0530 | [diff] [blame] | 3255 |  | 
|  | 3256 | /** | 
| Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 3257 | * regulator_sync_voltage - re-apply last regulator output voltage | 
|  | 3258 | * @regulator: regulator source | 
|  | 3259 | * | 
|  | 3260 | * Re-apply the last configured voltage.  This is intended to be used | 
|  | 3261 | * where some external control source the consumer is cooperating with | 
|  | 3262 | * has caused the configured voltage to change. | 
|  | 3263 | */ | 
|  | 3264 | int regulator_sync_voltage(struct regulator *regulator) | 
|  | 3265 | { | 
|  | 3266 | struct regulator_dev *rdev = regulator->rdev; | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 3267 | struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON]; | 
| Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 3268 | int ret, min_uV, max_uV; | 
|  | 3269 |  | 
|  | 3270 | mutex_lock(&rdev->mutex); | 
|  | 3271 |  | 
|  | 3272 | if (!rdev->desc->ops->set_voltage && | 
|  | 3273 | !rdev->desc->ops->set_voltage_sel) { | 
|  | 3274 | ret = -EINVAL; | 
|  | 3275 | goto out; | 
|  | 3276 | } | 
|  | 3277 |  | 
|  | 3278 | /* This is only going to work if we've had a voltage configured. */ | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 3279 | if (!voltage->min_uV && !voltage->max_uV) { | 
| Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 3280 | ret = -EINVAL; | 
|  | 3281 | goto out; | 
|  | 3282 | } | 
|  | 3283 |  | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 3284 | min_uV = voltage->min_uV; | 
|  | 3285 | max_uV = voltage->max_uV; | 
| Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 3286 |  | 
|  | 3287 | /* This should be a paranoia check... */ | 
|  | 3288 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | 
|  | 3289 | if (ret < 0) | 
|  | 3290 | goto out; | 
|  | 3291 |  | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 3292 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0); | 
| Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 3293 | if (ret < 0) | 
|  | 3294 | goto out; | 
|  | 3295 |  | 
|  | 3296 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); | 
|  | 3297 |  | 
|  | 3298 | out: | 
|  | 3299 | mutex_unlock(&rdev->mutex); | 
|  | 3300 | return ret; | 
|  | 3301 | } | 
|  | 3302 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); | 
|  | 3303 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3304 | static int _regulator_get_voltage(struct regulator_dev *rdev) | 
|  | 3305 | { | 
| Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3306 | int sel, ret; | 
| Mark Brown | fef9501 | 2016-04-07 16:22:36 +0200 | [diff] [blame] | 3307 | bool bypassed; | 
|  | 3308 |  | 
|  | 3309 | if (rdev->desc->ops->get_bypass) { | 
|  | 3310 | ret = rdev->desc->ops->get_bypass(rdev, &bypassed); | 
|  | 3311 | if (ret < 0) | 
|  | 3312 | return ret; | 
|  | 3313 | if (bypassed) { | 
|  | 3314 | /* if bypassed the regulator must have a supply */ | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 3315 | if (!rdev->supply) { | 
|  | 3316 | rdev_err(rdev, | 
|  | 3317 | "bypassed regulator has no supply!\n"); | 
|  | 3318 | return -EPROBE_DEFER; | 
|  | 3319 | } | 
| Mark Brown | fef9501 | 2016-04-07 16:22:36 +0200 | [diff] [blame] | 3320 |  | 
|  | 3321 | return _regulator_get_voltage(rdev->supply->rdev); | 
|  | 3322 | } | 
|  | 3323 | } | 
| Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 3324 |  | 
|  | 3325 | if (rdev->desc->ops->get_voltage_sel) { | 
|  | 3326 | sel = rdev->desc->ops->get_voltage_sel(rdev); | 
|  | 3327 | if (sel < 0) | 
|  | 3328 | return sel; | 
| Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3329 | ret = rdev->desc->ops->list_voltage(rdev, sel); | 
| Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 3330 | } else if (rdev->desc->ops->get_voltage) { | 
| Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3331 | ret = rdev->desc->ops->get_voltage(rdev); | 
| Mark Brown | f7df20e | 2012-08-09 16:42:19 +0100 | [diff] [blame] | 3332 | } else if (rdev->desc->ops->list_voltage) { | 
|  | 3333 | ret = rdev->desc->ops->list_voltage(rdev, 0); | 
| Laxman Dewangan | 5a52360 | 2013-09-10 15:45:05 +0530 | [diff] [blame] | 3334 | } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { | 
|  | 3335 | ret = rdev->desc->fixed_uV; | 
| Javier Martinez Canillas | e303996 | 2014-07-29 18:28:55 +0200 | [diff] [blame] | 3336 | } else if (rdev->supply) { | 
| Mark Brown | d9b96d3 | 2015-11-03 05:58:14 +0000 | [diff] [blame] | 3337 | ret = _regulator_get_voltage(rdev->supply->rdev); | 
| Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 3338 | } else { | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3339 | return -EINVAL; | 
| Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 3340 | } | 
| Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3341 |  | 
| Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 3342 | if (ret < 0) | 
|  | 3343 | return ret; | 
| Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3344 | return ret - rdev->constraints->uV_offset; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3345 | } | 
|  | 3346 |  | 
|  | 3347 | /** | 
|  | 3348 | * regulator_get_voltage - get regulator output voltage | 
|  | 3349 | * @regulator: regulator source | 
|  | 3350 | * | 
|  | 3351 | * This returns the current regulator voltage in uV. | 
|  | 3352 | * | 
|  | 3353 | * NOTE: If the regulator is disabled it will return the voltage value. This | 
|  | 3354 | * function should not be used to determine regulator state. | 
|  | 3355 | */ | 
|  | 3356 | int regulator_get_voltage(struct regulator *regulator) | 
|  | 3357 | { | 
|  | 3358 | int ret; | 
|  | 3359 |  | 
| Mark Brown | d9b96d3 | 2015-11-03 05:58:14 +0000 | [diff] [blame] | 3360 | regulator_lock_supply(regulator->rdev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3361 |  | 
|  | 3362 | ret = _regulator_get_voltage(regulator->rdev); | 
|  | 3363 |  | 
| Mark Brown | d9b96d3 | 2015-11-03 05:58:14 +0000 | [diff] [blame] | 3364 | regulator_unlock_supply(regulator->rdev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3365 |  | 
|  | 3366 | return ret; | 
|  | 3367 | } | 
|  | 3368 | EXPORT_SYMBOL_GPL(regulator_get_voltage); | 
|  | 3369 |  | 
|  | 3370 | /** | 
|  | 3371 | * regulator_set_current_limit - set regulator output current limit | 
|  | 3372 | * @regulator: regulator source | 
| Charles Keepax | ce0d10f | 2013-05-21 15:04:07 +0100 | [diff] [blame] | 3373 | * @min_uA: Minimum supported current in uA | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3374 | * @max_uA: Maximum supported current in uA | 
|  | 3375 | * | 
|  | 3376 | * Sets current sink to the desired output current. This can be set during | 
|  | 3377 | * any regulator state. IOW, regulator can be disabled or enabled. | 
|  | 3378 | * | 
|  | 3379 | * If the regulator is enabled then the current will change to the new value | 
|  | 3380 | * immediately otherwise if the regulator is disabled the regulator will | 
|  | 3381 | * output at the new current when enabled. | 
|  | 3382 | * | 
|  | 3383 | * NOTE: Regulator system constraints must be set for this regulator before | 
|  | 3384 | * calling this function otherwise this call will fail. | 
|  | 3385 | */ | 
|  | 3386 | int regulator_set_current_limit(struct regulator *regulator, | 
|  | 3387 | int min_uA, int max_uA) | 
|  | 3388 | { | 
|  | 3389 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 3390 | int ret; | 
|  | 3391 |  | 
|  | 3392 | mutex_lock(&rdev->mutex); | 
|  | 3393 |  | 
|  | 3394 | /* sanity check */ | 
|  | 3395 | if (!rdev->desc->ops->set_current_limit) { | 
|  | 3396 | ret = -EINVAL; | 
|  | 3397 | goto out; | 
|  | 3398 | } | 
|  | 3399 |  | 
|  | 3400 | /* constraints check */ | 
|  | 3401 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); | 
|  | 3402 | if (ret < 0) | 
|  | 3403 | goto out; | 
|  | 3404 |  | 
|  | 3405 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); | 
|  | 3406 | out: | 
|  | 3407 | mutex_unlock(&rdev->mutex); | 
|  | 3408 | return ret; | 
|  | 3409 | } | 
|  | 3410 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); | 
|  | 3411 |  | 
|  | 3412 | static int _regulator_get_current_limit(struct regulator_dev *rdev) | 
|  | 3413 | { | 
|  | 3414 | int ret; | 
|  | 3415 |  | 
|  | 3416 | mutex_lock(&rdev->mutex); | 
|  | 3417 |  | 
|  | 3418 | /* sanity check */ | 
|  | 3419 | if (!rdev->desc->ops->get_current_limit) { | 
|  | 3420 | ret = -EINVAL; | 
|  | 3421 | goto out; | 
|  | 3422 | } | 
|  | 3423 |  | 
|  | 3424 | ret = rdev->desc->ops->get_current_limit(rdev); | 
|  | 3425 | out: | 
|  | 3426 | mutex_unlock(&rdev->mutex); | 
|  | 3427 | return ret; | 
|  | 3428 | } | 
|  | 3429 |  | 
|  | 3430 | /** | 
|  | 3431 | * regulator_get_current_limit - get regulator output current | 
|  | 3432 | * @regulator: regulator source | 
|  | 3433 | * | 
|  | 3434 | * This returns the current supplied by the specified current sink in uA. | 
|  | 3435 | * | 
|  | 3436 | * NOTE: If the regulator is disabled it will return the current value. This | 
|  | 3437 | * function should not be used to determine regulator state. | 
|  | 3438 | */ | 
|  | 3439 | int regulator_get_current_limit(struct regulator *regulator) | 
|  | 3440 | { | 
|  | 3441 | return _regulator_get_current_limit(regulator->rdev); | 
|  | 3442 | } | 
|  | 3443 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); | 
|  | 3444 |  | 
|  | 3445 | /** | 
|  | 3446 | * regulator_set_mode - set regulator operating mode | 
|  | 3447 | * @regulator: regulator source | 
|  | 3448 | * @mode: operating mode - one of the REGULATOR_MODE constants | 
|  | 3449 | * | 
|  | 3450 | * Set regulator operating mode to increase regulator efficiency or improve | 
|  | 3451 | * regulation performance. | 
|  | 3452 | * | 
|  | 3453 | * NOTE: Regulator system constraints must be set for this regulator before | 
|  | 3454 | * calling this function otherwise this call will fail. | 
|  | 3455 | */ | 
|  | 3456 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) | 
|  | 3457 | { | 
|  | 3458 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 3459 | int ret; | 
| Sundar R Iyer | 500b4ac | 2010-05-17 21:24:48 +0530 | [diff] [blame] | 3460 | int regulator_curr_mode; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3461 |  | 
|  | 3462 | mutex_lock(&rdev->mutex); | 
|  | 3463 |  | 
|  | 3464 | /* sanity check */ | 
|  | 3465 | if (!rdev->desc->ops->set_mode) { | 
|  | 3466 | ret = -EINVAL; | 
|  | 3467 | goto out; | 
|  | 3468 | } | 
|  | 3469 |  | 
| Sundar R Iyer | 500b4ac | 2010-05-17 21:24:48 +0530 | [diff] [blame] | 3470 | /* return if the same mode is requested */ | 
|  | 3471 | if (rdev->desc->ops->get_mode) { | 
|  | 3472 | regulator_curr_mode = rdev->desc->ops->get_mode(rdev); | 
|  | 3473 | if (regulator_curr_mode == mode) { | 
|  | 3474 | ret = 0; | 
|  | 3475 | goto out; | 
|  | 3476 | } | 
|  | 3477 | } | 
|  | 3478 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3479 | /* constraints check */ | 
| Axel Lin | 22c51b4 | 2011-04-01 18:25:25 +0800 | [diff] [blame] | 3480 | ret = regulator_mode_constrain(rdev, &mode); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3481 | if (ret < 0) | 
|  | 3482 | goto out; | 
|  | 3483 |  | 
|  | 3484 | ret = rdev->desc->ops->set_mode(rdev, mode); | 
|  | 3485 | out: | 
|  | 3486 | mutex_unlock(&rdev->mutex); | 
|  | 3487 | return ret; | 
|  | 3488 | } | 
|  | 3489 | EXPORT_SYMBOL_GPL(regulator_set_mode); | 
|  | 3490 |  | 
|  | 3491 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) | 
|  | 3492 | { | 
|  | 3493 | int ret; | 
|  | 3494 |  | 
|  | 3495 | mutex_lock(&rdev->mutex); | 
|  | 3496 |  | 
|  | 3497 | /* sanity check */ | 
|  | 3498 | if (!rdev->desc->ops->get_mode) { | 
|  | 3499 | ret = -EINVAL; | 
|  | 3500 | goto out; | 
|  | 3501 | } | 
|  | 3502 |  | 
|  | 3503 | ret = rdev->desc->ops->get_mode(rdev); | 
|  | 3504 | out: | 
|  | 3505 | mutex_unlock(&rdev->mutex); | 
|  | 3506 | return ret; | 
|  | 3507 | } | 
|  | 3508 |  | 
|  | 3509 | /** | 
|  | 3510 | * regulator_get_mode - get regulator operating mode | 
|  | 3511 | * @regulator: regulator source | 
|  | 3512 | * | 
|  | 3513 | * Get the current regulator operating mode. | 
|  | 3514 | */ | 
|  | 3515 | unsigned int regulator_get_mode(struct regulator *regulator) | 
|  | 3516 | { | 
|  | 3517 | return _regulator_get_mode(regulator->rdev); | 
|  | 3518 | } | 
|  | 3519 | EXPORT_SYMBOL_GPL(regulator_get_mode); | 
|  | 3520 |  | 
| Axel Haslam | 1b5b422 | 2016-11-03 12:11:42 +0100 | [diff] [blame] | 3521 | static int _regulator_get_error_flags(struct regulator_dev *rdev, | 
|  | 3522 | unsigned int *flags) | 
|  | 3523 | { | 
|  | 3524 | int ret; | 
|  | 3525 |  | 
|  | 3526 | mutex_lock(&rdev->mutex); | 
|  | 3527 |  | 
|  | 3528 | /* sanity check */ | 
|  | 3529 | if (!rdev->desc->ops->get_error_flags) { | 
|  | 3530 | ret = -EINVAL; | 
|  | 3531 | goto out; | 
|  | 3532 | } | 
|  | 3533 |  | 
|  | 3534 | ret = rdev->desc->ops->get_error_flags(rdev, flags); | 
|  | 3535 | out: | 
|  | 3536 | mutex_unlock(&rdev->mutex); | 
|  | 3537 | return ret; | 
|  | 3538 | } | 
|  | 3539 |  | 
|  | 3540 | /** | 
|  | 3541 | * regulator_get_error_flags - get regulator error information | 
|  | 3542 | * @regulator: regulator source | 
|  | 3543 | * @flags: pointer to store error flags | 
|  | 3544 | * | 
|  | 3545 | * Get the current regulator error information. | 
|  | 3546 | */ | 
|  | 3547 | int regulator_get_error_flags(struct regulator *regulator, | 
|  | 3548 | unsigned int *flags) | 
|  | 3549 | { | 
|  | 3550 | return _regulator_get_error_flags(regulator->rdev, flags); | 
|  | 3551 | } | 
|  | 3552 | EXPORT_SYMBOL_GPL(regulator_get_error_flags); | 
|  | 3553 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3554 | /** | 
| Bjorn Andersson | e39ce48 | 2015-02-11 19:35:27 -0800 | [diff] [blame] | 3555 | * regulator_set_load - set regulator load | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3556 | * @regulator: regulator source | 
|  | 3557 | * @uA_load: load current | 
|  | 3558 | * | 
|  | 3559 | * Notifies the regulator core of a new device load. This is then used by | 
|  | 3560 | * DRMS (if enabled by constraints) to set the most efficient regulator | 
|  | 3561 | * operating mode for the new regulator loading. | 
|  | 3562 | * | 
|  | 3563 | * Consumer devices notify their supply regulator of the maximum power | 
|  | 3564 | * they will require (can be taken from device datasheet in the power | 
|  | 3565 | * consumption tables) when they change operational status and hence power | 
|  | 3566 | * state. Examples of operational state changes that can affect power | 
|  | 3567 | * consumption are :- | 
|  | 3568 | * | 
|  | 3569 | *    o Device is opened / closed. | 
|  | 3570 | *    o Device I/O is about to begin or has just finished. | 
|  | 3571 | *    o Device is idling in between work. | 
|  | 3572 | * | 
|  | 3573 | * This information is also exported via sysfs to userspace. | 
|  | 3574 | * | 
|  | 3575 | * DRMS will sum the total requested load on the regulator and change | 
|  | 3576 | * to the most efficient operating mode if platform constraints allow. | 
|  | 3577 | * | 
| Bjorn Andersson | e39ce48 | 2015-02-11 19:35:27 -0800 | [diff] [blame] | 3578 | * On error a negative errno is returned. | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3579 | */ | 
| Bjorn Andersson | e39ce48 | 2015-02-11 19:35:27 -0800 | [diff] [blame] | 3580 | int regulator_set_load(struct regulator *regulator, int uA_load) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3581 | { | 
|  | 3582 | struct regulator_dev *rdev = regulator->rdev; | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 3583 | int ret; | 
| Stephen Boyd | d92d95b6 | 2012-07-02 19:21:06 -0700 | [diff] [blame] | 3584 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3585 | mutex_lock(&rdev->mutex); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3586 | regulator->uA_load = uA_load; | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 3587 | ret = drms_uA_update(rdev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3588 | mutex_unlock(&rdev->mutex); | 
| Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 3589 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3590 | return ret; | 
|  | 3591 | } | 
| Bjorn Andersson | e39ce48 | 2015-02-11 19:35:27 -0800 | [diff] [blame] | 3592 | EXPORT_SYMBOL_GPL(regulator_set_load); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3593 |  | 
|  | 3594 | /** | 
| Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3595 | * regulator_allow_bypass - allow the regulator to go into bypass mode | 
|  | 3596 | * | 
|  | 3597 | * @regulator: Regulator to configure | 
| Nishanth Menon | 9345dfb | 2013-02-28 18:44:54 -0600 | [diff] [blame] | 3598 | * @enable: enable or disable bypass mode | 
| Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3599 | * | 
|  | 3600 | * Allow the regulator to go into bypass mode if all other consumers | 
|  | 3601 | * for the regulator also enable bypass mode and the machine | 
|  | 3602 | * constraints allow this.  Bypass mode means that the regulator is | 
|  | 3603 | * simply passing the input directly to the output with no regulation. | 
|  | 3604 | */ | 
|  | 3605 | int regulator_allow_bypass(struct regulator *regulator, bool enable) | 
|  | 3606 | { | 
|  | 3607 | struct regulator_dev *rdev = regulator->rdev; | 
|  | 3608 | int ret = 0; | 
|  | 3609 |  | 
|  | 3610 | if (!rdev->desc->ops->set_bypass) | 
|  | 3611 | return 0; | 
|  | 3612 |  | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 3613 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS)) | 
| Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3614 | return 0; | 
|  | 3615 |  | 
|  | 3616 | mutex_lock(&rdev->mutex); | 
|  | 3617 |  | 
|  | 3618 | if (enable && !regulator->bypass) { | 
|  | 3619 | rdev->bypass_count++; | 
|  | 3620 |  | 
|  | 3621 | if (rdev->bypass_count == rdev->open_count) { | 
|  | 3622 | ret = rdev->desc->ops->set_bypass(rdev, enable); | 
|  | 3623 | if (ret != 0) | 
|  | 3624 | rdev->bypass_count--; | 
|  | 3625 | } | 
|  | 3626 |  | 
|  | 3627 | } else if (!enable && regulator->bypass) { | 
|  | 3628 | rdev->bypass_count--; | 
|  | 3629 |  | 
|  | 3630 | if (rdev->bypass_count != rdev->open_count) { | 
|  | 3631 | ret = rdev->desc->ops->set_bypass(rdev, enable); | 
|  | 3632 | if (ret != 0) | 
|  | 3633 | rdev->bypass_count++; | 
|  | 3634 | } | 
|  | 3635 | } | 
|  | 3636 |  | 
|  | 3637 | if (ret == 0) | 
|  | 3638 | regulator->bypass = enable; | 
|  | 3639 |  | 
|  | 3640 | mutex_unlock(&rdev->mutex); | 
|  | 3641 |  | 
|  | 3642 | return ret; | 
|  | 3643 | } | 
|  | 3644 | EXPORT_SYMBOL_GPL(regulator_allow_bypass); | 
|  | 3645 |  | 
|  | 3646 | /** | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3647 | * regulator_register_notifier - register regulator event notifier | 
|  | 3648 | * @regulator: regulator source | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3649 | * @nb: notifier block | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3650 | * | 
|  | 3651 | * Register notifier block to receive regulator events. | 
|  | 3652 | */ | 
|  | 3653 | int regulator_register_notifier(struct regulator *regulator, | 
|  | 3654 | struct notifier_block *nb) | 
|  | 3655 | { | 
|  | 3656 | return blocking_notifier_chain_register(®ulator->rdev->notifier, | 
|  | 3657 | nb); | 
|  | 3658 | } | 
|  | 3659 | EXPORT_SYMBOL_GPL(regulator_register_notifier); | 
|  | 3660 |  | 
|  | 3661 | /** | 
|  | 3662 | * regulator_unregister_notifier - unregister regulator event notifier | 
|  | 3663 | * @regulator: regulator source | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3664 | * @nb: notifier block | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3665 | * | 
|  | 3666 | * Unregister regulator event notifier block. | 
|  | 3667 | */ | 
|  | 3668 | int regulator_unregister_notifier(struct regulator *regulator, | 
|  | 3669 | struct notifier_block *nb) | 
|  | 3670 | { | 
|  | 3671 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, | 
|  | 3672 | nb); | 
|  | 3673 | } | 
|  | 3674 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); | 
|  | 3675 |  | 
| Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 3676 | /* notify regulator consumers and downstream regulator consumers. | 
|  | 3677 | * Note mutex must be held by caller. | 
|  | 3678 | */ | 
| Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 3679 | static int _notifier_call_chain(struct regulator_dev *rdev, | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3680 | unsigned long event, void *data) | 
|  | 3681 | { | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3682 | /* call rdev chain first */ | 
| Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 3683 | return blocking_notifier_call_chain(&rdev->notifier, event, data); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3684 | } | 
|  | 3685 |  | 
|  | 3686 | /** | 
|  | 3687 | * regulator_bulk_get - get multiple regulator consumers | 
|  | 3688 | * | 
|  | 3689 | * @dev:           Device to supply | 
|  | 3690 | * @num_consumers: Number of consumers to register | 
|  | 3691 | * @consumers:     Configuration of consumers; clients are stored here. | 
|  | 3692 | * | 
|  | 3693 | * @return 0 on success, an errno on failure. | 
|  | 3694 | * | 
|  | 3695 | * This helper function allows drivers to get several regulator | 
|  | 3696 | * consumers in one operation.  If any of the regulators cannot be | 
|  | 3697 | * acquired then any regulators that were allocated will be freed | 
|  | 3698 | * before returning to the caller. | 
|  | 3699 | */ | 
|  | 3700 | int regulator_bulk_get(struct device *dev, int num_consumers, | 
|  | 3701 | struct regulator_bulk_data *consumers) | 
|  | 3702 | { | 
|  | 3703 | int i; | 
|  | 3704 | int ret; | 
|  | 3705 |  | 
|  | 3706 | for (i = 0; i < num_consumers; i++) | 
|  | 3707 | consumers[i].consumer = NULL; | 
|  | 3708 |  | 
|  | 3709 | for (i = 0; i < num_consumers; i++) { | 
| Bjorn Andersson | 565f9b0 | 2016-08-16 11:50:32 -0700 | [diff] [blame] | 3710 | consumers[i].consumer = regulator_get(dev, | 
|  | 3711 | consumers[i].supply); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3712 | if (IS_ERR(consumers[i].consumer)) { | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3713 | ret = PTR_ERR(consumers[i].consumer); | 
| Mark Brown | 5b30762 | 2009-10-13 13:06:49 +0100 | [diff] [blame] | 3714 | dev_err(dev, "Failed to get supply '%s': %d\n", | 
|  | 3715 | consumers[i].supply, ret); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3716 | consumers[i].consumer = NULL; | 
|  | 3717 | goto err; | 
|  | 3718 | } | 
|  | 3719 | } | 
|  | 3720 |  | 
|  | 3721 | return 0; | 
|  | 3722 |  | 
|  | 3723 | err: | 
| Axel Lin | b29c769 | 2012-02-20 10:32:16 +0800 | [diff] [blame] | 3724 | while (--i >= 0) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3725 | regulator_put(consumers[i].consumer); | 
|  | 3726 |  | 
|  | 3727 | return ret; | 
|  | 3728 | } | 
|  | 3729 | EXPORT_SYMBOL_GPL(regulator_bulk_get); | 
|  | 3730 |  | 
| Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3731 | static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) | 
|  | 3732 | { | 
|  | 3733 | struct regulator_bulk_data *bulk = data; | 
|  | 3734 |  | 
|  | 3735 | bulk->ret = regulator_enable(bulk->consumer); | 
|  | 3736 | } | 
|  | 3737 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3738 | /** | 
|  | 3739 | * regulator_bulk_enable - enable multiple regulator consumers | 
|  | 3740 | * | 
|  | 3741 | * @num_consumers: Number of consumers | 
|  | 3742 | * @consumers:     Consumer data; clients are stored here. | 
|  | 3743 | * @return         0 on success, an errno on failure | 
|  | 3744 | * | 
|  | 3745 | * This convenience API allows consumers to enable multiple regulator | 
|  | 3746 | * clients in a single API call.  If any consumers cannot be enabled | 
|  | 3747 | * then any others that were enabled will be disabled again prior to | 
|  | 3748 | * return. | 
|  | 3749 | */ | 
|  | 3750 | int regulator_bulk_enable(int num_consumers, | 
|  | 3751 | struct regulator_bulk_data *consumers) | 
|  | 3752 | { | 
| Dan Williams | 2955b47 | 2012-07-09 19:33:25 -0700 | [diff] [blame] | 3753 | ASYNC_DOMAIN_EXCLUSIVE(async_domain); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3754 | int i; | 
| Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3755 | int ret = 0; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3756 |  | 
| Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 3757 | for (i = 0; i < num_consumers; i++) { | 
|  | 3758 | if (consumers[i].consumer->always_on) | 
|  | 3759 | consumers[i].ret = 0; | 
|  | 3760 | else | 
|  | 3761 | async_schedule_domain(regulator_bulk_enable_async, | 
|  | 3762 | &consumers[i], &async_domain); | 
|  | 3763 | } | 
| Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3764 |  | 
|  | 3765 | async_synchronize_full_domain(&async_domain); | 
|  | 3766 |  | 
|  | 3767 | /* If any consumer failed we need to unwind any that succeeded */ | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3768 | for (i = 0; i < num_consumers; i++) { | 
| Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3769 | if (consumers[i].ret != 0) { | 
|  | 3770 | ret = consumers[i].ret; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3771 | goto err; | 
| Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3772 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3773 | } | 
|  | 3774 |  | 
|  | 3775 | return 0; | 
|  | 3776 |  | 
|  | 3777 | err: | 
| Andrzej Hajda | fbe3105 | 2013-03-01 12:24:05 +0100 | [diff] [blame] | 3778 | for (i = 0; i < num_consumers; i++) { | 
|  | 3779 | if (consumers[i].ret < 0) | 
|  | 3780 | pr_err("Failed to enable %s: %d\n", consumers[i].supply, | 
|  | 3781 | consumers[i].ret); | 
|  | 3782 | else | 
|  | 3783 | regulator_disable(consumers[i].consumer); | 
|  | 3784 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3785 |  | 
|  | 3786 | return ret; | 
|  | 3787 | } | 
|  | 3788 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); | 
|  | 3789 |  | 
|  | 3790 | /** | 
|  | 3791 | * regulator_bulk_disable - disable multiple regulator consumers | 
|  | 3792 | * | 
|  | 3793 | * @num_consumers: Number of consumers | 
|  | 3794 | * @consumers:     Consumer data; clients are stored here. | 
|  | 3795 | * @return         0 on success, an errno on failure | 
|  | 3796 | * | 
|  | 3797 | * This convenience API allows consumers to disable multiple regulator | 
| Sylwester Nawrocki | 49e2263 | 2012-01-25 12:35:38 +0100 | [diff] [blame] | 3798 | * clients in a single API call.  If any consumers cannot be disabled | 
|  | 3799 | * then any others that were disabled will be enabled again prior to | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3800 | * return. | 
|  | 3801 | */ | 
|  | 3802 | int regulator_bulk_disable(int num_consumers, | 
|  | 3803 | struct regulator_bulk_data *consumers) | 
|  | 3804 | { | 
|  | 3805 | int i; | 
| Mark Brown | 01e86f4 | 2012-03-28 21:36:38 +0100 | [diff] [blame] | 3806 | int ret, r; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3807 |  | 
| Sylwester Nawrocki | 49e2263 | 2012-01-25 12:35:38 +0100 | [diff] [blame] | 3808 | for (i = num_consumers - 1; i >= 0; --i) { | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3809 | ret = regulator_disable(consumers[i].consumer); | 
|  | 3810 | if (ret != 0) | 
|  | 3811 | goto err; | 
|  | 3812 | } | 
|  | 3813 |  | 
|  | 3814 | return 0; | 
|  | 3815 |  | 
|  | 3816 | err: | 
| Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 3817 | pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); | 
| Mark Brown | 01e86f4 | 2012-03-28 21:36:38 +0100 | [diff] [blame] | 3818 | for (++i; i < num_consumers; ++i) { | 
|  | 3819 | r = regulator_enable(consumers[i].consumer); | 
|  | 3820 | if (r != 0) | 
| Dmitry Torokhov | d1642ea | 2017-02-03 15:16:16 -0800 | [diff] [blame] | 3821 | pr_err("Failed to re-enable %s: %d\n", | 
| Mark Brown | 01e86f4 | 2012-03-28 21:36:38 +0100 | [diff] [blame] | 3822 | consumers[i].supply, r); | 
|  | 3823 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3824 |  | 
|  | 3825 | return ret; | 
|  | 3826 | } | 
|  | 3827 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); | 
|  | 3828 |  | 
|  | 3829 | /** | 
| Donggeun Kim | e1de2f4 | 2012-01-03 16:22:03 +0900 | [diff] [blame] | 3830 | * regulator_bulk_force_disable - force disable multiple regulator consumers | 
|  | 3831 | * | 
|  | 3832 | * @num_consumers: Number of consumers | 
|  | 3833 | * @consumers:     Consumer data; clients are stored here. | 
|  | 3834 | * @return         0 on success, an errno on failure | 
|  | 3835 | * | 
|  | 3836 | * This convenience API allows consumers to forcibly disable multiple regulator | 
|  | 3837 | * clients in a single API call. | 
|  | 3838 | * NOTE: This should be used for situations when device damage will | 
|  | 3839 | * likely occur if the regulators are not disabled (e.g. over temp). | 
|  | 3840 | * Although regulator_force_disable function call for some consumers can | 
|  | 3841 | * return error numbers, the function is called for all consumers. | 
|  | 3842 | */ | 
|  | 3843 | int regulator_bulk_force_disable(int num_consumers, | 
|  | 3844 | struct regulator_bulk_data *consumers) | 
|  | 3845 | { | 
|  | 3846 | int i; | 
| Dmitry Torokhov | b8c77ff | 2017-02-03 15:16:17 -0800 | [diff] [blame] | 3847 | int ret = 0; | 
| Donggeun Kim | e1de2f4 | 2012-01-03 16:22:03 +0900 | [diff] [blame] | 3848 |  | 
| Dmitry Torokhov | b8c77ff | 2017-02-03 15:16:17 -0800 | [diff] [blame] | 3849 | for (i = 0; i < num_consumers; i++) { | 
| Donggeun Kim | e1de2f4 | 2012-01-03 16:22:03 +0900 | [diff] [blame] | 3850 | consumers[i].ret = | 
|  | 3851 | regulator_force_disable(consumers[i].consumer); | 
|  | 3852 |  | 
| Dmitry Torokhov | b8c77ff | 2017-02-03 15:16:17 -0800 | [diff] [blame] | 3853 | /* Store first error for reporting */ | 
|  | 3854 | if (consumers[i].ret && !ret) | 
| Donggeun Kim | e1de2f4 | 2012-01-03 16:22:03 +0900 | [diff] [blame] | 3855 | ret = consumers[i].ret; | 
| Donggeun Kim | e1de2f4 | 2012-01-03 16:22:03 +0900 | [diff] [blame] | 3856 | } | 
|  | 3857 |  | 
| Donggeun Kim | e1de2f4 | 2012-01-03 16:22:03 +0900 | [diff] [blame] | 3858 | return ret; | 
|  | 3859 | } | 
|  | 3860 | EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); | 
|  | 3861 |  | 
|  | 3862 | /** | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3863 | * regulator_bulk_free - free multiple regulator consumers | 
|  | 3864 | * | 
|  | 3865 | * @num_consumers: Number of consumers | 
|  | 3866 | * @consumers:     Consumer data; clients are stored here. | 
|  | 3867 | * | 
|  | 3868 | * This convenience API allows consumers to free multiple regulator | 
|  | 3869 | * clients in a single API call. | 
|  | 3870 | */ | 
|  | 3871 | void regulator_bulk_free(int num_consumers, | 
|  | 3872 | struct regulator_bulk_data *consumers) | 
|  | 3873 | { | 
|  | 3874 | int i; | 
|  | 3875 |  | 
|  | 3876 | for (i = 0; i < num_consumers; i++) { | 
|  | 3877 | regulator_put(consumers[i].consumer); | 
|  | 3878 | consumers[i].consumer = NULL; | 
|  | 3879 | } | 
|  | 3880 | } | 
|  | 3881 | EXPORT_SYMBOL_GPL(regulator_bulk_free); | 
|  | 3882 |  | 
|  | 3883 | /** | 
|  | 3884 | * regulator_notifier_call_chain - call regulator event notifier | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3885 | * @rdev: regulator source | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3886 | * @event: notifier block | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3887 | * @data: callback-specific data. | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3888 | * | 
|  | 3889 | * Called by regulator drivers to notify clients a regulator event has | 
|  | 3890 | * occurred. We also notify regulator clients downstream. | 
| Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 3891 | * Note lock must be held by caller. | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3892 | */ | 
|  | 3893 | int regulator_notifier_call_chain(struct regulator_dev *rdev, | 
|  | 3894 | unsigned long event, void *data) | 
|  | 3895 | { | 
| Krzysztof Kozlowski | 70cfef2 | 2015-06-29 09:04:43 +0900 | [diff] [blame] | 3896 | lockdep_assert_held_once(&rdev->mutex); | 
|  | 3897 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3898 | _notifier_call_chain(rdev, event, data); | 
|  | 3899 | return NOTIFY_DONE; | 
|  | 3900 |  | 
|  | 3901 | } | 
|  | 3902 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); | 
|  | 3903 |  | 
| Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 3904 | /** | 
|  | 3905 | * regulator_mode_to_status - convert a regulator mode into a status | 
|  | 3906 | * | 
|  | 3907 | * @mode: Mode to convert | 
|  | 3908 | * | 
|  | 3909 | * Convert a regulator mode into a status. | 
|  | 3910 | */ | 
|  | 3911 | int regulator_mode_to_status(unsigned int mode) | 
|  | 3912 | { | 
|  | 3913 | switch (mode) { | 
|  | 3914 | case REGULATOR_MODE_FAST: | 
|  | 3915 | return REGULATOR_STATUS_FAST; | 
|  | 3916 | case REGULATOR_MODE_NORMAL: | 
|  | 3917 | return REGULATOR_STATUS_NORMAL; | 
|  | 3918 | case REGULATOR_MODE_IDLE: | 
|  | 3919 | return REGULATOR_STATUS_IDLE; | 
| Krystian Garbaciak | 03ffcf3 | 2012-07-12 11:50:38 +0100 | [diff] [blame] | 3920 | case REGULATOR_MODE_STANDBY: | 
| Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 3921 | return REGULATOR_STATUS_STANDBY; | 
|  | 3922 | default: | 
| Krystian Garbaciak | 1beaf76 | 2012-07-12 13:53:35 +0100 | [diff] [blame] | 3923 | return REGULATOR_STATUS_UNDEFINED; | 
| Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 3924 | } | 
|  | 3925 | } | 
|  | 3926 | EXPORT_SYMBOL_GPL(regulator_mode_to_status); | 
|  | 3927 |  | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3928 | static struct attribute *regulator_dev_attrs[] = { | 
|  | 3929 | &dev_attr_name.attr, | 
|  | 3930 | &dev_attr_num_users.attr, | 
|  | 3931 | &dev_attr_type.attr, | 
|  | 3932 | &dev_attr_microvolts.attr, | 
|  | 3933 | &dev_attr_microamps.attr, | 
|  | 3934 | &dev_attr_opmode.attr, | 
|  | 3935 | &dev_attr_state.attr, | 
|  | 3936 | &dev_attr_status.attr, | 
|  | 3937 | &dev_attr_bypass.attr, | 
|  | 3938 | &dev_attr_requested_microamps.attr, | 
|  | 3939 | &dev_attr_min_microvolts.attr, | 
|  | 3940 | &dev_attr_max_microvolts.attr, | 
|  | 3941 | &dev_attr_min_microamps.attr, | 
|  | 3942 | &dev_attr_max_microamps.attr, | 
|  | 3943 | &dev_attr_suspend_standby_state.attr, | 
|  | 3944 | &dev_attr_suspend_mem_state.attr, | 
|  | 3945 | &dev_attr_suspend_disk_state.attr, | 
|  | 3946 | &dev_attr_suspend_standby_microvolts.attr, | 
|  | 3947 | &dev_attr_suspend_mem_microvolts.attr, | 
|  | 3948 | &dev_attr_suspend_disk_microvolts.attr, | 
|  | 3949 | &dev_attr_suspend_standby_mode.attr, | 
|  | 3950 | &dev_attr_suspend_mem_mode.attr, | 
|  | 3951 | &dev_attr_suspend_disk_mode.attr, | 
|  | 3952 | NULL | 
|  | 3953 | }; | 
|  | 3954 |  | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3955 | /* | 
|  | 3956 | * To avoid cluttering sysfs (and memory) with useless state, only | 
|  | 3957 | * create attributes that can be meaningfully displayed. | 
|  | 3958 | */ | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3959 | static umode_t regulator_attr_is_visible(struct kobject *kobj, | 
|  | 3960 | struct attribute *attr, int idx) | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3961 | { | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3962 | struct device *dev = kobj_to_dev(kobj); | 
| Geliang Tang | 83080a1 | 2016-01-05 22:07:55 +0800 | [diff] [blame] | 3963 | struct regulator_dev *rdev = dev_to_rdev(dev); | 
| Guodong Xu | 272e231 | 2014-08-13 19:33:38 +0800 | [diff] [blame] | 3964 | const struct regulator_ops *ops = rdev->desc->ops; | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3965 | umode_t mode = attr->mode; | 
|  | 3966 |  | 
|  | 3967 | /* these three are always present */ | 
|  | 3968 | if (attr == &dev_attr_name.attr || | 
|  | 3969 | attr == &dev_attr_num_users.attr || | 
|  | 3970 | attr == &dev_attr_type.attr) | 
|  | 3971 | return mode; | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3972 |  | 
|  | 3973 | /* some attributes need specific methods to be displayed */ | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3974 | if (attr == &dev_attr_microvolts.attr) { | 
|  | 3975 | if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || | 
|  | 3976 | (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || | 
|  | 3977 | (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || | 
|  | 3978 | (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1)) | 
|  | 3979 | return mode; | 
|  | 3980 | return 0; | 
| Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3981 | } | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3982 |  | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3983 | if (attr == &dev_attr_microamps.attr) | 
|  | 3984 | return ops->get_current_limit ? mode : 0; | 
|  | 3985 |  | 
|  | 3986 | if (attr == &dev_attr_opmode.attr) | 
|  | 3987 | return ops->get_mode ? mode : 0; | 
|  | 3988 |  | 
|  | 3989 | if (attr == &dev_attr_state.attr) | 
|  | 3990 | return (rdev->ena_pin || ops->is_enabled) ? mode : 0; | 
|  | 3991 |  | 
|  | 3992 | if (attr == &dev_attr_status.attr) | 
|  | 3993 | return ops->get_status ? mode : 0; | 
|  | 3994 |  | 
|  | 3995 | if (attr == &dev_attr_bypass.attr) | 
|  | 3996 | return ops->get_bypass ? mode : 0; | 
|  | 3997 |  | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3998 | /* some attributes are type-specific */ | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3999 | if (attr == &dev_attr_requested_microamps.attr) | 
|  | 4000 | return rdev->desc->type == REGULATOR_CURRENT ? mode : 0; | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 4001 |  | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 4002 | /* constraints need specific supporting methods */ | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 4003 | if (attr == &dev_attr_min_microvolts.attr || | 
|  | 4004 | attr == &dev_attr_max_microvolts.attr) | 
|  | 4005 | return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0; | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 4006 |  | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 4007 | if (attr == &dev_attr_min_microamps.attr || | 
|  | 4008 | attr == &dev_attr_max_microamps.attr) | 
|  | 4009 | return ops->set_current_limit ? mode : 0; | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 4010 |  | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 4011 | if (attr == &dev_attr_suspend_standby_state.attr || | 
|  | 4012 | attr == &dev_attr_suspend_mem_state.attr || | 
|  | 4013 | attr == &dev_attr_suspend_disk_state.attr) | 
|  | 4014 | return mode; | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 4015 |  | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 4016 | if (attr == &dev_attr_suspend_standby_microvolts.attr || | 
|  | 4017 | attr == &dev_attr_suspend_mem_microvolts.attr || | 
|  | 4018 | attr == &dev_attr_suspend_disk_microvolts.attr) | 
|  | 4019 | return ops->set_suspend_voltage ? mode : 0; | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 4020 |  | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 4021 | if (attr == &dev_attr_suspend_standby_mode.attr || | 
|  | 4022 | attr == &dev_attr_suspend_mem_mode.attr || | 
|  | 4023 | attr == &dev_attr_suspend_disk_mode.attr) | 
|  | 4024 | return ops->set_suspend_mode ? mode : 0; | 
|  | 4025 |  | 
|  | 4026 | return mode; | 
| David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 4027 | } | 
|  | 4028 |  | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 4029 | static const struct attribute_group regulator_dev_group = { | 
|  | 4030 | .attrs = regulator_dev_attrs, | 
|  | 4031 | .is_visible = regulator_attr_is_visible, | 
|  | 4032 | }; | 
|  | 4033 |  | 
|  | 4034 | static const struct attribute_group *regulator_dev_groups[] = { | 
|  | 4035 | ®ulator_dev_group, | 
|  | 4036 | NULL | 
|  | 4037 | }; | 
|  | 4038 |  | 
|  | 4039 | static void regulator_dev_release(struct device *dev) | 
|  | 4040 | { | 
|  | 4041 | struct regulator_dev *rdev = dev_get_drvdata(dev); | 
| Mark Brown | 29f5f48 | 2015-08-07 20:01:32 +0100 | [diff] [blame] | 4042 |  | 
|  | 4043 | kfree(rdev->constraints); | 
|  | 4044 | of_node_put(rdev->dev.of_node); | 
| Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 4045 | kfree(rdev); | 
|  | 4046 | } | 
|  | 4047 |  | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4048 | static void rdev_init_debugfs(struct regulator_dev *rdev) | 
|  | 4049 | { | 
| Guenter Roeck | a9eaa81 | 2014-10-17 08:17:03 -0700 | [diff] [blame] | 4050 | struct device *parent = rdev->dev.parent; | 
|  | 4051 | const char *rname = rdev_get_name(rdev); | 
|  | 4052 | char name[NAME_MAX]; | 
|  | 4053 |  | 
|  | 4054 | /* Avoid duplicate debugfs directory names */ | 
|  | 4055 | if (parent && rname == rdev->desc->name) { | 
|  | 4056 | snprintf(name, sizeof(name), "%s-%s", dev_name(parent), | 
|  | 4057 | rname); | 
|  | 4058 | rname = name; | 
|  | 4059 | } | 
|  | 4060 |  | 
|  | 4061 | rdev->debugfs = debugfs_create_dir(rname, debugfs_root); | 
| Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4062 | if (!rdev->debugfs) { | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4063 | rdev_warn(rdev, "Failed to create debugfs directory\n"); | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4064 | return; | 
|  | 4065 | } | 
|  | 4066 |  | 
|  | 4067 | debugfs_create_u32("use_count", 0444, rdev->debugfs, | 
|  | 4068 | &rdev->use_count); | 
|  | 4069 | debugfs_create_u32("open_count", 0444, rdev->debugfs, | 
|  | 4070 | &rdev->open_count); | 
| Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 4071 | debugfs_create_u32("bypass_count", 0444, rdev->debugfs, | 
|  | 4072 | &rdev->bypass_count); | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4073 | } | 
|  | 4074 |  | 
| Javier Martinez Canillas | 5e3ca2b | 2016-03-23 20:59:34 -0300 | [diff] [blame] | 4075 | static int regulator_register_resolve_supply(struct device *dev, void *data) | 
|  | 4076 | { | 
| Jon Hunter | 7ddede6 | 2016-04-21 17:11:57 +0100 | [diff] [blame] | 4077 | struct regulator_dev *rdev = dev_to_rdev(dev); | 
|  | 4078 |  | 
|  | 4079 | if (regulator_resolve_supply(rdev)) | 
|  | 4080 | rdev_dbg(rdev, "unable to resolve supply\n"); | 
|  | 4081 |  | 
|  | 4082 | return 0; | 
| Javier Martinez Canillas | 5e3ca2b | 2016-03-23 20:59:34 -0300 | [diff] [blame] | 4083 | } | 
|  | 4084 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4085 | /** | 
|  | 4086 | * regulator_register - register regulator | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 4087 | * @regulator_desc: regulator to register | 
| Krzysztof Kozlowski | f47531b | 2015-01-12 09:01:39 +0100 | [diff] [blame] | 4088 | * @cfg: runtime configuration for regulator | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4089 | * | 
|  | 4090 | * Called by regulator drivers to register a regulator. | 
| Axel Lin | 0384618 | 2013-01-03 21:01:47 +0800 | [diff] [blame] | 4091 | * Returns a valid pointer to struct regulator_dev on success | 
|  | 4092 | * or an ERR_PTR() on error. | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4093 | */ | 
| Mark Brown | 65f2684 | 2012-04-03 20:46:53 +0100 | [diff] [blame] | 4094 | struct regulator_dev * | 
|  | 4095 | regulator_register(const struct regulator_desc *regulator_desc, | 
| Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4096 | const struct regulator_config *cfg) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4097 | { | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4098 | const struct regulation_constraints *constraints = NULL; | 
| Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 4099 | const struct regulator_init_data *init_data; | 
| Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4100 | struct regulator_config *config = NULL; | 
| Aniroop Mathur | 72dca06 | 2014-12-28 22:08:38 +0530 | [diff] [blame] | 4101 | static atomic_t regulator_no = ATOMIC_INIT(-1); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4102 | struct regulator_dev *rdev; | 
| Mark Brown | 32c8fad | 2012-04-11 10:19:12 +0100 | [diff] [blame] | 4103 | struct device *dev; | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4104 | int ret, i; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4105 |  | 
| Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4106 | if (regulator_desc == NULL || cfg == NULL) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4107 | return ERR_PTR(-EINVAL); | 
|  | 4108 |  | 
| Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4109 | dev = cfg->dev; | 
| Mark Brown | dcf7011 | 2012-05-08 18:09:12 +0100 | [diff] [blame] | 4110 | WARN_ON(!dev); | 
| Mark Brown | 32c8fad | 2012-04-11 10:19:12 +0100 | [diff] [blame] | 4111 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4112 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) | 
|  | 4113 | return ERR_PTR(-EINVAL); | 
|  | 4114 |  | 
| Diego Liziero | cd78dfc | 2009-04-14 03:04:47 +0200 | [diff] [blame] | 4115 | if (regulator_desc->type != REGULATOR_VOLTAGE && | 
|  | 4116 | regulator_desc->type != REGULATOR_CURRENT) | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4117 | return ERR_PTR(-EINVAL); | 
|  | 4118 |  | 
| Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 4119 | /* Only one of each should be implemented */ | 
|  | 4120 | WARN_ON(regulator_desc->ops->get_voltage && | 
|  | 4121 | regulator_desc->ops->get_voltage_sel); | 
| Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 4122 | WARN_ON(regulator_desc->ops->set_voltage && | 
|  | 4123 | regulator_desc->ops->set_voltage_sel); | 
| Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 4124 |  | 
|  | 4125 | /* If we're using selectors we must implement list_voltage. */ | 
|  | 4126 | if (regulator_desc->ops->get_voltage_sel && | 
|  | 4127 | !regulator_desc->ops->list_voltage) { | 
|  | 4128 | return ERR_PTR(-EINVAL); | 
|  | 4129 | } | 
| Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 4130 | if (regulator_desc->ops->set_voltage_sel && | 
|  | 4131 | !regulator_desc->ops->list_voltage) { | 
|  | 4132 | return ERR_PTR(-EINVAL); | 
|  | 4133 | } | 
| Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 4134 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4135 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); | 
|  | 4136 | if (rdev == NULL) | 
|  | 4137 | return ERR_PTR(-ENOMEM); | 
|  | 4138 |  | 
| Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4139 | /* | 
|  | 4140 | * Duplicate the config so the driver could override it after | 
|  | 4141 | * parsing init data. | 
|  | 4142 | */ | 
|  | 4143 | config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); | 
|  | 4144 | if (config == NULL) { | 
|  | 4145 | kfree(rdev); | 
|  | 4146 | return ERR_PTR(-ENOMEM); | 
|  | 4147 | } | 
|  | 4148 |  | 
| Krzysztof Kozlowski | bfa21a0 | 2015-01-05 12:48:42 +0100 | [diff] [blame] | 4149 | init_data = regulator_of_get_init_data(dev, regulator_desc, config, | 
| Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 4150 | &rdev->dev.of_node); | 
|  | 4151 | if (!init_data) { | 
|  | 4152 | init_data = config->init_data; | 
|  | 4153 | rdev->dev.of_node = of_node_get(config->of_node); | 
|  | 4154 | } | 
|  | 4155 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4156 | mutex_init(&rdev->mutex); | 
| Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 4157 | rdev->reg_data = config->driver_data; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4158 | rdev->owner = regulator_desc->owner; | 
|  | 4159 | rdev->desc = regulator_desc; | 
| Mark Brown | 3a4b0a0 | 2012-05-08 18:10:45 +0100 | [diff] [blame] | 4160 | if (config->regmap) | 
|  | 4161 | rdev->regmap = config->regmap; | 
| AnilKumar Ch | 52b84da | 2012-09-07 20:45:05 +0530 | [diff] [blame] | 4162 | else if (dev_get_regmap(dev, NULL)) | 
| Mark Brown | 3a4b0a0 | 2012-05-08 18:10:45 +0100 | [diff] [blame] | 4163 | rdev->regmap = dev_get_regmap(dev, NULL); | 
| AnilKumar Ch | 52b84da | 2012-09-07 20:45:05 +0530 | [diff] [blame] | 4164 | else if (dev->parent) | 
|  | 4165 | rdev->regmap = dev_get_regmap(dev->parent, NULL); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4166 | INIT_LIST_HEAD(&rdev->consumer_list); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4167 | INIT_LIST_HEAD(&rdev->list); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4168 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); | 
| Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 4169 | INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4170 |  | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4171 | /* preform any regulator specific init */ | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4172 | if (init_data && init_data->regulator_init) { | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4173 | ret = init_data->regulator_init(rdev->reg_data); | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 4174 | if (ret < 0) | 
|  | 4175 | goto clean; | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4176 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4177 |  | 
| Krzysztof Adamski | daad134 | 2016-02-22 09:24:00 +0100 | [diff] [blame] | 4178 | if ((config->ena_gpio || config->ena_gpio_initialized) && | 
|  | 4179 | gpio_is_valid(config->ena_gpio)) { | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4180 | mutex_lock(®ulator_list_mutex); | 
| Krzysztof Adamski | daad134 | 2016-02-22 09:24:00 +0100 | [diff] [blame] | 4181 | ret = regulator_ena_gpio_request(rdev, config); | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4182 | mutex_unlock(®ulator_list_mutex); | 
| Krzysztof Adamski | daad134 | 2016-02-22 09:24:00 +0100 | [diff] [blame] | 4183 | if (ret != 0) { | 
|  | 4184 | rdev_err(rdev, "Failed to request enable GPIO%d: %d\n", | 
|  | 4185 | config->ena_gpio, ret); | 
| Krzysztof Adamski | 3216523 | 2016-02-24 11:52:50 +0100 | [diff] [blame] | 4186 | goto clean; | 
| Krzysztof Adamski | daad134 | 2016-02-22 09:24:00 +0100 | [diff] [blame] | 4187 | } | 
|  | 4188 | } | 
|  | 4189 |  | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4190 | /* register with sysfs */ | 
|  | 4191 | rdev->dev.class = ®ulator_class; | 
|  | 4192 | rdev->dev.parent = dev; | 
| Aniroop Mathur | 72dca06 | 2014-12-28 22:08:38 +0530 | [diff] [blame] | 4193 | dev_set_name(&rdev->dev, "regulator.%lu", | 
| Aniroop Mathur | 3913881 | 2014-12-29 22:36:48 +0530 | [diff] [blame] | 4194 | (unsigned long) atomic_inc_return(®ulator_no)); | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4195 |  | 
| Mike Rapoport | 74f544c | 2008-11-25 14:53:53 +0200 | [diff] [blame] | 4196 | /* set regulator constraints */ | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4197 | if (init_data) | 
|  | 4198 | constraints = &init_data->constraints; | 
|  | 4199 |  | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4200 | if (init_data && init_data->supply_regulator) | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 4201 | rdev->supply_name = init_data->supply_regulator; | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 4202 | else if (regulator_desc->supply_name) | 
| Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 4203 | rdev->supply_name = regulator_desc->supply_name; | 
| Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 4204 |  | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4205 | /* | 
|  | 4206 | * Attempt to resolve the regulator supply, if specified, | 
|  | 4207 | * but don't return an error if we fail because we will try | 
|  | 4208 | * to resolve it again later as more regulators are added. | 
|  | 4209 | */ | 
|  | 4210 | if (regulator_resolve_supply(rdev)) | 
|  | 4211 | rdev_dbg(rdev, "unable to resolve supply\n"); | 
|  | 4212 |  | 
|  | 4213 | ret = set_machine_constraints(rdev, constraints); | 
|  | 4214 | if (ret < 0) | 
|  | 4215 | goto wash; | 
|  | 4216 |  | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4217 | /* add consumers devices */ | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4218 | if (init_data) { | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4219 | mutex_lock(®ulator_list_mutex); | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4220 | for (i = 0; i < init_data->num_consumer_supplies; i++) { | 
|  | 4221 | ret = set_consumer_device_supply(rdev, | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4222 | init_data->consumer_supplies[i].dev_name, | 
| Mark Brown | 23c2f04 | 2011-02-24 17:39:09 +0000 | [diff] [blame] | 4223 | init_data->consumer_supplies[i].supply); | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4224 | if (ret < 0) { | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4225 | mutex_unlock(®ulator_list_mutex); | 
| Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4226 | dev_err(dev, "Failed to set supply %s\n", | 
|  | 4227 | init_data->consumer_supplies[i].supply); | 
|  | 4228 | goto unset_supplies; | 
|  | 4229 | } | 
| Mark Brown | 23c2f04 | 2011-02-24 17:39:09 +0000 | [diff] [blame] | 4230 | } | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4231 | mutex_unlock(®ulator_list_mutex); | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4232 | } | 
|  | 4233 |  | 
| Matthias Kaehlcke | fd08604 | 2017-03-27 16:54:12 -0700 | [diff] [blame] | 4234 | if (!rdev->desc->ops->get_voltage && | 
|  | 4235 | !rdev->desc->ops->list_voltage && | 
|  | 4236 | !rdev->desc->fixed_uV) | 
|  | 4237 | rdev->is_switch = true; | 
|  | 4238 |  | 
| Jon Hunter | c438b9d | 2016-04-21 17:11:59 +0100 | [diff] [blame] | 4239 | ret = device_register(&rdev->dev); | 
|  | 4240 | if (ret != 0) { | 
|  | 4241 | put_device(&rdev->dev); | 
|  | 4242 | goto unset_supplies; | 
|  | 4243 | } | 
|  | 4244 |  | 
|  | 4245 | dev_set_drvdata(&rdev->dev, rdev); | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4246 | rdev_init_debugfs(rdev); | 
| Javier Martinez Canillas | 5e3ca2b | 2016-03-23 20:59:34 -0300 | [diff] [blame] | 4247 |  | 
|  | 4248 | /* try to resolve regulators supply since a new one was registered */ | 
|  | 4249 | class_for_each_device(®ulator_class, NULL, NULL, | 
|  | 4250 | regulator_register_resolve_supply); | 
| Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4251 | kfree(config); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4252 | return rdev; | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 4253 |  | 
| Jani Nikula | d4033b5 | 2010-04-29 10:55:11 +0300 | [diff] [blame] | 4254 | unset_supplies: | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4255 | mutex_lock(®ulator_list_mutex); | 
| Jani Nikula | d4033b5 | 2010-04-29 10:55:11 +0300 | [diff] [blame] | 4256 | unset_regulator_supplies(rdev); | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4257 | mutex_unlock(®ulator_list_mutex); | 
| Krzysztof Adamski | 3216523 | 2016-02-24 11:52:50 +0100 | [diff] [blame] | 4258 | wash: | 
| Boris Brezillon | 469b640 | 2016-04-12 12:31:00 +0200 | [diff] [blame] | 4259 | kfree(rdev->constraints); | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4260 | mutex_lock(®ulator_list_mutex); | 
| Krzysztof Adamski | 3216523 | 2016-02-24 11:52:50 +0100 | [diff] [blame] | 4261 | regulator_ena_gpio_free(rdev); | 
| Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4262 | mutex_unlock(®ulator_list_mutex); | 
| David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 4263 | clean: | 
|  | 4264 | kfree(rdev); | 
| Jon Hunter | a215137 | 2016-03-30 17:09:13 +0100 | [diff] [blame] | 4265 | kfree(config); | 
|  | 4266 | return ERR_PTR(ret); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4267 | } | 
|  | 4268 | EXPORT_SYMBOL_GPL(regulator_register); | 
|  | 4269 |  | 
|  | 4270 | /** | 
|  | 4271 | * regulator_unregister - unregister regulator | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 4272 | * @rdev: regulator to unregister | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4273 | * | 
|  | 4274 | * Called by regulator drivers to unregister a regulator. | 
|  | 4275 | */ | 
|  | 4276 | void regulator_unregister(struct regulator_dev *rdev) | 
|  | 4277 | { | 
|  | 4278 | if (rdev == NULL) | 
|  | 4279 | return; | 
|  | 4280 |  | 
| Mark Brown | 891636e | 2013-07-08 09:14:45 +0100 | [diff] [blame] | 4281 | if (rdev->supply) { | 
|  | 4282 | while (rdev->use_count--) | 
|  | 4283 | regulator_disable(rdev->supply); | 
| Mark Brown | e032b37 | 2012-03-28 21:17:55 +0100 | [diff] [blame] | 4284 | regulator_put(rdev->supply); | 
| Mark Brown | 891636e | 2013-07-08 09:14:45 +0100 | [diff] [blame] | 4285 | } | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4286 | mutex_lock(®ulator_list_mutex); | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4287 | debugfs_remove_recursive(rdev->debugfs); | 
| Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 4288 | flush_work(&rdev->disable_work.work); | 
| Mark Brown | 6bf87d1 | 2009-07-21 16:00:25 +0100 | [diff] [blame] | 4289 | WARN_ON(rdev->open_count); | 
| Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 4290 | unset_regulator_supplies(rdev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4291 | list_del(&rdev->list); | 
| Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 4292 | regulator_ena_gpio_free(rdev); | 
| Mark Brown | 2c0a303 | 2016-04-12 08:05:43 +0100 | [diff] [blame] | 4293 | mutex_unlock(®ulator_list_mutex); | 
| Lothar Waßmann | 58fb5cf | 2011-11-28 15:38:37 +0100 | [diff] [blame] | 4294 | device_unregister(&rdev->dev); | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4295 | } | 
|  | 4296 | EXPORT_SYMBOL_GPL(regulator_unregister); | 
|  | 4297 |  | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 4298 | #ifdef CONFIG_SUSPEND | 
|  | 4299 | static int _regulator_suspend_late(struct device *dev, void *data) | 
|  | 4300 | { | 
|  | 4301 | struct regulator_dev *rdev = dev_to_rdev(dev); | 
|  | 4302 | suspend_state_t *state = data; | 
|  | 4303 | int ret; | 
| MyungJoo Ham | 7a32b58 | 2011-03-11 10:13:59 +0900 | [diff] [blame] | 4304 |  | 
| Chunyan Zhang | f7efad1 | 2018-01-26 21:08:47 +0800 | [diff] [blame^] | 4305 | mutex_lock(&rdev->mutex); | 
|  | 4306 | ret = suspend_set_state(rdev, *state); | 
|  | 4307 | mutex_unlock(&rdev->mutex); | 
|  | 4308 |  | 
|  | 4309 | return ret; | 
|  | 4310 | } | 
|  | 4311 |  | 
|  | 4312 | /** | 
|  | 4313 | * regulator_suspend_late - prepare regulators for system wide suspend | 
|  | 4314 | * @state: system suspend state | 
|  | 4315 | * | 
|  | 4316 | * Configure each regulator with it's suspend operating parameters for state. | 
|  | 4317 | */ | 
|  | 4318 | static int regulator_suspend_late(struct device *dev) | 
|  | 4319 | { | 
|  | 4320 | suspend_state_t state = pm_suspend_target_state; | 
|  | 4321 |  | 
|  | 4322 | return class_for_each_device(®ulator_class, NULL, &state, | 
|  | 4323 | _regulator_suspend_late); | 
|  | 4324 | } | 
|  | 4325 | static int _regulator_resume_early(struct device *dev, void *data) | 
|  | 4326 | { | 
|  | 4327 | int ret = 0; | 
|  | 4328 | struct regulator_dev *rdev = dev_to_rdev(dev); | 
|  | 4329 | suspend_state_t *state = data; | 
|  | 4330 | struct regulator_state *rstate; | 
|  | 4331 |  | 
|  | 4332 | rstate = regulator_get_suspend_state(rdev, *state); | 
|  | 4333 | if (rstate == NULL) | 
|  | 4334 | return -EINVAL; | 
|  | 4335 |  | 
|  | 4336 | mutex_lock(&rdev->mutex); | 
|  | 4337 |  | 
|  | 4338 | if (rdev->desc->ops->resume_early && | 
|  | 4339 | (rstate->enabled == ENABLE_IN_SUSPEND || | 
|  | 4340 | rstate->enabled == DISABLE_IN_SUSPEND)) | 
|  | 4341 | ret = rdev->desc->ops->resume_early(rdev); | 
|  | 4342 |  | 
|  | 4343 | mutex_unlock(&rdev->mutex); | 
|  | 4344 |  | 
|  | 4345 | return ret; | 
|  | 4346 | } | 
|  | 4347 |  | 
|  | 4348 | static int regulator_resume_early(struct device *dev) | 
|  | 4349 | { | 
|  | 4350 | suspend_state_t state = pm_suspend_target_state; | 
|  | 4351 |  | 
|  | 4352 | return class_for_each_device(®ulator_class, NULL, &state, | 
|  | 4353 | _regulator_resume_early); | 
|  | 4354 | } | 
|  | 4355 |  | 
|  | 4356 | #else /* !CONFIG_SUSPEND */ | 
|  | 4357 |  | 
|  | 4358 | #define regulator_suspend_late	NULL | 
|  | 4359 | #define regulator_resume_early	NULL | 
|  | 4360 |  | 
|  | 4361 | #endif /* !CONFIG_SUSPEND */ | 
|  | 4362 |  | 
|  | 4363 | #ifdef CONFIG_PM | 
|  | 4364 | static const struct dev_pm_ops __maybe_unused regulator_pm_ops = { | 
|  | 4365 | .suspend_late	= regulator_suspend_late, | 
|  | 4366 | .resume_early	= regulator_resume_early, | 
|  | 4367 | }; | 
|  | 4368 | #endif | 
|  | 4369 |  | 
|  | 4370 | static struct class regulator_class = { | 
|  | 4371 | .name = "regulator", | 
|  | 4372 | .dev_release = regulator_dev_release, | 
|  | 4373 | .dev_groups = regulator_dev_groups, | 
|  | 4374 | #ifdef CONFIG_PM | 
|  | 4375 | .pm = ®ulator_pm_ops, | 
|  | 4376 | #endif | 
|  | 4377 | }; | 
| MyungJoo Ham | 7a32b58 | 2011-03-11 10:13:59 +0900 | [diff] [blame] | 4378 | /** | 
| Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4379 | * regulator_has_full_constraints - the system has fully specified constraints | 
|  | 4380 | * | 
|  | 4381 | * Calling this function will cause the regulator API to disable all | 
|  | 4382 | * regulators which have a zero use count and don't have an always_on | 
|  | 4383 | * constraint in a late_initcall. | 
|  | 4384 | * | 
|  | 4385 | * The intention is that this will become the default behaviour in a | 
|  | 4386 | * future kernel release so users are encouraged to use this facility | 
|  | 4387 | * now. | 
|  | 4388 | */ | 
|  | 4389 | void regulator_has_full_constraints(void) | 
|  | 4390 | { | 
|  | 4391 | has_full_constraints = 1; | 
|  | 4392 | } | 
|  | 4393 | EXPORT_SYMBOL_GPL(regulator_has_full_constraints); | 
|  | 4394 |  | 
|  | 4395 | /** | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4396 | * rdev_get_drvdata - get rdev regulator driver data | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 4397 | * @rdev: regulator | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4398 | * | 
|  | 4399 | * Get rdev regulator driver private data. This call can be used in the | 
|  | 4400 | * regulator driver context. | 
|  | 4401 | */ | 
|  | 4402 | void *rdev_get_drvdata(struct regulator_dev *rdev) | 
|  | 4403 | { | 
|  | 4404 | return rdev->reg_data; | 
|  | 4405 | } | 
|  | 4406 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); | 
|  | 4407 |  | 
|  | 4408 | /** | 
|  | 4409 | * regulator_get_drvdata - get regulator driver data | 
|  | 4410 | * @regulator: regulator | 
|  | 4411 | * | 
|  | 4412 | * Get regulator driver private data. This call can be used in the consumer | 
|  | 4413 | * driver context when non API regulator specific functions need to be called. | 
|  | 4414 | */ | 
|  | 4415 | void *regulator_get_drvdata(struct regulator *regulator) | 
|  | 4416 | { | 
|  | 4417 | return regulator->rdev->reg_data; | 
|  | 4418 | } | 
|  | 4419 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); | 
|  | 4420 |  | 
|  | 4421 | /** | 
|  | 4422 | * regulator_set_drvdata - set regulator driver data | 
|  | 4423 | * @regulator: regulator | 
|  | 4424 | * @data: data | 
|  | 4425 | */ | 
|  | 4426 | void regulator_set_drvdata(struct regulator *regulator, void *data) | 
|  | 4427 | { | 
|  | 4428 | regulator->rdev->reg_data = data; | 
|  | 4429 | } | 
|  | 4430 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); | 
|  | 4431 |  | 
|  | 4432 | /** | 
|  | 4433 | * regulator_get_id - get regulator ID | 
| Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 4434 | * @rdev: regulator | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4435 | */ | 
|  | 4436 | int rdev_get_id(struct regulator_dev *rdev) | 
|  | 4437 | { | 
|  | 4438 | return rdev->desc->id; | 
|  | 4439 | } | 
|  | 4440 | EXPORT_SYMBOL_GPL(rdev_get_id); | 
|  | 4441 |  | 
| Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4442 | struct device *rdev_get_dev(struct regulator_dev *rdev) | 
|  | 4443 | { | 
|  | 4444 | return &rdev->dev; | 
|  | 4445 | } | 
|  | 4446 | EXPORT_SYMBOL_GPL(rdev_get_dev); | 
|  | 4447 |  | 
|  | 4448 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) | 
|  | 4449 | { | 
|  | 4450 | return reg_init_data->driver_data; | 
|  | 4451 | } | 
|  | 4452 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); | 
|  | 4453 |  | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4454 | #ifdef CONFIG_DEBUG_FS | 
| Haishan Zhou | dbc5595 | 2017-06-30 11:43:42 +0800 | [diff] [blame] | 4455 | static int supply_map_show(struct seq_file *sf, void *data) | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4456 | { | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4457 | struct regulator_map *map; | 
|  | 4458 |  | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4459 | list_for_each_entry(map, ®ulator_map_list, list) { | 
| Haishan Zhou | dbc5595 | 2017-06-30 11:43:42 +0800 | [diff] [blame] | 4460 | seq_printf(sf, "%s -> %s.%s\n", | 
|  | 4461 | rdev_get_name(map->regulator), map->dev_name, | 
|  | 4462 | map->supply); | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4463 | } | 
|  | 4464 |  | 
| Haishan Zhou | dbc5595 | 2017-06-30 11:43:42 +0800 | [diff] [blame] | 4465 | return 0; | 
|  | 4466 | } | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4467 |  | 
| Haishan Zhou | dbc5595 | 2017-06-30 11:43:42 +0800 | [diff] [blame] | 4468 | static int supply_map_open(struct inode *inode, struct file *file) | 
|  | 4469 | { | 
|  | 4470 | return single_open(file, supply_map_show, inode->i_private); | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4471 | } | 
| Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4472 | #endif | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4473 |  | 
|  | 4474 | static const struct file_operations supply_map_fops = { | 
| Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4475 | #ifdef CONFIG_DEBUG_FS | 
| Haishan Zhou | dbc5595 | 2017-06-30 11:43:42 +0800 | [diff] [blame] | 4476 | .open = supply_map_open, | 
|  | 4477 | .read = seq_read, | 
|  | 4478 | .llseek = seq_lseek, | 
|  | 4479 | .release = single_release, | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4480 | #endif | 
| Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4481 | }; | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4482 |  | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4483 | #ifdef CONFIG_DEBUG_FS | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4484 | struct summary_data { | 
|  | 4485 | struct seq_file *s; | 
|  | 4486 | struct regulator_dev *parent; | 
|  | 4487 | int level; | 
|  | 4488 | }; | 
|  | 4489 |  | 
|  | 4490 | static void regulator_summary_show_subtree(struct seq_file *s, | 
|  | 4491 | struct regulator_dev *rdev, | 
|  | 4492 | int level); | 
|  | 4493 |  | 
|  | 4494 | static int regulator_summary_show_children(struct device *dev, void *data) | 
|  | 4495 | { | 
|  | 4496 | struct regulator_dev *rdev = dev_to_rdev(dev); | 
|  | 4497 | struct summary_data *summary_data = data; | 
|  | 4498 |  | 
|  | 4499 | if (rdev->supply && rdev->supply->rdev == summary_data->parent) | 
|  | 4500 | regulator_summary_show_subtree(summary_data->s, rdev, | 
|  | 4501 | summary_data->level + 1); | 
|  | 4502 |  | 
|  | 4503 | return 0; | 
|  | 4504 | } | 
|  | 4505 |  | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4506 | static void regulator_summary_show_subtree(struct seq_file *s, | 
|  | 4507 | struct regulator_dev *rdev, | 
|  | 4508 | int level) | 
|  | 4509 | { | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4510 | struct regulation_constraints *c; | 
|  | 4511 | struct regulator *consumer; | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4512 | struct summary_data summary_data; | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4513 |  | 
|  | 4514 | if (!rdev) | 
|  | 4515 | return; | 
|  | 4516 |  | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4517 | seq_printf(s, "%*s%-*s %3d %4d %6d ", | 
|  | 4518 | level * 3 + 1, "", | 
|  | 4519 | 30 - level * 3, rdev_get_name(rdev), | 
|  | 4520 | rdev->use_count, rdev->open_count, rdev->bypass_count); | 
|  | 4521 |  | 
| Heiko Stübner | 23296099 | 2015-04-10 13:48:41 +0200 | [diff] [blame] | 4522 | seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000); | 
|  | 4523 | seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000); | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4524 |  | 
|  | 4525 | c = rdev->constraints; | 
|  | 4526 | if (c) { | 
|  | 4527 | switch (rdev->desc->type) { | 
|  | 4528 | case REGULATOR_VOLTAGE: | 
|  | 4529 | seq_printf(s, "%5dmV %5dmV ", | 
|  | 4530 | c->min_uV / 1000, c->max_uV / 1000); | 
|  | 4531 | break; | 
|  | 4532 | case REGULATOR_CURRENT: | 
|  | 4533 | seq_printf(s, "%5dmA %5dmA ", | 
|  | 4534 | c->min_uA / 1000, c->max_uA / 1000); | 
|  | 4535 | break; | 
|  | 4536 | } | 
|  | 4537 | } | 
|  | 4538 |  | 
|  | 4539 | seq_puts(s, "\n"); | 
|  | 4540 |  | 
|  | 4541 | list_for_each_entry(consumer, &rdev->consumer_list, list) { | 
| Leonard Crestez | e42a46b | 2017-02-14 17:31:03 +0200 | [diff] [blame] | 4542 | if (consumer->dev && consumer->dev->class == ®ulator_class) | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4543 | continue; | 
|  | 4544 |  | 
|  | 4545 | seq_printf(s, "%*s%-*s ", | 
|  | 4546 | (level + 1) * 3 + 1, "", | 
| Leonard Crestez | e42a46b | 2017-02-14 17:31:03 +0200 | [diff] [blame] | 4547 | 30 - (level + 1) * 3, | 
|  | 4548 | consumer->dev ? dev_name(consumer->dev) : "deviceless"); | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4549 |  | 
|  | 4550 | switch (rdev->desc->type) { | 
|  | 4551 | case REGULATOR_VOLTAGE: | 
| Heiko Stübner | 23296099 | 2015-04-10 13:48:41 +0200 | [diff] [blame] | 4552 | seq_printf(s, "%37dmV %5dmV", | 
| Chunyan Zhang | c360a6d | 2018-01-26 21:08:44 +0800 | [diff] [blame] | 4553 | consumer->voltage[PM_SUSPEND_ON].min_uV / 1000, | 
|  | 4554 | consumer->voltage[PM_SUSPEND_ON].max_uV / 1000); | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4555 | break; | 
|  | 4556 | case REGULATOR_CURRENT: | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4557 | break; | 
|  | 4558 | } | 
|  | 4559 |  | 
|  | 4560 | seq_puts(s, "\n"); | 
|  | 4561 | } | 
|  | 4562 |  | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4563 | summary_data.s = s; | 
|  | 4564 | summary_data.level = level; | 
|  | 4565 | summary_data.parent = rdev; | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4566 |  | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4567 | class_for_each_device(®ulator_class, NULL, &summary_data, | 
|  | 4568 | regulator_summary_show_children); | 
|  | 4569 | } | 
|  | 4570 |  | 
|  | 4571 | static int regulator_summary_show_roots(struct device *dev, void *data) | 
|  | 4572 | { | 
|  | 4573 | struct regulator_dev *rdev = dev_to_rdev(dev); | 
|  | 4574 | struct seq_file *s = data; | 
|  | 4575 |  | 
|  | 4576 | if (!rdev->supply) | 
|  | 4577 | regulator_summary_show_subtree(s, rdev, 0); | 
|  | 4578 |  | 
|  | 4579 | return 0; | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4580 | } | 
|  | 4581 |  | 
|  | 4582 | static int regulator_summary_show(struct seq_file *s, void *data) | 
|  | 4583 | { | 
| Heiko Stübner | 23296099 | 2015-04-10 13:48:41 +0200 | [diff] [blame] | 4584 | seq_puts(s, " regulator                      use open bypass voltage current     min     max\n"); | 
|  | 4585 | seq_puts(s, "-------------------------------------------------------------------------------\n"); | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4586 |  | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4587 | class_for_each_device(®ulator_class, NULL, s, | 
|  | 4588 | regulator_summary_show_roots); | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4589 |  | 
|  | 4590 | return 0; | 
|  | 4591 | } | 
|  | 4592 |  | 
|  | 4593 | static int regulator_summary_open(struct inode *inode, struct file *file) | 
|  | 4594 | { | 
|  | 4595 | return single_open(file, regulator_summary_show, inode->i_private); | 
|  | 4596 | } | 
|  | 4597 | #endif | 
|  | 4598 |  | 
|  | 4599 | static const struct file_operations regulator_summary_fops = { | 
|  | 4600 | #ifdef CONFIG_DEBUG_FS | 
|  | 4601 | .open		= regulator_summary_open, | 
|  | 4602 | .read		= seq_read, | 
|  | 4603 | .llseek		= seq_lseek, | 
|  | 4604 | .release	= single_release, | 
|  | 4605 | #endif | 
|  | 4606 | }; | 
|  | 4607 |  | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4608 | static int __init regulator_init(void) | 
|  | 4609 | { | 
| Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 4610 | int ret; | 
|  | 4611 |  | 
| Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 4612 | ret = class_register(®ulator_class); | 
|  | 4613 |  | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4614 | debugfs_root = debugfs_create_dir("regulator", NULL); | 
| Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4615 | if (!debugfs_root) | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4616 | pr_warn("regulator: Failed to create debugfs directory\n"); | 
| Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4617 |  | 
| Mark Brown | f4d562c | 2012-02-20 21:01:04 +0000 | [diff] [blame] | 4618 | debugfs_create_file("supply_map", 0444, debugfs_root, NULL, | 
|  | 4619 | &supply_map_fops); | 
| Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4620 |  | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4621 | debugfs_create_file("regulator_summary", 0444, debugfs_root, | 
| Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4622 | NULL, ®ulator_summary_fops); | 
| Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4623 |  | 
| Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 4624 | regulator_dummy_init(); | 
|  | 4625 |  | 
|  | 4626 | return ret; | 
| Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4627 | } | 
|  | 4628 |  | 
|  | 4629 | /* init early to allow our consumers to complete system booting */ | 
|  | 4630 | core_initcall(regulator_init); | 
| Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4631 |  | 
| Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4632 | static int __init regulator_late_cleanup(struct device *dev, void *data) | 
| Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4633 | { | 
| Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4634 | struct regulator_dev *rdev = dev_to_rdev(dev); | 
|  | 4635 | const struct regulator_ops *ops = rdev->desc->ops; | 
|  | 4636 | struct regulation_constraints *c = rdev->constraints; | 
| Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4637 | int enabled, ret; | 
| Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4638 |  | 
| Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4639 | if (c && c->always_on) | 
|  | 4640 | return 0; | 
|  | 4641 |  | 
| WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 4642 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) | 
| Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4643 | return 0; | 
|  | 4644 |  | 
|  | 4645 | mutex_lock(&rdev->mutex); | 
|  | 4646 |  | 
|  | 4647 | if (rdev->use_count) | 
|  | 4648 | goto unlock; | 
|  | 4649 |  | 
|  | 4650 | /* If we can't read the status assume it's on. */ | 
|  | 4651 | if (ops->is_enabled) | 
|  | 4652 | enabled = ops->is_enabled(rdev); | 
|  | 4653 | else | 
|  | 4654 | enabled = 1; | 
|  | 4655 |  | 
|  | 4656 | if (!enabled) | 
|  | 4657 | goto unlock; | 
|  | 4658 |  | 
|  | 4659 | if (have_full_constraints()) { | 
|  | 4660 | /* We log since this may kill the system if it goes | 
|  | 4661 | * wrong. */ | 
|  | 4662 | rdev_info(rdev, "disabling\n"); | 
|  | 4663 | ret = _regulator_do_disable(rdev); | 
|  | 4664 | if (ret != 0) | 
|  | 4665 | rdev_err(rdev, "couldn't disable: %d\n", ret); | 
|  | 4666 | } else { | 
|  | 4667 | /* The intention is that in future we will | 
|  | 4668 | * assume that full constraints are provided | 
|  | 4669 | * so warn even if we aren't going to do | 
|  | 4670 | * anything here. | 
|  | 4671 | */ | 
|  | 4672 | rdev_warn(rdev, "incomplete constraints, leaving on\n"); | 
|  | 4673 | } | 
|  | 4674 |  | 
|  | 4675 | unlock: | 
|  | 4676 | mutex_unlock(&rdev->mutex); | 
|  | 4677 |  | 
|  | 4678 | return 0; | 
|  | 4679 | } | 
|  | 4680 |  | 
|  | 4681 | static int __init regulator_init_complete(void) | 
|  | 4682 | { | 
| Mark Brown | 86f5fcf | 2012-07-06 18:19:13 +0100 | [diff] [blame] | 4683 | /* | 
|  | 4684 | * Since DT doesn't provide an idiomatic mechanism for | 
|  | 4685 | * enabling full constraints and since it's much more natural | 
|  | 4686 | * with DT to provide them just assume that a DT enabled | 
|  | 4687 | * system has full constraints. | 
|  | 4688 | */ | 
|  | 4689 | if (of_have_populated_dt()) | 
|  | 4690 | has_full_constraints = true; | 
|  | 4691 |  | 
| Javier Martinez Canillas | 3827b64 | 2017-02-16 14:30:02 -0300 | [diff] [blame] | 4692 | /* | 
|  | 4693 | * Regulators may had failed to resolve their input supplies | 
|  | 4694 | * when were registered, either because the input supply was | 
|  | 4695 | * not registered yet or because its parent device was not | 
|  | 4696 | * bound yet. So attempt to resolve the input supplies for | 
|  | 4697 | * pending regulators before trying to disable unused ones. | 
|  | 4698 | */ | 
|  | 4699 | class_for_each_device(®ulator_class, NULL, NULL, | 
|  | 4700 | regulator_register_resolve_supply); | 
|  | 4701 |  | 
| Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4702 | /* If we have a full configuration then disable any regulators | 
| Mark Brown | e953583 | 2014-06-01 19:15:16 +0100 | [diff] [blame] | 4703 | * we have permission to change the status for and which are | 
|  | 4704 | * not in use or always_on.  This is effectively the default | 
|  | 4705 | * for DT and ACPI as they have full constraints. | 
| Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4706 | */ | 
| Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4707 | class_for_each_device(®ulator_class, NULL, NULL, | 
|  | 4708 | regulator_late_cleanup); | 
| Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4709 |  | 
|  | 4710 | return 0; | 
|  | 4711 | } | 
| Saravana Kannan | fd482a3 | 2014-04-23 18:10:50 -0500 | [diff] [blame] | 4712 | late_initcall_sync(regulator_init_complete); |