Skip to content

Commit bd93ee3

Browse files
idlethreaddlezcano
authored andcommitted
drivers: thermal: tsens: Create function to return sign-extended temperature
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 <[email protected]> Reviewed-by: Stephen Boyd <[email protected]> Signed-off-by: Daniel Lezcano <[email protected]> Link: https://lore.kernel.org/r/0689917475cf83b7e01f6978504fd37352a5e3ca.1572526427.git.amit.kucheria@linaro.org
1 parent a877e76 commit bd93ee3

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

drivers/thermal/qcom/tsens-common.c

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,46 @@ static inline int code_to_degc(u32 adc_code, const struct tsens_sensor *s)
8484
return degc;
8585
}
8686

87+
/**
88+
* tsens_hw_to_mC - Return sign-extended temperature in mCelsius.
89+
* @s: Pointer to sensor struct
90+
* @field: Index into regmap_field array pointing to temperature data
91+
*
92+
* This function handles temperature returned in ADC code or deciCelsius
93+
* depending on IP version.
94+
*
95+
* Return: Temperature in milliCelsius on success, a negative errno will
96+
* be returned in error cases
97+
*/
98+
static int tsens_hw_to_mC(struct tsens_sensor *s, int field)
99+
{
100+
struct tsens_priv *priv = s->priv;
101+
u32 resolution;
102+
u32 temp = 0;
103+
int ret;
104+
105+
resolution = priv->fields[LAST_TEMP_0].msb -
106+
priv->fields[LAST_TEMP_0].lsb;
107+
108+
ret = regmap_field_read(priv->rf[field], &temp);
109+
if (ret)
110+
return ret;
111+
112+
/* Convert temperature from ADC code to milliCelsius */
113+
if (priv->feat->adc)
114+
return code_to_degc(temp, s) * 1000;
115+
116+
/* deciCelsius -> milliCelsius along with sign extension */
117+
return sign_extend32(temp, resolution) * 100;
118+
}
119+
87120
int get_temp_tsens_valid(struct tsens_sensor *s, int *temp)
88121
{
89122
struct tsens_priv *priv = s->priv;
90123
int hw_id = s->hw_id;
91124
u32 temp_idx = LAST_TEMP_0 + hw_id;
92125
u32 valid_idx = VALID_0 + hw_id;
93-
u32 last_temp = 0, valid, mask;
126+
u32 valid;
94127
int ret;
95128

96129
ret = regmap_field_read(priv->rf[valid_idx], &valid);
@@ -108,19 +141,7 @@ int get_temp_tsens_valid(struct tsens_sensor *s, int *temp)
108141
}
109142

110143
/* Valid bit is set, OK to read the temperature */
111-
ret = regmap_field_read(priv->rf[temp_idx], &last_temp);
112-
if (ret)
113-
return ret;
114-
115-
if (priv->feat->adc) {
116-
/* Convert temperature from ADC code to milliCelsius */
117-
*temp = code_to_degc(last_temp, s) * 1000;
118-
} else {
119-
mask = GENMASK(priv->fields[LAST_TEMP_0].msb,
120-
priv->fields[LAST_TEMP_0].lsb);
121-
/* Convert temperature from deciCelsius to milliCelsius */
122-
*temp = sign_extend32(last_temp, fls(mask) - 1) * 100;
123-
}
144+
*temp = tsens_hw_to_mC(s, temp_idx);
124145

125146
return 0;
126147
}

0 commit comments

Comments
 (0)