1 /* 2 * core.c -- Voltage/Current Regulator framework. 3 * 4 * Copyright 2007, 2008 Wolfson Microelectronics PLC. 5 * Copyright 2008 SlimLogic Ltd. 6 * 7 * Author: Liam Girdwood <lrg@slimlogic.co.uk> 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License as published by the 11 * Free Software Foundation; either version 2 of the License, or (at your 12 * option) any later version. 13 * 14 */ 15 16 #include <linux/kernel.h> 17 #include <linux/init.h> 18 #include <linux/debugfs.h> 19 #include <linux/device.h> 20 #include <linux/slab.h> 21 #include <linux/async.h> 22 #include <linux/err.h> 23 #include <linux/mutex.h> 24 #include <linux/suspend.h> 25 #include <linux/delay.h> 26 #include <linux/gpio.h> 27 #include <linux/gpio/consumer.h> 28 #include <linux/of.h> 29 #include <linux/regmap.h> 30 #include <linux/regulator/of_regulator.h> 31 #include <linux/regulator/consumer.h> 32 #include <linux/regulator/driver.h> 33 #include <linux/regulator/machine.h> 34 #include <linux/module.h> 35 36 #define CREATE_TRACE_POINTS 37 #include <trace/events/regulator.h> 38 39 #include "dummy.h" 40 #include "internal.h" 41 42 #define rdev_crit(rdev, fmt, ...) \ 43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 44 #define rdev_err(rdev, fmt, ...) \ 45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 46 #define rdev_warn(rdev, fmt, ...) \ 47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 48 #define rdev_info(rdev, fmt, ...) \ 49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 50 #define rdev_dbg(rdev, fmt, ...) \ 51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) 52 53 static DEFINE_MUTEX(regulator_list_mutex); 54 static LIST_HEAD(regulator_list); 55 static LIST_HEAD(regulator_map_list); 56 static LIST_HEAD(regulator_ena_gpio_list); 57 static LIST_HEAD(regulator_supply_alias_list); 58 static bool has_full_constraints; 59 60 static struct dentry *debugfs_root; 61 62 /* 63 * struct regulator_map 64 * 65 * Used to provide symbolic supply names to devices. 66 */ 67 struct regulator_map { 68 struct list_head list; 69 const char *dev_name; /* The dev_name() for the consumer */ 70 const char *supply; 71 struct regulator_dev *regulator; 72 }; 73 74 /* 75 * struct regulator_enable_gpio 76 * 77 * Management for shared enable GPIO pin 78 */ 79 struct regulator_enable_gpio { 80 struct list_head list; 81 struct gpio_desc *gpiod; 82 u32 enable_count; /* a number of enabled shared GPIO */ 83 u32 request_count; /* a number of requested shared GPIO */ 84 unsigned int ena_gpio_invert:1; 85 }; 86 87 /* 88 * struct regulator_supply_alias 89 * 90 * Used to map lookups for a supply onto an alternative device. 91 */ 92 struct regulator_supply_alias { 93 struct list_head list; 94 struct device *src_dev; 95 const char *src_supply; 96 struct device *alias_dev; 97 const char *alias_supply; 98 }; 99 100 static int _regulator_is_enabled(struct regulator_dev *rdev); 101 static int _regulator_disable(struct regulator_dev *rdev); 102 static int _regulator_get_voltage(struct regulator_dev *rdev); 103 static int _regulator_get_current_limit(struct regulator_dev *rdev); 104 static unsigned int _regulator_get_mode(struct regulator_dev *rdev); 105 static int _notifier_call_chain(struct regulator_dev *rdev, 106 unsigned long event, void *data); 107 static int _regulator_do_set_voltage(struct regulator_dev *rdev, 108 int min_uV, int max_uV); 109 static struct regulator *create_regulator(struct regulator_dev *rdev, 110 struct device *dev, 111 const char *supply_name); 112 static void _regulator_put(struct regulator *regulator); 113 114 static const char *rdev_get_name(struct regulator_dev *rdev) 115 { 116 if (rdev->constraints && rdev->constraints->name) 117 return rdev->constraints->name; 118 else if (rdev->desc->name) 119 return rdev->desc->name; 120 else 121 return ""; 122 } 123 124 static bool have_full_constraints(void) 125 { 126 return has_full_constraints || of_have_populated_dt(); 127 } 128 129 /** 130 * of_get_regulator - get a regulator device node based on supply name 131 * @dev: Device pointer for the consumer (of regulator) device 132 * @supply: regulator supply name 133 * 134 * Extract the regulator device node corresponding to the supply name. 135 * returns the device node corresponding to the regulator if found, else 136 * returns NULL. 137 */ 138 static struct device_node *of_get_regulator(struct device *dev, const char *supply) 139 { 140 struct device_node *regnode = NULL; 141 char prop_name[32]; /* 32 is max size of property name */ 142 143 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); 144 145 snprintf(prop_name, 32, "%s-supply", supply); 146 regnode = of_parse_phandle(dev->of_node, prop_name, 0); 147 148 if (!regnode) { 149 dev_dbg(dev, "Looking up %s property in node %s failed", 150 prop_name, dev->of_node->full_name); 151 return NULL; 152 } 153 return regnode; 154 } 155 156 static int _regulator_can_change_status(struct regulator_dev *rdev) 157 { 158 if (!rdev->constraints) 159 return 0; 160 161 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS) 162 return 1; 163 else 164 return 0; 165 } 166 167 /* Platform voltage constraint check */ 168 static int regulator_check_voltage(struct regulator_dev *rdev, 169 int *min_uV, int *max_uV) 170 { 171 BUG_ON(*min_uV > *max_uV); 172 173 if (!rdev->constraints) { 174 rdev_err(rdev, "no constraints\n"); 175 return -ENODEV; 176 } 177 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { 178 rdev_err(rdev, "operation not allowed\n"); 179 return -EPERM; 180 } 181 182 if (*max_uV > rdev->constraints->max_uV) 183 *max_uV = rdev->constraints->max_uV; 184 if (*min_uV < rdev->constraints->min_uV) 185 *min_uV = rdev->constraints->min_uV; 186 187 if (*min_uV > *max_uV) { 188 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", 189 *min_uV, *max_uV); 190 return -EINVAL; 191 } 192 193 return 0; 194 } 195 196 /* Make sure we select a voltage that suits the needs of all 197 * regulator consumers 198 */ 199 static int regulator_check_consumers(struct regulator_dev *rdev, 200 int *min_uV, int *max_uV) 201 { 202 struct regulator *regulator; 203 204 list_for_each_entry(regulator, &rdev->consumer_list, list) { 205 /* 206 * Assume consumers that didn't say anything are OK 207 * with anything in the constraint range. 208 */ 209 if (!regulator->min_uV && !regulator->max_uV) 210 continue; 211 212 if (*max_uV > regulator->max_uV) 213 *max_uV = regulator->max_uV; 214 if (*min_uV < regulator->min_uV) 215 *min_uV = regulator->min_uV; 216 } 217 218 if (*min_uV > *max_uV) { 219 rdev_err(rdev, "Restricting voltage, %u-%uuV\n", 220 *min_uV, *max_uV); 221 return -EINVAL; 222 } 223 224 return 0; 225 } 226 227 /* current constraint check */ 228 static int regulator_check_current_limit(struct regulator_dev *rdev, 229 int *min_uA, int *max_uA) 230 { 231 BUG_ON(*min_uA > *max_uA); 232 233 if (!rdev->constraints) { 234 rdev_err(rdev, "no constraints\n"); 235 return -ENODEV; 236 } 237 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) { 238 rdev_err(rdev, "operation not allowed\n"); 239 return -EPERM; 240 } 241 242 if (*max_uA > rdev->constraints->max_uA) 243 *max_uA = rdev->constraints->max_uA; 244 if (*min_uA < rdev->constraints->min_uA) 245 *min_uA = rdev->constraints->min_uA; 246 247 if (*min_uA > *max_uA) { 248 rdev_err(rdev, "unsupportable current range: %d-%duA\n", 249 *min_uA, *max_uA); 250 return -EINVAL; 251 } 252 253 return 0; 254 } 255 256 /* operating mode constraint check */ 257 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode) 258 { 259 switch (*mode) { 260 case REGULATOR_MODE_FAST: 261 case REGULATOR_MODE_NORMAL: 262 case REGULATOR_MODE_IDLE: 263 case REGULATOR_MODE_STANDBY: 264 break; 265 default: 266 rdev_err(rdev, "invalid mode %x specified\n", *mode); 267 return -EINVAL; 268 } 269 270 if (!rdev->constraints) { 271 rdev_err(rdev, "no constraints\n"); 272 return -ENODEV; 273 } 274 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) { 275 rdev_err(rdev, "operation not allowed\n"); 276 return -EPERM; 277 } 278 279 /* The modes are bitmasks, the most power hungry modes having 280 * the lowest values. If the requested mode isn't supported 281 * try higher modes. */ 282 while (*mode) { 283 if (rdev->constraints->valid_modes_mask & *mode) 284 return 0; 285 *mode /= 2; 286 } 287 288 return -EINVAL; 289 } 290 291 /* dynamic regulator mode switching constraint check */ 292 static int regulator_check_drms(struct regulator_dev *rdev) 293 { 294 if (!rdev->constraints) { 295 rdev_err(rdev, "no constraints\n"); 296 return -ENODEV; 297 } 298 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) { 299 rdev_err(rdev, "operation not allowed\n"); 300 return -EPERM; 301 } 302 return 0; 303 } 304 305 static ssize_t regulator_uV_show(struct device *dev, 306 struct device_attribute *attr, char *buf) 307 { 308 struct regulator_dev *rdev = dev_get_drvdata(dev); 309 ssize_t ret; 310 311 mutex_lock(&rdev->mutex); 312 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); 313 mutex_unlock(&rdev->mutex); 314 315 return ret; 316 } 317 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); 318 319 static ssize_t regulator_uA_show(struct device *dev, 320 struct device_attribute *attr, char *buf) 321 { 322 struct regulator_dev *rdev = dev_get_drvdata(dev); 323 324 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); 325 } 326 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); 327 328 static ssize_t name_show(struct device *dev, struct device_attribute *attr, 329 char *buf) 330 { 331 struct regulator_dev *rdev = dev_get_drvdata(dev); 332 333 return sprintf(buf, "%s\n", rdev_get_name(rdev)); 334 } 335 static DEVICE_ATTR_RO(name); 336 337 static ssize_t regulator_print_opmode(char *buf, int mode) 338 { 339 switch (mode) { 340 case REGULATOR_MODE_FAST: 341 return sprintf(buf, "fast\n"); 342 case REGULATOR_MODE_NORMAL: 343 return sprintf(buf, "normal\n"); 344 case REGULATOR_MODE_IDLE: 345 return sprintf(buf, "idle\n"); 346 case REGULATOR_MODE_STANDBY: 347 return sprintf(buf, "standby\n"); 348 } 349 return sprintf(buf, "unknown\n"); 350 } 351 352 static ssize_t regulator_opmode_show(struct device *dev, 353 struct device_attribute *attr, char *buf) 354 { 355 struct regulator_dev *rdev = dev_get_drvdata(dev); 356 357 return regulator_print_opmode(buf, _regulator_get_mode(rdev)); 358 } 359 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); 360 361 static ssize_t regulator_print_state(char *buf, int state) 362 { 363 if (state > 0) 364 return sprintf(buf, "enabled\n"); 365 else if (state == 0) 366 return sprintf(buf, "disabled\n"); 367 else 368 return sprintf(buf, "unknown\n"); 369 } 370 371 static ssize_t regulator_state_show(struct device *dev, 372 struct device_attribute *attr, char *buf) 373 { 374 struct regulator_dev *rdev = dev_get_drvdata(dev); 375 ssize_t ret; 376 377 mutex_lock(&rdev->mutex); 378 ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); 379 mutex_unlock(&rdev->mutex); 380 381 return ret; 382 } 383 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); 384 385 static ssize_t regulator_status_show(struct device *dev, 386 struct device_attribute *attr, char *buf) 387 { 388 struct regulator_dev *rdev = dev_get_drvdata(dev); 389 int status; 390 char *label; 391 392 status = rdev->desc->ops->get_status(rdev); 393 if (status < 0) 394 return status; 395 396 switch (status) { 397 case REGULATOR_STATUS_OFF: 398 label = "off"; 399 break; 400 case REGULATOR_STATUS_ON: 401 label = "on"; 402 break; 403 case REGULATOR_STATUS_ERROR: 404 label = "error"; 405 break; 406 case REGULATOR_STATUS_FAST: 407 label = "fast"; 408 break; 409 case REGULATOR_STATUS_NORMAL: 410 label = "normal"; 411 break; 412 case REGULATOR_STATUS_IDLE: 413 label = "idle"; 414 break; 415 case REGULATOR_STATUS_STANDBY: 416 label = "standby"; 417 break; 418 case REGULATOR_STATUS_BYPASS: 419 label = "bypass"; 420 break; 421 case REGULATOR_STATUS_UNDEFINED: 422 label = "undefined"; 423 break; 424 default: 425 return -ERANGE; 426 } 427 428 return sprintf(buf, "%s\n", label); 429 } 430 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); 431 432 static ssize_t regulator_min_uA_show(struct device *dev, 433 struct device_attribute *attr, char *buf) 434 { 435 struct regulator_dev *rdev = dev_get_drvdata(dev); 436 437 if (!rdev->constraints) 438 return sprintf(buf, "constraint not defined\n"); 439 440 return sprintf(buf, "%d\n", rdev->constraints->min_uA); 441 } 442 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); 443 444 static ssize_t regulator_max_uA_show(struct device *dev, 445 struct device_attribute *attr, char *buf) 446 { 447 struct regulator_dev *rdev = dev_get_drvdata(dev); 448 449 if (!rdev->constraints) 450 return sprintf(buf, "constraint not defined\n"); 451 452 return sprintf(buf, "%d\n", rdev->constraints->max_uA); 453 } 454 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); 455 456 static ssize_t regulator_min_uV_show(struct device *dev, 457 struct device_attribute *attr, char *buf) 458 { 459 struct regulator_dev *rdev = dev_get_drvdata(dev); 460 461 if (!rdev->constraints) 462 return sprintf(buf, "constraint not defined\n"); 463 464 return sprintf(buf, "%d\n", rdev->constraints->min_uV); 465 } 466 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); 467 468 static ssize_t regulator_max_uV_show(struct device *dev, 469 struct device_attribute *attr, char *buf) 470 { 471 struct regulator_dev *rdev = dev_get_drvdata(dev); 472 473 if (!rdev->constraints) 474 return sprintf(buf, "constraint not defined\n"); 475 476 return sprintf(buf, "%d\n", rdev->constraints->max_uV); 477 } 478 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); 479 480 static ssize_t regulator_total_uA_show(struct device *dev, 481 struct device_attribute *attr, char *buf) 482 { 483 struct regulator_dev *rdev = dev_get_drvdata(dev); 484 struct regulator *regulator; 485 int uA = 0; 486 487 mutex_lock(&rdev->mutex); 488 list_for_each_entry(regulator, &rdev->consumer_list, list) 489 uA += regulator->uA_load; 490 mutex_unlock(&rdev->mutex); 491 return sprintf(buf, "%d\n", uA); 492 } 493 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); 494 495 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr, 496 char *buf) 497 { 498 struct regulator_dev *rdev = dev_get_drvdata(dev); 499 return sprintf(buf, "%d\n", rdev->use_count); 500 } 501 static DEVICE_ATTR_RO(num_users); 502 503 static ssize_t type_show(struct device *dev, struct device_attribute *attr, 504 char *buf) 505 { 506 struct regulator_dev *rdev = dev_get_drvdata(dev); 507 508 switch (rdev->desc->type) { 509 case REGULATOR_VOLTAGE: 510 return sprintf(buf, "voltage\n"); 511 case REGULATOR_CURRENT: 512 return sprintf(buf, "current\n"); 513 } 514 return sprintf(buf, "unknown\n"); 515 } 516 static DEVICE_ATTR_RO(type); 517 518 static ssize_t regulator_suspend_mem_uV_show(struct device *dev, 519 struct device_attribute *attr, char *buf) 520 { 521 struct regulator_dev *rdev = dev_get_drvdata(dev); 522 523 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); 524 } 525 static DEVICE_ATTR(suspend_mem_microvolts, 0444, 526 regulator_suspend_mem_uV_show, NULL); 527 528 static ssize_t regulator_suspend_disk_uV_show(struct device *dev, 529 struct device_attribute *attr, char *buf) 530 { 531 struct regulator_dev *rdev = dev_get_drvdata(dev); 532 533 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); 534 } 535 static DEVICE_ATTR(suspend_disk_microvolts, 0444, 536 regulator_suspend_disk_uV_show, NULL); 537 538 static ssize_t regulator_suspend_standby_uV_show(struct device *dev, 539 struct device_attribute *attr, char *buf) 540 { 541 struct regulator_dev *rdev = dev_get_drvdata(dev); 542 543 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); 544 } 545 static DEVICE_ATTR(suspend_standby_microvolts, 0444, 546 regulator_suspend_standby_uV_show, NULL); 547 548 static ssize_t regulator_suspend_mem_mode_show(struct device *dev, 549 struct device_attribute *attr, char *buf) 550 { 551 struct regulator_dev *rdev = dev_get_drvdata(dev); 552 553 return regulator_print_opmode(buf, 554 rdev->constraints->state_mem.mode); 555 } 556 static DEVICE_ATTR(suspend_mem_mode, 0444, 557 regulator_suspend_mem_mode_show, NULL); 558 559 static ssize_t regulator_suspend_disk_mode_show(struct device *dev, 560 struct device_attribute *attr, char *buf) 561 { 562 struct regulator_dev *rdev = dev_get_drvdata(dev); 563 564 return regulator_print_opmode(buf, 565 rdev->constraints->state_disk.mode); 566 } 567 static DEVICE_ATTR(suspend_disk_mode, 0444, 568 regulator_suspend_disk_mode_show, NULL); 569 570 static ssize_t regulator_suspend_standby_mode_show(struct device *dev, 571 struct device_attribute *attr, char *buf) 572 { 573 struct regulator_dev *rdev = dev_get_drvdata(dev); 574 575 return regulator_print_opmode(buf, 576 rdev->constraints->state_standby.mode); 577 } 578 static DEVICE_ATTR(suspend_standby_mode, 0444, 579 regulator_suspend_standby_mode_show, NULL); 580 581 static ssize_t regulator_suspend_mem_state_show(struct device *dev, 582 struct device_attribute *attr, char *buf) 583 { 584 struct regulator_dev *rdev = dev_get_drvdata(dev); 585 586 return regulator_print_state(buf, 587 rdev->constraints->state_mem.enabled); 588 } 589 static DEVICE_ATTR(suspend_mem_state, 0444, 590 regulator_suspend_mem_state_show, NULL); 591 592 static ssize_t regulator_suspend_disk_state_show(struct device *dev, 593 struct device_attribute *attr, char *buf) 594 { 595 struct regulator_dev *rdev = dev_get_drvdata(dev); 596 597 return regulator_print_state(buf, 598 rdev->constraints->state_disk.enabled); 599 } 600 static DEVICE_ATTR(suspend_disk_state, 0444, 601 regulator_suspend_disk_state_show, NULL); 602 603 static ssize_t regulator_suspend_standby_state_show(struct device *dev, 604 struct device_attribute *attr, char *buf) 605 { 606 struct regulator_dev *rdev = dev_get_drvdata(dev); 607 608 return regulator_print_state(buf, 609 rdev->constraints->state_standby.enabled); 610 } 611 static DEVICE_ATTR(suspend_standby_state, 0444, 612 regulator_suspend_standby_state_show, NULL); 613 614 static ssize_t regulator_bypass_show(struct device *dev, 615 struct device_attribute *attr, char *buf) 616 { 617 struct regulator_dev *rdev = dev_get_drvdata(dev); 618 const char *report; 619 bool bypass; 620 int ret; 621 622 ret = rdev->desc->ops->get_bypass(rdev, &bypass); 623 624 if (ret != 0) 625 report = "unknown"; 626 else if (bypass) 627 report = "enabled"; 628 else 629 report = "disabled"; 630 631 return sprintf(buf, "%s\n", report); 632 } 633 static DEVICE_ATTR(bypass, 0444, 634 regulator_bypass_show, NULL); 635 636 /* Calculate the new optimum regulator operating mode based on the new total 637 * consumer load. All locks held by caller */ 638 static int drms_uA_update(struct regulator_dev *rdev) 639 { 640 struct regulator *sibling; 641 int current_uA = 0, output_uV, input_uV, err; 642 unsigned int mode; 643 644 /* 645 * first check to see if we can set modes at all, otherwise just 646 * tell the consumer everything is OK. 647 */ 648 err = regulator_check_drms(rdev); 649 if (err < 0) 650 return 0; 651 652 if (!rdev->desc->ops->get_optimum_mode && 653 !rdev->desc->ops->set_load) 654 return 0; 655 656 if (!rdev->desc->ops->set_mode && 657 !rdev->desc->ops->set_load) 658 return -EINVAL; 659 660 /* get output voltage */ 661 output_uV = _regulator_get_voltage(rdev); 662 if (output_uV <= 0) { 663 rdev_err(rdev, "invalid output voltage found\n"); 664 return -EINVAL; 665 } 666 667 /* get input voltage */ 668 input_uV = 0; 669 if (rdev->supply) 670 input_uV = regulator_get_voltage(rdev->supply); 671 if (input_uV <= 0) 672 input_uV = rdev->constraints->input_uV; 673 if (input_uV <= 0) { 674 rdev_err(rdev, "invalid input voltage found\n"); 675 return -EINVAL; 676 } 677 678 /* calc total requested load */ 679 list_for_each_entry(sibling, &rdev->consumer_list, list) 680 current_uA += sibling->uA_load; 681 682 current_uA += rdev->constraints->system_load; 683 684 if (rdev->desc->ops->set_load) { 685 /* set the optimum mode for our new total regulator load */ 686 err = rdev->desc->ops->set_load(rdev, current_uA); 687 if (err < 0) 688 rdev_err(rdev, "failed to set load %d\n", current_uA); 689 } else { 690 /* now get the optimum mode for our new total regulator load */ 691 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, 692 output_uV, current_uA); 693 694 /* check the new mode is allowed */ 695 err = regulator_mode_constrain(rdev, &mode); 696 if (err < 0) { 697 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", 698 current_uA, input_uV, output_uV); 699 return err; 700 } 701 702 err = rdev->desc->ops->set_mode(rdev, mode); 703 if (err < 0) 704 rdev_err(rdev, "failed to set optimum mode %x\n", mode); 705 } 706 707 return err; 708 } 709 710 static int suspend_set_state(struct regulator_dev *rdev, 711 struct regulator_state *rstate) 712 { 713 int ret = 0; 714 715 /* If we have no suspend mode configration don't set anything; 716 * only warn if the driver implements set_suspend_voltage or 717 * set_suspend_mode callback. 718 */ 719 if (!rstate->enabled && !rstate->disabled) { 720 if (rdev->desc->ops->set_suspend_voltage || 721 rdev->desc->ops->set_suspend_mode) 722 rdev_warn(rdev, "No configuration\n"); 723 return 0; 724 } 725 726 if (rstate->enabled && rstate->disabled) { 727 rdev_err(rdev, "invalid configuration\n"); 728 return -EINVAL; 729 } 730 731 if (rstate->enabled && rdev->desc->ops->set_suspend_enable) 732 ret = rdev->desc->ops->set_suspend_enable(rdev); 733 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable) 734 ret = rdev->desc->ops->set_suspend_disable(rdev); 735 else /* OK if set_suspend_enable or set_suspend_disable is NULL */ 736 ret = 0; 737 738 if (ret < 0) { 739 rdev_err(rdev, "failed to enabled/disable\n"); 740 return ret; 741 } 742 743 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { 744 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); 745 if (ret < 0) { 746 rdev_err(rdev, "failed to set voltage\n"); 747 return ret; 748 } 749 } 750 751 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { 752 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); 753 if (ret < 0) { 754 rdev_err(rdev, "failed to set mode\n"); 755 return ret; 756 } 757 } 758 return ret; 759 } 760 761 /* locks held by caller */ 762 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state) 763 { 764 if (!rdev->constraints) 765 return -EINVAL; 766 767 switch (state) { 768 case PM_SUSPEND_STANDBY: 769 return suspend_set_state(rdev, 770 &rdev->constraints->state_standby); 771 case PM_SUSPEND_MEM: 772 return suspend_set_state(rdev, 773 &rdev->constraints->state_mem); 774 case PM_SUSPEND_MAX: 775 return suspend_set_state(rdev, 776 &rdev->constraints->state_disk); 777 default: 778 return -EINVAL; 779 } 780 } 781 782 static void print_constraints(struct regulator_dev *rdev) 783 { 784 struct regulation_constraints *constraints = rdev->constraints; 785 char buf[160] = ""; 786 size_t len = sizeof(buf) - 1; 787 int count = 0; 788 int ret; 789 790 if (constraints->min_uV && constraints->max_uV) { 791 if (constraints->min_uV == constraints->max_uV) 792 count += scnprintf(buf + count, len - count, "%d mV ", 793 constraints->min_uV / 1000); 794 else 795 count += scnprintf(buf + count, len - count, 796 "%d <--> %d mV ", 797 constraints->min_uV / 1000, 798 constraints->max_uV / 1000); 799 } 800 801 if (!constraints->min_uV || 802 constraints->min_uV != constraints->max_uV) { 803 ret = _regulator_get_voltage(rdev); 804 if (ret > 0) 805 count += scnprintf(buf + count, len - count, 806 "at %d mV ", ret / 1000); 807 } 808 809 if (constraints->uV_offset) 810 count += scnprintf(buf + count, len - count, "%dmV offset ", 811 constraints->uV_offset / 1000); 812 813 if (constraints->min_uA && constraints->max_uA) { 814 if (constraints->min_uA == constraints->max_uA) 815 count += scnprintf(buf + count, len - count, "%d mA ", 816 constraints->min_uA / 1000); 817 else 818 count += scnprintf(buf + count, len - count, 819 "%d <--> %d mA ", 820 constraints->min_uA / 1000, 821 constraints->max_uA / 1000); 822 } 823 824 if (!constraints->min_uA || 825 constraints->min_uA != constraints->max_uA) { 826 ret = _regulator_get_current_limit(rdev); 827 if (ret > 0) 828 count += scnprintf(buf + count, len - count, 829 "at %d mA ", ret / 1000); 830 } 831 832 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) 833 count += scnprintf(buf + count, len - count, "fast "); 834 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) 835 count += scnprintf(buf + count, len - count, "normal "); 836 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) 837 count += scnprintf(buf + count, len - count, "idle "); 838 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) 839 count += scnprintf(buf + count, len - count, "standby"); 840 841 if (!count) 842 scnprintf(buf, len, "no parameters"); 843 844 rdev_dbg(rdev, "%s\n", buf); 845 846 if ((constraints->min_uV != constraints->max_uV) && 847 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) 848 rdev_warn(rdev, 849 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n"); 850 } 851 852 static int machine_constraints_voltage(struct regulator_dev *rdev, 853 struct regulation_constraints *constraints) 854 { 855 const struct regulator_ops *ops = rdev->desc->ops; 856 int ret; 857 858 /* do we need to apply the constraint voltage */ 859 if (rdev->constraints->apply_uV && 860 rdev->constraints->min_uV == rdev->constraints->max_uV) { 861 int current_uV = _regulator_get_voltage(rdev); 862 if (current_uV < 0) { 863 rdev_err(rdev, 864 "failed to get the current voltage(%d)\n", 865 current_uV); 866 return current_uV; 867 } 868 if (current_uV < rdev->constraints->min_uV || 869 current_uV > rdev->constraints->max_uV) { 870 ret = _regulator_do_set_voltage( 871 rdev, rdev->constraints->min_uV, 872 rdev->constraints->max_uV); 873 if (ret < 0) { 874 rdev_err(rdev, 875 "failed to apply %duV constraint(%d)\n", 876 rdev->constraints->min_uV, ret); 877 return ret; 878 } 879 } 880 } 881 882 /* constrain machine-level voltage specs to fit 883 * the actual range supported by this regulator. 884 */ 885 if (ops->list_voltage && rdev->desc->n_voltages) { 886 int count = rdev->desc->n_voltages; 887 int i; 888 int min_uV = INT_MAX; 889 int max_uV = INT_MIN; 890 int cmin = constraints->min_uV; 891 int cmax = constraints->max_uV; 892 893 /* it's safe to autoconfigure fixed-voltage supplies 894 and the constraints are used by list_voltage. */ 895 if (count == 1 && !cmin) { 896 cmin = 1; 897 cmax = INT_MAX; 898 constraints->min_uV = cmin; 899 constraints->max_uV = cmax; 900 } 901 902 /* voltage constraints are optional */ 903 if ((cmin == 0) && (cmax == 0)) 904 return 0; 905 906 /* else require explicit machine-level constraints */ 907 if (cmin <= 0 || cmax <= 0 || cmax < cmin) { 908 rdev_err(rdev, "invalid voltage constraints\n"); 909 return -EINVAL; 910 } 911 912 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ 913 for (i = 0; i < count; i++) { 914 int value; 915 916 value = ops->list_voltage(rdev, i); 917 if (value <= 0) 918 continue; 919 920 /* maybe adjust [min_uV..max_uV] */ 921 if (value >= cmin && value < min_uV) 922 min_uV = value; 923 if (value <= cmax && value > max_uV) 924 max_uV = value; 925 } 926 927 /* final: [min_uV..max_uV] valid iff constraints valid */ 928 if (max_uV < min_uV) { 929 rdev_err(rdev, 930 "unsupportable voltage constraints %u-%uuV\n", 931 min_uV, max_uV); 932 return -EINVAL; 933 } 934 935 /* use regulator's subset of machine constraints */ 936 if (constraints->min_uV < min_uV) { 937 rdev_dbg(rdev, "override min_uV, %d -> %d\n", 938 constraints->min_uV, min_uV); 939 constraints->min_uV = min_uV; 940 } 941 if (constraints->max_uV > max_uV) { 942 rdev_dbg(rdev, "override max_uV, %d -> %d\n", 943 constraints->max_uV, max_uV); 944 constraints->max_uV = max_uV; 945 } 946 } 947 948 return 0; 949 } 950 951 static int machine_constraints_current(struct regulator_dev *rdev, 952 struct regulation_constraints *constraints) 953 { 954 const struct regulator_ops *ops = rdev->desc->ops; 955 int ret; 956 957 if (!constraints->min_uA && !constraints->max_uA) 958 return 0; 959 960 if (constraints->min_uA > constraints->max_uA) { 961 rdev_err(rdev, "Invalid current constraints\n"); 962 return -EINVAL; 963 } 964 965 if (!ops->set_current_limit || !ops->get_current_limit) { 966 rdev_warn(rdev, "Operation of current configuration missing\n"); 967 return 0; 968 } 969 970 /* Set regulator current in constraints range */ 971 ret = ops->set_current_limit(rdev, constraints->min_uA, 972 constraints->max_uA); 973 if (ret < 0) { 974 rdev_err(rdev, "Failed to set current constraint, %d\n", ret); 975 return ret; 976 } 977 978 return 0; 979 } 980 981 static int _regulator_do_enable(struct regulator_dev *rdev); 982 983 /** 984 * set_machine_constraints - sets regulator constraints 985 * @rdev: regulator source 986 * @constraints: constraints to apply 987 * 988 * Allows platform initialisation code to define and constrain 989 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: 990 * Constraints *must* be set by platform code in order for some 991 * regulator operations to proceed i.e. set_voltage, set_current_limit, 992 * set_mode. 993 */ 994 static int set_machine_constraints(struct regulator_dev *rdev, 995 const struct regulation_constraints *constraints) 996 { 997 int ret = 0; 998 const struct regulator_ops *ops = rdev->desc->ops; 999 1000 if (constraints) 1001 rdev->constraints = kmemdup(constraints, sizeof(*constraints), 1002 GFP_KERNEL); 1003 else 1004 rdev->constraints = kzalloc(sizeof(*constraints), 1005 GFP_KERNEL); 1006 if (!rdev->constraints) 1007 return -ENOMEM; 1008 1009 ret = machine_constraints_voltage(rdev, rdev->constraints); 1010 if (ret != 0) 1011 goto out; 1012 1013 ret = machine_constraints_current(rdev, rdev->constraints); 1014 if (ret != 0) 1015 goto out; 1016 1017 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) { 1018 ret = ops->set_input_current_limit(rdev, 1019 rdev->constraints->ilim_uA); 1020 if (ret < 0) { 1021 rdev_err(rdev, "failed to set input limit\n"); 1022 goto out; 1023 } 1024 } 1025 1026 /* do we need to setup our suspend state */ 1027 if (rdev->constraints->initial_state) { 1028 ret = suspend_prepare(rdev, rdev->constraints->initial_state); 1029 if (ret < 0) { 1030 rdev_err(rdev, "failed to set suspend state\n"); 1031 goto out; 1032 } 1033 } 1034 1035 if (rdev->constraints->initial_mode) { 1036 if (!ops->set_mode) { 1037 rdev_err(rdev, "no set_mode operation\n"); 1038 ret = -EINVAL; 1039 goto out; 1040 } 1041 1042 ret = ops->set_mode(rdev, rdev->constraints->initial_mode); 1043 if (ret < 0) { 1044 rdev_err(rdev, "failed to set initial mode: %d\n", ret); 1045 goto out; 1046 } 1047 } 1048 1049 /* If the constraints say the regulator should be on at this point 1050 * and we have control then make sure it is enabled. 1051 */ 1052 if (rdev->constraints->always_on || rdev->constraints->boot_on) { 1053 ret = _regulator_do_enable(rdev); 1054 if (ret < 0 && ret != -EINVAL) { 1055 rdev_err(rdev, "failed to enable\n"); 1056 goto out; 1057 } 1058 } 1059 1060 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) 1061 && ops->set_ramp_delay) { 1062 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); 1063 if (ret < 0) { 1064 rdev_err(rdev, "failed to set ramp_delay\n"); 1065 goto out; 1066 } 1067 } 1068 1069 if (rdev->constraints->pull_down && ops->set_pull_down) { 1070 ret = ops->set_pull_down(rdev); 1071 if (ret < 0) { 1072 rdev_err(rdev, "failed to set pull down\n"); 1073 goto out; 1074 } 1075 } 1076 1077 if (rdev->constraints->soft_start && ops->set_soft_start) { 1078 ret = ops->set_soft_start(rdev); 1079 if (ret < 0) { 1080 rdev_err(rdev, "failed to set soft start\n"); 1081 goto out; 1082 } 1083 } 1084 1085 print_constraints(rdev); 1086 return 0; 1087 out: 1088 kfree(rdev->constraints); 1089 rdev->constraints = NULL; 1090 return ret; 1091 } 1092 1093 /** 1094 * set_supply - set regulator supply regulator 1095 * @rdev: regulator name 1096 * @supply_rdev: supply regulator name 1097 * 1098 * Called by platform initialisation code to set the supply regulator for this 1099 * regulator. This ensures that a regulators supply will also be enabled by the 1100 * core if it's child is enabled. 1101 */ 1102 static int set_supply(struct regulator_dev *rdev, 1103 struct regulator_dev *supply_rdev) 1104 { 1105 int err; 1106 1107 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev)); 1108 1109 if (!try_module_get(supply_rdev->owner)) 1110 return -ENODEV; 1111 1112 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); 1113 if (rdev->supply == NULL) { 1114 err = -ENOMEM; 1115 return err; 1116 } 1117 supply_rdev->open_count++; 1118 1119 return 0; 1120 } 1121 1122 /** 1123 * set_consumer_device_supply - Bind a regulator to a symbolic supply 1124 * @rdev: regulator source 1125 * @consumer_dev_name: dev_name() string for device supply applies to 1126 * @supply: symbolic name for supply 1127 * 1128 * Allows platform initialisation code to map physical regulator 1129 * sources to symbolic names for supplies for use by devices. Devices 1130 * should use these symbolic names to request regulators, avoiding the 1131 * need to provide board-specific regulator names as platform data. 1132 */ 1133 static int set_consumer_device_supply(struct regulator_dev *rdev, 1134 const char *consumer_dev_name, 1135 const char *supply) 1136 { 1137 struct regulator_map *node; 1138 int has_dev; 1139 1140 if (supply == NULL) 1141 return -EINVAL; 1142 1143 if (consumer_dev_name != NULL) 1144 has_dev = 1; 1145 else 1146 has_dev = 0; 1147 1148 list_for_each_entry(node, ®ulator_map_list, list) { 1149 if (node->dev_name && consumer_dev_name) { 1150 if (strcmp(node->dev_name, consumer_dev_name) != 0) 1151 continue; 1152 } else if (node->dev_name || consumer_dev_name) { 1153 continue; 1154 } 1155 1156 if (strcmp(node->supply, supply) != 0) 1157 continue; 1158 1159 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n", 1160 consumer_dev_name, 1161 dev_name(&node->regulator->dev), 1162 node->regulator->desc->name, 1163 supply, 1164 dev_name(&rdev->dev), rdev_get_name(rdev)); 1165 return -EBUSY; 1166 } 1167 1168 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); 1169 if (node == NULL) 1170 return -ENOMEM; 1171 1172 node->regulator = rdev; 1173 node->supply = supply; 1174 1175 if (has_dev) { 1176 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); 1177 if (node->dev_name == NULL) { 1178 kfree(node); 1179 return -ENOMEM; 1180 } 1181 } 1182 1183 list_add(&node->list, ®ulator_map_list); 1184 return 0; 1185 } 1186 1187 static void unset_regulator_supplies(struct regulator_dev *rdev) 1188 { 1189 struct regulator_map *node, *n; 1190 1191 list_for_each_entry_safe(node, n, ®ulator_map_list, list) { 1192 if (rdev == node->regulator) { 1193 list_del(&node->list); 1194 kfree(node->dev_name); 1195 kfree(node); 1196 } 1197 } 1198 } 1199 1200 #define REG_STR_SIZE 64 1201 1202 static struct regulator *create_regulator(struct regulator_dev *rdev, 1203 struct device *dev, 1204 const char *supply_name) 1205 { 1206 struct regulator *regulator; 1207 char buf[REG_STR_SIZE]; 1208 int err, size; 1209 1210 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); 1211 if (regulator == NULL) 1212 return NULL; 1213 1214 mutex_lock(&rdev->mutex); 1215 regulator->rdev = rdev; 1216 list_add(®ulator->list, &rdev->consumer_list); 1217 1218 if (dev) { 1219 regulator->dev = dev; 1220 1221 /* Add a link to the device sysfs entry */ 1222 size = scnprintf(buf, REG_STR_SIZE, "%s-%s", 1223 dev->kobj.name, supply_name); 1224 if (size >= REG_STR_SIZE) 1225 goto overflow_err; 1226 1227 regulator->supply_name = kstrdup(buf, GFP_KERNEL); 1228 if (regulator->supply_name == NULL) 1229 goto overflow_err; 1230 1231 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj, 1232 buf); 1233 if (err) { 1234 rdev_dbg(rdev, "could not add device link %s err %d\n", 1235 dev->kobj.name, err); 1236 /* non-fatal */ 1237 } 1238 } else { 1239 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL); 1240 if (regulator->supply_name == NULL) 1241 goto overflow_err; 1242 } 1243 1244 regulator->debugfs = debugfs_create_dir(regulator->supply_name, 1245 rdev->debugfs); 1246 if (!regulator->debugfs) { 1247 rdev_warn(rdev, "Failed to create debugfs directory\n"); 1248 } else { 1249 debugfs_create_u32("uA_load", 0444, regulator->debugfs, 1250 ®ulator->uA_load); 1251 debugfs_create_u32("min_uV", 0444, regulator->debugfs, 1252 ®ulator->min_uV); 1253 debugfs_create_u32("max_uV", 0444, regulator->debugfs, 1254 ®ulator->max_uV); 1255 } 1256 1257 /* 1258 * Check now if the regulator is an always on regulator - if 1259 * it is then we don't need to do nearly so much work for 1260 * enable/disable calls. 1261 */ 1262 if (!_regulator_can_change_status(rdev) && 1263 _regulator_is_enabled(rdev)) 1264 regulator->always_on = true; 1265 1266 mutex_unlock(&rdev->mutex); 1267 return regulator; 1268 overflow_err: 1269 list_del(®ulator->list); 1270 kfree(regulator); 1271 mutex_unlock(&rdev->mutex); 1272 return NULL; 1273 } 1274 1275 static int _regulator_get_enable_time(struct regulator_dev *rdev) 1276 { 1277 if (rdev->constraints && rdev->constraints->enable_time) 1278 return rdev->constraints->enable_time; 1279 if (!rdev->desc->ops->enable_time) 1280 return rdev->desc->enable_time; 1281 return rdev->desc->ops->enable_time(rdev); 1282 } 1283 1284 static struct regulator_supply_alias *regulator_find_supply_alias( 1285 struct device *dev, const char *supply) 1286 { 1287 struct regulator_supply_alias *map; 1288 1289 list_for_each_entry(map, ®ulator_supply_alias_list, list) 1290 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0) 1291 return map; 1292 1293 return NULL; 1294 } 1295 1296 static void regulator_supply_alias(struct device **dev, const char **supply) 1297 { 1298 struct regulator_supply_alias *map; 1299 1300 map = regulator_find_supply_alias(*dev, *supply); 1301 if (map) { 1302 dev_dbg(*dev, "Mapping supply %s to %s,%s\n", 1303 *supply, map->alias_supply, 1304 dev_name(map->alias_dev)); 1305 *dev = map->alias_dev; 1306 *supply = map->alias_supply; 1307 } 1308 } 1309 1310 static struct regulator_dev *regulator_dev_lookup(struct device *dev, 1311 const char *supply, 1312 int *ret) 1313 { 1314 struct regulator_dev *r; 1315 struct device_node *node; 1316 struct regulator_map *map; 1317 const char *devname = NULL; 1318 1319 regulator_supply_alias(&dev, &supply); 1320 1321 /* first do a dt based lookup */ 1322 if (dev && dev->of_node) { 1323 node = of_get_regulator(dev, supply); 1324 if (node) { 1325 list_for_each_entry(r, ®ulator_list, list) 1326 if (r->dev.parent && 1327 node == r->dev.of_node) 1328 return r; 1329 *ret = -EPROBE_DEFER; 1330 return NULL; 1331 } else { 1332 /* 1333 * If we couldn't even get the node then it's 1334 * not just that the device didn't register 1335 * yet, there's no node and we'll never 1336 * succeed. 1337 */ 1338 *ret = -ENODEV; 1339 } 1340 } 1341 1342 /* if not found, try doing it non-dt way */ 1343 if (dev) 1344 devname = dev_name(dev); 1345 1346 list_for_each_entry(r, ®ulator_list, list) 1347 if (strcmp(rdev_get_name(r), supply) == 0) 1348 return r; 1349 1350 list_for_each_entry(map, ®ulator_map_list, list) { 1351 /* If the mapping has a device set up it must match */ 1352 if (map->dev_name && 1353 (!devname || strcmp(map->dev_name, devname))) 1354 continue; 1355 1356 if (strcmp(map->supply, supply) == 0) 1357 return map->regulator; 1358 } 1359 1360 1361 return NULL; 1362 } 1363 1364 static int regulator_resolve_supply(struct regulator_dev *rdev) 1365 { 1366 struct regulator_dev *r; 1367 struct device *dev = rdev->dev.parent; 1368 int ret; 1369 1370 /* No supply to resovle? */ 1371 if (!rdev->supply_name) 1372 return 0; 1373 1374 /* Supply already resolved? */ 1375 if (rdev->supply) 1376 return 0; 1377 1378 r = regulator_dev_lookup(dev, rdev->supply_name, &ret); 1379 if (ret == -ENODEV) { 1380 /* 1381 * No supply was specified for this regulator and 1382 * there will never be one. 1383 */ 1384 return 0; 1385 } 1386 1387 if (!r) { 1388 if (have_full_constraints()) { 1389 r = dummy_regulator_rdev; 1390 } else { 1391 dev_err(dev, "Failed to resolve %s-supply for %s\n", 1392 rdev->supply_name, rdev->desc->name); 1393 return -EPROBE_DEFER; 1394 } 1395 } 1396 1397 /* Recursively resolve the supply of the supply */ 1398 ret = regulator_resolve_supply(r); 1399 if (ret < 0) 1400 return ret; 1401 1402 ret = set_supply(rdev, r); 1403 if (ret < 0) 1404 return ret; 1405 1406 /* Cascade always-on state to supply */ 1407 if (_regulator_is_enabled(rdev)) { 1408 ret = regulator_enable(rdev->supply); 1409 if (ret < 0) { 1410 if (rdev->supply) 1411 _regulator_put(rdev->supply); 1412 return ret; 1413 } 1414 } 1415 1416 return 0; 1417 } 1418 1419 /* Internal regulator request function */ 1420 static struct regulator *_regulator_get(struct device *dev, const char *id, 1421 bool exclusive, bool allow_dummy) 1422 { 1423 struct regulator_dev *rdev; 1424 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER); 1425 const char *devname = NULL; 1426 int ret; 1427 1428 if (id == NULL) { 1429 pr_err("get() with no identifier\n"); 1430 return ERR_PTR(-EINVAL); 1431 } 1432 1433 if (dev) 1434 devname = dev_name(dev); 1435 1436 if (have_full_constraints()) 1437 ret = -ENODEV; 1438 else 1439 ret = -EPROBE_DEFER; 1440 1441 mutex_lock(®ulator_list_mutex); 1442 1443 rdev = regulator_dev_lookup(dev, id, &ret); 1444 if (rdev) 1445 goto found; 1446 1447 regulator = ERR_PTR(ret); 1448 1449 /* 1450 * If we have return value from dev_lookup fail, we do not expect to 1451 * succeed, so, quit with appropriate error value 1452 */ 1453 if (ret && ret != -ENODEV) 1454 goto out; 1455 1456 if (!devname) 1457 devname = "deviceless"; 1458 1459 /* 1460 * Assume that a regulator is physically present and enabled 1461 * even if it isn't hooked up and just provide a dummy. 1462 */ 1463 if (have_full_constraints() && allow_dummy) { 1464 pr_warn("%s supply %s not found, using dummy regulator\n", 1465 devname, id); 1466 1467 rdev = dummy_regulator_rdev; 1468 goto found; 1469 /* Don't log an error when called from regulator_get_optional() */ 1470 } else if (!have_full_constraints() || exclusive) { 1471 dev_warn(dev, "dummy supplies not allowed\n"); 1472 } 1473 1474 mutex_unlock(®ulator_list_mutex); 1475 return regulator; 1476 1477 found: 1478 if (rdev->exclusive) { 1479 regulator = ERR_PTR(-EPERM); 1480 goto out; 1481 } 1482 1483 if (exclusive && rdev->open_count) { 1484 regulator = ERR_PTR(-EBUSY); 1485 goto out; 1486 } 1487 1488 ret = regulator_resolve_supply(rdev); 1489 if (ret < 0) { 1490 regulator = ERR_PTR(ret); 1491 goto out; 1492 } 1493 1494 if (!try_module_get(rdev->owner)) 1495 goto out; 1496 1497 regulator = create_regulator(rdev, dev, id); 1498 if (regulator == NULL) { 1499 regulator = ERR_PTR(-ENOMEM); 1500 module_put(rdev->owner); 1501 goto out; 1502 } 1503 1504 rdev->open_count++; 1505 if (exclusive) { 1506 rdev->exclusive = 1; 1507 1508 ret = _regulator_is_enabled(rdev); 1509 if (ret > 0) 1510 rdev->use_count = 1; 1511 else 1512 rdev->use_count = 0; 1513 } 1514 1515 out: 1516 mutex_unlock(®ulator_list_mutex); 1517 1518 return regulator; 1519 } 1520 1521 /** 1522 * regulator_get - lookup and obtain a reference to a regulator. 1523 * @dev: device for regulator "consumer" 1524 * @id: Supply name or regulator ID. 1525 * 1526 * Returns a struct regulator corresponding to the regulator producer, 1527 * or IS_ERR() condition containing errno. 1528 * 1529 * Use of supply names configured via regulator_set_device_supply() is 1530 * strongly encouraged. It is recommended that the supply name used 1531 * should match the name used for the supply and/or the relevant 1532 * device pins in the datasheet. 1533 */ 1534 struct regulator *regulator_get(struct device *dev, const char *id) 1535 { 1536 return _regulator_get(dev, id, false, true); 1537 } 1538 EXPORT_SYMBOL_GPL(regulator_get); 1539 1540 /** 1541 * regulator_get_exclusive - obtain exclusive access to a regulator. 1542 * @dev: device for regulator "consumer" 1543 * @id: Supply name or regulator ID. 1544 * 1545 * Returns a struct regulator corresponding to the regulator producer, 1546 * or IS_ERR() condition containing errno. Other consumers will be 1547 * unable to obtain this regulator while this reference is held and the 1548 * use count for the regulator will be initialised to reflect the current 1549 * state of the regulator. 1550 * 1551 * This is intended for use by consumers which cannot tolerate shared 1552 * use of the regulator such as those which need to force the 1553 * regulator off for correct operation of the hardware they are 1554 * controlling. 1555 * 1556 * Use of supply names configured via regulator_set_device_supply() is 1557 * strongly encouraged. It is recommended that the supply name used 1558 * should match the name used for the supply and/or the relevant 1559 * device pins in the datasheet. 1560 */ 1561 struct regulator *regulator_get_exclusive(struct device *dev, const char *id) 1562 { 1563 return _regulator_get(dev, id, true, false); 1564 } 1565 EXPORT_SYMBOL_GPL(regulator_get_exclusive); 1566 1567 /** 1568 * regulator_get_optional - obtain optional access to a regulator. 1569 * @dev: device for regulator "consumer" 1570 * @id: Supply name or regulator ID. 1571 * 1572 * Returns a struct regulator corresponding to the regulator producer, 1573 * or IS_ERR() condition containing errno. 1574 * 1575 * This is intended for use by consumers for devices which can have 1576 * some supplies unconnected in normal use, such as some MMC devices. 1577 * It can allow the regulator core to provide stub supplies for other 1578 * supplies requested using normal regulator_get() calls without 1579 * disrupting the operation of drivers that can handle absent 1580 * supplies. 1581 * 1582 * Use of supply names configured via regulator_set_device_supply() is 1583 * strongly encouraged. It is recommended that the supply name used 1584 * should match the name used for the supply and/or the relevant 1585 * device pins in the datasheet. 1586 */ 1587 struct regulator *regulator_get_optional(struct device *dev, const char *id) 1588 { 1589 return _regulator_get(dev, id, false, false); 1590 } 1591 EXPORT_SYMBOL_GPL(regulator_get_optional); 1592 1593 /* regulator_list_mutex lock held by regulator_put() */ 1594 static void _regulator_put(struct regulator *regulator) 1595 { 1596 struct regulator_dev *rdev; 1597 1598 if (regulator == NULL || IS_ERR(regulator)) 1599 return; 1600 1601 rdev = regulator->rdev; 1602 1603 debugfs_remove_recursive(regulator->debugfs); 1604 1605 /* remove any sysfs entries */ 1606 if (regulator->dev) 1607 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); 1608 mutex_lock(&rdev->mutex); 1609 kfree(regulator->supply_name); 1610 list_del(®ulator->list); 1611 kfree(regulator); 1612 1613 rdev->open_count--; 1614 rdev->exclusive = 0; 1615 mutex_unlock(&rdev->mutex); 1616 1617 module_put(rdev->owner); 1618 } 1619 1620 /** 1621 * regulator_put - "free" the regulator source 1622 * @regulator: regulator source 1623 * 1624 * Note: drivers must ensure that all regulator_enable calls made on this 1625 * regulator source are balanced by regulator_disable calls prior to calling 1626 * this function. 1627 */ 1628 void regulator_put(struct regulator *regulator) 1629 { 1630 mutex_lock(®ulator_list_mutex); 1631 _regulator_put(regulator); 1632 mutex_unlock(®ulator_list_mutex); 1633 } 1634 EXPORT_SYMBOL_GPL(regulator_put); 1635 1636 /** 1637 * regulator_register_supply_alias - Provide device alias for supply lookup 1638 * 1639 * @dev: device that will be given as the regulator "consumer" 1640 * @id: Supply name or regulator ID 1641 * @alias_dev: device that should be used to lookup the supply 1642 * @alias_id: Supply name or regulator ID that should be used to lookup the 1643 * supply 1644 * 1645 * All lookups for id on dev will instead be conducted for alias_id on 1646 * alias_dev. 1647 */ 1648 int regulator_register_supply_alias(struct device *dev, const char *id, 1649 struct device *alias_dev, 1650 const char *alias_id) 1651 { 1652 struct regulator_supply_alias *map; 1653 1654 map = regulator_find_supply_alias(dev, id); 1655 if (map) 1656 return -EEXIST; 1657 1658 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL); 1659 if (!map) 1660 return -ENOMEM; 1661 1662 map->src_dev = dev; 1663 map->src_supply = id; 1664 map->alias_dev = alias_dev; 1665 map->alias_supply = alias_id; 1666 1667 list_add(&map->list, ®ulator_supply_alias_list); 1668 1669 pr_info("Adding alias for supply %s,%s -> %s,%s\n", 1670 id, dev_name(dev), alias_id, dev_name(alias_dev)); 1671 1672 return 0; 1673 } 1674 EXPORT_SYMBOL_GPL(regulator_register_supply_alias); 1675 1676 /** 1677 * regulator_unregister_supply_alias - Remove device alias 1678 * 1679 * @dev: device that will be given as the regulator "consumer" 1680 * @id: Supply name or regulator ID 1681 * 1682 * Remove a lookup alias if one exists for id on dev. 1683 */ 1684 void regulator_unregister_supply_alias(struct device *dev, const char *id) 1685 { 1686 struct regulator_supply_alias *map; 1687 1688 map = regulator_find_supply_alias(dev, id); 1689 if (map) { 1690 list_del(&map->list); 1691 kfree(map); 1692 } 1693 } 1694 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias); 1695 1696 /** 1697 * regulator_bulk_register_supply_alias - register multiple aliases 1698 * 1699 * @dev: device that will be given as the regulator "consumer" 1700 * @id: List of supply names or regulator IDs 1701 * @alias_dev: device that should be used to lookup the supply 1702 * @alias_id: List of supply names or regulator IDs that should be used to 1703 * lookup the supply 1704 * @num_id: Number of aliases to register 1705 * 1706 * @return 0 on success, an errno on failure. 1707 * 1708 * This helper function allows drivers to register several supply 1709 * aliases in one operation. If any of the aliases cannot be 1710 * registered any aliases that were registered will be removed 1711 * before returning to the caller. 1712 */ 1713 int regulator_bulk_register_supply_alias(struct device *dev, 1714 const char *const *id, 1715 struct device *alias_dev, 1716 const char *const *alias_id, 1717 int num_id) 1718 { 1719 int i; 1720 int ret; 1721 1722 for (i = 0; i < num_id; ++i) { 1723 ret = regulator_register_supply_alias(dev, id[i], alias_dev, 1724 alias_id[i]); 1725 if (ret < 0) 1726 goto err; 1727 } 1728 1729 return 0; 1730 1731 err: 1732 dev_err(dev, 1733 "Failed to create supply alias %s,%s -> %s,%s\n", 1734 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev)); 1735 1736 while (--i >= 0) 1737 regulator_unregister_supply_alias(dev, id[i]); 1738 1739 return ret; 1740 } 1741 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias); 1742 1743 /** 1744 * regulator_bulk_unregister_supply_alias - unregister multiple aliases 1745 * 1746 * @dev: device that will be given as the regulator "consumer" 1747 * @id: List of supply names or regulator IDs 1748 * @num_id: Number of aliases to unregister 1749 * 1750 * This helper function allows drivers to unregister several supply 1751 * aliases in one operation. 1752 */ 1753 void regulator_bulk_unregister_supply_alias(struct device *dev, 1754 const char *const *id, 1755 int num_id) 1756 { 1757 int i; 1758 1759 for (i = 0; i < num_id; ++i) 1760 regulator_unregister_supply_alias(dev, id[i]); 1761 } 1762 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias); 1763 1764 1765 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */ 1766 static int regulator_ena_gpio_request(struct regulator_dev *rdev, 1767 const struct regulator_config *config) 1768 { 1769 struct regulator_enable_gpio *pin; 1770 struct gpio_desc *gpiod; 1771 int ret; 1772 1773 gpiod = gpio_to_desc(config->ena_gpio); 1774 1775 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) { 1776 if (pin->gpiod == gpiod) { 1777 rdev_dbg(rdev, "GPIO %d is already used\n", 1778 config->ena_gpio); 1779 goto update_ena_gpio_to_rdev; 1780 } 1781 } 1782 1783 ret = gpio_request_one(config->ena_gpio, 1784 GPIOF_DIR_OUT | config->ena_gpio_flags, 1785 rdev_get_name(rdev)); 1786 if (ret) 1787 return ret; 1788 1789 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL); 1790 if (pin == NULL) { 1791 gpio_free(config->ena_gpio); 1792 return -ENOMEM; 1793 } 1794 1795 pin->gpiod = gpiod; 1796 pin->ena_gpio_invert = config->ena_gpio_invert; 1797 list_add(&pin->list, ®ulator_ena_gpio_list); 1798 1799 update_ena_gpio_to_rdev: 1800 pin->request_count++; 1801 rdev->ena_pin = pin; 1802 return 0; 1803 } 1804 1805 static void regulator_ena_gpio_free(struct regulator_dev *rdev) 1806 { 1807 struct regulator_enable_gpio *pin, *n; 1808 1809 if (!rdev->ena_pin) 1810 return; 1811 1812 /* Free the GPIO only in case of no use */ 1813 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) { 1814 if (pin->gpiod == rdev->ena_pin->gpiod) { 1815 if (pin->request_count <= 1) { 1816 pin->request_count = 0; 1817 gpiod_put(pin->gpiod); 1818 list_del(&pin->list); 1819 kfree(pin); 1820 rdev->ena_pin = NULL; 1821 return; 1822 } else { 1823 pin->request_count--; 1824 } 1825 } 1826 } 1827 } 1828 1829 /** 1830 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control 1831 * @rdev: regulator_dev structure 1832 * @enable: enable GPIO at initial use? 1833 * 1834 * GPIO is enabled in case of initial use. (enable_count is 0) 1835 * GPIO is disabled when it is not shared any more. (enable_count <= 1) 1836 */ 1837 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable) 1838 { 1839 struct regulator_enable_gpio *pin = rdev->ena_pin; 1840 1841 if (!pin) 1842 return -EINVAL; 1843 1844 if (enable) { 1845 /* Enable GPIO at initial use */ 1846 if (pin->enable_count == 0) 1847 gpiod_set_value_cansleep(pin->gpiod, 1848 !pin->ena_gpio_invert); 1849 1850 pin->enable_count++; 1851 } else { 1852 if (pin->enable_count > 1) { 1853 pin->enable_count--; 1854 return 0; 1855 } 1856 1857 /* Disable GPIO if not used */ 1858 if (pin->enable_count <= 1) { 1859 gpiod_set_value_cansleep(pin->gpiod, 1860 pin->ena_gpio_invert); 1861 pin->enable_count = 0; 1862 } 1863 } 1864 1865 return 0; 1866 } 1867 1868 /** 1869 * _regulator_enable_delay - a delay helper function 1870 * @delay: time to delay in microseconds 1871 * 1872 * Delay for the requested amount of time as per the guidelines in: 1873 * 1874 * Documentation/timers/timers-howto.txt 1875 * 1876 * The assumption here is that regulators will never be enabled in 1877 * atomic context and therefore sleeping functions can be used. 1878 */ 1879 static void _regulator_enable_delay(unsigned int delay) 1880 { 1881 unsigned int ms = delay / 1000; 1882 unsigned int us = delay % 1000; 1883 1884 if (ms > 0) { 1885 /* 1886 * For small enough values, handle super-millisecond 1887 * delays in the usleep_range() call below. 1888 */ 1889 if (ms < 20) 1890 us += ms * 1000; 1891 else 1892 msleep(ms); 1893 } 1894 1895 /* 1896 * Give the scheduler some room to coalesce with any other 1897 * wakeup sources. For delays shorter than 10 us, don't even 1898 * bother setting up high-resolution timers and just busy- 1899 * loop. 1900 */ 1901 if (us >= 10) 1902 usleep_range(us, us + 100); 1903 else 1904 udelay(us); 1905 } 1906 1907 static int _regulator_do_enable(struct regulator_dev *rdev) 1908 { 1909 int ret, delay; 1910 1911 /* Query before enabling in case configuration dependent. */ 1912 ret = _regulator_get_enable_time(rdev); 1913 if (ret >= 0) { 1914 delay = ret; 1915 } else { 1916 rdev_warn(rdev, "enable_time() failed: %d\n", ret); 1917 delay = 0; 1918 } 1919 1920 trace_regulator_enable(rdev_get_name(rdev)); 1921 1922 if (rdev->desc->off_on_delay) { 1923 /* if needed, keep a distance of off_on_delay from last time 1924 * this regulator was disabled. 1925 */ 1926 unsigned long start_jiffy = jiffies; 1927 unsigned long intended, max_delay, remaining; 1928 1929 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay); 1930 intended = rdev->last_off_jiffy + max_delay; 1931 1932 if (time_before(start_jiffy, intended)) { 1933 /* calc remaining jiffies to deal with one-time 1934 * timer wrapping. 1935 * in case of multiple timer wrapping, either it can be 1936 * detected by out-of-range remaining, or it cannot be 1937 * detected and we gets a panelty of 1938 * _regulator_enable_delay(). 1939 */ 1940 remaining = intended - start_jiffy; 1941 if (remaining <= max_delay) 1942 _regulator_enable_delay( 1943 jiffies_to_usecs(remaining)); 1944 } 1945 } 1946 1947 if (rdev->ena_pin) { 1948 if (!rdev->ena_gpio_state) { 1949 ret = regulator_ena_gpio_ctrl(rdev, true); 1950 if (ret < 0) 1951 return ret; 1952 rdev->ena_gpio_state = 1; 1953 } 1954 } else if (rdev->desc->ops->enable) { 1955 ret = rdev->desc->ops->enable(rdev); 1956 if (ret < 0) 1957 return ret; 1958 } else { 1959 return -EINVAL; 1960 } 1961 1962 /* Allow the regulator to ramp; it would be useful to extend 1963 * this for bulk operations so that the regulators can ramp 1964 * together. */ 1965 trace_regulator_enable_delay(rdev_get_name(rdev)); 1966 1967 _regulator_enable_delay(delay); 1968 1969 trace_regulator_enable_complete(rdev_get_name(rdev)); 1970 1971 return 0; 1972 } 1973 1974 /* locks held by regulator_enable() */ 1975 static int _regulator_enable(struct regulator_dev *rdev) 1976 { 1977 int ret; 1978 1979 /* check voltage and requested load before enabling */ 1980 if (rdev->constraints && 1981 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) 1982 drms_uA_update(rdev); 1983 1984 if (rdev->use_count == 0) { 1985 /* The regulator may on if it's not switchable or left on */ 1986 ret = _regulator_is_enabled(rdev); 1987 if (ret == -EINVAL || ret == 0) { 1988 if (!_regulator_can_change_status(rdev)) 1989 return -EPERM; 1990 1991 ret = _regulator_do_enable(rdev); 1992 if (ret < 0) 1993 return ret; 1994 1995 } else if (ret < 0) { 1996 rdev_err(rdev, "is_enabled() failed: %d\n", ret); 1997 return ret; 1998 } 1999 /* Fallthrough on positive return values - already enabled */ 2000 } 2001 2002 rdev->use_count++; 2003 2004 return 0; 2005 } 2006 2007 /** 2008 * regulator_enable - enable regulator output 2009 * @regulator: regulator source 2010 * 2011 * Request that the regulator be enabled with the regulator output at 2012 * the predefined voltage or current value. Calls to regulator_enable() 2013 * must be balanced with calls to regulator_disable(). 2014 * 2015 * NOTE: the output value can be set by other drivers, boot loader or may be 2016 * hardwired in the regulator. 2017 */ 2018 int regulator_enable(struct regulator *regulator) 2019 { 2020 struct regulator_dev *rdev = regulator->rdev; 2021 int ret = 0; 2022 2023 if (regulator->always_on) 2024 return 0; 2025 2026 if (rdev->supply) { 2027 ret = regulator_enable(rdev->supply); 2028 if (ret != 0) 2029 return ret; 2030 } 2031 2032 mutex_lock(&rdev->mutex); 2033 ret = _regulator_enable(rdev); 2034 mutex_unlock(&rdev->mutex); 2035 2036 if (ret != 0 && rdev->supply) 2037 regulator_disable(rdev->supply); 2038 2039 return ret; 2040 } 2041 EXPORT_SYMBOL_GPL(regulator_enable); 2042 2043 static int _regulator_do_disable(struct regulator_dev *rdev) 2044 { 2045 int ret; 2046 2047 trace_regulator_disable(rdev_get_name(rdev)); 2048 2049 if (rdev->ena_pin) { 2050 if (rdev->ena_gpio_state) { 2051 ret = regulator_ena_gpio_ctrl(rdev, false); 2052 if (ret < 0) 2053 return ret; 2054 rdev->ena_gpio_state = 0; 2055 } 2056 2057 } else if (rdev->desc->ops->disable) { 2058 ret = rdev->desc->ops->disable(rdev); 2059 if (ret != 0) 2060 return ret; 2061 } 2062 2063 /* cares about last_off_jiffy only if off_on_delay is required by 2064 * device. 2065 */ 2066 if (rdev->desc->off_on_delay) 2067 rdev->last_off_jiffy = jiffies; 2068 2069 trace_regulator_disable_complete(rdev_get_name(rdev)); 2070 2071 return 0; 2072 } 2073 2074 /* locks held by regulator_disable() */ 2075 static int _regulator_disable(struct regulator_dev *rdev) 2076 { 2077 int ret = 0; 2078 2079 if (WARN(rdev->use_count <= 0, 2080 "unbalanced disables for %s\n", rdev_get_name(rdev))) 2081 return -EIO; 2082 2083 /* are we the last user and permitted to disable ? */ 2084 if (rdev->use_count == 1 && 2085 (rdev->constraints && !rdev->constraints->always_on)) { 2086 2087 /* we are last user */ 2088 if (_regulator_can_change_status(rdev)) { 2089 ret = _notifier_call_chain(rdev, 2090 REGULATOR_EVENT_PRE_DISABLE, 2091 NULL); 2092 if (ret & NOTIFY_STOP_MASK) 2093 return -EINVAL; 2094 2095 ret = _regulator_do_disable(rdev); 2096 if (ret < 0) { 2097 rdev_err(rdev, "failed to disable\n"); 2098 _notifier_call_chain(rdev, 2099 REGULATOR_EVENT_ABORT_DISABLE, 2100 NULL); 2101 return ret; 2102 } 2103 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, 2104 NULL); 2105 } 2106 2107 rdev->use_count = 0; 2108 } else if (rdev->use_count > 1) { 2109 2110 if (rdev->constraints && 2111 (rdev->constraints->valid_ops_mask & 2112 REGULATOR_CHANGE_DRMS)) 2113 drms_uA_update(rdev); 2114 2115 rdev->use_count--; 2116 } 2117 2118 return ret; 2119 } 2120 2121 /** 2122 * regulator_disable - disable regulator output 2123 * @regulator: regulator source 2124 * 2125 * Disable the regulator output voltage or current. Calls to 2126 * regulator_enable() must be balanced with calls to 2127 * regulator_disable(). 2128 * 2129 * NOTE: this will only disable the regulator output if no other consumer 2130 * devices have it enabled, the regulator device supports disabling and 2131 * machine constraints permit this operation. 2132 */ 2133 int regulator_disable(struct regulator *regulator) 2134 { 2135 struct regulator_dev *rdev = regulator->rdev; 2136 int ret = 0; 2137 2138 if (regulator->always_on) 2139 return 0; 2140 2141 mutex_lock(&rdev->mutex); 2142 ret = _regulator_disable(rdev); 2143 mutex_unlock(&rdev->mutex); 2144 2145 if (ret == 0 && rdev->supply) 2146 regulator_disable(rdev->supply); 2147 2148 return ret; 2149 } 2150 EXPORT_SYMBOL_GPL(regulator_disable); 2151 2152 /* locks held by regulator_force_disable() */ 2153 static int _regulator_force_disable(struct regulator_dev *rdev) 2154 { 2155 int ret = 0; 2156 2157 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | 2158 REGULATOR_EVENT_PRE_DISABLE, NULL); 2159 if (ret & NOTIFY_STOP_MASK) 2160 return -EINVAL; 2161 2162 ret = _regulator_do_disable(rdev); 2163 if (ret < 0) { 2164 rdev_err(rdev, "failed to force disable\n"); 2165 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | 2166 REGULATOR_EVENT_ABORT_DISABLE, NULL); 2167 return ret; 2168 } 2169 2170 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | 2171 REGULATOR_EVENT_DISABLE, NULL); 2172 2173 return 0; 2174 } 2175 2176 /** 2177 * regulator_force_disable - force disable regulator output 2178 * @regulator: regulator source 2179 * 2180 * Forcibly disable the regulator output voltage or current. 2181 * NOTE: this *will* disable the regulator output even if other consumer 2182 * devices have it enabled. This should be used for situations when device 2183 * damage will likely occur if the regulator is not disabled (e.g. over temp). 2184 */ 2185 int regulator_force_disable(struct regulator *regulator) 2186 { 2187 struct regulator_dev *rdev = regulator->rdev; 2188 int ret; 2189 2190 mutex_lock(&rdev->mutex); 2191 regulator->uA_load = 0; 2192 ret = _regulator_force_disable(regulator->rdev); 2193 mutex_unlock(&rdev->mutex); 2194 2195 if (rdev->supply) 2196 while (rdev->open_count--) 2197 regulator_disable(rdev->supply); 2198 2199 return ret; 2200 } 2201 EXPORT_SYMBOL_GPL(regulator_force_disable); 2202 2203 static void regulator_disable_work(struct work_struct *work) 2204 { 2205 struct regulator_dev *rdev = container_of(work, struct regulator_dev, 2206 disable_work.work); 2207 int count, i, ret; 2208 2209 mutex_lock(&rdev->mutex); 2210 2211 BUG_ON(!rdev->deferred_disables); 2212 2213 count = rdev->deferred_disables; 2214 rdev->deferred_disables = 0; 2215 2216 for (i = 0; i < count; i++) { 2217 ret = _regulator_disable(rdev); 2218 if (ret != 0) 2219 rdev_err(rdev, "Deferred disable failed: %d\n", ret); 2220 } 2221 2222 mutex_unlock(&rdev->mutex); 2223 2224 if (rdev->supply) { 2225 for (i = 0; i < count; i++) { 2226 ret = regulator_disable(rdev->supply); 2227 if (ret != 0) { 2228 rdev_err(rdev, 2229 "Supply disable failed: %d\n", ret); 2230 } 2231 } 2232 } 2233 } 2234 2235 /** 2236 * regulator_disable_deferred - disable regulator output with delay 2237 * @regulator: regulator source 2238 * @ms: miliseconds until the regulator is disabled 2239 * 2240 * Execute regulator_disable() on the regulator after a delay. This 2241 * is intended for use with devices that require some time to quiesce. 2242 * 2243 * NOTE: this will only disable the regulator output if no other consumer 2244 * devices have it enabled, the regulator device supports disabling and 2245 * machine constraints permit this operation. 2246 */ 2247 int regulator_disable_deferred(struct regulator *regulator, int ms) 2248 { 2249 struct regulator_dev *rdev = regulator->rdev; 2250 int ret; 2251 2252 if (regulator->always_on) 2253 return 0; 2254 2255 if (!ms) 2256 return regulator_disable(regulator); 2257 2258 mutex_lock(&rdev->mutex); 2259 rdev->deferred_disables++; 2260 mutex_unlock(&rdev->mutex); 2261 2262 ret = queue_delayed_work(system_power_efficient_wq, 2263 &rdev->disable_work, 2264 msecs_to_jiffies(ms)); 2265 if (ret < 0) 2266 return ret; 2267 else 2268 return 0; 2269 } 2270 EXPORT_SYMBOL_GPL(regulator_disable_deferred); 2271 2272 static int _regulator_is_enabled(struct regulator_dev *rdev) 2273 { 2274 /* A GPIO control always takes precedence */ 2275 if (rdev->ena_pin) 2276 return rdev->ena_gpio_state; 2277 2278 /* If we don't know then assume that the regulator is always on */ 2279 if (!rdev->desc->ops->is_enabled) 2280 return 1; 2281 2282 return rdev->desc->ops->is_enabled(rdev); 2283 } 2284 2285 /** 2286 * regulator_is_enabled - is the regulator output enabled 2287 * @regulator: regulator source 2288 * 2289 * Returns positive if the regulator driver backing the source/client 2290 * has requested that the device be enabled, zero if it hasn't, else a 2291 * negative errno code. 2292 * 2293 * Note that the device backing this regulator handle can have multiple 2294 * users, so it might be enabled even if regulator_enable() was never 2295 * called for this particular source. 2296 */ 2297 int regulator_is_enabled(struct regulator *regulator) 2298 { 2299 int ret; 2300 2301 if (regulator->always_on) 2302 return 1; 2303 2304 mutex_lock(®ulator->rdev->mutex); 2305 ret = _regulator_is_enabled(regulator->rdev); 2306 mutex_unlock(®ulator->rdev->mutex); 2307 2308 return ret; 2309 } 2310 EXPORT_SYMBOL_GPL(regulator_is_enabled); 2311 2312 /** 2313 * regulator_can_change_voltage - check if regulator can change voltage 2314 * @regulator: regulator source 2315 * 2316 * Returns positive if the regulator driver backing the source/client 2317 * can change its voltage, false otherwise. Useful for detecting fixed 2318 * or dummy regulators and disabling voltage change logic in the client 2319 * driver. 2320 */ 2321 int regulator_can_change_voltage(struct regulator *regulator) 2322 { 2323 struct regulator_dev *rdev = regulator->rdev; 2324 2325 if (rdev->constraints && 2326 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { 2327 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1) 2328 return 1; 2329 2330 if (rdev->desc->continuous_voltage_range && 2331 rdev->constraints->min_uV && rdev->constraints->max_uV && 2332 rdev->constraints->min_uV != rdev->constraints->max_uV) 2333 return 1; 2334 } 2335 2336 return 0; 2337 } 2338 EXPORT_SYMBOL_GPL(regulator_can_change_voltage); 2339 2340 /** 2341 * regulator_count_voltages - count regulator_list_voltage() selectors 2342 * @regulator: regulator source 2343 * 2344 * Returns number of selectors, or negative errno. Selectors are 2345 * numbered starting at zero, and typically correspond to bitfields 2346 * in hardware registers. 2347 */ 2348 int regulator_count_voltages(struct regulator *regulator) 2349 { 2350 struct regulator_dev *rdev = regulator->rdev; 2351 2352 if (rdev->desc->n_voltages) 2353 return rdev->desc->n_voltages; 2354 2355 if (!rdev->supply) 2356 return -EINVAL; 2357 2358 return regulator_count_voltages(rdev->supply); 2359 } 2360 EXPORT_SYMBOL_GPL(regulator_count_voltages); 2361 2362 /** 2363 * regulator_list_voltage - enumerate supported voltages 2364 * @regulator: regulator source 2365 * @selector: identify voltage to list 2366 * Context: can sleep 2367 * 2368 * Returns a voltage that can be passed to @regulator_set_voltage(), 2369 * zero if this selector code can't be used on this system, or a 2370 * negative errno. 2371 */ 2372 int regulator_list_voltage(struct regulator *regulator, unsigned selector) 2373 { 2374 struct regulator_dev *rdev = regulator->rdev; 2375 const struct regulator_ops *ops = rdev->desc->ops; 2376 int ret; 2377 2378 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) 2379 return rdev->desc->fixed_uV; 2380 2381 if (ops->list_voltage) { 2382 if (selector >= rdev->desc->n_voltages) 2383 return -EINVAL; 2384 mutex_lock(&rdev->mutex); 2385 ret = ops->list_voltage(rdev, selector); 2386 mutex_unlock(&rdev->mutex); 2387 } else if (rdev->supply) { 2388 ret = regulator_list_voltage(rdev->supply, selector); 2389 } else { 2390 return -EINVAL; 2391 } 2392 2393 if (ret > 0) { 2394 if (ret < rdev->constraints->min_uV) 2395 ret = 0; 2396 else if (ret > rdev->constraints->max_uV) 2397 ret = 0; 2398 } 2399 2400 return ret; 2401 } 2402 EXPORT_SYMBOL_GPL(regulator_list_voltage); 2403 2404 /** 2405 * regulator_get_regmap - get the regulator's register map 2406 * @regulator: regulator source 2407 * 2408 * Returns the register map for the given regulator, or an ERR_PTR value 2409 * if the regulator doesn't use regmap. 2410 */ 2411 struct regmap *regulator_get_regmap(struct regulator *regulator) 2412 { 2413 struct regmap *map = regulator->rdev->regmap; 2414 2415 return map ? map : ERR_PTR(-EOPNOTSUPP); 2416 } 2417 2418 /** 2419 * regulator_get_hardware_vsel_register - get the HW voltage selector register 2420 * @regulator: regulator source 2421 * @vsel_reg: voltage selector register, output parameter 2422 * @vsel_mask: mask for voltage selector bitfield, output parameter 2423 * 2424 * Returns the hardware register offset and bitmask used for setting the 2425 * regulator voltage. This might be useful when configuring voltage-scaling 2426 * hardware or firmware that can make I2C requests behind the kernel's back, 2427 * for example. 2428 * 2429 * On success, the output parameters @vsel_reg and @vsel_mask are filled in 2430 * and 0 is returned, otherwise a negative errno is returned. 2431 */ 2432 int regulator_get_hardware_vsel_register(struct regulator *regulator, 2433 unsigned *vsel_reg, 2434 unsigned *vsel_mask) 2435 { 2436 struct regulator_dev *rdev = regulator->rdev; 2437 const struct regulator_ops *ops = rdev->desc->ops; 2438 2439 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) 2440 return -EOPNOTSUPP; 2441 2442 *vsel_reg = rdev->desc->vsel_reg; 2443 *vsel_mask = rdev->desc->vsel_mask; 2444 2445 return 0; 2446 } 2447 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register); 2448 2449 /** 2450 * regulator_list_hardware_vsel - get the HW-specific register value for a selector 2451 * @regulator: regulator source 2452 * @selector: identify voltage to list 2453 * 2454 * Converts the selector to a hardware-specific voltage selector that can be 2455 * directly written to the regulator registers. The address of the voltage 2456 * register can be determined by calling @regulator_get_hardware_vsel_register. 2457 * 2458 * On error a negative errno is returned. 2459 */ 2460 int regulator_list_hardware_vsel(struct regulator *regulator, 2461 unsigned selector) 2462 { 2463 struct regulator_dev *rdev = regulator->rdev; 2464 const struct regulator_ops *ops = rdev->desc->ops; 2465 2466 if (selector >= rdev->desc->n_voltages) 2467 return -EINVAL; 2468 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) 2469 return -EOPNOTSUPP; 2470 2471 return selector; 2472 } 2473 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel); 2474 2475 /** 2476 * regulator_get_linear_step - return the voltage step size between VSEL values 2477 * @regulator: regulator source 2478 * 2479 * Returns the voltage step size between VSEL values for linear 2480 * regulators, or return 0 if the regulator isn't a linear regulator. 2481 */ 2482 unsigned int regulator_get_linear_step(struct regulator *regulator) 2483 { 2484 struct regulator_dev *rdev = regulator->rdev; 2485 2486 return rdev->desc->uV_step; 2487 } 2488 EXPORT_SYMBOL_GPL(regulator_get_linear_step); 2489 2490 /** 2491 * regulator_is_supported_voltage - check if a voltage range can be supported 2492 * 2493 * @regulator: Regulator to check. 2494 * @min_uV: Minimum required voltage in uV. 2495 * @max_uV: Maximum required voltage in uV. 2496 * 2497 * Returns a boolean or a negative error code. 2498 */ 2499 int regulator_is_supported_voltage(struct regulator *regulator, 2500 int min_uV, int max_uV) 2501 { 2502 struct regulator_dev *rdev = regulator->rdev; 2503 int i, voltages, ret; 2504 2505 /* If we can't change voltage check the current voltage */ 2506 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { 2507 ret = regulator_get_voltage(regulator); 2508 if (ret >= 0) 2509 return min_uV <= ret && ret <= max_uV; 2510 else 2511 return ret; 2512 } 2513 2514 /* Any voltage within constrains range is fine? */ 2515 if (rdev->desc->continuous_voltage_range) 2516 return min_uV >= rdev->constraints->min_uV && 2517 max_uV <= rdev->constraints->max_uV; 2518 2519 ret = regulator_count_voltages(regulator); 2520 if (ret < 0) 2521 return ret; 2522 voltages = ret; 2523 2524 for (i = 0; i < voltages; i++) { 2525 ret = regulator_list_voltage(regulator, i); 2526 2527 if (ret >= min_uV && ret <= max_uV) 2528 return 1; 2529 } 2530 2531 return 0; 2532 } 2533 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); 2534 2535 static int _regulator_call_set_voltage(struct regulator_dev *rdev, 2536 int min_uV, int max_uV, 2537 unsigned *selector) 2538 { 2539 struct pre_voltage_change_data data; 2540 int ret; 2541 2542 data.old_uV = _regulator_get_voltage(rdev); 2543 data.min_uV = min_uV; 2544 data.max_uV = max_uV; 2545 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, 2546 &data); 2547 if (ret & NOTIFY_STOP_MASK) 2548 return -EINVAL; 2549 2550 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector); 2551 if (ret >= 0) 2552 return ret; 2553 2554 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, 2555 (void *)data.old_uV); 2556 2557 return ret; 2558 } 2559 2560 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev, 2561 int uV, unsigned selector) 2562 { 2563 struct pre_voltage_change_data data; 2564 int ret; 2565 2566 data.old_uV = _regulator_get_voltage(rdev); 2567 data.min_uV = uV; 2568 data.max_uV = uV; 2569 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, 2570 &data); 2571 if (ret & NOTIFY_STOP_MASK) 2572 return -EINVAL; 2573 2574 ret = rdev->desc->ops->set_voltage_sel(rdev, selector); 2575 if (ret >= 0) 2576 return ret; 2577 2578 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, 2579 (void *)data.old_uV); 2580 2581 return ret; 2582 } 2583 2584 static int _regulator_do_set_voltage(struct regulator_dev *rdev, 2585 int min_uV, int max_uV) 2586 { 2587 int ret; 2588 int delay = 0; 2589 int best_val = 0; 2590 unsigned int selector; 2591 int old_selector = -1; 2592 2593 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); 2594 2595 min_uV += rdev->constraints->uV_offset; 2596 max_uV += rdev->constraints->uV_offset; 2597 2598 /* 2599 * If we can't obtain the old selector there is not enough 2600 * info to call set_voltage_time_sel(). 2601 */ 2602 if (_regulator_is_enabled(rdev) && 2603 rdev->desc->ops->set_voltage_time_sel && 2604 rdev->desc->ops->get_voltage_sel) { 2605 old_selector = rdev->desc->ops->get_voltage_sel(rdev); 2606 if (old_selector < 0) 2607 return old_selector; 2608 } 2609 2610 if (rdev->desc->ops->set_voltage) { 2611 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV, 2612 &selector); 2613 2614 if (ret >= 0) { 2615 if (rdev->desc->ops->list_voltage) 2616 best_val = rdev->desc->ops->list_voltage(rdev, 2617 selector); 2618 else 2619 best_val = _regulator_get_voltage(rdev); 2620 } 2621 2622 } else if (rdev->desc->ops->set_voltage_sel) { 2623 if (rdev->desc->ops->map_voltage) { 2624 ret = rdev->desc->ops->map_voltage(rdev, min_uV, 2625 max_uV); 2626 } else { 2627 if (rdev->desc->ops->list_voltage == 2628 regulator_list_voltage_linear) 2629 ret = regulator_map_voltage_linear(rdev, 2630 min_uV, max_uV); 2631 else if (rdev->desc->ops->list_voltage == 2632 regulator_list_voltage_linear_range) 2633 ret = regulator_map_voltage_linear_range(rdev, 2634 min_uV, max_uV); 2635 else 2636 ret = regulator_map_voltage_iterate(rdev, 2637 min_uV, max_uV); 2638 } 2639 2640 if (ret >= 0) { 2641 best_val = rdev->desc->ops->list_voltage(rdev, ret); 2642 if (min_uV <= best_val && max_uV >= best_val) { 2643 selector = ret; 2644 if (old_selector == selector) 2645 ret = 0; 2646 else 2647 ret = _regulator_call_set_voltage_sel( 2648 rdev, best_val, selector); 2649 } else { 2650 ret = -EINVAL; 2651 } 2652 } 2653 } else { 2654 ret = -EINVAL; 2655 } 2656 2657 /* Call set_voltage_time_sel if successfully obtained old_selector */ 2658 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0 2659 && old_selector != selector) { 2660 2661 delay = rdev->desc->ops->set_voltage_time_sel(rdev, 2662 old_selector, selector); 2663 if (delay < 0) { 2664 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", 2665 delay); 2666 delay = 0; 2667 } 2668 2669 /* Insert any necessary delays */ 2670 if (delay >= 1000) { 2671 mdelay(delay / 1000); 2672 udelay(delay % 1000); 2673 } else if (delay) { 2674 udelay(delay); 2675 } 2676 } 2677 2678 if (ret == 0 && best_val >= 0) { 2679 unsigned long data = best_val; 2680 2681 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, 2682 (void *)data); 2683 } 2684 2685 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val); 2686 2687 return ret; 2688 } 2689 2690 /** 2691 * regulator_set_voltage - set regulator output voltage 2692 * @regulator: regulator source 2693 * @min_uV: Minimum required voltage in uV 2694 * @max_uV: Maximum acceptable voltage in uV 2695 * 2696 * Sets a voltage regulator to the desired output voltage. This can be set 2697 * during any regulator state. IOW, regulator can be disabled or enabled. 2698 * 2699 * If the regulator is enabled then the voltage will change to the new value 2700 * immediately otherwise if the regulator is disabled the regulator will 2701 * output at the new voltage when enabled. 2702 * 2703 * NOTE: If the regulator is shared between several devices then the lowest 2704 * request voltage that meets the system constraints will be used. 2705 * Regulator system constraints must be set for this regulator before 2706 * calling this function otherwise this call will fail. 2707 */ 2708 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) 2709 { 2710 struct regulator_dev *rdev = regulator->rdev; 2711 int ret = 0; 2712 int old_min_uV, old_max_uV; 2713 int current_uV; 2714 2715 mutex_lock(&rdev->mutex); 2716 2717 /* If we're setting the same range as last time the change 2718 * should be a noop (some cpufreq implementations use the same 2719 * voltage for multiple frequencies, for example). 2720 */ 2721 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV) 2722 goto out; 2723 2724 /* If we're trying to set a range that overlaps the current voltage, 2725 * return succesfully even though the regulator does not support 2726 * changing the voltage. 2727 */ 2728 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { 2729 current_uV = _regulator_get_voltage(rdev); 2730 if (min_uV <= current_uV && current_uV <= max_uV) { 2731 regulator->min_uV = min_uV; 2732 regulator->max_uV = max_uV; 2733 goto out; 2734 } 2735 } 2736 2737 /* sanity check */ 2738 if (!rdev->desc->ops->set_voltage && 2739 !rdev->desc->ops->set_voltage_sel) { 2740 ret = -EINVAL; 2741 goto out; 2742 } 2743 2744 /* constraints check */ 2745 ret = regulator_check_voltage(rdev, &min_uV, &max_uV); 2746 if (ret < 0) 2747 goto out; 2748 2749 /* restore original values in case of error */ 2750 old_min_uV = regulator->min_uV; 2751 old_max_uV = regulator->max_uV; 2752 regulator->min_uV = min_uV; 2753 regulator->max_uV = max_uV; 2754 2755 ret = regulator_check_consumers(rdev, &min_uV, &max_uV); 2756 if (ret < 0) 2757 goto out2; 2758 2759 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); 2760 if (ret < 0) 2761 goto out2; 2762 2763 out: 2764 mutex_unlock(&rdev->mutex); 2765 return ret; 2766 out2: 2767 regulator->min_uV = old_min_uV; 2768 regulator->max_uV = old_max_uV; 2769 mutex_unlock(&rdev->mutex); 2770 return ret; 2771 } 2772 EXPORT_SYMBOL_GPL(regulator_set_voltage); 2773 2774 /** 2775 * regulator_set_voltage_time - get raise/fall time 2776 * @regulator: regulator source 2777 * @old_uV: starting voltage in microvolts 2778 * @new_uV: target voltage in microvolts 2779 * 2780 * Provided with the starting and ending voltage, this function attempts to 2781 * calculate the time in microseconds required to rise or fall to this new 2782 * voltage. 2783 */ 2784 int regulator_set_voltage_time(struct regulator *regulator, 2785 int old_uV, int new_uV) 2786 { 2787 struct regulator_dev *rdev = regulator->rdev; 2788 const struct regulator_ops *ops = rdev->desc->ops; 2789 int old_sel = -1; 2790 int new_sel = -1; 2791 int voltage; 2792 int i; 2793 2794 /* Currently requires operations to do this */ 2795 if (!ops->list_voltage || !ops->set_voltage_time_sel 2796 || !rdev->desc->n_voltages) 2797 return -EINVAL; 2798 2799 for (i = 0; i < rdev->desc->n_voltages; i++) { 2800 /* We only look for exact voltage matches here */ 2801 voltage = regulator_list_voltage(regulator, i); 2802 if (voltage < 0) 2803 return -EINVAL; 2804 if (voltage == 0) 2805 continue; 2806 if (voltage == old_uV) 2807 old_sel = i; 2808 if (voltage == new_uV) 2809 new_sel = i; 2810 } 2811 2812 if (old_sel < 0 || new_sel < 0) 2813 return -EINVAL; 2814 2815 return ops->set_voltage_time_sel(rdev, old_sel, new_sel); 2816 } 2817 EXPORT_SYMBOL_GPL(regulator_set_voltage_time); 2818 2819 /** 2820 * regulator_set_voltage_time_sel - get raise/fall time 2821 * @rdev: regulator source device 2822 * @old_selector: selector for starting voltage 2823 * @new_selector: selector for target voltage 2824 * 2825 * Provided with the starting and target voltage selectors, this function 2826 * returns time in microseconds required to rise or fall to this new voltage 2827 * 2828 * Drivers providing ramp_delay in regulation_constraints can use this as their 2829 * set_voltage_time_sel() operation. 2830 */ 2831 int regulator_set_voltage_time_sel(struct regulator_dev *rdev, 2832 unsigned int old_selector, 2833 unsigned int new_selector) 2834 { 2835 unsigned int ramp_delay = 0; 2836 int old_volt, new_volt; 2837 2838 if (rdev->constraints->ramp_delay) 2839 ramp_delay = rdev->constraints->ramp_delay; 2840 else if (rdev->desc->ramp_delay) 2841 ramp_delay = rdev->desc->ramp_delay; 2842 2843 if (ramp_delay == 0) { 2844 rdev_warn(rdev, "ramp_delay not set\n"); 2845 return 0; 2846 } 2847 2848 /* sanity check */ 2849 if (!rdev->desc->ops->list_voltage) 2850 return -EINVAL; 2851 2852 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); 2853 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); 2854 2855 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay); 2856 } 2857 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel); 2858 2859 /** 2860 * regulator_sync_voltage - re-apply last regulator output voltage 2861 * @regulator: regulator source 2862 * 2863 * Re-apply the last configured voltage. This is intended to be used 2864 * where some external control source the consumer is cooperating with 2865 * has caused the configured voltage to change. 2866 */ 2867 int regulator_sync_voltage(struct regulator *regulator) 2868 { 2869 struct regulator_dev *rdev = regulator->rdev; 2870 int ret, min_uV, max_uV; 2871 2872 mutex_lock(&rdev->mutex); 2873 2874 if (!rdev->desc->ops->set_voltage && 2875 !rdev->desc->ops->set_voltage_sel) { 2876 ret = -EINVAL; 2877 goto out; 2878 } 2879 2880 /* This is only going to work if we've had a voltage configured. */ 2881 if (!regulator->min_uV && !regulator->max_uV) { 2882 ret = -EINVAL; 2883 goto out; 2884 } 2885 2886 min_uV = regulator->min_uV; 2887 max_uV = regulator->max_uV; 2888 2889 /* This should be a paranoia check... */ 2890 ret = regulator_check_voltage(rdev, &min_uV, &max_uV); 2891 if (ret < 0) 2892 goto out; 2893 2894 ret = regulator_check_consumers(rdev, &min_uV, &max_uV); 2895 if (ret < 0) 2896 goto out; 2897 2898 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); 2899 2900 out: 2901 mutex_unlock(&rdev->mutex); 2902 return ret; 2903 } 2904 EXPORT_SYMBOL_GPL(regulator_sync_voltage); 2905 2906 static int _regulator_get_voltage(struct regulator_dev *rdev) 2907 { 2908 int sel, ret; 2909 2910 if (rdev->desc->ops->get_voltage_sel) { 2911 sel = rdev->desc->ops->get_voltage_sel(rdev); 2912 if (sel < 0) 2913 return sel; 2914 ret = rdev->desc->ops->list_voltage(rdev, sel); 2915 } else if (rdev->desc->ops->get_voltage) { 2916 ret = rdev->desc->ops->get_voltage(rdev); 2917 } else if (rdev->desc->ops->list_voltage) { 2918 ret = rdev->desc->ops->list_voltage(rdev, 0); 2919 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { 2920 ret = rdev->desc->fixed_uV; 2921 } else if (rdev->supply) { 2922 ret = regulator_get_voltage(rdev->supply); 2923 } else { 2924 return -EINVAL; 2925 } 2926 2927 if (ret < 0) 2928 return ret; 2929 return ret - rdev->constraints->uV_offset; 2930 } 2931 2932 /** 2933 * regulator_get_voltage - get regulator output voltage 2934 * @regulator: regulator source 2935 * 2936 * This returns the current regulator voltage in uV. 2937 * 2938 * NOTE: If the regulator is disabled it will return the voltage value. This 2939 * function should not be used to determine regulator state. 2940 */ 2941 int regulator_get_voltage(struct regulator *regulator) 2942 { 2943 int ret; 2944 2945 mutex_lock(®ulator->rdev->mutex); 2946 2947 ret = _regulator_get_voltage(regulator->rdev); 2948 2949 mutex_unlock(®ulator->rdev->mutex); 2950 2951 return ret; 2952 } 2953 EXPORT_SYMBOL_GPL(regulator_get_voltage); 2954 2955 /** 2956 * regulator_set_current_limit - set regulator output current limit 2957 * @regulator: regulator source 2958 * @min_uA: Minimum supported current in uA 2959 * @max_uA: Maximum supported current in uA 2960 * 2961 * Sets current sink to the desired output current. This can be set during 2962 * any regulator state. IOW, regulator can be disabled or enabled. 2963 * 2964 * If the regulator is enabled then the current will change to the new value 2965 * immediately otherwise if the regulator is disabled the regulator will 2966 * output at the new current when enabled. 2967 * 2968 * NOTE: Regulator system constraints must be set for this regulator before 2969 * calling this function otherwise this call will fail. 2970 */ 2971 int regulator_set_current_limit(struct regulator *regulator, 2972 int min_uA, int max_uA) 2973 { 2974 struct regulator_dev *rdev = regulator->rdev; 2975 int ret; 2976 2977 mutex_lock(&rdev->mutex); 2978 2979 /* sanity check */ 2980 if (!rdev->desc->ops->set_current_limit) { 2981 ret = -EINVAL; 2982 goto out; 2983 } 2984 2985 /* constraints check */ 2986 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); 2987 if (ret < 0) 2988 goto out; 2989 2990 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); 2991 out: 2992 mutex_unlock(&rdev->mutex); 2993 return ret; 2994 } 2995 EXPORT_SYMBOL_GPL(regulator_set_current_limit); 2996 2997 static int _regulator_get_current_limit(struct regulator_dev *rdev) 2998 { 2999 int ret; 3000 3001 mutex_lock(&rdev->mutex); 3002 3003 /* sanity check */ 3004 if (!rdev->desc->ops->get_current_limit) { 3005 ret = -EINVAL; 3006 goto out; 3007 } 3008 3009 ret = rdev->desc->ops->get_current_limit(rdev); 3010 out: 3011 mutex_unlock(&rdev->mutex); 3012 return ret; 3013 } 3014 3015 /** 3016 * regulator_get_current_limit - get regulator output current 3017 * @regulator: regulator source 3018 * 3019 * This returns the current supplied by the specified current sink in uA. 3020 * 3021 * NOTE: If the regulator is disabled it will return the current value. This 3022 * function should not be used to determine regulator state. 3023 */ 3024 int regulator_get_current_limit(struct regulator *regulator) 3025 { 3026 return _regulator_get_current_limit(regulator->rdev); 3027 } 3028 EXPORT_SYMBOL_GPL(regulator_get_current_limit); 3029 3030 /** 3031 * regulator_set_mode - set regulator operating mode 3032 * @regulator: regulator source 3033 * @mode: operating mode - one of the REGULATOR_MODE constants 3034 * 3035 * Set regulator operating mode to increase regulator efficiency or improve 3036 * regulation performance. 3037 * 3038 * NOTE: Regulator system constraints must be set for this regulator before 3039 * calling this function otherwise this call will fail. 3040 */ 3041 int regulator_set_mode(struct regulator *regulator, unsigned int mode) 3042 { 3043 struct regulator_dev *rdev = regulator->rdev; 3044 int ret; 3045 int regulator_curr_mode; 3046 3047 mutex_lock(&rdev->mutex); 3048 3049 /* sanity check */ 3050 if (!rdev->desc->ops->set_mode) { 3051 ret = -EINVAL; 3052 goto out; 3053 } 3054 3055 /* return if the same mode is requested */ 3056 if (rdev->desc->ops->get_mode) { 3057 regulator_curr_mode = rdev->desc->ops->get_mode(rdev); 3058 if (regulator_curr_mode == mode) { 3059 ret = 0; 3060 goto out; 3061 } 3062 } 3063 3064 /* constraints check */ 3065 ret = regulator_mode_constrain(rdev, &mode); 3066 if (ret < 0) 3067 goto out; 3068 3069 ret = rdev->desc->ops->set_mode(rdev, mode); 3070 out: 3071 mutex_unlock(&rdev->mutex); 3072 return ret; 3073 } 3074 EXPORT_SYMBOL_GPL(regulator_set_mode); 3075 3076 static unsigned int _regulator_get_mode(struct regulator_dev *rdev) 3077 { 3078 int ret; 3079 3080 mutex_lock(&rdev->mutex); 3081 3082 /* sanity check */ 3083 if (!rdev->desc->ops->get_mode) { 3084 ret = -EINVAL; 3085 goto out; 3086 } 3087 3088 ret = rdev->desc->ops->get_mode(rdev); 3089 out: 3090 mutex_unlock(&rdev->mutex); 3091 return ret; 3092 } 3093 3094 /** 3095 * regulator_get_mode - get regulator operating mode 3096 * @regulator: regulator source 3097 * 3098 * Get the current regulator operating mode. 3099 */ 3100 unsigned int regulator_get_mode(struct regulator *regulator) 3101 { 3102 return _regulator_get_mode(regulator->rdev); 3103 } 3104 EXPORT_SYMBOL_GPL(regulator_get_mode); 3105 3106 /** 3107 * regulator_set_load - set regulator load 3108 * @regulator: regulator source 3109 * @uA_load: load current 3110 * 3111 * Notifies the regulator core of a new device load. This is then used by 3112 * DRMS (if enabled by constraints) to set the most efficient regulator 3113 * operating mode for the new regulator loading. 3114 * 3115 * Consumer devices notify their supply regulator of the maximum power 3116 * they will require (can be taken from device datasheet in the power 3117 * consumption tables) when they change operational status and hence power 3118 * state. Examples of operational state changes that can affect power 3119 * consumption are :- 3120 * 3121 * o Device is opened / closed. 3122 * o Device I/O is about to begin or has just finished. 3123 * o Device is idling in between work. 3124 * 3125 * This information is also exported via sysfs to userspace. 3126 * 3127 * DRMS will sum the total requested load on the regulator and change 3128 * to the most efficient operating mode if platform constraints allow. 3129 * 3130 * On error a negative errno is returned. 3131 */ 3132 int regulator_set_load(struct regulator *regulator, int uA_load) 3133 { 3134 struct regulator_dev *rdev = regulator->rdev; 3135 int ret; 3136 3137 mutex_lock(&rdev->mutex); 3138 regulator->uA_load = uA_load; 3139 ret = drms_uA_update(rdev); 3140 mutex_unlock(&rdev->mutex); 3141 3142 return ret; 3143 } 3144 EXPORT_SYMBOL_GPL(regulator_set_load); 3145 3146 /** 3147 * regulator_allow_bypass - allow the regulator to go into bypass mode 3148 * 3149 * @regulator: Regulator to configure 3150 * @enable: enable or disable bypass mode 3151 * 3152 * Allow the regulator to go into bypass mode if all other consumers 3153 * for the regulator also enable bypass mode and the machine 3154 * constraints allow this. Bypass mode means that the regulator is 3155 * simply passing the input directly to the output with no regulation. 3156 */ 3157 int regulator_allow_bypass(struct regulator *regulator, bool enable) 3158 { 3159 struct regulator_dev *rdev = regulator->rdev; 3160 int ret = 0; 3161 3162 if (!rdev->desc->ops->set_bypass) 3163 return 0; 3164 3165 if (rdev->constraints && 3166 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS)) 3167 return 0; 3168 3169 mutex_lock(&rdev->mutex); 3170 3171 if (enable && !regulator->bypass) { 3172 rdev->bypass_count++; 3173 3174 if (rdev->bypass_count == rdev->open_count) { 3175 ret = rdev->desc->ops->set_bypass(rdev, enable); 3176 if (ret != 0) 3177 rdev->bypass_count--; 3178 } 3179 3180 } else if (!enable && regulator->bypass) { 3181 rdev->bypass_count--; 3182 3183 if (rdev->bypass_count != rdev->open_count) { 3184 ret = rdev->desc->ops->set_bypass(rdev, enable); 3185 if (ret != 0) 3186 rdev->bypass_count++; 3187 } 3188 } 3189 3190 if (ret == 0) 3191 regulator->bypass = enable; 3192 3193 mutex_unlock(&rdev->mutex); 3194 3195 return ret; 3196 } 3197 EXPORT_SYMBOL_GPL(regulator_allow_bypass); 3198 3199 /** 3200 * regulator_register_notifier - register regulator event notifier 3201 * @regulator: regulator source 3202 * @nb: notifier block 3203 * 3204 * Register notifier block to receive regulator events. 3205 */ 3206 int regulator_register_notifier(struct regulator *regulator, 3207 struct notifier_block *nb) 3208 { 3209 return blocking_notifier_chain_register(®ulator->rdev->notifier, 3210 nb); 3211 } 3212 EXPORT_SYMBOL_GPL(regulator_register_notifier); 3213 3214 /** 3215 * regulator_unregister_notifier - unregister regulator event notifier 3216 * @regulator: regulator source 3217 * @nb: notifier block 3218 * 3219 * Unregister regulator event notifier block. 3220 */ 3221 int regulator_unregister_notifier(struct regulator *regulator, 3222 struct notifier_block *nb) 3223 { 3224 return blocking_notifier_chain_unregister(®ulator->rdev->notifier, 3225 nb); 3226 } 3227 EXPORT_SYMBOL_GPL(regulator_unregister_notifier); 3228 3229 /* notify regulator consumers and downstream regulator consumers. 3230 * Note mutex must be held by caller. 3231 */ 3232 static int _notifier_call_chain(struct regulator_dev *rdev, 3233 unsigned long event, void *data) 3234 { 3235 /* call rdev chain first */ 3236 return blocking_notifier_call_chain(&rdev->notifier, event, data); 3237 } 3238 3239 /** 3240 * regulator_bulk_get - get multiple regulator consumers 3241 * 3242 * @dev: Device to supply 3243 * @num_consumers: Number of consumers to register 3244 * @consumers: Configuration of consumers; clients are stored here. 3245 * 3246 * @return 0 on success, an errno on failure. 3247 * 3248 * This helper function allows drivers to get several regulator 3249 * consumers in one operation. If any of the regulators cannot be 3250 * acquired then any regulators that were allocated will be freed 3251 * before returning to the caller. 3252 */ 3253 int regulator_bulk_get(struct device *dev, int num_consumers, 3254 struct regulator_bulk_data *consumers) 3255 { 3256 int i; 3257 int ret; 3258 3259 for (i = 0; i < num_consumers; i++) 3260 consumers[i].consumer = NULL; 3261 3262 for (i = 0; i < num_consumers; i++) { 3263 consumers[i].consumer = regulator_get(dev, 3264 consumers[i].supply); 3265 if (IS_ERR(consumers[i].consumer)) { 3266 ret = PTR_ERR(consumers[i].consumer); 3267 dev_err(dev, "Failed to get supply '%s': %d\n", 3268 consumers[i].supply, ret); 3269 consumers[i].consumer = NULL; 3270 goto err; 3271 } 3272 } 3273 3274 return 0; 3275 3276 err: 3277 while (--i >= 0) 3278 regulator_put(consumers[i].consumer); 3279 3280 return ret; 3281 } 3282 EXPORT_SYMBOL_GPL(regulator_bulk_get); 3283 3284 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) 3285 { 3286 struct regulator_bulk_data *bulk = data; 3287 3288 bulk->ret = regulator_enable(bulk->consumer); 3289 } 3290 3291 /** 3292 * regulator_bulk_enable - enable multiple regulator consumers 3293 * 3294 * @num_consumers: Number of consumers 3295 * @consumers: Consumer data; clients are stored here. 3296 * @return 0 on success, an errno on failure 3297 * 3298 * This convenience API allows consumers to enable multiple regulator 3299 * clients in a single API call. If any consumers cannot be enabled 3300 * then any others that were enabled will be disabled again prior to 3301 * return. 3302 */ 3303 int regulator_bulk_enable(int num_consumers, 3304 struct regulator_bulk_data *consumers) 3305 { 3306 ASYNC_DOMAIN_EXCLUSIVE(async_domain); 3307 int i; 3308 int ret = 0; 3309 3310 for (i = 0; i < num_consumers; i++) { 3311 if (consumers[i].consumer->always_on) 3312 consumers[i].ret = 0; 3313 else 3314 async_schedule_domain(regulator_bulk_enable_async, 3315 &consumers[i], &async_domain); 3316 } 3317 3318 async_synchronize_full_domain(&async_domain); 3319 3320 /* If any consumer failed we need to unwind any that succeeded */ 3321 for (i = 0; i < num_consumers; i++) { 3322 if (consumers[i].ret != 0) { 3323 ret = consumers[i].ret; 3324 goto err; 3325 } 3326 } 3327 3328 return 0; 3329 3330 err: 3331 for (i = 0; i < num_consumers; i++) { 3332 if (consumers[i].ret < 0) 3333 pr_err("Failed to enable %s: %d\n", consumers[i].supply, 3334 consumers[i].ret); 3335 else 3336 regulator_disable(consumers[i].consumer); 3337 } 3338 3339 return ret; 3340 } 3341 EXPORT_SYMBOL_GPL(regulator_bulk_enable); 3342 3343 /** 3344 * regulator_bulk_disable - disable multiple regulator consumers 3345 * 3346 * @num_consumers: Number of consumers 3347 * @consumers: Consumer data; clients are stored here. 3348 * @return 0 on success, an errno on failure 3349 * 3350 * This convenience API allows consumers to disable multiple regulator 3351 * clients in a single API call. If any consumers cannot be disabled 3352 * then any others that were disabled will be enabled again prior to 3353 * return. 3354 */ 3355 int regulator_bulk_disable(int num_consumers, 3356 struct regulator_bulk_data *consumers) 3357 { 3358 int i; 3359 int ret, r; 3360 3361 for (i = num_consumers - 1; i >= 0; --i) { 3362 ret = regulator_disable(consumers[i].consumer); 3363 if (ret != 0) 3364 goto err; 3365 } 3366 3367 return 0; 3368 3369 err: 3370 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); 3371 for (++i; i < num_consumers; ++i) { 3372 r = regulator_enable(consumers[i].consumer); 3373 if (r != 0) 3374 pr_err("Failed to reename %s: %d\n", 3375 consumers[i].supply, r); 3376 } 3377 3378 return ret; 3379 } 3380 EXPORT_SYMBOL_GPL(regulator_bulk_disable); 3381 3382 /** 3383 * regulator_bulk_force_disable - force disable multiple regulator consumers 3384 * 3385 * @num_consumers: Number of consumers 3386 * @consumers: Consumer data; clients are stored here. 3387 * @return 0 on success, an errno on failure 3388 * 3389 * This convenience API allows consumers to forcibly disable multiple regulator 3390 * clients in a single API call. 3391 * NOTE: This should be used for situations when device damage will 3392 * likely occur if the regulators are not disabled (e.g. over temp). 3393 * Although regulator_force_disable function call for some consumers can 3394 * return error numbers, the function is called for all consumers. 3395 */ 3396 int regulator_bulk_force_disable(int num_consumers, 3397 struct regulator_bulk_data *consumers) 3398 { 3399 int i; 3400 int ret; 3401 3402 for (i = 0; i < num_consumers; i++) 3403 consumers[i].ret = 3404 regulator_force_disable(consumers[i].consumer); 3405 3406 for (i = 0; i < num_consumers; i++) { 3407 if (consumers[i].ret != 0) { 3408 ret = consumers[i].ret; 3409 goto out; 3410 } 3411 } 3412 3413 return 0; 3414 out: 3415 return ret; 3416 } 3417 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); 3418 3419 /** 3420 * regulator_bulk_free - free multiple regulator consumers 3421 * 3422 * @num_consumers: Number of consumers 3423 * @consumers: Consumer data; clients are stored here. 3424 * 3425 * This convenience API allows consumers to free multiple regulator 3426 * clients in a single API call. 3427 */ 3428 void regulator_bulk_free(int num_consumers, 3429 struct regulator_bulk_data *consumers) 3430 { 3431 int i; 3432 3433 for (i = 0; i < num_consumers; i++) { 3434 regulator_put(consumers[i].consumer); 3435 consumers[i].consumer = NULL; 3436 } 3437 } 3438 EXPORT_SYMBOL_GPL(regulator_bulk_free); 3439 3440 /** 3441 * regulator_notifier_call_chain - call regulator event notifier 3442 * @rdev: regulator source 3443 * @event: notifier block 3444 * @data: callback-specific data. 3445 * 3446 * Called by regulator drivers to notify clients a regulator event has 3447 * occurred. We also notify regulator clients downstream. 3448 * Note lock must be held by caller. 3449 */ 3450 int regulator_notifier_call_chain(struct regulator_dev *rdev, 3451 unsigned long event, void *data) 3452 { 3453 _notifier_call_chain(rdev, event, data); 3454 return NOTIFY_DONE; 3455 3456 } 3457 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); 3458 3459 /** 3460 * regulator_mode_to_status - convert a regulator mode into a status 3461 * 3462 * @mode: Mode to convert 3463 * 3464 * Convert a regulator mode into a status. 3465 */ 3466 int regulator_mode_to_status(unsigned int mode) 3467 { 3468 switch (mode) { 3469 case REGULATOR_MODE_FAST: 3470 return REGULATOR_STATUS_FAST; 3471 case REGULATOR_MODE_NORMAL: 3472 return REGULATOR_STATUS_NORMAL; 3473 case REGULATOR_MODE_IDLE: 3474 return REGULATOR_STATUS_IDLE; 3475 case REGULATOR_MODE_STANDBY: 3476 return REGULATOR_STATUS_STANDBY; 3477 default: 3478 return REGULATOR_STATUS_UNDEFINED; 3479 } 3480 } 3481 EXPORT_SYMBOL_GPL(regulator_mode_to_status); 3482 3483 static struct attribute *regulator_dev_attrs[] = { 3484 &dev_attr_name.attr, 3485 &dev_attr_num_users.attr, 3486 &dev_attr_type.attr, 3487 &dev_attr_microvolts.attr, 3488 &dev_attr_microamps.attr, 3489 &dev_attr_opmode.attr, 3490 &dev_attr_state.attr, 3491 &dev_attr_status.attr, 3492 &dev_attr_bypass.attr, 3493 &dev_attr_requested_microamps.attr, 3494 &dev_attr_min_microvolts.attr, 3495 &dev_attr_max_microvolts.attr, 3496 &dev_attr_min_microamps.attr, 3497 &dev_attr_max_microamps.attr, 3498 &dev_attr_suspend_standby_state.attr, 3499 &dev_attr_suspend_mem_state.attr, 3500 &dev_attr_suspend_disk_state.attr, 3501 &dev_attr_suspend_standby_microvolts.attr, 3502 &dev_attr_suspend_mem_microvolts.attr, 3503 &dev_attr_suspend_disk_microvolts.attr, 3504 &dev_attr_suspend_standby_mode.attr, 3505 &dev_attr_suspend_mem_mode.attr, 3506 &dev_attr_suspend_disk_mode.attr, 3507 NULL 3508 }; 3509 3510 /* 3511 * To avoid cluttering sysfs (and memory) with useless state, only 3512 * create attributes that can be meaningfully displayed. 3513 */ 3514 static umode_t regulator_attr_is_visible(struct kobject *kobj, 3515 struct attribute *attr, int idx) 3516 { 3517 struct device *dev = kobj_to_dev(kobj); 3518 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev); 3519 const struct regulator_ops *ops = rdev->desc->ops; 3520 umode_t mode = attr->mode; 3521 3522 /* these three are always present */ 3523 if (attr == &dev_attr_name.attr || 3524 attr == &dev_attr_num_users.attr || 3525 attr == &dev_attr_type.attr) 3526 return mode; 3527 3528 /* some attributes need specific methods to be displayed */ 3529 if (attr == &dev_attr_microvolts.attr) { 3530 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || 3531 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || 3532 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || 3533 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1)) 3534 return mode; 3535 return 0; 3536 } 3537 3538 if (attr == &dev_attr_microamps.attr) 3539 return ops->get_current_limit ? mode : 0; 3540 3541 if (attr == &dev_attr_opmode.attr) 3542 return ops->get_mode ? mode : 0; 3543 3544 if (attr == &dev_attr_state.attr) 3545 return (rdev->ena_pin || ops->is_enabled) ? mode : 0; 3546 3547 if (attr == &dev_attr_status.attr) 3548 return ops->get_status ? mode : 0; 3549 3550 if (attr == &dev_attr_bypass.attr) 3551 return ops->get_bypass ? mode : 0; 3552 3553 /* some attributes are type-specific */ 3554 if (attr == &dev_attr_requested_microamps.attr) 3555 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0; 3556 3557 /* constraints need specific supporting methods */ 3558 if (attr == &dev_attr_min_microvolts.attr || 3559 attr == &dev_attr_max_microvolts.attr) 3560 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0; 3561 3562 if (attr == &dev_attr_min_microamps.attr || 3563 attr == &dev_attr_max_microamps.attr) 3564 return ops->set_current_limit ? mode : 0; 3565 3566 if (attr == &dev_attr_suspend_standby_state.attr || 3567 attr == &dev_attr_suspend_mem_state.attr || 3568 attr == &dev_attr_suspend_disk_state.attr) 3569 return mode; 3570 3571 if (attr == &dev_attr_suspend_standby_microvolts.attr || 3572 attr == &dev_attr_suspend_mem_microvolts.attr || 3573 attr == &dev_attr_suspend_disk_microvolts.attr) 3574 return ops->set_suspend_voltage ? mode : 0; 3575 3576 if (attr == &dev_attr_suspend_standby_mode.attr || 3577 attr == &dev_attr_suspend_mem_mode.attr || 3578 attr == &dev_attr_suspend_disk_mode.attr) 3579 return ops->set_suspend_mode ? mode : 0; 3580 3581 return mode; 3582 } 3583 3584 static const struct attribute_group regulator_dev_group = { 3585 .attrs = regulator_dev_attrs, 3586 .is_visible = regulator_attr_is_visible, 3587 }; 3588 3589 static const struct attribute_group *regulator_dev_groups[] = { 3590 ®ulator_dev_group, 3591 NULL 3592 }; 3593 3594 static void regulator_dev_release(struct device *dev) 3595 { 3596 struct regulator_dev *rdev = dev_get_drvdata(dev); 3597 kfree(rdev); 3598 } 3599 3600 static struct class regulator_class = { 3601 .name = "regulator", 3602 .dev_release = regulator_dev_release, 3603 .dev_groups = regulator_dev_groups, 3604 }; 3605 3606 static void rdev_init_debugfs(struct regulator_dev *rdev) 3607 { 3608 struct device *parent = rdev->dev.parent; 3609 const char *rname = rdev_get_name(rdev); 3610 char name[NAME_MAX]; 3611 3612 /* Avoid duplicate debugfs directory names */ 3613 if (parent && rname == rdev->desc->name) { 3614 snprintf(name, sizeof(name), "%s-%s", dev_name(parent), 3615 rname); 3616 rname = name; 3617 } 3618 3619 rdev->debugfs = debugfs_create_dir(rname, debugfs_root); 3620 if (!rdev->debugfs) { 3621 rdev_warn(rdev, "Failed to create debugfs directory\n"); 3622 return; 3623 } 3624 3625 debugfs_create_u32("use_count", 0444, rdev->debugfs, 3626 &rdev->use_count); 3627 debugfs_create_u32("open_count", 0444, rdev->debugfs, 3628 &rdev->open_count); 3629 debugfs_create_u32("bypass_count", 0444, rdev->debugfs, 3630 &rdev->bypass_count); 3631 } 3632 3633 /** 3634 * regulator_register - register regulator 3635 * @regulator_desc: regulator to register 3636 * @cfg: runtime configuration for regulator 3637 * 3638 * Called by regulator drivers to register a regulator. 3639 * Returns a valid pointer to struct regulator_dev on success 3640 * or an ERR_PTR() on error. 3641 */ 3642 struct regulator_dev * 3643 regulator_register(const struct regulator_desc *regulator_desc, 3644 const struct regulator_config *cfg) 3645 { 3646 const struct regulation_constraints *constraints = NULL; 3647 const struct regulator_init_data *init_data; 3648 struct regulator_config *config = NULL; 3649 static atomic_t regulator_no = ATOMIC_INIT(-1); 3650 struct regulator_dev *rdev; 3651 struct device *dev; 3652 int ret, i; 3653 3654 if (regulator_desc == NULL || cfg == NULL) 3655 return ERR_PTR(-EINVAL); 3656 3657 dev = cfg->dev; 3658 WARN_ON(!dev); 3659 3660 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) 3661 return ERR_PTR(-EINVAL); 3662 3663 if (regulator_desc->type != REGULATOR_VOLTAGE && 3664 regulator_desc->type != REGULATOR_CURRENT) 3665 return ERR_PTR(-EINVAL); 3666 3667 /* Only one of each should be implemented */ 3668 WARN_ON(regulator_desc->ops->get_voltage && 3669 regulator_desc->ops->get_voltage_sel); 3670 WARN_ON(regulator_desc->ops->set_voltage && 3671 regulator_desc->ops->set_voltage_sel); 3672 3673 /* If we're using selectors we must implement list_voltage. */ 3674 if (regulator_desc->ops->get_voltage_sel && 3675 !regulator_desc->ops->list_voltage) { 3676 return ERR_PTR(-EINVAL); 3677 } 3678 if (regulator_desc->ops->set_voltage_sel && 3679 !regulator_desc->ops->list_voltage) { 3680 return ERR_PTR(-EINVAL); 3681 } 3682 3683 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); 3684 if (rdev == NULL) 3685 return ERR_PTR(-ENOMEM); 3686 3687 /* 3688 * Duplicate the config so the driver could override it after 3689 * parsing init data. 3690 */ 3691 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); 3692 if (config == NULL) { 3693 kfree(rdev); 3694 return ERR_PTR(-ENOMEM); 3695 } 3696 3697 init_data = regulator_of_get_init_data(dev, regulator_desc, config, 3698 &rdev->dev.of_node); 3699 if (!init_data) { 3700 init_data = config->init_data; 3701 rdev->dev.of_node = of_node_get(config->of_node); 3702 } 3703 3704 mutex_lock(®ulator_list_mutex); 3705 3706 mutex_init(&rdev->mutex); 3707 rdev->reg_data = config->driver_data; 3708 rdev->owner = regulator_desc->owner; 3709 rdev->desc = regulator_desc; 3710 if (config->regmap) 3711 rdev->regmap = config->regmap; 3712 else if (dev_get_regmap(dev, NULL)) 3713 rdev->regmap = dev_get_regmap(dev, NULL); 3714 else if (dev->parent) 3715 rdev->regmap = dev_get_regmap(dev->parent, NULL); 3716 INIT_LIST_HEAD(&rdev->consumer_list); 3717 INIT_LIST_HEAD(&rdev->list); 3718 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); 3719 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); 3720 3721 /* preform any regulator specific init */ 3722 if (init_data && init_data->regulator_init) { 3723 ret = init_data->regulator_init(rdev->reg_data); 3724 if (ret < 0) 3725 goto clean; 3726 } 3727 3728 /* register with sysfs */ 3729 rdev->dev.class = ®ulator_class; 3730 rdev->dev.parent = dev; 3731 dev_set_name(&rdev->dev, "regulator.%lu", 3732 (unsigned long) atomic_inc_return(®ulator_no)); 3733 ret = device_register(&rdev->dev); 3734 if (ret != 0) { 3735 put_device(&rdev->dev); 3736 goto clean; 3737 } 3738 3739 dev_set_drvdata(&rdev->dev, rdev); 3740 3741 if ((config->ena_gpio || config->ena_gpio_initialized) && 3742 gpio_is_valid(config->ena_gpio)) { 3743 ret = regulator_ena_gpio_request(rdev, config); 3744 if (ret != 0) { 3745 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n", 3746 config->ena_gpio, ret); 3747 goto wash; 3748 } 3749 } 3750 3751 /* set regulator constraints */ 3752 if (init_data) 3753 constraints = &init_data->constraints; 3754 3755 ret = set_machine_constraints(rdev, constraints); 3756 if (ret < 0) 3757 goto scrub; 3758 3759 if (init_data && init_data->supply_regulator) 3760 rdev->supply_name = init_data->supply_regulator; 3761 else if (regulator_desc->supply_name) 3762 rdev->supply_name = regulator_desc->supply_name; 3763 3764 /* add consumers devices */ 3765 if (init_data) { 3766 for (i = 0; i < init_data->num_consumer_supplies; i++) { 3767 ret = set_consumer_device_supply(rdev, 3768 init_data->consumer_supplies[i].dev_name, 3769 init_data->consumer_supplies[i].supply); 3770 if (ret < 0) { 3771 dev_err(dev, "Failed to set supply %s\n", 3772 init_data->consumer_supplies[i].supply); 3773 goto unset_supplies; 3774 } 3775 } 3776 } 3777 3778 list_add(&rdev->list, ®ulator_list); 3779 3780 rdev_init_debugfs(rdev); 3781 out: 3782 mutex_unlock(®ulator_list_mutex); 3783 kfree(config); 3784 return rdev; 3785 3786 unset_supplies: 3787 unset_regulator_supplies(rdev); 3788 3789 scrub: 3790 regulator_ena_gpio_free(rdev); 3791 kfree(rdev->constraints); 3792 wash: 3793 device_unregister(&rdev->dev); 3794 /* device core frees rdev */ 3795 rdev = ERR_PTR(ret); 3796 goto out; 3797 3798 clean: 3799 kfree(rdev); 3800 rdev = ERR_PTR(ret); 3801 goto out; 3802 } 3803 EXPORT_SYMBOL_GPL(regulator_register); 3804 3805 /** 3806 * regulator_unregister - unregister regulator 3807 * @rdev: regulator to unregister 3808 * 3809 * Called by regulator drivers to unregister a regulator. 3810 */ 3811 void regulator_unregister(struct regulator_dev *rdev) 3812 { 3813 if (rdev == NULL) 3814 return; 3815 3816 if (rdev->supply) { 3817 while (rdev->use_count--) 3818 regulator_disable(rdev->supply); 3819 regulator_put(rdev->supply); 3820 } 3821 mutex_lock(®ulator_list_mutex); 3822 debugfs_remove_recursive(rdev->debugfs); 3823 flush_work(&rdev->disable_work.work); 3824 WARN_ON(rdev->open_count); 3825 unset_regulator_supplies(rdev); 3826 list_del(&rdev->list); 3827 kfree(rdev->constraints); 3828 regulator_ena_gpio_free(rdev); 3829 of_node_put(rdev->dev.of_node); 3830 device_unregister(&rdev->dev); 3831 mutex_unlock(®ulator_list_mutex); 3832 } 3833 EXPORT_SYMBOL_GPL(regulator_unregister); 3834 3835 /** 3836 * regulator_suspend_prepare - prepare regulators for system wide suspend 3837 * @state: system suspend state 3838 * 3839 * Configure each regulator with it's suspend operating parameters for state. 3840 * This will usually be called by machine suspend code prior to supending. 3841 */ 3842 int regulator_suspend_prepare(suspend_state_t state) 3843 { 3844 struct regulator_dev *rdev; 3845 int ret = 0; 3846 3847 /* ON is handled by regulator active state */ 3848 if (state == PM_SUSPEND_ON) 3849 return -EINVAL; 3850 3851 mutex_lock(®ulator_list_mutex); 3852 list_for_each_entry(rdev, ®ulator_list, list) { 3853 3854 mutex_lock(&rdev->mutex); 3855 ret = suspend_prepare(rdev, state); 3856 mutex_unlock(&rdev->mutex); 3857 3858 if (ret < 0) { 3859 rdev_err(rdev, "failed to prepare\n"); 3860 goto out; 3861 } 3862 } 3863 out: 3864 mutex_unlock(®ulator_list_mutex); 3865 return ret; 3866 } 3867 EXPORT_SYMBOL_GPL(regulator_suspend_prepare); 3868 3869 /** 3870 * regulator_suspend_finish - resume regulators from system wide suspend 3871 * 3872 * Turn on regulators that might be turned off by regulator_suspend_prepare 3873 * and that should be turned on according to the regulators properties. 3874 */ 3875 int regulator_suspend_finish(void) 3876 { 3877 struct regulator_dev *rdev; 3878 int ret = 0, error; 3879 3880 mutex_lock(®ulator_list_mutex); 3881 list_for_each_entry(rdev, ®ulator_list, list) { 3882 mutex_lock(&rdev->mutex); 3883 if (rdev->use_count > 0 || rdev->constraints->always_on) { 3884 if (!_regulator_is_enabled(rdev)) { 3885 error = _regulator_do_enable(rdev); 3886 if (error) 3887 ret = error; 3888 } 3889 } else { 3890 if (!have_full_constraints()) 3891 goto unlock; 3892 if (!_regulator_is_enabled(rdev)) 3893 goto unlock; 3894 3895 error = _regulator_do_disable(rdev); 3896 if (error) 3897 ret = error; 3898 } 3899 unlock: 3900 mutex_unlock(&rdev->mutex); 3901 } 3902 mutex_unlock(®ulator_list_mutex); 3903 return ret; 3904 } 3905 EXPORT_SYMBOL_GPL(regulator_suspend_finish); 3906 3907 /** 3908 * regulator_has_full_constraints - the system has fully specified constraints 3909 * 3910 * Calling this function will cause the regulator API to disable all 3911 * regulators which have a zero use count and don't have an always_on 3912 * constraint in a late_initcall. 3913 * 3914 * The intention is that this will become the default behaviour in a 3915 * future kernel release so users are encouraged to use this facility 3916 * now. 3917 */ 3918 void regulator_has_full_constraints(void) 3919 { 3920 has_full_constraints = 1; 3921 } 3922 EXPORT_SYMBOL_GPL(regulator_has_full_constraints); 3923 3924 /** 3925 * rdev_get_drvdata - get rdev regulator driver data 3926 * @rdev: regulator 3927 * 3928 * Get rdev regulator driver private data. This call can be used in the 3929 * regulator driver context. 3930 */ 3931 void *rdev_get_drvdata(struct regulator_dev *rdev) 3932 { 3933 return rdev->reg_data; 3934 } 3935 EXPORT_SYMBOL_GPL(rdev_get_drvdata); 3936 3937 /** 3938 * regulator_get_drvdata - get regulator driver data 3939 * @regulator: regulator 3940 * 3941 * Get regulator driver private data. This call can be used in the consumer 3942 * driver context when non API regulator specific functions need to be called. 3943 */ 3944 void *regulator_get_drvdata(struct regulator *regulator) 3945 { 3946 return regulator->rdev->reg_data; 3947 } 3948 EXPORT_SYMBOL_GPL(regulator_get_drvdata); 3949 3950 /** 3951 * regulator_set_drvdata - set regulator driver data 3952 * @regulator: regulator 3953 * @data: data 3954 */ 3955 void regulator_set_drvdata(struct regulator *regulator, void *data) 3956 { 3957 regulator->rdev->reg_data = data; 3958 } 3959 EXPORT_SYMBOL_GPL(regulator_set_drvdata); 3960 3961 /** 3962 * regulator_get_id - get regulator ID 3963 * @rdev: regulator 3964 */ 3965 int rdev_get_id(struct regulator_dev *rdev) 3966 { 3967 return rdev->desc->id; 3968 } 3969 EXPORT_SYMBOL_GPL(rdev_get_id); 3970 3971 struct device *rdev_get_dev(struct regulator_dev *rdev) 3972 { 3973 return &rdev->dev; 3974 } 3975 EXPORT_SYMBOL_GPL(rdev_get_dev); 3976 3977 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) 3978 { 3979 return reg_init_data->driver_data; 3980 } 3981 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); 3982 3983 #ifdef CONFIG_DEBUG_FS 3984 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf, 3985 size_t count, loff_t *ppos) 3986 { 3987 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); 3988 ssize_t len, ret = 0; 3989 struct regulator_map *map; 3990 3991 if (!buf) 3992 return -ENOMEM; 3993 3994 list_for_each_entry(map, ®ulator_map_list, list) { 3995 len = snprintf(buf + ret, PAGE_SIZE - ret, 3996 "%s -> %s.%s\n", 3997 rdev_get_name(map->regulator), map->dev_name, 3998 map->supply); 3999 if (len >= 0) 4000 ret += len; 4001 if (ret > PAGE_SIZE) { 4002 ret = PAGE_SIZE; 4003 break; 4004 } 4005 } 4006 4007 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); 4008 4009 kfree(buf); 4010 4011 return ret; 4012 } 4013 #endif 4014 4015 static const struct file_operations supply_map_fops = { 4016 #ifdef CONFIG_DEBUG_FS 4017 .read = supply_map_read_file, 4018 .llseek = default_llseek, 4019 #endif 4020 }; 4021 4022 #ifdef CONFIG_DEBUG_FS 4023 static void regulator_summary_show_subtree(struct seq_file *s, 4024 struct regulator_dev *rdev, 4025 int level) 4026 { 4027 struct list_head *list = s->private; 4028 struct regulator_dev *child; 4029 struct regulation_constraints *c; 4030 struct regulator *consumer; 4031 4032 if (!rdev) 4033 return; 4034 4035 seq_printf(s, "%*s%-*s %3d %4d %6d ", 4036 level * 3 + 1, "", 4037 30 - level * 3, rdev_get_name(rdev), 4038 rdev->use_count, rdev->open_count, rdev->bypass_count); 4039 4040 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000); 4041 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000); 4042 4043 c = rdev->constraints; 4044 if (c) { 4045 switch (rdev->desc->type) { 4046 case REGULATOR_VOLTAGE: 4047 seq_printf(s, "%5dmV %5dmV ", 4048 c->min_uV / 1000, c->max_uV / 1000); 4049 break; 4050 case REGULATOR_CURRENT: 4051 seq_printf(s, "%5dmA %5dmA ", 4052 c->min_uA / 1000, c->max_uA / 1000); 4053 break; 4054 } 4055 } 4056 4057 seq_puts(s, "\n"); 4058 4059 list_for_each_entry(consumer, &rdev->consumer_list, list) { 4060 if (consumer->dev->class == ®ulator_class) 4061 continue; 4062 4063 seq_printf(s, "%*s%-*s ", 4064 (level + 1) * 3 + 1, "", 4065 30 - (level + 1) * 3, dev_name(consumer->dev)); 4066 4067 switch (rdev->desc->type) { 4068 case REGULATOR_VOLTAGE: 4069 seq_printf(s, "%37dmV %5dmV", 4070 consumer->min_uV / 1000, 4071 consumer->max_uV / 1000); 4072 break; 4073 case REGULATOR_CURRENT: 4074 break; 4075 } 4076 4077 seq_puts(s, "\n"); 4078 } 4079 4080 list_for_each_entry(child, list, list) { 4081 /* handle only non-root regulators supplied by current rdev */ 4082 if (!child->supply || child->supply->rdev != rdev) 4083 continue; 4084 4085 regulator_summary_show_subtree(s, child, level + 1); 4086 } 4087 } 4088 4089 static int regulator_summary_show(struct seq_file *s, void *data) 4090 { 4091 struct list_head *list = s->private; 4092 struct regulator_dev *rdev; 4093 4094 seq_puts(s, " regulator use open bypass voltage current min max\n"); 4095 seq_puts(s, "-------------------------------------------------------------------------------\n"); 4096 4097 mutex_lock(®ulator_list_mutex); 4098 4099 list_for_each_entry(rdev, list, list) { 4100 if (rdev->supply) 4101 continue; 4102 4103 regulator_summary_show_subtree(s, rdev, 0); 4104 } 4105 4106 mutex_unlock(®ulator_list_mutex); 4107 4108 return 0; 4109 } 4110 4111 static int regulator_summary_open(struct inode *inode, struct file *file) 4112 { 4113 return single_open(file, regulator_summary_show, inode->i_private); 4114 } 4115 #endif 4116 4117 static const struct file_operations regulator_summary_fops = { 4118 #ifdef CONFIG_DEBUG_FS 4119 .open = regulator_summary_open, 4120 .read = seq_read, 4121 .llseek = seq_lseek, 4122 .release = single_release, 4123 #endif 4124 }; 4125 4126 static int __init regulator_init(void) 4127 { 4128 int ret; 4129 4130 ret = class_register(®ulator_class); 4131 4132 debugfs_root = debugfs_create_dir("regulator", NULL); 4133 if (!debugfs_root) 4134 pr_warn("regulator: Failed to create debugfs directory\n"); 4135 4136 debugfs_create_file("supply_map", 0444, debugfs_root, NULL, 4137 &supply_map_fops); 4138 4139 debugfs_create_file("regulator_summary", 0444, debugfs_root, 4140 ®ulator_list, ®ulator_summary_fops); 4141 4142 regulator_dummy_init(); 4143 4144 return ret; 4145 } 4146 4147 /* init early to allow our consumers to complete system booting */ 4148 core_initcall(regulator_init); 4149 4150 static int __init regulator_init_complete(void) 4151 { 4152 struct regulator_dev *rdev; 4153 const struct regulator_ops *ops; 4154 struct regulation_constraints *c; 4155 int enabled, ret; 4156 4157 /* 4158 * Since DT doesn't provide an idiomatic mechanism for 4159 * enabling full constraints and since it's much more natural 4160 * with DT to provide them just assume that a DT enabled 4161 * system has full constraints. 4162 */ 4163 if (of_have_populated_dt()) 4164 has_full_constraints = true; 4165 4166 mutex_lock(®ulator_list_mutex); 4167 4168 /* If we have a full configuration then disable any regulators 4169 * we have permission to change the status for and which are 4170 * not in use or always_on. This is effectively the default 4171 * for DT and ACPI as they have full constraints. 4172 */ 4173 list_for_each_entry(rdev, ®ulator_list, list) { 4174 ops = rdev->desc->ops; 4175 c = rdev->constraints; 4176 4177 if (c && c->always_on) 4178 continue; 4179 4180 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS)) 4181 continue; 4182 4183 mutex_lock(&rdev->mutex); 4184 4185 if (rdev->use_count) 4186 goto unlock; 4187 4188 /* If we can't read the status assume it's on. */ 4189 if (ops->is_enabled) 4190 enabled = ops->is_enabled(rdev); 4191 else 4192 enabled = 1; 4193 4194 if (!enabled) 4195 goto unlock; 4196 4197 if (have_full_constraints()) { 4198 /* We log since this may kill the system if it 4199 * goes wrong. */ 4200 rdev_info(rdev, "disabling\n"); 4201 ret = _regulator_do_disable(rdev); 4202 if (ret != 0) 4203 rdev_err(rdev, "couldn't disable: %d\n", ret); 4204 } else { 4205 /* The intention is that in future we will 4206 * assume that full constraints are provided 4207 * so warn even if we aren't going to do 4208 * anything here. 4209 */ 4210 rdev_warn(rdev, "incomplete constraints, leaving on\n"); 4211 } 4212 4213 unlock: 4214 mutex_unlock(&rdev->mutex); 4215 } 4216 4217 mutex_unlock(®ulator_list_mutex); 4218 4219 return 0; 4220 } 4221 late_initcall_sync(regulator_init_complete); 4222