xref: /openbmc/linux/drivers/thermal/qcom/tsens-v1.c (revision 234489ac)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2019, Linaro Limited
4  */
5 
6 #include <linux/bitops.h>
7 #include <linux/regmap.h>
8 #include <linux/delay.h>
9 #include <linux/slab.h>
10 #include "tsens.h"
11 
12 /* ----- SROT ------ */
13 #define SROT_HW_VER_OFF	0x0000
14 #define SROT_CTRL_OFF		0x0004
15 
16 /* ----- TM ------ */
17 #define TM_INT_EN_OFF				0x0000
18 #define TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF	0x0004
19 #define TM_Sn_STATUS_OFF			0x0044
20 #define TM_TRDY_OFF				0x0084
21 #define TM_HIGH_LOW_INT_STATUS_OFF		0x0088
22 #define TM_HIGH_LOW_Sn_INT_THRESHOLD_OFF	0x0090
23 
24 struct tsens_legacy_calibration_format tsens_qcs404_nvmem = {
25 	.base_len = 8,
26 	.base_shift = 2,
27 	.sp_len = 6,
28 	.mode = { 4, 0 },
29 	.invalid = { 4, 2 },
30 	.base = { { 4, 3 }, { 4, 11 } },
31 	.sp = {
32 		{ { 0, 0 }, { 0, 6 } },
33 		{ { 0, 12 }, { 0, 18 } },
34 		{ { 0, 24 }, { 0, 30 } },
35 		{ { 1, 4 },  { 1, 10 } },
36 		{ { 1, 16 }, { 1, 22 } },
37 		{ { 2, 0 },  { 2, 6 } },
38 		{ { 2, 12 }, { 2, 18 } },
39 		{ { 2, 24 }, { 2, 30 } },
40 		{ { 3, 4 },  { 3, 10 } },
41 		{ { 3, 16 }, { 3, 22 } },
42 	},
43 };
44 
45 struct tsens_legacy_calibration_format tsens_8976_nvmem = {
46 	.base_len = 8,
47 	.base_shift = 2,
48 	.sp_len = 6,
49 	.mode = { 4, 0 },
50 	.invalid = { 4, 2 },
51 	.base = { { 0, 0 }, { 2, 8 } },
52 	.sp = {
53 		{ { 0, 8 },  { 0, 14 } },
54 		{ { 0, 20 }, { 0, 26 } },
55 		{ { 1, 0 },  { 1, 6 } },
56 		{ { 1, 12 }, { 1, 18 } },
57 		{ { 2, 8 },  { 2, 14 } },
58 		{ { 2, 20 }, { 2, 26 } },
59 		{ { 3, 0 },  { 3, 6 } },
60 		{ { 3, 12 }, { 3, 18 } },
61 		{ { 4, 2 },  { 4, 9 } },
62 		{ { 4, 14 }, { 4, 21 } },
63 		{ { 4, 26 }, { 5, 1 } },
64 	},
65 };
66 
67 static int calibrate_v1(struct tsens_priv *priv)
68 {
69 	u32 p1[10], p2[10];
70 	u32 *qfprom_cdata;
71 	int mode, ret;
72 
73 	ret = tsens_calibrate_common(priv);
74 	if (!ret)
75 		return 0;
76 
77 	qfprom_cdata = (u32 *)qfprom_read(priv->dev, "calib");
78 	if (IS_ERR(qfprom_cdata))
79 		return PTR_ERR(qfprom_cdata);
80 
81 	mode = tsens_read_calibration_legacy(priv, &tsens_qcs404_nvmem,
82 					     p1, p2,
83 					     qfprom_cdata, NULL);
84 
85 	compute_intercept_slope(priv, p1, p2, mode);
86 	kfree(qfprom_cdata);
87 
88 	return 0;
89 }
90 
91 /* v1.x: msm8956,8976,qcs404,405 */
92 
93 static struct tsens_features tsens_v1_feat = {
94 	.ver_major	= VER_1_X,
95 	.crit_int	= 0,
96 	.combo_int	= 0,
97 	.adc		= 1,
98 	.srot_split	= 1,
99 	.max_sensors	= 11,
100 	.trip_min_temp	= -40000,
101 	.trip_max_temp	= 120000,
102 };
103 
104 static const struct reg_field tsens_v1_regfields[MAX_REGFIELDS] = {
105 	/* ----- SROT ------ */
106 	/* VERSION */
107 	[VER_MAJOR] = REG_FIELD(SROT_HW_VER_OFF, 28, 31),
108 	[VER_MINOR] = REG_FIELD(SROT_HW_VER_OFF, 16, 27),
109 	[VER_STEP]  = REG_FIELD(SROT_HW_VER_OFF,  0, 15),
110 	/* CTRL_OFFSET */
111 	[TSENS_EN]     = REG_FIELD(SROT_CTRL_OFF, 0,  0),
112 	[TSENS_SW_RST] = REG_FIELD(SROT_CTRL_OFF, 1,  1),
113 	[SENSOR_EN]    = REG_FIELD(SROT_CTRL_OFF, 3, 13),
114 
115 	/* ----- TM ------ */
116 	/* INTERRUPT ENABLE */
117 	[INT_EN]     = REG_FIELD(TM_INT_EN_OFF, 0, 0),
118 
119 	/* UPPER/LOWER TEMPERATURE THRESHOLDS */
120 	REG_FIELD_FOR_EACH_SENSOR11(LOW_THRESH,    TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF,  0,  9),
121 	REG_FIELD_FOR_EACH_SENSOR11(UP_THRESH,     TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 10, 19),
122 
123 	/* UPPER/LOWER INTERRUPTS [CLEAR/STATUS] */
124 	REG_FIELD_FOR_EACH_SENSOR11(LOW_INT_CLEAR, TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 20, 20),
125 	REG_FIELD_FOR_EACH_SENSOR11(UP_INT_CLEAR,  TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 21, 21),
126 	[LOW_INT_STATUS_0] = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  0,  0),
127 	[LOW_INT_STATUS_1] = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  1,  1),
128 	[LOW_INT_STATUS_2] = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  2,  2),
129 	[LOW_INT_STATUS_3] = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  3,  3),
130 	[LOW_INT_STATUS_4] = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  4,  4),
131 	[LOW_INT_STATUS_5] = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  5,  5),
132 	[LOW_INT_STATUS_6] = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  6,  6),
133 	[LOW_INT_STATUS_7] = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  7,  7),
134 	[UP_INT_STATUS_0]  = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  8,  8),
135 	[UP_INT_STATUS_1]  = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF,  9,  9),
136 	[UP_INT_STATUS_2]  = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF, 10, 10),
137 	[UP_INT_STATUS_3]  = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF, 11, 11),
138 	[UP_INT_STATUS_4]  = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF, 12, 12),
139 	[UP_INT_STATUS_5]  = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF, 13, 13),
140 	[UP_INT_STATUS_6]  = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF, 14, 14),
141 	[UP_INT_STATUS_7]  = REG_FIELD(TM_HIGH_LOW_INT_STATUS_OFF, 15, 15),
142 
143 	/* NO CRITICAL INTERRUPT SUPPORT on v1 */
144 
145 	/* Sn_STATUS */
146 	REG_FIELD_FOR_EACH_SENSOR11(LAST_TEMP,    TM_Sn_STATUS_OFF,  0,  9),
147 	REG_FIELD_FOR_EACH_SENSOR11(VALID,        TM_Sn_STATUS_OFF, 14, 14),
148 	/* xxx_STATUS bits: 1 == threshold violated */
149 	REG_FIELD_FOR_EACH_SENSOR11(MIN_STATUS,   TM_Sn_STATUS_OFF, 10, 10),
150 	REG_FIELD_FOR_EACH_SENSOR11(LOWER_STATUS, TM_Sn_STATUS_OFF, 11, 11),
151 	REG_FIELD_FOR_EACH_SENSOR11(UPPER_STATUS, TM_Sn_STATUS_OFF, 12, 12),
152 	/* No CRITICAL field on v1.x */
153 	REG_FIELD_FOR_EACH_SENSOR11(MAX_STATUS,   TM_Sn_STATUS_OFF, 13, 13),
154 
155 	/* TRDY: 1=ready, 0=in progress */
156 	[TRDY] = REG_FIELD(TM_TRDY_OFF, 0, 0),
157 };
158 
159 static int __init init_8956(struct tsens_priv *priv) {
160 	priv->sensor[0].slope = 3313;
161 	priv->sensor[1].slope = 3275;
162 	priv->sensor[2].slope = 3320;
163 	priv->sensor[3].slope = 3246;
164 	priv->sensor[4].slope = 3279;
165 	priv->sensor[5].slope = 3257;
166 	priv->sensor[6].slope = 3234;
167 	priv->sensor[7].slope = 3269;
168 	priv->sensor[8].slope = 3255;
169 	priv->sensor[9].slope = 3239;
170 	priv->sensor[10].slope = 3286;
171 
172 	return init_common(priv);
173 }
174 
175 static const struct tsens_ops ops_generic_v1 = {
176 	.init		= init_common,
177 	.calibrate	= calibrate_v1,
178 	.get_temp	= get_temp_tsens_valid,
179 };
180 
181 struct tsens_plat_data data_tsens_v1 = {
182 	.ops		= &ops_generic_v1,
183 	.feat		= &tsens_v1_feat,
184 	.fields	= tsens_v1_regfields,
185 };
186 
187 static const struct tsens_ops ops_8956 = {
188 	.init		= init_8956,
189 	.calibrate	= tsens_calibrate_common,
190 	.get_temp	= get_temp_tsens_valid,
191 };
192 
193 struct tsens_plat_data data_8956 = {
194 	.num_sensors	= 11,
195 	.ops		= &ops_8956,
196 	.feat		= &tsens_v1_feat,
197 	.fields		= tsens_v1_regfields,
198 };
199 
200 static const struct tsens_ops ops_8976 = {
201 	.init		= init_common,
202 	.calibrate	= tsens_calibrate_common,
203 	.get_temp	= get_temp_tsens_valid,
204 };
205 
206 struct tsens_plat_data data_8976 = {
207 	.num_sensors	= 11,
208 	.ops		= &ops_8976,
209 	.feat		= &tsens_v1_feat,
210 	.fields		= tsens_v1_regfields,
211 };
212