From b0394425e1fd0e1515f5999cfe1c90706b6d4e96 Mon Sep 17 00:00:00 2001 From: Yong Cong Sin Date: Fri, 17 May 2024 14:02:28 +0800 Subject: [PATCH] drivers: sensor: st: vbat: check for ADC nodes The vbat driver requires the adc node to be enabled: ```c .adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst)) ``` Update its Kconfig to depend on `DT_HAS_ST_STM32_ADC_ENABLED`, which is the `"st,stm32-adc"` compat that all ST ADC bindings include, this will guarantee that at least one ADC node is enabled, but not necessarily the ADC used by the vbat node. To make sure that it at least compiles, we init the `adc` pointer only if the specified ADC node is enabled, otherwise it will points to `NULL`. Finally, check if the `adc` points to `NULL` in `stm32_vbat_init`. We are not relying on the existing `device_is_ready` check because `DEVICE_DT_GET` will not return `NULL` if the ADC is enabled. `adc == NULL` means that the ADC node is not enabled in the devicetree. Signed-off-by: Yong Cong Sin --- drivers/sensor/st/stm32_vbat/Kconfig | 1 + drivers/sensor/st/stm32_vbat/stm32_vbat.c | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/drivers/sensor/st/stm32_vbat/Kconfig b/drivers/sensor/st/stm32_vbat/Kconfig index 2638269224d..f5430c91a6b 100644 --- a/drivers/sensor/st/stm32_vbat/Kconfig +++ b/drivers/sensor/st/stm32_vbat/Kconfig @@ -7,6 +7,7 @@ config STM32_VBAT bool "STM32 Vbat Sensor" default y depends on DT_HAS_ST_STM32_VBAT_ENABLED + depends on DT_HAS_ST_STM32_ADC_ENABLED depends on SOC_FAMILY_STM32 && !SOC_SERIES_STM32F1X select ADC help diff --git a/drivers/sensor/st/stm32_vbat/stm32_vbat.c b/drivers/sensor/st/stm32_vbat/stm32_vbat.c index 84511c1a4cc..352c5a22920 100644 --- a/drivers/sensor/st/stm32_vbat/stm32_vbat.c +++ b/drivers/sensor/st/stm32_vbat/stm32_vbat.c @@ -101,6 +101,11 @@ static int stm32_vbat_init(const struct device *dev) k_mutex_init(&data->mutex); + if (data->adc == NULL) { + LOG_ERR("ADC is not enabled"); + return -ENODEV; + } + if (!device_is_ready(data->adc)) { LOG_ERR("Device %s is not ready", data->adc->name); return -ENODEV; @@ -116,9 +121,13 @@ static int stm32_vbat_init(const struct device *dev) return 0; } +#define STM32_VBAT_GET_ADC_OR_NULL(inst) \ + COND_CODE_1(DT_NODE_HAS_STATUS(DT_INST_IO_CHANNELS_CTLR(inst), okay), \ + (DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst))), (NULL)) + #define STM32_VBAT_DEFINE(inst) \ static struct stm32_vbat_data stm32_vbat_dev_data_##inst = { \ - .adc = DEVICE_DT_GET(DT_INST_IO_CHANNELS_CTLR(inst)), \ + .adc = STM32_VBAT_GET_ADC_OR_NULL(inst), \ .adc_base = (ADC_TypeDef *)DT_REG_ADDR(DT_INST_IO_CHANNELS_CTLR(0)), \ .adc_cfg = { \ .gain = ADC_GAIN_1, \ @@ -136,6 +145,6 @@ static int stm32_vbat_init(const struct device *dev) SENSOR_DEVICE_DT_INST_DEFINE(inst, stm32_vbat_init, NULL, \ &stm32_vbat_dev_data_##inst, &stm32_vbat_dev_config_##inst, \ POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \ - &stm32_vbat_driver_api); \ + &stm32_vbat_driver_api); DT_INST_FOREACH_STATUS_OKAY(STM32_VBAT_DEFINE)