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