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