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