forked from FrameworkComputer/EmbeddedController
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdp_hpd_gpio.c
83 lines (69 loc) · 2.11 KB
/
dp_hpd_gpio.c
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
/* Copyright 2023 The ChromiumOS Authors
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
* Support for setting the Hot Plug Detect indication to the AP
*/
#include "chipset.h"
#include "common.h"
#include "console.h"
#include "gpio.h"
#include "timer.h"
#include "usb_pd.h"
#include <stdbool.h>
#include <stdint.h>
#ifdef CONFIG_COMMON_RUNTIME
#define CPRINTF(format, args...) cprintf(CC_USBPD, format, ##args)
#define CPRINTS(format, args...) cprints(CC_USBPD, format, ##args)
#else
#define CPRINTF(format, args...)
#define CPRINTS(format, args...)
#endif
/* TODO(b/270409742): Remove this macro system for determining the GPIO */
#ifndef PORT_TO_HPD
#define PORT_TO_HPD(port) ((port) ? GPIO_USB_C1_DP_HPD : GPIO_USB_C0_DP_HPD)
#endif /* PORT_TO_HPD */
/*
* Note: the following DP-related variables and functions must be kept
* as-is since some boards are using them in their board-specific code.
* TODO(b/267545470): Fold board DP code into the DP module
*/
#if defined(CONFIG_USB_PD_DP_HPD_GPIO) && \
!defined(CONFIG_USB_PD_DP_HPD_GPIO_CUSTOM)
void svdm_set_hpd_gpio(int port, int en)
{
gpio_set_level(PORT_TO_HPD(port), en);
}
int svdm_get_hpd_gpio(int port)
{
return gpio_get_level(PORT_TO_HPD(port));
}
#endif
enum ec_error_list dp_hpd_gpio_set(int port, bool level, bool irq)
{
int cur_level = svdm_get_hpd_gpio(port);
if (irq && !level) {
/*
* IRQ can only be generated when the level is high, because
* the IRQ is signaled by a short low pulse from the high level.
*/
CPRINTF("ERR:HPD:IRQ&LOW\n");
return EC_ERROR_INVAL;
}
if (irq && cur_level) {
uint64_t now = get_time().val;
/* wait for the minimum spacing between IRQ_HPD if needed */
if (now < svdm_hpd_deadline[port])
usleep(svdm_hpd_deadline[port] - now);
/* generate IRQ_HPD pulse */
svdm_set_hpd_gpio(port, 0);
usleep(HPD_DSTREAM_DEBOUNCE_IRQ);
svdm_set_hpd_gpio(port, 1);
} else {
svdm_set_hpd_gpio(port, level);
}
/* set the minimum time delay (2ms) for the next HPD IRQ */
svdm_hpd_deadline[port] = get_time().val + HPD_USTREAM_DEBOUNCE_LVL;
return EC_SUCCESS;
}