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