From 052eb6180d25f42de0b25b06b5fe0cef88441ae4 Mon Sep 17 00:00:00 2001 From: Jeppe Odgaard Date: Wed, 22 May 2024 08:46:56 +0200 Subject: [PATCH] drivers: adc: adc_ad559x: fix 5593 adc read The AD5593 conversion result also contains the channel read and not just a value: | adc channel (3 bits) | adc value (12 bits) | This value was not removed from the result before using it as an adc value. Reuse the AD5592 code to fix the issue. Signed-off-by: Jeppe Odgaard --- drivers/adc/adc_ad559x.c | 49 +++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/drivers/adc/adc_ad559x.c b/drivers/adc/adc_ad559x.c index 21ee1e62e18..b0e7bb95810 100644 --- a/drivers/adc/adc_ad559x.c +++ b/drivers/adc/adc_ad559x.c @@ -130,9 +130,6 @@ static int adc_ad559x_read_channel(const struct device *dev, uint8_t channel, ui if (ret < 0) { return ret; } - - *result = sys_get_be16((uint8_t *)&val); - } else { /* * Invalid data: @@ -145,31 +142,31 @@ static int adc_ad559x_read_channel(const struct device *dev, uint8_t channel, ui if (ret < 0) { return ret; } - - val = sys_be16_to_cpu(val); - - /* - * Invalid data: - * See "ADC section" in "Theory of operation" chapter. - * Valid ADC result has MSB bit set to 0. - */ - if ((val & AD559X_ADC_RES_IND_BIT) != 0) { - return -EAGAIN; - } - - /* - * Invalid channel converted: - * See "ADC section" in "Theory of operation" chapter. - * Conversion result contains channel number which should match requested channel. - */ - conv_channel = FIELD_GET(AD559X_ADC_RES_CHAN_MASK, val); - if (conv_channel != channel) { - return -EIO; - } - - *result = val & AD559X_ADC_RES_VAL_MASK; } + val = sys_be16_to_cpu(val); + + /* + * Invalid data: + * See AD5592 "ADC section" in "Theory of operation" chapter. + * Valid ADC result has MSB bit set to 0. + */ + if ((val & AD559X_ADC_RES_IND_BIT) != 0) { + return -EAGAIN; + } + + /* + * Invalid channel converted: + * See AD5592 "ADC section" in "Theory of operation" chapter. + * Conversion result contains channel number which should match requested channel. + */ + conv_channel = FIELD_GET(AD559X_ADC_RES_CHAN_MASK, val); + if (conv_channel != channel) { + return -EIO; + } + + *result = val & AD559X_ADC_RES_VAL_MASK; + return 0; }