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