summaryrefslogtreecommitdiff
path: root/common/charge_state.c
blob: 088f43669fe92d6d8d41166f8e8dda9d275ffdc2 (plain)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Battery charging task and state machine.
 */


#include "battery_pack.h"
#include "board.h"
#include "console.h"
#include "charger.h"
#include "gpio.h"
#include "lpc.h"
#include "lpc_commands.h"
#include "smart_battery.h"
#include "timer.h"
#include "uart.h"
#include "util.h"

/* Stop charge when state of charge reaches this percentage */
#define STOP_CHARGE_THRESHOLD 100
/* Critical level of when discharging reaches this percentage */
#define BATT_CRITICAL_LEVEL   10

/* power state task polling period in usec */
#define POLL_PERIOD_LONG        500000
#define POLL_PERIOD_CHARGE      250000
#define POLL_PERIOD_SHORT       100000
#define MIN_SLEEP_USEC          50000

/* Power states */
enum power_state {
	PWR_STATE_UNCHANGE = 0,
	PWR_STATE_INIT,
	PWR_STATE_IDLE,
	PWR_STATE_DISCHARGE,
	PWR_STATE_CHARGE,
	PWR_STATE_ERROR
};
/* Debugging constants, in the same order as power_state.
 * This state name table and debug print will be removed
 * before production.
 */
const static char *_state_name[] = {
	"null",
	"init",
	"idle",
	"discharge",
	"charge",
	"error"
};


/* helper function(s) */
static inline int get_ac(void)
{
	return gpio_get_level(GPIO_AC_PRESENT);
}

/* Get battery charging parameters from battery and
 * battery pack vendor table
 */
static int battery_params(struct batt_params *batt)
{
	int rv, d;
	/* Direct mem mapped EC data */
	uint32_t *memmap_batt_volt  = (uint32_t*)(lpc_get_memmap_range() +
					EC_LPC_MEMMAP_BATT_VOLT);
	uint32_t *memmap_batt_rate  = (uint32_t*)(lpc_get_memmap_range() +
					EC_LPC_MEMMAP_BATT_RATE);
	uint32_t *memmap_batt_cap   = (uint32_t*)(lpc_get_memmap_range() +
					EC_LPC_MEMMAP_BATT_CAP);

	rv = battery_temperature(&batt->temperature);
	if (rv)
		return rv;

	rv = battery_voltage(&batt->voltage);
	if (rv)
		return rv;
	*memmap_batt_volt = batt->voltage;

	rv = battery_current(&batt->current);
	if (rv)
		return rv;
	*memmap_batt_rate = batt->current < 0 ? -batt->current : 0;

	rv = battery_desired_voltage(&batt->desired_voltage);
	if (rv)
		return rv;

	rv = battery_desired_current(&batt->desired_current);
	if (rv)
		return rv;

	battery_vendor_params(batt);

	rv = battery_get_battery_mode(&d);
	if (rv)
		return rv;

	if (d & MODE_CAPACITY) {
		/* Battery capacity mode was set to mW, set it back to mAh */
		d &= ~MODE_CAPACITY;
		rv = battery_set_battery_mode(d);
		if (rv)
			return rv;
	}
	rv = battery_remaining_capacity(&d);
	if (rv)
		return rv;
	*memmap_batt_cap = d;

	return EC_SUCCESS;
}

/* Init state handler
 *	- check ac, charger, battery and temperature
 *	- initialize charger
 *	- new states: DISCHARGE, IDLE
 */
static enum power_state state_init(void)
{
	int rv, val;

	/* Stop charger, unconditionally */
	charger_set_current(0);
	charger_set_voltage(0);

	/* Detect AC, init charger */
	if (!get_ac())
		return PWR_STATE_DISCHARGE;

	/* Initialize charger to power on reset mode */
	rv = charger_post_init();
	if (rv)
		return PWR_STATE_ERROR;
	/* Check if charger is online */
	rv = charger_get_status(&val);
	if (rv)
		return PWR_STATE_ERROR;

	/* Detect battery */
	if (battery_temperature(&val))
		return PWR_STATE_ERROR;

	return PWR_STATE_IDLE;
}

/* Idle state handler
 *	- both charger and battery are online
 *	- detect charger and battery status change
 *	- new states: CHARGE, INIT
 */
static enum power_state state_idle(void)
{
	int voltage, current, state_of_charge;
	struct batt_params batt;

