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