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