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 regulator_get_optional(struct device *dev, const char *id) 336 { 337 return ERR_PTR(-ENODEV); 338 } 339 340 341 static inline struct regulator *__must_check 342 devm_regulator_get_optional(struct device *dev, const char *id) 343 { 344 return ERR_PTR(-ENODEV); 345 } 346 347 static inline void regulator_put(struct regulator *regulator) 348 { 349 } 350 351 static inline void devm_regulator_put(struct regulator *regulator) 352 { 353 } 354 355 static inline int regulator_register_supply_alias(struct device *dev, 356 const char *id, 357 struct device *alias_dev, 358 const char *alias_id) 359 { 360 return 0; 361 } 362 363 static inline void regulator_unregister_supply_alias(struct device *dev, 364 const char *id) 365 { 366 } 367 368 static inline int regulator_bulk_register_supply_alias(struct device *dev, 369 const char *const *id, 370 struct device *alias_dev, 371 const char * const *alias_id, 372 int num_id) 373 { 374 return 0; 375 } 376 377 static inline void regulator_bulk_unregister_supply_alias(struct device *dev, 378 const char * const *id, 379 int num_id) 380 { 381 } 382 383 static inline int devm_regulator_register_supply_alias(struct device *dev, 384 const char *id, 385 struct device *alias_dev, 386 const char *alias_id) 387 { 388 return 0; 389 } 390 391 static inline void devm_regulator_unregister_supply_alias(struct device *dev, 392 const char *id) 393 { 394 } 395 396 static inline int devm_regulator_bulk_register_supply_alias(struct device *dev, 397 const char *const *id, 398 struct device *alias_dev, 399 const char *const *alias_id, 400 int num_id) 401 { 402 return 0; 403 } 404 405 static inline void devm_regulator_bulk_unregister_supply_alias( 406 struct device *dev, const char *const *id, int num_id) 407 { 408 } 409 410 static inline int regulator_enable(struct regulator *regulator) 411 { 412 return 0; 413 } 414 415 static inline int regulator_disable(struct regulator *regulator) 416 { 417 return 0; 418 } 419 420 static inline int regulator_force_disable(struct regulator *regulator) 421 { 422 return 0; 423 } 424 425 static inline int regulator_disable_deferred(struct regulator *regulator, 426 int ms) 427 { 428 return 0; 429 } 430 431 static inline int regulator_is_enabled(struct regulator *regulator) 432 { 433 return 1; 434 } 435 436 static inline int regulator_bulk_get(struct device *dev, 437 int num_consumers, 438 struct regulator_bulk_data *consumers) 439 { 440 return 0; 441 } 442 443 static inline int devm_regulator_bulk_get(struct device *dev, int num_consumers, 444 struct regulator_bulk_data *consumers) 445 { 446 return 0; 447 } 448 449 static inline int regulator_bulk_enable(int num_consumers, 450 struct regulator_bulk_data *consumers) 451 { 452 return 0; 453 } 454 455 static inline int regulator_bulk_disable(int num_consumers, 456 struct regulator_bulk_data *consumers) 457 { 458 return 0; 459 } 460 461 static inline int regulator_bulk_force_disable(int num_consumers, 462 struct regulator_bulk_data *consumers) 463 { 464 return 0; 465 } 466 467 static inline void regulator_bulk_free(int num_consumers, 468 struct regulator_bulk_data *consumers) 469 { 470 } 471 472 static inline int regulator_set_voltage(struct regulator *regulator, 473 int min_uV, int max_uV) 474 { 475 return 0; 476 } 477 478 static inline int regulator_set_voltage_time(struct regulator *regulator, 479 int old_uV, int new_uV) 480 { 481 return 0; 482 } 483 484 static inline int regulator_get_voltage(struct regulator *regulator) 485 { 486 return -EINVAL; 487 } 488 489 static inline int regulator_is_supported_voltage(struct regulator *regulator, 490 int min_uV, int max_uV) 491 { 492 return 0; 493 } 494 495 static inline unsigned int regulator_get_linear_step(struct regulator *regulator) 496 { 497 return 0; 498 } 499 500 static inline int regulator_set_current_limit(struct regulator *regulator, 501 int min_uA, int max_uA) 502 { 503 return 0; 504 } 505 506 static inline int regulator_get_current_limit(struct regulator *regulator) 507 { 508 return 0; 509 } 510 511 static inline int regulator_set_mode(struct regulator *regulator, 512 unsigned int mode) 513 { 514 return 0; 515 } 516 517 static inline unsigned int regulator_get_mode(struct regulator *regulator) 518 { 519 return REGULATOR_MODE_NORMAL; 520 } 521 522 static inline int regulator_get_error_flags(struct regulator *regulator, 523 unsigned int *flags) 524 { 525 return -EINVAL; 526 } 527 528 static inline int regulator_set_load(struct regulator *regulator, int load_uA) 529 { 530 return 0; 531 } 532 533 static inline int regulator_allow_bypass(struct regulator *regulator, 534 bool allow) 535 { 536 return 0; 537 } 538 539 static inline struct regmap *regulator_get_regmap(struct regulator *regulator) 540 { 541 return ERR_PTR(-EOPNOTSUPP); 542 } 543 544 static inline int regulator_get_hardware_vsel_register(struct regulator *regulator, 545 unsigned *vsel_reg, 546 unsigned *vsel_mask) 547 { 548 return -EOPNOTSUPP; 549 } 550 551 static inline int regulator_list_hardware_vsel(struct regulator *regulator, 552 unsigned selector) 553 { 554 return -EOPNOTSUPP; 555 } 556 557 static inline int regulator_register_notifier(struct regulator *regulator, 558 struct notifier_block *nb) 559 { 560 return 0; 561 } 562 563 static inline int devm_regulator_register_notifier(struct regulator *regulator, 564 struct notifier_block *nb) 565 { 566 return 0; 567 } 568 569 static inline int regulator_unregister_notifier(struct regulator *regulator, 570 struct notifier_block *nb) 571 { 572 return 0; 573 } 574 575 static inline int devm_regulator_unregister_notifier(struct regulator *regulator, 576 struct notifier_block *nb) 577 { 578 return 0; 579 } 580 581 static inline void *regulator_get_drvdata(struct regulator *regulator) 582 { 583 return NULL; 584 } 585 586 static inline void regulator_set_drvdata(struct regulator *regulator, 587 void *data) 588 { 589 } 590 591 static inline int regulator_count_voltages(struct regulator *regulator) 592 { 593 return 0; 594 } 595 596 static inline int regulator_list_voltage(struct regulator *regulator, unsigned selector) 597 { 598 return -EINVAL; 599 } 600 601 static inline void 602 regulator_bulk_set_supply_names(struct regulator_bulk_data *consumers, 603 const char *const *supply_names, 604 unsigned int num_supplies) 605 { 606 } 607 608 static inline bool 609 regulator_is_equal(struct regulator *reg1, struct regulator *reg2) 610 { 611 return false; 612 } 613 #endif 614 615 static inline int regulator_set_voltage_triplet(struct regulator *regulator, 616 int min_uV, int target_uV, 617 int max_uV) 618 { 619 if (regulator_set_voltage(regulator, target_uV, max_uV) == 0) 620 return 0; 621 622 return regulator_set_voltage(regulator, min_uV, max_uV); 623 } 624 625 static inline int regulator_set_voltage_tol(struct regulator *regulator, 626 int new_uV, int tol_uV) 627 { 628 if (regulator_set_voltage(regulator, new_uV, new_uV + tol_uV) == 0) 629 return 0; 630 else 631 return regulator_set_voltage(regulator, 632 new_uV - tol_uV, new_uV + tol_uV); 633 } 634 635 static inline int regulator_is_supported_voltage_tol(struct regulator *regulator, 636 int target_uV, int tol_uV) 637 { 638 return regulator_is_supported_voltage(regulator, 639 target_uV - tol_uV, 640 target_uV + tol_uV); 641 } 642 643 #endif 644