summaryrefslogtreecommitdiff
path: root/zephyr/projects/corsola/src/variant_db_detection.c
blob: 6099d86bdd4526e7a67b84e011ffdaffa79f7285 (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
/* 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 <zephyr/drivers/gpio.h>

#include "console.h"
#include "cros_cbi.h"
#include "gpio/gpio_int.h"
#include "hooks.h"

#include "variant_db_detection.h"

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

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);