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