core.c revision c8520b4c5d25eb7b8b54f1ae9ba7da71375f2b2c
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/gpio.h> 27#include <linux/of.h> 28#include <linux/regmap.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 57static struct dentry *debugfs_root; 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 unsigned int always_on:1; 80 unsigned int bypass:1; 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 struct dentry *debugfs; 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/** 114 * of_get_regulator - get a regulator device node based on supply name 115 * @dev: Device pointer for the consumer (of regulator) device 116 * @supply: regulator supply name 117 * 118 * Extract the regulator device node corresponding to the supply name. 119 * retruns the device node corresponding to the regulator if found, else 120 * returns NULL. 121 */ 122static struct device_node *of_get_regulator(struct device *dev, const char *supply) 123{ 124 struct device_node *regnode = NULL; 125 char prop_name[32]; /* 32 is max size of property name */ 126 127 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); 128 129 snprintf(prop_name, 32, "%s-supply", supply); 130 regnode = of_parse_phandle(dev->of_node, prop_name, 0); 131 132 if (!regnode) { 133 dev_dbg(dev, "Looking up %s property in node %s failed", 134 prop_name, dev->of_node->full_name); 135 return NULL; 136 } 137 return regnode; 138} 139 140static int _regulator_can_change_status(struct regulator_dev *rdev) 141{ 142 if (!rdev->constraints) 143 return 0; 144 145 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS) 146 return 1; 147 else 148 return 0; 149} 150 151/* Platform voltage constraint check */ 152static int regulator_check_voltage(struct regulator_dev *rdev, 153 int *min_uV, int *max_uV) 154{ 155 BUG_ON(*min_uV > *max_uV); 156 157 if (!rdev->constraints) { 158 rdev_err(rdev, "no constraints\n"); 159 return -ENODEV; 160 } 161 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { 162 rdev_err(rdev, "operation not allowed\n"); 163 return -EPERM; 164 } 165 166 if (*max_uV > rdev->constraints->max_uV) 167 *max_uV = rdev->constraints->max_uV; 168 if (*min_uV < rdev->constraints->min_uV) 169 *min_uV = rdev->constraints->min_uV; 170 171 if (*min_uV > *max_uV) { 172 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", 173 *min_uV, *max_uV); 174 return -EINVAL; 175 } 176 177 return 0; 178} 179 180/* Make sure we select a voltage that suits the needs of all 181 * regulator consumers 182 */ 183static int regulator_check_consumers(struct regulator_dev *rdev, 184 int *min_uV, int *max_uV) 185{ 186 struct regulator *regulator; 187 188 list_for_each_entry(regulator, &rdev->consumer_list, list) { 189 /* 190 * Assume consumers that didn't say anything are OK 191 * with anything in the constraint range. 192 */ 193 if (!regulator->min_uV && !regulator->max_uV) 194 continue; 195 196 if (*max_uV > regulator->max_uV) 197 *max_uV = regulator->max_uV; 198 if (*min_uV < regulator->min_uV) 199 *min_uV = regulator->min_uV; 200 } 201 202 if (*min_uV > *max_uV) { 203 dev_err(regulator->dev, "Restricting voltage, %u-%uuV\n", 204 regulator->min_uV, regulator->max_uV); 205 return -EINVAL; 206 } 207 208 return 0; 209} 210 211/* current constraint check */ 212static int regulator_check_current_limit(struct regulator_dev *rdev, 213 int *min_uA, int *max_uA) 214{ 215 BUG_ON(*min_uA > *max_uA); 216 217 if (!rdev->constraints) { 218 rdev_err(rdev, "no constraints\n"); 219 return -ENODEV; 220 } 221 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) { 222 rdev_err(rdev, "operation not allowed\n"); 223 return -EPERM; 224 } 225 226 if (*max_uA > rdev->constraints->max_uA) 227 *max_uA = rdev->constraints->max_uA; 228 if (*min_uA < rdev->constraints->min_uA) 229 *min_uA = rdev->constraints->min_uA; 230 231 if (*min_uA > *max_uA) { 232 rdev_err(rdev, "unsupportable current range: %d-%duA\n", 233 *min_uA, *max_uA); 234 return -EINVAL; 235 } 236 237 return 0; 238} 239 240/* operating mode constraint check */ 241static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode) 242{ 243 switch (*mode) { 244 case REGULATOR_MODE_FAST: 245 case REGULATOR_MODE_NORMAL: 246 case REGULATOR_MODE_IDLE: 247 case REGULATOR_MODE_STANDBY: 248 break; 249 default: 250 rdev_err(rdev, "invalid mode %x specified\n", *mode); 251 return -EINVAL; 252 } 253 254 if (!rdev->constraints) { 255 rdev_err(rdev, "no constraints\n"); 256 return -ENODEV; 257 } 258 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) { 259 rdev_err(rdev, "operation not allowed\n"); 260 return -EPERM; 261 } 262 263 /* The modes are bitmasks, the most power hungry modes having 264 * the lowest values. If the requested mode isn't supported 265 * try higher modes. */ 266 while (*mode) { 267 if (rdev->constraints->valid_modes_mask & *mode) 268 return 0; 269 *mode /= 2; 270 } 271 272 return -EINVAL; 273} 274 275/* dynamic regulator mode switching constraint check */ 276static int regulator_check_drms(struct regulator_dev *rdev) 277{ 278 if (!rdev->constraints) { 279 rdev_err(rdev, "no constraints\n"); 280 return -ENODEV; 281 } 282 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) { 283 rdev_err(rdev, "operation not allowed\n"); 284 return -EPERM; 285 } 286 return 0; 287} 288 289static ssize_t regulator_uV_show(struct device *dev, 290 struct device_attribute *attr, char *buf) 291{ 292 struct regulator_dev *rdev = dev_get_drvdata(dev); 293 ssize_t ret; 294 295 mutex_lock(&rdev->mutex); 296 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); 297 mutex_unlock(&rdev->mutex); 298 299 return ret; 300} 301static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); 302 303static ssize_t regulator_uA_show(struct device *dev, 304 struct device_attribute *attr, char *buf) 305{ 306 struct regulator_dev *rdev = dev_get_drvdata(dev); 307 308 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); 309} 310static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); 311 312static ssize_t regulator_name_show(struct device *dev, 313 struct device_attribute *attr, char *buf) 314{ 315 struct regulator_dev *rdev = dev_get_drvdata(dev); 316 317 return sprintf(buf, "%s\n", rdev_get_name(rdev)); 318} 319 320static ssize_t regulator_print_opmode(char *buf, int mode) 321{ 322 switch (mode) { 323 case REGULATOR_MODE_FAST: 324 return sprintf(buf, "fast\n"); 325 case REGULATOR_MODE_NORMAL: 326 return sprintf(buf, "normal\n"); 327 case REGULATOR_MODE_IDLE: 328 return sprintf(buf, "idle\n"); 329 case REGULATOR_MODE_STANDBY: 330 return sprintf(buf, "standby\n"); 331 } 332 return sprintf(buf, "unknown\n"); 333} 334 335static ssize_t regulator_opmode_show(struct device *dev, 336 struct device_attribute *attr, char *buf) 337{ 338 struct regulator_dev *rdev = dev_get_drvdata(dev); 339 340 return regulator_print_opmode(buf, _regulator_get_mode(rdev)); 341} 342static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); 343 344static ssize_t regulator_print_state(char *buf, int state) 345{ 346 if (state > 0) 347 return sprintf(buf, "enabled\n"); 348 else if (state == 0) 349 return sprintf(buf, "disabled\n"); 350 else 351 return sprintf(buf, "unknown\n"); 352} 353 354static ssize_t regulator_state_show(struct device *dev, 355 struct device_attribute *attr, char *buf) 356{ 357 struct regulator_dev *rdev = dev_get_drvdata(dev); 358 ssize_t ret; 359 360 mutex_lock(&rdev->mutex); 361 ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); 362 mutex_unlock(&rdev->mutex); 363 364 return ret; 365} 366static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); 367 368static ssize_t regulator_status_show(struct device *dev, 369 struct device_attribute *attr, char *buf) 370{ 371 struct regulator_dev *rdev = dev_get_drvdata(dev); 372 int status; 373 char *label; 374 375 status = rdev->desc->ops->get_status(rdev); 376 if (status < 0) 377 return status; 378 379 switch (status) { 380 case REGULATOR_STATUS_OFF: 381 label = "off"; 382 break; 383 case REGULATOR_STATUS_ON: 384 label = "on"; 385 break; 386 case REGULATOR_STATUS_ERROR: 387 label = "error"; 388 break; 389 case REGULATOR_STATUS_FAST: 390 label = "fast"; 391 break; 392 case REGULATOR_STATUS_NORMAL: 393 label = "normal"; 394 break; 395 case REGULATOR_STATUS_IDLE: 396 label = "idle"; 397 break; 398 case REGULATOR_STATUS_STANDBY: 399 label = "standby"; 400 break; 401 case REGULATOR_STATUS_BYPASS: 402 label = "bypass"; 403 break; 404 case REGULATOR_STATUS_UNDEFINED: 405 label = "undefined"; 406 break; 407 default: 408 return -ERANGE; 409 } 410 411 return sprintf(buf, "%s\n", label); 412} 413static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); 414 415static ssize_t regulator_min_uA_show(struct device *dev, 416 struct device_attribute *attr, char *buf) 417{ 418 struct regulator_dev *rdev = dev_get_drvdata(dev); 419 420 if (!rdev->constraints) 421 return sprintf(buf, "constraint not defined\n"); 422 423 return sprintf(buf, "%d\n", rdev->constraints->min_uA); 424} 425static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); 426 427static ssize_t regulator_max_uA_show(struct device *dev, 428 struct device_attribute *attr, char *buf) 429{ 430 struct regulator_dev *rdev = dev_get_drvdata(dev); 431 432 if (!rdev->constraints) 433 return sprintf(buf, "constraint not defined\n"); 434 435 return sprintf(buf, "%d\n", rdev->constraints->max_uA); 436} 437static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); 438 439static ssize_t regulator_min_uV_show(struct device *dev, 440 struct device_attribute *attr, char *buf) 441{ 442 struct regulator_dev *rdev = dev_get_drvdata(dev); 443 444 if (!rdev->constraints) 445 return sprintf(buf, "constraint not defined\n"); 446 447 return sprintf(buf, "%d\n", rdev->constraints->min_uV); 448} 449static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); 450 451static ssize_t regulator_max_uV_show(struct device *dev, 452 struct device_attribute *attr, char *buf) 453{ 454 struct regulator_dev *rdev = dev_get_drvdata(dev); 455 456 if (!rdev->constraints) 457 return sprintf(buf, "constraint not defined\n"); 458 459 return sprintf(buf, "%d\n", rdev->constraints->max_uV); 460} 461static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); 462 463static ssize_t regulator_total_uA_show(struct device *dev, 464 struct device_attribute *attr, char *buf) 465{ 466 struct regulator_dev *rdev = dev_get_drvdata(dev); 467 struct regulator *regulator; 468 int uA = 0; 469 470 mutex_lock(&rdev->mutex); 471 list_for_each_entry(regulator, &rdev->consumer_list, list) 472 uA += regulator->uA_load; 473 mutex_unlock(&rdev->mutex); 474 return sprintf(buf, "%d\n", uA); 475} 476static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); 477 478static ssize_t regulator_num_users_show(struct device *dev, 479 struct device_attribute *attr, char *buf) 480{ 481 struct regulator_dev *rdev = dev_get_drvdata(dev); 482 return sprintf(buf, "%d\n", rdev->use_count); 483} 484 485static ssize_t regulator_type_show(struct device *dev, 486 struct device_attribute *attr, char *buf) 487{ 488 struct regulator_dev *rdev = dev_get_drvdata(dev); 489 490 switch (rdev->desc->type) { 491 case REGULATOR_VOLTAGE: 492 return sprintf(buf, "voltage\n"); 493 case REGULATOR_CURRENT: 494 return sprintf(buf, "current\n"); 495 } 496 return sprintf(buf, "unknown\n"); 497} 498 499static ssize_t regulator_suspend_mem_uV_show(struct device *dev, 500 struct device_attribute *attr, char *buf) 501{ 502 struct regulator_dev *rdev = dev_get_drvdata(dev); 503 504 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); 505} 506static DEVICE_ATTR(suspend_mem_microvolts, 0444, 507 regulator_suspend_mem_uV_show, NULL); 508 509static ssize_t regulator_suspend_disk_uV_show(struct device *dev, 510 struct device_attribute *attr, char *buf) 511{ 512 struct regulator_dev *rdev = dev_get_drvdata(dev); 513 514 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); 515} 516static DEVICE_ATTR(suspend_disk_microvolts, 0444, 517 regulator_suspend_disk_uV_show, NULL); 518 519static ssize_t regulator_suspend_standby_uV_show(struct device *dev, 520 struct device_attribute *attr, char *buf) 521{ 522 struct regulator_dev *rdev = dev_get_drvdata(dev); 523 524 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); 525} 526static DEVICE_ATTR(suspend_standby_microvolts, 0444, 527 regulator_suspend_standby_uV_show, NULL); 528 529static ssize_t regulator_suspend_mem_mode_show(struct device *dev, 530 struct device_attribute *attr, char *buf) 531{ 532 struct regulator_dev *rdev = dev_get_drvdata(dev); 533 534 return regulator_print_opmode(buf, 535 rdev->constraints->state_mem.mode); 536} 537static DEVICE_ATTR(suspend_mem_mode, 0444, 538 regulator_suspend_mem_mode_show, NULL); 539 540static ssize_t regulator_suspend_disk_mode_show(struct device *dev, 541 struct device_attribute *attr, char *buf) 542{ 543 struct regulator_dev *rdev = dev_get_drvdata(dev); 544 545 return regulator_print_opmode(buf, 546 rdev->constraints->state_disk.mode); 547} 548static DEVICE_ATTR(suspend_disk_mode, 0444, 549 regulator_suspend_disk_mode_show, NULL); 550 551static ssize_t regulator_suspend_standby_mode_show(struct device *dev, 552 struct device_attribute *attr, char *buf) 553{ 554 struct regulator_dev *rdev = dev_get_drvdata(dev); 555 556 return regulator_print_opmode(buf, 557 rdev->constraints->state_standby.mode); 558} 559static DEVICE_ATTR(suspend_standby_mode, 0444, 560 regulator_suspend_standby_mode_show, NULL); 561 562static ssize_t regulator_suspend_mem_state_show(struct device *dev, 563 struct device_attribute *attr, char *buf) 564{ 565 struct regulator_dev *rdev = dev_get_drvdata(dev); 566 567 return regulator_print_state(buf, 568 rdev->constraints->state_mem.enabled); 569} 570static DEVICE_ATTR(suspend_mem_state, 0444, 571 regulator_suspend_mem_state_show, NULL); 572 573static ssize_t regulator_suspend_disk_state_show(struct device *dev, 574 struct device_attribute *attr, char *buf) 575{ 576 struct regulator_dev *rdev = dev_get_drvdata(dev); 577 578 return regulator_print_state(buf, 579 rdev->constraints->state_disk.enabled); 580} 581static DEVICE_ATTR(suspend_disk_state, 0444, 582 regulator_suspend_disk_state_show, NULL); 583 584static ssize_t regulator_suspend_standby_state_show(struct device *dev, 585 struct device_attribute *attr, char *buf) 586{ 587 struct regulator_dev *rdev = dev_get_drvdata(dev); 588 589 return regulator_print_state(buf, 590 rdev->constraints->state_standby.enabled); 591} 592static DEVICE_ATTR(suspend_standby_state, 0444, 593 regulator_suspend_standby_state_show, NULL); 594 595static ssize_t regulator_bypass_show(struct device *dev, 596 struct device_attribute *attr, char *buf) 597{ 598 struct regulator_dev *rdev = dev_get_drvdata(dev); 599 const char *report; 600 bool bypass; 601 int ret; 602 603 ret = rdev->desc->ops->get_bypass(rdev, &bypass); 604 605 if (ret != 0) 606 report = "unknown"; 607 else if (bypass) 608 report = "enabled"; 609 else 610 report = "disabled"; 611 612 return sprintf(buf, "%s\n", report); 613} 614static DEVICE_ATTR(bypass, 0444, 615 regulator_bypass_show, NULL); 616 617/* 618 * These are the only attributes are present for all regulators. 619 * Other attributes are a function of regulator functionality. 620 */ 621static struct device_attribute regulator_dev_attrs[] = { 622 __ATTR(name, 0444, regulator_name_show, NULL), 623 __ATTR(num_users, 0444, regulator_num_users_show, NULL), 624 __ATTR(type, 0444, regulator_type_show, NULL), 625 __ATTR_NULL, 626}; 627 628static void regulator_dev_release(struct device *dev) 629{ 630 struct regulator_dev *rdev = dev_get_drvdata(dev); 631 kfree(rdev); 632} 633 634static struct class regulator_class = { 635 .name = "regulator", 636 .dev_release = regulator_dev_release, 637 .dev_attrs = regulator_dev_attrs, 638}; 639 640/* Calculate the new optimum regulator operating mode based on the new total 641 * consumer load. All locks held by caller */ 642static void drms_uA_update(struct regulator_dev *rdev) 643{ 644 struct regulator *sibling; 645 int current_uA = 0, output_uV, input_uV, err; 646 unsigned int mode; 647 648 err = regulator_check_drms(rdev); 649 if (err < 0 || !rdev->desc->ops->get_optimum_mode || 650 (!rdev->desc->ops->get_voltage && 651 !rdev->desc->ops->get_voltage_sel) || 652 !rdev->desc->ops->set_mode) 653 return; 654 655 /* get output voltage */ 656 output_uV = _regulator_get_voltage(rdev); 657 if (output_uV <= 0) 658 return; 659 660 /* get input voltage */ 661 input_uV = 0; 662 if (rdev->supply) 663 input_uV = regulator_get_voltage(rdev->supply); 664 if (input_uV <= 0) 665 input_uV = rdev->constraints->input_uV; 666 if (input_uV <= 0) 667 return; 668 669 /* calc total requested load */ 670 list_for_each_entry(sibling, &rdev->consumer_list, list) 671 current_uA += sibling->uA_load; 672 673 /* now get the optimum mode for our new total regulator load */ 674 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, 675 output_uV, current_uA); 676 677 /* check the new mode is allowed */ 678 err = regulator_mode_constrain(rdev, &mode); 679 if (err == 0) 680 rdev->desc->ops->set_mode(rdev, mode); 681} 682 683static int suspend_set_state(struct regulator_dev *rdev, 684 struct regulator_state *rstate) 685{ 686 int ret = 0; 687 688 /* If we have no suspend mode configration don't set anything; 689 * only warn if the driver implements set_suspend_voltage or 690 * set_suspend_mode callback. 691 */ 692 if (!rstate->enabled && !rstate->disabled) { 693 if (rdev->desc->ops->set_suspend_voltage || 694 rdev->desc->ops->set_suspend_mode) 695 rdev_warn(rdev, "No configuration\n"); 696 return 0; 697 } 698 699 if (rstate->enabled && rstate->disabled) { 700 rdev_err(rdev, "invalid configuration\n"); 701 return -EINVAL; 702 } 703 704 if (rstate->enabled && rdev->desc->ops->set_suspend_enable) 705 ret = rdev->desc->ops->set_suspend_enable(rdev); 706 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable) 707 ret = rdev->desc->ops->set_suspend_disable(rdev); 708 else /* OK if set_suspend_enable or set_suspend_disable is NULL */ 709 ret = 0; 710 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 if (!count) 810 sprintf(buf, "no parameters"); 811 812 rdev_info(rdev, "%s\n", buf); 813 814 if ((constraints->min_uV != constraints->max_uV) && 815 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) 816 rdev_warn(rdev, 817 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n"); 818} 819 820static int machine_constraints_voltage(struct regulator_dev *rdev, 821 struct regulation_constraints *constraints) 822{ 823 struct regulator_ops *ops = rdev->desc->ops; 824 int ret; 825 826 /* do we need to apply the constraint voltage */ 827 if (rdev->constraints->apply_uV && 828 rdev->constraints->min_uV == rdev->constraints->max_uV) { 829 ret = _regulator_do_set_voltage(rdev, 830 rdev->constraints->min_uV, 831 rdev->constraints->max_uV); 832 if (ret < 0) { 833 rdev_err(rdev, "failed to apply %duV constraint\n", 834 rdev->constraints->min_uV); 835 return ret; 836 } 837 } 838 839 /* constrain machine-level voltage specs to fit 840 * the actual range supported by this regulator. 841 */ 842 if (ops->list_voltage && rdev->desc->n_voltages) { 843 int count = rdev->desc->n_voltages; 844 int i; 845 int min_uV = INT_MAX; 846 int max_uV = INT_MIN; 847 int cmin = constraints->min_uV; 848 int cmax = constraints->max_uV; 849 850 /* it's safe to autoconfigure fixed-voltage supplies 851 and the constraints are used by list_voltage. */ 852 if (count == 1 && !cmin) { 853 cmin = 1; 854 cmax = INT_MAX; 855 constraints->min_uV = cmin; 856 constraints->max_uV = cmax; 857 } 858 859 /* voltage constraints are optional */ 860 if ((cmin == 0) && (cmax == 0)) 861 return 0; 862 863 /* else require explicit machine-level constraints */ 864 if (cmin <= 0 || cmax <= 0 || cmax < cmin) { 865 rdev_err(rdev, "invalid voltage constraints\n"); 866 return -EINVAL; 867 } 868 869 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ 870 for (i = 0; i < count; i++) { 871 int value; 872 873 value = ops->list_voltage(rdev, i); 874 if (value <= 0) 875 continue; 876 877 /* maybe adjust [min_uV..max_uV] */ 878 if (value >= cmin && value < min_uV) 879 min_uV = value; 880 if (value <= cmax && value > max_uV) 881 max_uV = value; 882 } 883 884 /* final: [min_uV..max_uV] valid iff constraints valid */ 885 if (max_uV < min_uV) { 886 rdev_err(rdev, 887 "unsupportable voltage constraints %u-%uuV\n", 888 min_uV, max_uV); 889 return -EINVAL; 890 } 891 892 /* use regulator's subset of machine constraints */ 893 if (constraints->min_uV < min_uV) { 894 rdev_dbg(rdev, "override min_uV, %d -> %d\n", 895 constraints->min_uV, min_uV); 896 constraints->min_uV = min_uV; 897 } 898 if (constraints->max_uV > max_uV) { 899 rdev_dbg(rdev, "override max_uV, %d -> %d\n", 900 constraints->max_uV, max_uV); 901 constraints->max_uV = max_uV; 902 } 903 } 904 905 return 0; 906} 907 908/** 909 * set_machine_constraints - sets regulator constraints 910 * @rdev: regulator source 911 * @constraints: constraints to apply 912 * 913 * Allows platform initialisation code to define and constrain 914 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: 915 * Constraints *must* be set by platform code in order for some 916 * regulator operations to proceed i.e. set_voltage, set_current_limit, 917 * set_mode. 918 */ 919static int set_machine_constraints(struct regulator_dev *rdev, 920 const struct regulation_constraints *constraints) 921{ 922 int ret = 0; 923 struct regulator_ops *ops = rdev->desc->ops; 924 925 if (constraints) 926 rdev->constraints = kmemdup(constraints, sizeof(*constraints), 927 GFP_KERNEL); 928 else 929 rdev->constraints = kzalloc(sizeof(*constraints), 930 GFP_KERNEL); 931 if (!rdev->constraints) 932 return -ENOMEM; 933 934 ret = machine_constraints_voltage(rdev, rdev->constraints); 935 if (ret != 0) 936 goto out; 937 938 /* do we need to setup our suspend state */ 939 if (rdev->constraints->initial_state) { 940 ret = suspend_prepare(rdev, rdev->constraints->initial_state); 941 if (ret < 0) { 942 rdev_err(rdev, "failed to set suspend state\n"); 943 goto out; 944 } 945 } 946 947 if (rdev->constraints->initial_mode) { 948 if (!ops->set_mode) { 949 rdev_err(rdev, "no set_mode operation\n"); 950 ret = -EINVAL; 951 goto out; 952 } 953 954 ret = ops->set_mode(rdev, rdev->constraints->initial_mode); 955 if (ret < 0) { 956 rdev_err(rdev, "failed to set initial mode: %d\n", ret); 957 goto out; 958 } 959 } 960 961 /* If the constraints say the regulator should be on at this point 962 * and we have control then make sure it is enabled. 963 */ 964 if ((rdev->constraints->always_on || rdev->constraints->boot_on) && 965 ops->enable) { 966 ret = ops->enable(rdev); 967 if (ret < 0) { 968 rdev_err(rdev, "failed to enable\n"); 969 goto out; 970 } 971 } 972 973 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) { 974 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); 975 if (ret < 0) { 976 rdev_err(rdev, "failed to set ramp_delay\n"); 977 goto out; 978 } 979 } 980 981 print_constraints(rdev); 982 return 0; 983out: 984 kfree(rdev->constraints); 985 rdev->constraints = NULL; 986 return ret; 987} 988 989/** 990 * set_supply - set regulator supply regulator 991 * @rdev: regulator name 992 * @supply_rdev: supply regulator name 993 * 994 * Called by platform initialisation code to set the supply regulator for this 995 * regulator. This ensures that a regulators supply will also be enabled by the 996 * core if it's child is enabled. 997 */ 998static int set_supply(struct regulator_dev *rdev, 999 struct regulator_dev *supply_rdev) 1000{ 1001 int err; 1002 1003 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev)); 1004 1005 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); 1006 if (rdev->supply == NULL) { 1007 err = -ENOMEM; 1008 return err; 1009 } 1010 supply_rdev->open_count++; 1011 1012 return 0; 1013} 1014 1015/** 1016 * set_consumer_device_supply - Bind a regulator to a symbolic supply 1017 * @rdev: regulator source 1018 * @consumer_dev_name: dev_name() string for device supply applies to 1019 * @supply: symbolic name for supply 1020 * 1021 * Allows platform initialisation code to map physical regulator 1022 * sources to symbolic names for supplies for use by devices. Devices 1023 * should use these symbolic names to request regulators, avoiding the 1024 * need to provide board-specific regulator names as platform data. 1025 */ 1026static int set_consumer_device_supply(struct regulator_dev *rdev, 1027 const char *consumer_dev_name, 1028 const char *supply) 1029{ 1030 struct regulator_map *node; 1031 int has_dev; 1032 1033 if (supply == NULL) 1034 return -EINVAL; 1035 1036 if (consumer_dev_name != NULL) 1037 has_dev = 1; 1038 else 1039 has_dev = 0; 1040 1041 list_for_each_entry(node, ®ulator_map_list, list) { 1042 if (node->dev_name && consumer_dev_name) { 1043 if (strcmp(node->dev_name, consumer_dev_name) != 0) 1044 continue; 1045 } else if (node->dev_name || consumer_dev_name) { 1046 continue; 1047 } 1048 1049 if (strcmp(node->supply, supply) != 0) 1050 continue; 1051 1052 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n", 1053 consumer_dev_name, 1054 dev_name(&node->regulator->dev), 1055 node->regulator->desc->name, 1056 supply, 1057 dev_name(&rdev->dev), rdev_get_name(rdev)); 1058 return -EBUSY; 1059 } 1060 1061 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); 1062 if (node == NULL) 1063 return -ENOMEM; 1064 1065 node->regulator = rdev; 1066 node->supply = supply; 1067 1068 if (has_dev) { 1069 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); 1070 if (node->dev_name == NULL) { 1071 kfree(node); 1072 return -ENOMEM; 1073 } 1074 } 1075 1076 list_add(&node->list, ®ulator_map_list); 1077 return 0; 1078} 1079 1080static void unset_regulator_supplies(struct regulator_dev *rdev) 1081{ 1082 struct regulator_map *node, *n; 1083 1084 list_for_each_entry_safe(node, n, ®ulator_map_list, list) { 1085 if (rdev == node->regulator) { 1086 list_del(&node->list); 1087 kfree(node->dev_name); 1088 kfree(node); 1089 } 1090 } 1091} 1092 1093#define REG_STR_SIZE 64 1094 1095static struct regulator *create_regulator(struct regulator_dev *rdev, 1096 struct device *dev, 1097 const char *supply_name) 1098{ 1099 struct regulator *regulator; 1100 char buf[REG_STR_SIZE]; 1101 int err, size; 1102 1103 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); 1104 if (regulator == NULL) 1105 return NULL; 1106 1107 mutex_lock(&rdev->mutex); 1108 regulator->rdev = rdev; 1109 list_add(®ulator->list, &rdev->consumer_list); 1110 1111 if (dev) { 1112 regulator->dev = dev; 1113 1114 /* Add a link to the device sysfs entry */ 1115 size = scnprintf(buf, REG_STR_SIZE, "%s-%s", 1116 dev->kobj.name, supply_name); 1117 if (size >= REG_STR_SIZE) 1118 goto overflow_err; 1119 1120 regulator->supply_name = kstrdup(buf, GFP_KERNEL); 1121 if (regulator->supply_name == NULL) 1122 goto overflow_err; 1123 1124 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj, 1125 buf); 1126 if (err) { 1127 rdev_warn(rdev, "could not add device link %s err %d\n", 1128 dev->kobj.name, err); 1129 /* non-fatal */ 1130 } 1131 } else { 1132 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL); 1133 if (regulator->supply_name == NULL) 1134 goto overflow_err; 1135 } 1136 1137 regulator->debugfs = debugfs_create_dir(regulator->supply_name, 1138 rdev->debugfs); 1139 if (!regulator->debugfs) { 1140 rdev_warn(rdev, "Failed to create debugfs directory\n"); 1141 } else { 1142 debugfs_create_u32("uA_load", 0444, regulator->debugfs, 1143 ®ulator->uA_load); 1144 debugfs_create_u32("min_uV", 0444, regulator->debugfs, 1145 ®ulator->min_uV); 1146 debugfs_create_u32("max_uV", 0444, regulator->debugfs, 1147 ®ulator->max_uV); 1148 } 1149 1150 /* 1151 * Check now if the regulator is an always on regulator - if 1152 * it is then we don't need to do nearly so much work for 1153 * enable/disable calls. 1154 */ 1155 if (!_regulator_can_change_status(rdev) && 1156 _regulator_is_enabled(rdev)) 1157 regulator->always_on = true; 1158 1159 mutex_unlock(&rdev->mutex); 1160 return regulator; 1161overflow_err: 1162 list_del(®ulator->list); 1163 kfree(regulator); 1164 mutex_unlock(&rdev->mutex); 1165 return NULL; 1166} 1167 1168static int _regulator_get_enable_time(struct regulator_dev *rdev) 1169{ 1170 if (!rdev->desc->ops->enable_time) 1171 return rdev->desc->enable_time; 1172 return rdev->desc->ops->enable_time(rdev); 1173} 1174 1175static struct regulator_dev *regulator_dev_lookup(struct device *dev, 1176 const char *supply, 1177 int *ret) 1178{ 1179 struct regulator_dev *r; 1180 struct device_node *node; 1181 struct regulator_map *map; 1182 const char *devname = NULL; 1183 1184 /* first do a dt based lookup */ 1185 if (dev && dev->of_node) { 1186 node = of_get_regulator(dev, supply); 1187 if (node) { 1188 list_for_each_entry(r, ®ulator_list, list) 1189 if (r->dev.parent && 1190 node == r->dev.of_node) 1191 return r; 1192 } else { 1193 /* 1194 * If we couldn't even get the node then it's 1195 * not just that the device didn't register 1196 * yet, there's no node and we'll never 1197 * succeed. 1198 */ 1199 *ret = -ENODEV; 1200 } 1201 } 1202 1203 /* if not found, try doing it non-dt way */ 1204 if (dev) 1205 devname = dev_name(dev); 1206 1207 list_for_each_entry(r, ®ulator_list, list) 1208 if (strcmp(rdev_get_name(r), supply) == 0) 1209 return r; 1210 1211 list_for_each_entry(map, ®ulator_map_list, list) { 1212 /* If the mapping has a device set up it must match */ 1213 if (map->dev_name && 1214 (!devname || strcmp(map->dev_name, devname))) 1215 continue; 1216 1217 if (strcmp(map->supply, supply) == 0) 1218 return map->regulator; 1219 } 1220 1221 1222 return NULL; 1223} 1224 1225/* Internal regulator request function */ 1226static struct regulator *_regulator_get(struct device *dev, const char *id, 1227 int exclusive) 1228{ 1229 struct regulator_dev *rdev; 1230 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER); 1231 const char *devname = NULL; 1232 int ret; 1233 1234 if (id == NULL) { 1235 pr_err("get() with no identifier\n"); 1236 return regulator; 1237 } 1238 1239 if (dev) 1240 devname = dev_name(dev); 1241 1242 mutex_lock(®ulator_list_mutex); 1243 1244 rdev = regulator_dev_lookup(dev, id, &ret); 1245 if (rdev) 1246 goto found; 1247 1248 if (board_wants_dummy_regulator) { 1249 rdev = dummy_regulator_rdev; 1250 goto found; 1251 } 1252 1253#ifdef CONFIG_REGULATOR_DUMMY 1254 if (!devname) 1255 devname = "deviceless"; 1256 1257 /* If the board didn't flag that it was fully constrained then 1258 * substitute in a dummy regulator so consumers can continue. 1259 */ 1260 if (!has_full_constraints) { 1261 pr_warn("%s supply %s not found, using dummy regulator\n", 1262 devname, id); 1263 rdev = dummy_regulator_rdev; 1264 goto found; 1265 } 1266#endif 1267 1268 mutex_unlock(®ulator_list_mutex); 1269 return regulator; 1270 1271found: 1272 if (rdev->exclusive) { 1273 regulator = ERR_PTR(-EPERM); 1274 goto out; 1275 } 1276 1277 if (exclusive && rdev->open_count) { 1278 regulator = ERR_PTR(-EBUSY); 1279 goto out; 1280 } 1281 1282 if (!try_module_get(rdev->owner)) 1283 goto out; 1284 1285 regulator = create_regulator(rdev, dev, id); 1286 if (regulator == NULL) { 1287 regulator = ERR_PTR(-ENOMEM); 1288 module_put(rdev->owner); 1289 goto out; 1290 } 1291 1292 rdev->open_count++; 1293 if (exclusive) { 1294 rdev->exclusive = 1; 1295 1296 ret = _regulator_is_enabled(rdev); 1297 if (ret > 0) 1298 rdev->use_count = 1; 1299 else 1300 rdev->use_count = 0; 1301 } 1302 1303out: 1304 mutex_unlock(®ulator_list_mutex); 1305 1306 return regulator; 1307} 1308 1309/** 1310 * regulator_get - lookup and obtain a reference to a regulator. 1311 * @dev: device for regulator "consumer" 1312 * @id: Supply name or regulator ID. 1313 * 1314 * Returns a struct regulator corresponding to the regulator producer, 1315 * or IS_ERR() condition containing errno. 1316 * 1317 * Use of supply names configured via regulator_set_device_supply() is 1318 * strongly encouraged. It is recommended that the supply name used 1319 * should match the name used for the supply and/or the relevant 1320 * device pins in the datasheet. 1321 */ 1322struct regulator *regulator_get(struct device *dev, const char *id) 1323{ 1324 return _regulator_get(dev, id, 0); 1325} 1326EXPORT_SYMBOL_GPL(regulator_get); 1327 1328static void devm_regulator_release(struct device *dev, void *res) 1329{ 1330 regulator_put(*(struct regulator **)res); 1331} 1332 1333/** 1334 * devm_regulator_get - Resource managed regulator_get() 1335 * @dev: device for regulator "consumer" 1336 * @id: Supply name or regulator ID. 1337 * 1338 * Managed regulator_get(). Regulators returned from this function are 1339 * automatically regulator_put() on driver detach. See regulator_get() for more 1340 * information. 1341 */ 1342struct regulator *devm_regulator_get(struct device *dev, const char *id) 1343{ 1344 struct regulator **ptr, *regulator; 1345 1346 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL); 1347 if (!ptr) 1348 return ERR_PTR(-ENOMEM); 1349 1350 regulator = regulator_get(dev, id); 1351 if (!IS_ERR(regulator)) { 1352 *ptr = regulator; 1353 devres_add(dev, ptr); 1354 } else { 1355 devres_free(ptr); 1356 } 1357 1358 return regulator; 1359} 1360EXPORT_SYMBOL_GPL(devm_regulator_get); 1361 1362/** 1363 * regulator_get_exclusive - obtain exclusive access to a regulator. 1364 * @dev: device for regulator "consumer" 1365 * @id: Supply name or regulator ID. 1366 * 1367 * Returns a struct regulator corresponding to the regulator producer, 1368 * or IS_ERR() condition containing errno. Other consumers will be 1369 * unable to obtain this reference is held and the use count for the 1370 * regulator will be initialised to reflect the current state of the 1371 * regulator. 1372 * 1373 * This is intended for use by consumers which cannot tolerate shared 1374 * use of the regulator such as those which need to force the 1375 * regulator off for correct operation of the hardware they are 1376 * controlling. 1377 * 1378 * Use of supply names configured via regulator_set_device_supply() is 1379 * strongly encouraged. It is recommended that the supply name used 1380 * should match the name used for the supply and/or the relevant 1381 * device pins in the datasheet. 1382 */ 1383struct regulator *regulator_get_exclusive(struct device *dev, const char *id) 1384{ 1385 return _regulator_get(dev, id, 1); 1386} 1387EXPORT_SYMBOL_GPL(regulator_get_exclusive); 1388 1389/* Locks held by regulator_put() */ 1390static void _regulator_put(struct regulator *regulator) 1391{ 1392 struct regulator_dev *rdev; 1393 1394 if (regulator == NULL || IS_ERR(regulator)) 1395 return; 1396 1397 rdev = regulator->rdev; 1398 1399 debugfs_remove_recursive(regulator->debugfs); 1400 1401 /* remove any sysfs entries */ 1402 if (regulator->dev) 1403 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); 1404 kfree(regulator->supply_name); 1405 list_del(®ulator->list); 1406 kfree(regulator); 1407 1408 rdev->open_count--; 1409 rdev->exclusive = 0; 1410 1411 module_put(rdev->owner); 1412} 1413 1414/** 1415 * regulator_put - "free" the regulator source 1416 * @regulator: regulator source 1417 * 1418 * Note: drivers must ensure that all regulator_enable calls made on this 1419 * regulator source are balanced by regulator_disable calls prior to calling 1420 * this function. 1421 */ 1422void regulator_put(struct regulator *regulator) 1423{ 1424 mutex_lock(®ulator_list_mutex); 1425 _regulator_put(regulator); 1426 mutex_unlock(®ulator_list_mutex); 1427} 1428EXPORT_SYMBOL_GPL(regulator_put); 1429 1430static int devm_regulator_match(struct device *dev, void *res, void *data) 1431{ 1432 struct regulator **r = res; 1433 if (!r || !*r) { 1434 WARN_ON(!r || !*r); 1435 return 0; 1436 } 1437 return *r == data; 1438} 1439 1440/** 1441 * devm_regulator_put - Resource managed regulator_put() 1442 * @regulator: regulator to free 1443 * 1444 * Deallocate a regulator allocated with devm_regulator_get(). Normally 1445 * this function will not need to be called and the resource management 1446 * code will ensure that the resource is freed. 1447 */ 1448void devm_regulator_put(struct regulator *regulator) 1449{ 1450 int rc; 1451 1452 rc = devres_release(regulator->dev, devm_regulator_release, 1453 devm_regulator_match, regulator); 1454 if (rc != 0) 1455 WARN_ON(rc); 1456} 1457EXPORT_SYMBOL_GPL(devm_regulator_put); 1458 1459static int _regulator_do_enable(struct regulator_dev *rdev) 1460{ 1461 int ret, delay; 1462 1463 /* Query before enabling in case configuration dependent. */ 1464 ret = _regulator_get_enable_time(rdev); 1465 if (ret >= 0) { 1466 delay = ret; 1467 } else { 1468 rdev_warn(rdev, "enable_time() failed: %d\n", ret); 1469 delay = 0; 1470 } 1471 1472 trace_regulator_enable(rdev_get_name(rdev)); 1473 1474 if (rdev->ena_gpio) { 1475 gpio_set_value_cansleep(rdev->ena_gpio, 1476 !rdev->ena_gpio_invert); 1477 rdev->ena_gpio_state = 1; 1478 } else if (rdev->desc->ops->enable) { 1479 ret = rdev->desc->ops->enable(rdev); 1480 if (ret < 0) 1481 return ret; 1482 } else { 1483 return -EINVAL; 1484 } 1485 1486 /* Allow the regulator to ramp; it would be useful to extend 1487 * this for bulk operations so that the regulators can ramp 1488 * together. */ 1489 trace_regulator_enable_delay(rdev_get_name(rdev)); 1490 1491 if (delay >= 1000) { 1492 mdelay(delay / 1000); 1493 udelay(delay % 1000); 1494 } else if (delay) { 1495 udelay(delay); 1496 } 1497 1498 trace_regulator_enable_complete(rdev_get_name(rdev)); 1499 1500 return 0; 1501} 1502 1503/* locks held by regulator_enable() */ 1504static int _regulator_enable(struct regulator_dev *rdev) 1505{ 1506 int ret; 1507 1508 /* check voltage and requested load before enabling */ 1509 if (rdev->constraints && 1510 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) 1511 drms_uA_update(rdev); 1512 1513 if (rdev->use_count == 0) { 1514 /* The regulator may on if it's not switchable or left on */ 1515 ret = _regulator_is_enabled(rdev); 1516 if (ret == -EINVAL || ret == 0) { 1517 if (!_regulator_can_change_status(rdev)) 1518 return -EPERM; 1519 1520 ret = _regulator_do_enable(rdev); 1521 if (ret < 0) 1522 return ret; 1523 1524 } else if (ret < 0) { 1525 rdev_err(rdev, "is_enabled() failed: %d\n", ret); 1526 return ret; 1527 } 1528 /* Fallthrough on positive return values - already enabled */ 1529 } 1530 1531 rdev->use_count++; 1532 1533 return 0; 1534} 1535 1536/** 1537 * regulator_enable - enable regulator output 1538 * @regulator: regulator source 1539 * 1540 * Request that the regulator be enabled with the regulator output at 1541 * the predefined voltage or current value. Calls to regulator_enable() 1542 * must be balanced with calls to regulator_disable(). 1543 * 1544 * NOTE: the output value can be set by other drivers, boot loader or may be 1545 * hardwired in the regulator. 1546 */ 1547int regulator_enable(struct regulator *regulator) 1548{ 1549 struct regulator_dev *rdev = regulator->rdev; 1550 int ret = 0; 1551 1552 if (regulator->always_on) 1553 return 0; 1554 1555 if (rdev->supply) { 1556 ret = regulator_enable(rdev->supply); 1557 if (ret != 0) 1558 return ret; 1559 } 1560 1561 mutex_lock(&rdev->mutex); 1562 ret = _regulator_enable(rdev); 1563 mutex_unlock(&rdev->mutex); 1564 1565 if (ret != 0 && rdev->supply) 1566 regulator_disable(rdev->supply); 1567 1568 return ret; 1569} 1570EXPORT_SYMBOL_GPL(regulator_enable); 1571 1572static int _regulator_do_disable(struct regulator_dev *rdev) 1573{ 1574 int ret; 1575 1576 trace_regulator_disable(rdev_get_name(rdev)); 1577 1578 if (rdev->ena_gpio) { 1579 gpio_set_value_cansleep(rdev->ena_gpio, 1580 rdev->ena_gpio_invert); 1581 rdev->ena_gpio_state = 0; 1582 1583 } else if (rdev->desc->ops->disable) { 1584 ret = rdev->desc->ops->disable(rdev); 1585 if (ret != 0) 1586 return ret; 1587 } 1588 1589 trace_regulator_disable_complete(rdev_get_name(rdev)); 1590 1591 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, 1592 NULL); 1593 return 0; 1594} 1595 1596/* locks held by regulator_disable() */ 1597static int _regulator_disable(struct regulator_dev *rdev) 1598{ 1599 int ret = 0; 1600 1601 if (WARN(rdev->use_count <= 0, 1602 "unbalanced disables for %s\n", rdev_get_name(rdev))) 1603 return -EIO; 1604 1605 /* are we the last user and permitted to disable ? */ 1606 if (rdev->use_count == 1 && 1607 (rdev->constraints && !rdev->constraints->always_on)) { 1608 1609 /* we are last user */ 1610 if (_regulator_can_change_status(rdev)) { 1611 ret = _regulator_do_disable(rdev); 1612 if (ret < 0) { 1613 rdev_err(rdev, "failed to disable\n"); 1614 return ret; 1615 } 1616 } 1617 1618 rdev->use_count = 0; 1619 } else if (rdev->use_count > 1) { 1620 1621 if (rdev->constraints && 1622 (rdev->constraints->valid_ops_mask & 1623 REGULATOR_CHANGE_DRMS)) 1624 drms_uA_update(rdev); 1625 1626 rdev->use_count--; 1627 } 1628 1629 return ret; 1630} 1631 1632/** 1633 * regulator_disable - disable regulator output 1634 * @regulator: regulator source 1635 * 1636 * Disable the regulator output voltage or current. Calls to 1637 * regulator_enable() must be balanced with calls to 1638 * regulator_disable(). 1639 * 1640 * NOTE: this will only disable the regulator output if no other consumer 1641 * devices have it enabled, the regulator device supports disabling and 1642 * machine constraints permit this operation. 1643 */ 1644int regulator_disable(struct regulator *regulator) 1645{ 1646 struct regulator_dev *rdev = regulator->rdev; 1647 int ret = 0; 1648 1649 if (regulator->always_on) 1650 return 0; 1651 1652 mutex_lock(&rdev->mutex); 1653 ret = _regulator_disable(rdev); 1654 mutex_unlock(&rdev->mutex); 1655 1656 if (ret == 0 && rdev->supply) 1657 regulator_disable(rdev->supply); 1658 1659 return ret; 1660} 1661EXPORT_SYMBOL_GPL(regulator_disable); 1662 1663/* locks held by regulator_force_disable() */ 1664static int _regulator_force_disable(struct regulator_dev *rdev) 1665{ 1666 int ret = 0; 1667 1668 /* force disable */ 1669 if (rdev->desc->ops->disable) { 1670 /* ah well, who wants to live forever... */ 1671 ret = rdev->desc->ops->disable(rdev); 1672 if (ret < 0) { 1673 rdev_err(rdev, "failed to force disable\n"); 1674 return ret; 1675 } 1676 /* notify other consumers that power has been forced off */ 1677 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | 1678 REGULATOR_EVENT_DISABLE, NULL); 1679 } 1680 1681 return ret; 1682} 1683 1684/** 1685 * regulator_force_disable - force disable regulator output 1686 * @regulator: regulator source 1687 * 1688 * Forcibly disable the regulator output voltage or current. 1689 * NOTE: this *will* disable the regulator output even if other consumer 1690 * devices have it enabled. This should be used for situations when device 1691 * damage will likely occur if the regulator is not disabled (e.g. over temp). 1692 */ 1693int regulator_force_disable(struct regulator *regulator) 1694{ 1695 struct regulator_dev *rdev = regulator->rdev; 1696 int ret; 1697 1698 mutex_lock(&rdev->mutex); 1699 regulator->uA_load = 0; 1700 ret = _regulator_force_disable(regulator->rdev); 1701 mutex_unlock(&rdev->mutex); 1702 1703 if (rdev->supply) 1704 while (rdev->open_count--) 1705 regulator_disable(rdev->supply); 1706 1707 return ret; 1708} 1709EXPORT_SYMBOL_GPL(regulator_force_disable); 1710 1711static void regulator_disable_work(struct work_struct *work) 1712{ 1713 struct regulator_dev *rdev = container_of(work, struct regulator_dev, 1714 disable_work.work); 1715 int count, i, ret; 1716 1717 mutex_lock(&rdev->mutex); 1718 1719 BUG_ON(!rdev->deferred_disables); 1720 1721 count = rdev->deferred_disables; 1722 rdev->deferred_disables = 0; 1723 1724 for (i = 0; i < count; i++) { 1725 ret = _regulator_disable(rdev); 1726 if (ret != 0) 1727 rdev_err(rdev, "Deferred disable failed: %d\n", ret); 1728 } 1729 1730 mutex_unlock(&rdev->mutex); 1731 1732 if (rdev->supply) { 1733 for (i = 0; i < count; i++) { 1734 ret = regulator_disable(rdev->supply); 1735 if (ret != 0) { 1736 rdev_err(rdev, 1737 "Supply disable failed: %d\n", ret); 1738 } 1739 } 1740 } 1741} 1742 1743/** 1744 * regulator_disable_deferred - disable regulator output with delay 1745 * @regulator: regulator source 1746 * @ms: miliseconds until the regulator is disabled 1747 * 1748 * Execute regulator_disable() on the regulator after a delay. This 1749 * is intended for use with devices that require some time to quiesce. 1750 * 1751 * NOTE: this will only disable the regulator output if no other consumer 1752 * devices have it enabled, the regulator device supports disabling and 1753 * machine constraints permit this operation. 1754 */ 1755int regulator_disable_deferred(struct regulator *regulator, int ms) 1756{ 1757 struct regulator_dev *rdev = regulator->rdev; 1758 int ret; 1759 1760 if (regulator->always_on) 1761 return 0; 1762 1763 if (!ms) 1764 return regulator_disable(regulator); 1765 1766 mutex_lock(&rdev->mutex); 1767 rdev->deferred_disables++; 1768 mutex_unlock(&rdev->mutex); 1769 1770 ret = schedule_delayed_work(&rdev->disable_work, 1771 msecs_to_jiffies(ms)); 1772 if (ret < 0) 1773 return ret; 1774 else 1775 return 0; 1776} 1777EXPORT_SYMBOL_GPL(regulator_disable_deferred); 1778 1779/** 1780 * regulator_is_enabled_regmap - standard is_enabled() for regmap users 1781 * 1782 * @rdev: regulator to operate on 1783 * 1784 * Regulators that use regmap for their register I/O can set the 1785 * enable_reg and enable_mask fields in their descriptor and then use 1786 * this as their is_enabled operation, saving some code. 1787 */ 1788int regulator_is_enabled_regmap(struct regulator_dev *rdev) 1789{ 1790 unsigned int val; 1791 int ret; 1792 1793 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val); 1794 if (ret != 0) 1795 return ret; 1796 1797 return (val & rdev->desc->enable_mask) != 0; 1798} 1799EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap); 1800 1801/** 1802 * regulator_enable_regmap - standard enable() for regmap users 1803 * 1804 * @rdev: regulator to operate on 1805 * 1806 * Regulators that use regmap for their register I/O can set the 1807 * enable_reg and enable_mask fields in their descriptor and then use 1808 * this as their enable() operation, saving some code. 1809 */ 1810int regulator_enable_regmap(struct regulator_dev *rdev) 1811{ 1812 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 1813 rdev->desc->enable_mask, 1814 rdev->desc->enable_mask); 1815} 1816EXPORT_SYMBOL_GPL(regulator_enable_regmap); 1817 1818/** 1819 * regulator_disable_regmap - standard disable() for regmap users 1820 * 1821 * @rdev: regulator to operate on 1822 * 1823 * Regulators that use regmap for their register I/O can set the 1824 * enable_reg and enable_mask fields in their descriptor and then use 1825 * this as their disable() operation, saving some code. 1826 */ 1827int regulator_disable_regmap(struct regulator_dev *rdev) 1828{ 1829 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, 1830 rdev->desc->enable_mask, 0); 1831} 1832EXPORT_SYMBOL_GPL(regulator_disable_regmap); 1833 1834static int _regulator_is_enabled(struct regulator_dev *rdev) 1835{ 1836 /* A GPIO control always takes precedence */ 1837 if (rdev->ena_gpio) 1838 return rdev->ena_gpio_state; 1839 1840 /* If we don't know then assume that the regulator is always on */ 1841 if (!rdev->desc->ops->is_enabled) 1842 return 1; 1843 1844 return rdev->desc->ops->is_enabled(rdev); 1845} 1846 1847/** 1848 * regulator_is_enabled - is the regulator output enabled 1849 * @regulator: regulator source 1850 * 1851 * Returns positive if the regulator driver backing the source/client 1852 * has requested that the device be enabled, zero if it hasn't, else a 1853 * negative errno code. 1854 * 1855 * Note that the device backing this regulator handle can have multiple 1856 * users, so it might be enabled even if regulator_enable() was never 1857 * called for this particular source. 1858 */ 1859int regulator_is_enabled(struct regulator *regulator) 1860{ 1861 int ret; 1862 1863 if (regulator->always_on) 1864 return 1; 1865 1866 mutex_lock(®ulator->rdev->mutex); 1867 ret = _regulator_is_enabled(regulator->rdev); 1868 mutex_unlock(®ulator->rdev->mutex); 1869 1870 return ret; 1871} 1872EXPORT_SYMBOL_GPL(regulator_is_enabled); 1873 1874/** 1875 * regulator_can_change_voltage - check if regulator can change voltage 1876 * @regulator: regulator source 1877 * 1878 * Returns positive if the regulator driver backing the source/client 1879 * can change its voltage, false otherwise. Usefull for detecting fixed 1880 * or dummy regulators and disabling voltage change logic in the client 1881 * driver. 1882 */ 1883int regulator_can_change_voltage(struct regulator *regulator) 1884{ 1885 struct regulator_dev *rdev = regulator->rdev; 1886 1887 if (rdev->constraints && 1888 rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE && 1889 (rdev->desc->n_voltages - rdev->desc->linear_min_sel) > 1) 1890 return 1; 1891 1892 return 0; 1893} 1894EXPORT_SYMBOL_GPL(regulator_can_change_voltage); 1895 1896/** 1897 * regulator_count_voltages - count regulator_list_voltage() selectors 1898 * @regulator: regulator source 1899 * 1900 * Returns number of selectors, or negative errno. Selectors are 1901 * numbered starting at zero, and typically correspond to bitfields 1902 * in hardware registers. 1903 */ 1904int regulator_count_voltages(struct regulator *regulator) 1905{ 1906 struct regulator_dev *rdev = regulator->rdev; 1907 1908 return rdev->desc->n_voltages ? : -EINVAL; 1909} 1910EXPORT_SYMBOL_GPL(regulator_count_voltages); 1911 1912/** 1913 * regulator_list_voltage_linear - List voltages with simple calculation 1914 * 1915 * @rdev: Regulator device 1916 * @selector: Selector to convert into a voltage 1917 * 1918 * Regulators with a simple linear mapping between voltages and 1919 * selectors can set min_uV and uV_step in the regulator descriptor 1920 * and then use this function as their list_voltage() operation, 1921 */ 1922int regulator_list_voltage_linear(struct regulator_dev *rdev, 1923 unsigned int selector) 1924{ 1925 if (selector >= rdev->desc->n_voltages) 1926 return -EINVAL; 1927 if (selector < rdev->desc->linear_min_sel) 1928 return 0; 1929 1930 selector -= rdev->desc->linear_min_sel; 1931 1932 return rdev->desc->min_uV + (rdev->desc->uV_step * selector); 1933} 1934EXPORT_SYMBOL_GPL(regulator_list_voltage_linear); 1935 1936/** 1937 * regulator_list_voltage_table - List voltages with table based mapping 1938 * 1939 * @rdev: Regulator device 1940 * @selector: Selector to convert into a voltage 1941 * 1942 * Regulators with table based mapping between voltages and 1943 * selectors can set volt_table in the regulator descriptor 1944 * and then use this function as their list_voltage() operation. 1945 */ 1946int regulator_list_voltage_table(struct regulator_dev *rdev, 1947 unsigned int selector) 1948{ 1949 if (!rdev->desc->volt_table) { 1950 BUG_ON(!rdev->desc->volt_table); 1951 return -EINVAL; 1952 } 1953 1954 if (selector >= rdev->desc->n_voltages) 1955 return -EINVAL; 1956 1957 return rdev->desc->volt_table[selector]; 1958} 1959EXPORT_SYMBOL_GPL(regulator_list_voltage_table); 1960 1961/** 1962 * regulator_list_voltage - enumerate supported voltages 1963 * @regulator: regulator source 1964 * @selector: identify voltage to list 1965 * Context: can sleep 1966 * 1967 * Returns a voltage that can be passed to @regulator_set_voltage(), 1968 * zero if this selector code can't be used on this system, or a 1969 * negative errno. 1970 */ 1971int regulator_list_voltage(struct regulator *regulator, unsigned selector) 1972{ 1973 struct regulator_dev *rdev = regulator->rdev; 1974 struct regulator_ops *ops = rdev->desc->ops; 1975 int ret; 1976 1977 if (!ops->list_voltage || selector >= rdev->desc->n_voltages) 1978 return -EINVAL; 1979 1980 mutex_lock(&rdev->mutex); 1981 ret = ops->list_voltage(rdev, selector); 1982 mutex_unlock(&rdev->mutex); 1983 1984 if (ret > 0) { 1985 if (ret < rdev->constraints->min_uV) 1986 ret = 0; 1987 else if (ret > rdev->constraints->max_uV) 1988 ret = 0; 1989 } 1990 1991 return ret; 1992} 1993EXPORT_SYMBOL_GPL(regulator_list_voltage); 1994 1995/** 1996 * regulator_is_supported_voltage - check if a voltage range can be supported 1997 * 1998 * @regulator: Regulator to check. 1999 * @min_uV: Minimum required voltage in uV. 2000 * @max_uV: Maximum required voltage in uV. 2001 * 2002 * Returns a boolean or a negative error code. 2003 */ 2004int regulator_is_supported_voltage(struct regulator *regulator, 2005 int min_uV, int max_uV) 2006{ 2007 struct regulator_dev *rdev = regulator->rdev; 2008 int i, voltages, ret; 2009 2010 /* If we can't change voltage check the current voltage */ 2011 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { 2012 ret = regulator_get_voltage(regulator); 2013 if (ret >= 0) 2014 return (min_uV <= ret && ret <= max_uV); 2015 else 2016 return ret; 2017 } 2018 2019 /* Any voltage within constrains range is fine? */ 2020 if (rdev->desc->continuous_voltage_range) 2021 return min_uV >= rdev->constraints->min_uV && 2022 max_uV <= rdev->constraints->max_uV; 2023 2024 ret = regulator_count_voltages(regulator); 2025 if (ret < 0) 2026 return ret; 2027 voltages = ret; 2028 2029 for (i = 0; i < voltages; i++) { 2030 ret = regulator_list_voltage(regulator, i); 2031 2032 if (ret >= min_uV && ret <= max_uV) 2033 return 1; 2034 } 2035 2036 return 0; 2037} 2038EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); 2039 2040/** 2041 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users 2042 * 2043 * @rdev: regulator to operate on 2044 * 2045 * Regulators that use regmap for their register I/O can set the 2046 * vsel_reg and vsel_mask fields in their descriptor and then use this 2047 * as their get_voltage_vsel operation, saving some code. 2048 */ 2049int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev) 2050{ 2051 unsigned int val; 2052 int ret; 2053 2054 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val); 2055 if (ret != 0) 2056 return ret; 2057 2058 val &= rdev->desc->vsel_mask; 2059 val >>= ffs(rdev->desc->vsel_mask) - 1; 2060 2061 return val; 2062} 2063EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap); 2064 2065/** 2066 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users 2067 * 2068 * @rdev: regulator to operate on 2069 * @sel: Selector to set 2070 * 2071 * Regulators that use regmap for their register I/O can set the 2072 * vsel_reg and vsel_mask fields in their descriptor and then use this 2073 * as their set_voltage_vsel operation, saving some code. 2074 */ 2075int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel) 2076{ 2077 int ret; 2078 2079 sel <<= ffs(rdev->desc->vsel_mask) - 1; 2080 2081 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg, 2082 rdev->desc->vsel_mask, sel); 2083 if (ret) 2084 return ret; 2085 2086 if (rdev->desc->apply_bit) 2087 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg, 2088 rdev->desc->apply_bit, 2089 rdev->desc->apply_bit); 2090 return ret; 2091} 2092EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap); 2093 2094/** 2095 * regulator_map_voltage_iterate - map_voltage() based on list_voltage() 2096 * 2097 * @rdev: Regulator to operate on 2098 * @min_uV: Lower bound for voltage 2099 * @max_uV: Upper bound for voltage 2100 * 2101 * Drivers implementing set_voltage_sel() and list_voltage() can use 2102 * this as their map_voltage() operation. It will find a suitable 2103 * voltage by calling list_voltage() until it gets something in bounds 2104 * for the requested voltages. 2105 */ 2106int regulator_map_voltage_iterate(struct regulator_dev *rdev, 2107 int min_uV, int max_uV) 2108{ 2109 int best_val = INT_MAX; 2110 int selector = 0; 2111 int i, ret; 2112 2113 /* Find the smallest voltage that falls within the specified 2114 * range. 2115 */ 2116 for (i = 0; i < rdev->desc->n_voltages; i++) { 2117 ret = rdev->desc->ops->list_voltage(rdev, i); 2118 if (ret < 0) 2119 continue; 2120 2121 if (ret < best_val && ret >= min_uV && ret <= max_uV) { 2122 best_val = ret; 2123 selector = i; 2124 } 2125 } 2126 2127 if (best_val != INT_MAX) 2128 return selector; 2129 else 2130 return -EINVAL; 2131} 2132EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate); 2133 2134/** 2135 * regulator_map_voltage_linear - map_voltage() for simple linear mappings 2136 * 2137 * @rdev: Regulator to operate on 2138 * @min_uV: Lower bound for voltage 2139 * @max_uV: Upper bound for voltage 2140 * 2141 * Drivers providing min_uV and uV_step in their regulator_desc can 2142 * use this as their map_voltage() operation. 2143 */ 2144int regulator_map_voltage_linear(struct regulator_dev *rdev, 2145 int min_uV, int max_uV) 2146{ 2147 int ret, voltage; 2148 2149 /* Allow uV_step to be 0 for fixed voltage */ 2150 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) { 2151 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV) 2152 return 0; 2153 else 2154 return -EINVAL; 2155 } 2156 2157 if (!rdev->desc->uV_step) { 2158 BUG_ON(!rdev->desc->uV_step); 2159 return -EINVAL; 2160 } 2161 2162 if (min_uV < rdev->desc->min_uV) 2163 min_uV = rdev->desc->min_uV; 2164 2165 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step); 2166 if (ret < 0) 2167 return ret; 2168 2169 ret += rdev->desc->linear_min_sel; 2170 2171 /* Map back into a voltage to verify we're still in bounds */ 2172 voltage = rdev->desc->ops->list_voltage(rdev, ret); 2173 if (voltage < min_uV || voltage > max_uV) 2174 return -EINVAL; 2175 2176 return ret; 2177} 2178EXPORT_SYMBOL_GPL(regulator_map_voltage_linear); 2179 2180static int _regulator_do_set_voltage(struct regulator_dev *rdev, 2181 int min_uV, int max_uV) 2182{ 2183 int ret; 2184 int delay = 0; 2185 int best_val = 0; 2186 unsigned int selector; 2187 int old_selector = -1; 2188 2189 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); 2190 2191 min_uV += rdev->constraints->uV_offset; 2192 max_uV += rdev->constraints->uV_offset; 2193 2194 /* 2195 * If we can't obtain the old selector there is not enough 2196 * info to call set_voltage_time_sel(). 2197 */ 2198 if (_regulator_is_enabled(rdev) && 2199 rdev->desc->ops->set_voltage_time_sel && 2200 rdev->desc->ops->get_voltage_sel) { 2201 old_selector = rdev->desc->ops->get_voltage_sel(rdev); 2202 if (old_selector < 0) 2203 return old_selector; 2204 } 2205 2206 if (rdev->desc->ops->set_voltage) { 2207 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, 2208 &selector); 2209 2210 if (ret >= 0) { 2211 if (rdev->desc->ops->list_voltage) 2212 best_val = rdev->desc->ops->list_voltage(rdev, 2213 selector); 2214 else 2215 best_val = _regulator_get_voltage(rdev); 2216 } 2217 2218 } else if (rdev->desc->ops->set_voltage_sel) { 2219 if (rdev->desc->ops->map_voltage) { 2220 ret = rdev->desc->ops->map_voltage(rdev, min_uV, 2221 max_uV); 2222 } else { 2223 if (rdev->desc->ops->list_voltage == 2224 regulator_list_voltage_linear) 2225 ret = regulator_map_voltage_linear(rdev, 2226 min_uV, max_uV); 2227 else 2228 ret = regulator_map_voltage_iterate(rdev, 2229 min_uV, max_uV); 2230 } 2231 2232 if (ret >= 0) { 2233 best_val = rdev->desc->ops->list_voltage(rdev, ret); 2234 if (min_uV <= best_val && max_uV >= best_val) { 2235 selector = ret; 2236 ret = rdev->desc->ops->set_voltage_sel(rdev, 2237 ret); 2238 } else { 2239 ret = -EINVAL; 2240 } 2241 } 2242 } else { 2243 ret = -EINVAL; 2244 } 2245 2246 /* Call set_voltage_time_sel if successfully obtained old_selector */ 2247 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 && 2248 rdev->desc->ops->set_voltage_time_sel) { 2249 2250 delay = rdev->desc->ops->set_voltage_time_sel(rdev, 2251 old_selector, selector); 2252 if (delay < 0) { 2253 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", 2254 delay); 2255 delay = 0; 2256 } 2257 2258 /* Insert any necessary delays */ 2259 if (delay >= 1000) { 2260 mdelay(delay / 1000); 2261 udelay(delay % 1000); 2262 } else if (delay) { 2263 udelay(delay); 2264 } 2265 } 2266 2267 if (ret == 0 && best_val >= 0) { 2268 unsigned long data = best_val; 2269 2270 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, 2271 (void *)data); 2272 } 2273 2274 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val); 2275 2276 return ret; 2277} 2278 2279/** 2280 * regulator_set_voltage - set regulator output voltage 2281 * @regulator: regulator source 2282 * @min_uV: Minimum required voltage in uV 2283 * @max_uV: Maximum acceptable voltage in uV 2284 * 2285 * Sets a voltage regulator to the desired output voltage. This can be set 2286 * during any regulator state. IOW, regulator can be disabled or enabled. 2287 * 2288 * If the regulator is enabled then the voltage will change to the new value 2289 * immediately otherwise if the regulator is disabled the regulator will 2290 * output at the new voltage when enabled. 2291 * 2292 * NOTE: If the regulator is shared between several devices then the lowest 2293 * request voltage that meets the system constraints will be used. 2294 * Regulator system constraints must be set for this regulator before 2295 * calling this function otherwise this call will fail. 2296 */ 2297int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) 2298{ 2299 struct regulator_dev *rdev = regulator->rdev; 2300 int ret = 0; 2301 2302 mutex_lock(&rdev->mutex); 2303 2304 /* If we're setting the same range as last time the change 2305 * should be a noop (some cpufreq implementations use the same 2306 * voltage for multiple frequencies, for example). 2307 */ 2308 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV) 2309 goto out; 2310 2311 /* sanity check */ 2312 if (!rdev->desc->ops->set_voltage && 2313 !rdev->desc->ops->set_voltage_sel) { 2314 ret = -EINVAL; 2315 goto out; 2316 } 2317 2318 /* constraints check */ 2319 ret = regulator_check_voltage(rdev, &min_uV, &max_uV); 2320 if (ret < 0) 2321 goto out; 2322 regulator->min_uV = min_uV; 2323 regulator->max_uV = max_uV; 2324 2325 ret = regulator_check_consumers(rdev, &min_uV, &max_uV); 2326 if (ret < 0) 2327 goto out; 2328 2329 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); 2330 2331out: 2332 mutex_unlock(&rdev->mutex); 2333 return ret; 2334} 2335EXPORT_SYMBOL_GPL(regulator_set_voltage); 2336 2337/** 2338 * regulator_set_voltage_time - get raise/fall time 2339 * @regulator: regulator source 2340 * @old_uV: starting voltage in microvolts 2341 * @new_uV: target voltage in microvolts 2342 * 2343 * Provided with the starting and ending voltage, this function attempts to 2344 * calculate the time in microseconds required to rise or fall to this new 2345 * voltage. 2346 */ 2347int regulator_set_voltage_time(struct regulator *regulator, 2348 int old_uV, int new_uV) 2349{ 2350 struct regulator_dev *rdev = regulator->rdev; 2351 struct regulator_ops *ops = rdev->desc->ops; 2352 int old_sel = -1; 2353 int new_sel = -1; 2354 int voltage; 2355 int i; 2356 2357 /* Currently requires operations to do this */ 2358 if (!ops->list_voltage || !ops->set_voltage_time_sel 2359 || !rdev->desc->n_voltages) 2360 return -EINVAL; 2361 2362 for (i = 0; i < rdev->desc->n_voltages; i++) { 2363 /* We only look for exact voltage matches here */ 2364 voltage = regulator_list_voltage(regulator, i); 2365 if (voltage < 0) 2366 return -EINVAL; 2367 if (voltage == 0) 2368 continue; 2369 if (voltage == old_uV) 2370 old_sel = i; 2371 if (voltage == new_uV) 2372 new_sel = i; 2373 } 2374 2375 if (old_sel < 0 || new_sel < 0) 2376 return -EINVAL; 2377 2378 return ops->set_voltage_time_sel(rdev, old_sel, new_sel); 2379} 2380EXPORT_SYMBOL_GPL(regulator_set_voltage_time); 2381 2382/** 2383 * regulator_set_voltage_time_sel - get raise/fall time 2384 * @rdev: regulator source device 2385 * @old_selector: selector for starting voltage 2386 * @new_selector: selector for target voltage 2387 * 2388 * Provided with the starting and target voltage selectors, this function 2389 * returns time in microseconds required to rise or fall to this new voltage 2390 * 2391 * Drivers providing ramp_delay in regulation_constraints can use this as their 2392 * set_voltage_time_sel() operation. 2393 */ 2394int regulator_set_voltage_time_sel(struct regulator_dev *rdev, 2395 unsigned int old_selector, 2396 unsigned int new_selector) 2397{ 2398 unsigned int ramp_delay = 0; 2399 int old_volt, new_volt; 2400 2401 if (rdev->constraints->ramp_delay) 2402 ramp_delay = rdev->constraints->ramp_delay; 2403 else if (rdev->desc->ramp_delay) 2404 ramp_delay = rdev->desc->ramp_delay; 2405 2406 if (ramp_delay == 0) { 2407 rdev_warn(rdev, "ramp_delay not set\n"); 2408 return 0; 2409 } 2410 2411 /* sanity check */ 2412 if (!rdev->desc->ops->list_voltage) 2413 return -EINVAL; 2414 2415 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); 2416 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); 2417 2418 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay); 2419} 2420EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel); 2421 2422/** 2423 * regulator_sync_voltage - re-apply last regulator output voltage 2424 * @regulator: regulator source 2425 * 2426 * Re-apply the last configured voltage. This is intended to be used 2427 * where some external control source the consumer is cooperating with 2428 * has caused the configured voltage to change. 2429 */ 2430int regulator_sync_voltage(struct regulator *regulator) 2431{ 2432 struct regulator_dev *rdev = regulator->rdev; 2433 int ret, min_uV, max_uV; 2434 2435 mutex_lock(&rdev->mutex); 2436 2437 if (!rdev->desc->ops->set_voltage && 2438 !rdev->desc->ops->set_voltage_sel) { 2439 ret = -EINVAL; 2440 goto out; 2441 } 2442 2443 /* This is only going to work if we've had a voltage configured. */ 2444 if (!regulator->min_uV && !regulator->max_uV) { 2445 ret = -EINVAL; 2446 goto out; 2447 } 2448 2449 min_uV = regulator->min_uV; 2450 max_uV = regulator->max_uV; 2451 2452 /* This should be a paranoia check... */ 2453 ret = regulator_check_voltage(rdev, &min_uV, &max_uV); 2454 if (ret < 0) 2455 goto out; 2456 2457 ret = regulator_check_consumers(rdev, &min_uV, &max_uV); 2458 if (ret < 0) 2459 goto out; 2460 2461 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); 2462 2463out: 2464 mutex_unlock(&rdev->mutex); 2465 return ret; 2466} 2467EXPORT_SYMBOL_GPL(regulator_sync_voltage); 2468 2469static int _regulator_get_voltage(struct regulator_dev *rdev) 2470{ 2471 int sel, ret; 2472 2473 if (rdev->desc->ops->get_voltage_sel) { 2474 sel = rdev->desc->ops->get_voltage_sel(rdev); 2475 if (sel < 0) 2476 return sel; 2477 ret = rdev->desc->ops->list_voltage(rdev, sel); 2478 } else if (rdev->desc->ops->get_voltage) { 2479 ret = rdev->desc->ops->get_voltage(rdev); 2480 } else if (rdev->desc->ops->list_voltage) { 2481 ret = rdev->desc->ops->list_voltage(rdev, 0); 2482 } else { 2483 return -EINVAL; 2484 } 2485 2486 if (ret < 0) 2487 return ret; 2488 return ret - rdev->constraints->uV_offset; 2489} 2490 2491/** 2492 * regulator_get_voltage - get regulator output voltage 2493 * @regulator: regulator source 2494 * 2495 * This returns the current regulator voltage in uV. 2496 * 2497 * NOTE: If the regulator is disabled it will return the voltage value. This 2498 * function should not be used to determine regulator state. 2499 */ 2500int regulator_get_voltage(struct regulator *regulator) 2501{ 2502 int ret; 2503 2504 mutex_lock(®ulator->rdev->mutex); 2505 2506 ret = _regulator_get_voltage(regulator->rdev); 2507 2508 mutex_unlock(®ulator->rdev->mutex); 2509 2510 return ret; 2511} 2512EXPORT_SYMBOL_GPL(regulator_get_voltage); 2513 2514/** 2515 * regulator_set_current_limit - set regulator output current limit 2516 * @regulator: regulator source 2517 * @min_uA: Minimuum supported current in uA 2518 * @max_uA: Maximum supported current in uA 2519 * 2520 * Sets current sink to the desired output current. This can be set during 2521 * any regulator state. IOW, regulator can be disabled or enabled. 2522 * 2523 * If the regulator is enabled then the current will change to the new value 2524 * immediately otherwise if the regulator is disabled the regulator will 2525 * output at the new current when enabled. 2526 * 2527 * NOTE: Regulator system constraints must be set for this regulator before 2528 * calling this function otherwise this call will fail. 2529 */ 2530int regulator_set_current_limit(struct regulator *regulator, 2531 int min_uA, int max_uA) 2532{ 2533 struct regulator_dev *rdev = regulator->rdev; 2534 int ret; 2535 2536 mutex_lock(&rdev->mutex); 2537 2538 /* sanity check */ 2539 if (!rdev->desc->ops->set_current_limit) { 2540 ret = -EINVAL; 2541 goto out; 2542 } 2543 2544 /* constraints check */ 2545 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); 2546 if (ret < 0) 2547 goto out; 2548 2549 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); 2550out: 2551 mutex_unlock(&rdev->mutex); 2552 return ret; 2553} 2554EXPORT_SYMBOL_GPL(regulator_set_current_limit); 2555 2556static int _regulator_get_current_limit(struct regulator_dev *rdev) 2557{ 2558 int ret; 2559 2560 mutex_lock(&rdev->mutex); 2561 2562 /* sanity check */ 2563 if (!rdev->desc->ops->get_current_limit) { 2564 ret = -EINVAL; 2565 goto out; 2566 } 2567 2568 ret = rdev->desc->ops->get_current_limit(rdev); 2569out: 2570 mutex_unlock(&rdev->mutex); 2571 return ret; 2572} 2573 2574/** 2575 * regulator_get_current_limit - get regulator output current 2576 * @regulator: regulator source 2577 * 2578 * This returns the current supplied by the specified current sink in uA. 2579 * 2580 * NOTE: If the regulator is disabled it will return the current value. This 2581 * function should not be used to determine regulator state. 2582 */ 2583int regulator_get_current_limit(struct regulator *regulator) 2584{ 2585 return _regulator_get_current_limit(regulator->rdev); 2586} 2587EXPORT_SYMBOL_GPL(regulator_get_current_limit); 2588 2589/** 2590 * regulator_set_mode - set regulator operating mode 2591 * @regulator: regulator source 2592 * @mode: operating mode - one of the REGULATOR_MODE constants 2593 * 2594 * Set regulator operating mode to increase regulator efficiency or improve 2595 * regulation performance. 2596 * 2597 * NOTE: Regulator system constraints must be set for this regulator before 2598 * calling this function otherwise this call will fail. 2599 */ 2600int regulator_set_mode(struct regulator *regulator, unsigned int mode) 2601{ 2602 struct regulator_dev *rdev = regulator->rdev; 2603 int ret; 2604 int regulator_curr_mode; 2605 2606 mutex_lock(&rdev->mutex); 2607 2608 /* sanity check */ 2609 if (!rdev->desc->ops->set_mode) { 2610 ret = -EINVAL; 2611 goto out; 2612 } 2613 2614 /* return if the same mode is requested */ 2615 if (rdev->desc->ops->get_mode) { 2616 regulator_curr_mode = rdev->desc->ops->get_mode(rdev); 2617 if (regulator_curr_mode == mode) { 2618 ret = 0; 2619 goto out; 2620 } 2621 } 2622 2623 /* constraints check */ 2624 ret = regulator_mode_constrain(rdev, &mode); 2625 if (ret < 0) 2626 goto out; 2627 2628 ret = rdev->desc->ops->set_mode(rdev, mode); 2629out: 2630 mutex_unlock(&rdev->mutex); 2631 return ret; 2632} 2633EXPORT_SYMBOL_GPL(regulator_set_mode); 2634 2635static unsigned int _regulator_get_mode(struct regulator_dev *rdev) 2636{ 2637 int ret; 2638 2639 mutex_lock(&rdev->mutex); 2640 2641 /* sanity check */ 2642 if (!rdev->desc->ops->get_mode) { 2643 ret = -EINVAL; 2644 goto out; 2645 } 2646 2647 ret = rdev->desc->ops->get_mode(rdev); 2648out: 2649 mutex_unlock(&rdev->mutex); 2650 return ret; 2651} 2652 2653/** 2654 * regulator_get_mode - get regulator operating mode 2655 * @regulator: regulator source 2656 * 2657 * Get the current regulator operating mode. 2658 */ 2659unsigned int regulator_get_mode(struct regulator *regulator) 2660{ 2661 return _regulator_get_mode(regulator->rdev); 2662} 2663EXPORT_SYMBOL_GPL(regulator_get_mode); 2664 2665/** 2666 * regulator_set_optimum_mode - set regulator optimum operating mode 2667 * @regulator: regulator source 2668 * @uA_load: load current 2669 * 2670 * Notifies the regulator core of a new device load. This is then used by 2671 * DRMS (if enabled by constraints) to set the most efficient regulator 2672 * operating mode for the new regulator loading. 2673 * 2674 * Consumer devices notify their supply regulator of the maximum power 2675 * they will require (can be taken from device datasheet in the power 2676 * consumption tables) when they change operational status and hence power 2677 * state. Examples of operational state changes that can affect power 2678 * consumption are :- 2679 * 2680 * o Device is opened / closed. 2681 * o Device I/O is about to begin or has just finished. 2682 * o Device is idling in between work. 2683 * 2684 * This information is also exported via sysfs to userspace. 2685 * 2686 * DRMS will sum the total requested load on the regulator and change 2687 * to the most efficient operating mode if platform constraints allow. 2688 * 2689 * Returns the new regulator mode or error. 2690 */ 2691int regulator_set_optimum_mode(struct regulator *regulator, int uA_load) 2692{ 2693 struct regulator_dev *rdev = regulator->rdev; 2694 struct regulator *consumer; 2695 int ret, output_uV, input_uV = 0, total_uA_load = 0; 2696 unsigned int mode; 2697 2698 if (rdev->supply) 2699 input_uV = regulator_get_voltage(rdev->supply); 2700 2701 mutex_lock(&rdev->mutex); 2702 2703 /* 2704 * first check to see if we can set modes at all, otherwise just 2705 * tell the consumer everything is OK. 2706 */ 2707 regulator->uA_load = uA_load; 2708 ret = regulator_check_drms(rdev); 2709 if (ret < 0) { 2710 ret = 0; 2711 goto out; 2712 } 2713 2714 if (!rdev->desc->ops->get_optimum_mode) 2715 goto out; 2716 2717 /* 2718 * we can actually do this so any errors are indicators of 2719 * potential real failure. 2720 */ 2721 ret = -EINVAL; 2722 2723 if (!rdev->desc->ops->set_mode) 2724 goto out; 2725 2726 /* get output voltage */ 2727 output_uV = _regulator_get_voltage(rdev); 2728 if (output_uV <= 0) { 2729 rdev_err(rdev, "invalid output voltage found\n"); 2730 goto out; 2731 } 2732 2733 /* No supply? Use constraint voltage */ 2734 if (input_uV <= 0) 2735 input_uV = rdev->constraints->input_uV; 2736 if (input_uV <= 0) { 2737 rdev_err(rdev, "invalid input voltage found\n"); 2738 goto out; 2739 } 2740 2741 /* calc total requested load for this regulator */ 2742 list_for_each_entry(consumer, &rdev->consumer_list, list) 2743 total_uA_load += consumer->uA_load; 2744 2745 mode = rdev->desc->ops->get_optimum_mode(rdev, 2746 input_uV, output_uV, 2747 total_uA_load); 2748 ret = regulator_mode_constrain(rdev, &mode); 2749 if (ret < 0) { 2750 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", 2751 total_uA_load, input_uV, output_uV); 2752 goto out; 2753 } 2754 2755 ret = rdev->desc->ops->set_mode(rdev, mode); 2756 if (ret < 0) { 2757 rdev_err(rdev, "failed to set optimum mode %x\n", mode); 2758 goto out; 2759 } 2760 ret = mode; 2761out: 2762 mutex_unlock(&rdev->mutex); 2763 return ret; 2764} 2765EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); 2766 2767/** 2768 * regulator_set_bypass_regmap - Default set_bypass() using regmap 2769 * 2770 * @rdev: device to operate on. 2771 * @enable: state to set. 2772 */ 2773int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable) 2774{ 2775 unsigned int val; 2776 2777 if (enable) 2778 val = rdev->desc->bypass_mask; 2779 else 2780 val = 0; 2781 2782 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg, 2783 rdev->desc->bypass_mask, val); 2784} 2785EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap); 2786 2787/** 2788 * regulator_get_bypass_regmap - Default get_bypass() using regmap 2789 * 2790 * @rdev: device to operate on. 2791 * @enable: current state. 2792 */ 2793int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable) 2794{ 2795 unsigned int val; 2796 int ret; 2797 2798 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val); 2799 if (ret != 0) 2800 return ret; 2801 2802 *enable = val & rdev->desc->bypass_mask; 2803 2804 return 0; 2805} 2806EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap); 2807 2808/** 2809 * regulator_allow_bypass - allow the regulator to go into bypass mode 2810 * 2811 * @regulator: Regulator to configure 2812 * @allow: enable or disable bypass mode 2813 * 2814 * Allow the regulator to go into bypass mode if all other consumers 2815 * for the regulator also enable bypass mode and the machine 2816 * constraints allow this. Bypass mode means that the regulator is 2817 * simply passing the input directly to the output with no regulation. 2818 */ 2819int regulator_allow_bypass(struct regulator *regulator, bool enable) 2820{ 2821 struct regulator_dev *rdev = regulator->rdev; 2822 int ret = 0; 2823 2824 if (!rdev->desc->ops->set_bypass) 2825 return 0; 2826 2827 if (rdev->constraints && 2828 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS)) 2829 return 0; 2830 2831 mutex_lock(&rdev->mutex); 2832 2833 if (enable && !regulator->bypass) { 2834 rdev->bypass_count++; 2835 2836 if (rdev->bypass_count == rdev->open_count) { 2837 ret = rdev->desc->ops->set_bypass(rdev, enable); 2838 if (ret != 0) 2839 rdev->bypass_count--; 2840 } 2841 2842 } else if (!enable && regulator->bypass) { 2843 rdev->bypass_count--; 2844 2845 if (rdev->bypass_count != rdev->open_count) { 2846 ret = rdev->desc->ops->set_bypass(rdev, enable); 2847 if (ret != 0) 2848 rdev->bypass_count++; 2849 } 2850 } 2851 2852 if (ret == 0) 2853 regulator->bypass = enable; 2854 2855 mutex_unlock(&rdev->mutex); 2856 2857 return ret; 2858} 2859EXPORT_SYMBOL_GPL(regulator_allow_bypass); 2860 2861/** 2862 * regulator_register_notifier - register regulator event notifier 2863 * @regulator: regulator source 2864 * @nb: notifier block 2865 * 2866 * Register notifier block to receive regulator events. 2867 */ 2868int regulator_register_notifier(struct regulator *regulator, 2869 struct notifier_block *nb) 2870{ 2871 return blocking_notifier_chain_register(®ulator->rdev->notifier, 2872 nb); 2873} 2874EXPORT_SYMBOL_GPL(regulator_register_notifier); 2875 2876/** 2877 * regulator_unregister_notifier - unregister regulator event notifier 2878 * @regulator: regulator source 2879 * @nb: notifier block 2880 * 2881 * Unregister regulator event notifier block. 2882 */ 2883int regulator_unregister_notifier(struct regulator *regulator, 2884 struct notifier_block *nb) 2885{ 2886 return blocking_notifier_chain_unregister(®ulator->rdev->notifier, 2887 nb); 2888} 2889EXPORT_SYMBOL_GPL(regulator_unregister_notifier); 2890 2891/* notify regulator consumers and downstream regulator consumers. 2892 * Note mutex must be held by caller. 2893 */ 2894static void _notifier_call_chain(struct regulator_dev *rdev, 2895 unsigned long event, void *data) 2896{ 2897 /* call rdev chain first */ 2898 blocking_notifier_call_chain(&rdev->notifier, event, data); 2899} 2900 2901/** 2902 * regulator_bulk_get - get multiple regulator consumers 2903 * 2904 * @dev: Device to supply 2905 * @num_consumers: Number of consumers to register 2906 * @consumers: Configuration of consumers; clients are stored here. 2907 * 2908 * @return 0 on success, an errno on failure. 2909 * 2910 * This helper function allows drivers to get several regulator 2911 * consumers in one operation. If any of the regulators cannot be 2912 * acquired then any regulators that were allocated will be freed 2913 * before returning to the caller. 2914 */ 2915int regulator_bulk_get(struct device *dev, int num_consumers, 2916 struct regulator_bulk_data *consumers) 2917{ 2918 int i; 2919 int ret; 2920 2921 for (i = 0; i < num_consumers; i++) 2922 consumers[i].consumer = NULL; 2923 2924 for (i = 0; i < num_consumers; i++) { 2925 consumers[i].consumer = regulator_get(dev, 2926 consumers[i].supply); 2927 if (IS_ERR(consumers[i].consumer)) { 2928 ret = PTR_ERR(consumers[i].consumer); 2929 dev_err(dev, "Failed to get supply '%s': %d\n", 2930 consumers[i].supply, ret); 2931 consumers[i].consumer = NULL; 2932 goto err; 2933 } 2934 } 2935 2936 return 0; 2937 2938err: 2939 while (--i >= 0) 2940 regulator_put(consumers[i].consumer); 2941 2942 return ret; 2943} 2944EXPORT_SYMBOL_GPL(regulator_bulk_get); 2945 2946/** 2947 * devm_regulator_bulk_get - managed get multiple regulator consumers 2948 * 2949 * @dev: Device to supply 2950 * @num_consumers: Number of consumers to register 2951 * @consumers: Configuration of consumers; clients are stored here. 2952 * 2953 * @return 0 on success, an errno on failure. 2954 * 2955 * This helper function allows drivers to get several regulator 2956 * consumers in one operation with management, the regulators will 2957 * automatically be freed when the device is unbound. If any of the 2958 * regulators cannot be acquired then any regulators that were 2959 * allocated will be freed before returning to the caller. 2960 */ 2961int devm_regulator_bulk_get(struct device *dev, int num_consumers, 2962 struct regulator_bulk_data *consumers) 2963{ 2964 int i; 2965 int ret; 2966 2967 for (i = 0; i < num_consumers; i++) 2968 consumers[i].consumer = NULL; 2969 2970 for (i = 0; i < num_consumers; i++) { 2971 consumers[i].consumer = devm_regulator_get(dev, 2972 consumers[i].supply); 2973 if (IS_ERR(consumers[i].consumer)) { 2974 ret = PTR_ERR(consumers[i].consumer); 2975 dev_err(dev, "Failed to get supply '%s': %d\n", 2976 consumers[i].supply, ret); 2977 consumers[i].consumer = NULL; 2978 goto err; 2979 } 2980 } 2981 2982 return 0; 2983 2984err: 2985 for (i = 0; i < num_consumers && consumers[i].consumer; i++) 2986 devm_regulator_put(consumers[i].consumer); 2987 2988 return ret; 2989} 2990EXPORT_SYMBOL_GPL(devm_regulator_bulk_get); 2991 2992static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) 2993{ 2994 struct regulator_bulk_data *bulk = data; 2995 2996 bulk->ret = regulator_enable(bulk->consumer); 2997} 2998 2999/** 3000 * regulator_bulk_enable - enable multiple regulator consumers 3001 * 3002 * @num_consumers: Number of consumers 3003 * @consumers: Consumer data; clients are stored here. 3004 * @return 0 on success, an errno on failure 3005 * 3006 * This convenience API allows consumers to enable multiple regulator 3007 * clients in a single API call. If any consumers cannot be enabled 3008 * then any others that were enabled will be disabled again prior to 3009 * return. 3010 */ 3011int regulator_bulk_enable(int num_consumers, 3012 struct regulator_bulk_data *consumers) 3013{ 3014 ASYNC_DOMAIN_EXCLUSIVE(async_domain); 3015 int i; 3016 int ret = 0; 3017 3018 for (i = 0; i < num_consumers; i++) { 3019 if (consumers[i].consumer->always_on) 3020 consumers[i].ret = 0; 3021 else 3022 async_schedule_domain(regulator_bulk_enable_async, 3023 &consumers[i], &async_domain); 3024 } 3025 3026 async_synchronize_full_domain(&async_domain); 3027 3028 /* If any consumer failed we need to unwind any that succeeded */ 3029 for (i = 0; i < num_consumers; i++) { 3030 if (consumers[i].ret != 0) { 3031 ret = consumers[i].ret; 3032 goto err; 3033 } 3034 } 3035 3036 return 0; 3037 3038err: 3039 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret); 3040 while (--i >= 0) 3041 regulator_disable(consumers[i].consumer); 3042 3043 return ret; 3044} 3045EXPORT_SYMBOL_GPL(regulator_bulk_enable); 3046 3047/** 3048 * regulator_bulk_disable - disable multiple regulator consumers 3049 * 3050 * @num_consumers: Number of consumers 3051 * @consumers: Consumer data; clients are stored here. 3052 * @return 0 on success, an errno on failure 3053 * 3054 * This convenience API allows consumers to disable multiple regulator 3055 * clients in a single API call. If any consumers cannot be disabled 3056 * then any others that were disabled will be enabled again prior to 3057 * return. 3058 */ 3059int regulator_bulk_disable(int num_consumers, 3060 struct regulator_bulk_data *consumers) 3061{ 3062 int i; 3063 int ret, r; 3064 3065 for (i = num_consumers - 1; i >= 0; --i) { 3066 ret = regulator_disable(consumers[i].consumer); 3067 if (ret != 0) 3068 goto err; 3069 } 3070 3071 return 0; 3072 3073err: 3074 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); 3075 for (++i; i < num_consumers; ++i) { 3076 r = regulator_enable(consumers[i].consumer); 3077 if (r != 0) 3078 pr_err("Failed to reename %s: %d\n", 3079 consumers[i].supply, r); 3080 } 3081 3082 return ret; 3083} 3084EXPORT_SYMBOL_GPL(regulator_bulk_disable); 3085 3086/** 3087 * regulator_bulk_force_disable - force disable multiple regulator consumers 3088 * 3089 * @num_consumers: Number of consumers 3090 * @consumers: Consumer data; clients are stored here. 3091 * @return 0 on success, an errno on failure 3092 * 3093 * This convenience API allows consumers to forcibly disable multiple regulator 3094 * clients in a single API call. 3095 * NOTE: This should be used for situations when device damage will 3096 * likely occur if the regulators are not disabled (e.g. over temp). 3097 * Although regulator_force_disable function call for some consumers can 3098 * return error numbers, the function is called for all consumers. 3099 */ 3100int regulator_bulk_force_disable(int num_consumers, 3101 struct regulator_bulk_data *consumers) 3102{ 3103 int i; 3104 int ret; 3105 3106 for (i = 0; i < num_consumers; i++) 3107 consumers[i].ret = 3108 regulator_force_disable(consumers[i].consumer); 3109 3110 for (i = 0; i < num_consumers; i++) { 3111 if (consumers[i].ret != 0) { 3112 ret = consumers[i].ret; 3113 goto out; 3114 } 3115 } 3116 3117 return 0; 3118out: 3119 return ret; 3120} 3121EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); 3122 3123/** 3124 * regulator_bulk_free - free multiple regulator consumers 3125 * 3126 * @num_consumers: Number of consumers 3127 * @consumers: Consumer data; clients are stored here. 3128 * 3129 * This convenience API allows consumers to free multiple regulator 3130 * clients in a single API call. 3131 */ 3132void regulator_bulk_free(int num_consumers, 3133 struct regulator_bulk_data *consumers) 3134{ 3135 int i; 3136 3137 for (i = 0; i < num_consumers; i++) { 3138 regulator_put(consumers[i].consumer); 3139 consumers[i].consumer = NULL; 3140 } 3141} 3142EXPORT_SYMBOL_GPL(regulator_bulk_free); 3143 3144/** 3145 * regulator_notifier_call_chain - call regulator event notifier 3146 * @rdev: regulator source 3147 * @event: notifier block 3148 * @data: callback-specific data. 3149 * 3150 * Called by regulator drivers to notify clients a regulator event has 3151 * occurred. We also notify regulator clients downstream. 3152 * Note lock must be held by caller. 3153 */ 3154int regulator_notifier_call_chain(struct regulator_dev *rdev, 3155 unsigned long event, void *data) 3156{ 3157 _notifier_call_chain(rdev, event, data); 3158 return NOTIFY_DONE; 3159 3160} 3161EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); 3162 3163/** 3164 * regulator_mode_to_status - convert a regulator mode into a status 3165 * 3166 * @mode: Mode to convert 3167 * 3168 * Convert a regulator mode into a status. 3169 */ 3170int regulator_mode_to_status(unsigned int mode) 3171{ 3172 switch (mode) { 3173 case REGULATOR_MODE_FAST: 3174 return REGULATOR_STATUS_FAST; 3175 case REGULATOR_MODE_NORMAL: 3176 return REGULATOR_STATUS_NORMAL; 3177 case REGULATOR_MODE_IDLE: 3178 return REGULATOR_STATUS_IDLE; 3179 case REGULATOR_MODE_STANDBY: 3180 return REGULATOR_STATUS_STANDBY; 3181 default: 3182 return REGULATOR_STATUS_UNDEFINED; 3183 } 3184} 3185EXPORT_SYMBOL_GPL(regulator_mode_to_status); 3186 3187/* 3188 * To avoid cluttering sysfs (and memory) with useless state, only 3189 * create attributes that can be meaningfully displayed. 3190 */ 3191static int add_regulator_attributes(struct regulator_dev *rdev) 3192{ 3193 struct device *dev = &rdev->dev; 3194 struct regulator_ops *ops = rdev->desc->ops; 3195 int status = 0; 3196 3197 /* some attributes need specific methods to be displayed */ 3198 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || 3199 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || 3200 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) { 3201 status = device_create_file(dev, &dev_attr_microvolts); 3202 if (status < 0) 3203 return status; 3204 } 3205 if (ops->get_current_limit) { 3206 status = device_create_file(dev, &dev_attr_microamps); 3207 if (status < 0) 3208 return status; 3209 } 3210 if (ops->get_mode) { 3211 status = device_create_file(dev, &dev_attr_opmode); 3212 if (status < 0) 3213 return status; 3214 } 3215 if (ops->is_enabled) { 3216 status = device_create_file(dev, &dev_attr_state); 3217 if (status < 0) 3218 return status; 3219 } 3220 if (ops->get_status) { 3221 status = device_create_file(dev, &dev_attr_status); 3222 if (status < 0) 3223 return status; 3224 } 3225 if (ops->get_bypass) { 3226 status = device_create_file(dev, &dev_attr_bypass); 3227 if (status < 0) 3228 return status; 3229 } 3230 3231 /* some attributes are type-specific */ 3232 if (rdev->desc->type == REGULATOR_CURRENT) { 3233 status = device_create_file(dev, &dev_attr_requested_microamps); 3234 if (status < 0) 3235 return status; 3236 } 3237 3238 /* all the other attributes exist to support constraints; 3239 * don't show them if there are no constraints, or if the 3240 * relevant supporting methods are missing. 3241 */ 3242 if (!rdev->constraints) 3243 return status; 3244 3245 /* constraints need specific supporting methods */ 3246 if (ops->set_voltage || ops->set_voltage_sel) { 3247 status = device_create_file(dev, &dev_attr_min_microvolts); 3248 if (status < 0) 3249 return status; 3250 status = device_create_file(dev, &dev_attr_max_microvolts); 3251 if (status < 0) 3252 return status; 3253 } 3254 if (ops->set_current_limit) { 3255 status = device_create_file(dev, &dev_attr_min_microamps); 3256 if (status < 0) 3257 return status; 3258 status = device_create_file(dev, &dev_attr_max_microamps); 3259 if (status < 0) 3260 return status; 3261 } 3262 3263 status = device_create_file(dev, &dev_attr_suspend_standby_state); 3264 if (status < 0) 3265 return status; 3266 status = device_create_file(dev, &dev_attr_suspend_mem_state); 3267 if (status < 0) 3268 return status; 3269 status = device_create_file(dev, &dev_attr_suspend_disk_state); 3270 if (status < 0) 3271 return status; 3272 3273 if (ops->set_suspend_voltage) { 3274 status = device_create_file(dev, 3275 &dev_attr_suspend_standby_microvolts); 3276 if (status < 0) 3277 return status; 3278 status = device_create_file(dev, 3279 &dev_attr_suspend_mem_microvolts); 3280 if (status < 0) 3281 return status; 3282 status = device_create_file(dev, 3283 &dev_attr_suspend_disk_microvolts); 3284 if (status < 0) 3285 return status; 3286 } 3287 3288 if (ops->set_suspend_mode) { 3289 status = device_create_file(dev, 3290 &dev_attr_suspend_standby_mode); 3291 if (status < 0) 3292 return status; 3293 status = device_create_file(dev, 3294 &dev_attr_suspend_mem_mode); 3295 if (status < 0) 3296 return status; 3297 status = device_create_file(dev, 3298 &dev_attr_suspend_disk_mode); 3299 if (status < 0) 3300 return status; 3301 } 3302 3303 return status; 3304} 3305 3306static void rdev_init_debugfs(struct regulator_dev *rdev) 3307{ 3308 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root); 3309 if (!rdev->debugfs) { 3310 rdev_warn(rdev, "Failed to create debugfs directory\n"); 3311 return; 3312 } 3313 3314 debugfs_create_u32("use_count", 0444, rdev->debugfs, 3315 &rdev->use_count); 3316 debugfs_create_u32("open_count", 0444, rdev->debugfs, 3317 &rdev->open_count); 3318 debugfs_create_u32("bypass_count", 0444, rdev->debugfs, 3319 &rdev->bypass_count); 3320} 3321 3322/** 3323 * regulator_register - register regulator 3324 * @regulator_desc: regulator to register 3325 * @config: runtime configuration for regulator 3326 * 3327 * Called by regulator drivers to register a regulator. 3328 * Returns 0 on success. 3329 */ 3330struct regulator_dev * 3331regulator_register(const struct regulator_desc *regulator_desc, 3332 const struct regulator_config *config) 3333{ 3334 const struct regulation_constraints *constraints = NULL; 3335 const struct regulator_init_data *init_data; 3336 static atomic_t regulator_no = ATOMIC_INIT(0); 3337 struct regulator_dev *rdev; 3338 struct device *dev; 3339 int ret, i; 3340 const char *supply = NULL; 3341 3342 if (regulator_desc == NULL || config == NULL) 3343 return ERR_PTR(-EINVAL); 3344 3345 dev = config->dev; 3346 WARN_ON(!dev); 3347 3348 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) 3349 return ERR_PTR(-EINVAL); 3350 3351 if (regulator_desc->type != REGULATOR_VOLTAGE && 3352 regulator_desc->type != REGULATOR_CURRENT) 3353 return ERR_PTR(-EINVAL); 3354 3355 /* Only one of each should be implemented */ 3356 WARN_ON(regulator_desc->ops->get_voltage && 3357 regulator_desc->ops->get_voltage_sel); 3358 WARN_ON(regulator_desc->ops->set_voltage && 3359 regulator_desc->ops->set_voltage_sel); 3360 3361 /* If we're using selectors we must implement list_voltage. */ 3362 if (regulator_desc->ops->get_voltage_sel && 3363 !regulator_desc->ops->list_voltage) { 3364 return ERR_PTR(-EINVAL); 3365 } 3366 if (regulator_desc->ops->set_voltage_sel && 3367 !regulator_desc->ops->list_voltage) { 3368 return ERR_PTR(-EINVAL); 3369 } 3370 3371 init_data = config->init_data; 3372 3373 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); 3374 if (rdev == NULL) 3375 return ERR_PTR(-ENOMEM); 3376 3377 mutex_lock(®ulator_list_mutex); 3378 3379 mutex_init(&rdev->mutex); 3380 rdev->reg_data = config->driver_data; 3381 rdev->owner = regulator_desc->owner; 3382 rdev->desc = regulator_desc; 3383 if (config->regmap) 3384 rdev->regmap = config->regmap; 3385 else if (dev_get_regmap(dev, NULL)) 3386 rdev->regmap = dev_get_regmap(dev, NULL); 3387 else if (dev->parent) 3388 rdev->regmap = dev_get_regmap(dev->parent, NULL); 3389 INIT_LIST_HEAD(&rdev->consumer_list); 3390 INIT_LIST_HEAD(&rdev->list); 3391 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); 3392 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); 3393 3394 /* preform any regulator specific init */ 3395 if (init_data && init_data->regulator_init) { 3396 ret = init_data->regulator_init(rdev->reg_data); 3397 if (ret < 0) 3398 goto clean; 3399 } 3400 3401 /* register with sysfs */ 3402 rdev->dev.class = ®ulator_class; 3403 rdev->dev.of_node = config->of_node; 3404 rdev->dev.parent = dev; 3405 dev_set_name(&rdev->dev, "regulator.%d", 3406 atomic_inc_return(®ulator_no) - 1); 3407 ret = device_register(&rdev->dev); 3408 if (ret != 0) { 3409 put_device(&rdev->dev); 3410 goto clean; 3411 } 3412 3413 dev_set_drvdata(&rdev->dev, rdev); 3414 3415 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) { 3416 ret = gpio_request_one(config->ena_gpio, 3417 GPIOF_DIR_OUT | config->ena_gpio_flags, 3418 rdev_get_name(rdev)); 3419 if (ret != 0) { 3420 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n", 3421 config->ena_gpio, ret); 3422 goto wash; 3423 } 3424 3425 rdev->ena_gpio = config->ena_gpio; 3426 rdev->ena_gpio_invert = config->ena_gpio_invert; 3427 3428 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH) 3429 rdev->ena_gpio_state = 1; 3430 3431 if (rdev->ena_gpio_invert) 3432 rdev->ena_gpio_state = !rdev->ena_gpio_state; 3433 } 3434 3435 /* set regulator constraints */ 3436 if (init_data) 3437 constraints = &init_data->constraints; 3438 3439 ret = set_machine_constraints(rdev, constraints); 3440 if (ret < 0) 3441 goto scrub; 3442 3443 /* add attributes supported by this regulator */ 3444 ret = add_regulator_attributes(rdev); 3445 if (ret < 0) 3446 goto scrub; 3447 3448 if (init_data && init_data->supply_regulator) 3449 supply = init_data->supply_regulator; 3450 else if (regulator_desc->supply_name) 3451 supply = regulator_desc->supply_name; 3452 3453 if (supply) { 3454 struct regulator_dev *r; 3455 3456 r = regulator_dev_lookup(dev, supply, &ret); 3457 3458 if (!r) { 3459 dev_err(dev, "Failed to find supply %s\n", supply); 3460 ret = -EPROBE_DEFER; 3461 goto scrub; 3462 } 3463 3464 ret = set_supply(rdev, r); 3465 if (ret < 0) 3466 goto scrub; 3467 3468 /* Enable supply if rail is enabled */ 3469 if (_regulator_is_enabled(rdev)) { 3470 ret = regulator_enable(rdev->supply); 3471 if (ret < 0) 3472 goto scrub; 3473 } 3474 } 3475 3476 /* add consumers devices */ 3477 if (init_data) { 3478 for (i = 0; i < init_data->num_consumer_supplies; i++) { 3479 ret = set_consumer_device_supply(rdev, 3480 init_data->consumer_supplies[i].dev_name, 3481 init_data->consumer_supplies[i].supply); 3482 if (ret < 0) { 3483 dev_err(dev, "Failed to set supply %s\n", 3484 init_data->consumer_supplies[i].supply); 3485 goto unset_supplies; 3486 } 3487 } 3488 } 3489 3490 list_add(&rdev->list, ®ulator_list); 3491 3492 rdev_init_debugfs(rdev); 3493out: 3494 mutex_unlock(®ulator_list_mutex); 3495 return rdev; 3496 3497unset_supplies: 3498 unset_regulator_supplies(rdev); 3499 3500scrub: 3501 if (rdev->supply) 3502 _regulator_put(rdev->supply); 3503 if (rdev->ena_gpio) 3504 gpio_free(rdev->ena_gpio); 3505 kfree(rdev->constraints); 3506wash: 3507 device_unregister(&rdev->dev); 3508 /* device core frees rdev */ 3509 rdev = ERR_PTR(ret); 3510 goto out; 3511 3512clean: 3513 kfree(rdev); 3514 rdev = ERR_PTR(ret); 3515 goto out; 3516} 3517EXPORT_SYMBOL_GPL(regulator_register); 3518 3519/** 3520 * regulator_unregister - unregister regulator 3521 * @rdev: regulator to unregister 3522 * 3523 * Called by regulator drivers to unregister a regulator. 3524 */ 3525void regulator_unregister(struct regulator_dev *rdev) 3526{ 3527 if (rdev == NULL) 3528 return; 3529 3530 if (rdev->supply) 3531 regulator_put(rdev->supply); 3532 mutex_lock(®ulator_list_mutex); 3533 debugfs_remove_recursive(rdev->debugfs); 3534 flush_work(&rdev->disable_work.work); 3535 WARN_ON(rdev->open_count); 3536 unset_regulator_supplies(rdev); 3537 list_del(&rdev->list); 3538 kfree(rdev->constraints); 3539 if (rdev->ena_gpio) 3540 gpio_free(rdev->ena_gpio); 3541 device_unregister(&rdev->dev); 3542 mutex_unlock(®ulator_list_mutex); 3543} 3544EXPORT_SYMBOL_GPL(regulator_unregister); 3545 3546/** 3547 * regulator_suspend_prepare - prepare regulators for system wide suspend 3548 * @state: system suspend state 3549 * 3550 * Configure each regulator with it's suspend operating parameters for state. 3551 * This will usually be called by machine suspend code prior to supending. 3552 */ 3553int regulator_suspend_prepare(suspend_state_t state) 3554{ 3555 struct regulator_dev *rdev; 3556 int ret = 0; 3557 3558 /* ON is handled by regulator active state */ 3559 if (state == PM_SUSPEND_ON) 3560 return -EINVAL; 3561 3562 mutex_lock(®ulator_list_mutex); 3563 list_for_each_entry(rdev, ®ulator_list, list) { 3564 3565 mutex_lock(&rdev->mutex); 3566 ret = suspend_prepare(rdev, state); 3567 mutex_unlock(&rdev->mutex); 3568 3569 if (ret < 0) { 3570 rdev_err(rdev, "failed to prepare\n"); 3571 goto out; 3572 } 3573 } 3574out: 3575 mutex_unlock(®ulator_list_mutex); 3576 return ret; 3577} 3578EXPORT_SYMBOL_GPL(regulator_suspend_prepare); 3579 3580/** 3581 * regulator_suspend_finish - resume regulators from system wide suspend 3582 * 3583 * Turn on regulators that might be turned off by regulator_suspend_prepare 3584 * and that should be turned on according to the regulators properties. 3585 */ 3586int regulator_suspend_finish(void) 3587{ 3588 struct regulator_dev *rdev; 3589 int ret = 0, error; 3590 3591 mutex_lock(®ulator_list_mutex); 3592 list_for_each_entry(rdev, ®ulator_list, list) { 3593 struct regulator_ops *ops = rdev->desc->ops; 3594 3595 mutex_lock(&rdev->mutex); 3596 if ((rdev->use_count > 0 || rdev->constraints->always_on) && 3597 ops->enable) { 3598 error = ops->enable(rdev); 3599 if (error) 3600 ret = error; 3601 } else { 3602 if (!has_full_constraints) 3603 goto unlock; 3604 if (!ops->disable) 3605 goto unlock; 3606 if (!_regulator_is_enabled(rdev)) 3607 goto unlock; 3608 3609 error = ops->disable(rdev); 3610 if (error) 3611 ret = error; 3612 } 3613unlock: 3614 mutex_unlock(&rdev->mutex); 3615 } 3616 mutex_unlock(®ulator_list_mutex); 3617 return ret; 3618} 3619EXPORT_SYMBOL_GPL(regulator_suspend_finish); 3620 3621/** 3622 * regulator_has_full_constraints - the system has fully specified constraints 3623 * 3624 * Calling this function will cause the regulator API to disable all 3625 * regulators which have a zero use count and don't have an always_on 3626 * constraint in a late_initcall. 3627 * 3628 * The intention is that this will become the default behaviour in a 3629 * future kernel release so users are encouraged to use this facility 3630 * now. 3631 */ 3632void regulator_has_full_constraints(void) 3633{ 3634 has_full_constraints = 1; 3635} 3636EXPORT_SYMBOL_GPL(regulator_has_full_constraints); 3637 3638/** 3639 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found 3640 * 3641 * Calling this function will cause the regulator API to provide a 3642 * dummy regulator to consumers if no physical regulator is found, 3643 * allowing most consumers to proceed as though a regulator were 3644 * configured. This allows systems such as those with software 3645 * controllable regulators for the CPU core only to be brought up more 3646 * readily. 3647 */ 3648void regulator_use_dummy_regulator(void) 3649{ 3650 board_wants_dummy_regulator = true; 3651} 3652EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator); 3653 3654/** 3655 * rdev_get_drvdata - get rdev regulator driver data 3656 * @rdev: regulator 3657 * 3658 * Get rdev regulator driver private data. This call can be used in the 3659 * regulator driver context. 3660 */ 3661void *rdev_get_drvdata(struct regulator_dev *rdev) 3662{ 3663 return rdev->reg_data; 3664} 3665EXPORT_SYMBOL_GPL(rdev_get_drvdata); 3666 3667/** 3668 * regulator_get_drvdata - get regulator driver data 3669 * @regulator: regulator 3670 * 3671 * Get regulator driver private data. This call can be used in the consumer 3672 * driver context when non API regulator specific functions need to be called. 3673 */ 3674void *regulator_get_drvdata(struct regulator *regulator) 3675{ 3676 return regulator->rdev->reg_data; 3677} 3678EXPORT_SYMBOL_GPL(regulator_get_drvdata); 3679 3680/** 3681 * regulator_set_drvdata - set regulator driver data 3682 * @regulator: regulator 3683 * @data: data 3684 */ 3685void regulator_set_drvdata(struct regulator *regulator, void *data) 3686{ 3687 regulator->rdev->reg_data = data; 3688} 3689EXPORT_SYMBOL_GPL(regulator_set_drvdata); 3690 3691/** 3692 * regulator_get_id - get regulator ID 3693 * @rdev: regulator 3694 */ 3695int rdev_get_id(struct regulator_dev *rdev) 3696{ 3697 return rdev->desc->id; 3698} 3699EXPORT_SYMBOL_GPL(rdev_get_id); 3700 3701struct device *rdev_get_dev(struct regulator_dev *rdev) 3702{ 3703 return &rdev->dev; 3704} 3705EXPORT_SYMBOL_GPL(rdev_get_dev); 3706 3707void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) 3708{ 3709 return reg_init_data->driver_data; 3710} 3711EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); 3712 3713#ifdef CONFIG_DEBUG_FS 3714static ssize_t supply_map_read_file(struct file *file, char __user *user_buf, 3715 size_t count, loff_t *ppos) 3716{ 3717 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3718 ssize_t len, ret = 0; 3719 struct regulator_map *map; 3720 3721 if (!buf) 3722 return -ENOMEM; 3723 3724 list_for_each_entry(map, ®ulator_map_list, list) { 3725 len = snprintf(buf + ret, PAGE_SIZE - ret, 3726 "%s -> %s.%s\n", 3727 rdev_get_name(map->regulator), map->dev_name, 3728 map->supply); 3729 if (len >= 0) 3730 ret += len; 3731 if (ret > PAGE_SIZE) { 3732 ret = PAGE_SIZE; 3733 break; 3734 } 3735 } 3736 3737 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 3738 3739 kfree(buf); 3740 3741 return ret; 3742} 3743#endif 3744 3745static const struct file_operations supply_map_fops = { 3746#ifdef CONFIG_DEBUG_FS 3747 .read = supply_map_read_file, 3748 .llseek = default_llseek, 3749#endif 3750}; 3751 3752static int __init regulator_init(void) 3753{ 3754 int ret; 3755 3756 ret = class_register(®ulator_class); 3757 3758 debugfs_root = debugfs_create_dir("regulator", NULL); 3759 if (!debugfs_root) 3760 pr_warn("regulator: Failed to create debugfs directory\n"); 3761 3762 debugfs_create_file("supply_map", 0444, debugfs_root, NULL, 3763 &supply_map_fops); 3764 3765 regulator_dummy_init(); 3766 3767 return ret; 3768} 3769 3770/* init early to allow our consumers to complete system booting */ 3771core_initcall(regulator_init); 3772 3773static int __init regulator_init_complete(void) 3774{ 3775 struct regulator_dev *rdev; 3776 struct regulator_ops *ops; 3777 struct regulation_constraints *c; 3778 int enabled, ret; 3779 3780 /* 3781 * Since DT doesn't provide an idiomatic mechanism for 3782 * enabling full constraints and since it's much more natural 3783 * with DT to provide them just assume that a DT enabled 3784 * system has full constraints. 3785 */ 3786 if (of_have_populated_dt()) 3787 has_full_constraints = true; 3788 3789 mutex_lock(®ulator_list_mutex); 3790 3791 /* If we have a full configuration then disable any regulators 3792 * which are not in use or always_on. This will become the 3793 * default behaviour in the future. 3794 */ 3795 list_for_each_entry(rdev, ®ulator_list, list) { 3796 ops = rdev->desc->ops; 3797 c = rdev->constraints; 3798 3799 if (!ops->disable || (c && c->always_on)) 3800 continue; 3801 3802 mutex_lock(&rdev->mutex); 3803 3804 if (rdev->use_count) 3805 goto unlock; 3806 3807 /* If we can't read the status assume it's on. */ 3808 if (ops->is_enabled) 3809 enabled = ops->is_enabled(rdev); 3810 else 3811 enabled = 1; 3812 3813 if (!enabled) 3814 goto unlock; 3815 3816 if (has_full_constraints) { 3817 /* We log since this may kill the system if it 3818 * goes wrong. */ 3819 rdev_info(rdev, "disabling\n"); 3820 ret = ops->disable(rdev); 3821 if (ret != 0) { 3822 rdev_err(rdev, "couldn't disable: %d\n", ret); 3823 } 3824 } else { 3825 /* The intention is that in future we will 3826 * assume that full constraints are provided 3827 * so warn even if we aren't going to do 3828 * anything here. 3829 */ 3830 rdev_warn(rdev, "incomplete constraints, leaving on\n"); 3831 } 3832 3833unlock: 3834 mutex_unlock(&rdev->mutex); 3835 } 3836 3837 mutex_unlock(®ulator_list_mutex); 3838 3839 return 0; 3840} 3841late_initcall(regulator_init_complete); 3842