drivers: usb_c: numaker: support Nuvoton's M2L31 series

1. Support USB-C drivers TCPC, PPC, and VBUS with UTCPD H/W IP
2. UTCPD is interconnected with Timer-triggered EADC for updating
   VBUS/VCONN voltage periodically

Signed-off-by: Chun-Chieh Li <ccli8@nuvoton.com>
This commit is contained in:
Chun-Chieh Li 2024-04-01 17:59:15 +08:00 committed by Carles Cufí
parent c84c2fc37d
commit 9e9e409cb9
18 changed files with 3183 additions and 0 deletions

View file

@ -4,3 +4,4 @@ zephyr_library()
zephyr_library_sources_ifdef(CONFIG_USBC_PPC_SHELL shell.c)
zephyr_library_sources_ifdef(CONFIG_USBC_PPC_NX20P3483 nxp_nx20p3483.c)
zephyr_library_sources_ifdef(CONFIG_USBC_PPC_NUMAKER usbc_ppc_numaker.c)

View file

@ -22,6 +22,7 @@ config USBC_PPC_SHELL
Add useful shell commands to manipulate and debug the PPCs
source "drivers/usb_c/ppc/Kconfig.nxp"
source "drivers/usb_c/ppc/Kconfig.numaker"
module = USBC_PPC
module-str = usbc-ppc

View file

@ -0,0 +1,11 @@
# Nuvoton NuMaker USB-C PPC device configuration options
# Copyright (c) 2024 Nuvoton Technology Corporation
# SPDX-License-Identifier: Apache-2.0
config USBC_PPC_NUMAKER
bool "Nuvoton NuMaker USB-C PPC"
default y
depends on DT_HAS_NUVOTON_NUMAKER_PPC_ENABLED && USBC_TCPC_NUMAKER
help
Enable USB-C PPC support for Nuvoton NuMaker chip with UTCPD.

View file

