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