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