summaryrefslogtreecommitdiff
path: root/zephyr/program/corsola/src/variant_db_detection.c
blob: 9ade14451368cde727e94b3491886ae4ed7c1910 (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
/* Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Corsola daughter board detection */
#include "baseboard_usbc_config.h"
#include "console.h"
#include "cros_cbi.h"
#include "gpio/gpio_int.h"
#include "hooks.h"
#include "usb_mux.h"
#include "variant_db_detection.h"

#include <zephyr/drivers/gpio.h>

#define CPRINTS(format, args...) cprints(CC_SYSTEM, format, ##args)
#define CPRINTF(format, args...) cprintf(CC_SYSTEM, format, ##args)

#ifdef TEST_BUILD
uint32_t dp_status[CONFIG_USB_PD_PORT_MAX_COUNT];
#endif

static void corsola_db_config(enum corsola_db_type type)
{
	switch (type) {
	case CORSOLA_DB_HDMI:
		/* EC_X_GPIO1 */
		gpio_pin_configure_dt(GPIO_DT_FROM_ALIAS(gpio_en_hdmi_pwr),
				      GPIO_OUTPUT_HIGH);
		/* X_EC_GPIO2 */
		gpio_pin_configure_dt(GPIO_DT_FROM_ALIAS(gpio_ps185_ec_dp_hpd),
				      GPIO_INPUT);
		gpio_enable_dt_interrupt(
			GPIO_INT_FROM_NODELABEL(int_x_ec_gpio2));
		/* EC_X_GPIO3 */
		gpio_pin_configure_dt(GPIO_DT_FROM_ALIAS(gpio_ps185_pwrdn_odl),
				      GPIO_OUTPUT_HIGH | GPIO_OPEN_DRAIN);
		return;
	case CORSOLA_DB_TYPEC:
		/* EC_X_GPIO1 */
		gpio_pin_configure_dt(GPIO_DT_FROM_ALIAS(gpio_usb_c1_frs_en),
				      GPIO_OUTPUT_LOW);
		/* X_EC_GPIO2 */
		gpio_pin_configure_dt(
			GPIO_DT_FROM_ALIAS(gpio_usb_c1_ppc_int_odl),
			GPIO_INPUT | GPIO_PULL_UP);
		gpio_enable_dt_interrupt(
			GPIO_INT_FROM_NODELABEL(int_x_ec_gpio2));
		/* EC_X_GPIO3 */
		gpio_pin_configure_dt(GPIO_DT_FROM_ALIAS(gpio_usb_c1_dp_in_hpd),
				      GPIO_OUTPUT_LOW);
		return;
	case CORSOLA_DB_NONE:
		/* Set floating pins as input with PU to prevent leakage */
		gpio_pin_configure_dt(GPIO_DT_FROM_NODELABEL(gpio_ec_x_gpio1),
				      GPIO_INPUT | GPIO_PULL_UP);
		gpio_pin_configure_dt(GPIO_DT_FROM_NODELABEL(gpio_x_ec_gpio2),
				      GPIO_INPUT | GPIO_PULL_UP);
		gpio_pin_configure_dt(GPIO_DT_FROM_NODELABEL(gpio_ec_x_gpio3),
				      GPIO_INPUT | GPIO_PULL_UP);
		return;
	default:
		break;
	}
}

enum corsola_db_type corsola_get_db_type(void)
{
#if DT_NODE_EXISTS(DT_NODELABEL(db_config))
	int ret;
	uint32_t val;
#endif
	static enum corsola_db_type db = CORSOLA_DB_UNINIT;

	if (db != CORSOLA_DB_UNINIT) {
		return db;
	}

	if (!gpio_pin_get_dt(GPIO_DT_FROM_NODELABEL(gpio_hdmi_prsnt_odl))) {
		db = CORSOLA_DB_HDMI;
	} else {
		db = CORSOLA_DB_TYPEC;
	}

/* Detect for no sub board case by FW_CONFIG */
#if DT_NODE_EXISTS(DT_NODELABEL(db_config))
	ret = cros_cbi_get_fw_config(DB, &val);
	if (ret != 0) {
		CPRINTS("Error retrieving CBI FW_CONFIG field %d", DB);
	} else if (val == DB_NONE) {
		db = CORSOLA_DB_NONE;
	}
#endif

	corsola_db_config(db);

	switch (db) {
	case CORSOLA_DB_NONE:
		CPRINTS("Detect %s DB", "NONE");
		break;
	case CORSOLA_DB_TYPEC:
		CPRINTS("Detect %s DB", "TYPEC");
		break;
	case CORSOLA_DB_HDMI:
		CPRINTS("Detect %s DB", "HDMI");
		break;
	default:
		CPRINTS("DB UNINIT");
		break;
	}

	return db;
}

static void corsola_db_init(void)
{
	corsola_get_db_type();
}
DECLARE_HOOK(HOOK_INIT, corsola_db_init, HOOK_PRIO_PRE_I2C);

/**
 * Handle PS185 HPD changing state.
 */
void ps185_hdmi_hpd_mux_set(void)
{
	const int hpd =
		gpio_pin_get_dt(GPIO_DT_FROM_ALIAS(gpio_ps185_ec_dp_hpd));

	if (!corsola_is_dp_muxable(USBC_PORT_C1)) {
		return;
	}

	if (hpd && !(usb_mux_get(USBC_PORT_C1) & USB_PD_MUX_DP_ENABLED)) {
		dp_status[USBC_PORT_C1] =
			VDO_DP_STATUS(0, /* HPD IRQ  ... not applicable */
				      0, /* HPD level ... not applicable */
				      0, /* exit DP? ... no */
				      0, /* usb mode? ... no */
				      0, /* multi-function ... no */
				      1, /* DP enabled ... yes */
				      0, /* power low?  ... no */
				      (!!DP_FLAGS_DP_ON));
		/* update C1 virtual mux */
		usb_mux_set(USBC_PORT_C1, USB_PD_MUX_DP_ENABLED,
			    USB_SWITCH_DISCONNECT,
			    0 /* polarity, don't care */);
		CPRINTS("HDMI plug");
	}
}

static void ps185_hdmi_hpd_deferred(void)
{
	const int hpd =
		gpio_pin_get_dt(GPIO_DT_FROM_ALIAS(gpio_ps185_ec_dp_hpd));

	if (!hpd && (usb_mux_get(USBC_PORT_C1) & USB_PD_MUX_DP_ENABLED)) {
		dp_status[USBC_PORT_C1] =
			VDO_DP_STATUS(0, /* HPD IRQ  ... not applicable */
				      0, /* HPD level ... not applicable */
				      0, /* exit DP? ... no */
				      0, /* usb mode? ... no */
				      0, /* multi-function ... no */
				      0, /* DP enabled ... no */
				      0, /* power low?  ... no */
				      (!DP_FLAGS_DP_ON));
		usb_mux_set(USBC_PORT_C1, USB_PD_MUX_NONE,
			    USB_SWITCH_DISCONNECT,
			    0 /* polarity, don't care */);
		CPRINTS("HDMI unplug");

		return;
	}

	ps185_hdmi_hpd_mux_set();
}
DECLARE_DEFERRED(ps185_hdmi_hpd_deferred);

#define HPD_SINK_ABSENCE_DEBOUNCE (2 * MSEC)

void hdmi_hpd_interrupt(enum gpio_signal signal)
{
	const int hpd =
		gpio_pin_get_dt(GPIO_DT_FROM_ALIAS(gpio_ps185_ec_dp_hpd));

	if (!hpd) {
		hook_call_deferred(&ps185_hdmi_hpd_deferred_data,
				   HPD_SINK_ABSENCE_DEBOUNCE);
	} else {
		hook_call_deferred(&ps185_hdmi_hpd_deferred_data, -1);
	}

	/* C0 DP is muxed, we should not send HPD to the AP */
	if (!corsola_is_dp_muxable(USBC_PORT_C1)) {
		if (hpd) {
			CPRINTS("C0 port is already muxed.");
		}
		return;
	}

	if (hpd && !(usb_mux_get(USBC_PORT_C1) & USB_PD_MUX_DP_ENABLED)) {
		/* set dp_aux_path_sel first, and configure the usb_mux in the
		 * deferred hook to prevent from dead locking.
		 */
		gpio_pin_set_dt(GPIO_DT_FROM_NODELABEL(dp_aux_path_sel), hpd);
		hook_call_deferred(&ps185_hdmi_hpd_deferred_data, 0);
	}

	svdm_set_hpd_gpio(USBC_PORT_C1, hpd);
}