summaryrefslogtreecommitdiff
path: root/board/kodama/base_detect_kodama.c
blob: 2ec9fa7d2e8479625d9e99218a0b5bd3040b878e (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
/* Copyright 2019 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 "board.h"
#include "charge_manager.h"
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "timer.h"
#include "usb_pd.h"
#include "util.h"

#define CPRINTS(format, args...) cprints(CC_USB, format, ## args)

/* Krane base detection code */

/* Base detection and debouncing */
#define BASE_DETECT_DEBOUNCE_US (20 * MSEC)

/*
 * If the base status is unclear (i.e. not within expected ranges, read
 * the ADC value again every 500ms.
 */
#define BASE_DETECT_RETRY_US (500 * MSEC)

enum kukui_pogo_device_type {
	DEVICE_TYPE_ERROR = -2,
	DEVICE_TYPE_UNKNOWN = -1,
	DEVICE_TYPE_DETACHED = 0,
	DEVICE_TYPE_DOCK = 1,
	DEVICE_TYPE_KEYBOARD = 2,
	DEVICE_TYPE_COUNT,
};

struct {
	int mv_low, mv_high;
} static const pogo_detect_table[] = {
	[DEVICE_TYPE_DETACHED] = {2700, 3500}, /* 10K, NC, around 3.3V */
	[DEVICE_TYPE_DOCK] = {141, 173},       /* 10K, 0.5K ohm */
	[DEVICE_TYPE_KEYBOARD] = {270, 400},   /* 10K, 1K ohm */
};
BUILD_ASSERT(ARRAY_SIZE(pogo_detect_table) == DEVICE_TYPE_COUNT);

static uint64_t base_detect_debounce_time;

static enum kukui_pogo_device_type get_device_type(int mv)
{
	int i;

	if (mv == ADC_READ_ERROR)
		return DEVICE_TYPE_ERROR;

	for (i = 0; i < DEVICE_TYPE_COUNT; i++) {
		if (pogo_detect_table[i].mv_low <= mv &&
				mv <= pogo_detect_table[i].mv_high)
			return i;
	}

	return DEVICE_TYPE_UNKNOWN;
}

static void enable_charge(int enable)
{
	if (enable) {
		struct charge_port_info info = {
			.voltage = 5000, .current = 1500};
		/*
		 * Set supplier type to PD to have same priority as type c
		 * port.
		 */
		charge_manager_update_charge(
			CHARGE_SUPPLIER_DEDICATED, CHARGE_PORT_POGO, &info);
	} else {
		charge_manager_update_charge(
			CHARGE_SUPPLIER_DEDICATED, CHARGE_PORT_POGO, NULL);
	}
	pd_send_host_event(PD_EVENT_POWER_CHANGE);
}

static void enable_power_supply(int enable)
{
	gpio_set_level(GPIO_EN_PP3300_POGO, enable);
}

static void base_detect_deferred(void);
DECLARE_DEFERRED(base_detect_deferred);

static void base_detect_deferred(void)
{
	uint64_t time_now = get_time().val;
	int mv;
	int device_type;

	if (base_detect_debounce_time > time_now) {
		hook_call_deferred(&base_detect_deferred_data,
				   base_detect_debounce_time - time_now);
		return;
	}

	/*
	 * Disable interrupt first to prevent it triggered by value
	 * changed from 1 to disabled state(=0).
	 */
	gpio_disable_interrupt(GPIO_POGO_ADC_INT_L);
	gpio_set_flags(GPIO_POGO_ADC_INT_L, GPIO_ANALOG);
	mv = adc_read_channel(ADC_POGO_ADC_INT_L);
	/* restore the pin function */
	gpio_set_flags(GPIO_POGO_ADC_INT_L, GPIO_INT_BOTH);
	gpio_enable_interrupt(GPIO_POGO_ADC_INT_L);

	device_type = get_device_type(mv);
	CPRINTS("POGO: adc=%d, device_type=%d", mv, device_type);

	switch (device_type) {
	case DEVICE_TYPE_ERROR:
	case DEVICE_TYPE_UNKNOWN:
		hook_call_deferred(&base_detect_deferred_data,
				BASE_DETECT_RETRY_US);
		break;

	case DEVICE_TYPE_DETACHED:
		enable_power_supply(0);
		enable_charge(0);
		break;

	case DEVICE_TYPE_DOCK:
		enable_power_supply(0);
		enable_charge(1);
		break;

	case DEVICE_TYPE_KEYBOARD:
		enable_charge(0);
		enable_power_supply(1);
		break;

	case DEVICE_TYPE_COUNT:
		/* should not happen */
		break;
	}
}

void pogo_adc_interrupt(enum gpio_signal signal)
{
	uint64_t time_now = get_time().val;

	if (base_detect_debounce_time <= time_now) {
		hook_call_deferred(&base_detect_deferred_data,
				   BASE_DETECT_DEBOUNCE_US);
	}

	base_detect_debounce_time = time_now + BASE_DETECT_DEBOUNCE_US;
}

static void base_init(void)
{
	hook_call_deferred(&base_detect_deferred_data, 0);
}
DECLARE_HOOK(HOOK_INIT, base_init, HOOK_PRIO_INIT_ADC + 1);