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