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