From cd1d7e4bb823832fbcb461e7a6f3a8922b548ca5 Mon Sep 17 00:00:00 2001 From: Raul Pando Date: Tue, 20 Dec 2022 20:05:21 +0000 Subject: [PATCH] drivers: usb: make USB VBUS sensing configurable for STM32 devices Vbus detection is required by self-powered, with external non-USB supply, devices to have an operational USB peripheral. The voltage sensing, when enabled, is performed via a designated pin (PA9 is the most common). For greater details see section 2.6, ST application note AN4879. Signed-off-by: Raul Pando --- drivers/usb/device/usb_dc_stm32.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/drivers/usb/device/usb_dc_stm32.c b/drivers/usb/device/usb_dc_stm32.c index eccec4cd293..653255716f7 100644 --- a/drivers/usb/device/usb_dc_stm32.c +++ b/drivers/usb/device/usb_dc_stm32.c @@ -36,15 +36,27 @@ LOG_MODULE_REGISTER(usb_dc_stm32); #error "Only one interface should be enabled at a time, OTG FS or OTG HS" #endif +/* + * Vbus sensing is determined based on the presence of the hardware detection + * pin(s) in the device tree. E.g: pinctrl-0 = <&usb_otg_fs_vbus_pa9 ...>; + * + * The detection pins are dependent on the enabled USB driver and the physical + * interface(s) offered by the hardware. These are mapped to PA9 and/or PB13 + * (subject to MCU), being the former the most widespread option. + */ #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otghs) -#define DT_DRV_COMPAT st_stm32_otghs -#define USB_IRQ_NAME otghs +#define DT_DRV_COMPAT st_stm32_otghs +#define USB_IRQ_NAME otghs +#define USB_VBUS_SENSING (DT_NODE_EXISTS(DT_CHILD(DT_NODELABEL(pinctrl), usb_otg_hs_vbus_pa9)) || \ + DT_NODE_EXISTS(DT_CHILD(DT_NODELABEL(pinctrl), usb_otg_hs_vbus_pb13))) #elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_otgfs) -#define DT_DRV_COMPAT st_stm32_otgfs -#define USB_IRQ_NAME otgfs +#define DT_DRV_COMPAT st_stm32_otgfs +#define USB_IRQ_NAME otgfs +#define USB_VBUS_SENSING DT_NODE_EXISTS(DT_CHILD(DT_NODELABEL(pinctrl), usb_otg_fs_vbus_pa9)) #elif DT_HAS_COMPAT_STATUS_OKAY(st_stm32_usb) -#define DT_DRV_COMPAT st_stm32_usb -#define USB_IRQ_NAME usb +#define DT_DRV_COMPAT st_stm32_usb +#define USB_IRQ_NAME usb +#define USB_VBUS_SENSING false #endif #define USB_BASE_ADDRESS DT_INST_REG_ADDR(0) @@ -402,7 +414,7 @@ static int usb_dc_stm32_init(void) usb_dc_stm32_state.pcd.Init.phy_itface = PCD_PHY_EMBEDDED; #endif usb_dc_stm32_state.pcd.Init.ep0_mps = USB_OTG_MAX_EP0_SIZE; - usb_dc_stm32_state.pcd.Init.vbus_sensing_enable = DISABLE; + usb_dc_stm32_state.pcd.Init.vbus_sensing_enable = USB_VBUS_SENSING ? ENABLE : DISABLE; #ifndef CONFIG_SOC_SERIES_STM32F1X usb_dc_stm32_state.pcd.Init.dma_enable = DISABLE;