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