未验证 提交 bc90c086 编写于 作者: O openharmony_ci 提交者: Gitee

!3084 #I4ZP42 完成,请审批

Merge pull request !3084 from Annie_wang/PR2551
# Regulator<a name="title_REGULATOR_des"></a> # Regulator
## Overview<a name="section1_REGULATOR_des"></a>
## Overview
### Regulator
The 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. The 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.
The regulator APIs provide a set of methods for managing a regulator, including those for:
- The regulator APIs provide a set of functions for managing a regulator, including those for: - Opening or closing a regulator device handle
- Opening or closing a regulator device handle - Setting the output voltage and current for a regulator
- Setting the output voltage and current for a regulator - Enabling or disabling a regulator
- Enabling or disabling a regulator - Obtaining the voltage, current, and status of a regulator
- Obtaining the voltage, current, and status of a regulator
## Available APIs<a name="section2_REGULATOR_des"></a>
**Table 1** Regulator APIs
<a name="table1_REGULATOR_des"></a>
<table border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse:
collapse;table-layout:fixed;width:855pt">
<colgroup><col width="183" style="mso-width-source:userset;mso-width-alt:5856;width:137pt">
<col width="159" style="mso-width-source:userset;mso-width-alt:5088;width:119pt">
<col width="213" style="mso-width-source:userset;mso-width-alt:6816;width:160pt">
</colgroup><tbody><tr height="19" style="height:14.25pt">
<td height="19" width="183" style="height:14.25pt;width:137pt">Category</td>
<td width="159" style="width:119pt">API</td>
<td width="213" style="width:160pt">Description</td>
</tr>
<tr height="19" style="height:14.25pt">
<td rowspan="2" height="38" class="xl65" style="height:28.5pt">Operating a regulator device handle</td>
<td>RegulatorOpen</td>
<td>Opens a regulator device handle.</td>
</tr>
<tr height="19" style="height:14.25pt">
<td height="19" style="height:14.25pt">RegulatorClose</td>
<td>Closes a regulator device handle.</td>
</tr>
<tr height="19" style="height:14.25pt">
<td rowspan="3" height="57" class="xl65" style="height:42.75pt">Enabling or disable a regulator</td>
<td>RegulatorEnable</td>
<td>Enables a regulator.</td>
</tr>
<tr height="19" style="height:14.25pt">
<td height="19" style="height:14.25pt">RegulatorDisable</td>
<td>Disables a regulator.</td>
</tr>
<tr height="19" style="height:14.25pt">
<td height="19" style="height:14.25pt">RegulatorForceDisable</td>
<td>Forcibly disables a regulator.</td>
</tr>
<tr height="19" style="height:14.25pt">
<td rowspan="2" height="38" class="xl65" style="height:28.5pt">Setting or obtaining the output voltage</td>
<td>RegulatorSetVoltage</td>
<td>Sets the output voltage for a regulator.</td>
</tr>
<tr height="19" style="height:14.25pt">
<td height="19" style="height:14.25pt">RegulatorGetVoltage</td>
<td>Obtains the output voltage of a regulator.</td>
</tr>
<tr height="19" style="height:14.25pt">
<td rowspan="2" height="38" class="xl65" style="height:28.5pt">Setting or obtaining the output current</td>
<td>RegulatorSetCurrent</td>
<td>Sets the output current for a regulator.</td>
</tr>
<tr height="19" style="height:14.25pt">
<td height="19" style="height:14.25pt">RegulatorGetCurrent</td>
<td>Obtains the output current of a regulator.</td>
</tr>
<tr height="19" style="height:14.25pt">
<td height="19" class="xl66" style="height:14.25pt">Querying the regulator status</td>
<td>RegulatorGetStatus</td>
<td>Obtains the regulator status.</td>
</tr>
<!--[if supportMisalignedColumns]-->
<tr height="0" style="display:none">
<td width="183" style="width:137pt"></td>
<td width="159" style="width:119pt"></td>
<td width="213" style="width:160pt"></td>
</tr>
<!--[endif]-->
</tbody></table>
>![](../public_sys-resources/icon-note.gif) **NOTE**
>The regulator module can be used in kernel mode but not in user mode.
## Usage Guidelines<a name="section3_REGULATOR_des"></a>
### How to Use<a name="section3.1_REGULATOR_des"></a>
During 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.
The figure below shows the process of using a regulator. ### Basic Concepts
- Calibrator
Software used to maintain stable output voltage when the output load is different from the input voltage.
- Consumer
The devices served by the regulator are called consumers. Consumers are classified into the following:
* 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.
* Dynamic consumer: The voltage and current need to be changed based on operation requirements for this type of consumers.
- Power Management IC (PMIC)
Power management chipset provides efficient, reliable, and safe voltage regulation.
### Working Principles
In 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.
The regulator module is divided into the following layers:
- The interface layer provides APIs for opening or closing a device and writing data.
- The core layer provides the capabilities of binding, initializing, and releasing devices.
- The adaptation layer implements other functions.
![](../public_sys-resources/icon-note.gif)NOTE<br/>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.
**Figure 1** Unified service mode
![image1](figures/unified-service-mode.png)
### Constraints
**Figure 1** Process of using a regulator<a name="fig1_regulator_des"></a> Currently, the regulator module supports only the kernels (LiteOS) of mini and small systems.
## Development Guidelines
### When to Use
Regulators are used to:
- Control the voltage/current supplies of some devices in the system.
- Manage regulated power supplies.
### Available APIs
**Table 1** Regulator module APIs
| API | Description |
| --------------------- | ------------------------- |
| RegulatorOpen | Opens the regulator device handle.|
| RegulatorClose | Closes the regulator device handle.|
| RegulatorEnable | Enables a regulator. |
| RegulatorDisable | Disables a regulator. |
| RegulatorForceDisable | Forcibly disables a regulator. |
| RegulatorSetVoltage | Sets the regulator output voltage. |
| RegulatorGetVoltage | Obtains the regulator output voltage. |
| RegulatorSetCurrent | Sets the regulator output current. |
| RegulatorGetCurrent | Obtains the regulator output current. |
| RegulatorGetStatus | Obtains the regulator status. |
### How to Develop
During 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.
The figure below illustrates the process of using a regulator.
**Figure 2** Process of using a regulator
![](figures/process-of-using-regulator.png) ![](figures/process-of-using-regulator.png)
### Opening a Regulator Device Handle<a name="section3.2_REGULATOR_des"></a> #### Opening a Regulator Device Handle
Before operating a regulator, call **RegulatorOpen** to open the device handle of the regulator. This function returns the device handle of the regulator. Before operating a regulator, call **RegulatorOpen** to open the device handle of the regulator. This function returns the device handle of the regulator.
```c ```
DevHandle RegulatorOpen(const char *name); DevHandle RegulatorOpen(const char *name);
``` ```
**Table 2** Description of RegulatorOpen **Table 2** Description of RegulatorOpen
<a name="table2_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ---------- | ----------------------------- | | ---------- | ----------------------------- |
| name | Name of the target regulator. | | name | Name of the target regulator. |
...@@ -116,8 +108,9 @@ DevHandle RegulatorOpen(const char *name); ...@@ -116,8 +108,9 @@ DevHandle RegulatorOpen(const char *name);
| NULL | The operation fails. | | NULL | The operation fails. |
```c
/* Regulator name. */ ```
/* Regulator device name */
const char *name = "regulator_virtual_1"; const char *name = "regulator_virtual_1";
DevHandle handle = NULL; DevHandle handle = NULL;
...@@ -128,48 +121,45 @@ if (handle == NULL) { ...@@ -128,48 +121,45 @@ if (handle == NULL) {
} }
``` ```
### Closing a Regulator Device Handle<a name="section3.3_REGULATOR_des"></a> #### Closing a Regulator Device Handle
Call **RegulatorClose** to close the regulator device handle to release resources. Call **RegulatorClose** to close the regulator device handle to release resources.
```c ```
void RegulatorClose(DevHandle handle); void RegulatorClose(DevHandle handle);
``` ```
**Table 3** Description of RegulatorClose **Table 3** Description of RegulatorClose
<a name="table3_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ------ | ----------------- | | ------ | ----------------- |
| handle | Regulator device handle to close.| | handle | Device handle of the target regulator.|
```c ```
/* Close the regulator device handle. */ /* Close the regulator device handle. */
RegulatorClose(handle); RegulatorClose(handle);
``` ```
### Enabling a Regulator<a name="section3.4_REGULATOR_des"></a> #### Enabling a Regulator
Call **RegulatorEnable** to enable a regulator. Call **RegulatorEnable()** to enable a regulator.
```c ```
int32_t RegulatorEnable(DevHandle handle); int32_t RegulatorEnable(DevHandle handle);
``` ```
**Table 4** Description of RegulatorEnable **Table 4** Description of RegulatorEnable
<a name="table4_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ---------- | ----------------- | | ---------- | ----------------- |
| handle | Device handle of the regulator to enable.| | handle | Device handle of the target regulator.|
| **Return Value**| **Description** | | **Return Value**| **Description** |
| 0 | The operation is successful. | | 0 | The operation is successful. |
| Negative value | The operation fails. | | Negative value | The operation fails. |
```c
```
int32_t ret; int32_t ret;
/* Enable the regulator. */ /* Enable the regulator. */
...@@ -179,26 +169,24 @@ if (ret != 0) { ...@@ -179,26 +169,24 @@ if (ret != 0) {
} }
``` ```
### Disabling a Regulator<a name="section3.5_REGULATOR_des"></a> #### Disabling a Regulator
Call **RegulatorDisable** to disable a regulator. The operation will fail if the regulator status is set to always on or if a child node of the regulator is not disabled. Call **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.
```c ```
int32_t RegulatorDisable(DevHandle handle); int32_t RegulatorDisable(DevHandle handle);
``` ```
**Table 5** Description of RegulatorDisable **Table 5** Description of RegulatorDisable
<a name="table5_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ---------- | ----------------- | | ---------- | ----------------- |
| handle | Device handle of the regulator to disable.| | handle | Device handle of the target regulator.|
| **Return Value**| **Description** | | **Return Value**| **Description** |
| 0 | The operation is successful. | | 0 | The operation is successful. |
| Negative value | The operation fails. | | Negative value | The operation fails. |
```c ```
int32_t ret; int32_t ret;
/* Disable the regulator. */ /* Disable the regulator. */
...@@ -208,17 +196,16 @@ if (ret != 0) { ...@@ -208,17 +196,16 @@ if (ret != 0) {
} }
``` ```
### Forcibly Disabling a Regulator<a name="section3.6_REGULATOR_des"></a> #### Forcibly Disabling a Regulator
Call **RegulatorForceDisable** to forcibly disable a regulator. The regulator will be disabled event if its status is set to always on or its child node is still enabled. Call **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.
```c ```
int32_t RegulatorForceDisable(DevHandle handle); int32_t RegulatorForceDisable(DevHandle handle);
``` ```
**Table 6** Description of RegulatorForceDisable **Table 6** Description of RegulatorForceDisable
<a name="table6_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ---------- | ----------------- | | ---------- | ----------------- |
...@@ -227,27 +214,26 @@ int32_t RegulatorForceDisable(DevHandle handle); ...@@ -227,27 +214,26 @@ int32_t RegulatorForceDisable(DevHandle handle);
| 0 | The operation is successful. | | 0 | The operation is successful. |
| Negative value | The operation fails. | | Negative value | The operation fails. |
```c ```
int32_t ret; int32_t ret;
/* Forcibly disable the regulator. */ /* Forcibly disable the regulator. /*
ret = RegulatorForceDisable(handle); ret = RegulatorForceDisable(handle);
if (ret != 0) { if (ret != 0) {
/* Error handling. */ /* Error handling. */
} }
``` ```
### Setting the Output Voltage Range for a Regulator<a name="section3.7_REGULATOR_des"></a>
#### Setting the Output Voltage Range
Call **RegulatorSetVoltage** to set the output voltage range for a regulator. Call **RegulatorSetVoltage** to set the output voltage range for a regulator.
```c ```
int32_t RegulatorSetVoltage(DevHandle handle, uint32_t minUv, uint32_t maxUv); int32_t RegulatorSetVoltage(DevHandle handle, uint32_t minUv, uint32_t maxUv);
``` ```
**Table 7** Description of RegulatorSetVoltage **Table 7** Description of RegulatorSetVoltage
<a name="table7_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ---------- | ----------------- | | ---------- | ----------------- |
| handle | Device handle of the target regulator.| | handle | Device handle of the target regulator.|
...@@ -257,28 +243,28 @@ int32_t RegulatorSetVoltage(DevHandle handle, uint32_t minUv, uint32_t maxUv); ...@@ -257,28 +243,28 @@ int32_t RegulatorSetVoltage(DevHandle handle, uint32_t minUv, uint32_t maxUv);
| 0 | The operation is successful. | | 0 | The operation is successful. |
| Negative value | The operation fails. | | Negative value | The operation fails. |
```c ```
int32_t ret; int32_t ret;
int32_t minUv = 0; // Set the minimum voltage to 0 µV. int32_t minUv = 0; // The minimum voltage is 0 µV.
int32_t maxUv = 20000; // Set the maximum voltage to 20000 µV. int32_t maxUv = 20000; // The maximum voltage is 20000 µV.
/* Set the output voltage range for the regulator. */ /* Set the output voltage range of the regulator. */
ret = RegulatorSetVoltage(handle, minUv, maxUv); ret = RegulatorSetVoltage(handle, minUv, maxUv);
if (ret != 0) { if (ret != 0) {
/* Error handling. */ /* Error handling. */
} }
``` ```
### Obtaining Regulator Voltage Information<a name="section3.8_REGULATOR_des"></a>
Call **RegulatorGetVoltage** to obtain voltage information of a regulator. #### Obtaining the Regulator Voltage
Call **RegulatorGetVoltage()** to obtain the regulator voltage.
```c ```
int32_t RegulatorGetVoltage(DevHandle handle, uint32_t *voltage); int32_t RegulatorGetVoltage(DevHandle handle, uint32_t *voltage);
``` ```
**Table 8** Description of RegulatorGetVoltage **Table 8** Description of RegulatorGetVoltage
<a name="table8_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ---------- | ----------------- | | ---------- | ----------------- |
...@@ -288,30 +274,27 @@ int32_t RegulatorGetVoltage(DevHandle handle, uint32_t *voltage); ...@@ -288,30 +274,27 @@ int32_t RegulatorGetVoltage(DevHandle handle, uint32_t *voltage);
| 0 | The operation is successful. | | 0 | The operation is successful. |
| Negative value | The operation fails. | | Negative value | The operation fails. |
```c ```
int32_t ret; int32_t ret;
uint32_t voltage; uint32_t voltage;
/* Obtain the regulator voltage information. */ /* Obtain the regulator voltage. */
ret = RegulatorGetVoltage(handle, &voltage); ret = RegulatorGetVoltage(handle, &voltage);
if (ret != 0) { if (ret != 0) {
/* Error handling. */ /* Error handling. */
} }
``` ```
#### Setting the Output Current Range
### Setting the Output Current Range for a Regulator<a name="section3.9_REGULATOR_des"></a> Call **RegulatorSetCurrent()** to set the output current range of the regulator.
Call **RegulatorSetCurrent** to set the output current range for a regulator.
```c ```
int32_t RegulatorSetCurrent(DevHandle handle, uint32_t minUa, uint32_t maxUa); int32_t RegulatorSetCurrent(DevHandle handle, uint32_t minUa, uint32_t maxUa);
``` ```
**Table 9** Description of RegulatorSetCurrent **Table 9** Description of RegulatorSetCurrent
<a name="table9_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ---------- | ----------------- | | ---------- | ----------------- |
| handle | Device handle of the target regulator.| | handle | Device handle of the target regulator.|
...@@ -321,30 +304,28 @@ int32_t RegulatorSetCurrent(DevHandle handle, uint32_t minUa, uint32_t maxUa); ...@@ -321,30 +304,28 @@ int32_t RegulatorSetCurrent(DevHandle handle, uint32_t minUa, uint32_t maxUa);
| 0 | The operation is successful. | | 0 | The operation is successful. |
| Negative value | The operation fails. | | Negative value | The operation fails. |
```c ```
int32_t ret; int32_t ret;
int32_t minUa = 0; // Set the minimum current to 0 μA. int32_t minUa = 0; // The minimum current is 0 μA.
int32_t maxUa = 200; // Set the maximum current to 200 μA. int32_t maxUa = 200; // The maximum current is 200 μA.
/* Set the output current range for the regulator. */ /* Set the output current range of the regulator. */
ret = RegulatorSetCurrent(handle, minUa, maxUa); ret = RegulatorSetCurrent(handle, minUa, maxUa);
if (ret != 0) { if (ret != 0) {
/* Error handling. */ /* Error handling. */
} }
``` ```
### Obtaining Regulator Current Information<a name="section3.10_REGULATOR_des"></a> #### Obtaining the Regulator Current
Call **RegulatorGetCurrent** to obtain current information of a regulator. Call **RegulatorGetCurrent()** to obtain the current of the regulator.
```c ```
int32_t RegulatorGetCurrent(DevHandle handle, uint32_t *regCurrent); int32_t RegulatorGetCurrent(DevHandle handle, uint32_t *regCurrent);
``` ```
**Table 10** Description of RegulatorGetCurrent **Table 10** Description of RegulatorGetCurrent
<a name="table10_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ----------- | ----------------- | | ----------- | ----------------- |
| handle | Device handle of the target regulator.| | handle | Device handle of the target regulator.|
...@@ -353,28 +334,27 @@ int32_t RegulatorGetCurrent(DevHandle handle, uint32_t *regCurrent); ...@@ -353,28 +334,27 @@ int32_t RegulatorGetCurrent(DevHandle handle, uint32_t *regCurrent);
| 0 | The operation is successful. | | 0 | The operation is successful. |
| Negative value | The operation fails. | | Negative value | The operation fails. |
```c ```
int32_t ret; int32_t ret;
uint32_t regCurrent; uint32_t regCurrent;
/* Obtain the regulator current information. */ /* Obtain the current of the regulator. */
ret = RegulatorGetCurrent(handle, &regCurrent); ret = RegulatorGetCurrent(handle, &regCurrent);
if (ret != 0) { if (ret != 0) {
/* Error handling. */ /* Error handling. */
} }
``` ```
### Obtaining Regulator Status<a name="section3.11_REGULATOR_des"></a>
Call **RegulatorGetStatus** to obtain the regulator status. #### Obtaining the Regulator Status
Call **RegulatorGetStatus()** to obtain the regulator status.
```c ```
int32_t RegulatorGetStatus(DevHandle handle, uint32_t *status); int32_t RegulatorGetStatus(DevHandle handle, uint32_t *status);
``` ```
**Table 11** Description of RegulatorGetStatus **Table 11** Description of RegulatorGetStatus
<a name="table11_REGULATOR_des"></a>
| **Parameter** | **Description** | | **Parameter** | **Description** |
| ---------- | ----------------- | | ---------- | ----------------- |
| handle | Device handle of the target regulator.| | handle | Device handle of the target regulator.|
...@@ -383,7 +363,7 @@ int32_t RegulatorGetStatus(DevHandle handle, uint32_t *status); ...@@ -383,7 +363,7 @@ int32_t RegulatorGetStatus(DevHandle handle, uint32_t *status);
| 0 | The operation is successful. | | 0 | The operation is successful. |
| Negative value | The operation fails. | | Negative value | The operation fails. |
```c ```
int32_t ret; int32_t ret;
uint32_t status; uint32_t status;
...@@ -394,16 +374,18 @@ if (ret != 0) { ...@@ -394,16 +374,18 @@ if (ret != 0) {
} }
``` ```
## Development Example<a name="section4_REGULATOR_des"></a>
## Development Example
The following is an example of using a regulator. The following is an example of using a regulator.
```c ```
void RegulatorTestSample(void) void RegulatorTestSample(void)
{ {
int32_t ret; int32_t ret;
/* Regulator name. */ /* Regulator device name */
const char *name = "regulator_virtual_1"; const char *name = "regulator_virtual_1";
DevHandle handle = NULL; DevHandle handle = NULL;
...@@ -421,10 +403,10 @@ void RegulatorTestSample(void) ...@@ -421,10 +403,10 @@ void RegulatorTestSample(void)
goto _ERR; goto _ERR;
} }
int32_t minUv = 0; // Set the minimum voltage to 0 µV. int32_t minUv = 0; // The minimum voltage is 0 µV.
int32_t maxUv = 20000; // Set the maximum voltage to 20000 µV. int32_t maxUv = 20000; // The maximum voltage is 20000 µV.
/* Set the output voltage range for the regulator. */ /* Set the output voltage range of the regulator. */
ret = RegulatorSetVoltage(handle, minUv, maxUv); ret = RegulatorSetVoltage(handle, minUv, maxUv);
if (ret != 0) { if (ret != 0) {
HDF_LOGE("RegulatorSetVoltage: failed, ret %d\n", ret); HDF_LOGE("RegulatorSetVoltage: failed, ret %d\n", ret);
...@@ -433,7 +415,7 @@ void RegulatorTestSample(void) ...@@ -433,7 +415,7 @@ void RegulatorTestSample(void)
uint32_t voltage; uint32_t voltage;
/* Obtain the regulator voltage information. */ /* Obtain the regulator voltage. */
ret = RegulatorGetVoltage(handle, &voltage); ret = RegulatorGetVoltage(handle, &voltage);
if (ret != 0) { if (ret != 0) {
HDF_LOGE("RegulatorGetVoltage: failed, ret %d\n", ret); HDF_LOGE("RegulatorGetVoltage: failed, ret %d\n", ret);
......
# Regulator<a name="title_REGULATORDevelop"></a> # Regulator
## Overview<a name="section1_REGULATORDevelop"></a> ## Overview
The regulator module controls the voltage and current supplies of some devices in the system. In 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. ### Regulator
The regulator module controls the voltage and current supplies of some devices in the system.
### Basic Concepts
The 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.
### Working Principles
In 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.
The regulator module is divided into the following layers:
- The interface layer provides APIs for opening or closing a device and writing data.
- The core layer provides the capabilities of binding, initializing, and releasing devices.
- The adaptation layer implements other functions.
![](../public_sys-resources/icon-note.gif)NOTE<br/>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.
**Figure 1** Unified service mode **Figure 1** Unified service mode
![image1](figures/unified-service-mode.png) ![image1](figures/unified-service-mode.png)
## Available APIs<a name="section2_REGULATORDevelop"></a>
### Constraints
Currently, the regulator module supports only the kernels (LiteOS) of mini and small systems.
## Development Guidelines
### When to Use
The regulator module controls the voltage and current supplies of some devices in the system.
### Available APIs
The functions in **RegulatorMethod** are used to call the corresponding regulator driver functions:
**RegulatorMethod** structure: **RegulatorMethod** structure:
```c ```
struct RegulatorMethod { struct RegulatorMethod {
int32_t (*open)(struct RegulatorNode *node); int32_t (*open)(struct RegulatorNode *node);
int32_t (*close)(struct RegulatorNode *node); int32_t (*close)(struct RegulatorNode *node);
...@@ -28,60 +61,39 @@ struct RegulatorMethod { ...@@ -28,60 +61,39 @@ struct RegulatorMethod {
}; };
``` ```
**Table 1** APIs for the members in the RegulatorMethod structure **Table 1** Description of the RegulatorMethod structure
| Method | Input Parameter | Return Value| Description|
| ------------ | ------------------------------------------- | ------ | ---- | | Method | Input Parameter | Return Value | Description |
| open | **node**: structure pointer to the regulator node at the core layer.|HDF_STATUS|Opens a device.| | ------------ | ------------------------------------------------------------ | ------------------ | ---------------- |
| close | **node**: structure pointer to the regulator node at the core layer.| HDF_STATUS| Closes a device.| | open | **node**: structure pointer to the regulator node at the core layer. | HDF_STATUS| Opens a device. |
| release | **node**: structure pointer to the regulator node at the core layer.| HDF_STATUS| Releases a device handle.| | close | **node**: structure pointer to the regulator node at the core layer. | HDF_STATUS| Closes a device. |
| enable | **node**: structure pointer to the regulator node at the core layer.| HDF_STATUS| Enables a device.| | release | **node**: structure pointer to the regulator node at the core layer. | HDF_STATUS| Releases a device handle. |
| disable | **node**: structure pointer to the regulator node at the core layer.| HDF_STATUS| Disables a device.| | enable | **node**: structure pointer to the regulator node at the core layer. | HDF_STATUS| Enabling a Regulator |
| forceDisable | **node**: structure pointer to the regulator node at the core layer.| HDF_STATUS| Forcibly disables a device.| | disable | **node**: structure pointer to the regulator node at the core layer. | HDF_STATUS| Disabling a Regulator |
| forceDisable | **node**: structure pointer to the regulator node at the core layer. | HDF_STATUS| Forcibly Disabling a Regulator |
| setVoltage | **node**: structure pointer to the regulator node at the core layer.<br>**minUv**: minimum voltage to set. It is a uint32_t variable.<br>**maxUv**: maximum voltage to set. It is a uint32_t variable.| HDF_STATUS| Sets the output voltage range.| | setVoltage | **node**: structure pointer to the regulator node at the core layer.<br>**minUv**: minimum voltage to set. It is a uint32_t variable.<br>**maxUv**: maximum voltage to set. It is a uint32_t variable.| HDF_STATUS| Sets the output voltage range.|
| getVoltage | **node**: structure pointer to the regulator node at the core layer.<br>**voltage**: pointer to the output voltage value.| HDF_STATUS| Obtains the voltage.| | getVoltage | **node**: structure pointer to the regulator node at the core layer.<br>**voltage**: pointer to the output voltage value.| HDF_STATUS| Obtains the voltage. |
| setCurrent | **node**: structure pointer to the regulator node at the core layer.<br>**minUa**: minimum current to set. It is a uint32_t variable.<br>**maxUa**: maximum current to set. It is a uint32_t variable.| HDF_STATUS| Sets the output current range.| | setCurrent | **node**: structure pointer to the regulator node at the core layer.<br>**minUa**: minimum current to set. It is a uint32_t variable.<br>**maxUa**: maximum current to set. It is a uint32_t variable.| HDF_STATUS| Sets the output current range.|
| getCurrent | **node**: structure pointer to the regulator node at the core layer.<br>**regCurrent**: pointer to the output current, which is of the uint32_t type.| HDF_STATUS| Obtains the current.| | getCurrent | **node**: structure pointer to the regulator node at the core layer.<br>**regCurrent**: pointer to the output current, which is of the uint32_t type.| HDF_STATUS| Obtains the current. |
| getStatus | **node**: structure pointer to the regulator node at the core layer.<br>**status**: pointer to the output status, which is of the uint32_t type.| HDF_STATUS| Obtains the device status.| | getStatus | **node**: structure pointer to the regulator node at the core layer.<br>**status**: pointer to the output status, which is of the uint32_t type.| HDF_STATUS| Obtains the device status. |
## How to Develop<a name="section3_REGULATORDevelop"></a>
The regulator module adaptation involves the following steps:
1. Instantiate the driver entry.
- Instantiate the **HdfDriverEntry** structure.
- Call **HDF_INIT** to register the **HdfDriverEntry** instance with the HDF.
2. Configure attribute files. ### How to Develop
- Add the **deviceNode** information to the **device_info.hcs** file.
- (Optional) Add the **regulator_config.hcs** file.
3. Instantiate the regulator controller object.
- Initialize **RegulatorNode** members.
- Instantiate **RegulatorMethod**. The regulator module adaptation procedure is as follows:
>![](../public_sys-resources/icon-note.gif) **NOTE**
>For details, see [Available APIs](#availableapis).
4. Debug the driver.
- (Optional) Verify basic functionalities of new drivers. For example, verify the information returned when the driver is loaded and whether data is successfully transmitted.
## Development Example<a name="section4_REGULATORDevelop"></a> - Instantiate the driver entry.
- Configure attribute files.
- Instantiate the core layer APIs.
- Debug the driver.
The following uses **regulator_virtual.c** as an example to present the content to be provided by the vendor to implement device functionalities. 1. Instantiate the driver entry.
1. Instantiate the driver entry. The driver entry must be a global variable of the **HdfDriverEntry** type (defined in **hdf\_device\_desc.h**), and the value of **moduleName** must be the same as that in **device\_info.hcs**. In the HDF, the start address of each **HdfDriverEntry** object of all loaded drivers are collected to form a segment address space similar to an array for the upper layer to invoke.
Generally, the HDF calls the **Bind** function and then the **Init** function to load the driver. If **Init** fails to be called, the HDF calls **Release** to release driver resources and exit. Instantiate the driver entry. The driver entry must be a global variable of the **HdfDriverEntry** type (defined in **hdf_device_desc.h**), and the value of **moduleName** must be the same as that in **device_info.hcs**. In the HDF, the start address of each **HdfDriverEntry** object of all loaded drivers are collected to form a segment address space similar to an array for the upper layer to invoke.
Regulator driver entry reference: Generally, the HDF calls the **Init()** function to load the driver. If **Init()** fails to be called, the HDF calls **Release** to release driver resources and exit.
```c ```
struct HdfDriverEntry g_regulatorDriverEntry = { struct HdfDriverEntry g_regulatorDriverEntry = {
.moduleVersion = 1, .moduleVersion = 1,
.moduleName = "virtual_regulator_driver",// (Mandatory) The value must be the same as that of moduleName in the .hcs file. .moduleName = "virtual_regulator_driver",// (Mandatory) The value must be the same as that of moduleName in the .hcs file.
...@@ -92,14 +104,28 @@ The following uses **regulator_virtual.c** as an example to present the content ...@@ -92,14 +104,28 @@ The following uses **regulator_virtual.c** as an example to present the content
HDF_INIT(g_regulatorDriverEntry); HDF_INIT(g_regulatorDriverEntry);
``` ```
2. Add **deviceNode** to the **device\_info.hcs** file, and set the device attributes in the **regulator\_config.hcs** file. The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the default values or value ranges of the **RegulatorNode** members at the core layer. 2. Configure attribute files.
>![](W:\doc\docs\en\device-dev\public_sys-resources\icon-note.gif) **NOTE** - Add the device node description to the **vendor/hisilicon/hispark_taurus/hdf_config/device_info/device_info.hcs** file.
>If there are multiple devices, add the **deviceNode** information to the **device\_info** file and add the corresponding device attributes to the **regulator\_config** file.
- **device\_info.hcs** reference: The **deviceNode** information is related to registration of the driver entry. The device attribute values are closely related to the default values or value ranges of the **RegulatorNode** members at the core layer.
```c In the unified service mode, the first device node in the **device_info.hcs** file must be the regulator manager. The parameters must be set as follows:
| Member | Value |
| --------------- | ------------------------------------------------------------ |
| policy | **0**, which indicates that no service is published. |
| priority | Driver startup priority. The value range is 0 to 200. A larger value indicates a lower priority. If the priorities are the same, the device loading sequence is not ensured.|
| permission | Driver permission. |
| moduleName | The value is **HDF_PLATFORM_REGULATOR_MANAGER**. |
| serviceName | The value is **HDF_PLATFORM_REGULATOR_MANAGER**. |
| deviceMatchAttr | Reserved. |
Configure regulator controller information from the second node. This node specifies a type of regulator controllers rather than a specific regulator controller. In this example, there is only one regulator device. If there are multiple regulator devices, you need to add the **deviceNode** information to the **device_info** file and add the corresponding device attributes to the **regulator\_config** file.
- **device_info.hcs** configuration reference
```
root { root {
device_info { device_info {
platform :: host { platform :: host {
...@@ -131,7 +157,7 @@ The following uses **regulator_virtual.c** as an example to present the content ...@@ -131,7 +157,7 @@ The following uses **regulator_virtual.c** as an example to present the content
- **regulator\_config.hcs** reference: - **regulator\_config.hcs** reference:
```c ```
root { root {
platform { platform {
regulator_config { regulator_config {
...@@ -175,13 +201,15 @@ The following uses **regulator_virtual.c** as an example to present the content ...@@ -175,13 +201,15 @@ The following uses **regulator_virtual.c** as an example to present the content
} }
``` ```
3. Initialize the **RegulatorNode** object at the core layer, including initializing the vendor custom structure (passing parameters and data), instantiating **RegulatorMethod** (used to call underlying functions of the driver) in **PinCntlr**, and implementing the **HdfDriverEntry** member functions (**Bind**, **Init**, and **Release**). 3. Instantiate the APIs of the core layer.
- Initialize the **RegulatorNode** object at the core layer, including initializing the vendor custom structure (passing parameters and data), instantiating **RegulatorMethod** (used to call underlying functions of the driver) in **PinCntlr**, and implementing the **HdfDriverEntry** member functions (**Bind**, **Init**, and **Release**).
- Initializing the vendor custom structure - Initializing the vendor custom structure
The **RegulatorNode** structure holds parameters and data for the driver. The HDF obtains the values in **regulator\_config.hcs** using **DeviceResourceIface**. The **RegulatorNode** structure holds parameters and data for the driver. The HDF obtains the values in **regulator\_config.hcs** using **DeviceResourceIface**.
```c ```
// RegulatorNode is the controller structure at the core layer. Its members are assigned with values by using the Init function. // RegulatorNode is the controller structure at the core layer. Its members are assigned with values by using the Init function.
struct RegulatorNode { struct RegulatorNode {
struct RegulatorDesc regulatorInfo; struct RegulatorDesc regulatorInfo;
...@@ -215,6 +243,8 @@ The following uses **regulator_virtual.c** as an example to present the content ...@@ -215,6 +243,8 @@ The following uses **regulator_virtual.c** as an example to present the content
}; };
``` ```
- Instantiating **RegulatorMethod** (other members are initialized by **Init**) - Instantiating **RegulatorMethod** (other members are initialized by **Init**)
```c ```c
...@@ -230,6 +260,8 @@ The following uses **regulator_virtual.c** as an example to present the content ...@@ -230,6 +260,8 @@ The following uses **regulator_virtual.c** as an example to present the content
}; };
``` ```
- **Init** function - **Init** function
Input parameters: Input parameters:
...@@ -242,47 +274,20 @@ The following uses **regulator_virtual.c** as an example to present the content ...@@ -242,47 +274,20 @@ The following uses **regulator_virtual.c** as an example to present the content
**Table 2** HDF\_STATUS **Table 2** HDF\_STATUS
<table><thead align="left"><tr id="row31521027164144"><th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.1"><p id="entry1990732428164144p0"><a name="entry1990732428164144p0"></a><a name="entry1990732428164144p0"></a>State</p> | State | Description |
</th> | ---------------------- | -------------- |
<th class="cellrowborder" valign="top" width="50%" id="mcps1.1.3.1.2"><p id="entry2123581292164144p0"><a name="entry2123581292164144p0"></a><a name="entry2123581292164144p0"></a>Description</p> | HDF_ERR_INVALID_OBJECT | Invalid controller object.|
</th> | HDF_ERR_MALLOC_FAIL | Failed to allocate memory. |
</tr> | HDF_ERR_INVALID_PARAM | Invalid parameter. |
</thead> | HDF_ERR_IO | I/O error. |
<tbody><tr id="row1749271383164144"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry202330388164144p0"><a name="entry202330388164144p0"></a><a name="entry202330388164144p0"></a>HDF_ERR_INVALID_OBJECT</p> | HDF_SUCCESS | Initialization successful. |
</td> | HDF_FAILURE | Initialization failed. |
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry1717598064164144p0"><a name="entry1717598064164144p0"></a><a name="entry1717598064164144p0"></a>Invalid controller object.</p>
</td>
</tr>
<tr id="row1715354988164144"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry450625221164144p0"><a name="entry450625221164144p0"></a><a name="entry450625221164144p0"></a>HDF_ERR_MALLOC_FAIL</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry361497788164144p0"><a name="entry361497788164144p0"></a><a name="entry361497788164144p0"></a>Failed to allocate memory.</p>
</td>
</tr>
<tr id="row1202091366164144"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry370837906164144p0"><a name="entry370837906164144p0"></a><a name="entry370837906164144p0"></a>HDF_ERR_INVALID_PARAM</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry353311523164144p0"><a name="entry353311523164144p0"></a><a name="entry353311523164144p0"></a>Invalid parameter.</p>
</td>
</tr>
<tr id="row602018308164144"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry1984036607164144p0"><a name="entry1984036607164144p0"></a><a name="entry1984036607164144p0"></a>HDF_ERR_IO</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry1221756048164144p0"><a name="entry1221756048164144p0"></a><a name="entry1221756048164144p0"></a>I/O error.</p>
</td>
</tr>
<tr id="row47997479164144"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry1220816374164144p0"><a name="entry1220816374164144p0"></a><a name="entry1220816374164144p0"></a>HDF_SUCCESS</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry1903499126164144p0"><a name="entry1903499126164144p0"></a><a name="entry1903499126164144p0"></a>Initialization successful.</p>
</td>
</tr>
<tr id="row2031856197164144"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.1 "><p id="entry463793674164144p0"><a name="entry463793674164144p0"></a><a name="entry463793674164144p0"></a>HDF_FAILURE</p>
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.1.3.1.2 "><p id="entry516362874164144p0"><a name="entry516362874164144p0"></a><a name="entry516362874164144p0"></a>Initialization failed.</p>
</td>
</tr>
</tbody>
</table>
Function description: Function description:
Initializes the custom structure and **RegulatorNode** members, and adds the regulator controller by calling the **RegulatorNodeAdd** function at the core layer. Initializes the custom structure and **RegulatorNode** members, and adds the regulator controller by calling the **RegulatorNodeAdd** function at the core layer.
```c ```c
static int32_t VirtualRegulatorInit(struct HdfDeviceObject *device) static int32_t VirtualRegulatorInit(struct HdfDeviceObject *device)
{ {
...@@ -312,7 +317,7 @@ The following uses **regulator_virtual.c** as an example to present the content ...@@ -312,7 +317,7 @@ The following uses **regulator_virtual.c** as an example to present the content
ret = RegulatorNodeAdd(regNode); // Add the node. ret = RegulatorNodeAdd(regNode); // Add the node.
... ...
} }
```
- **Release** function - **Release** function
...@@ -326,7 +331,7 @@ The following uses **regulator_virtual.c** as an example to present the content ...@@ -326,7 +331,7 @@ The following uses **regulator_virtual.c** as an example to present the content
Function description: Function description:
Releases memory and deletes the controller. This function assigns a value to the **Release** API in the driver entry structure. If the HDF fails to call the **Init** function to initialize the driver, the **Release** function can be called to release driver resources. Releases memory and deletes the controller. This function assigns a value to the **Release** API in the driver entry structure. If the HDF fails to call the **Init()** function to initialize the driver, the **Release()** function can be called to release driver resources.
```c ```c
static void VirtualRegulatorRelease(struct HdfDeviceObject *device) static void VirtualRegulatorRelease(struct HdfDeviceObject *device)
...@@ -334,3 +339,10 @@ The following uses **regulator_virtual.c** as an example to present the content ...@@ -334,3 +339,10 @@ The following uses **regulator_virtual.c** as an example to present the content
... ...
RegulatorNodeRemoveAll();// (Mandatory) Call the function at the core layer to release regulator controller devices and services. RegulatorNodeRemoveAll();// (Mandatory) Call the function at the core layer to release regulator controller devices and services.
} }
```
4. Debug the driver.
(Optional) Verify the basic functions of the new driver, for example, whether the test cases are successful after the driver is loaded.
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册