summaryrefslogtreecommitdiff
path: root/board/scarlet/battery.c
blob: 0be4cc93e2d24666e06bce4988d922c65de76cd6 (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
/* Copyright 2016 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 pack vendor provided charging profile
 */

#include "battery.h"
#include "battery_smart.h"
#include "charge_state.h"
#include "chipset.h"
#include "console.h"
#include "driver/battery/max17055.h"
#include "driver/charger/rt946x.h"
#include "ec_commands.h"
#include "extpower.h"
#include "gpio.h"
#include "hooks.h"
#include "system.h"
#include "util.h"

/*
 * AE-Tech battery pack has two charging phases when operating
 * between 10 and 20C
 */
#define CHARGE_PHASE_CHANGE_TRIP_VOLTAGE_MV 4200
#define CHARGE_PHASE_CHANGE_HYSTERESIS_MV 50
#define CHARGE_PHASE_CHANGED_CURRENT_MA 1800

#define TEMP_OUT_OF_RANGE TEMP_ZONE_COUNT

static uint8_t batt_id = 0xff;

/* Do not change the enum values. We directly use strap gpio level to index. */
enum battery_type {
	BATTERY_SIMPLO = 0,
	BATTERY_AETECH,
	BATTERY_COUNT
};

static const struct battery_info info[] = {
	[BATTERY_SIMPLO] = {
		.voltage_max		= 4400,
		.voltage_normal		= 3840,
		.voltage_min		= 3000,
		.precharge_current	= 256,
		.start_charging_min_c	= 0,
		.start_charging_max_c	= 45,
		.charging_min_c		= 0,
		.charging_max_c		= 60,
		.discharging_min_c	= -20,
		.discharging_max_c	= 60,
	},
	[BATTERY_AETECH] = {
		.voltage_max		= 4350,
		.voltage_normal		= 3800,
		.voltage_min		= 3000,
		.precharge_current	= 700,
		.start_charging_min_c	= 0,
		.start_charging_max_c	= 45,
		.charging_min_c		= 0,
		.charging_max_c		= 45,
		.discharging_min_c	= -20,
		.discharging_max_c	= 55,
	}
};

static const struct max17055_batt_profile batt_profile[] = {
	[BATTERY_SIMPLO] = {
		.is_ez_config		= 0,
		.design_cap		= 0x221e, /* 8734mAh */
		.ichg_term		= 0x589, /* 443 mA */
		/* Empty voltage = 3000mV, Recovery voltage = 3600mV */
		.v_empty_detect		= 0x965a,
		.learn_cfg		= 0x4406,
		.dpacc			= 0x0c7a,
		.rcomp0			= 0x0062,
		.tempco			= 0x1327,
		.qr_table00		= 0x1680,
		.qr_table10		= 0x0900,
		.qr_table20		= 0x0280,
		.qr_table30		= 0x0280,
	},
	[BATTERY_AETECH] = {
		.is_ez_config		= 0,
		.design_cap		= 0x232f, /* 9007mAh */
		.ichg_term		= 0x0240, /* 180mA */
		/* Empty voltage = 2700mV, Recovery voltage = 3280mV */
		.v_empty_detect		= 0x8752,
		.learn_cfg		= 0x4476,
		.dpacc			= 0x0c7b,
		.rcomp0			= 0x0077,
		.tempco			= 0x1d3f,
		.qr_table00		= 0x1200,
		.qr_table10		= 0x0900,
		.qr_table20		= 0x0480,
		.qr_table30		= 0x0480,
	},
};

const struct battery_info *battery_get_info(void)
{
	if (batt_id >= BATTERY_COUNT)
		batt_id = gpio_get_level(GPIO_BATT_ID);

	return &info[batt_id];
}

const struct max17055_batt_profile *max17055_get_batt_profile(void)
{
	if (batt_id >= BATTERY_COUNT)
		batt_id = gpio_get_level(GPIO_BATT_ID);

	return &batt_profile[batt_id];
}

int board_cut_off_battery(void)
{
	return rt946x_cutoff_battery();
}

enum battery_disconnect_state battery_get_disconnect_state(void)
{
	if (battery_is_present() == BP_YES)
		return BATTERY_NOT_DISCONNECTED;
	return BATTERY_DISCONNECTED;
}

int charger_profile_override(struct charge_state_data *curr)
{
	/* battery temp in 0.1 deg C */
	int bat_temp_c = curr->batt.temperature - 2731;

	/*
	 * Keep track of battery temperature range:
	 *
	 *        ZONE_0   ZONE_1     ZONE_2
	 * -----+--------+--------+------------+----- Temperature (C)
	 *      t0       t1       t2           t3
	 */
	enum {
		TEMP_ZONE_0, /* t0 < bat_temp_c <= t1 */
		TEMP_ZONE_1, /* t1 < bat_temp_c <= t2 */
		TEMP_ZONE_2, /* t2 < bat_temp_c <= t3 */
		TEMP_ZONE_COUNT
	} temp_zone;

	static struct {
		int temp_min; /* 0.1 deg C */
		int temp_max; /* 0.1 deg C */
		int desired_current; /* mA */
		int desired_voltage; /* mV */
	} temp_zones[BATTERY_COUNT][TEMP_ZONE_COUNT] = {
		[BATTERY_SIMPLO] = {
			{0, 150, 1772, 4376}, /* TEMP_ZONE_0 */
			{150, 450, 4000, 4376}, /* TEMP_ZONE_1 */
			{450, 600, 4000, 4100}, /* TEMP_ZONE_2 */
		},
		[BATTERY_AETECH] = {
			{0, 100, 900, 4200}, /* TEMP_ZONE_0 */
			{100, 200, 2700, 4350}, /* TEMP_ZONE_1 */
			/*
			 * TODO(b:70287349): Limit the charging current to
			 * 2A unless AE-Tech fix their battery pack.
			 */
			{200, 450, 2000, 4350}, /* TEMP_ZONE_2 */
		}
	};
	BUILD_ASSERT(ARRAY_SIZE(temp_zones[0]) == TEMP_ZONE_COUNT);
	BUILD_ASSERT(ARRAY_SIZE(temp_zones) == BATTERY_COUNT);

	static int charge_phase = 1;
	static uint8_t quirk_batt_update;

	/*
	 * This is a quirk for old Simplo battery to clamp
	 * charging current to 3A.
	 */
	if ((board_get_version() <= 4) && !quirk_batt_update) {
		temp_zones[BATTERY_SIMPLO][TEMP_ZONE_1].desired_current = 3000;
		temp_zones[BATTERY_SIMPLO][TEMP_ZONE_2].desired_current = 3000;
		quirk_batt_update = 1;
	}

	if (batt_id >= BATTERY_COUNT)
		batt_id = gpio_get_level(GPIO_BATT_ID);

	if ((curr->batt.flags & BATT_FLAG_BAD_TEMPERATURE) ||
	    (bat_temp_c < temp_zones[batt_id][0].temp_min) ||
	    (bat_temp_c >= temp_zones[batt_id][TEMP_ZONE_COUNT - 1].temp_max))
		temp_zone = TEMP_OUT_OF_RANGE;
	else {
		for (temp_zone = 0; temp_zone < TEMP_ZONE_COUNT; temp_zone++) {
			if (bat_temp_c <
				temp_zones[batt_id][temp_zone].temp_max)
				break;
		}
	}

	if (curr->state != ST_CHARGE) {
		charge_phase = 1;
		return 0;
	}

	switch (temp_zone) {
	case TEMP_ZONE_0:
	case TEMP_ZONE_2:
		curr->requested_current =
			temp_zones[batt_id][temp_zone].desired_current;
		curr->requested_voltage =
			temp_zones[batt_id][temp_zone].desired_voltage;
		break;
	case TEMP_ZONE_1:
		/* No phase change for Simplo battery pack */
		if (batt_id == BATTERY_SIMPLO)
			charge_phase = 0;
		/*
		 * If AE-Tech battery pack is used and the voltage reading
		 * is bad, let's be conservative and assume change_phase == 1.
		 */
		else if (curr->batt.flags & BATT_FLAG_BAD_VOLTAGE)
			charge_phase = 1;
		else {
			if (curr->batt.voltage <
			    (CHARGE_PHASE_CHANGE_TRIP_VOLTAGE_MV -
			     CHARGE_PHASE_CHANGE_HYSTERESIS_MV))
				charge_phase = 0;
			else if (curr->batt.voltage >
				 CHARGE_PHASE_CHANGE_TRIP_VOLTAGE_MV)
				charge_phase = 1;
		}

		curr->requested_voltage =
			temp_zones[batt_id][temp_zone].desired_voltage;

		curr->requested_current = (charge_phase) ?
			CHARGE_PHASE_CHANGED_CURRENT_MA :
			temp_zones[batt_id][temp_zone].desired_current;
		break;
	case TEMP_OUT_OF_RANGE:
		curr->requested_current = curr->requested_voltage = 0;
		curr->batt.flags &= ~BATT_FLAG_WANT_CHARGE;
		curr->state = ST_IDLE;
		break;
	}

	/*
	 * When the charger says it's done charging, even if fuel gauge says
	 * SOC < BATTERY_LEVEL_NEAR_FULL, we'll overwrite SOC with
	 * BATTERY_LEVEL_NEAR_FULL. So we can ensure both Chrome OS UI
	 * and battery LED indicate full charge.
	 */
	if (rt946x_is_charge_done()) {
		curr->batt.state_of_charge = MAX(BATTERY_LEVEL_NEAR_FULL,
						 curr->batt.state_of_charge);
		/*
		 * This is a workaround for b:78792296. When AP is off and
		 * charge termination is detected, we disable idle mode.
		 */
		if (chipset_in_state(CHIPSET_STATE_ANY_OFF))
			disable_idle();
		else
			enable_idle();
	}

	return 0;
}

static void board_enable_idle(void)
{
	enable_idle();
}
DECLARE_HOOK(HOOK_AC_CHANGE, board_enable_idle, HOOK_PRIO_DEFAULT);

static void board_charge_termination(void)
{
	static uint8_t te;
	/* Enable charge termination when we are sure battery is present. */
	if (!te && battery_is_present() == BP_YES) {
		if (!rt946x_enable_charge_termination(1))
			te = 1;
	}
}
DECLARE_HOOK(HOOK_BATTERY_SOC_CHANGE,
	     board_charge_termination,
	     HOOK_PRIO_DEFAULT);

/* Customs options controllable by host command. */
#define PARAM_FASTCHARGE (CS_PARAM_CUSTOM_PROFILE_MIN + 0)

enum ec_status charger_profile_override_get_param(uint32_t param,
						  uint32_t *value)
{
	return EC_RES_INVALID_PARAM;
}

enum ec_status charger_profile_override_set_param(uint32_t param,
						  uint32_t value)
{
	return EC_RES_INVALID_PARAM;
}