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