1# Regulator 2 3 4## Overview 5 6### Regulator 7 8The regulator module controls the voltage and current supplies of some devices in the system. In an embedded system (especially a mobile phone), it is important to control the power consumption, which directly affects the battery endurance. You can use a regulator to shut down the power supply to an idle module in the system or reduce the voltage and current for the module. 9The regulator APIs provide a set of methods for managing a regulator, including those for: 10 11- Opening or closing a regulator device handle 12- Setting the output voltage and current for a regulator 13- Enabling or disabling a regulator 14- Obtaining the voltage, current, and status of a regulator 15 16 17### Basic Concepts 18 19- Calibrator 20 21 Software used to maintain stable output voltage when the output load is different from the input voltage. 22 23- Consumer 24 25 The devices served by the regulator are called consumers. Consumers are classified into the following: 26 27 * Static consumer: Only the power needs to be turned on or off for this type of consumers. The voltage and current do not need to be changed. Generally, the consumers are set in the bootloader, firmware, and kernel board phases. 28 * Dynamic consumer: The voltage and current need to be changed based on operation requirements for this type of consumers. 29 30- Power Management IC (PMIC) 31 32 Power management chipset provides efficient, reliable, and safe voltage regulation. 33 34 35### Working Principles 36 37In the Hardware Driver Foundation (HDF), the regulator module uses the unified service mode for API adaptation. In this mode, a device service is used as the regulator manager to handle external access requests in a unified manner, which is reflected in the configuration file. The unified service mode applies to the scenario where there are many device objects of the same type, for example, when the regulator has more than 10 controllers. If the independent service mode is used, more device nodes need to be configured and more memory resources will be consumed by services. 38 39The regulator module is divided into the following layers: 40 41- The interface layer provides APIs for opening or closing a device and writing data. 42- The core layer provides the capabilities of binding, initializing, and releasing devices. 43- The adaptation layer implements other functions. 44 45> ![icon-note.gif](../public_sys-resources/icon-note.gif) **NOTE**<br/> 46> The core layer can call the functions of the interface layer and uses the hook to call functions of the adaptation layer. In this way, the adaptation layer can indirectly call the functions of the interface layer, but the interface layer cannot call the functions of the adaptation layer. 47 48**Figure 1** Unified service mode 49 50![image1](figures/unified-service-mode.png) 51 52### Constraints 53 54Currently, the regulator module supports only the kernels (LiteOS) of mini and small systems. 55 56## Development Guidelines 57 58### When to Use 59 60Regulators are used to: 61 62- Control the voltage/current supplies of some devices in the system. 63- Manage regulated power supplies. 64 65### Available APIs 66 67**Table 1** Regulator module APIs 68 69| API | Description | 70| --------------------- | ------------------------- | 71| RegulatorOpen | Opens the regulator device handle.| 72| RegulatorClose | Closes the regulator device handle.| 73| RegulatorEnable | Enables a regulator. | 74| RegulatorDisable | Disables a regulator. | 75| RegulatorForceDisable | Forcibly disables a regulator. | 76| RegulatorSetVoltage | Sets the regulator output voltage. | 77| RegulatorGetVoltage | Obtains the regulator output voltage. | 78| RegulatorSetCurrent | Sets the regulator output current. | 79| RegulatorGetCurrent | Obtains the regulator output current. | 80| RegulatorGetStatus | Obtains the regulator status. | 81 82 83 84### How to Develop 85 86During the OS startup process, the driver management module loads the regulator driver based on the configuration file. Then, the regulator driver detects the regulator devices and initializes the driver. 87 88The figure below illustrates how to use the APIs. 89 90**Figure 2** Using regulator driver APIs 91 92![](figures/using-regulator-process.png) 93 94#### Opening a Regulator Device Handle 95 96Before operating a regulator, call **RegulatorOpen** to open the device handle of the regulator. This function returns the device handle of the regulator. 97 98``` 99DevHandle RegulatorOpen(const char *name); 100``` 101 102**Table 2** Description of RegulatorOpen 103 104| **Parameter** | **Description** | 105| ---------- | ----------------------------- | 106| name | Name of the target regulator. | 107| **Return Value**| **Description** | 108| handle | The regulator device handle is returned if the operation is successful.| 109| NULL | The operation failed. | 110 111 112 113``` 114/* Regulator device name */ 115const char *name = "regulator_virtual_1"; 116DevHandle handle = NULL; 117 118/* Open the regulator device handle. */ 119handle = RegulatorOpen(name); 120if (handle == NULL) { 121 /* Error handling. */ 122} 123``` 124 125#### Closing a Regulator Device Handle 126 127Call **RegulatorClose** to close the regulator device handle to release resources. 128 129``` 130void RegulatorClose(DevHandle handle); 131``` 132 133**Table 3** Description of RegulatorClose 134 135| **Parameter** | **Description** | 136| ------ | ----------------- | 137| handle | Device handle of the target regulator.| 138 139``` 140/* Close the regulator device handle. */ 141RegulatorClose(handle); 142``` 143 144#### Enabling a Regulator 145 146Call **RegulatorEnable()** to enable a regulator. 147 148``` 149int32_t RegulatorEnable(DevHandle handle); 150``` 151 152**Table 4** Description of RegulatorEnable 153 154| **Parameter** | **Description** | 155| ---------- | ----------------- | 156| handle | Device handle of the target regulator.| 157| **Return Value**| **Description** | 158| 0 | The operation is successful. | 159| Negative value | The operation failed. | 160 161 162 163``` 164int32_t ret; 165 166/* Enable the regulator. */ 167ret = RegulatorEnable(handle); 168if (ret != 0) { 169 /* Error handling. */ 170} 171``` 172 173#### Disabling a Regulator 174 175Call **RegulatorDisable()** to disable a regulator. The operation will fail if the regulator status is set to always on or a child node of the regulator is not disabled. 176 177``` 178int32_t RegulatorDisable(DevHandle handle); 179``` 180 181**Table 5** Description of RegulatorDisable 182 183| **Parameter** | **Description** | 184| ---------- | ----------------- | 185| handle | Device handle of the target regulator.| 186| **Return Value**| **Description** | 187| 0 | The operation is successful. | 188| Negative value | The operation failed. | 189 190``` 191int32_t ret; 192 193/* Disable the regulator. */ 194ret = RegulatorDisable(handle); 195if (ret != 0) { 196 /* Error handling. */ 197} 198``` 199 200#### Forcibly Disabling a Regulator 201 202Call **RegulatorForceDisable()** to forcibly disable a regulator. The regulator will be disabled even if its status is set to always on or its child node is still enabled. 203 204``` 205int32_t RegulatorForceDisable(DevHandle handle); 206``` 207 208**Table 6** Description of RegulatorForceDisable 209 210 211| **Parameter** | **Description** | 212| ---------- | ----------------- | 213| handle | Device handle of the target regulator.| 214| **Return Value**| **Description** | 215| 0 | The operation is successful. | 216| Negative value | The operation failed. | 217 218``` 219int32_t ret; 220 221/* Forcibly disable the regulator. /* 222ret = RegulatorForceDisable(handle); 223if (ret != 0) { 224 /* Error handling. */ 225} 226``` 227 228#### Setting the Output Voltage Range 229 230Call **RegulatorSetVoltage** to set the output voltage range for a regulator. 231 232``` 233int32_t RegulatorSetVoltage(DevHandle handle, uint32_t minUv, uint32_t maxUv); 234``` 235 236**Table 7** Description of RegulatorSetVoltage 237 238| **Parameter** | **Description** | 239| ---------- | ----------------- | 240| handle | Device handle of the target regulator.| 241| minUv | Minimum voltage to set. | 242| maxUv | Maximum voltage to set. | 243| **Return Value**| **Description** | 244| 0 | The operation is successful. | 245| Negative value | The operation failed. | 246 247``` 248int32_t ret; 249int32_t minUv = 0; // The minimum voltage is 0 µV. 250int32_t maxUv = 20000; // The maximum voltage is 20000 µV. 251 252/* Set the output voltage range of the regulator. */ 253ret = RegulatorSetVoltage(handle, minUv, maxUv); 254if (ret != 0) { 255 /* Error handling. */ 256} 257``` 258 259#### Obtaining the Regulator Voltage 260 261Call **RegulatorGetVoltage()** to obtain the regulator voltage. 262 263``` 264int32_t RegulatorGetVoltage(DevHandle handle, uint32_t *voltage); 265``` 266 267**Table 8** Description of RegulatorGetVoltage 268 269 270| **Parameter** | **Description** | 271| ---------- | ----------------- | 272| handle | Device handle of the target regulator.| 273| *voltage | Pointer to the regulator voltage information. | 274| **Return Value**| **Description** | 275| 0 | The operation is successful. | 276| Negative value | The operation failed. | 277 278``` 279int32_t ret; 280uint32_t voltage; 281 282/* Obtain the regulator voltage. */ 283ret = RegulatorGetVoltage(handle, &voltage); 284if (ret != 0) { 285 /* Error handling. */ 286} 287``` 288 289#### Setting the Output Current Range 290 291Call **RegulatorSetCurrent()** to set the output current range of the regulator. 292 293``` 294int32_t RegulatorSetCurrent(DevHandle handle, uint32_t minUa, uint32_t maxUa); 295``` 296 297**Table 9** Description of RegulatorSetCurrent 298 299| **Parameter** | **Description** | 300| ---------- | ----------------- | 301| handle | Device handle of the target regulator.| 302| minUa | Minimum current to set. | 303| maxUa | Maximum current to set. | 304| **Return Value**| **Description** | 305| 0 | The operation is successful. | 306| Negative value | The operation failed. | 307 308``` 309int32_t ret; 310int32_t minUa = 0; // The minimum current is 0 μA. 311int32_t maxUa = 200; // The maximum current is 200 μA. 312 313/* Set the output current range of the regulator. */ 314ret = RegulatorSetCurrent(handle, minUa, maxUa); 315if (ret != 0) { 316 /* Error handling. */ 317} 318``` 319 320#### Obtaining the Regulator Current 321 322Call **RegulatorGetCurrent()** to obtain the current of the regulator. 323 324``` 325int32_t RegulatorGetCurrent(DevHandle handle, uint32_t *regCurrent); 326``` 327 328**Table 10** Description of RegulatorGetCurrent 329 330| **Parameter** | **Description** | 331| ----------- | ----------------- | 332| handle | Device handle of the target regulator.| 333| *regCurrent | Pointer to the regulator current information. | 334| **Return Value** | **Description** | 335| 0 | The operation is successful. | 336| Negative value | The operation failed. | 337 338``` 339int32_t ret; 340uint32_t regCurrent; 341 342/* Obtain the current of the regulator. */ 343ret = RegulatorGetCurrent(handle, ®Current); 344if (ret != 0) { 345 /* Error handling. */ 346} 347``` 348 349#### Obtaining the Regulator Status 350 351Call **RegulatorGetStatus()** to obtain the regulator status. 352 353``` 354int32_t RegulatorGetStatus(DevHandle handle, uint32_t *status); 355``` 356 357**Table 11** Description of RegulatorGetStatus 358 359| **Parameter** | **Description** | 360| ---------- | ----------------- | 361| handle | Device handle of the target regulator.| 362| *status | Pointer to the regulator status information. | 363| **Return Value**| **Description** | 364| 0 | The operation is successful. | 365| Negative value | The operation failed. | 366 367``` 368int32_t ret; 369uint32_t status; 370 371/* Obtain the regulator status. */ 372ret = RegulatorGetStatus(handle, &status); 373if (ret != 0) { 374 /* Error handling. */ 375} 376``` 377 378 379 380## Development Example 381 382The following is an example of using a regulator. 383 384``` 385void RegulatorTestSample(void) 386{ 387 int32_t ret; 388 389 /* Regulator device name */ 390 const char *name = "regulator_virtual_1"; 391 DevHandle handle = NULL; 392 393 /* Open the regulator device handle. */ 394 handle = RegulatorOpen(name); 395 if (handle == NULL) { 396 HDF_LOGE("RegulatorOpen: failed!\n"); 397 return; 398 } 399 400 /* Enable the regulator. */ 401 ret = RegulatorEnable(handle); 402 if (ret != 0) { 403 HDF_LOGE("RegulatorEnable: failed, ret %d\n", ret); 404 goto _ERR; 405 } 406 407 int32_t minUv = 0; // The minimum voltage is 0 µV. 408 int32_t maxUv = 20000; // The maximum voltage is 20000 µV. 409 410 /* Set the output voltage range of the regulator. */ 411 ret = RegulatorSetVoltage(handle, minUv, maxUv); 412 if (ret != 0) { 413 HDF_LOGE("RegulatorSetVoltage: failed, ret %d\n", ret); 414 goto _ERR; 415 } 416 417 uint32_t voltage; 418 419 /* Obtain the regulator voltage. */ 420 ret = RegulatorGetVoltage(handle, &voltage); 421 if (ret != 0) { 422 HDF_LOGE("RegulatorGetVoltage: failed, ret %d\n", ret); 423 goto _ERR; 424 } 425 426 uint32_t status; 427 428 /* Obtain the regulator status. */ 429 ret = RegulatorGetStatus(handle, &status); 430 if (ret != 0) { 431 HDF_LOGE("RegulatorGetStatus: failed, ret %d\n", ret); 432 goto _ERR; 433 } 434 435 /* Disable the regulator. */ 436 ret = RegulatorDisable(handle); 437 if (ret != 0) { 438 HDF_LOGE("RegulatorDisable: failed, ret %d\n", ret); 439 goto _ERR; 440 } 441 442_ERR: 443 /* Close the regulator device handle. */ 444 RegulatorClose(handle); 445} 446``` 447