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