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