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