@ -0,0 +1,225 @@
/*
* Copyright (c) 2024 Nuvoton Technology Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nuvoton_numaker_ppc
#include <zephyr/kernel.h>
#include <zephyr/drivers/usb_c/usbc_ppc.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/drivers/clock_control/clock_control_numaker.h>
#include <zephyr/drivers/reset.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(ppc_numaker, CONFIG_USBC_LOG_LEVEL);
#include <soc.h>
#include <NuMicro.h>
#include "../tcpc/ucpd_numaker.h"
/* Implementation notes on NuMaker TCPC/PPC/VBUS
*
* PPC and VBUS rely on TCPC/UTCPD and are just pseudo. They are completely
* implemented in TCPC/UTCPD.
*/
/**
* @brief Immutable device context
*/
struct numaker_ppc_config {
const struct device *tcpc_dev;
};
/**
* @brief Initializes the usb-c ppc driver
*
* @retval 0 on success
* @retval -ENODEV if dependent TCPC device is not ready
*/
static int numaker_ppc_init(const struct device *dev)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
/* Rely on TCPC */
if (!device_is_ready(tcpc_dev)) {
LOG_ERR("TCPC device not ready");
return -ENODEV;
}
return 0;
}
/**
* @brief Check if PPC is in the dead battery mode
*
* @retval 1 if PPC is in the dead battery mode
* @retval 0 if PPC is not in the dead battery mode
* @retval -EIO if on failure
*/
static int numaker_ppc_is_dead_battery_mode(const struct device *dev)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_is_dead_battery_mode(tcpc_dev);
}
/**
* @brief Request the PPC to exit from the dead battery mode
*
* @retval 0 if request was successfully sent
* @retval -EIO if on failure
*/
static int numaker_ppc_exit_dead_battery_mode(const struct device *dev)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_exit_dead_battery_mode(tcpc_dev);
}
/**
* @brief Check if the PPC is sourcing the VBUS
*
* @retval 1 if the PPC is sourcing the VBUS
* @retval 0 if the PPC is not sourcing the VBUS
* @retval -EIO on failure
*/
static int numaker_ppc_is_vbus_source(const struct device *dev)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_is_vbus_source(tcpc_dev);
}
/**
* @brief Check if the PPC is sinking the VBUS
*
* @retval 1 if the PPC is sinking the VBUS
* @retval 0 if the PPC is not sinking the VBUS
* @retval -EIO on failure
*/
static int numaker_ppc_is_vbus_sink(const struct device *dev)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_is_vbus_sink(tcpc_dev);
}
/**
* @brief Set the state of VBUS sinking
*
* @retval 0 if success
* @retval -EIO on failure
*/
static int numaker_ppc_set_snk_ctrl(const struct device *dev, bool enable)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_set_snk_ctrl(tcpc_dev, enable);
}
/**
* @brief Set the state of VBUS sourcing
*
* @retval 0 if success
* @retval -EIO on failure
*/
static int numaker_ppc_set_src_ctrl(const struct device *dev, bool enable)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_set_src_ctrl(tcpc_dev, enable);
}
/**
* @brief Set the state of VBUS discharging
*
* @retval 0 if success
* @retval -EIO on failure
*/
static int numaker_ppc_set_vbus_discharge(const struct device *dev, bool enable)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_set_vbus_discharge(tcpc_dev, enable);
}
/**
* @brief Check if VBUS is present
*
* @retval 1 if VBUS voltage is present
* @retval 0 if no VBUS voltage is detected
* @retval -EIO on failure
*/
static int numaker_ppc_is_vbus_present(const struct device *dev)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_is_vbus_present(tcpc_dev);
}
/**
* @brief Set the callback used to notify about PPC events
*
* @retval 0 if success
*/
static int numaker_ppc_set_event_handler(const struct device *dev, usbc_ppc_event_cb_t handler,
void *data)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_set_event_handler(tcpc_dev, handler, data);
}
/**
* @brief Print the values or PPC registers
*
* @retval 0 if success
* @retval -EIO on failure
*/
static int numaker_ppc_dump_regs(const struct device *dev)
{
const struct numaker_ppc_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_ppc_dump_regs(tcpc_dev);
}
static const struct usbc_ppc_driver_api numaker_ppc_driver_api = {
.is_dead_battery_mode = numaker_ppc_is_dead_battery_mode,
.exit_dead_battery_mode = numaker_ppc_exit_dead_battery_mode,
.is_vbus_source = numaker_ppc_is_vbus_source,
.is_vbus_sink = numaker_ppc_is_vbus_sink,
.set_snk_ctrl = numaker_ppc_set_snk_ctrl,
.set_src_ctrl = numaker_ppc_set_src_ctrl,
.set_vbus_discharge = numaker_ppc_set_vbus_discharge,
.is_vbus_present = numaker_ppc_is_vbus_present,
.set_event_handler = numaker_ppc_set_event_handler,
.dump_regs = numaker_ppc_dump_regs,
};
#define NUMAKER_TCPC(inst) DT_INST_PARENT(inst)
#define PPC_NUMAKER_INIT(inst) \
static const struct numaker_ppc_config numaker_ppc_config_##inst = { \
.tcpc_dev = DEVICE_DT_GET(NUMAKER_TCPC(inst)), \
}; \
\
DEVICE_DT_INST_DEFINE(inst, numaker_ppc_init, NULL, NULL, &numaker_ppc_config_##inst, \
POST_KERNEL, CONFIG_USBC_PPC_INIT_PRIORITY, \
&numaker_ppc_driver_api);
DT_INST_FOREACH_STATUS_OKAY(PPC_NUMAKER_INIT);

View file

@ -4,3 +4,4 @@ zephyr_library()
zephyr_library_sources_ifdef(CONFIG_USBC_TCPC_SHELL shell.c)
zephyr_library_sources_ifdef(CONFIG_USBC_TCPC_STM32 ucpd_stm32.c)
zephyr_library_sources_ifdef(CONFIG_USBC_TCPC_NUMAKER ucpd_numaker.c)

View file

@ -26,6 +26,7 @@ config USBC_TCPC_SHELL
Example functions are printing vbus, chip information and dumping registers.
source "drivers/usb_c/tcpc/Kconfig.tcpc_stm32"
source "drivers/usb_c/tcpc/Kconfig.tcpc_numaker"
module = USBC
module-str = usbc

