162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2015, The Linux Foundation. All rights reserved.
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#ifndef __QCOM_TSENS_H__
762306a36Sopenharmony_ci#define __QCOM_TSENS_H__
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#define NO_PT_CALIB		0x0
1062306a36Sopenharmony_ci#define ONE_PT_CALIB		0x1
1162306a36Sopenharmony_ci#define ONE_PT_CALIB2		0x2
1262306a36Sopenharmony_ci#define TWO_PT_CALIB		0x3
1362306a36Sopenharmony_ci#define ONE_PT_CALIB2_NO_OFFSET	0x6
1462306a36Sopenharmony_ci#define TWO_PT_CALIB_NO_OFFSET	0x7
1562306a36Sopenharmony_ci#define CAL_DEGC_PT1		30
1662306a36Sopenharmony_ci#define CAL_DEGC_PT2		120
1762306a36Sopenharmony_ci#define SLOPE_FACTOR		1000
1862306a36Sopenharmony_ci#define SLOPE_DEFAULT		3200
1962306a36Sopenharmony_ci#define TIMEOUT_US		100
2062306a36Sopenharmony_ci#define THRESHOLD_MAX_ADC_CODE	0x3ff
2162306a36Sopenharmony_ci#define THRESHOLD_MIN_ADC_CODE	0x0
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#define MAX_SENSORS 16
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci#include <linux/interrupt.h>
2662306a36Sopenharmony_ci#include <linux/thermal.h>
2762306a36Sopenharmony_ci#include <linux/regmap.h>
2862306a36Sopenharmony_ci#include <linux/slab.h>
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_cistruct tsens_priv;
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci/* IP version numbers in ascending order */
3362306a36Sopenharmony_cienum tsens_ver {
3462306a36Sopenharmony_ci	VER_0 = 0,
3562306a36Sopenharmony_ci	VER_0_1,
3662306a36Sopenharmony_ci	VER_1_X,
3762306a36Sopenharmony_ci	VER_2_X,
3862306a36Sopenharmony_ci};
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cienum tsens_irq_type {
4162306a36Sopenharmony_ci	LOWER,
4262306a36Sopenharmony_ci	UPPER,
4362306a36Sopenharmony_ci	CRITICAL,
4462306a36Sopenharmony_ci};
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/**
4762306a36Sopenharmony_ci * struct tsens_sensor - data for each sensor connected to the tsens device
4862306a36Sopenharmony_ci * @priv: tsens device instance that this sensor is connected to
4962306a36Sopenharmony_ci * @tzd: pointer to the thermal zone that this sensor is in
5062306a36Sopenharmony_ci * @offset: offset of temperature adjustment curve
5162306a36Sopenharmony_ci * @hw_id: HW ID can be used in case of platform-specific IDs
5262306a36Sopenharmony_ci * @slope: slope of temperature adjustment curve
5362306a36Sopenharmony_ci * @status: 8960-specific variable to track 8960 and 8660 status register offset
5462306a36Sopenharmony_ci */
5562306a36Sopenharmony_cistruct tsens_sensor {
5662306a36Sopenharmony_ci	struct tsens_priv		*priv;
5762306a36Sopenharmony_ci	struct thermal_zone_device	*tzd;
5862306a36Sopenharmony_ci	int				offset;
5962306a36Sopenharmony_ci	unsigned int			hw_id;
6062306a36Sopenharmony_ci	int				slope;
6162306a36Sopenharmony_ci	u32				status;
6262306a36Sopenharmony_ci	int				p1_calib_offset;
6362306a36Sopenharmony_ci	int				p2_calib_offset;
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/**
6762306a36Sopenharmony_ci * struct tsens_ops - operations as supported by the tsens device
6862306a36Sopenharmony_ci * @init: Function to initialize the tsens device
6962306a36Sopenharmony_ci * @calibrate: Function to calibrate the tsens device
7062306a36Sopenharmony_ci * @get_temp: Function which returns the temp in millidegC
7162306a36Sopenharmony_ci * @enable: Function to enable (clocks/power) tsens device
7262306a36Sopenharmony_ci * @disable: Function to disable the tsens device
7362306a36Sopenharmony_ci * @suspend: Function to suspend the tsens device
7462306a36Sopenharmony_ci * @resume: Function to resume the tsens device
7562306a36Sopenharmony_ci */
7662306a36Sopenharmony_cistruct tsens_ops {
7762306a36Sopenharmony_ci	/* mandatory callbacks */
7862306a36Sopenharmony_ci	int (*init)(struct tsens_priv *priv);
7962306a36Sopenharmony_ci	int (*calibrate)(struct tsens_priv *priv);
8062306a36Sopenharmony_ci	int (*get_temp)(const struct tsens_sensor *s, int *temp);
8162306a36Sopenharmony_ci	/* optional callbacks */
8262306a36Sopenharmony_ci	int (*enable)(struct tsens_priv *priv, int i);
8362306a36Sopenharmony_ci	void (*disable)(struct tsens_priv *priv);
8462306a36Sopenharmony_ci	int (*suspend)(struct tsens_priv *priv);
8562306a36Sopenharmony_ci	int (*resume)(struct tsens_priv *priv);
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci#define REG_FIELD_FOR_EACH_SENSOR11(_name, _offset, _startbit, _stopbit) \
8962306a36Sopenharmony_ci	[_name##_##0]  = REG_FIELD(_offset,      _startbit, _stopbit),	\
9062306a36Sopenharmony_ci	[_name##_##1]  = REG_FIELD(_offset +  4, _startbit, _stopbit), \
9162306a36Sopenharmony_ci	[_name##_##2]  = REG_FIELD(_offset +  8, _startbit, _stopbit), \
9262306a36Sopenharmony_ci	[_name##_##3]  = REG_FIELD(_offset + 12, _startbit, _stopbit), \
9362306a36Sopenharmony_ci	[_name##_##4]  = REG_FIELD(_offset + 16, _startbit, _stopbit), \
9462306a36Sopenharmony_ci	[_name##_##5]  = REG_FIELD(_offset + 20, _startbit, _stopbit), \
9562306a36Sopenharmony_ci	[_name##_##6]  = REG_FIELD(_offset + 24, _startbit, _stopbit), \
9662306a36Sopenharmony_ci	[_name##_##7]  = REG_FIELD(_offset + 28, _startbit, _stopbit), \
9762306a36Sopenharmony_ci	[_name##_##8]  = REG_FIELD(_offset + 32, _startbit, _stopbit), \
9862306a36Sopenharmony_ci	[_name##_##9]  = REG_FIELD(_offset + 36, _startbit, _stopbit), \
9962306a36Sopenharmony_ci	[_name##_##10] = REG_FIELD(_offset + 40, _startbit, _stopbit)
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci#define REG_FIELD_FOR_EACH_SENSOR16(_name, _offset, _startbit, _stopbit) \
10262306a36Sopenharmony_ci	[_name##_##0]  = REG_FIELD(_offset,      _startbit, _stopbit),	\
10362306a36Sopenharmony_ci	[_name##_##1]  = REG_FIELD(_offset +  4, _startbit, _stopbit), \
10462306a36Sopenharmony_ci	[_name##_##2]  = REG_FIELD(_offset +  8, _startbit, _stopbit), \
10562306a36Sopenharmony_ci	[_name##_##3]  = REG_FIELD(_offset + 12, _startbit, _stopbit), \
10662306a36Sopenharmony_ci	[_name##_##4]  = REG_FIELD(_offset + 16, _startbit, _stopbit), \
10762306a36Sopenharmony_ci	[_name##_##5]  = REG_FIELD(_offset + 20, _startbit, _stopbit), \
10862306a36Sopenharmony_ci	[_name##_##6]  = REG_FIELD(_offset + 24, _startbit, _stopbit), \
10962306a36Sopenharmony_ci	[_name##_##7]  = REG_FIELD(_offset + 28, _startbit, _stopbit), \
11062306a36Sopenharmony_ci	[_name##_##8]  = REG_FIELD(_offset + 32, _startbit, _stopbit), \
11162306a36Sopenharmony_ci	[_name##_##9]  = REG_FIELD(_offset + 36, _startbit, _stopbit), \
11262306a36Sopenharmony_ci	[_name##_##10] = REG_FIELD(_offset + 40, _startbit, _stopbit), \
11362306a36Sopenharmony_ci	[_name##_##11] = REG_FIELD(_offset + 44, _startbit, _stopbit), \
11462306a36Sopenharmony_ci	[_name##_##12] = REG_FIELD(_offset + 48, _startbit, _stopbit), \
11562306a36Sopenharmony_ci	[_name##_##13] = REG_FIELD(_offset + 52, _startbit, _stopbit), \
11662306a36Sopenharmony_ci	[_name##_##14] = REG_FIELD(_offset + 56, _startbit, _stopbit), \
11762306a36Sopenharmony_ci	[_name##_##15] = REG_FIELD(_offset + 60, _startbit, _stopbit)
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci#define REG_FIELD_SPLIT_BITS_0_15(_name, _offset)		\
12062306a36Sopenharmony_ci	[_name##_##0]  = REG_FIELD(_offset,  0,  0),		\
12162306a36Sopenharmony_ci	[_name##_##1]  = REG_FIELD(_offset,  1,  1),	\
12262306a36Sopenharmony_ci	[_name##_##2]  = REG_FIELD(_offset,  2,  2),	\
12362306a36Sopenharmony_ci	[_name##_##3]  = REG_FIELD(_offset,  3,  3),	\
12462306a36Sopenharmony_ci	[_name##_##4]  = REG_FIELD(_offset,  4,  4),	\
12562306a36Sopenharmony_ci	[_name##_##5]  = REG_FIELD(_offset,  5,  5),	\
12662306a36Sopenharmony_ci	[_name##_##6]  = REG_FIELD(_offset,  6,  6),	\
12762306a36Sopenharmony_ci	[_name##_##7]  = REG_FIELD(_offset,  7,  7),	\
12862306a36Sopenharmony_ci	[_name##_##8]  = REG_FIELD(_offset,  8,  8),	\
12962306a36Sopenharmony_ci	[_name##_##9]  = REG_FIELD(_offset,  9,  9),	\
13062306a36Sopenharmony_ci	[_name##_##10] = REG_FIELD(_offset, 10, 10),	\
13162306a36Sopenharmony_ci	[_name##_##11] = REG_FIELD(_offset, 11, 11),	\
13262306a36Sopenharmony_ci	[_name##_##12] = REG_FIELD(_offset, 12, 12),	\
13362306a36Sopenharmony_ci	[_name##_##13] = REG_FIELD(_offset, 13, 13),	\
13462306a36Sopenharmony_ci	[_name##_##14] = REG_FIELD(_offset, 14, 14),	\
13562306a36Sopenharmony_ci	[_name##_##15] = REG_FIELD(_offset, 15, 15)
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci#define REG_FIELD_SPLIT_BITS_16_31(_name, _offset)		\
13862306a36Sopenharmony_ci	[_name##_##0]  = REG_FIELD(_offset, 16, 16),		\
13962306a36Sopenharmony_ci	[_name##_##1]  = REG_FIELD(_offset, 17, 17),	\
14062306a36Sopenharmony_ci	[_name##_##2]  = REG_FIELD(_offset, 18, 18),	\
14162306a36Sopenharmony_ci	[_name##_##3]  = REG_FIELD(_offset, 19, 19),	\
14262306a36Sopenharmony_ci	[_name##_##4]  = REG_FIELD(_offset, 20, 20),	\
14362306a36Sopenharmony_ci	[_name##_##5]  = REG_FIELD(_offset, 21, 21),	\
14462306a36Sopenharmony_ci	[_name##_##6]  = REG_FIELD(_offset, 22, 22),	\
14562306a36Sopenharmony_ci	[_name##_##7]  = REG_FIELD(_offset, 23, 23),	\
14662306a36Sopenharmony_ci	[_name##_##8]  = REG_FIELD(_offset, 24, 24),	\
14762306a36Sopenharmony_ci	[_name##_##9]  = REG_FIELD(_offset, 25, 25),	\
14862306a36Sopenharmony_ci	[_name##_##10] = REG_FIELD(_offset, 26, 26),	\
14962306a36Sopenharmony_ci	[_name##_##11] = REG_FIELD(_offset, 27, 27),	\
15062306a36Sopenharmony_ci	[_name##_##12] = REG_FIELD(_offset, 28, 28),	\
15162306a36Sopenharmony_ci	[_name##_##13] = REG_FIELD(_offset, 29, 29),	\
15262306a36Sopenharmony_ci	[_name##_##14] = REG_FIELD(_offset, 30, 30),	\
15362306a36Sopenharmony_ci	[_name##_##15] = REG_FIELD(_offset, 31, 31)
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci/*
15662306a36Sopenharmony_ci * reg_field IDs to use as an index into an array
15762306a36Sopenharmony_ci * If you change the order of the entries, check the devm_regmap_field_alloc()
15862306a36Sopenharmony_ci * calls in init_common()
15962306a36Sopenharmony_ci */
16062306a36Sopenharmony_cienum regfield_ids {
16162306a36Sopenharmony_ci	/* ----- SROT ------ */
16262306a36Sopenharmony_ci	/* HW_VER */
16362306a36Sopenharmony_ci	VER_MAJOR,
16462306a36Sopenharmony_ci	VER_MINOR,
16562306a36Sopenharmony_ci	VER_STEP,
16662306a36Sopenharmony_ci	/* CTRL_OFFSET */
16762306a36Sopenharmony_ci	TSENS_EN,
16862306a36Sopenharmony_ci	TSENS_SW_RST,
16962306a36Sopenharmony_ci	SENSOR_EN,
17062306a36Sopenharmony_ci	CODE_OR_TEMP,
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	/* ----- TM ------ */
17362306a36Sopenharmony_ci	/* TRDY */
17462306a36Sopenharmony_ci	TRDY,
17562306a36Sopenharmony_ci	/* INTERRUPT ENABLE */
17662306a36Sopenharmony_ci	INT_EN,	/* v2+ has separate enables for crit, upper and lower irq */
17762306a36Sopenharmony_ci	/* STATUS */
17862306a36Sopenharmony_ci	LAST_TEMP_0,	/* Last temperature reading */
17962306a36Sopenharmony_ci	LAST_TEMP_1,
18062306a36Sopenharmony_ci	LAST_TEMP_2,
18162306a36Sopenharmony_ci	LAST_TEMP_3,
18262306a36Sopenharmony_ci	LAST_TEMP_4,
18362306a36Sopenharmony_ci	LAST_TEMP_5,
18462306a36Sopenharmony_ci	LAST_TEMP_6,
18562306a36Sopenharmony_ci	LAST_TEMP_7,
18662306a36Sopenharmony_ci	LAST_TEMP_8,
18762306a36Sopenharmony_ci	LAST_TEMP_9,
18862306a36Sopenharmony_ci	LAST_TEMP_10,
18962306a36Sopenharmony_ci	LAST_TEMP_11,
19062306a36Sopenharmony_ci	LAST_TEMP_12,
19162306a36Sopenharmony_ci	LAST_TEMP_13,
19262306a36Sopenharmony_ci	LAST_TEMP_14,
19362306a36Sopenharmony_ci	LAST_TEMP_15,
19462306a36Sopenharmony_ci	VALID_0,		/* VALID reading or not */
19562306a36Sopenharmony_ci	VALID_1,
19662306a36Sopenharmony_ci	VALID_2,
19762306a36Sopenharmony_ci	VALID_3,
19862306a36Sopenharmony_ci	VALID_4,
19962306a36Sopenharmony_ci	VALID_5,
20062306a36Sopenharmony_ci	VALID_6,
20162306a36Sopenharmony_ci	VALID_7,
20262306a36Sopenharmony_ci	VALID_8,
20362306a36Sopenharmony_ci	VALID_9,
20462306a36Sopenharmony_ci	VALID_10,
20562306a36Sopenharmony_ci	VALID_11,
20662306a36Sopenharmony_ci	VALID_12,
20762306a36Sopenharmony_ci	VALID_13,
20862306a36Sopenharmony_ci	VALID_14,
20962306a36Sopenharmony_ci	VALID_15,
21062306a36Sopenharmony_ci	LOWER_STATUS_0,	/* LOWER threshold violated */
21162306a36Sopenharmony_ci	LOWER_STATUS_1,
21262306a36Sopenharmony_ci	LOWER_STATUS_2,
21362306a36Sopenharmony_ci	LOWER_STATUS_3,
21462306a36Sopenharmony_ci	LOWER_STATUS_4,
21562306a36Sopenharmony_ci	LOWER_STATUS_5,
21662306a36Sopenharmony_ci	LOWER_STATUS_6,
21762306a36Sopenharmony_ci	LOWER_STATUS_7,
21862306a36Sopenharmony_ci	LOWER_STATUS_8,
21962306a36Sopenharmony_ci	LOWER_STATUS_9,
22062306a36Sopenharmony_ci	LOWER_STATUS_10,
22162306a36Sopenharmony_ci	LOWER_STATUS_11,
22262306a36Sopenharmony_ci	LOWER_STATUS_12,
22362306a36Sopenharmony_ci	LOWER_STATUS_13,
22462306a36Sopenharmony_ci	LOWER_STATUS_14,
22562306a36Sopenharmony_ci	LOWER_STATUS_15,
22662306a36Sopenharmony_ci	LOW_INT_STATUS_0,	/* LOWER interrupt status */
22762306a36Sopenharmony_ci	LOW_INT_STATUS_1,
22862306a36Sopenharmony_ci	LOW_INT_STATUS_2,
22962306a36Sopenharmony_ci	LOW_INT_STATUS_3,
23062306a36Sopenharmony_ci	LOW_INT_STATUS_4,
23162306a36Sopenharmony_ci	LOW_INT_STATUS_5,
23262306a36Sopenharmony_ci	LOW_INT_STATUS_6,
23362306a36Sopenharmony_ci	LOW_INT_STATUS_7,
23462306a36Sopenharmony_ci	LOW_INT_STATUS_8,
23562306a36Sopenharmony_ci	LOW_INT_STATUS_9,
23662306a36Sopenharmony_ci	LOW_INT_STATUS_10,
23762306a36Sopenharmony_ci	LOW_INT_STATUS_11,
23862306a36Sopenharmony_ci	LOW_INT_STATUS_12,
23962306a36Sopenharmony_ci	LOW_INT_STATUS_13,
24062306a36Sopenharmony_ci	LOW_INT_STATUS_14,
24162306a36Sopenharmony_ci	LOW_INT_STATUS_15,
24262306a36Sopenharmony_ci	LOW_INT_CLEAR_0,	/* LOWER interrupt clear */
24362306a36Sopenharmony_ci	LOW_INT_CLEAR_1,
24462306a36Sopenharmony_ci	LOW_INT_CLEAR_2,
24562306a36Sopenharmony_ci	LOW_INT_CLEAR_3,
24662306a36Sopenharmony_ci	LOW_INT_CLEAR_4,
24762306a36Sopenharmony_ci	LOW_INT_CLEAR_5,
24862306a36Sopenharmony_ci	LOW_INT_CLEAR_6,
24962306a36Sopenharmony_ci	LOW_INT_CLEAR_7,
25062306a36Sopenharmony_ci	LOW_INT_CLEAR_8,
25162306a36Sopenharmony_ci	LOW_INT_CLEAR_9,
25262306a36Sopenharmony_ci	LOW_INT_CLEAR_10,
25362306a36Sopenharmony_ci	LOW_INT_CLEAR_11,
25462306a36Sopenharmony_ci	LOW_INT_CLEAR_12,
25562306a36Sopenharmony_ci	LOW_INT_CLEAR_13,
25662306a36Sopenharmony_ci	LOW_INT_CLEAR_14,
25762306a36Sopenharmony_ci	LOW_INT_CLEAR_15,
25862306a36Sopenharmony_ci	LOW_INT_MASK_0,	/* LOWER interrupt mask */
25962306a36Sopenharmony_ci	LOW_INT_MASK_1,
26062306a36Sopenharmony_ci	LOW_INT_MASK_2,
26162306a36Sopenharmony_ci	LOW_INT_MASK_3,
26262306a36Sopenharmony_ci	LOW_INT_MASK_4,
26362306a36Sopenharmony_ci	LOW_INT_MASK_5,
26462306a36Sopenharmony_ci	LOW_INT_MASK_6,
26562306a36Sopenharmony_ci	LOW_INT_MASK_7,
26662306a36Sopenharmony_ci	LOW_INT_MASK_8,
26762306a36Sopenharmony_ci	LOW_INT_MASK_9,
26862306a36Sopenharmony_ci	LOW_INT_MASK_10,
26962306a36Sopenharmony_ci	LOW_INT_MASK_11,
27062306a36Sopenharmony_ci	LOW_INT_MASK_12,
27162306a36Sopenharmony_ci	LOW_INT_MASK_13,
27262306a36Sopenharmony_ci	LOW_INT_MASK_14,
27362306a36Sopenharmony_ci	LOW_INT_MASK_15,
27462306a36Sopenharmony_ci	LOW_THRESH_0,		/* LOWER threshold values */
27562306a36Sopenharmony_ci	LOW_THRESH_1,
27662306a36Sopenharmony_ci	LOW_THRESH_2,
27762306a36Sopenharmony_ci	LOW_THRESH_3,
27862306a36Sopenharmony_ci	LOW_THRESH_4,
27962306a36Sopenharmony_ci	LOW_THRESH_5,
28062306a36Sopenharmony_ci	LOW_THRESH_6,
28162306a36Sopenharmony_ci	LOW_THRESH_7,
28262306a36Sopenharmony_ci	LOW_THRESH_8,
28362306a36Sopenharmony_ci	LOW_THRESH_9,
28462306a36Sopenharmony_ci	LOW_THRESH_10,
28562306a36Sopenharmony_ci	LOW_THRESH_11,
28662306a36Sopenharmony_ci	LOW_THRESH_12,
28762306a36Sopenharmony_ci	LOW_THRESH_13,
28862306a36Sopenharmony_ci	LOW_THRESH_14,
28962306a36Sopenharmony_ci	LOW_THRESH_15,
29062306a36Sopenharmony_ci	UPPER_STATUS_0,	/* UPPER threshold violated */
29162306a36Sopenharmony_ci	UPPER_STATUS_1,
29262306a36Sopenharmony_ci	UPPER_STATUS_2,
29362306a36Sopenharmony_ci	UPPER_STATUS_3,
29462306a36Sopenharmony_ci	UPPER_STATUS_4,
29562306a36Sopenharmony_ci	UPPER_STATUS_5,
29662306a36Sopenharmony_ci	UPPER_STATUS_6,
29762306a36Sopenharmony_ci	UPPER_STATUS_7,
29862306a36Sopenharmony_ci	UPPER_STATUS_8,
29962306a36Sopenharmony_ci	UPPER_STATUS_9,
30062306a36Sopenharmony_ci	UPPER_STATUS_10,
30162306a36Sopenharmony_ci	UPPER_STATUS_11,
30262306a36Sopenharmony_ci	UPPER_STATUS_12,
30362306a36Sopenharmony_ci	UPPER_STATUS_13,
30462306a36Sopenharmony_ci	UPPER_STATUS_14,
30562306a36Sopenharmony_ci	UPPER_STATUS_15,
30662306a36Sopenharmony_ci	UP_INT_STATUS_0,	/* UPPER interrupt status */
30762306a36Sopenharmony_ci	UP_INT_STATUS_1,
30862306a36Sopenharmony_ci	UP_INT_STATUS_2,
30962306a36Sopenharmony_ci	UP_INT_STATUS_3,
31062306a36Sopenharmony_ci	UP_INT_STATUS_4,
31162306a36Sopenharmony_ci	UP_INT_STATUS_5,
31262306a36Sopenharmony_ci	UP_INT_STATUS_6,
31362306a36Sopenharmony_ci	UP_INT_STATUS_7,
31462306a36Sopenharmony_ci	UP_INT_STATUS_8,
31562306a36Sopenharmony_ci	UP_INT_STATUS_9,
31662306a36Sopenharmony_ci	UP_INT_STATUS_10,
31762306a36Sopenharmony_ci	UP_INT_STATUS_11,
31862306a36Sopenharmony_ci	UP_INT_STATUS_12,
31962306a36Sopenharmony_ci	UP_INT_STATUS_13,
32062306a36Sopenharmony_ci	UP_INT_STATUS_14,
32162306a36Sopenharmony_ci	UP_INT_STATUS_15,
32262306a36Sopenharmony_ci	UP_INT_CLEAR_0,	/* UPPER interrupt clear */
32362306a36Sopenharmony_ci	UP_INT_CLEAR_1,
32462306a36Sopenharmony_ci	UP_INT_CLEAR_2,
32562306a36Sopenharmony_ci	UP_INT_CLEAR_3,
32662306a36Sopenharmony_ci	UP_INT_CLEAR_4,
32762306a36Sopenharmony_ci	UP_INT_CLEAR_5,
32862306a36Sopenharmony_ci	UP_INT_CLEAR_6,
32962306a36Sopenharmony_ci	UP_INT_CLEAR_7,
33062306a36Sopenharmony_ci	UP_INT_CLEAR_8,
33162306a36Sopenharmony_ci	UP_INT_CLEAR_9,
33262306a36Sopenharmony_ci	UP_INT_CLEAR_10,
33362306a36Sopenharmony_ci	UP_INT_CLEAR_11,
33462306a36Sopenharmony_ci	UP_INT_CLEAR_12,
33562306a36Sopenharmony_ci	UP_INT_CLEAR_13,
33662306a36Sopenharmony_ci	UP_INT_CLEAR_14,
33762306a36Sopenharmony_ci	UP_INT_CLEAR_15,
33862306a36Sopenharmony_ci	UP_INT_MASK_0,		/* UPPER interrupt mask */
33962306a36Sopenharmony_ci	UP_INT_MASK_1,
34062306a36Sopenharmony_ci	UP_INT_MASK_2,
34162306a36Sopenharmony_ci	UP_INT_MASK_3,
34262306a36Sopenharmony_ci	UP_INT_MASK_4,
34362306a36Sopenharmony_ci	UP_INT_MASK_5,
34462306a36Sopenharmony_ci	UP_INT_MASK_6,
34562306a36Sopenharmony_ci	UP_INT_MASK_7,
34662306a36Sopenharmony_ci	UP_INT_MASK_8,
34762306a36Sopenharmony_ci	UP_INT_MASK_9,
34862306a36Sopenharmony_ci	UP_INT_MASK_10,
34962306a36Sopenharmony_ci	UP_INT_MASK_11,
35062306a36Sopenharmony_ci	UP_INT_MASK_12,
35162306a36Sopenharmony_ci	UP_INT_MASK_13,
35262306a36Sopenharmony_ci	UP_INT_MASK_14,
35362306a36Sopenharmony_ci	UP_INT_MASK_15,
35462306a36Sopenharmony_ci	UP_THRESH_0,		/* UPPER threshold values */
35562306a36Sopenharmony_ci	UP_THRESH_1,
35662306a36Sopenharmony_ci	UP_THRESH_2,
35762306a36Sopenharmony_ci	UP_THRESH_3,
35862306a36Sopenharmony_ci	UP_THRESH_4,
35962306a36Sopenharmony_ci	UP_THRESH_5,
36062306a36Sopenharmony_ci	UP_THRESH_6,
36162306a36Sopenharmony_ci	UP_THRESH_7,
36262306a36Sopenharmony_ci	UP_THRESH_8,
36362306a36Sopenharmony_ci	UP_THRESH_9,
36462306a36Sopenharmony_ci	UP_THRESH_10,
36562306a36Sopenharmony_ci	UP_THRESH_11,
36662306a36Sopenharmony_ci	UP_THRESH_12,
36762306a36Sopenharmony_ci	UP_THRESH_13,
36862306a36Sopenharmony_ci	UP_THRESH_14,
36962306a36Sopenharmony_ci	UP_THRESH_15,
37062306a36Sopenharmony_ci	CRITICAL_STATUS_0,	/* CRITICAL threshold violated */
37162306a36Sopenharmony_ci	CRITICAL_STATUS_1,
37262306a36Sopenharmony_ci	CRITICAL_STATUS_2,
37362306a36Sopenharmony_ci	CRITICAL_STATUS_3,
37462306a36Sopenharmony_ci	CRITICAL_STATUS_4,
37562306a36Sopenharmony_ci	CRITICAL_STATUS_5,
37662306a36Sopenharmony_ci	CRITICAL_STATUS_6,
37762306a36Sopenharmony_ci	CRITICAL_STATUS_7,
37862306a36Sopenharmony_ci	CRITICAL_STATUS_8,
37962306a36Sopenharmony_ci	CRITICAL_STATUS_9,
38062306a36Sopenharmony_ci	CRITICAL_STATUS_10,
38162306a36Sopenharmony_ci	CRITICAL_STATUS_11,
38262306a36Sopenharmony_ci	CRITICAL_STATUS_12,
38362306a36Sopenharmony_ci	CRITICAL_STATUS_13,
38462306a36Sopenharmony_ci	CRITICAL_STATUS_14,
38562306a36Sopenharmony_ci	CRITICAL_STATUS_15,
38662306a36Sopenharmony_ci	CRIT_INT_STATUS_0,	/* CRITICAL interrupt status */
38762306a36Sopenharmony_ci	CRIT_INT_STATUS_1,
38862306a36Sopenharmony_ci	CRIT_INT_STATUS_2,
38962306a36Sopenharmony_ci	CRIT_INT_STATUS_3,
39062306a36Sopenharmony_ci	CRIT_INT_STATUS_4,
39162306a36Sopenharmony_ci	CRIT_INT_STATUS_5,
39262306a36Sopenharmony_ci	CRIT_INT_STATUS_6,
39362306a36Sopenharmony_ci	CRIT_INT_STATUS_7,
39462306a36Sopenharmony_ci	CRIT_INT_STATUS_8,
39562306a36Sopenharmony_ci	CRIT_INT_STATUS_9,
39662306a36Sopenharmony_ci	CRIT_INT_STATUS_10,
39762306a36Sopenharmony_ci	CRIT_INT_STATUS_11,
39862306a36Sopenharmony_ci	CRIT_INT_STATUS_12,
39962306a36Sopenharmony_ci	CRIT_INT_STATUS_13,
40062306a36Sopenharmony_ci	CRIT_INT_STATUS_14,
40162306a36Sopenharmony_ci	CRIT_INT_STATUS_15,
40262306a36Sopenharmony_ci	CRIT_INT_CLEAR_0,	/* CRITICAL interrupt clear */
40362306a36Sopenharmony_ci	CRIT_INT_CLEAR_1,
40462306a36Sopenharmony_ci	CRIT_INT_CLEAR_2,
40562306a36Sopenharmony_ci	CRIT_INT_CLEAR_3,
40662306a36Sopenharmony_ci	CRIT_INT_CLEAR_4,
40762306a36Sopenharmony_ci	CRIT_INT_CLEAR_5,
40862306a36Sopenharmony_ci	CRIT_INT_CLEAR_6,
40962306a36Sopenharmony_ci	CRIT_INT_CLEAR_7,
41062306a36Sopenharmony_ci	CRIT_INT_CLEAR_8,
41162306a36Sopenharmony_ci	CRIT_INT_CLEAR_9,
41262306a36Sopenharmony_ci	CRIT_INT_CLEAR_10,
41362306a36Sopenharmony_ci	CRIT_INT_CLEAR_11,
41462306a36Sopenharmony_ci	CRIT_INT_CLEAR_12,
41562306a36Sopenharmony_ci	CRIT_INT_CLEAR_13,
41662306a36Sopenharmony_ci	CRIT_INT_CLEAR_14,
41762306a36Sopenharmony_ci	CRIT_INT_CLEAR_15,
41862306a36Sopenharmony_ci	CRIT_INT_MASK_0,	/* CRITICAL interrupt mask */
41962306a36Sopenharmony_ci	CRIT_INT_MASK_1,
42062306a36Sopenharmony_ci	CRIT_INT_MASK_2,
42162306a36Sopenharmony_ci	CRIT_INT_MASK_3,
42262306a36Sopenharmony_ci	CRIT_INT_MASK_4,
42362306a36Sopenharmony_ci	CRIT_INT_MASK_5,
42462306a36Sopenharmony_ci	CRIT_INT_MASK_6,
42562306a36Sopenharmony_ci	CRIT_INT_MASK_7,
42662306a36Sopenharmony_ci	CRIT_INT_MASK_8,
42762306a36Sopenharmony_ci	CRIT_INT_MASK_9,
42862306a36Sopenharmony_ci	CRIT_INT_MASK_10,
42962306a36Sopenharmony_ci	CRIT_INT_MASK_11,
43062306a36Sopenharmony_ci	CRIT_INT_MASK_12,
43162306a36Sopenharmony_ci	CRIT_INT_MASK_13,
43262306a36Sopenharmony_ci	CRIT_INT_MASK_14,
43362306a36Sopenharmony_ci	CRIT_INT_MASK_15,
43462306a36Sopenharmony_ci	CRIT_THRESH_0,		/* CRITICAL threshold values */
43562306a36Sopenharmony_ci	CRIT_THRESH_1,
43662306a36Sopenharmony_ci	CRIT_THRESH_2,
43762306a36Sopenharmony_ci	CRIT_THRESH_3,
43862306a36Sopenharmony_ci	CRIT_THRESH_4,
43962306a36Sopenharmony_ci	CRIT_THRESH_5,
44062306a36Sopenharmony_ci	CRIT_THRESH_6,
44162306a36Sopenharmony_ci	CRIT_THRESH_7,
44262306a36Sopenharmony_ci	CRIT_THRESH_8,
44362306a36Sopenharmony_ci	CRIT_THRESH_9,
44462306a36Sopenharmony_ci	CRIT_THRESH_10,
44562306a36Sopenharmony_ci	CRIT_THRESH_11,
44662306a36Sopenharmony_ci	CRIT_THRESH_12,
44762306a36Sopenharmony_ci	CRIT_THRESH_13,
44862306a36Sopenharmony_ci	CRIT_THRESH_14,
44962306a36Sopenharmony_ci	CRIT_THRESH_15,
45062306a36Sopenharmony_ci
45162306a36Sopenharmony_ci	/* WATCHDOG */
45262306a36Sopenharmony_ci	WDOG_BARK_STATUS,
45362306a36Sopenharmony_ci	WDOG_BARK_CLEAR,
45462306a36Sopenharmony_ci	WDOG_BARK_MASK,
45562306a36Sopenharmony_ci	WDOG_BARK_COUNT,
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ci	/* CYCLE COMPLETION MONITOR */
45862306a36Sopenharmony_ci	CC_MON_STATUS,
45962306a36Sopenharmony_ci	CC_MON_CLEAR,
46062306a36Sopenharmony_ci	CC_MON_MASK,
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_ci	MIN_STATUS_0,		/* MIN threshold violated */
46362306a36Sopenharmony_ci	MIN_STATUS_1,
46462306a36Sopenharmony_ci	MIN_STATUS_2,
46562306a36Sopenharmony_ci	MIN_STATUS_3,
46662306a36Sopenharmony_ci	MIN_STATUS_4,
46762306a36Sopenharmony_ci	MIN_STATUS_5,
46862306a36Sopenharmony_ci	MIN_STATUS_6,
46962306a36Sopenharmony_ci	MIN_STATUS_7,
47062306a36Sopenharmony_ci	MIN_STATUS_8,
47162306a36Sopenharmony_ci	MIN_STATUS_9,
47262306a36Sopenharmony_ci	MIN_STATUS_10,
47362306a36Sopenharmony_ci	MIN_STATUS_11,
47462306a36Sopenharmony_ci	MIN_STATUS_12,
47562306a36Sopenharmony_ci	MIN_STATUS_13,
47662306a36Sopenharmony_ci	MIN_STATUS_14,
47762306a36Sopenharmony_ci	MIN_STATUS_15,
47862306a36Sopenharmony_ci	MAX_STATUS_0,		/* MAX threshold violated */
47962306a36Sopenharmony_ci	MAX_STATUS_1,
48062306a36Sopenharmony_ci	MAX_STATUS_2,
48162306a36Sopenharmony_ci	MAX_STATUS_3,
48262306a36Sopenharmony_ci	MAX_STATUS_4,
48362306a36Sopenharmony_ci	MAX_STATUS_5,
48462306a36Sopenharmony_ci	MAX_STATUS_6,
48562306a36Sopenharmony_ci	MAX_STATUS_7,
48662306a36Sopenharmony_ci	MAX_STATUS_8,
48762306a36Sopenharmony_ci	MAX_STATUS_9,
48862306a36Sopenharmony_ci	MAX_STATUS_10,
48962306a36Sopenharmony_ci	MAX_STATUS_11,
49062306a36Sopenharmony_ci	MAX_STATUS_12,
49162306a36Sopenharmony_ci	MAX_STATUS_13,
49262306a36Sopenharmony_ci	MAX_STATUS_14,
49362306a36Sopenharmony_ci	MAX_STATUS_15,
49462306a36Sopenharmony_ci
49562306a36Sopenharmony_ci	/* Keep last */
49662306a36Sopenharmony_ci	MAX_REGFIELDS
49762306a36Sopenharmony_ci};
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci/**
50062306a36Sopenharmony_ci * struct tsens_features - Features supported by the IP
50162306a36Sopenharmony_ci * @ver_major: Major number of IP version
50262306a36Sopenharmony_ci * @crit_int: does the IP support critical interrupts?
50362306a36Sopenharmony_ci * @combo_int: does the IP use one IRQ for up, low and critical thresholds?
50462306a36Sopenharmony_ci * @adc:      do the sensors only output adc code (instead of temperature)?
50562306a36Sopenharmony_ci * @srot_split: does the IP neatly splits the register space into SROT and TM,
50662306a36Sopenharmony_ci *              with SROT only being available to secure boot firmware?
50762306a36Sopenharmony_ci * @has_watchdog: does this IP support watchdog functionality?
50862306a36Sopenharmony_ci * @max_sensors: maximum sensors supported by this version of the IP
50962306a36Sopenharmony_ci * @trip_min_temp: minimum trip temperature supported by this version of the IP
51062306a36Sopenharmony_ci * @trip_max_temp: maximum trip temperature supported by this version of the IP
51162306a36Sopenharmony_ci */
51262306a36Sopenharmony_cistruct tsens_features {
51362306a36Sopenharmony_ci	unsigned int ver_major;
51462306a36Sopenharmony_ci	unsigned int crit_int:1;
51562306a36Sopenharmony_ci	unsigned int combo_int:1;
51662306a36Sopenharmony_ci	unsigned int adc:1;
51762306a36Sopenharmony_ci	unsigned int srot_split:1;
51862306a36Sopenharmony_ci	unsigned int has_watchdog:1;
51962306a36Sopenharmony_ci	unsigned int max_sensors;
52062306a36Sopenharmony_ci	int trip_min_temp;
52162306a36Sopenharmony_ci	int trip_max_temp;
52262306a36Sopenharmony_ci};
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci/**
52562306a36Sopenharmony_ci * struct tsens_plat_data - tsens compile-time platform data
52662306a36Sopenharmony_ci * @num_sensors: Number of sensors supported by platform
52762306a36Sopenharmony_ci * @ops: operations the tsens instance supports
52862306a36Sopenharmony_ci * @hw_ids: Subset of sensors ids supported by platform, if not the first n
52962306a36Sopenharmony_ci * @feat: features of the IP
53062306a36Sopenharmony_ci * @fields: bitfield locations
53162306a36Sopenharmony_ci */
53262306a36Sopenharmony_cistruct tsens_plat_data {
53362306a36Sopenharmony_ci	const u32		num_sensors;
53462306a36Sopenharmony_ci	const struct tsens_ops	*ops;
53562306a36Sopenharmony_ci	unsigned int		*hw_ids;
53662306a36Sopenharmony_ci	struct tsens_features	*feat;
53762306a36Sopenharmony_ci	const struct reg_field		*fields;
53862306a36Sopenharmony_ci};
53962306a36Sopenharmony_ci
54062306a36Sopenharmony_ci/**
54162306a36Sopenharmony_ci * struct tsens_context - Registers to be saved/restored across a context loss
54262306a36Sopenharmony_ci * @threshold: Threshold register value
54362306a36Sopenharmony_ci * @control: Control register value
54462306a36Sopenharmony_ci */
54562306a36Sopenharmony_cistruct tsens_context {
54662306a36Sopenharmony_ci	int	threshold;
54762306a36Sopenharmony_ci	int	control;
54862306a36Sopenharmony_ci};
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci/**
55162306a36Sopenharmony_ci * struct tsens_priv - private data for each instance of the tsens IP
55262306a36Sopenharmony_ci * @dev: pointer to struct device
55362306a36Sopenharmony_ci * @num_sensors: number of sensors enabled on this device
55462306a36Sopenharmony_ci * @tm_map: pointer to TM register address space
55562306a36Sopenharmony_ci * @srot_map: pointer to SROT register address space
55662306a36Sopenharmony_ci * @tm_offset: deal with old device trees that don't address TM and SROT
55762306a36Sopenharmony_ci *             address space separately
55862306a36Sopenharmony_ci * @ul_lock: lock while processing upper/lower threshold interrupts
55962306a36Sopenharmony_ci * @crit_lock: lock while processing critical threshold interrupts
56062306a36Sopenharmony_ci * @rf: array of regmap_fields used to store value of the field
56162306a36Sopenharmony_ci * @ctx: registers to be saved and restored during suspend/resume
56262306a36Sopenharmony_ci * @feat: features of the IP
56362306a36Sopenharmony_ci * @fields: bitfield locations
56462306a36Sopenharmony_ci * @ops: pointer to list of callbacks supported by this device
56562306a36Sopenharmony_ci * @debug_root: pointer to debugfs dentry for all tsens
56662306a36Sopenharmony_ci * @debug: pointer to debugfs dentry for tsens controller
56762306a36Sopenharmony_ci * @sensor: list of sensors attached to this device
56862306a36Sopenharmony_ci */
56962306a36Sopenharmony_cistruct tsens_priv {
57062306a36Sopenharmony_ci	struct device			*dev;
57162306a36Sopenharmony_ci	u32				num_sensors;
57262306a36Sopenharmony_ci	struct regmap			*tm_map;
57362306a36Sopenharmony_ci	struct regmap			*srot_map;
57462306a36Sopenharmony_ci	u32				tm_offset;
57562306a36Sopenharmony_ci
57662306a36Sopenharmony_ci	/* lock for upper/lower threshold interrupts */
57762306a36Sopenharmony_ci	spinlock_t			ul_lock;
57862306a36Sopenharmony_ci
57962306a36Sopenharmony_ci	struct regmap_field		*rf[MAX_REGFIELDS];
58062306a36Sopenharmony_ci	struct tsens_context		ctx;
58162306a36Sopenharmony_ci	struct tsens_features		*feat;
58262306a36Sopenharmony_ci	const struct reg_field		*fields;
58362306a36Sopenharmony_ci	const struct tsens_ops		*ops;
58462306a36Sopenharmony_ci
58562306a36Sopenharmony_ci	struct dentry			*debug_root;
58662306a36Sopenharmony_ci	struct dentry			*debug;
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci	struct tsens_sensor		sensor[];
58962306a36Sopenharmony_ci};
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci/**
59262306a36Sopenharmony_ci * struct tsens_single_value - internal representation of a single field inside nvmem calibration data
59362306a36Sopenharmony_ci * @idx: index into the u32 data array
59462306a36Sopenharmony_ci * @shift: the shift of the first bit in the value
59562306a36Sopenharmony_ci * @blob: index of the data blob to use for this cell
59662306a36Sopenharmony_ci */
59762306a36Sopenharmony_cistruct tsens_single_value {
59862306a36Sopenharmony_ci	u8 idx;
59962306a36Sopenharmony_ci	u8 shift;
60062306a36Sopenharmony_ci	u8 blob;
60162306a36Sopenharmony_ci};
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci/**
60462306a36Sopenharmony_ci * struct tsens_legacy_calibration_format - description of calibration data used when parsing the legacy nvmem blob
60562306a36Sopenharmony_ci * @base_len: the length of the base fields inside calibration data
60662306a36Sopenharmony_ci * @base_shift: the shift to be applied to base data
60762306a36Sopenharmony_ci * @sp_len: the length of the sN_pM fields inside calibration data
60862306a36Sopenharmony_ci * @mode: descriptor of the calibration mode field
60962306a36Sopenharmony_ci * @invalid: descriptor of the calibration mode invalid field
61062306a36Sopenharmony_ci * @base: descriptors of the base0 and base1 fields
61162306a36Sopenharmony_ci * @sp: descriptors of the sN_pM fields
61262306a36Sopenharmony_ci */
61362306a36Sopenharmony_cistruct tsens_legacy_calibration_format {
61462306a36Sopenharmony_ci	unsigned int base_len;
61562306a36Sopenharmony_ci	unsigned int base_shift;
61662306a36Sopenharmony_ci	unsigned int sp_len;
61762306a36Sopenharmony_ci	/* just two bits */
61862306a36Sopenharmony_ci	struct tsens_single_value mode;
61962306a36Sopenharmony_ci	/* on all platforms except 8974 invalid is the third bit of what downstream calls 'mode' */
62062306a36Sopenharmony_ci	struct tsens_single_value invalid;
62162306a36Sopenharmony_ci	struct tsens_single_value base[2];
62262306a36Sopenharmony_ci	struct tsens_single_value sp[][2];
62362306a36Sopenharmony_ci};
62462306a36Sopenharmony_ci
62562306a36Sopenharmony_cichar *qfprom_read(struct device *dev, const char *cname);
62662306a36Sopenharmony_ciint tsens_read_calibration_legacy(struct tsens_priv *priv,
62762306a36Sopenharmony_ci				  const struct tsens_legacy_calibration_format *format,
62862306a36Sopenharmony_ci				  u32 *p1, u32 *p2,
62962306a36Sopenharmony_ci				  u32 *cdata, u32 *csel);
63062306a36Sopenharmony_ciint tsens_read_calibration(struct tsens_priv *priv, int shift, u32 *p1, u32 *p2, bool backup);
63162306a36Sopenharmony_ciint tsens_calibrate_nvmem(struct tsens_priv *priv, int shift);
63262306a36Sopenharmony_ciint tsens_calibrate_common(struct tsens_priv *priv);
63362306a36Sopenharmony_civoid compute_intercept_slope(struct tsens_priv *priv, u32 *pt1, u32 *pt2, u32 mode);
63462306a36Sopenharmony_ciint init_common(struct tsens_priv *priv);
63562306a36Sopenharmony_ciint get_temp_tsens_valid(const struct tsens_sensor *s, int *temp);
63662306a36Sopenharmony_ciint get_temp_common(const struct tsens_sensor *s, int *temp);
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_ci/* TSENS target */
63962306a36Sopenharmony_ciextern struct tsens_plat_data data_8960;
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci/* TSENS v0.1 targets */
64262306a36Sopenharmony_ciextern struct tsens_plat_data data_8226, data_8909, data_8916, data_8939, data_8974, data_9607;
64362306a36Sopenharmony_ci
64462306a36Sopenharmony_ci/* TSENS v1 targets */
64562306a36Sopenharmony_ciextern struct tsens_plat_data data_tsens_v1, data_8976, data_8956;
64662306a36Sopenharmony_ci
64762306a36Sopenharmony_ci/* TSENS v2 targets */
64862306a36Sopenharmony_ciextern struct tsens_plat_data data_8996, data_ipq8074, data_tsens_v2;
64962306a36Sopenharmony_ci
65062306a36Sopenharmony_ci#endif /* __QCOM_TSENS_H__ */
651