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