View file

@ -0,0 +1,13 @@
# Nuvoton NuMaker USB-C TCPC device configuration options
# Copyright (c) 2024 Nuvoton Technology Corporation
# SPDX-License-Identifier: Apache-2.0
config USBC_TCPC_NUMAKER
bool "Nuvoton NuMaker USB-C TCPC device controller driver"
default y
select HAS_NUMAKER_ADC
select HAS_NUMAKER_TMR
depends on DT_HAS_NUVOTON_NUMAKER_TCPC_ENABLED
help
Enable USB-C TCPC support for Nuvoton NuMaker chip with UTCPD.

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Nuvoton Technology Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_DRIVERS_USBC_TCPC_UCPD_NUMAKER_H_
#define ZEPHYR_DRIVERS_USBC_TCPC_UCPD_NUMAKER_H_
#include <zephyr/drivers/usb_c/usbc_ppc.h>
#include <zephyr/drivers/usb_c/usbc_vbus.h>
/* TCPC exported for PPC */
int numaker_tcpc_ppc_is_dead_battery_mode(const struct device *dev);
int numaker_tcpc_ppc_exit_dead_battery_mode(const struct device *dev);
int numaker_tcpc_ppc_is_vbus_source(const struct device *dev);
int numaker_tcpc_ppc_is_vbus_sink(const struct device *dev);
int numaker_tcpc_ppc_set_snk_ctrl(const struct device *dev, bool enable);
int numaker_tcpc_ppc_set_src_ctrl(const struct device *dev, bool enable);
int numaker_tcpc_ppc_set_vbus_discharge(const struct device *dev, bool enable);
int numaker_tcpc_ppc_is_vbus_present(const struct device *dev);
int numaker_tcpc_ppc_set_event_handler(const struct device *dev, usbc_ppc_event_cb_t handler,
void *data);
int numaker_tcpc_ppc_dump_regs(const struct device *dev);
/* TCPC exported for VBUS */
bool numaker_tcpc_vbus_check_level(const struct device *dev, enum tc_vbus_level level);
int numaker_tcpc_vbus_measure(const struct device *dev, int *vbus_meas);
int numaker_tcpc_vbus_discharge(const struct device *dev, bool enable);
int numaker_tcpc_vbus_enable(const struct device *dev, bool enable);
#endif /* ZEPHYR_DRIVERS_USBC_TCPC_UCPD_NUMAKER_H_ */

View file

@ -3,3 +3,4 @@
zephyr_library()
zephyr_library_sources_ifdef(CONFIG_USBC_VBUS_ADC usbc_vbus_adc.c)
zephyr_library_sources_ifdef(CONFIG_USBC_VBUS_NUMAKER usbc_vbus_numaker.c)

View file

@ -17,6 +17,7 @@ config USBC_VBUS_INIT_PRIORITY
Initialization priority of the USB-C VBUS measurement drivers in POST_KERNEL.
source "drivers/usb_c/vbus/Kconfig.usbc_vbus_adc"
source "drivers/usb_c/vbus/Kconfig.numaker"
endif # USBC_VBUS_DRIVER

View file

@ -0,0 +1,11 @@
# Nuvoton NuMaker USB-C VBUS device configuration options
# Copyright (c) 2024 Nuvoton Technology Corporation
# SPDX-License-Identifier: Apache-2.0
config USBC_VBUS_NUMAKER
bool "Nuvoton NuMaker USB-C VBUS"
default y
depends on DT_HAS_NUVOTON_NUMAKER_VBUS_ENABLED && USBC_TCPC_NUMAKER
help
Enable USB-C VBUS support for Nuvoton NuMaker chip with UTCPD.

View file

