1// SPDX-License-Identifier: GPL-2.0-or-later 2// 3// core.c -- Voltage/Current Regulator framework. 4// 5// Copyright 2007, 2008 Wolfson Microelectronics PLC. 6// Copyright 2008 SlimLogic Ltd. 7// 8// Author: Liam Girdwood <lrg@slimlogic.co.uk> 9 10#include <linux/kernel.h> 11#include <linux/init.h> 12#include <linux/debugfs.h> 13#include <linux/device.h> 14#include <linux/slab.h> 15#include <linux/async.h> 16#include <linux/err.h> 17#include <linux/mutex.h> 18#include <linux/suspend.h> 19#include <linux/delay.h> 20#include <linux/gpio/consumer.h> 21#include <linux/of.h> 22#include <linux/regmap.h> 23#include <linux/regulator/of_regulator.h> 24#include <linux/regulator/consumer.h> 25#include <linux/regulator/coupler.h> 26#include <linux/regulator/driver.h> 27#include <linux/regulator/machine.h> 28#include <linux/module.h> 29 30#define CREATE_TRACE_POINTS 31#include <trace/events/regulator.h> 32 33#include "dummy.h" 34#include "internal.h" 35 36#define rdev_crit(rdev, fmt, ...) \ 37 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 38#define rdev_err(rdev, fmt, ...) \ 39 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 40#define rdev_warn(rdev, fmt, ...) \ 41 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 42#define rdev_info(rdev, fmt, ...) \ 43 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 44#define rdev_dbg(rdev, fmt, ...) \ 45 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 46 47static DEFINE_WW_CLASS(regulator_ww_class); 48static DEFINE_MUTEX(regulator_nesting_mutex); 49static DEFINE_MUTEX(regulator_list_mutex); 50static LIST_HEAD(regulator_map_list); 51static LIST_HEAD(regulator_ena_gpio_list); 52static LIST_HEAD(regulator_supply_alias_list); 53static LIST_HEAD(regulator_coupler_list); 54static bool has_full_constraints; 55 56static struct dentry *debugfs_root; 57 58/* 59 * struct regulator_map 60 * 61 * Used to provide symbolic supply names to devices. 62 */ 63struct regulator_map { 64 struct list_head list; 65 const char *dev_name; /* The dev_name() for the consumer */ 66 const char *supply; 67 struct regulator_dev *regulator; 68}; 69 70/* 71 * struct regulator_enable_gpio 72 * 73 * Management for shared enable GPIO pin 74 */ 75struct regulator_enable_gpio { 76 struct list_head list; 77 struct gpio_desc *gpiod; 78 u32 enable_count; /* a number of enabled shared GPIO */ 79 u32 request_count; /* a number of requested shared GPIO */ 80}; 81 82/* 83 * struct regulator_supply_alias 84 * 85 * Used to map lookups for a supply onto an alternative device. 86 */ 87struct regulator_supply_alias { 88 struct list_head list; 89 struct device *src_dev; 90 const char *src_supply; 91 struct device *alias_dev; 92 const char *alias_supply; 93}; 94 95static int _regulator_is_enabled(struct regulator_dev *rdev); 96static int _regulator_disable(struct regulator *regulator); 97static int _regulator_get_current_limit(struct regulator_dev *rdev); 98static unsigned int _regulator_get_mode(struct regulator_dev *rdev); 99static int _notifier_call_chain(struct regulator_dev *rdev, 100 unsigned long event, void *data); 101static int _regulator_do_set_voltage(struct regulator_dev *rdev, 102 int min_uV, int max_uV); 103static int regulator_balance_voltage(struct regulator_dev *rdev, 104 suspend_state_t state); 105static struct regulator *create_regulator(struct regulator_dev *rdev, 106 struct device *dev, 107 const char *supply_name); 108static void destroy_regulator(struct regulator *regulator); 109static void _regulator_put(struct regulator *regulator); 110 111const char *rdev_get_name(struct regulator_dev *rdev) 112{ 113 if (rdev->constraints && rdev->constraints->name) 114 return rdev->constraints->name; 115 else if (rdev->desc->name) 116 return rdev->desc->name; 117 else 118 return ""; 119} 120 121static bool have_full_constraints(void) 122{ 123 return has_full_constraints || of_have_populated_dt(); 124} 125 126static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops) 127{ 128 if (!rdev->constraints) { 129 rdev_err(rdev, "no constraints\n"); 130 return false; 131 } 132 133 if (rdev->constraints->valid_ops_mask & ops) 134 return true; 135 136 return false; 137} 138 139/** 140 * regulator_lock_nested - lock a single regulator 141 * @rdev: regulator source 142 * @ww_ctx: w/w mutex acquire context 143 * 144 * This function can be called many times by one task on 145 * a single regulator and its mutex will be locked only 146 * once. If a task, which is calling this function is other 147 * than the one, which initially locked the mutex, it will 148 * wait on mutex. 149 */ 150static inline int regulator_lock_nested(struct regulator_dev *rdev, 151 struct ww_acquire_ctx *ww_ctx) 152{ 153 bool lock = false; 154 int ret = 0; 155 156 mutex_lock(®ulator_nesting_mutex); 157 158 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) { 159 if (rdev->mutex_owner == current) 160 rdev->ref_cnt++; 161 else 162 lock = true; 163 164 if (lock) { 165 mutex_unlock(®ulator_nesting_mutex); 166 ret = ww_mutex_lock(&rdev->mutex, ww_ctx); 167 mutex_lock(®ulator_nesting_mutex); 168 } 169 } else { 170 lock = true; 171 } 172 173 if (lock && ret != -EDEADLK) { 174 rdev->ref_cnt++; 175 rdev->mutex_owner = current; 176 } 177 178 mutex_unlock(®ulator_nesting_mutex); 179 180 return ret; 181} 182 183/** 184 * regulator_lock - lock a single regulator 185 * @rdev: regulator source 186 * 187 * This function can be called many times by one task on 188 * a single regulator and its mutex will be locked only 189 * once. If a task, which is calling this function is other 190 * than the one, which initially locked the mutex, it will 191 * wait on mutex. 192 */ 193static void regulator_lock(struct regulator_dev *rdev) 194{ 195 regulator_lock_nested(rdev, NULL); 196} 197 198/** 199 * regulator_unlock - unlock a single regulator 200 * @rdev: regulator_source 201 * 202 * This function unlocks the mutex when the 203 * reference counter reaches 0. 204 */ 205static void regulator_unlock(struct regulator_dev *rdev) 206{ 207 mutex_lock(®ulator_nesting_mutex); 208 209 if (--rdev->ref_cnt == 0) { 210 rdev->mutex_owner = NULL; 211 ww_mutex_unlock(&rdev->mutex); 212 } 213 214 WARN_ON_ONCE(rdev->ref_cnt < 0); 215 216 mutex_unlock(®ulator_nesting_mutex); 217} 218 219/** 220 * regulator_lock_two - lock two regulators 221 * @rdev1: first regulator 222 * @rdev2: second regulator 223 * @ww_ctx: w/w mutex acquire context 224 * 225 * Locks both rdevs using the regulator_ww_class. 226 */ 227static void regulator_lock_two(struct regulator_dev *rdev1, 228 struct regulator_dev *rdev2, 229 struct ww_acquire_ctx *ww_ctx) 230{ 231 struct regulator_dev *tmp; 232 int ret; 233 234 ww_acquire_init(ww_ctx, ®ulator_ww_class); 235 236 /* Try to just grab both of them */ 237 ret = regulator_lock_nested(rdev1, ww_ctx); 238 WARN_ON(ret); 239 ret = regulator_lock_nested(rdev2, ww_ctx); 240 if (ret != -EDEADLOCK) { 241 WARN_ON(ret); 242 goto exit; 243 } 244 245 while (true) { 246 /* 247 * Start of loop: rdev1 was locked and rdev2 was contended. 248 * Need to unlock rdev1, slowly lock rdev2, then try rdev1 249 * again. 250 */ 251 regulator_unlock(rdev1); 252 253 ww_mutex_lock_slow(&rdev2->mutex, ww_ctx); 254 rdev2->ref_cnt++; 255 rdev2->mutex_owner = current; 256 ret = regulator_lock_nested(rdev1, ww_ctx); 257 258 if (ret == -EDEADLOCK) { 259 /* More contention; swap which needs to be slow */ 260 tmp = rdev1; 261 rdev1 = rdev2; 262 rdev2 = tmp; 263 } else { 264 WARN_ON(ret); 265 break; 266 } 267 } 268 269exit: 270 ww_acquire_done(ww_ctx); 271} 272 273/** 274 * regulator_unlock_two - unlock two regulators 275 * @rdev1: first regulator 276 * @rdev2: second regulator 277 * @ww_ctx: w/w mutex acquire context 278 * 279 * The inverse of regulator_lock_two(). 280 */ 281 282static void regulator_unlock_two(struct regulator_dev *rdev1, 283 struct regulator_dev *rdev2, 284 struct ww_acquire_ctx *ww_ctx) 285{ 286 regulator_unlock(rdev2); 287 regulator_unlock(rdev1); 288 ww_acquire_fini(ww_ctx); 289} 290 291static bool regulator_supply_is_couple(struct regulator_dev *rdev) 292{ 293 struct regulator_dev *c_rdev; 294 int i; 295 296 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) { 297 c_rdev = rdev->coupling_desc.coupled_rdevs[i]; 298 299 if (rdev->supply->rdev == c_rdev) 300 return true; 301 } 302 303 return false; 304} 305 306static void regulator_unlock_recursive(struct regulator_dev *rdev, 307 unsigned int n_coupled) 308{ 309 struct regulator_dev *c_rdev, *supply_rdev; 310 int i, supply_n_coupled; 311 312 for (i = n_coupled; i > 0; i--) { 313 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1]; 314 315 if (!c_rdev) 316 continue; 317 318 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { 319 supply_rdev = c_rdev->supply->rdev; 320 supply_n_coupled = supply_rdev->coupling_desc.n_coupled; 321 322 regulator_unlock_recursive(supply_rdev, 323 supply_n_coupled); 324 } 325 326 regulator_unlock(c_rdev); 327 } 328} 329 330static int regulator_lock_recursive(struct regulator_dev *rdev, 331 struct regulator_dev **new_contended_rdev, 332 struct regulator_dev **old_contended_rdev, 333 struct ww_acquire_ctx *ww_ctx) 334{ 335 struct regulator_dev *c_rdev; 336 int i, err; 337 338 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) { 339 c_rdev = rdev->coupling_desc.coupled_rdevs[i]; 340 341 if (!c_rdev) 342 continue; 343 344 if (c_rdev != *old_contended_rdev) { 345 err = regulator_lock_nested(c_rdev, ww_ctx); 346 if (err) { 347 if (err == -EDEADLK) { 348 *new_contended_rdev = c_rdev; 349 goto err_unlock; 350 } 351 352 /* shouldn't happen */ 353 WARN_ON_ONCE(err != -EALREADY); 354 } 355 } else { 356 *old_contended_rdev = NULL; 357 } 358 359 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) { 360 err = regulator_lock_recursive(c_rdev->supply->rdev, 361 new_contended_rdev, 362 old_contended_rdev, 363 ww_ctx); 364 if (err) { 365 regulator_unlock(c_rdev); 366 goto err_unlock; 367 } 368 } 369 } 370 371 return 0; 372 373err_unlock: 374 regulator_unlock_recursive(rdev, i); 375 376 return err; 377} 378 379/** 380 * regulator_unlock_dependent - unlock regulator's suppliers and coupled 381 * regulators 382 * @rdev: regulator source 383 * @ww_ctx: w/w mutex acquire context 384 * 385 * Unlock all regulators related with rdev by coupling or supplying. 386 */ 387static void regulator_unlock_dependent(struct regulator_dev *rdev, 388 struct ww_acquire_ctx *ww_ctx) 389{ 390 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled); 391 ww_acquire_fini(ww_ctx); 392} 393 394/** 395 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators 396 * @rdev: regulator source 397 * @ww_ctx: w/w mutex acquire context 398 * 399 * This function as a wrapper on regulator_lock_recursive(), which locks 400 * all regulators related with rdev by coupling or supplying. 401 */ 402static void regulator_lock_dependent(struct regulator_dev *rdev, 403 struct ww_acquire_ctx *ww_ctx) 404{ 405 struct regulator_dev *new_contended_rdev = NULL; 406 struct regulator_dev *old_contended_rdev = NULL; 407 int err; 408 409 mutex_lock(®ulator_list_mutex); 410 411 ww_acquire_init(ww_ctx, ®ulator_ww_class); 412 413 do { 414 if (new_contended_rdev) { 415 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); 416 old_contended_rdev = new_contended_rdev; 417 old_contended_rdev->ref_cnt++; 418 old_contended_rdev->mutex_owner = current; 419 } 420 421 err = regulator_lock_recursive(rdev, 422 &new_contended_rdev, 423 &old_contended_rdev, 424 ww_ctx); 425 426 if (old_contended_rdev) 427 regulator_unlock(old_contended_rdev); 428 429 } while (err == -EDEADLK); 430 431 ww_acquire_done(ww_ctx); 432 433 mutex_unlock(®ulator_list_mutex); 434} 435 436/** 437 * of_get_child_regulator - get a child regulator device node 438 * based on supply name 439 * @parent: Parent device node 440 * @prop_name: Combination regulator supply name and "-supply" 441 * 442 * Traverse all child nodes. 443 * Extract the child regulator device node corresponding to the supply name. 444 * returns the device node corresponding to the regulator if found, else 445 * returns NULL. 446 */ 447static struct device_node *of_get_child_regulator(struct device_node *parent, 448 const char *prop_name) 449{ 450 struct device_node *regnode = NULL; 451 struct device_node *child = NULL; 452 453 for_each_child_of_node(parent, child) { 454 regnode = of_parse_phandle(child, prop_name, 0); 455 456 if (!regnode) { 457 regnode = of_get_child_regulator(child, prop_name); 458 if (regnode) 459 goto err_node_put; 460 } else { 461 goto err_node_put; 462 } 463 } 464 return NULL; 465 466err_node_put: 467 of_node_put(child); 468 return regnode; 469} 470 471/** 472 * of_get_regulator - get a regulator device node based on supply name 473 * @dev: Device pointer for the consumer (of regulator) device 474 * @supply: regulator supply name 475 * 476 * Extract the regulator device node corresponding to the supply name. 477 * returns the device node corresponding to the regulator if found, else 478 * returns NULL. 479 */ 480static struct device_node *of_get_regulator(struct device *dev, const char *supply) 481{ 482 struct device_node *regnode = NULL; 483 char prop_name[64]; /* 64 is max size of property name */ 484 485 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); 486 487 snprintf(prop_name, 64, "%s-supply", supply); 488 regnode = of_parse_phandle(dev->of_node, prop_name, 0); 489 490 if (!regnode) { 491 regnode = of_get_child_regulator(dev->of_node, prop_name); 492 if (regnode) 493 return regnode; 494 495 dev_dbg(dev, "Looking up %s property in node %pOF failed\n", 496 prop_name, dev->of_node); 497 return NULL; 498 } 499 return regnode; 500} 501 502/* Platform voltage constraint check */ 503int regulator_check_voltage(struct regulator_dev *rdev, 504 int *min_uV, int *max_uV) 505{ 506 BUG_ON(*min_uV > *max_uV); 507 508 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { 509 rdev_err(rdev, "voltage operation not allowed\n"); 510 return -EPERM; 511 } 512 513 if (*max_uV > rdev->constraints->max_uV) 514 *max_uV = rdev->constraints->max_uV; 515 if (*min_uV < rdev->constraints->min_uV) 516 *min_uV = rdev->constraints->min_uV; 517 518 if (*min_uV > *max_uV) { 519 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", 520 *min_uV, *max_uV); 521 return -EINVAL; 522 } 523 524 return 0; 525} 526 527/* return 0 if the state is valid */ 528static int regulator_check_states(suspend_state_t state) 529{ 530 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE); 531} 532 533/* Make sure we select a voltage that suits the needs of all 534 * regulator consumers 535 */ 536int regulator_check_consumers(struct regulator_dev *rdev, 537 int *min_uV, int *max_uV, 538 suspend_state_t state) 539{ 540 struct regulator *regulator; 541 struct regulator_voltage *voltage; 542 543 list_for_each_entry(regulator, &rdev->consumer_list, list) { 544 voltage = ®ulator->voltage[state]; 545 /* 546 * Assume consumers that didn't say anything are OK 547 * with anything in the constraint range. 548 */ 549 if (!voltage->min_uV && !voltage->max_uV) 550 continue; 551 552 if (*max_uV > voltage->max_uV) 553 *max_uV = voltage->max_uV; 554 if (*min_uV < voltage->min_uV) 555 *min_uV = voltage->min_uV; 556 } 557 558 if (*min_uV > *max_uV) { 559 rdev_err(rdev, "Restricting voltage, %u-%uuV\n", 560 *min_uV, *max_uV); 561 return -EINVAL; 562 } 563 564 return 0; 565} 566 567/* current constraint check */ 568static int regulator_check_current_limit(struct regulator_dev *rdev, 569 int *min_uA, int *max_uA) 570{ 571 BUG_ON(*min_uA > *max_uA); 572 573 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) { 574 rdev_err(rdev, "current operation not allowed\n"); 575 return -EPERM; 576 } 577 578 if (*max_uA > rdev->constraints->max_uA) 579 *max_uA = rdev->constraints->max_uA; 580 if (*min_uA < rdev->constraints->min_uA) 581 *min_uA = rdev->constraints->min_uA; 582 583 if (*min_uA > *max_uA) { 584 rdev_err(rdev, "unsupportable current range: %d-%duA\n", 585 *min_uA, *max_uA); 586 return -EINVAL; 587 } 588 589 return 0; 590} 591 592/* operating mode constraint check */ 593static int regulator_mode_constrain(struct regulator_dev *rdev, 594 unsigned int *mode) 595{ 596 switch (*mode) { 597 case REGULATOR_MODE_FAST: 598 case REGULATOR_MODE_NORMAL: 599 case REGULATOR_MODE_IDLE: 600 case REGULATOR_MODE_STANDBY: 601 break; 602 default: 603 rdev_err(rdev, "invalid mode %x specified\n", *mode); 604 return -EINVAL; 605 } 606 607 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) { 608 rdev_err(rdev, "mode operation not allowed\n"); 609 return -EPERM; 610 } 611 612 /* The modes are bitmasks, the most power hungry modes having 613 * the lowest values. If the requested mode isn't supported 614 * try higher modes. */ 615 while (*mode) { 616 if (rdev->constraints->valid_modes_mask & *mode) 617 return 0; 618 *mode /= 2; 619 } 620 621 return -EINVAL; 622} 623 624static inline struct regulator_state * 625regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state) 626{ 627 if (rdev->constraints == NULL) 628 return NULL; 629 630 switch (state) { 631 case PM_SUSPEND_STANDBY: 632 return &rdev->constraints->state_standby; 633 case PM_SUSPEND_MEM: 634 return &rdev->constraints->state_mem; 635 case PM_SUSPEND_MAX: 636 return &rdev->constraints->state_disk; 637 default: 638 return NULL; 639 } 640} 641 642static const struct regulator_state * 643regulator_get_suspend_state_check(struct regulator_dev *rdev, suspend_state_t state) 644{ 645 const struct regulator_state *rstate; 646 647 rstate = regulator_get_suspend_state(rdev, state); 648 if (rstate == NULL) 649 return NULL; 650 651 /* If we have no suspend mode configuration don't set anything; 652 * only warn if the driver implements set_suspend_voltage or 653 * set_suspend_mode callback. 654 */ 655 if (rstate->enabled != ENABLE_IN_SUSPEND && 656 rstate->enabled != DISABLE_IN_SUSPEND) { 657 if (rdev->desc->ops->set_suspend_voltage || 658 rdev->desc->ops->set_suspend_mode) 659 rdev_warn(rdev, "No configuration\n"); 660 return NULL; 661 } 662 663 return rstate; 664} 665 666static ssize_t regulator_uV_show(struct device *dev, 667 struct device_attribute *attr, char *buf) 668{ 669 struct regulator_dev *rdev = dev_get_drvdata(dev); 670 int uV; 671 672 regulator_lock(rdev); 673 uV = regulator_get_voltage_rdev(rdev); 674 regulator_unlock(rdev); 675 676 if (uV < 0) 677 return uV; 678 return sprintf(buf, "%d\n", uV); 679} 680static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); 681 682static ssize_t regulator_uA_show(struct device *dev, 683 struct device_attribute *attr, char *buf) 684{ 685 struct regulator_dev *rdev = dev_get_drvdata(dev); 686 687 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); 688} 689static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); 690 691static ssize_t name_show(struct device *dev, struct device_attribute *attr, 692 char *buf) 693{ 694 struct regulator_dev *rdev = dev_get_drvdata(dev); 695 696 return sprintf(buf, "%s\n", rdev_get_name(rdev)); 697} 698static DEVICE_ATTR_RO(name); 699 700static const char *regulator_opmode_to_str(int mode) 701{ 702 switch (mode) { 703 case REGULATOR_MODE_FAST: 704 return "fast"; 705 case REGULATOR_MODE_NORMAL: 706 return "normal"; 707 case REGULATOR_MODE_IDLE: 708 return "idle"; 709 case REGULATOR_MODE_STANDBY: 710 return "standby"; 711 } 712 return "unknown"; 713} 714 715static ssize_t regulator_print_opmode(char *buf, int mode) 716{ 717 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode)); 718} 719 720static ssize_t regulator_opmode_show(struct device *dev, 721 struct device_attribute *attr, char *buf) 722{ 723 struct regulator_dev *rdev = dev_get_drvdata(dev); 724 725 return regulator_print_opmode(buf, _regulator_get_mode(rdev)); 726} 727static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); 728 729static ssize_t regulator_print_state(char *buf, int state) 730{ 731 if (state > 0) 732 return sprintf(buf, "enabled\n"); 733 else if (state == 0) 734 return sprintf(buf, "disabled\n"); 735 else 736 return sprintf(buf, "unknown\n"); 737} 738 739static ssize_t regulator_state_show(struct device *dev, 740 struct device_attribute *attr, char *buf) 741{ 742 struct regulator_dev *rdev = dev_get_drvdata(dev); 743 ssize_t ret; 744 745 regulator_lock(rdev); 746 ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); 747 regulator_unlock(rdev); 748 749 return ret; 750} 751static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); 752 753static ssize_t regulator_status_show(struct device *dev, 754 struct device_attribute *attr, char *buf) 755{ 756 struct regulator_dev *rdev = dev_get_drvdata(dev); 757 int status; 758 char *label; 759 760 status = rdev->desc->ops->get_status(rdev); 761 if (status < 0) 762 return status; 763 764 switch (status) { 765 case REGULATOR_STATUS_OFF: 766 label = "off"; 767 break; 768 case REGULATOR_STATUS_ON: 769 label = "on"; 770 break; 771 case REGULATOR_STATUS_ERROR: 772 label = "error"; 773 break; 774 case REGULATOR_STATUS_FAST: 775 label = "fast"; 776 break; 777 case REGULATOR_STATUS_NORMAL: 778 label = "normal"; 779 break; 780 case REGULATOR_STATUS_IDLE: 781 label = "idle"; 782 break; 783 case REGULATOR_STATUS_STANDBY: 784 label = "standby"; 785 break; 786 case REGULATOR_STATUS_BYPASS: 787 label = "bypass"; 788 break; 789 case REGULATOR_STATUS_UNDEFINED: 790 label = "undefined"; 791 break; 792 default: 793 return -ERANGE; 794 } 795 796 return sprintf(buf, "%s\n", label); 797} 798static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); 799 800static ssize_t regulator_min_uA_show(struct device *dev, 801 struct device_attribute *attr, char *buf) 802{ 803 struct regulator_dev *rdev = dev_get_drvdata(dev); 804 805 if (!rdev->constraints) 806 return sprintf(buf, "constraint not defined\n"); 807 808 return sprintf(buf, "%d\n", rdev->constraints->min_uA); 809} 810static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); 811 812static ssize_t regulator_max_uA_show(struct device *dev, 813 struct device_attribute *attr, char *buf) 814{ 815 struct regulator_dev *rdev = dev_get_drvdata(dev); 816 817 if (!rdev->constraints) 818 return sprintf(buf, "constraint not defined\n"); 819 820 return sprintf(buf, "%d\n", rdev->constraints->max_uA); 821} 822static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); 823 824static ssize_t regulator_min_uV_show(struct device *dev, 825 struct device_attribute *attr, char *buf) 826{ 827 struct regulator_dev *rdev = dev_get_drvdata(dev); 828 829 if (!rdev->constraints) 830 return sprintf(buf, "constraint not defined\n"); 831 832 return sprintf(buf, "%d\n", rdev->constraints->min_uV); 833} 834static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); 835 836static ssize_t regulator_max_uV_show(struct device *dev, 837 struct device_attribute *attr, char *buf) 838{ 839 struct regulator_dev *rdev = dev_get_drvdata(dev); 840 841 if (!rdev->constraints) 842 return sprintf(buf, "constraint not defined\n"); 843 844 return sprintf(buf, "%d\n", rdev->constraints->max_uV); 845} 846static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); 847 848static ssize_t regulator_total_uA_show(struct device *dev, 849 struct device_attribute *attr, char *buf) 850{ 851 struct regulator_dev *rdev = dev_get_drvdata(dev); 852 struct regulator *regulator; 853 int uA = 0; 854 855 regulator_lock(rdev); 856 list_for_each_entry(regulator, &rdev->consumer_list, list) { 857 if (regulator->enable_count) 858 uA += regulator->uA_load; 859 } 860 regulator_unlock(rdev); 861 return sprintf(buf, "%d\n", uA); 862} 863static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); 864 865static ssize_t num_users_show(struct device *dev, struct device_attribute *attr, 866 char *buf) 867{ 868 struct regulator_dev *rdev = dev_get_drvdata(dev); 869 return sprintf(buf, "%d\n", rdev->use_count); 870} 871static DEVICE_ATTR_RO(num_users); 872 873static ssize_t type_show(struct device *dev, struct device_attribute *attr, 874 char *buf) 875{ 876 struct regulator_dev *rdev = dev_get_drvdata(dev); 877 878 switch (rdev->desc->type) { 879 case REGULATOR_VOLTAGE: 880 return sprintf(buf, "voltage\n"); 881 case REGULATOR_CURRENT: 882 return sprintf(buf, "current\n"); 883 } 884 return sprintf(buf, "unknown\n"); 885} 886static DEVICE_ATTR_RO(type); 887 888static ssize_t regulator_suspend_mem_uV_show(struct device *dev, 889 struct device_attribute *attr, char *buf) 890{ 891 struct regulator_dev *rdev = dev_get_drvdata(dev); 892 893 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); 894} 895static DEVICE_ATTR(suspend_mem_microvolts, 0444, 896 regulator_suspend_mem_uV_show, NULL); 897 898static ssize_t regulator_suspend_disk_uV_show(struct device *dev, 899 struct device_attribute *attr, char *buf) 900{ 901 struct regulator_dev *rdev = dev_get_drvdata(dev); 902 903 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); 904} 905static DEVICE_ATTR(suspend_disk_microvolts, 0444, 906 regulator_suspend_disk_uV_show, NULL); 907 908static ssize_t regulator_suspend_standby_uV_show(struct device *dev, 909 struct device_attribute *attr, char *buf) 910{ 911 struct regulator_dev *rdev = dev_get_drvdata(dev); 912 913 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); 914} 915static DEVICE_ATTR(suspend_standby_microvolts, 0444, 916 regulator_suspend_standby_uV_show, NULL); 917 918static ssize_t regulator_suspend_mem_mode_show(struct device *dev, 919 struct device_attribute *attr, char *buf) 920{ 921 struct regulator_dev *rdev = dev_get_drvdata(dev); 922 923 return regulator_print_opmode(buf, 924 rdev->constraints->state_mem.mode); 925} 926static DEVICE_ATTR(suspend_mem_mode, 0444, 927 regulator_suspend_mem_mode_show, NULL); 928 929static ssize_t regulator_suspend_disk_mode_show(struct device *dev, 930 struct device_attribute *attr, char *buf) 931{ 932 struct regulator_dev *rdev = dev_get_drvdata(dev); 933 934 return regulator_print_opmode(buf, 935 rdev->constraints->state_disk.mode); 936} 937static DEVICE_ATTR(suspend_disk_mode, 0444, 938 regulator_suspend_disk_mode_show, NULL); 939 940static ssize_t regulator_suspend_standby_mode_show(struct device *dev, 941 struct device_attribute *attr, char *buf) 942{ 943 struct regulator_dev *rdev = dev_get_drvdata(dev); 944 945 return regulator_print_opmode(buf, 946 rdev->constraints->state_standby.mode); 947} 948static DEVICE_ATTR(suspend_standby_mode, 0444, 949 regulator_suspend_standby_mode_show, NULL); 950 951static ssize_t regulator_suspend_mem_state_show(struct device *dev, 952 struct device_attribute *attr, char *buf) 953{ 954 struct regulator_dev *rdev = dev_get_drvdata(dev); 955 956 return regulator_print_state(buf, 957 rdev->constraints->state_mem.enabled); 958} 959static DEVICE_ATTR(suspend_mem_state, 0444, 960 regulator_suspend_mem_state_show, NULL); 961 962static ssize_t regulator_suspend_disk_state_show(struct device *dev, 963 struct device_attribute *attr, char *buf) 964{ 965 struct regulator_dev *rdev = dev_get_drvdata(dev); 966 967 return regulator_print_state(buf, 968 rdev->constraints->state_disk.enabled); 969} 970static DEVICE_ATTR(suspend_disk_state, 0444, 971 regulator_suspend_disk_state_show, NULL); 972 973static ssize_t regulator_suspend_standby_state_show(struct device *dev, 974 struct device_attribute *attr, char *buf) 975{ 976 struct regulator_dev *rdev = dev_get_drvdata(dev); 977 978 return regulator_print_state(buf, 979 rdev->constraints->state_standby.enabled); 980} 981static DEVICE_ATTR(suspend_standby_state, 0444, 982 regulator_suspend_standby_state_show, NULL); 983 984static ssize_t regulator_bypass_show(struct device *dev, 985 struct device_attribute *attr, char *buf) 986{ 987 struct regulator_dev *rdev = dev_get_drvdata(dev); 988 const char *report; 989 bool bypass; 990 int ret; 991 992 ret = rdev->desc->ops->get_bypass(rdev, &bypass); 993 994 if (ret != 0) 995 report = "unknown"; 996 else if (bypass) 997 report = "enabled"; 998 else 999 report = "disabled"; 1000 1001 return sprintf(buf, "%s\n", report); 1002} 1003static DEVICE_ATTR(bypass, 0444, 1004 regulator_bypass_show, NULL); 1005 1006/* Calculate the new optimum regulator operating mode based on the new total 1007 * consumer load. All locks held by caller */ 1008static int drms_uA_update(struct regulator_dev *rdev) 1009{ 1010 struct regulator *sibling; 1011 int current_uA = 0, output_uV, input_uV, err; 1012 unsigned int mode; 1013 1014 /* 1015 * first check to see if we can set modes at all, otherwise just 1016 * tell the consumer everything is OK. 1017 */ 1018 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) { 1019 rdev_dbg(rdev, "DRMS operation not allowed\n"); 1020 return 0; 1021 } 1022 1023 if (!rdev->desc->ops->get_optimum_mode && 1024 !rdev->desc->ops->set_load) 1025 return 0; 1026 1027 if (!rdev->desc->ops->set_mode && 1028 !rdev->desc->ops->set_load) 1029 return -EINVAL; 1030 1031 /* calc total requested load */ 1032 list_for_each_entry(sibling, &rdev->consumer_list, list) { 1033 if (sibling->enable_count) 1034 current_uA += sibling->uA_load; 1035 } 1036 1037 current_uA += rdev->constraints->system_load; 1038 1039 if (rdev->desc->ops->set_load) { 1040 /* set the optimum mode for our new total regulator load */ 1041 err = rdev->desc->ops->set_load(rdev, current_uA); 1042 if (err < 0) 1043 rdev_err(rdev, "failed to set load %d: %pe\n", 1044 current_uA, ERR_PTR(err)); 1045 } else { 1046 /* get output voltage */ 1047 output_uV = regulator_get_voltage_rdev(rdev); 1048 if (output_uV <= 0) { 1049 rdev_err(rdev, "invalid output voltage found\n"); 1050 return -EINVAL; 1051 } 1052 1053 /* get input voltage */ 1054 input_uV = 0; 1055 if (rdev->supply) 1056 input_uV = regulator_get_voltage_rdev(rdev->supply->rdev); 1057 if (input_uV <= 0) 1058 input_uV = rdev->constraints->input_uV; 1059 if (input_uV <= 0) { 1060 rdev_err(rdev, "invalid input voltage found\n"); 1061 return -EINVAL; 1062 } 1063 1064 /* now get the optimum mode for our new total regulator load */ 1065 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, 1066 output_uV, current_uA); 1067 1068 /* check the new mode is allowed */ 1069 err = regulator_mode_constrain(rdev, &mode); 1070 if (err < 0) { 1071 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV: %pe\n", 1072 current_uA, input_uV, output_uV, ERR_PTR(err)); 1073 return err; 1074 } 1075 1076 err = rdev->desc->ops->set_mode(rdev, mode); 1077 if (err < 0) 1078 rdev_err(rdev, "failed to set optimum mode %x: %pe\n", 1079 mode, ERR_PTR(err)); 1080 } 1081 1082 return err; 1083} 1084 1085static int __suspend_set_state(struct regulator_dev *rdev, 1086 const struct regulator_state *rstate) 1087{ 1088 int ret = 0; 1089 1090 if (rstate->enabled == ENABLE_IN_SUSPEND && 1091 rdev->desc->ops->set_suspend_enable) 1092 ret = rdev->desc->ops->set_suspend_enable(rdev); 1093 else if (rstate->enabled == DISABLE_IN_SUSPEND && 1094 rdev->desc->ops->set_suspend_disable) 1095 ret = rdev->desc->ops->set_suspend_disable(rdev); 1096 else /* OK if set_suspend_enable or set_suspend_disable is NULL */ 1097 ret = 0; 1098 1099 if (ret < 0) { 1100 rdev_err(rdev, "failed to enabled/disable: %pe\n", ERR_PTR(ret)); 1101 return ret; 1102 } 1103 1104 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { 1105 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); 1106 if (ret < 0) { 1107 rdev_err(rdev, "failed to set voltage: %pe\n", ERR_PTR(ret)); 1108 return ret; 1109 } 1110 } 1111 1112 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { 1113 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); 1114 if (ret < 0) { 1115 rdev_err(rdev, "failed to set mode: %pe\n", ERR_PTR(ret)); 1116 return ret; 1117 } 1118 } 1119 1120 return ret; 1121} 1122 1123static int suspend_set_initial_state(struct regulator_dev *rdev) 1124{ 1125 const struct regulator_state *rstate; 1126 1127 rstate = regulator_get_suspend_state_check(rdev, 1128 rdev->constraints->initial_state); 1129 if (!rstate) 1130 return 0; 1131 1132 return __suspend_set_state(rdev, rstate); 1133} 1134 1135#if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) 1136static void print_constraints_debug(struct regulator_dev *rdev) 1137{ 1138 struct regulation_constraints *constraints = rdev->constraints; 1139 char buf[160] = ""; 1140 size_t len = sizeof(buf) - 1; 1141 int count = 0; 1142 int ret; 1143 1144 if (constraints->min_uV && constraints->max_uV) { 1145 if (constraints->min_uV == constraints->max_uV) 1146 count += scnprintf(buf + count, len - count, "%d mV ", 1147 constraints->min_uV / 1000); 1148 else 1149 count += scnprintf(buf + count, len - count, 1150 "%d <--> %d mV ", 1151 constraints->min_uV / 1000, 1152 constraints->max_uV / 1000); 1153 } 1154 1155 if (!constraints->min_uV || 1156 constraints->min_uV != constraints->max_uV) { 1157 ret = regulator_get_voltage_rdev(rdev); 1158 if (ret > 0) 1159 count += scnprintf(buf + count, len - count, 1160 "at %d mV ", ret / 1000); 1161 } 1162 1163 if (constraints->uV_offset) 1164 count += scnprintf(buf + count, len - count, "%dmV offset ", 1165 constraints->uV_offset / 1000); 1166 1167 if (constraints->min_uA && constraints->max_uA) { 1168 if (constraints->min_uA == constraints->max_uA) 1169 count += scnprintf(buf + count, len - count, "%d mA ", 1170 constraints->min_uA / 1000); 1171 else 1172 count += scnprintf(buf + count, len - count, 1173 "%d <--> %d mA ", 1174 constraints->min_uA / 1000, 1175 constraints->max_uA / 1000); 1176 } 1177 1178 if (!constraints->min_uA || 1179 constraints->min_uA != constraints->max_uA) { 1180 ret = _regulator_get_current_limit(rdev); 1181 if (ret > 0) 1182 count += scnprintf(buf + count, len - count, 1183 "at %d mA ", ret / 1000); 1184 } 1185 1186 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) 1187 count += scnprintf(buf + count, len - count, "fast "); 1188 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) 1189 count += scnprintf(buf + count, len - count, "normal "); 1190 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) 1191 count += scnprintf(buf + count, len - count, "idle "); 1192 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) 1193 count += scnprintf(buf + count, len - count, "standby "); 1194 1195 if (!count) 1196 count = scnprintf(buf, len, "no parameters"); 1197 else 1198 --count; 1199 1200 count += scnprintf(buf + count, len - count, ", %s", 1201 _regulator_is_enabled(rdev) ? "enabled" : "disabled"); 1202 1203 rdev_dbg(rdev, "%s\n", buf); 1204} 1205#else /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */ 1206static inline void print_constraints_debug(struct regulator_dev *rdev) {} 1207#endif /* !DEBUG && !CONFIG_DYNAMIC_DEBUG */ 1208 1209static void print_constraints(struct regulator_dev *rdev) 1210{ 1211 struct regulation_constraints *constraints = rdev->constraints; 1212 1213 print_constraints_debug(rdev); 1214 1215 if ((constraints->min_uV != constraints->max_uV) && 1216 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) 1217 rdev_warn(rdev, 1218 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n"); 1219} 1220 1221static int machine_constraints_voltage(struct regulator_dev *rdev, 1222 struct regulation_constraints *constraints) 1223{ 1224 const struct regulator_ops *ops = rdev->desc->ops; 1225 int ret; 1226 1227 /* do we need to apply the constraint voltage */ 1228 if (rdev->constraints->apply_uV && 1229 rdev->constraints->min_uV && rdev->constraints->max_uV) { 1230 int target_min, target_max; 1231 int current_uV = regulator_get_voltage_rdev(rdev); 1232 1233 if (current_uV == -ENOTRECOVERABLE) { 1234 /* This regulator can't be read and must be initialized */ 1235 rdev_info(rdev, "Setting %d-%duV\n", 1236 rdev->constraints->min_uV, 1237 rdev->constraints->max_uV); 1238 _regulator_do_set_voltage(rdev, 1239 rdev->constraints->min_uV, 1240 rdev->constraints->max_uV); 1241 current_uV = regulator_get_voltage_rdev(rdev); 1242 } 1243 1244 if (current_uV < 0) { 1245 rdev_err(rdev, 1246 "failed to get the current voltage: %pe\n", 1247 ERR_PTR(current_uV)); 1248 return current_uV; 1249 } 1250 1251 /* 1252 * If we're below the minimum voltage move up to the 1253 * minimum voltage, if we're above the maximum voltage 1254 * then move down to the maximum. 1255 */ 1256 target_min = current_uV; 1257 target_max = current_uV; 1258 1259 if (current_uV < rdev->constraints->min_uV) { 1260 target_min = rdev->constraints->min_uV; 1261 target_max = rdev->constraints->min_uV; 1262 } 1263 1264 if (current_uV > rdev->constraints->max_uV) { 1265 target_min = rdev->constraints->max_uV; 1266 target_max = rdev->constraints->max_uV; 1267 } 1268 1269 if (target_min != current_uV || target_max != current_uV) { 1270 rdev_info(rdev, "Bringing %duV into %d-%duV\n", 1271 current_uV, target_min, target_max); 1272 ret = _regulator_do_set_voltage( 1273 rdev, target_min, target_max); 1274 if (ret < 0) { 1275 rdev_err(rdev, 1276 "failed to apply %d-%duV constraint: %pe\n", 1277 target_min, target_max, ERR_PTR(ret)); 1278 return ret; 1279 } 1280 } 1281 } 1282 1283 /* constrain machine-level voltage specs to fit 1284 * the actual range supported by this regulator. 1285 */ 1286 if (ops->list_voltage && rdev->desc->n_voltages) { 1287 int count = rdev->desc->n_voltages; 1288 int i; 1289 int min_uV = INT_MAX; 1290 int max_uV = INT_MIN; 1291 int cmin = constraints->min_uV; 1292 int cmax = constraints->max_uV; 1293 1294 /* it's safe to autoconfigure fixed-voltage supplies 1295 and the constraints are used by list_voltage. */ 1296 if (count == 1 && !cmin) { 1297 cmin = 1; 1298 cmax = INT_MAX; 1299 constraints->min_uV = cmin; 1300 constraints->max_uV = cmax; 1301 } 1302 1303 /* voltage constraints are optional */ 1304 if ((cmin == 0) && (cmax == 0)) 1305 return 0; 1306 1307 /* else require explicit machine-level constraints */ 1308 if (cmin <= 0 || cmax <= 0 || cmax < cmin) { 1309 rdev_err(rdev, "invalid voltage constraints\n"); 1310 return -EINVAL; 1311 } 1312 1313 /* no need to loop voltages if range is continuous */ 1314 if (rdev->desc->continuous_voltage_range) 1315 return 0; 1316 1317 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ 1318 for (i = 0; i < count; i++) { 1319 int value; 1320 1321 value = ops->list_voltage(rdev, i); 1322 if (value <= 0) 1323 continue; 1324 1325 /* maybe adjust [min_uV..max_uV] */ 1326 if (value >= cmin && value < min_uV) 1327 min_uV = value; 1328 if (value <= cmax && value > max_uV) 1329 max_uV = value; 1330 } 1331 1332 /* final: [min_uV..max_uV] valid iff constraints valid */ 1333 if (max_uV < min_uV) { 1334 rdev_err(rdev, 1335 "unsupportable voltage constraints %u-%uuV\n", 1336 min_uV, max_uV); 1337 return -EINVAL; 1338 } 1339 1340 /* use regulator's subset of machine constraints */ 1341 if (constraints->min_uV < min_uV) { 1342 rdev_dbg(rdev, "override min_uV, %d -> %d\n", 1343 constraints->min_uV, min_uV); 1344 constraints->min_uV = min_uV; 1345 } 1346 if (constraints->max_uV > max_uV) { 1347 rdev_dbg(rdev, "override max_uV, %d -> %d\n", 1348 constraints->max_uV, max_uV); 1349 constraints->max_uV = max_uV; 1350 } 1351 } 1352 1353 return 0; 1354} 1355 1356static int machine_constraints_current(struct regulator_dev *rdev, 1357 struct regulation_constraints *constraints) 1358{ 1359 const struct regulator_ops *ops = rdev->desc->ops; 1360 int ret; 1361 1362 if (!constraints->min_uA && !constraints->max_uA) 1363 return 0; 1364 1365 if (constraints->min_uA > constraints->max_uA) { 1366 rdev_err(rdev, "Invalid current constraints\n"); 1367 return -EINVAL; 1368 } 1369 1370 if (!ops->set_current_limit || !ops->get_current_limit) { 1371 rdev_warn(rdev, "Operation of current configuration missing\n"); 1372 return 0; 1373 } 1374 1375 /* Set regulator current in constraints range */ 1376 ret = ops->set_current_limit(rdev, constraints->min_uA, 1377 constraints->max_uA); 1378 if (ret < 0) { 1379 rdev_err(rdev, "Failed to set current constraint, %d\n", ret); 1380 return ret; 1381 } 1382 1383 return 0; 1384} 1385 1386static int _regulator_do_enable(struct regulator_dev *rdev); 1387 1388/** 1389 * set_machine_constraints - sets regulator constraints 1390 * @rdev: regulator source 1391 * 1392 * Allows platform initialisation code to define and constrain 1393 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: 1394 * Constraints *must* be set by platform code in order for some 1395 * regulator operations to proceed i.e. set_voltage, set_current_limit, 1396 * set_mode. 1397 */ 1398static int set_machine_constraints(struct regulator_dev *rdev) 1399{ 1400 int ret = 0; 1401 const struct regulator_ops *ops = rdev->desc->ops; 1402 1403 ret = machine_constraints_voltage(rdev, rdev->constraints); 1404 if (ret != 0) 1405 return ret; 1406 1407 ret = machine_constraints_current(rdev, rdev->constraints); 1408 if (ret != 0) 1409 return ret; 1410 1411 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) { 1412 ret = ops->set_input_current_limit(rdev, 1413 rdev->constraints->ilim_uA); 1414 if (ret < 0) { 1415 rdev_err(rdev, "failed to set input limit: %pe\n", ERR_PTR(ret)); 1416 return ret; 1417 } 1418 } 1419 1420 /* do we need to setup our suspend state */ 1421 if (rdev->constraints->initial_state) { 1422 ret = suspend_set_initial_state(rdev); 1423 if (ret < 0) { 1424 rdev_err(rdev, "failed to set suspend state: %pe\n", ERR_PTR(ret)); 1425 return ret; 1426 } 1427 } 1428 1429 if (rdev->constraints->initial_mode) { 1430 if (!ops->set_mode) { 1431 rdev_err(rdev, "no set_mode operation\n"); 1432 return -EINVAL; 1433 } 1434 1435 ret = ops->set_mode(rdev, rdev->constraints->initial_mode); 1436 if (ret < 0) { 1437 rdev_err(rdev, "failed to set initial mode: %pe\n", ERR_PTR(ret)); 1438 return ret; 1439 } 1440 } else if (rdev->constraints->system_load) { 1441 /* 1442 * We'll only apply the initial system load if an 1443 * initial mode wasn't specified. 1444 */ 1445 drms_uA_update(rdev); 1446 } 1447 1448 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) 1449 && ops->set_ramp_delay) { 1450 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); 1451 if (ret < 0) { 1452 rdev_err(rdev, "failed to set ramp_delay: %pe\n", ERR_PTR(ret)); 1453 return ret; 1454 } 1455 } 1456 1457 if (rdev->constraints->pull_down && ops->set_pull_down) { 1458 ret = ops->set_pull_down(rdev); 1459 if (ret < 0) { 1460 rdev_err(rdev, "failed to set pull down: %pe\n", ERR_PTR(ret)); 1461 return ret; 1462 } 1463 } 1464 1465 if (rdev->constraints->soft_start && ops->set_soft_start) { 1466 ret = ops->set_soft_start(rdev); 1467 if (ret < 0) { 1468 rdev_err(rdev, "failed to set soft start: %pe\n", ERR_PTR(ret)); 1469 return ret; 1470 } 1471 } 1472 1473 if (rdev->constraints->over_current_protection 1474 && ops->set_over_current_protection) { 1475 ret = ops->set_over_current_protection(rdev); 1476 if (ret < 0) { 1477 rdev_err(rdev, "failed to set over current protection: %pe\n", 1478 ERR_PTR(ret)); 1479 return ret; 1480 } 1481 } 1482 1483 if (rdev->constraints->active_discharge && ops->set_active_discharge) { 1484 bool ad_state = (rdev->constraints->active_discharge == 1485 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false; 1486 1487 ret = ops->set_active_discharge(rdev, ad_state); 1488 if (ret < 0) { 1489 rdev_err(rdev, "failed to set active discharge: %pe\n", ERR_PTR(ret)); 1490 return ret; 1491 } 1492 } 1493 1494 /* If the constraints say the regulator should be on at this point 1495 * and we have control then make sure it is enabled. 1496 */ 1497 if (rdev->constraints->always_on || rdev->constraints->boot_on) { 1498 /* If we want to enable this regulator, make sure that we know 1499 * the supplying regulator. 1500 */ 1501 if (rdev->supply_name && !rdev->supply) 1502 return -EPROBE_DEFER; 1503 1504 /* If supplying regulator has already been enabled, 1505 * it's not intended to have use_count increment 1506 * when rdev is only boot-on. 1507 */ 1508 if (rdev->supply && 1509 (rdev->constraints->always_on || 1510 !regulator_is_enabled(rdev->supply))) { 1511 ret = regulator_enable(rdev->supply); 1512 if (ret < 0) { 1513 _regulator_put(rdev->supply); 1514 rdev->supply = NULL; 1515 return ret; 1516 } 1517 } 1518 1519 ret = _regulator_do_enable(rdev); 1520 if (ret < 0 && ret != -EINVAL) { 1521 rdev_err(rdev, "failed to enable: %pe\n", ERR_PTR(ret)); 1522 return ret; 1523 } 1524 1525 if (rdev->constraints->always_on) 1526 rdev->use_count++; 1527 } else if (rdev->desc->off_on_delay) { 1528 rdev->last_off_jiffy = jiffies; 1529 } 1530 1531 print_constraints(rdev); 1532 return 0; 1533} 1534 1535/** 1536 * set_supply - set regulator supply regulator 1537 * @rdev: regulator (locked) 1538 * @supply_rdev: supply regulator (locked)) 1539 * 1540 * Called by platform initialisation code to set the supply regulator for this 1541 * regulator. This ensures that a regulators supply will also be enabled by the 1542 * core if it's child is enabled. 1543 */ 1544static int set_supply(struct regulator_dev *rdev, 1545 struct regulator_dev *supply_rdev) 1546{ 1547 int err; 1548 1549 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev)); 1550 1551 if (!try_module_get(supply_rdev->owner)) 1552 return -ENODEV; 1553 1554 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); 1555 if (rdev->supply == NULL) { 1556 module_put(supply_rdev->owner); 1557 err = -ENOMEM; 1558 return err; 1559 } 1560 supply_rdev->open_count++; 1561 1562 return 0; 1563} 1564 1565/** 1566 * set_consumer_device_supply - Bind a regulator to a symbolic supply 1567 * @rdev: regulator source 1568 * @consumer_dev_name: dev_name() string for device supply applies to 1569 * @supply: symbolic name for supply 1570 * 1571 * Allows platform initialisation code to map physical regulator 1572 * sources to symbolic names for supplies for use by devices. Devices 1573 * should use these symbolic names to request regulators, avoiding the 1574 * need to provide board-specific regulator names as platform data. 1575 */ 1576static int set_consumer_device_supply(struct regulator_dev *rdev, 1577 const char *consumer_dev_name, 1578 const char *supply) 1579{ 1580 struct regulator_map *node, *new_node; 1581 int has_dev; 1582 1583 if (supply == NULL) 1584 return -EINVAL; 1585 1586 if (consumer_dev_name != NULL) 1587 has_dev = 1; 1588 else 1589 has_dev = 0; 1590 1591 new_node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); 1592 if (new_node == NULL) 1593 return -ENOMEM; 1594 1595 new_node->regulator = rdev; 1596 new_node->supply = supply; 1597 1598 if (has_dev) { 1599 new_node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); 1600 if (new_node->dev_name == NULL) { 1601 kfree(new_node); 1602 return -ENOMEM; 1603 } 1604 } 1605 1606 mutex_lock(®ulator_list_mutex); 1607 list_for_each_entry(node, ®ulator_map_list, list) { 1608 if (node->dev_name && consumer_dev_name) { 1609 if (strcmp(node->dev_name, consumer_dev_name) != 0) 1610 continue; 1611 } else if (node->dev_name || consumer_dev_name) { 1612 continue; 1613 } 1614 1615 if (strcmp(node->supply, supply) != 0) 1616 continue; 1617 1618 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n", 1619 consumer_dev_name, 1620 dev_name(&node->regulator->dev), 1621 node->regulator->desc->name, 1622 supply, 1623 dev_name(&rdev->dev), rdev_get_name(rdev)); 1624 goto fail; 1625 } 1626 1627 list_add(&new_node->list, ®ulator_map_list); 1628 mutex_unlock(®ulator_list_mutex); 1629 1630 return 0; 1631 1632fail: 1633 mutex_unlock(®ulator_list_mutex); 1634 kfree(new_node->dev_name); 1635 kfree(new_node); 1636 return -EBUSY; 1637} 1638 1639static void unset_regulator_supplies(struct regulator_dev *rdev) 1640{ 1641 struct regulator_map *node, *n; 1642 1643 list_for_each_entry_safe(node, n, ®ulator_map_list, list) { 1644 if (rdev == node->regulator) { 1645 list_del(&node->list); 1646 kfree(node->dev_name); 1647 kfree(node); 1648 } 1649 } 1650} 1651 1652#ifdef CONFIG_DEBUG_FS 1653static ssize_t constraint_flags_read_file(struct file *file, 1654 char __user *user_buf, 1655 size_t count, loff_t *ppos) 1656{ 1657 const struct regulator *regulator = file->private_data; 1658 const struct regulation_constraints *c = regulator->rdev->constraints; 1659 char *buf; 1660 ssize_t ret; 1661 1662 if (!c) 1663 return 0; 1664 1665 buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 1666 if (!buf) 1667 return -ENOMEM; 1668 1669 ret = snprintf(buf, PAGE_SIZE, 1670 "always_on: %u\n" 1671 "boot_on: %u\n" 1672 "apply_uV: %u\n" 1673 "ramp_disable: %u\n" 1674 "soft_start: %u\n" 1675 "pull_down: %u\n" 1676 "over_current_protection: %u\n", 1677 c->always_on, 1678 c->boot_on, 1679 c->apply_uV, 1680 c->ramp_disable, 1681 c->soft_start, 1682 c->pull_down, 1683 c->over_current_protection); 1684 1685 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 1686 kfree(buf); 1687 1688 return ret; 1689} 1690 1691#endif 1692 1693static const struct file_operations constraint_flags_fops = { 1694#ifdef CONFIG_DEBUG_FS 1695 .open = simple_open, 1696 .read = constraint_flags_read_file, 1697 .llseek = default_llseek, 1698#endif 1699}; 1700 1701#define REG_STR_SIZE 64 1702 1703static struct regulator *create_regulator(struct regulator_dev *rdev, 1704 struct device *dev, 1705 const char *supply_name) 1706{ 1707 struct regulator *regulator; 1708 int err = 0; 1709 1710 lockdep_assert_held_once(&rdev->mutex.base); 1711 1712 if (dev) { 1713 char buf[REG_STR_SIZE]; 1714 int size; 1715 1716 size = snprintf(buf, REG_STR_SIZE, "%s-%s", 1717 dev->kobj.name, supply_name); 1718 if (size >= REG_STR_SIZE) 1719 return NULL; 1720 1721 supply_name = kstrdup(buf, GFP_KERNEL); 1722 if (supply_name == NULL) 1723 return NULL; 1724 } else { 1725 supply_name = kstrdup_const(supply_name, GFP_KERNEL); 1726 if (supply_name == NULL) 1727 return NULL; 1728 } 1729 1730 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); 1731 if (regulator == NULL) { 1732 kfree_const(supply_name); 1733 return NULL; 1734 } 1735 1736 regulator->rdev = rdev; 1737 regulator->supply_name = supply_name; 1738 1739 list_add(®ulator->list, &rdev->consumer_list); 1740 1741 if (dev) { 1742 regulator->dev = dev; 1743 1744 /* Add a link to the device sysfs entry */ 1745 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj, 1746 supply_name); 1747 if (err) { 1748 rdev_dbg(rdev, "could not add device link %s: %pe\n", 1749 dev->kobj.name, ERR_PTR(err)); 1750 /* non-fatal */ 1751 } 1752 } 1753 1754 if (err != -EEXIST) 1755 regulator->debugfs = debugfs_create_dir(supply_name, rdev->debugfs); 1756 if (IS_ERR(regulator->debugfs)) 1757 rdev_dbg(rdev, "Failed to create debugfs directory\n"); 1758 1759 debugfs_create_u32("uA_load", 0444, regulator->debugfs, 1760 ®ulator->uA_load); 1761 debugfs_create_u32("min_uV", 0444, regulator->debugfs, 1762 ®ulator->voltage[PM_SUSPEND_ON].min_uV); 1763 debugfs_create_u32("max_uV", 0444, regulator->debugfs, 1764 ®ulator->voltage[PM_SUSPEND_ON].max_uV); 1765 debugfs_create_file("constraint_flags", 0444, regulator->debugfs, 1766 regulator, &constraint_flags_fops); 1767 1768 /* 1769 * Check now if the regulator is an always on regulator - if 1770 * it is then we don't need to do nearly so much work for 1771 * enable/disable calls. 1772 */ 1773 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) && 1774 _regulator_is_enabled(rdev)) 1775 regulator->always_on = true; 1776 1777 return regulator; 1778} 1779 1780static int _regulator_get_enable_time(struct regulator_dev *rdev) 1781{ 1782 if (rdev->constraints && rdev->constraints->enable_time) 1783 return rdev->constraints->enable_time; 1784 if (rdev->desc->ops->enable_time) 1785 return rdev->desc->ops->enable_time(rdev); 1786 return rdev->desc->enable_time; 1787} 1788 1789static struct regulator_supply_alias *regulator_find_supply_alias( 1790 struct device *dev, const char *supply) 1791{ 1792 struct regulator_supply_alias *map; 1793 1794 list_for_each_entry(map, ®ulator_supply_alias_list, list) 1795 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0) 1796 return map; 1797 1798 return NULL; 1799} 1800 1801static void regulator_supply_alias(struct device **dev, const char **supply) 1802{ 1803 struct regulator_supply_alias *map; 1804 1805 map = regulator_find_supply_alias(*dev, *supply); 1806 if (map) { 1807 dev_dbg(*dev, "Mapping supply %s to %s,%s\n", 1808 *supply, map->alias_supply, 1809 dev_name(map->alias_dev)); 1810 *dev = map->alias_dev; 1811 *supply = map->alias_supply; 1812 } 1813} 1814 1815static int regulator_match(struct device *dev, const void *data) 1816{ 1817 struct regulator_dev *r = dev_to_rdev(dev); 1818 1819 return strcmp(rdev_get_name(r), data) == 0; 1820} 1821 1822static struct regulator_dev *regulator_lookup_by_name(const char *name) 1823{ 1824 struct device *dev; 1825 1826 dev = class_find_device(®ulator_class, NULL, name, regulator_match); 1827 1828 return dev ? dev_to_rdev(dev) : NULL; 1829} 1830 1831/** 1832 * regulator_dev_lookup - lookup a regulator device. 1833 * @dev: device for regulator "consumer". 1834 * @supply: Supply name or regulator ID. 1835 * 1836 * If successful, returns a struct regulator_dev that corresponds to the name 1837 * @supply and with the embedded struct device refcount incremented by one. 1838 * The refcount must be dropped by calling put_device(). 1839 * On failure one of the following ERR-PTR-encoded values is returned: 1840 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed 1841 * in the future. 1842 */ 1843static struct regulator_dev *regulator_dev_lookup(struct device *dev, 1844 const char *supply) 1845{ 1846 struct regulator_dev *r = NULL; 1847 struct device_node *node; 1848 struct regulator_map *map; 1849 const char *devname = NULL; 1850 1851 regulator_supply_alias(&dev, &supply); 1852 1853 /* first do a dt based lookup */ 1854 if (dev && dev->of_node) { 1855 node = of_get_regulator(dev, supply); 1856 if (node) { 1857 r = of_find_regulator_by_node(node); 1858 of_node_put(node); 1859 if (r) 1860 return r; 1861 1862 /* 1863 * We have a node, but there is no device. 1864 * assume it has not registered yet. 1865 */ 1866 return ERR_PTR(-EPROBE_DEFER); 1867 } 1868 } 1869 1870 /* if not found, try doing it non-dt way */ 1871 if (dev) 1872 devname = dev_name(dev); 1873 1874 mutex_lock(®ulator_list_mutex); 1875 list_for_each_entry(map, ®ulator_map_list, list) { 1876 /* If the mapping has a device set up it must match */ 1877 if (map->dev_name && 1878 (!devname || strcmp(map->dev_name, devname))) 1879 continue; 1880 1881 if (strcmp(map->supply, supply) == 0 && 1882 get_device(&map->regulator->dev)) { 1883 r = map->regulator; 1884 break; 1885 } 1886 } 1887 mutex_unlock(®ulator_list_mutex); 1888 1889 if (r) 1890 return r; 1891 1892 r = regulator_lookup_by_name(supply); 1893 if (r) 1894 return r; 1895 1896 return ERR_PTR(-ENODEV); 1897} 1898 1899static int regulator_resolve_supply(struct regulator_dev *rdev) 1900{ 1901 struct regulator_dev *r; 1902 struct device *dev = rdev->dev.parent; 1903 struct ww_acquire_ctx ww_ctx; 1904 int ret = 0; 1905 1906 /* No supply to resolve? */ 1907 if (!rdev->supply_name) 1908 return 0; 1909 1910 /* Supply already resolved? (fast-path without locking contention) */ 1911 if (rdev->supply) 1912 return 0; 1913 1914 r = regulator_dev_lookup(dev, rdev->supply_name); 1915 if (IS_ERR(r)) { 1916 ret = PTR_ERR(r); 1917 1918 /* Did the lookup explicitly defer for us? */ 1919 if (ret == -EPROBE_DEFER) 1920 goto out; 1921 1922 if (have_full_constraints()) { 1923 r = dummy_regulator_rdev; 1924 get_device(&r->dev); 1925 } else { 1926 dev_err(dev, "Failed to resolve %s-supply for %s\n", 1927 rdev->supply_name, rdev->desc->name); 1928 ret = -EPROBE_DEFER; 1929 goto out; 1930 } 1931 } 1932 1933 if (r == rdev) { 1934 dev_err(dev, "Supply for %s (%s) resolved to itself\n", 1935 rdev->desc->name, rdev->supply_name); 1936 if (!have_full_constraints()) { 1937 ret = -EINVAL; 1938 goto out; 1939 } 1940 r = dummy_regulator_rdev; 1941 get_device(&r->dev); 1942 } 1943 1944 /* 1945 * If the supply's parent device is not the same as the 1946 * regulator's parent device, then ensure the parent device 1947 * is bound before we resolve the supply, in case the parent 1948 * device get probe deferred and unregisters the supply. 1949 */ 1950 if (r->dev.parent && r->dev.parent != rdev->dev.parent) { 1951 if (!device_is_bound(r->dev.parent)) { 1952 put_device(&r->dev); 1953 ret = -EPROBE_DEFER; 1954 goto out; 1955 } 1956 } 1957 1958 /* Recursively resolve the supply of the supply */ 1959 ret = regulator_resolve_supply(r); 1960 if (ret < 0) { 1961 put_device(&r->dev); 1962 goto out; 1963 } 1964 1965 /* 1966 * Recheck rdev->supply with rdev->mutex lock held to avoid a race 1967 * between rdev->supply null check and setting rdev->supply in 1968 * set_supply() from concurrent tasks. 1969 */ 1970 regulator_lock_two(rdev, r, &ww_ctx); 1971 1972 /* Supply just resolved by a concurrent task? */ 1973 if (rdev->supply) { 1974 regulator_unlock_two(rdev, r, &ww_ctx); 1975 put_device(&r->dev); 1976 goto out; 1977 } 1978 1979 ret = set_supply(rdev, r); 1980 if (ret < 0) { 1981 regulator_unlock_two(rdev, r, &ww_ctx); 1982 put_device(&r->dev); 1983 goto out; 1984 } 1985 1986 regulator_unlock_two(rdev, r, &ww_ctx); 1987 1988 /* 1989 * In set_machine_constraints() we may have turned this regulator on 1990 * but we couldn't propagate to the supply if it hadn't been resolved 1991 * yet. Do it now. 1992 */ 1993 if (rdev->use_count) { 1994 ret = regulator_enable(rdev->supply); 1995 if (ret < 0) { 1996 _regulator_put(rdev->supply); 1997 rdev->supply = NULL; 1998 goto out; 1999 } 2000 } 2001 2002out: 2003 return ret; 2004} 2005 2006/* Internal regulator request function */ 2007struct regulator *_regulator_get(struct device *dev, const char *id, 2008 enum regulator_get_type get_type) 2009{ 2010 struct regulator_dev *rdev; 2011 struct regulator *regulator; 2012 struct device_link *link; 2013 int ret; 2014 2015 if (get_type >= MAX_GET_TYPE) { 2016 dev_err(dev, "invalid type %d in %s\n", get_type, __func__); 2017 return ERR_PTR(-EINVAL); 2018 } 2019 2020 if (id == NULL) { 2021 pr_err("get() with no identifier\n"); 2022 return ERR_PTR(-EINVAL); 2023 } 2024 2025 rdev = regulator_dev_lookup(dev, id); 2026 if (IS_ERR(rdev)) { 2027 ret = PTR_ERR(rdev); 2028 2029 /* 2030 * If regulator_dev_lookup() fails with error other 2031 * than -ENODEV our job here is done, we simply return it. 2032 */ 2033 if (ret != -ENODEV) 2034 return ERR_PTR(ret); 2035 2036 if (!have_full_constraints()) { 2037 dev_warn(dev, 2038 "incomplete constraints, dummy supplies not allowed\n"); 2039 return ERR_PTR(-ENODEV); 2040 } 2041 2042 switch (get_type) { 2043 case NORMAL_GET: 2044 /* 2045 * Assume that a regulator is physically present and 2046 * enabled, even if it isn't hooked up, and just 2047 * provide a dummy. 2048 */ 2049 dev_warn(dev, "supply %s not found, using dummy regulator\n", id); 2050 rdev = dummy_regulator_rdev; 2051 get_device(&rdev->dev); 2052 break; 2053 2054 case EXCLUSIVE_GET: 2055 dev_warn(dev, 2056 "dummy supplies not allowed for exclusive requests\n"); 2057 fallthrough; 2058 2059 default: 2060 return ERR_PTR(-ENODEV); 2061 } 2062 } 2063 2064 if (rdev->exclusive) { 2065 regulator = ERR_PTR(-EPERM); 2066 put_device(&rdev->dev); 2067 return regulator; 2068 } 2069 2070 if (get_type == EXCLUSIVE_GET && rdev->open_count) { 2071 regulator = ERR_PTR(-EBUSY); 2072 put_device(&rdev->dev); 2073 return regulator; 2074 } 2075 2076 mutex_lock(®ulator_list_mutex); 2077 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled); 2078 mutex_unlock(®ulator_list_mutex); 2079 2080 if (ret != 0) { 2081 regulator = ERR_PTR(-EPROBE_DEFER); 2082 put_device(&rdev->dev); 2083 return regulator; 2084 } 2085 2086 ret = regulator_resolve_supply(rdev); 2087 if (ret < 0) { 2088 regulator = ERR_PTR(ret); 2089 put_device(&rdev->dev); 2090 return regulator; 2091 } 2092 2093 if (!try_module_get(rdev->owner)) { 2094 regulator = ERR_PTR(-EPROBE_DEFER); 2095 put_device(&rdev->dev); 2096 return regulator; 2097 } 2098 2099 regulator_lock(rdev); 2100 regulator = create_regulator(rdev, dev, id); 2101 regulator_unlock(rdev); 2102 if (regulator == NULL) { 2103 regulator = ERR_PTR(-ENOMEM); 2104 module_put(rdev->owner); 2105 put_device(&rdev->dev); 2106 return regulator; 2107 } 2108 2109 rdev->open_count++; 2110 if (get_type == EXCLUSIVE_GET) { 2111 rdev->exclusive = 1; 2112 2113 ret = _regulator_is_enabled(rdev); 2114 if (ret > 0) { 2115 rdev->use_count = 1; 2116 regulator->enable_count = 1; 2117 } else { 2118 rdev->use_count = 0; 2119 regulator->enable_count = 0; 2120 } 2121 } 2122 2123 link = device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS); 2124 if (!IS_ERR_OR_NULL(link)) 2125 regulator->device_link = true; 2126 2127 return regulator; 2128} 2129 2130/** 2131 * regulator_get - lookup and obtain a reference to a regulator. 2132 * @dev: device for regulator "consumer" 2133 * @id: Supply name or regulator ID. 2134 * 2135 * Returns a struct regulator corresponding to the regulator producer, 2136 * or IS_ERR() condition containing errno. 2137 * 2138 * Use of supply names configured via regulator_set_device_supply() is 2139 * strongly encouraged. It is recommended that the supply name used 2140 * should match the name used for the supply and/or the relevant 2141 * device pins in the datasheet. 2142 */ 2143struct regulator *regulator_get(struct device *dev, const char *id) 2144{ 2145 return _regulator_get(dev, id, NORMAL_GET); 2146} 2147EXPORT_SYMBOL_GPL(regulator_get); 2148 2149/** 2150 * regulator_get_exclusive - obtain exclusive access to a regulator. 2151 * @dev: device for regulator "consumer" 2152 * @id: Supply name or regulator ID. 2153 * 2154 * Returns a struct regulator corresponding to the regulator producer, 2155 * or IS_ERR() condition containing errno. Other consumers will be 2156 * unable to obtain this regulator while this reference is held and the 2157 * use count for the regulator will be initialised to reflect the current 2158 * state of the regulator. 2159 * 2160 * This is intended for use by consumers which cannot tolerate shared 2161 * use of the regulator such as those which need to force the 2162 * regulator off for correct operation of the hardware they are 2163 * controlling. 2164 * 2165 * Use of supply names configured via regulator_set_device_supply() is 2166 * strongly encouraged. It is recommended that the supply name used 2167 * should match the name used for the supply and/or the relevant 2168 * device pins in the datasheet. 2169 */ 2170struct regulator *regulator_get_exclusive(struct device *dev, const char *id) 2171{ 2172 return _regulator_get(dev, id, EXCLUSIVE_GET); 2173} 2174EXPORT_SYMBOL_GPL(regulator_get_exclusive); 2175 2176/** 2177 * regulator_get_optional - obtain optional access to a regulator. 2178 * @dev: device for regulator "consumer" 2179 * @id: Supply name or regulator ID. 2180 * 2181 * Returns a struct regulator corresponding to the regulator producer, 2182 * or IS_ERR() condition containing errno. 2183 * 2184 * This is intended for use by consumers for devices which can have 2185 * some supplies unconnected in normal use, such as some MMC devices. 2186 * It can allow the regulator core to provide stub supplies for other 2187 * supplies requested using normal regulator_get() calls without 2188 * disrupting the operation of drivers that can handle absent 2189 * supplies. 2190 * 2191 * Use of supply names configured via regulator_set_device_supply() is 2192 * strongly encouraged. It is recommended that the supply name used 2193 * should match the name used for the supply and/or the relevant 2194 * device pins in the datasheet. 2195 */ 2196struct regulator *regulator_get_optional(struct device *dev, const char *id) 2197{ 2198 return _regulator_get(dev, id, OPTIONAL_GET); 2199} 2200EXPORT_SYMBOL_GPL(regulator_get_optional); 2201 2202static void destroy_regulator(struct regulator *regulator) 2203{ 2204 struct regulator_dev *rdev = regulator->rdev; 2205 2206 debugfs_remove_recursive(regulator->debugfs); 2207 2208 if (regulator->dev) { 2209 if (regulator->device_link) 2210 device_link_remove(regulator->dev, &rdev->dev); 2211 2212 /* remove any sysfs entries */ 2213 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); 2214 } 2215 2216 regulator_lock(rdev); 2217 list_del(®ulator->list); 2218 2219 rdev->open_count--; 2220 rdev->exclusive = 0; 2221 regulator_unlock(rdev); 2222 2223 kfree_const(regulator->supply_name); 2224 kfree(regulator); 2225} 2226 2227/* regulator_list_mutex lock held by regulator_put() */ 2228static void _regulator_put(struct regulator *regulator) 2229{ 2230 struct regulator_dev *rdev; 2231 2232 if (IS_ERR_OR_NULL(regulator)) 2233 return; 2234 2235 lockdep_assert_held_once(®ulator_list_mutex); 2236 2237 /* Docs say you must disable before calling regulator_put() */ 2238 WARN_ON(regulator->enable_count); 2239 2240 rdev = regulator->rdev; 2241 2242 destroy_regulator(regulator); 2243 2244 module_put(rdev->owner); 2245 put_device(&rdev->dev); 2246} 2247 2248/** 2249 * regulator_put - "free" the regulator source 2250 * @regulator: regulator source 2251 * 2252 * Note: drivers must ensure that all regulator_enable calls made on this 2253 * regulator source are balanced by regulator_disable calls prior to calling 2254 * this function. 2255 */ 2256void regulator_put(struct regulator *regulator) 2257{ 2258 mutex_lock(®ulator_list_mutex); 2259 _regulator_put(regulator); 2260 mutex_unlock(®ulator_list_mutex); 2261} 2262EXPORT_SYMBOL_GPL(regulator_put); 2263 2264/** 2265 * regulator_register_supply_alias - Provide device alias for supply lookup 2266 * 2267 * @dev: device that will be given as the regulator "consumer" 2268 * @id: Supply name or regulator ID 2269 * @alias_dev: device that should be used to lookup the supply 2270 * @alias_id: Supply name or regulator ID that should be used to lookup the 2271 * supply 2272 * 2273 * All lookups for id on dev will instead be conducted for alias_id on 2274 * alias_dev. 2275 */ 2276int regulator_register_supply_alias(struct device *dev, const char *id, 2277 struct device *alias_dev, 2278 const char *alias_id) 2279{ 2280 struct regulator_supply_alias *map; 2281 2282 map = regulator_find_supply_alias(dev, id); 2283 if (map) 2284 return -EEXIST; 2285 2286 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL); 2287 if (!map) 2288 return -ENOMEM; 2289 2290 map->src_dev = dev; 2291 map->src_supply = id; 2292 map->alias_dev = alias_dev; 2293 map->alias_supply = alias_id; 2294 2295 list_add(&map->list, ®ulator_supply_alias_list); 2296 2297 pr_info("Adding alias for supply %s,%s -> %s,%s\n", 2298 id, dev_name(dev), alias_id, dev_name(alias_dev)); 2299 2300 return 0; 2301} 2302EXPORT_SYMBOL_GPL(regulator_register_supply_alias); 2303 2304/** 2305 * regulator_unregister_supply_alias - Remove device alias 2306 * 2307 * @dev: device that will be given as the regulator "consumer" 2308 * @id: Supply name or regulator ID 2309 * 2310 * Remove a lookup alias if one exists for id on dev. 2311 */ 2312void regulator_unregister_supply_alias(struct device *dev, const char *id) 2313{ 2314 struct regulator_supply_alias *map; 2315 2316 map = regulator_find_supply_alias(dev, id); 2317 if (map) { 2318 list_del(&map->list); 2319 kfree(map); 2320 } 2321} 2322EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias); 2323 2324/** 2325 * regulator_bulk_register_supply_alias - register multiple aliases 2326 * 2327 * @dev: device that will be given as the regulator "consumer" 2328 * @id: List of supply names or regulator IDs 2329 * @alias_dev: device that should be used to lookup the supply 2330 * @alias_id: List of supply names or regulator IDs that should be used to 2331 * lookup the supply 2332 * @num_id: Number of aliases to register 2333 * 2334 * @return 0 on success, an errno on failure. 2335 * 2336 * This helper function allows drivers to register several supply 2337 * aliases in one operation. If any of the aliases cannot be 2338 * registered any aliases that were registered will be removed 2339 * before returning to the caller. 2340 */ 2341int regulator_bulk_register_supply_alias(struct device *dev, 2342 const char *const *id, 2343 struct device *alias_dev, 2344 const char *const *alias_id, 2345 int num_id) 2346{ 2347 int i; 2348 int ret; 2349 2350 for (i = 0; i < num_id; ++i) { 2351 ret = regulator_register_supply_alias(dev, id[i], alias_dev, 2352 alias_id[i]); 2353 if (ret < 0) 2354 goto err; 2355 } 2356 2357 return 0; 2358 2359err: 2360 dev_err(dev, 2361 "Failed to create supply alias %s,%s -> %s,%s\n", 2362 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev)); 2363 2364 while (--i >= 0) 2365 regulator_unregister_supply_alias(dev, id[i]); 2366 2367 return ret; 2368} 2369EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias); 2370 2371/** 2372 * regulator_bulk_unregister_supply_alias - unregister multiple aliases 2373 * 2374 * @dev: device that will be given as the regulator "consumer" 2375 * @id: List of supply names or regulator IDs 2376 * @num_id: Number of aliases to unregister 2377 * 2378 * This helper function allows drivers to unregister several supply 2379 * aliases in one operation. 2380 */ 2381void regulator_bulk_unregister_supply_alias(struct device *dev, 2382 const char *const *id, 2383 int num_id) 2384{ 2385 int i; 2386 2387 for (i = 0; i < num_id; ++i) 2388 regulator_unregister_supply_alias(dev, id[i]); 2389} 2390EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias); 2391 2392 2393/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */ 2394static int regulator_ena_gpio_request(struct regulator_dev *rdev, 2395 const struct regulator_config *config) 2396{ 2397 struct regulator_enable_gpio *pin, *new_pin; 2398 struct gpio_desc *gpiod; 2399 2400 gpiod = config->ena_gpiod; 2401 new_pin = kzalloc(sizeof(*new_pin), GFP_KERNEL); 2402 2403 mutex_lock(®ulator_list_mutex); 2404 2405 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) { 2406 if (pin->gpiod == gpiod) { 2407 rdev_dbg(rdev, "GPIO is already used\n"); 2408 goto update_ena_gpio_to_rdev; 2409 } 2410 } 2411 2412 if (new_pin == NULL) { 2413 mutex_unlock(®ulator_list_mutex); 2414 return -ENOMEM; 2415 } 2416 2417 pin = new_pin; 2418 new_pin = NULL; 2419 2420 pin->gpiod = gpiod; 2421 list_add(&pin->list, ®ulator_ena_gpio_list); 2422 2423update_ena_gpio_to_rdev: 2424 pin->request_count++; 2425 rdev->ena_pin = pin; 2426 2427 mutex_unlock(®ulator_list_mutex); 2428 kfree(new_pin); 2429 2430 return 0; 2431} 2432 2433static void regulator_ena_gpio_free(struct regulator_dev *rdev) 2434{ 2435 struct regulator_enable_gpio *pin, *n; 2436 2437 if (!rdev->ena_pin) 2438 return; 2439 2440 /* Free the GPIO only in case of no use */ 2441 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) { 2442 if (pin != rdev->ena_pin) 2443 continue; 2444 2445 if (--pin->request_count) 2446 break; 2447 2448 gpiod_put(pin->gpiod); 2449 list_del(&pin->list); 2450 kfree(pin); 2451 break; 2452 } 2453 2454 rdev->ena_pin = NULL; 2455} 2456 2457/** 2458 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control 2459 * @rdev: regulator_dev structure 2460 * @enable: enable GPIO at initial use? 2461 * 2462 * GPIO is enabled in case of initial use. (enable_count is 0) 2463 * GPIO is disabled when it is not shared any more. (enable_count <= 1) 2464 */ 2465static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable) 2466{ 2467 struct regulator_enable_gpio *pin = rdev->ena_pin; 2468 2469 if (!pin) 2470 return -EINVAL; 2471 2472 if (enable) { 2473 /* Enable GPIO at initial use */ 2474 if (pin->enable_count == 0) 2475 gpiod_set_value_cansleep(pin->gpiod, 1); 2476 2477 pin->enable_count++; 2478 } else { 2479 if (pin->enable_count > 1) { 2480 pin->enable_count--; 2481 return 0; 2482 } 2483 2484 /* Disable GPIO if not used */ 2485 if (pin->enable_count <= 1) { 2486 gpiod_set_value_cansleep(pin->gpiod, 0); 2487 pin->enable_count = 0; 2488 } 2489 } 2490 2491 return 0; 2492} 2493 2494/** 2495 * _regulator_enable_delay - a delay helper function 2496 * @delay: time to delay in microseconds 2497 * 2498 * Delay for the requested amount of time as per the guidelines in: 2499 * 2500 * Documentation/timers/timers-howto.rst 2501 * 2502 * The assumption here is that regulators will never be enabled in 2503 * atomic context and therefore sleeping functions can be used. 2504 */ 2505static void _regulator_enable_delay(unsigned int delay) 2506{ 2507 unsigned int ms = delay / 1000; 2508 unsigned int us = delay % 1000; 2509 2510 if (ms > 0) { 2511 /* 2512 * For small enough values, handle super-millisecond 2513 * delays in the usleep_range() call below. 2514 */ 2515 if (ms < 20) 2516 us += ms * 1000; 2517 else 2518 msleep(ms); 2519 } 2520 2521 /* 2522 * Give the scheduler some room to coalesce with any other 2523 * wakeup sources. For delays shorter than 10 us, don't even 2524 * bother setting up high-resolution timers and just busy- 2525 * loop. 2526 */ 2527 if (us >= 10) 2528 usleep_range(us, us + 100); 2529 else 2530 udelay(us); 2531} 2532 2533/** 2534 * _regulator_check_status_enabled 2535 * 2536 * A helper function to check if the regulator status can be interpreted 2537 * as 'regulator is enabled'. 2538 * @rdev: the regulator device to check 2539 * 2540 * Return: 2541 * * 1 - if status shows regulator is in enabled state 2542 * * 0 - if not enabled state 2543 * * Error Value - as received from ops->get_status() 2544 */ 2545static inline int _regulator_check_status_enabled(struct regulator_dev *rdev) 2546{ 2547 int ret = rdev->desc->ops->get_status(rdev); 2548 2549 if (ret < 0) { 2550 rdev_info(rdev, "get_status returned error: %d\n", ret); 2551 return ret; 2552 } 2553 2554 switch (ret) { 2555 case REGULATOR_STATUS_OFF: 2556 case REGULATOR_STATUS_ERROR: 2557 case REGULATOR_STATUS_UNDEFINED: 2558 return 0; 2559 default: 2560 return 1; 2561 } 2562} 2563 2564static int _regulator_do_enable(struct regulator_dev *rdev) 2565{ 2566 int ret, delay; 2567 2568 /* Query before enabling in case configuration dependent. */ 2569 ret = _regulator_get_enable_time(rdev); 2570 if (ret >= 0) { 2571 delay = ret; 2572 } else { 2573 rdev_warn(rdev, "enable_time() failed: %pe\n", ERR_PTR(ret)); 2574 delay = 0; 2575 } 2576 2577 trace_regulator_enable(rdev_get_name(rdev)); 2578 2579 if (rdev->desc->off_on_delay) { 2580 /* if needed, keep a distance of off_on_delay from last time 2581 * this regulator was disabled. 2582 */ 2583 unsigned long start_jiffy = jiffies; 2584 unsigned long intended, max_delay, remaining; 2585 2586 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay); 2587 intended = rdev->last_off_jiffy + max_delay; 2588 2589 if (time_before(start_jiffy, intended)) { 2590 /* calc remaining jiffies to deal with one-time 2591 * timer wrapping. 2592 * in case of multiple timer wrapping, either it can be 2593 * detected by out-of-range remaining, or it cannot be 2594 * detected and we get a penalty of 2595 * _regulator_enable_delay(). 2596 */ 2597 remaining = intended - start_jiffy; 2598 if (remaining <= max_delay) 2599 _regulator_enable_delay( 2600 jiffies_to_usecs(remaining)); 2601 } 2602 } 2603 2604 if (rdev->ena_pin) { 2605 if (!rdev->ena_gpio_state) { 2606 ret = regulator_ena_gpio_ctrl(rdev, true); 2607 if (ret < 0) 2608 return ret; 2609 rdev->ena_gpio_state = 1; 2610 } 2611 } else if (rdev->desc->ops->enable) { 2612 ret = rdev->desc->ops->enable(rdev); 2613 if (ret < 0) 2614 return ret; 2615 } else { 2616 return -EINVAL; 2617 } 2618 2619 /* Allow the regulator to ramp; it would be useful to extend 2620 * this for bulk operations so that the regulators can ramp 2621 * together. */ 2622 trace_regulator_enable_delay(rdev_get_name(rdev)); 2623 2624 /* If poll_enabled_time is set, poll upto the delay calculated 2625 * above, delaying poll_enabled_time uS to check if the regulator 2626 * actually got enabled. 2627 * If the regulator isn't enabled after enable_delay has 2628 * expired, return -ETIMEDOUT. 2629 */ 2630 if (rdev->desc->poll_enabled_time) { 2631 int time_remaining = delay; 2632 2633 while (time_remaining > 0) { 2634 _regulator_enable_delay(rdev->desc->poll_enabled_time); 2635 2636 if (rdev->desc->ops->get_status) { 2637 ret = _regulator_check_status_enabled(rdev); 2638 if (ret < 0) 2639 return ret; 2640 else if (ret) 2641 break; 2642 } else if (rdev->desc->ops->is_enabled(rdev)) 2643 break; 2644 2645 time_remaining -= rdev->desc->poll_enabled_time; 2646 } 2647 2648 if (time_remaining <= 0) { 2649 rdev_err(rdev, "Enabled check timed out\n"); 2650 return -ETIMEDOUT; 2651 } 2652 } else { 2653 _regulator_enable_delay(delay); 2654 } 2655 2656 trace_regulator_enable_complete(rdev_get_name(rdev)); 2657 2658 return 0; 2659} 2660 2661/** 2662 * _regulator_handle_consumer_enable - handle that a consumer enabled 2663 * @regulator: regulator source 2664 * 2665 * Some things on a regulator consumer (like the contribution towards total 2666 * load on the regulator) only have an effect when the consumer wants the 2667 * regulator enabled. Explained in example with two consumers of the same 2668 * regulator: 2669 * consumer A: set_load(100); => total load = 0 2670 * consumer A: regulator_enable(); => total load = 100 2671 * consumer B: set_load(1000); => total load = 100 2672 * consumer B: regulator_enable(); => total load = 1100 2673 * consumer A: regulator_disable(); => total_load = 1000 2674 * 2675 * This function (together with _regulator_handle_consumer_disable) is 2676 * responsible for keeping track of the refcount for a given regulator consumer 2677 * and applying / unapplying these things. 2678 * 2679 * Returns 0 upon no error; -error upon error. 2680 */ 2681static int _regulator_handle_consumer_enable(struct regulator *regulator) 2682{ 2683 int ret; 2684 struct regulator_dev *rdev = regulator->rdev; 2685 2686 lockdep_assert_held_once(&rdev->mutex.base); 2687 2688 regulator->enable_count++; 2689 if (regulator->uA_load && regulator->enable_count == 1) { 2690 ret = drms_uA_update(rdev); 2691 if (ret) 2692 regulator->enable_count--; 2693 return ret; 2694 } 2695 2696 return 0; 2697} 2698 2699/** 2700 * _regulator_handle_consumer_disable - handle that a consumer disabled 2701 * @regulator: regulator source 2702 * 2703 * The opposite of _regulator_handle_consumer_enable(). 2704 * 2705 * Returns 0 upon no error; -error upon error. 2706 */ 2707static int _regulator_handle_consumer_disable(struct regulator *regulator) 2708{ 2709 struct regulator_dev *rdev = regulator->rdev; 2710 2711 lockdep_assert_held_once(&rdev->mutex.base); 2712 2713 if (!regulator->enable_count) { 2714 rdev_err(rdev, "Underflow of regulator enable count\n"); 2715 return -EINVAL; 2716 } 2717 2718 regulator->enable_count--; 2719 if (regulator->uA_load && regulator->enable_count == 0) 2720 return drms_uA_update(rdev); 2721 2722 return 0; 2723} 2724 2725/* locks held by regulator_enable() */ 2726static int _regulator_enable(struct regulator *regulator) 2727{ 2728 struct regulator_dev *rdev = regulator->rdev; 2729 int ret; 2730 2731 lockdep_assert_held_once(&rdev->mutex.base); 2732 2733 if (rdev->use_count == 0 && rdev->supply) { 2734 ret = _regulator_enable(rdev->supply); 2735 if (ret < 0) 2736 return ret; 2737 } 2738 2739 /* balance only if there are regulators coupled */ 2740 if (rdev->coupling_desc.n_coupled > 1) { 2741 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); 2742 if (ret < 0) 2743 goto err_disable_supply; 2744 } 2745 2746 ret = _regulator_handle_consumer_enable(regulator); 2747 if (ret < 0) 2748 goto err_disable_supply; 2749 2750 if (rdev->use_count == 0) { 2751 /* The regulator may on if it's not switchable or left on */ 2752 ret = _regulator_is_enabled(rdev); 2753 if (ret == -EINVAL || ret == 0) { 2754 if (!regulator_ops_is_valid(rdev, 2755 REGULATOR_CHANGE_STATUS)) { 2756 ret = -EPERM; 2757 goto err_consumer_disable; 2758 } 2759 2760 ret = _regulator_do_enable(rdev); 2761 if (ret < 0) 2762 goto err_consumer_disable; 2763 2764 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE, 2765 NULL); 2766 } else if (ret < 0) { 2767 rdev_err(rdev, "is_enabled() failed: %pe\n", ERR_PTR(ret)); 2768 goto err_consumer_disable; 2769 } 2770 /* Fallthrough on positive return values - already enabled */ 2771 } 2772 2773 if (regulator->enable_count == 1) 2774 rdev->use_count++; 2775 2776 return 0; 2777 2778err_consumer_disable: 2779 _regulator_handle_consumer_disable(regulator); 2780 2781err_disable_supply: 2782 if (rdev->use_count == 0 && rdev->supply) 2783 _regulator_disable(rdev->supply); 2784 2785 return ret; 2786} 2787 2788/** 2789 * regulator_enable - enable regulator output 2790 * @regulator: regulator source 2791 * 2792 * Request that the regulator be enabled with the regulator output at 2793 * the predefined voltage or current value. Calls to regulator_enable() 2794 * must be balanced with calls to regulator_disable(). 2795 * 2796 * NOTE: the output value can be set by other drivers, boot loader or may be 2797 * hardwired in the regulator. 2798 */ 2799int regulator_enable(struct regulator *regulator) 2800{ 2801 struct regulator_dev *rdev = regulator->rdev; 2802 struct ww_acquire_ctx ww_ctx; 2803 int ret; 2804 2805 regulator_lock_dependent(rdev, &ww_ctx); 2806 ret = _regulator_enable(regulator); 2807 regulator_unlock_dependent(rdev, &ww_ctx); 2808 2809 return ret; 2810} 2811EXPORT_SYMBOL_GPL(regulator_enable); 2812 2813static int _regulator_do_disable(struct regulator_dev *rdev) 2814{ 2815 int ret; 2816 2817 trace_regulator_disable(rdev_get_name(rdev)); 2818 2819 if (rdev->ena_pin) { 2820 if (rdev->ena_gpio_state) { 2821 ret = regulator_ena_gpio_ctrl(rdev, false); 2822 if (ret < 0) 2823 return ret; 2824 rdev->ena_gpio_state = 0; 2825 } 2826 2827 } else if (rdev->desc->ops->disable) { 2828 ret = rdev->desc->ops->disable(rdev); 2829 if (ret != 0) 2830 return ret; 2831 } 2832 2833 /* cares about last_off_jiffy only if off_on_delay is required by 2834 * device. 2835 */ 2836 if (rdev->desc->off_on_delay) 2837 rdev->last_off_jiffy = jiffies; 2838 2839 trace_regulator_disable_complete(rdev_get_name(rdev)); 2840 2841 return 0; 2842} 2843 2844/* locks held by regulator_disable() */ 2845static int _regulator_disable(struct regulator *regulator) 2846{ 2847 struct regulator_dev *rdev = regulator->rdev; 2848 int ret = 0; 2849 2850 lockdep_assert_held_once(&rdev->mutex.base); 2851 2852 if (WARN(regulator->enable_count == 0, 2853 "unbalanced disables for %s\n", rdev_get_name(rdev))) 2854 return -EIO; 2855 2856 if (regulator->enable_count == 1) { 2857 /* disabling last enable_count from this regulator */ 2858 /* are we the last user and permitted to disable ? */ 2859 if (rdev->use_count == 1 && 2860 (rdev->constraints && !rdev->constraints->always_on)) { 2861 2862 /* we are last user */ 2863 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) { 2864 ret = _notifier_call_chain(rdev, 2865 REGULATOR_EVENT_PRE_DISABLE, 2866 NULL); 2867 if (ret & NOTIFY_STOP_MASK) 2868 return -EINVAL; 2869 2870 ret = _regulator_do_disable(rdev); 2871 if (ret < 0) { 2872 rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret)); 2873 _notifier_call_chain(rdev, 2874 REGULATOR_EVENT_ABORT_DISABLE, 2875 NULL); 2876 return ret; 2877 } 2878 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, 2879 NULL); 2880 } 2881 2882 rdev->use_count = 0; 2883 } else if (rdev->use_count > 1) { 2884 rdev->use_count--; 2885 } 2886 } 2887 2888 if (ret == 0) 2889 ret = _regulator_handle_consumer_disable(regulator); 2890 2891 if (ret == 0 && rdev->coupling_desc.n_coupled > 1) 2892 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON); 2893 2894 if (ret == 0 && rdev->use_count == 0 && rdev->supply) 2895 ret = _regulator_disable(rdev->supply); 2896 2897 return ret; 2898} 2899 2900/** 2901 * regulator_disable - disable regulator output 2902 * @regulator: regulator source 2903 * 2904 * Disable the regulator output voltage or current. Calls to 2905 * regulator_enable() must be balanced with calls to 2906 * regulator_disable(). 2907 * 2908 * NOTE: this will only disable the regulator output if no other consumer 2909 * devices have it enabled, the regulator device supports disabling and 2910 * machine constraints permit this operation. 2911 */ 2912int regulator_disable(struct regulator *regulator) 2913{ 2914 struct regulator_dev *rdev = regulator->rdev; 2915 struct ww_acquire_ctx ww_ctx; 2916 int ret; 2917 2918 regulator_lock_dependent(rdev, &ww_ctx); 2919 ret = _regulator_disable(regulator); 2920 regulator_unlock_dependent(rdev, &ww_ctx); 2921 2922 return ret; 2923} 2924EXPORT_SYMBOL_GPL(regulator_disable); 2925 2926/* locks held by regulator_force_disable() */ 2927static int _regulator_force_disable(struct regulator_dev *rdev) 2928{ 2929 int ret = 0; 2930 2931 lockdep_assert_held_once(&rdev->mutex.base); 2932 2933 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | 2934 REGULATOR_EVENT_PRE_DISABLE, NULL); 2935 if (ret & NOTIFY_STOP_MASK) 2936 return -EINVAL; 2937 2938 ret = _regulator_do_disable(rdev); 2939 if (ret < 0) { 2940 rdev_err(rdev, "failed to force disable: %pe\n", ERR_PTR(ret)); 2941 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | 2942 REGULATOR_EVENT_ABORT_DISABLE, NULL); 2943 return ret; 2944 } 2945 2946 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | 2947 REGULATOR_EVENT_DISABLE, NULL); 2948 2949 return 0; 2950} 2951 2952/** 2953 * regulator_force_disable - force disable regulator output 2954 * @regulator: regulator source 2955 * 2956 * Forcibly disable the regulator output voltage or current. 2957 * NOTE: this *will* disable the regulator output even if other consumer 2958 * devices have it enabled. This should be used for situations when device 2959 * damage will likely occur if the regulator is not disabled (e.g. over temp). 2960 */ 2961int regulator_force_disable(struct regulator *regulator) 2962{ 2963 struct regulator_dev *rdev = regulator->rdev; 2964 struct ww_acquire_ctx ww_ctx; 2965 int ret; 2966 2967 regulator_lock_dependent(rdev, &ww_ctx); 2968 2969 ret = _regulator_force_disable(regulator->rdev); 2970 2971 if (rdev->coupling_desc.n_coupled > 1) 2972 regulator_balance_voltage(rdev, PM_SUSPEND_ON); 2973 2974 if (regulator->uA_load) { 2975 regulator->uA_load = 0; 2976 ret = drms_uA_update(rdev); 2977 } 2978 2979 if (rdev->use_count != 0 && rdev->supply) 2980 _regulator_disable(rdev->supply); 2981 2982 regulator_unlock_dependent(rdev, &ww_ctx); 2983 2984 return ret; 2985} 2986EXPORT_SYMBOL_GPL(regulator_force_disable); 2987 2988static void regulator_disable_work(struct work_struct *work) 2989{ 2990 struct regulator_dev *rdev = container_of(work, struct regulator_dev, 2991 disable_work.work); 2992 struct ww_acquire_ctx ww_ctx; 2993 int count, i, ret; 2994 struct regulator *regulator; 2995 int total_count = 0; 2996 2997 regulator_lock_dependent(rdev, &ww_ctx); 2998 2999 /* 3000 * Workqueue functions queue the new work instance while the previous 3001 * work instance is being processed. Cancel the queued work instance 3002 * as the work instance under processing does the job of the queued 3003 * work instance. 3004 */ 3005 cancel_delayed_work(&rdev->disable_work); 3006 3007 list_for_each_entry(regulator, &rdev->consumer_list, list) { 3008 count = regulator->deferred_disables; 3009 3010 if (!count) 3011 continue; 3012 3013 total_count += count; 3014 regulator->deferred_disables = 0; 3015 3016 for (i = 0; i < count; i++) { 3017 ret = _regulator_disable(regulator); 3018 if (ret != 0) 3019 rdev_err(rdev, "Deferred disable failed: %pe\n", 3020 ERR_PTR(ret)); 3021 } 3022 } 3023 WARN_ON(!total_count); 3024 3025 if (rdev->coupling_desc.n_coupled > 1) 3026 regulator_balance_voltage(rdev, PM_SUSPEND_ON); 3027 3028 regulator_unlock_dependent(rdev, &ww_ctx); 3029} 3030 3031/** 3032 * regulator_disable_deferred - disable regulator output with delay 3033 * @regulator: regulator source 3034 * @ms: milliseconds until the regulator is disabled 3035 * 3036 * Execute regulator_disable() on the regulator after a delay. This 3037 * is intended for use with devices that require some time to quiesce. 3038 * 3039 * NOTE: this will only disable the regulator output if no other consumer 3040 * devices have it enabled, the regulator device supports disabling and 3041 * machine constraints permit this operation. 3042 */ 3043int regulator_disable_deferred(struct regulator *regulator, int ms) 3044{ 3045 struct regulator_dev *rdev = regulator->rdev; 3046 3047 if (!ms) 3048 return regulator_disable(regulator); 3049 3050 regulator_lock(rdev); 3051 regulator->deferred_disables++; 3052 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work, 3053 msecs_to_jiffies(ms)); 3054 regulator_unlock(rdev); 3055 3056 return 0; 3057} 3058EXPORT_SYMBOL_GPL(regulator_disable_deferred); 3059 3060static int _regulator_is_enabled(struct regulator_dev *rdev) 3061{ 3062 /* A GPIO control always takes precedence */ 3063 if (rdev->ena_pin) 3064 return rdev->ena_gpio_state; 3065 3066 /* If we don't know then assume that the regulator is always on */ 3067 if (!rdev->desc->ops->is_enabled) 3068 return 1; 3069 3070 return rdev->desc->ops->is_enabled(rdev); 3071} 3072 3073static int _regulator_list_voltage(struct regulator_dev *rdev, 3074 unsigned selector, int lock) 3075{ 3076 const struct regulator_ops *ops = rdev->desc->ops; 3077 int ret; 3078 3079 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) 3080 return rdev->desc->fixed_uV; 3081 3082 if (ops->list_voltage) { 3083 if (selector >= rdev->desc->n_voltages) 3084 return -EINVAL; 3085 if (lock) 3086 regulator_lock(rdev); 3087 ret = ops->list_voltage(rdev, selector); 3088 if (lock) 3089 regulator_unlock(rdev); 3090 } else if (rdev->is_switch && rdev->supply) { 3091 ret = _regulator_list_voltage(rdev->supply->rdev, 3092 selector, lock); 3093 } else { 3094 return -EINVAL; 3095 } 3096 3097 if (ret > 0) { 3098 if (ret < rdev->constraints->min_uV) 3099 ret = 0; 3100 else if (ret > rdev->constraints->max_uV) 3101 ret = 0; 3102 } 3103 3104 return ret; 3105} 3106 3107/** 3108 * regulator_is_enabled - is the regulator output enabled 3109 * @regulator: regulator source 3110 * 3111 * Returns positive if the regulator driver backing the source/client 3112 * has requested that the device be enabled, zero if it hasn't, else a 3113 * negative errno code. 3114 * 3115 * Note that the device backing this regulator handle can have multiple 3116 * users, so it might be enabled even if regulator_enable() was never 3117 * called for this particular source. 3118 */ 3119int regulator_is_enabled(struct regulator *regulator) 3120{ 3121 int ret; 3122 3123 if (regulator->always_on) 3124 return 1; 3125 3126 regulator_lock(regulator->rdev); 3127 ret = _regulator_is_enabled(regulator->rdev); 3128 regulator_unlock(regulator->rdev); 3129 3130 return ret; 3131} 3132EXPORT_SYMBOL_GPL(regulator_is_enabled); 3133 3134/** 3135 * regulator_count_voltages - count regulator_list_voltage() selectors 3136 * @regulator: regulator source 3137 * 3138 * Returns number of selectors, or negative errno. Selectors are 3139 * numbered starting at zero, and typically correspond to bitfields 3140 * in hardware registers. 3141 */ 3142int regulator_count_voltages(struct regulator *regulator) 3143{ 3144 struct regulator_dev *rdev = regulator->rdev; 3145 3146 if (rdev->desc->n_voltages) 3147 return rdev->desc->n_voltages; 3148 3149 if (!rdev->is_switch || !rdev->supply) 3150 return -EINVAL; 3151 3152 return regulator_count_voltages(rdev->supply); 3153} 3154EXPORT_SYMBOL_GPL(regulator_count_voltages); 3155 3156/** 3157 * regulator_list_voltage - enumerate supported voltages 3158 * @regulator: regulator source 3159 * @selector: identify voltage to list 3160 * Context: can sleep 3161 * 3162 * Returns a voltage that can be passed to @regulator_set_voltage(), 3163 * zero if this selector code can't be used on this system, or a 3164 * negative errno. 3165 */ 3166int regulator_list_voltage(struct regulator *regulator, unsigned selector) 3167{ 3168 return _regulator_list_voltage(regulator->rdev, selector, 1); 3169} 3170EXPORT_SYMBOL_GPL(regulator_list_voltage); 3171 3172/** 3173 * regulator_get_regmap - get the regulator's register map 3174 * @regulator: regulator source 3175 * 3176 * Returns the register map for the given regulator, or an ERR_PTR value 3177 * if the regulator doesn't use regmap. 3178 */ 3179struct regmap *regulator_get_regmap(struct regulator *regulator) 3180{ 3181 struct regmap *map = regulator->rdev->regmap; 3182 3183 return map ? map : ERR_PTR(-EOPNOTSUPP); 3184} 3185 3186/** 3187 * regulator_get_hardware_vsel_register - get the HW voltage selector register 3188 * @regulator: regulator source 3189 * @vsel_reg: voltage selector register, output parameter 3190 * @vsel_mask: mask for voltage selector bitfield, output parameter 3191 * 3192 * Returns the hardware register offset and bitmask used for setting the 3193 * regulator voltage. This might be useful when configuring voltage-scaling 3194 * hardware or firmware that can make I2C requests behind the kernel's back, 3195 * for example. 3196 * 3197 * On success, the output parameters @vsel_reg and @vsel_mask are filled in 3198 * and 0 is returned, otherwise a negative errno is returned. 3199 */ 3200int regulator_get_hardware_vsel_register(struct regulator *regulator, 3201 unsigned *vsel_reg, 3202 unsigned *vsel_mask) 3203{ 3204 struct regulator_dev *rdev = regulator->rdev; 3205 const struct regulator_ops *ops = rdev->desc->ops; 3206 3207 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) 3208 return -EOPNOTSUPP; 3209 3210 *vsel_reg = rdev->desc->vsel_reg; 3211 *vsel_mask = rdev->desc->vsel_mask; 3212 3213 return 0; 3214} 3215EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register); 3216 3217/** 3218 * regulator_list_hardware_vsel - get the HW-specific register value for a selector 3219 * @regulator: regulator source 3220 * @selector: identify voltage to list 3221 * 3222 * Converts the selector to a hardware-specific voltage selector that can be 3223 * directly written to the regulator registers. The address of the voltage 3224 * register can be determined by calling @regulator_get_hardware_vsel_register. 3225 * 3226 * On error a negative errno is returned. 3227 */ 3228int regulator_list_hardware_vsel(struct regulator *regulator, 3229 unsigned selector) 3230{ 3231 struct regulator_dev *rdev = regulator->rdev; 3232 const struct regulator_ops *ops = rdev->desc->ops; 3233 3234 if (selector >= rdev->desc->n_voltages) 3235 return -EINVAL; 3236 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) 3237 return -EOPNOTSUPP; 3238 3239 return selector; 3240} 3241EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel); 3242 3243/** 3244 * regulator_get_linear_step - return the voltage step size between VSEL values 3245 * @regulator: regulator source 3246 * 3247 * Returns the voltage step size between VSEL values for linear 3248 * regulators, or return 0 if the regulator isn't a linear regulator. 3249 */ 3250unsigned int regulator_get_linear_step(struct regulator *regulator) 3251{ 3252 struct regulator_dev *rdev = regulator->rdev; 3253 3254 return rdev->desc->uV_step; 3255} 3256EXPORT_SYMBOL_GPL(regulator_get_linear_step); 3257 3258/** 3259 * regulator_is_supported_voltage - check if a voltage range can be supported 3260 * 3261 * @regulator: Regulator to check. 3262 * @min_uV: Minimum required voltage in uV. 3263 * @max_uV: Maximum required voltage in uV. 3264 * 3265 * Returns a boolean. 3266 */ 3267int regulator_is_supported_voltage(struct regulator *regulator, 3268 int min_uV, int max_uV) 3269{ 3270 struct regulator_dev *rdev = regulator->rdev; 3271 int i, voltages, ret; 3272 3273 /* If we can't change voltage check the current voltage */ 3274 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { 3275 ret = regulator_get_voltage(regulator); 3276 if (ret >= 0) 3277 return min_uV <= ret && ret <= max_uV; 3278 else 3279 return ret; 3280 } 3281 3282 /* Any voltage within constrains range is fine? */ 3283 if (rdev->desc->continuous_voltage_range) 3284 return min_uV >= rdev->constraints->min_uV && 3285 max_uV <= rdev->constraints->max_uV; 3286 3287 ret = regulator_count_voltages(regulator); 3288 if (ret < 0) 3289 return 0; 3290 voltages = ret; 3291 3292 for (i = 0; i < voltages; i++) { 3293 ret = regulator_list_voltage(regulator, i); 3294 3295 if (ret >= min_uV && ret <= max_uV) 3296 return 1; 3297 } 3298 3299 return 0; 3300} 3301EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); 3302 3303static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV, 3304 int max_uV) 3305{ 3306 const struct regulator_desc *desc = rdev->desc; 3307 3308 if (desc->ops->map_voltage) 3309 return desc->ops->map_voltage(rdev, min_uV, max_uV); 3310 3311 if (desc->ops->list_voltage == regulator_list_voltage_linear) 3312 return regulator_map_voltage_linear(rdev, min_uV, max_uV); 3313 3314 if (desc->ops->list_voltage == regulator_list_voltage_linear_range) 3315 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV); 3316 3317 if (desc->ops->list_voltage == 3318 regulator_list_voltage_pickable_linear_range) 3319 return regulator_map_voltage_pickable_linear_range(rdev, 3320 min_uV, max_uV); 3321 3322 return regulator_map_voltage_iterate(rdev, min_uV, max_uV); 3323} 3324 3325static int _regulator_call_set_voltage(struct regulator_dev *rdev, 3326 int min_uV, int max_uV, 3327 unsigned *selector) 3328{ 3329 struct pre_voltage_change_data data; 3330 int ret; 3331 3332 data.old_uV = regulator_get_voltage_rdev(rdev); 3333 data.min_uV = min_uV; 3334 data.max_uV = max_uV; 3335 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, 3336 &data); 3337 if (ret & NOTIFY_STOP_MASK) 3338 return -EINVAL; 3339 3340 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector); 3341 if (ret >= 0) 3342 return ret; 3343 3344 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, 3345 (void *)data.old_uV); 3346 3347 return ret; 3348} 3349 3350static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev, 3351 int uV, unsigned selector) 3352{ 3353 struct pre_voltage_change_data data; 3354 int ret; 3355 3356 data.old_uV = regulator_get_voltage_rdev(rdev); 3357 data.min_uV = uV; 3358 data.max_uV = uV; 3359 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, 3360 &data); 3361 if (ret & NOTIFY_STOP_MASK) 3362 return -EINVAL; 3363 3364 ret = rdev->desc->ops->set_voltage_sel(rdev, selector); 3365 if (ret >= 0) 3366 return ret; 3367 3368 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, 3369 (void *)data.old_uV); 3370 3371 return ret; 3372} 3373 3374static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev, 3375 int uV, int new_selector) 3376{ 3377 const struct regulator_ops *ops = rdev->desc->ops; 3378 int diff, old_sel, curr_sel, ret; 3379 3380 /* Stepping is only needed if the regulator is enabled. */ 3381 if (!_regulator_is_enabled(rdev)) 3382 goto final_set; 3383 3384 if (!ops->get_voltage_sel) 3385 return -EINVAL; 3386 3387 old_sel = ops->get_voltage_sel(rdev); 3388 if (old_sel < 0) 3389 return old_sel; 3390 3391 diff = new_selector - old_sel; 3392 if (diff == 0) 3393 return 0; /* No change needed. */ 3394 3395 if (diff > 0) { 3396 /* Stepping up. */ 3397 for (curr_sel = old_sel + rdev->desc->vsel_step; 3398 curr_sel < new_selector; 3399 curr_sel += rdev->desc->vsel_step) { 3400 /* 3401 * Call the callback directly instead of using 3402 * _regulator_call_set_voltage_sel() as we don't 3403 * want to notify anyone yet. Same in the branch 3404 * below. 3405 */ 3406 ret = ops->set_voltage_sel(rdev, curr_sel); 3407 if (ret) 3408 goto try_revert; 3409 } 3410 } else { 3411 /* Stepping down. */ 3412 for (curr_sel = old_sel - rdev->desc->vsel_step; 3413 curr_sel > new_selector; 3414 curr_sel -= rdev->desc->vsel_step) { 3415 ret = ops->set_voltage_sel(rdev, curr_sel); 3416 if (ret) 3417 goto try_revert; 3418 } 3419 } 3420 3421final_set: 3422 /* The final selector will trigger the notifiers. */ 3423 return _regulator_call_set_voltage_sel(rdev, uV, new_selector); 3424 3425try_revert: 3426 /* 3427 * At least try to return to the previous voltage if setting a new 3428 * one failed. 3429 */ 3430 (void)ops->set_voltage_sel(rdev, old_sel); 3431 return ret; 3432} 3433 3434static int _regulator_set_voltage_time(struct regulator_dev *rdev, 3435 int old_uV, int new_uV) 3436{ 3437 unsigned int ramp_delay = 0; 3438 3439 if (rdev->constraints->ramp_delay) 3440 ramp_delay = rdev->constraints->ramp_delay; 3441 else if (rdev->desc->ramp_delay) 3442 ramp_delay = rdev->desc->ramp_delay; 3443 else if (rdev->constraints->settling_time) 3444 return rdev->constraints->settling_time; 3445 else if (rdev->constraints->settling_time_up && 3446 (new_uV > old_uV)) 3447 return rdev->constraints->settling_time_up; 3448 else if (rdev->constraints->settling_time_down && 3449 (new_uV < old_uV)) 3450 return rdev->constraints->settling_time_down; 3451 3452 if (ramp_delay == 0) { 3453 rdev_dbg(rdev, "ramp_delay not set\n"); 3454 return 0; 3455 } 3456 3457 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); 3458} 3459 3460static int _regulator_do_set_voltage(struct regulator_dev *rdev, 3461 int min_uV, int max_uV) 3462{ 3463 int ret; 3464 int delay = 0; 3465 int best_val = 0; 3466 unsigned int selector; 3467 int old_selector = -1; 3468 const struct regulator_ops *ops = rdev->desc->ops; 3469 int old_uV = regulator_get_voltage_rdev(rdev); 3470 3471 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); 3472 3473 min_uV += rdev->constraints->uV_offset; 3474 max_uV += rdev->constraints->uV_offset; 3475 3476 /* 3477 * If we can't obtain the old selector there is not enough 3478 * info to call set_voltage_time_sel(). 3479 */ 3480 if (_regulator_is_enabled(rdev) && 3481 ops->set_voltage_time_sel && ops->get_voltage_sel) { 3482 old_selector = ops->get_voltage_sel(rdev); 3483 if (old_selector < 0) 3484 return old_selector; 3485 } 3486 3487 if (ops->set_voltage) { 3488 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV, 3489 &selector); 3490 3491 if (ret >= 0) { 3492 if (ops->list_voltage) 3493 best_val = ops->list_voltage(rdev, 3494 selector); 3495 else 3496 best_val = regulator_get_voltage_rdev(rdev); 3497 } 3498 3499 } else if (ops->set_voltage_sel) { 3500 ret = regulator_map_voltage(rdev, min_uV, max_uV); 3501 if (ret >= 0) { 3502 best_val = ops->list_voltage(rdev, ret); 3503 if (min_uV <= best_val && max_uV >= best_val) { 3504 selector = ret; 3505 if (old_selector == selector) 3506 ret = 0; 3507 else if (rdev->desc->vsel_step) 3508 ret = _regulator_set_voltage_sel_step( 3509 rdev, best_val, selector); 3510 else 3511 ret = _regulator_call_set_voltage_sel( 3512 rdev, best_val, selector); 3513 } else { 3514 ret = -EINVAL; 3515 } 3516 } 3517 } else { 3518 ret = -EINVAL; 3519 } 3520 3521 if (ret) 3522 goto out; 3523 3524 if (ops->set_voltage_time_sel) { 3525 /* 3526 * Call set_voltage_time_sel if successfully obtained 3527 * old_selector 3528 */ 3529 if (old_selector >= 0 && old_selector != selector) 3530 delay = ops->set_voltage_time_sel(rdev, old_selector, 3531 selector); 3532 } else { 3533 if (old_uV != best_val) { 3534 if (ops->set_voltage_time) 3535 delay = ops->set_voltage_time(rdev, old_uV, 3536 best_val); 3537 else 3538 delay = _regulator_set_voltage_time(rdev, 3539 old_uV, 3540 best_val); 3541 } 3542 } 3543 3544 if (delay < 0) { 3545 rdev_warn(rdev, "failed to get delay: %pe\n", ERR_PTR(delay)); 3546 delay = 0; 3547 } 3548 3549 /* Insert any necessary delays */ 3550 if (delay >= 1000) { 3551 mdelay(delay / 1000); 3552 udelay(delay % 1000); 3553 } else if (delay) { 3554 udelay(delay); 3555 } 3556 3557 if (best_val >= 0) { 3558 unsigned long data = best_val; 3559 3560 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, 3561 (void *)data); 3562 } 3563 3564out: 3565 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val); 3566 3567 return ret; 3568} 3569 3570static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev, 3571 int min_uV, int max_uV, suspend_state_t state) 3572{ 3573 struct regulator_state *rstate; 3574 int uV, sel; 3575 3576 rstate = regulator_get_suspend_state(rdev, state); 3577 if (rstate == NULL) 3578 return -EINVAL; 3579 3580 if (min_uV < rstate->min_uV) 3581 min_uV = rstate->min_uV; 3582 if (max_uV > rstate->max_uV) 3583 max_uV = rstate->max_uV; 3584 3585 sel = regulator_map_voltage(rdev, min_uV, max_uV); 3586 if (sel < 0) 3587 return sel; 3588 3589 uV = rdev->desc->ops->list_voltage(rdev, sel); 3590 if (uV >= min_uV && uV <= max_uV) 3591 rstate->uV = uV; 3592 3593 return 0; 3594} 3595 3596static int regulator_set_voltage_unlocked(struct regulator *regulator, 3597 int min_uV, int max_uV, 3598 suspend_state_t state) 3599{ 3600 struct regulator_dev *rdev = regulator->rdev; 3601 struct regulator_voltage *voltage = ®ulator->voltage[state]; 3602 int ret = 0; 3603 int old_min_uV, old_max_uV; 3604 int current_uV; 3605 3606 /* If we're setting the same range as last time the change 3607 * should be a noop (some cpufreq implementations use the same 3608 * voltage for multiple frequencies, for example). 3609 */ 3610 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV) 3611 goto out; 3612 3613 /* If we're trying to set a range that overlaps the current voltage, 3614 * return successfully even though the regulator does not support 3615 * changing the voltage. 3616 */ 3617 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { 3618 current_uV = regulator_get_voltage_rdev(rdev); 3619 if (min_uV <= current_uV && current_uV <= max_uV) { 3620 voltage->min_uV = min_uV; 3621 voltage->max_uV = max_uV; 3622 goto out; 3623 } 3624 } 3625 3626 /* sanity check */ 3627 if (!rdev->desc->ops->set_voltage && 3628 !rdev->desc->ops->set_voltage_sel) { 3629 ret = -EINVAL; 3630 goto out; 3631 } 3632 3633 /* constraints check */ 3634 ret = regulator_check_voltage(rdev, &min_uV, &max_uV); 3635 if (ret < 0) 3636 goto out; 3637 3638 /* restore original values in case of error */ 3639 old_min_uV = voltage->min_uV; 3640 old_max_uV = voltage->max_uV; 3641 voltage->min_uV = min_uV; 3642 voltage->max_uV = max_uV; 3643 3644 /* for not coupled regulators this will just set the voltage */ 3645 ret = regulator_balance_voltage(rdev, state); 3646 if (ret < 0) { 3647 voltage->min_uV = old_min_uV; 3648 voltage->max_uV = old_max_uV; 3649 } 3650 3651out: 3652 return ret; 3653} 3654 3655int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV, 3656 int max_uV, suspend_state_t state) 3657{ 3658 int best_supply_uV = 0; 3659 int supply_change_uV = 0; 3660 int ret; 3661 3662 if (rdev->supply && 3663 regulator_ops_is_valid(rdev->supply->rdev, 3664 REGULATOR_CHANGE_VOLTAGE) && 3665 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage || 3666 rdev->desc->ops->get_voltage_sel))) { 3667 int current_supply_uV; 3668 int selector; 3669 3670 selector = regulator_map_voltage(rdev, min_uV, max_uV); 3671 if (selector < 0) { 3672 ret = selector; 3673 goto out; 3674 } 3675 3676 best_supply_uV = _regulator_list_voltage(rdev, selector, 0); 3677 if (best_supply_uV < 0) { 3678 ret = best_supply_uV; 3679 goto out; 3680 } 3681 3682 best_supply_uV += rdev->desc->min_dropout_uV; 3683 3684 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev); 3685 if (current_supply_uV < 0) { 3686 ret = current_supply_uV; 3687 goto out; 3688 } 3689 3690 supply_change_uV = best_supply_uV - current_supply_uV; 3691 } 3692 3693 if (supply_change_uV > 0) { 3694 ret = regulator_set_voltage_unlocked(rdev->supply, 3695 best_supply_uV, INT_MAX, state); 3696 if (ret) { 3697 dev_err(&rdev->dev, "Failed to increase supply voltage: %pe\n", 3698 ERR_PTR(ret)); 3699 goto out; 3700 } 3701 } 3702 3703 if (state == PM_SUSPEND_ON) 3704 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); 3705 else 3706 ret = _regulator_do_set_suspend_voltage(rdev, min_uV, 3707 max_uV, state); 3708 if (ret < 0) 3709 goto out; 3710 3711 if (supply_change_uV < 0) { 3712 ret = regulator_set_voltage_unlocked(rdev->supply, 3713 best_supply_uV, INT_MAX, state); 3714 if (ret) 3715 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %pe\n", 3716 ERR_PTR(ret)); 3717 /* No need to fail here */ 3718 ret = 0; 3719 } 3720 3721out: 3722 return ret; 3723} 3724EXPORT_SYMBOL_GPL(regulator_set_voltage_rdev); 3725 3726static int regulator_limit_voltage_step(struct regulator_dev *rdev, 3727 int *current_uV, int *min_uV) 3728{ 3729 struct regulation_constraints *constraints = rdev->constraints; 3730 3731 /* Limit voltage change only if necessary */ 3732 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev)) 3733 return 1; 3734 3735 if (*current_uV < 0) { 3736 *current_uV = regulator_get_voltage_rdev(rdev); 3737 3738 if (*current_uV < 0) 3739 return *current_uV; 3740 } 3741 3742 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step) 3743 return 1; 3744 3745 /* Clamp target voltage within the given step */ 3746 if (*current_uV < *min_uV) 3747 *min_uV = min(*current_uV + constraints->max_uV_step, 3748 *min_uV); 3749 else 3750 *min_uV = max(*current_uV - constraints->max_uV_step, 3751 *min_uV); 3752 3753 return 0; 3754} 3755 3756static int regulator_get_optimal_voltage(struct regulator_dev *rdev, 3757 int *current_uV, 3758 int *min_uV, int *max_uV, 3759 suspend_state_t state, 3760 int n_coupled) 3761{ 3762 struct coupling_desc *c_desc = &rdev->coupling_desc; 3763 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs; 3764 struct regulation_constraints *constraints = rdev->constraints; 3765 int desired_min_uV = 0, desired_max_uV = INT_MAX; 3766 int max_current_uV = 0, min_current_uV = INT_MAX; 3767 int highest_min_uV = 0, target_uV, possible_uV; 3768 int i, ret, max_spread; 3769 bool done; 3770 3771 *current_uV = -1; 3772 3773 /* 3774 * If there are no coupled regulators, simply set the voltage 3775 * demanded by consumers. 3776 */ 3777 if (n_coupled == 1) { 3778 /* 3779 * If consumers don't provide any demands, set voltage 3780 * to min_uV 3781 */ 3782 desired_min_uV = constraints->min_uV; 3783 desired_max_uV = constraints->max_uV; 3784 3785 ret = regulator_check_consumers(rdev, 3786 &desired_min_uV, 3787 &desired_max_uV, state); 3788 if (ret < 0) 3789 return ret; 3790 3791 possible_uV = desired_min_uV; 3792 done = true; 3793 3794 goto finish; 3795 } 3796 3797 /* Find highest min desired voltage */ 3798 for (i = 0; i < n_coupled; i++) { 3799 int tmp_min = 0; 3800 int tmp_max = INT_MAX; 3801 3802 lockdep_assert_held_once(&c_rdevs[i]->mutex.base); 3803 3804 ret = regulator_check_consumers(c_rdevs[i], 3805 &tmp_min, 3806 &tmp_max, state); 3807 if (ret < 0) 3808 return ret; 3809 3810 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max); 3811 if (ret < 0) 3812 return ret; 3813 3814 highest_min_uV = max(highest_min_uV, tmp_min); 3815 3816 if (i == 0) { 3817 desired_min_uV = tmp_min; 3818 desired_max_uV = tmp_max; 3819 } 3820 } 3821 3822 max_spread = constraints->max_spread[0]; 3823 3824 /* 3825 * Let target_uV be equal to the desired one if possible. 3826 * If not, set it to minimum voltage, allowed by other coupled 3827 * regulators. 3828 */ 3829 target_uV = max(desired_min_uV, highest_min_uV - max_spread); 3830 3831 /* 3832 * Find min and max voltages, which currently aren't violating 3833 * max_spread. 3834 */ 3835 for (i = 1; i < n_coupled; i++) { 3836 int tmp_act; 3837 3838 if (!_regulator_is_enabled(c_rdevs[i])) 3839 continue; 3840 3841 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]); 3842 if (tmp_act < 0) 3843 return tmp_act; 3844 3845 min_current_uV = min(tmp_act, min_current_uV); 3846 max_current_uV = max(tmp_act, max_current_uV); 3847 } 3848 3849 /* There aren't any other regulators enabled */ 3850 if (max_current_uV == 0) { 3851 possible_uV = target_uV; 3852 } else { 3853 /* 3854 * Correct target voltage, so as it currently isn't 3855 * violating max_spread 3856 */ 3857 possible_uV = max(target_uV, max_current_uV - max_spread); 3858 possible_uV = min(possible_uV, min_current_uV + max_spread); 3859 } 3860 3861 if (possible_uV > desired_max_uV) 3862 return -EINVAL; 3863 3864 done = (possible_uV == target_uV); 3865 desired_min_uV = possible_uV; 3866 3867finish: 3868 /* Apply max_uV_step constraint if necessary */ 3869 if (state == PM_SUSPEND_ON) { 3870 ret = regulator_limit_voltage_step(rdev, current_uV, 3871 &desired_min_uV); 3872 if (ret < 0) 3873 return ret; 3874 3875 if (ret == 0) 3876 done = false; 3877 } 3878 3879 /* Set current_uV if wasn't done earlier in the code and if necessary */ 3880 if (n_coupled > 1 && *current_uV == -1) { 3881 3882 if (_regulator_is_enabled(rdev)) { 3883 ret = regulator_get_voltage_rdev(rdev); 3884 if (ret < 0) 3885 return ret; 3886 3887 *current_uV = ret; 3888 } else { 3889 *current_uV = desired_min_uV; 3890 } 3891 } 3892 3893 *min_uV = desired_min_uV; 3894 *max_uV = desired_max_uV; 3895 3896 return done; 3897} 3898 3899int regulator_do_balance_voltage(struct regulator_dev *rdev, 3900 suspend_state_t state, bool skip_coupled) 3901{ 3902 struct regulator_dev **c_rdevs; 3903 struct regulator_dev *best_rdev; 3904 struct coupling_desc *c_desc = &rdev->coupling_desc; 3905 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev; 3906 unsigned int delta, best_delta; 3907 unsigned long c_rdev_done = 0; 3908 bool best_c_rdev_done; 3909 3910 c_rdevs = c_desc->coupled_rdevs; 3911 n_coupled = skip_coupled ? 1 : c_desc->n_coupled; 3912 3913 /* 3914 * Find the best possible voltage change on each loop. Leave the loop 3915 * if there isn't any possible change. 3916 */ 3917 do { 3918 best_c_rdev_done = false; 3919 best_delta = 0; 3920 best_min_uV = 0; 3921 best_max_uV = 0; 3922 best_c_rdev = 0; 3923 best_rdev = NULL; 3924 3925 /* 3926 * Find highest difference between optimal voltage 3927 * and current voltage. 3928 */ 3929 for (i = 0; i < n_coupled; i++) { 3930 /* 3931 * optimal_uV is the best voltage that can be set for 3932 * i-th regulator at the moment without violating 3933 * max_spread constraint in order to balance 3934 * the coupled voltages. 3935 */ 3936 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0; 3937 3938 if (test_bit(i, &c_rdev_done)) 3939 continue; 3940 3941 ret = regulator_get_optimal_voltage(c_rdevs[i], 3942 ¤t_uV, 3943 &optimal_uV, 3944 &optimal_max_uV, 3945 state, n_coupled); 3946 if (ret < 0) 3947 goto out; 3948 3949 delta = abs(optimal_uV - current_uV); 3950 3951 if (delta && best_delta <= delta) { 3952 best_c_rdev_done = ret; 3953 best_delta = delta; 3954 best_rdev = c_rdevs[i]; 3955 best_min_uV = optimal_uV; 3956 best_max_uV = optimal_max_uV; 3957 best_c_rdev = i; 3958 } 3959 } 3960 3961 /* Nothing to change, return successfully */ 3962 if (!best_rdev) { 3963 ret = 0; 3964 goto out; 3965 } 3966 3967 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV, 3968 best_max_uV, state); 3969 3970 if (ret < 0) 3971 goto out; 3972 3973 if (best_c_rdev_done) 3974 set_bit(best_c_rdev, &c_rdev_done); 3975 3976 } while (n_coupled > 1); 3977 3978out: 3979 return ret; 3980} 3981 3982static int regulator_balance_voltage(struct regulator_dev *rdev, 3983 suspend_state_t state) 3984{ 3985 struct coupling_desc *c_desc = &rdev->coupling_desc; 3986 struct regulator_coupler *coupler = c_desc->coupler; 3987 bool skip_coupled = false; 3988 3989 /* 3990 * If system is in a state other than PM_SUSPEND_ON, don't check 3991 * other coupled regulators. 3992 */ 3993 if (state != PM_SUSPEND_ON) 3994 skip_coupled = true; 3995 3996 if (c_desc->n_resolved < c_desc->n_coupled) { 3997 rdev_err(rdev, "Not all coupled regulators registered\n"); 3998 return -EPERM; 3999 } 4000 4001 /* Invoke custom balancer for customized couplers */ 4002 if (coupler && coupler->balance_voltage) 4003 return coupler->balance_voltage(coupler, rdev, state); 4004 4005 return regulator_do_balance_voltage(rdev, state, skip_coupled); 4006} 4007 4008/** 4009 * regulator_set_voltage - set regulator output voltage 4010 * @regulator: regulator source 4011 * @min_uV: Minimum required voltage in uV 4012 * @max_uV: Maximum acceptable voltage in uV 4013 * 4014 * Sets a voltage regulator to the desired output voltage. This can be set 4015 * during any regulator state. IOW, regulator can be disabled or enabled. 4016 * 4017 * If the regulator is enabled then the voltage will change to the new value 4018 * immediately otherwise if the regulator is disabled the regulator will 4019 * output at the new voltage when enabled. 4020 * 4021 * NOTE: If the regulator is shared between several devices then the lowest 4022 * request voltage that meets the system constraints will be used. 4023 * Regulator system constraints must be set for this regulator before 4024 * calling this function otherwise this call will fail. 4025 */ 4026int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) 4027{ 4028 struct ww_acquire_ctx ww_ctx; 4029 int ret; 4030 4031 regulator_lock_dependent(regulator->rdev, &ww_ctx); 4032 4033 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV, 4034 PM_SUSPEND_ON); 4035 4036 regulator_unlock_dependent(regulator->rdev, &ww_ctx); 4037 4038 return ret; 4039} 4040EXPORT_SYMBOL_GPL(regulator_set_voltage); 4041 4042static inline int regulator_suspend_toggle(struct regulator_dev *rdev, 4043 suspend_state_t state, bool en) 4044{ 4045 struct regulator_state *rstate; 4046 4047 rstate = regulator_get_suspend_state(rdev, state); 4048 if (rstate == NULL) 4049 return -EINVAL; 4050 4051 if (!rstate->changeable) 4052 return -EPERM; 4053 4054 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND; 4055 4056 return 0; 4057} 4058 4059int regulator_suspend_enable(struct regulator_dev *rdev, 4060 suspend_state_t state) 4061{ 4062 return regulator_suspend_toggle(rdev, state, true); 4063} 4064EXPORT_SYMBOL_GPL(regulator_suspend_enable); 4065 4066int regulator_suspend_disable(struct regulator_dev *rdev, 4067 suspend_state_t state) 4068{ 4069 struct regulator *regulator; 4070 struct regulator_voltage *voltage; 4071 4072 /* 4073 * if any consumer wants this regulator device keeping on in 4074 * suspend states, don't set it as disabled. 4075 */ 4076 list_for_each_entry(regulator, &rdev->consumer_list, list) { 4077 voltage = ®ulator->voltage[state]; 4078 if (voltage->min_uV || voltage->max_uV) 4079 return 0; 4080 } 4081 4082 return regulator_suspend_toggle(rdev, state, false); 4083} 4084EXPORT_SYMBOL_GPL(regulator_suspend_disable); 4085 4086static int _regulator_set_suspend_voltage(struct regulator *regulator, 4087 int min_uV, int max_uV, 4088 suspend_state_t state) 4089{ 4090 struct regulator_dev *rdev = regulator->rdev; 4091 struct regulator_state *rstate; 4092 4093 rstate = regulator_get_suspend_state(rdev, state); 4094 if (rstate == NULL) 4095 return -EINVAL; 4096 4097 if (rstate->min_uV == rstate->max_uV) { 4098 rdev_err(rdev, "The suspend voltage can't be changed!\n"); 4099 return -EPERM; 4100 } 4101 4102 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state); 4103} 4104 4105int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, 4106 int max_uV, suspend_state_t state) 4107{ 4108 struct ww_acquire_ctx ww_ctx; 4109 int ret; 4110 4111 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */ 4112 if (regulator_check_states(state) || state == PM_SUSPEND_ON) 4113 return -EINVAL; 4114 4115 regulator_lock_dependent(regulator->rdev, &ww_ctx); 4116 4117 ret = _regulator_set_suspend_voltage(regulator, min_uV, 4118 max_uV, state); 4119 4120 regulator_unlock_dependent(regulator->rdev, &ww_ctx); 4121 4122 return ret; 4123} 4124EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage); 4125 4126/** 4127 * regulator_set_voltage_time - get raise/fall time 4128 * @regulator: regulator source 4129 * @old_uV: starting voltage in microvolts 4130 * @new_uV: target voltage in microvolts 4131 * 4132 * Provided with the starting and ending voltage, this function attempts to 4133 * calculate the time in microseconds required to rise or fall to this new 4134 * voltage. 4135 */ 4136int regulator_set_voltage_time(struct regulator *regulator, 4137 int old_uV, int new_uV) 4138{ 4139 struct regulator_dev *rdev = regulator->rdev; 4140 const struct regulator_ops *ops = rdev->desc->ops; 4141 int old_sel = -1; 4142 int new_sel = -1; 4143 int voltage; 4144 int i; 4145 4146 if (ops->set_voltage_time) 4147 return ops->set_voltage_time(rdev, old_uV, new_uV); 4148 else if (!ops->set_voltage_time_sel) 4149 return _regulator_set_voltage_time(rdev, old_uV, new_uV); 4150 4151 /* Currently requires operations to do this */ 4152 if (!ops->list_voltage || !rdev->desc->n_voltages) 4153 return -EINVAL; 4154 4155 for (i = 0; i < rdev->desc->n_voltages; i++) { 4156 /* We only look for exact voltage matches here */ 4157 4158 if (old_sel >= 0 && new_sel >= 0) 4159 break; 4160 4161 voltage = regulator_list_voltage(regulator, i); 4162 if (voltage < 0) 4163 return -EINVAL; 4164 if (voltage == 0) 4165 continue; 4166 if (voltage == old_uV) 4167 old_sel = i; 4168 if (voltage == new_uV) 4169 new_sel = i; 4170 } 4171 4172 if (old_sel < 0 || new_sel < 0) 4173 return -EINVAL; 4174 4175 return ops->set_voltage_time_sel(rdev, old_sel, new_sel); 4176} 4177EXPORT_SYMBOL_GPL(regulator_set_voltage_time); 4178 4179/** 4180 * regulator_set_voltage_time_sel - get raise/fall time 4181 * @rdev: regulator source device 4182 * @old_selector: selector for starting voltage 4183 * @new_selector: selector for target voltage 4184 * 4185 * Provided with the starting and target voltage selectors, this function 4186 * returns time in microseconds required to rise or fall to this new voltage 4187 * 4188 * Drivers providing ramp_delay in regulation_constraints can use this as their 4189 * set_voltage_time_sel() operation. 4190 */ 4191int regulator_set_voltage_time_sel(struct regulator_dev *rdev, 4192 unsigned int old_selector, 4193 unsigned int new_selector) 4194{ 4195 int old_volt, new_volt; 4196 4197 /* sanity check */ 4198 if (!rdev->desc->ops->list_voltage) 4199 return -EINVAL; 4200 4201 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); 4202 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); 4203 4204 if (rdev->desc->ops->set_voltage_time) 4205 return rdev->desc->ops->set_voltage_time(rdev, old_volt, 4206 new_volt); 4207 else 4208 return _regulator_set_voltage_time(rdev, old_volt, new_volt); 4209} 4210EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel); 4211 4212/** 4213 * regulator_sync_voltage - re-apply last regulator output voltage 4214 * @regulator: regulator source 4215 * 4216 * Re-apply the last configured voltage. This is intended to be used 4217 * where some external control source the consumer is cooperating with 4218 * has caused the configured voltage to change. 4219 */ 4220int regulator_sync_voltage(struct regulator *regulator) 4221{ 4222 struct regulator_dev *rdev = regulator->rdev; 4223 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON]; 4224 int ret, min_uV, max_uV; 4225 4226 regulator_lock(rdev); 4227 4228 if (!rdev->desc->ops->set_voltage && 4229 !rdev->desc->ops->set_voltage_sel) { 4230 ret = -EINVAL; 4231 goto out; 4232 } 4233 4234 /* This is only going to work if we've had a voltage configured. */ 4235 if (!voltage->min_uV && !voltage->max_uV) { 4236 ret = -EINVAL; 4237 goto out; 4238 } 4239 4240 min_uV = voltage->min_uV; 4241 max_uV = voltage->max_uV; 4242 4243 /* This should be a paranoia check... */ 4244 ret = regulator_check_voltage(rdev, &min_uV, &max_uV); 4245 if (ret < 0) 4246 goto out; 4247 4248 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0); 4249 if (ret < 0) 4250 goto out; 4251 4252 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); 4253 4254out: 4255 regulator_unlock(rdev); 4256 return ret; 4257} 4258EXPORT_SYMBOL_GPL(regulator_sync_voltage); 4259 4260int regulator_get_voltage_rdev(struct regulator_dev *rdev) 4261{ 4262 int sel, ret; 4263 bool bypassed; 4264 4265 if (rdev->desc->ops->get_bypass) { 4266 ret = rdev->desc->ops->get_bypass(rdev, &bypassed); 4267 if (ret < 0) 4268 return ret; 4269 if (bypassed) { 4270 /* if bypassed the regulator must have a supply */ 4271 if (!rdev->supply) { 4272 rdev_err(rdev, 4273 "bypassed regulator has no supply!\n"); 4274 return -EPROBE_DEFER; 4275 } 4276 4277 return regulator_get_voltage_rdev(rdev->supply->rdev); 4278 } 4279 } 4280 4281 if (rdev->desc->ops->get_voltage_sel) { 4282 sel = rdev->desc->ops->get_voltage_sel(rdev); 4283 if (sel < 0) 4284 return sel; 4285 ret = rdev->desc->ops->list_voltage(rdev, sel); 4286 } else if (rdev->desc->ops->get_voltage) { 4287 ret = rdev->desc->ops->get_voltage(rdev); 4288 } else if (rdev->desc->ops->list_voltage) { 4289 ret = rdev->desc->ops->list_voltage(rdev, 0); 4290 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { 4291 ret = rdev->desc->fixed_uV; 4292 } else if (rdev->supply) { 4293 ret = regulator_get_voltage_rdev(rdev->supply->rdev); 4294 } else if (rdev->supply_name) { 4295 return -EPROBE_DEFER; 4296 } else { 4297 return -EINVAL; 4298 } 4299 4300 if (ret < 0) 4301 return ret; 4302 return ret - rdev->constraints->uV_offset; 4303} 4304EXPORT_SYMBOL_GPL(regulator_get_voltage_rdev); 4305 4306/** 4307 * regulator_get_voltage - get regulator output voltage 4308 * @regulator: regulator source 4309 * 4310 * This returns the current regulator voltage in uV. 4311 * 4312 * NOTE: If the regulator is disabled it will return the voltage value. This 4313 * function should not be used to determine regulator state. 4314 */ 4315int regulator_get_voltage(struct regulator *regulator) 4316{ 4317 struct ww_acquire_ctx ww_ctx; 4318 int ret; 4319 4320 regulator_lock_dependent(regulator->rdev, &ww_ctx); 4321 ret = regulator_get_voltage_rdev(regulator->rdev); 4322 regulator_unlock_dependent(regulator->rdev, &ww_ctx); 4323 4324 return ret; 4325} 4326EXPORT_SYMBOL_GPL(regulator_get_voltage); 4327 4328/** 4329 * regulator_set_current_limit - set regulator output current limit 4330 * @regulator: regulator source 4331 * @min_uA: Minimum supported current in uA 4332 * @max_uA: Maximum supported current in uA 4333 * 4334 * Sets current sink to the desired output current. This can be set during 4335 * any regulator state. IOW, regulator can be disabled or enabled. 4336 * 4337 * If the regulator is enabled then the current will change to the new value 4338 * immediately otherwise if the regulator is disabled the regulator will 4339 * output at the new current when enabled. 4340 * 4341 * NOTE: Regulator system constraints must be set for this regulator before 4342 * calling this function otherwise this call will fail. 4343 */ 4344int regulator_set_current_limit(struct regulator *regulator, 4345 int min_uA, int max_uA) 4346{ 4347 struct regulator_dev *rdev = regulator->rdev; 4348 int ret; 4349 4350 regulator_lock(rdev); 4351 4352 /* sanity check */ 4353 if (!rdev->desc->ops->set_current_limit) { 4354 ret = -EINVAL; 4355 goto out; 4356 } 4357 4358 /* constraints check */ 4359 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); 4360 if (ret < 0) 4361 goto out; 4362 4363 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); 4364out: 4365 regulator_unlock(rdev); 4366 return ret; 4367} 4368EXPORT_SYMBOL_GPL(regulator_set_current_limit); 4369 4370static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev) 4371{ 4372 /* sanity check */ 4373 if (!rdev->desc->ops->get_current_limit) 4374 return -EINVAL; 4375 4376 return rdev->desc->ops->get_current_limit(rdev); 4377} 4378 4379static int _regulator_get_current_limit(struct regulator_dev *rdev) 4380{ 4381 int ret; 4382 4383 regulator_lock(rdev); 4384 ret = _regulator_get_current_limit_unlocked(rdev); 4385 regulator_unlock(rdev); 4386 4387 return ret; 4388} 4389 4390/** 4391 * regulator_get_current_limit - get regulator output current 4392 * @regulator: regulator source 4393 * 4394 * This returns the current supplied by the specified current sink in uA. 4395 * 4396 * NOTE: If the regulator is disabled it will return the current value. This 4397 * function should not be used to determine regulator state. 4398 */ 4399int regulator_get_current_limit(struct regulator *regulator) 4400{ 4401 return _regulator_get_current_limit(regulator->rdev); 4402} 4403EXPORT_SYMBOL_GPL(regulator_get_current_limit); 4404 4405/** 4406 * regulator_set_mode - set regulator operating mode 4407 * @regulator: regulator source 4408 * @mode: operating mode - one of the REGULATOR_MODE constants 4409 * 4410 * Set regulator operating mode to increase regulator efficiency or improve 4411 * regulation performance. 4412 * 4413 * NOTE: Regulator system constraints must be set for this regulator before 4414 * calling this function otherwise this call will fail. 4415 */ 4416int regulator_set_mode(struct regulator *regulator, unsigned int mode) 4417{ 4418 struct regulator_dev *rdev = regulator->rdev; 4419 int ret; 4420 int regulator_curr_mode; 4421 4422 regulator_lock(rdev); 4423 4424 /* sanity check */ 4425 if (!rdev->desc->ops->set_mode) { 4426 ret = -EINVAL; 4427 goto out; 4428 } 4429 4430 /* return if the same mode is requested */ 4431 if (rdev->desc->ops->get_mode) { 4432 regulator_curr_mode = rdev->desc->ops->get_mode(rdev); 4433 if (regulator_curr_mode == mode) { 4434 ret = 0; 4435 goto out; 4436 } 4437 } 4438 4439 /* constraints check */ 4440 ret = regulator_mode_constrain(rdev, &mode); 4441 if (ret < 0) 4442 goto out; 4443 4444 ret = rdev->desc->ops->set_mode(rdev, mode); 4445out: 4446 regulator_unlock(rdev); 4447 return ret; 4448} 4449EXPORT_SYMBOL_GPL(regulator_set_mode); 4450 4451static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev) 4452{ 4453 /* sanity check */ 4454 if (!rdev->desc->ops->get_mode) 4455 return -EINVAL; 4456 4457 return rdev->desc->ops->get_mode(rdev); 4458} 4459 4460static unsigned int _regulator_get_mode(struct regulator_dev *rdev) 4461{ 4462 int ret; 4463 4464 regulator_lock(rdev); 4465 ret = _regulator_get_mode_unlocked(rdev); 4466 regulator_unlock(rdev); 4467 4468 return ret; 4469} 4470 4471/** 4472 * regulator_get_mode - get regulator operating mode 4473 * @regulator: regulator source 4474 * 4475 * Get the current regulator operating mode. 4476 */ 4477unsigned int regulator_get_mode(struct regulator *regulator) 4478{ 4479 return _regulator_get_mode(regulator->rdev); 4480} 4481EXPORT_SYMBOL_GPL(regulator_get_mode); 4482 4483static int _regulator_get_error_flags(struct regulator_dev *rdev, 4484 unsigned int *flags) 4485{ 4486 int ret; 4487 4488 regulator_lock(rdev); 4489 4490 /* sanity check */ 4491 if (!rdev->desc->ops->get_error_flags) { 4492 ret = -EINVAL; 4493 goto out; 4494 } 4495 4496 ret = rdev->desc->ops->get_error_flags(rdev, flags); 4497out: 4498 regulator_unlock(rdev); 4499 return ret; 4500} 4501 4502/** 4503 * regulator_get_error_flags - get regulator error information 4504 * @regulator: regulator source 4505 * @flags: pointer to store error flags 4506 * 4507 * Get the current regulator error information. 4508 */ 4509int regulator_get_error_flags(struct regulator *regulator, 4510 unsigned int *flags) 4511{ 4512 return _regulator_get_error_flags(regulator->rdev, flags); 4513} 4514EXPORT_SYMBOL_GPL(regulator_get_error_flags); 4515 4516/** 4517 * regulator_set_load - set regulator load 4518 * @regulator: regulator source 4519 * @uA_load: load current 4520 * 4521 * Notifies the regulator core of a new device load. This is then used by 4522 * DRMS (if enabled by constraints) to set the most efficient regulator 4523 * operating mode for the new regulator loading. 4524 * 4525 * Consumer devices notify their supply regulator of the maximum power 4526 * they will require (can be taken from device datasheet in the power 4527 * consumption tables) when they change operational status and hence power 4528 * state. Examples of operational state changes that can affect power 4529 * consumption are :- 4530 * 4531 * o Device is opened / closed. 4532 * o Device I/O is about to begin or has just finished. 4533 * o Device is idling in between work. 4534 * 4535 * This information is also exported via sysfs to userspace. 4536 * 4537 * DRMS will sum the total requested load on the regulator and change 4538 * to the most efficient operating mode if platform constraints allow. 4539 * 4540 * NOTE: when a regulator consumer requests to have a regulator 4541 * disabled then any load that consumer requested no longer counts 4542 * toward the total requested load. If the regulator is re-enabled 4543 * then the previously requested load will start counting again. 4544 * 4545 * If a regulator is an always-on regulator then an individual consumer's 4546 * load will still be removed if that consumer is fully disabled. 4547 * 4548 * On error a negative errno is returned. 4549 */ 4550int regulator_set_load(struct regulator *regulator, int uA_load) 4551{ 4552 struct regulator_dev *rdev = regulator->rdev; 4553 int old_uA_load; 4554 int ret = 0; 4555 4556 regulator_lock(rdev); 4557 old_uA_load = regulator->uA_load; 4558 regulator->uA_load = uA_load; 4559 if (regulator->enable_count && old_uA_load != uA_load) { 4560 ret = drms_uA_update(rdev); 4561 if (ret < 0) 4562 regulator->uA_load = old_uA_load; 4563 } 4564 regulator_unlock(rdev); 4565 4566 return ret; 4567} 4568EXPORT_SYMBOL_GPL(regulator_set_load); 4569 4570/** 4571 * regulator_allow_bypass - allow the regulator to go into bypass mode 4572 * 4573 * @regulator: Regulator to configure 4574 * @enable: enable or disable bypass mode 4575 * 4576 * Allow the regulator to go into bypass mode if all other consumers 4577 * for the regulator also enable bypass mode and the machine 4578 * constraints allow this. Bypass mode means that the regulator is 4579 * simply passing the input directly to the output with no regulation. 4580 */ 4581int regulator_allow_bypass(struct regulator *regulator, bool enable) 4582{ 4583 struct regulator_dev *rdev = regulator->rdev; 4584 const char *name = rdev_get_name(rdev); 4585 int ret = 0; 4586 4587 if (!rdev->desc->ops->set_bypass) 4588 return 0; 4589 4590 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS)) 4591 return 0; 4592 4593 regulator_lock(rdev); 4594 4595 if (enable && !regulator->bypass) { 4596 rdev->bypass_count++; 4597 4598 if (rdev->bypass_count == rdev->open_count) { 4599 trace_regulator_bypass_enable(name); 4600 4601 ret = rdev->desc->ops->set_bypass(rdev, enable); 4602 if (ret != 0) 4603 rdev->bypass_count--; 4604 else 4605 trace_regulator_bypass_enable_complete(name); 4606 } 4607 4608 } else if (!enable && regulator->bypass) { 4609 rdev->bypass_count--; 4610 4611 if (rdev->bypass_count != rdev->open_count) { 4612 trace_regulator_bypass_disable(name); 4613 4614 ret = rdev->desc->ops->set_bypass(rdev, enable); 4615 if (ret != 0) 4616 rdev->bypass_count++; 4617 else 4618 trace_regulator_bypass_disable_complete(name); 4619 } 4620 } 4621 4622 if (ret == 0) 4623 regulator->bypass = enable; 4624 4625 regulator_unlock(rdev); 4626 4627 return ret; 4628} 4629EXPORT_SYMBOL_GPL(regulator_allow_bypass); 4630 4631/** 4632 * regulator_register_notifier - register regulator event notifier 4633 * @regulator: regulator source 4634 * @nb: notifier block 4635 * 4636 * Register notifier block to receive regulator events. 4637 */ 4638int regulator_register_notifier(struct regulator *regulator, 4639 struct notifier_block *nb) 4640{ 4641 return blocking_notifier_chain_register(®ulator->rdev->notifier, 4642 nb); 4643} 4644EXPORT_SYMBOL_GPL(regulator_register_notifier); 4645 4646/** 4647 * regulator_unregister_notifier - unregister regulator event notifier 4648 * @regulator: regulator source 4649 * @nb: notifier block 4650 * 4651 * Unregister regulator event notifier block. 4652 */ 4653int regulator_unregister_notifier(struct regulator *regulator, 4654 struct notifier_block *nb) 4655{ 4656 return blocking_notifier_chain_unregister(®ulator->rdev->notifier, 4657 nb); 4658} 4659EXPORT_SYMBOL_GPL(regulator_unregister_notifier); 4660 4661/* notify regulator consumers and downstream regulator consumers. 4662 * Note mutex must be held by caller. 4663 */ 4664static int _notifier_call_chain(struct regulator_dev *rdev, 4665 unsigned long event, void *data) 4666{ 4667 /* call rdev chain first */ 4668 return blocking_notifier_call_chain(&rdev->notifier, event, data); 4669} 4670 4671/** 4672 * regulator_bulk_get - get multiple regulator consumers 4673 * 4674 * @dev: Device to supply 4675 * @num_consumers: Number of consumers to register 4676 * @consumers: Configuration of consumers; clients are stored here. 4677 * 4678 * @return 0 on success, an errno on failure. 4679 * 4680 * This helper function allows drivers to get several regulator 4681 * consumers in one operation. If any of the regulators cannot be 4682 * acquired then any regulators that were allocated will be freed 4683 * before returning to the caller. 4684 */ 4685int regulator_bulk_get(struct device *dev, int num_consumers, 4686 struct regulator_bulk_data *consumers) 4687{ 4688 int i; 4689 int ret; 4690 4691 for (i = 0; i < num_consumers; i++) 4692 consumers[i].consumer = NULL; 4693 4694 for (i = 0; i < num_consumers; i++) { 4695 consumers[i].consumer = regulator_get(dev, 4696 consumers[i].supply); 4697 if (IS_ERR(consumers[i].consumer)) { 4698 ret = PTR_ERR(consumers[i].consumer); 4699 consumers[i].consumer = NULL; 4700 goto err; 4701 } 4702 } 4703 4704 return 0; 4705 4706err: 4707 if (ret != -EPROBE_DEFER) 4708 dev_err(dev, "Failed to get supply '%s': %pe\n", 4709 consumers[i].supply, ERR_PTR(ret)); 4710 else 4711 dev_dbg(dev, "Failed to get supply '%s', deferring\n", 4712 consumers[i].supply); 4713 4714 while (--i >= 0) 4715 regulator_put(consumers[i].consumer); 4716 4717 return ret; 4718} 4719EXPORT_SYMBOL_GPL(regulator_bulk_get); 4720 4721static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) 4722{ 4723 struct regulator_bulk_data *bulk = data; 4724 4725 bulk->ret = regulator_enable(bulk->consumer); 4726} 4727 4728/** 4729 * regulator_bulk_enable - enable multiple regulator consumers 4730 * 4731 * @num_consumers: Number of consumers 4732 * @consumers: Consumer data; clients are stored here. 4733 * @return 0 on success, an errno on failure 4734 * 4735 * This convenience API allows consumers to enable multiple regulator 4736 * clients in a single API call. If any consumers cannot be enabled 4737 * then any others that were enabled will be disabled again prior to 4738 * return. 4739 */ 4740int regulator_bulk_enable(int num_consumers, 4741 struct regulator_bulk_data *consumers) 4742{ 4743 ASYNC_DOMAIN_EXCLUSIVE(async_domain); 4744 int i; 4745 int ret = 0; 4746 4747 for (i = 0; i < num_consumers; i++) { 4748 async_schedule_domain(regulator_bulk_enable_async, 4749 &consumers[i], &async_domain); 4750 } 4751 4752 async_synchronize_full_domain(&async_domain); 4753 4754 /* If any consumer failed we need to unwind any that succeeded */ 4755 for (i = 0; i < num_consumers; i++) { 4756 if (consumers[i].ret != 0) { 4757 ret = consumers[i].ret; 4758 goto err; 4759 } 4760 } 4761 4762 return 0; 4763 4764err: 4765 for (i = 0; i < num_consumers; i++) { 4766 if (consumers[i].ret < 0) 4767 pr_err("Failed to enable %s: %pe\n", consumers[i].supply, 4768 ERR_PTR(consumers[i].ret)); 4769 else 4770 regulator_disable(consumers[i].consumer); 4771 } 4772 4773 return ret; 4774} 4775EXPORT_SYMBOL_GPL(regulator_bulk_enable); 4776 4777/** 4778 * regulator_bulk_disable - disable multiple regulator consumers 4779 * 4780 * @num_consumers: Number of consumers 4781 * @consumers: Consumer data; clients are stored here. 4782 * @return 0 on success, an errno on failure 4783 * 4784 * This convenience API allows consumers to disable multiple regulator 4785 * clients in a single API call. If any consumers cannot be disabled 4786 * then any others that were disabled will be enabled again prior to 4787 * return. 4788 */ 4789int regulator_bulk_disable(int num_consumers, 4790 struct regulator_bulk_data *consumers) 4791{ 4792 int i; 4793 int ret, r; 4794 4795 for (i = num_consumers - 1; i >= 0; --i) { 4796 ret = regulator_disable(consumers[i].consumer); 4797 if (ret != 0) 4798 goto err; 4799 } 4800 4801 return 0; 4802 4803err: 4804 pr_err("Failed to disable %s: %pe\n", consumers[i].supply, ERR_PTR(ret)); 4805 for (++i; i < num_consumers; ++i) { 4806 r = regulator_enable(consumers[i].consumer); 4807 if (r != 0) 4808 pr_err("Failed to re-enable %s: %pe\n", 4809 consumers[i].supply, ERR_PTR(r)); 4810 } 4811 4812 return ret; 4813} 4814EXPORT_SYMBOL_GPL(regulator_bulk_disable); 4815 4816/** 4817 * regulator_bulk_force_disable - force disable multiple regulator consumers 4818 * 4819 * @num_consumers: Number of consumers 4820 * @consumers: Consumer data; clients are stored here. 4821 * @return 0 on success, an errno on failure 4822 * 4823 * This convenience API allows consumers to forcibly disable multiple regulator 4824 * clients in a single API call. 4825 * NOTE: This should be used for situations when device damage will 4826 * likely occur if the regulators are not disabled (e.g. over temp). 4827 * Although regulator_force_disable function call for some consumers can 4828 * return error numbers, the function is called for all consumers. 4829 */ 4830int regulator_bulk_force_disable(int num_consumers, 4831 struct regulator_bulk_data *consumers) 4832{ 4833 int i; 4834 int ret = 0; 4835 4836 for (i = 0; i < num_consumers; i++) { 4837 consumers[i].ret = 4838 regulator_force_disable(consumers[i].consumer); 4839 4840 /* Store first error for reporting */ 4841 if (consumers[i].ret && !ret) 4842 ret = consumers[i].ret; 4843 } 4844 4845 return ret; 4846} 4847EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); 4848 4849/** 4850 * regulator_bulk_free - free multiple regulator consumers 4851 * 4852 * @num_consumers: Number of consumers 4853 * @consumers: Consumer data; clients are stored here. 4854 * 4855 * This convenience API allows consumers to free multiple regulator 4856 * clients in a single API call. 4857 */ 4858void regulator_bulk_free(int num_consumers, 4859 struct regulator_bulk_data *consumers) 4860{ 4861 int i; 4862 4863 for (i = 0; i < num_consumers; i++) { 4864 regulator_put(consumers[i].consumer); 4865 consumers[i].consumer = NULL; 4866 } 4867} 4868EXPORT_SYMBOL_GPL(regulator_bulk_free); 4869 4870/** 4871 * regulator_notifier_call_chain - call regulator event notifier 4872 * @rdev: regulator source 4873 * @event: notifier block 4874 * @data: callback-specific data. 4875 * 4876 * Called by regulator drivers to notify clients a regulator event has 4877 * occurred. 4878 */ 4879int regulator_notifier_call_chain(struct regulator_dev *rdev, 4880 unsigned long event, void *data) 4881{ 4882 _notifier_call_chain(rdev, event, data); 4883 return NOTIFY_DONE; 4884 4885} 4886EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); 4887 4888/** 4889 * regulator_mode_to_status - convert a regulator mode into a status 4890 * 4891 * @mode: Mode to convert 4892 * 4893 * Convert a regulator mode into a status. 4894 */ 4895int regulator_mode_to_status(unsigned int mode) 4896{ 4897 switch (mode) { 4898 case REGULATOR_MODE_FAST: 4899 return REGULATOR_STATUS_FAST; 4900 case REGULATOR_MODE_NORMAL: 4901 return REGULATOR_STATUS_NORMAL; 4902 case REGULATOR_MODE_IDLE: 4903 return REGULATOR_STATUS_IDLE; 4904 case REGULATOR_MODE_STANDBY: 4905 return REGULATOR_STATUS_STANDBY; 4906 default: 4907 return REGULATOR_STATUS_UNDEFINED; 4908 } 4909} 4910EXPORT_SYMBOL_GPL(regulator_mode_to_status); 4911 4912static struct attribute *regulator_dev_attrs[] = { 4913 &dev_attr_name.attr, 4914 &dev_attr_num_users.attr, 4915 &dev_attr_type.attr, 4916 &dev_attr_microvolts.attr, 4917 &dev_attr_microamps.attr, 4918 &dev_attr_opmode.attr, 4919 &dev_attr_state.attr, 4920 &dev_attr_status.attr, 4921 &dev_attr_bypass.attr, 4922 &dev_attr_requested_microamps.attr, 4923 &dev_attr_min_microvolts.attr, 4924 &dev_attr_max_microvolts.attr, 4925 &dev_attr_min_microamps.attr, 4926 &dev_attr_max_microamps.attr, 4927 &dev_attr_suspend_standby_state.attr, 4928 &dev_attr_suspend_mem_state.attr, 4929 &dev_attr_suspend_disk_state.attr, 4930 &dev_attr_suspend_standby_microvolts.attr, 4931 &dev_attr_suspend_mem_microvolts.attr, 4932 &dev_attr_suspend_disk_microvolts.attr, 4933 &dev_attr_suspend_standby_mode.attr, 4934 &dev_attr_suspend_mem_mode.attr, 4935 &dev_attr_suspend_disk_mode.attr, 4936 NULL 4937}; 4938 4939/* 4940 * To avoid cluttering sysfs (and memory) with useless state, only 4941 * create attributes that can be meaningfully displayed. 4942 */ 4943static umode_t regulator_attr_is_visible(struct kobject *kobj, 4944 struct attribute *attr, int idx) 4945{ 4946 struct device *dev = kobj_to_dev(kobj); 4947 struct regulator_dev *rdev = dev_to_rdev(dev); 4948 const struct regulator_ops *ops = rdev->desc->ops; 4949 umode_t mode = attr->mode; 4950 4951 /* these three are always present */ 4952 if (attr == &dev_attr_name.attr || 4953 attr == &dev_attr_num_users.attr || 4954 attr == &dev_attr_type.attr) 4955 return mode; 4956 4957 /* some attributes need specific methods to be displayed */ 4958 if (attr == &dev_attr_microvolts.attr) { 4959 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || 4960 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || 4961 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || 4962 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1)) 4963 return mode; 4964 return 0; 4965 } 4966 4967 if (attr == &dev_attr_microamps.attr) 4968 return ops->get_current_limit ? mode : 0; 4969 4970 if (attr == &dev_attr_opmode.attr) 4971 return ops->get_mode ? mode : 0; 4972 4973 if (attr == &dev_attr_state.attr) 4974 return (rdev->ena_pin || ops->is_enabled) ? mode : 0; 4975 4976 if (attr == &dev_attr_status.attr) 4977 return ops->get_status ? mode : 0; 4978 4979 if (attr == &dev_attr_bypass.attr) 4980 return ops->get_bypass ? mode : 0; 4981 4982 /* constraints need specific supporting methods */ 4983 if (attr == &dev_attr_min_microvolts.attr || 4984 attr == &dev_attr_max_microvolts.attr) 4985 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0; 4986 4987 if (attr == &dev_attr_min_microamps.attr || 4988 attr == &dev_attr_max_microamps.attr) 4989 return ops->set_current_limit ? mode : 0; 4990 4991 if (attr == &dev_attr_suspend_standby_state.attr || 4992 attr == &dev_attr_suspend_mem_state.attr || 4993 attr == &dev_attr_suspend_disk_state.attr) 4994 return mode; 4995 4996 if (attr == &dev_attr_suspend_standby_microvolts.attr || 4997 attr == &dev_attr_suspend_mem_microvolts.attr || 4998 attr == &dev_attr_suspend_disk_microvolts.attr) 4999 return ops->set_suspend_voltage ? mode : 0; 5000 5001 if (attr == &dev_attr_suspend_standby_mode.attr || 5002 attr == &dev_attr_suspend_mem_mode.attr || 5003 attr == &dev_attr_suspend_disk_mode.attr) 5004 return ops->set_suspend_mode ? mode : 0; 5005 5006 return mode; 5007} 5008 5009static const struct attribute_group regulator_dev_group = { 5010 .attrs = regulator_dev_attrs, 5011 .is_visible = regulator_attr_is_visible, 5012}; 5013 5014static const struct attribute_group *regulator_dev_groups[] = { 5015 ®ulator_dev_group, 5016 NULL 5017}; 5018 5019static void regulator_dev_release(struct device *dev) 5020{ 5021 struct regulator_dev *rdev = dev_get_drvdata(dev); 5022 5023 debugfs_remove_recursive(rdev->debugfs); 5024 kfree(rdev->constraints); 5025 of_node_put(rdev->dev.of_node); 5026 kfree(rdev); 5027} 5028 5029static void rdev_init_debugfs(struct regulator_dev *rdev) 5030{ 5031 struct device *parent = rdev->dev.parent; 5032 const char *rname = rdev_get_name(rdev); 5033 char name[NAME_MAX]; 5034 5035 /* Avoid duplicate debugfs directory names */ 5036 if (parent && rname == rdev->desc->name) { 5037 snprintf(name, sizeof(name), "%s-%s", dev_name(parent), 5038 rname); 5039 rname = name; 5040 } 5041 5042 rdev->debugfs = debugfs_create_dir(rname, debugfs_root); 5043 if (IS_ERR(rdev->debugfs)) 5044 rdev_dbg(rdev, "Failed to create debugfs directory\n"); 5045 5046 debugfs_create_u32("use_count", 0444, rdev->debugfs, 5047 &rdev->use_count); 5048 debugfs_create_u32("open_count", 0444, rdev->debugfs, 5049 &rdev->open_count); 5050 debugfs_create_u32("bypass_count", 0444, rdev->debugfs, 5051 &rdev->bypass_count); 5052} 5053 5054static int regulator_register_resolve_supply(struct device *dev, void *data) 5055{ 5056 struct regulator_dev *rdev = dev_to_rdev(dev); 5057 5058 if (regulator_resolve_supply(rdev)) 5059 rdev_dbg(rdev, "unable to resolve supply\n"); 5060 5061 return 0; 5062} 5063 5064int regulator_coupler_register(struct regulator_coupler *coupler) 5065{ 5066 mutex_lock(®ulator_list_mutex); 5067 list_add_tail(&coupler->list, ®ulator_coupler_list); 5068 mutex_unlock(®ulator_list_mutex); 5069 5070 return 0; 5071} 5072 5073static struct regulator_coupler * 5074regulator_find_coupler(struct regulator_dev *rdev) 5075{ 5076 struct regulator_coupler *coupler; 5077 int err; 5078 5079 /* 5080 * Note that regulators are appended to the list and the generic 5081 * coupler is registered first, hence it will be attached at last 5082 * if nobody cared. 5083 */ 5084 list_for_each_entry_reverse(coupler, ®ulator_coupler_list, list) { 5085 err = coupler->attach_regulator(coupler, rdev); 5086 if (!err) { 5087 if (!coupler->balance_voltage && 5088 rdev->coupling_desc.n_coupled > 2) 5089 goto err_unsupported; 5090 5091 return coupler; 5092 } 5093 5094 if (err < 0) 5095 return ERR_PTR(err); 5096 5097 if (err == 1) 5098 continue; 5099 5100 break; 5101 } 5102 5103 return ERR_PTR(-EINVAL); 5104 5105err_unsupported: 5106 if (coupler->detach_regulator) 5107 coupler->detach_regulator(coupler, rdev); 5108 5109 rdev_err(rdev, 5110 "Voltage balancing for multiple regulator couples is unimplemented\n"); 5111 5112 return ERR_PTR(-EPERM); 5113} 5114 5115static void regulator_resolve_coupling(struct regulator_dev *rdev) 5116{ 5117 struct regulator_coupler *coupler = rdev->coupling_desc.coupler; 5118 struct coupling_desc *c_desc = &rdev->coupling_desc; 5119 int n_coupled = c_desc->n_coupled; 5120 struct regulator_dev *c_rdev; 5121 int i; 5122 5123 for (i = 1; i < n_coupled; i++) { 5124 /* already resolved */ 5125 if (c_desc->coupled_rdevs[i]) 5126 continue; 5127 5128 c_rdev = of_parse_coupled_regulator(rdev, i - 1); 5129 5130 if (!c_rdev) 5131 continue; 5132 5133 if (c_rdev->coupling_desc.coupler != coupler) { 5134 rdev_err(rdev, "coupler mismatch with %s\n", 5135 rdev_get_name(c_rdev)); 5136 return; 5137 } 5138 5139 c_desc->coupled_rdevs[i] = c_rdev; 5140 c_desc->n_resolved++; 5141 5142 regulator_resolve_coupling(c_rdev); 5143 } 5144} 5145 5146static void regulator_remove_coupling(struct regulator_dev *rdev) 5147{ 5148 struct regulator_coupler *coupler = rdev->coupling_desc.coupler; 5149 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc; 5150 struct regulator_dev *__c_rdev, *c_rdev; 5151 unsigned int __n_coupled, n_coupled; 5152 int i, k; 5153 int err; 5154 5155 n_coupled = c_desc->n_coupled; 5156 5157 for (i = 1; i < n_coupled; i++) { 5158 c_rdev = c_desc->coupled_rdevs[i]; 5159 5160 if (!c_rdev) 5161 continue; 5162 5163 regulator_lock(c_rdev); 5164 5165 __c_desc = &c_rdev->coupling_desc; 5166 __n_coupled = __c_desc->n_coupled; 5167 5168 for (k = 1; k < __n_coupled; k++) { 5169 __c_rdev = __c_desc->coupled_rdevs[k]; 5170 5171 if (__c_rdev == rdev) { 5172 __c_desc->coupled_rdevs[k] = NULL; 5173 __c_desc->n_resolved--; 5174 break; 5175 } 5176 } 5177 5178 regulator_unlock(c_rdev); 5179 5180 c_desc->coupled_rdevs[i] = NULL; 5181 c_desc->n_resolved--; 5182 } 5183 5184 if (coupler && coupler->detach_regulator) { 5185 err = coupler->detach_regulator(coupler, rdev); 5186 if (err) 5187 rdev_err(rdev, "failed to detach from coupler: %pe\n", 5188 ERR_PTR(err)); 5189 } 5190 5191 kfree(rdev->coupling_desc.coupled_rdevs); 5192 rdev->coupling_desc.coupled_rdevs = NULL; 5193} 5194 5195static int regulator_init_coupling(struct regulator_dev *rdev) 5196{ 5197 struct regulator_dev **coupled; 5198 int err, n_phandles; 5199 5200 if (!IS_ENABLED(CONFIG_OF)) 5201 n_phandles = 0; 5202 else 5203 n_phandles = of_get_n_coupled(rdev); 5204 5205 coupled = kcalloc(n_phandles + 1, sizeof(*coupled), GFP_KERNEL); 5206 if (!coupled) 5207 return -ENOMEM; 5208 5209 rdev->coupling_desc.coupled_rdevs = coupled; 5210 5211 /* 5212 * Every regulator should always have coupling descriptor filled with 5213 * at least pointer to itself. 5214 */ 5215 rdev->coupling_desc.coupled_rdevs[0] = rdev; 5216 rdev->coupling_desc.n_coupled = n_phandles + 1; 5217 rdev->coupling_desc.n_resolved++; 5218 5219 /* regulator isn't coupled */ 5220 if (n_phandles == 0) 5221 return 0; 5222 5223 if (!of_check_coupling_data(rdev)) 5224 return -EPERM; 5225 5226 mutex_lock(®ulator_list_mutex); 5227 rdev->coupling_desc.coupler = regulator_find_coupler(rdev); 5228 mutex_unlock(®ulator_list_mutex); 5229 5230 if (IS_ERR(rdev->coupling_desc.coupler)) { 5231 err = PTR_ERR(rdev->coupling_desc.coupler); 5232 rdev_err(rdev, "failed to get coupler: %pe\n", ERR_PTR(err)); 5233 return err; 5234 } 5235 5236 return 0; 5237} 5238 5239static int generic_coupler_attach(struct regulator_coupler *coupler, 5240 struct regulator_dev *rdev) 5241{ 5242 if (rdev->coupling_desc.n_coupled > 2) { 5243 rdev_err(rdev, 5244 "Voltage balancing for multiple regulator couples is unimplemented\n"); 5245 return -EPERM; 5246 } 5247 5248 if (!rdev->constraints->always_on) { 5249 rdev_err(rdev, 5250 "Coupling of a non always-on regulator is unimplemented\n"); 5251 return -ENOTSUPP; 5252 } 5253 5254 return 0; 5255} 5256 5257static struct regulator_coupler generic_regulator_coupler = { 5258 .attach_regulator = generic_coupler_attach, 5259}; 5260 5261/** 5262 * regulator_register - register regulator 5263 * @regulator_desc: regulator to register 5264 * @cfg: runtime configuration for regulator 5265 * 5266 * Called by regulator drivers to register a regulator. 5267 * Returns a valid pointer to struct regulator_dev on success 5268 * or an ERR_PTR() on error. 5269 */ 5270struct regulator_dev * 5271regulator_register(const struct regulator_desc *regulator_desc, 5272 const struct regulator_config *cfg) 5273{ 5274 const struct regulator_init_data *init_data; 5275 struct regulator_config *config = NULL; 5276 static atomic_t regulator_no = ATOMIC_INIT(-1); 5277 struct regulator_dev *rdev; 5278 bool dangling_cfg_gpiod = false; 5279 bool dangling_of_gpiod = false; 5280 struct device *dev; 5281 int ret, i; 5282 5283 if (cfg == NULL) 5284 return ERR_PTR(-EINVAL); 5285 if (cfg->ena_gpiod) 5286 dangling_cfg_gpiod = true; 5287 if (regulator_desc == NULL) { 5288 ret = -EINVAL; 5289 goto rinse; 5290 } 5291 5292 dev = cfg->dev; 5293 WARN_ON(!dev); 5294 5295 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) { 5296 ret = -EINVAL; 5297 goto rinse; 5298 } 5299 5300 if (regulator_desc->type != REGULATOR_VOLTAGE && 5301 regulator_desc->type != REGULATOR_CURRENT) { 5302 ret = -EINVAL; 5303 goto rinse; 5304 } 5305 5306 /* Only one of each should be implemented */ 5307 WARN_ON(regulator_desc->ops->get_voltage && 5308 regulator_desc->ops->get_voltage_sel); 5309 WARN_ON(regulator_desc->ops->set_voltage && 5310 regulator_desc->ops->set_voltage_sel); 5311 5312 /* If we're using selectors we must implement list_voltage. */ 5313 if (regulator_desc->ops->get_voltage_sel && 5314 !regulator_desc->ops->list_voltage) { 5315 ret = -EINVAL; 5316 goto rinse; 5317 } 5318 if (regulator_desc->ops->set_voltage_sel && 5319 !regulator_desc->ops->list_voltage) { 5320 ret = -EINVAL; 5321 goto rinse; 5322 } 5323 5324 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); 5325 if (rdev == NULL) { 5326 ret = -ENOMEM; 5327 goto rinse; 5328 } 5329 device_initialize(&rdev->dev); 5330 5331 /* 5332 * Duplicate the config so the driver could override it after 5333 * parsing init data. 5334 */ 5335 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); 5336 if (config == NULL) { 5337 ret = -ENOMEM; 5338 goto clean; 5339 } 5340 5341 init_data = regulator_of_get_init_data(dev, regulator_desc, config, 5342 &rdev->dev.of_node); 5343 5344 /* 5345 * Sometimes not all resources are probed already so we need to take 5346 * that into account. This happens most the time if the ena_gpiod comes 5347 * from a gpio extender or something else. 5348 */ 5349 if (PTR_ERR(init_data) == -EPROBE_DEFER) { 5350 ret = -EPROBE_DEFER; 5351 goto clean; 5352 } 5353 5354 /* 5355 * We need to keep track of any GPIO descriptor coming from the 5356 * device tree until we have handled it over to the core. If the 5357 * config that was passed in to this function DOES NOT contain 5358 * a descriptor, and the config after this call DOES contain 5359 * a descriptor, we definitely got one from parsing the device 5360 * tree. 5361 */ 5362 if (!cfg->ena_gpiod && config->ena_gpiod) 5363 dangling_of_gpiod = true; 5364 if (!init_data) { 5365 init_data = config->init_data; 5366 rdev->dev.of_node = of_node_get(config->of_node); 5367 } 5368 5369 ww_mutex_init(&rdev->mutex, ®ulator_ww_class); 5370 rdev->reg_data = config->driver_data; 5371 rdev->owner = regulator_desc->owner; 5372 rdev->desc = regulator_desc; 5373 if (config->regmap) 5374 rdev->regmap = config->regmap; 5375 else if (dev_get_regmap(dev, NULL)) 5376 rdev->regmap = dev_get_regmap(dev, NULL); 5377 else if (dev->parent) 5378 rdev->regmap = dev_get_regmap(dev->parent, NULL); 5379 INIT_LIST_HEAD(&rdev->consumer_list); 5380 INIT_LIST_HEAD(&rdev->list); 5381 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); 5382 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); 5383 5384 /* preform any regulator specific init */ 5385 if (init_data && init_data->regulator_init) { 5386 ret = init_data->regulator_init(rdev->reg_data); 5387 if (ret < 0) 5388 goto clean; 5389 } 5390 5391 if (config->ena_gpiod) { 5392 ret = regulator_ena_gpio_request(rdev, config); 5393 if (ret != 0) { 5394 rdev_err(rdev, "Failed to request enable GPIO: %pe\n", 5395 ERR_PTR(ret)); 5396 goto clean; 5397 } 5398 /* The regulator core took over the GPIO descriptor */ 5399 dangling_cfg_gpiod = false; 5400 dangling_of_gpiod = false; 5401 } 5402 5403 /* register with sysfs */ 5404 rdev->dev.class = ®ulator_class; 5405 rdev->dev.parent = dev; 5406 dev_set_name(&rdev->dev, "regulator.%lu", 5407 (unsigned long) atomic_inc_return(®ulator_no)); 5408 dev_set_drvdata(&rdev->dev, rdev); 5409 5410 /* set regulator constraints */ 5411 if (init_data) 5412 rdev->constraints = kmemdup(&init_data->constraints, 5413 sizeof(*rdev->constraints), 5414 GFP_KERNEL); 5415 else 5416 rdev->constraints = kzalloc(sizeof(*rdev->constraints), 5417 GFP_KERNEL); 5418 if (!rdev->constraints) { 5419 ret = -ENOMEM; 5420 goto wash; 5421 } 5422 5423 if (init_data && init_data->supply_regulator) 5424 rdev->supply_name = init_data->supply_regulator; 5425 else if (regulator_desc->supply_name) 5426 rdev->supply_name = regulator_desc->supply_name; 5427 5428 ret = set_machine_constraints(rdev); 5429 if (ret == -EPROBE_DEFER) { 5430 /* Regulator might be in bypass mode and so needs its supply 5431 * to set the constraints */ 5432 /* FIXME: this currently triggers a chicken-and-egg problem 5433 * when creating -SUPPLY symlink in sysfs to a regulator 5434 * that is just being created */ 5435 ret = regulator_resolve_supply(rdev); 5436 if (!ret) 5437 ret = set_machine_constraints(rdev); 5438 else 5439 rdev_dbg(rdev, "unable to resolve supply early: %pe\n", 5440 ERR_PTR(ret)); 5441 } 5442 if (ret < 0) 5443 goto wash; 5444 5445 ret = regulator_init_coupling(rdev); 5446 if (ret < 0) 5447 goto wash; 5448 5449 /* add consumers devices */ 5450 if (init_data) { 5451 for (i = 0; i < init_data->num_consumer_supplies; i++) { 5452 ret = set_consumer_device_supply(rdev, 5453 init_data->consumer_supplies[i].dev_name, 5454 init_data->consumer_supplies[i].supply); 5455 if (ret < 0) { 5456 dev_err(dev, "Failed to set supply %s\n", 5457 init_data->consumer_supplies[i].supply); 5458 goto unset_supplies; 5459 } 5460 } 5461 } 5462 5463 if (!rdev->desc->ops->get_voltage && 5464 !rdev->desc->ops->list_voltage && 5465 !rdev->desc->fixed_uV) 5466 rdev->is_switch = true; 5467 5468 ret = device_add(&rdev->dev); 5469 if (ret != 0) 5470 goto unset_supplies; 5471 5472 rdev_init_debugfs(rdev); 5473 5474 /* try to resolve regulators coupling since a new one was registered */ 5475 mutex_lock(®ulator_list_mutex); 5476 regulator_resolve_coupling(rdev); 5477 mutex_unlock(®ulator_list_mutex); 5478 5479 /* try to resolve regulators supply since a new one was registered */ 5480 class_for_each_device(®ulator_class, NULL, NULL, 5481 regulator_register_resolve_supply); 5482 kfree(config); 5483 return rdev; 5484 5485unset_supplies: 5486 mutex_lock(®ulator_list_mutex); 5487 unset_regulator_supplies(rdev); 5488 regulator_remove_coupling(rdev); 5489 mutex_unlock(®ulator_list_mutex); 5490wash: 5491 regulator_put(rdev->supply); 5492 kfree(rdev->coupling_desc.coupled_rdevs); 5493 mutex_lock(®ulator_list_mutex); 5494 regulator_ena_gpio_free(rdev); 5495 mutex_unlock(®ulator_list_mutex); 5496clean: 5497 if (dangling_of_gpiod) 5498 gpiod_put(config->ena_gpiod); 5499 kfree(config); 5500 put_device(&rdev->dev); 5501rinse: 5502 if (dangling_cfg_gpiod) 5503 gpiod_put(cfg->ena_gpiod); 5504 return ERR_PTR(ret); 5505} 5506EXPORT_SYMBOL_GPL(regulator_register); 5507 5508/** 5509 * regulator_unregister - unregister regulator 5510 * @rdev: regulator to unregister 5511 * 5512 * Called by regulator drivers to unregister a regulator. 5513 */ 5514void regulator_unregister(struct regulator_dev *rdev) 5515{ 5516 if (rdev == NULL) 5517 return; 5518 5519 if (rdev->supply) { 5520 while (rdev->use_count--) 5521 regulator_disable(rdev->supply); 5522 regulator_put(rdev->supply); 5523 } 5524 5525 flush_work(&rdev->disable_work.work); 5526 5527 mutex_lock(®ulator_list_mutex); 5528 5529 WARN_ON(rdev->open_count); 5530 regulator_remove_coupling(rdev); 5531 unset_regulator_supplies(rdev); 5532 list_del(&rdev->list); 5533 regulator_ena_gpio_free(rdev); 5534 device_unregister(&rdev->dev); 5535 5536 mutex_unlock(®ulator_list_mutex); 5537} 5538EXPORT_SYMBOL_GPL(regulator_unregister); 5539 5540#ifdef CONFIG_SUSPEND 5541/** 5542 * regulator_suspend - prepare regulators for system wide suspend 5543 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend() 5544 * 5545 * Configure each regulator with it's suspend operating parameters for state. 5546 */ 5547static int regulator_suspend(struct device *dev) 5548{ 5549 struct regulator_dev *rdev = dev_to_rdev(dev); 5550 suspend_state_t state = pm_suspend_target_state; 5551 int ret; 5552 const struct regulator_state *rstate; 5553 5554 rstate = regulator_get_suspend_state_check(rdev, state); 5555 if (!rstate) 5556 return 0; 5557 5558 regulator_lock(rdev); 5559 ret = __suspend_set_state(rdev, rstate); 5560 regulator_unlock(rdev); 5561 5562 return ret; 5563} 5564 5565static int regulator_resume(struct device *dev) 5566{ 5567 suspend_state_t state = pm_suspend_target_state; 5568 struct regulator_dev *rdev = dev_to_rdev(dev); 5569 struct regulator_state *rstate; 5570 int ret = 0; 5571 5572 rstate = regulator_get_suspend_state(rdev, state); 5573 if (rstate == NULL) 5574 return 0; 5575 5576 /* Avoid grabbing the lock if we don't need to */ 5577 if (!rdev->desc->ops->resume) 5578 return 0; 5579 5580 regulator_lock(rdev); 5581 5582 if (rstate->enabled == ENABLE_IN_SUSPEND || 5583 rstate->enabled == DISABLE_IN_SUSPEND) 5584 ret = rdev->desc->ops->resume(rdev); 5585 5586 regulator_unlock(rdev); 5587 5588 return ret; 5589} 5590#else /* !CONFIG_SUSPEND */ 5591 5592#define regulator_suspend NULL 5593#define regulator_resume NULL 5594 5595#endif /* !CONFIG_SUSPEND */ 5596 5597#ifdef CONFIG_PM 5598static const struct dev_pm_ops __maybe_unused regulator_pm_ops = { 5599 .suspend = regulator_suspend, 5600 .resume = regulator_resume, 5601}; 5602#endif 5603 5604struct class regulator_class = { 5605 .name = "regulator", 5606 .dev_release = regulator_dev_release, 5607 .dev_groups = regulator_dev_groups, 5608#ifdef CONFIG_PM 5609 .pm = ®ulator_pm_ops, 5610#endif 5611}; 5612/** 5613 * regulator_has_full_constraints - the system has fully specified constraints 5614 * 5615 * Calling this function will cause the regulator API to disable all 5616 * regulators which have a zero use count and don't have an always_on 5617 * constraint in a late_initcall. 5618 * 5619 * The intention is that this will become the default behaviour in a 5620 * future kernel release so users are encouraged to use this facility 5621 * now. 5622 */ 5623void regulator_has_full_constraints(void) 5624{ 5625 has_full_constraints = 1; 5626} 5627EXPORT_SYMBOL_GPL(regulator_has_full_constraints); 5628 5629/** 5630 * rdev_get_drvdata - get rdev regulator driver data 5631 * @rdev: regulator 5632 * 5633 * Get rdev regulator driver private data. This call can be used in the 5634 * regulator driver context. 5635 */ 5636void *rdev_get_drvdata(struct regulator_dev *rdev) 5637{ 5638 return rdev->reg_data; 5639} 5640EXPORT_SYMBOL_GPL(rdev_get_drvdata); 5641 5642/** 5643 * regulator_get_drvdata - get regulator driver data 5644 * @regulator: regulator 5645 * 5646 * Get regulator driver private data. This call can be used in the consumer 5647 * driver context when non API regulator specific functions need to be called. 5648 */ 5649void *regulator_get_drvdata(struct regulator *regulator) 5650{ 5651 return regulator->rdev->reg_data; 5652} 5653EXPORT_SYMBOL_GPL(regulator_get_drvdata); 5654 5655/** 5656 * regulator_set_drvdata - set regulator driver data 5657 * @regulator: regulator 5658 * @data: data 5659 */ 5660void regulator_set_drvdata(struct regulator *regulator, void *data) 5661{ 5662 regulator->rdev->reg_data = data; 5663} 5664EXPORT_SYMBOL_GPL(regulator_set_drvdata); 5665 5666/** 5667 * regulator_get_id - get regulator ID 5668 * @rdev: regulator 5669 */ 5670int rdev_get_id(struct regulator_dev *rdev) 5671{ 5672 return rdev->desc->id; 5673} 5674EXPORT_SYMBOL_GPL(rdev_get_id); 5675 5676struct device *rdev_get_dev(struct regulator_dev *rdev) 5677{ 5678 return &rdev->dev; 5679} 5680EXPORT_SYMBOL_GPL(rdev_get_dev); 5681 5682struct regmap *rdev_get_regmap(struct regulator_dev *rdev) 5683{ 5684 return rdev->regmap; 5685} 5686EXPORT_SYMBOL_GPL(rdev_get_regmap); 5687 5688void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) 5689{ 5690 return reg_init_data->driver_data; 5691} 5692EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); 5693 5694#ifdef CONFIG_DEBUG_FS 5695static int supply_map_show(struct seq_file *sf, void *data) 5696{ 5697 struct regulator_map *map; 5698 5699 list_for_each_entry(map, ®ulator_map_list, list) { 5700 seq_printf(sf, "%s -> %s.%s\n", 5701 rdev_get_name(map->regulator), map->dev_name, 5702 map->supply); 5703 } 5704 5705 return 0; 5706} 5707DEFINE_SHOW_ATTRIBUTE(supply_map); 5708 5709struct summary_data { 5710 struct seq_file *s; 5711 struct regulator_dev *parent; 5712 int level; 5713}; 5714 5715static void regulator_summary_show_subtree(struct seq_file *s, 5716 struct regulator_dev *rdev, 5717 int level); 5718 5719static int regulator_summary_show_children(struct device *dev, void *data) 5720{ 5721 struct regulator_dev *rdev = dev_to_rdev(dev); 5722 struct summary_data *summary_data = data; 5723 5724 if (rdev->supply && rdev->supply->rdev == summary_data->parent) 5725 regulator_summary_show_subtree(summary_data->s, rdev, 5726 summary_data->level + 1); 5727 5728 return 0; 5729} 5730 5731static void regulator_summary_show_subtree(struct seq_file *s, 5732 struct regulator_dev *rdev, 5733 int level) 5734{ 5735 struct regulation_constraints *c; 5736 struct regulator *consumer; 5737 struct summary_data summary_data; 5738 unsigned int opmode; 5739 5740 if (!rdev) 5741 return; 5742 5743 opmode = _regulator_get_mode_unlocked(rdev); 5744 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ", 5745 level * 3 + 1, "", 5746 30 - level * 3, rdev_get_name(rdev), 5747 rdev->use_count, rdev->open_count, rdev->bypass_count, 5748 regulator_opmode_to_str(opmode)); 5749 5750 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000); 5751 seq_printf(s, "%5dmA ", 5752 _regulator_get_current_limit_unlocked(rdev) / 1000); 5753 5754 c = rdev->constraints; 5755 if (c) { 5756 switch (rdev->desc->type) { 5757 case REGULATOR_VOLTAGE: 5758 seq_printf(s, "%5dmV %5dmV ", 5759 c->min_uV / 1000, c->max_uV / 1000); 5760 break; 5761 case REGULATOR_CURRENT: 5762 seq_printf(s, "%5dmA %5dmA ", 5763 c->min_uA / 1000, c->max_uA / 1000); 5764 break; 5765 } 5766 } 5767 5768 seq_puts(s, "\n"); 5769 5770 list_for_each_entry(consumer, &rdev->consumer_list, list) { 5771 if (consumer->dev && consumer->dev->class == ®ulator_class) 5772 continue; 5773 5774 seq_printf(s, "%*s%-*s ", 5775 (level + 1) * 3 + 1, "", 5776 30 - (level + 1) * 3, 5777 consumer->supply_name ? consumer->supply_name : 5778 consumer->dev ? dev_name(consumer->dev) : "deviceless"); 5779 5780 switch (rdev->desc->type) { 5781 case REGULATOR_VOLTAGE: 5782 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV", 5783 consumer->enable_count, 5784 consumer->uA_load / 1000, 5785 consumer->uA_load && !consumer->enable_count ? 5786 '*' : ' ', 5787 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000, 5788 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000); 5789 break; 5790 case REGULATOR_CURRENT: 5791 break; 5792 } 5793 5794 seq_puts(s, "\n"); 5795 } 5796 5797 summary_data.s = s; 5798 summary_data.level = level; 5799 summary_data.parent = rdev; 5800 5801 class_for_each_device(®ulator_class, NULL, &summary_data, 5802 regulator_summary_show_children); 5803} 5804 5805struct summary_lock_data { 5806 struct ww_acquire_ctx *ww_ctx; 5807 struct regulator_dev **new_contended_rdev; 5808 struct regulator_dev **old_contended_rdev; 5809}; 5810 5811static int regulator_summary_lock_one(struct device *dev, void *data) 5812{ 5813 struct regulator_dev *rdev = dev_to_rdev(dev); 5814 struct summary_lock_data *lock_data = data; 5815 int ret = 0; 5816 5817 if (rdev != *lock_data->old_contended_rdev) { 5818 ret = regulator_lock_nested(rdev, lock_data->ww_ctx); 5819 5820 if (ret == -EDEADLK) 5821 *lock_data->new_contended_rdev = rdev; 5822 else 5823 WARN_ON_ONCE(ret); 5824 } else { 5825 *lock_data->old_contended_rdev = NULL; 5826 } 5827 5828 return ret; 5829} 5830 5831static int regulator_summary_unlock_one(struct device *dev, void *data) 5832{ 5833 struct regulator_dev *rdev = dev_to_rdev(dev); 5834 struct summary_lock_data *lock_data = data; 5835 5836 if (lock_data) { 5837 if (rdev == *lock_data->new_contended_rdev) 5838 return -EDEADLK; 5839 } 5840 5841 regulator_unlock(rdev); 5842 5843 return 0; 5844} 5845 5846static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx, 5847 struct regulator_dev **new_contended_rdev, 5848 struct regulator_dev **old_contended_rdev) 5849{ 5850 struct summary_lock_data lock_data; 5851 int ret; 5852 5853 lock_data.ww_ctx = ww_ctx; 5854 lock_data.new_contended_rdev = new_contended_rdev; 5855 lock_data.old_contended_rdev = old_contended_rdev; 5856 5857 ret = class_for_each_device(®ulator_class, NULL, &lock_data, 5858 regulator_summary_lock_one); 5859 if (ret) 5860 class_for_each_device(®ulator_class, NULL, &lock_data, 5861 regulator_summary_unlock_one); 5862 5863 return ret; 5864} 5865 5866static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx) 5867{ 5868 struct regulator_dev *new_contended_rdev = NULL; 5869 struct regulator_dev *old_contended_rdev = NULL; 5870 int err; 5871 5872 mutex_lock(®ulator_list_mutex); 5873 5874 ww_acquire_init(ww_ctx, ®ulator_ww_class); 5875 5876 do { 5877 if (new_contended_rdev) { 5878 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx); 5879 old_contended_rdev = new_contended_rdev; 5880 old_contended_rdev->ref_cnt++; 5881 old_contended_rdev->mutex_owner = current; 5882 } 5883 5884 err = regulator_summary_lock_all(ww_ctx, 5885 &new_contended_rdev, 5886 &old_contended_rdev); 5887 5888 if (old_contended_rdev) 5889 regulator_unlock(old_contended_rdev); 5890 5891 } while (err == -EDEADLK); 5892 5893 ww_acquire_done(ww_ctx); 5894} 5895 5896static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx) 5897{ 5898 class_for_each_device(®ulator_class, NULL, NULL, 5899 regulator_summary_unlock_one); 5900 ww_acquire_fini(ww_ctx); 5901 5902 mutex_unlock(®ulator_list_mutex); 5903} 5904 5905static int regulator_summary_show_roots(struct device *dev, void *data) 5906{ 5907 struct regulator_dev *rdev = dev_to_rdev(dev); 5908 struct seq_file *s = data; 5909 5910 if (!rdev->supply) 5911 regulator_summary_show_subtree(s, rdev, 0); 5912 5913 return 0; 5914} 5915 5916static int regulator_summary_show(struct seq_file *s, void *data) 5917{ 5918 struct ww_acquire_ctx ww_ctx; 5919 5920 seq_puts(s, " regulator use open bypass opmode voltage current min max\n"); 5921 seq_puts(s, "---------------------------------------------------------------------------------------\n"); 5922 5923 regulator_summary_lock(&ww_ctx); 5924 5925 class_for_each_device(®ulator_class, NULL, s, 5926 regulator_summary_show_roots); 5927 5928 regulator_summary_unlock(&ww_ctx); 5929 5930 return 0; 5931} 5932DEFINE_SHOW_ATTRIBUTE(regulator_summary); 5933#endif /* CONFIG_DEBUG_FS */ 5934 5935static int __init regulator_init(void) 5936{ 5937 int ret; 5938 5939 ret = class_register(®ulator_class); 5940 5941 debugfs_root = debugfs_create_dir("regulator", NULL); 5942 if (IS_ERR(debugfs_root)) 5943 pr_debug("regulator: Failed to create debugfs directory\n"); 5944 5945#ifdef CONFIG_DEBUG_FS 5946 debugfs_create_file("supply_map", 0444, debugfs_root, NULL, 5947 &supply_map_fops); 5948 5949 debugfs_create_file("regulator_summary", 0444, debugfs_root, 5950 NULL, ®ulator_summary_fops); 5951#endif 5952 regulator_dummy_init(); 5953 5954 regulator_coupler_register(&generic_regulator_coupler); 5955 5956 return ret; 5957} 5958 5959/* init early to allow our consumers to complete system booting */ 5960core_initcall(regulator_init); 5961 5962static int regulator_late_cleanup(struct device *dev, void *data) 5963{ 5964 struct regulator_dev *rdev = dev_to_rdev(dev); 5965 struct regulation_constraints *c = rdev->constraints; 5966 int ret; 5967 5968 if (c && c->always_on) 5969 return 0; 5970 5971 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) 5972 return 0; 5973 5974 regulator_lock(rdev); 5975 5976 if (rdev->use_count) 5977 goto unlock; 5978 5979 /* If reading the status failed, assume that it's off. */ 5980 if (_regulator_is_enabled(rdev) <= 0) 5981 goto unlock; 5982 5983 if (have_full_constraints()) { 5984 /* We log since this may kill the system if it goes 5985 * wrong. */ 5986 rdev_info(rdev, "disabling\n"); 5987 ret = _regulator_do_disable(rdev); 5988 if (ret != 0) 5989 rdev_err(rdev, "couldn't disable: %pe\n", ERR_PTR(ret)); 5990 } else { 5991 /* The intention is that in future we will 5992 * assume that full constraints are provided 5993 * so warn even if we aren't going to do 5994 * anything here. 5995 */ 5996 rdev_warn(rdev, "incomplete constraints, leaving on\n"); 5997 } 5998 5999unlock: 6000 regulator_unlock(rdev); 6001 6002 return 0; 6003} 6004 6005static void regulator_init_complete_work_function(struct work_struct *work) 6006{ 6007 /* 6008 * Regulators may had failed to resolve their input supplies 6009 * when were registered, either because the input supply was 6010 * not registered yet or because its parent device was not 6011 * bound yet. So attempt to resolve the input supplies for 6012 * pending regulators before trying to disable unused ones. 6013 */ 6014 class_for_each_device(®ulator_class, NULL, NULL, 6015 regulator_register_resolve_supply); 6016 6017 /* If we have a full configuration then disable any regulators 6018 * we have permission to change the status for and which are 6019 * not in use or always_on. This is effectively the default 6020 * for DT and ACPI as they have full constraints. 6021 */ 6022 class_for_each_device(®ulator_class, NULL, NULL, 6023 regulator_late_cleanup); 6024} 6025 6026static DECLARE_DELAYED_WORK(regulator_init_complete_work, 6027 regulator_init_complete_work_function); 6028 6029static int __init regulator_init_complete(void) 6030{ 6031 /* 6032 * Since DT doesn't provide an idiomatic mechanism for 6033 * enabling full constraints and since it's much more natural 6034 * with DT to provide them just assume that a DT enabled 6035 * system has full constraints. 6036 */ 6037 if (of_have_populated_dt()) 6038 has_full_constraints = true; 6039 6040 /* 6041 * We punt completion for an arbitrary amount of time since 6042 * systems like distros will load many drivers from userspace 6043 * so consumers might not always be ready yet, this is 6044 * particularly an issue with laptops where this might bounce 6045 * the display off then on. Ideally we'd get a notification 6046 * from userspace when this happens but we don't so just wait 6047 * a bit and hope we waited long enough. It'd be better if 6048 * we'd only do this on systems that need it, and a kernel 6049 * command line option might be useful. 6050 */ 6051 schedule_delayed_work(®ulator_init_complete_work, 6052 msecs_to_jiffies(30000)); 6053 6054 return 0; 6055} 6056late_initcall_sync(regulator_init_complete); 6057