1 /*
2 * Copyright (c) 2022 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
9 #include "hdf_chip_config.h"
10
ParsePowerConfig(const struct DeviceResourceNode * node,struct HdfPowerConfig * config)11 static int ParsePowerConfig(const struct DeviceResourceNode *node, struct HdfPowerConfig *config)
12 {
13 struct DeviceResourceIface *drsOps = NULL;
14 if (node == NULL || config == NULL) {
15 HDF_LOGE("%s: one of the input para is NULL!", __func__);
16 return HDF_FAILURE;
17 }
18 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
19 if (drsOps == NULL || drsOps->GetUint8 == NULL) {
20 HDF_LOGE("%s: at least one of the paras is NULL!", __func__);
21 return HDF_FAILURE;
22 }
23
24 if (drsOps->GetUint8(node, "powerSeqDelay", &config->powerSeqDelay, 0) != HDF_SUCCESS) {
25 HDF_LOGE("%s: powersSeqDelay fail!", __func__);
26 return HDF_FAILURE;
27 }
28
29 if (drsOps->GetUint8(node, "powerType", &config->type, 0) != HDF_SUCCESS) {
30 HDF_LOGE("%s: type fail!", __func__);
31 return HDF_FAILURE;
32 }
33
34 if (config->type == POWER_TYPE_GPIO) {
35 if (drsOps->GetUint16(node, "gpioId", &config->gpio.gpioId, 0) != HDF_SUCCESS) {
36 HDF_LOGE("%s: gpioId fail!", __func__);
37 return HDF_FAILURE;
38 }
39 if (drsOps->GetUint8(node, "activeLevel", &config->gpio.activeLevel, 0) != HDF_SUCCESS) {
40 HDF_LOGE("%s: activeLevel fail!", __func__);
41 return HDF_FAILURE;
42 }
43 }
44
45 return HDF_SUCCESS;
46 }
47
ParsePowersConfig(const struct DeviceResourceNode * node)48 static struct HdfPowersConfig *ParsePowersConfig(const struct DeviceResourceNode *node)
49 {
50 struct DeviceResourceIface *drsOps = NULL;
51 struct DeviceResourceNode *childNode = NULL;
52 struct HdfPowersConfig *config = NULL;
53 uint8_t nodeCount = 0;
54 int32_t ret;
55 if (node == NULL) {
56 HDF_LOGE("%s: input para is NULL!", __func__);
57 return NULL;
58 }
59 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
60 if (drsOps == NULL || drsOps->GetChildNode == NULL) {
61 HDF_LOGE("%s: at least one of the paras is NULL!", __func__);
62 return NULL;
63 }
64 DEV_RES_NODE_FOR_EACH_CHILD_NODE(node, childNode) { ++nodeCount; }
65 if (nodeCount > HDF_CHIP_MAX_POWER_SUPPORTED) {
66 return NULL;
67 }
68 config = OsalMemCalloc(sizeof(struct HdfPowersConfig) + nodeCount * sizeof(struct HdfPowerConfig));
69 if (config == NULL) {
70 return NULL;
71 }
72 config->powerCount = nodeCount;
73 for (uint8_t i = 0; i < nodeCount; i++) {
74 char buff[MAX_POWER_COUNT_LEN] = {0};
75 ret = snprintf_s(buff, MAX_POWER_COUNT_LEN, MAX_POWER_COUNT_LEN-1, "power%d", i);
76 if (ret < 0) {
77 HDF_LOGE("%s:snprintf_s failed!ret=%d, i=%d", __func__, ret, i);
78 break;
79 }
80 const struct DeviceResourceNode *powerNode = drsOps->GetChildNode(node, buff);
81 if (powerNode == NULL) {
82 HDF_LOGE("%s:Can not get node %s", __func__, buff);
83 ret = HDF_FAILURE;
84 break;
85 }
86 ret = ParsePowerConfig(powerNode, config->power + i);
87 if (ret != HDF_SUCCESS) {
88 HDF_LOGE("%s:parse node %s failed!ret=%d", __func__, buff, ret);
89 break;
90 }
91 }
92
93 if (ret != HDF_SUCCESS) {
94 OsalMemFree(config);
95 config = NULL;
96 }
97 return config;
98 }
99
ParseResetConfig(const struct DeviceResourceNode * node,struct HdfResetConfig * reset)100 static int ParseResetConfig(const struct DeviceResourceNode *node, struct HdfResetConfig *reset)
101 {
102 struct DeviceResourceIface *drsOps = NULL;
103 if (node == NULL || reset == NULL) {
104 HDF_LOGE("%s: at least one of the paras is NULL!", __func__);
105 return HDF_FAILURE;
106 }
107
108 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
109 if (drsOps == NULL || drsOps->GetUint8 == NULL) {
110 HDF_LOGE("%s: at least one of the paras is NULL!", __func__);
111 return HDF_FAILURE;
112 }
113
114 if (drsOps->GetUint8(node, "resetType", &reset->resetType, 0) != HDF_SUCCESS) {
115 HDF_LOGE("%s: powersSeqDelay fail!", __func__);
116 return HDF_FAILURE;
117 }
118 if (reset->resetType == RESET_TYPE_GPIO) {
119 if (drsOps->GetUint16(node, "gpioId", &reset->gpio.gpioId, 0) != HDF_SUCCESS) {
120 HDF_LOGE("%s: gpioId fail!", __func__);
121 return HDF_FAILURE;
122 }
123
124 if (drsOps->GetUint8(node, "activeLevel", &reset->gpio.activeLevel, 0) != HDF_SUCCESS) {
125 HDF_LOGE("%s: read activeLevel fail!", __func__);
126 return HDF_FAILURE;
127 }
128
129 if (drsOps->GetUint8(node, "resetHoldTime", &reset->resetHoldTime, 0) != HDF_SUCCESS) {
130 HDF_LOGE("%s: read resetHoldTime fail!", __func__);
131 return HDF_FAILURE;
132 }
133 }
134 return HDF_SUCCESS;
135 }
136
ClearChipConfig(struct HdfChipConfig * config)137 void ClearChipConfig(struct HdfChipConfig *config)
138 {
139 if (config->powers != NULL) {
140 OsalMemFree(config->powers);
141 config->powers = NULL;
142 }
143 }
144
ParseChipConfig(const struct DeviceResourceNode * node,struct HdfChipConfig * config)145 int32_t ParseChipConfig(const struct DeviceResourceNode *node, struct HdfChipConfig *config)
146 {
147 struct DeviceResourceIface *drsOps = NULL;
148 const struct DeviceResourceNode *devPowerNode = NULL;
149 const struct DeviceResourceNode *resetNode = NULL;
150 int32_t ret = HDF_SUCCESS;
151 if (node == NULL || config == NULL) {
152 HDF_LOGE("%s: invalid node or devLstConfig!", __func__);
153 return HDF_FAILURE;
154 }
155 drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE);
156 if (drsOps == NULL || drsOps->GetUint8 == NULL || drsOps->GetChildNode == NULL) {
157 HDF_LOGE("%s: at least one of the paras is NULL!", __func__);
158 return HDF_FAILURE;
159 }
160 config->name = node->name;
161
162 if (drsOps->GetUint8(node, "bootUpTimeOut", &config->bootUpTimeOut, 0) != HDF_SUCCESS) {
163 HDF_LOGE("%s: bootUpTimeOut fail!", __func__);
164 return HDF_FAILURE;
165 }
166
167 resetNode = drsOps->GetChildNode(node, "reset");
168 if (resetNode == NULL) {
169 HDF_LOGE("%s: GetChildNode fail!", __func__);
170 return HDF_FAILURE;
171 }
172 if (ParseResetConfig(resetNode, &config->reset) != HDF_SUCCESS) {
173 return HDF_FAILURE;
174 }
175
176 do {
177 devPowerNode = drsOps->GetChildNode(node, "powers");
178 if (devPowerNode == NULL) {
179 HDF_LOGE("%s: GetChildNode fail!", __func__);
180 ret = HDF_FAILURE;
181 break;
182 }
183 config->powers = ParsePowersConfig(devPowerNode);
184 if (config->powers == NULL) {
185 ret = HDF_FAILURE;
186 break;
187 }
188 } while (false);
189
190 if (ret != HDF_SUCCESS) {
191 ClearChipConfig(config);
192 }
193 return ret;
194 }