@ -0,0 +1,131 @@
/*
* Copyright (c) 2024 Nuvoton Technology Corporation.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT nuvoton_numaker_vbus
#include <zephyr/kernel.h>
#include <zephyr/drivers/usb_c/usbc_vbus.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/drivers/clock_control/clock_control_numaker.h>
#include <zephyr/drivers/reset.h>
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(vbus_numaker, CONFIG_USBC_LOG_LEVEL);
#include <soc.h>
#include <NuMicro.h>
#include "../tcpc/ucpd_numaker.h"
/* Implementation notes on NuMaker TCPC/PPC/VBUS
*
* PPC and VBUS rely on TCPC/UTCPD and are just pseudo. They are completely
* implemented in TCPC/UTCPD.
*/
/**
* @brief Immutable device context
*/
struct numaker_vbus_config {
const struct device *tcpc_dev;
};
/**
* @brief Initializes the usb-c vbus driver
*
* @retval 0 on success
* @retval -ENODEV if dependent TCPC device is not ready
*/
static int numaker_vbus_init(const struct device *dev)
{
const struct numaker_vbus_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
/* Rely on TCPC */
if (!device_is_ready(tcpc_dev)) {
LOG_ERR("TCPC device not ready");
return -ENODEV;
}
return 0;
}
/**
* @brief Checks if VBUS is at a particular level
*
* @retval true if VBUS is at the level voltage
* @retval false if VBUS is not at that level voltage
*/
static bool numaker_vbus_check_level(const struct device *dev, enum tc_vbus_level level)
{
const struct numaker_vbus_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_vbus_check_level(tcpc_dev, level);
}
/**
* @brief Reads and returns VBUS measured in mV
*
* @retval 0 on success
* @retval -EIO on failure
*/
static int numaker_vbus_measure(const struct device *dev, int *vbus_meas)
{
const struct numaker_vbus_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_vbus_measure(tcpc_dev, vbus_meas);
}
/**
* @brief Controls a pin that discharges VBUS
*
* @retval 0 on success
* @retval -EIO on failure
*/
static int numaker_vbus_discharge(const struct device *dev, bool enable)
{
const struct numaker_vbus_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_vbus_discharge(tcpc_dev, enable);
}
/**
* @brief Controls a pin that enables VBUS measurments
*
* @retval 0 on success
* @retval -EIO on failure
*/
static int numaker_vbus_enable(const struct device *dev, bool enable)
{
const struct numaker_vbus_config *const config = dev->config;
const struct device *tcpc_dev = config->tcpc_dev;
return numaker_tcpc_vbus_enable(tcpc_dev, enable);
}
static const struct usbc_vbus_driver_api numaker_vbus_driver_api = {
.check_level = numaker_vbus_check_level,
.measure = numaker_vbus_measure,
.discharge = numaker_vbus_discharge,
.enable = numaker_vbus_enable,
};
#define NUMAKER_TCPC(inst) DT_INST_PARENT(inst)
#define VBUS_NUMAKER_INIT(inst) \
static const struct numaker_vbus_config numaker_vbus_config_##inst = { \
.tcpc_dev = DEVICE_DT_GET(NUMAKER_TCPC(inst)), \
}; \
\
DEVICE_DT_INST_DEFINE(inst, numaker_vbus_init, NULL, NULL, &numaker_vbus_config_##inst, \
POST_KERNEL, CONFIG_USBC_VBUS_INIT_PRIORITY, \
&numaker_vbus_driver_api);
DT_INST_FOREACH_STATUS_OKAY(VBUS_NUMAKER_INIT);

View file

@ -394,6 +394,33 @@
clocks = <&pcc NUMAKER_WWDT_MODULE NUMAKER_CLK_CLKSEL1_WWDTSEL_LIRC 0>;
status = "disabled";
};
tcpc0: utcpd@400c6000 {
compatible = "nuvoton,numaker-tcpc";
reg = <0x400c6000 0x1000>,
<0x40043000 0x1000>,
<0x40050000 0x1000>;
reg-names = "utcpd", "eadc", "timer";
interrupts = <108 0>;
interrupt-names = "utcpd";
resets = <&rst NUMAKER_UTCPD0_RST>,
<&rst NUMAKER_TMR0_RST>;
reset-names = "utcpd", "timer";
clocks = <&pcc NUMAKER_UTCPD0_MODULE 0 0>,
<&pcc NUMAKER_TMR0_MODULE NUMAKER_CLK_CLKSEL1_TMR0SEL_HIRC 0>;
clock-names = "utcpd", "timer";
status = "disabled";
vbus0: vbus0 {
compatible = "nuvoton,numaker-vbus";
status = "disabled";
};
ppc0: ppc0 {
compatible = "nuvoton,numaker-ppc";
status = "disabled";
};
};
};
};

