-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharger_algo.c
320 lines (280 loc) · 10.4 KB
/
charger_algo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright (C) 2023 Xiaomi Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/****************************************************************************
* Included Files
****************************************************************************/
#include "charger_algo.h"
#include "charger_hwintf.h"
#include "charger_manager.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BIT(n) (1U << (n))
#define BUCK_ALGO_INIT_VOL 3000
#define MAX_ALGO_NUM 5
#define VBAT_OVP_MASK BIT(0)
#define IBAT_OCP_MASK BIT(1)
#define VBUS_OVP_MASK BIT(2)
#define IBUS_OCP_MASK BIT(3)
#define IBUS_UCP_MASK BIT(4)
#define ADAPTER_INSERT_MASK BIT(5)
#define VBAT_INSERT_MASK BIT(6)
#define ADC_DONE_MASK BIT(7)
#define VBUS_ERRORLO_STAT_MASK BIT(8)
#define VBUS_ERRORHI_STAT_MASK BIT(9)
#define CP_SWITCHING_STAT_MASK BIT(10)
#define CHG_EN_STAT_MASK BIT(11)
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int buck_algo_start(struct charger_algo* algo);
static int buck_algo_update(struct charger_algo* algo, struct charger_plot_parameter* pa);
static int buck_algo_stop(struct charger_algo* algo);
static int pump_algo_start(struct charger_algo* algo);
static int pump_algo_update(struct charger_algo* algo, struct charger_plot_parameter* pa);
static int pump_algo_stop(struct charger_algo* algo);
/****************************************************************************
* Private Data
****************************************************************************/
static struct charger_algo_ops buck_algo = {
.start = buck_algo_start,
.update = buck_algo_update,
.stop = buck_algo_stop,
};
static struct charger_algo_ops pump_algo = {
.start = pump_algo_start,
.update = pump_algo_update,
.stop = pump_algo_stop,
};
static struct charger_algo_class algo_tlbs[MAX_ALGO_NUM] = {
{ "buck", &buck_algo },
{ "pump", &pump_algo },
{ NULL, NULL },
};
/****************************************************************************
* Private Functions
****************************************************************************/
static int is_pa_changed(struct charger_plot_parameter* spa, struct charger_plot_parameter* dpa)
{
return memcmp(spa, dpa, sizeof(struct charger_plot_parameter));
}
static int buck_algo_start(struct charger_algo* algo)
{
int ret;
chargerinfo("buck algo start\n");
if (is_supply_exist()) {
ret = set_supply_voltage(algo->cm, BUCK_ALGO_INIT_VOL);
if (ret < 0) {
chargererr("set supply voltage %d failed\n", BUCK_ALGO_INIT_VOL);
return CHARGER_FAILED;
}
}
ret = enable_charger(algo->cm, algo->index, true);
if (ret < 0) {
chargererr("enable charger %d failed\n", algo->index);
return CHARGER_FAILED;
}
#ifdef CONFIG_CHARGERD_SYNC_CHARGE_STATE
set_battery_charge_state(algo->cm, BATTERY_CHARGING);
#endif
memset(&algo->sp, 0, sizeof(struct charger_plot_parameter));
return CHARGER_OK;
}
static int buck_algo_update(struct charger_algo* algo, struct charger_plot_parameter* pa)
{
int ret = CHARGER_OK;
#ifdef CONFIG_CHARGERD_SYNC_CHARGE_STATE
unsigned int state = BATTERY_CHARGING;
get_charger_state(algo->cm, pa->charger_index, &state);
set_battery_charge_state(algo->cm, state);
#endif
if (pa && is_pa_changed(&algo->sp, pa)) {
chargerinfo("buck_algo_update t_min:%d t_max:%d v_min:%d v_max:%d index:%d"
"current:%d supply_vol:%d\n",
pa->temp_range_min, pa->temp_range_max,
pa->vol_range_min, pa->vol_range_max, pa->charger_index,
pa->work_current, pa->supply_vol);
ret = set_charger_current(algo->cm, pa->charger_index, pa->work_current);
if (ret < 0) {
chargererr("enable charger %d failed\n", algo->index);
return CHARGER_FAILED;
}
if (pa->supply_vol > 0) {
if (is_supply_exist()) {
ret = set_supply_voltage(algo->cm, pa->supply_vol);
if (ret < 0) {
chargererr("set supply %d failed\n", pa->supply_vol);
return CHARGER_FAILED;
}
}
}
memcpy(&algo->sp, pa, sizeof(struct charger_plot_parameter));
}
return CHARGER_OK;
}
static int buck_algo_stop(struct charger_algo* algo)
{
struct charger_manager* manager = algo->cm;
int ret;
chargerinfo("buck algo stop\n");
ret = enable_charger(manager, algo->index, false);
if (ret < 0) {
chargererr("disable charger %d failed\n", algo->index);
return CHARGER_FAILED;
}
if (is_supply_exist()) {
ret = set_supply_voltage(manager, BUCK_ALGO_INIT_VOL);
if (ret < 0) {
chargererr("set supply %d failed\n", BUCK_ALGO_INIT_VOL);
return CHARGER_FAILED;
}
}
return CHARGER_OK;
}
static int pump_algo_start(struct charger_algo* algo)
{
int voltage = 0;
int current = 0;
int vbase = 0;
int rx_vout = 0;
unsigned int errstate = 0;
unsigned int enstate = 0;
int psvc = 0;
int ret = 0;
chargerinfo("pump algo start\n");
ret = get_battery_voltage(algo->cm, &voltage);
ret |= get_battery_current(algo->cm, ¤t);
if (ret < 0) {
chargererr("get battery info failed\n");
return CHARGER_FAILED;
}
vbase = voltage - current * 0.25;
do {
rx_vout = vbase * 1.91 + PUMP_CONF_VOUT_OFFSET + (PUMP_CONF_STARTUP_VOLTAGE + PUMP_CONF_STARTUP_VOLTAGE_OFFSET * psvc);
if (rx_vout > PUMP_CONF_VOUT_MAX) {
chargererr("rx_vout = %d over %d\n", rx_vout, PUMP_CONF_VOUT_MAX);
return CHARGER_FAILED;
}
ret = set_supply_voltage(algo->cm, rx_vout);
usleep(1000 * 100);
ret |= get_charger_state(algo->cm, algo->index, &errstate);
if (ret < 0) {
chargererr("get charger error state failed\n");
return CHARGER_FAILED;
}
psvc++;
} while (errstate & (VBUS_ERRORLO_STAT_MASK | VBUS_ERRORHI_STAT_MASK));
ret = enable_charger(algo->cm, algo->index, true);
usleep(1000 * 500);
ret |= get_charger_state(algo->cm, algo->index, &enstate);
enstate = enstate & CHG_EN_STAT_MASK;
if (ret < 0 || !enstate) {
enable_charger(algo->cm, algo->index, false);
return CHARGER_FAILED;
}
#ifdef CONFIG_CHARGERD_SYNC_CHARGE_STATE
set_battery_charge_state(algo->cm, BATTERY_CHARGING);
#endif
memset(&algo->sp, 0, sizeof(struct charger_plot_parameter));
return CHARGER_OK;
}
static int pump_algo_update(struct charger_algo* algo, struct charger_plot_parameter* pa)
{
unsigned int state = 0;
unsigned int ovp;
unsigned int enstate;
int current = 0;
int vol = 0;
int ret = 0;
if (pa) {
ret = get_charger_state(algo->cm, algo->index, &state);
enstate = state & CHG_EN_STAT_MASK;
ovp = state & (VBAT_OVP_MASK | VBUS_OVP_MASK);
if (ret < 0 || !enstate || ovp) {
return CHARGER_FAILED;
}
if (is_pa_changed(&algo->sp, pa)) {
chargerinfo("pump_algo_update t_min:%d t_max:%d v_min:%d v_max:%d index:%d"
"current:%d supply_vol:%d\n",
pa->temp_range_min, pa->temp_range_max,
pa->vol_range_min, pa->vol_range_max, pa->charger_index,
pa->work_current, pa->supply_vol);
memcpy(&algo->sp, pa, sizeof(struct charger_plot_parameter));
return set_charger_current(algo->cm, algo->index, pa->work_current);
} else {
ret = get_battery_current(algo->cm, ¤t);
chargerassert_return(ret < 0, "pump_algo_update get battery current failed\n");
}
if (current < (pa->work_current - PUMP_CONF_COUT_STEP_DEC)) {
if (get_supply_voltage(algo->cm, &vol) < 0) {
return CHARGER_FAILED;
}
vol += PUMP_CONF_VOUT_STEP_INC;
if (vol > PUMP_CONF_VOUT_MAX) {
vol = PUMP_CONF_VOUT_MAX;
}
if (set_supply_voltage(algo->cm, vol) < 0) {
return CHARGER_FAILED;
}
}
if (current > (pa->work_current + PUMP_CONF_COUT_STEP_INC)) {
if (get_supply_voltage(algo->cm, &vol) < 0) {
return CHARGER_FAILED;
}
vol -= PUMP_CONF_VOUT_STEP_DEC;
if (set_supply_voltage(algo->cm, vol) < 0) {
return CHARGER_FAILED;
}
}
}
return CHARGER_OK;
}
static int pump_algo_stop(struct charger_algo* algo)
{
int ret;
chargerinfo("pump algo stop\n");
ret = enable_charger(algo->cm, algo->index, false);
if (ret < 0) {
chargererr("disable charger %d failed\n", algo->index);
return CHARGER_FAILED;
}
if (is_supply_exist()) {
ret = set_supply_voltage(algo->cm, PUMP_CONF_STARTUP_VOLTAGE);
if (ret < 0) {
chargererr("set supply %d failed\n", PUMP_CONF_STARTUP_VOLTAGE);
return CHARGER_FAILED;
}
}
return CHARGER_OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
struct charger_algo_ops* get_algo_ops(char* id)
{
int i = 0;
struct charger_algo_class* algo = NULL;
for (i = 0; i < MAX_ALGO_NUM; i++) {
algo = &algo_tlbs[i];
if (NULL != algo->algo_id && 0 == strcmp(id, algo->algo_id)) {
return algo->ops;
}
}
return NULL;
}