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