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