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