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