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