1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
6 #include <linux/bitfield.h>
7 #include <linux/nvmem-consumer.h>
8 #include <linux/platform_device.h>
11 /* ----- SROT ------ */
12 #define SROT_CTRL_OFF 0x0000
15 #define TM_INT_EN_OFF 0x0000
16 #define TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF 0x0004
17 #define TM_Sn_STATUS_OFF 0x0030
18 #define TM_TRDY_OFF 0x005c
20 /* extra data for 8974 */
22 #define BKP_REDUN_SEL 0xe0000000
24 #define BIT_APPEND 0x3
26 struct tsens_legacy_calibration_format tsens_8916_nvmem = {
31 .invalid = { 0, 31, 1 },
32 .base = { { 0, 0 }, { 1, 25 } },
34 { { 0, 7 }, { 0, 12 } },
35 { { 0, 17 }, { 0, 22 } },
36 { { 0, 27 }, { 1, 0 } },
37 { { 1, 5 }, { 1, 10 } },
38 { { 1, 15 }, { 1, 20 } },
42 struct tsens_legacy_calibration_format tsens_8939_nvmem = {
48 .base = { { 0, 0 }, { 1, 24 } },
50 { { 12, 3 }, { 12, 9 } },
51 { { 12, 15 }, { 12, 21 } },
52 { { 12, 27 }, { 13, 1 } },
53 { { 13, 7 }, { 13, 13 } },
54 { { 13, 19 }, { 13, 25 } },
55 { { 0, 8 }, { 0, 14 } },
56 { { 0, 20 }, { 0, 26 } },
57 { { 1, 0 }, { 1, 6 } },
58 { { 1, 12 }, { 1, 18 } },
62 struct tsens_legacy_calibration_format tsens_8974_nvmem = {
68 .base = { { 0, 0 }, { 2, 12 } },
70 { { 0, 8 }, { 2, 20 } },
71 { { 0, 14 }, { 2, 26 } },
72 { { 0, 20 }, { 3, 0 } },
73 { { 0, 26 }, { 3, 6 } },
74 { { 1, 0 }, { 3, 12 } },
75 { { 1, 6 }, { 3, 18 } },
76 { { 1, 12 }, { 3, 24 } },
77 { { 1, 18 }, { 4, 0 } },
78 { { 1, 24 }, { 4, 6 } },
79 { { 2, 0 }, { 4, 12 } },
80 { { 2, 6 }, { 4, 18 } },
84 struct tsens_legacy_calibration_format tsens_8974_backup_nvmem = {
89 .invalid = { 5, 30, 1 },
90 .base = { { 0, 0 }, { 2, 18 } },
92 { { 0, 8 }, { 2, 26 } },
93 { { 0, 14 }, { 3, 0 } },
94 { { 0, 20 }, { 3, 6 } },
95 { { 0, 26 }, { 3, 12 } },
96 { { 1, 0 }, { 3, 18 } },
97 { { 1, 6 }, { 3, 24, 1 } },
98 { { 1, 12 }, { 4, 0, 1 } },
99 { { 1, 18 }, { 4, 6, 1 } },
100 { { 2, 0 }, { 4, 12, 1 } },
101 { { 2, 6 }, { 4, 18, 1 } },
102 { { 2, 12 }, { 4, 24, 1 } },
106 struct tsens_legacy_calibration_format tsens_9607_nvmem = {
111 .invalid = { 2, 22 },
112 .base = { { 0, 0 }, { 2, 12 } },
114 { { 0, 8 }, { 0, 14 } },
115 { { 0, 20 }, { 0, 26 } },
116 { { 1, 0 }, { 1, 6 } },
117 { { 1, 12 }, { 1, 18 } },
118 { { 2, 0 }, { 2, 6 } },
122 static int calibrate_8916(struct tsens_priv *priv)
125 u32 *qfprom_cdata, *qfprom_csel;
128 ret = tsens_calibrate_nvmem(priv, 3);
132 qfprom_cdata = (u32 *)qfprom_read(priv->dev, "calib");
133 if (IS_ERR(qfprom_cdata))
134 return PTR_ERR(qfprom_cdata);
136 qfprom_csel = (u32 *)qfprom_read(priv->dev, "calib_sel");
137 if (IS_ERR(qfprom_csel)) {
139 return PTR_ERR(qfprom_csel);
142 mode = tsens_read_calibration_legacy(priv, &tsens_8916_nvmem,
144 qfprom_cdata, qfprom_csel);
146 compute_intercept_slope(priv, p1, p2, mode);
153 static void fixup_8974_points(int mode, u32 *p1, u32 *p2)
157 if (mode == NO_PT_CALIB) {
170 for (i = 0; i < 11; i++) {
172 * ONE_PT_CALIB requires using addition here instead of
173 * using OR operation.
182 static int calibrate_8974_nvmem(struct tsens_priv *priv)
188 ret = nvmem_cell_read_variable_le_u32(priv->dev, "use_backup", &backup);
190 dev_warn(priv->dev, "Please migrate to separate nvmem cells for calibration data\n");
194 mode = tsens_read_calibration(priv, 2, p1, p2, backup == BKP_SEL);
198 fixup_8974_points(mode, p1, p2);
200 compute_intercept_slope(priv, p1, p2, mode);
205 static int calibrate_8974(struct tsens_priv *priv)
212 ret = calibrate_8974_nvmem(priv);
216 calib = (u32 *)qfprom_read(priv->dev, "calib");
218 return PTR_ERR(calib);
220 bkp = (u32 *)qfprom_read(priv->dev, "calib_backup");
226 calib_redun_sel = FIELD_GET(BKP_REDUN_SEL, bkp[1]);
228 if (calib_redun_sel == BKP_SEL)
229 mode = tsens_read_calibration_legacy(priv, &tsens_8974_backup_nvmem,
233 mode = tsens_read_calibration_legacy(priv, &tsens_8974_nvmem,
237 fixup_8974_points(mode, p1, p2);
239 compute_intercept_slope(priv, p1, p2, mode);
246 static int __init init_8939(struct tsens_priv *priv) {
247 priv->sensor[0].slope = 2911;
248 priv->sensor[1].slope = 2789;
249 priv->sensor[2].slope = 2906;
250 priv->sensor[3].slope = 2763;
251 priv->sensor[4].slope = 2922;
252 priv->sensor[5].slope = 2867;
253 priv->sensor[6].slope = 2833;
254 priv->sensor[7].slope = 2838;
255 priv->sensor[8].slope = 2840;
256 /* priv->sensor[9].slope = 2852; */
258 return init_common(priv);
261 /* v0.1: 8916, 8939, 8974, 9607 */
263 static struct tsens_features tsens_v0_1_feat = {
264 .ver_major = VER_0_1,
270 .trip_min_temp = -40000,
271 .trip_max_temp = 120000,
274 static const struct reg_field tsens_v0_1_regfields[MAX_REGFIELDS] = {
275 /* ----- SROT ------ */
276 /* No VERSION information */
279 [TSENS_EN] = REG_FIELD(SROT_CTRL_OFF, 0, 0),
280 [TSENS_SW_RST] = REG_FIELD(SROT_CTRL_OFF, 1, 1),
282 /* ----- TM ------ */
283 /* INTERRUPT ENABLE */
284 [INT_EN] = REG_FIELD(TM_INT_EN_OFF, 0, 0),
286 /* UPPER/LOWER TEMPERATURE THRESHOLDS */
287 REG_FIELD_FOR_EACH_SENSOR11(LOW_THRESH, TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 0, 9),
288 REG_FIELD_FOR_EACH_SENSOR11(UP_THRESH, TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 10, 19),
290 /* UPPER/LOWER INTERRUPTS [CLEAR/STATUS] */
291 REG_FIELD_FOR_EACH_SENSOR11(LOW_INT_CLEAR, TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 20, 20),
292 REG_FIELD_FOR_EACH_SENSOR11(UP_INT_CLEAR, TM_Sn_UPPER_LOWER_STATUS_CTRL_OFF, 21, 21),
294 /* NO CRITICAL INTERRUPT SUPPORT on v0.1 */
297 REG_FIELD_FOR_EACH_SENSOR11(LAST_TEMP, TM_Sn_STATUS_OFF, 0, 9),
298 /* No VALID field on v0.1 */
299 /* xxx_STATUS bits: 1 == threshold violated */
300 REG_FIELD_FOR_EACH_SENSOR11(MIN_STATUS, TM_Sn_STATUS_OFF, 10, 10),
301 REG_FIELD_FOR_EACH_SENSOR11(LOWER_STATUS, TM_Sn_STATUS_OFF, 11, 11),
302 REG_FIELD_FOR_EACH_SENSOR11(UPPER_STATUS, TM_Sn_STATUS_OFF, 12, 12),
303 /* No CRITICAL field on v0.1 */
304 REG_FIELD_FOR_EACH_SENSOR11(MAX_STATUS, TM_Sn_STATUS_OFF, 13, 13),
306 /* TRDY: 1=ready, 0=in progress */
307 [TRDY] = REG_FIELD(TM_TRDY_OFF, 0, 0),
310 static const struct tsens_ops ops_v0_1 = {
312 .calibrate = tsens_calibrate_common,
313 .get_temp = get_temp_common,
316 static const struct tsens_ops ops_8916 = {
318 .calibrate = calibrate_8916,
319 .get_temp = get_temp_common,
322 struct tsens_plat_data data_8916 = {
325 .hw_ids = (unsigned int []){0, 1, 2, 4, 5 },
327 .feat = &tsens_v0_1_feat,
328 .fields = tsens_v0_1_regfields,
331 static const struct tsens_ops ops_8939 = {
333 .calibrate = tsens_calibrate_common,
334 .get_temp = get_temp_common,
337 struct tsens_plat_data data_8939 = {
340 .hw_ids = (unsigned int []){ 0, 1, 2, 3, 5, 6, 7, 8, 9, /* 10 */ },
342 .feat = &tsens_v0_1_feat,
343 .fields = tsens_v0_1_regfields,
346 static const struct tsens_ops ops_8974 = {
348 .calibrate = calibrate_8974,
349 .get_temp = get_temp_common,
352 struct tsens_plat_data data_8974 = {
355 .feat = &tsens_v0_1_feat,
356 .fields = tsens_v0_1_regfields,
359 struct tsens_plat_data data_9607 = {
362 .feat = &tsens_v0_1_feat,
363 .fields = tsens_v0_1_regfields,