summaryrefslogtreecommitdiff
path: root/board/kinox/power_detection.c
blob: 54ca5912527952d23ab4b6676e7e6728976b5b84 (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
/* Copyright 2022 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.
 */
#include "adc.h"
#include "charge_manager.h"
#include "chipset.h"
#include "common.h"
#include "compile_time_macros.h"
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "power.h"
#include "registers.h"
#include "task.h"
#include "timer.h"
#include "util.h"

/* Console output macros */
#define CPRINTF(format, args...) cprintf(CC_SYSTEM, format, ## args)
#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ## args)

/******************************************************************************/

static const char * const adp_id_names[] = {
	"unknown",
	"tiny",
	"tio1",
	"tio2",
	"typec",
};

/* ADP_ID control */
struct adpater_id_params tio1_power[] = {
	{
	.min_voltage = 3300,
	.max_voltage = 3300,
	.charge_voltage = 20000,
	.charge_current = 6000,
	.watt = 120,
	.obp95 = 1990,
	.obp85 = 1780,
	},
};

struct adpater_id_params tio2_power[] = {
	{
	.min_voltage = 0,
	.max_voltage = 68,
	.charge_voltage = 20000,
	.charge_current = 8500,
	.watt = 170,
	.obp95 = 2820,
	.obp85 = 916,
	},
	{
	.min_voltage = 68,
	.max_voltage = 142,
	.charge_voltage = 20000,
	.charge_current = 2250,
	.watt = 45,
	.obp95 = 750,
	.obp85 = 670,
	},
	{
	.min_voltage = 200,
	.max_voltage = 288,
	.charge_voltage = 20000,
	.charge_current = 3250,
	.watt = 65,
	.obp95 = 1080,
	.obp85 = 960,
	},
	{
	.min_voltage = 531,
	.max_voltage = 607,
	.charge_voltage = 20000,
	.charge_current = 6000,
	.watt = 120,
	.obp95 = 1990,
	.obp85 = 1780,
	},
	{
	.min_voltage = 384,
	.max_voltage = 480,
	.charge_voltage = 20000,
	.charge_current = 7500,
	.watt = 150,
	.obp95 = 2490,
	.obp85 = 2220,
	},
	{
	.min_voltage = 1062,
	.max_voltage = 1126,
	.charge_voltage = 20000,
	.charge_current = 8500,
	.watt = 170,
	.obp95 = 2820,
	.obp85 = 916,
	},
	{
	.min_voltage = 2816,
	.max_voltage = 3300,
	.charge_voltage = 20000,
	.charge_current = 6000,
	.watt = 120,
	.obp95 = 1990,
	.obp85 = 1780,
	},
};

struct adpater_id_params tiny_power[] = {
	{
	.min_voltage = 68,
	.max_voltage = 142,
	.charge_voltage = 20000,
	.charge_current = 2250,
	.watt = 45,
	.obp95 = 750,
	.obp85 = 670,
	},
	{
	.min_voltage = 200,
	.max_voltage = 288,
	.charge_voltage = 20000,
	.charge_current = 3250,
	.watt = 65,
	.obp95 = 1080,
	.obp85 = 960,
	},
	{
	.min_voltage = 384,
	.max_voltage = 480,
	.charge_voltage = 20000,
	.charge_current = 4500,
	.watt = 90,
	.obp95 = 1490,
	.obp85 = 1330,
	},
	{
	.min_voltage = 531,
	.max_voltage = 607,
	.charge_voltage = 20000,
	.charge_current = 6000,
	.watt = 120,
	.obp95 = 0x2D3,
	.obp85 = 0x286,
	},
	{
	.min_voltage = 653,
	.max_voltage = 783,
	.charge_voltage = 20000,
	.charge_current = 6750,
	.watt = 135,
	.obp95 = 2240,
	.obp85 = 2000,
	},
	{
	.min_voltage = 851,
	.max_voltage = 997,
	.charge_voltage = 20000,
	.charge_current = 7500,
	.watt = 150,
	.obp95 = 2490,
	.obp85 = 2220,
	},
	{
	.min_voltage = 1063,
	.max_voltage = 1226,
	.charge_voltage = 20000,
	.charge_current = 8500,
	.watt = 170,
	.obp95 = 2820,
	.obp85 = 916,
	},
	{
	.min_voltage = 1749,
	.max_voltage = 1968,
	.charge_voltage = 20000,
	.charge_current = 11500,
	.watt = 230,
	.obp95 = 3810,
	.obp85 = 3410,
	},
};

struct adpater_id_params typec_power[] = {
	{
	.charge_voltage = 20000,
	.charge_current = 1500,
	.watt = 30,
	.obp95 = 500,
	.obp85 = 440,
	},
	{
	.charge_voltage = 15000,
	.charge_current = 2000,
	.watt = 30,
	.obp95 = 660,
	.obp85 = 590,
	},
	{
	.charge_voltage = 20000,
	.charge_current = 2250,
	.watt = 45,
	.obp95 = 750,
	.obp85 = 670,
	},
	{
	.charge_voltage = 15000,
	.charge_current = 3000,
	.watt = 45,
	.obp95 = 990,
	.obp85 = 890,
	},
	{
	.charge_voltage = 20000,
	.charge_current = 3250,
	.watt = 65,
	.obp95 = 1080,
	.obp85 = 960,
	},
	{
	.charge_voltage = 20000,
	.charge_current = 5000,
	.watt = 100,
	.obp95 = 1660,
	.obp85 = 1480,
	},
};

struct adpater_id_params power_type[8];
static int adp_id_value_debounce;

void obp_point_95(void)
{
	/* Disable this interrupt while it's asserted. */
	npcx_adc_thresh_int_enable(NPCX_ADC_THRESH1, 0);
	/* Enable the voltage low interrupt. */
	npcx_adc_thresh_int_enable(NPCX_ADC_THRESH2, 1);

	/* Trigger the PROCHOT */
	gpio_set_level(GPIO_EC_PROCHOT_ODL, 0);
	CPRINTF("Adapter voltage over then 95%% trigger prochot.");
}

void obp_point_85(void)
{
	/* Disable this interrupt while it's asserted. */
	npcx_adc_thresh_int_enable(NPCX_ADC_THRESH2, 0);
	/* Enable the voltage high interrupt. */
	npcx_adc_thresh_int_enable(NPCX_ADC_THRESH1, 1);

	/* Release the PROCHOT */
	gpio_set_level(GPIO_EC_PROCHOT_ODL, 1);
	CPRINTF("Adapter voltage less then 85%% release prochot.");
}

struct npcx_adc_thresh_t adc_obp_point_95 = {
	.adc_ch = ADC_PWR_IN_IMON,
	.adc_thresh_cb = obp_point_95,
	.thresh_assert = 3300,	/* Default */
};

struct npcx_adc_thresh_t adc_obp_point_85 = {
	.adc_ch = ADC_PWR_IN_IMON,
	.adc_thresh_cb = obp_point_85,
	.lower_or_higher = 1,
	.thresh_assert = 0,	/* Default */
};

static void set_up_adc_irqs(void)
{
	/* Set interrupt thresholds for the ADC. */
	CPRINTS("%s", __func__);
	npcx_adc_register_thresh_irq(NPCX_ADC_THRESH1, &adc_obp_point_95);
	npcx_adc_register_thresh_irq(NPCX_ADC_THRESH2, &adc_obp_point_85);
	npcx_set_adc_repetitive(adc_channels[ADC_PWR_IN_IMON].input_ch, 1);
	npcx_adc_thresh_int_enable(NPCX_ADC_THRESH1, 1);
	npcx_adc_thresh_int_enable(NPCX_ADC_THRESH2, 1);
}

void set_the_obp(int power_type_index, int adp_type)
{
	struct charge_port_info pi = { 0 };

	adc_obp_point_95.thresh_assert = power_type[power_type_index].obp95;
	adc_obp_point_85.thresh_assert = power_type[power_type_index].obp85;
	set_up_adc_irqs();
	if (adp_type != TYPEC) {
		/* Only the TIO and Tiny need to update */
		pi.voltage = power_type[power_type_index].charge_voltage;
		pi.current = power_type[power_type_index].charge_current;

		switch (adp_type) {
		case TIO1:
		case TIO2:
			charge_manager_update_charge(
						CHARGE_SUPPLIER_PROPRIETARY,
						DEDICATED_CHARGE_PORT, &pi);
			break;
		case TINY:
			charge_manager_update_charge(
						CHARGE_SUPPLIER_DEDICATED,
						DEDICATED_CHARGE_PORT, &pi);
			break;
		}
	}

	CPRINTS("Power type %s, %dW", adp_id_names[adp_type],
		power_type[power_type_index].watt);
}

/*
 *       Scalar change to   Scalar change to
 *      downgrade voltage    3.3V voltage
 *               |                |
 *               |   SIO collect  |   SIO collect
 *               |   1st adapter  |   2nd adapter
 *               |   information  |   information
 *               |   |  |  |  |   |   |  |  |  |
 * -------------------------------------------------------
 *  |            |                |
 *  |---220 ms---|-----400 ms-----|
 *
 * Tiny: Twice adapter ADC values are less than 0x3FF.
 * TIO1: Twice adapter ADC values are 0x3FF.
 * TIO2: First adapter ADC value less than 0x3FF.
 *       Second adpater ADC value is 0x3FF.
 */
static void adp_id_deferred(void);
DECLARE_DEFERRED(adp_id_deferred);
void adp_id_deferred(void)
{
	int i = 0;
	int adp_type = 0;
	int adp_id_value;
	int adp_finial_adc_value;
	int power_type_len = 0;

	adp_id_value = adc_read_channel(ADC_ADP_ID);

	if (!adp_id_value_debounce) {
		adp_id_value_debounce = adp_id_value;
		/* for delay the 400ms to get the next APD_ID value */
		hook_call_deferred(&adp_id_deferred_data, 400 * MSEC);
	} else if (adp_id_value_debounce == 0x3FF && adp_id_value == 0x3FF) {
		adp_finial_adc_value = adp_id_value;
		adp_type = TIO1;
	} else if (adp_id_value_debounce < 0x3FF && adp_id_value == 0x3FF) {
		adp_finial_adc_value = adp_id_value_debounce;
		adp_type = TIO2;
	} else if (adp_id_value_debounce < 0x3FF && adp_id_value < 0x3FF) {
		adp_finial_adc_value = adp_id_value;
		adp_type = TINY;
	} else {
		CPRINTS("ADP_ID mismatch anything!");
		/* Set the default 65w adaptor max ADC value */
		adp_finial_adc_value = 0x69;
		adp_type = TINY;
	}

	switch (adp_type) {
	case TIO1:
		power_type_len = sizeof(tio1_power) /
					sizeof(struct adpater_id_params);
		memcpy(&power_type, &tio1_power, sizeof(tio1_power));
		break;
	case TIO2:
		power_type_len = sizeof(tio2_power) /
					sizeof(struct adpater_id_params);
		memcpy(&power_type, &tio2_power, sizeof(tio2_power));
		break;
	case TINY:
		power_type_len = sizeof(tiny_power) /
					sizeof(struct adpater_id_params);
		memcpy(&power_type, &tiny_power, sizeof(tiny_power));
		break;
	}

	for (i = 0; (i < power_type_len) && adp_type; i++) {
		if (adp_finial_adc_value <= power_type[i].max_voltage) {
			set_the_obp(i, adp_type);
			break;
		}
	}
}

static void barrel_jack_setting(void)
{
	/* Check ADP_ID when barrel jack is present */
	if (!gpio_get_level(GPIO_BJ_ADP_PRESENT_ODL))
		/* Delay 220ms to get the first ADP_ID value */
		hook_call_deferred(&adp_id_deferred_data, 220 * MSEC);
}
DECLARE_HOOK(HOOK_INIT, barrel_jack_setting, HOOK_PRIO_DEFAULT);

static void typec_adapter_setting(void)
{
	int i = 0;
	int adp_type = TYPEC;
	int adapter_current_ma;
	int power_type_len;

	/* Check the barrel jack is not present */
	if (gpio_get_level(GPIO_BJ_ADP_PRESENT_ODL)) {
		adapter_current_ma = charge_manager_get_charger_current();
		power_type_len = sizeof(typec_power) /
					sizeof(struct adpater_id_params);

		memcpy(&power_type, &typec_power, sizeof(typec_power));
		for (i = (power_type_len - 1); i >= 0; i--) {
			if (adapter_current_ma >=
				power_type[i].charge_current) {
				set_the_obp(i, adp_type);
				break;
			}
		}
	}
}
DECLARE_HOOK(HOOK_CHIPSET_RESUME, typec_adapter_setting, HOOK_PRIO_DEFAULT);