summaryrefslogtreecommitdiff
path: root/common/pmu_tps65090_charger.c
blob: 28bcfafe0bccb0263ae8da64c28c32a0265f2466 (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
/* 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.
 *
 * TI TPS65090 PMU charging task.
 */

#include "board.h"
#include "chipset.h"
#include "console.h"
#include "gpio.h"
#include "pmu_tpschrome.h"
#include "smart_battery.h"
#include "system.h"
#include "task.h"
#include "timer.h"
#include "util.h"

#define CPUTS(outstr) cputs(CC_CHARGER, outstr)
#define CPRINTF(format, args...) cprintf(CC_CHARGER, format, ## args)

/* Charging and discharging alarms */
#define ALARM_DISCHARGING (ALARM_TERMINATE_DISCHARGE | ALARM_OVER_TEMP)
#define ALARM_CHARGING (ALARM_TERMINATE_CHARGE | \
		ALARM_OVER_CHARGED | \
		ALARM_OVER_TEMP)


/* Time delay in usec for idle, charging and discharging.
 * Defined in battery charging flow.
 */
#define T1_USEC 5000000
#define T2_USEC 10000000
#define T3_USEC 10000000

/* Non-SBS charging states */
enum charging_state {
	ST_NONE = 0,
	ST_IDLE,
	ST_PRE_CHARGING,
	ST_CHARGING,
	ST_DISCHARGING,
};

static const char * const state_list[] = {
	"none",
	"idle",
	"pre-charging",
	"charging",
	"discharging"
};

static void enable_charging(int enable)
{
	enable = enable ? 1 : 0;
	if (gpio_get_level(GPIO_CHARGER_EN) != enable)
		gpio_set_level(GPIO_CHARGER_EN, enable);

	/* With NOITERM bit set, charger can be controlled by gpio.
	 * Hence following charger enable command can be removed.
	 *
	 * pmu_enable_charger(enable);
	 */
}


/**
 * An ES1 workaround to get AC state
 * The dev boards without rework can not get AC state directly from gpio.
 * And the tps65090 VACG/VBATG does not reflect AC and battery state correctly.
 * This workaround uses tps65090 irq event and battery i2c to get correct AC
 * state.
 */
static int get_ac(void)
{
	int rv;
	int ac, batt, alarm;

	rv = pmu_get_power_source(&ac, &batt);
	if (rv)
		return 0;

	if (ac && batt)
		return 1;

	if (ac || batt) {
		enable_charging(1);
		usleep(10000);
		enable_charging(0);
	}


	if (battery_status(&alarm) == 0)
		return 0;

	return 1;
}

/* TODO: move battery vendor specific functions to battery pack module */
static int battery_temperature_celsius(int t)
{
	return (t - 2731) / 10;
}

static int battery_start_charging_range(int t)
{
	t = battery_temperature_celsius(t);
	return (t > 5 && t < 40);
}

static int battery_charging_range(int t)
{
	t = battery_temperature_celsius(t);
	return (t > 5 && t < 60);
}

static int battery_discharging_range(int t)
{
	t = battery_temperature_celsius(t);
	return (t < 70);
}

static int wait_t1_idle(void)
{
	usleep(T1_USEC);
	return ST_IDLE;
}

static int wait_t2_charging(void)
{
	usleep(T2_USEC);
	return ST_CHARGING;
}

static int wait_t3_discharging(void)
{
	usleep(T3_USEC);
	return ST_DISCHARGING;
}

static int system_off(void)
{
	if (chipset_in_state(CHIPSET_STATE_ON)) {
		CPUTS("[pmu] turn system off\n");
		chipset_exit_hard_off();

		/* TODO: After have impl in chipset_exit_hard_off(),
		 * remove these gpio hack
		 */
		gpio_set_level(GPIO_EN_PP3300, 0);
		gpio_set_level(GPIO_EN_PP1350, 0);
		gpio_set_level(GPIO_PMIC_PWRON_L, 1);
		gpio_set_level(GPIO_EN_PP5000, 0);
	}

	return ST_IDLE;
}

