core.c revision 688fe99a439f7c9dfcc52fbf7cb347f140a2dc8b
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/device.h> 19#include <linux/slab.h> 20#include <linux/err.h> 21#include <linux/mutex.h> 22#include <linux/suspend.h> 23#include <linux/delay.h> 24#include <linux/regulator/consumer.h> 25#include <linux/regulator/driver.h> 26#include <linux/regulator/machine.h> 27 28#include "dummy.h" 29 30#define REGULATOR_VERSION "0.5" 31 32static DEFINE_MUTEX(regulator_list_mutex); 33static LIST_HEAD(regulator_list); 34static LIST_HEAD(regulator_map_list); 35static int has_full_constraints; 36static bool board_wants_dummy_regulator; 37 38/* 39 * struct regulator_map 40 * 41 * Used to provide symbolic supply names to devices. 42 */ 43struct regulator_map { 44 struct list_head list; 45 const char *dev_name; /* The dev_name() for the consumer */ 46 const char *supply; 47 struct regulator_dev *regulator; 48}; 49 50/* 51 * struct regulator 52 * 53 * One for each consumer device. 54 */ 55struct regulator { 56 struct device *dev; 57 struct list_head list; 58 int uA_load; 59 int min_uV; 60 int max_uV; 61 char *supply_name; 62 struct device_attribute dev_attr; 63 struct regulator_dev *rdev; 64}; 65 66static int _regulator_is_enabled(struct regulator_dev *rdev); 67static int _regulator_disable(struct regulator_dev *rdev); 68static int _regulator_get_voltage(struct regulator_dev *rdev); 69static int _regulator_get_current_limit(struct regulator_dev *rdev); 70static unsigned int _regulator_get_mode(struct regulator_dev *rdev); 71static void _notifier_call_chain(struct regulator_dev *rdev, 72 unsigned long event, void *data); 73 74static const char *rdev_get_name(struct regulator_dev *rdev) 75{ 76 if (rdev->constraints && rdev->constraints->name) 77 return rdev->constraints->name; 78 else if (rdev->desc->name) 79 return rdev->desc->name; 80 else 81 return ""; 82} 83 84/* gets the regulator for a given consumer device */ 85static struct regulator *get_device_regulator(struct device *dev) 86{ 87 struct regulator *regulator = NULL; 88 struct regulator_dev *rdev; 89 90 mutex_lock(®ulator_list_mutex); 91 list_for_each_entry(rdev, ®ulator_list, list) { 92 mutex_lock(&rdev->mutex); 93 list_for_each_entry(regulator, &rdev->consumer_list, list) { 94 if (regulator->dev == dev) { 95 mutex_unlock(&rdev->mutex); 96 mutex_unlock(®ulator_list_mutex); 97 return regulator; 98 } 99 } 100 mutex_unlock(&rdev->mutex); 101 } 102 mutex_unlock(®ulator_list_mutex); 103 return NULL; 104} 105 106/* Platform voltage constraint check */ 107static int regulator_check_voltage(struct regulator_dev *rdev, 108 int *min_uV, int *max_uV) 109{ 110 BUG_ON(*min_uV > *max_uV); 111 112 if (!rdev->constraints) { 113 printk(KERN_ERR "%s: no constraints for %s\n", __func__, 114 rdev_get_name(rdev)); 115 return -ENODEV; 116 } 117 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { 118 printk(KERN_ERR "%s: operation not allowed for %s\n", 119 __func__, rdev_get_name(rdev)); 120 return -EPERM; 121 } 122 123 if (*max_uV > rdev->constraints->max_uV) 124 *max_uV = rdev->constraints->max_uV; 125 if (*min_uV < rdev->constraints->min_uV) 126 *min_uV = rdev->constraints->min_uV; 127 128 if (*min_uV > *max_uV) 129 return -EINVAL; 130 131 return 0; 132} 133 134/* current constraint check */ 135static int regulator_check_current_limit(struct regulator_dev *rdev, 136 int *min_uA, int *max_uA) 137{ 138 BUG_ON(*min_uA > *max_uA); 139 140 if (!rdev->constraints) { 141 printk(KERN_ERR "%s: no constraints for %s\n", __func__, 142 rdev_get_name(rdev)); 143 return -ENODEV; 144 } 145 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) { 146 printk(KERN_ERR "%s: operation not allowed for %s\n", 147 __func__, rdev_get_name(rdev)); 148 return -EPERM; 149 } 150 151 if (*max_uA > rdev->constraints->max_uA) 152 *max_uA = rdev->constraints->max_uA; 153 if (*min_uA < rdev->constraints->min_uA) 154 *min_uA = rdev->constraints->min_uA; 155 156 if (*min_uA > *max_uA) 157 return -EINVAL; 158 159 return 0; 160} 161 162/* operating mode constraint check */ 163static int regulator_check_mode(struct regulator_dev *rdev, int mode) 164{ 165 switch (mode) { 166 case REGULATOR_MODE_FAST: 167 case REGULATOR_MODE_NORMAL: 168 case REGULATOR_MODE_IDLE: 169 case REGULATOR_MODE_STANDBY: 170 break; 171 default: 172 return -EINVAL; 173 } 174 175 if (!rdev->constraints) { 176 printk(KERN_ERR "%s: no constraints for %s\n", __func__, 177 rdev_get_name(rdev)); 178 return -ENODEV; 179 } 180 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) { 181 printk(KERN_ERR "%s: operation not allowed for %s\n", 182 __func__, rdev_get_name(rdev)); 183 return -EPERM; 184 } 185 if (!(rdev->constraints->valid_modes_mask & mode)) { 186 printk(KERN_ERR "%s: invalid mode %x for %s\n", 187 __func__, mode, rdev_get_name(rdev)); 188 return -EINVAL; 189 } 190 return 0; 191} 192 193/* dynamic regulator mode switching constraint check */ 194static int regulator_check_drms(struct regulator_dev *rdev) 195{ 196 if (!rdev->constraints) { 197 printk(KERN_ERR "%s: no constraints for %s\n", __func__, 198 rdev_get_name(rdev)); 199 return -ENODEV; 200 } 201 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) { 202 printk(KERN_ERR "%s: operation not allowed for %s\n", 203 __func__, rdev_get_name(rdev)); 204 return -EPERM; 205 } 206 return 0; 207} 208 209static ssize_t device_requested_uA_show(struct device *dev, 210 struct device_attribute *attr, char *buf) 211{ 212 struct regulator *regulator; 213 214 regulator = get_device_regulator(dev); 215 if (regulator == NULL) 216 return 0; 217 218 return sprintf(buf, "%d\n", regulator->uA_load); 219} 220 221static ssize_t regulator_uV_show(struct device *dev, 222 struct device_attribute *attr, char *buf) 223{ 224 struct regulator_dev *rdev = dev_get_drvdata(dev); 225 ssize_t ret; 226 227 mutex_lock(&rdev->mutex); 228 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); 229 mutex_unlock(&rdev->mutex); 230 231 return ret; 232} 233static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); 234 235static ssize_t regulator_uA_show(struct device *dev, 236 struct device_attribute *attr, char *buf) 237{ 238 struct regulator_dev *rdev = dev_get_drvdata(dev); 239 240 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); 241} 242static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); 243 244static ssize_t regulator_name_show(struct device *dev, 245 struct device_attribute *attr, char *buf) 246{ 247 struct regulator_dev *rdev = dev_get_drvdata(dev); 248 249 return sprintf(buf, "%s\n", rdev_get_name(rdev)); 250} 251 252static ssize_t regulator_print_opmode(char *buf, int mode) 253{ 254 switch (mode) { 255 case REGULATOR_MODE_FAST: 256 return sprintf(buf, "fast\n"); 257 case REGULATOR_MODE_NORMAL: 258 return sprintf(buf, "normal\n"); 259 case REGULATOR_MODE_IDLE: 260 return sprintf(buf, "idle\n"); 261 case REGULATOR_MODE_STANDBY: 262 return sprintf(buf, "standby\n"); 263 } 264 return sprintf(buf, "unknown\n"); 265} 266 267static ssize_t regulator_opmode_show(struct device *dev, 268 struct device_attribute *attr, char *buf) 269{ 270 struct regulator_dev *rdev = dev_get_drvdata(dev); 271 272 return regulator_print_opmode(buf, _regulator_get_mode(rdev)); 273} 274static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); 275 276static ssize_t regulator_print_state(char *buf, int state) 277{ 278 if (state > 0) 279 return sprintf(buf, "enabled\n"); 280 else if (state == 0) 281 return sprintf(buf, "disabled\n"); 282 else 283 return sprintf(buf, "unknown\n"); 284} 285 286static ssize_t regulator_state_show(struct device *dev, 287 struct device_attribute *attr, char *buf) 288{ 289 struct regulator_dev *rdev = dev_get_drvdata(dev); 290 ssize_t ret; 291 292 mutex_lock(&rdev->mutex); 293 ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); 294 mutex_unlock(&rdev->mutex); 295 296 return ret; 297} 298static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); 299 300static ssize_t regulator_status_show(struct device *dev, 301 struct device_attribute *attr, char *buf) 302{ 303 struct regulator_dev *rdev = dev_get_drvdata(dev); 304 int status; 305 char *label; 306 307 status = rdev->desc->ops->get_status(rdev); 308 if (status < 0) 309 return status; 310 311 switch (status) { 312 case REGULATOR_STATUS_OFF: 313 label = "off"; 314 break; 315 case REGULATOR_STATUS_ON: 316 label = "on"; 317 break; 318 case REGULATOR_STATUS_ERROR: 319 label = "error"; 320 break; 321 case REGULATOR_STATUS_FAST: 322 label = "fast"; 323 break; 324 case REGULATOR_STATUS_NORMAL: 325 label = "normal"; 326 break; 327 case REGULATOR_STATUS_IDLE: 328 label = "idle"; 329 break; 330 case REGULATOR_STATUS_STANDBY: 331 label = "standby"; 332 break; 333 default: 334 return -ERANGE; 335 } 336 337 return sprintf(buf, "%s\n", label); 338} 339static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); 340 341static ssize_t regulator_min_uA_show(struct device *dev, 342 struct device_attribute *attr, char *buf) 343{ 344 struct regulator_dev *rdev = dev_get_drvdata(dev); 345 346 if (!rdev->constraints) 347 return sprintf(buf, "constraint not defined\n"); 348 349 return sprintf(buf, "%d\n", rdev->constraints->min_uA); 350} 351static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); 352 353static ssize_t regulator_max_uA_show(struct device *dev, 354 struct device_attribute *attr, char *buf) 355{ 356 struct regulator_dev *rdev = dev_get_drvdata(dev); 357 358 if (!rdev->constraints) 359 return sprintf(buf, "constraint not defined\n"); 360 361 return sprintf(buf, "%d\n", rdev->constraints->max_uA); 362} 363static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); 364 365static ssize_t regulator_min_uV_show(struct device *dev, 366 struct device_attribute *attr, char *buf) 367{ 368 struct regulator_dev *rdev = dev_get_drvdata(dev); 369 370 if (!rdev->constraints) 371 return sprintf(buf, "constraint not defined\n"); 372 373 return sprintf(buf, "%d\n", rdev->constraints->min_uV); 374} 375static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); 376 377static ssize_t regulator_max_uV_show(struct device *dev, 378 struct device_attribute *attr, char *buf) 379{ 380 struct regulator_dev *rdev = dev_get_drvdata(dev); 381 382 if (!rdev->constraints) 383 return sprintf(buf, "constraint not defined\n"); 384 385 return sprintf(buf, "%d\n", rdev->constraints->max_uV); 386} 387static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); 388 389static ssize_t regulator_total_uA_show(struct device *dev, 390 struct device_attribute *attr, char *buf) 391{ 392 struct regulator_dev *rdev = dev_get_drvdata(dev); 393 struct regulator *regulator; 394 int uA = 0; 395 396 mutex_lock(&rdev->mutex); 397 list_for_each_entry(regulator, &rdev->consumer_list, list) 398 uA += regulator->uA_load; 399 mutex_unlock(&rdev->mutex); 400 return sprintf(buf, "%d\n", uA); 401} 402static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); 403 404static ssize_t regulator_num_users_show(struct device *dev, 405 struct device_attribute *attr, char *buf) 406{ 407 struct regulator_dev *rdev = dev_get_drvdata(dev); 408 return sprintf(buf, "%d\n", rdev->use_count); 409} 410 411static ssize_t regulator_type_show(struct device *dev, 412 struct device_attribute *attr, char *buf) 413{ 414 struct regulator_dev *rdev = dev_get_drvdata(dev); 415 416 switch (rdev->desc->type) { 417 case REGULATOR_VOLTAGE: 418 return sprintf(buf, "voltage\n"); 419 case REGULATOR_CURRENT: 420 return sprintf(buf, "current\n"); 421 } 422 return sprintf(buf, "unknown\n"); 423} 424 425static ssize_t regulator_suspend_mem_uV_show(struct device *dev, 426 struct device_attribute *attr, char *buf) 427{ 428 struct regulator_dev *rdev = dev_get_drvdata(dev); 429 430 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); 431} 432static DEVICE_ATTR(suspend_mem_microvolts, 0444, 433 regulator_suspend_mem_uV_show, NULL); 434 435static ssize_t regulator_suspend_disk_uV_show(struct device *dev, 436 struct device_attribute *attr, char *buf) 437{ 438 struct regulator_dev *rdev = dev_get_drvdata(dev); 439 440 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); 441} 442static DEVICE_ATTR(suspend_disk_microvolts, 0444, 443 regulator_suspend_disk_uV_show, NULL); 444 445static ssize_t regulator_suspend_standby_uV_show(struct device *dev, 446 struct device_attribute *attr, char *buf) 447{ 448 struct regulator_dev *rdev = dev_get_drvdata(dev); 449 450 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); 451} 452static DEVICE_ATTR(suspend_standby_microvolts, 0444, 453 regulator_suspend_standby_uV_show, NULL); 454 455static ssize_t regulator_suspend_mem_mode_show(struct device *dev, 456 struct device_attribute *attr, char *buf) 457{ 458 struct regulator_dev *rdev = dev_get_drvdata(dev); 459 460 return regulator_print_opmode(buf, 461 rdev->constraints->state_mem.mode); 462} 463static DEVICE_ATTR(suspend_mem_mode, 0444, 464 regulator_suspend_mem_mode_show, NULL); 465 466static ssize_t regulator_suspend_disk_mode_show(struct device *dev, 467 struct device_attribute *attr, char *buf) 468{ 469 struct regulator_dev *rdev = dev_get_drvdata(dev); 470 471 return regulator_print_opmode(buf, 472 rdev->constraints->state_disk.mode); 473} 474static DEVICE_ATTR(suspend_disk_mode, 0444, 475 regulator_suspend_disk_mode_show, NULL); 476 477static ssize_t regulator_suspend_standby_mode_show(struct device *dev, 478 struct device_attribute *attr, char *buf) 479{ 480 struct regulator_dev *rdev = dev_get_drvdata(dev); 481 482 return regulator_print_opmode(buf, 483 rdev->constraints->state_standby.mode); 484} 485static DEVICE_ATTR(suspend_standby_mode, 0444, 486 regulator_suspend_standby_mode_show, NULL); 487 488static ssize_t regulator_suspend_mem_state_show(struct device *dev, 489 struct device_attribute *attr, char *buf) 490{ 491 struct regulator_dev *rdev = dev_get_drvdata(dev); 492 493 return regulator_print_state(buf, 494 rdev->constraints->state_mem.enabled); 495} 496static DEVICE_ATTR(suspend_mem_state, 0444, 497 regulator_suspend_mem_state_show, NULL); 498 499static ssize_t regulator_suspend_disk_state_show(struct device *dev, 500 struct device_attribute *attr, char *buf) 501{ 502 struct regulator_dev *rdev = dev_get_drvdata(dev); 503 504 return regulator_print_state(buf, 505 rdev->constraints->state_disk.enabled); 506} 507static DEVICE_ATTR(suspend_disk_state, 0444, 508 regulator_suspend_disk_state_show, NULL); 509 510static ssize_t regulator_suspend_standby_state_show(struct device *dev, 511 struct device_attribute *attr, char *buf) 512{ 513 struct regulator_dev *rdev = dev_get_drvdata(dev); 514 515 return regulator_print_state(buf, 516 rdev->constraints->state_standby.enabled); 517} 518static DEVICE_ATTR(suspend_standby_state, 0444, 519 regulator_suspend_standby_state_show, NULL); 520 521 522/* 523 * These are the only attributes are present for all regulators. 524 * Other attributes are a function of regulator functionality. 525 */ 526static struct device_attribute regulator_dev_attrs[] = { 527 __ATTR(name, 0444, regulator_name_show, NULL), 528 __ATTR(num_users, 0444, regulator_num_users_show, NULL), 529 __ATTR(type, 0444, regulator_type_show, NULL), 530 __ATTR_NULL, 531}; 532 533static void regulator_dev_release(struct device *dev) 534{ 535 struct regulator_dev *rdev = dev_get_drvdata(dev); 536 kfree(rdev); 537} 538 539static struct class regulator_class = { 540 .name = "regulator", 541 .dev_release = regulator_dev_release, 542 .dev_attrs = regulator_dev_attrs, 543}; 544 545/* Calculate the new optimum regulator operating mode based on the new total 546 * consumer load. All locks held by caller */ 547static void drms_uA_update(struct regulator_dev *rdev) 548{ 549 struct regulator *sibling; 550 int current_uA = 0, output_uV, input_uV, err; 551 unsigned int mode; 552 553 err = regulator_check_drms(rdev); 554 if (err < 0 || !rdev->desc->ops->get_optimum_mode || 555 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode) 556 return; 557 558 /* get output voltage */ 559 output_uV = rdev->desc->ops->get_voltage(rdev); 560 if (output_uV <= 0) 561 return; 562 563 /* get input voltage */ 564 if (rdev->supply && rdev->supply->desc->ops->get_voltage) 565 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); 566 else 567 input_uV = rdev->constraints->input_uV; 568 if (input_uV <= 0) 569 return; 570 571 /* calc total requested load */ 572 list_for_each_entry(sibling, &rdev->consumer_list, list) 573 current_uA += sibling->uA_load; 574 575 /* now get the optimum mode for our new total regulator load */ 576 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, 577 output_uV, current_uA); 578 579 /* check the new mode is allowed */ 580 err = regulator_check_mode(rdev, mode); 581 if (err == 0) 582 rdev->desc->ops->set_mode(rdev, mode); 583} 584 585static int suspend_set_state(struct regulator_dev *rdev, 586 struct regulator_state *rstate) 587{ 588 int ret = 0; 589 bool can_set_state; 590 591 can_set_state = rdev->desc->ops->set_suspend_enable && 592 rdev->desc->ops->set_suspend_disable; 593 594 /* If we have no suspend mode configration don't set anything; 595 * only warn if the driver actually makes the suspend mode 596 * configurable. 597 */ 598 if (!rstate->enabled && !rstate->disabled) { 599 if (can_set_state) 600 printk(KERN_WARNING "%s: No configuration for %s\n", 601 __func__, rdev_get_name(rdev)); 602 return 0; 603 } 604 605 if (rstate->enabled && rstate->disabled) { 606 printk(KERN_ERR "%s: invalid configuration for %s\n", 607 __func__, rdev_get_name(rdev)); 608 return -EINVAL; 609 } 610 611 if (!can_set_state) { 612 printk(KERN_ERR "%s: no way to set suspend state\n", 613 __func__); 614 return -EINVAL; 615 } 616 617 if (rstate->enabled) 618 ret = rdev->desc->ops->set_suspend_enable(rdev); 619 else 620 ret = rdev->desc->ops->set_suspend_disable(rdev); 621 if (ret < 0) { 622 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__); 623 return ret; 624 } 625 626 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { 627 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); 628 if (ret < 0) { 629 printk(KERN_ERR "%s: failed to set voltage\n", 630 __func__); 631 return ret; 632 } 633 } 634 635 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { 636 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); 637 if (ret < 0) { 638 printk(KERN_ERR "%s: failed to set mode\n", __func__); 639 return ret; 640 } 641 } 642 return ret; 643} 644 645/* locks held by caller */ 646static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state) 647{ 648 if (!rdev->constraints) 649 return -EINVAL; 650 651 switch (state) { 652 case PM_SUSPEND_STANDBY: 653 return suspend_set_state(rdev, 654 &rdev->constraints->state_standby); 655 case PM_SUSPEND_MEM: 656 return suspend_set_state(rdev, 657 &rdev->constraints->state_mem); 658 case PM_SUSPEND_MAX: 659 return suspend_set_state(rdev, 660 &rdev->constraints->state_disk); 661 default: 662 return -EINVAL; 663 } 664} 665 666static void print_constraints(struct regulator_dev *rdev) 667{ 668 struct regulation_constraints *constraints = rdev->constraints; 669 char buf[80] = ""; 670 int count = 0; 671 int ret; 672 673 if (constraints->min_uV && constraints->max_uV) { 674 if (constraints->min_uV == constraints->max_uV) 675 count += sprintf(buf + count, "%d mV ", 676 constraints->min_uV / 1000); 677 else 678 count += sprintf(buf + count, "%d <--> %d mV ", 679 constraints->min_uV / 1000, 680 constraints->max_uV / 1000); 681 } 682 683 if (!constraints->min_uV || 684 constraints->min_uV != constraints->max_uV) { 685 ret = _regulator_get_voltage(rdev); 686 if (ret > 0) 687 count += sprintf(buf + count, "at %d mV ", ret / 1000); 688 } 689 690 if (constraints->min_uA && constraints->max_uA) { 691 if (constraints->min_uA == constraints->max_uA) 692 count += sprintf(buf + count, "%d mA ", 693 constraints->min_uA / 1000); 694 else 695 count += sprintf(buf + count, "%d <--> %d mA ", 696 constraints->min_uA / 1000, 697 constraints->max_uA / 1000); 698 } 699 700 if (!constraints->min_uA || 701 constraints->min_uA != constraints->max_uA) { 702 ret = _regulator_get_current_limit(rdev); 703 if (ret > 0) 704 count += sprintf(buf + count, "at %d mA ", ret / 1000); 705 } 706 707 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) 708 count += sprintf(buf + count, "fast "); 709 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) 710 count += sprintf(buf + count, "normal "); 711 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) 712 count += sprintf(buf + count, "idle "); 713 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) 714 count += sprintf(buf + count, "standby"); 715 716 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf); 717} 718 719static int machine_constraints_voltage(struct regulator_dev *rdev, 720 struct regulation_constraints *constraints) 721{ 722 struct regulator_ops *ops = rdev->desc->ops; 723 const char *name = rdev_get_name(rdev); 724 int ret; 725 726 /* do we need to apply the constraint voltage */ 727 if (rdev->constraints->apply_uV && 728 rdev->constraints->min_uV == rdev->constraints->max_uV && 729 ops->set_voltage) { 730 ret = ops->set_voltage(rdev, 731 rdev->constraints->min_uV, rdev->constraints->max_uV); 732 if (ret < 0) { 733 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n", 734 __func__, 735 rdev->constraints->min_uV, name); 736 rdev->constraints = NULL; 737 return ret; 738 } 739 } 740 741 /* constrain machine-level voltage specs to fit 742 * the actual range supported by this regulator. 743 */ 744 if (ops->list_voltage && rdev->desc->n_voltages) { 745 int count = rdev->desc->n_voltages; 746 int i; 747 int min_uV = INT_MAX; 748 int max_uV = INT_MIN; 749 int cmin = constraints->min_uV; 750 int cmax = constraints->max_uV; 751 752 /* it's safe to autoconfigure fixed-voltage supplies 753 and the constraints are used by list_voltage. */ 754 if (count == 1 && !cmin) { 755 cmin = 1; 756 cmax = INT_MAX; 757 constraints->min_uV = cmin; 758 constraints->max_uV = cmax; 759 } 760 761 /* voltage constraints are optional */ 762 if ((cmin == 0) && (cmax == 0)) 763 return 0; 764 765 /* else require explicit machine-level constraints */ 766 if (cmin <= 0 || cmax <= 0 || cmax < cmin) { 767 pr_err("%s: %s '%s' voltage constraints\n", 768 __func__, "invalid", name); 769 return -EINVAL; 770 } 771 772 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ 773 for (i = 0; i < count; i++) { 774 int value; 775 776 value = ops->list_voltage(rdev, i); 777 if (value <= 0) 778 continue; 779 780 /* maybe adjust [min_uV..max_uV] */ 781 if (value >= cmin && value < min_uV) 782 min_uV = value; 783 if (value <= cmax && value > max_uV) 784 max_uV = value; 785 } 786 787 /* final: [min_uV..max_uV] valid iff constraints valid */ 788 if (max_uV < min_uV) { 789 pr_err("%s: %s '%s' voltage constraints\n", 790 __func__, "unsupportable", name); 791 return -EINVAL; 792 } 793 794 /* use regulator's subset of machine constraints */ 795 if (constraints->min_uV < min_uV) { 796 pr_debug("%s: override '%s' %s, %d -> %d\n", 797 __func__, name, "min_uV", 798 constraints->min_uV, min_uV); 799 constraints->min_uV = min_uV; 800 } 801 if (constraints->max_uV > max_uV) { 802 pr_debug("%s: override '%s' %s, %d -> %d\n", 803 __func__, name, "max_uV", 804 constraints->max_uV, max_uV); 805 constraints->max_uV = max_uV; 806 } 807 } 808 809 return 0; 810} 811 812/** 813 * set_machine_constraints - sets regulator constraints 814 * @rdev: regulator source 815 * @constraints: constraints to apply 816 * 817 * Allows platform initialisation code to define and constrain 818 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: 819 * Constraints *must* be set by platform code in order for some 820 * regulator operations to proceed i.e. set_voltage, set_current_limit, 821 * set_mode. 822 */ 823static int set_machine_constraints(struct regulator_dev *rdev, 824 struct regulation_constraints *constraints) 825{ 826 int ret = 0; 827 const char *name; 828 struct regulator_ops *ops = rdev->desc->ops; 829 830 rdev->constraints = constraints; 831 832 name = rdev_get_name(rdev); 833 834 ret = machine_constraints_voltage(rdev, constraints); 835 if (ret != 0) 836 goto out; 837 838 /* do we need to setup our suspend state */ 839 if (constraints->initial_state) { 840 ret = suspend_prepare(rdev, constraints->initial_state); 841 if (ret < 0) { 842 printk(KERN_ERR "%s: failed to set suspend state for %s\n", 843 __func__, name); 844 rdev->constraints = NULL; 845 goto out; 846 } 847 } 848 849 if (constraints->initial_mode) { 850 if (!ops->set_mode) { 851 printk(KERN_ERR "%s: no set_mode operation for %s\n", 852 __func__, name); 853 ret = -EINVAL; 854 goto out; 855 } 856 857 ret = ops->set_mode(rdev, constraints->initial_mode); 858 if (ret < 0) { 859 printk(KERN_ERR 860 "%s: failed to set initial mode for %s: %d\n", 861 __func__, name, ret); 862 goto out; 863 } 864 } 865 866 /* If the constraints say the regulator should be on at this point 867 * and we have control then make sure it is enabled. 868 */ 869 if ((constraints->always_on || constraints->boot_on) && ops->enable) { 870 ret = ops->enable(rdev); 871 if (ret < 0) { 872 printk(KERN_ERR "%s: failed to enable %s\n", 873 __func__, name); 874 rdev->constraints = NULL; 875 goto out; 876 } 877 } 878 879 print_constraints(rdev); 880out: 881 return ret; 882} 883 884/** 885 * set_supply - set regulator supply regulator 886 * @rdev: regulator name 887 * @supply_rdev: supply regulator name 888 * 889 * Called by platform initialisation code to set the supply regulator for this 890 * regulator. This ensures that a regulators supply will also be enabled by the 891 * core if it's child is enabled. 892 */ 893static int set_supply(struct regulator_dev *rdev, 894 struct regulator_dev *supply_rdev) 895{ 896 int err; 897 898 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj, 899 "supply"); 900 if (err) { 901 printk(KERN_ERR 902 "%s: could not add device link %s err %d\n", 903 __func__, supply_rdev->dev.kobj.name, err); 904 goto out; 905 } 906 rdev->supply = supply_rdev; 907 list_add(&rdev->slist, &supply_rdev->supply_list); 908out: 909 return err; 910} 911 912/** 913 * set_consumer_device_supply: Bind a regulator to a symbolic supply 914 * @rdev: regulator source 915 * @consumer_dev: device the supply applies to 916 * @consumer_dev_name: dev_name() string for device supply applies to 917 * @supply: symbolic name for supply 918 * 919 * Allows platform initialisation code to map physical regulator 920 * sources to symbolic names for supplies for use by devices. Devices 921 * should use these symbolic names to request regulators, avoiding the 922 * need to provide board-specific regulator names as platform data. 923 * 924 * Only one of consumer_dev and consumer_dev_name may be specified. 925 */ 926static int set_consumer_device_supply(struct regulator_dev *rdev, 927 struct device *consumer_dev, const char *consumer_dev_name, 928 const char *supply) 929{ 930 struct regulator_map *node; 931 int has_dev; 932 933 if (consumer_dev && consumer_dev_name) 934 return -EINVAL; 935 936 if (!consumer_dev_name && consumer_dev) 937 consumer_dev_name = dev_name(consumer_dev); 938 939 if (supply == NULL) 940 return -EINVAL; 941 942 if (consumer_dev_name != NULL) 943 has_dev = 1; 944 else 945 has_dev = 0; 946 947 list_for_each_entry(node, ®ulator_map_list, list) { 948 if (node->dev_name && consumer_dev_name) { 949 if (strcmp(node->dev_name, consumer_dev_name) != 0) 950 continue; 951 } else if (node->dev_name || consumer_dev_name) { 952 continue; 953 } 954 955 if (strcmp(node->supply, supply) != 0) 956 continue; 957 958 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n", 959 dev_name(&node->regulator->dev), 960 node->regulator->desc->name, 961 supply, 962 dev_name(&rdev->dev), rdev_get_name(rdev)); 963 return -EBUSY; 964 } 965 966 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); 967 if (node == NULL) 968 return -ENOMEM; 969 970 node->regulator = rdev; 971 node->supply = supply; 972 973 if (has_dev) { 974 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); 975 if (node->dev_name == NULL) { 976 kfree(node); 977 return -ENOMEM; 978 } 979 } 980 981 list_add(&node->list, ®ulator_map_list); 982 return 0; 983} 984 985static void unset_regulator_supplies(struct regulator_dev *rdev) 986{ 987 struct regulator_map *node, *n; 988 989 list_for_each_entry_safe(node, n, ®ulator_map_list, list) { 990 if (rdev == node->regulator) { 991 list_del(&node->list); 992 kfree(node->dev_name); 993 kfree(node); 994 } 995 } 996} 997 998#define REG_STR_SIZE 32 999 1000static struct regulator *create_regulator(struct regulator_dev *rdev, 1001 struct device *dev, 1002 const char *supply_name) 1003{ 1004 struct regulator *regulator; 1005 char buf[REG_STR_SIZE]; 1006 int err, size; 1007 1008 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); 1009 if (regulator == NULL) 1010 return NULL; 1011 1012 mutex_lock(&rdev->mutex); 1013 regulator->rdev = rdev; 1014 list_add(®ulator->list, &rdev->consumer_list); 1015 1016 if (dev) { 1017 /* create a 'requested_microamps_name' sysfs entry */ 1018 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s", 1019 supply_name); 1020 if (size >= REG_STR_SIZE) 1021 goto overflow_err; 1022 1023 regulator->dev = dev; 1024 sysfs_attr_init(®ulator->dev_attr.attr); 1025 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL); 1026 if (regulator->dev_attr.attr.name == NULL) 1027 goto attr_name_err; 1028 1029 regulator->dev_attr.attr.mode = 0444; 1030 regulator->dev_attr.show = device_requested_uA_show; 1031 err = device_create_file(dev, ®ulator->dev_attr); 1032 if (err < 0) { 1033 printk(KERN_WARNING "%s: could not add regulator_dev" 1034 " load sysfs\n", __func__); 1035 goto attr_name_err; 1036 } 1037 1038 /* also add a link to the device sysfs entry */ 1039 size = scnprintf(buf, REG_STR_SIZE, "%s-%s", 1040 dev->kobj.name, supply_name); 1041 if (size >= REG_STR_SIZE) 1042 goto attr_err; 1043 1044 regulator->supply_name = kstrdup(buf, GFP_KERNEL); 1045 if (regulator->supply_name == NULL) 1046 goto attr_err; 1047 1048 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj, 1049 buf); 1050 if (err) { 1051 printk(KERN_WARNING 1052 "%s: could not add device link %s err %d\n", 1053 __func__, dev->kobj.name, err); 1054 device_remove_file(dev, ®ulator->dev_attr); 1055 goto link_name_err; 1056 } 1057 } 1058 mutex_unlock(&rdev->mutex); 1059 return regulator; 1060link_name_err: 1061 kfree(regulator->supply_name); 1062attr_err: 1063 device_remove_file(regulator->dev, ®ulator->dev_attr); 1064attr_name_err: 1065 kfree(regulator->dev_attr.attr.name); 1066overflow_err: 1067 list_del(®ulator->list); 1068 kfree(regulator); 1069 mutex_unlock(&rdev->mutex); 1070 return NULL; 1071} 1072 1073static int _regulator_get_enable_time(struct regulator_dev *rdev) 1074{ 1075 if (!rdev->desc->ops->enable_time) 1076 return 0; 1077 return rdev->desc->ops->enable_time(rdev); 1078} 1079 1080/* Internal regulator request function */ 1081static struct regulator *_regulator_get(struct device *dev, const char *id, 1082 int exclusive) 1083{ 1084 struct regulator_dev *rdev; 1085 struct regulator_map *map; 1086 struct regulator *regulator = ERR_PTR(-ENODEV); 1087 const char *devname = NULL; 1088 int ret; 1089 1090 if (id == NULL) { 1091 printk(KERN_ERR "regulator: get() with no identifier\n"); 1092 return regulator; 1093 } 1094 1095 if (dev) 1096 devname = dev_name(dev); 1097 1098 mutex_lock(®ulator_list_mutex); 1099 1100 list_for_each_entry(map, ®ulator_map_list, list) { 1101 /* If the mapping has a device set up it must match */ 1102 if (map->dev_name && 1103 (!devname || strcmp(map->dev_name, devname))) 1104 continue; 1105 1106 if (strcmp(map->supply, id) == 0) { 1107 rdev = map->regulator; 1108 goto found; 1109 } 1110 } 1111 1112 if (board_wants_dummy_regulator) { 1113 rdev = dummy_regulator_rdev; 1114 goto found; 1115 } 1116 1117#ifdef CONFIG_REGULATOR_DUMMY 1118 if (!devname) 1119 devname = "deviceless"; 1120 1121 /* If the board didn't flag that it was fully constrained then 1122 * substitute in a dummy regulator so consumers can continue. 1123 */ 1124 if (!has_full_constraints) { 1125 pr_warning("%s supply %s not found, using dummy regulator\n", 1126 devname, id); 1127 rdev = dummy_regulator_rdev; 1128 goto found; 1129 } 1130#endif 1131 1132 mutex_unlock(®ulator_list_mutex); 1133 return regulator; 1134 1135found: 1136 if (rdev->exclusive) { 1137 regulator = ERR_PTR(-EPERM); 1138 goto out; 1139 } 1140 1141 if (exclusive && rdev->open_count) { 1142 regulator = ERR_PTR(-EBUSY); 1143 goto out; 1144 } 1145 1146 if (!try_module_get(rdev->owner)) 1147 goto out; 1148 1149 regulator = create_regulator(rdev, dev, id); 1150 if (regulator == NULL) { 1151 regulator = ERR_PTR(-ENOMEM); 1152 module_put(rdev->owner); 1153 } 1154 1155 rdev->open_count++; 1156 if (exclusive) { 1157 rdev->exclusive = 1; 1158 1159 ret = _regulator_is_enabled(rdev); 1160 if (ret > 0) 1161 rdev->use_count = 1; 1162 else 1163 rdev->use_count = 0; 1164 } 1165 1166out: 1167 mutex_unlock(®ulator_list_mutex); 1168 1169 return regulator; 1170} 1171 1172/** 1173 * regulator_get - lookup and obtain a reference to a regulator. 1174 * @dev: device for regulator "consumer" 1175 * @id: Supply name or regulator ID. 1176 * 1177 * Returns a struct regulator corresponding to the regulator producer, 1178 * or IS_ERR() condition containing errno. 1179 * 1180 * Use of supply names configured via regulator_set_device_supply() is 1181 * strongly encouraged. It is recommended that the supply name used 1182 * should match the name used for the supply and/or the relevant 1183 * device pins in the datasheet. 1184 */ 1185struct regulator *regulator_get(struct device *dev, const char *id) 1186{ 1187 return _regulator_get(dev, id, 0); 1188} 1189EXPORT_SYMBOL_GPL(regulator_get); 1190 1191/** 1192 * regulator_get_exclusive - obtain exclusive access to a regulator. 1193 * @dev: device for regulator "consumer" 1194 * @id: Supply name or regulator ID. 1195 * 1196 * Returns a struct regulator corresponding to the regulator producer, 1197 * or IS_ERR() condition containing errno. Other consumers will be 1198 * unable to obtain this reference is held and the use count for the 1199 * regulator will be initialised to reflect the current state of the 1200 * regulator. 1201 * 1202 * This is intended for use by consumers which cannot tolerate shared 1203 * use of the regulator such as those which need to force the 1204 * regulator off for correct operation of the hardware they are 1205 * controlling. 1206 * 1207 * Use of supply names configured via regulator_set_device_supply() is 1208 * strongly encouraged. It is recommended that the supply name used 1209 * should match the name used for the supply and/or the relevant 1210 * device pins in the datasheet. 1211 */ 1212struct regulator *regulator_get_exclusive(struct device *dev, const char *id) 1213{ 1214 return _regulator_get(dev, id, 1); 1215} 1216EXPORT_SYMBOL_GPL(regulator_get_exclusive); 1217 1218/** 1219 * regulator_put - "free" the regulator source 1220 * @regulator: regulator source 1221 * 1222 * Note: drivers must ensure that all regulator_enable calls made on this 1223 * regulator source are balanced by regulator_disable calls prior to calling 1224 * this function. 1225 */ 1226void regulator_put(struct regulator *regulator) 1227{ 1228 struct regulator_dev *rdev; 1229 1230 if (regulator == NULL || IS_ERR(regulator)) 1231 return; 1232 1233 mutex_lock(®ulator_list_mutex); 1234 rdev = regulator->rdev; 1235 1236 /* remove any sysfs entries */ 1237 if (regulator->dev) { 1238 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); 1239 kfree(regulator->supply_name); 1240 device_remove_file(regulator->dev, ®ulator->dev_attr); 1241 kfree(regulator->dev_attr.attr.name); 1242 } 1243 list_del(®ulator->list); 1244 kfree(regulator); 1245 1246 rdev->open_count--; 1247 rdev->exclusive = 0; 1248 1249 module_put(rdev->owner); 1250 mutex_unlock(®ulator_list_mutex); 1251} 1252EXPORT_SYMBOL_GPL(regulator_put); 1253 1254static int _regulator_can_change_status(struct regulator_dev *rdev) 1255{ 1256 if (!rdev->constraints) 1257 return 0; 1258 1259 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS) 1260 return 1; 1261 else 1262 return 0; 1263} 1264 1265/* locks held by regulator_enable() */ 1266static int _regulator_enable(struct regulator_dev *rdev) 1267{ 1268 int ret, delay; 1269 1270 /* do we need to enable the supply regulator first */ 1271 if (rdev->supply) { 1272 ret = _regulator_enable(rdev->supply); 1273 if (ret < 0) { 1274 printk(KERN_ERR "%s: failed to enable %s: %d\n", 1275 __func__, rdev_get_name(rdev), ret); 1276 return ret; 1277 } 1278 } 1279 1280 /* check voltage and requested load before enabling */ 1281 if (rdev->constraints && 1282 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) 1283 drms_uA_update(rdev); 1284 1285 if (rdev->use_count == 0) { 1286 /* The regulator may on if it's not switchable or left on */ 1287 ret = _regulator_is_enabled(rdev); 1288 if (ret == -EINVAL || ret == 0) { 1289 if (!_regulator_can_change_status(rdev)) 1290 return -EPERM; 1291 1292 if (!rdev->desc->ops->enable) 1293 return -EINVAL; 1294 1295 /* Query before enabling in case configuration 1296 * dependant. */ 1297 ret = _regulator_get_enable_time(rdev); 1298 if (ret >= 0) { 1299 delay = ret; 1300 } else { 1301 printk(KERN_WARNING 1302 "%s: enable_time() failed for %s: %d\n", 1303 __func__, rdev_get_name(rdev), 1304 ret); 1305 delay = 0; 1306 } 1307 1308 /* Allow the regulator to ramp; it would be useful 1309 * to extend this for bulk operations so that the 1310 * regulators can ramp together. */ 1311 ret = rdev->desc->ops->enable(rdev); 1312 if (ret < 0) 1313 return ret; 1314 1315 if (delay >= 1000) 1316 mdelay(delay / 1000); 1317 else if (delay) 1318 udelay(delay); 1319 1320 } else if (ret < 0) { 1321 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n", 1322 __func__, rdev_get_name(rdev), ret); 1323 return ret; 1324 } 1325 /* Fallthrough on positive return values - already enabled */ 1326 } 1327 1328 rdev->use_count++; 1329 1330 return 0; 1331} 1332 1333/** 1334 * regulator_enable - enable regulator output 1335 * @regulator: regulator source 1336 * 1337 * Request that the regulator be enabled with the regulator output at 1338 * the predefined voltage or current value. Calls to regulator_enable() 1339 * must be balanced with calls to regulator_disable(). 1340 * 1341 * NOTE: the output value can be set by other drivers, boot loader or may be 1342 * hardwired in the regulator. 1343 */ 1344int regulator_enable(struct regulator *regulator) 1345{ 1346 struct regulator_dev *rdev = regulator->rdev; 1347 int ret = 0; 1348 1349 mutex_lock(&rdev->mutex); 1350 ret = _regulator_enable(rdev); 1351 mutex_unlock(&rdev->mutex); 1352 return ret; 1353} 1354EXPORT_SYMBOL_GPL(regulator_enable); 1355 1356/* locks held by regulator_disable() */ 1357static int _regulator_disable(struct regulator_dev *rdev) 1358{ 1359 int ret = 0; 1360 1361 if (WARN(rdev->use_count <= 0, 1362 "unbalanced disables for %s\n", 1363 rdev_get_name(rdev))) 1364 return -EIO; 1365 1366 /* are we the last user and permitted to disable ? */ 1367 if (rdev->use_count == 1 && 1368 (rdev->constraints && !rdev->constraints->always_on)) { 1369 1370 /* we are last user */ 1371 if (_regulator_can_change_status(rdev) && 1372 rdev->desc->ops->disable) { 1373 ret = rdev->desc->ops->disable(rdev); 1374 if (ret < 0) { 1375 printk(KERN_ERR "%s: failed to disable %s\n", 1376 __func__, rdev_get_name(rdev)); 1377 return ret; 1378 } 1379 1380 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, 1381 NULL); 1382 } 1383 1384 /* decrease our supplies ref count and disable if required */ 1385 if (rdev->supply) 1386 _regulator_disable(rdev->supply); 1387 1388 rdev->use_count = 0; 1389 } else if (rdev->use_count > 1) { 1390 1391 if (rdev->constraints && 1392 (rdev->constraints->valid_ops_mask & 1393 REGULATOR_CHANGE_DRMS)) 1394 drms_uA_update(rdev); 1395 1396 rdev->use_count--; 1397 } 1398 return ret; 1399} 1400 1401/** 1402 * regulator_disable - disable regulator output 1403 * @regulator: regulator source 1404 * 1405 * Disable the regulator output voltage or current. Calls to 1406 * regulator_enable() must be balanced with calls to 1407 * regulator_disable(). 1408 * 1409 * NOTE: this will only disable the regulator output if no other consumer 1410 * devices have it enabled, the regulator device supports disabling and 1411 * machine constraints permit this operation. 1412 */ 1413int regulator_disable(struct regulator *regulator) 1414{ 1415 struct regulator_dev *rdev = regulator->rdev; 1416 int ret = 0; 1417 1418 mutex_lock(&rdev->mutex); 1419 ret = _regulator_disable(rdev); 1420 mutex_unlock(&rdev->mutex); 1421 return ret; 1422} 1423EXPORT_SYMBOL_GPL(regulator_disable); 1424 1425/* locks held by regulator_force_disable() */ 1426static int _regulator_force_disable(struct regulator_dev *rdev) 1427{ 1428 int ret = 0; 1429 1430 /* force disable */ 1431 if (rdev->desc->ops->disable) { 1432 /* ah well, who wants to live forever... */ 1433 ret = rdev->desc->ops->disable(rdev); 1434 if (ret < 0) { 1435 printk(KERN_ERR "%s: failed to force disable %s\n", 1436 __func__, rdev_get_name(rdev)); 1437 return ret; 1438 } 1439 /* notify other consumers that power has been forced off */ 1440 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | 1441 REGULATOR_EVENT_DISABLE, NULL); 1442 } 1443 1444 /* decrease our supplies ref count and disable if required */ 1445 if (rdev->supply) 1446 _regulator_disable(rdev->supply); 1447 1448 rdev->use_count = 0; 1449 return ret; 1450} 1451 1452/** 1453 * regulator_force_disable - force disable regulator output 1454 * @regulator: regulator source 1455 * 1456 * Forcibly disable the regulator output voltage or current. 1457 * NOTE: this *will* disable the regulator output even if other consumer 1458 * devices have it enabled. This should be used for situations when device 1459 * damage will likely occur if the regulator is not disabled (e.g. over temp). 1460 */ 1461int regulator_force_disable(struct regulator *regulator) 1462{ 1463 int ret; 1464 1465 mutex_lock(®ulator->rdev->mutex); 1466 regulator->uA_load = 0; 1467 ret = _regulator_force_disable(regulator->rdev); 1468 mutex_unlock(®ulator->rdev->mutex); 1469 return ret; 1470} 1471EXPORT_SYMBOL_GPL(regulator_force_disable); 1472 1473static int _regulator_is_enabled(struct regulator_dev *rdev) 1474{ 1475 /* If we don't know then assume that the regulator is always on */ 1476 if (!rdev->desc->ops->is_enabled) 1477 return 1; 1478 1479 return rdev->desc->ops->is_enabled(rdev); 1480} 1481 1482/** 1483 * regulator_is_enabled - is the regulator output enabled 1484 * @regulator: regulator source 1485 * 1486 * Returns positive if the regulator driver backing the source/client 1487 * has requested that the device be enabled, zero if it hasn't, else a 1488 * negative errno code. 1489 * 1490 * Note that the device backing this regulator handle can have multiple 1491 * users, so it might be enabled even if regulator_enable() was never 1492 * called for this particular source. 1493 */ 1494int regulator_is_enabled(struct regulator *regulator) 1495{ 1496 int ret; 1497 1498 mutex_lock(®ulator->rdev->mutex); 1499 ret = _regulator_is_enabled(regulator->rdev); 1500 mutex_unlock(®ulator->rdev->mutex); 1501 1502 return ret; 1503} 1504EXPORT_SYMBOL_GPL(regulator_is_enabled); 1505 1506/** 1507 * regulator_count_voltages - count regulator_list_voltage() selectors 1508 * @regulator: regulator source 1509 * 1510 * Returns number of selectors, or negative errno. Selectors are 1511 * numbered starting at zero, and typically correspond to bitfields 1512 * in hardware registers. 1513 */ 1514int regulator_count_voltages(struct regulator *regulator) 1515{ 1516 struct regulator_dev *rdev = regulator->rdev; 1517 1518 return rdev->desc->n_voltages ? : -EINVAL; 1519} 1520EXPORT_SYMBOL_GPL(regulator_count_voltages); 1521 1522/** 1523 * regulator_list_voltage - enumerate supported voltages 1524 * @regulator: regulator source 1525 * @selector: identify voltage to list 1526 * Context: can sleep 1527 * 1528 * Returns a voltage that can be passed to @regulator_set_voltage(), 1529 * zero if this selector code can't be used on this system, or a 1530 * negative errno. 1531 */ 1532int regulator_list_voltage(struct regulator *regulator, unsigned selector) 1533{ 1534 struct regulator_dev *rdev = regulator->rdev; 1535 struct regulator_ops *ops = rdev->desc->ops; 1536 int ret; 1537 1538 if (!ops->list_voltage || selector >= rdev->desc->n_voltages) 1539 return -EINVAL; 1540 1541 mutex_lock(&rdev->mutex); 1542 ret = ops->list_voltage(rdev, selector); 1543 mutex_unlock(&rdev->mutex); 1544 1545 if (ret > 0) { 1546 if (ret < rdev->constraints->min_uV) 1547 ret = 0; 1548 else if (ret > rdev->constraints->max_uV) 1549 ret = 0; 1550 } 1551 1552 return ret; 1553} 1554EXPORT_SYMBOL_GPL(regulator_list_voltage); 1555 1556/** 1557 * regulator_is_supported_voltage - check if a voltage range can be supported 1558 * 1559 * @regulator: Regulator to check. 1560 * @min_uV: Minimum required voltage in uV. 1561 * @max_uV: Maximum required voltage in uV. 1562 * 1563 * Returns a boolean or a negative error code. 1564 */ 1565int regulator_is_supported_voltage(struct regulator *regulator, 1566 int min_uV, int max_uV) 1567{ 1568 int i, voltages, ret; 1569 1570 ret = regulator_count_voltages(regulator); 1571 if (ret < 0) 1572 return ret; 1573 voltages = ret; 1574 1575 for (i = 0; i < voltages; i++) { 1576 ret = regulator_list_voltage(regulator, i); 1577 1578 if (ret >= min_uV && ret <= max_uV) 1579 return 1; 1580 } 1581 1582 return 0; 1583} 1584 1585/** 1586 * regulator_set_voltage - set regulator output voltage 1587 * @regulator: regulator source 1588 * @min_uV: Minimum required voltage in uV 1589 * @max_uV: Maximum acceptable voltage in uV 1590 * 1591 * Sets a voltage regulator to the desired output voltage. This can be set 1592 * during any regulator state. IOW, regulator can be disabled or enabled. 1593 * 1594 * If the regulator is enabled then the voltage will change to the new value 1595 * immediately otherwise if the regulator is disabled the regulator will 1596 * output at the new voltage when enabled. 1597 * 1598 * NOTE: If the regulator is shared between several devices then the lowest 1599 * request voltage that meets the system constraints will be used. 1600 * Regulator system constraints must be set for this regulator before 1601 * calling this function otherwise this call will fail. 1602 */ 1603int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) 1604{ 1605 struct regulator_dev *rdev = regulator->rdev; 1606 int ret; 1607 1608 mutex_lock(&rdev->mutex); 1609 1610 /* sanity check */ 1611 if (!rdev->desc->ops->set_voltage) { 1612 ret = -EINVAL; 1613 goto out; 1614 } 1615 1616 /* constraints check */ 1617 ret = regulator_check_voltage(rdev, &min_uV, &max_uV); 1618 if (ret < 0) 1619 goto out; 1620 regulator->min_uV = min_uV; 1621 regulator->max_uV = max_uV; 1622 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV); 1623 1624out: 1625 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL); 1626 mutex_unlock(&rdev->mutex); 1627 return ret; 1628} 1629EXPORT_SYMBOL_GPL(regulator_set_voltage); 1630 1631static int _regulator_get_voltage(struct regulator_dev *rdev) 1632{ 1633 /* sanity check */ 1634 if (rdev->desc->ops->get_voltage) 1635 return rdev->desc->ops->get_voltage(rdev); 1636 else 1637 return -EINVAL; 1638} 1639 1640/** 1641 * regulator_get_voltage - get regulator output voltage 1642 * @regulator: regulator source 1643 * 1644 * This returns the current regulator voltage in uV. 1645 * 1646 * NOTE: If the regulator is disabled it will return the voltage value. This 1647 * function should not be used to determine regulator state. 1648 */ 1649int regulator_get_voltage(struct regulator *regulator) 1650{ 1651 int ret; 1652 1653 mutex_lock(®ulator->rdev->mutex); 1654 1655 ret = _regulator_get_voltage(regulator->rdev); 1656 1657 mutex_unlock(®ulator->rdev->mutex); 1658 1659 return ret; 1660} 1661EXPORT_SYMBOL_GPL(regulator_get_voltage); 1662 1663/** 1664 * regulator_set_current_limit - set regulator output current limit 1665 * @regulator: regulator source 1666 * @min_uA: Minimuum supported current in uA 1667 * @max_uA: Maximum supported current in uA 1668 * 1669 * Sets current sink to the desired output current. This can be set during 1670 * any regulator state. IOW, regulator can be disabled or enabled. 1671 * 1672 * If the regulator is enabled then the current will change to the new value 1673 * immediately otherwise if the regulator is disabled the regulator will 1674 * output at the new current when enabled. 1675 * 1676 * NOTE: Regulator system constraints must be set for this regulator before 1677 * calling this function otherwise this call will fail. 1678 */ 1679int regulator_set_current_limit(struct regulator *regulator, 1680 int min_uA, int max_uA) 1681{ 1682 struct regulator_dev *rdev = regulator->rdev; 1683 int ret; 1684 1685 mutex_lock(&rdev->mutex); 1686 1687 /* sanity check */ 1688 if (!rdev->desc->ops->set_current_limit) { 1689 ret = -EINVAL; 1690 goto out; 1691 } 1692 1693 /* constraints check */ 1694 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); 1695 if (ret < 0) 1696 goto out; 1697 1698 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); 1699out: 1700 mutex_unlock(&rdev->mutex); 1701 return ret; 1702} 1703EXPORT_SYMBOL_GPL(regulator_set_current_limit); 1704 1705static int _regulator_get_current_limit(struct regulator_dev *rdev) 1706{ 1707 int ret; 1708 1709 mutex_lock(&rdev->mutex); 1710 1711 /* sanity check */ 1712 if (!rdev->desc->ops->get_current_limit) { 1713 ret = -EINVAL; 1714 goto out; 1715 } 1716 1717 ret = rdev->desc->ops->get_current_limit(rdev); 1718out: 1719 mutex_unlock(&rdev->mutex); 1720 return ret; 1721} 1722 1723/** 1724 * regulator_get_current_limit - get regulator output current 1725 * @regulator: regulator source 1726 * 1727 * This returns the current supplied by the specified current sink in uA. 1728 * 1729 * NOTE: If the regulator is disabled it will return the current value. This 1730 * function should not be used to determine regulator state. 1731 */ 1732int regulator_get_current_limit(struct regulator *regulator) 1733{ 1734 return _regulator_get_current_limit(regulator->rdev); 1735} 1736EXPORT_SYMBOL_GPL(regulator_get_current_limit); 1737 1738/** 1739 * regulator_set_mode - set regulator operating mode 1740 * @regulator: regulator source 1741 * @mode: operating mode - one of the REGULATOR_MODE constants 1742 * 1743 * Set regulator operating mode to increase regulator efficiency or improve 1744 * regulation performance. 1745 * 1746 * NOTE: Regulator system constraints must be set for this regulator before 1747 * calling this function otherwise this call will fail. 1748 */ 1749int regulator_set_mode(struct regulator *regulator, unsigned int mode) 1750{ 1751 struct regulator_dev *rdev = regulator->rdev; 1752 int ret; 1753 int regulator_curr_mode; 1754 1755 mutex_lock(&rdev->mutex); 1756 1757 /* sanity check */ 1758 if (!rdev->desc->ops->set_mode) { 1759 ret = -EINVAL; 1760 goto out; 1761 } 1762 1763 /* return if the same mode is requested */ 1764 if (rdev->desc->ops->get_mode) { 1765 regulator_curr_mode = rdev->desc->ops->get_mode(rdev); 1766 if (regulator_curr_mode == mode) { 1767 ret = 0; 1768 goto out; 1769 } 1770 } 1771 1772 /* constraints check */ 1773 ret = regulator_check_mode(rdev, mode); 1774 if (ret < 0) 1775 goto out; 1776 1777 ret = rdev->desc->ops->set_mode(rdev, mode); 1778out: 1779 mutex_unlock(&rdev->mutex); 1780 return ret; 1781} 1782EXPORT_SYMBOL_GPL(regulator_set_mode); 1783 1784static unsigned int _regulator_get_mode(struct regulator_dev *rdev) 1785{ 1786 int ret; 1787 1788 mutex_lock(&rdev->mutex); 1789 1790 /* sanity check */ 1791 if (!rdev->desc->ops->get_mode) { 1792 ret = -EINVAL; 1793 goto out; 1794 } 1795 1796 ret = rdev->desc->ops->get_mode(rdev); 1797out: 1798 mutex_unlock(&rdev->mutex); 1799 return ret; 1800} 1801 1802/** 1803 * regulator_get_mode - get regulator operating mode 1804 * @regulator: regulator source 1805 * 1806 * Get the current regulator operating mode. 1807 */ 1808unsigned int regulator_get_mode(struct regulator *regulator) 1809{ 1810 return _regulator_get_mode(regulator->rdev); 1811} 1812EXPORT_SYMBOL_GPL(regulator_get_mode); 1813 1814/** 1815 * regulator_set_optimum_mode - set regulator optimum operating mode 1816 * @regulator: regulator source 1817 * @uA_load: load current 1818 * 1819 * Notifies the regulator core of a new device load. This is then used by 1820 * DRMS (if enabled by constraints) to set the most efficient regulator 1821 * operating mode for the new regulator loading. 1822 * 1823 * Consumer devices notify their supply regulator of the maximum power 1824 * they will require (can be taken from device datasheet in the power 1825 * consumption tables) when they change operational status and hence power 1826 * state. Examples of operational state changes that can affect power 1827 * consumption are :- 1828 * 1829 * o Device is opened / closed. 1830 * o Device I/O is about to begin or has just finished. 1831 * o Device is idling in between work. 1832 * 1833 * This information is also exported via sysfs to userspace. 1834 * 1835 * DRMS will sum the total requested load on the regulator and change 1836 * to the most efficient operating mode if platform constraints allow. 1837 * 1838 * Returns the new regulator mode or error. 1839 */ 1840int regulator_set_optimum_mode(struct regulator *regulator, int uA_load) 1841{ 1842 struct regulator_dev *rdev = regulator->rdev; 1843 struct regulator *consumer; 1844 int ret, output_uV, input_uV, total_uA_load = 0; 1845 unsigned int mode; 1846 1847 mutex_lock(&rdev->mutex); 1848 1849 regulator->uA_load = uA_load; 1850 ret = regulator_check_drms(rdev); 1851 if (ret < 0) 1852 goto out; 1853 ret = -EINVAL; 1854 1855 /* sanity check */ 1856 if (!rdev->desc->ops->get_optimum_mode) 1857 goto out; 1858 1859 /* get output voltage */ 1860 output_uV = rdev->desc->ops->get_voltage(rdev); 1861 if (output_uV <= 0) { 1862 printk(KERN_ERR "%s: invalid output voltage found for %s\n", 1863 __func__, rdev_get_name(rdev)); 1864 goto out; 1865 } 1866 1867 /* get input voltage */ 1868 if (rdev->supply && rdev->supply->desc->ops->get_voltage) 1869 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply); 1870 else 1871 input_uV = rdev->constraints->input_uV; 1872 if (input_uV <= 0) { 1873 printk(KERN_ERR "%s: invalid input voltage found for %s\n", 1874 __func__, rdev_get_name(rdev)); 1875 goto out; 1876 } 1877 1878 /* calc total requested load for this regulator */ 1879 list_for_each_entry(consumer, &rdev->consumer_list, list) 1880 total_uA_load += consumer->uA_load; 1881 1882 mode = rdev->desc->ops->get_optimum_mode(rdev, 1883 input_uV, output_uV, 1884 total_uA_load); 1885 ret = regulator_check_mode(rdev, mode); 1886 if (ret < 0) { 1887 printk(KERN_ERR "%s: failed to get optimum mode for %s @" 1888 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev), 1889 total_uA_load, input_uV, output_uV); 1890 goto out; 1891 } 1892 1893 ret = rdev->desc->ops->set_mode(rdev, mode); 1894 if (ret < 0) { 1895 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n", 1896 __func__, mode, rdev_get_name(rdev)); 1897 goto out; 1898 } 1899 ret = mode; 1900out: 1901 mutex_unlock(&rdev->mutex); 1902 return ret; 1903} 1904EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); 1905 1906/** 1907 * regulator_register_notifier - register regulator event notifier 1908 * @regulator: regulator source 1909 * @nb: notifier block 1910 * 1911 * Register notifier block to receive regulator events. 1912 */ 1913int regulator_register_notifier(struct regulator *regulator, 1914 struct notifier_block *nb) 1915{ 1916 return blocking_notifier_chain_register(®ulator->rdev->notifier, 1917 nb); 1918} 1919EXPORT_SYMBOL_GPL(regulator_register_notifier); 1920 1921/** 1922 * regulator_unregister_notifier - unregister regulator event notifier 1923 * @regulator: regulator source 1924 * @nb: notifier block 1925 * 1926 * Unregister regulator event notifier block. 1927 */ 1928int regulator_unregister_notifier(struct regulator *regulator, 1929 struct notifier_block *nb) 1930{ 1931 return blocking_notifier_chain_unregister(®ulator->rdev->notifier, 1932 nb); 1933} 1934EXPORT_SYMBOL_GPL(regulator_unregister_notifier); 1935 1936/* notify regulator consumers and downstream regulator consumers. 1937 * Note mutex must be held by caller. 1938 */ 1939static void _notifier_call_chain(struct regulator_dev *rdev, 1940 unsigned long event, void *data) 1941{ 1942 struct regulator_dev *_rdev; 1943 1944 /* call rdev chain first */ 1945 blocking_notifier_call_chain(&rdev->notifier, event, NULL); 1946 1947 /* now notify regulator we supply */ 1948 list_for_each_entry(_rdev, &rdev->supply_list, slist) { 1949 mutex_lock(&_rdev->mutex); 1950 _notifier_call_chain(_rdev, event, data); 1951 mutex_unlock(&_rdev->mutex); 1952 } 1953} 1954 1955/** 1956 * regulator_bulk_get - get multiple regulator consumers 1957 * 1958 * @dev: Device to supply 1959 * @num_consumers: Number of consumers to register 1960 * @consumers: Configuration of consumers; clients are stored here. 1961 * 1962 * @return 0 on success, an errno on failure. 1963 * 1964 * This helper function allows drivers to get several regulator 1965 * consumers in one operation. If any of the regulators cannot be 1966 * acquired then any regulators that were allocated will be freed 1967 * before returning to the caller. 1968 */ 1969int regulator_bulk_get(struct device *dev, int num_consumers, 1970 struct regulator_bulk_data *consumers) 1971{ 1972 int i; 1973 int ret; 1974 1975 for (i = 0; i < num_consumers; i++) 1976 consumers[i].consumer = NULL; 1977 1978 for (i = 0; i < num_consumers; i++) { 1979 consumers[i].consumer = regulator_get(dev, 1980 consumers[i].supply); 1981 if (IS_ERR(consumers[i].consumer)) { 1982 ret = PTR_ERR(consumers[i].consumer); 1983 dev_err(dev, "Failed to get supply '%s': %d\n", 1984 consumers[i].supply, ret); 1985 consumers[i].consumer = NULL; 1986 goto err; 1987 } 1988 } 1989 1990 return 0; 1991 1992err: 1993 for (i = 0; i < num_consumers && consumers[i].consumer; i++) 1994 regulator_put(consumers[i].consumer); 1995 1996 return ret; 1997} 1998EXPORT_SYMBOL_GPL(regulator_bulk_get); 1999 2000/** 2001 * regulator_bulk_enable - enable multiple regulator consumers 2002 * 2003 * @num_consumers: Number of consumers 2004 * @consumers: Consumer data; clients are stored here. 2005 * @return 0 on success, an errno on failure 2006 * 2007 * This convenience API allows consumers to enable multiple regulator 2008 * clients in a single API call. If any consumers cannot be enabled 2009 * then any others that were enabled will be disabled again prior to 2010 * return. 2011 */ 2012int regulator_bulk_enable(int num_consumers, 2013 struct regulator_bulk_data *consumers) 2014{ 2015 int i; 2016 int ret; 2017 2018 for (i = 0; i < num_consumers; i++) { 2019 ret = regulator_enable(consumers[i].consumer); 2020 if (ret != 0) 2021 goto err; 2022 } 2023 2024 return 0; 2025 2026err: 2027 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret); 2028 for (--i; i >= 0; --i) 2029 regulator_disable(consumers[i].consumer); 2030 2031 return ret; 2032} 2033EXPORT_SYMBOL_GPL(regulator_bulk_enable); 2034 2035/** 2036 * regulator_bulk_disable - disable multiple regulator consumers 2037 * 2038 * @num_consumers: Number of consumers 2039 * @consumers: Consumer data; clients are stored here. 2040 * @return 0 on success, an errno on failure 2041 * 2042 * This convenience API allows consumers to disable multiple regulator 2043 * clients in a single API call. If any consumers cannot be enabled 2044 * then any others that were disabled will be disabled again prior to 2045 * return. 2046 */ 2047int regulator_bulk_disable(int num_consumers, 2048 struct regulator_bulk_data *consumers) 2049{ 2050 int i; 2051 int ret; 2052 2053 for (i = 0; i < num_consumers; i++) { 2054 ret = regulator_disable(consumers[i].consumer); 2055 if (ret != 0) 2056 goto err; 2057 } 2058 2059 return 0; 2060 2061err: 2062 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply, 2063 ret); 2064 for (--i; i >= 0; --i) 2065 regulator_enable(consumers[i].consumer); 2066 2067 return ret; 2068} 2069EXPORT_SYMBOL_GPL(regulator_bulk_disable); 2070 2071/** 2072 * regulator_bulk_free - free multiple regulator consumers 2073 * 2074 * @num_consumers: Number of consumers 2075 * @consumers: Consumer data; clients are stored here. 2076 * 2077 * This convenience API allows consumers to free multiple regulator 2078 * clients in a single API call. 2079 */ 2080void regulator_bulk_free(int num_consumers, 2081 struct regulator_bulk_data *consumers) 2082{ 2083 int i; 2084 2085 for (i = 0; i < num_consumers; i++) { 2086 regulator_put(consumers[i].consumer); 2087 consumers[i].consumer = NULL; 2088 } 2089} 2090EXPORT_SYMBOL_GPL(regulator_bulk_free); 2091 2092/** 2093 * regulator_notifier_call_chain - call regulator event notifier 2094 * @rdev: regulator source 2095 * @event: notifier block 2096 * @data: callback-specific data. 2097 * 2098 * Called by regulator drivers to notify clients a regulator event has 2099 * occurred. We also notify regulator clients downstream. 2100 * Note lock must be held by caller. 2101 */ 2102int regulator_notifier_call_chain(struct regulator_dev *rdev, 2103 unsigned long event, void *data) 2104{ 2105 _notifier_call_chain(rdev, event, data); 2106 return NOTIFY_DONE; 2107 2108} 2109EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); 2110 2111/** 2112 * regulator_mode_to_status - convert a regulator mode into a status 2113 * 2114 * @mode: Mode to convert 2115 * 2116 * Convert a regulator mode into a status. 2117 */ 2118int regulator_mode_to_status(unsigned int mode) 2119{ 2120 switch (mode) { 2121 case REGULATOR_MODE_FAST: 2122 return REGULATOR_STATUS_FAST; 2123 case REGULATOR_MODE_NORMAL: 2124 return REGULATOR_STATUS_NORMAL; 2125 case REGULATOR_MODE_IDLE: 2126 return REGULATOR_STATUS_IDLE; 2127 case REGULATOR_STATUS_STANDBY: 2128 return REGULATOR_STATUS_STANDBY; 2129 default: 2130 return 0; 2131 } 2132} 2133EXPORT_SYMBOL_GPL(regulator_mode_to_status); 2134 2135/* 2136 * To avoid cluttering sysfs (and memory) with useless state, only 2137 * create attributes that can be meaningfully displayed. 2138 */ 2139static int add_regulator_attributes(struct regulator_dev *rdev) 2140{ 2141 struct device *dev = &rdev->dev; 2142 struct regulator_ops *ops = rdev->desc->ops; 2143 int status = 0; 2144 2145 /* some attributes need specific methods to be displayed */ 2146 if (ops->get_voltage) { 2147 status = device_create_file(dev, &dev_attr_microvolts); 2148 if (status < 0) 2149 return status; 2150 } 2151 if (ops->get_current_limit) { 2152 status = device_create_file(dev, &dev_attr_microamps); 2153 if (status < 0) 2154 return status; 2155 } 2156 if (ops->get_mode) { 2157 status = device_create_file(dev, &dev_attr_opmode); 2158 if (status < 0) 2159 return status; 2160 } 2161 if (ops->is_enabled) { 2162 status = device_create_file(dev, &dev_attr_state); 2163 if (status < 0) 2164 return status; 2165 } 2166 if (ops->get_status) { 2167 status = device_create_file(dev, &dev_attr_status); 2168 if (status < 0) 2169 return status; 2170 } 2171 2172 /* some attributes are type-specific */ 2173 if (rdev->desc->type == REGULATOR_CURRENT) { 2174 status = device_create_file(dev, &dev_attr_requested_microamps); 2175 if (status < 0) 2176 return status; 2177 } 2178 2179 /* all the other attributes exist to support constraints; 2180 * don't show them if there are no constraints, or if the 2181 * relevant supporting methods are missing. 2182 */ 2183 if (!rdev->constraints) 2184 return status; 2185 2186 /* constraints need specific supporting methods */ 2187 if (ops->set_voltage) { 2188 status = device_create_file(dev, &dev_attr_min_microvolts); 2189 if (status < 0) 2190 return status; 2191 status = device_create_file(dev, &dev_attr_max_microvolts); 2192 if (status < 0) 2193 return status; 2194 } 2195 if (ops->set_current_limit) { 2196 status = device_create_file(dev, &dev_attr_min_microamps); 2197 if (status < 0) 2198 return status; 2199 status = device_create_file(dev, &dev_attr_max_microamps); 2200 if (status < 0) 2201 return status; 2202 } 2203 2204 /* suspend mode constraints need multiple supporting methods */ 2205 if (!(ops->set_suspend_enable && ops->set_suspend_disable)) 2206 return status; 2207 2208 status = device_create_file(dev, &dev_attr_suspend_standby_state); 2209 if (status < 0) 2210 return status; 2211 status = device_create_file(dev, &dev_attr_suspend_mem_state); 2212 if (status < 0) 2213 return status; 2214 status = device_create_file(dev, &dev_attr_suspend_disk_state); 2215 if (status < 0) 2216 return status; 2217 2218 if (ops->set_suspend_voltage) { 2219 status = device_create_file(dev, 2220 &dev_attr_suspend_standby_microvolts); 2221 if (status < 0) 2222 return status; 2223 status = device_create_file(dev, 2224 &dev_attr_suspend_mem_microvolts); 2225 if (status < 0) 2226 return status; 2227 status = device_create_file(dev, 2228 &dev_attr_suspend_disk_microvolts); 2229 if (status < 0) 2230 return status; 2231 } 2232 2233 if (ops->set_suspend_mode) { 2234 status = device_create_file(dev, 2235 &dev_attr_suspend_standby_mode); 2236 if (status < 0) 2237 return status; 2238 status = device_create_file(dev, 2239 &dev_attr_suspend_mem_mode); 2240 if (status < 0) 2241 return status; 2242 status = device_create_file(dev, 2243 &dev_attr_suspend_disk_mode); 2244 if (status < 0) 2245 return status; 2246 } 2247 2248 return status; 2249} 2250 2251/** 2252 * regulator_register - register regulator 2253 * @regulator_desc: regulator to register 2254 * @dev: struct device for the regulator 2255 * @init_data: platform provided init data, passed through by driver 2256 * @driver_data: private regulator data 2257 * 2258 * Called by regulator drivers to register a regulator. 2259 * Returns 0 on success. 2260 */ 2261struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, 2262 struct device *dev, struct regulator_init_data *init_data, 2263 void *driver_data) 2264{ 2265 static atomic_t regulator_no = ATOMIC_INIT(0); 2266 struct regulator_dev *rdev; 2267 int ret, i; 2268 2269 if (regulator_desc == NULL) 2270 return ERR_PTR(-EINVAL); 2271 2272 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) 2273 return ERR_PTR(-EINVAL); 2274 2275 if (regulator_desc->type != REGULATOR_VOLTAGE && 2276 regulator_desc->type != REGULATOR_CURRENT) 2277 return ERR_PTR(-EINVAL); 2278 2279 if (!init_data) 2280 return ERR_PTR(-EINVAL); 2281 2282 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); 2283 if (rdev == NULL) 2284 return ERR_PTR(-ENOMEM); 2285 2286 mutex_lock(®ulator_list_mutex); 2287 2288 mutex_init(&rdev->mutex); 2289 rdev->reg_data = driver_data; 2290 rdev->owner = regulator_desc->owner; 2291 rdev->desc = regulator_desc; 2292 INIT_LIST_HEAD(&rdev->consumer_list); 2293 INIT_LIST_HEAD(&rdev->supply_list); 2294 INIT_LIST_HEAD(&rdev->list); 2295 INIT_LIST_HEAD(&rdev->slist); 2296 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); 2297 2298 /* preform any regulator specific init */ 2299 if (init_data->regulator_init) { 2300 ret = init_data->regulator_init(rdev->reg_data); 2301 if (ret < 0) 2302 goto clean; 2303 } 2304 2305 /* register with sysfs */ 2306 rdev->dev.class = ®ulator_class; 2307 rdev->dev.parent = dev; 2308 dev_set_name(&rdev->dev, "regulator.%d", 2309 atomic_inc_return(®ulator_no) - 1); 2310 ret = device_register(&rdev->dev); 2311 if (ret != 0) { 2312 put_device(&rdev->dev); 2313 goto clean; 2314 } 2315 2316 dev_set_drvdata(&rdev->dev, rdev); 2317 2318 /* set regulator constraints */ 2319 ret = set_machine_constraints(rdev, &init_data->constraints); 2320 if (ret < 0) 2321 goto scrub; 2322 2323 /* add attributes supported by this regulator */ 2324 ret = add_regulator_attributes(rdev); 2325 if (ret < 0) 2326 goto scrub; 2327 2328 /* set supply regulator if it exists */ 2329 if (init_data->supply_regulator && init_data->supply_regulator_dev) { 2330 dev_err(dev, 2331 "Supply regulator specified by both name and dev\n"); 2332 goto scrub; 2333 } 2334 2335 if (init_data->supply_regulator) { 2336 struct regulator_dev *r; 2337 int found = 0; 2338 2339 list_for_each_entry(r, ®ulator_list, list) { 2340 if (strcmp(rdev_get_name(r), 2341 init_data->supply_regulator) == 0) { 2342 found = 1; 2343 break; 2344 } 2345 } 2346 2347 if (!found) { 2348 dev_err(dev, "Failed to find supply %s\n", 2349 init_data->supply_regulator); 2350 goto scrub; 2351 } 2352 2353 ret = set_supply(rdev, r); 2354 if (ret < 0) 2355 goto scrub; 2356 } 2357 2358 if (init_data->supply_regulator_dev) { 2359 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n"); 2360 ret = set_supply(rdev, 2361 dev_get_drvdata(init_data->supply_regulator_dev)); 2362 if (ret < 0) 2363 goto scrub; 2364 } 2365 2366 /* add consumers devices */ 2367 for (i = 0; i < init_data->num_consumer_supplies; i++) { 2368 ret = set_consumer_device_supply(rdev, 2369 init_data->consumer_supplies[i].dev, 2370 init_data->consumer_supplies[i].dev_name, 2371 init_data->consumer_supplies[i].supply); 2372 if (ret < 0) 2373 goto unset_supplies; 2374 } 2375 2376 list_add(&rdev->list, ®ulator_list); 2377out: 2378 mutex_unlock(®ulator_list_mutex); 2379 return rdev; 2380 2381unset_supplies: 2382 unset_regulator_supplies(rdev); 2383 2384scrub: 2385 device_unregister(&rdev->dev); 2386 /* device core frees rdev */ 2387 rdev = ERR_PTR(ret); 2388 goto out; 2389 2390clean: 2391 kfree(rdev); 2392 rdev = ERR_PTR(ret); 2393 goto out; 2394} 2395EXPORT_SYMBOL_GPL(regulator_register); 2396 2397/** 2398 * regulator_unregister - unregister regulator 2399 * @rdev: regulator to unregister 2400 * 2401 * Called by regulator drivers to unregister a regulator. 2402 */ 2403void regulator_unregister(struct regulator_dev *rdev) 2404{ 2405 if (rdev == NULL) 2406 return; 2407 2408 mutex_lock(®ulator_list_mutex); 2409 WARN_ON(rdev->open_count); 2410 unset_regulator_supplies(rdev); 2411 list_del(&rdev->list); 2412 if (rdev->supply) 2413 sysfs_remove_link(&rdev->dev.kobj, "supply"); 2414 device_unregister(&rdev->dev); 2415 mutex_unlock(®ulator_list_mutex); 2416} 2417EXPORT_SYMBOL_GPL(regulator_unregister); 2418 2419/** 2420 * regulator_suspend_prepare - prepare regulators for system wide suspend 2421 * @state: system suspend state 2422 * 2423 * Configure each regulator with it's suspend operating parameters for state. 2424 * This will usually be called by machine suspend code prior to supending. 2425 */ 2426int regulator_suspend_prepare(suspend_state_t state) 2427{ 2428 struct regulator_dev *rdev; 2429 int ret = 0; 2430 2431 /* ON is handled by regulator active state */ 2432 if (state == PM_SUSPEND_ON) 2433 return -EINVAL; 2434 2435 mutex_lock(®ulator_list_mutex); 2436 list_for_each_entry(rdev, ®ulator_list, list) { 2437 2438 mutex_lock(&rdev->mutex); 2439 ret = suspend_prepare(rdev, state); 2440 mutex_unlock(&rdev->mutex); 2441 2442 if (ret < 0) { 2443 printk(KERN_ERR "%s: failed to prepare %s\n", 2444 __func__, rdev_get_name(rdev)); 2445 goto out; 2446 } 2447 } 2448out: 2449 mutex_unlock(®ulator_list_mutex); 2450 return ret; 2451} 2452EXPORT_SYMBOL_GPL(regulator_suspend_prepare); 2453 2454/** 2455 * regulator_has_full_constraints - the system has fully specified constraints 2456 * 2457 * Calling this function will cause the regulator API to disable all 2458 * regulators which have a zero use count and don't have an always_on 2459 * constraint in a late_initcall. 2460 * 2461 * The intention is that this will become the default behaviour in a 2462 * future kernel release so users are encouraged to use this facility 2463 * now. 2464 */ 2465void regulator_has_full_constraints(void) 2466{ 2467 has_full_constraints = 1; 2468} 2469EXPORT_SYMBOL_GPL(regulator_has_full_constraints); 2470 2471/** 2472 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found 2473 * 2474 * Calling this function will cause the regulator API to provide a 2475 * dummy regulator to consumers if no physical regulator is found, 2476 * allowing most consumers to proceed as though a regulator were 2477 * configured. This allows systems such as those with software 2478 * controllable regulators for the CPU core only to be brought up more 2479 * readily. 2480 */ 2481void regulator_use_dummy_regulator(void) 2482{ 2483 board_wants_dummy_regulator = true; 2484} 2485EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator); 2486 2487/** 2488 * rdev_get_drvdata - get rdev regulator driver data 2489 * @rdev: regulator 2490 * 2491 * Get rdev regulator driver private data. This call can be used in the 2492 * regulator driver context. 2493 */ 2494void *rdev_get_drvdata(struct regulator_dev *rdev) 2495{ 2496 return rdev->reg_data; 2497} 2498EXPORT_SYMBOL_GPL(rdev_get_drvdata); 2499 2500/** 2501 * regulator_get_drvdata - get regulator driver data 2502 * @regulator: regulator 2503 * 2504 * Get regulator driver private data. This call can be used in the consumer 2505 * driver context when non API regulator specific functions need to be called. 2506 */ 2507void *regulator_get_drvdata(struct regulator *regulator) 2508{ 2509 return regulator->rdev->reg_data; 2510} 2511EXPORT_SYMBOL_GPL(regulator_get_drvdata); 2512 2513/** 2514 * regulator_set_drvdata - set regulator driver data 2515 * @regulator: regulator 2516 * @data: data 2517 */ 2518void regulator_set_drvdata(struct regulator *regulator, void *data) 2519{ 2520 regulator->rdev->reg_data = data; 2521} 2522EXPORT_SYMBOL_GPL(regulator_set_drvdata); 2523 2524/** 2525 * regulator_get_id - get regulator ID 2526 * @rdev: regulator 2527 */ 2528int rdev_get_id(struct regulator_dev *rdev) 2529{ 2530 return rdev->desc->id; 2531} 2532EXPORT_SYMBOL_GPL(rdev_get_id); 2533 2534struct device *rdev_get_dev(struct regulator_dev *rdev) 2535{ 2536 return &rdev->dev; 2537} 2538EXPORT_SYMBOL_GPL(rdev_get_dev); 2539 2540void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) 2541{ 2542 return reg_init_data->driver_data; 2543} 2544EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); 2545 2546static int __init regulator_init(void) 2547{ 2548 int ret; 2549 2550 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION); 2551 2552 ret = class_register(®ulator_class); 2553 2554 regulator_dummy_init(); 2555 2556 return ret; 2557} 2558 2559/* init early to allow our consumers to complete system booting */ 2560core_initcall(regulator_init); 2561 2562static int __init regulator_init_complete(void) 2563{ 2564 struct regulator_dev *rdev; 2565 struct regulator_ops *ops; 2566 struct regulation_constraints *c; 2567 int enabled, ret; 2568 const char *name; 2569 2570 mutex_lock(®ulator_list_mutex); 2571 2572 /* If we have a full configuration then disable any regulators 2573 * which are not in use or always_on. This will become the 2574 * default behaviour in the future. 2575 */ 2576 list_for_each_entry(rdev, ®ulator_list, list) { 2577 ops = rdev->desc->ops; 2578 c = rdev->constraints; 2579 2580 name = rdev_get_name(rdev); 2581 2582 if (!ops->disable || (c && c->always_on)) 2583 continue; 2584 2585 mutex_lock(&rdev->mutex); 2586 2587 if (rdev->use_count) 2588 goto unlock; 2589 2590 /* If we can't read the status assume it's on. */ 2591 if (ops->is_enabled) 2592 enabled = ops->is_enabled(rdev); 2593 else 2594 enabled = 1; 2595 2596 if (!enabled) 2597 goto unlock; 2598 2599 if (has_full_constraints) { 2600 /* We log since this may kill the system if it 2601 * goes wrong. */ 2602 printk(KERN_INFO "%s: disabling %s\n", 2603 __func__, name); 2604 ret = ops->disable(rdev); 2605 if (ret != 0) { 2606 printk(KERN_ERR 2607 "%s: couldn't disable %s: %d\n", 2608 __func__, name, ret); 2609 } 2610 } else { 2611 /* The intention is that in future we will 2612 * assume that full constraints are provided 2613 * so warn even if we aren't going to do 2614 * anything here. 2615 */ 2616 printk(KERN_WARNING 2617 "%s: incomplete constraints, leaving %s on\n", 2618 __func__, name); 2619 } 2620 2621unlock: 2622 mutex_unlock(&rdev->mutex); 2623 } 2624 2625 mutex_unlock(®ulator_list_mutex); 2626 2627 return 0; 2628} 2629late_initcall(regulator_init_complete); 2630