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