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