	if (!get_ac())
		return PWR_STATE_INIT;

	/* Prevent charging in idle mode */
	if (charger_get_voltage(&voltage))
		return PWR_STATE_ERROR;
	if (charger_get_current(&current))
		return PWR_STATE_ERROR;
	if (voltage || current)
		return PWR_STATE_INIT;

	if (battery_state_of_charge(&state_of_charge))
		return PWR_STATE_ERROR;

	if (state_of_charge >= STOP_CHARGE_THRESHOLD)
		return PWR_STATE_UNCHANGE;

	/* Check if the batter is good to charge */
	if (battery_params(&batt))
		return PWR_STATE_ERROR;

	/* Configure init charger state and switch to charge state */
	if (batt.desired_voltage && batt.desired_current) {
		if (charger_set_voltage(batt.desired_voltage))
			return PWR_STATE_ERROR;
		if (charger_set_current(batt.desired_current))
			return PWR_STATE_ERROR;
		return PWR_STATE_CHARGE;
	}

	return PWR_STATE_UNCHANGE;
}

/* Charge state handler
 *	- detect battery status change
 *	- new state: INIT
 */
static enum power_state state_charge(void)
{
	int chg_voltage, chg_current;
	struct batt_params batt;

	if (!get_ac())
		return PWR_STATE_INIT;

	if (charger_get_voltage(&chg_voltage))
		return PWR_STATE_ERROR;

	if (charger_get_current(&chg_current))
		return PWR_STATE_ERROR;

	/* Check charger reset */
	if (chg_voltage == 0 || chg_current == 0)
		return PWR_STATE_INIT;

	if (battery_params(&batt))
		return PWR_STATE_ERROR;

	if (batt.desired_voltage != chg_voltage)
		if (charger_set_voltage(batt.desired_voltage))
			return PWR_STATE_ERROR;
	if (batt.desired_current != chg_current)
		if (charger_set_current(batt.desired_current))
			return PWR_STATE_ERROR;

	if (battery_state_of_charge(&batt.state_of_charge))
		return PWR_STATE_ERROR;

	if (batt.state_of_charge >= STOP_CHARGE_THRESHOLD) {
		if (charger_set_voltage(0) || charger_set_current(0))
			return PWR_STATE_ERROR;
		return PWR_STATE_IDLE;
	}

	return PWR_STATE_UNCHANGE;
}

/* Discharge state handler
 *	- detect ac status
 *	- new state: INIT
 */
static enum power_state state_discharge(void)
{
	struct batt_params batt;
	uint8_t *memmap_batt_flags = (uint8_t*)(lpc_get_memmap_range() +
					EC_LPC_MEMMAP_BATT_FLAG);
	if (get_ac())
		return PWR_STATE_INIT;

	if (battery_params(&batt))
		return PWR_STATE_ERROR;

	if (batt.state_of_charge <= BATT_CRITICAL_LEVEL)
		*memmap_batt_flags |= EC_BATT_FLAG_LEVEL_CRITICAL;

	/* TODO: handle overtemp in discharge mode */

	return PWR_STATE_UNCHANGE;
}

/* Error state handler
 *	- check charger and battery communication
 *	- log error
 *	- new state: INIT
 */
static enum power_state state_error(void)
{
	enum { F_CHG_V, F_CHG_I, F_BAT_V, F_BAT_I,
		F_DES_V, F_DES_I, F_BAT_T, F_LAST };
	static int last_error_flags;
	int error_flags = 0;
	int ac = 0;
	int bat_v = -1, bat_i = -1, bat_temp = -1;
	int desired_v = -1, desired_i = -1;
	uint8_t batt_flags = 0;
	uint8_t *memmap_batt_flags = (uint8_t*)(lpc_get_memmap_range() +
					EC_LPC_MEMMAP_BATT_FLAG);

	ac = get_ac();
	if (ac) {
		batt_flags = EC_BATT_FLAG_AC_PRESENT;
		if (charger_set_voltage(0))
			error_flags |= (1 << F_CHG_V);
		if (charger_set_current(0))
			error_flags |= (1 << F_CHG_I);
	} else
		batt_flags = EC_BATT_FLAG_DISCHARGING;

