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 8 9
 */

#include <linux/regmap.h>
#include "tsens.h"

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

15
static int get_temp_tsens_v2(struct tsens_device *tmdev, int id, int *temp)
16 17 18 19 20 21
{
	struct tsens_sensor *s = &tmdev->sensor[id];
	u32 code;
	unsigned int sensor_addr;
	int last_temp = 0, last_temp2 = 0, last_temp3 = 0, ret;

22
	sensor_addr = tmdev->tm_offset + STATUS_OFFSET + s->hw_id * 4;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
	ret = regmap_read(tmdev->map, sensor_addr, &code);
	if (ret)
		return ret;
	last_temp = code & LAST_TEMP_MASK;
	if (code & STATUS_VALID_BIT)
		goto done;

	/* Try a second time */
	ret = regmap_read(tmdev->map, sensor_addr, &code);
	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 */
	ret = regmap_read(tmdev->map, sensor_addr, &code);
	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:
	/* Code sign bit is the sign extension for a negative value */
	if (last_temp & CODE_SIGN_BIT)
		last_temp |= ~CODE_SIGN_BIT;

	/* Temperatures are in deciCelicius */
	*temp = last_temp * 100;

	return 0;
}

67
static const struct tsens_ops ops_generic_v2 = {
68
	.init		= init_common,
69
	.get_temp	= get_temp_tsens_v2,
70 71
};

72 73 74 75 76
const struct tsens_data data_tsens_v2 = {
	.ops            = &ops_generic_v2,
};

/* Kept around for backward compatibility with old msm8996.dtsi */
77 78
const struct tsens_data data_8996 = {
	.num_sensors	= 13,
79
	.ops		= &ops_generic_v2,
80
};