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 | |
| 32 | /** |
| 33 | * struct regulator_dev |
| 34 | * |
| 35 | * Voltage / Current regulator class device. One for each regulator. |
| 36 | */ |
| 37 | struct regulator_dev { |
| 38 | struct regulator_desc *desc; |
| 39 | int use_count; |
| 40 | |
| 41 | /* lists we belong to */ |
| 42 | struct list_head list; /* list of all regulators */ |
| 43 | struct list_head slist; /* list of supplied regulators */ |
| 44 | |
| 45 | /* lists we own */ |
| 46 | struct list_head consumer_list; /* consumers we supply */ |
| 47 | struct list_head supply_list; /* regulators we supply */ |
| 48 | |
| 49 | struct blocking_notifier_head notifier; |
| 50 | struct mutex mutex; /* consumer lock */ |
| 51 | struct module *owner; |
| 52 | struct device dev; |
| 53 | struct regulation_constraints *constraints; |
| 54 | struct regulator_dev *supply; /* for tree */ |
| 55 | |
| 56 | void *reg_data; /* regulator_dev data */ |
| 57 | }; |
| 58 | |
| 59 | /** |
| 60 | * struct regulator_map |
| 61 | * |
| 62 | * Used to provide symbolic supply names to devices. |
| 63 | */ |
| 64 | struct regulator_map { |
| 65 | struct list_head list; |
| 66 | struct device *dev; |
| 67 | const char *supply; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 68 | struct regulator_dev *regulator; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 71 | /* |
| 72 | * struct regulator |
| 73 | * |
| 74 | * One for each consumer device. |
| 75 | */ |
| 76 | struct regulator { |
| 77 | struct device *dev; |
| 78 | struct list_head list; |
| 79 | int uA_load; |
| 80 | int min_uV; |
| 81 | int max_uV; |
| 82 | int enabled; /* client has called enabled */ |
| 83 | char *supply_name; |
| 84 | struct device_attribute dev_attr; |
| 85 | struct regulator_dev *rdev; |
| 86 | }; |
| 87 | |
| 88 | static int _regulator_is_enabled(struct regulator_dev *rdev); |
| 89 | static int _regulator_disable(struct regulator_dev *rdev); |
| 90 | static int _regulator_get_voltage(struct regulator_dev *rdev); |
| 91 | static int _regulator_get_current_limit(struct regulator_dev *rdev); |
| 92 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev); |
| 93 | static void _notifier_call_chain(struct regulator_dev *rdev, |
| 94 | unsigned long event, void *data); |
| 95 | |
| 96 | /* gets the regulator for a given consumer device */ |
| 97 | static struct regulator *get_device_regulator(struct device *dev) |
| 98 | { |
| 99 | struct regulator *regulator = NULL; |
| 100 | struct regulator_dev *rdev; |
| 101 | |
| 102 | mutex_lock(®ulator_list_mutex); |
| 103 | list_for_each_entry(rdev, ®ulator_list, list) { |
| 104 | mutex_lock(&rdev->mutex); |
| 105 | list_for_each_entry(regulator, &rdev->consumer_list, list) { |
| 106 | if (regulator->dev == dev) { |
| 107 | mutex_unlock(&rdev->mutex); |
| 108 | mutex_unlock(®ulator_list_mutex); |
| 109 | return regulator; |
| 110 | } |
| 111 | } |
| 112 | mutex_unlock(&rdev->mutex); |
| 113 | } |
| 114 | mutex_unlock(®ulator_list_mutex); |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | /* Platform voltage constraint check */ |
| 119 | static int regulator_check_voltage(struct regulator_dev *rdev, |
| 120 | int *min_uV, int *max_uV) |
| 121 | { |
| 122 | BUG_ON(*min_uV > *max_uV); |
| 123 | |
| 124 | if (!rdev->constraints) { |
| 125 | printk(KERN_ERR "%s: no constraints for %s\n", __func__, |
| 126 | rdev->desc->name); |
| 127 | return -ENODEV; |
| 128 | } |
| 129 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { |
| 130 | printk(KERN_ERR "%s: operation not allowed for %s\n", |
| 131 | __func__, rdev->desc->name); |
| 132 | return -EPERM; |
| 133 | } |
| 134 | |
| 135 | if (*max_uV > rdev->constraints->max_uV) |
| 136 | *max_uV = rdev->constraints->max_uV; |
| 137 | if (*min_uV < rdev->constraints->min_uV) |
| 138 | *min_uV = rdev->constraints->min_uV; |
| 139 | |
| 140 | if (*min_uV > *max_uV) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | /* current constraint check */ |
| 147 | static int regulator_check_current_limit(struct regulator_dev *rdev, |
| 148 | int *min_uA, int *max_uA) |
| 149 | { |
| 150 | BUG_ON(*min_uA > *max_uA); |
| 151 | |
| 152 | if (!rdev->constraints) { |
| 153 | printk(KERN_ERR "%s: no constraints for %s\n", __func__, |
| 154 | rdev->desc->name); |
| 155 | return -ENODEV; |
| 156 | } |
| 157 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) { |
| 158 | printk(KERN_ERR "%s: operation not allowed for %s\n", |
| 159 | __func__, rdev->desc->name); |
| 160 | return -EPERM; |
| 161 | } |
| 162 | |
| 163 | if (*max_uA > rdev->constraints->max_uA) |
| 164 | *max_uA = rdev->constraints->max_uA; |
| 165 | if (*min_uA < rdev->constraints->min_uA) |
| 166 | *min_uA = rdev->constraints->min_uA; |
| 167 | |
| 168 | if (*min_uA > *max_uA) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* operating mode constraint check */ |
| 175 | static int regulator_check_mode(struct regulator_dev *rdev, int mode) |
| 176 | { |
| 177 | if (!rdev->constraints) { |
| 178 | printk(KERN_ERR "%s: no constraints for %s\n", __func__, |
| 179 | rdev->desc->name); |
| 180 | return -ENODEV; |
| 181 | } |
| 182 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) { |
| 183 | printk(KERN_ERR "%s: operation not allowed for %s\n", |
| 184 | __func__, rdev->desc->name); |
| 185 | return -EPERM; |
| 186 | } |
| 187 | if (!(rdev->constraints->valid_modes_mask & mode)) { |
| 188 | printk(KERN_ERR "%s: invalid mode %x for %s\n", |
| 189 | __func__, mode, rdev->desc->name); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | /* dynamic regulator mode switching constraint check */ |
| 196 | static int regulator_check_drms(struct regulator_dev *rdev) |
| 197 | { |
| 198 | if (!rdev->constraints) { |
| 199 | printk(KERN_ERR "%s: no constraints for %s\n", __func__, |
| 200 | rdev->desc->name); |
| 201 | return -ENODEV; |
| 202 | } |
| 203 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) { |
| 204 | printk(KERN_ERR "%s: operation not allowed for %s\n", |
| 205 | __func__, rdev->desc->name); |
| 206 | return -EPERM; |
| 207 | } |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static ssize_t device_requested_uA_show(struct device *dev, |
| 212 | struct device_attribute *attr, char *buf) |
| 213 | { |
| 214 | struct regulator *regulator; |
| 215 | |
| 216 | regulator = get_device_regulator(dev); |
| 217 | if (regulator == NULL) |
| 218 | return 0; |
| 219 | |
| 220 | return sprintf(buf, "%d\n", regulator->uA_load); |
| 221 | } |
| 222 | |
| 223 | static ssize_t regulator_uV_show(struct device *dev, |
| 224 | struct device_attribute *attr, char *buf) |
| 225 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 226 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 227 | ssize_t ret; |
| 228 | |
| 229 | mutex_lock(&rdev->mutex); |
| 230 | ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); |
| 231 | mutex_unlock(&rdev->mutex); |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
| 236 | static ssize_t regulator_uA_show(struct device *dev, |
| 237 | struct device_attribute *attr, char *buf) |
| 238 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 239 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 240 | |
| 241 | return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); |
| 242 | } |
| 243 | |
| 244 | static ssize_t regulator_opmode_show(struct device *dev, |
| 245 | struct device_attribute *attr, char *buf) |
| 246 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 247 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 248 | int mode = _regulator_get_mode(rdev); |
| 249 | |
| 250 | switch (mode) { |
| 251 | case REGULATOR_MODE_FAST: |
| 252 | return sprintf(buf, "fast\n"); |
| 253 | case REGULATOR_MODE_NORMAL: |
| 254 | return sprintf(buf, "normal\n"); |
| 255 | case REGULATOR_MODE_IDLE: |
| 256 | return sprintf(buf, "idle\n"); |
| 257 | case REGULATOR_MODE_STANDBY: |
| 258 | return sprintf(buf, "standby\n"); |
| 259 | } |
| 260 | return sprintf(buf, "unknown\n"); |
| 261 | } |
| 262 | |
| 263 | static ssize_t regulator_state_show(struct device *dev, |
| 264 | struct device_attribute *attr, char *buf) |
| 265 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 266 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 267 | int state = _regulator_is_enabled(rdev); |
| 268 | |
| 269 | if (state > 0) |
| 270 | return sprintf(buf, "enabled\n"); |
| 271 | else if (state == 0) |
| 272 | return sprintf(buf, "disabled\n"); |
| 273 | else |
| 274 | return sprintf(buf, "unknown\n"); |
| 275 | } |
| 276 | |
| 277 | static ssize_t regulator_min_uA_show(struct device *dev, |
| 278 | struct device_attribute *attr, char *buf) |
| 279 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 280 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 281 | |
| 282 | if (!rdev->constraints) |
| 283 | return sprintf(buf, "constraint not defined\n"); |
| 284 | |
| 285 | return sprintf(buf, "%d\n", rdev->constraints->min_uA); |
| 286 | } |
| 287 | |
| 288 | static ssize_t regulator_max_uA_show(struct device *dev, |
| 289 | struct device_attribute *attr, char *buf) |
| 290 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 291 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 292 | |
| 293 | if (!rdev->constraints) |
| 294 | return sprintf(buf, "constraint not defined\n"); |
| 295 | |
| 296 | return sprintf(buf, "%d\n", rdev->constraints->max_uA); |
| 297 | } |
| 298 | |
| 299 | static ssize_t regulator_min_uV_show(struct device *dev, |
| 300 | struct device_attribute *attr, char *buf) |
| 301 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 302 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 303 | |
| 304 | if (!rdev->constraints) |
| 305 | return sprintf(buf, "constraint not defined\n"); |
| 306 | |
| 307 | return sprintf(buf, "%d\n", rdev->constraints->min_uV); |
| 308 | } |
| 309 | |
| 310 | static ssize_t regulator_max_uV_show(struct device *dev, |
| 311 | struct device_attribute *attr, char *buf) |
| 312 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 313 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 314 | |
| 315 | if (!rdev->constraints) |
| 316 | return sprintf(buf, "constraint not defined\n"); |
| 317 | |
| 318 | return sprintf(buf, "%d\n", rdev->constraints->max_uV); |
| 319 | } |
| 320 | |
| 321 | static ssize_t regulator_total_uA_show(struct device *dev, |
| 322 | struct device_attribute *attr, char *buf) |
| 323 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 324 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 325 | struct regulator *regulator; |
| 326 | int uA = 0; |
| 327 | |
| 328 | mutex_lock(&rdev->mutex); |
| 329 | list_for_each_entry(regulator, &rdev->consumer_list, list) |
| 330 | uA += regulator->uA_load; |
| 331 | mutex_unlock(&rdev->mutex); |
| 332 | return sprintf(buf, "%d\n", uA); |
| 333 | } |
| 334 | |
| 335 | static ssize_t regulator_num_users_show(struct device *dev, |
| 336 | struct device_attribute *attr, char *buf) |
| 337 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 338 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 339 | return sprintf(buf, "%d\n", rdev->use_count); |
| 340 | } |
| 341 | |
| 342 | static ssize_t regulator_type_show(struct device *dev, |
| 343 | struct device_attribute *attr, char *buf) |
| 344 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 345 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 346 | |
| 347 | switch (rdev->desc->type) { |
| 348 | case REGULATOR_VOLTAGE: |
| 349 | return sprintf(buf, "voltage\n"); |
| 350 | case REGULATOR_CURRENT: |
| 351 | return sprintf(buf, "current\n"); |
| 352 | } |
| 353 | return sprintf(buf, "unknown\n"); |
| 354 | } |
| 355 | |
| 356 | static ssize_t regulator_suspend_mem_uV_show(struct device *dev, |
| 357 | struct device_attribute *attr, char *buf) |
| 358 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 359 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 360 | |
| 361 | if (!rdev->constraints) |
| 362 | return sprintf(buf, "not defined\n"); |
| 363 | return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); |
| 364 | } |
| 365 | |
| 366 | static ssize_t regulator_suspend_disk_uV_show(struct device *dev, |
| 367 | struct device_attribute *attr, char *buf) |
| 368 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 369 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 370 | |
| 371 | if (!rdev->constraints) |
| 372 | return sprintf(buf, "not defined\n"); |
| 373 | return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); |
| 374 | } |
| 375 | |
| 376 | static ssize_t regulator_suspend_standby_uV_show(struct device *dev, |
| 377 | struct device_attribute *attr, char *buf) |
| 378 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 379 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 380 | |
| 381 | if (!rdev->constraints) |
| 382 | return sprintf(buf, "not defined\n"); |
| 383 | return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); |
| 384 | } |
| 385 | |
| 386 | static ssize_t suspend_opmode_show(struct regulator_dev *rdev, |
| 387 | unsigned int mode, char *buf) |
| 388 | { |
| 389 | switch (mode) { |
| 390 | case REGULATOR_MODE_FAST: |
| 391 | return sprintf(buf, "fast\n"); |
| 392 | case REGULATOR_MODE_NORMAL: |
| 393 | return sprintf(buf, "normal\n"); |
| 394 | case REGULATOR_MODE_IDLE: |
| 395 | return sprintf(buf, "idle\n"); |
| 396 | case REGULATOR_MODE_STANDBY: |
| 397 | return sprintf(buf, "standby\n"); |
| 398 | } |
| 399 | return sprintf(buf, "unknown\n"); |
| 400 | } |
| 401 | |
| 402 | static ssize_t regulator_suspend_mem_mode_show(struct device *dev, |
| 403 | struct device_attribute *attr, char *buf) |
| 404 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 405 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 406 | |
| 407 | if (!rdev->constraints) |
| 408 | return sprintf(buf, "not defined\n"); |
| 409 | return suspend_opmode_show(rdev, |
| 410 | rdev->constraints->state_mem.mode, buf); |
| 411 | } |
| 412 | |
| 413 | static ssize_t regulator_suspend_disk_mode_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 | |
| 418 | if (!rdev->constraints) |
| 419 | return sprintf(buf, "not defined\n"); |
| 420 | return suspend_opmode_show(rdev, |
| 421 | rdev->constraints->state_disk.mode, buf); |
| 422 | } |
| 423 | |
| 424 | static ssize_t regulator_suspend_standby_mode_show(struct device *dev, |
| 425 | struct device_attribute *attr, char *buf) |
| 426 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 427 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 428 | |
| 429 | if (!rdev->constraints) |
| 430 | return sprintf(buf, "not defined\n"); |
| 431 | return suspend_opmode_show(rdev, |
| 432 | rdev->constraints->state_standby.mode, buf); |
| 433 | } |
| 434 | |
| 435 | static ssize_t regulator_suspend_mem_state_show(struct device *dev, |
| 436 | struct device_attribute *attr, char *buf) |
| 437 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 438 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 439 | |
| 440 | if (!rdev->constraints) |
| 441 | return sprintf(buf, "not defined\n"); |
| 442 | |
| 443 | if (rdev->constraints->state_mem.enabled) |
| 444 | return sprintf(buf, "enabled\n"); |
| 445 | else |
| 446 | return sprintf(buf, "disabled\n"); |
| 447 | } |
| 448 | |
| 449 | static ssize_t regulator_suspend_disk_state_show(struct device *dev, |
| 450 | struct device_attribute *attr, char *buf) |
| 451 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 452 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 453 | |
| 454 | if (!rdev->constraints) |
| 455 | return sprintf(buf, "not defined\n"); |
| 456 | |
| 457 | if (rdev->constraints->state_disk.enabled) |
| 458 | return sprintf(buf, "enabled\n"); |
| 459 | else |
| 460 | return sprintf(buf, "disabled\n"); |
| 461 | } |
| 462 | |
| 463 | static ssize_t regulator_suspend_standby_state_show(struct device *dev, |
| 464 | struct device_attribute *attr, char *buf) |
| 465 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 466 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 467 | |
| 468 | if (!rdev->constraints) |
| 469 | return sprintf(buf, "not defined\n"); |
| 470 | |
| 471 | if (rdev->constraints->state_standby.enabled) |
| 472 | return sprintf(buf, "enabled\n"); |
| 473 | else |
| 474 | return sprintf(buf, "disabled\n"); |
| 475 | } |
| 476 | static struct device_attribute regulator_dev_attrs[] = { |
| 477 | __ATTR(microvolts, 0444, regulator_uV_show, NULL), |
| 478 | __ATTR(microamps, 0444, regulator_uA_show, NULL), |
| 479 | __ATTR(opmode, 0444, regulator_opmode_show, NULL), |
| 480 | __ATTR(state, 0444, regulator_state_show, NULL), |
| 481 | __ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL), |
| 482 | __ATTR(min_microamps, 0444, regulator_min_uA_show, NULL), |
| 483 | __ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL), |
| 484 | __ATTR(max_microamps, 0444, regulator_max_uA_show, NULL), |
| 485 | __ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL), |
| 486 | __ATTR(num_users, 0444, regulator_num_users_show, NULL), |
| 487 | __ATTR(type, 0444, regulator_type_show, NULL), |
| 488 | __ATTR(suspend_mem_microvolts, 0444, |
| 489 | regulator_suspend_mem_uV_show, NULL), |
| 490 | __ATTR(suspend_disk_microvolts, 0444, |
| 491 | regulator_suspend_disk_uV_show, NULL), |
| 492 | __ATTR(suspend_standby_microvolts, 0444, |
| 493 | regulator_suspend_standby_uV_show, NULL), |
| 494 | __ATTR(suspend_mem_mode, 0444, |
| 495 | regulator_suspend_mem_mode_show, NULL), |
| 496 | __ATTR(suspend_disk_mode, 0444, |
| 497 | regulator_suspend_disk_mode_show, NULL), |
| 498 | __ATTR(suspend_standby_mode, 0444, |
| 499 | regulator_suspend_standby_mode_show, NULL), |
| 500 | __ATTR(suspend_mem_state, 0444, |
| 501 | regulator_suspend_mem_state_show, NULL), |
| 502 | __ATTR(suspend_disk_state, 0444, |
| 503 | regulator_suspend_disk_state_show, NULL), |
| 504 | __ATTR(suspend_standby_state, 0444, |
| 505 | regulator_suspend_standby_state_show, NULL), |
| 506 | __ATTR_NULL, |
| 507 | }; |
| 508 | |
| 509 | static void regulator_dev_release(struct device *dev) |
| 510 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 511 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 512 | kfree(rdev); |
| 513 | } |
| 514 | |
| 515 | static struct class regulator_class = { |
| 516 | .name = "regulator", |
| 517 | .dev_release = regulator_dev_release, |
| 518 | .dev_attrs = regulator_dev_attrs, |
| 519 | }; |
| 520 | |
| 521 | /* Calculate the new optimum regulator operating mode based on the new total |
| 522 | * consumer load. All locks held by caller */ |
| 523 | static void drms_uA_update(struct regulator_dev *rdev) |
| 524 | { |
| 525 | struct regulator *sibling; |
| 526 | int current_uA = 0, output_uV, input_uV, err; |
| 527 | unsigned int mode; |
| 528 | |
| 529 | err = regulator_check_drms(rdev); |
| 530 | if (err < 0 || !rdev->desc->ops->get_optimum_mode || |
| 531 | !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode); |
| 532 | return; |
| 533 | |
| 534 | /* get output voltage */ |
| 535 | output_uV = rdev->desc->ops->get_voltage(rdev); |
| 536 | if (output_uV <= 0) |
| 537 | return; |
| 538 | |
| 539 | /* get input voltage */ |
| 540 | if (rdev->supply && rdev->supply->desc->ops->get_voltage) |
| 541 | input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); |
| 542 | else |
| 543 | input_uV = rdev->constraints->input_uV; |
| 544 | if (input_uV <= 0) |
| 545 | return; |
| 546 | |
| 547 | /* calc total requested load */ |
| 548 | list_for_each_entry(sibling, &rdev->consumer_list, list) |
| 549 | current_uA += sibling->uA_load; |
| 550 | |
| 551 | /* now get the optimum mode for our new total regulator load */ |
| 552 | mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, |
| 553 | output_uV, current_uA); |
| 554 | |
| 555 | /* check the new mode is allowed */ |
| 556 | err = regulator_check_mode(rdev, mode); |
| 557 | if (err == 0) |
| 558 | rdev->desc->ops->set_mode(rdev, mode); |
| 559 | } |
| 560 | |
| 561 | static int suspend_set_state(struct regulator_dev *rdev, |
| 562 | struct regulator_state *rstate) |
| 563 | { |
| 564 | int ret = 0; |
| 565 | |
| 566 | /* enable & disable are mandatory for suspend control */ |
| 567 | if (!rdev->desc->ops->set_suspend_enable || |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 568 | !rdev->desc->ops->set_suspend_disable) { |
| 569 | printk(KERN_ERR "%s: no way to set suspend state\n", |
| 570 | __func__); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 571 | return -EINVAL; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 572 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 573 | |
| 574 | if (rstate->enabled) |
| 575 | ret = rdev->desc->ops->set_suspend_enable(rdev); |
| 576 | else |
| 577 | ret = rdev->desc->ops->set_suspend_disable(rdev); |
| 578 | if (ret < 0) { |
| 579 | printk(KERN_ERR "%s: failed to enabled/disable\n", __func__); |
| 580 | return ret; |
| 581 | } |
| 582 | |
| 583 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { |
| 584 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); |
| 585 | if (ret < 0) { |
| 586 | printk(KERN_ERR "%s: failed to set voltage\n", |
| 587 | __func__); |
| 588 | return ret; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { |
| 593 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); |
| 594 | if (ret < 0) { |
| 595 | printk(KERN_ERR "%s: failed to set mode\n", __func__); |
| 596 | return ret; |
| 597 | } |
| 598 | } |
| 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | /* locks held by caller */ |
| 603 | static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state) |
| 604 | { |
| 605 | if (!rdev->constraints) |
| 606 | return -EINVAL; |
| 607 | |
| 608 | switch (state) { |
| 609 | case PM_SUSPEND_STANDBY: |
| 610 | return suspend_set_state(rdev, |
| 611 | &rdev->constraints->state_standby); |
| 612 | case PM_SUSPEND_MEM: |
| 613 | return suspend_set_state(rdev, |
| 614 | &rdev->constraints->state_mem); |
| 615 | case PM_SUSPEND_MAX: |
| 616 | return suspend_set_state(rdev, |
| 617 | &rdev->constraints->state_disk); |
| 618 | default: |
| 619 | return -EINVAL; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | static void print_constraints(struct regulator_dev *rdev) |
| 624 | { |
| 625 | struct regulation_constraints *constraints = rdev->constraints; |
| 626 | char buf[80]; |
| 627 | int count; |
| 628 | |
| 629 | if (rdev->desc->type == REGULATOR_VOLTAGE) { |
| 630 | if (constraints->min_uV == constraints->max_uV) |
| 631 | count = sprintf(buf, "%d mV ", |
| 632 | constraints->min_uV / 1000); |
| 633 | else |
| 634 | count = sprintf(buf, "%d <--> %d mV ", |
| 635 | constraints->min_uV / 1000, |
| 636 | constraints->max_uV / 1000); |
| 637 | } else { |
| 638 | if (constraints->min_uA == constraints->max_uA) |
| 639 | count = sprintf(buf, "%d mA ", |
| 640 | constraints->min_uA / 1000); |
| 641 | else |
| 642 | count = sprintf(buf, "%d <--> %d mA ", |
| 643 | constraints->min_uA / 1000, |
| 644 | constraints->max_uA / 1000); |
| 645 | } |
| 646 | if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) |
| 647 | count += sprintf(buf + count, "fast "); |
| 648 | if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) |
| 649 | count += sprintf(buf + count, "normal "); |
| 650 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) |
| 651 | count += sprintf(buf + count, "idle "); |
| 652 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) |
| 653 | count += sprintf(buf + count, "standby"); |
| 654 | |
| 655 | printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf); |
| 656 | } |
| 657 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 658 | /** |
| 659 | * set_machine_constraints - sets regulator constraints |
| 660 | * @regulator: regulator source |
| 661 | * |
| 662 | * Allows platform initialisation code to define and constrain |
| 663 | * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: |
| 664 | * Constraints *must* be set by platform code in order for some |
| 665 | * regulator operations to proceed i.e. set_voltage, set_current_limit, |
| 666 | * set_mode. |
| 667 | */ |
| 668 | static int set_machine_constraints(struct regulator_dev *rdev, |
| 669 | struct regulation_constraints *constraints) |
| 670 | { |
| 671 | int ret = 0; |
| 672 | |
| 673 | rdev->constraints = constraints; |
| 674 | |
| 675 | /* do we need to apply the constraint voltage */ |
| 676 | if (rdev->constraints->apply_uV && |
| 677 | rdev->constraints->min_uV == rdev->constraints->max_uV && |
| 678 | rdev->desc->ops->set_voltage) { |
| 679 | ret = rdev->desc->ops->set_voltage(rdev, |
| 680 | rdev->constraints->min_uV, rdev->constraints->max_uV); |
| 681 | if (ret < 0) { |
| 682 | printk(KERN_ERR "%s: failed to apply %duV" |
| 683 | " constraint\n", __func__, |
| 684 | rdev->constraints->min_uV); |
| 685 | rdev->constraints = NULL; |
| 686 | goto out; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | /* are we enabled at boot time by firmware / bootloader */ |
| 691 | if (rdev->constraints->boot_on) |
| 692 | rdev->use_count = 1; |
| 693 | |
| 694 | /* do we need to setup our suspend state */ |
| 695 | if (constraints->initial_state) |
| 696 | ret = suspend_prepare(rdev, constraints->initial_state); |
| 697 | |
| 698 | print_constraints(rdev); |
| 699 | out: |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * set_supply - set regulator supply regulator |
| 705 | * @regulator: regulator name |
| 706 | * @supply: supply regulator name |
| 707 | * |
| 708 | * Called by platform initialisation code to set the supply regulator for this |
| 709 | * regulator. This ensures that a regulators supply will also be enabled by the |
| 710 | * core if it's child is enabled. |
| 711 | */ |
| 712 | static int set_supply(struct regulator_dev *rdev, |
| 713 | struct regulator_dev *supply_rdev) |
| 714 | { |
| 715 | int err; |
| 716 | |
| 717 | err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj, |
| 718 | "supply"); |
| 719 | if (err) { |
| 720 | printk(KERN_ERR |
| 721 | "%s: could not add device link %s err %d\n", |
| 722 | __func__, supply_rdev->dev.kobj.name, err); |
| 723 | goto out; |
| 724 | } |
| 725 | rdev->supply = supply_rdev; |
| 726 | list_add(&rdev->slist, &supply_rdev->supply_list); |
| 727 | out: |
| 728 | return err; |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * set_consumer_device_supply: Bind a regulator to a symbolic supply |
| 733 | * @regulator: regulator source |
| 734 | * @dev: device the supply applies to |
| 735 | * @supply: symbolic name for supply |
| 736 | * |
| 737 | * Allows platform initialisation code to map physical regulator |
| 738 | * sources to symbolic names for supplies for use by devices. Devices |
| 739 | * should use these symbolic names to request regulators, avoiding the |
| 740 | * need to provide board-specific regulator names as platform data. |
| 741 | */ |
| 742 | static int set_consumer_device_supply(struct regulator_dev *rdev, |
| 743 | struct device *consumer_dev, const char *supply) |
| 744 | { |
| 745 | struct regulator_map *node; |
| 746 | |
| 747 | if (supply == NULL) |
| 748 | return -EINVAL; |
| 749 | |
| 750 | node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL); |
| 751 | if (node == NULL) |
| 752 | return -ENOMEM; |
| 753 | |
| 754 | node->regulator = rdev; |
| 755 | node->dev = consumer_dev; |
| 756 | node->supply = supply; |
| 757 | |
| 758 | list_add(&node->list, ®ulator_map_list); |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | static void unset_consumer_device_supply(struct regulator_dev *rdev, |
| 763 | struct device *consumer_dev) |
| 764 | { |
| 765 | struct regulator_map *node, *n; |
| 766 | |
| 767 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { |
| 768 | if (rdev == node->regulator && |
| 769 | consumer_dev == node->dev) { |
| 770 | list_del(&node->list); |
| 771 | kfree(node); |
| 772 | return; |
| 773 | } |
| 774 | } |
| 775 | } |
| 776 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 777 | #define REG_STR_SIZE 32 |
| 778 | |
| 779 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
| 780 | struct device *dev, |
| 781 | const char *supply_name) |
| 782 | { |
| 783 | struct regulator *regulator; |
| 784 | char buf[REG_STR_SIZE]; |
| 785 | int err, size; |
| 786 | |
| 787 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); |
| 788 | if (regulator == NULL) |
| 789 | return NULL; |
| 790 | |
| 791 | mutex_lock(&rdev->mutex); |
| 792 | regulator->rdev = rdev; |
| 793 | list_add(®ulator->list, &rdev->consumer_list); |
| 794 | |
| 795 | if (dev) { |
| 796 | /* create a 'requested_microamps_name' sysfs entry */ |
| 797 | size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s", |
| 798 | supply_name); |
| 799 | if (size >= REG_STR_SIZE) |
| 800 | goto overflow_err; |
| 801 | |
| 802 | regulator->dev = dev; |
| 803 | regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL); |
| 804 | if (regulator->dev_attr.attr.name == NULL) |
| 805 | goto attr_name_err; |
| 806 | |
| 807 | regulator->dev_attr.attr.owner = THIS_MODULE; |
| 808 | regulator->dev_attr.attr.mode = 0444; |
| 809 | regulator->dev_attr.show = device_requested_uA_show; |
| 810 | err = device_create_file(dev, ®ulator->dev_attr); |
| 811 | if (err < 0) { |
| 812 | printk(KERN_WARNING "%s: could not add regulator_dev" |
| 813 | " load sysfs\n", __func__); |
| 814 | goto attr_name_err; |
| 815 | } |
| 816 | |
| 817 | /* also add a link to the device sysfs entry */ |
| 818 | size = scnprintf(buf, REG_STR_SIZE, "%s-%s", |
| 819 | dev->kobj.name, supply_name); |
| 820 | if (size >= REG_STR_SIZE) |
| 821 | goto attr_err; |
| 822 | |
| 823 | regulator->supply_name = kstrdup(buf, GFP_KERNEL); |
| 824 | if (regulator->supply_name == NULL) |
| 825 | goto attr_err; |
| 826 | |
| 827 | err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj, |
| 828 | buf); |
| 829 | if (err) { |
| 830 | printk(KERN_WARNING |
| 831 | "%s: could not add device link %s err %d\n", |
| 832 | __func__, dev->kobj.name, err); |
| 833 | device_remove_file(dev, ®ulator->dev_attr); |
| 834 | goto link_name_err; |
| 835 | } |
| 836 | } |
| 837 | mutex_unlock(&rdev->mutex); |
| 838 | return regulator; |
| 839 | link_name_err: |
| 840 | kfree(regulator->supply_name); |
| 841 | attr_err: |
| 842 | device_remove_file(regulator->dev, ®ulator->dev_attr); |
| 843 | attr_name_err: |
| 844 | kfree(regulator->dev_attr.attr.name); |
| 845 | overflow_err: |
| 846 | list_del(®ulator->list); |
| 847 | kfree(regulator); |
| 848 | mutex_unlock(&rdev->mutex); |
| 849 | return NULL; |
| 850 | } |
| 851 | |
| 852 | /** |
| 853 | * regulator_get - lookup and obtain a reference to a regulator. |
| 854 | * @dev: device for regulator "consumer" |
| 855 | * @id: Supply name or regulator ID. |
| 856 | * |
| 857 | * Returns a struct regulator corresponding to the regulator producer, |
| 858 | * or IS_ERR() condition containing errno. Use of supply names |
| 859 | * configured via regulator_set_device_supply() is strongly |
| 860 | * encouraged. |
| 861 | */ |
| 862 | struct regulator *regulator_get(struct device *dev, const char *id) |
| 863 | { |
| 864 | struct regulator_dev *rdev; |
| 865 | struct regulator_map *map; |
| 866 | struct regulator *regulator = ERR_PTR(-ENODEV); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 867 | |
| 868 | if (id == NULL) { |
| 869 | printk(KERN_ERR "regulator: get() with no identifier\n"); |
| 870 | return regulator; |
| 871 | } |
| 872 | |
| 873 | mutex_lock(®ulator_list_mutex); |
| 874 | |
| 875 | list_for_each_entry(map, ®ulator_map_list, list) { |
| 876 | if (dev == map->dev && |
| 877 | strcmp(map->supply, id) == 0) { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 878 | rdev = map->regulator; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 879 | goto found; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 880 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 881 | } |
| 882 | printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n", |
| 883 | id); |
| 884 | mutex_unlock(®ulator_list_mutex); |
| 885 | return regulator; |
| 886 | |
| 887 | found: |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 888 | if (!try_module_get(rdev->owner)) |
| 889 | goto out; |
| 890 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 891 | regulator = create_regulator(rdev, dev, id); |
| 892 | if (regulator == NULL) { |
| 893 | regulator = ERR_PTR(-ENOMEM); |
| 894 | module_put(rdev->owner); |
| 895 | } |
| 896 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 897 | out: |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 898 | mutex_unlock(®ulator_list_mutex); |
| 899 | return regulator; |
| 900 | } |
| 901 | EXPORT_SYMBOL_GPL(regulator_get); |
| 902 | |
| 903 | /** |
| 904 | * regulator_put - "free" the regulator source |
| 905 | * @regulator: regulator source |
| 906 | * |
| 907 | * Note: drivers must ensure that all regulator_enable calls made on this |
| 908 | * regulator source are balanced by regulator_disable calls prior to calling |
| 909 | * this function. |
| 910 | */ |
| 911 | void regulator_put(struct regulator *regulator) |
| 912 | { |
| 913 | struct regulator_dev *rdev; |
| 914 | |
| 915 | if (regulator == NULL || IS_ERR(regulator)) |
| 916 | return; |
| 917 | |
| 918 | if (regulator->enabled) { |
| 919 | printk(KERN_WARNING "Releasing supply %s while enabled\n", |
| 920 | regulator->supply_name); |
| 921 | WARN_ON(regulator->enabled); |
| 922 | regulator_disable(regulator); |
| 923 | } |
| 924 | |
| 925 | mutex_lock(®ulator_list_mutex); |
| 926 | rdev = regulator->rdev; |
| 927 | |
| 928 | /* remove any sysfs entries */ |
| 929 | if (regulator->dev) { |
| 930 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); |
| 931 | kfree(regulator->supply_name); |
| 932 | device_remove_file(regulator->dev, ®ulator->dev_attr); |
| 933 | kfree(regulator->dev_attr.attr.name); |
| 934 | } |
| 935 | list_del(®ulator->list); |
| 936 | kfree(regulator); |
| 937 | |
| 938 | module_put(rdev->owner); |
| 939 | mutex_unlock(®ulator_list_mutex); |
| 940 | } |
| 941 | EXPORT_SYMBOL_GPL(regulator_put); |
| 942 | |
| 943 | /* locks held by regulator_enable() */ |
| 944 | static int _regulator_enable(struct regulator_dev *rdev) |
| 945 | { |
| 946 | int ret = -EINVAL; |
| 947 | |
| 948 | if (!rdev->constraints) { |
| 949 | printk(KERN_ERR "%s: %s has no constraints\n", |
| 950 | __func__, rdev->desc->name); |
| 951 | return ret; |
| 952 | } |
| 953 | |
| 954 | /* do we need to enable the supply regulator first */ |
| 955 | if (rdev->supply) { |
| 956 | ret = _regulator_enable(rdev->supply); |
| 957 | if (ret < 0) { |
| 958 | printk(KERN_ERR "%s: failed to enable %s: %d\n", |
| 959 | __func__, rdev->desc->name, ret); |
| 960 | return ret; |
| 961 | } |
| 962 | } |
| 963 | |
| 964 | /* check voltage and requested load before enabling */ |
| 965 | if (rdev->desc->ops->enable) { |
| 966 | |
| 967 | if (rdev->constraints && |
| 968 | (rdev->constraints->valid_ops_mask & |
| 969 | REGULATOR_CHANGE_DRMS)) |
| 970 | drms_uA_update(rdev); |
| 971 | |
| 972 | ret = rdev->desc->ops->enable(rdev); |
| 973 | if (ret < 0) { |
| 974 | printk(KERN_ERR "%s: failed to enable %s: %d\n", |
| 975 | __func__, rdev->desc->name, ret); |
| 976 | return ret; |
| 977 | } |
| 978 | rdev->use_count++; |
| 979 | return ret; |
| 980 | } |
| 981 | |
| 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * regulator_enable - enable regulator output |
| 987 | * @regulator: regulator source |
| 988 | * |
| 989 | * Enable the regulator output at the predefined voltage or current value. |
| 990 | * NOTE: the output value can be set by other drivers, boot loader or may be |
| 991 | * hardwired in the regulator. |
| 992 | * NOTE: calls to regulator_enable() must be balanced with calls to |
| 993 | * regulator_disable(). |
| 994 | */ |
| 995 | int regulator_enable(struct regulator *regulator) |
| 996 | { |
| 997 | int ret; |
| 998 | |
| 999 | if (regulator->enabled) { |
| 1000 | printk(KERN_CRIT "Regulator %s already enabled\n", |
| 1001 | regulator->supply_name); |
| 1002 | WARN_ON(regulator->enabled); |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | mutex_lock(®ulator->rdev->mutex); |
| 1007 | regulator->enabled = 1; |
| 1008 | ret = _regulator_enable(regulator->rdev); |
| 1009 | if (ret != 0) |
| 1010 | regulator->enabled = 0; |
| 1011 | mutex_unlock(®ulator->rdev->mutex); |
| 1012 | return ret; |
| 1013 | } |
| 1014 | EXPORT_SYMBOL_GPL(regulator_enable); |
| 1015 | |
| 1016 | /* locks held by regulator_disable() */ |
| 1017 | static int _regulator_disable(struct regulator_dev *rdev) |
| 1018 | { |
| 1019 | int ret = 0; |
| 1020 | |
| 1021 | /* are we the last user and permitted to disable ? */ |
| 1022 | if (rdev->use_count == 1 && !rdev->constraints->always_on) { |
| 1023 | |
| 1024 | /* we are last user */ |
| 1025 | if (rdev->desc->ops->disable) { |
| 1026 | ret = rdev->desc->ops->disable(rdev); |
| 1027 | if (ret < 0) { |
| 1028 | printk(KERN_ERR "%s: failed to disable %s\n", |
| 1029 | __func__, rdev->desc->name); |
| 1030 | return ret; |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /* decrease our supplies ref count and disable if required */ |
| 1035 | if (rdev->supply) |
| 1036 | _regulator_disable(rdev->supply); |
| 1037 | |
| 1038 | rdev->use_count = 0; |
| 1039 | } else if (rdev->use_count > 1) { |
| 1040 | |
| 1041 | if (rdev->constraints && |
| 1042 | (rdev->constraints->valid_ops_mask & |
| 1043 | REGULATOR_CHANGE_DRMS)) |
| 1044 | drms_uA_update(rdev); |
| 1045 | |
| 1046 | rdev->use_count--; |
| 1047 | } |
| 1048 | return ret; |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * regulator_disable - disable regulator output |
| 1053 | * @regulator: regulator source |
| 1054 | * |
| 1055 | * Disable the regulator output voltage or current. |
| 1056 | * NOTE: this will only disable the regulator output if no other consumer |
| 1057 | * devices have it enabled. |
| 1058 | * NOTE: calls to regulator_enable() must be balanced with calls to |
| 1059 | * regulator_disable(). |
| 1060 | */ |
| 1061 | int regulator_disable(struct regulator *regulator) |
| 1062 | { |
| 1063 | int ret; |
| 1064 | |
| 1065 | if (!regulator->enabled) { |
| 1066 | printk(KERN_ERR "%s: not in use by this consumer\n", |
| 1067 | __func__); |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | mutex_lock(®ulator->rdev->mutex); |
| 1072 | regulator->enabled = 0; |
| 1073 | regulator->uA_load = 0; |
| 1074 | ret = _regulator_disable(regulator->rdev); |
| 1075 | mutex_unlock(®ulator->rdev->mutex); |
| 1076 | return ret; |
| 1077 | } |
| 1078 | EXPORT_SYMBOL_GPL(regulator_disable); |
| 1079 | |
| 1080 | /* locks held by regulator_force_disable() */ |
| 1081 | static int _regulator_force_disable(struct regulator_dev *rdev) |
| 1082 | { |
| 1083 | int ret = 0; |
| 1084 | |
| 1085 | /* force disable */ |
| 1086 | if (rdev->desc->ops->disable) { |
| 1087 | /* ah well, who wants to live forever... */ |
| 1088 | ret = rdev->desc->ops->disable(rdev); |
| 1089 | if (ret < 0) { |
| 1090 | printk(KERN_ERR "%s: failed to force disable %s\n", |
| 1091 | __func__, rdev->desc->name); |
| 1092 | return ret; |
| 1093 | } |
| 1094 | /* notify other consumers that power has been forced off */ |
| 1095 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE, |
| 1096 | NULL); |
| 1097 | } |
| 1098 | |
| 1099 | /* decrease our supplies ref count and disable if required */ |
| 1100 | if (rdev->supply) |
| 1101 | _regulator_disable(rdev->supply); |
| 1102 | |
| 1103 | rdev->use_count = 0; |
| 1104 | return ret; |
| 1105 | } |
| 1106 | |
| 1107 | /** |
| 1108 | * regulator_force_disable - force disable regulator output |
| 1109 | * @regulator: regulator source |
| 1110 | * |
| 1111 | * Forcibly disable the regulator output voltage or current. |
| 1112 | * NOTE: this *will* disable the regulator output even if other consumer |
| 1113 | * devices have it enabled. This should be used for situations when device |
| 1114 | * damage will likely occur if the regulator is not disabled (e.g. over temp). |
| 1115 | */ |
| 1116 | int regulator_force_disable(struct regulator *regulator) |
| 1117 | { |
| 1118 | int ret; |
| 1119 | |
| 1120 | mutex_lock(®ulator->rdev->mutex); |
| 1121 | regulator->enabled = 0; |
| 1122 | regulator->uA_load = 0; |
| 1123 | ret = _regulator_force_disable(regulator->rdev); |
| 1124 | mutex_unlock(®ulator->rdev->mutex); |
| 1125 | return ret; |
| 1126 | } |
| 1127 | EXPORT_SYMBOL_GPL(regulator_force_disable); |
| 1128 | |
| 1129 | static int _regulator_is_enabled(struct regulator_dev *rdev) |
| 1130 | { |
| 1131 | int ret; |
| 1132 | |
| 1133 | mutex_lock(&rdev->mutex); |
| 1134 | |
| 1135 | /* sanity check */ |
| 1136 | if (!rdev->desc->ops->is_enabled) { |
| 1137 | ret = -EINVAL; |
| 1138 | goto out; |
| 1139 | } |
| 1140 | |
| 1141 | ret = rdev->desc->ops->is_enabled(rdev); |
| 1142 | out: |
| 1143 | mutex_unlock(&rdev->mutex); |
| 1144 | return ret; |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * regulator_is_enabled - is the regulator output enabled |
| 1149 | * @regulator: regulator source |
| 1150 | * |
| 1151 | * Returns zero for disabled otherwise return number of enable requests. |
| 1152 | */ |
| 1153 | int regulator_is_enabled(struct regulator *regulator) |
| 1154 | { |
| 1155 | return _regulator_is_enabled(regulator->rdev); |
| 1156 | } |
| 1157 | EXPORT_SYMBOL_GPL(regulator_is_enabled); |
| 1158 | |
| 1159 | /** |
| 1160 | * regulator_set_voltage - set regulator output voltage |
| 1161 | * @regulator: regulator source |
| 1162 | * @min_uV: Minimum required voltage in uV |
| 1163 | * @max_uV: Maximum acceptable voltage in uV |
| 1164 | * |
| 1165 | * Sets a voltage regulator to the desired output voltage. This can be set |
| 1166 | * during any regulator state. IOW, regulator can be disabled or enabled. |
| 1167 | * |
| 1168 | * If the regulator is enabled then the voltage will change to the new value |
| 1169 | * immediately otherwise if the regulator is disabled the regulator will |
| 1170 | * output at the new voltage when enabled. |
| 1171 | * |
| 1172 | * NOTE: If the regulator is shared between several devices then the lowest |
| 1173 | * request voltage that meets the system constraints will be used. |
| 1174 | * NOTE: Regulator system constraints must be set for this regulator before |
| 1175 | * calling this function otherwise this call will fail. |
| 1176 | */ |
| 1177 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) |
| 1178 | { |
| 1179 | struct regulator_dev *rdev = regulator->rdev; |
| 1180 | int ret; |
| 1181 | |
| 1182 | mutex_lock(&rdev->mutex); |
| 1183 | |
| 1184 | /* sanity check */ |
| 1185 | if (!rdev->desc->ops->set_voltage) { |
| 1186 | ret = -EINVAL; |
| 1187 | goto out; |
| 1188 | } |
| 1189 | |
| 1190 | /* constraints check */ |
| 1191 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); |
| 1192 | if (ret < 0) |
| 1193 | goto out; |
| 1194 | regulator->min_uV = min_uV; |
| 1195 | regulator->max_uV = max_uV; |
| 1196 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV); |
| 1197 | |
| 1198 | out: |
| 1199 | mutex_unlock(&rdev->mutex); |
| 1200 | return ret; |
| 1201 | } |
| 1202 | EXPORT_SYMBOL_GPL(regulator_set_voltage); |
| 1203 | |
| 1204 | static int _regulator_get_voltage(struct regulator_dev *rdev) |
| 1205 | { |
| 1206 | /* sanity check */ |
| 1207 | if (rdev->desc->ops->get_voltage) |
| 1208 | return rdev->desc->ops->get_voltage(rdev); |
| 1209 | else |
| 1210 | return -EINVAL; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * regulator_get_voltage - get regulator output voltage |
| 1215 | * @regulator: regulator source |
| 1216 | * |
| 1217 | * This returns the current regulator voltage in uV. |
| 1218 | * |
| 1219 | * NOTE: If the regulator is disabled it will return the voltage value. This |
| 1220 | * function should not be used to determine regulator state. |
| 1221 | */ |
| 1222 | int regulator_get_voltage(struct regulator *regulator) |
| 1223 | { |
| 1224 | int ret; |
| 1225 | |
| 1226 | mutex_lock(®ulator->rdev->mutex); |
| 1227 | |
| 1228 | ret = _regulator_get_voltage(regulator->rdev); |
| 1229 | |
| 1230 | mutex_unlock(®ulator->rdev->mutex); |
| 1231 | |
| 1232 | return ret; |
| 1233 | } |
| 1234 | EXPORT_SYMBOL_GPL(regulator_get_voltage); |
| 1235 | |
| 1236 | /** |
| 1237 | * regulator_set_current_limit - set regulator output current limit |
| 1238 | * @regulator: regulator source |
| 1239 | * @min_uA: Minimuum supported current in uA |
| 1240 | * @max_uA: Maximum supported current in uA |
| 1241 | * |
| 1242 | * Sets current sink to the desired output current. This can be set during |
| 1243 | * any regulator state. IOW, regulator can be disabled or enabled. |
| 1244 | * |
| 1245 | * If the regulator is enabled then the current will change to the new value |
| 1246 | * immediately otherwise if the regulator is disabled the regulator will |
| 1247 | * output at the new current when enabled. |
| 1248 | * |
| 1249 | * NOTE: Regulator system constraints must be set for this regulator before |
| 1250 | * calling this function otherwise this call will fail. |
| 1251 | */ |
| 1252 | int regulator_set_current_limit(struct regulator *regulator, |
| 1253 | int min_uA, int max_uA) |
| 1254 | { |
| 1255 | struct regulator_dev *rdev = regulator->rdev; |
| 1256 | int ret; |
| 1257 | |
| 1258 | mutex_lock(&rdev->mutex); |
| 1259 | |
| 1260 | /* sanity check */ |
| 1261 | if (!rdev->desc->ops->set_current_limit) { |
| 1262 | ret = -EINVAL; |
| 1263 | goto out; |
| 1264 | } |
| 1265 | |
| 1266 | /* constraints check */ |
| 1267 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); |
| 1268 | if (ret < 0) |
| 1269 | goto out; |
| 1270 | |
| 1271 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); |
| 1272 | out: |
| 1273 | mutex_unlock(&rdev->mutex); |
| 1274 | return ret; |
| 1275 | } |
| 1276 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); |
| 1277 | |
| 1278 | static int _regulator_get_current_limit(struct regulator_dev *rdev) |
| 1279 | { |
| 1280 | int ret; |
| 1281 | |
| 1282 | mutex_lock(&rdev->mutex); |
| 1283 | |
| 1284 | /* sanity check */ |
| 1285 | if (!rdev->desc->ops->get_current_limit) { |
| 1286 | ret = -EINVAL; |
| 1287 | goto out; |
| 1288 | } |
| 1289 | |
| 1290 | ret = rdev->desc->ops->get_current_limit(rdev); |
| 1291 | out: |
| 1292 | mutex_unlock(&rdev->mutex); |
| 1293 | return ret; |
| 1294 | } |
| 1295 | |
| 1296 | /** |
| 1297 | * regulator_get_current_limit - get regulator output current |
| 1298 | * @regulator: regulator source |
| 1299 | * |
| 1300 | * This returns the current supplied by the specified current sink in uA. |
| 1301 | * |
| 1302 | * NOTE: If the regulator is disabled it will return the current value. This |
| 1303 | * function should not be used to determine regulator state. |
| 1304 | */ |
| 1305 | int regulator_get_current_limit(struct regulator *regulator) |
| 1306 | { |
| 1307 | return _regulator_get_current_limit(regulator->rdev); |
| 1308 | } |
| 1309 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); |
| 1310 | |
| 1311 | /** |
| 1312 | * regulator_set_mode - set regulator operating mode |
| 1313 | * @regulator: regulator source |
| 1314 | * @mode: operating mode - one of the REGULATOR_MODE constants |
| 1315 | * |
| 1316 | * Set regulator operating mode to increase regulator efficiency or improve |
| 1317 | * regulation performance. |
| 1318 | * |
| 1319 | * NOTE: Regulator system constraints must be set for this regulator before |
| 1320 | * calling this function otherwise this call will fail. |
| 1321 | */ |
| 1322 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) |
| 1323 | { |
| 1324 | struct regulator_dev *rdev = regulator->rdev; |
| 1325 | int ret; |
| 1326 | |
| 1327 | mutex_lock(&rdev->mutex); |
| 1328 | |
| 1329 | /* sanity check */ |
| 1330 | if (!rdev->desc->ops->set_mode) { |
| 1331 | ret = -EINVAL; |
| 1332 | goto out; |
| 1333 | } |
| 1334 | |
| 1335 | /* constraints check */ |
| 1336 | ret = regulator_check_mode(rdev, mode); |
| 1337 | if (ret < 0) |
| 1338 | goto out; |
| 1339 | |
| 1340 | ret = rdev->desc->ops->set_mode(rdev, mode); |
| 1341 | out: |
| 1342 | mutex_unlock(&rdev->mutex); |
| 1343 | return ret; |
| 1344 | } |
| 1345 | EXPORT_SYMBOL_GPL(regulator_set_mode); |
| 1346 | |
| 1347 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) |
| 1348 | { |
| 1349 | int ret; |
| 1350 | |
| 1351 | mutex_lock(&rdev->mutex); |
| 1352 | |
| 1353 | /* sanity check */ |
| 1354 | if (!rdev->desc->ops->get_mode) { |
| 1355 | ret = -EINVAL; |
| 1356 | goto out; |
| 1357 | } |
| 1358 | |
| 1359 | ret = rdev->desc->ops->get_mode(rdev); |
| 1360 | out: |
| 1361 | mutex_unlock(&rdev->mutex); |
| 1362 | return ret; |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * regulator_get_mode - get regulator operating mode |
| 1367 | * @regulator: regulator source |
| 1368 | * |
| 1369 | * Get the current regulator operating mode. |
| 1370 | */ |
| 1371 | unsigned int regulator_get_mode(struct regulator *regulator) |
| 1372 | { |
| 1373 | return _regulator_get_mode(regulator->rdev); |
| 1374 | } |
| 1375 | EXPORT_SYMBOL_GPL(regulator_get_mode); |
| 1376 | |
| 1377 | /** |
| 1378 | * regulator_set_optimum_mode - set regulator optimum operating mode |
| 1379 | * @regulator: regulator source |
| 1380 | * @uA_load: load current |
| 1381 | * |
| 1382 | * Notifies the regulator core of a new device load. This is then used by |
| 1383 | * DRMS (if enabled by constraints) to set the most efficient regulator |
| 1384 | * operating mode for the new regulator loading. |
| 1385 | * |
| 1386 | * Consumer devices notify their supply regulator of the maximum power |
| 1387 | * they will require (can be taken from device datasheet in the power |
| 1388 | * consumption tables) when they change operational status and hence power |
| 1389 | * state. Examples of operational state changes that can affect power |
| 1390 | * consumption are :- |
| 1391 | * |
| 1392 | * o Device is opened / closed. |
| 1393 | * o Device I/O is about to begin or has just finished. |
| 1394 | * o Device is idling in between work. |
| 1395 | * |
| 1396 | * This information is also exported via sysfs to userspace. |
| 1397 | * |
| 1398 | * DRMS will sum the total requested load on the regulator and change |
| 1399 | * to the most efficient operating mode if platform constraints allow. |
| 1400 | * |
| 1401 | * Returns the new regulator mode or error. |
| 1402 | */ |
| 1403 | int regulator_set_optimum_mode(struct regulator *regulator, int uA_load) |
| 1404 | { |
| 1405 | struct regulator_dev *rdev = regulator->rdev; |
| 1406 | struct regulator *consumer; |
| 1407 | int ret, output_uV, input_uV, total_uA_load = 0; |
| 1408 | unsigned int mode; |
| 1409 | |
| 1410 | mutex_lock(&rdev->mutex); |
| 1411 | |
| 1412 | regulator->uA_load = uA_load; |
| 1413 | ret = regulator_check_drms(rdev); |
| 1414 | if (ret < 0) |
| 1415 | goto out; |
| 1416 | ret = -EINVAL; |
| 1417 | |
| 1418 | /* sanity check */ |
| 1419 | if (!rdev->desc->ops->get_optimum_mode) |
| 1420 | goto out; |
| 1421 | |
| 1422 | /* get output voltage */ |
| 1423 | output_uV = rdev->desc->ops->get_voltage(rdev); |
| 1424 | if (output_uV <= 0) { |
| 1425 | printk(KERN_ERR "%s: invalid output voltage found for %s\n", |
| 1426 | __func__, rdev->desc->name); |
| 1427 | goto out; |
| 1428 | } |
| 1429 | |
| 1430 | /* get input voltage */ |
| 1431 | if (rdev->supply && rdev->supply->desc->ops->get_voltage) |
| 1432 | input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); |
| 1433 | else |
| 1434 | input_uV = rdev->constraints->input_uV; |
| 1435 | if (input_uV <= 0) { |
| 1436 | printk(KERN_ERR "%s: invalid input voltage found for %s\n", |
| 1437 | __func__, rdev->desc->name); |
| 1438 | goto out; |
| 1439 | } |
| 1440 | |
| 1441 | /* calc total requested load for this regulator */ |
| 1442 | list_for_each_entry(consumer, &rdev->consumer_list, list) |
| 1443 | total_uA_load += consumer->uA_load; |
| 1444 | |
| 1445 | mode = rdev->desc->ops->get_optimum_mode(rdev, |
| 1446 | input_uV, output_uV, |
| 1447 | total_uA_load); |
| 1448 | if (ret <= 0) { |
| 1449 | printk(KERN_ERR "%s: failed to get optimum mode for %s @" |
| 1450 | " %d uA %d -> %d uV\n", __func__, rdev->desc->name, |
| 1451 | total_uA_load, input_uV, output_uV); |
| 1452 | goto out; |
| 1453 | } |
| 1454 | |
| 1455 | ret = rdev->desc->ops->set_mode(rdev, mode); |
| 1456 | if (ret <= 0) { |
| 1457 | printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n", |
| 1458 | __func__, mode, rdev->desc->name); |
| 1459 | goto out; |
| 1460 | } |
| 1461 | ret = mode; |
| 1462 | out: |
| 1463 | mutex_unlock(&rdev->mutex); |
| 1464 | return ret; |
| 1465 | } |
| 1466 | EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); |
| 1467 | |
| 1468 | /** |
| 1469 | * regulator_register_notifier - register regulator event notifier |
| 1470 | * @regulator: regulator source |
| 1471 | * @notifier_block: notifier block |
| 1472 | * |
| 1473 | * Register notifier block to receive regulator events. |
| 1474 | */ |
| 1475 | int regulator_register_notifier(struct regulator *regulator, |
| 1476 | struct notifier_block *nb) |
| 1477 | { |
| 1478 | return blocking_notifier_chain_register(®ulator->rdev->notifier, |
| 1479 | nb); |
| 1480 | } |
| 1481 | EXPORT_SYMBOL_GPL(regulator_register_notifier); |
| 1482 | |
| 1483 | /** |
| 1484 | * regulator_unregister_notifier - unregister regulator event notifier |
| 1485 | * @regulator: regulator source |
| 1486 | * @notifier_block: notifier block |
| 1487 | * |
| 1488 | * Unregister regulator event notifier block. |
| 1489 | */ |
| 1490 | int regulator_unregister_notifier(struct regulator *regulator, |
| 1491 | struct notifier_block *nb) |
| 1492 | { |
| 1493 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, |
| 1494 | nb); |
| 1495 | } |
| 1496 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); |
| 1497 | |
| 1498 | /* notify regulator consumers and downstream regulator consumers */ |
| 1499 | static void _notifier_call_chain(struct regulator_dev *rdev, |
| 1500 | unsigned long event, void *data) |
| 1501 | { |
| 1502 | struct regulator_dev *_rdev; |
| 1503 | |
| 1504 | /* call rdev chain first */ |
| 1505 | mutex_lock(&rdev->mutex); |
| 1506 | blocking_notifier_call_chain(&rdev->notifier, event, NULL); |
| 1507 | mutex_unlock(&rdev->mutex); |
| 1508 | |
| 1509 | /* now notify regulator we supply */ |
| 1510 | list_for_each_entry(_rdev, &rdev->supply_list, slist) |
| 1511 | _notifier_call_chain(_rdev, event, data); |
| 1512 | } |
| 1513 | |
| 1514 | /** |
| 1515 | * regulator_bulk_get - get multiple regulator consumers |
| 1516 | * |
| 1517 | * @dev: Device to supply |
| 1518 | * @num_consumers: Number of consumers to register |
| 1519 | * @consumers: Configuration of consumers; clients are stored here. |
| 1520 | * |
| 1521 | * @return 0 on success, an errno on failure. |
| 1522 | * |
| 1523 | * This helper function allows drivers to get several regulator |
| 1524 | * consumers in one operation. If any of the regulators cannot be |
| 1525 | * acquired then any regulators that were allocated will be freed |
| 1526 | * before returning to the caller. |
| 1527 | */ |
| 1528 | int regulator_bulk_get(struct device *dev, int num_consumers, |
| 1529 | struct regulator_bulk_data *consumers) |
| 1530 | { |
| 1531 | int i; |
| 1532 | int ret; |
| 1533 | |
| 1534 | for (i = 0; i < num_consumers; i++) |
| 1535 | consumers[i].consumer = NULL; |
| 1536 | |
| 1537 | for (i = 0; i < num_consumers; i++) { |
| 1538 | consumers[i].consumer = regulator_get(dev, |
| 1539 | consumers[i].supply); |
| 1540 | if (IS_ERR(consumers[i].consumer)) { |
| 1541 | dev_err(dev, "Failed to get supply '%s'\n", |
| 1542 | consumers[i].supply); |
| 1543 | ret = PTR_ERR(consumers[i].consumer); |
| 1544 | consumers[i].consumer = NULL; |
| 1545 | goto err; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | return 0; |
| 1550 | |
| 1551 | err: |
| 1552 | for (i = 0; i < num_consumers && consumers[i].consumer; i++) |
| 1553 | regulator_put(consumers[i].consumer); |
| 1554 | |
| 1555 | return ret; |
| 1556 | } |
| 1557 | EXPORT_SYMBOL_GPL(regulator_bulk_get); |
| 1558 | |
| 1559 | /** |
| 1560 | * regulator_bulk_enable - enable multiple regulator consumers |
| 1561 | * |
| 1562 | * @num_consumers: Number of consumers |
| 1563 | * @consumers: Consumer data; clients are stored here. |
| 1564 | * @return 0 on success, an errno on failure |
| 1565 | * |
| 1566 | * This convenience API allows consumers to enable multiple regulator |
| 1567 | * clients in a single API call. If any consumers cannot be enabled |
| 1568 | * then any others that were enabled will be disabled again prior to |
| 1569 | * return. |
| 1570 | */ |
| 1571 | int regulator_bulk_enable(int num_consumers, |
| 1572 | struct regulator_bulk_data *consumers) |
| 1573 | { |
| 1574 | int i; |
| 1575 | int ret; |
| 1576 | |
| 1577 | for (i = 0; i < num_consumers; i++) { |
| 1578 | ret = regulator_enable(consumers[i].consumer); |
| 1579 | if (ret != 0) |
| 1580 | goto err; |
| 1581 | } |
| 1582 | |
| 1583 | return 0; |
| 1584 | |
| 1585 | err: |
| 1586 | printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply); |
| 1587 | for (i = 0; i < num_consumers; i++) |
| 1588 | regulator_disable(consumers[i].consumer); |
| 1589 | |
| 1590 | return ret; |
| 1591 | } |
| 1592 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); |
| 1593 | |
| 1594 | /** |
| 1595 | * regulator_bulk_disable - disable multiple regulator consumers |
| 1596 | * |
| 1597 | * @num_consumers: Number of consumers |
| 1598 | * @consumers: Consumer data; clients are stored here. |
| 1599 | * @return 0 on success, an errno on failure |
| 1600 | * |
| 1601 | * This convenience API allows consumers to disable multiple regulator |
| 1602 | * clients in a single API call. If any consumers cannot be enabled |
| 1603 | * then any others that were disabled will be disabled again prior to |
| 1604 | * return. |
| 1605 | */ |
| 1606 | int regulator_bulk_disable(int num_consumers, |
| 1607 | struct regulator_bulk_data *consumers) |
| 1608 | { |
| 1609 | int i; |
| 1610 | int ret; |
| 1611 | |
| 1612 | for (i = 0; i < num_consumers; i++) { |
| 1613 | ret = regulator_disable(consumers[i].consumer); |
| 1614 | if (ret != 0) |
| 1615 | goto err; |
| 1616 | } |
| 1617 | |
| 1618 | return 0; |
| 1619 | |
| 1620 | err: |
| 1621 | printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply); |
| 1622 | for (i = 0; i < num_consumers; i++) |
| 1623 | regulator_enable(consumers[i].consumer); |
| 1624 | |
| 1625 | return ret; |
| 1626 | } |
| 1627 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); |
| 1628 | |
| 1629 | /** |
| 1630 | * regulator_bulk_free - free multiple regulator consumers |
| 1631 | * |
| 1632 | * @num_consumers: Number of consumers |
| 1633 | * @consumers: Consumer data; clients are stored here. |
| 1634 | * |
| 1635 | * This convenience API allows consumers to free multiple regulator |
| 1636 | * clients in a single API call. |
| 1637 | */ |
| 1638 | void regulator_bulk_free(int num_consumers, |
| 1639 | struct regulator_bulk_data *consumers) |
| 1640 | { |
| 1641 | int i; |
| 1642 | |
| 1643 | for (i = 0; i < num_consumers; i++) { |
| 1644 | regulator_put(consumers[i].consumer); |
| 1645 | consumers[i].consumer = NULL; |
| 1646 | } |
| 1647 | } |
| 1648 | EXPORT_SYMBOL_GPL(regulator_bulk_free); |
| 1649 | |
| 1650 | /** |
| 1651 | * regulator_notifier_call_chain - call regulator event notifier |
| 1652 | * @regulator: regulator source |
| 1653 | * @event: notifier block |
| 1654 | * @data: |
| 1655 | * |
| 1656 | * Called by regulator drivers to notify clients a regulator event has |
| 1657 | * occurred. We also notify regulator clients downstream. |
| 1658 | */ |
| 1659 | int regulator_notifier_call_chain(struct regulator_dev *rdev, |
| 1660 | unsigned long event, void *data) |
| 1661 | { |
| 1662 | _notifier_call_chain(rdev, event, data); |
| 1663 | return NOTIFY_DONE; |
| 1664 | |
| 1665 | } |
| 1666 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); |
| 1667 | |
| 1668 | /** |
| 1669 | * regulator_register - register regulator |
| 1670 | * @regulator: regulator source |
| 1671 | * @reg_data: private regulator data |
| 1672 | * |
| 1673 | * Called by regulator drivers to register a regulator. |
| 1674 | * Returns 0 on success. |
| 1675 | */ |
| 1676 | struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1677 | struct device *dev, void *driver_data) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1678 | { |
| 1679 | static atomic_t regulator_no = ATOMIC_INIT(0); |
| 1680 | struct regulator_dev *rdev; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1681 | struct regulator_init_data *init_data = dev->platform_data; |
| 1682 | int ret, i; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1683 | |
| 1684 | if (regulator_desc == NULL) |
| 1685 | return ERR_PTR(-EINVAL); |
| 1686 | |
| 1687 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) |
| 1688 | return ERR_PTR(-EINVAL); |
| 1689 | |
| 1690 | if (!regulator_desc->type == REGULATOR_VOLTAGE && |
| 1691 | !regulator_desc->type == REGULATOR_CURRENT) |
| 1692 | return ERR_PTR(-EINVAL); |
| 1693 | |
| 1694 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
| 1695 | if (rdev == NULL) |
| 1696 | return ERR_PTR(-ENOMEM); |
| 1697 | |
| 1698 | mutex_lock(®ulator_list_mutex); |
| 1699 | |
| 1700 | mutex_init(&rdev->mutex); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1701 | rdev->reg_data = driver_data; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1702 | rdev->owner = regulator_desc->owner; |
| 1703 | rdev->desc = regulator_desc; |
| 1704 | INIT_LIST_HEAD(&rdev->consumer_list); |
| 1705 | INIT_LIST_HEAD(&rdev->supply_list); |
| 1706 | INIT_LIST_HEAD(&rdev->list); |
| 1707 | INIT_LIST_HEAD(&rdev->slist); |
| 1708 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); |
| 1709 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1710 | /* preform any regulator specific init */ |
| 1711 | if (init_data->regulator_init) { |
| 1712 | ret = init_data->regulator_init(rdev->reg_data); |
| 1713 | if (ret < 0) { |
| 1714 | kfree(rdev); |
| 1715 | rdev = ERR_PTR(ret); |
| 1716 | goto out; |
| 1717 | } |
| 1718 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1719 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1720 | /* set regulator constraints */ |
| 1721 | ret = set_machine_constraints(rdev, &init_data->constraints); |
| 1722 | if (ret < 0) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1723 | kfree(rdev); |
| 1724 | rdev = ERR_PTR(ret); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1725 | goto out; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1726 | } |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1727 | |
| 1728 | /* register with sysfs */ |
| 1729 | rdev->dev.class = ®ulator_class; |
| 1730 | rdev->dev.parent = dev; |
| 1731 | snprintf(rdev->dev.bus_id, sizeof(rdev->dev.bus_id), |
| 1732 | "regulator.%d", atomic_inc_return(®ulator_no) - 1); |
| 1733 | ret = device_register(&rdev->dev); |
| 1734 | if (ret != 0) { |
| 1735 | kfree(rdev); |
| 1736 | rdev = ERR_PTR(ret); |
| 1737 | goto out; |
| 1738 | } |
| 1739 | |
| 1740 | dev_set_drvdata(&rdev->dev, rdev); |
| 1741 | |
| 1742 | /* set supply regulator if it exists */ |
| 1743 | if (init_data->supply_regulator_dev) { |
| 1744 | ret = set_supply(rdev, |
| 1745 | dev_get_drvdata(init_data->supply_regulator_dev)); |
| 1746 | if (ret < 0) { |
| 1747 | device_unregister(&rdev->dev); |
| 1748 | kfree(rdev); |
| 1749 | rdev = ERR_PTR(ret); |
| 1750 | goto out; |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | /* add consumers devices */ |
| 1755 | for (i = 0; i < init_data->num_consumer_supplies; i++) { |
| 1756 | ret = set_consumer_device_supply(rdev, |
| 1757 | init_data->consumer_supplies[i].dev, |
| 1758 | init_data->consumer_supplies[i].supply); |
| 1759 | if (ret < 0) { |
| 1760 | for (--i; i >= 0; i--) |
| 1761 | unset_consumer_device_supply(rdev, |
| 1762 | init_data->consumer_supplies[i].dev); |
| 1763 | device_unregister(&rdev->dev); |
| 1764 | kfree(rdev); |
| 1765 | rdev = ERR_PTR(ret); |
| 1766 | goto out; |
| 1767 | } |
| 1768 | } |
| 1769 | |
| 1770 | list_add(&rdev->list, ®ulator_list); |
| 1771 | out: |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1772 | mutex_unlock(®ulator_list_mutex); |
| 1773 | return rdev; |
| 1774 | } |
| 1775 | EXPORT_SYMBOL_GPL(regulator_register); |
| 1776 | |
| 1777 | /** |
| 1778 | * regulator_unregister - unregister regulator |
| 1779 | * @regulator: regulator source |
| 1780 | * |
| 1781 | * Called by regulator drivers to unregister a regulator. |
| 1782 | */ |
| 1783 | void regulator_unregister(struct regulator_dev *rdev) |
| 1784 | { |
| 1785 | if (rdev == NULL) |
| 1786 | return; |
| 1787 | |
| 1788 | mutex_lock(®ulator_list_mutex); |
| 1789 | list_del(&rdev->list); |
| 1790 | if (rdev->supply) |
| 1791 | sysfs_remove_link(&rdev->dev.kobj, "supply"); |
| 1792 | device_unregister(&rdev->dev); |
| 1793 | mutex_unlock(®ulator_list_mutex); |
| 1794 | } |
| 1795 | EXPORT_SYMBOL_GPL(regulator_unregister); |
| 1796 | |
| 1797 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1798 | * regulator_suspend_prepare: prepare regulators for system wide suspend |
| 1799 | * @state: system suspend state |
| 1800 | * |
| 1801 | * Configure each regulator with it's suspend operating parameters for state. |
| 1802 | * This will usually be called by machine suspend code prior to supending. |
| 1803 | */ |
| 1804 | int regulator_suspend_prepare(suspend_state_t state) |
| 1805 | { |
| 1806 | struct regulator_dev *rdev; |
| 1807 | int ret = 0; |
| 1808 | |
| 1809 | /* ON is handled by regulator active state */ |
| 1810 | if (state == PM_SUSPEND_ON) |
| 1811 | return -EINVAL; |
| 1812 | |
| 1813 | mutex_lock(®ulator_list_mutex); |
| 1814 | list_for_each_entry(rdev, ®ulator_list, list) { |
| 1815 | |
| 1816 | mutex_lock(&rdev->mutex); |
| 1817 | ret = suspend_prepare(rdev, state); |
| 1818 | mutex_unlock(&rdev->mutex); |
| 1819 | |
| 1820 | if (ret < 0) { |
| 1821 | printk(KERN_ERR "%s: failed to prepare %s\n", |
| 1822 | __func__, rdev->desc->name); |
| 1823 | goto out; |
| 1824 | } |
| 1825 | } |
| 1826 | out: |
| 1827 | mutex_unlock(®ulator_list_mutex); |
| 1828 | return ret; |
| 1829 | } |
| 1830 | EXPORT_SYMBOL_GPL(regulator_suspend_prepare); |
| 1831 | |
| 1832 | /** |
| 1833 | * rdev_get_drvdata - get rdev regulator driver data |
| 1834 | * @regulator: regulator |
| 1835 | * |
| 1836 | * Get rdev regulator driver private data. This call can be used in the |
| 1837 | * regulator driver context. |
| 1838 | */ |
| 1839 | void *rdev_get_drvdata(struct regulator_dev *rdev) |
| 1840 | { |
| 1841 | return rdev->reg_data; |
| 1842 | } |
| 1843 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); |
| 1844 | |
| 1845 | /** |
| 1846 | * regulator_get_drvdata - get regulator driver data |
| 1847 | * @regulator: regulator |
| 1848 | * |
| 1849 | * Get regulator driver private data. This call can be used in the consumer |
| 1850 | * driver context when non API regulator specific functions need to be called. |
| 1851 | */ |
| 1852 | void *regulator_get_drvdata(struct regulator *regulator) |
| 1853 | { |
| 1854 | return regulator->rdev->reg_data; |
| 1855 | } |
| 1856 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); |
| 1857 | |
| 1858 | /** |
| 1859 | * regulator_set_drvdata - set regulator driver data |
| 1860 | * @regulator: regulator |
| 1861 | * @data: data |
| 1862 | */ |
| 1863 | void regulator_set_drvdata(struct regulator *regulator, void *data) |
| 1864 | { |
| 1865 | regulator->rdev->reg_data = data; |
| 1866 | } |
| 1867 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); |
| 1868 | |
| 1869 | /** |
| 1870 | * regulator_get_id - get regulator ID |
| 1871 | * @regulator: regulator |
| 1872 | */ |
| 1873 | int rdev_get_id(struct regulator_dev *rdev) |
| 1874 | { |
| 1875 | return rdev->desc->id; |
| 1876 | } |
| 1877 | EXPORT_SYMBOL_GPL(rdev_get_id); |
| 1878 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1879 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
| 1880 | { |
| 1881 | return &rdev->dev; |
| 1882 | } |
| 1883 | EXPORT_SYMBOL_GPL(rdev_get_dev); |
| 1884 | |
| 1885 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) |
| 1886 | { |
| 1887 | return reg_init_data->driver_data; |
| 1888 | } |
| 1889 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); |
| 1890 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1891 | static int __init regulator_init(void) |
| 1892 | { |
| 1893 | printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION); |
| 1894 | return class_register(®ulator_class); |
| 1895 | } |
| 1896 | |
| 1897 | /* init early to allow our consumers to complete system booting */ |
| 1898 | core_initcall(regulator_init); |