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