1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * consumer.h -- SoC Regulator consumer support. 4 * 5 * Copyright (C) 2007, 2008 Wolfson Microelectronics PLC. 6 * 7 * Author: Liam Girdwood <[email protected]> 8 * 9 * Regulator Consumer Interface. 10 * 11 * A Power Management Regulator framework for SoC based devices. 12 * Features:- 13 * o Voltage and current level control. 14 * o Operating mode control. 15 * o Regulator status. 16 * o sysfs entries for showing client devices and status 17 * 18 * EXPERIMENTAL FEATURES: 19 * Dynamic Regulator operating Mode Switching (DRMS) - allows regulators 20 * to use most efficient operating mode depending upon voltage and load and 21 * is transparent to client drivers. 22 * 23 * e.g. Devices x,y,z share regulator r. Device x and y draw 20mA each during 24 * IO and 1mA at idle. Device z draws 100mA when under load and 5mA when 25 * idling. Regulator r has > 90% efficiency in NORMAL mode at loads > 100mA 26 * but this drops rapidly to 60% when below 100mA. Regulator r has > 90% 27 * efficiency in IDLE mode at loads < 10mA. Thus regulator r will operate 28 * in normal mode for loads > 10mA and in IDLE mode for load <= 10mA. 29 */ 30 31 #ifndef __LINUX_REGULATOR_CONSUMER_H_ 32 #define __LINUX_REGULATOR_CONSUMER_H_ 33 34 #include <linux/err.h> 35 #include <linux/suspend.h> 36 37 struct device; 38 struct notifier_block; 39 struct regmap; 40 struct regulator_dev; 41 42 /* 43 * Regulator operating modes. 44 * 45 * Regulators can run in a variety of different operating modes depending on 46 * output load. This allows further system power savings by selecting the 47 * best (and most efficient) regulator mode for a desired load. 48 * 49 * Most drivers will only care about NORMAL. The modes below are generic and 50 * will probably not match the naming convention of your regulator data sheet 51 * but should match the use cases in the datasheet. 52 * 53 * In order of power efficiency (least efficient at top). 54 * 55 * Mode Description 56 * FAST Regulator can handle fast changes in it's load. 57 * e.g. useful in CPU voltage & frequency scaling where 58 * load can quickly increase with CPU frequency increases. 59 * 60 * NORMAL Normal regulator power supply mode. Most drivers will 61 * use this mode. 62 * 63 * IDLE Regulator runs in a more efficient mode for light 64 * loads. Can be used for devices that have a low power 65 * requirement during periods of inactivity. This mode 66 * may be more noisy than NORMAL and may not be able 67 * to handle fast load switching. 68 * 69 * STANDBY Regulator runs in the most efficient mode for very 70 * light loads. Can be used by devices when they are 71 * in a sleep/standby state. This mode is likely to be 72 * the most noisy and may not be able to handle fast load 73 * switching. 74 * 75 * NOTE: Most regulators will only support a subset of these modes. Some 76 * will only just support NORMAL. 77 * 78 * These modes can be OR'ed together to make up a mask of valid register modes. 79 */ 80 81 #define REGULATOR_MODE_INVALID 0x0 82 #define REGULATOR_MODE_FAST 0x1 83 #define REGULATOR_MODE_NORMAL 0x2 84 #define REGULATOR_MODE_IDLE 0x4 85 #define REGULATOR_MODE_STANDBY 0x8 86 87 /* 88 * Regulator notifier events. 89 * 90 * UNDER_VOLTAGE Regulator output is under voltage. 91 * OVER_CURRENT Regulator output current is too high. 92 * REGULATION_OUT Regulator output is out of regulation. 93 * FAIL Regulator output has failed. 94 * OVER_TEMP Regulator over temp. 95 * FORCE_DISABLE Regulator forcibly shut down by software. 96 * VOLTAGE_CHANGE Regulator voltage changed. 97 * Data passed is old voltage cast to (void *). 98 * DISABLE Regulator was disabled. 99 * PRE_VOLTAGE_CHANGE Regulator is about to have voltage changed. 100 * Data passed is "struct pre_voltage_change_data" 101 * ABORT_VOLTAGE_CHANGE Regulator voltage change failed for some reason. 102 * Data passed is old voltage cast to (void *). 103 * PRE_DISABLE Regulator is about to be disabled 104 * ABORT_DISABLE Regulator disable failed for some reason 105 * 106 * NOTE: These events can be OR'ed together when passed into handler. 107 */ 108 109 #define REGULATOR_EVENT_UNDER_VOLTAGE 0x01 110 #define REGULATOR_EVENT_OVER_CURRENT 0x02 111 #define REGULATOR_EVENT_REGULATION_OUT 0x04 112 #define REGULATOR_EVENT_FAIL 0x08 113 #define REGULATOR_EVENT_OVER_TEMP 0x10 114 #define REGULATOR_EVENT_FORCE_DISABLE 0x20 115 #define REGULATOR_EVENT_VOLTAGE_CHANGE 0x40 116 #define REGULATOR_EVENT_DISABLE 0x80 117 #define REGULATOR_EVENT_PRE_VOLTAGE_CHANGE 0x100 118 #define REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE 0x200 119 #define REGULATOR_EVENT_PRE_DISABLE 0x400 120 #define REGULATOR_EVENT_ABORT_DISABLE 0x800 121 #define REGULATOR_EVENT_ENABLE 0x1000 122 123 /* 124 * Regulator errors that can be queried using regulator_get_error_flags 125 * 126 * UNDER_VOLTAGE Regulator output is under voltage. 127 * OVER_CURRENT Regulator output current is too high. 128 * REGULATION_OUT Regulator output is out of regulation. 129 * FAIL Regulator output has failed. 130 * OVER_TEMP Regulator over temp. 131 * 132 * NOTE: These errors can be OR'ed together. 133 */ 134 135 #define REGULATOR_ERROR_UNDER_VOLTAGE BIT(1) 136 #define REGULATOR_ERROR_OVER_CURRENT BIT(2) 137 #define REGULATOR_ERROR_REGULATION_OUT BIT(3) 138 #define REGULATOR_ERROR_FAIL BIT(4) 139 #define REGULATOR_ERROR_OVER_TEMP BIT(5) 140 141 142 /** 143 * struct pre_voltage_change_data - Data sent with PRE_VOLTAGE_CHANGE event 144 * 145 * @old_uV: Current voltage before change. 146 * @min_uV: Min voltage we'll change to. 147 * @max_uV: Max voltage we'll change to. 148 */ 149 struct pre_voltage_change_data { 150 unsigned long old_uV; 151 unsigned long min_uV; 152 unsigned long max_uV; 153 }; 154 155 struct regulator; 156 157 /** 158 * struct regulator_bulk_data - Data used for bulk regulator operations. 159 * 160 * @supply: The name of the supply. Initialised by the user before 161 * using the bulk regulator APIs. 162 * @consumer: The regulator consumer for the supply. This will be managed 163 * by the bulk API. 164 * 165 * The regulator APIs provide a series of regulator_bulk_() API calls as 166 * a convenience to consumers which require multiple supplies. This 167 * structure is used to manage data for these calls. 168 */ 169 struct regulator_bulk_data { 170 const char *supply; 171 struct regulator *consumer; 172 173 /* private: Internal use */ 174 int ret; 175 }; 176 177 #if defined(CONFIG_REGULATOR) 178 179 /* regulator get and put */ 180 struct regulator *__must_check regulator_get(struct device *dev, 181 const char *id); 182 struct regulator *__must_check devm_regulator_get(struct device *dev, 183 const char *id); 184 struct regulator *__must_check regulator_get_exclusive(struct device *dev, 185 const char *id); 186 struct regulator *__must_check devm_regulator_get_exclusive(struct device *dev, 187 const char *id); 188 struct regulator *__must_check regulator_get_optional(struct device *dev, 189 const char *id); 190 struct regulator *__must_check devm_regulator_get_optional(struct device *dev, 191 const char *id); 192 void regulator_put(struct regulator *regulator); 193 void devm_regulator_put(struct regulator *regulator); 194 195 int regulator_register_supply_alias(struct device *dev, const char *id, 196 struct device *alias_dev, 197 const char *alias_id); 198 void regulator_unregister_supply_alias(struct device *dev, const char *id); 199 200 int regulator_bulk_register_supply_alias(struct device *dev, 201 const char *const *id, 202 struct device *alias_dev, 203 const char *const *alias_id, 204 int num_id); 205 void regulator_bulk_unregister_supply_alias(struct device *dev, 206 const char * const *id, int num_id); 207 208 int devm_regulator_register_supply_alias(struct device *dev, const char *id, 209 struct device *alias_dev, 210 const char *alias_id); 211 void devm_regulator_unregister_supply_alias(struct device *dev, 212 const char *id); 213 214 int devm_regulator_bulk_register_supply_alias(struct device *dev, 215 const char *const *id, 216 struct device *alias_dev, 217 const char *const *alias_id, 218 int num_id); 219 void devm_regulator_bulk_unregister_supply_alias(struct device *dev, 220 const char *const *id, 221 int num_id); 222 223 /* regulator output control and status */ 224 int __must_check regulator_enable(struct regulator *regulator); 225 int regulator_disable(struct regulator *regulator); 226 int regulator_force_disable(struct regulator *regulator); 227 int regulator_is_enabled(struct regulator *regulator); 228 int regulator_disable_deferred(struct regulator *regulator, int ms); 229 230 int __must_check regulator_bulk_get(struct device *dev, int num_consumers, 231 struct regulator_bulk_data *consumers); 232 int __must_check devm_regulator_bulk_get(struct device *dev, int num_consumers, 233 struct regulator_bulk_data *consumers); 234 int __must_check regulator_bulk_enable(int num_consumers, 235 struct regulator_bulk_data *consumers); 236 int regulator_bulk_disable(int num_consumers, 237 struct regulator_bulk_data *consumers); 238 int regulator_bulk_force_disable(int num_consumers, 239 struct regulator_bulk_data *consumers); 240 void regulator_bulk_free(int num_consumers, 241 struct regulator_bulk_data *consumers); 242 243 int regulator_count_voltages(struct regulator *regulator); 244 int regulator_list_voltage(struct regulator *regulator, unsigned selector); 245 int regulator_is_supported_voltage(struct regulator *regulator, 246 int min_uV, int max_uV); 247 unsigned int regulator_get_linear_step(struct regulator *regulator); 248 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV); 249 int regulator_set_voltage_time(struct regulator *regulator, 250 int old_uV, int new_uV); 251 int regulator_get_voltage(struct regulator *regulator); 252 int regulator_sync_voltage(struct regulator *regulator); 253 int regulator_set_current_limit(struct regulator *regulator, 254 int min_uA, int max_uA); 255 int regulator_get_current_limit(struct regulator *regulator); 256 257 int regulator_set_mode(struct regulator *regulator, unsigned int mode); 258 unsigned int regulator_get_mode(struct regulator *regulator); 259 int regulator_get_error_flags(struct regulator *regulator, 260 unsigned int *flags); 261 int regulator_set_load(struct regulator *regulator, int load_uA); 262 263 int regulator_allow_bypass(struct regulator *regulator, bool allow); 264 265 struct regmap *regulator_get_regmap(struct regulator *regulator); 266 int regulator_get_hardware_vsel_register(struct regulator *regulator, 267 unsigned *vsel_reg, 268 unsigned *vsel_mask); 269 int regulator_list_hardware_vsel(struct regulator *regulator, 270 unsigned selector); 271 272 /* regulator notifier block */ 273 int regulator_register_notifier(struct regulator *regulator, 274 struct notifier_block *nb); 275 int devm_regulator_register_notifier(struct regulator *regulator, 276 struct notifier_block *nb); 277 int regulator_unregister_notifier(struct regulator *regulator, 278 struct notifier_block *nb); 279 void devm_regulator_unregister_notifier(struct regulator *regulator, 280 struct notifier_block *nb); 281 282 /* regulator suspend */ 283 int regulator_suspend_enable(struct regulator_dev *rdev, 284 suspend_state_t state); 285 int regulator_suspend_disable(struct regulator_dev *rdev, 286 suspend_state_t state); 287 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, 288 int max_uV, suspend_state_t state); 289 290 /* driver data - core doesn't touch */ 291 void *regulator_get_drvdata(struct regulator *regulator); 292 void regulator_set_drvdata(struct regulator *regulator, void *data); 293 294 /* misc helpers */ 295 296 void regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers, 297 const char *const *supply_names, 298 unsigned int num_supplies); 299 300 bool regulator_is_equal(struct regulator *reg1, struct regulator *reg2); 301 302 #else 303 304 /* 305 * Make sure client drivers will still build on systems with no software 306 * controllable voltage or current regulators. 307 */ 308 static inline struct regulator *__must_check regulator_get(struct device *dev, 309 const char *id) 310 { 311 /* Nothing except the stubbed out regulator API should be 312 * looking at the value except to check if it is an error 313 * value. Drivers are free to handle NULL specifically by 314 * skipping all regulator API calls, but they don't have to. 315 * Drivers which don't, should make sure they properly handle 316 * corner cases of the API, such as regulator_get_voltage() 317 * returning 0. 318 */ 319 return NULL; 320 } 321 322 static inline struct regulator *__must_check 323 devm_regulator_get(struct device *dev, const char *id) 324 { 325 return NULL; 326 } 327 328 static inline struct regulator *__must_check 329 regulator_get_exclusive(struct device *dev, const char *id) 330 { 331 return ERR_PTR(-ENODEV); 332 } 333 334 static inline struct regulator *__must_check 335 devm_regulator_get_exclusive(struct device *dev, const char *id) 336 { 337 return ERR_PTR(-ENODEV); 338 } 339 340 static inline struct regulator *__must_check 341 regulator_get_optional(struct device *dev, const char *id) 342 { 343 return ERR_PTR(-ENODEV); 344 } 345 346 347 static inline struct regulator *__must_check 348 devm_regulator_get_optional(struct device *dev, const char *id) 349 { 350 return ERR_PTR(-ENODEV); 351 } 352 353 static inline void regulator_put(struct regulator *regulator) 354 { 355 } 356 357 static inline void devm_regulator_put(struct regulator *regulator) 358 { 359 } 360 361 static inline int regulator_register_supply_alias(struct device *dev, 362 const char *id, 363 struct device *alias_dev, 364 const char *alias_id) 365 { 366 return 0; 367 } 368 369 static inline void regulator_unregister_supply_alias(struct device *dev, 370 const char *id) 371 { 372 } 373 374 static inline int regulator_bulk_register_supply_alias(struct device *dev, 375 const char *const *id, 376 struct device *alias_dev, 377 const char * const *alias_id, 378 int num_id) 379 { 380 return 0; 381 } 382 383 static inline void regulator_bulk_unregister_supply_alias(struct device *dev, 384 const char * const *id, 385 int num_id) 386 { 387 } 388 389 static inline int devm_regulator_register_supply_alias(struct device *dev, 390 const char *id, 391 struct device *alias_dev, 392 const char *alias_id) 393 { 394 return 0; 395 } 396 397 static inline void devm_regulator_unregister_supply_alias(struct device *dev, 398 const char *id) 399 { 400 } 401 402 static inline int devm_regulator_bulk_register_supply_alias(struct device *dev, 403 const char *const *id, 404 struct device *alias_dev, 405 const char *const *alias_id, 406 int num_id) 407 { 408 return 0; 409 } 410 411 static inline void devm_regulator_bulk_unregister_supply_alias( 412 struct device *dev, const char *const *id, int num_id) 413 { 414 } 415 416 static inline int regulator_enable(struct regulator *regulator) 417 { 418 return 0; 419 } 420 421 static inline int regulator_disable(struct regulator *regulator) 422 { 423 return 0; 424 } 425 426 static inline int regulator_force_disable(struct regulator *regulator) 427 { 428 return 0; 429 } 430 431 static inline int regulator_disable_deferred(struct regulator *regulator, 432 int ms) 433 { 434 return 0; 435 } 436 437 static inline int regulator_is_enabled(struct regulator *regulator) 438 { 439 return 1; 440 } 441 442 static inline int regulator_bulk_get(struct device *dev, 443 int num_consumers, 444 struct regulator_bulk_data *consumers) 445 { 446 return 0; 447 } 448 449 static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers, 450 struct regulator_bulk_data *consumers) 451 { 452 return 0; 453 } 454 455 static inline int regulator_bulk_enable(int num_consumers, 456 struct regulator_bulk_data *consumers) 457 { 458 return 0; 459 } 460 461 static inline int regulator_bulk_disable(int num_consumers, 462 struct regulator_bulk_data *consumers) 463 { 464 return 0; 465 } 466 467 static inline int regulator_bulk_force_disable(int num_consumers, 468 struct regulator_bulk_data *consumers) 469 { 470 return 0; 471 } 472 473 static inline void regulator_bulk_free(int num_consumers, 474 struct regulator_bulk_data *consumers) 475 { 476 } 477 478 static inline int regulator_set_voltage(struct regulator *regulator, 479 int min_uV, int max_uV) 480 { 481 return 0; 482 } 483 484 static inline int regulator_set_voltage_time(struct regulator *regulator, 485 int old_uV, int new_uV) 486 { 487 return 0; 488 } 489 490 static inline int regulator_get_voltage(struct regulator *regulator) 491 { 492 return -EINVAL; 493 } 494 495 static inline int regulator_sync_voltage(struct regulator *regulator) 496 { 497 return -EINVAL; 498 } 499 500 static inline int regulator_is_supported_voltage(struct regulator *regulator, 501 int min_uV, int max_uV) 502 { 503 return 0; 504 } 505 506 static inline unsigned int regulator_get_linear_step(struct regulator *regulator) 507 { 508 return 0; 509 } 510 511 static inline int regulator_set_current_limit(struct regulator *regulator, 512 int min_uA, int max_uA) 513 { 514 return 0; 515 } 516 517 static inline int regulator_get_current_limit(struct regulator *regulator) 518 { 519 return 0; 520 } 521 522 static inline int regulator_set_mode(struct regulator *regulator, 523 unsigned int mode) 524 { 525 return 0; 526 } 527 528 static inline unsigned int regulator_get_mode(struct regulator *regulator) 529 { 530 return REGULATOR_MODE_NORMAL; 531 } 532 533 static inline int regulator_get_error_flags(struct regulator *regulator, 534 unsigned int *flags) 535 { 536 return -EINVAL; 537 } 538 539 static inline int regulator_set_load(struct regulator *regulator, int load_uA) 540 { 541 return 0; 542 } 543 544 static inline int regulator_allow_bypass(struct regulator *regulator, 545 bool allow) 546 { 547 return 0; 548 } 549 550 static inline struct regmap *regulator_get_regmap(struct regulator *regulator) 551 { 552 return ERR_PTR(-EOPNOTSUPP); 553 } 554 555 static inline int regulator_get_hardware_vsel_register(struct regulator *regulator, 556 unsigned *vsel_reg, 557 unsigned *vsel_mask) 558 { 559 return -EOPNOTSUPP; 560 } 561 562 static inline int regulator_list_hardware_vsel(struct regulator *regulator, 563 unsigned selector) 564 { 565 return -EOPNOTSUPP; 566 } 567 568 static inline int regulator_register_notifier(struct regulator *regulator, 569 struct notifier_block *nb) 570 { 571 return 0; 572 } 573 574 static inline int devm_regulator_register_notifier(struct regulator *regulator, 575 struct notifier_block *nb) 576 { 577 return 0; 578 } 579 580 static inline int regulator_unregister_notifier(struct regulator *regulator, 581 struct notifier_block *nb) 582 { 583 return 0; 584 } 585 586 static inline int devm_regulator_unregister_notifier(struct regulator *regulator, 587 struct notifier_block *nb) 588 { 589 return 0; 590 } 591 592 static inline int regulator_suspend_enable(struct regulator_dev *rdev, 593 suspend_state_t state) 594 { 595 return -EINVAL; 596 } 597 598 static inline int regulator_suspend_disable(struct regulator_dev *rdev, 599 suspend_state_t state) 600 { 601 return -EINVAL; 602 } 603 604 static inline int regulator_set_suspend_voltage(struct regulator *regulator, 605 int min_uV, int max_uV, 606 suspend_state_t state) 607 { 608 return -EINVAL; 609 } 610 611 static inline void *regulator_get_drvdata(struct regulator *regulator) 612 { 613 return NULL; 614 } 615 616 static inline void regulator_set_drvdata(struct regulator *regulator, 617 void *data) 618 { 619 } 620 621 static inline int regulator_count_voltages(struct regulator *regulator) 622 { 623 return 0; 624 } 625 626 static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector) 627 { 628 return -EINVAL; 629 } 630 631 static inline void 632 regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers, 633 const char *const *supply_names, 634 unsigned int num_supplies) 635 { 636 } 637 638 static inline bool 639 regulator_is_equal(struct regulator *reg1, struct regulator *reg2) 640 { 641 return false; 642 } 643 #endif 644 645 static inline int regulator_set_voltage_triplet(struct regulator *regulator, 646 int min_uV, int target_uV, 647 int max_uV) 648 { 649 if (regulator_set_voltage(regulator, target_uV, max_uV) == 0) 650 return 0; 651 652 return regulator_set_voltage(regulator, min_uV, max_uV); 653 } 654 655 static inline int regulator_set_voltage_tol(struct regulator *regulator, 656 int new_uV, int tol_uV) 657 { 658 if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0) 659 return 0; 660 else 661 return regulator_set_voltage(regulator, 662 new_uV - tol_uV, new_uV + tol_uV); 663 } 664 665 static inline int regulator_is_supported_voltage_tol(struct regulator *regulator, 666 int target_uV, int tol_uV) 667 { 668 return regulator_is_supported_voltage(regulator, 669 target_uV - tol_uV, 670 target_uV + tol_uV); 671 } 672 673 #endif 674