drivers: adc: nrfx_saadc: Add validation of channel configuration

On nrf54h20 there are additional analog pins (AIN8+). When differential
mode is used they must not be mixed with AIN0-AIN7. Add build time
validation which detects if configuration is invalid.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruściński 2025-03-20 13:22:10 +01:00 committed by Benjamin Cabé
commit 2f7d799c99

View file

@ -318,8 +318,13 @@ static int adc_nrfx_channel_setup(const struct device *dev,
}
if (config.mode == NRF_SAADC_MODE_DIFFERENTIAL) {
#if defined(CONFIG_NRF_PLATFORM_HALTIUM)
if ((input_negative > NRF_SAADC_AIN7) !=
(channel_cfg->input_positive > NRF_SAADC_AIN7)) {
#else
if (input_negative > NRF_SAADC_AIN7 ||
input_negative < NRF_SAADC_AIN0) {
#endif
return -EINVAL;
}
@ -719,6 +724,27 @@ static DEVICE_API(adc, adc_nrfx_driver_api) = {
#endif
};
#if defined(CONFIG_NRF_PLATFORM_HALTIUM)
/* AIN8-AIN14 inputs are on 3v3 GPIO port and they cannot be mixed with other
* analog inputs (from 1v8 ports) in differential mode.
*/
#define CH_IS_3V3(val) (val >= NRF_SAADC_AIN8)
#define MIXED_3V3_1V8_INPUTS(node) \
(DT_NODE_HAS_PROP(node, zephyr_input_negative) && \
(CH_IS_3V3(DT_PROP_OR(node, zephyr_input_negative, 0)) != \
CH_IS_3V3(DT_PROP_OR(node, zephyr_input_positive, 0))))
#else
#define MIXED_3V3_1V8_INPUTS(node) false
#endif
#define VALIDATE_CHANNEL_CONFIG(node) \
BUILD_ASSERT(MIXED_3V3_1V8_INPUTS(node) == false, \
"1v8 inputs cannot be mixed with 3v3 inputs");
/* Validate configuration of all channels. */
#define VALIDATE_CHANNELS_CONFIG(inst) DT_FOREACH_CHILD(DT_DRV_INST(inst), VALIDATE_CHANNEL_CONFIG)
/*
* There is only one instance on supported SoCs, so inst is guaranteed
* to be 0 if any instance is okay. (We use adc_0 above, so the driver
@ -731,6 +757,7 @@ static DEVICE_API(adc, adc_nrfx_driver_api) = {
#define SAADC_INIT(inst) \
BUILD_ASSERT((inst) == 0, \
"multiple instances not supported"); \
VALIDATE_CHANNELS_CONFIG(inst) \
PM_DEVICE_DT_INST_DEFINE(0, saadc_pm_hook, 1); \
DEVICE_DT_INST_DEFINE(0, \
init_saadc, \