1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8 #include "regulator_if.h"
9 #include "hdf_log.h"
10 #include "regulator_core.h"
11
RegulatorOpen(const char * name)12 DevHandle RegulatorOpen(const char *name)
13 {
14 if (name == NULL) {
15 HDF_LOGE("RegulatorOpen: name is null!");
16 return NULL;
17 }
18
19 return (DevHandle)RegulatorNodeOpen(name);
20 }
21
RegulatorClose(DevHandle handle)22 void RegulatorClose(DevHandle handle)
23 {
24 struct RegulatorNode *node = (struct RegulatorNode *)handle;
25
26 if (node == NULL) {
27 HDF_LOGE("RegulatorClose: node is null!");
28 return;
29 }
30
31 if (RegulatorNodeClose(node) != HDF_SUCCESS) {
32 HDF_LOGE("RegulatorClose: RegulatorNodeClose fail!");
33 return;
34 }
35 }
36
RegulatorEnable(DevHandle handle)37 int32_t RegulatorEnable(DevHandle handle)
38 {
39 struct RegulatorNode *node = (struct RegulatorNode *)handle;
40
41 if (node == NULL) {
42 HDF_LOGE("RegulatorEnable: node is null!");
43 return HDF_FAILURE;
44 }
45
46 int ret = RegulatorNodeEnable(node);
47 if (ret != HDF_SUCCESS) {
48 HDF_LOGE("RegulatorEnable: RegulatorNodeEnable fail!");
49 return ret;
50 }
51
52 return HDF_SUCCESS;
53 }
54
RegulatorDisable(DevHandle handle)55 int32_t RegulatorDisable(DevHandle handle)
56 {
57 struct RegulatorNode *node = (struct RegulatorNode *)handle;
58
59 if (node == NULL) {
60 HDF_LOGE("RegulatorDisable: node is null!");
61 return HDF_FAILURE;
62 }
63
64 int ret = RegulatorNodeDisable(node);
65 if (ret != HDF_SUCCESS) {
66 HDF_LOGE("RegulatorDisable: RegulatorNodeDisable fail!");
67 return ret;
68 }
69
70 return HDF_SUCCESS;
71 }
72
RegulatorForceDisable(DevHandle handle)73 int32_t RegulatorForceDisable(DevHandle handle)
74 {
75 struct RegulatorNode *node = (struct RegulatorNode *)handle;
76
77 if (node == NULL) {
78 HDF_LOGE("RegulatorForceDisable: node is null!");
79 return HDF_FAILURE;
80 }
81
82 int ret = RegulatorNodeForceDisable(node);
83 if (ret != HDF_SUCCESS) {
84 HDF_LOGE("RegulatorForceDisable: RegulatorNodeForceDisable fail!");
85 return ret;
86 }
87
88 return HDF_SUCCESS;
89 }
90
RegulatorSetVoltage(DevHandle handle,uint32_t minUv,uint32_t maxUv)91 int32_t RegulatorSetVoltage(DevHandle handle, uint32_t minUv, uint32_t maxUv)
92 {
93 struct RegulatorNode *node = (struct RegulatorNode *)handle;
94
95 if (node == NULL) {
96 HDF_LOGE("RegulatorSetVoltage: node is null!");
97 return HDF_FAILURE;
98 }
99
100 if (minUv > maxUv) {
101 HDF_LOGE("RegulatorSetVoltage: %s Uv [%u, %u] invalid!",
102 node->regulatorInfo.name, minUv, maxUv);
103 return HDF_FAILURE;
104 }
105
106 int ret = RegulatorNodeSetVoltage(node, minUv, maxUv);
107 if (ret != HDF_SUCCESS) {
108 HDF_LOGE("RegulatorSetVoltage: RegulatorNodeSetVoltage fail!");
109 return HDF_FAILURE;
110 }
111
112 return HDF_SUCCESS;
113 }
114
RegulatorGetVoltage(DevHandle handle,uint32_t * voltage)115 int32_t RegulatorGetVoltage(DevHandle handle, uint32_t *voltage)
116 {
117 struct RegulatorNode *node = (struct RegulatorNode *)handle;
118
119 if (node == NULL || voltage == NULL) {
120 HDF_LOGE("RegulatorGetVoltage: param is null!");
121 return HDF_FAILURE;
122 }
123
124 int ret = RegulatorNodeGetVoltage(node, voltage);
125 if (ret != HDF_SUCCESS) {
126 HDF_LOGE("RegulatorGetVoltage: RegulatorNodeGetVoltage fail!");
127 return HDF_FAILURE;
128 }
129
130 return HDF_SUCCESS;
131 }
132
RegulatorSetCurrent(DevHandle handle,uint32_t minUa,uint32_t maxUa)133 int32_t RegulatorSetCurrent(DevHandle handle, uint32_t minUa, uint32_t maxUa)
134 {
135 struct RegulatorNode *node = (struct RegulatorNode *)handle;
136
137 if (node == NULL) {
138 HDF_LOGE("RegulatorSetCurrent: node is null!");
139 return HDF_FAILURE;
140 }
141
142 if (minUa > maxUa) {
143 HDF_LOGE("RegulatorSetCurrent: %s Ua [%u, %u] invalid!",
144 node->regulatorInfo.name, minUa, maxUa);
145 return HDF_FAILURE;
146 }
147
148 int ret = RegulatorNodeSetCurrent(node, minUa, maxUa);
149 if (ret != HDF_SUCCESS) {
150 HDF_LOGE("RegulatorSetCurrent: RegulatorNodeSetCurrent fail!");
151 return HDF_FAILURE;
152 }
153
154 return HDF_SUCCESS;
155 }
156
RegulatorGetCurrent(DevHandle handle,uint32_t * regCurrent)157 int32_t RegulatorGetCurrent(DevHandle handle, uint32_t *regCurrent)
158 {
159 struct RegulatorNode *node = (struct RegulatorNode *)handle;
160
161 if (node == NULL || regCurrent == NULL) {
162 HDF_LOGE("RegulatorGetCurrent: node or regCurrent is null!");
163 return HDF_FAILURE;
164 }
165
166 int ret = RegulatorNodeGetCurrent(node, regCurrent);
167 if (ret != HDF_SUCCESS) {
168 HDF_LOGE("RegulatorGetCurrent: RegulatorNodeGetCurrent fail!");
169 return HDF_FAILURE;
170 }
171
172 return HDF_SUCCESS;
173 }
174
RegulatorGetStatus(DevHandle handle,uint32_t * status)175 int32_t RegulatorGetStatus(DevHandle handle, uint32_t *status)
176 {
177 struct RegulatorNode *node = (struct RegulatorNode *)handle;
178
179 if (node == NULL || status == NULL) {
180 HDF_LOGE("RegulatorGetStatus: node or status is null!");
181 return HDF_FAILURE;
182 }
183
184 int ret = RegulatorNodeGetStatus(node, status);
185 if (ret != HDF_SUCCESS) {
186 HDF_LOGE("RegulatorGetStatus: RegulatorNodeGetStatus fail!");
187 return HDF_FAILURE;
188 }
189
190 return HDF_SUCCESS;
191 }
192