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