summaryrefslogtreecommitdiff
path: root/board/samus/extpower.c
blob: 51cf5454e858bc00c471227541bbde96e5987abf (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
/* Copyright (c) 2013 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.
 */

/*
 * Pure GPIO-based external power detection, buffered to PCH.
 * Drive high in S5-S0 when AC_PRESENT is high, otherwise drive low.
 */

#include "bq24773.h"
#include "charge_state.h"
#include "charger.h"
#include "chipset.h"
#include "common.h"
#include "console.h"
#include "extpower.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
#include "i2c.h"
#include "system.h"
#include "task.h"
#include "util.h"

/* Console output macros */
#define CPRINTS(format, args...) cprints(CC_USBCHARGE, format, ## args)

/* Max number of attempts to enable/disable NVDC charger */
#define CHARGER_MODE_ATTEMPTS 3

/* Backboost has been detected */
static int bkboost_detected;

/* Charging is disabled */
static int charge_is_disabled;

/*
 * Charge circuit occasionally gets wedged and doesn't charge.
 * This variable keeps track of the state of the circuit.
 */
static enum {
	CHARGE_CIRCUIT_OK,
	CHARGE_CIRCUIT_WEDGED,
} charge_circuit_state = CHARGE_CIRCUIT_OK;

int extpower_is_present(void)
{
	return gpio_get_level(GPIO_AC_PRESENT);
}

static void extpower_buffer_to_pch(void)
{
	if (chipset_in_state(CHIPSET_STATE_HARD_OFF)) {
		/* Drive low in G3 state */
		gpio_set_level(GPIO_PCH_ACOK, 0);
	} else {
		/* Buffer from extpower in S5+ (where 3.3DSW enabled) */
		gpio_set_level(GPIO_PCH_ACOK, extpower_is_present());
	}
}
DECLARE_HOOK(HOOK_CHIPSET_PRE_INIT, extpower_buffer_to_pch, HOOK_PRIO_DEFAULT);

static void extpower_shutdown(void)
{
	/* Drive ACOK buffer to PCH low when shutting down */
	gpio_set_level(GPIO_PCH_ACOK, 0);
}
DECLARE_HOOK(HOOK_CHIPSET_SHUTDOWN, extpower_shutdown, HOOK_PRIO_DEFAULT);

void extpower_interrupt(enum gpio_signal signal)
{
	extpower_buffer_to_pch();

	/* Trigger notification of external power change */
	task_wake(TASK_ID_EXTPOWER);
}

static void extpower_init(void)
{
	extpower_buffer_to_pch();

	/* Enable interrupts, now that we've initialized */
	gpio_enable_interrupt(GPIO_AC_PRESENT);
}
DECLARE_HOOK(HOOK_INIT, extpower_init, HOOK_PRIO_DEFAULT);

/*
 * Save power in S3/S5/G3 by disabling charging when the battery is
 * full. Restore charging when battery is not full anymore. This saves
 * power because our input AC path is inefficient.
 */

static void check_charging_cutoff(void)
{
	/* If battery is full disable charging */
	if (charge_get_percent() == 100) {
		charge_is_disabled = 1;
		host_command_pd_send_status(PD_CHARGE_NONE);
	}
}
DECLARE_HOOK(HOOK_CHIPSET_SUSPEND, check_charging_cutoff, HOOK_PRIO_DEFAULT);

static void cancel_charging_cutoff(void)
{
	/* If charging is disabled, enable it */
	if (charge_is_disabled) {
		charge_is_disabled = 0;
		host_command_pd_send_status(PD_CHARGE_5V);
	}
}
DECLARE_HOOK(HOOK_CHIPSET_RESUME, cancel_charging_cutoff, HOOK_PRIO_DEFAULT);

static void batt_soc_change(void)
{
	/* If in S0, leave charging alone */
	if (chipset_in_state(CHIPSET_STATE_ON))
		return;

	/* Check to disable or enable charging based on batt state of charge */
	if (!charge_is_disabled && charge_get_percent() == 100) {
		host_command_pd_send_status(PD_CHARGE_NONE);
		charge_is_disabled = 1;
	} else if (charge_is_disabled && charge_get_percent() < 100) {
		charge_is_disabled = 0;
		host_command_pd_send_status(PD_CHARGE_5V);
	}
}
DECLARE_HOOK(HOOK_BATTERY_SOC_CHANGE, batt_soc_change, HOOK_PRIO_DEFAULT);

/**
 * Enable/disable NVDC charger to control AC to system and battery.
 */
static void charger_disable(int disable)
{
	int i, rv;

	for (i = 0; i < CHARGER_MODE_ATTEMPTS; i++) {
		rv = charger_discharge_on_ac(disable);
		if (rv == EC_SUCCESS)
			return;
	}

	CPRINTS("Setting learn mode %d failed!", disable);
}

static void allow_max_request(void)
{
	int prochot_status;
	if (charge_circuit_state == CHARGE_CIRCUIT_WEDGED) {
		/* Read PROCHOT status register to clear it */
		i2c_read8(I2C_PORT_CHARGER, BQ24773_ADDR,
			  BQ24773_PROCHOT_STATUS, &prochot_status);
		charge_circuit_state = CHARGE_CIRCUIT_OK;
	}
	host_command_pd_send_status(PD_CHARGE_MAX);
}
DECLARE_DEFERRED(allow_max_request);

static void extpower_board_hacks(int extpower, int extpower_prev)
{
	/* Cancel deferred attempt to enable max charge request */
	hook_call_deferred(allow_max_request, -1);

	/*
	 * When AC is detected, delay briefly before allowing PD
	 * to negotiate up to the max voltage to give charge circuit
	 * time to settle down. When AC goes away, set PD to only allow
	 * 5V charging for the next time AC is connected.
	 *
	 * Use NVDC charger learn mode (charger_disable()) when AC
	 * is not present to avoid backboosting when AC is plugged in.
	 *
	 * When in G3, PP5000 needs to be enabled to accurately sense
	 * CC voltage when AC is attached. When AC is disconnceted
	 * it needs to be off to save power.
	 */
	if (extpower && !extpower_prev) {
		/* AC connected */
		charger_disable(0);
		hook_call_deferred(allow_max_request, 500*MSEC);
		set_pp5000_in_g3(PP5000_IN_G3_AC, 1);
	} else if (extpower && extpower_prev) {
		/*
		 * Glitch on AC_PRESENT, attempt to recover from
		 * backboost
		 */
		host_command_pd_send_status(PD_CHARGE_NONE);
	} else {
		/* AC disconnected */
		if (!charge_is_disabled &&
		    charge_circuit_state == CHARGE_CIRCUIT_OK)
			host_command_pd_send_status(PD_CHARGE_NONE);

		charger_disable(1);

		if (!charge_is_disabled &&
		    charge_circuit_state == CHARGE_CIRCUIT_OK)
			host_command_pd_send_status(PD_CHARGE_5V);

		set_pp5000_in_g3(PP5000_IN_G3_AC, 0);
	}
	extpower_prev = extpower;
}

static void check_charge_wedged(void)
{
	int rv, prochot_status;

	if (charge_circuit_state == CHARGE_CIRCUIT_OK) {
		/* Check PROCHOT warning */
		rv = i2c_read8(I2C_PORT_CHARGER, BQ24773_ADDR,
				BQ24773_PROCHOT_STATUS, &prochot_status);
		if (rv)
			return;

		/*
		 * If PROCHOT is asserted, then charge circuit is wedged, turn
		 * on learn mode and notify PD to disable charging on all ports.
		 * Note: learn mode is critical here because when in this state
		 * backboosting causes >20V on boostin even after PD disables
		 * CHARGE_EN lines.
		 */
		if (prochot_status) {
			host_command_pd_send_status(PD_CHARGE_NONE);
			charge_circuit_state = CHARGE_CIRCUIT_WEDGED;
			CPRINTS("Charge circuit wedged!");
		}
	} else {
		/*
		 * Charge circuit is wedged and we already disabled charging,
		 * Now start to recover from wedged state by allowing 5V.
		 */
		host_command_pd_send_status(PD_CHARGE_5V);
	}
}

/**
 * Task to handle external power change
 */
void extpower_task(void)
{
	int extpower = extpower_is_present();
	int extpower_prev = 0;

	extpower_board_hacks(extpower, extpower_prev);

	/* Enable backboost detection interrupt */
	gpio_enable_interrupt(GPIO_BKBOOST_DET);

	while (1) {
		if (task_wait_event(2*SECOND) == TASK_EVENT_TIMER) {
			/* Periodically check if charge circuit is wedged */
			check_charge_wedged();
		} else {
			/* Must have received power change interrupt */
			extpower = extpower_is_present();

			/* Various board hacks to run on extpower change */
			extpower_board_hacks(extpower, extpower_prev);
			extpower_prev = extpower;

			hook_notify(HOOK_AC_CHANGE);

			/* Forward notification to host */
			host_set_single_event(extpower ?
						EC_HOST_EVENT_AC_CONNECTED :
						EC_HOST_EVENT_AC_DISCONNECTED);
		}
	}
}

void bkboost_det_interrupt(enum gpio_signal signal)
{
	/* Backboost has been detected, save it, and disable interrupt */
	bkboost_detected = 1;
	gpio_disable_interrupt(GPIO_BKBOOST_DET);
}

static int command_backboost_det(int argc, char **argv)
{
	ccprintf("Backboost detected: %d\n", bkboost_detected);
	return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(bkboost, command_backboost_det, NULL,
			"Read backboost detection",
			NULL);