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; |
| 201 | char prop_name[32]; /* 32 is max size of property name */ |
| 202 | |
| 203 | dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); |
| 204 | |
| 205 | snprintf(prop_name, 32, "%s-supply", supply); |
| 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 | |
| 2411 | for (i = 0; i < count; i++) { |
| 2412 | ret = _regulator_disable(rdev); |
| 2413 | if (ret != 0) |
| 2414 | rdev_err(rdev, "Deferred disable failed: %d\n", ret); |
| 2415 | } |
| 2416 | |
| 2417 | mutex_unlock(&rdev->mutex); |
| 2418 | |
| 2419 | if (rdev->supply) { |
| 2420 | for (i = 0; i < count; i++) { |
| 2421 | ret = regulator_disable(rdev->supply); |
| 2422 | if (ret != 0) { |
| 2423 | rdev_err(rdev, |
| 2424 | "Supply disable failed: %d\n", ret); |
| 2425 | } |
| 2426 | } |
| 2427 | } |
| 2428 | } |
| 2429 | |
| 2430 | /** |
| 2431 | * regulator_disable_deferred - disable regulator output with delay |
| 2432 | * @regulator: regulator source |
| 2433 | * @ms: miliseconds until the regulator is disabled |
| 2434 | * |
| 2435 | * Execute regulator_disable() on the regulator after a delay. This |
| 2436 | * is intended for use with devices that require some time to quiesce. |
| 2437 | * |
| 2438 | * NOTE: this will only disable the regulator output if no other consumer |
| 2439 | * devices have it enabled, the regulator device supports disabling and |
| 2440 | * machine constraints permit this operation. |
| 2441 | */ |
| 2442 | int regulator_disable_deferred(struct regulator *regulator, int ms) |
| 2443 | { |
| 2444 | struct regulator_dev *rdev = regulator->rdev; |
| 2445 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 2446 | if (regulator->always_on) |
| 2447 | return 0; |
| 2448 | |
Mark Brown | 2b5a24a | 2012-09-07 11:00:53 +0800 | [diff] [blame] | 2449 | if (!ms) |
| 2450 | return regulator_disable(regulator); |
| 2451 | |
Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 2452 | mutex_lock(&rdev->mutex); |
| 2453 | rdev->deferred_disables++; |
| 2454 | mutex_unlock(&rdev->mutex); |
| 2455 | |
Dan Carpenter | 70dc6da | 2016-01-07 13:03:08 +0300 | [diff] [blame] | 2456 | queue_delayed_work(system_power_efficient_wq, &rdev->disable_work, |
| 2457 | msecs_to_jiffies(ms)); |
| 2458 | return 0; |
Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 2459 | } |
| 2460 | EXPORT_SYMBOL_GPL(regulator_disable_deferred); |
| 2461 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2462 | static int _regulator_is_enabled(struct regulator_dev *rdev) |
| 2463 | { |
Mark Brown | 65f7350 | 2012-06-27 14:14:38 +0100 | [diff] [blame] | 2464 | /* A GPIO control always takes precedence */ |
Kim, Milo | 7b74d14 | 2013-02-18 06:50:55 +0000 | [diff] [blame] | 2465 | if (rdev->ena_pin) |
Mark Brown | 65f7350 | 2012-06-27 14:14:38 +0100 | [diff] [blame] | 2466 | return rdev->ena_gpio_state; |
| 2467 | |
Mark Brown | 9a7f6a4 | 2010-02-11 17:22:45 +0000 | [diff] [blame] | 2468 | /* 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] | 2469 | if (!rdev->desc->ops->is_enabled) |
Mark Brown | 9a7f6a4 | 2010-02-11 17:22:45 +0000 | [diff] [blame] | 2470 | return 1; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2471 | |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 2472 | return rdev->desc->ops->is_enabled(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2473 | } |
| 2474 | |
Sascha Hauer | 3a40cfc | 2015-09-30 16:05:43 +0200 | [diff] [blame] | 2475 | static int _regulator_list_voltage(struct regulator *regulator, |
| 2476 | unsigned selector, int lock) |
| 2477 | { |
| 2478 | struct regulator_dev *rdev = regulator->rdev; |
| 2479 | const struct regulator_ops *ops = rdev->desc->ops; |
| 2480 | int ret; |
| 2481 | |
| 2482 | if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) |
| 2483 | return rdev->desc->fixed_uV; |
| 2484 | |
| 2485 | if (ops->list_voltage) { |
| 2486 | if (selector >= rdev->desc->n_voltages) |
| 2487 | return -EINVAL; |
| 2488 | if (lock) |
| 2489 | mutex_lock(&rdev->mutex); |
| 2490 | ret = ops->list_voltage(rdev, selector); |
| 2491 | if (lock) |
| 2492 | mutex_unlock(&rdev->mutex); |
| 2493 | } else if (rdev->supply) { |
| 2494 | ret = _regulator_list_voltage(rdev->supply, selector, lock); |
| 2495 | } else { |
| 2496 | return -EINVAL; |
| 2497 | } |
| 2498 | |
| 2499 | if (ret > 0) { |
| 2500 | if (ret < rdev->constraints->min_uV) |
| 2501 | ret = 0; |
| 2502 | else if (ret > rdev->constraints->max_uV) |
| 2503 | ret = 0; |
| 2504 | } |
| 2505 | |
| 2506 | return ret; |
| 2507 | } |
| 2508 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2509 | /** |
| 2510 | * regulator_is_enabled - is the regulator output enabled |
| 2511 | * @regulator: regulator source |
| 2512 | * |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 2513 | * Returns positive if the regulator driver backing the source/client |
| 2514 | * has requested that the device be enabled, zero if it hasn't, else a |
| 2515 | * negative errno code. |
| 2516 | * |
| 2517 | * Note that the device backing this regulator handle can have multiple |
| 2518 | * users, so it might be enabled even if regulator_enable() was never |
| 2519 | * called for this particular source. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2520 | */ |
| 2521 | int regulator_is_enabled(struct regulator *regulator) |
| 2522 | { |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 2523 | int ret; |
| 2524 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 2525 | if (regulator->always_on) |
| 2526 | return 1; |
| 2527 | |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 2528 | mutex_lock(®ulator->rdev->mutex); |
| 2529 | ret = _regulator_is_enabled(regulator->rdev); |
| 2530 | mutex_unlock(®ulator->rdev->mutex); |
| 2531 | |
| 2532 | return ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2533 | } |
| 2534 | EXPORT_SYMBOL_GPL(regulator_is_enabled); |
| 2535 | |
| 2536 | /** |
David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 2537 | * regulator_count_voltages - count regulator_list_voltage() selectors |
| 2538 | * @regulator: regulator source |
| 2539 | * |
| 2540 | * Returns number of selectors, or negative errno. Selectors are |
| 2541 | * numbered starting at zero, and typically correspond to bitfields |
| 2542 | * in hardware registers. |
| 2543 | */ |
| 2544 | int regulator_count_voltages(struct regulator *regulator) |
| 2545 | { |
| 2546 | struct regulator_dev *rdev = regulator->rdev; |
| 2547 | |
Javier Martinez Canillas | 26988ef | 2014-07-29 18:28:56 +0200 | [diff] [blame] | 2548 | if (rdev->desc->n_voltages) |
| 2549 | return rdev->desc->n_voltages; |
| 2550 | |
| 2551 | if (!rdev->supply) |
| 2552 | return -EINVAL; |
| 2553 | |
| 2554 | return regulator_count_voltages(rdev->supply); |
David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 2555 | } |
| 2556 | EXPORT_SYMBOL_GPL(regulator_count_voltages); |
| 2557 | |
| 2558 | /** |
| 2559 | * regulator_list_voltage - enumerate supported voltages |
| 2560 | * @regulator: regulator source |
| 2561 | * @selector: identify voltage to list |
| 2562 | * Context: can sleep |
| 2563 | * |
| 2564 | * Returns a voltage that can be passed to @regulator_set_voltage(), |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 2565 | * 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] | 2566 | * negative errno. |
| 2567 | */ |
| 2568 | int regulator_list_voltage(struct regulator *regulator, unsigned selector) |
| 2569 | { |
Sascha Hauer | 3a40cfc | 2015-09-30 16:05:43 +0200 | [diff] [blame] | 2570 | return _regulator_list_voltage(regulator, selector, 1); |
David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 2571 | } |
| 2572 | EXPORT_SYMBOL_GPL(regulator_list_voltage); |
| 2573 | |
| 2574 | /** |
Tuomas Tynkkynen | 04eca28 | 2014-07-21 18:38:48 +0300 | [diff] [blame] | 2575 | * regulator_get_regmap - get the regulator's register map |
| 2576 | * @regulator: regulator source |
| 2577 | * |
| 2578 | * Returns the register map for the given regulator, or an ERR_PTR value |
| 2579 | * if the regulator doesn't use regmap. |
| 2580 | */ |
| 2581 | struct regmap *regulator_get_regmap(struct regulator *regulator) |
| 2582 | { |
| 2583 | struct regmap *map = regulator->rdev->regmap; |
| 2584 | |
| 2585 | return map ? map : ERR_PTR(-EOPNOTSUPP); |
| 2586 | } |
| 2587 | |
| 2588 | /** |
| 2589 | * regulator_get_hardware_vsel_register - get the HW voltage selector register |
| 2590 | * @regulator: regulator source |
| 2591 | * @vsel_reg: voltage selector register, output parameter |
| 2592 | * @vsel_mask: mask for voltage selector bitfield, output parameter |
| 2593 | * |
| 2594 | * Returns the hardware register offset and bitmask used for setting the |
| 2595 | * regulator voltage. This might be useful when configuring voltage-scaling |
| 2596 | * hardware or firmware that can make I2C requests behind the kernel's back, |
| 2597 | * for example. |
| 2598 | * |
| 2599 | * On success, the output parameters @vsel_reg and @vsel_mask are filled in |
| 2600 | * and 0 is returned, otherwise a negative errno is returned. |
| 2601 | */ |
| 2602 | int regulator_get_hardware_vsel_register(struct regulator *regulator, |
| 2603 | unsigned *vsel_reg, |
| 2604 | unsigned *vsel_mask) |
| 2605 | { |
Guodong Xu | 39f5460 | 2014-08-19 18:07:41 +0800 | [diff] [blame] | 2606 | struct regulator_dev *rdev = regulator->rdev; |
| 2607 | const struct regulator_ops *ops = rdev->desc->ops; |
Tuomas Tynkkynen | 04eca28 | 2014-07-21 18:38:48 +0300 | [diff] [blame] | 2608 | |
| 2609 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) |
| 2610 | return -EOPNOTSUPP; |
| 2611 | |
| 2612 | *vsel_reg = rdev->desc->vsel_reg; |
| 2613 | *vsel_mask = rdev->desc->vsel_mask; |
| 2614 | |
| 2615 | return 0; |
| 2616 | } |
| 2617 | EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register); |
| 2618 | |
| 2619 | /** |
| 2620 | * regulator_list_hardware_vsel - get the HW-specific register value for a selector |
| 2621 | * @regulator: regulator source |
| 2622 | * @selector: identify voltage to list |
| 2623 | * |
| 2624 | * Converts the selector to a hardware-specific voltage selector that can be |
| 2625 | * directly written to the regulator registers. The address of the voltage |
| 2626 | * register can be determined by calling @regulator_get_hardware_vsel_register. |
| 2627 | * |
| 2628 | * On error a negative errno is returned. |
| 2629 | */ |
| 2630 | int regulator_list_hardware_vsel(struct regulator *regulator, |
| 2631 | unsigned selector) |
| 2632 | { |
Guodong Xu | 39f5460 | 2014-08-19 18:07:41 +0800 | [diff] [blame] | 2633 | struct regulator_dev *rdev = regulator->rdev; |
| 2634 | const struct regulator_ops *ops = rdev->desc->ops; |
Tuomas Tynkkynen | 04eca28 | 2014-07-21 18:38:48 +0300 | [diff] [blame] | 2635 | |
| 2636 | if (selector >= rdev->desc->n_voltages) |
| 2637 | return -EINVAL; |
| 2638 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) |
| 2639 | return -EOPNOTSUPP; |
| 2640 | |
| 2641 | return selector; |
| 2642 | } |
| 2643 | EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel); |
| 2644 | |
| 2645 | /** |
David Collins | 85a82e8 | 2015-04-09 14:44:15 -0700 | [diff] [blame] | 2646 | * regulator_list_corner_voltage - return the maximum voltage in microvolts that |
| 2647 | * can be physically configured for the regulator when operating at the |
| 2648 | * specified voltage corner |
| 2649 | * @regulator: regulator source |
| 2650 | * @corner: voltage corner value |
| 2651 | * Context: can sleep |
| 2652 | * |
| 2653 | * This function can be used for regulators which allow scaling between |
| 2654 | * different voltage corners as opposed to be different absolute voltages. The |
| 2655 | * absolute voltage for a given corner may vary part-to-part or for a given part |
| 2656 | * at runtime based upon various factors. |
| 2657 | * |
| 2658 | * Returns a voltage corresponding to the specified voltage corner or a negative |
| 2659 | * errno if the corner value can't be used on this system. |
| 2660 | */ |
| 2661 | int regulator_list_corner_voltage(struct regulator *regulator, int corner) |
| 2662 | { |
| 2663 | struct regulator_dev *rdev = regulator->rdev; |
| 2664 | int ret; |
| 2665 | |
| 2666 | if (corner < rdev->constraints->min_uV || |
| 2667 | corner > rdev->constraints->max_uV || |
| 2668 | !rdev->desc->ops->list_corner_voltage) |
| 2669 | return -EINVAL; |
| 2670 | |
| 2671 | mutex_lock(&rdev->mutex); |
| 2672 | ret = rdev->desc->ops->list_corner_voltage(rdev, corner); |
| 2673 | mutex_unlock(&rdev->mutex); |
| 2674 | |
| 2675 | return ret; |
| 2676 | } |
| 2677 | EXPORT_SYMBOL(regulator_list_corner_voltage); |
| 2678 | |
| 2679 | /** |
Paul Walmsley | 2a668a8 | 2013-06-07 08:06:56 +0000 | [diff] [blame] | 2680 | * regulator_get_linear_step - return the voltage step size between VSEL values |
| 2681 | * @regulator: regulator source |
| 2682 | * |
| 2683 | * Returns the voltage step size between VSEL values for linear |
| 2684 | * regulators, or return 0 if the regulator isn't a linear regulator. |
| 2685 | */ |
| 2686 | unsigned int regulator_get_linear_step(struct regulator *regulator) |
| 2687 | { |
| 2688 | struct regulator_dev *rdev = regulator->rdev; |
| 2689 | |
| 2690 | return rdev->desc->uV_step; |
| 2691 | } |
| 2692 | EXPORT_SYMBOL_GPL(regulator_get_linear_step); |
| 2693 | |
| 2694 | /** |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 2695 | * regulator_is_supported_voltage - check if a voltage range can be supported |
| 2696 | * |
| 2697 | * @regulator: Regulator to check. |
| 2698 | * @min_uV: Minimum required voltage in uV. |
| 2699 | * @max_uV: Maximum required voltage in uV. |
| 2700 | * |
| 2701 | * Returns a boolean or a negative error code. |
| 2702 | */ |
| 2703 | int regulator_is_supported_voltage(struct regulator *regulator, |
| 2704 | int min_uV, int max_uV) |
| 2705 | { |
Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 2706 | struct regulator_dev *rdev = regulator->rdev; |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 2707 | int i, voltages, ret; |
| 2708 | |
Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 2709 | /* If we can't change voltage check the current voltage */ |
WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 2710 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 2711 | ret = regulator_get_voltage(regulator); |
| 2712 | if (ret >= 0) |
Jingoo Han | 0d25d09 | 2014-01-08 10:02:16 +0900 | [diff] [blame] | 2713 | return min_uV <= ret && ret <= max_uV; |
Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 2714 | else |
| 2715 | return ret; |
| 2716 | } |
| 2717 | |
Pawel Moll | bd7a2b6 | 2012-09-24 18:56:53 +0100 | [diff] [blame] | 2718 | /* Any voltage within constrains range is fine? */ |
| 2719 | if (rdev->desc->continuous_voltage_range) |
| 2720 | return min_uV >= rdev->constraints->min_uV && |
| 2721 | max_uV <= rdev->constraints->max_uV; |
| 2722 | |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 2723 | ret = regulator_count_voltages(regulator); |
| 2724 | if (ret < 0) |
| 2725 | return ret; |
| 2726 | voltages = ret; |
| 2727 | |
| 2728 | for (i = 0; i < voltages; i++) { |
| 2729 | ret = regulator_list_voltage(regulator, i); |
| 2730 | |
| 2731 | if (ret >= min_uV && ret <= max_uV) |
| 2732 | return 1; |
| 2733 | } |
| 2734 | |
| 2735 | return 0; |
| 2736 | } |
Mark Brown | a398eaa | 2011-12-28 12:48:45 +0000 | [diff] [blame] | 2737 | EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 2738 | |
Sascha Hauer | a204f41 | 2015-10-20 14:37:27 +0200 | [diff] [blame] | 2739 | static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV, |
| 2740 | int max_uV) |
| 2741 | { |
| 2742 | const struct regulator_desc *desc = rdev->desc; |
| 2743 | |
| 2744 | if (desc->ops->map_voltage) |
| 2745 | return desc->ops->map_voltage(rdev, min_uV, max_uV); |
| 2746 | |
| 2747 | if (desc->ops->list_voltage == regulator_list_voltage_linear) |
| 2748 | return regulator_map_voltage_linear(rdev, min_uV, max_uV); |
| 2749 | |
| 2750 | if (desc->ops->list_voltage == regulator_list_voltage_linear_range) |
| 2751 | return regulator_map_voltage_linear_range(rdev, min_uV, max_uV); |
| 2752 | |
| 2753 | return regulator_map_voltage_iterate(rdev, min_uV, max_uV); |
| 2754 | } |
| 2755 | |
Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 2756 | static int _regulator_call_set_voltage(struct regulator_dev *rdev, |
| 2757 | int min_uV, int max_uV, |
| 2758 | unsigned *selector) |
| 2759 | { |
| 2760 | struct pre_voltage_change_data data; |
| 2761 | int ret; |
| 2762 | |
| 2763 | data.old_uV = _regulator_get_voltage(rdev); |
| 2764 | data.min_uV = min_uV; |
| 2765 | data.max_uV = max_uV; |
| 2766 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, |
| 2767 | &data); |
| 2768 | if (ret & NOTIFY_STOP_MASK) |
| 2769 | return -EINVAL; |
| 2770 | |
| 2771 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector); |
| 2772 | if (ret >= 0) |
| 2773 | return ret; |
| 2774 | |
| 2775 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, |
| 2776 | (void *)data.old_uV); |
| 2777 | |
| 2778 | return ret; |
| 2779 | } |
| 2780 | |
| 2781 | static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev, |
| 2782 | int uV, unsigned selector) |
| 2783 | { |
| 2784 | struct pre_voltage_change_data data; |
| 2785 | int ret; |
| 2786 | |
| 2787 | data.old_uV = _regulator_get_voltage(rdev); |
| 2788 | data.min_uV = uV; |
| 2789 | data.max_uV = uV; |
| 2790 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, |
| 2791 | &data); |
| 2792 | if (ret & NOTIFY_STOP_MASK) |
| 2793 | return -EINVAL; |
| 2794 | |
| 2795 | ret = rdev->desc->ops->set_voltage_sel(rdev, selector); |
| 2796 | if (ret >= 0) |
| 2797 | return ret; |
| 2798 | |
| 2799 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, |
| 2800 | (void *)data.old_uV); |
| 2801 | |
| 2802 | return ret; |
| 2803 | } |
| 2804 | |
Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2805 | static int _regulator_set_voltage_time(struct regulator_dev *rdev, |
| 2806 | int old_uV, int new_uV) |
| 2807 | { |
| 2808 | unsigned int ramp_delay = 0; |
| 2809 | |
| 2810 | if (rdev->constraints->ramp_delay) |
| 2811 | ramp_delay = rdev->constraints->ramp_delay; |
| 2812 | else if (rdev->desc->ramp_delay) |
| 2813 | ramp_delay = rdev->desc->ramp_delay; |
| 2814 | |
| 2815 | if (ramp_delay == 0) { |
H. Nikolaus Schaller | ba14fa1 | 2016-10-27 14:31:39 +0200 | [diff] [blame] | 2816 | rdev_dbg(rdev, "ramp_delay not set\n"); |
Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2817 | return 0; |
| 2818 | } |
| 2819 | |
| 2820 | return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); |
| 2821 | } |
| 2822 | |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2823 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
| 2824 | int min_uV, int max_uV) |
| 2825 | { |
| 2826 | int ret; |
Linus Walleij | 77af1b264 | 2011-03-17 13:24:36 +0100 | [diff] [blame] | 2827 | int delay = 0; |
Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2828 | int best_val = 0; |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2829 | unsigned int selector; |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2830 | int old_selector = -1; |
Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2831 | const struct regulator_ops *ops = rdev->desc->ops; |
Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2832 | int old_uV = _regulator_get_voltage(rdev); |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2833 | |
| 2834 | trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); |
| 2835 | |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2836 | min_uV += rdev->constraints->uV_offset; |
| 2837 | max_uV += rdev->constraints->uV_offset; |
| 2838 | |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2839 | /* |
| 2840 | * If we can't obtain the old selector there is not enough |
| 2841 | * info to call set_voltage_time_sel(). |
| 2842 | */ |
Axel Lin | 8b7485e | 2012-05-21 09:37:52 +0800 | [diff] [blame] | 2843 | if (_regulator_is_enabled(rdev) && |
Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2844 | ops->set_voltage_time_sel && ops->get_voltage_sel) { |
| 2845 | old_selector = ops->get_voltage_sel(rdev); |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2846 | if (old_selector < 0) |
| 2847 | return old_selector; |
| 2848 | } |
| 2849 | |
Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2850 | if (ops->set_voltage) { |
Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 2851 | ret = _regulator_call_set_voltage(rdev, min_uV, max_uV, |
| 2852 | &selector); |
Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2853 | |
| 2854 | if (ret >= 0) { |
Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2855 | if (ops->list_voltage) |
| 2856 | best_val = ops->list_voltage(rdev, |
| 2857 | selector); |
Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2858 | else |
| 2859 | best_val = _regulator_get_voltage(rdev); |
| 2860 | } |
| 2861 | |
Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2862 | } else if (ops->set_voltage_sel) { |
Sascha Hauer | a204f41 | 2015-10-20 14:37:27 +0200 | [diff] [blame] | 2863 | ret = regulator_map_voltage(rdev, min_uV, max_uV); |
Mark Brown | e843fc4 | 2012-05-09 21:16:06 +0100 | [diff] [blame] | 2864 | if (ret >= 0) { |
Matthias Kaehlcke | 57995a4 | 2016-09-14 09:52:05 -0700 | [diff] [blame] | 2865 | best_val = ops->list_voltage(rdev, ret); |
Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2866 | if (min_uV <= best_val && max_uV >= best_val) { |
| 2867 | selector = ret; |
Axel Lin | c66a566 | 2013-02-06 11:09:48 +0800 | [diff] [blame] | 2868 | if (old_selector == selector) |
| 2869 | ret = 0; |
| 2870 | else |
Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 2871 | ret = _regulator_call_set_voltage_sel( |
| 2872 | rdev, best_val, selector); |
Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2873 | } else { |
| 2874 | ret = -EINVAL; |
| 2875 | } |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2876 | } |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2877 | } else { |
| 2878 | ret = -EINVAL; |
| 2879 | } |
| 2880 | |
Matthias Kaehlcke | 31dfe68 | 2016-09-14 09:52:06 -0700 | [diff] [blame] | 2881 | if (ret) |
| 2882 | goto out; |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2883 | |
Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2884 | if (ops->set_voltage_time_sel) { |
| 2885 | /* |
| 2886 | * Call set_voltage_time_sel if successfully obtained |
| 2887 | * old_selector |
| 2888 | */ |
| 2889 | if (old_selector >= 0 && old_selector != selector) |
| 2890 | delay = ops->set_voltage_time_sel(rdev, old_selector, |
| 2891 | selector); |
| 2892 | } else { |
| 2893 | if (old_uV != best_val) { |
| 2894 | if (ops->set_voltage_time) |
| 2895 | delay = ops->set_voltage_time(rdev, old_uV, |
| 2896 | best_val); |
| 2897 | else |
| 2898 | delay = _regulator_set_voltage_time(rdev, |
| 2899 | old_uV, |
| 2900 | best_val); |
Philip Rakity | 8b96de3 | 2012-06-14 15:07:56 -0700 | [diff] [blame] | 2901 | } |
Linus Walleij | 77af1b264 | 2011-03-17 13:24:36 +0100 | [diff] [blame] | 2902 | } |
| 2903 | |
Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 2904 | if (delay < 0) { |
| 2905 | rdev_warn(rdev, "failed to get delay: %d\n", delay); |
| 2906 | delay = 0; |
| 2907 | } |
| 2908 | |
| 2909 | /* Insert any necessary delays */ |
| 2910 | if (delay >= 1000) { |
| 2911 | mdelay(delay / 1000); |
| 2912 | udelay(delay % 1000); |
| 2913 | } else if (delay) { |
| 2914 | udelay(delay); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2915 | } |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2916 | |
Matthias Kaehlcke | 31dfe68 | 2016-09-14 09:52:06 -0700 | [diff] [blame] | 2917 | if (best_val >= 0) { |
Axel Lin | 2f6c797 | 2012-08-06 23:44:19 +0800 | [diff] [blame] | 2918 | unsigned long data = best_val; |
| 2919 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2920 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, |
Axel Lin | 2f6c797 | 2012-08-06 23:44:19 +0800 | [diff] [blame] | 2921 | (void *)data); |
| 2922 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2923 | |
Matthias Kaehlcke | 31dfe68 | 2016-09-14 09:52:06 -0700 | [diff] [blame] | 2924 | out: |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2925 | trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2926 | |
| 2927 | return ret; |
| 2928 | } |
| 2929 | |
Sascha Hauer | a9f226b | 2015-10-13 12:45:25 +0200 | [diff] [blame] | 2930 | static int regulator_set_voltage_unlocked(struct regulator *regulator, |
| 2931 | int min_uV, int max_uV) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2932 | { |
| 2933 | struct regulator_dev *rdev = regulator->rdev; |
Mark Brown | 95a3c23 | 2010-12-16 15:49:37 +0000 | [diff] [blame] | 2934 | int ret = 0; |
Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 2935 | int old_min_uV, old_max_uV; |
Bjorn Andersson | c00dc35 | 2014-02-05 12:30:26 -0800 | [diff] [blame] | 2936 | int current_uV; |
Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 2937 | int best_supply_uV = 0; |
| 2938 | int supply_change_uV = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2939 | |
Mark Brown | 95a3c23 | 2010-12-16 15:49:37 +0000 | [diff] [blame] | 2940 | /* If we're setting the same range as last time the change |
| 2941 | * should be a noop (some cpufreq implementations use the same |
| 2942 | * voltage for multiple frequencies, for example). |
| 2943 | */ |
| 2944 | if (regulator->min_uV == min_uV && regulator->max_uV == max_uV) |
| 2945 | goto out; |
| 2946 | |
Bjorn Andersson | c00dc35 | 2014-02-05 12:30:26 -0800 | [diff] [blame] | 2947 | /* 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] | 2948 | * return successfully even though the regulator does not support |
Bjorn Andersson | c00dc35 | 2014-02-05 12:30:26 -0800 | [diff] [blame] | 2949 | * changing the voltage. |
| 2950 | */ |
WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 2951 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
Bjorn Andersson | c00dc35 | 2014-02-05 12:30:26 -0800 | [diff] [blame] | 2952 | current_uV = _regulator_get_voltage(rdev); |
| 2953 | if (min_uV <= current_uV && current_uV <= max_uV) { |
| 2954 | regulator->min_uV = min_uV; |
| 2955 | regulator->max_uV = max_uV; |
| 2956 | goto out; |
| 2957 | } |
| 2958 | } |
| 2959 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2960 | /* sanity check */ |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2961 | if (!rdev->desc->ops->set_voltage && |
| 2962 | !rdev->desc->ops->set_voltage_sel) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2963 | ret = -EINVAL; |
| 2964 | goto out; |
| 2965 | } |
| 2966 | |
| 2967 | /* constraints check */ |
| 2968 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); |
| 2969 | if (ret < 0) |
| 2970 | goto out; |
Jingoo Han | 0d25d09 | 2014-01-08 10:02:16 +0900 | [diff] [blame] | 2971 | |
Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 2972 | /* restore original values in case of error */ |
| 2973 | old_min_uV = regulator->min_uV; |
| 2974 | old_max_uV = regulator->max_uV; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2975 | regulator->min_uV = min_uV; |
| 2976 | regulator->max_uV = max_uV; |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 2977 | |
Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 2978 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV); |
| 2979 | if (ret < 0) |
Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 2980 | goto out2; |
Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 2981 | |
Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 2982 | if (rdev->supply && (rdev->desc->min_dropout_uV || |
| 2983 | !rdev->desc->ops->get_voltage)) { |
| 2984 | int current_supply_uV; |
| 2985 | int selector; |
| 2986 | |
| 2987 | selector = regulator_map_voltage(rdev, min_uV, max_uV); |
| 2988 | if (selector < 0) { |
| 2989 | ret = selector; |
| 2990 | goto out2; |
| 2991 | } |
| 2992 | |
| 2993 | best_supply_uV = _regulator_list_voltage(regulator, selector, 0); |
| 2994 | if (best_supply_uV < 0) { |
| 2995 | ret = best_supply_uV; |
| 2996 | goto out2; |
| 2997 | } |
| 2998 | |
| 2999 | best_supply_uV += rdev->desc->min_dropout_uV; |
| 3000 | |
| 3001 | current_supply_uV = _regulator_get_voltage(rdev->supply->rdev); |
| 3002 | if (current_supply_uV < 0) { |
| 3003 | ret = current_supply_uV; |
| 3004 | goto out2; |
| 3005 | } |
| 3006 | |
| 3007 | supply_change_uV = best_supply_uV - current_supply_uV; |
| 3008 | } |
| 3009 | |
| 3010 | if (supply_change_uV > 0) { |
| 3011 | ret = regulator_set_voltage_unlocked(rdev->supply, |
| 3012 | best_supply_uV, INT_MAX); |
| 3013 | if (ret) { |
| 3014 | dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n", |
| 3015 | ret); |
| 3016 | goto out2; |
| 3017 | } |
| 3018 | } |
| 3019 | |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 3020 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); |
Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 3021 | if (ret < 0) |
| 3022 | goto out2; |
Jingoo Han | 0d25d09 | 2014-01-08 10:02:16 +0900 | [diff] [blame] | 3023 | |
Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 3024 | if (supply_change_uV < 0) { |
| 3025 | ret = regulator_set_voltage_unlocked(rdev->supply, |
| 3026 | best_supply_uV, INT_MAX); |
| 3027 | if (ret) |
| 3028 | dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n", |
| 3029 | ret); |
| 3030 | /* No need to fail here */ |
| 3031 | ret = 0; |
| 3032 | } |
| 3033 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3034 | out: |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3035 | return ret; |
Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 3036 | out2: |
| 3037 | regulator->min_uV = old_min_uV; |
| 3038 | regulator->max_uV = old_max_uV; |
Sascha Hauer | a9f226b | 2015-10-13 12:45:25 +0200 | [diff] [blame] | 3039 | |
| 3040 | return ret; |
| 3041 | } |
| 3042 | |
| 3043 | /** |
| 3044 | * regulator_set_voltage - set regulator output voltage |
| 3045 | * @regulator: regulator source |
| 3046 | * @min_uV: Minimum required voltage in uV |
| 3047 | * @max_uV: Maximum acceptable voltage in uV |
| 3048 | * |
| 3049 | * Sets a voltage regulator to the desired output voltage. This can be set |
| 3050 | * during any regulator state. IOW, regulator can be disabled or enabled. |
| 3051 | * |
| 3052 | * If the regulator is enabled then the voltage will change to the new value |
| 3053 | * immediately otherwise if the regulator is disabled the regulator will |
| 3054 | * output at the new voltage when enabled. |
| 3055 | * |
| 3056 | * NOTE: If the regulator is shared between several devices then the lowest |
| 3057 | * request voltage that meets the system constraints will be used. |
| 3058 | * Regulator system constraints must be set for this regulator before |
| 3059 | * calling this function otherwise this call will fail. |
| 3060 | */ |
| 3061 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) |
| 3062 | { |
| 3063 | int ret = 0; |
| 3064 | |
Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 3065 | regulator_lock_supply(regulator->rdev); |
Sascha Hauer | a9f226b | 2015-10-13 12:45:25 +0200 | [diff] [blame] | 3066 | |
| 3067 | ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV); |
| 3068 | |
Sascha Hauer | fc42112 | 2015-10-20 14:37:28 +0200 | [diff] [blame] | 3069 | regulator_unlock_supply(regulator->rdev); |
Sascha Hauer | a9f226b | 2015-10-13 12:45:25 +0200 | [diff] [blame] | 3070 | |
Paolo Pisati | 92d7a55 | 2012-12-13 10:13:00 +0100 | [diff] [blame] | 3071 | return ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3072 | } |
| 3073 | EXPORT_SYMBOL_GPL(regulator_set_voltage); |
| 3074 | |
Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 3075 | /** |
Linus Walleij | 88cd222b | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 3076 | * regulator_set_voltage_time - get raise/fall time |
| 3077 | * @regulator: regulator source |
| 3078 | * @old_uV: starting voltage in microvolts |
| 3079 | * @new_uV: target voltage in microvolts |
| 3080 | * |
| 3081 | * Provided with the starting and ending voltage, this function attempts to |
| 3082 | * calculate the time in microseconds required to rise or fall to this new |
| 3083 | * voltage. |
| 3084 | */ |
| 3085 | int regulator_set_voltage_time(struct regulator *regulator, |
| 3086 | int old_uV, int new_uV) |
| 3087 | { |
Guodong Xu | 272e231 | 2014-08-13 19:33:38 +0800 | [diff] [blame] | 3088 | struct regulator_dev *rdev = regulator->rdev; |
| 3089 | const struct regulator_ops *ops = rdev->desc->ops; |
Linus Walleij | 88cd222b | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 3090 | int old_sel = -1; |
| 3091 | int new_sel = -1; |
| 3092 | int voltage; |
| 3093 | int i; |
| 3094 | |
Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 3095 | if (ops->set_voltage_time) |
| 3096 | return ops->set_voltage_time(rdev, old_uV, new_uV); |
| 3097 | else if (!ops->set_voltage_time_sel) |
| 3098 | return _regulator_set_voltage_time(rdev, old_uV, new_uV); |
| 3099 | |
Linus Walleij | 88cd222b | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 3100 | /* Currently requires operations to do this */ |
Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 3101 | if (!ops->list_voltage || !rdev->desc->n_voltages) |
Linus Walleij | 88cd222b | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 3102 | return -EINVAL; |
| 3103 | |
| 3104 | for (i = 0; i < rdev->desc->n_voltages; i++) { |
| 3105 | /* We only look for exact voltage matches here */ |
| 3106 | voltage = regulator_list_voltage(regulator, i); |
| 3107 | if (voltage < 0) |
| 3108 | return -EINVAL; |
| 3109 | if (voltage == 0) |
| 3110 | continue; |
| 3111 | if (voltage == old_uV) |
| 3112 | old_sel = i; |
| 3113 | if (voltage == new_uV) |
| 3114 | new_sel = i; |
| 3115 | } |
| 3116 | |
| 3117 | if (old_sel < 0 || new_sel < 0) |
| 3118 | return -EINVAL; |
| 3119 | |
| 3120 | return ops->set_voltage_time_sel(rdev, old_sel, new_sel); |
| 3121 | } |
| 3122 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time); |
| 3123 | |
| 3124 | /** |
Randy Dunlap | 296c656 | 2012-08-18 17:44:14 -0700 | [diff] [blame] | 3125 | * regulator_set_voltage_time_sel - get raise/fall time |
| 3126 | * @rdev: regulator source device |
Yadwinder Singh Brar | 98a175b | 2012-06-09 16:40:38 +0530 | [diff] [blame] | 3127 | * @old_selector: selector for starting voltage |
| 3128 | * @new_selector: selector for target voltage |
| 3129 | * |
| 3130 | * Provided with the starting and target voltage selectors, this function |
| 3131 | * returns time in microseconds required to rise or fall to this new voltage |
| 3132 | * |
Axel Lin | f11d08c | 2012-06-19 09:38:45 +0800 | [diff] [blame] | 3133 | * Drivers providing ramp_delay in regulation_constraints can use this as their |
Axel Lin | 398715a | 2012-06-18 10:11:28 +0800 | [diff] [blame] | 3134 | * set_voltage_time_sel() operation. |
Yadwinder Singh Brar | 98a175b | 2012-06-09 16:40:38 +0530 | [diff] [blame] | 3135 | */ |
| 3136 | int regulator_set_voltage_time_sel(struct regulator_dev *rdev, |
| 3137 | unsigned int old_selector, |
| 3138 | unsigned int new_selector) |
| 3139 | { |
Axel Lin | f11d08c | 2012-06-19 09:38:45 +0800 | [diff] [blame] | 3140 | int old_volt, new_volt; |
Axel Lin | 398715a | 2012-06-18 10:11:28 +0800 | [diff] [blame] | 3141 | |
Axel Lin | f11d08c | 2012-06-19 09:38:45 +0800 | [diff] [blame] | 3142 | /* sanity check */ |
| 3143 | if (!rdev->desc->ops->list_voltage) |
| 3144 | return -EINVAL; |
Axel Lin | 398715a | 2012-06-18 10:11:28 +0800 | [diff] [blame] | 3145 | |
Axel Lin | f11d08c | 2012-06-19 09:38:45 +0800 | [diff] [blame] | 3146 | old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); |
| 3147 | new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); |
| 3148 | |
Matthias Kaehlcke | 73e705b | 2016-09-14 09:52:08 -0700 | [diff] [blame] | 3149 | if (rdev->desc->ops->set_voltage_time) |
| 3150 | return rdev->desc->ops->set_voltage_time(rdev, old_volt, |
| 3151 | new_volt); |
| 3152 | else |
| 3153 | return _regulator_set_voltage_time(rdev, old_volt, new_volt); |
Yadwinder Singh Brar | 98a175b | 2012-06-09 16:40:38 +0530 | [diff] [blame] | 3154 | } |
Mark Brown | b19dbf7 | 2012-06-23 11:34:20 +0100 | [diff] [blame] | 3155 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel); |
Yadwinder Singh Brar | 98a175b | 2012-06-09 16:40:38 +0530 | [diff] [blame] | 3156 | |
| 3157 | /** |
Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 3158 | * regulator_sync_voltage - re-apply last regulator output voltage |
| 3159 | * @regulator: regulator source |
| 3160 | * |
| 3161 | * Re-apply the last configured voltage. This is intended to be used |
| 3162 | * where some external control source the consumer is cooperating with |
| 3163 | * has caused the configured voltage to change. |
| 3164 | */ |
| 3165 | int regulator_sync_voltage(struct regulator *regulator) |
| 3166 | { |
| 3167 | struct regulator_dev *rdev = regulator->rdev; |
| 3168 | int ret, min_uV, max_uV; |
| 3169 | |
| 3170 | mutex_lock(&rdev->mutex); |
| 3171 | |
| 3172 | if (!rdev->desc->ops->set_voltage && |
| 3173 | !rdev->desc->ops->set_voltage_sel) { |
| 3174 | ret = -EINVAL; |
| 3175 | goto out; |
| 3176 | } |
| 3177 | |
| 3178 | /* This is only going to work if we've had a voltage configured. */ |
| 3179 | if (!regulator->min_uV && !regulator->max_uV) { |
| 3180 | ret = -EINVAL; |
| 3181 | goto out; |
| 3182 | } |
| 3183 | |
| 3184 | min_uV = regulator->min_uV; |
| 3185 | max_uV = regulator->max_uV; |
| 3186 | |
| 3187 | /* This should be a paranoia check... */ |
| 3188 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); |
| 3189 | if (ret < 0) |
| 3190 | goto out; |
| 3191 | |
| 3192 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV); |
| 3193 | if (ret < 0) |
| 3194 | goto out; |
| 3195 | |
| 3196 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); |
| 3197 | |
| 3198 | out: |
| 3199 | mutex_unlock(&rdev->mutex); |
| 3200 | return ret; |
| 3201 | } |
| 3202 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); |
| 3203 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3204 | static int _regulator_get_voltage(struct regulator_dev *rdev) |
| 3205 | { |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3206 | int sel, ret; |
Mark Brown | fef9501 | 2016-04-07 16:22:36 +0200 | [diff] [blame] | 3207 | bool bypassed; |
| 3208 | |
| 3209 | if (rdev->desc->ops->get_bypass) { |
| 3210 | ret = rdev->desc->ops->get_bypass(rdev, &bypassed); |
| 3211 | if (ret < 0) |
| 3212 | return ret; |
| 3213 | if (bypassed) { |
| 3214 | /* if bypassed the regulator must have a supply */ |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 3215 | if (!rdev->supply) { |
| 3216 | rdev_err(rdev, |
| 3217 | "bypassed regulator has no supply!\n"); |
| 3218 | return -EPROBE_DEFER; |
| 3219 | } |
Mark Brown | fef9501 | 2016-04-07 16:22:36 +0200 | [diff] [blame] | 3220 | |
| 3221 | return _regulator_get_voltage(rdev->supply->rdev); |
| 3222 | } |
| 3223 | } |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 3224 | |
| 3225 | if (rdev->desc->ops->get_voltage_sel) { |
| 3226 | sel = rdev->desc->ops->get_voltage_sel(rdev); |
| 3227 | if (sel < 0) |
| 3228 | return sel; |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3229 | ret = rdev->desc->ops->list_voltage(rdev, sel); |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 3230 | } else if (rdev->desc->ops->get_voltage) { |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3231 | ret = rdev->desc->ops->get_voltage(rdev); |
Mark Brown | f7df20e | 2012-08-09 16:42:19 +0100 | [diff] [blame] | 3232 | } else if (rdev->desc->ops->list_voltage) { |
| 3233 | ret = rdev->desc->ops->list_voltage(rdev, 0); |
Laxman Dewangan | 5a52360 | 2013-09-10 15:45:05 +0530 | [diff] [blame] | 3234 | } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { |
| 3235 | ret = rdev->desc->fixed_uV; |
Javier Martinez Canillas | e303996 | 2014-07-29 18:28:55 +0200 | [diff] [blame] | 3236 | } else if (rdev->supply) { |
Mark Brown | d9b96d3 | 2015-11-03 05:58:14 +0000 | [diff] [blame] | 3237 | ret = _regulator_get_voltage(rdev->supply->rdev); |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 3238 | } else { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3239 | return -EINVAL; |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 3240 | } |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3241 | |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 3242 | if (ret < 0) |
| 3243 | return ret; |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 3244 | return ret - rdev->constraints->uV_offset; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3245 | } |
| 3246 | |
| 3247 | /** |
| 3248 | * regulator_get_voltage - get regulator output voltage |
| 3249 | * @regulator: regulator source |
| 3250 | * |
| 3251 | * This returns the current regulator voltage in uV. |
| 3252 | * |
| 3253 | * NOTE: If the regulator is disabled it will return the voltage value. This |
| 3254 | * function should not be used to determine regulator state. |
| 3255 | */ |
| 3256 | int regulator_get_voltage(struct regulator *regulator) |
| 3257 | { |
| 3258 | int ret; |
| 3259 | |
Mark Brown | d9b96d3 | 2015-11-03 05:58:14 +0000 | [diff] [blame] | 3260 | regulator_lock_supply(regulator->rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3261 | |
| 3262 | ret = _regulator_get_voltage(regulator->rdev); |
| 3263 | |
Mark Brown | d9b96d3 | 2015-11-03 05:58:14 +0000 | [diff] [blame] | 3264 | regulator_unlock_supply(regulator->rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3265 | |
| 3266 | return ret; |
| 3267 | } |
| 3268 | EXPORT_SYMBOL_GPL(regulator_get_voltage); |
| 3269 | |
| 3270 | /** |
| 3271 | * regulator_set_current_limit - set regulator output current limit |
| 3272 | * @regulator: regulator source |
Charles Keepax | ce0d10f | 2013-05-21 15:04:07 +0100 | [diff] [blame] | 3273 | * @min_uA: Minimum supported current in uA |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3274 | * @max_uA: Maximum supported current in uA |
| 3275 | * |
| 3276 | * Sets current sink to the desired output current. This can be set during |
| 3277 | * any regulator state. IOW, regulator can be disabled or enabled. |
| 3278 | * |
| 3279 | * If the regulator is enabled then the current will change to the new value |
| 3280 | * immediately otherwise if the regulator is disabled the regulator will |
| 3281 | * output at the new current when enabled. |
| 3282 | * |
| 3283 | * NOTE: Regulator system constraints must be set for this regulator before |
| 3284 | * calling this function otherwise this call will fail. |
| 3285 | */ |
| 3286 | int regulator_set_current_limit(struct regulator *regulator, |
| 3287 | int min_uA, int max_uA) |
| 3288 | { |
| 3289 | struct regulator_dev *rdev = regulator->rdev; |
| 3290 | int ret; |
| 3291 | |
| 3292 | mutex_lock(&rdev->mutex); |
| 3293 | |
| 3294 | /* sanity check */ |
| 3295 | if (!rdev->desc->ops->set_current_limit) { |
| 3296 | ret = -EINVAL; |
| 3297 | goto out; |
| 3298 | } |
| 3299 | |
| 3300 | /* constraints check */ |
| 3301 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); |
| 3302 | if (ret < 0) |
| 3303 | goto out; |
| 3304 | |
| 3305 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); |
| 3306 | out: |
| 3307 | mutex_unlock(&rdev->mutex); |
| 3308 | return ret; |
| 3309 | } |
| 3310 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); |
| 3311 | |
| 3312 | static int _regulator_get_current_limit(struct regulator_dev *rdev) |
| 3313 | { |
| 3314 | int ret; |
| 3315 | |
| 3316 | mutex_lock(&rdev->mutex); |
| 3317 | |
| 3318 | /* sanity check */ |
| 3319 | if (!rdev->desc->ops->get_current_limit) { |
| 3320 | ret = -EINVAL; |
| 3321 | goto out; |
| 3322 | } |
| 3323 | |
| 3324 | ret = rdev->desc->ops->get_current_limit(rdev); |
| 3325 | out: |
| 3326 | mutex_unlock(&rdev->mutex); |
| 3327 | return ret; |
| 3328 | } |
| 3329 | |
| 3330 | /** |
| 3331 | * regulator_get_current_limit - get regulator output current |
| 3332 | * @regulator: regulator source |
| 3333 | * |
| 3334 | * This returns the current supplied by the specified current sink in uA. |
| 3335 | * |
| 3336 | * NOTE: If the regulator is disabled it will return the current value. This |
| 3337 | * function should not be used to determine regulator state. |
| 3338 | */ |
| 3339 | int regulator_get_current_limit(struct regulator *regulator) |
| 3340 | { |
| 3341 | return _regulator_get_current_limit(regulator->rdev); |
| 3342 | } |
| 3343 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); |
| 3344 | |
| 3345 | /** |
| 3346 | * regulator_set_mode - set regulator operating mode |
| 3347 | * @regulator: regulator source |
| 3348 | * @mode: operating mode - one of the REGULATOR_MODE constants |
| 3349 | * |
| 3350 | * Set regulator operating mode to increase regulator efficiency or improve |
| 3351 | * regulation performance. |
| 3352 | * |
| 3353 | * NOTE: Regulator system constraints must be set for this regulator before |
| 3354 | * calling this function otherwise this call will fail. |
| 3355 | */ |
| 3356 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) |
| 3357 | { |
| 3358 | struct regulator_dev *rdev = regulator->rdev; |
| 3359 | int ret; |
Sundar R Iyer | 500b4ac | 2010-05-17 21:24:48 +0530 | [diff] [blame] | 3360 | int regulator_curr_mode; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3361 | |
| 3362 | mutex_lock(&rdev->mutex); |
| 3363 | |
| 3364 | /* sanity check */ |
| 3365 | if (!rdev->desc->ops->set_mode) { |
| 3366 | ret = -EINVAL; |
| 3367 | goto out; |
| 3368 | } |
| 3369 | |
Sundar R Iyer | 500b4ac | 2010-05-17 21:24:48 +0530 | [diff] [blame] | 3370 | /* return if the same mode is requested */ |
| 3371 | if (rdev->desc->ops->get_mode) { |
| 3372 | regulator_curr_mode = rdev->desc->ops->get_mode(rdev); |
| 3373 | if (regulator_curr_mode == mode) { |
| 3374 | ret = 0; |
| 3375 | goto out; |
| 3376 | } |
| 3377 | } |
| 3378 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3379 | /* constraints check */ |
Axel Lin | 22c51b4 | 2011-04-01 18:25:25 +0800 | [diff] [blame] | 3380 | ret = regulator_mode_constrain(rdev, &mode); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3381 | if (ret < 0) |
| 3382 | goto out; |
| 3383 | |
| 3384 | ret = rdev->desc->ops->set_mode(rdev, mode); |
| 3385 | out: |
| 3386 | mutex_unlock(&rdev->mutex); |
| 3387 | return ret; |
| 3388 | } |
| 3389 | EXPORT_SYMBOL_GPL(regulator_set_mode); |
| 3390 | |
| 3391 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) |
| 3392 | { |
| 3393 | int ret; |
| 3394 | |
| 3395 | mutex_lock(&rdev->mutex); |
| 3396 | |
| 3397 | /* sanity check */ |
| 3398 | if (!rdev->desc->ops->get_mode) { |
| 3399 | ret = -EINVAL; |
| 3400 | goto out; |
| 3401 | } |
| 3402 | |
| 3403 | ret = rdev->desc->ops->get_mode(rdev); |
| 3404 | out: |
| 3405 | mutex_unlock(&rdev->mutex); |
| 3406 | return ret; |
| 3407 | } |
| 3408 | |
| 3409 | /** |
| 3410 | * regulator_get_mode - get regulator operating mode |
| 3411 | * @regulator: regulator source |
| 3412 | * |
| 3413 | * Get the current regulator operating mode. |
| 3414 | */ |
| 3415 | unsigned int regulator_get_mode(struct regulator *regulator) |
| 3416 | { |
| 3417 | return _regulator_get_mode(regulator->rdev); |
| 3418 | } |
| 3419 | EXPORT_SYMBOL_GPL(regulator_get_mode); |
| 3420 | |
| 3421 | /** |
Bjorn Andersson | e39ce48 | 2015-02-11 19:35:27 -0800 | [diff] [blame] | 3422 | * regulator_set_load - set regulator load |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3423 | * @regulator: regulator source |
| 3424 | * @uA_load: load current |
| 3425 | * |
| 3426 | * Notifies the regulator core of a new device load. This is then used by |
| 3427 | * DRMS (if enabled by constraints) to set the most efficient regulator |
| 3428 | * operating mode for the new regulator loading. |
| 3429 | * |
| 3430 | * Consumer devices notify their supply regulator of the maximum power |
| 3431 | * they will require (can be taken from device datasheet in the power |
| 3432 | * consumption tables) when they change operational status and hence power |
| 3433 | * state. Examples of operational state changes that can affect power |
| 3434 | * consumption are :- |
| 3435 | * |
| 3436 | * o Device is opened / closed. |
| 3437 | * o Device I/O is about to begin or has just finished. |
| 3438 | * o Device is idling in between work. |
| 3439 | * |
| 3440 | * This information is also exported via sysfs to userspace. |
| 3441 | * |
| 3442 | * DRMS will sum the total requested load on the regulator and change |
| 3443 | * to the most efficient operating mode if platform constraints allow. |
| 3444 | * |
Bjorn Andersson | e39ce48 | 2015-02-11 19:35:27 -0800 | [diff] [blame] | 3445 | * On error a negative errno is returned. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3446 | */ |
Bjorn Andersson | e39ce48 | 2015-02-11 19:35:27 -0800 | [diff] [blame] | 3447 | int regulator_set_load(struct regulator *regulator, int uA_load) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3448 | { |
| 3449 | struct regulator_dev *rdev = regulator->rdev; |
Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 3450 | int ret; |
Stephen Boyd | d92d95b6 | 2012-07-02 19:21:06 -0700 | [diff] [blame] | 3451 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3452 | mutex_lock(&rdev->mutex); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3453 | regulator->uA_load = uA_load; |
Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 3454 | ret = drms_uA_update(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3455 | mutex_unlock(&rdev->mutex); |
Bjorn Andersson | 8460ef3 | 2015-01-27 18:46:31 -0800 | [diff] [blame] | 3456 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3457 | return ret; |
| 3458 | } |
Bjorn Andersson | e39ce48 | 2015-02-11 19:35:27 -0800 | [diff] [blame] | 3459 | EXPORT_SYMBOL_GPL(regulator_set_load); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3460 | |
| 3461 | /** |
Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3462 | * regulator_allow_bypass - allow the regulator to go into bypass mode |
| 3463 | * |
| 3464 | * @regulator: Regulator to configure |
Nishanth Menon | 9345dfb | 2013-02-28 18:44:54 -0600 | [diff] [blame] | 3465 | * @enable: enable or disable bypass mode |
Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3466 | * |
| 3467 | * Allow the regulator to go into bypass mode if all other consumers |
| 3468 | * for the regulator also enable bypass mode and the machine |
| 3469 | * constraints allow this. Bypass mode means that the regulator is |
| 3470 | * simply passing the input directly to the output with no regulation. |
| 3471 | */ |
| 3472 | int regulator_allow_bypass(struct regulator *regulator, bool enable) |
| 3473 | { |
| 3474 | struct regulator_dev *rdev = regulator->rdev; |
| 3475 | int ret = 0; |
| 3476 | |
| 3477 | if (!rdev->desc->ops->set_bypass) |
| 3478 | return 0; |
| 3479 | |
WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 3480 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS)) |
Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3481 | return 0; |
| 3482 | |
| 3483 | mutex_lock(&rdev->mutex); |
| 3484 | |
| 3485 | if (enable && !regulator->bypass) { |
| 3486 | rdev->bypass_count++; |
| 3487 | |
David Collins | cea4135 | 2017-01-10 17:34:21 -0800 | [diff] [blame] | 3488 | if (rdev->bypass_count == rdev->open_count - |
| 3489 | rdev->open_offset) { |
Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3490 | ret = rdev->desc->ops->set_bypass(rdev, enable); |
| 3491 | if (ret != 0) |
| 3492 | rdev->bypass_count--; |
| 3493 | } |
| 3494 | |
| 3495 | } else if (!enable && regulator->bypass) { |
| 3496 | rdev->bypass_count--; |
| 3497 | |
David Collins | cea4135 | 2017-01-10 17:34:21 -0800 | [diff] [blame] | 3498 | if (rdev->bypass_count != rdev->open_count - |
| 3499 | rdev->open_offset) { |
Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3500 | ret = rdev->desc->ops->set_bypass(rdev, enable); |
| 3501 | if (ret != 0) |
| 3502 | rdev->bypass_count++; |
| 3503 | } |
| 3504 | } |
| 3505 | |
| 3506 | if (ret == 0) |
| 3507 | regulator->bypass = enable; |
| 3508 | |
| 3509 | mutex_unlock(&rdev->mutex); |
| 3510 | |
| 3511 | return ret; |
| 3512 | } |
| 3513 | EXPORT_SYMBOL_GPL(regulator_allow_bypass); |
| 3514 | |
| 3515 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3516 | * regulator_register_notifier - register regulator event notifier |
| 3517 | * @regulator: regulator source |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3518 | * @nb: notifier block |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3519 | * |
| 3520 | * Register notifier block to receive regulator events. |
| 3521 | */ |
| 3522 | int regulator_register_notifier(struct regulator *regulator, |
| 3523 | struct notifier_block *nb) |
| 3524 | { |
| 3525 | return blocking_notifier_chain_register(®ulator->rdev->notifier, |
| 3526 | nb); |
| 3527 | } |
| 3528 | EXPORT_SYMBOL_GPL(regulator_register_notifier); |
| 3529 | |
| 3530 | /** |
| 3531 | * regulator_unregister_notifier - unregister regulator event notifier |
| 3532 | * @regulator: regulator source |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3533 | * @nb: notifier block |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3534 | * |
| 3535 | * Unregister regulator event notifier block. |
| 3536 | */ |
| 3537 | int regulator_unregister_notifier(struct regulator *regulator, |
| 3538 | struct notifier_block *nb) |
| 3539 | { |
| 3540 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, |
| 3541 | nb); |
| 3542 | } |
| 3543 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); |
| 3544 | |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 3545 | /* notify regulator consumers and downstream regulator consumers. |
| 3546 | * Note mutex must be held by caller. |
| 3547 | */ |
Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 3548 | static int _notifier_call_chain(struct regulator_dev *rdev, |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3549 | unsigned long event, void *data) |
| 3550 | { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3551 | /* call rdev chain first */ |
Heiko Stübner | 7179569 | 2014-08-28 12:36:04 -0700 | [diff] [blame] | 3552 | return blocking_notifier_call_chain(&rdev->notifier, event, data); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | /** |
| 3556 | * regulator_bulk_get - get multiple regulator consumers |
| 3557 | * |
| 3558 | * @dev: Device to supply |
| 3559 | * @num_consumers: Number of consumers to register |
| 3560 | * @consumers: Configuration of consumers; clients are stored here. |
| 3561 | * |
| 3562 | * @return 0 on success, an errno on failure. |
| 3563 | * |
| 3564 | * This helper function allows drivers to get several regulator |
| 3565 | * consumers in one operation. If any of the regulators cannot be |
| 3566 | * acquired then any regulators that were allocated will be freed |
| 3567 | * before returning to the caller. |
| 3568 | */ |
| 3569 | int regulator_bulk_get(struct device *dev, int num_consumers, |
| 3570 | struct regulator_bulk_data *consumers) |
| 3571 | { |
| 3572 | int i; |
| 3573 | int ret; |
| 3574 | |
| 3575 | for (i = 0; i < num_consumers; i++) |
| 3576 | consumers[i].consumer = NULL; |
| 3577 | |
| 3578 | for (i = 0; i < num_consumers; i++) { |
Bjorn Andersson | 565f9b0 | 2016-08-16 11:50:32 -0700 | [diff] [blame] | 3579 | consumers[i].consumer = regulator_get(dev, |
| 3580 | consumers[i].supply); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3581 | if (IS_ERR(consumers[i].consumer)) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3582 | ret = PTR_ERR(consumers[i].consumer); |
Mark Brown | 5b30762 | 2009-10-13 13:06:49 +0100 | [diff] [blame] | 3583 | dev_err(dev, "Failed to get supply '%s': %d\n", |
| 3584 | consumers[i].supply, ret); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3585 | consumers[i].consumer = NULL; |
| 3586 | goto err; |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | return 0; |
| 3591 | |
| 3592 | err: |
Axel Lin | b29c769 | 2012-02-20 10:32:16 +0800 | [diff] [blame] | 3593 | while (--i >= 0) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3594 | regulator_put(consumers[i].consumer); |
| 3595 | |
| 3596 | return ret; |
| 3597 | } |
| 3598 | EXPORT_SYMBOL_GPL(regulator_bulk_get); |
| 3599 | |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3600 | static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) |
| 3601 | { |
| 3602 | struct regulator_bulk_data *bulk = data; |
| 3603 | |
| 3604 | bulk->ret = regulator_enable(bulk->consumer); |
| 3605 | } |
| 3606 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3607 | /** |
| 3608 | * regulator_bulk_enable - enable multiple regulator consumers |
| 3609 | * |
| 3610 | * @num_consumers: Number of consumers |
| 3611 | * @consumers: Consumer data; clients are stored here. |
| 3612 | * @return 0 on success, an errno on failure |
| 3613 | * |
| 3614 | * This convenience API allows consumers to enable multiple regulator |
| 3615 | * clients in a single API call. If any consumers cannot be enabled |
| 3616 | * then any others that were enabled will be disabled again prior to |
| 3617 | * return. |
| 3618 | */ |
| 3619 | int regulator_bulk_enable(int num_consumers, |
| 3620 | struct regulator_bulk_data *consumers) |
| 3621 | { |
Dan Williams | 2955b47 | 2012-07-09 19:33:25 -0700 | [diff] [blame] | 3622 | ASYNC_DOMAIN_EXCLUSIVE(async_domain); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3623 | int i; |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3624 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3625 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 3626 | for (i = 0; i < num_consumers; i++) { |
| 3627 | if (consumers[i].consumer->always_on) |
| 3628 | consumers[i].ret = 0; |
| 3629 | else |
| 3630 | async_schedule_domain(regulator_bulk_enable_async, |
| 3631 | &consumers[i], &async_domain); |
| 3632 | } |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3633 | |
| 3634 | async_synchronize_full_domain(&async_domain); |
| 3635 | |
| 3636 | /* If any consumer failed we need to unwind any that succeeded */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3637 | for (i = 0; i < num_consumers; i++) { |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3638 | if (consumers[i].ret != 0) { |
| 3639 | ret = consumers[i].ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3640 | goto err; |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 3641 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3642 | } |
| 3643 | |
| 3644 | return 0; |
| 3645 | |
| 3646 | err: |
Andrzej Hajda | fbe3105 | 2013-03-01 12:24:05 +0100 | [diff] [blame] | 3647 | for (i = 0; i < num_consumers; i++) { |
| 3648 | if (consumers[i].ret < 0) |
| 3649 | pr_err("Failed to enable %s: %d\n", consumers[i].supply, |
| 3650 | consumers[i].ret); |
| 3651 | else |
| 3652 | regulator_disable(consumers[i].consumer); |
| 3653 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3654 | |
| 3655 | return ret; |
| 3656 | } |
| 3657 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); |
| 3658 | |
| 3659 | /** |
| 3660 | * regulator_bulk_disable - disable multiple regulator consumers |
| 3661 | * |
| 3662 | * @num_consumers: Number of consumers |
| 3663 | * @consumers: Consumer data; clients are stored here. |
| 3664 | * @return 0 on success, an errno on failure |
| 3665 | * |
| 3666 | * This convenience API allows consumers to disable multiple regulator |
Sylwester Nawrocki | 49e2263 | 2012-01-25 12:35:38 +0100 | [diff] [blame] | 3667 | * clients in a single API call. If any consumers cannot be disabled |
| 3668 | * then any others that were disabled will be enabled again prior to |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3669 | * return. |
| 3670 | */ |
| 3671 | int regulator_bulk_disable(int num_consumers, |
| 3672 | struct regulator_bulk_data *consumers) |
| 3673 | { |
| 3674 | int i; |
Mark Brown | 01e86f4 | 2012-03-28 21:36:38 +0100 | [diff] [blame] | 3675 | int ret, r; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3676 | |
Sylwester Nawrocki | 49e2263 | 2012-01-25 12:35:38 +0100 | [diff] [blame] | 3677 | for (i = num_consumers - 1; i >= 0; --i) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3678 | ret = regulator_disable(consumers[i].consumer); |
| 3679 | if (ret != 0) |
| 3680 | goto err; |
| 3681 | } |
| 3682 | |
| 3683 | return 0; |
| 3684 | |
| 3685 | err: |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 3686 | pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); |
Mark Brown | 01e86f4 | 2012-03-28 21:36:38 +0100 | [diff] [blame] | 3687 | for (++i; i < num_consumers; ++i) { |
| 3688 | r = regulator_enable(consumers[i].consumer); |
| 3689 | if (r != 0) |
| 3690 | pr_err("Failed to reename %s: %d\n", |
| 3691 | consumers[i].supply, r); |
| 3692 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3693 | |
| 3694 | return ret; |
| 3695 | } |
| 3696 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); |
| 3697 | |
| 3698 | /** |
Donggeun Kim | e1de2f4 | 2012-01-03 16:22:03 +0900 | [diff] [blame] | 3699 | * regulator_bulk_force_disable - force disable multiple regulator consumers |
| 3700 | * |
| 3701 | * @num_consumers: Number of consumers |
| 3702 | * @consumers: Consumer data; clients are stored here. |
| 3703 | * @return 0 on success, an errno on failure |
| 3704 | * |
| 3705 | * This convenience API allows consumers to forcibly disable multiple regulator |
| 3706 | * clients in a single API call. |
| 3707 | * NOTE: This should be used for situations when device damage will |
| 3708 | * likely occur if the regulators are not disabled (e.g. over temp). |
| 3709 | * Although regulator_force_disable function call for some consumers can |
| 3710 | * return error numbers, the function is called for all consumers. |
| 3711 | */ |
| 3712 | int regulator_bulk_force_disable(int num_consumers, |
| 3713 | struct regulator_bulk_data *consumers) |
| 3714 | { |
| 3715 | int i; |
| 3716 | int ret; |
| 3717 | |
| 3718 | for (i = 0; i < num_consumers; i++) |
| 3719 | consumers[i].ret = |
| 3720 | regulator_force_disable(consumers[i].consumer); |
| 3721 | |
| 3722 | for (i = 0; i < num_consumers; i++) { |
| 3723 | if (consumers[i].ret != 0) { |
| 3724 | ret = consumers[i].ret; |
| 3725 | goto out; |
| 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | return 0; |
| 3730 | out: |
| 3731 | return ret; |
| 3732 | } |
| 3733 | EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); |
| 3734 | |
| 3735 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3736 | * regulator_bulk_free - free multiple regulator consumers |
| 3737 | * |
| 3738 | * @num_consumers: Number of consumers |
| 3739 | * @consumers: Consumer data; clients are stored here. |
| 3740 | * |
| 3741 | * This convenience API allows consumers to free multiple regulator |
| 3742 | * clients in a single API call. |
| 3743 | */ |
| 3744 | void regulator_bulk_free(int num_consumers, |
| 3745 | struct regulator_bulk_data *consumers) |
| 3746 | { |
| 3747 | int i; |
| 3748 | |
| 3749 | for (i = 0; i < num_consumers; i++) { |
| 3750 | regulator_put(consumers[i].consumer); |
| 3751 | consumers[i].consumer = NULL; |
| 3752 | } |
| 3753 | } |
| 3754 | EXPORT_SYMBOL_GPL(regulator_bulk_free); |
| 3755 | |
| 3756 | /** |
| 3757 | * regulator_notifier_call_chain - call regulator event notifier |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3758 | * @rdev: regulator source |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3759 | * @event: notifier block |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3760 | * @data: callback-specific data. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3761 | * |
| 3762 | * Called by regulator drivers to notify clients a regulator event has |
| 3763 | * occurred. We also notify regulator clients downstream. |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 3764 | * Note lock must be held by caller. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3765 | */ |
| 3766 | int regulator_notifier_call_chain(struct regulator_dev *rdev, |
| 3767 | unsigned long event, void *data) |
| 3768 | { |
Krzysztof Kozlowski | 70cfef2 | 2015-06-29 09:04:43 +0900 | [diff] [blame] | 3769 | lockdep_assert_held_once(&rdev->mutex); |
| 3770 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3771 | _notifier_call_chain(rdev, event, data); |
| 3772 | return NOTIFY_DONE; |
| 3773 | |
| 3774 | } |
| 3775 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); |
| 3776 | |
Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 3777 | /** |
| 3778 | * regulator_mode_to_status - convert a regulator mode into a status |
| 3779 | * |
| 3780 | * @mode: Mode to convert |
| 3781 | * |
| 3782 | * Convert a regulator mode into a status. |
| 3783 | */ |
| 3784 | int regulator_mode_to_status(unsigned int mode) |
| 3785 | { |
| 3786 | switch (mode) { |
| 3787 | case REGULATOR_MODE_FAST: |
| 3788 | return REGULATOR_STATUS_FAST; |
| 3789 | case REGULATOR_MODE_NORMAL: |
| 3790 | return REGULATOR_STATUS_NORMAL; |
| 3791 | case REGULATOR_MODE_IDLE: |
| 3792 | return REGULATOR_STATUS_IDLE; |
Krystian Garbaciak | 03ffcf3 | 2012-07-12 11:50:38 +0100 | [diff] [blame] | 3793 | case REGULATOR_MODE_STANDBY: |
Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 3794 | return REGULATOR_STATUS_STANDBY; |
| 3795 | default: |
Krystian Garbaciak | 1beaf76 | 2012-07-12 13:53:35 +0100 | [diff] [blame] | 3796 | return REGULATOR_STATUS_UNDEFINED; |
Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 3797 | } |
| 3798 | } |
| 3799 | EXPORT_SYMBOL_GPL(regulator_mode_to_status); |
| 3800 | |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3801 | static struct attribute *regulator_dev_attrs[] = { |
| 3802 | &dev_attr_name.attr, |
| 3803 | &dev_attr_num_users.attr, |
| 3804 | &dev_attr_type.attr, |
| 3805 | &dev_attr_microvolts.attr, |
| 3806 | &dev_attr_microamps.attr, |
| 3807 | &dev_attr_opmode.attr, |
| 3808 | &dev_attr_state.attr, |
| 3809 | &dev_attr_status.attr, |
| 3810 | &dev_attr_bypass.attr, |
| 3811 | &dev_attr_requested_microamps.attr, |
| 3812 | &dev_attr_min_microvolts.attr, |
| 3813 | &dev_attr_max_microvolts.attr, |
| 3814 | &dev_attr_min_microamps.attr, |
| 3815 | &dev_attr_max_microamps.attr, |
| 3816 | &dev_attr_suspend_standby_state.attr, |
| 3817 | &dev_attr_suspend_mem_state.attr, |
| 3818 | &dev_attr_suspend_disk_state.attr, |
| 3819 | &dev_attr_suspend_standby_microvolts.attr, |
| 3820 | &dev_attr_suspend_mem_microvolts.attr, |
| 3821 | &dev_attr_suspend_disk_microvolts.attr, |
| 3822 | &dev_attr_suspend_standby_mode.attr, |
| 3823 | &dev_attr_suspend_mem_mode.attr, |
| 3824 | &dev_attr_suspend_disk_mode.attr, |
| 3825 | NULL |
| 3826 | }; |
| 3827 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3828 | /* |
| 3829 | * To avoid cluttering sysfs (and memory) with useless state, only |
| 3830 | * create attributes that can be meaningfully displayed. |
| 3831 | */ |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3832 | static umode_t regulator_attr_is_visible(struct kobject *kobj, |
| 3833 | struct attribute *attr, int idx) |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3834 | { |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3835 | struct device *dev = kobj_to_dev(kobj); |
Geliang Tang | 83080a1 | 2016-01-05 22:07:55 +0800 | [diff] [blame] | 3836 | struct regulator_dev *rdev = dev_to_rdev(dev); |
Guodong Xu | 272e231 | 2014-08-13 19:33:38 +0800 | [diff] [blame] | 3837 | const struct regulator_ops *ops = rdev->desc->ops; |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3838 | umode_t mode = attr->mode; |
| 3839 | |
| 3840 | /* these three are always present */ |
| 3841 | if (attr == &dev_attr_name.attr || |
| 3842 | attr == &dev_attr_num_users.attr || |
| 3843 | attr == &dev_attr_type.attr) |
| 3844 | return mode; |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3845 | |
| 3846 | /* some attributes need specific methods to be displayed */ |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3847 | if (attr == &dev_attr_microvolts.attr) { |
| 3848 | if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || |
| 3849 | (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || |
| 3850 | (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || |
| 3851 | (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1)) |
| 3852 | return mode; |
| 3853 | return 0; |
Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 3854 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3855 | |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3856 | if (attr == &dev_attr_microamps.attr) |
| 3857 | return ops->get_current_limit ? mode : 0; |
| 3858 | |
| 3859 | if (attr == &dev_attr_opmode.attr) |
| 3860 | return ops->get_mode ? mode : 0; |
| 3861 | |
| 3862 | if (attr == &dev_attr_state.attr) |
| 3863 | return (rdev->ena_pin || ops->is_enabled) ? mode : 0; |
| 3864 | |
| 3865 | if (attr == &dev_attr_status.attr) |
| 3866 | return ops->get_status ? mode : 0; |
| 3867 | |
| 3868 | if (attr == &dev_attr_bypass.attr) |
| 3869 | return ops->get_bypass ? mode : 0; |
| 3870 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3871 | /* some attributes are type-specific */ |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3872 | if (attr == &dev_attr_requested_microamps.attr) |
| 3873 | return rdev->desc->type == REGULATOR_CURRENT ? mode : 0; |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3874 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3875 | /* constraints need specific supporting methods */ |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3876 | if (attr == &dev_attr_min_microvolts.attr || |
| 3877 | attr == &dev_attr_max_microvolts.attr) |
| 3878 | return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0; |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3879 | |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3880 | if (attr == &dev_attr_min_microamps.attr || |
| 3881 | attr == &dev_attr_max_microamps.attr) |
| 3882 | return ops->set_current_limit ? mode : 0; |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3883 | |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3884 | if (attr == &dev_attr_suspend_standby_state.attr || |
| 3885 | attr == &dev_attr_suspend_mem_state.attr || |
| 3886 | attr == &dev_attr_suspend_disk_state.attr) |
| 3887 | return mode; |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3888 | |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3889 | if (attr == &dev_attr_suspend_standby_microvolts.attr || |
| 3890 | attr == &dev_attr_suspend_mem_microvolts.attr || |
| 3891 | attr == &dev_attr_suspend_disk_microvolts.attr) |
| 3892 | return ops->set_suspend_voltage ? mode : 0; |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3893 | |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3894 | if (attr == &dev_attr_suspend_standby_mode.attr || |
| 3895 | attr == &dev_attr_suspend_mem_mode.attr || |
| 3896 | attr == &dev_attr_suspend_disk_mode.attr) |
| 3897 | return ops->set_suspend_mode ? mode : 0; |
| 3898 | |
| 3899 | return mode; |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3900 | } |
| 3901 | |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3902 | static const struct attribute_group regulator_dev_group = { |
| 3903 | .attrs = regulator_dev_attrs, |
| 3904 | .is_visible = regulator_attr_is_visible, |
| 3905 | }; |
| 3906 | |
| 3907 | static const struct attribute_group *regulator_dev_groups[] = { |
| 3908 | ®ulator_dev_group, |
| 3909 | NULL |
| 3910 | }; |
| 3911 | |
| 3912 | static void regulator_dev_release(struct device *dev) |
| 3913 | { |
| 3914 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Mark Brown | 29f5f48 | 2015-08-07 20:01:32 +0100 | [diff] [blame] | 3915 | |
| 3916 | kfree(rdev->constraints); |
| 3917 | of_node_put(rdev->dev.of_node); |
Takashi Iwai | 39f802d | 2015-01-30 20:29:31 +0100 | [diff] [blame] | 3918 | kfree(rdev); |
| 3919 | } |
| 3920 | |
| 3921 | static struct class regulator_class = { |
| 3922 | .name = "regulator", |
| 3923 | .dev_release = regulator_dev_release, |
| 3924 | .dev_groups = regulator_dev_groups, |
| 3925 | }; |
| 3926 | |
David Collins | cea4135 | 2017-01-10 17:34:21 -0800 | [diff] [blame] | 3927 | #ifdef CONFIG_DEBUG_FS |
| 3928 | |
| 3929 | static int reg_debug_enable_set(void *data, u64 val) |
| 3930 | { |
| 3931 | struct regulator *regulator = data; |
| 3932 | int ret; |
| 3933 | |
| 3934 | if (val) { |
| 3935 | ret = regulator_enable(regulator); |
| 3936 | if (ret) |
| 3937 | rdev_err(regulator->rdev, "enable failed, ret=%d\n", |
| 3938 | ret); |
| 3939 | } else { |
| 3940 | ret = regulator_disable(regulator); |
| 3941 | if (ret) |
| 3942 | rdev_err(regulator->rdev, "disable failed, ret=%d\n", |
| 3943 | ret); |
| 3944 | } |
| 3945 | |
| 3946 | return ret; |
| 3947 | } |
| 3948 | |
| 3949 | static int reg_debug_enable_get(void *data, u64 *val) |
| 3950 | { |
| 3951 | struct regulator *regulator = data; |
| 3952 | |
| 3953 | *val = regulator_is_enabled(regulator); |
| 3954 | |
| 3955 | return 0; |
| 3956 | } |
| 3957 | DEFINE_SIMPLE_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get, |
| 3958 | reg_debug_enable_set, "%llu\n"); |
| 3959 | |
| 3960 | static int reg_debug_bypass_enable_get(void *data, u64 *val) |
| 3961 | { |
| 3962 | struct regulator *regulator = data; |
| 3963 | struct regulator_dev *rdev = regulator->rdev; |
| 3964 | bool enable = false; |
| 3965 | int ret = 0; |
| 3966 | |
| 3967 | mutex_lock(&rdev->mutex); |
| 3968 | if (rdev->desc->ops->get_bypass) { |
| 3969 | ret = rdev->desc->ops->get_bypass(rdev, &enable); |
| 3970 | if (ret) |
| 3971 | rdev_err(rdev, "get_bypass() failed, ret=%d\n", ret); |
| 3972 | } else { |
| 3973 | enable = (rdev->bypass_count == rdev->open_count |
| 3974 | - rdev->open_offset); |
| 3975 | } |
| 3976 | mutex_unlock(&rdev->mutex); |
| 3977 | |
| 3978 | *val = enable; |
| 3979 | |
| 3980 | return ret; |
| 3981 | } |
| 3982 | |
| 3983 | static int reg_debug_bypass_enable_set(void *data, u64 val) |
| 3984 | { |
| 3985 | struct regulator *regulator = data; |
| 3986 | struct regulator_dev *rdev = regulator->rdev; |
| 3987 | int ret = 0; |
| 3988 | |
| 3989 | mutex_lock(&rdev->mutex); |
| 3990 | rdev->open_offset = 0; |
| 3991 | mutex_unlock(&rdev->mutex); |
| 3992 | |
| 3993 | ret = regulator_allow_bypass(data, val); |
| 3994 | |
| 3995 | return ret; |
| 3996 | } |
| 3997 | DEFINE_SIMPLE_ATTRIBUTE(reg_bypass_enable_fops, reg_debug_bypass_enable_get, |
| 3998 | reg_debug_bypass_enable_set, "%llu\n"); |
| 3999 | |
| 4000 | static int reg_debug_force_disable_set(void *data, u64 val) |
| 4001 | { |
| 4002 | struct regulator *regulator = data; |
| 4003 | int ret = 0; |
| 4004 | |
| 4005 | if (val > 0) { |
| 4006 | ret = regulator_force_disable(regulator); |
| 4007 | if (ret) |
| 4008 | rdev_err(regulator->rdev, "force_disable failed, ret=%d\n", |
| 4009 | ret); |
| 4010 | } |
| 4011 | |
| 4012 | return ret; |
| 4013 | } |
| 4014 | DEFINE_SIMPLE_ATTRIBUTE(reg_force_disable_fops, reg_debug_enable_get, |
| 4015 | reg_debug_force_disable_set, "%llu\n"); |
| 4016 | |
| 4017 | #define MAX_DEBUG_BUF_LEN 50 |
| 4018 | |
| 4019 | static ssize_t reg_debug_voltage_write(struct file *file, |
| 4020 | const char __user *ubuf, size_t count, loff_t *ppos) |
| 4021 | { |
| 4022 | struct regulator *regulator = file->private_data; |
| 4023 | char buf[MAX_DEBUG_BUF_LEN]; |
| 4024 | int ret, filled; |
| 4025 | int min_uV, max_uV = -1; |
| 4026 | |
| 4027 | if (count < MAX_DEBUG_BUF_LEN) { |
| 4028 | if (copy_from_user(buf, ubuf, count)) |
| 4029 | return -EFAULT; |
| 4030 | |
| 4031 | buf[count] = '\0'; |
| 4032 | filled = sscanf(buf, "%d %d", &min_uV, &max_uV); |
| 4033 | |
| 4034 | /* Check that both min and max voltage were specified. */ |
| 4035 | if (filled < 2 || min_uV < 0 || max_uV < min_uV) { |
| 4036 | rdev_err(regulator->rdev, "incorrect values specified: \"%s\"; should be: \"min_uV max_uV\"\n", |
| 4037 | buf); |
| 4038 | return -EINVAL; |
| 4039 | } |
| 4040 | |
| 4041 | ret = regulator_set_voltage(regulator, min_uV, max_uV); |
| 4042 | if (ret) { |
| 4043 | rdev_err(regulator->rdev, "set voltage(%d, %d) failed, ret=%d\n", |
| 4044 | min_uV, max_uV, ret); |
| 4045 | return ret; |
| 4046 | } |
| 4047 | } else { |
| 4048 | rdev_err(regulator->rdev, "voltage request string exceeds maximum buffer size\n"); |
| 4049 | return -EINVAL; |
| 4050 | } |
| 4051 | |
| 4052 | return count; |
| 4053 | } |
| 4054 | |
| 4055 | static ssize_t reg_debug_voltage_read(struct file *file, char __user *ubuf, |
| 4056 | size_t count, loff_t *ppos) |
| 4057 | { |
| 4058 | struct regulator *regulator = file->private_data; |
| 4059 | char buf[MAX_DEBUG_BUF_LEN]; |
| 4060 | int voltage, ret; |
| 4061 | |
| 4062 | voltage = regulator_get_voltage(regulator); |
| 4063 | |
| 4064 | ret = snprintf(buf, MAX_DEBUG_BUF_LEN - 1, "%d\n", voltage); |
| 4065 | |
| 4066 | return simple_read_from_buffer(ubuf, count, ppos, buf, ret); |
| 4067 | } |
| 4068 | |
| 4069 | static int reg_debug_voltage_open(struct inode *inode, struct file *file) |
| 4070 | { |
| 4071 | file->private_data = inode->i_private; |
| 4072 | |
| 4073 | return 0; |
| 4074 | } |
| 4075 | |
| 4076 | static const struct file_operations reg_voltage_fops = { |
| 4077 | .write = reg_debug_voltage_write, |
| 4078 | .open = reg_debug_voltage_open, |
| 4079 | .read = reg_debug_voltage_read, |
| 4080 | }; |
| 4081 | |
| 4082 | static int reg_debug_mode_set(void *data, u64 val) |
| 4083 | { |
| 4084 | struct regulator *regulator = data; |
| 4085 | unsigned int mode = val; |
| 4086 | int ret; |
| 4087 | |
| 4088 | ret = regulator_set_mode(regulator, mode); |
| 4089 | if (ret) |
| 4090 | rdev_err(regulator->rdev, "set mode=%u failed, ret=%d\n", |
| 4091 | mode, ret); |
| 4092 | |
| 4093 | return ret; |
| 4094 | } |
| 4095 | |
| 4096 | static int reg_debug_mode_get(void *data, u64 *val) |
| 4097 | { |
| 4098 | struct regulator *regulator = data; |
| 4099 | int mode; |
| 4100 | |
| 4101 | mode = regulator_get_mode(regulator); |
| 4102 | if (mode < 0) { |
| 4103 | rdev_err(regulator->rdev, "get mode failed, ret=%d\n", mode); |
| 4104 | return mode; |
| 4105 | } |
| 4106 | |
| 4107 | *val = mode; |
| 4108 | |
| 4109 | return 0; |
| 4110 | } |
| 4111 | DEFINE_SIMPLE_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get, reg_debug_mode_set, |
| 4112 | "%llu\n"); |
| 4113 | |
| 4114 | static int reg_debug_set_load(void *data, u64 val) |
| 4115 | { |
| 4116 | struct regulator *regulator = data; |
| 4117 | int load = val; |
| 4118 | int ret; |
| 4119 | |
| 4120 | ret = regulator_set_load(regulator, load); |
| 4121 | if (ret) |
| 4122 | rdev_err(regulator->rdev, "set load=%d failed, ret=%d\n", |
| 4123 | load, ret); |
| 4124 | |
| 4125 | return ret; |
| 4126 | } |
| 4127 | DEFINE_SIMPLE_ATTRIBUTE(reg_set_load_fops, reg_debug_mode_get, |
| 4128 | reg_debug_set_load, "%llu\n"); |
| 4129 | |
| 4130 | static int reg_debug_consumers_show(struct seq_file *m, void *v) |
| 4131 | { |
| 4132 | struct regulator_dev *rdev = m->private; |
| 4133 | struct regulator *reg; |
| 4134 | char *supply_name; |
| 4135 | |
| 4136 | mutex_lock(&rdev->mutex); |
| 4137 | |
| 4138 | /* Print a header if there are consumers. */ |
| 4139 | if (rdev->open_count) |
| 4140 | seq_printf(m, "%-32s EN Min_uV Max_uV load_uA\n", |
| 4141 | "Device-Supply"); |
| 4142 | |
| 4143 | list_for_each_entry(reg, &rdev->consumer_list, list) { |
| 4144 | if (reg->supply_name) |
| 4145 | supply_name = reg->supply_name; |
| 4146 | else |
| 4147 | supply_name = "(null)-(null)"; |
| 4148 | |
| 4149 | seq_printf(m, "%-32s %c %8d %8d %8d\n", supply_name, |
| 4150 | (reg->enabled ? 'Y' : 'N'), reg->min_uV, reg->max_uV, |
| 4151 | reg->uA_load); |
| 4152 | } |
| 4153 | |
| 4154 | mutex_unlock(&rdev->mutex); |
| 4155 | |
| 4156 | return 0; |
| 4157 | } |
| 4158 | |
| 4159 | static int reg_debug_consumers_open(struct inode *inode, struct file *file) |
| 4160 | { |
| 4161 | return single_open(file, reg_debug_consumers_show, inode->i_private); |
| 4162 | } |
| 4163 | |
| 4164 | static const struct file_operations reg_consumers_fops = { |
| 4165 | .owner = THIS_MODULE, |
| 4166 | .open = reg_debug_consumers_open, |
| 4167 | .read = seq_read, |
| 4168 | .llseek = seq_lseek, |
| 4169 | .release = single_release, |
| 4170 | }; |
| 4171 | |
| 4172 | static void rdev_deinit_debugfs(struct regulator_dev *rdev) |
| 4173 | { |
| 4174 | if (!IS_ERR_OR_NULL(rdev)) { |
| 4175 | debugfs_remove_recursive(rdev->debugfs); |
| 4176 | if (rdev->debug_consumer) |
| 4177 | rdev->debug_consumer->debugfs = NULL; |
| 4178 | regulator_put(rdev->debug_consumer); |
| 4179 | } |
| 4180 | } |
| 4181 | |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4182 | static void rdev_init_debugfs(struct regulator_dev *rdev) |
| 4183 | { |
Guenter Roeck | a9eaa81 | 2014-10-17 08:17:03 -0700 | [diff] [blame] | 4184 | struct device *parent = rdev->dev.parent; |
| 4185 | const char *rname = rdev_get_name(rdev); |
| 4186 | char name[NAME_MAX]; |
David Collins | cea4135 | 2017-01-10 17:34:21 -0800 | [diff] [blame] | 4187 | struct regulator *regulator; |
| 4188 | const struct regulator_ops *ops; |
| 4189 | mode_t mode; |
Guenter Roeck | a9eaa81 | 2014-10-17 08:17:03 -0700 | [diff] [blame] | 4190 | |
| 4191 | /* Avoid duplicate debugfs directory names */ |
| 4192 | if (parent && rname == rdev->desc->name) { |
| 4193 | snprintf(name, sizeof(name), "%s-%s", dev_name(parent), |
| 4194 | rname); |
| 4195 | rname = name; |
| 4196 | } |
| 4197 | |
| 4198 | rdev->debugfs = debugfs_create_dir(rname, debugfs_root); |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4199 | if (!rdev->debugfs) { |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4200 | rdev_warn(rdev, "Failed to create debugfs directory\n"); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4201 | return; |
| 4202 | } |
| 4203 | |
| 4204 | debugfs_create_u32("use_count", 0444, rdev->debugfs, |
| 4205 | &rdev->use_count); |
| 4206 | debugfs_create_u32("open_count", 0444, rdev->debugfs, |
| 4207 | &rdev->open_count); |
Mark Brown | f59c8f9 | 2012-08-31 10:36:37 -0700 | [diff] [blame] | 4208 | debugfs_create_u32("bypass_count", 0444, rdev->debugfs, |
| 4209 | &rdev->bypass_count); |
David Collins | cea4135 | 2017-01-10 17:34:21 -0800 | [diff] [blame] | 4210 | debugfs_create_file("consumers", 0444, rdev->debugfs, rdev, |
| 4211 | ®_consumers_fops); |
| 4212 | |
| 4213 | regulator = regulator_get(NULL, rdev_get_name(rdev)); |
| 4214 | if (IS_ERR(regulator)) { |
| 4215 | rdev_err(rdev, "regulator get failed, ret=%ld\n", |
| 4216 | PTR_ERR(regulator)); |
| 4217 | return; |
| 4218 | } |
| 4219 | rdev->debug_consumer = regulator; |
| 4220 | |
| 4221 | rdev->open_offset = 1; |
| 4222 | ops = rdev->desc->ops; |
| 4223 | |
| 4224 | debugfs_create_file("enable", 0644, rdev->debugfs, regulator, |
| 4225 | ®_enable_fops); |
| 4226 | if (ops->set_bypass) |
| 4227 | debugfs_create_file("bypass", 0644, rdev->debugfs, regulator, |
| 4228 | ®_bypass_enable_fops); |
| 4229 | |
| 4230 | mode = 0; |
| 4231 | if (ops->is_enabled) |
| 4232 | mode |= 0444; |
| 4233 | if (ops->disable) |
| 4234 | mode |= 0200; |
| 4235 | if (mode) |
| 4236 | debugfs_create_file("force_disable", mode, rdev->debugfs, |
| 4237 | regulator, ®_force_disable_fops); |
| 4238 | |
| 4239 | mode = 0; |
| 4240 | if (ops->get_voltage || ops->get_voltage_sel) |
| 4241 | mode |= 0444; |
| 4242 | if (ops->set_voltage || ops->set_voltage_sel) |
| 4243 | mode |= 0200; |
| 4244 | if (mode) |
| 4245 | debugfs_create_file("voltage", mode, rdev->debugfs, regulator, |
| 4246 | ®_voltage_fops); |
| 4247 | |
| 4248 | mode = 0; |
| 4249 | if (ops->get_mode) |
| 4250 | mode |= 0444; |
| 4251 | if (ops->set_mode) |
| 4252 | mode |= 0200; |
| 4253 | if (mode) |
| 4254 | debugfs_create_file("mode", mode, rdev->debugfs, regulator, |
| 4255 | ®_mode_fops); |
| 4256 | |
| 4257 | mode = 0; |
| 4258 | if (ops->get_mode) |
| 4259 | mode |= 0444; |
| 4260 | if (ops->set_load || (ops->get_optimum_mode && ops->set_mode)) |
| 4261 | mode |= 0200; |
| 4262 | if (mode) |
| 4263 | debugfs_create_file("load", mode, rdev->debugfs, regulator, |
| 4264 | ®_set_load_fops); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4265 | } |
| 4266 | |
David Collins | cea4135 | 2017-01-10 17:34:21 -0800 | [diff] [blame] | 4267 | #else |
| 4268 | |
| 4269 | static inline void rdev_deinit_debugfs(struct regulator_dev *rdev) |
| 4270 | { |
| 4271 | } |
| 4272 | |
| 4273 | static inline void rdev_init_debugfs(struct regulator_dev *rdev) |
| 4274 | { |
| 4275 | } |
| 4276 | |
| 4277 | #endif |
| 4278 | |
Javier Martinez Canillas | 5e3ca2b | 2016-03-23 20:59:34 -0300 | [diff] [blame] | 4279 | static int regulator_register_resolve_supply(struct device *dev, void *data) |
| 4280 | { |
Jon Hunter | 7ddede6 | 2016-04-21 17:11:57 +0100 | [diff] [blame] | 4281 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 4282 | |
| 4283 | if (regulator_resolve_supply(rdev)) |
| 4284 | rdev_dbg(rdev, "unable to resolve supply\n"); |
| 4285 | |
| 4286 | return 0; |
Javier Martinez Canillas | 5e3ca2b | 2016-03-23 20:59:34 -0300 | [diff] [blame] | 4287 | } |
| 4288 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4289 | /** |
| 4290 | * regulator_register - register regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 4291 | * @regulator_desc: regulator to register |
Krzysztof Kozlowski | f47531b | 2015-01-12 09:01:39 +0100 | [diff] [blame] | 4292 | * @cfg: runtime configuration for regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4293 | * |
| 4294 | * Called by regulator drivers to register a regulator. |
Axel Lin | 0384618 | 2013-01-03 21:01:47 +0800 | [diff] [blame] | 4295 | * Returns a valid pointer to struct regulator_dev on success |
| 4296 | * or an ERR_PTR() on error. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4297 | */ |
Mark Brown | 65f2684 | 2012-04-03 20:46:53 +0100 | [diff] [blame] | 4298 | struct regulator_dev * |
| 4299 | regulator_register(const struct regulator_desc *regulator_desc, |
Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4300 | const struct regulator_config *cfg) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4301 | { |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4302 | const struct regulation_constraints *constraints = NULL; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 4303 | const struct regulator_init_data *init_data; |
Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4304 | struct regulator_config *config = NULL; |
Aniroop Mathur | 72dca06 | 2014-12-28 22:08:38 +0530 | [diff] [blame] | 4305 | static atomic_t regulator_no = ATOMIC_INIT(-1); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4306 | struct regulator_dev *rdev; |
Mark Brown | 32c8fad | 2012-04-11 10:19:12 +0100 | [diff] [blame] | 4307 | struct device *dev; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4308 | int ret, i; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4309 | |
Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4310 | if (regulator_desc == NULL || cfg == NULL) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4311 | return ERR_PTR(-EINVAL); |
| 4312 | |
Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4313 | dev = cfg->dev; |
Mark Brown | dcf7011 | 2012-05-08 18:09:12 +0100 | [diff] [blame] | 4314 | WARN_ON(!dev); |
Mark Brown | 32c8fad | 2012-04-11 10:19:12 +0100 | [diff] [blame] | 4315 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4316 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) |
| 4317 | return ERR_PTR(-EINVAL); |
| 4318 | |
Diego Liziero | cd78dfc | 2009-04-14 03:04:47 +0200 | [diff] [blame] | 4319 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
| 4320 | regulator_desc->type != REGULATOR_CURRENT) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4321 | return ERR_PTR(-EINVAL); |
| 4322 | |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 4323 | /* Only one of each should be implemented */ |
| 4324 | WARN_ON(regulator_desc->ops->get_voltage && |
| 4325 | regulator_desc->ops->get_voltage_sel); |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 4326 | WARN_ON(regulator_desc->ops->set_voltage && |
| 4327 | regulator_desc->ops->set_voltage_sel); |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 4328 | |
| 4329 | /* If we're using selectors we must implement list_voltage. */ |
| 4330 | if (regulator_desc->ops->get_voltage_sel && |
| 4331 | !regulator_desc->ops->list_voltage) { |
| 4332 | return ERR_PTR(-EINVAL); |
| 4333 | } |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 4334 | if (regulator_desc->ops->set_voltage_sel && |
| 4335 | !regulator_desc->ops->list_voltage) { |
| 4336 | return ERR_PTR(-EINVAL); |
| 4337 | } |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 4338 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4339 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
| 4340 | if (rdev == NULL) |
| 4341 | return ERR_PTR(-ENOMEM); |
| 4342 | |
Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4343 | /* |
| 4344 | * Duplicate the config so the driver could override it after |
| 4345 | * parsing init data. |
| 4346 | */ |
| 4347 | config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); |
| 4348 | if (config == NULL) { |
| 4349 | kfree(rdev); |
| 4350 | return ERR_PTR(-ENOMEM); |
| 4351 | } |
| 4352 | |
Krzysztof Kozlowski | bfa21a0 | 2015-01-05 12:48:42 +0100 | [diff] [blame] | 4353 | init_data = regulator_of_get_init_data(dev, regulator_desc, config, |
Mark Brown | a0c7b16 | 2014-09-09 23:13:57 +0100 | [diff] [blame] | 4354 | &rdev->dev.of_node); |
| 4355 | if (!init_data) { |
| 4356 | init_data = config->init_data; |
| 4357 | rdev->dev.of_node = of_node_get(config->of_node); |
| 4358 | } |
| 4359 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4360 | mutex_init(&rdev->mutex); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 4361 | rdev->reg_data = config->driver_data; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4362 | rdev->owner = regulator_desc->owner; |
| 4363 | rdev->desc = regulator_desc; |
Mark Brown | 3a4b0a0 | 2012-05-08 18:10:45 +0100 | [diff] [blame] | 4364 | if (config->regmap) |
| 4365 | rdev->regmap = config->regmap; |
AnilKumar Ch | 52b84da | 2012-09-07 20:45:05 +0530 | [diff] [blame] | 4366 | else if (dev_get_regmap(dev, NULL)) |
Mark Brown | 3a4b0a0 | 2012-05-08 18:10:45 +0100 | [diff] [blame] | 4367 | rdev->regmap = dev_get_regmap(dev, NULL); |
AnilKumar Ch | 52b84da | 2012-09-07 20:45:05 +0530 | [diff] [blame] | 4368 | else if (dev->parent) |
| 4369 | rdev->regmap = dev_get_regmap(dev->parent, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4370 | INIT_LIST_HEAD(&rdev->consumer_list); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4371 | INIT_LIST_HEAD(&rdev->list); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4372 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); |
Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 4373 | INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4374 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4375 | /* preform any regulator specific init */ |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4376 | if (init_data && init_data->regulator_init) { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4377 | ret = init_data->regulator_init(rdev->reg_data); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 4378 | if (ret < 0) |
| 4379 | goto clean; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4380 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4381 | |
Krzysztof Adamski | daad134 | 2016-02-22 09:24:00 +0100 | [diff] [blame] | 4382 | if ((config->ena_gpio || config->ena_gpio_initialized) && |
| 4383 | gpio_is_valid(config->ena_gpio)) { |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4384 | mutex_lock(®ulator_list_mutex); |
Krzysztof Adamski | daad134 | 2016-02-22 09:24:00 +0100 | [diff] [blame] | 4385 | ret = regulator_ena_gpio_request(rdev, config); |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4386 | mutex_unlock(®ulator_list_mutex); |
Krzysztof Adamski | daad134 | 2016-02-22 09:24:00 +0100 | [diff] [blame] | 4387 | if (ret != 0) { |
| 4388 | rdev_err(rdev, "Failed to request enable GPIO%d: %d\n", |
| 4389 | config->ena_gpio, ret); |
Krzysztof Adamski | 3216523 | 2016-02-24 11:52:50 +0100 | [diff] [blame] | 4390 | goto clean; |
Krzysztof Adamski | daad134 | 2016-02-22 09:24:00 +0100 | [diff] [blame] | 4391 | } |
| 4392 | } |
| 4393 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4394 | /* register with sysfs */ |
| 4395 | rdev->dev.class = ®ulator_class; |
| 4396 | rdev->dev.parent = dev; |
Aniroop Mathur | 72dca06 | 2014-12-28 22:08:38 +0530 | [diff] [blame] | 4397 | dev_set_name(&rdev->dev, "regulator.%lu", |
Aniroop Mathur | 3913881 | 2014-12-29 22:36:48 +0530 | [diff] [blame] | 4398 | (unsigned long) atomic_inc_return(®ulator_no)); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4399 | |
Mike Rapoport | 74f544c | 2008-11-25 14:53:53 +0200 | [diff] [blame] | 4400 | /* set regulator constraints */ |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4401 | if (init_data) |
| 4402 | constraints = &init_data->constraints; |
| 4403 | |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4404 | if (init_data && init_data->supply_regulator) |
Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 4405 | rdev->supply_name = init_data->supply_regulator; |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 4406 | else if (regulator_desc->supply_name) |
Bjorn Andersson | 6261b06 | 2015-03-24 18:56:05 -0700 | [diff] [blame] | 4407 | rdev->supply_name = regulator_desc->supply_name; |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 4408 | |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4409 | /* |
| 4410 | * Attempt to resolve the regulator supply, if specified, |
| 4411 | * but don't return an error if we fail because we will try |
| 4412 | * to resolve it again later as more regulators are added. |
| 4413 | */ |
| 4414 | if (regulator_resolve_supply(rdev)) |
| 4415 | rdev_dbg(rdev, "unable to resolve supply\n"); |
| 4416 | |
| 4417 | ret = set_machine_constraints(rdev, constraints); |
| 4418 | if (ret < 0) |
| 4419 | goto wash; |
| 4420 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4421 | /* add consumers devices */ |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4422 | if (init_data) { |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4423 | mutex_lock(®ulator_list_mutex); |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4424 | for (i = 0; i < init_data->num_consumer_supplies; i++) { |
| 4425 | ret = set_consumer_device_supply(rdev, |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4426 | init_data->consumer_supplies[i].dev_name, |
Mark Brown | 23c2f04 | 2011-02-24 17:39:09 +0000 | [diff] [blame] | 4427 | init_data->consumer_supplies[i].supply); |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4428 | if (ret < 0) { |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4429 | mutex_unlock(®ulator_list_mutex); |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 4430 | dev_err(dev, "Failed to set supply %s\n", |
| 4431 | init_data->consumer_supplies[i].supply); |
| 4432 | goto unset_supplies; |
| 4433 | } |
Mark Brown | 23c2f04 | 2011-02-24 17:39:09 +0000 | [diff] [blame] | 4434 | } |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4435 | mutex_unlock(®ulator_list_mutex); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4436 | } |
| 4437 | |
Jon Hunter | c438b9d | 2016-04-21 17:11:59 +0100 | [diff] [blame] | 4438 | ret = device_register(&rdev->dev); |
| 4439 | if (ret != 0) { |
| 4440 | put_device(&rdev->dev); |
| 4441 | goto unset_supplies; |
| 4442 | } |
| 4443 | |
| 4444 | dev_set_drvdata(&rdev->dev, rdev); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4445 | rdev_init_debugfs(rdev); |
David Collins | 169b18a | 2013-07-08 14:33:22 -0700 | [diff] [blame] | 4446 | rdev->proxy_consumer = regulator_proxy_consumer_register(dev, |
| 4447 | config->of_node); |
Javier Martinez Canillas | 5e3ca2b | 2016-03-23 20:59:34 -0300 | [diff] [blame] | 4448 | |
| 4449 | /* try to resolve regulators supply since a new one was registered */ |
| 4450 | class_for_each_device(®ulator_class, NULL, NULL, |
| 4451 | regulator_register_resolve_supply); |
Krzysztof Kozlowski | 1b3de22 | 2015-01-05 12:48:41 +0100 | [diff] [blame] | 4452 | kfree(config); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4453 | return rdev; |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 4454 | |
Jani Nikula | d4033b5 | 2010-04-29 10:55:11 +0300 | [diff] [blame] | 4455 | unset_supplies: |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4456 | mutex_lock(®ulator_list_mutex); |
Jani Nikula | d4033b5 | 2010-04-29 10:55:11 +0300 | [diff] [blame] | 4457 | unset_regulator_supplies(rdev); |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4458 | mutex_unlock(®ulator_list_mutex); |
Krzysztof Adamski | 3216523 | 2016-02-24 11:52:50 +0100 | [diff] [blame] | 4459 | wash: |
Boris Brezillon | 469b640 | 2016-04-12 12:31:00 +0200 | [diff] [blame] | 4460 | kfree(rdev->constraints); |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4461 | mutex_lock(®ulator_list_mutex); |
Krzysztof Adamski | 3216523 | 2016-02-24 11:52:50 +0100 | [diff] [blame] | 4462 | regulator_ena_gpio_free(rdev); |
Jon Hunter | 45389c4 | 2016-04-26 11:29:45 +0100 | [diff] [blame] | 4463 | mutex_unlock(®ulator_list_mutex); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 4464 | clean: |
| 4465 | kfree(rdev); |
Jon Hunter | a215137 | 2016-03-30 17:09:13 +0100 | [diff] [blame] | 4466 | kfree(config); |
| 4467 | return ERR_PTR(ret); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4468 | } |
| 4469 | EXPORT_SYMBOL_GPL(regulator_register); |
| 4470 | |
| 4471 | /** |
| 4472 | * regulator_unregister - unregister regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 4473 | * @rdev: regulator to unregister |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4474 | * |
| 4475 | * Called by regulator drivers to unregister a regulator. |
| 4476 | */ |
| 4477 | void regulator_unregister(struct regulator_dev *rdev) |
| 4478 | { |
| 4479 | if (rdev == NULL) |
| 4480 | return; |
| 4481 | |
Mark Brown | 891636e | 2013-07-08 09:14:45 +0100 | [diff] [blame] | 4482 | if (rdev->supply) { |
| 4483 | while (rdev->use_count--) |
| 4484 | regulator_disable(rdev->supply); |
Mark Brown | e032b37 | 2012-03-28 21:17:55 +0100 | [diff] [blame] | 4485 | regulator_put(rdev->supply); |
Mark Brown | 891636e | 2013-07-08 09:14:45 +0100 | [diff] [blame] | 4486 | } |
David Collins | 169b18a | 2013-07-08 14:33:22 -0700 | [diff] [blame] | 4487 | regulator_proxy_consumer_unregister(rdev->proxy_consumer); |
David Collins | cea4135 | 2017-01-10 17:34:21 -0800 | [diff] [blame] | 4488 | rdev_deinit_debugfs(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4489 | mutex_lock(®ulator_list_mutex); |
Tejun Heo | 4382973 | 2012-08-20 14:51:24 -0700 | [diff] [blame] | 4490 | flush_work(&rdev->disable_work.work); |
Mark Brown | 6bf87d1 | 2009-07-21 16:00:25 +0100 | [diff] [blame] | 4491 | WARN_ON(rdev->open_count); |
Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 4492 | unset_regulator_supplies(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4493 | list_del(&rdev->list); |
Kim, Milo | f19b00d | 2013-02-18 06:50:39 +0000 | [diff] [blame] | 4494 | regulator_ena_gpio_free(rdev); |
Mark Brown | 2c0a303 | 2016-04-12 08:05:43 +0100 | [diff] [blame] | 4495 | mutex_unlock(®ulator_list_mutex); |
Lothar Waßmann | 58fb5cf | 2011-11-28 15:38:37 +0100 | [diff] [blame] | 4496 | device_unregister(&rdev->dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4497 | } |
| 4498 | EXPORT_SYMBOL_GPL(regulator_unregister); |
| 4499 | |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4500 | static int _regulator_suspend_prepare(struct device *dev, void *data) |
| 4501 | { |
| 4502 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 4503 | const suspend_state_t *state = data; |
| 4504 | int ret; |
| 4505 | |
| 4506 | mutex_lock(&rdev->mutex); |
| 4507 | ret = suspend_prepare(rdev, *state); |
| 4508 | mutex_unlock(&rdev->mutex); |
| 4509 | |
| 4510 | return ret; |
| 4511 | } |
| 4512 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4513 | /** |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 4514 | * regulator_suspend_prepare - prepare regulators for system wide suspend |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4515 | * @state: system suspend state |
| 4516 | * |
| 4517 | * Configure each regulator with it's suspend operating parameters for state. |
| 4518 | * This will usually be called by machine suspend code prior to supending. |
| 4519 | */ |
| 4520 | int regulator_suspend_prepare(suspend_state_t state) |
| 4521 | { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4522 | /* ON is handled by regulator active state */ |
| 4523 | if (state == PM_SUSPEND_ON) |
| 4524 | return -EINVAL; |
| 4525 | |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4526 | return class_for_each_device(®ulator_class, NULL, &state, |
| 4527 | _regulator_suspend_prepare); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4528 | } |
| 4529 | EXPORT_SYMBOL_GPL(regulator_suspend_prepare); |
| 4530 | |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4531 | static int _regulator_suspend_finish(struct device *dev, void *data) |
| 4532 | { |
| 4533 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 4534 | int ret; |
| 4535 | |
| 4536 | mutex_lock(&rdev->mutex); |
| 4537 | if (rdev->use_count > 0 || rdev->constraints->always_on) { |
| 4538 | if (!_regulator_is_enabled(rdev)) { |
| 4539 | ret = _regulator_do_enable(rdev); |
| 4540 | if (ret) |
| 4541 | dev_err(dev, |
| 4542 | "Failed to resume regulator %d\n", |
| 4543 | ret); |
| 4544 | } |
| 4545 | } else { |
| 4546 | if (!have_full_constraints()) |
| 4547 | goto unlock; |
| 4548 | if (!_regulator_is_enabled(rdev)) |
| 4549 | goto unlock; |
| 4550 | |
| 4551 | ret = _regulator_do_disable(rdev); |
| 4552 | if (ret) |
| 4553 | dev_err(dev, "Failed to suspend regulator %d\n", ret); |
| 4554 | } |
| 4555 | unlock: |
| 4556 | mutex_unlock(&rdev->mutex); |
| 4557 | |
| 4558 | /* Keep processing regulators in spite of any errors */ |
| 4559 | return 0; |
| 4560 | } |
| 4561 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4562 | /** |
MyungJoo Ham | 7a32b58 | 2011-03-11 10:13:59 +0900 | [diff] [blame] | 4563 | * regulator_suspend_finish - resume regulators from system wide suspend |
| 4564 | * |
| 4565 | * Turn on regulators that might be turned off by regulator_suspend_prepare |
| 4566 | * and that should be turned on according to the regulators properties. |
| 4567 | */ |
| 4568 | int regulator_suspend_finish(void) |
| 4569 | { |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4570 | return class_for_each_device(®ulator_class, NULL, NULL, |
| 4571 | _regulator_suspend_finish); |
MyungJoo Ham | 7a32b58 | 2011-03-11 10:13:59 +0900 | [diff] [blame] | 4572 | } |
| 4573 | EXPORT_SYMBOL_GPL(regulator_suspend_finish); |
| 4574 | |
| 4575 | /** |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4576 | * regulator_has_full_constraints - the system has fully specified constraints |
| 4577 | * |
| 4578 | * Calling this function will cause the regulator API to disable all |
| 4579 | * regulators which have a zero use count and don't have an always_on |
| 4580 | * constraint in a late_initcall. |
| 4581 | * |
| 4582 | * The intention is that this will become the default behaviour in a |
| 4583 | * future kernel release so users are encouraged to use this facility |
| 4584 | * now. |
| 4585 | */ |
| 4586 | void regulator_has_full_constraints(void) |
| 4587 | { |
| 4588 | has_full_constraints = 1; |
| 4589 | } |
| 4590 | EXPORT_SYMBOL_GPL(regulator_has_full_constraints); |
| 4591 | |
| 4592 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4593 | * rdev_get_drvdata - get rdev regulator driver data |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 4594 | * @rdev: regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4595 | * |
| 4596 | * Get rdev regulator driver private data. This call can be used in the |
| 4597 | * regulator driver context. |
| 4598 | */ |
| 4599 | void *rdev_get_drvdata(struct regulator_dev *rdev) |
| 4600 | { |
| 4601 | return rdev->reg_data; |
| 4602 | } |
| 4603 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); |
| 4604 | |
| 4605 | /** |
| 4606 | * regulator_get_drvdata - get regulator driver data |
| 4607 | * @regulator: regulator |
| 4608 | * |
| 4609 | * Get regulator driver private data. This call can be used in the consumer |
| 4610 | * driver context when non API regulator specific functions need to be called. |
| 4611 | */ |
| 4612 | void *regulator_get_drvdata(struct regulator *regulator) |
| 4613 | { |
| 4614 | return regulator->rdev->reg_data; |
| 4615 | } |
| 4616 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); |
| 4617 | |
| 4618 | /** |
| 4619 | * regulator_set_drvdata - set regulator driver data |
| 4620 | * @regulator: regulator |
| 4621 | * @data: data |
| 4622 | */ |
| 4623 | void regulator_set_drvdata(struct regulator *regulator, void *data) |
| 4624 | { |
| 4625 | regulator->rdev->reg_data = data; |
| 4626 | } |
| 4627 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); |
| 4628 | |
| 4629 | /** |
| 4630 | * regulator_get_id - get regulator ID |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 4631 | * @rdev: regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4632 | */ |
| 4633 | int rdev_get_id(struct regulator_dev *rdev) |
| 4634 | { |
| 4635 | return rdev->desc->id; |
| 4636 | } |
| 4637 | EXPORT_SYMBOL_GPL(rdev_get_id); |
| 4638 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 4639 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
| 4640 | { |
| 4641 | return &rdev->dev; |
| 4642 | } |
| 4643 | EXPORT_SYMBOL_GPL(rdev_get_dev); |
| 4644 | |
| 4645 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) |
| 4646 | { |
| 4647 | return reg_init_data->driver_data; |
| 4648 | } |
| 4649 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); |
| 4650 | |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4651 | #ifdef CONFIG_DEBUG_FS |
| 4652 | static ssize_t supply_map_read_file(struct file *file, char __user *user_buf, |
| 4653 | size_t count, loff_t *ppos) |
| 4654 | { |
| 4655 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 4656 | ssize_t len, ret = 0; |
| 4657 | struct regulator_map *map; |
| 4658 | |
| 4659 | if (!buf) |
| 4660 | return -ENOMEM; |
| 4661 | |
| 4662 | list_for_each_entry(map, ®ulator_map_list, list) { |
| 4663 | len = snprintf(buf + ret, PAGE_SIZE - ret, |
| 4664 | "%s -> %s.%s\n", |
| 4665 | rdev_get_name(map->regulator), map->dev_name, |
| 4666 | map->supply); |
| 4667 | if (len >= 0) |
| 4668 | ret += len; |
| 4669 | if (ret > PAGE_SIZE) { |
| 4670 | ret = PAGE_SIZE; |
| 4671 | break; |
| 4672 | } |
| 4673 | } |
| 4674 | |
| 4675 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
| 4676 | |
| 4677 | kfree(buf); |
| 4678 | |
| 4679 | return ret; |
| 4680 | } |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4681 | #endif |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4682 | |
| 4683 | static const struct file_operations supply_map_fops = { |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4684 | #ifdef CONFIG_DEBUG_FS |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4685 | .read = supply_map_read_file, |
| 4686 | .llseek = default_llseek, |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4687 | #endif |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4688 | }; |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4689 | |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4690 | #ifdef CONFIG_DEBUG_FS |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4691 | struct summary_data { |
| 4692 | struct seq_file *s; |
| 4693 | struct regulator_dev *parent; |
| 4694 | int level; |
| 4695 | }; |
| 4696 | |
| 4697 | static void regulator_summary_show_subtree(struct seq_file *s, |
| 4698 | struct regulator_dev *rdev, |
| 4699 | int level); |
| 4700 | |
| 4701 | static int regulator_summary_show_children(struct device *dev, void *data) |
| 4702 | { |
| 4703 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 4704 | struct summary_data *summary_data = data; |
| 4705 | |
| 4706 | if (rdev->supply && rdev->supply->rdev == summary_data->parent) |
| 4707 | regulator_summary_show_subtree(summary_data->s, rdev, |
| 4708 | summary_data->level + 1); |
| 4709 | |
| 4710 | return 0; |
| 4711 | } |
| 4712 | |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4713 | static void regulator_summary_show_subtree(struct seq_file *s, |
| 4714 | struct regulator_dev *rdev, |
| 4715 | int level) |
| 4716 | { |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4717 | struct regulation_constraints *c; |
| 4718 | struct regulator *consumer; |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4719 | struct summary_data summary_data; |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4720 | |
| 4721 | if (!rdev) |
| 4722 | return; |
| 4723 | |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4724 | seq_printf(s, "%*s%-*s %3d %4d %6d ", |
| 4725 | level * 3 + 1, "", |
| 4726 | 30 - level * 3, rdev_get_name(rdev), |
| 4727 | rdev->use_count, rdev->open_count, rdev->bypass_count); |
| 4728 | |
Heiko Stübner | 2329609 | 2015-04-10 13:48:41 +0200 | [diff] [blame] | 4729 | seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000); |
| 4730 | seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000); |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4731 | |
| 4732 | c = rdev->constraints; |
| 4733 | if (c) { |
| 4734 | switch (rdev->desc->type) { |
| 4735 | case REGULATOR_VOLTAGE: |
| 4736 | seq_printf(s, "%5dmV %5dmV ", |
| 4737 | c->min_uV / 1000, c->max_uV / 1000); |
| 4738 | break; |
| 4739 | case REGULATOR_CURRENT: |
| 4740 | seq_printf(s, "%5dmA %5dmA ", |
| 4741 | c->min_uA / 1000, c->max_uA / 1000); |
| 4742 | break; |
| 4743 | } |
| 4744 | } |
| 4745 | |
| 4746 | seq_puts(s, "\n"); |
| 4747 | |
| 4748 | list_for_each_entry(consumer, &rdev->consumer_list, list) { |
| 4749 | if (consumer->dev->class == ®ulator_class) |
| 4750 | continue; |
| 4751 | |
| 4752 | seq_printf(s, "%*s%-*s ", |
| 4753 | (level + 1) * 3 + 1, "", |
| 4754 | 30 - (level + 1) * 3, dev_name(consumer->dev)); |
| 4755 | |
| 4756 | switch (rdev->desc->type) { |
| 4757 | case REGULATOR_VOLTAGE: |
Heiko Stübner | 2329609 | 2015-04-10 13:48:41 +0200 | [diff] [blame] | 4758 | seq_printf(s, "%37dmV %5dmV", |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4759 | consumer->min_uV / 1000, |
| 4760 | consumer->max_uV / 1000); |
| 4761 | break; |
| 4762 | case REGULATOR_CURRENT: |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4763 | break; |
| 4764 | } |
| 4765 | |
| 4766 | seq_puts(s, "\n"); |
| 4767 | } |
| 4768 | |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4769 | summary_data.s = s; |
| 4770 | summary_data.level = level; |
| 4771 | summary_data.parent = rdev; |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4772 | |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4773 | class_for_each_device(®ulator_class, NULL, &summary_data, |
| 4774 | regulator_summary_show_children); |
| 4775 | } |
| 4776 | |
| 4777 | static int regulator_summary_show_roots(struct device *dev, void *data) |
| 4778 | { |
| 4779 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 4780 | struct seq_file *s = data; |
| 4781 | |
| 4782 | if (!rdev->supply) |
| 4783 | regulator_summary_show_subtree(s, rdev, 0); |
| 4784 | |
| 4785 | return 0; |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4786 | } |
| 4787 | |
| 4788 | static int regulator_summary_show(struct seq_file *s, void *data) |
| 4789 | { |
Heiko Stübner | 2329609 | 2015-04-10 13:48:41 +0200 | [diff] [blame] | 4790 | seq_puts(s, " regulator use open bypass voltage current min max\n"); |
| 4791 | seq_puts(s, "-------------------------------------------------------------------------------\n"); |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4792 | |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4793 | class_for_each_device(®ulator_class, NULL, s, |
| 4794 | regulator_summary_show_roots); |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4795 | |
| 4796 | return 0; |
| 4797 | } |
| 4798 | |
| 4799 | static int regulator_summary_open(struct inode *inode, struct file *file) |
| 4800 | { |
| 4801 | return single_open(file, regulator_summary_show, inode->i_private); |
| 4802 | } |
| 4803 | #endif |
| 4804 | |
| 4805 | static const struct file_operations regulator_summary_fops = { |
| 4806 | #ifdef CONFIG_DEBUG_FS |
| 4807 | .open = regulator_summary_open, |
| 4808 | .read = seq_read, |
| 4809 | .llseek = seq_lseek, |
| 4810 | .release = single_release, |
| 4811 | #endif |
| 4812 | }; |
| 4813 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4814 | static int __init regulator_init(void) |
| 4815 | { |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 4816 | int ret; |
| 4817 | |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 4818 | ret = class_register(®ulator_class); |
| 4819 | |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4820 | debugfs_root = debugfs_create_dir("regulator", NULL); |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 4821 | if (!debugfs_root) |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4822 | pr_warn("regulator: Failed to create debugfs directory\n"); |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 4823 | |
Mark Brown | f4d562c | 2012-02-20 21:01:04 +0000 | [diff] [blame] | 4824 | debugfs_create_file("supply_map", 0444, debugfs_root, NULL, |
| 4825 | &supply_map_fops); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 4826 | |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4827 | debugfs_create_file("regulator_summary", 0444, debugfs_root, |
Tomeu Vizoso | 85f3b43 | 2015-09-21 16:02:47 +0200 | [diff] [blame] | 4828 | NULL, ®ulator_summary_fops); |
Heiko Stübner | 7c225ec | 2015-04-07 16:16:39 +0200 | [diff] [blame] | 4829 | |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 4830 | regulator_dummy_init(); |
| 4831 | |
| 4832 | return ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 4833 | } |
| 4834 | |
| 4835 | /* init early to allow our consumers to complete system booting */ |
| 4836 | core_initcall(regulator_init); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4837 | |
Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4838 | static int __init regulator_late_cleanup(struct device *dev, void *data) |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4839 | { |
Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4840 | struct regulator_dev *rdev = dev_to_rdev(dev); |
| 4841 | const struct regulator_ops *ops = rdev->desc->ops; |
| 4842 | struct regulation_constraints *c = rdev->constraints; |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4843 | int enabled, ret; |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4844 | |
Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4845 | if (c && c->always_on) |
| 4846 | return 0; |
| 4847 | |
WEN Pingbo | 8a34e97 | 2016-04-23 15:11:05 +0800 | [diff] [blame] | 4848 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) |
Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4849 | return 0; |
| 4850 | |
| 4851 | mutex_lock(&rdev->mutex); |
| 4852 | |
| 4853 | if (rdev->use_count) |
| 4854 | goto unlock; |
| 4855 | |
| 4856 | /* If we can't read the status assume it's on. */ |
| 4857 | if (ops->is_enabled) |
| 4858 | enabled = ops->is_enabled(rdev); |
| 4859 | else |
| 4860 | enabled = 1; |
| 4861 | |
| 4862 | if (!enabled) |
| 4863 | goto unlock; |
| 4864 | |
| 4865 | if (have_full_constraints()) { |
| 4866 | /* We log since this may kill the system if it goes |
| 4867 | * wrong. */ |
| 4868 | rdev_info(rdev, "disabling\n"); |
| 4869 | ret = _regulator_do_disable(rdev); |
| 4870 | if (ret != 0) |
| 4871 | rdev_err(rdev, "couldn't disable: %d\n", ret); |
| 4872 | } else { |
| 4873 | /* The intention is that in future we will |
| 4874 | * assume that full constraints are provided |
| 4875 | * so warn even if we aren't going to do |
| 4876 | * anything here. |
| 4877 | */ |
| 4878 | rdev_warn(rdev, "incomplete constraints, leaving on\n"); |
| 4879 | } |
| 4880 | |
| 4881 | unlock: |
| 4882 | mutex_unlock(&rdev->mutex); |
| 4883 | |
| 4884 | return 0; |
| 4885 | } |
| 4886 | |
| 4887 | static int __init regulator_init_complete(void) |
| 4888 | { |
Mark Brown | 86f5fcf | 2012-07-06 18:19:13 +0100 | [diff] [blame] | 4889 | /* |
| 4890 | * Since DT doesn't provide an idiomatic mechanism for |
| 4891 | * enabling full constraints and since it's much more natural |
| 4892 | * with DT to provide them just assume that a DT enabled |
| 4893 | * system has full constraints. |
| 4894 | */ |
| 4895 | if (of_have_populated_dt()) |
| 4896 | has_full_constraints = true; |
| 4897 | |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4898 | /* If we have a full configuration then disable any regulators |
Mark Brown | e953583 | 2014-06-01 19:15:16 +0100 | [diff] [blame] | 4899 | * we have permission to change the status for and which are |
| 4900 | * not in use or always_on. This is effectively the default |
| 4901 | * for DT and ACPI as they have full constraints. |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4902 | */ |
Mark Brown | 609ca5f | 2015-08-10 19:43:47 +0100 | [diff] [blame] | 4903 | class_for_each_device(®ulator_class, NULL, NULL, |
| 4904 | regulator_late_cleanup); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 4905 | |
| 4906 | return 0; |
| 4907 | } |
Saravana Kannan | fd482a3 | 2014-04-23 18:10:50 -0500 | [diff] [blame] | 4908 | late_initcall_sync(regulator_init_complete); |