From 81fe95c85f2d49a8991203a570cb7999fa2c267b Mon Sep 17 00:00:00 2001 From: Jishnu Prakash Date: Wed, 2 Sep 2020 14:25:04 +0530 Subject: [PATCH] iio: adc: Correct smb1398 scaling function Correct scaling function for SMB1398 to ensure temperature is calculated accurately. Change-Id: I7ec497ef9d1171da7a08a97730912727c583830a Signed-off-by: Jishnu Prakash [quic_collinsd@quicinc.com: changed prescale "num" and "den" to "numerator" and "denominator" respectively] Signed-off-by: David Collins --- drivers/iio/adc/qcom-vadc-common.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/drivers/iio/adc/qcom-vadc-common.c b/drivers/iio/adc/qcom-vadc-common.c index 99ef49e191a4..8901c385a24b 100644 --- a/drivers/iio/adc/qcom-vadc-common.c +++ b/drivers/iio/adc/qcom-vadc-common.c @@ -929,9 +929,26 @@ static int qcom_vadc_scale_hw_smb1398_temp( const struct adc5_data *data, u16 adc_code, int *result_mdec) { - *result_mdec = qcom_vadc_scale_code_voltage_factor(adc_code * 100, - prescale, data, PMIC5_SMB1398_TEMP_SCALE_FACTOR); - *result_mdec = PMIC5_SMB1398_TEMP_CONSTANT - *result_mdec; + s64 voltage = 0, adc_vdd_ref_mv = 1875; + u64 temp; + + if (adc_code > VADC5_MAX_CODE) + adc_code = 0; + + /* (ADC code * vref_vadc (1.875V)) / full_scale_code */ + voltage = (s64) adc_code * adc_vdd_ref_mv * 1000; + voltage = div64_s64(voltage, data->full_scale_code_volt); + if (voltage > 0) { + temp = voltage * prescale->denominator; + temp *= 100; + do_div(temp, prescale->numerator * PMIC5_SMB1398_TEMP_SCALE_FACTOR); + voltage = temp; + } else { + voltage = 0; + } + + voltage = voltage - PMIC5_SMB1398_TEMP_CONSTANT; + *result_mdec = voltage; return 0; }