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