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); |
| 31 | |
Mark Brown | 8dc5390 | 2008-12-31 12:52:40 +0000 | [diff] [blame] | 32 | /* |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 33 | * struct regulator_map |
| 34 | * |
| 35 | * Used to provide symbolic supply names to devices. |
| 36 | */ |
| 37 | struct regulator_map { |
| 38 | struct list_head list; |
| 39 | struct device *dev; |
| 40 | const char *supply; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 41 | struct regulator_dev *regulator; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 42 | }; |
| 43 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 44 | /* |
| 45 | * struct regulator |
| 46 | * |
| 47 | * One for each consumer device. |
| 48 | */ |
| 49 | struct regulator { |
| 50 | struct device *dev; |
| 51 | struct list_head list; |
| 52 | int uA_load; |
| 53 | int min_uV; |
| 54 | int max_uV; |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 55 | int enabled; /* count of client enables */ |
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 || |
| 543 | !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode); |
| 544 | return; |
| 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 | |
| 695 | rdev->constraints = constraints; |
| 696 | |
| 697 | /* do we need to apply the constraint voltage */ |
| 698 | if (rdev->constraints->apply_uV && |
| 699 | rdev->constraints->min_uV == rdev->constraints->max_uV && |
Mark Brown | e5fda26 | 2008-09-09 16:21:20 +0100 | [diff] [blame] | 700 | ops->set_voltage) { |
| 701 | ret = ops->set_voltage(rdev, |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 702 | rdev->constraints->min_uV, rdev->constraints->max_uV); |
| 703 | if (ret < 0) { |
Mark Brown | e06f5b4 | 2008-09-09 16:21:19 +0100 | [diff] [blame] | 704 | printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n", |
| 705 | __func__, |
| 706 | rdev->constraints->min_uV, name); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 707 | rdev->constraints = NULL; |
| 708 | goto out; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | /* are we enabled at boot time by firmware / bootloader */ |
| 713 | if (rdev->constraints->boot_on) |
| 714 | rdev->use_count = 1; |
| 715 | |
| 716 | /* do we need to setup our suspend state */ |
Mark Brown | e06f5b4 | 2008-09-09 16:21:19 +0100 | [diff] [blame] | 717 | if (constraints->initial_state) { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 718 | ret = suspend_prepare(rdev, constraints->initial_state); |
Mark Brown | e06f5b4 | 2008-09-09 16:21:19 +0100 | [diff] [blame] | 719 | if (ret < 0) { |
| 720 | printk(KERN_ERR "%s: failed to set suspend state for %s\n", |
| 721 | __func__, name); |
| 722 | rdev->constraints = NULL; |
| 723 | goto out; |
| 724 | } |
| 725 | } |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 726 | |
Mark Brown | e5fda26 | 2008-09-09 16:21:20 +0100 | [diff] [blame] | 727 | /* if always_on is set then turn the regulator on if it's not |
| 728 | * already on. */ |
| 729 | if (constraints->always_on && ops->enable && |
| 730 | ((ops->is_enabled && !ops->is_enabled(rdev)) || |
| 731 | (!ops->is_enabled && !constraints->boot_on))) { |
| 732 | ret = ops->enable(rdev); |
| 733 | if (ret < 0) { |
| 734 | printk(KERN_ERR "%s: failed to enable %s\n", |
| 735 | __func__, name); |
| 736 | rdev->constraints = NULL; |
| 737 | goto out; |
| 738 | } |
| 739 | } |
| 740 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 741 | print_constraints(rdev); |
| 742 | out: |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | /** |
| 747 | * set_supply - set regulator supply regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 748 | * @rdev: regulator name |
| 749 | * @supply_rdev: supply regulator name |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 750 | * |
| 751 | * Called by platform initialisation code to set the supply regulator for this |
| 752 | * regulator. This ensures that a regulators supply will also be enabled by the |
| 753 | * core if it's child is enabled. |
| 754 | */ |
| 755 | static int set_supply(struct regulator_dev *rdev, |
| 756 | struct regulator_dev *supply_rdev) |
| 757 | { |
| 758 | int err; |
| 759 | |
| 760 | err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj, |
| 761 | "supply"); |
| 762 | if (err) { |
| 763 | printk(KERN_ERR |
| 764 | "%s: could not add device link %s err %d\n", |
| 765 | __func__, supply_rdev->dev.kobj.name, err); |
| 766 | goto out; |
| 767 | } |
| 768 | rdev->supply = supply_rdev; |
| 769 | list_add(&rdev->slist, &supply_rdev->supply_list); |
| 770 | out: |
| 771 | return err; |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * set_consumer_device_supply: Bind a regulator to a symbolic supply |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 776 | * @rdev: regulator source |
| 777 | * @consumer_dev: device the supply applies to |
| 778 | * @supply: symbolic name for supply |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 779 | * |
| 780 | * Allows platform initialisation code to map physical regulator |
| 781 | * sources to symbolic names for supplies for use by devices. Devices |
| 782 | * should use these symbolic names to request regulators, avoiding the |
| 783 | * need to provide board-specific regulator names as platform data. |
| 784 | */ |
| 785 | static int set_consumer_device_supply(struct regulator_dev *rdev, |
| 786 | struct device *consumer_dev, const char *supply) |
| 787 | { |
| 788 | struct regulator_map *node; |
| 789 | |
| 790 | if (supply == NULL) |
| 791 | return -EINVAL; |
| 792 | |
David Brownell | 6001e13 | 2008-12-31 12:54:19 +0000 | [diff] [blame] | 793 | list_for_each_entry(node, ®ulator_map_list, list) { |
| 794 | if (consumer_dev != node->dev) |
| 795 | continue; |
| 796 | if (strcmp(node->supply, supply) != 0) |
| 797 | continue; |
| 798 | |
| 799 | dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n", |
| 800 | dev_name(&node->regulator->dev), |
| 801 | node->regulator->desc->name, |
| 802 | supply, |
| 803 | dev_name(&rdev->dev), rdev->desc->name); |
| 804 | return -EBUSY; |
| 805 | } |
| 806 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 807 | node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL); |
| 808 | if (node == NULL) |
| 809 | return -ENOMEM; |
| 810 | |
| 811 | node->regulator = rdev; |
| 812 | node->dev = consumer_dev; |
| 813 | node->supply = supply; |
| 814 | |
| 815 | list_add(&node->list, ®ulator_map_list); |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | static void unset_consumer_device_supply(struct regulator_dev *rdev, |
| 820 | struct device *consumer_dev) |
| 821 | { |
| 822 | struct regulator_map *node, *n; |
| 823 | |
| 824 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { |
| 825 | if (rdev == node->regulator && |
| 826 | consumer_dev == node->dev) { |
| 827 | list_del(&node->list); |
| 828 | kfree(node); |
| 829 | return; |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | |
Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 834 | static void unset_regulator_supplies(struct regulator_dev *rdev) |
| 835 | { |
| 836 | struct regulator_map *node, *n; |
| 837 | |
| 838 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { |
| 839 | if (rdev == node->regulator) { |
| 840 | list_del(&node->list); |
| 841 | kfree(node); |
| 842 | return; |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 847 | #define REG_STR_SIZE 32 |
| 848 | |
| 849 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
| 850 | struct device *dev, |
| 851 | const char *supply_name) |
| 852 | { |
| 853 | struct regulator *regulator; |
| 854 | char buf[REG_STR_SIZE]; |
| 855 | int err, size; |
| 856 | |
| 857 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); |
| 858 | if (regulator == NULL) |
| 859 | return NULL; |
| 860 | |
| 861 | mutex_lock(&rdev->mutex); |
| 862 | regulator->rdev = rdev; |
| 863 | list_add(®ulator->list, &rdev->consumer_list); |
| 864 | |
| 865 | if (dev) { |
| 866 | /* create a 'requested_microamps_name' sysfs entry */ |
| 867 | size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s", |
| 868 | supply_name); |
| 869 | if (size >= REG_STR_SIZE) |
| 870 | goto overflow_err; |
| 871 | |
| 872 | regulator->dev = dev; |
| 873 | regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL); |
| 874 | if (regulator->dev_attr.attr.name == NULL) |
| 875 | goto attr_name_err; |
| 876 | |
| 877 | regulator->dev_attr.attr.owner = THIS_MODULE; |
| 878 | regulator->dev_attr.attr.mode = 0444; |
| 879 | regulator->dev_attr.show = device_requested_uA_show; |
| 880 | err = device_create_file(dev, ®ulator->dev_attr); |
| 881 | if (err < 0) { |
| 882 | printk(KERN_WARNING "%s: could not add regulator_dev" |
| 883 | " load sysfs\n", __func__); |
| 884 | goto attr_name_err; |
| 885 | } |
| 886 | |
| 887 | /* also add a link to the device sysfs entry */ |
| 888 | size = scnprintf(buf, REG_STR_SIZE, "%s-%s", |
| 889 | dev->kobj.name, supply_name); |
| 890 | if (size >= REG_STR_SIZE) |
| 891 | goto attr_err; |
| 892 | |
| 893 | regulator->supply_name = kstrdup(buf, GFP_KERNEL); |
| 894 | if (regulator->supply_name == NULL) |
| 895 | goto attr_err; |
| 896 | |
| 897 | err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj, |
| 898 | buf); |
| 899 | if (err) { |
| 900 | printk(KERN_WARNING |
| 901 | "%s: could not add device link %s err %d\n", |
| 902 | __func__, dev->kobj.name, err); |
| 903 | device_remove_file(dev, ®ulator->dev_attr); |
| 904 | goto link_name_err; |
| 905 | } |
| 906 | } |
| 907 | mutex_unlock(&rdev->mutex); |
| 908 | return regulator; |
| 909 | link_name_err: |
| 910 | kfree(regulator->supply_name); |
| 911 | attr_err: |
| 912 | device_remove_file(regulator->dev, ®ulator->dev_attr); |
| 913 | attr_name_err: |
| 914 | kfree(regulator->dev_attr.attr.name); |
| 915 | overflow_err: |
| 916 | list_del(®ulator->list); |
| 917 | kfree(regulator); |
| 918 | mutex_unlock(&rdev->mutex); |
| 919 | return NULL; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * regulator_get - lookup and obtain a reference to a regulator. |
| 924 | * @dev: device for regulator "consumer" |
| 925 | * @id: Supply name or regulator ID. |
| 926 | * |
| 927 | * Returns a struct regulator corresponding to the regulator producer, |
Mark Brown | fe203dd | 2009-02-11 15:32:06 +0000 | [diff] [blame^] | 928 | * or IS_ERR() condition containing errno. |
| 929 | * |
| 930 | * Use of supply names configured via regulator_set_device_supply() is |
| 931 | * strongly encouraged. It is recommended that the supply name used |
| 932 | * should match the name used for the supply and/or the relevant |
| 933 | * device pins in the datasheet. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 934 | */ |
| 935 | struct regulator *regulator_get(struct device *dev, const char *id) |
| 936 | { |
| 937 | struct regulator_dev *rdev; |
| 938 | struct regulator_map *map; |
| 939 | struct regulator *regulator = ERR_PTR(-ENODEV); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 940 | |
| 941 | if (id == NULL) { |
| 942 | printk(KERN_ERR "regulator: get() with no identifier\n"); |
| 943 | return regulator; |
| 944 | } |
| 945 | |
| 946 | mutex_lock(®ulator_list_mutex); |
| 947 | |
| 948 | list_for_each_entry(map, ®ulator_map_list, list) { |
| 949 | if (dev == map->dev && |
| 950 | strcmp(map->supply, id) == 0) { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 951 | rdev = map->regulator; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 952 | goto found; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 953 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 954 | } |
| 955 | printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n", |
| 956 | id); |
| 957 | mutex_unlock(®ulator_list_mutex); |
| 958 | return regulator; |
| 959 | |
| 960 | found: |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 961 | if (!try_module_get(rdev->owner)) |
| 962 | goto out; |
| 963 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 964 | regulator = create_regulator(rdev, dev, id); |
| 965 | if (regulator == NULL) { |
| 966 | regulator = ERR_PTR(-ENOMEM); |
| 967 | module_put(rdev->owner); |
| 968 | } |
| 969 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 970 | out: |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 971 | mutex_unlock(®ulator_list_mutex); |
| 972 | return regulator; |
| 973 | } |
| 974 | EXPORT_SYMBOL_GPL(regulator_get); |
| 975 | |
| 976 | /** |
| 977 | * regulator_put - "free" the regulator source |
| 978 | * @regulator: regulator source |
| 979 | * |
| 980 | * Note: drivers must ensure that all regulator_enable calls made on this |
| 981 | * regulator source are balanced by regulator_disable calls prior to calling |
| 982 | * this function. |
| 983 | */ |
| 984 | void regulator_put(struct regulator *regulator) |
| 985 | { |
| 986 | struct regulator_dev *rdev; |
| 987 | |
| 988 | if (regulator == NULL || IS_ERR(regulator)) |
| 989 | return; |
| 990 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 991 | mutex_lock(®ulator_list_mutex); |
| 992 | rdev = regulator->rdev; |
| 993 | |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 994 | if (WARN(regulator->enabled, "Releasing supply %s while enabled\n", |
| 995 | regulator->supply_name)) |
| 996 | _regulator_disable(rdev); |
| 997 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 998 | /* remove any sysfs entries */ |
| 999 | if (regulator->dev) { |
| 1000 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); |
| 1001 | kfree(regulator->supply_name); |
| 1002 | device_remove_file(regulator->dev, ®ulator->dev_attr); |
| 1003 | kfree(regulator->dev_attr.attr.name); |
| 1004 | } |
| 1005 | list_del(®ulator->list); |
| 1006 | kfree(regulator); |
| 1007 | |
| 1008 | module_put(rdev->owner); |
| 1009 | mutex_unlock(®ulator_list_mutex); |
| 1010 | } |
| 1011 | EXPORT_SYMBOL_GPL(regulator_put); |
| 1012 | |
| 1013 | /* locks held by regulator_enable() */ |
| 1014 | static int _regulator_enable(struct regulator_dev *rdev) |
| 1015 | { |
| 1016 | int ret = -EINVAL; |
| 1017 | |
| 1018 | if (!rdev->constraints) { |
| 1019 | printk(KERN_ERR "%s: %s has no constraints\n", |
| 1020 | __func__, rdev->desc->name); |
| 1021 | return ret; |
| 1022 | } |
| 1023 | |
| 1024 | /* do we need to enable the supply regulator first */ |
| 1025 | if (rdev->supply) { |
| 1026 | ret = _regulator_enable(rdev->supply); |
| 1027 | if (ret < 0) { |
| 1028 | printk(KERN_ERR "%s: failed to enable %s: %d\n", |
| 1029 | __func__, rdev->desc->name, ret); |
| 1030 | return ret; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /* check voltage and requested load before enabling */ |
| 1035 | if (rdev->desc->ops->enable) { |
| 1036 | |
| 1037 | if (rdev->constraints && |
| 1038 | (rdev->constraints->valid_ops_mask & |
| 1039 | REGULATOR_CHANGE_DRMS)) |
| 1040 | drms_uA_update(rdev); |
| 1041 | |
| 1042 | ret = rdev->desc->ops->enable(rdev); |
| 1043 | if (ret < 0) { |
| 1044 | printk(KERN_ERR "%s: failed to enable %s: %d\n", |
| 1045 | __func__, rdev->desc->name, ret); |
| 1046 | return ret; |
| 1047 | } |
| 1048 | rdev->use_count++; |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | return ret; |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * regulator_enable - enable regulator output |
| 1057 | * @regulator: regulator source |
| 1058 | * |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1059 | * Request that the regulator be enabled with the regulator output at |
| 1060 | * the predefined voltage or current value. Calls to regulator_enable() |
| 1061 | * must be balanced with calls to regulator_disable(). |
| 1062 | * |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1063 | * 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] | 1064 | * hardwired in the regulator. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1065 | */ |
| 1066 | int regulator_enable(struct regulator *regulator) |
| 1067 | { |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1068 | struct regulator_dev *rdev = regulator->rdev; |
| 1069 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1070 | |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1071 | mutex_lock(&rdev->mutex); |
| 1072 | if (regulator->enabled == 0) |
| 1073 | ret = _regulator_enable(rdev); |
| 1074 | else if (regulator->enabled < 0) |
| 1075 | ret = -EIO; |
| 1076 | if (ret == 0) |
| 1077 | regulator->enabled++; |
| 1078 | mutex_unlock(&rdev->mutex); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1079 | return ret; |
| 1080 | } |
| 1081 | EXPORT_SYMBOL_GPL(regulator_enable); |
| 1082 | |
| 1083 | /* locks held by regulator_disable() */ |
| 1084 | static int _regulator_disable(struct regulator_dev *rdev) |
| 1085 | { |
| 1086 | int ret = 0; |
| 1087 | |
| 1088 | /* are we the last user and permitted to disable ? */ |
| 1089 | if (rdev->use_count == 1 && !rdev->constraints->always_on) { |
| 1090 | |
| 1091 | /* we are last user */ |
| 1092 | if (rdev->desc->ops->disable) { |
| 1093 | ret = rdev->desc->ops->disable(rdev); |
| 1094 | if (ret < 0) { |
| 1095 | printk(KERN_ERR "%s: failed to disable %s\n", |
| 1096 | __func__, rdev->desc->name); |
| 1097 | return ret; |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | /* decrease our supplies ref count and disable if required */ |
| 1102 | if (rdev->supply) |
| 1103 | _regulator_disable(rdev->supply); |
| 1104 | |
| 1105 | rdev->use_count = 0; |
| 1106 | } else if (rdev->use_count > 1) { |
| 1107 | |
| 1108 | if (rdev->constraints && |
| 1109 | (rdev->constraints->valid_ops_mask & |
| 1110 | REGULATOR_CHANGE_DRMS)) |
| 1111 | drms_uA_update(rdev); |
| 1112 | |
| 1113 | rdev->use_count--; |
| 1114 | } |
| 1115 | return ret; |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * regulator_disable - disable regulator output |
| 1120 | * @regulator: regulator source |
| 1121 | * |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1122 | * Disable the regulator output voltage or current. Calls to |
| 1123 | * regulator_enable() must be balanced with calls to |
| 1124 | * regulator_disable(). |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1125 | * |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1126 | * NOTE: this will only disable the regulator output if no other consumer |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1127 | * devices have it enabled, the regulator device supports disabling and |
| 1128 | * machine constraints permit this operation. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1129 | */ |
| 1130 | int regulator_disable(struct regulator *regulator) |
| 1131 | { |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1132 | struct regulator_dev *rdev = regulator->rdev; |
| 1133 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1134 | |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1135 | mutex_lock(&rdev->mutex); |
| 1136 | if (regulator->enabled == 1) { |
| 1137 | ret = _regulator_disable(rdev); |
| 1138 | if (ret == 0) |
| 1139 | regulator->uA_load = 0; |
| 1140 | } else if (WARN(regulator->enabled <= 0, |
| 1141 | "unbalanced disables for supply %s\n", |
| 1142 | regulator->supply_name)) |
| 1143 | ret = -EIO; |
| 1144 | if (ret == 0) |
| 1145 | regulator->enabled--; |
| 1146 | mutex_unlock(&rdev->mutex); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1147 | return ret; |
| 1148 | } |
| 1149 | EXPORT_SYMBOL_GPL(regulator_disable); |
| 1150 | |
| 1151 | /* locks held by regulator_force_disable() */ |
| 1152 | static int _regulator_force_disable(struct regulator_dev *rdev) |
| 1153 | { |
| 1154 | int ret = 0; |
| 1155 | |
| 1156 | /* force disable */ |
| 1157 | if (rdev->desc->ops->disable) { |
| 1158 | /* ah well, who wants to live forever... */ |
| 1159 | ret = rdev->desc->ops->disable(rdev); |
| 1160 | if (ret < 0) { |
| 1161 | printk(KERN_ERR "%s: failed to force disable %s\n", |
| 1162 | __func__, rdev->desc->name); |
| 1163 | return ret; |
| 1164 | } |
| 1165 | /* notify other consumers that power has been forced off */ |
| 1166 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE, |
| 1167 | NULL); |
| 1168 | } |
| 1169 | |
| 1170 | /* decrease our supplies ref count and disable if required */ |
| 1171 | if (rdev->supply) |
| 1172 | _regulator_disable(rdev->supply); |
| 1173 | |
| 1174 | rdev->use_count = 0; |
| 1175 | return ret; |
| 1176 | } |
| 1177 | |
| 1178 | /** |
| 1179 | * regulator_force_disable - force disable regulator output |
| 1180 | * @regulator: regulator source |
| 1181 | * |
| 1182 | * Forcibly disable the regulator output voltage or current. |
| 1183 | * NOTE: this *will* disable the regulator output even if other consumer |
| 1184 | * devices have it enabled. This should be used for situations when device |
| 1185 | * damage will likely occur if the regulator is not disabled (e.g. over temp). |
| 1186 | */ |
| 1187 | int regulator_force_disable(struct regulator *regulator) |
| 1188 | { |
| 1189 | int ret; |
| 1190 | |
| 1191 | mutex_lock(®ulator->rdev->mutex); |
| 1192 | regulator->enabled = 0; |
| 1193 | regulator->uA_load = 0; |
| 1194 | ret = _regulator_force_disable(regulator->rdev); |
| 1195 | mutex_unlock(®ulator->rdev->mutex); |
| 1196 | return ret; |
| 1197 | } |
| 1198 | EXPORT_SYMBOL_GPL(regulator_force_disable); |
| 1199 | |
| 1200 | static int _regulator_is_enabled(struct regulator_dev *rdev) |
| 1201 | { |
| 1202 | int ret; |
| 1203 | |
| 1204 | mutex_lock(&rdev->mutex); |
| 1205 | |
| 1206 | /* sanity check */ |
| 1207 | if (!rdev->desc->ops->is_enabled) { |
| 1208 | ret = -EINVAL; |
| 1209 | goto out; |
| 1210 | } |
| 1211 | |
| 1212 | ret = rdev->desc->ops->is_enabled(rdev); |
| 1213 | out: |
| 1214 | mutex_unlock(&rdev->mutex); |
| 1215 | return ret; |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * regulator_is_enabled - is the regulator output enabled |
| 1220 | * @regulator: regulator source |
| 1221 | * |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1222 | * Returns positive if the regulator driver backing the source/client |
| 1223 | * has requested that the device be enabled, zero if it hasn't, else a |
| 1224 | * negative errno code. |
| 1225 | * |
| 1226 | * Note that the device backing this regulator handle can have multiple |
| 1227 | * users, so it might be enabled even if regulator_enable() was never |
| 1228 | * called for this particular source. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1229 | */ |
| 1230 | int regulator_is_enabled(struct regulator *regulator) |
| 1231 | { |
| 1232 | return _regulator_is_enabled(regulator->rdev); |
| 1233 | } |
| 1234 | EXPORT_SYMBOL_GPL(regulator_is_enabled); |
| 1235 | |
| 1236 | /** |
| 1237 | * regulator_set_voltage - set regulator output voltage |
| 1238 | * @regulator: regulator source |
| 1239 | * @min_uV: Minimum required voltage in uV |
| 1240 | * @max_uV: Maximum acceptable voltage in uV |
| 1241 | * |
| 1242 | * Sets a voltage regulator to the desired output voltage. This can be set |
| 1243 | * during any regulator state. IOW, regulator can be disabled or enabled. |
| 1244 | * |
| 1245 | * If the regulator is enabled then the voltage will change to the new value |
| 1246 | * immediately otherwise if the regulator is disabled the regulator will |
| 1247 | * output at the new voltage when enabled. |
| 1248 | * |
| 1249 | * NOTE: If the regulator is shared between several devices then the lowest |
| 1250 | * request voltage that meets the system constraints will be used. |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1251 | * Regulator system constraints must be set for this regulator before |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1252 | * calling this function otherwise this call will fail. |
| 1253 | */ |
| 1254 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) |
| 1255 | { |
| 1256 | struct regulator_dev *rdev = regulator->rdev; |
| 1257 | int ret; |
| 1258 | |
| 1259 | mutex_lock(&rdev->mutex); |
| 1260 | |
| 1261 | /* sanity check */ |
| 1262 | if (!rdev->desc->ops->set_voltage) { |
| 1263 | ret = -EINVAL; |
| 1264 | goto out; |
| 1265 | } |
| 1266 | |
| 1267 | /* constraints check */ |
| 1268 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); |
| 1269 | if (ret < 0) |
| 1270 | goto out; |
| 1271 | regulator->min_uV = min_uV; |
| 1272 | regulator->max_uV = max_uV; |
| 1273 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV); |
| 1274 | |
| 1275 | out: |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 1276 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1277 | mutex_unlock(&rdev->mutex); |
| 1278 | return ret; |
| 1279 | } |
| 1280 | EXPORT_SYMBOL_GPL(regulator_set_voltage); |
| 1281 | |
| 1282 | static int _regulator_get_voltage(struct regulator_dev *rdev) |
| 1283 | { |
| 1284 | /* sanity check */ |
| 1285 | if (rdev->desc->ops->get_voltage) |
| 1286 | return rdev->desc->ops->get_voltage(rdev); |
| 1287 | else |
| 1288 | return -EINVAL; |
| 1289 | } |
| 1290 | |
| 1291 | /** |
| 1292 | * regulator_get_voltage - get regulator output voltage |
| 1293 | * @regulator: regulator source |
| 1294 | * |
| 1295 | * This returns the current regulator voltage in uV. |
| 1296 | * |
| 1297 | * NOTE: If the regulator is disabled it will return the voltage value. This |
| 1298 | * function should not be used to determine regulator state. |
| 1299 | */ |
| 1300 | int regulator_get_voltage(struct regulator *regulator) |
| 1301 | { |
| 1302 | int ret; |
| 1303 | |
| 1304 | mutex_lock(®ulator->rdev->mutex); |
| 1305 | |
| 1306 | ret = _regulator_get_voltage(regulator->rdev); |
| 1307 | |
| 1308 | mutex_unlock(®ulator->rdev->mutex); |
| 1309 | |
| 1310 | return ret; |
| 1311 | } |
| 1312 | EXPORT_SYMBOL_GPL(regulator_get_voltage); |
| 1313 | |
| 1314 | /** |
| 1315 | * regulator_set_current_limit - set regulator output current limit |
| 1316 | * @regulator: regulator source |
| 1317 | * @min_uA: Minimuum supported current in uA |
| 1318 | * @max_uA: Maximum supported current in uA |
| 1319 | * |
| 1320 | * Sets current sink to the desired output current. This can be set during |
| 1321 | * any regulator state. IOW, regulator can be disabled or enabled. |
| 1322 | * |
| 1323 | * If the regulator is enabled then the current will change to the new value |
| 1324 | * immediately otherwise if the regulator is disabled the regulator will |
| 1325 | * output at the new current when enabled. |
| 1326 | * |
| 1327 | * NOTE: Regulator system constraints must be set for this regulator before |
| 1328 | * calling this function otherwise this call will fail. |
| 1329 | */ |
| 1330 | int regulator_set_current_limit(struct regulator *regulator, |
| 1331 | int min_uA, int max_uA) |
| 1332 | { |
| 1333 | struct regulator_dev *rdev = regulator->rdev; |
| 1334 | int ret; |
| 1335 | |
| 1336 | mutex_lock(&rdev->mutex); |
| 1337 | |
| 1338 | /* sanity check */ |
| 1339 | if (!rdev->desc->ops->set_current_limit) { |
| 1340 | ret = -EINVAL; |
| 1341 | goto out; |
| 1342 | } |
| 1343 | |
| 1344 | /* constraints check */ |
| 1345 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); |
| 1346 | if (ret < 0) |
| 1347 | goto out; |
| 1348 | |
| 1349 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); |
| 1350 | out: |
| 1351 | mutex_unlock(&rdev->mutex); |
| 1352 | return ret; |
| 1353 | } |
| 1354 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); |
| 1355 | |
| 1356 | static int _regulator_get_current_limit(struct regulator_dev *rdev) |
| 1357 | { |
| 1358 | int ret; |
| 1359 | |
| 1360 | mutex_lock(&rdev->mutex); |
| 1361 | |
| 1362 | /* sanity check */ |
| 1363 | if (!rdev->desc->ops->get_current_limit) { |
| 1364 | ret = -EINVAL; |
| 1365 | goto out; |
| 1366 | } |
| 1367 | |
| 1368 | ret = rdev->desc->ops->get_current_limit(rdev); |
| 1369 | out: |
| 1370 | mutex_unlock(&rdev->mutex); |
| 1371 | return ret; |
| 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * regulator_get_current_limit - get regulator output current |
| 1376 | * @regulator: regulator source |
| 1377 | * |
| 1378 | * This returns the current supplied by the specified current sink in uA. |
| 1379 | * |
| 1380 | * NOTE: If the regulator is disabled it will return the current value. This |
| 1381 | * function should not be used to determine regulator state. |
| 1382 | */ |
| 1383 | int regulator_get_current_limit(struct regulator *regulator) |
| 1384 | { |
| 1385 | return _regulator_get_current_limit(regulator->rdev); |
| 1386 | } |
| 1387 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); |
| 1388 | |
| 1389 | /** |
| 1390 | * regulator_set_mode - set regulator operating mode |
| 1391 | * @regulator: regulator source |
| 1392 | * @mode: operating mode - one of the REGULATOR_MODE constants |
| 1393 | * |
| 1394 | * Set regulator operating mode to increase regulator efficiency or improve |
| 1395 | * regulation performance. |
| 1396 | * |
| 1397 | * NOTE: Regulator system constraints must be set for this regulator before |
| 1398 | * calling this function otherwise this call will fail. |
| 1399 | */ |
| 1400 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) |
| 1401 | { |
| 1402 | struct regulator_dev *rdev = regulator->rdev; |
| 1403 | int ret; |
| 1404 | |
| 1405 | mutex_lock(&rdev->mutex); |
| 1406 | |
| 1407 | /* sanity check */ |
| 1408 | if (!rdev->desc->ops->set_mode) { |
| 1409 | ret = -EINVAL; |
| 1410 | goto out; |
| 1411 | } |
| 1412 | |
| 1413 | /* constraints check */ |
| 1414 | ret = regulator_check_mode(rdev, mode); |
| 1415 | if (ret < 0) |
| 1416 | goto out; |
| 1417 | |
| 1418 | ret = rdev->desc->ops->set_mode(rdev, mode); |
| 1419 | out: |
| 1420 | mutex_unlock(&rdev->mutex); |
| 1421 | return ret; |
| 1422 | } |
| 1423 | EXPORT_SYMBOL_GPL(regulator_set_mode); |
| 1424 | |
| 1425 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) |
| 1426 | { |
| 1427 | int ret; |
| 1428 | |
| 1429 | mutex_lock(&rdev->mutex); |
| 1430 | |
| 1431 | /* sanity check */ |
| 1432 | if (!rdev->desc->ops->get_mode) { |
| 1433 | ret = -EINVAL; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | |
| 1437 | ret = rdev->desc->ops->get_mode(rdev); |
| 1438 | out: |
| 1439 | mutex_unlock(&rdev->mutex); |
| 1440 | return ret; |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * regulator_get_mode - get regulator operating mode |
| 1445 | * @regulator: regulator source |
| 1446 | * |
| 1447 | * Get the current regulator operating mode. |
| 1448 | */ |
| 1449 | unsigned int regulator_get_mode(struct regulator *regulator) |
| 1450 | { |
| 1451 | return _regulator_get_mode(regulator->rdev); |
| 1452 | } |
| 1453 | EXPORT_SYMBOL_GPL(regulator_get_mode); |
| 1454 | |
| 1455 | /** |
| 1456 | * regulator_set_optimum_mode - set regulator optimum operating mode |
| 1457 | * @regulator: regulator source |
| 1458 | * @uA_load: load current |
| 1459 | * |
| 1460 | * Notifies the regulator core of a new device load. This is then used by |
| 1461 | * DRMS (if enabled by constraints) to set the most efficient regulator |
| 1462 | * operating mode for the new regulator loading. |
| 1463 | * |
| 1464 | * Consumer devices notify their supply regulator of the maximum power |
| 1465 | * they will require (can be taken from device datasheet in the power |
| 1466 | * consumption tables) when they change operational status and hence power |
| 1467 | * state. Examples of operational state changes that can affect power |
| 1468 | * consumption are :- |
| 1469 | * |
| 1470 | * o Device is opened / closed. |
| 1471 | * o Device I/O is about to begin or has just finished. |
| 1472 | * o Device is idling in between work. |
| 1473 | * |
| 1474 | * This information is also exported via sysfs to userspace. |
| 1475 | * |
| 1476 | * DRMS will sum the total requested load on the regulator and change |
| 1477 | * to the most efficient operating mode if platform constraints allow. |
| 1478 | * |
| 1479 | * Returns the new regulator mode or error. |
| 1480 | */ |
| 1481 | int regulator_set_optimum_mode(struct regulator *regulator, int uA_load) |
| 1482 | { |
| 1483 | struct regulator_dev *rdev = regulator->rdev; |
| 1484 | struct regulator *consumer; |
| 1485 | int ret, output_uV, input_uV, total_uA_load = 0; |
| 1486 | unsigned int mode; |
| 1487 | |
| 1488 | mutex_lock(&rdev->mutex); |
| 1489 | |
| 1490 | regulator->uA_load = uA_load; |
| 1491 | ret = regulator_check_drms(rdev); |
| 1492 | if (ret < 0) |
| 1493 | goto out; |
| 1494 | ret = -EINVAL; |
| 1495 | |
| 1496 | /* sanity check */ |
| 1497 | if (!rdev->desc->ops->get_optimum_mode) |
| 1498 | goto out; |
| 1499 | |
| 1500 | /* get output voltage */ |
| 1501 | output_uV = rdev->desc->ops->get_voltage(rdev); |
| 1502 | if (output_uV <= 0) { |
| 1503 | printk(KERN_ERR "%s: invalid output voltage found for %s\n", |
| 1504 | __func__, rdev->desc->name); |
| 1505 | goto out; |
| 1506 | } |
| 1507 | |
| 1508 | /* get input voltage */ |
| 1509 | if (rdev->supply && rdev->supply->desc->ops->get_voltage) |
| 1510 | input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); |
| 1511 | else |
| 1512 | input_uV = rdev->constraints->input_uV; |
| 1513 | if (input_uV <= 0) { |
| 1514 | printk(KERN_ERR "%s: invalid input voltage found for %s\n", |
| 1515 | __func__, rdev->desc->name); |
| 1516 | goto out; |
| 1517 | } |
| 1518 | |
| 1519 | /* calc total requested load for this regulator */ |
| 1520 | list_for_each_entry(consumer, &rdev->consumer_list, list) |
| 1521 | total_uA_load += consumer->uA_load; |
| 1522 | |
| 1523 | mode = rdev->desc->ops->get_optimum_mode(rdev, |
| 1524 | input_uV, output_uV, |
| 1525 | total_uA_load); |
David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 1526 | ret = regulator_check_mode(rdev, mode); |
| 1527 | if (ret < 0) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1528 | printk(KERN_ERR "%s: failed to get optimum mode for %s @" |
| 1529 | " %d uA %d -> %d uV\n", __func__, rdev->desc->name, |
| 1530 | total_uA_load, input_uV, output_uV); |
| 1531 | goto out; |
| 1532 | } |
| 1533 | |
| 1534 | ret = rdev->desc->ops->set_mode(rdev, mode); |
David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 1535 | if (ret < 0) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1536 | printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n", |
| 1537 | __func__, mode, rdev->desc->name); |
| 1538 | goto out; |
| 1539 | } |
| 1540 | ret = mode; |
| 1541 | out: |
| 1542 | mutex_unlock(&rdev->mutex); |
| 1543 | return ret; |
| 1544 | } |
| 1545 | EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); |
| 1546 | |
| 1547 | /** |
| 1548 | * regulator_register_notifier - register regulator event notifier |
| 1549 | * @regulator: regulator source |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1550 | * @nb: notifier block |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1551 | * |
| 1552 | * Register notifier block to receive regulator events. |
| 1553 | */ |
| 1554 | int regulator_register_notifier(struct regulator *regulator, |
| 1555 | struct notifier_block *nb) |
| 1556 | { |
| 1557 | return blocking_notifier_chain_register(®ulator->rdev->notifier, |
| 1558 | nb); |
| 1559 | } |
| 1560 | EXPORT_SYMBOL_GPL(regulator_register_notifier); |
| 1561 | |
| 1562 | /** |
| 1563 | * regulator_unregister_notifier - unregister regulator event notifier |
| 1564 | * @regulator: regulator source |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1565 | * @nb: notifier block |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1566 | * |
| 1567 | * Unregister regulator event notifier block. |
| 1568 | */ |
| 1569 | int regulator_unregister_notifier(struct regulator *regulator, |
| 1570 | struct notifier_block *nb) |
| 1571 | { |
| 1572 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, |
| 1573 | nb); |
| 1574 | } |
| 1575 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); |
| 1576 | |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 1577 | /* notify regulator consumers and downstream regulator consumers. |
| 1578 | * Note mutex must be held by caller. |
| 1579 | */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1580 | static void _notifier_call_chain(struct regulator_dev *rdev, |
| 1581 | unsigned long event, void *data) |
| 1582 | { |
| 1583 | struct regulator_dev *_rdev; |
| 1584 | |
| 1585 | /* call rdev chain first */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1586 | blocking_notifier_call_chain(&rdev->notifier, event, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1587 | |
| 1588 | /* now notify regulator we supply */ |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 1589 | list_for_each_entry(_rdev, &rdev->supply_list, slist) { |
| 1590 | mutex_lock(&_rdev->mutex); |
| 1591 | _notifier_call_chain(_rdev, event, data); |
| 1592 | mutex_unlock(&_rdev->mutex); |
| 1593 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | /** |
| 1597 | * regulator_bulk_get - get multiple regulator consumers |
| 1598 | * |
| 1599 | * @dev: Device to supply |
| 1600 | * @num_consumers: Number of consumers to register |
| 1601 | * @consumers: Configuration of consumers; clients are stored here. |
| 1602 | * |
| 1603 | * @return 0 on success, an errno on failure. |
| 1604 | * |
| 1605 | * This helper function allows drivers to get several regulator |
| 1606 | * consumers in one operation. If any of the regulators cannot be |
| 1607 | * acquired then any regulators that were allocated will be freed |
| 1608 | * before returning to the caller. |
| 1609 | */ |
| 1610 | int regulator_bulk_get(struct device *dev, int num_consumers, |
| 1611 | struct regulator_bulk_data *consumers) |
| 1612 | { |
| 1613 | int i; |
| 1614 | int ret; |
| 1615 | |
| 1616 | for (i = 0; i < num_consumers; i++) |
| 1617 | consumers[i].consumer = NULL; |
| 1618 | |
| 1619 | for (i = 0; i < num_consumers; i++) { |
| 1620 | consumers[i].consumer = regulator_get(dev, |
| 1621 | consumers[i].supply); |
| 1622 | if (IS_ERR(consumers[i].consumer)) { |
| 1623 | dev_err(dev, "Failed to get supply '%s'\n", |
| 1624 | consumers[i].supply); |
| 1625 | ret = PTR_ERR(consumers[i].consumer); |
| 1626 | consumers[i].consumer = NULL; |
| 1627 | goto err; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | return 0; |
| 1632 | |
| 1633 | err: |
| 1634 | for (i = 0; i < num_consumers && consumers[i].consumer; i++) |
| 1635 | regulator_put(consumers[i].consumer); |
| 1636 | |
| 1637 | return ret; |
| 1638 | } |
| 1639 | EXPORT_SYMBOL_GPL(regulator_bulk_get); |
| 1640 | |
| 1641 | /** |
| 1642 | * regulator_bulk_enable - enable multiple regulator consumers |
| 1643 | * |
| 1644 | * @num_consumers: Number of consumers |
| 1645 | * @consumers: Consumer data; clients are stored here. |
| 1646 | * @return 0 on success, an errno on failure |
| 1647 | * |
| 1648 | * This convenience API allows consumers to enable multiple regulator |
| 1649 | * clients in a single API call. If any consumers cannot be enabled |
| 1650 | * then any others that were enabled will be disabled again prior to |
| 1651 | * return. |
| 1652 | */ |
| 1653 | int regulator_bulk_enable(int num_consumers, |
| 1654 | struct regulator_bulk_data *consumers) |
| 1655 | { |
| 1656 | int i; |
| 1657 | int ret; |
| 1658 | |
| 1659 | for (i = 0; i < num_consumers; i++) { |
| 1660 | ret = regulator_enable(consumers[i].consumer); |
| 1661 | if (ret != 0) |
| 1662 | goto err; |
| 1663 | } |
| 1664 | |
| 1665 | return 0; |
| 1666 | |
| 1667 | err: |
| 1668 | printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply); |
| 1669 | for (i = 0; i < num_consumers; i++) |
| 1670 | regulator_disable(consumers[i].consumer); |
| 1671 | |
| 1672 | return ret; |
| 1673 | } |
| 1674 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); |
| 1675 | |
| 1676 | /** |
| 1677 | * regulator_bulk_disable - disable multiple regulator consumers |
| 1678 | * |
| 1679 | * @num_consumers: Number of consumers |
| 1680 | * @consumers: Consumer data; clients are stored here. |
| 1681 | * @return 0 on success, an errno on failure |
| 1682 | * |
| 1683 | * This convenience API allows consumers to disable multiple regulator |
| 1684 | * clients in a single API call. If any consumers cannot be enabled |
| 1685 | * then any others that were disabled will be disabled again prior to |
| 1686 | * return. |
| 1687 | */ |
| 1688 | int regulator_bulk_disable(int num_consumers, |
| 1689 | struct regulator_bulk_data *consumers) |
| 1690 | { |
| 1691 | int i; |
| 1692 | int ret; |
| 1693 | |
| 1694 | for (i = 0; i < num_consumers; i++) { |
| 1695 | ret = regulator_disable(consumers[i].consumer); |
| 1696 | if (ret != 0) |
| 1697 | goto err; |
| 1698 | } |
| 1699 | |
| 1700 | return 0; |
| 1701 | |
| 1702 | err: |
| 1703 | printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply); |
| 1704 | for (i = 0; i < num_consumers; i++) |
| 1705 | regulator_enable(consumers[i].consumer); |
| 1706 | |
| 1707 | return ret; |
| 1708 | } |
| 1709 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); |
| 1710 | |
| 1711 | /** |
| 1712 | * regulator_bulk_free - free multiple regulator consumers |
| 1713 | * |
| 1714 | * @num_consumers: Number of consumers |
| 1715 | * @consumers: Consumer data; clients are stored here. |
| 1716 | * |
| 1717 | * This convenience API allows consumers to free multiple regulator |
| 1718 | * clients in a single API call. |
| 1719 | */ |
| 1720 | void regulator_bulk_free(int num_consumers, |
| 1721 | struct regulator_bulk_data *consumers) |
| 1722 | { |
| 1723 | int i; |
| 1724 | |
| 1725 | for (i = 0; i < num_consumers; i++) { |
| 1726 | regulator_put(consumers[i].consumer); |
| 1727 | consumers[i].consumer = NULL; |
| 1728 | } |
| 1729 | } |
| 1730 | EXPORT_SYMBOL_GPL(regulator_bulk_free); |
| 1731 | |
| 1732 | /** |
| 1733 | * regulator_notifier_call_chain - call regulator event notifier |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1734 | * @rdev: regulator source |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1735 | * @event: notifier block |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1736 | * @data: callback-specific data. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1737 | * |
| 1738 | * Called by regulator drivers to notify clients a regulator event has |
| 1739 | * occurred. We also notify regulator clients downstream. |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 1740 | * Note lock must be held by caller. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1741 | */ |
| 1742 | int regulator_notifier_call_chain(struct regulator_dev *rdev, |
| 1743 | unsigned long event, void *data) |
| 1744 | { |
| 1745 | _notifier_call_chain(rdev, event, data); |
| 1746 | return NOTIFY_DONE; |
| 1747 | |
| 1748 | } |
| 1749 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); |
| 1750 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 1751 | /* |
| 1752 | * To avoid cluttering sysfs (and memory) with useless state, only |
| 1753 | * create attributes that can be meaningfully displayed. |
| 1754 | */ |
| 1755 | static int add_regulator_attributes(struct regulator_dev *rdev) |
| 1756 | { |
| 1757 | struct device *dev = &rdev->dev; |
| 1758 | struct regulator_ops *ops = rdev->desc->ops; |
| 1759 | int status = 0; |
| 1760 | |
| 1761 | /* some attributes need specific methods to be displayed */ |
| 1762 | if (ops->get_voltage) { |
| 1763 | status = device_create_file(dev, &dev_attr_microvolts); |
| 1764 | if (status < 0) |
| 1765 | return status; |
| 1766 | } |
| 1767 | if (ops->get_current_limit) { |
| 1768 | status = device_create_file(dev, &dev_attr_microamps); |
| 1769 | if (status < 0) |
| 1770 | return status; |
| 1771 | } |
| 1772 | if (ops->get_mode) { |
| 1773 | status = device_create_file(dev, &dev_attr_opmode); |
| 1774 | if (status < 0) |
| 1775 | return status; |
| 1776 | } |
| 1777 | if (ops->is_enabled) { |
| 1778 | status = device_create_file(dev, &dev_attr_state); |
| 1779 | if (status < 0) |
| 1780 | return status; |
| 1781 | } |
David Brownell | 853116a | 2009-01-14 23:03:17 -0800 | [diff] [blame] | 1782 | if (ops->get_status) { |
| 1783 | status = device_create_file(dev, &dev_attr_status); |
| 1784 | if (status < 0) |
| 1785 | return status; |
| 1786 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 1787 | |
| 1788 | /* some attributes are type-specific */ |
| 1789 | if (rdev->desc->type == REGULATOR_CURRENT) { |
| 1790 | status = device_create_file(dev, &dev_attr_requested_microamps); |
| 1791 | if (status < 0) |
| 1792 | return status; |
| 1793 | } |
| 1794 | |
| 1795 | /* all the other attributes exist to support constraints; |
| 1796 | * don't show them if there are no constraints, or if the |
| 1797 | * relevant supporting methods are missing. |
| 1798 | */ |
| 1799 | if (!rdev->constraints) |
| 1800 | return status; |
| 1801 | |
| 1802 | /* constraints need specific supporting methods */ |
| 1803 | if (ops->set_voltage) { |
| 1804 | status = device_create_file(dev, &dev_attr_min_microvolts); |
| 1805 | if (status < 0) |
| 1806 | return status; |
| 1807 | status = device_create_file(dev, &dev_attr_max_microvolts); |
| 1808 | if (status < 0) |
| 1809 | return status; |
| 1810 | } |
| 1811 | if (ops->set_current_limit) { |
| 1812 | status = device_create_file(dev, &dev_attr_min_microamps); |
| 1813 | if (status < 0) |
| 1814 | return status; |
| 1815 | status = device_create_file(dev, &dev_attr_max_microamps); |
| 1816 | if (status < 0) |
| 1817 | return status; |
| 1818 | } |
| 1819 | |
| 1820 | /* suspend mode constraints need multiple supporting methods */ |
| 1821 | if (!(ops->set_suspend_enable && ops->set_suspend_disable)) |
| 1822 | return status; |
| 1823 | |
| 1824 | status = device_create_file(dev, &dev_attr_suspend_standby_state); |
| 1825 | if (status < 0) |
| 1826 | return status; |
| 1827 | status = device_create_file(dev, &dev_attr_suspend_mem_state); |
| 1828 | if (status < 0) |
| 1829 | return status; |
| 1830 | status = device_create_file(dev, &dev_attr_suspend_disk_state); |
| 1831 | if (status < 0) |
| 1832 | return status; |
| 1833 | |
| 1834 | if (ops->set_suspend_voltage) { |
| 1835 | status = device_create_file(dev, |
| 1836 | &dev_attr_suspend_standby_microvolts); |
| 1837 | if (status < 0) |
| 1838 | return status; |
| 1839 | status = device_create_file(dev, |
| 1840 | &dev_attr_suspend_mem_microvolts); |
| 1841 | if (status < 0) |
| 1842 | return status; |
| 1843 | status = device_create_file(dev, |
| 1844 | &dev_attr_suspend_disk_microvolts); |
| 1845 | if (status < 0) |
| 1846 | return status; |
| 1847 | } |
| 1848 | |
| 1849 | if (ops->set_suspend_mode) { |
| 1850 | status = device_create_file(dev, |
| 1851 | &dev_attr_suspend_standby_mode); |
| 1852 | if (status < 0) |
| 1853 | return status; |
| 1854 | status = device_create_file(dev, |
| 1855 | &dev_attr_suspend_mem_mode); |
| 1856 | if (status < 0) |
| 1857 | return status; |
| 1858 | status = device_create_file(dev, |
| 1859 | &dev_attr_suspend_disk_mode); |
| 1860 | if (status < 0) |
| 1861 | return status; |
| 1862 | } |
| 1863 | |
| 1864 | return status; |
| 1865 | } |
| 1866 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1867 | /** |
| 1868 | * regulator_register - register regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1869 | * @regulator_desc: regulator to register |
| 1870 | * @dev: struct device for the regulator |
Mark Brown | 0527100 | 2009-01-19 13:37:02 +0000 | [diff] [blame] | 1871 | * @init_data: platform provided init data, passed through by driver |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1872 | * @driver_data: private regulator data |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1873 | * |
| 1874 | * Called by regulator drivers to register a regulator. |
| 1875 | * Returns 0 on success. |
| 1876 | */ |
| 1877 | struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, |
Mark Brown | 0527100 | 2009-01-19 13:37:02 +0000 | [diff] [blame] | 1878 | struct device *dev, struct regulator_init_data *init_data, |
| 1879 | void *driver_data) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1880 | { |
| 1881 | static atomic_t regulator_no = ATOMIC_INIT(0); |
| 1882 | struct regulator_dev *rdev; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1883 | int ret, i; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1884 | |
| 1885 | if (regulator_desc == NULL) |
| 1886 | return ERR_PTR(-EINVAL); |
| 1887 | |
| 1888 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) |
| 1889 | return ERR_PTR(-EINVAL); |
| 1890 | |
| 1891 | if (!regulator_desc->type == REGULATOR_VOLTAGE && |
| 1892 | !regulator_desc->type == REGULATOR_CURRENT) |
| 1893 | return ERR_PTR(-EINVAL); |
| 1894 | |
Mark Brown | 46fabe1e | 2008-09-09 16:21:18 +0100 | [diff] [blame] | 1895 | if (!init_data) |
| 1896 | return ERR_PTR(-EINVAL); |
| 1897 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1898 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
| 1899 | if (rdev == NULL) |
| 1900 | return ERR_PTR(-ENOMEM); |
| 1901 | |
| 1902 | mutex_lock(®ulator_list_mutex); |
| 1903 | |
| 1904 | mutex_init(&rdev->mutex); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1905 | rdev->reg_data = driver_data; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1906 | rdev->owner = regulator_desc->owner; |
| 1907 | rdev->desc = regulator_desc; |
| 1908 | INIT_LIST_HEAD(&rdev->consumer_list); |
| 1909 | INIT_LIST_HEAD(&rdev->supply_list); |
| 1910 | INIT_LIST_HEAD(&rdev->list); |
| 1911 | INIT_LIST_HEAD(&rdev->slist); |
| 1912 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); |
| 1913 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1914 | /* preform any regulator specific init */ |
| 1915 | if (init_data->regulator_init) { |
| 1916 | ret = init_data->regulator_init(rdev->reg_data); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 1917 | if (ret < 0) |
| 1918 | goto clean; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1919 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1920 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1921 | /* register with sysfs */ |
| 1922 | rdev->dev.class = ®ulator_class; |
| 1923 | rdev->dev.parent = dev; |
Kay Sievers | 812460a | 2008-11-02 03:55:10 +0100 | [diff] [blame] | 1924 | dev_set_name(&rdev->dev, "regulator.%d", |
| 1925 | atomic_inc_return(®ulator_no) - 1); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1926 | ret = device_register(&rdev->dev); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 1927 | if (ret != 0) |
| 1928 | goto clean; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1929 | |
| 1930 | dev_set_drvdata(&rdev->dev, rdev); |
| 1931 | |
Mike Rapoport | 74f544c | 2008-11-25 14:53:53 +0200 | [diff] [blame] | 1932 | /* set regulator constraints */ |
| 1933 | ret = set_machine_constraints(rdev, &init_data->constraints); |
| 1934 | if (ret < 0) |
| 1935 | goto scrub; |
| 1936 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 1937 | /* add attributes supported by this regulator */ |
| 1938 | ret = add_regulator_attributes(rdev); |
| 1939 | if (ret < 0) |
| 1940 | goto scrub; |
| 1941 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1942 | /* set supply regulator if it exists */ |
| 1943 | if (init_data->supply_regulator_dev) { |
| 1944 | ret = set_supply(rdev, |
| 1945 | dev_get_drvdata(init_data->supply_regulator_dev)); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 1946 | if (ret < 0) |
| 1947 | goto scrub; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1948 | } |
| 1949 | |
| 1950 | /* add consumers devices */ |
| 1951 | for (i = 0; i < init_data->num_consumer_supplies; i++) { |
| 1952 | ret = set_consumer_device_supply(rdev, |
| 1953 | init_data->consumer_supplies[i].dev, |
| 1954 | init_data->consumer_supplies[i].supply); |
| 1955 | if (ret < 0) { |
| 1956 | for (--i; i >= 0; i--) |
| 1957 | unset_consumer_device_supply(rdev, |
| 1958 | init_data->consumer_supplies[i].dev); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 1959 | goto scrub; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1960 | } |
| 1961 | } |
| 1962 | |
| 1963 | list_add(&rdev->list, ®ulator_list); |
| 1964 | out: |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1965 | mutex_unlock(®ulator_list_mutex); |
| 1966 | return rdev; |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 1967 | |
| 1968 | scrub: |
| 1969 | device_unregister(&rdev->dev); |
| 1970 | clean: |
| 1971 | kfree(rdev); |
| 1972 | rdev = ERR_PTR(ret); |
| 1973 | goto out; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1974 | } |
| 1975 | EXPORT_SYMBOL_GPL(regulator_register); |
| 1976 | |
| 1977 | /** |
| 1978 | * regulator_unregister - unregister regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1979 | * @rdev: regulator to unregister |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1980 | * |
| 1981 | * Called by regulator drivers to unregister a regulator. |
| 1982 | */ |
| 1983 | void regulator_unregister(struct regulator_dev *rdev) |
| 1984 | { |
| 1985 | if (rdev == NULL) |
| 1986 | return; |
| 1987 | |
| 1988 | mutex_lock(®ulator_list_mutex); |
Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 1989 | unset_regulator_supplies(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1990 | list_del(&rdev->list); |
| 1991 | if (rdev->supply) |
| 1992 | sysfs_remove_link(&rdev->dev.kobj, "supply"); |
| 1993 | device_unregister(&rdev->dev); |
| 1994 | mutex_unlock(®ulator_list_mutex); |
| 1995 | } |
| 1996 | EXPORT_SYMBOL_GPL(regulator_unregister); |
| 1997 | |
| 1998 | /** |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1999 | * regulator_suspend_prepare - prepare regulators for system wide suspend |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2000 | * @state: system suspend state |
| 2001 | * |
| 2002 | * Configure each regulator with it's suspend operating parameters for state. |
| 2003 | * This will usually be called by machine suspend code prior to supending. |
| 2004 | */ |
| 2005 | int regulator_suspend_prepare(suspend_state_t state) |
| 2006 | { |
| 2007 | struct regulator_dev *rdev; |
| 2008 | int ret = 0; |
| 2009 | |
| 2010 | /* ON is handled by regulator active state */ |
| 2011 | if (state == PM_SUSPEND_ON) |
| 2012 | return -EINVAL; |
| 2013 | |
| 2014 | mutex_lock(®ulator_list_mutex); |
| 2015 | list_for_each_entry(rdev, ®ulator_list, list) { |
| 2016 | |
| 2017 | mutex_lock(&rdev->mutex); |
| 2018 | ret = suspend_prepare(rdev, state); |
| 2019 | mutex_unlock(&rdev->mutex); |
| 2020 | |
| 2021 | if (ret < 0) { |
| 2022 | printk(KERN_ERR "%s: failed to prepare %s\n", |
| 2023 | __func__, rdev->desc->name); |
| 2024 | goto out; |
| 2025 | } |
| 2026 | } |
| 2027 | out: |
| 2028 | mutex_unlock(®ulator_list_mutex); |
| 2029 | return ret; |
| 2030 | } |
| 2031 | EXPORT_SYMBOL_GPL(regulator_suspend_prepare); |
| 2032 | |
| 2033 | /** |
| 2034 | * rdev_get_drvdata - get rdev regulator driver data |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2035 | * @rdev: regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2036 | * |
| 2037 | * Get rdev regulator driver private data. This call can be used in the |
| 2038 | * regulator driver context. |
| 2039 | */ |
| 2040 | void *rdev_get_drvdata(struct regulator_dev *rdev) |
| 2041 | { |
| 2042 | return rdev->reg_data; |
| 2043 | } |
| 2044 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); |
| 2045 | |
| 2046 | /** |
| 2047 | * regulator_get_drvdata - get regulator driver data |
| 2048 | * @regulator: regulator |
| 2049 | * |
| 2050 | * Get regulator driver private data. This call can be used in the consumer |
| 2051 | * driver context when non API regulator specific functions need to be called. |
| 2052 | */ |
| 2053 | void *regulator_get_drvdata(struct regulator *regulator) |
| 2054 | { |
| 2055 | return regulator->rdev->reg_data; |
| 2056 | } |
| 2057 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); |
| 2058 | |
| 2059 | /** |
| 2060 | * regulator_set_drvdata - set regulator driver data |
| 2061 | * @regulator: regulator |
| 2062 | * @data: data |
| 2063 | */ |
| 2064 | void regulator_set_drvdata(struct regulator *regulator, void *data) |
| 2065 | { |
| 2066 | regulator->rdev->reg_data = data; |
| 2067 | } |
| 2068 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); |
| 2069 | |
| 2070 | /** |
| 2071 | * regulator_get_id - get regulator ID |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2072 | * @rdev: regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2073 | */ |
| 2074 | int rdev_get_id(struct regulator_dev *rdev) |
| 2075 | { |
| 2076 | return rdev->desc->id; |
| 2077 | } |
| 2078 | EXPORT_SYMBOL_GPL(rdev_get_id); |
| 2079 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 2080 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
| 2081 | { |
| 2082 | return &rdev->dev; |
| 2083 | } |
| 2084 | EXPORT_SYMBOL_GPL(rdev_get_dev); |
| 2085 | |
| 2086 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) |
| 2087 | { |
| 2088 | return reg_init_data->driver_data; |
| 2089 | } |
| 2090 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); |
| 2091 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2092 | static int __init regulator_init(void) |
| 2093 | { |
| 2094 | printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION); |
| 2095 | return class_register(®ulator_class); |
| 2096 | } |
| 2097 | |
| 2098 | /* init early to allow our consumers to complete system booting */ |
| 2099 | core_initcall(regulator_init); |