diff options
-rw-r--r-- | driver/tcpm/nct38xx.c | 48 | ||||
-rw-r--r-- | zephyr/dts/bindings/usbc/nuvoton,nct38xx.yaml | 14 | ||||
-rw-r--r-- | zephyr/shim/include/usbc/tcpc_nct38xx.h | 18 | ||||
-rw-r--r-- | zephyr/shim/src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | zephyr/shim/src/tcpc_nct38xx.c | 31 |
5 files changed, 106 insertions, 7 deletions
diff --git a/driver/tcpm/nct38xx.c b/driver/tcpm/nct38xx.c index 00240516e5..0d45c8aa9f 100644 --- a/driver/tcpm/nct38xx.c +++ b/driver/tcpm/nct38xx.c @@ -14,6 +14,17 @@ #include "tcpm/tcpci.h" #include "usb_common.h" +#ifdef CONFIG_ZEPHYR +#include <device.h> +#include <drivers/gpio/gpio_nct38xx.h> +#include "usbc/tcpc_nct38xx.h" +#endif + +#if defined(CONFIG_ZEPHYR) && defined(CONFIG_IO_EXPANDER_NCT38XX) +#error CONFIG_IO_EXPANDER_NCT38XX cannot be used with Zephyr. +#error Enable the Zephyr driver CONFIG_GPIO_NCT38XX instead. +#endif + #if !defined(CONFIG_USB_PD_TCPM_TCPCI) #error "NCT38XX is using part of standard TCPCI control" #error "Please upgrade your board configuration" @@ -134,8 +145,19 @@ static int nct38xx_init(int port) * Enable the Vendor Define alert event only when the IO expander * feature is defined */ - if (IS_ENABLED(CONFIG_IO_EXPANDER_NCT38XX)) + if (IS_ENABLED(CONFIG_IO_EXPANDER_NCT38XX) || + IS_ENABLED(CONFIG_GPIO_NCT38XX)) { +#ifdef CONFIG_ZEPHYR + const struct device *dev = + nct38xx_get_gpio_device_from_port(port); + + if (!device_is_ready(dev)) { + CPRINTS("C%d: device is not ready", port); + return EC_ERROR_BUSY; + } +#endif /* CONFIG_ZEPHYR */ reg |= TCPC_REG_ALERT_VENDOR_DEF; + } rv = tcpc_update16(port, TCPC_REG_ALERT_MASK, @@ -244,6 +266,20 @@ __overridable int board_map_nct38xx_tcpc_port_to_ioex(int port) return port; } +static inline void nct38xx_tcpc_vendor_defined_alert(int port) +{ +#ifdef CONFIG_ZEPHYR + const struct device *dev = nct38xx_get_gpio_device_from_port(port); + + nct38xx_gpio_alert_handler(dev); +#else + int ioexport; + + ioexport = board_map_nct38xx_tcpc_port_to_ioex(port); + nct38xx_ioex_event_handler(ioexport); +#endif /* CONFIG_ZEPHYR */ +} + static void nct38xx_tcpc_alert(int port) { int alert, rv; @@ -274,12 +310,10 @@ static void nct38xx_tcpc_alert(int port) * tcpci_tcpc_alert(). Check the Vendor Defined Alert bit to * handle the IOEX IO's interrupt event. */ - if (IS_ENABLED(CONFIG_IO_EXPANDER_NCT38XX) && - rv == EC_SUCCESS && (alert & TCPC_REG_ALERT_VENDOR_DEF)) { - int ioexport; - - ioexport = board_map_nct38xx_tcpc_port_to_ioex(port); - nct38xx_ioex_event_handler(ioexport); + if ((IS_ENABLED(CONFIG_IO_EXPANDER_NCT38XX) || + IS_ENABLED(CONFIG_GPIO_NCT38XX)) && + rv == EC_SUCCESS && (alert & TCPC_REG_ALERT_VENDOR_DEF)) { + nct38xx_tcpc_vendor_defined_alert(port); } } diff --git a/zephyr/dts/bindings/usbc/nuvoton,nct38xx.yaml b/zephyr/dts/bindings/usbc/nuvoton,nct38xx.yaml new file mode 100644 index 0000000000..2d85d14f79 --- /dev/null +++ b/zephyr/dts/bindings/usbc/nuvoton,nct38xx.yaml @@ -0,0 +1,14 @@ +# Copyright 2021 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. + +description: Nuvoton NCT38XX USB TCPC binding + +compatible: "nuvoton,nct38xx" + +properties: + gpio-dev: + type: phandle + description: | + Pointer to the NCT38XX GPIO device. This is used to binding the Cros TCPC + port index to Zephyr NCT38XX GPIO device. diff --git a/zephyr/shim/include/usbc/tcpc_nct38xx.h b/zephyr/shim/include/usbc/tcpc_nct38xx.h new file mode 100644 index 0000000000..fa60d1caa9 --- /dev/null +++ b/zephyr/shim/include/usbc/tcpc_nct38xx.h @@ -0,0 +1,18 @@ +/* Copyright 2021 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. + */ + +#ifndef __CROS_EC_TCPC_NCT38XX_H +#define __CROS_EC_TCPC_NCT38XX_H + +/** + * @brief Get the NCT38XX GPIO device from the TCPC port enumeration + * + * @param port The enumeration of TCPC port + * + * @return NULL if failed, otherwise a pointer to NCT38XX GPIO device + */ +const struct device *nct38xx_get_gpio_device_from_port(const int port); + +#endif /* __CROS_EC_TCPC_NCT38XX_H */ diff --git a/zephyr/shim/src/CMakeLists.txt b/zephyr/shim/src/CMakeLists.txt index 0c795f6b9b..fe9a5a015d 100644 --- a/zephyr/shim/src/CMakeLists.txt +++ b/zephyr/shim/src/CMakeLists.txt @@ -53,3 +53,5 @@ zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_WATCHDOG watchdog.c) zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_BC12_DETECT_PI3USB9201 bc12_pi3usb9201.c) zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_USBC_PPC ppc.c) +zephyr_library_sources_ifdef(CONFIG_PLATFORM_EC_USB_PD_TCPM_NCT38XX + tcpc_nct38xx.c) diff --git a/zephyr/shim/src/tcpc_nct38xx.c b/zephyr/shim/src/tcpc_nct38xx.c new file mode 100644 index 0000000000..d18e4ccbbe --- /dev/null +++ b/zephyr/shim/src/tcpc_nct38xx.c @@ -0,0 +1,31 @@ +/* Copyright 2021 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 <device.h> +#include <devicetree.h> + +#include "config.h" +#include "usbc/tcpc_nct38xx.h" + +#define TCPC_PORT(id) DT_REG_ADDR(DT_PARENT(id)) + +#define GPIO_DEV_WITH_COMMA(id) DEVICE_DT_GET(DT_PHANDLE(id, gpio_dev)), + +#define GPIO_DEV_BINDING(id) \ + COND_CODE_1(DT_NODE_HAS_PROP(id, gpio_dev), \ + ([TCPC_PORT(id)] = GPIO_DEV_WITH_COMMA(id)), ()) + +/* NCT38XX GPIO device pool for binding the TCPC port and NCT38XX GPIO device */ +static const struct device + *nct38xx_gpio_devices[CONFIG_PLATFORM_EC_USB_PD_PORT_MAX_COUNT] = { + DT_FOREACH_STATUS_OKAY(nuvoton_nct38xx, GPIO_DEV_BINDING) + }; + +const struct device *nct38xx_get_gpio_device_from_port(const int port) +{ + if (port < 0 || port >= CONFIG_PLATFORM_EC_USB_PD_PORT_MAX_COUNT) + return NULL; + return nct38xx_gpio_devices[port]; +} |