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