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