1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
6 #include <linux/platform_device.h>
7 #include <linux/delay.h>
8 #include <linux/bitops.h>
9 #include <linux/regmap.h>
10 #include <linux/thermal.h>
13 #define CONFIG_ADDR 0x3640
14 #define CONFIG_ADDR_8660 0x3620
15 /* CONFIG_ADDR bitmasks */
17 #define CONFIG_MASK 0xf
19 #define CONFIG_SHIFT_8660 28
20 #define CONFIG_MASK_8660 (3 << CONFIG_SHIFT_8660)
22 #define CNTL_ADDR 0x3620
23 /* CNTL_ADDR bitmasks */
27 #define MEASURE_PERIOD BIT(18)
28 #define SLP_CLK_ENA BIT(26)
29 #define SLP_CLK_ENA_8660 BIT(24)
30 #define SENSOR0_SHIFT 3
32 #define THRESHOLD_ADDR 0x3624
34 #define INT_STATUS_ADDR 0x363c
36 #define S0_STATUS_OFF 0x3628
37 #define S1_STATUS_OFF 0x362c
38 #define S2_STATUS_OFF 0x3630
39 #define S3_STATUS_OFF 0x3634
40 #define S4_STATUS_OFF 0x3638
41 #define S5_STATUS_OFF 0x3664 /* Sensors 5-10 found on apq8064/msm8960 */
42 #define S6_STATUS_OFF 0x3668
43 #define S7_STATUS_OFF 0x366c
44 #define S8_STATUS_OFF 0x3670
45 #define S9_STATUS_OFF 0x3674
46 #define S10_STATUS_OFF 0x3678
48 /* Original slope - 350 to compensate mC to C inaccuracy */
49 static u32 tsens_msm8960_slope
[] = {
55 static int suspend_8960(struct tsens_priv
*priv
)
59 struct regmap
*map
= priv
->tm_map
;
61 ret
= regmap_read(map
, THRESHOLD_ADDR
, &priv
->ctx
.threshold
);
65 ret
= regmap_read(map
, CNTL_ADDR
, &priv
->ctx
.control
);
69 if (priv
->num_sensors
> 1)
70 mask
= SLP_CLK_ENA
| EN
;
72 mask
= SLP_CLK_ENA_8660
| EN
;
74 ret
= regmap_update_bits(map
, CNTL_ADDR
, mask
, 0);
81 static int resume_8960(struct tsens_priv
*priv
)
84 struct regmap
*map
= priv
->tm_map
;
86 ret
= regmap_update_bits(map
, CNTL_ADDR
, SW_RST
, SW_RST
);
91 * Separate CONFIG restore is not needed only for 8660 as
92 * config is part of CTRL Addr and its restored as such
94 if (priv
->num_sensors
> 1) {
95 ret
= regmap_update_bits(map
, CONFIG_ADDR
, CONFIG_MASK
, CONFIG
);
100 ret
= regmap_write(map
, THRESHOLD_ADDR
, priv
->ctx
.threshold
);
104 ret
= regmap_write(map
, CNTL_ADDR
, priv
->ctx
.control
);
111 static int enable_8960(struct tsens_priv
*priv
, int id
)
114 u32 reg
, mask
= BIT(id
);
116 ret
= regmap_read(priv
->tm_map
, CNTL_ADDR
, ®
);
121 * On platforms with more than 6 sensors, all remaining sensors
122 * must be enabled together, otherwise undefined results are expected.
123 * (Sensor 6-7 disabled, Sensor 3 disabled...) In the original driver,
124 * all the sensors are enabled in one step hence this bug is not
128 mask
= GENMASK(10, 6);
130 mask
<<= SENSOR0_SHIFT
;
132 /* Sensors already enabled. Skip. */
133 if ((reg
& mask
) == mask
)
136 ret
= regmap_write(priv
->tm_map
, CNTL_ADDR
, reg
| SW_RST
);
140 reg
|= MEASURE_PERIOD
;
142 if (priv
->num_sensors
> 1)
143 reg
|= mask
| SLP_CLK_ENA
| EN
;
145 reg
|= mask
| SLP_CLK_ENA_8660
| EN
;
147 ret
= regmap_write(priv
->tm_map
, CNTL_ADDR
, reg
);
154 static void disable_8960(struct tsens_priv
*priv
)
160 mask
= GENMASK(priv
->num_sensors
- 1, 0);
161 mask
<<= SENSOR0_SHIFT
;
164 ret
= regmap_read(priv
->tm_map
, CNTL_ADDR
, ®_cntl
);
170 if (priv
->num_sensors
> 1)
171 reg_cntl
&= ~SLP_CLK_ENA
;
173 reg_cntl
&= ~SLP_CLK_ENA_8660
;
175 regmap_write(priv
->tm_map
, CNTL_ADDR
, reg_cntl
);
178 static int calibrate_8960(struct tsens_priv
*priv
)
184 data
= qfprom_read(priv
->dev
, "calib");
186 data
= qfprom_read(priv
->dev
, "calib_backup");
188 return PTR_ERR(data
);
190 for (i
= 0; i
< priv
->num_sensors
; i
++) {
192 priv
->sensor
[i
].slope
= tsens_msm8960_slope
[i
];
195 compute_intercept_slope(priv
, p1
, NULL
, ONE_PT_CALIB
);
202 static const struct reg_field tsens_8960_regfields
[MAX_REGFIELDS
] = {
203 /* ----- SROT ------ */
204 /* No VERSION information */
207 [TSENS_EN
] = REG_FIELD(CNTL_ADDR
, 0, 0),
208 [TSENS_SW_RST
] = REG_FIELD(CNTL_ADDR
, 1, 1),
209 /* 8960 has 5 sensors, 8660 has 11, we only handle 5 */
210 [SENSOR_EN
] = REG_FIELD(CNTL_ADDR
, 3, 7),
212 /* ----- TM ------ */
213 /* INTERRUPT ENABLE */
214 /* NO INTERRUPT ENABLE */
216 /* Single UPPER/LOWER TEMPERATURE THRESHOLD for all sensors */
217 [LOW_THRESH_0
] = REG_FIELD(THRESHOLD_ADDR
, 0, 7),
218 [UP_THRESH_0
] = REG_FIELD(THRESHOLD_ADDR
, 8, 15),
219 /* MIN_THRESH_0 and MAX_THRESH_0 are not present in the regfield
220 * Recycle CRIT_THRESH_0 and 1 to set the required regs to hardcoded temp
221 * MIN_THRESH_0 -> CRIT_THRESH_1
222 * MAX_THRESH_0 -> CRIT_THRESH_0
224 [CRIT_THRESH_1
] = REG_FIELD(THRESHOLD_ADDR
, 16, 23),
225 [CRIT_THRESH_0
] = REG_FIELD(THRESHOLD_ADDR
, 24, 31),
227 /* UPPER/LOWER INTERRUPT [CLEAR/STATUS] */
228 /* 1 == clear, 0 == normal operation */
229 [LOW_INT_CLEAR_0
] = REG_FIELD(CNTL_ADDR
, 9, 9),
230 [UP_INT_CLEAR_0
] = REG_FIELD(CNTL_ADDR
, 10, 10),
232 /* NO CRITICAL INTERRUPT SUPPORT on 8960 */
235 [LAST_TEMP_0
] = REG_FIELD(S0_STATUS_OFF
, 0, 7),
236 [LAST_TEMP_1
] = REG_FIELD(S1_STATUS_OFF
, 0, 7),
237 [LAST_TEMP_2
] = REG_FIELD(S2_STATUS_OFF
, 0, 7),
238 [LAST_TEMP_3
] = REG_FIELD(S3_STATUS_OFF
, 0, 7),
239 [LAST_TEMP_4
] = REG_FIELD(S4_STATUS_OFF
, 0, 7),
240 [LAST_TEMP_5
] = REG_FIELD(S5_STATUS_OFF
, 0, 7),
241 [LAST_TEMP_6
] = REG_FIELD(S6_STATUS_OFF
, 0, 7),
242 [LAST_TEMP_7
] = REG_FIELD(S7_STATUS_OFF
, 0, 7),
243 [LAST_TEMP_8
] = REG_FIELD(S8_STATUS_OFF
, 0, 7),
244 [LAST_TEMP_9
] = REG_FIELD(S9_STATUS_OFF
, 0, 7),
245 [LAST_TEMP_10
] = REG_FIELD(S10_STATUS_OFF
, 0, 7),
247 /* No VALID field on 8960 */
248 /* TSENS_INT_STATUS bits: 1 == threshold violated */
249 [MIN_STATUS_0
] = REG_FIELD(INT_STATUS_ADDR
, 0, 0),
250 [LOWER_STATUS_0
] = REG_FIELD(INT_STATUS_ADDR
, 1, 1),
251 [UPPER_STATUS_0
] = REG_FIELD(INT_STATUS_ADDR
, 2, 2),
252 /* No CRITICAL field on 8960 */
253 [MAX_STATUS_0
] = REG_FIELD(INT_STATUS_ADDR
, 3, 3),
255 /* TRDY: 1=ready, 0=in progress */
256 [TRDY
] = REG_FIELD(INT_STATUS_ADDR
, 7, 7),
259 static const struct tsens_ops ops_8960
= {
261 .calibrate
= calibrate_8960
,
262 .get_temp
= get_temp_common
,
263 .enable
= enable_8960
,
264 .disable
= disable_8960
,
265 .suspend
= suspend_8960
,
266 .resume
= resume_8960
,
269 static struct tsens_features tsens_8960_feat
= {
276 .trip_min_temp
= -40000,
277 .trip_max_temp
= 120000,
280 struct tsens_plat_data data_8960
= {
283 .feat
= &tsens_8960_feat
,
284 .fields
= tsens_8960_regfields
,