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 <ycsin@meta.com>
This commit is contained in:
Yong Cong Sin 2024-05-17 14:02:28 +08:00 committed by Carles Cufí
commit b0394425e1
2 changed files with 12 additions and 2 deletions

View file

@ -7,6 +7,7 @@ config STM32_VBAT
bool "STM32 Vbat Sensor" bool "STM32 Vbat Sensor"
default y default y
depends on DT_HAS_ST_STM32_VBAT_ENABLED depends on DT_HAS_ST_STM32_VBAT_ENABLED
depends on DT_HAS_ST_STM32_ADC_ENABLED
depends on SOC_FAMILY_STM32 && !SOC_SERIES_STM32F1X depends on SOC_FAMILY_STM32 && !SOC_SERIES_STM32F1X
select ADC select ADC
help help

View file

@ -101,6 +101,11 @@ static int stm32_vbat_init(const struct device *dev)
k_mutex_init(&data->mutex); k_mutex_init(&data->mutex);
if (data->adc == NULL) {
LOG_ERR("ADC is not enabled");
return -ENODEV;
}
if (!device_is_ready(data->adc)) { if (!device_is_ready(data->adc)) {
LOG_ERR("Device %s is not ready", data->adc->name); LOG_ERR("Device %s is not ready", data->adc->name);
return -ENODEV; return -ENODEV;
@ -116,9 +121,13 @@ static int stm32_vbat_init(const struct device *dev)
return 0; 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) \ #define STM32_VBAT_DEFINE(inst) \
static struct stm32_vbat_data stm32_vbat_dev_data_##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_base = (ADC_TypeDef *)DT_REG_ADDR(DT_INST_IO_CHANNELS_CTLR(0)), \
.adc_cfg = { \ .adc_cfg = { \
.gain = ADC_GAIN_1, \ .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, \ SENSOR_DEVICE_DT_INST_DEFINE(inst, stm32_vbat_init, NULL, \
&stm32_vbat_dev_data_##inst, &stm32_vbat_dev_config_##inst, \ &stm32_vbat_dev_data_##inst, &stm32_vbat_dev_config_##inst, \
POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \ POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, \
&stm32_vbat_driver_api); \ &stm32_vbat_driver_api);
DT_INST_FOREACH_STATUS_OKAY(STM32_VBAT_DEFINE) DT_INST_FOREACH_STATUS_OKAY(STM32_VBAT_DEFINE)