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