static int notify_battery_low(void)
{
	static timestamp_t last_notify_time;
	timestamp_t now;

	if (chipset_in_state(CHIPSET_STATE_ON)) {
		now = get_time();
		if (now.val - last_notify_time.val > 60000000) {
			CPUTS("[pmu] notify battery low (< 10%)\n");
			last_notify_time = now;
			/* TODO: notify AP */
		}
	}
	return ST_DISCHARGING;
}

static int calc_next_state(int state)
{
	int batt_temp, alarm, capacity, charge;

	switch (state) {
	case ST_IDLE:

		/* Check AC and chiset state */
		if (!get_ac()) {
			if (chipset_in_state(CHIPSET_STATE_ON))
				return ST_DISCHARGING;

			/* Enable charging and wait ac on */
			enable_charging(1);
			return wait_t1_idle();
		}

		/* Enable charging when battery doesn't respond */
		if (battery_temperature(&batt_temp)) {
			enable_charging(1);
			wait_t1_idle();
			return ST_PRE_CHARGING;
		}

		/* Turn off charger when battery temperature is out
		 * of the start charging range.
		 */
		if (!battery_start_charging_range(batt_temp)) {
			enable_charging(0);
			return wait_t1_idle();
		}

		/* Turn off charger on battery charging alarm */
		if (battery_status(&alarm) || (alarm & ALARM_CHARGING)) {
			if (!(alarm & ALARM_TERMINATE_CHARGE))
				CPRINTF("[pmu] idle %016b\n", alarm);
			enable_charging(0);
			return wait_t1_idle();
		}

		/* Start charging only when battery charge lower than 100% */
		if (!battery_state_of_charge(&charge) && charge < 100) {
			enable_charging(1);
			wait_t1_idle();
			return ST_CHARGING;
		}

		return wait_t1_idle();

	case ST_PRE_CHARGING:
		if (!get_ac())
			return wait_t1_idle();

		/* If the battery goes online after enable the charger,
		 * go into charging state.
		 */
		if (battery_temperature(&batt_temp) == EC_SUCCESS)
			return ST_CHARGING;

		wait_t1_idle();
		return ST_PRE_CHARGING;

	case ST_CHARGING:
		if (!get_ac())
			break;
		if (battery_temperature(&batt_temp) ||
				!battery_charging_range(batt_temp)) {
			CPUTS("[pmu] charging: battery hot\n");
			break;
		}
		if (battery_status(&alarm) || (alarm & ALARM_CHARGING)) {
			CPUTS("[pmu] charging: battery alarm\n");
			break;
		}
		if (pmu_is_charger_alarm()) {
			CPUTS("[pmu] charging: charger alarm\n");
			break;
		}
		return wait_t2_charging();

	case ST_DISCHARGING:

		if (get_ac())
			return wait_t1_idle();

		/* Check battery discharging temperature range */
		if (battery_temperature(&batt_temp) == 0) {
			if (!battery_discharging_range(batt_temp)) {
				CPUTS("[pmu] discharging: battery hot\n");
				return system_off();
			}
		}
		/* Check discharging alarm */
		if (!battery_status(&alarm) && (alarm & ALARM_DISCHARGING)) {
			CPRINTF("[pmu] discharging: battery alarm %016b\n",
					alarm);
			return system_off();
		}
		/* Check remaining charge % */
		if (battery_state_of_charge(&capacity) == 0 && capacity < 10)
			return notify_battery_low();

		return wait_t3_discharging();
	}

	return wait_t1_idle();
}

void pmu_charger_task(void)
{
	int state = ST_IDLE;
	int next_state;

	pmu_init();

	while (1) {
		next_state = calc_next_state(state);
		if (next_state != state) {
			CPRINTF("[batt] state %s -> %s\n",
				state_list[state],
				state_list[next_state]);
			state = next_state;
		}
	}
}