Hide the details of how to convert values read from TSENS HW to mCelsius
behind a function. All versions of the IP can be supported as a result.

Signed-off-by: Amit Kucheria <amit.kuche...@linaro.org>
---
 drivers/thermal/qcom/tsens-common.c | 34 ++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 10 deletions(-)

diff --git a/drivers/thermal/qcom/tsens-common.c 
b/drivers/thermal/qcom/tsens-common.c
index 7ab2e740a1da..13a875b99094 100644
--- a/drivers/thermal/qcom/tsens-common.c
+++ b/drivers/thermal/qcom/tsens-common.c
@@ -84,13 +84,35 @@ static inline int code_to_degc(u32 adc_code, const struct 
tsens_sensor *s)
        return degc;
 }
 
+/**
+ * tsens_hw_to_mC - Return properly sign extended temperature in mCelsius,
+ * whether in ADC code or deciCelsius depending on IP version.
+ * This function handles the different widths of the signed integer across IPs.
+ */
+static int tsens_hw_to_mC(char *str, struct tsens_sensor *s, int field, int 
temp)
+{
+       struct tsens_priv *priv = s->priv;
+       u32 mask;
+
+       if (priv->feat->adc) {
+               /* Convert temperature from ADC code to milliCelsius */
+               return code_to_degc(temp, s) * 1000;
+       } else {
+               mask = GENMASK(priv->fields[field].msb,
+                              priv->fields[field].lsb) >> 
priv->fields[field].lsb;
+               dev_dbg(priv->dev, "%s: mask: %d\n", str, fls(mask));
+               /* Convert temperature from deciCelsius to milliCelsius */
+               return sign_extend32(temp, fls(mask) - 1) * 100;
+       }
+}
+
 int get_temp_tsens_valid(struct tsens_sensor *s, int *temp)
 {
        struct tsens_priv *priv = s->priv;
        int hw_id = s->hw_id;
        u32 temp_idx = LAST_TEMP_0 + hw_id;
        u32 valid_idx = VALID_0 + hw_id;
-       u32 last_temp = 0, valid, mask;
+       u32 last_temp = 0, valid;
        int ret;
 
        ret = regmap_field_read(priv->rf[valid_idx], &valid);
@@ -112,15 +134,7 @@ int get_temp_tsens_valid(struct tsens_sensor *s, int *temp)
        if (ret)
                return ret;
 
-       if (priv->feat->adc) {
-               /* Convert temperature from ADC code to milliCelsius */
-               *temp = code_to_degc(last_temp, s) * 1000;
-       } else {
-               mask = GENMASK(priv->fields[LAST_TEMP_0].msb,
-                              priv->fields[LAST_TEMP_0].lsb);
-               /* Convert temperature from deciCelsius to milliCelsius */
-               *temp = sign_extend32(last_temp, fls(mask) - 1) * 100;
-       }
+       *temp = tsens_hw_to_mC("get_temp", s, LAST_TEMP_0, last_temp);
 
        return 0;
 }
-- 
2.17.1

Reply via email to