View file

@ -0,0 +1,8 @@
# Copyright (c) 2024 Nuvoton Technology Corporation
# SPDX-License-Identifier: Apache-2.0
description: Nuvoton NuMaker USB Type-C power path controller
compatible: "nuvoton,numaker-ppc"
include: [base.yaml]

View file

@ -0,0 +1,149 @@
# Copyright (c) 2024 Nuvoton Technology Corporation
# SPDX-License-Identifier: Apache-2.0
description: Nuvoton NuMaker USB Type-C port controller
compatible: "nuvoton,numaker-tcpc"
include: [base.yaml, reset-device.yaml, pinctrl-device.yaml]
properties:
reg:
required: true
interrupts:
required: true
resets:
required: true
clocks:
required: true
vconn-overcurrent-event-polarity:
type: string
description: |
Polarity of VCONN overcurrent event
enum:
- "low-active"
- "high-active"
vconn-discharge-polarity:
type: string
description: |
Polarity of VCONN discharge
enum:
- "low-active"
- "high-active"
vconn-enable-polarity:
type: string
description: |
Polarity of VCONN enable
enum:
- "low-active"
- "high-active"
vbus-overcurrent-event-polarity:
type: string
description: |
Polarity of VBUS overcurrent event
enum:
- "low-active"
- "high-active"
vbus-forceoff-event-polarity:
type: string
description: |
Polarity of VBUS force-off event
enum:
- "low-active"
- "high-active"
frs-tx-polarity:
type: string
description: |
Polarity of fast role swap tx
enum:
- "low-active"
- "high-active"
vbus-discharge-enable-polarity:
type: string
description: |
Polarity of VBUS discharge enable
enum:
- "low-active"
- "high-active"
vbus-sink-enable-polarity:
type: string
description: |
Polarity of VBUS sink enable
enum:
- "low-active"
- "high-active"
vbus-source-enable-polarity:
type: string
description: |
Polarity of VBUS source enable
enum:
- "low-active"
- "high-active"
vbus-divide:
type: string
required: true
description: |
VBUS measurement divider
enum:
- "no-divide"
- "divide-10"
- "divide-20"
dead-battery:
type: boolean
description: |
Determine if USB-C Dead Battery pull-down resistor should be
applied to the CC lines.
pinctrl-0:
required: true
pinctrl-names:
required: true
gpios:
type: phandle-array
required: true
gpio-names:
type: string-array
required: true
description: |
Valid names of GPIO:
"vbus-detect": GPIO for VBUS detect (must)
"vbus-discharge": GPIO for VBUS discharge (option)
"vconn-discharge": GPIO for VCONN discharge (option)
io-channels:
type: phandle-array
description: |
EADC channels for measuring VBUS/VCONN voltage
io-channel-names:
type: string-array
description: |
Valid names of EADC channels:
"chn-vbus": EADC channel for measuring VBUS voltage (option)
"chn-vconn": EADC channel for measuring VCONN voltage (option)
adc-measure-timer-trigger-rate:
type: int
description: |
Rate of timer-triggered EADC measurement (Hz).
This is ignored when none of above is specified.
The default is chosen by following BSP sample,
and is to update UTCPD in a proper rate.
default: 100

View file

@ -0,0 +1,8 @@
# Copyright (c) 2024 Nuvoton Technology Corporation
# SPDX-License-Identifier: Apache-2.0
description: Nuvoton NuMaker USB Type-C VBUS controller
compatible: "nuvoton,numaker-vbus"
include: [base.yaml]

View file

@ -79,4 +79,8 @@ menu "Nuvoton NuMaker drivers"
bool "NuMaker RTC"
help
Enable Nuvoton RTC HAL module driver
config HAS_NUMAKER_TMR
bool "NuMaker Timer"
help
Enable Nuvoton Timer HAL module driver
endmenu