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