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