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