tsens-v2.c 1.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3
/*
 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
4
 * Copyright (c) 2018, Linaro Limited
5 6 7
 */

#include <linux/regmap.h>
8
#include <linux/bitops.h>
9 10
#include "tsens.h"

11 12
#define STATUS_OFFSET		0xa0
#define LAST_TEMP_MASK		0xfff
13 14
#define STATUS_VALID_BIT	BIT(21)

15
static int get_temp_tsens_v2(struct tsens_priv *priv, int id, int *temp)
16
{
17
	struct tsens_sensor *s = &priv->sensor[id];
18
	u32 code;
A
Amit Kucheria 已提交
19
	unsigned int status_reg;
20 21
	u32 last_temp = 0, last_temp2 = 0, last_temp3 = 0;
	int ret;
22

23 24
	status_reg = priv->tm_offset + STATUS_OFFSET + s->hw_id * 4;
	ret = regmap_read(priv->tm_map, status_reg, &code);
25 26 27 28 29 30 31
	if (ret)
		return ret;
	last_temp = code & LAST_TEMP_MASK;
	if (code & STATUS_VALID_BIT)
		goto done;

	/* Try a second time */
32
	ret = regmap_read(priv->tm_map, status_reg, &code);
33 34 35 36 37 38 39 40 41 42
	if (ret)
		return ret;
	if (code & STATUS_VALID_BIT) {
		last_temp = code & LAST_TEMP_MASK;
		goto done;
	} else {
		last_temp2 = code & LAST_TEMP_MASK;
	}

	/* Try a third/last time */
43
	ret = regmap_read(priv->tm_map, status_reg, &code);
44 45 46 47 48 49 50 51 52 53 54 55 56 57
	if (ret)
		return ret;
	if (code & STATUS_VALID_BIT) {
		last_temp = code & LAST_TEMP_MASK;
		goto done;
	} else {
		last_temp3 = code & LAST_TEMP_MASK;
	}

	if (last_temp == last_temp2)
		last_temp = last_temp2;
	else if (last_temp2 == last_temp3)
		last_temp = last_temp3;
done:
58 59
	/* Convert temperature from deciCelsius to milliCelsius */
	*temp = sign_extend32(last_temp, fls(LAST_TEMP_MASK) - 1) * 100;
60 61 62 63

	return 0;
}

64
static const struct tsens_ops ops_generic_v2 = {
65
	.init		= init_common,
66
	.get_temp	= get_temp_tsens_v2,
67 68
};

69
const struct tsens_plat_data data_tsens_v2 = {
70
	.ops            = &ops_generic_v2,
71
	.reg_offsets	= { [SROT_CTRL_OFFSET] = 0x4 },
72 73 74
};

/* Kept around for backward compatibility with old msm8996.dtsi */
75
const struct tsens_plat_data data_8996 = {
76
	.num_sensors	= 13,
77
	.ops		= &ops_generic_v2,
78
	.reg_offsets	= { [SROT_CTRL_OFFSET] = 0x4 },
79
};