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