drivers: hwmon: rp1-adc: check conversion validity before supplying value
authorJonathan Bell <jonathan@raspberrypi.com>
Thu, 4 May 2023 14:48:53 +0000 (15:48 +0100)
committerDom Cobley <popcornmix@gmail.com>
Mon, 19 Feb 2024 11:34:51 +0000 (11:34 +0000)
The SAR ADC architecture may complete a conversion but instability in the
comparator can corrupt the result. Such corruption is signalled in the CS
ERR bit, asserted alongside each conversion result.

Signed-off-by: Jonathan Bell <jonathan@raspberrypi.com>
drivers/hwmon/rp1-adc.c

index 4d1734a..f1ec4c3 100644 (file)
@@ -97,8 +97,14 @@ static int rp1_adc_read(struct rp1_adc_data *data,
               data->base + RP1_ADC_RWTYPE_SET + RP1_ADC_CS);
 
        ret = rp1_adc_ready_wait(data);
-       if (!ret)
-               *val = readl(data->base + RP1_ADC_RESULT);
+       if (ret)
+               return ret;
+
+       /* Asserted if the completed conversion had a convergence error */
+       if (readl(data->base + RP1_ADC_CS) & RP1_ADC_CS_ERR)
+               return -EIO;
+
+       *val = readl(data->base + RP1_ADC_RESULT);
 
        spin_unlock(&data->lock);