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