1/* SPDX-License-Identifier: GPL-2.0+ */ 2/* 3 * Copyright (C) 2014-2015 Samsung Electronics 4 * Przemyslaw Marczak <p.marczak@samsung.com> 5 */ 6 7#ifndef _INCLUDE_REGULATOR_H_ 8#define _INCLUDE_REGULATOR_H_ 9 10struct udevice; 11 12/** 13 * U-Boot Voltage/Current Regulator 14 * ================================ 15 * 16 * The regulator API is based on a driver model, with the device tree support. 17 * And this header describes the functions and data types for the uclass id: 18 * 'UCLASS_REGULATOR' and the regulator driver API. 19 * 20 * The regulator uclass - is based on uclass platform data which is allocated, 21 * automatically for each regulator device on bind and 'dev->uclass_plat' 22 * points to it. The data type is: 'struct dm_regulator_uclass_plat'. 23 * The uclass file: 'drivers/power/regulator/regulator-uclass.c' 24 * 25 * The regulator device - is based on driver's model 'struct udevice'. 26 * The API can use regulator name in two meanings: 27 * - devname - the regulator device's name: 'dev->name' 28 * - platname - the device's plat's name. So in the code it looks like: 29 * 'uc_pdata = dev->uclass_plat'; 'name = uc_pdata->name'. 30 * 31 * The regulator device driver - provide an implementation of uclass operations 32 * pointed by 'dev->driver->ops' as a struct of type 'struct dm_regulator_ops'. 33 * 34 * To proper bind the regulator device, the device tree node should provide 35 * regulator constraints, like in the example below: 36 * 37 * ldo1 { 38 * regulator-name = "VDD_MMC_1.8V"; (must be unique for proper bind) 39 * regulator-min-microvolt = <1000000>; (optional) 40 * regulator-max-microvolt = <1000000>; (optional) 41 * regulator-min-microamp = <1000>; (optional) 42 * regulator-max-microamp = <1000>; (optional) 43 * regulator-always-on; (optional) 44 * regulator-boot-on; (optional) 45 * }; 46 * 47 * Note: For the proper operation, at least name constraint is needed, since 48 * it can be used when calling regulator_get_by_platname(). And the mandatory 49 * rule for this name is, that it must be globally unique for the single dts. 50 * If regulator-name property is not provided, node name will be chosen. 51 * 52 * Regulator bind: 53 * For each regulator device, the device_bind() should be called with passed 54 * device tree offset. This is required for this uclass's '.post_bind' method, 55 * which does the scan on the device node, for the 'regulator-name' constraint. 56 * If the parent is not a PMIC device, and the child is not bind by function: 57 * 'pmic_bind_childs()', then it's recommended to bind the device by call to 58 * dm_scan_fdt_dev() - this is usually done automatically for bus devices, 59 * as a post bind method. 60 * 61 * Regulator get: 62 * Having the device's name constraint, we can call regulator_by_platname(), 63 * to find the required regulator. Before return, the regulator is probed, 64 * and the rest of its constraints are put into the device's uclass platform 65 * data, by the uclass regulator '.pre_probe' method. 66 * 67 * For more info about PMIC bind, please refer to file: 'include/power/pmic.h' 68 * 69 * Note: 70 * Please do not use the device_bind_by_name() function, since it pass '-1' as 71 * device node offset - and the bind will fail on uclass .post_bind method, 72 * because of missing 'regulator-name' constraint. 73 * 74 * 75 * Fixed Voltage/Current Regulator 76 * =============================== 77 * 78 * When fixed voltage regulator is needed, then enable the config: 79 * - CONFIG_DM_REGULATOR_FIXED 80 * 81 * The driver file: 'drivers/power/regulator/fixed.c', provides basic support 82 * for control the GPIO, and return the device tree constraint values. 83 * 84 * To bind the fixed voltage regulator device, we usually use a 'simple-bus' 85 * node as a parent. And 'regulator-fixed' for the driver compatible. This is 86 * the same as in the kernel. The example node of fixed regulator: 87 * 88 * simple-bus { 89 * compatible = "simple-bus"; 90 * #address-cells = <1>; 91 * #size-cells = <0>; 92 * 93 * blue_led { 94 * compatible = "regulator-fixed"; 95 * regulator-name = "VDD_LED_3.3V"; 96 * regulator-min-microvolt = <3300000>; 97 * regulator-max-microvolt = <3300000>; 98 * gpio = <&gpc1 0 GPIO_ACTIVE_LOW>; 99 * }; 100 * }; 101 * 102 * The fixed regulator devices also provide regulator uclass platform data. And 103 * devices bound from such node, can use the regulator drivers API. 104*/ 105 106/* enum regulator_type - used for regulator_*() variant calls */ 107enum regulator_type { 108 REGULATOR_TYPE_LDO = 0, 109 REGULATOR_TYPE_BUCK, 110 REGULATOR_TYPE_DVS, 111 REGULATOR_TYPE_FIXED, 112 REGULATOR_TYPE_GPIO, 113 REGULATOR_TYPE_OTHER, 114}; 115 116/** 117 * struct dm_regulator_mode - this structure holds an information about 118 * each regulator operation mode. Probably in most cases - an array. 119 * This will be probably a driver-static data, since it is device-specific. 120 * 121 * @id - a driver-specific mode id 122 * @register_value - a driver-specific value for its mode id 123 * @name - the name of mode - used for regulator command 124 * Note: 125 * The field 'id', should be always a positive number, since the negative values 126 * are reserved for the errno numbers when returns the mode id. 127 */ 128struct dm_regulator_mode { 129 int id; /* Set only as >= 0 (negative value is reserved for errno) */ 130 int register_value; 131 const char *name; 132}; 133 134enum regulator_flag { 135 REGULATOR_FLAG_AUTOSET_UV = 1 << 0, 136 REGULATOR_FLAG_AUTOSET_UA = 1 << 1, 137}; 138 139/** 140 * struct dm_regulator_uclass_plat - pointed by dev->uclass_plat, and 141 * allocated on each regulator bind. This structure holds an information 142 * about each regulator's constraints and supported operation modes. 143 * There is no "step" voltage value - so driver should take care of this. 144 * 145 * @type - one of 'enum regulator_type' 146 * @mode - pointer to the regulator mode (array if more than one) 147 * @mode_count - number of '.mode' entries 148 * @min_uV* - minimum voltage (micro Volts) 149 * @max_uV* - maximum voltage (micro Volts) 150 * @min_uA* - minimum amperage (micro Amps) 151 * @max_uA* - maximum amperage (micro Amps) 152 * @always_on* - bool type, true or false 153 * @boot_on* - bool type, true or false 154 * @force_off* - bool type, true or false 155 * TODO(sjg@chromium.org): Consider putting the above two into @flags 156 * @ramp_delay - Time to settle down after voltage change (unit: uV/us) 157 * @flags: - flags value (see REGULATOR_FLAG_...) 158 * @name** - fdt regulator name - should be taken from the device tree 159 * ctrl_reg: - Control register offset used to enable/disable regulator 160 * volt_reg: - register offset for writing voltage vsel values 161 * 162 * Note: 163 * * - set automatically on device probe by the uclass's '.pre_probe' method. 164 * ** - set automatically on device bind by the uclass's '.post_bind' method. 165 * The constraints: type, mode, mode_count, can be set by device driver, e.g. 166 * by the driver '.probe' method. 167 */ 168struct dm_regulator_uclass_plat { 169 enum regulator_type type; 170 struct dm_regulator_mode *mode; 171 int mode_count; 172 int min_uV; 173 int max_uV; 174 int init_uV; 175 int min_uA; 176 int max_uA; 177 unsigned int ramp_delay; 178 bool always_on; 179 bool boot_on; 180 bool force_off; 181 const char *name; 182 int flags; 183 u8 ctrl_reg; 184 u8 volt_reg; 185 bool suspend_on; 186 u32 suspend_uV; 187}; 188 189/* Regulator device operations */ 190struct dm_regulator_ops { 191 /** 192 * The regulator output value function calls operates on a micro Volts. 193 * 194 * get/set_value - get/set output value of the given output number 195 * @dev - regulator device 196 * Sets: 197 * @uV - set the output value [micro Volts] 198 * @return output value [uV] on success or negative errno if fail. 199 */ 200 int (*get_value)(struct udevice *dev); 201 int (*set_value)(struct udevice *dev, int uV); 202 203 /** 204 * The regulator suspend output value function calls operates 205 * on a micro Volts. 206 * 207 * get/set_suspen_value - get/set suspend mode output value 208 * @dev - regulator device 209 * Sets: 210 * @uV - set the suspend output value [micro Volts] 211 * @return output value [uV] on success or negative errno if fail. 212 */ 213 int (*set_suspend_value)(struct udevice *dev, int uV); 214 int (*get_suspend_value)(struct udevice *dev); 215 216 /** 217 * The regulator output current function calls operates on a micro Amps. 218 * 219 * get/set_current - get/set output current of the given output number 220 * @dev - regulator device 221 * Sets: 222 * @uA - set the output current [micro Amps] 223 * @return output value [uA] on success or negative errno if fail. 224 */ 225 int (*get_current)(struct udevice *dev); 226 int (*set_current)(struct udevice *dev, int uA); 227 228 /** 229 * The most basic feature of the regulator output is its enable state. 230 * 231 * get/set_enable - get/set enable state of the given output number 232 * @dev - regulator device 233 * Sets: 234 * @enable - set true - enable or false - disable 235 * @return true/false for get or -errno if fail; 0 / -errno for set. 236 */ 237 int (*get_enable)(struct udevice *dev); 238 int (*set_enable)(struct udevice *dev, bool enable); 239 240 /** 241 * The most basic feature of the regulator output is its enable state 242 * in suspend mode. 243 * 244 * get/set_suspend_enable - get/set enable state of the suspend output 245 * @dev - regulator device 246 * Sets: 247 * @enable - set true - enable or false - disable 248 * @return true/false for get or -errno if fail; 0 / -errno for set. 249 */ 250 int (*set_suspend_enable)(struct udevice *dev, bool enable); 251 int (*get_suspend_enable)(struct udevice *dev); 252 253 /** 254 * The 'get/set_mode()' function calls should operate on a driver- 255 * specific mode id definitions, which should be found in: 256 * field 'id' of struct dm_regulator_mode. 257 * 258 * get/set_mode - get/set operation mode of the given output number 259 * @dev - regulator device 260 * Sets 261 * @mode_id - set output mode id (struct dm_regulator_mode->id) 262 * @return id/0 for get/set on success or negative errno if fail. 263 * Note: 264 * The field 'id' of struct type 'dm_regulator_mode', should be always 265 * a positive number, since the negative is reserved for the error. 266 */ 267 int (*get_mode)(struct udevice *dev); 268 int (*set_mode)(struct udevice *dev, int mode_id); 269}; 270 271#if CONFIG_IS_ENABLED(DM_REGULATOR) 272/** 273 * regulator_mode: returns a pointer to the array of regulator mode info 274 * 275 * @dev - pointer to the regulator device 276 * @modep - pointer to the returned mode info array 277 * @return - count of modep entries on success or negative errno if fail. 278 */ 279int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep); 280 281/** 282 * regulator_get_value: get microvoltage voltage value of a given regulator 283 * 284 * @dev - pointer to the regulator device 285 * @return - positive output value [uV] on success or negative errno if fail. 286 */ 287int regulator_get_value(struct udevice *dev); 288 289/** 290 * regulator_set_value: set the microvoltage value of a given regulator. 291 * 292 * @dev - pointer to the regulator device 293 * @uV - the output value to set [micro Volts] 294 * @return - 0 on success or -errno val if fails 295 */ 296int regulator_set_value(struct udevice *dev, int uV); 297 298/** 299 * regulator_set_suspend_value: set the suspend microvoltage value of a given regulator. 300 * 301 * @dev - pointer to the regulator device 302 * @uV - the output suspend value to set [micro Volts] 303 * @return - 0 on success or -errno val if fails 304 */ 305int regulator_set_suspend_value(struct udevice *dev, int uV); 306 307/** 308 * regulator_get_suspend_value: get the suspend microvoltage value of a given regulator. 309 * 310 * @dev - pointer to the regulator device 311 * @return - positive output value [uV] on success or negative errno if fail. 312 */ 313int regulator_get_suspend_value(struct udevice *dev); 314 315/** 316 * regulator_set_value_force: set the microvoltage value of a given regulator 317 * without any min-,max condition check 318 * 319 * @dev - pointer to the regulator device 320 * @uV - the output value to set [micro Volts] 321 * @return - 0 on success or -errno val if fails 322 */ 323int regulator_set_value_force(struct udevice *dev, int uV); 324 325/** 326 * regulator_get_current: get microampere value of a given regulator 327 * 328 * @dev - pointer to the regulator device 329 * @return - positive output current [uA] on success or negative errno if fail. 330 */ 331int regulator_get_current(struct udevice *dev); 332 333/** 334 * regulator_set_current: set the microampere value of a given regulator. 335 * 336 * @dev - pointer to the regulator device 337 * @uA - set the output current [micro Amps] 338 * @return - 0 on success or -errno val if fails 339 */ 340int regulator_set_current(struct udevice *dev, int uA); 341 342/** 343 * regulator_get_enable: get regulator device enable state. 344 * 345 * @dev - pointer to the regulator device 346 * @return - true/false of enable state or -errno val if fails 347 */ 348int regulator_get_enable(struct udevice *dev); 349 350/** 351 * regulator_set_enable: set regulator enable state 352 * 353 * @dev - pointer to the regulator device 354 * @enable - set true or false 355 * @return - 0 on success or -errno val if fails 356 */ 357int regulator_set_enable(struct udevice *dev, bool enable); 358 359/** 360 * regulator_set_enable_if_allowed: set regulator enable state if allowed by 361 * regulator 362 * 363 * @dev - pointer to the regulator device 364 * @enable - set true or false 365 * @return - 0 on success or if enabling is not supported 366 * -errno val if fails. 367 */ 368int regulator_set_enable_if_allowed(struct udevice *dev, bool enable); 369 370/** 371 * regulator_set_suspend_enable: set regulator suspend enable state 372 * 373 * @dev - pointer to the regulator device 374 * @enable - set true or false 375 * @return - 0 on success or -errno val if fails 376 */ 377int regulator_set_suspend_enable(struct udevice *dev, bool enable); 378 379/** 380 * regulator_get_suspend_enable: get regulator suspend enable state 381 * 382 * @dev - pointer to the regulator device 383 * @return - true/false of enable state or -errno val if fails 384 */ 385int regulator_get_suspend_enable(struct udevice *dev); 386 387/** 388 * regulator_get_mode: get active operation mode id of a given regulator 389 * 390 * @dev - pointer to the regulator device 391 * @return - positive mode 'id' number on success or -errno val if fails 392 * Note: 393 * The device can provide an array of operating modes, which is type of struct 394 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside 395 * that array. By calling this function, the driver should return an active mode 396 * id of the given regulator device. 397 */ 398int regulator_get_mode(struct udevice *dev); 399 400/** 401 * regulator_set_mode: set the given regulator's, active mode id 402 * 403 * @dev - pointer to the regulator device 404 * @mode_id - mode id to set ('id' field of struct type dm_regulator_mode) 405 * @return - 0 on success or -errno value if fails 406 * Note: 407 * The device can provide an array of operating modes, which is type of struct 408 * dm_regulator_mode. Each mode has it's own 'id', which should be unique inside 409 * that array. By calling this function, the driver should set the active mode 410 * of a given regulator to given by "mode_id" argument. 411 */ 412int regulator_set_mode(struct udevice *dev, int mode_id); 413 414/** 415 * regulators_enable_boot_on() - enable regulators needed for boot 416 * 417 * This enables all regulators which are marked to be on at boot time. This 418 * only works for regulators which don't have a range for voltage/current, 419 * since in that case it is not possible to know which value to use. 420 * 421 * This effectively calls regulator_autoset() for every regulator. 422 */ 423int regulators_enable_boot_on(bool verbose); 424 425/** 426 * regulators_enable_boot_off() - disable regulators needed for boot 427 * 428 * This disables all regulators which are marked to be off at boot time. 429 * 430 * This effectively calls regulator_unset() for every regulator. 431 */ 432int regulators_enable_boot_off(bool verbose); 433 434/** 435 * regulator_autoset: setup the voltage/current on a regulator 436 * 437 * The setup depends on constraints found in device's uclass's platform data 438 * (struct dm_regulator_uclass_plat): 439 * 440 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true, 441 * or if both are unset, then the function returns 442 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal 443 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal 444 * 445 * The function returns on the first-encountered error. 446 * 447 * @platname - expected string for dm_regulator_uclass_plat .name field 448 * @devp - returned pointer to the regulator device - if non-NULL passed 449 * @return: 0 on success or negative value of errno. 450 */ 451int regulator_autoset(struct udevice *dev); 452 453/** 454 * regulator_unset: turn off a regulator 455 * 456 * The setup depends on constraints found in device's uclass's platform data 457 * (struct dm_regulator_uclass_platdata): 458 * 459 * - Disable - will set - if 'force_off' is set to true, 460 * 461 * The function returns on the first-encountered error. 462 */ 463int regulator_unset(struct udevice *dev); 464 465/** 466 * regulator_autoset_by_name: setup the regulator given by its uclass's 467 * platform data name field. The setup depends on constraints found in device's 468 * uclass's platform data (struct dm_regulator_uclass_plat): 469 * - Enable - will set - if any of: 'always_on' or 'boot_on' is set to true, 470 * or if both are unset, then the function returns 471 * - Voltage value - will set - if '.min_uV' and '.max_uV' values are equal 472 * - Current limit - will set - if '.min_uA' and '.max_uA' values are equal 473 * 474 * The function returns on first encountered error. 475 * 476 * @platname - expected string for dm_regulator_uclass_plat .name field 477 * @devp - returned pointer to the regulator device - if non-NULL passed 478 * @return: 0 on success or negative value of errno. 479 * 480 * The returned 'regulator' device can be used with: 481 * - regulator_get/set_* 482 */ 483int regulator_autoset_by_name(const char *platname, struct udevice **devp); 484 485/** 486 * regulator_list_autoset: setup the regulators given by list of their uclass's 487 * platform data name field. The setup depends on constraints found in device's 488 * uclass's platform data. The function loops with calls to: 489 * regulator_autoset_by_name() for each name from the list. 490 * 491 * @list_platname - an array of expected strings for .name field of each 492 * regulator's uclass plat 493 * @list_devp - an array of returned pointers to the successfully setup 494 * regulator devices if non-NULL passed 495 * @verbose - (true/false) print each regulator setup info, or be quiet 496 * @return 0 on successfully setup of all list entries, otherwise first error. 497 * 498 * The returned 'regulator' devices can be used with: 499 * - regulator_get/set_* 500 * 501 * Note: The list must ends with NULL entry, like in the "platname" list below: 502 * char *my_regulators[] = { 503 * "VCC_3.3V", 504 * "VCC_1.8V", 505 * NULL, 506 * }; 507 */ 508int regulator_list_autoset(const char *list_platname[], 509 struct udevice *list_devp[], 510 bool verbose); 511 512/** 513 * regulator_get_by_devname: returns the pointer to the pmic regulator device. 514 * Search by name, found in regulator device's name. 515 * 516 * @devname - expected string for 'dev->name' of regulator device 517 * @devp - returned pointer to the regulator device 518 * @return 0 on success or negative value of errno. 519 * 520 * The returned 'regulator' device is probed and can be used with: 521 * - regulator_get/set_* 522 */ 523int regulator_get_by_devname(const char *devname, struct udevice **devp); 524 525/** 526 * regulator_get_by_platname: returns the pointer to the pmic regulator device. 527 * Search by name, found in regulator uclass plat. 528 * 529 * @platname - expected string for uc_pdata->name of regulator uclass plat 530 * @devp - returns pointer to the regulator device or NULL on error 531 * @return 0 on success or negative value of errno. 532 * 533 * The returned 'regulator' device is probed and can be used with: 534 * - regulator_get/set_* 535 */ 536int regulator_get_by_platname(const char *platname, struct udevice **devp); 537 538/** 539 * device_get_supply_regulator: returns the pointer to the supply regulator. 540 * Search by phandle, found in device's node. 541 * 542 * Note: Please pay attention to proper order of device bind sequence. 543 * The regulator device searched by the phandle, must be binded before 544 * this function call. 545 * 546 * @dev - device with supply phandle 547 * @supply_name - phandle name of regulator 548 * @devp - returned pointer to the supply device 549 * @return 0 on success or negative value of errno. 550 */ 551int device_get_supply_regulator(struct udevice *dev, const char *supply_name, 552 struct udevice **devp); 553#else 554static inline int regulator_mode(struct udevice *dev, struct dm_regulator_mode **modep) 555{ 556 return -ENOSYS; 557} 558 559static inline int regulator_get_value(struct udevice *dev) 560{ 561 return -ENOSYS; 562} 563 564static inline int regulator_set_value(struct udevice *dev, int uV) 565{ 566 return -ENOSYS; 567} 568 569static inline int regulator_set_suspend_value(struct udevice *dev, int uV) 570{ 571 return -ENOSYS; 572} 573 574static inline int regulator_get_suspend_value(struct udevice *dev) 575{ 576 return -ENOSYS; 577} 578 579static inline int regulator_set_value_force(struct udevice *dev, int uV) 580{ 581 return -ENOSYS; 582} 583 584static inline int regulator_get_current(struct udevice *dev) 585{ 586 return -ENOSYS; 587} 588 589static inline int regulator_set_current(struct udevice *dev, int uA) 590{ 591 return -ENOSYS; 592} 593 594static inline int regulator_get_enable(struct udevice *dev) 595{ 596 return -ENOSYS; 597} 598 599static inline int regulator_set_enable(struct udevice *dev, bool enable) 600{ 601 return -ENOSYS; 602} 603 604static inline int regulator_set_enable_if_allowed(struct udevice *dev, bool enable) 605{ 606 return -ENOSYS; 607} 608 609static inline int regulator_set_suspend_enable(struct udevice *dev, bool enable) 610{ 611 return -ENOSYS; 612} 613 614static inline int regulator_get_suspend_enable(struct udevice *dev) 615{ 616 return -ENOSYS; 617} 618 619static inline int regulator_get_mode(struct udevice *dev) 620{ 621 return -ENOSYS; 622} 623 624static inline int regulator_set_mode(struct udevice *dev, int mode_id) 625{ 626 return -ENOSYS; 627} 628 629static inline int regulators_enable_boot_on(bool verbose) 630{ 631 return -ENOSYS; 632} 633 634static inline int regulator_autoset(struct udevice *dev) 635{ 636 return -ENOSYS; 637} 638 639static inline int regulator_autoset_by_name(const char *platname, struct udevice **devp) 640{ 641 return -ENOSYS; 642} 643 644static inline int regulator_list_autoset(const char *list_platname[], struct udevice *list_devp[], 645 bool verbose) 646{ 647 return -ENOSYS; 648} 649 650static inline int regulator_get_by_devname(const char *devname, struct udevice **devp) 651{ 652 return -ENOSYS; 653} 654 655static inline int regulator_get_by_platname(const char *platname, struct udevice **devp) 656{ 657 return -ENOSYS; 658} 659 660static inline int device_get_supply_regulator(struct udevice *dev, const char *supply_name, 661 struct udevice **devp) 662{ 663 return -ENOSYS; 664} 665#endif 666 667#endif /* _INCLUDE_REGULATOR_H_ */ 668