From eff4baf03f28f04cf1e89a602fe3a244228cbb6e Mon Sep 17 00:00:00 2001 From: Aseda Aboagye Date: Thu, 30 Nov 2017 00:02:20 -0800 Subject: [PATCH] sn5s330: Add support for Vbus detection. The SN5S330 has support for detecting when Vbus is present on a port. This commit simply adds an API to query the PPC. BUG=None BRANCH=None TEST=`make -j buildall`. TEST=Flash a board with the SN5S330, with some extra code, verify that Vbus can be detected with this API. Change-Id: I45bf7ff24bcdc447efe12932f51f8094108e29d5 Signed-off-by: Aseda Aboagye Reviewed-on: https://chromium-review.googlesource.com/791502 Commit-Ready: Aseda Aboagye Tested-by: Aseda Aboagye Reviewed-by: Edward Hill Reviewed-by: Shawn N --- common/usbc_ppc.c | 10 ++++++++++ driver/ppc/sn5s330.c | 25 ++++++++++++++++++++----- driver/ppc/sn5s330.h | 3 +++ include/config.h | 2 ++ include/usbc_ppc.h | 28 ++++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/common/usbc_ppc.c b/common/usbc_ppc.c index 5d41619a79..081cef811f 100644 --- a/common/usbc_ppc.c +++ b/common/usbc_ppc.c @@ -42,6 +42,16 @@ int ppc_vbus_source_enable(int port, int enable) return ppc_chips[port].drv->vbus_source_enable(port, enable); } +#ifdef CONFIG_USB_PD_VBUS_DETECT_PPC +int ppc_is_vbus_present(int port, int *vbus_present) +{ + if (port >= ppc_cnt) + return EC_ERROR_INVAL; + + return ppc_chips[port].drv->is_vbus_present(port, vbus_present); +} +#endif /* defined(CONFIG_USB_PD_VBUS_DETECT_PPC) */ + static void ppc_init(void) { int i; diff --git a/driver/ppc/sn5s330.c b/driver/ppc/sn5s330.c index 2949a74a99..f261f434ca 100644 --- a/driver/ppc/sn5s330.c +++ b/driver/ppc/sn5s330.c @@ -122,14 +122,12 @@ static int sn5s330_is_pp_fet_enabled(uint8_t port, enum sn5s330_pp_idx pp, int status; int regval; - if (pp == SN5S330_PP1) { + if (pp == SN5S330_PP1) pp_bit = SN5S330_PP1_EN; - } else if (pp == SN5S330_PP2) { + else if (pp == SN5S330_PP2) pp_bit = SN5S330_PP2_EN; - } else { - CPRINTF("bad PP idx(%d)!", pp); + else return EC_ERROR_INVAL; - } status = get_func_set3(port, ®val); if (status) @@ -415,6 +413,20 @@ static int sn5s330_init(int port) return EC_SUCCESS; } +#ifdef CONFIG_USB_PD_VBUS_DETECT_PPC +static int sn5s330_is_vbus_present(int port, int *vbus_present) +{ + int regval; + int rv; + + rv = read_reg(port, SN5S330_INT_STATUS_REG3, ®val); + if (!rv) + *vbus_present = !!(regval & SN5S330_VBUS_GOOD); + + return rv; +} +#endif /* defined(CONFIG_USB_PD_VBUS_DETECT_PPC) */ + static int sn5s330_is_sourcing_vbus(int port) { int is_sourcing_vbus = 0; @@ -448,4 +460,7 @@ const struct ppc_drv sn5s330_drv = { #ifdef CONFIG_CMD_PPC_DUMP .reg_dump = &sn5s330_dump, #endif /* defined(CONFIG_CMD_PPC_DUMP) */ +#ifdef CONFIG_USB_PD_VBUS_DETECT_PPC + .is_vbus_present = &sn5s330_is_vbus_present, +#endif /* defined(CONFIG_USB_PD_VBUS_DETECT_PPC) */ }; diff --git a/driver/ppc/sn5s330.h b/driver/ppc/sn5s330.h index 059df14a30..683c3f6807 100644 --- a/driver/ppc/sn5s330.h +++ b/driver/ppc/sn5s330.h @@ -100,6 +100,9 @@ enum sn5s330_pp_idx { #define SN5S330_PP2_CONFIG (1 << 2) #define SN5S330_OVP_EN_CC (1 << 4) +/* INT_STATUS_REG3 */ +#define SN5S330_VBUS_GOOD (1 << 0) + /* INT_STATUS_REG4 */ #define SN5S330_DIG_RES (1 << 0) #define SN5S330_DB_BOOT (1 << 1) diff --git a/include/config.h b/include/config.h index bb4584b0b6..713bb87bb5 100644 --- a/include/config.h +++ b/include/config.h @@ -2641,6 +2641,7 @@ * - Some TCPCs can detect and report the presence of VBUS. * - In some configurations, charger ICs can report the presence of VBUS. * - On some boards, dedicated VBUS interrupt pins are available. + * - Some power path controllers (PPC) can report the presence of VBUS. * * Exactly one of these should be defined for all boards that run the PD * state machine. @@ -2648,6 +2649,7 @@ #undef CONFIG_USB_PD_VBUS_DETECT_TCPC #undef CONFIG_USB_PD_VBUS_DETECT_CHARGER #undef CONFIG_USB_PD_VBUS_DETECT_GPIO +#undef CONFIG_USB_PD_VBUS_DETECT_PPC #undef CONFIG_USB_PD_VBUS_DETECT_NONE diff --git a/include/usbc_ppc.h b/include/usbc_ppc.h index 05e671728a..f5b72742d2 100644 --- a/include/usbc_ppc.h +++ b/include/usbc_ppc.h @@ -56,6 +56,24 @@ struct ppc_drv { */ int (*reg_dump)(int port); #endif /* defined(CONFIG_CMD_PPC_DUMP) */ + +#ifdef CONFIG_USB_PD_VBUS_DETECT_PPC + /* + * TODO(aaboagye): In order for VBUS detection to work properly for our + * system, we need to enable VBUS interrupts and send the appropriate + * notifications. + */ + + /** + * Determine if VBUS is present or not. + * + * @param port: The Type-C port number. + * @param vbus_present: 1: VBUS is present. 0: VBUS is not present. + * @return EC_SUCCESS if able to determine VBUS status, otherwise an + * error. + */ + int (*is_vbus_present)(int port, int *vbus_present); +#endif /* defined(CONFIG_USB_PD_VBUS_DETECT_PPC) */ }; struct ppc_config_t { @@ -67,6 +85,16 @@ struct ppc_config_t { extern const struct ppc_config_t ppc_chips[]; extern const unsigned int ppc_cnt; +/** + * Determine if VBUS is present or not. + * + * @param port: The Type-C port number. + * @param vbus_present: 1: VBUS is present. 0: VBUS is not present. + * @return EC_SUCCESS if able to determine VBUS status, otherwise an + * error. + */ +int ppc_is_vbus_present(int port, int *vbus_present); + /** * Is the port sourcing Vbus? *