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