Commit dbdaa582 authored by Amit Kucheria's avatar Amit Kucheria Committed by Eduardo Valentin
Browse files

drivers: thermal: tsens: simplify get_temp_tsens_v2 routine



The current implementation is based on an algorithm published in the
docs. Instead of reading the temperature thrice w/o any explanation,
improve the algorithm.

This will become the basis for a common get_temp routine in the future.

Signed-off-by: default avatarAmit Kucheria <amit.kucheria@linaro.org>
Signed-off-by: default avatarEduardo Valentin <edubezval@gmail.com>
parent 1b6e3e51
Loading
Loading
Loading
Loading
+17 −38
Original line number Original line Diff line number Diff line
@@ -25,58 +25,37 @@
#define TM_Sn_STATUS_OFF		0x00a0
#define TM_Sn_STATUS_OFF		0x00a0
#define TM_TRDY_OFF			0x00e4
#define TM_TRDY_OFF			0x00e4


#define LAST_TEMP_MASK		0xfff
static int get_temp_tsens_v2(struct tsens_priv *priv, int i, int *temp)

static int get_temp_tsens_v2(struct tsens_priv *priv, int id, int *temp)
{
{
	struct tsens_sensor *s = &priv->sensor[id];
	struct tsens_sensor *s = &priv->sensor[i];
	u32 temp_idx = LAST_TEMP_0 + s->hw_id;
	u32 temp_idx = LAST_TEMP_0 + s->hw_id;
	u32 valid_idx = VALID_0 + s->hw_id;
	u32 valid_idx = VALID_0 + s->hw_id;
	u32 last_temp = 0, last_temp2 = 0, last_temp3 = 0, valid;
	u32 last_temp = 0, valid, mask;
	int ret;
	int ret;


	ret = regmap_field_read(priv->rf[temp_idx], &last_temp);
	if (ret)
		return ret;

	ret = regmap_field_read(priv->rf[valid_idx], &valid);
	ret = regmap_field_read(priv->rf[valid_idx], &valid);
	if (ret)
	if (ret)
		return ret;
		return ret;

	while (!valid) {
	if (valid)
		/* Valid bit is 0 for 6 AHB clock cycles.
		goto done;
		 * At 19.2MHz, 1 AHB clock is ~60ns.

		 * We should enter this loop very, very rarely.
	/* Try a second time */
		 */
		ndelay(400);
		ret = regmap_field_read(priv->rf[valid_idx], &valid);
		ret = regmap_field_read(priv->rf[valid_idx], &valid);
		if (ret)
		if (ret)
			return ret;
			return ret;
	ret = regmap_field_read(priv->rf[temp_idx], &last_temp2);
	if (ret)
		return ret;
	if (valid) {
		last_temp = last_temp2;
		goto done;
	}
	}


	/* Try a third/last time */
	/* Valid bit is set, OK to read the temperature */
	ret = regmap_field_read(priv->rf[valid_idx], &valid);
	ret = regmap_field_read(priv->rf[temp_idx], &last_temp);
	if (ret)
		return ret;
	ret = regmap_field_read(priv->rf[temp_idx], &last_temp3);
	if (ret)
	if (ret)
		return ret;
		return ret;
	if (valid) {
		last_temp = last_temp3;
		goto done;
	}


	if (last_temp == last_temp2)
	mask = GENMASK(priv->fields[LAST_TEMP_0].msb,
		last_temp = last_temp2;
		       priv->fields[LAST_TEMP_0].lsb);
	else if (last_temp2 == last_temp3)
		last_temp = last_temp3;
done:
	/* Convert temperature from deciCelsius to milliCelsius */
	/* Convert temperature from deciCelsius to milliCelsius */
	*temp = sign_extend32(last_temp, fls(LAST_TEMP_MASK) - 1) * 100;
	*temp = sign_extend32(last_temp, fls(mask) - 1) * 100;


	return 0;
	return 0;
}
}