Skip to content

Commit

Permalink
drivers: adc: adc_ad559x: improve adc read
Browse files Browse the repository at this point in the history
Use information encoded in conversion result value to validate it.
Check if MSB bit is set to zero.
Check if channel number included in the result matches channel
number selected for conversion.
Use bitmask to extract converted value instead of math calculations.

(cherry picked from commit 71e7a77)

Original-Signed-off-by: Lukasz Madej <l.madej@grinn-global.com>
GitOrigin-RevId: 71e7a77
Change-Id: I023f3ecd1127f41df288e0ee89512b571cb14f5f
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/zephyr/+/5488598
Commit-Queue: Dawid Niedźwiecki <dawidn@google.com>
Reviewed-by: Dawid Niedźwiecki <dawidn@google.com>
Tested-by: ChromeOS Prod (Robot) <chromeos-ci-prod@chromeos-bot.iam.gserviceaccount.com>
Tested-by: Dawid Niedźwiecki <dawidn@google.com>
  • Loading branch information
LukaszMadejGrinn authored and Chromeos LUCI committed Apr 25, 2024
1 parent 43ec3ea commit 2444353
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions drivers/adc/adc_ad559x.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ LOG_MODULE_REGISTER(adc_ad559x, CONFIG_ADC_LOG_LEVEL);
#define AD559X_ADC_RESOLUTION 12U
#define AD559X_ADC_VREF_MV 2500U

#define AD559X_ADC_RES_IND_BIT BIT(15)
#define AD559X_ADC_RES_CHAN_MASK GENMASK(14, 12)
#define AD559X_ADC_RES_VAL_MASK GENMASK(11, 0)

struct adc_ad559x_config {
const struct device *mfd_dev;
};
Expand Down Expand Up @@ -103,6 +107,7 @@ static int adc_ad559x_read_channel(const struct device *dev, uint8_t channel, ui
{
const struct adc_ad559x_config *config = dev->config;
uint16_t val;
uint8_t conv_channel;
int ret;

/* Select channel */
Expand Down Expand Up @@ -142,11 +147,27 @@ static int adc_ad559x_read_channel(const struct device *dev, uint8_t channel, ui
}

val = sys_be16_to_cpu(val);
if (channel >= 1) {
val -= channel * BIT(AD559X_ADC_RESOLUTION);

/*
* 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;
*result = val & AD559X_ADC_RES_VAL_MASK;
}

return 0;
Expand Down

0 comments on commit 2444353

Please sign in to comment.