Skip to contentSkip to conversation

hw/adc: add default vref and defend against DIV0 for npcm7xx

PATCH New Alex Bennée 1 participant 1 message Sep 8

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/adc: add default vref and defend against DIV0 for npcm7xx

Googling seems to imply vref is usually expected to be somewhere between 1.6V and 2.5V. For lack of any better idea default it to the internal reference voltage. While at it guard against div0 which in the real world is likely going to saturate the result.

---
 hw/adc/npcm7xx_adc.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
hw/adc/npcm7xx_adc.c +7 -3
index 3584c27c75a..54d3cfaa813 100644
@@ -57,11 +57,14 @@ static uint32_t npcm7xx_adc_convert(uint32_t input, uint32_t ref)
 {
     uint32_t result;
 
-    result = input * (NPCM7XX_ADC_MAX_RESULT + 1) / ref;
-    if (result > NPCM7XX_ADC_MAX_RESULT) {
+    if (ref) {
+        result = input * (NPCM7XX_ADC_MAX_RESULT + 1) / ref;
+        if (result > NPCM7XX_ADC_MAX_RESULT) {
+            result = NPCM7XX_ADC_MAX_RESULT;
+        }
+    } else {
         result = NPCM7XX_ADC_MAX_RESULT;
     }
-
     return result;
 }
 
@@ -244,6 +247,7 @@ static void npcm7xx_adc_init(Object *obj)
         object_property_add_uint32_ptr(obj, "adci[*]",
                 &s->adci[i], OBJ_PROP_FLAG_READWRITE);
     }
+    s->vref = NPCM7XX_ADC_DEFAULT_IREF;
     object_property_add_uint32_ptr(obj, "vref",
             &s->vref, OBJ_PROP_FLAG_WRITE);
     npcm7xx_adc_calibrate(s);
Signature · 1 line
2.47.3