	if (battery_voltage(&bat_v))
		error_flags |= (1 << F_BAT_V);
	if (battery_current(&bat_i))
		error_flags |= (1 << F_BAT_I);
	if (battery_temperature(&bat_temp))
		error_flags |= (1 << F_BAT_T);
	if (battery_desired_voltage(&desired_v))
		error_flags |= (1 << F_DES_V);
	if (battery_desired_current(&desired_i))
		error_flags |= (1 << F_DES_I);

	if (error_flags == 0) {
		last_error_flags = 0;
		return PWR_STATE_INIT;
	}

	/* Check if all battery operation returned success */
	if (!(error_flags & (F_BAT_V | F_BAT_I | F_DES_V | F_DES_I | F_BAT_T)))
		batt_flags |= EC_BATT_FLAG_BATT_PRESENT;

	/* Debug output */
	if (error_flags != last_error_flags) {
		uart_printf("[Charge error flags %02x -> %02x; AC=%d",
			    last_error_flags, error_flags, ac);

		if (error_flags & (F_CHG_V | F_CHG_I))
			uart_puts(", charger error");

		uart_printf(", battery V=%d I=%d T=%d Vwant=%d Iwant=%d]\n",
			    bat_v, bat_i, (bat_temp - 2731) / 10,
			    desired_v, desired_i);

		last_error_flags = error_flags;
	}

	*memmap_batt_flags = batt_flags;

	return PWR_STATE_UNCHANGE;
}

static void charging_progress(void)
{
	static int state_of_charge;
	int d;

	if (battery_state_of_charge(&d))
		return;

	if (d != state_of_charge) {
		state_of_charge = d;
		if (get_ac())
			battery_time_to_full(&d);
		else
			battery_time_to_empty(&d);

		uart_printf("[Battery %3d%% / %dh:%d]\n", state_of_charge,
			d / 60, d % 60);
	}

}

/* Battery charging task */
void charge_state_machine_task(void)
{
	timestamp_t prev_ts, ts;
	int sleep_usec, diff_usec;
	enum power_state current_state, new_state;
	uint8_t *memmap_batt_flags = (uint8_t*)(lpc_get_memmap_range() +
					EC_LPC_MEMMAP_BATT_FLAG);
	uint8_t batt_flags;

	prev_ts.val = 0;
	current_state = PWR_STATE_INIT;

	while (1) {
		ts = get_time();

		switch (current_state) {
		case PWR_STATE_INIT:
			new_state = state_init();
			break;
		case PWR_STATE_IDLE:
			new_state = state_idle();
			break;
		case PWR_STATE_DISCHARGE:
			new_state = state_discharge();
			break;
		case PWR_STATE_CHARGE:
			new_state = state_charge();
			break;
		case PWR_STATE_ERROR:
			new_state = state_error();
			break;
		default:
			new_state = PWR_STATE_ERROR;
		}

		if (new_state)
			uart_printf("[Charge state %s -> %s]\n",
				_state_name[current_state],
				_state_name[new_state]);

		switch (new_state) {
		case PWR_STATE_IDLE:
			*memmap_batt_flags = EC_BATT_FLAG_AC_PRESENT |
				EC_BATT_FLAG_BATT_PRESENT;
			sleep_usec = POLL_PERIOD_LONG;
			break;
		case PWR_STATE_DISCHARGE:
			batt_flags = *memmap_batt_flags;
			*memmap_batt_flags = EC_BATT_FLAG_AC_PRESENT |
				EC_BATT_FLAG_BATT_PRESENT |
				EC_BATT_FLAG_DISCHARGING |
				(batt_flags & EC_BATT_FLAG_LEVEL_CRITICAL);
			sleep_usec = POLL_PERIOD_LONG;
			break;
		case PWR_STATE_CHARGE:
			*memmap_batt_flags = EC_BATT_FLAG_AC_PRESENT |
				EC_BATT_FLAG_BATT_PRESENT |
				EC_BATT_FLAG_CHARGING;
			sleep_usec = POLL_PERIOD_CHARGE;
			break;
		case PWR_STATE_ERROR:
			sleep_usec = POLL_PERIOD_SHORT;
			break;
		default:
			sleep_usec = POLL_PERIOD_SHORT;
		}

		diff_usec = (int)(ts.val - prev_ts.val);
		sleep_usec -= diff_usec;
		if (sleep_usec < MIN_SLEEP_USEC)
			sleep_usec = MIN_SLEEP_USEC;

		prev_ts = ts;
		usleep(sleep_usec);

		if (new_state)
			current_state = new_state;

		charging_progress();
	}
}