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 | |
Stephen Boyd | acf149d | 2012-01-16 19:39:58 -0800 | [diff] [blame] | 1322 | static void devm_regulator_release(struct device *dev, void *res) |
| 1323 | { |
| 1324 | regulator_put(*(struct regulator **)res); |
| 1325 | } |
| 1326 | |
| 1327 | /** |
| 1328 | * devm_regulator_get - Resource managed regulator_get() |
| 1329 | * @dev: device for regulator "consumer" |
| 1330 | * @id: Supply name or regulator ID. |
| 1331 | * |
| 1332 | * Managed regulator_get(). Regulators returned from this function are |
| 1333 | * automatically regulator_put() on driver detach. See regulator_get() for more |
| 1334 | * information. |
| 1335 | */ |
| 1336 | struct regulator *devm_regulator_get(struct device *dev, const char *id) |
| 1337 | { |
| 1338 | struct regulator **ptr, *regulator; |
| 1339 | |
| 1340 | ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL); |
| 1341 | if (!ptr) |
| 1342 | return ERR_PTR(-ENOMEM); |
| 1343 | |
| 1344 | regulator = regulator_get(dev, id); |
| 1345 | if (!IS_ERR(regulator)) { |
| 1346 | *ptr = regulator; |
| 1347 | devres_add(dev, ptr); |
| 1348 | } else { |
| 1349 | devres_free(ptr); |
| 1350 | } |
| 1351 | |
| 1352 | return regulator; |
| 1353 | } |
| 1354 | EXPORT_SYMBOL_GPL(devm_regulator_get); |
| 1355 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1356 | /** |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1357 | * regulator_get_exclusive - obtain exclusive access to a regulator. |
| 1358 | * @dev: device for regulator "consumer" |
| 1359 | * @id: Supply name or regulator ID. |
| 1360 | * |
| 1361 | * Returns a struct regulator corresponding to the regulator producer, |
| 1362 | * or IS_ERR() condition containing errno. Other consumers will be |
| 1363 | * unable to obtain this reference is held and the use count for the |
| 1364 | * regulator will be initialised to reflect the current state of the |
| 1365 | * regulator. |
| 1366 | * |
| 1367 | * This is intended for use by consumers which cannot tolerate shared |
| 1368 | * use of the regulator such as those which need to force the |
| 1369 | * regulator off for correct operation of the hardware they are |
| 1370 | * controlling. |
| 1371 | * |
| 1372 | * Use of supply names configured via regulator_set_device_supply() is |
| 1373 | * strongly encouraged. It is recommended that the supply name used |
| 1374 | * should match the name used for the supply and/or the relevant |
| 1375 | * device pins in the datasheet. |
| 1376 | */ |
| 1377 | struct regulator *regulator_get_exclusive(struct device *dev, const char *id) |
| 1378 | { |
| 1379 | return _regulator_get(dev, id, 1); |
| 1380 | } |
| 1381 | EXPORT_SYMBOL_GPL(regulator_get_exclusive); |
| 1382 | |
| 1383 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1384 | * regulator_put - "free" the regulator source |
| 1385 | * @regulator: regulator source |
| 1386 | * |
| 1387 | * Note: drivers must ensure that all regulator_enable calls made on this |
| 1388 | * regulator source are balanced by regulator_disable calls prior to calling |
| 1389 | * this function. |
| 1390 | */ |
| 1391 | void regulator_put(struct regulator *regulator) |
| 1392 | { |
| 1393 | struct regulator_dev *rdev; |
| 1394 | |
| 1395 | if (regulator == NULL || IS_ERR(regulator)) |
| 1396 | return; |
| 1397 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1398 | mutex_lock(®ulator_list_mutex); |
| 1399 | rdev = regulator->rdev; |
| 1400 | |
| 1401 | /* remove any sysfs entries */ |
| 1402 | if (regulator->dev) { |
| 1403 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); |
| 1404 | kfree(regulator->supply_name); |
| 1405 | device_remove_file(regulator->dev, ®ulator->dev_attr); |
| 1406 | kfree(regulator->dev_attr.attr.name); |
| 1407 | } |
| 1408 | list_del(®ulator->list); |
| 1409 | kfree(regulator); |
| 1410 | |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1411 | rdev->open_count--; |
| 1412 | rdev->exclusive = 0; |
| 1413 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1414 | module_put(rdev->owner); |
| 1415 | mutex_unlock(®ulator_list_mutex); |
| 1416 | } |
| 1417 | EXPORT_SYMBOL_GPL(regulator_put); |
| 1418 | |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1419 | static int _regulator_can_change_status(struct regulator_dev *rdev) |
| 1420 | { |
| 1421 | if (!rdev->constraints) |
| 1422 | return 0; |
| 1423 | |
| 1424 | if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS) |
| 1425 | return 1; |
| 1426 | else |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1430 | /* locks held by regulator_enable() */ |
| 1431 | static int _regulator_enable(struct regulator_dev *rdev) |
| 1432 | { |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1433 | int ret, delay; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1434 | |
Bengt Jonsson | acaf6ff | 2010-11-10 11:06:22 +0100 | [diff] [blame] | 1435 | if (rdev->use_count == 0) { |
| 1436 | /* do we need to enable the supply regulator first */ |
| 1437 | if (rdev->supply) { |
| 1438 | mutex_lock(&rdev->supply->mutex); |
| 1439 | ret = _regulator_enable(rdev->supply); |
| 1440 | mutex_unlock(&rdev->supply->mutex); |
| 1441 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1442 | rdev_err(rdev, "failed to enable: %d\n", ret); |
Bengt Jonsson | acaf6ff | 2010-11-10 11:06:22 +0100 | [diff] [blame] | 1443 | return ret; |
| 1444 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | |
| 1448 | /* check voltage and requested load before enabling */ |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1449 | if (rdev->constraints && |
| 1450 | (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) |
| 1451 | drms_uA_update(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1452 | |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1453 | if (rdev->use_count == 0) { |
| 1454 | /* The regulator may on if it's not switchable or left on */ |
| 1455 | ret = _regulator_is_enabled(rdev); |
| 1456 | if (ret == -EINVAL || ret == 0) { |
| 1457 | if (!_regulator_can_change_status(rdev)) |
| 1458 | return -EPERM; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1459 | |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1460 | if (!rdev->desc->ops->enable) |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1461 | return -EINVAL; |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1462 | |
| 1463 | /* Query before enabling in case configuration |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1464 | * dependent. */ |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1465 | ret = _regulator_get_enable_time(rdev); |
| 1466 | if (ret >= 0) { |
| 1467 | delay = ret; |
| 1468 | } else { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1469 | rdev_warn(rdev, "enable_time() failed: %d\n", |
Daniel Walker | 1d7372e | 2010-11-17 15:30:28 -0800 | [diff] [blame] | 1470 | ret); |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1471 | delay = 0; |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1472 | } |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1473 | |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1474 | trace_regulator_enable(rdev_get_name(rdev)); |
| 1475 | |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1476 | /* Allow the regulator to ramp; it would be useful |
| 1477 | * to extend this for bulk operations so that the |
| 1478 | * regulators can ramp together. */ |
| 1479 | ret = rdev->desc->ops->enable(rdev); |
| 1480 | if (ret < 0) |
| 1481 | return ret; |
| 1482 | |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1483 | trace_regulator_enable_delay(rdev_get_name(rdev)); |
| 1484 | |
Axel Lin | e36c1df | 2010-11-05 21:51:32 +0800 | [diff] [blame] | 1485 | if (delay >= 1000) { |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1486 | mdelay(delay / 1000); |
Axel Lin | e36c1df | 2010-11-05 21:51:32 +0800 | [diff] [blame] | 1487 | udelay(delay % 1000); |
| 1488 | } else if (delay) { |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1489 | udelay(delay); |
Axel Lin | e36c1df | 2010-11-05 21:51:32 +0800 | [diff] [blame] | 1490 | } |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1491 | |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1492 | trace_regulator_enable_complete(rdev_get_name(rdev)); |
| 1493 | |
Linus Walleij | a7433cf | 2009-08-26 12:54:04 +0200 | [diff] [blame] | 1494 | } else if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1495 | rdev_err(rdev, "is_enabled() failed: %d\n", ret); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1496 | return ret; |
| 1497 | } |
Linus Walleij | a7433cf | 2009-08-26 12:54:04 +0200 | [diff] [blame] | 1498 | /* Fallthrough on positive return values - already enabled */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1499 | } |
| 1500 | |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1501 | rdev->use_count++; |
| 1502 | |
| 1503 | return 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | /** |
| 1507 | * regulator_enable - enable regulator output |
| 1508 | * @regulator: regulator source |
| 1509 | * |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1510 | * Request that the regulator be enabled with the regulator output at |
| 1511 | * the predefined voltage or current value. Calls to regulator_enable() |
| 1512 | * must be balanced with calls to regulator_disable(). |
| 1513 | * |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1514 | * 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] | 1515 | * hardwired in the regulator. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1516 | */ |
| 1517 | int regulator_enable(struct regulator *regulator) |
| 1518 | { |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1519 | struct regulator_dev *rdev = regulator->rdev; |
| 1520 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1521 | |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1522 | mutex_lock(&rdev->mutex); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1523 | |
| 1524 | if (!regulator_check_voltage_update(rdev)) { |
| 1525 | if (regulator->min_uV < rdev->constraints->min_uV || |
| 1526 | regulator->max_uV > rdev->constraints->max_uV) { |
| 1527 | rdev_err(rdev, "invalid input - constraint: [%d, %d], " |
| 1528 | "set point: [%d, %d]\n", |
| 1529 | rdev->constraints->min_uV, |
| 1530 | rdev->constraints->max_uV, |
| 1531 | regulator->min_uV, |
| 1532 | regulator->max_uV); |
| 1533 | ret = -EINVAL; |
| 1534 | goto out; |
| 1535 | } |
| 1536 | |
| 1537 | ret = update_voltage(regulator, regulator->min_uV, |
| 1538 | regulator->max_uV); |
| 1539 | if (ret) |
| 1540 | goto out; |
| 1541 | } |
| 1542 | |
David Brownell | cd94b50 | 2009-03-11 16:43:34 -0800 | [diff] [blame] | 1543 | ret = _regulator_enable(rdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1544 | if (ret) |
| 1545 | goto out; |
| 1546 | |
| 1547 | regulator->enabled++; |
| 1548 | |
| 1549 | out: |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1550 | mutex_unlock(&rdev->mutex); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1551 | return ret; |
| 1552 | } |
| 1553 | EXPORT_SYMBOL_GPL(regulator_enable); |
| 1554 | |
| 1555 | /* locks held by regulator_disable() */ |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1556 | static int _regulator_disable(struct regulator_dev *rdev, |
| 1557 | struct regulator_dev **supply_rdev_ptr) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1558 | { |
| 1559 | int ret = 0; |
Mattias Wallin | b12a1e2 | 2010-11-02 14:55:34 +0100 | [diff] [blame] | 1560 | *supply_rdev_ptr = NULL; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1561 | |
David Brownell | cd94b50 | 2009-03-11 16:43:34 -0800 | [diff] [blame] | 1562 | if (WARN(rdev->use_count <= 0, |
Joe Perches | 43e7ee3 | 2010-12-06 14:05:19 -0800 | [diff] [blame] | 1563 | "unbalanced disables for %s\n", rdev_get_name(rdev))) |
David Brownell | cd94b50 | 2009-03-11 16:43:34 -0800 | [diff] [blame] | 1564 | return -EIO; |
| 1565 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1566 | /* are we the last user and permitted to disable ? */ |
Mark Brown | 60ef66f | 2009-10-13 13:06:50 +0100 | [diff] [blame] | 1567 | if (rdev->use_count == 1 && |
| 1568 | (rdev->constraints && !rdev->constraints->always_on)) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1569 | |
| 1570 | /* we are last user */ |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1571 | if (_regulator_can_change_status(rdev) && |
| 1572 | rdev->desc->ops->disable) { |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1573 | trace_regulator_disable(rdev_get_name(rdev)); |
| 1574 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1575 | ret = rdev->desc->ops->disable(rdev); |
| 1576 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1577 | rdev_err(rdev, "failed to disable\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1578 | return ret; |
| 1579 | } |
Mark Brown | 84b6826 | 2009-12-01 21:12:27 +0000 | [diff] [blame] | 1580 | |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1581 | trace_regulator_disable_complete(rdev_get_name(rdev)); |
| 1582 | |
Mark Brown | 84b6826 | 2009-12-01 21:12:27 +0000 | [diff] [blame] | 1583 | _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, |
| 1584 | NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | /* decrease our supplies ref count and disable if required */ |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1588 | *supply_rdev_ptr = rdev->supply; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1589 | |
| 1590 | rdev->use_count = 0; |
| 1591 | } else if (rdev->use_count > 1) { |
| 1592 | |
| 1593 | if (rdev->constraints && |
| 1594 | (rdev->constraints->valid_ops_mask & |
| 1595 | REGULATOR_CHANGE_DRMS)) |
| 1596 | drms_uA_update(rdev); |
| 1597 | |
| 1598 | rdev->use_count--; |
| 1599 | } |
| 1600 | return ret; |
| 1601 | } |
| 1602 | |
| 1603 | /** |
| 1604 | * regulator_disable - disable regulator output |
| 1605 | * @regulator: regulator source |
| 1606 | * |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1607 | * Disable the regulator output voltage or current. Calls to |
| 1608 | * regulator_enable() must be balanced with calls to |
| 1609 | * regulator_disable(). |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1610 | * |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1611 | * NOTE: this will only disable the regulator output if no other consumer |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1612 | * devices have it enabled, the regulator device supports disabling and |
| 1613 | * machine constraints permit this operation. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1614 | */ |
| 1615 | int regulator_disable(struct regulator *regulator) |
| 1616 | { |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1617 | struct regulator_dev *rdev = regulator->rdev; |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1618 | struct regulator_dev *supply_rdev = NULL; |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1619 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1620 | |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1621 | mutex_lock(&rdev->mutex); |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1622 | ret = _regulator_disable(rdev, &supply_rdev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1623 | if (ret) |
| 1624 | goto out; |
| 1625 | |
| 1626 | regulator->enabled--; |
| 1627 | |
| 1628 | if (!regulator_check_voltage_update(rdev)) |
| 1629 | update_voltage_prev(rdev); |
| 1630 | |
| 1631 | out: |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1632 | mutex_unlock(&rdev->mutex); |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1633 | |
| 1634 | /* decrease our supplies ref count and disable if required */ |
| 1635 | while (supply_rdev != NULL) { |
| 1636 | rdev = supply_rdev; |
| 1637 | |
| 1638 | mutex_lock(&rdev->mutex); |
| 1639 | _regulator_disable(rdev, &supply_rdev); |
| 1640 | mutex_unlock(&rdev->mutex); |
| 1641 | } |
| 1642 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1643 | return ret; |
| 1644 | } |
| 1645 | EXPORT_SYMBOL_GPL(regulator_disable); |
| 1646 | |
| 1647 | /* locks held by regulator_force_disable() */ |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1648 | static int _regulator_force_disable(struct regulator_dev *rdev, |
| 1649 | struct regulator_dev **supply_rdev_ptr) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1650 | { |
| 1651 | int ret = 0; |
| 1652 | |
| 1653 | /* force disable */ |
| 1654 | if (rdev->desc->ops->disable) { |
| 1655 | /* ah well, who wants to live forever... */ |
| 1656 | ret = rdev->desc->ops->disable(rdev); |
| 1657 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1658 | rdev_err(rdev, "failed to force disable\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1659 | return ret; |
| 1660 | } |
| 1661 | /* notify other consumers that power has been forced off */ |
Mark Brown | 84b6826 | 2009-12-01 21:12:27 +0000 | [diff] [blame] | 1662 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
| 1663 | REGULATOR_EVENT_DISABLE, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | /* decrease our supplies ref count and disable if required */ |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1667 | *supply_rdev_ptr = rdev->supply; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1668 | |
| 1669 | rdev->use_count = 0; |
| 1670 | return ret; |
| 1671 | } |
| 1672 | |
| 1673 | /** |
| 1674 | * regulator_force_disable - force disable regulator output |
| 1675 | * @regulator: regulator source |
| 1676 | * |
| 1677 | * Forcibly disable the regulator output voltage or current. |
| 1678 | * NOTE: this *will* disable the regulator output even if other consumer |
| 1679 | * devices have it enabled. This should be used for situations when device |
| 1680 | * damage will likely occur if the regulator is not disabled (e.g. over temp). |
| 1681 | */ |
| 1682 | int regulator_force_disable(struct regulator *regulator) |
| 1683 | { |
Mark Brown | 82d1583 | 2011-05-09 11:41:02 +0200 | [diff] [blame] | 1684 | struct regulator_dev *rdev = regulator->rdev; |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1685 | struct regulator_dev *supply_rdev = NULL; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1686 | int ret; |
| 1687 | |
Mark Brown | 82d1583 | 2011-05-09 11:41:02 +0200 | [diff] [blame] | 1688 | mutex_lock(&rdev->mutex); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1689 | regulator->uA_load = 0; |
Mark Brown | 82d1583 | 2011-05-09 11:41:02 +0200 | [diff] [blame] | 1690 | ret = _regulator_force_disable(rdev, &supply_rdev); |
| 1691 | mutex_unlock(&rdev->mutex); |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1692 | |
| 1693 | if (supply_rdev) |
| 1694 | regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev))); |
| 1695 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1696 | return ret; |
| 1697 | } |
| 1698 | EXPORT_SYMBOL_GPL(regulator_force_disable); |
| 1699 | |
| 1700 | static int _regulator_is_enabled(struct regulator_dev *rdev) |
| 1701 | { |
Mark Brown | 9a7f6a4 | 2010-02-11 17:22:45 +0000 | [diff] [blame] | 1702 | /* 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] | 1703 | if (!rdev->desc->ops->is_enabled) |
Mark Brown | 9a7f6a4 | 2010-02-11 17:22:45 +0000 | [diff] [blame] | 1704 | return 1; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1705 | |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 1706 | return rdev->desc->ops->is_enabled(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | /** |
| 1710 | * regulator_is_enabled - is the regulator output enabled |
| 1711 | * @regulator: regulator source |
| 1712 | * |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1713 | * Returns positive if the regulator driver backing the source/client |
| 1714 | * has requested that the device be enabled, zero if it hasn't, else a |
| 1715 | * negative errno code. |
| 1716 | * |
| 1717 | * Note that the device backing this regulator handle can have multiple |
| 1718 | * users, so it might be enabled even if regulator_enable() was never |
| 1719 | * called for this particular source. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1720 | */ |
| 1721 | int regulator_is_enabled(struct regulator *regulator) |
| 1722 | { |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 1723 | int ret; |
| 1724 | |
| 1725 | mutex_lock(®ulator->rdev->mutex); |
| 1726 | ret = _regulator_is_enabled(regulator->rdev); |
| 1727 | mutex_unlock(®ulator->rdev->mutex); |
| 1728 | |
| 1729 | return ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1730 | } |
| 1731 | EXPORT_SYMBOL_GPL(regulator_is_enabled); |
| 1732 | |
| 1733 | /** |
David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 1734 | * regulator_count_voltages - count regulator_list_voltage() selectors |
| 1735 | * @regulator: regulator source |
| 1736 | * |
| 1737 | * Returns number of selectors, or negative errno. Selectors are |
| 1738 | * numbered starting at zero, and typically correspond to bitfields |
| 1739 | * in hardware registers. |
| 1740 | */ |
| 1741 | int regulator_count_voltages(struct regulator *regulator) |
| 1742 | { |
| 1743 | struct regulator_dev *rdev = regulator->rdev; |
| 1744 | |
| 1745 | return rdev->desc->n_voltages ? : -EINVAL; |
| 1746 | } |
| 1747 | EXPORT_SYMBOL_GPL(regulator_count_voltages); |
| 1748 | |
| 1749 | /** |
| 1750 | * regulator_list_voltage - enumerate supported voltages |
| 1751 | * @regulator: regulator source |
| 1752 | * @selector: identify voltage to list |
| 1753 | * Context: can sleep |
| 1754 | * |
| 1755 | * Returns a voltage that can be passed to @regulator_set_voltage(), |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 1756 | * 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] | 1757 | * negative errno. |
| 1758 | */ |
| 1759 | int regulator_list_voltage(struct regulator *regulator, unsigned selector) |
| 1760 | { |
| 1761 | struct regulator_dev *rdev = regulator->rdev; |
| 1762 | struct regulator_ops *ops = rdev->desc->ops; |
| 1763 | int ret; |
| 1764 | |
| 1765 | if (!ops->list_voltage || selector >= rdev->desc->n_voltages) |
| 1766 | return -EINVAL; |
| 1767 | |
| 1768 | mutex_lock(&rdev->mutex); |
| 1769 | ret = ops->list_voltage(rdev, selector); |
| 1770 | mutex_unlock(&rdev->mutex); |
| 1771 | |
| 1772 | if (ret > 0) { |
| 1773 | if (ret < rdev->constraints->min_uV) |
| 1774 | ret = 0; |
| 1775 | else if (ret > rdev->constraints->max_uV) |
| 1776 | ret = 0; |
| 1777 | } |
| 1778 | |
| 1779 | return ret; |
| 1780 | } |
| 1781 | EXPORT_SYMBOL_GPL(regulator_list_voltage); |
| 1782 | |
| 1783 | /** |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 1784 | * regulator_is_supported_voltage - check if a voltage range can be supported |
| 1785 | * |
| 1786 | * @regulator: Regulator to check. |
| 1787 | * @min_uV: Minimum required voltage in uV. |
| 1788 | * @max_uV: Maximum required voltage in uV. |
| 1789 | * |
| 1790 | * Returns a boolean or a negative error code. |
| 1791 | */ |
| 1792 | int regulator_is_supported_voltage(struct regulator *regulator, |
| 1793 | int min_uV, int max_uV) |
| 1794 | { |
| 1795 | int i, voltages, ret; |
| 1796 | |
| 1797 | ret = regulator_count_voltages(regulator); |
| 1798 | if (ret < 0) |
| 1799 | return ret; |
| 1800 | voltages = ret; |
| 1801 | |
| 1802 | for (i = 0; i < voltages; i++) { |
| 1803 | ret = regulator_list_voltage(regulator, i); |
| 1804 | |
| 1805 | if (ret >= min_uV && ret <= max_uV) |
| 1806 | return 1; |
| 1807 | } |
| 1808 | |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 1812 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
| 1813 | int min_uV, int max_uV) |
| 1814 | { |
| 1815 | int ret; |
Linus Walleij | 77af1b2 | 2011-03-17 13:24:36 +0100 | [diff] [blame] | 1816 | int delay = 0; |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 1817 | unsigned int selector; |
| 1818 | |
| 1819 | trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); |
| 1820 | |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 1821 | min_uV += rdev->constraints->uV_offset; |
| 1822 | max_uV += rdev->constraints->uV_offset; |
| 1823 | |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 1824 | if (rdev->desc->ops->set_voltage) { |
| 1825 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, |
| 1826 | &selector); |
| 1827 | |
| 1828 | if (rdev->desc->ops->list_voltage) |
| 1829 | selector = rdev->desc->ops->list_voltage(rdev, |
| 1830 | selector); |
| 1831 | else |
| 1832 | selector = -1; |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 1833 | } else if (rdev->desc->ops->set_voltage_sel) { |
| 1834 | int best_val = INT_MAX; |
| 1835 | int i; |
| 1836 | |
| 1837 | selector = 0; |
| 1838 | |
| 1839 | /* Find the smallest voltage that falls within the specified |
| 1840 | * range. |
| 1841 | */ |
| 1842 | for (i = 0; i < rdev->desc->n_voltages; i++) { |
| 1843 | ret = rdev->desc->ops->list_voltage(rdev, i); |
| 1844 | if (ret < 0) |
| 1845 | continue; |
| 1846 | |
| 1847 | if (ret < best_val && ret >= min_uV && ret <= max_uV) { |
| 1848 | best_val = ret; |
| 1849 | selector = i; |
| 1850 | } |
| 1851 | } |
| 1852 | |
Linus Walleij | 77af1b2 | 2011-03-17 13:24:36 +0100 | [diff] [blame] | 1853 | /* |
| 1854 | * If we can't obtain the old selector there is not enough |
| 1855 | * info to call set_voltage_time_sel(). |
| 1856 | */ |
| 1857 | if (rdev->desc->ops->set_voltage_time_sel && |
| 1858 | rdev->desc->ops->get_voltage_sel) { |
| 1859 | unsigned int old_selector = 0; |
| 1860 | |
| 1861 | ret = rdev->desc->ops->get_voltage_sel(rdev); |
| 1862 | if (ret < 0) |
| 1863 | return ret; |
| 1864 | old_selector = ret; |
| 1865 | delay = rdev->desc->ops->set_voltage_time_sel(rdev, |
| 1866 | old_selector, selector); |
| 1867 | } |
| 1868 | |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 1869 | if (best_val != INT_MAX) { |
| 1870 | ret = rdev->desc->ops->set_voltage_sel(rdev, selector); |
| 1871 | selector = best_val; |
| 1872 | } else { |
| 1873 | ret = -EINVAL; |
| 1874 | } |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 1875 | } else { |
| 1876 | ret = -EINVAL; |
| 1877 | } |
| 1878 | |
Linus Walleij | 77af1b2 | 2011-03-17 13:24:36 +0100 | [diff] [blame] | 1879 | /* Insert any necessary delays */ |
| 1880 | if (delay >= 1000) { |
| 1881 | mdelay(delay / 1000); |
| 1882 | udelay(delay % 1000); |
| 1883 | } else if (delay) { |
| 1884 | udelay(delay); |
| 1885 | } |
| 1886 | |
Mark Brown | ded06a5 | 2010-12-16 13:59:10 +0000 | [diff] [blame] | 1887 | if (ret == 0) |
| 1888 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, |
| 1889 | NULL); |
| 1890 | |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 1891 | trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector); |
| 1892 | |
| 1893 | return ret; |
| 1894 | } |
| 1895 | |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 1896 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1897 | * regulator_set_voltage - set regulator output voltage |
| 1898 | * @regulator: regulator source |
| 1899 | * @min_uV: Minimum required voltage in uV |
| 1900 | * @max_uV: Maximum acceptable voltage in uV |
| 1901 | * |
| 1902 | * Sets a voltage regulator to the desired output voltage. This can be set |
| 1903 | * during any regulator state. IOW, regulator can be disabled or enabled. |
| 1904 | * |
| 1905 | * If the regulator is enabled then the voltage will change to the new value |
| 1906 | * immediately otherwise if the regulator is disabled the regulator will |
| 1907 | * output at the new voltage when enabled. |
| 1908 | * |
| 1909 | * NOTE: If the regulator is shared between several devices then the lowest |
| 1910 | * request voltage that meets the system constraints will be used. |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1911 | * Regulator system constraints must be set for this regulator before |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1912 | * calling this function otherwise this call will fail. |
| 1913 | */ |
| 1914 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) |
| 1915 | { |
| 1916 | struct regulator_dev *rdev = regulator->rdev; |
Mark Brown | 95a3c23 | 2010-12-16 15:49:37 +0000 | [diff] [blame] | 1917 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1918 | |
| 1919 | mutex_lock(&rdev->mutex); |
| 1920 | |
Mark Brown | 95a3c23 | 2010-12-16 15:49:37 +0000 | [diff] [blame] | 1921 | /* If we're setting the same range as last time the change |
| 1922 | * should be a noop (some cpufreq implementations use the same |
| 1923 | * voltage for multiple frequencies, for example). |
| 1924 | */ |
| 1925 | if (regulator->min_uV == min_uV && regulator->max_uV == max_uV) |
| 1926 | goto out; |
| 1927 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1928 | /* sanity check */ |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 1929 | if (!rdev->desc->ops->set_voltage && |
| 1930 | !rdev->desc->ops->set_voltage_sel) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1931 | ret = -EINVAL; |
| 1932 | goto out; |
| 1933 | } |
| 1934 | |
| 1935 | /* constraints check */ |
| 1936 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); |
| 1937 | if (ret < 0) |
| 1938 | goto out; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1939 | |
| 1940 | if (regulator->enabled) { |
| 1941 | ret = update_voltage(regulator, min_uV, max_uV); |
| 1942 | if (ret) |
| 1943 | goto out; |
| 1944 | } |
| 1945 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1946 | regulator->min_uV = min_uV; |
| 1947 | regulator->max_uV = max_uV; |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 1948 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1949 | out: |
| 1950 | mutex_unlock(&rdev->mutex); |
| 1951 | return ret; |
| 1952 | } |
| 1953 | EXPORT_SYMBOL_GPL(regulator_set_voltage); |
| 1954 | |
Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 1955 | /** |
Linus Walleij | 88cd222 | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 1956 | * regulator_set_voltage_time - get raise/fall time |
| 1957 | * @regulator: regulator source |
| 1958 | * @old_uV: starting voltage in microvolts |
| 1959 | * @new_uV: target voltage in microvolts |
| 1960 | * |
| 1961 | * Provided with the starting and ending voltage, this function attempts to |
| 1962 | * calculate the time in microseconds required to rise or fall to this new |
| 1963 | * voltage. |
| 1964 | */ |
| 1965 | int regulator_set_voltage_time(struct regulator *regulator, |
| 1966 | int old_uV, int new_uV) |
| 1967 | { |
| 1968 | struct regulator_dev *rdev = regulator->rdev; |
| 1969 | struct regulator_ops *ops = rdev->desc->ops; |
| 1970 | int old_sel = -1; |
| 1971 | int new_sel = -1; |
| 1972 | int voltage; |
| 1973 | int i; |
| 1974 | |
| 1975 | /* Currently requires operations to do this */ |
| 1976 | if (!ops->list_voltage || !ops->set_voltage_time_sel |
| 1977 | || !rdev->desc->n_voltages) |
| 1978 | return -EINVAL; |
| 1979 | |
| 1980 | for (i = 0; i < rdev->desc->n_voltages; i++) { |
| 1981 | /* We only look for exact voltage matches here */ |
| 1982 | voltage = regulator_list_voltage(regulator, i); |
| 1983 | if (voltage < 0) |
| 1984 | return -EINVAL; |
| 1985 | if (voltage == 0) |
| 1986 | continue; |
| 1987 | if (voltage == old_uV) |
| 1988 | old_sel = i; |
| 1989 | if (voltage == new_uV) |
| 1990 | new_sel = i; |
| 1991 | } |
| 1992 | |
| 1993 | if (old_sel < 0 || new_sel < 0) |
| 1994 | return -EINVAL; |
| 1995 | |
| 1996 | return ops->set_voltage_time_sel(rdev, old_sel, new_sel); |
| 1997 | } |
| 1998 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time); |
| 1999 | |
| 2000 | /** |
Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 2001 | * regulator_sync_voltage - re-apply last regulator output voltage |
| 2002 | * @regulator: regulator source |
| 2003 | * |
| 2004 | * Re-apply the last configured voltage. This is intended to be used |
| 2005 | * where some external control source the consumer is cooperating with |
| 2006 | * has caused the configured voltage to change. |
| 2007 | */ |
| 2008 | int regulator_sync_voltage(struct regulator *regulator) |
| 2009 | { |
| 2010 | struct regulator_dev *rdev = regulator->rdev; |
| 2011 | int ret, min_uV, max_uV; |
| 2012 | |
| 2013 | mutex_lock(&rdev->mutex); |
| 2014 | |
| 2015 | if (!rdev->desc->ops->set_voltage && |
| 2016 | !rdev->desc->ops->set_voltage_sel) { |
| 2017 | ret = -EINVAL; |
| 2018 | goto out; |
| 2019 | } |
| 2020 | |
| 2021 | /* This is only going to work if we've had a voltage configured. */ |
| 2022 | if (!regulator->min_uV && !regulator->max_uV) { |
| 2023 | ret = -EINVAL; |
| 2024 | goto out; |
| 2025 | } |
| 2026 | |
| 2027 | min_uV = regulator->min_uV; |
| 2028 | max_uV = regulator->max_uV; |
| 2029 | |
| 2030 | /* This should be a paranoia check... */ |
| 2031 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); |
| 2032 | if (ret < 0) |
| 2033 | goto out; |
| 2034 | |
| 2035 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV); |
| 2036 | if (ret < 0) |
| 2037 | goto out; |
| 2038 | |
| 2039 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); |
| 2040 | |
| 2041 | out: |
| 2042 | mutex_unlock(&rdev->mutex); |
| 2043 | return ret; |
| 2044 | } |
| 2045 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); |
| 2046 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2047 | static int _regulator_get_voltage(struct regulator_dev *rdev) |
| 2048 | { |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2049 | int sel, ret; |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 2050 | |
| 2051 | if (rdev->desc->ops->get_voltage_sel) { |
| 2052 | sel = rdev->desc->ops->get_voltage_sel(rdev); |
| 2053 | if (sel < 0) |
| 2054 | return sel; |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2055 | ret = rdev->desc->ops->list_voltage(rdev, sel); |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 2056 | } else if (rdev->desc->ops->get_voltage) { |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2057 | ret = rdev->desc->ops->get_voltage(rdev); |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 2058 | } else { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2059 | return -EINVAL; |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 2060 | } |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2061 | |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 2062 | if (ret < 0) |
| 2063 | return ret; |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2064 | return ret - rdev->constraints->uV_offset; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | /** |
| 2068 | * regulator_get_voltage - get regulator output voltage |
| 2069 | * @regulator: regulator source |
| 2070 | * |
| 2071 | * This returns the current regulator voltage in uV. |
| 2072 | * |
| 2073 | * NOTE: If the regulator is disabled it will return the voltage value. This |
| 2074 | * function should not be used to determine regulator state. |
| 2075 | */ |
| 2076 | int regulator_get_voltage(struct regulator *regulator) |
| 2077 | { |
| 2078 | int ret; |
| 2079 | |
| 2080 | mutex_lock(®ulator->rdev->mutex); |
| 2081 | |
| 2082 | ret = _regulator_get_voltage(regulator->rdev); |
| 2083 | |
| 2084 | mutex_unlock(®ulator->rdev->mutex); |
| 2085 | |
| 2086 | return ret; |
| 2087 | } |
| 2088 | EXPORT_SYMBOL_GPL(regulator_get_voltage); |
| 2089 | |
| 2090 | /** |
| 2091 | * regulator_set_current_limit - set regulator output current limit |
| 2092 | * @regulator: regulator source |
| 2093 | * @min_uA: Minimuum supported current in uA |
| 2094 | * @max_uA: Maximum supported current in uA |
| 2095 | * |
| 2096 | * Sets current sink to the desired output current. This can be set during |
| 2097 | * any regulator state. IOW, regulator can be disabled or enabled. |
| 2098 | * |
| 2099 | * If the regulator is enabled then the current will change to the new value |
| 2100 | * immediately otherwise if the regulator is disabled the regulator will |
| 2101 | * output at the new current when enabled. |
| 2102 | * |
| 2103 | * NOTE: Regulator system constraints must be set for this regulator before |
| 2104 | * calling this function otherwise this call will fail. |
| 2105 | */ |
| 2106 | int regulator_set_current_limit(struct regulator *regulator, |
| 2107 | int min_uA, int max_uA) |
| 2108 | { |
| 2109 | struct regulator_dev *rdev = regulator->rdev; |
| 2110 | int ret; |
| 2111 | |
| 2112 | mutex_lock(&rdev->mutex); |
| 2113 | |
| 2114 | /* sanity check */ |
| 2115 | if (!rdev->desc->ops->set_current_limit) { |
| 2116 | ret = -EINVAL; |
| 2117 | goto out; |
| 2118 | } |
| 2119 | |
| 2120 | /* constraints check */ |
| 2121 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); |
| 2122 | if (ret < 0) |
| 2123 | goto out; |
| 2124 | |
| 2125 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); |
| 2126 | out: |
| 2127 | mutex_unlock(&rdev->mutex); |
| 2128 | return ret; |
| 2129 | } |
| 2130 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); |
| 2131 | |
| 2132 | static int _regulator_get_current_limit(struct regulator_dev *rdev) |
| 2133 | { |
| 2134 | int ret; |
| 2135 | |
| 2136 | mutex_lock(&rdev->mutex); |
| 2137 | |
| 2138 | /* sanity check */ |
| 2139 | if (!rdev->desc->ops->get_current_limit) { |
| 2140 | ret = -EINVAL; |
| 2141 | goto out; |
| 2142 | } |
| 2143 | |
| 2144 | ret = rdev->desc->ops->get_current_limit(rdev); |
| 2145 | out: |
| 2146 | mutex_unlock(&rdev->mutex); |
| 2147 | return ret; |
| 2148 | } |
| 2149 | |
| 2150 | /** |
| 2151 | * regulator_get_current_limit - get regulator output current |
| 2152 | * @regulator: regulator source |
| 2153 | * |
| 2154 | * This returns the current supplied by the specified current sink in uA. |
| 2155 | * |
| 2156 | * NOTE: If the regulator is disabled it will return the current value. This |
| 2157 | * function should not be used to determine regulator state. |
| 2158 | */ |
| 2159 | int regulator_get_current_limit(struct regulator *regulator) |
| 2160 | { |
| 2161 | return _regulator_get_current_limit(regulator->rdev); |
| 2162 | } |
| 2163 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); |
| 2164 | |
| 2165 | /** |
| 2166 | * regulator_set_mode - set regulator operating mode |
| 2167 | * @regulator: regulator source |
| 2168 | * @mode: operating mode - one of the REGULATOR_MODE constants |
| 2169 | * |
| 2170 | * Set regulator operating mode to increase regulator efficiency or improve |
| 2171 | * regulation performance. |
| 2172 | * |
| 2173 | * NOTE: Regulator system constraints must be set for this regulator before |
| 2174 | * calling this function otherwise this call will fail. |
| 2175 | */ |
| 2176 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) |
| 2177 | { |
| 2178 | struct regulator_dev *rdev = regulator->rdev; |
| 2179 | int ret; |
Sundar R Iyer | 500b4ac | 2010-05-17 21:24:48 +0530 | [diff] [blame] | 2180 | int regulator_curr_mode; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2181 | |
| 2182 | mutex_lock(&rdev->mutex); |
| 2183 | |
| 2184 | /* sanity check */ |
| 2185 | if (!rdev->desc->ops->set_mode) { |
| 2186 | ret = -EINVAL; |
| 2187 | goto out; |
| 2188 | } |
| 2189 | |
Sundar R Iyer | 500b4ac | 2010-05-17 21:24:48 +0530 | [diff] [blame] | 2190 | /* return if the same mode is requested */ |
| 2191 | if (rdev->desc->ops->get_mode) { |
| 2192 | regulator_curr_mode = rdev->desc->ops->get_mode(rdev); |
| 2193 | if (regulator_curr_mode == mode) { |
| 2194 | ret = 0; |
| 2195 | goto out; |
| 2196 | } |
| 2197 | } |
| 2198 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2199 | /* constraints check */ |
Axel Lin | 22c51b4 | 2011-04-01 18:25:25 +0800 | [diff] [blame] | 2200 | ret = regulator_mode_constrain(rdev, &mode); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2201 | if (ret < 0) |
| 2202 | goto out; |
| 2203 | |
| 2204 | ret = rdev->desc->ops->set_mode(rdev, mode); |
| 2205 | out: |
| 2206 | mutex_unlock(&rdev->mutex); |
| 2207 | return ret; |
| 2208 | } |
| 2209 | EXPORT_SYMBOL_GPL(regulator_set_mode); |
| 2210 | |
| 2211 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) |
| 2212 | { |
| 2213 | int ret; |
| 2214 | |
| 2215 | mutex_lock(&rdev->mutex); |
| 2216 | |
| 2217 | /* sanity check */ |
| 2218 | if (!rdev->desc->ops->get_mode) { |
| 2219 | ret = -EINVAL; |
| 2220 | goto out; |
| 2221 | } |
| 2222 | |
| 2223 | ret = rdev->desc->ops->get_mode(rdev); |
| 2224 | out: |
| 2225 | mutex_unlock(&rdev->mutex); |
| 2226 | return ret; |
| 2227 | } |
| 2228 | |
| 2229 | /** |
| 2230 | * regulator_get_mode - get regulator operating mode |
| 2231 | * @regulator: regulator source |
| 2232 | * |
| 2233 | * Get the current regulator operating mode. |
| 2234 | */ |
| 2235 | unsigned int regulator_get_mode(struct regulator *regulator) |
| 2236 | { |
| 2237 | return _regulator_get_mode(regulator->rdev); |
| 2238 | } |
| 2239 | EXPORT_SYMBOL_GPL(regulator_get_mode); |
| 2240 | |
| 2241 | /** |
| 2242 | * regulator_set_optimum_mode - set regulator optimum operating mode |
| 2243 | * @regulator: regulator source |
| 2244 | * @uA_load: load current |
| 2245 | * |
| 2246 | * Notifies the regulator core of a new device load. This is then used by |
| 2247 | * DRMS (if enabled by constraints) to set the most efficient regulator |
| 2248 | * operating mode for the new regulator loading. |
| 2249 | * |
| 2250 | * Consumer devices notify their supply regulator of the maximum power |
| 2251 | * they will require (can be taken from device datasheet in the power |
| 2252 | * consumption tables) when they change operational status and hence power |
| 2253 | * state. Examples of operational state changes that can affect power |
| 2254 | * consumption are :- |
| 2255 | * |
| 2256 | * o Device is opened / closed. |
| 2257 | * o Device I/O is about to begin or has just finished. |
| 2258 | * o Device is idling in between work. |
| 2259 | * |
| 2260 | * This information is also exported via sysfs to userspace. |
| 2261 | * |
| 2262 | * DRMS will sum the total requested load on the regulator and change |
| 2263 | * to the most efficient operating mode if platform constraints allow. |
| 2264 | * |
| 2265 | * Returns the new regulator mode or error. |
| 2266 | */ |
| 2267 | int regulator_set_optimum_mode(struct regulator *regulator, int uA_load) |
| 2268 | { |
| 2269 | struct regulator_dev *rdev = regulator->rdev; |
| 2270 | struct regulator *consumer; |
| 2271 | int ret, output_uV, input_uV, total_uA_load = 0; |
| 2272 | unsigned int mode; |
| 2273 | |
| 2274 | mutex_lock(&rdev->mutex); |
| 2275 | |
Mark Brown | a4b4148 | 2011-05-14 11:19:45 -0700 | [diff] [blame] | 2276 | /* |
| 2277 | * first check to see if we can set modes at all, otherwise just |
| 2278 | * tell the consumer everything is OK. |
| 2279 | */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2280 | regulator->uA_load = uA_load; |
| 2281 | ret = regulator_check_drms(rdev); |
Mark Brown | a4b4148 | 2011-05-14 11:19:45 -0700 | [diff] [blame] | 2282 | if (ret < 0) { |
| 2283 | ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2284 | goto out; |
Mark Brown | a4b4148 | 2011-05-14 11:19:45 -0700 | [diff] [blame] | 2285 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2286 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2287 | if (!rdev->desc->ops->get_optimum_mode) |
| 2288 | goto out; |
| 2289 | |
Mark Brown | a4b4148 | 2011-05-14 11:19:45 -0700 | [diff] [blame] | 2290 | /* |
| 2291 | * we can actually do this so any errors are indicators of |
| 2292 | * potential real failure. |
| 2293 | */ |
| 2294 | ret = -EINVAL; |
| 2295 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2296 | /* get output voltage */ |
Mark Brown | 1bf5a1f | 2010-12-10 17:28:06 +0000 | [diff] [blame] | 2297 | output_uV = _regulator_get_voltage(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2298 | if (output_uV <= 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2299 | rdev_err(rdev, "invalid output voltage found\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2300 | goto out; |
| 2301 | } |
| 2302 | |
| 2303 | /* get input voltage */ |
Mark Brown | 1bf5a1f | 2010-12-10 17:28:06 +0000 | [diff] [blame] | 2304 | input_uV = 0; |
| 2305 | if (rdev->supply) |
| 2306 | input_uV = _regulator_get_voltage(rdev->supply); |
| 2307 | if (input_uV <= 0) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2308 | input_uV = rdev->constraints->input_uV; |
| 2309 | if (input_uV <= 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2310 | rdev_err(rdev, "invalid input voltage found\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2311 | goto out; |
| 2312 | } |
| 2313 | |
| 2314 | /* calc total requested load for this regulator */ |
| 2315 | list_for_each_entry(consumer, &rdev->consumer_list, list) |
Stefan Roese | fa2984d | 2009-11-27 15:56:34 +0100 | [diff] [blame] | 2316 | total_uA_load += consumer->uA_load; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2317 | |
| 2318 | mode = rdev->desc->ops->get_optimum_mode(rdev, |
| 2319 | input_uV, output_uV, |
| 2320 | total_uA_load); |
Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 2321 | ret = regulator_mode_constrain(rdev, &mode); |
David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 2322 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2323 | rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", |
| 2324 | total_uA_load, input_uV, output_uV); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2325 | goto out; |
| 2326 | } |
| 2327 | |
| 2328 | ret = rdev->desc->ops->set_mode(rdev, mode); |
David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 2329 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2330 | rdev_err(rdev, "failed to set optimum mode %x\n", mode); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2331 | goto out; |
| 2332 | } |
| 2333 | ret = mode; |
| 2334 | out: |
| 2335 | mutex_unlock(&rdev->mutex); |
| 2336 | return ret; |
| 2337 | } |
| 2338 | EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); |
| 2339 | |
| 2340 | /** |
| 2341 | * regulator_register_notifier - register regulator event notifier |
| 2342 | * @regulator: regulator source |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2343 | * @nb: notifier block |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2344 | * |
| 2345 | * Register notifier block to receive regulator events. |
| 2346 | */ |
| 2347 | int regulator_register_notifier(struct regulator *regulator, |
| 2348 | struct notifier_block *nb) |
| 2349 | { |
| 2350 | return blocking_notifier_chain_register(®ulator->rdev->notifier, |
| 2351 | nb); |
| 2352 | } |
| 2353 | EXPORT_SYMBOL_GPL(regulator_register_notifier); |
| 2354 | |
| 2355 | /** |
| 2356 | * regulator_unregister_notifier - unregister regulator event notifier |
| 2357 | * @regulator: regulator source |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2358 | * @nb: notifier block |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2359 | * |
| 2360 | * Unregister regulator event notifier block. |
| 2361 | */ |
| 2362 | int regulator_unregister_notifier(struct regulator *regulator, |
| 2363 | struct notifier_block *nb) |
| 2364 | { |
| 2365 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, |
| 2366 | nb); |
| 2367 | } |
| 2368 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); |
| 2369 | |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 2370 | /* notify regulator consumers and downstream regulator consumers. |
| 2371 | * Note mutex must be held by caller. |
| 2372 | */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2373 | static void _notifier_call_chain(struct regulator_dev *rdev, |
| 2374 | unsigned long event, void *data) |
| 2375 | { |
| 2376 | struct regulator_dev *_rdev; |
| 2377 | |
| 2378 | /* call rdev chain first */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2379 | blocking_notifier_call_chain(&rdev->notifier, event, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2380 | |
| 2381 | /* now notify regulator we supply */ |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 2382 | list_for_each_entry(_rdev, &rdev->supply_list, slist) { |
Stefan Roese | fa2984d | 2009-11-27 15:56:34 +0100 | [diff] [blame] | 2383 | mutex_lock(&_rdev->mutex); |
| 2384 | _notifier_call_chain(_rdev, event, data); |
| 2385 | mutex_unlock(&_rdev->mutex); |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 2386 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | /** |
| 2390 | * regulator_bulk_get - get multiple regulator consumers |
| 2391 | * |
| 2392 | * @dev: Device to supply |
| 2393 | * @num_consumers: Number of consumers to register |
| 2394 | * @consumers: Configuration of consumers; clients are stored here. |
| 2395 | * |
| 2396 | * @return 0 on success, an errno on failure. |
| 2397 | * |
| 2398 | * This helper function allows drivers to get several regulator |
| 2399 | * consumers in one operation. If any of the regulators cannot be |
| 2400 | * acquired then any regulators that were allocated will be freed |
| 2401 | * before returning to the caller. |
| 2402 | */ |
| 2403 | int regulator_bulk_get(struct device *dev, int num_consumers, |
| 2404 | struct regulator_bulk_data *consumers) |
| 2405 | { |
| 2406 | int i; |
| 2407 | int ret; |
| 2408 | |
| 2409 | for (i = 0; i < num_consumers; i++) |
| 2410 | consumers[i].consumer = NULL; |
| 2411 | |
| 2412 | for (i = 0; i < num_consumers; i++) { |
| 2413 | consumers[i].consumer = regulator_get(dev, |
| 2414 | consumers[i].supply); |
| 2415 | if (IS_ERR(consumers[i].consumer)) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2416 | ret = PTR_ERR(consumers[i].consumer); |
Mark Brown | 5b30762 | 2009-10-13 13:06:49 +0100 | [diff] [blame] | 2417 | dev_err(dev, "Failed to get supply '%s': %d\n", |
| 2418 | consumers[i].supply, ret); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2419 | consumers[i].consumer = NULL; |
| 2420 | goto err; |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | return 0; |
| 2425 | |
| 2426 | err: |
| 2427 | for (i = 0; i < num_consumers && consumers[i].consumer; i++) |
| 2428 | regulator_put(consumers[i].consumer); |
| 2429 | |
| 2430 | return ret; |
| 2431 | } |
| 2432 | EXPORT_SYMBOL_GPL(regulator_bulk_get); |
| 2433 | |
| 2434 | /** |
| 2435 | * regulator_bulk_enable - enable multiple regulator consumers |
| 2436 | * |
| 2437 | * @num_consumers: Number of consumers |
| 2438 | * @consumers: Consumer data; clients are stored here. |
| 2439 | * @return 0 on success, an errno on failure |
| 2440 | * |
| 2441 | * This convenience API allows consumers to enable multiple regulator |
| 2442 | * clients in a single API call. If any consumers cannot be enabled |
| 2443 | * then any others that were enabled will be disabled again prior to |
| 2444 | * return. |
| 2445 | */ |
| 2446 | int regulator_bulk_enable(int num_consumers, |
| 2447 | struct regulator_bulk_data *consumers) |
| 2448 | { |
| 2449 | int i; |
| 2450 | int ret; |
| 2451 | |
| 2452 | for (i = 0; i < num_consumers; i++) { |
| 2453 | ret = regulator_enable(consumers[i].consumer); |
| 2454 | if (ret != 0) |
| 2455 | goto err; |
| 2456 | } |
| 2457 | |
| 2458 | return 0; |
| 2459 | |
| 2460 | err: |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2461 | 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] | 2462 | for (--i; i >= 0; --i) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2463 | regulator_disable(consumers[i].consumer); |
| 2464 | |
| 2465 | return ret; |
| 2466 | } |
| 2467 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); |
| 2468 | |
| 2469 | /** |
Justin Paupore | 1d17cf5 | 2011-08-16 15:39:01 -0700 | [diff] [blame] | 2470 | * regulator_bulk_set_voltage - set voltage for multiple regulator consumers |
| 2471 | * |
| 2472 | * @num_consumers: Number of consumers |
| 2473 | * @consumers: Consumer data; clients are stored here. |
| 2474 | * @return 0 on success, an errno on failure |
| 2475 | * |
| 2476 | * This convenience API allows the voted voltage ranges of multiple regulator |
| 2477 | * clients to be set in a single API call. If any consumers cannot have their |
| 2478 | * voltages set, this function returns WITHOUT withdrawing votes for any |
| 2479 | * consumers that have already been set. |
| 2480 | */ |
| 2481 | int regulator_bulk_set_voltage(int num_consumers, |
| 2482 | struct regulator_bulk_data *consumers) |
| 2483 | { |
| 2484 | int i; |
| 2485 | int rc; |
| 2486 | |
| 2487 | for (i = 0; i < num_consumers; i++) { |
| 2488 | if (!consumers[i].min_uV && !consumers[i].max_uV) |
| 2489 | continue; |
| 2490 | rc = regulator_set_voltage(consumers[i].consumer, |
| 2491 | consumers[i].min_uV, |
| 2492 | consumers[i].max_uV); |
| 2493 | if (rc) |
| 2494 | goto err; |
| 2495 | } |
| 2496 | |
| 2497 | return 0; |
| 2498 | |
| 2499 | err: |
| 2500 | pr_err("Failed to set voltage for %s: %d\n", consumers[i].supply, rc); |
| 2501 | return rc; |
| 2502 | } |
| 2503 | EXPORT_SYMBOL_GPL(regulator_bulk_set_voltage); |
| 2504 | |
| 2505 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2506 | * regulator_bulk_disable - disable multiple regulator consumers |
| 2507 | * |
| 2508 | * @num_consumers: Number of consumers |
| 2509 | * @consumers: Consumer data; clients are stored here. |
| 2510 | * @return 0 on success, an errno on failure |
| 2511 | * |
| 2512 | * This convenience API allows consumers to disable multiple regulator |
| 2513 | * clients in a single API call. If any consumers cannot be enabled |
| 2514 | * then any others that were disabled will be disabled again prior to |
| 2515 | * return. |
| 2516 | */ |
| 2517 | int regulator_bulk_disable(int num_consumers, |
| 2518 | struct regulator_bulk_data *consumers) |
| 2519 | { |
| 2520 | int i; |
| 2521 | int ret; |
| 2522 | |
| 2523 | for (i = 0; i < num_consumers; i++) { |
| 2524 | ret = regulator_disable(consumers[i].consumer); |
| 2525 | if (ret != 0) |
| 2526 | goto err; |
| 2527 | } |
| 2528 | |
| 2529 | return 0; |
| 2530 | |
| 2531 | err: |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2532 | 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] | 2533 | for (--i; i >= 0; --i) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2534 | regulator_enable(consumers[i].consumer); |
| 2535 | |
| 2536 | return ret; |
| 2537 | } |
| 2538 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); |
| 2539 | |
| 2540 | /** |
| 2541 | * regulator_bulk_free - free multiple regulator consumers |
| 2542 | * |
| 2543 | * @num_consumers: Number of consumers |
| 2544 | * @consumers: Consumer data; clients are stored here. |
| 2545 | * |
| 2546 | * This convenience API allows consumers to free multiple regulator |
| 2547 | * clients in a single API call. |
| 2548 | */ |
| 2549 | void regulator_bulk_free(int num_consumers, |
| 2550 | struct regulator_bulk_data *consumers) |
| 2551 | { |
| 2552 | int i; |
| 2553 | |
| 2554 | for (i = 0; i < num_consumers; i++) { |
| 2555 | regulator_put(consumers[i].consumer); |
| 2556 | consumers[i].consumer = NULL; |
| 2557 | } |
| 2558 | } |
| 2559 | EXPORT_SYMBOL_GPL(regulator_bulk_free); |
| 2560 | |
| 2561 | /** |
| 2562 | * regulator_notifier_call_chain - call regulator event notifier |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2563 | * @rdev: regulator source |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2564 | * @event: notifier block |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2565 | * @data: callback-specific data. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2566 | * |
| 2567 | * Called by regulator drivers to notify clients a regulator event has |
| 2568 | * occurred. We also notify regulator clients downstream. |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 2569 | * Note lock must be held by caller. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2570 | */ |
| 2571 | int regulator_notifier_call_chain(struct regulator_dev *rdev, |
| 2572 | unsigned long event, void *data) |
| 2573 | { |
| 2574 | _notifier_call_chain(rdev, event, data); |
| 2575 | return NOTIFY_DONE; |
| 2576 | |
| 2577 | } |
| 2578 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); |
| 2579 | |
Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 2580 | /** |
| 2581 | * regulator_mode_to_status - convert a regulator mode into a status |
| 2582 | * |
| 2583 | * @mode: Mode to convert |
| 2584 | * |
| 2585 | * Convert a regulator mode into a status. |
| 2586 | */ |
| 2587 | int regulator_mode_to_status(unsigned int mode) |
| 2588 | { |
| 2589 | switch (mode) { |
| 2590 | case REGULATOR_MODE_FAST: |
| 2591 | return REGULATOR_STATUS_FAST; |
| 2592 | case REGULATOR_MODE_NORMAL: |
| 2593 | return REGULATOR_STATUS_NORMAL; |
| 2594 | case REGULATOR_MODE_IDLE: |
| 2595 | return REGULATOR_STATUS_IDLE; |
| 2596 | case REGULATOR_STATUS_STANDBY: |
| 2597 | return REGULATOR_STATUS_STANDBY; |
| 2598 | default: |
| 2599 | return 0; |
| 2600 | } |
| 2601 | } |
| 2602 | EXPORT_SYMBOL_GPL(regulator_mode_to_status); |
| 2603 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 2604 | /* |
| 2605 | * To avoid cluttering sysfs (and memory) with useless state, only |
| 2606 | * create attributes that can be meaningfully displayed. |
| 2607 | */ |
| 2608 | static int add_regulator_attributes(struct regulator_dev *rdev) |
| 2609 | { |
| 2610 | struct device *dev = &rdev->dev; |
| 2611 | struct regulator_ops *ops = rdev->desc->ops; |
| 2612 | int status = 0; |
| 2613 | |
| 2614 | /* some attributes need specific methods to be displayed */ |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 2615 | if (ops->get_voltage || ops->get_voltage_sel) { |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 2616 | status = device_create_file(dev, &dev_attr_microvolts); |
| 2617 | if (status < 0) |
| 2618 | return status; |
| 2619 | } |
| 2620 | if (ops->get_current_limit) { |
| 2621 | status = device_create_file(dev, &dev_attr_microamps); |
| 2622 | if (status < 0) |
| 2623 | return status; |
| 2624 | } |
| 2625 | if (ops->get_mode) { |
| 2626 | status = device_create_file(dev, &dev_attr_opmode); |
| 2627 | if (status < 0) |
| 2628 | return status; |
| 2629 | } |
| 2630 | if (ops->is_enabled) { |
| 2631 | status = device_create_file(dev, &dev_attr_state); |
| 2632 | if (status < 0) |
| 2633 | return status; |
| 2634 | } |
David Brownell | 853116a | 2009-01-14 23:03:17 -0800 | [diff] [blame] | 2635 | if (ops->get_status) { |
| 2636 | status = device_create_file(dev, &dev_attr_status); |
| 2637 | if (status < 0) |
| 2638 | return status; |
| 2639 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 2640 | |
| 2641 | /* some attributes are type-specific */ |
| 2642 | if (rdev->desc->type == REGULATOR_CURRENT) { |
| 2643 | status = device_create_file(dev, &dev_attr_requested_microamps); |
| 2644 | if (status < 0) |
| 2645 | return status; |
| 2646 | } |
| 2647 | |
| 2648 | /* all the other attributes exist to support constraints; |
| 2649 | * don't show them if there are no constraints, or if the |
| 2650 | * relevant supporting methods are missing. |
| 2651 | */ |
| 2652 | if (!rdev->constraints) |
| 2653 | return status; |
| 2654 | |
| 2655 | /* constraints need specific supporting methods */ |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2656 | if (ops->set_voltage || ops->set_voltage_sel) { |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 2657 | status = device_create_file(dev, &dev_attr_min_microvolts); |
| 2658 | if (status < 0) |
| 2659 | return status; |
| 2660 | status = device_create_file(dev, &dev_attr_max_microvolts); |
| 2661 | if (status < 0) |
| 2662 | return status; |
| 2663 | } |
| 2664 | if (ops->set_current_limit) { |
| 2665 | status = device_create_file(dev, &dev_attr_min_microamps); |
| 2666 | if (status < 0) |
| 2667 | return status; |
| 2668 | status = device_create_file(dev, &dev_attr_max_microamps); |
| 2669 | if (status < 0) |
| 2670 | return status; |
| 2671 | } |
| 2672 | |
| 2673 | /* suspend mode constraints need multiple supporting methods */ |
| 2674 | if (!(ops->set_suspend_enable && ops->set_suspend_disable)) |
| 2675 | return status; |
| 2676 | |
| 2677 | status = device_create_file(dev, &dev_attr_suspend_standby_state); |
| 2678 | if (status < 0) |
| 2679 | return status; |
| 2680 | status = device_create_file(dev, &dev_attr_suspend_mem_state); |
| 2681 | if (status < 0) |
| 2682 | return status; |
| 2683 | status = device_create_file(dev, &dev_attr_suspend_disk_state); |
| 2684 | if (status < 0) |
| 2685 | return status; |
| 2686 | |
| 2687 | if (ops->set_suspend_voltage) { |
| 2688 | status = device_create_file(dev, |
| 2689 | &dev_attr_suspend_standby_microvolts); |
| 2690 | if (status < 0) |
| 2691 | return status; |
| 2692 | status = device_create_file(dev, |
| 2693 | &dev_attr_suspend_mem_microvolts); |
| 2694 | if (status < 0) |
| 2695 | return status; |
| 2696 | status = device_create_file(dev, |
| 2697 | &dev_attr_suspend_disk_microvolts); |
| 2698 | if (status < 0) |
| 2699 | return status; |
| 2700 | } |
| 2701 | |
| 2702 | if (ops->set_suspend_mode) { |
| 2703 | status = device_create_file(dev, |
| 2704 | &dev_attr_suspend_standby_mode); |
| 2705 | if (status < 0) |
| 2706 | return status; |
| 2707 | status = device_create_file(dev, |
| 2708 | &dev_attr_suspend_mem_mode); |
| 2709 | if (status < 0) |
| 2710 | return status; |
| 2711 | status = device_create_file(dev, |
| 2712 | &dev_attr_suspend_disk_mode); |
| 2713 | if (status < 0) |
| 2714 | return status; |
| 2715 | } |
| 2716 | |
| 2717 | return status; |
| 2718 | } |
| 2719 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2720 | #ifdef CONFIG_DEBUG_FS |
| 2721 | |
| 2722 | #define MAX_DEBUG_BUF_LEN 50 |
| 2723 | |
| 2724 | static DEFINE_MUTEX(debug_buf_mutex); |
| 2725 | static char debug_buf[MAX_DEBUG_BUF_LEN]; |
| 2726 | |
| 2727 | static int reg_debug_enable_set(void *data, u64 val) |
| 2728 | { |
| 2729 | int err_info; |
| 2730 | if (IS_ERR(data) || data == NULL) { |
| 2731 | pr_err("Function Input Error %ld\n", PTR_ERR(data)); |
| 2732 | return -ENOMEM; |
| 2733 | } |
| 2734 | |
| 2735 | if (val) |
| 2736 | err_info = regulator_enable(data); |
| 2737 | else |
| 2738 | err_info = regulator_disable(data); |
| 2739 | |
| 2740 | return err_info; |
| 2741 | } |
| 2742 | |
| 2743 | static int reg_debug_enable_get(void *data, u64 *val) |
| 2744 | { |
| 2745 | if (IS_ERR(data) || data == NULL) { |
| 2746 | pr_err("Function Input Error %ld\n", PTR_ERR(data)); |
| 2747 | return -ENOMEM; |
| 2748 | } |
| 2749 | |
| 2750 | *val = regulator_is_enabled(data); |
| 2751 | return 0; |
| 2752 | } |
| 2753 | |
| 2754 | DEFINE_SIMPLE_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get, |
| 2755 | reg_debug_enable_set, "%llu\n"); |
| 2756 | |
| 2757 | static int reg_debug_fdisable_set(void *data, u64 val) |
| 2758 | { |
| 2759 | int err_info; |
| 2760 | if (IS_ERR(data) || data == NULL) { |
| 2761 | pr_err("Function Input Error %ld\n", PTR_ERR(data)); |
| 2762 | return -ENOMEM; |
| 2763 | } |
| 2764 | |
| 2765 | if (val > 0) |
| 2766 | err_info = regulator_force_disable(data); |
| 2767 | else |
| 2768 | err_info = 0; |
| 2769 | |
| 2770 | return err_info; |
| 2771 | } |
| 2772 | |
| 2773 | DEFINE_SIMPLE_ATTRIBUTE(reg_fdisable_fops, reg_debug_enable_get, |
| 2774 | reg_debug_fdisable_set, "%llu\n"); |
| 2775 | |
| 2776 | static ssize_t reg_debug_volt_set(struct file *file, const char __user *buf, |
| 2777 | size_t count, loff_t *ppos) |
| 2778 | { |
| 2779 | int err_info, filled; |
| 2780 | int min, max = -1; |
| 2781 | if (IS_ERR(file) || file == NULL) { |
| 2782 | pr_err("Function Input Error %ld\n", PTR_ERR(file)); |
| 2783 | return -ENOMEM; |
| 2784 | } |
| 2785 | |
| 2786 | if (count < MAX_DEBUG_BUF_LEN) { |
| 2787 | mutex_lock(&debug_buf_mutex); |
| 2788 | |
| 2789 | if (copy_from_user(debug_buf, (void __user *) buf, count)) |
| 2790 | return -EFAULT; |
| 2791 | |
| 2792 | debug_buf[count] = '\0'; |
| 2793 | filled = sscanf(debug_buf, "%d %d", &min, &max); |
| 2794 | |
| 2795 | mutex_unlock(&debug_buf_mutex); |
| 2796 | /* check that user entered two numbers */ |
| 2797 | if (filled < 2 || min < 0 || max < min) { |
| 2798 | pr_info("Error, correct format: 'echo \"min max\"" |
| 2799 | " > voltage"); |
| 2800 | return -ENOMEM; |
| 2801 | } else { |
| 2802 | err_info = regulator_set_voltage(file->private_data, |
| 2803 | min, max); |
| 2804 | } |
| 2805 | } else { |
| 2806 | pr_err("Error-Input voltage pair" |
| 2807 | " string exceeds maximum buffer length"); |
| 2808 | |
| 2809 | return -ENOMEM; |
| 2810 | } |
| 2811 | |
| 2812 | return count; |
| 2813 | } |
| 2814 | |
| 2815 | static ssize_t reg_debug_volt_get(struct file *file, char __user *buf, |
| 2816 | size_t count, loff_t *ppos) |
| 2817 | { |
| 2818 | int voltage, output, rc; |
| 2819 | if (IS_ERR(file) || file == NULL) { |
| 2820 | pr_err("Function Input Error %ld\n", PTR_ERR(file)); |
| 2821 | return -ENOMEM; |
| 2822 | } |
| 2823 | |
| 2824 | voltage = regulator_get_voltage(file->private_data); |
| 2825 | mutex_lock(&debug_buf_mutex); |
| 2826 | |
| 2827 | output = snprintf(debug_buf, MAX_DEBUG_BUF_LEN-1, "%d\n", voltage); |
| 2828 | rc = simple_read_from_buffer((void __user *) buf, output, ppos, |
| 2829 | (void *) debug_buf, output); |
| 2830 | |
| 2831 | mutex_unlock(&debug_buf_mutex); |
| 2832 | |
| 2833 | return rc; |
| 2834 | } |
| 2835 | |
| 2836 | static int reg_debug_volt_open(struct inode *inode, struct file *file) |
| 2837 | { |
| 2838 | if (IS_ERR(file) || file == NULL) { |
| 2839 | pr_err("Function Input Error %ld\n", PTR_ERR(file)); |
| 2840 | return -ENOMEM; |
| 2841 | } |
| 2842 | |
| 2843 | file->private_data = inode->i_private; |
| 2844 | return 0; |
| 2845 | } |
| 2846 | |
| 2847 | static const struct file_operations reg_volt_fops = { |
| 2848 | .write = reg_debug_volt_set, |
| 2849 | .open = reg_debug_volt_open, |
| 2850 | .read = reg_debug_volt_get, |
| 2851 | }; |
| 2852 | |
| 2853 | static int reg_debug_mode_set(void *data, u64 val) |
| 2854 | { |
| 2855 | int err_info; |
| 2856 | if (IS_ERR(data) || data == NULL) { |
| 2857 | pr_err("Function Input Error %ld\n", PTR_ERR(data)); |
| 2858 | return -ENOMEM; |
| 2859 | } |
| 2860 | |
| 2861 | err_info = regulator_set_mode(data, (unsigned int)val); |
| 2862 | |
| 2863 | return err_info; |
| 2864 | } |
| 2865 | |
| 2866 | static int reg_debug_mode_get(void *data, u64 *val) |
| 2867 | { |
| 2868 | int err_info; |
| 2869 | if (IS_ERR(data) || data == NULL) { |
| 2870 | pr_err("Function Input Error %ld\n", PTR_ERR(data)); |
| 2871 | return -ENOMEM; |
| 2872 | } |
| 2873 | |
| 2874 | err_info = regulator_get_mode(data); |
| 2875 | |
| 2876 | if (err_info < 0) { |
| 2877 | pr_err("Regulator_get_mode returned an error!\n"); |
| 2878 | return -ENOMEM; |
| 2879 | } else { |
| 2880 | *val = err_info; |
| 2881 | return 0; |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | DEFINE_SIMPLE_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get, |
| 2886 | reg_debug_mode_set, "%llu\n"); |
| 2887 | |
| 2888 | static int reg_debug_optimum_mode_set(void *data, u64 val) |
| 2889 | { |
| 2890 | int err_info; |
| 2891 | if (IS_ERR(data) || data == NULL) { |
| 2892 | pr_err("Function Input Error %ld\n", PTR_ERR(data)); |
| 2893 | return -ENOMEM; |
| 2894 | } |
| 2895 | |
| 2896 | err_info = regulator_set_optimum_mode(data, (unsigned int)val); |
| 2897 | |
| 2898 | if (err_info < 0) { |
| 2899 | pr_err("Regulator_set_optimum_mode returned an error!\n"); |
| 2900 | return err_info; |
| 2901 | } |
| 2902 | |
| 2903 | return 0; |
| 2904 | } |
| 2905 | |
| 2906 | DEFINE_SIMPLE_ATTRIBUTE(reg_optimum_mode_fops, reg_debug_mode_get, |
| 2907 | reg_debug_optimum_mode_set, "%llu\n"); |
| 2908 | |
| 2909 | static int reg_debug_consumers_show(struct seq_file *m, void *v) |
| 2910 | { |
| 2911 | struct regulator_dev *rdev = m->private; |
| 2912 | struct regulator *reg; |
| 2913 | char *supply_name; |
| 2914 | |
| 2915 | if (!rdev) { |
| 2916 | pr_err("regulator device missing"); |
| 2917 | return -EINVAL; |
| 2918 | } |
| 2919 | |
| 2920 | mutex_lock(&rdev->mutex); |
| 2921 | |
| 2922 | /* Print a header if there are consumers. */ |
| 2923 | if (rdev->open_count) |
| 2924 | seq_printf(m, "Device-Supply " |
| 2925 | "EN Min_uV Max_uV load_uA\n"); |
| 2926 | |
| 2927 | list_for_each_entry(reg, &rdev->consumer_list, list) { |
| 2928 | if (reg->supply_name) |
| 2929 | supply_name = reg->supply_name; |
| 2930 | else |
| 2931 | supply_name = "(null)-(null)"; |
| 2932 | |
| 2933 | seq_printf(m, "%-32s %c %8d %8d %8d\n", supply_name, |
| 2934 | (reg->enabled ? 'Y' : 'N'), reg->min_uV, reg->max_uV, |
| 2935 | reg->uA_load); |
| 2936 | } |
| 2937 | |
| 2938 | mutex_unlock(&rdev->mutex); |
| 2939 | |
| 2940 | return 0; |
| 2941 | } |
| 2942 | |
| 2943 | static int reg_debug_consumers_open(struct inode *inode, struct file *file) |
| 2944 | { |
| 2945 | return single_open(file, reg_debug_consumers_show, inode->i_private); |
| 2946 | } |
| 2947 | |
| 2948 | static const struct file_operations reg_consumers_fops = { |
| 2949 | .owner = THIS_MODULE, |
| 2950 | .open = reg_debug_consumers_open, |
| 2951 | .read = seq_read, |
| 2952 | .llseek = seq_lseek, |
| 2953 | .release = single_release, |
| 2954 | }; |
| 2955 | |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 2956 | static void rdev_init_debugfs(struct regulator_dev *rdev) |
| 2957 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2958 | struct dentry *err_ptr = NULL; |
| 2959 | struct regulator *reg; |
| 2960 | struct regulator_ops *reg_ops; |
| 2961 | mode_t mode; |
| 2962 | |
| 2963 | if (IS_ERR(rdev) || rdev == NULL || |
| 2964 | IS_ERR(debugfs_root) || debugfs_root == NULL) { |
| 2965 | pr_err("Error-Bad Function Input\n"); |
| 2966 | goto error; |
| 2967 | } |
| 2968 | |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 2969 | rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root); |
| 2970 | if (IS_ERR(rdev->debugfs) || !rdev->debugfs) { |
| 2971 | rdev_warn(rdev, "Failed to create debugfs directory\n"); |
| 2972 | rdev->debugfs = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2973 | goto error; |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | debugfs_create_u32("use_count", 0444, rdev->debugfs, |
| 2977 | &rdev->use_count); |
| 2978 | debugfs_create_u32("open_count", 0444, rdev->debugfs, |
| 2979 | &rdev->open_count); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2980 | debugfs_create_file("consumers", 0444, rdev->debugfs, rdev, |
| 2981 | ®_consumers_fops); |
| 2982 | |
| 2983 | reg = regulator_get(NULL, rdev->desc->name); |
| 2984 | if (IS_ERR(reg) || reg == NULL) { |
| 2985 | pr_err("Error-Bad Function Input\n"); |
| 2986 | goto error; |
| 2987 | } |
| 2988 | |
| 2989 | reg_ops = rdev->desc->ops; |
| 2990 | mode = S_IRUGO | S_IWUSR; |
| 2991 | /* Enabled File */ |
| 2992 | if (mode) |
| 2993 | err_ptr = debugfs_create_file("enable", mode, rdev->debugfs, |
| 2994 | reg, ®_enable_fops); |
| 2995 | if (IS_ERR(err_ptr)) { |
| 2996 | pr_err("Error-Could not create enable file\n"); |
| 2997 | debugfs_remove_recursive(rdev->debugfs); |
| 2998 | goto error; |
| 2999 | } |
| 3000 | |
| 3001 | mode = 0; |
| 3002 | /* Force-Disable File */ |
| 3003 | if (reg_ops->is_enabled) |
| 3004 | mode |= S_IRUGO; |
| 3005 | if (reg_ops->enable || reg_ops->disable) |
| 3006 | mode |= S_IWUSR; |
| 3007 | if (mode) |
| 3008 | err_ptr = debugfs_create_file("force_disable", mode, |
| 3009 | rdev->debugfs, reg, ®_fdisable_fops); |
| 3010 | if (IS_ERR(err_ptr)) { |
| 3011 | pr_err("Error-Could not create force_disable file\n"); |
| 3012 | debugfs_remove_recursive(rdev->debugfs); |
| 3013 | goto error; |
| 3014 | } |
| 3015 | |
| 3016 | mode = 0; |
| 3017 | /* Voltage File */ |
| 3018 | if (reg_ops->get_voltage) |
| 3019 | mode |= S_IRUGO; |
| 3020 | if (reg_ops->set_voltage) |
| 3021 | mode |= S_IWUSR; |
| 3022 | if (mode) |
| 3023 | err_ptr = debugfs_create_file("voltage", mode, rdev->debugfs, |
| 3024 | reg, ®_volt_fops); |
| 3025 | if (IS_ERR(err_ptr)) { |
| 3026 | pr_err("Error-Could not create voltage file\n"); |
| 3027 | debugfs_remove_recursive(rdev->debugfs); |
| 3028 | goto error; |
| 3029 | } |
| 3030 | |
| 3031 | mode = 0; |
| 3032 | /* Mode File */ |
| 3033 | if (reg_ops->get_mode) |
| 3034 | mode |= S_IRUGO; |
| 3035 | if (reg_ops->set_mode) |
| 3036 | mode |= S_IWUSR; |
| 3037 | if (mode) |
| 3038 | err_ptr = debugfs_create_file("mode", mode, rdev->debugfs, |
| 3039 | reg, ®_mode_fops); |
| 3040 | if (IS_ERR(err_ptr)) { |
| 3041 | pr_err("Error-Could not create mode file\n"); |
| 3042 | debugfs_remove_recursive(rdev->debugfs); |
| 3043 | goto error; |
| 3044 | } |
| 3045 | |
| 3046 | mode = 0; |
| 3047 | /* Optimum Mode File */ |
| 3048 | if (reg_ops->get_mode) |
| 3049 | mode |= S_IRUGO; |
| 3050 | if (reg_ops->set_mode) |
| 3051 | mode |= S_IWUSR; |
| 3052 | if (mode) |
| 3053 | err_ptr = debugfs_create_file("optimum_mode", mode, |
| 3054 | rdev->debugfs, reg, ®_optimum_mode_fops); |
| 3055 | if (IS_ERR(err_ptr)) { |
| 3056 | pr_err("Error-Could not create optimum_mode file\n"); |
| 3057 | debugfs_remove_recursive(rdev->debugfs); |
| 3058 | goto error; |
| 3059 | } |
| 3060 | |
| 3061 | error: |
| 3062 | return; |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3063 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3064 | #else |
| 3065 | static inline void rdev_init_debugfs(struct regulator_dev *rdev) |
| 3066 | { |
| 3067 | return; |
| 3068 | } |
| 3069 | #endif |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3070 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3071 | /** |
| 3072 | * regulator_register - register regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3073 | * @regulator_desc: regulator to register |
| 3074 | * @dev: struct device for the regulator |
Mark Brown | 0527100 | 2009-01-19 13:37:02 +0000 | [diff] [blame] | 3075 | * @init_data: platform provided init data, passed through by driver |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3076 | * @driver_data: private regulator data |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3077 | * |
| 3078 | * Called by regulator drivers to register a regulator. |
| 3079 | * Returns 0 on success. |
| 3080 | */ |
| 3081 | struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, |
Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 3082 | struct device *dev, const struct regulator_init_data *init_data, |
Mark Brown | 0527100 | 2009-01-19 13:37:02 +0000 | [diff] [blame] | 3083 | void *driver_data) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3084 | { |
| 3085 | static atomic_t regulator_no = ATOMIC_INIT(0); |
| 3086 | struct regulator_dev *rdev; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3087 | int ret, i; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3088 | |
| 3089 | if (regulator_desc == NULL) |
| 3090 | return ERR_PTR(-EINVAL); |
| 3091 | |
| 3092 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) |
| 3093 | return ERR_PTR(-EINVAL); |
| 3094 | |
Diego Liziero | cd78dfc | 2009-04-14 03:04:47 +0200 | [diff] [blame] | 3095 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
| 3096 | regulator_desc->type != REGULATOR_CURRENT) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3097 | return ERR_PTR(-EINVAL); |
| 3098 | |
Mark Brown | 46fabe1e | 2008-09-09 16:21:18 +0100 | [diff] [blame] | 3099 | if (!init_data) |
| 3100 | return ERR_PTR(-EINVAL); |
| 3101 | |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 3102 | /* Only one of each should be implemented */ |
| 3103 | WARN_ON(regulator_desc->ops->get_voltage && |
| 3104 | regulator_desc->ops->get_voltage_sel); |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 3105 | WARN_ON(regulator_desc->ops->set_voltage && |
| 3106 | regulator_desc->ops->set_voltage_sel); |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 3107 | |
| 3108 | /* If we're using selectors we must implement list_voltage. */ |
| 3109 | if (regulator_desc->ops->get_voltage_sel && |
| 3110 | !regulator_desc->ops->list_voltage) { |
| 3111 | return ERR_PTR(-EINVAL); |
| 3112 | } |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 3113 | if (regulator_desc->ops->set_voltage_sel && |
| 3114 | !regulator_desc->ops->list_voltage) { |
| 3115 | return ERR_PTR(-EINVAL); |
| 3116 | } |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 3117 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3118 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
| 3119 | if (rdev == NULL) |
| 3120 | return ERR_PTR(-ENOMEM); |
| 3121 | |
| 3122 | mutex_lock(®ulator_list_mutex); |
| 3123 | |
| 3124 | mutex_init(&rdev->mutex); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3125 | rdev->reg_data = driver_data; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3126 | rdev->owner = regulator_desc->owner; |
| 3127 | rdev->desc = regulator_desc; |
| 3128 | INIT_LIST_HEAD(&rdev->consumer_list); |
| 3129 | INIT_LIST_HEAD(&rdev->supply_list); |
| 3130 | INIT_LIST_HEAD(&rdev->list); |
| 3131 | INIT_LIST_HEAD(&rdev->slist); |
| 3132 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); |
| 3133 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3134 | /* preform any regulator specific init */ |
| 3135 | if (init_data->regulator_init) { |
| 3136 | ret = init_data->regulator_init(rdev->reg_data); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3137 | if (ret < 0) |
| 3138 | goto clean; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3139 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3140 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3141 | /* register with sysfs */ |
| 3142 | rdev->dev.class = ®ulator_class; |
| 3143 | rdev->dev.parent = dev; |
Kay Sievers | 812460a | 2008-11-02 03:55:10 +0100 | [diff] [blame] | 3144 | dev_set_name(&rdev->dev, "regulator.%d", |
| 3145 | atomic_inc_return(®ulator_no) - 1); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3146 | ret = device_register(&rdev->dev); |
Vasiliy Kulikov | ad7725c | 2010-09-19 16:55:01 +0400 | [diff] [blame] | 3147 | if (ret != 0) { |
| 3148 | put_device(&rdev->dev); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3149 | goto clean; |
Vasiliy Kulikov | ad7725c | 2010-09-19 16:55:01 +0400 | [diff] [blame] | 3150 | } |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3151 | |
| 3152 | dev_set_drvdata(&rdev->dev, rdev); |
| 3153 | |
Mike Rapoport | 74f544c | 2008-11-25 14:53:53 +0200 | [diff] [blame] | 3154 | /* set regulator constraints */ |
| 3155 | ret = set_machine_constraints(rdev, &init_data->constraints); |
| 3156 | if (ret < 0) |
| 3157 | goto scrub; |
| 3158 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3159 | /* add attributes supported by this regulator */ |
| 3160 | ret = add_regulator_attributes(rdev); |
| 3161 | if (ret < 0) |
| 3162 | goto scrub; |
| 3163 | |
Mark Brown | 0178f3e | 2010-04-26 15:18:14 +0100 | [diff] [blame] | 3164 | if (init_data->supply_regulator) { |
| 3165 | struct regulator_dev *r; |
| 3166 | int found = 0; |
| 3167 | |
| 3168 | list_for_each_entry(r, ®ulator_list, list) { |
| 3169 | if (strcmp(rdev_get_name(r), |
| 3170 | init_data->supply_regulator) == 0) { |
| 3171 | found = 1; |
| 3172 | break; |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | if (!found) { |
| 3177 | dev_err(dev, "Failed to find supply %s\n", |
| 3178 | init_data->supply_regulator); |
Axel Lin | 7727da2 | 2010-11-05 15:27:17 +0800 | [diff] [blame] | 3179 | ret = -ENODEV; |
Mark Brown | 0178f3e | 2010-04-26 15:18:14 +0100 | [diff] [blame] | 3180 | goto scrub; |
| 3181 | } |
| 3182 | |
| 3183 | ret = set_supply(rdev, r); |
| 3184 | if (ret < 0) |
| 3185 | goto scrub; |
| 3186 | } |
| 3187 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3188 | /* add consumers devices */ |
| 3189 | for (i = 0; i < init_data->num_consumer_supplies; i++) { |
| 3190 | ret = set_consumer_device_supply(rdev, |
| 3191 | init_data->consumer_supplies[i].dev, |
Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 3192 | init_data->consumer_supplies[i].dev_name, |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3193 | init_data->consumer_supplies[i].supply); |
Mark Brown | 23c2f04 | 2011-02-24 17:39:09 +0000 | [diff] [blame] | 3194 | if (ret < 0) { |
| 3195 | dev_err(dev, "Failed to set supply %s\n", |
| 3196 | init_data->consumer_supplies[i].supply); |
Jani Nikula | d4033b5 | 2010-04-29 10:55:11 +0300 | [diff] [blame] | 3197 | goto unset_supplies; |
Mark Brown | 23c2f04 | 2011-02-24 17:39:09 +0000 | [diff] [blame] | 3198 | } |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | list_add(&rdev->list, ®ulator_list); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3202 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3203 | out: |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3204 | mutex_unlock(®ulator_list_mutex); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3205 | rdev_init_debugfs(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3206 | return rdev; |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3207 | |
Jani Nikula | d4033b5 | 2010-04-29 10:55:11 +0300 | [diff] [blame] | 3208 | unset_supplies: |
| 3209 | unset_regulator_supplies(rdev); |
| 3210 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3211 | scrub: |
| 3212 | device_unregister(&rdev->dev); |
Paul Walmsley | 53032da | 2009-04-25 05:28:36 -0600 | [diff] [blame] | 3213 | /* device core frees rdev */ |
| 3214 | rdev = ERR_PTR(ret); |
| 3215 | goto out; |
| 3216 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3217 | clean: |
| 3218 | kfree(rdev); |
| 3219 | rdev = ERR_PTR(ret); |
| 3220 | goto out; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3221 | } |
| 3222 | EXPORT_SYMBOL_GPL(regulator_register); |
| 3223 | |
| 3224 | /** |
| 3225 | * regulator_unregister - unregister regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3226 | * @rdev: regulator to unregister |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3227 | * |
| 3228 | * Called by regulator drivers to unregister a regulator. |
| 3229 | */ |
| 3230 | void regulator_unregister(struct regulator_dev *rdev) |
| 3231 | { |
| 3232 | if (rdev == NULL) |
| 3233 | return; |
| 3234 | |
| 3235 | mutex_lock(®ulator_list_mutex); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3236 | #ifdef CONFIG_DEBUG_FS |
| 3237 | debugfs_remove_recursive(rdev->debugfs); |
| 3238 | #endif |
Mark Brown | 6bf87d1 | 2009-07-21 16:00:25 +0100 | [diff] [blame] | 3239 | WARN_ON(rdev->open_count); |
Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 3240 | unset_regulator_supplies(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3241 | list_del(&rdev->list); |
| 3242 | if (rdev->supply) |
| 3243 | sysfs_remove_link(&rdev->dev.kobj, "supply"); |
| 3244 | device_unregister(&rdev->dev); |
Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 3245 | kfree(rdev->constraints); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3246 | mutex_unlock(®ulator_list_mutex); |
| 3247 | } |
| 3248 | EXPORT_SYMBOL_GPL(regulator_unregister); |
| 3249 | |
| 3250 | /** |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 3251 | * regulator_suspend_prepare - prepare regulators for system wide suspend |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3252 | * @state: system suspend state |
| 3253 | * |
| 3254 | * Configure each regulator with it's suspend operating parameters for state. |
| 3255 | * This will usually be called by machine suspend code prior to supending. |
| 3256 | */ |
| 3257 | int regulator_suspend_prepare(suspend_state_t state) |
| 3258 | { |
| 3259 | struct regulator_dev *rdev; |
| 3260 | int ret = 0; |
| 3261 | |
| 3262 | /* ON is handled by regulator active state */ |
| 3263 | if (state == PM_SUSPEND_ON) |
| 3264 | return -EINVAL; |
| 3265 | |
| 3266 | mutex_lock(®ulator_list_mutex); |
| 3267 | list_for_each_entry(rdev, ®ulator_list, list) { |
| 3268 | |
| 3269 | mutex_lock(&rdev->mutex); |
| 3270 | ret = suspend_prepare(rdev, state); |
| 3271 | mutex_unlock(&rdev->mutex); |
| 3272 | |
| 3273 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 3274 | rdev_err(rdev, "failed to prepare\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3275 | goto out; |
| 3276 | } |
| 3277 | } |
| 3278 | out: |
| 3279 | mutex_unlock(®ulator_list_mutex); |
| 3280 | return ret; |
| 3281 | } |
| 3282 | EXPORT_SYMBOL_GPL(regulator_suspend_prepare); |
| 3283 | |
| 3284 | /** |
MyungJoo Ham | 7a32b58 | 2011-03-11 10:13:59 +0900 | [diff] [blame] | 3285 | * regulator_suspend_finish - resume regulators from system wide suspend |
| 3286 | * |
| 3287 | * Turn on regulators that might be turned off by regulator_suspend_prepare |
| 3288 | * and that should be turned on according to the regulators properties. |
| 3289 | */ |
| 3290 | int regulator_suspend_finish(void) |
| 3291 | { |
| 3292 | struct regulator_dev *rdev; |
| 3293 | int ret = 0, error; |
| 3294 | |
| 3295 | mutex_lock(®ulator_list_mutex); |
| 3296 | list_for_each_entry(rdev, ®ulator_list, list) { |
| 3297 | struct regulator_ops *ops = rdev->desc->ops; |
| 3298 | |
| 3299 | mutex_lock(&rdev->mutex); |
| 3300 | if ((rdev->use_count > 0 || rdev->constraints->always_on) && |
| 3301 | ops->enable) { |
| 3302 | error = ops->enable(rdev); |
| 3303 | if (error) |
| 3304 | ret = error; |
| 3305 | } else { |
| 3306 | if (!has_full_constraints) |
| 3307 | goto unlock; |
| 3308 | if (!ops->disable) |
| 3309 | goto unlock; |
| 3310 | if (ops->is_enabled && !ops->is_enabled(rdev)) |
| 3311 | goto unlock; |
| 3312 | |
| 3313 | error = ops->disable(rdev); |
| 3314 | if (error) |
| 3315 | ret = error; |
| 3316 | } |
| 3317 | unlock: |
| 3318 | mutex_unlock(&rdev->mutex); |
| 3319 | } |
| 3320 | mutex_unlock(®ulator_list_mutex); |
| 3321 | return ret; |
| 3322 | } |
| 3323 | EXPORT_SYMBOL_GPL(regulator_suspend_finish); |
| 3324 | |
| 3325 | /** |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3326 | * regulator_has_full_constraints - the system has fully specified constraints |
| 3327 | * |
| 3328 | * Calling this function will cause the regulator API to disable all |
| 3329 | * regulators which have a zero use count and don't have an always_on |
| 3330 | * constraint in a late_initcall. |
| 3331 | * |
| 3332 | * The intention is that this will become the default behaviour in a |
| 3333 | * future kernel release so users are encouraged to use this facility |
| 3334 | * now. |
| 3335 | */ |
| 3336 | void regulator_has_full_constraints(void) |
| 3337 | { |
| 3338 | has_full_constraints = 1; |
| 3339 | } |
| 3340 | EXPORT_SYMBOL_GPL(regulator_has_full_constraints); |
| 3341 | |
| 3342 | /** |
Mark Brown | 688fe99 | 2010-10-05 19:18:32 -0700 | [diff] [blame] | 3343 | * regulator_use_dummy_regulator - Provide a dummy regulator when none is found |
| 3344 | * |
| 3345 | * Calling this function will cause the regulator API to provide a |
| 3346 | * dummy regulator to consumers if no physical regulator is found, |
| 3347 | * allowing most consumers to proceed as though a regulator were |
| 3348 | * configured. This allows systems such as those with software |
| 3349 | * controllable regulators for the CPU core only to be brought up more |
| 3350 | * readily. |
| 3351 | */ |
| 3352 | void regulator_use_dummy_regulator(void) |
| 3353 | { |
| 3354 | board_wants_dummy_regulator = true; |
| 3355 | } |
| 3356 | EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator); |
| 3357 | |
| 3358 | /** |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3359 | * regulator_suppress_info_printing - disable printing of info messages |
| 3360 | * |
| 3361 | * The regulator framework calls print_constraints() when a regulator is |
| 3362 | * registered. It also prints a disable message for each unused regulator in |
| 3363 | * regulator_init_complete(). |
| 3364 | * |
| 3365 | * Calling this function ensures that such messages do not end up in the |
| 3366 | * log. |
| 3367 | */ |
| 3368 | void regulator_suppress_info_printing(void) |
| 3369 | { |
| 3370 | suppress_info_printing = 1; |
| 3371 | } |
| 3372 | EXPORT_SYMBOL_GPL(regulator_suppress_info_printing); |
| 3373 | |
| 3374 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3375 | * rdev_get_drvdata - get rdev regulator driver data |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3376 | * @rdev: regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3377 | * |
| 3378 | * Get rdev regulator driver private data. This call can be used in the |
| 3379 | * regulator driver context. |
| 3380 | */ |
| 3381 | void *rdev_get_drvdata(struct regulator_dev *rdev) |
| 3382 | { |
| 3383 | return rdev->reg_data; |
| 3384 | } |
| 3385 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); |
| 3386 | |
| 3387 | /** |
| 3388 | * regulator_get_drvdata - get regulator driver data |
| 3389 | * @regulator: regulator |
| 3390 | * |
| 3391 | * Get regulator driver private data. This call can be used in the consumer |
| 3392 | * driver context when non API regulator specific functions need to be called. |
| 3393 | */ |
| 3394 | void *regulator_get_drvdata(struct regulator *regulator) |
| 3395 | { |
| 3396 | return regulator->rdev->reg_data; |
| 3397 | } |
| 3398 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); |
| 3399 | |
| 3400 | /** |
| 3401 | * regulator_set_drvdata - set regulator driver data |
| 3402 | * @regulator: regulator |
| 3403 | * @data: data |
| 3404 | */ |
| 3405 | void regulator_set_drvdata(struct regulator *regulator, void *data) |
| 3406 | { |
| 3407 | regulator->rdev->reg_data = data; |
| 3408 | } |
| 3409 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); |
| 3410 | |
| 3411 | /** |
| 3412 | * regulator_get_id - get regulator ID |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3413 | * @rdev: regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3414 | */ |
| 3415 | int rdev_get_id(struct regulator_dev *rdev) |
| 3416 | { |
| 3417 | return rdev->desc->id; |
| 3418 | } |
| 3419 | EXPORT_SYMBOL_GPL(rdev_get_id); |
| 3420 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3421 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
| 3422 | { |
| 3423 | return &rdev->dev; |
| 3424 | } |
| 3425 | EXPORT_SYMBOL_GPL(rdev_get_dev); |
| 3426 | |
| 3427 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) |
| 3428 | { |
| 3429 | return reg_init_data->driver_data; |
| 3430 | } |
| 3431 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); |
| 3432 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3433 | static int __init regulator_init(void) |
| 3434 | { |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 3435 | int ret; |
| 3436 | |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 3437 | ret = class_register(®ulator_class); |
| 3438 | |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3439 | #ifdef CONFIG_DEBUG_FS |
| 3440 | debugfs_root = debugfs_create_dir("regulator", NULL); |
| 3441 | if (IS_ERR(debugfs_root) || !debugfs_root) { |
| 3442 | pr_warn("regulator: Failed to create debugfs directory\n"); |
| 3443 | debugfs_root = NULL; |
| 3444 | } |
| 3445 | #endif |
| 3446 | |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 3447 | regulator_dummy_init(); |
| 3448 | |
| 3449 | return ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3450 | } |
| 3451 | |
| 3452 | /* init early to allow our consumers to complete system booting */ |
| 3453 | core_initcall(regulator_init); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3454 | |
| 3455 | static int __init regulator_init_complete(void) |
| 3456 | { |
| 3457 | struct regulator_dev *rdev; |
| 3458 | struct regulator_ops *ops; |
| 3459 | struct regulation_constraints *c; |
| 3460 | int enabled, ret; |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3461 | |
| 3462 | mutex_lock(®ulator_list_mutex); |
| 3463 | |
| 3464 | /* If we have a full configuration then disable any regulators |
| 3465 | * which are not in use or always_on. This will become the |
| 3466 | * default behaviour in the future. |
| 3467 | */ |
| 3468 | list_for_each_entry(rdev, ®ulator_list, list) { |
| 3469 | ops = rdev->desc->ops; |
| 3470 | c = rdev->constraints; |
| 3471 | |
Mark Brown | f25e0b4 | 2009-08-03 18:49:55 +0100 | [diff] [blame] | 3472 | if (!ops->disable || (c && c->always_on)) |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3473 | continue; |
| 3474 | |
| 3475 | mutex_lock(&rdev->mutex); |
| 3476 | |
| 3477 | if (rdev->use_count) |
| 3478 | goto unlock; |
| 3479 | |
| 3480 | /* If we can't read the status assume it's on. */ |
| 3481 | if (ops->is_enabled) |
| 3482 | enabled = ops->is_enabled(rdev); |
| 3483 | else |
| 3484 | enabled = 1; |
| 3485 | |
| 3486 | if (!enabled) |
| 3487 | goto unlock; |
| 3488 | |
| 3489 | if (has_full_constraints) { |
| 3490 | /* We log since this may kill the system if it |
| 3491 | * goes wrong. */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3492 | if (!suppress_info_printing) |
| 3493 | rdev_info(rdev, "disabling\n"); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3494 | ret = ops->disable(rdev); |
| 3495 | if (ret != 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 3496 | rdev_err(rdev, "couldn't disable: %d\n", ret); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3497 | } |
| 3498 | } else { |
| 3499 | /* The intention is that in future we will |
| 3500 | * assume that full constraints are provided |
| 3501 | * so warn even if we aren't going to do |
| 3502 | * anything here. |
| 3503 | */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3504 | if (!suppress_info_printing) |
| 3505 | rdev_warn(rdev, "incomplete constraints, " |
| 3506 | "leaving on\n"); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | unlock: |
| 3510 | mutex_unlock(&rdev->mutex); |
| 3511 | } |
| 3512 | |
| 3513 | mutex_unlock(®ulator_list_mutex); |
| 3514 | |
| 3515 | return 0; |
| 3516 | } |
| 3517 | late_initcall(regulator_init_complete); |