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