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