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