]>
Commit | Line | Data |
---|---|---|
5a729246 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
9a5238a9 | 2 | /* |
4481b39f | 3 | * HiSilicon thermal sensor driver |
9a5238a9 | 4 | * |
4481b39f | 5 | * Copyright (c) 2014-2015 HiSilicon Limited. |
9a5238a9 | 6 | * Copyright (c) 2014-2015 Linaro Limited. |
7 | * | |
8 | * Xinwei Kong <[email protected]> | |
9 | * Leo Yan <[email protected]> | |
9a5238a9 | 10 | */ |
11 | ||
12 | #include <linux/cpufreq.h> | |
13 | #include <linux/delay.h> | |
14 | #include <linux/interrupt.h> | |
15 | #include <linux/module.h> | |
f6a756e8 | 16 | #include <linux/of.h> |
9a5238a9 | 17 | #include <linux/platform_device.h> |
18 | #include <linux/io.h> | |
9272d2d4 | 19 | #include <linux/thermal.h> |
9a5238a9 | 20 | |
5ed82b79 KW |
21 | #define HI6220_TEMP0_LAG (0x0) |
22 | #define HI6220_TEMP0_TH (0x4) | |
23 | #define HI6220_TEMP0_RST_TH (0x8) | |
24 | #define HI6220_TEMP0_CFG (0xC) | |
a160a465 | 25 | #define HI6220_TEMP0_CFG_SS_MSK (0xF000) |
5ed82b79 KW |
26 | #define HI6220_TEMP0_CFG_HDAK_MSK (0x30) |
27 | #define HI6220_TEMP0_EN (0x10) | |
28 | #define HI6220_TEMP0_INT_EN (0x14) | |
29 | #define HI6220_TEMP0_INT_CLR (0x18) | |
30 | #define HI6220_TEMP0_RST_MSK (0x1C) | |
31 | #define HI6220_TEMP0_VALUE (0x28) | |
32 | ||
2bb60a8e KW |
33 | #define HI3660_OFFSET(chan) ((chan) * 0x40) |
34 | #define HI3660_TEMP(chan) (HI3660_OFFSET(chan) + 0x1C) | |
35 | #define HI3660_TH(chan) (HI3660_OFFSET(chan) + 0x20) | |
36 | #define HI3660_LAG(chan) (HI3660_OFFSET(chan) + 0x28) | |
37 | #define HI3660_INT_EN(chan) (HI3660_OFFSET(chan) + 0x2C) | |
38 | #define HI3660_INT_CLR(chan) (HI3660_OFFSET(chan) + 0x30) | |
39 | ||
5ed82b79 KW |
40 | #define HI6220_TEMP_BASE (-60000) |
41 | #define HI6220_TEMP_RESET (100000) | |
42 | #define HI6220_TEMP_STEP (785) | |
a160a465 | 43 | #define HI6220_TEMP_LAG (3500) |
5ed82b79 | 44 | |
2bb60a8e KW |
45 | #define HI3660_TEMP_BASE (-63780) |
46 | #define HI3660_TEMP_STEP (205) | |
47 | #define HI3660_TEMP_LAG (4000) | |
48 | ||
a849eece | 49 | #define HI6220_CLUSTER0_SENSOR 2 |
ce8c0700 DL |
50 | #define HI6220_CLUSTER1_SENSOR 1 |
51 | ||
52 | #define HI3660_LITTLE_SENSOR 0 | |
a849eece | 53 | #define HI3660_BIG_SENSOR 1 |
ce8c0700 DL |
54 | #define HI3660_G3D_SENSOR 2 |
55 | #define HI3660_MODEM_SENSOR 3 | |
9a5238a9 | 56 | |
9c9ae8da DL |
57 | struct hisi_thermal_data; |
58 | ||
9a5238a9 | 59 | struct hisi_thermal_sensor { |
9c9ae8da | 60 | struct hisi_thermal_data *data; |
9a5238a9 | 61 | struct thermal_zone_device *tzd; |
2cffaeff | 62 | const char *irq_name; |
9a5238a9 | 63 | uint32_t id; |
64 | uint32_t thres_temp; | |
65 | }; | |
66 | ||
c90aaecc | 67 | struct hisi_thermal_ops { |
9c9ae8da DL |
68 | int (*get_temp)(struct hisi_thermal_sensor *sensor); |
69 | int (*enable_sensor)(struct hisi_thermal_sensor *sensor); | |
70 | int (*disable_sensor)(struct hisi_thermal_sensor *sensor); | |
71 | int (*irq_handler)(struct hisi_thermal_sensor *sensor); | |
c90aaecc DL |
72 | int (*probe)(struct hisi_thermal_data *data); |
73 | }; | |
74 | ||
75 | struct hisi_thermal_data { | |
76 | const struct hisi_thermal_ops *ops; | |
8c0ffc8f | 77 | struct hisi_thermal_sensor *sensor; |
9a5238a9 | 78 | struct platform_device *pdev; |
79 | struct clk *clk; | |
9a5238a9 | 80 | void __iomem *regs; |
8c0ffc8f | 81 | int nr_sensors; |
9a5238a9 | 82 | }; |
83 | ||
48880b97 DL |
84 | /* |
85 | * The temperature computation on the tsensor is as follow: | |
86 | * Unit: millidegree Celsius | |
e42bbe11 | 87 | * Step: 200/255 (0.7843) |
48880b97 DL |
88 | * Temperature base: -60°C |
89 | * | |
e42bbe11 | 90 | * The register is programmed in temperature steps, every step is 785 |
48880b97 DL |
91 | * millidegree and begins at -60 000 m°C |
92 | * | |
93 | * The temperature from the steps: | |
94 | * | |
e42bbe11 | 95 | * Temp = TempBase + (steps x 785) |
48880b97 DL |
96 | * |
97 | * and the steps from the temperature: | |
98 | * | |
e42bbe11 | 99 | * steps = (Temp - TempBase) / 785 |
48880b97 DL |
100 | * |
101 | */ | |
5ed82b79 | 102 | static inline int hi6220_thermal_step_to_temp(int step) |
9a5238a9 | 103 | { |
5ed82b79 | 104 | return HI6220_TEMP_BASE + (step * HI6220_TEMP_STEP); |
9a5238a9 | 105 | } |
106 | ||
5ed82b79 | 107 | static inline int hi6220_thermal_temp_to_step(int temp) |
9a5238a9 | 108 | { |
5ed82b79 | 109 | return DIV_ROUND_UP(temp - HI6220_TEMP_BASE, HI6220_TEMP_STEP); |
db2b0332 DL |
110 | } |
111 | ||
2bb60a8e KW |
112 | /* |
113 | * for Hi3660, | |
114 | * Step: 189/922 (0.205) | |
115 | * Temperature base: -63.780°C | |
116 | * | |
117 | * The register is programmed in temperature steps, every step is 205 | |
118 | * millidegree and begins at -63 780 m°C | |
119 | */ | |
120 | static inline int hi3660_thermal_step_to_temp(int step) | |
121 | { | |
122 | return HI3660_TEMP_BASE + step * HI3660_TEMP_STEP; | |
123 | } | |
124 | ||
125 | static inline int hi3660_thermal_temp_to_step(int temp) | |
126 | { | |
127 | return DIV_ROUND_UP(temp - HI3660_TEMP_BASE, HI3660_TEMP_STEP); | |
128 | } | |
129 | ||
10d7e9a9 DL |
130 | /* |
131 | * The lag register contains 5 bits encoding the temperature in steps. | |
132 | * | |
133 | * Each time the temperature crosses the threshold boundary, an | |
134 | * interrupt is raised. It could be when the temperature is going | |
135 | * above the threshold or below. However, if the temperature is | |
136 | * fluctuating around this value due to the load, we can receive | |
137 | * several interrupts which may not desired. | |
138 | * | |
139 | * We can setup a temperature representing the delta between the | |
140 | * threshold and the current temperature when the temperature is | |
141 | * decreasing. | |
142 | * | |
143 | * For instance: the lag register is 5°C, the threshold is 65°C, when | |
144 | * the temperature reaches 65°C an interrupt is raised and when the | |
145 | * temperature decrease to 65°C - 5°C another interrupt is raised. | |
146 | * | |
147 | * A very short lag can lead to an interrupt storm, a long lag | |
148 | * increase the latency to react to the temperature changes. In our | |
149 | * case, that is not really a problem as we are polling the | |
150 | * temperature. | |
151 | * | |
152 | * [0:4] : lag register | |
153 | * | |
5ed82b79 | 154 | * The temperature is coded in steps, cf. HI6220_TEMP_STEP. |
10d7e9a9 DL |
155 | * |
156 | * Min : 0x00 : 0.0 °C | |
157 | * Max : 0x1F : 24.3 °C | |
158 | * | |
159 | * The 'value' parameter is in milliCelsius. | |
160 | */ | |
5ed82b79 | 161 | static inline void hi6220_thermal_set_lag(void __iomem *addr, int value) |
1e11b014 | 162 | { |
5ed82b79 KW |
163 | writel(DIV_ROUND_UP(value, HI6220_TEMP_STEP) & 0x1F, |
164 | addr + HI6220_TEMP0_LAG); | |
1e11b014 DL |
165 | } |
166 | ||
5ed82b79 | 167 | static inline void hi6220_thermal_alarm_clear(void __iomem *addr, int value) |
1e11b014 | 168 | { |
5ed82b79 | 169 | writel(value, addr + HI6220_TEMP0_INT_CLR); |
1e11b014 DL |
170 | } |
171 | ||
5ed82b79 | 172 | static inline void hi6220_thermal_alarm_enable(void __iomem *addr, int value) |
1e11b014 | 173 | { |
5ed82b79 | 174 | writel(value, addr + HI6220_TEMP0_INT_EN); |
1e11b014 DL |
175 | } |
176 | ||
5ed82b79 | 177 | static inline void hi6220_thermal_alarm_set(void __iomem *addr, int temp) |
1e11b014 | 178 | { |
5ed82b79 KW |
179 | writel(hi6220_thermal_temp_to_step(temp) | 0x0FFFFFF00, |
180 | addr + HI6220_TEMP0_TH); | |
1e11b014 DL |
181 | } |
182 | ||
5ed82b79 | 183 | static inline void hi6220_thermal_reset_set(void __iomem *addr, int temp) |
1e11b014 | 184 | { |
5ed82b79 | 185 | writel(hi6220_thermal_temp_to_step(temp), addr + HI6220_TEMP0_RST_TH); |
1e11b014 DL |
186 | } |
187 | ||
5ed82b79 | 188 | static inline void hi6220_thermal_reset_enable(void __iomem *addr, int value) |
1e11b014 | 189 | { |
5ed82b79 | 190 | writel(value, addr + HI6220_TEMP0_RST_MSK); |
1e11b014 DL |
191 | } |
192 | ||
5ed82b79 | 193 | static inline void hi6220_thermal_enable(void __iomem *addr, int value) |
1e11b014 | 194 | { |
5ed82b79 | 195 | writel(value, addr + HI6220_TEMP0_EN); |
1e11b014 DL |
196 | } |
197 | ||
5ed82b79 | 198 | static inline int hi6220_thermal_get_temperature(void __iomem *addr) |
1e11b014 | 199 | { |
5ed82b79 | 200 | return hi6220_thermal_step_to_temp(readl(addr + HI6220_TEMP0_VALUE)); |
1e11b014 DL |
201 | } |
202 | ||
2bb60a8e KW |
203 | /* |
204 | * [0:6] lag register | |
205 | * | |
206 | * The temperature is coded in steps, cf. HI3660_TEMP_STEP. | |
207 | * | |
208 | * Min : 0x00 : 0.0 °C | |
209 | * Max : 0x7F : 26.0 °C | |
210 | * | |
211 | */ | |
212 | static inline void hi3660_thermal_set_lag(void __iomem *addr, | |
213 | int id, int value) | |
214 | { | |
215 | writel(DIV_ROUND_UP(value, HI3660_TEMP_STEP) & 0x7F, | |
216 | addr + HI3660_LAG(id)); | |
217 | } | |
218 | ||
219 | static inline void hi3660_thermal_alarm_clear(void __iomem *addr, | |
220 | int id, int value) | |
221 | { | |
222 | writel(value, addr + HI3660_INT_CLR(id)); | |
223 | } | |
224 | ||
225 | static inline void hi3660_thermal_alarm_enable(void __iomem *addr, | |
226 | int id, int value) | |
227 | { | |
228 | writel(value, addr + HI3660_INT_EN(id)); | |
229 | } | |
230 | ||
231 | static inline void hi3660_thermal_alarm_set(void __iomem *addr, | |
232 | int id, int value) | |
233 | { | |
234 | writel(value, addr + HI3660_TH(id)); | |
235 | } | |
236 | ||
237 | static inline int hi3660_thermal_get_temperature(void __iomem *addr, int id) | |
238 | { | |
239 | return hi3660_thermal_step_to_temp(readl(addr + HI3660_TEMP(id))); | |
240 | } | |
241 | ||
b424315a DL |
242 | /* |
243 | * Temperature configuration register - Sensor selection | |
244 | * | |
245 | * Bits [19:12] | |
246 | * | |
247 | * 0x0: local sensor (default) | |
248 | * 0x1: remote sensor 1 (ACPU cluster 1) | |
249 | * 0x2: remote sensor 2 (ACPU cluster 0) | |
250 | * 0x3: remote sensor 3 (G3D) | |
251 | */ | |
5ed82b79 | 252 | static inline void hi6220_thermal_sensor_select(void __iomem *addr, int sensor) |
1e11b014 | 253 | { |
5ed82b79 KW |
254 | writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_SS_MSK) | |
255 | (sensor << 12), addr + HI6220_TEMP0_CFG); | |
1e11b014 DL |
256 | } |
257 | ||
b424315a DL |
258 | /* |
259 | * Temperature configuration register - Hdak conversion polling interval | |
260 | * | |
261 | * Bits [5:4] | |
262 | * | |
263 | * 0x0 : 0.768 ms | |
264 | * 0x1 : 6.144 ms | |
265 | * 0x2 : 49.152 ms | |
266 | * 0x3 : 393.216 ms | |
267 | */ | |
5ed82b79 | 268 | static inline void hi6220_thermal_hdak_set(void __iomem *addr, int value) |
1e11b014 | 269 | { |
5ed82b79 KW |
270 | writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_HDAK_MSK) | |
271 | (value << 4), addr + HI6220_TEMP0_CFG); | |
1e11b014 DL |
272 | } |
273 | ||
9c9ae8da | 274 | static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor *sensor) |
a160a465 | 275 | { |
9c9ae8da DL |
276 | struct hisi_thermal_data *data = sensor->data; |
277 | ||
a160a465 KW |
278 | hi6220_thermal_alarm_clear(data->regs, 1); |
279 | return 0; | |
280 | } | |
281 | ||
9c9ae8da | 282 | static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor *sensor) |
2bb60a8e | 283 | { |
9c9ae8da DL |
284 | struct hisi_thermal_data *data = sensor->data; |
285 | ||
286 | hi3660_thermal_alarm_clear(data->regs, sensor->id, 1); | |
2bb60a8e KW |
287 | return 0; |
288 | } | |
289 | ||
9c9ae8da | 290 | static int hi6220_thermal_get_temp(struct hisi_thermal_sensor *sensor) |
a160a465 | 291 | { |
9c9ae8da DL |
292 | struct hisi_thermal_data *data = sensor->data; |
293 | ||
a160a465 KW |
294 | return hi6220_thermal_get_temperature(data->regs); |
295 | } | |
296 | ||
9c9ae8da | 297 | static int hi3660_thermal_get_temp(struct hisi_thermal_sensor *sensor) |
2bb60a8e | 298 | { |
9c9ae8da DL |
299 | struct hisi_thermal_data *data = sensor->data; |
300 | ||
301 | return hi3660_thermal_get_temperature(data->regs, sensor->id); | |
2bb60a8e KW |
302 | } |
303 | ||
9c9ae8da | 304 | static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor *sensor) |
9a5238a9 | 305 | { |
9c9ae8da DL |
306 | struct hisi_thermal_data *data = sensor->data; |
307 | ||
9a5238a9 | 308 | /* disable sensor module */ |
5ed82b79 KW |
309 | hi6220_thermal_enable(data->regs, 0); |
310 | hi6220_thermal_alarm_enable(data->regs, 0); | |
311 | hi6220_thermal_reset_enable(data->regs, 0); | |
943c0f6a KW |
312 | |
313 | clk_disable_unprepare(data->clk); | |
a160a465 KW |
314 | |
315 | return 0; | |
9a5238a9 | 316 | } |
317 | ||
9c9ae8da | 318 | static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor *sensor) |
2bb60a8e | 319 | { |
9c9ae8da DL |
320 | struct hisi_thermal_data *data = sensor->data; |
321 | ||
2bb60a8e | 322 | /* disable sensor module */ |
9c9ae8da | 323 | hi3660_thermal_alarm_enable(data->regs, sensor->id, 0); |
2bb60a8e KW |
324 | return 0; |
325 | } | |
326 | ||
9c9ae8da | 327 | static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor *sensor) |
a0678da8 | 328 | { |
9c9ae8da | 329 | struct hisi_thermal_data *data = sensor->data; |
a0678da8 KW |
330 | int ret; |
331 | ||
332 | /* enable clock for tsensor */ | |
333 | ret = clk_prepare_enable(data->clk); | |
334 | if (ret) | |
335 | return ret; | |
336 | ||
337 | /* disable module firstly */ | |
5ed82b79 KW |
338 | hi6220_thermal_reset_enable(data->regs, 0); |
339 | hi6220_thermal_enable(data->regs, 0); | |
a0678da8 KW |
340 | |
341 | /* select sensor id */ | |
5ed82b79 | 342 | hi6220_thermal_sensor_select(data->regs, sensor->id); |
a0678da8 KW |
343 | |
344 | /* setting the hdak time */ | |
5ed82b79 | 345 | hi6220_thermal_hdak_set(data->regs, 0); |
a0678da8 KW |
346 | |
347 | /* setting lag value between current temp and the threshold */ | |
5ed82b79 | 348 | hi6220_thermal_set_lag(data->regs, HI6220_TEMP_LAG); |
a0678da8 KW |
349 | |
350 | /* enable for interrupt */ | |
5ed82b79 | 351 | hi6220_thermal_alarm_set(data->regs, sensor->thres_temp); |
a0678da8 | 352 | |
5ed82b79 | 353 | hi6220_thermal_reset_set(data->regs, HI6220_TEMP_RESET); |
a0678da8 KW |
354 | |
355 | /* enable module */ | |
5ed82b79 KW |
356 | hi6220_thermal_reset_enable(data->regs, 1); |
357 | hi6220_thermal_enable(data->regs, 1); | |
a0678da8 | 358 | |
5ed82b79 KW |
359 | hi6220_thermal_alarm_clear(data->regs, 0); |
360 | hi6220_thermal_alarm_enable(data->regs, 1); | |
a0678da8 KW |
361 | |
362 | return 0; | |
363 | } | |
364 | ||
9c9ae8da | 365 | static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor *sensor) |
2bb60a8e KW |
366 | { |
367 | unsigned int value; | |
9c9ae8da | 368 | struct hisi_thermal_data *data = sensor->data; |
2bb60a8e KW |
369 | |
370 | /* disable interrupt */ | |
371 | hi3660_thermal_alarm_enable(data->regs, sensor->id, 0); | |
372 | ||
373 | /* setting lag value between current temp and the threshold */ | |
374 | hi3660_thermal_set_lag(data->regs, sensor->id, HI3660_TEMP_LAG); | |
375 | ||
376 | /* set interrupt threshold */ | |
377 | value = hi3660_thermal_temp_to_step(sensor->thres_temp); | |
378 | hi3660_thermal_alarm_set(data->regs, sensor->id, value); | |
379 | ||
380 | /* enable interrupt */ | |
381 | hi3660_thermal_alarm_clear(data->regs, sensor->id, 1); | |
382 | hi3660_thermal_alarm_enable(data->regs, sensor->id, 1); | |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
a160a465 KW |
387 | static int hi6220_thermal_probe(struct hisi_thermal_data *data) |
388 | { | |
389 | struct platform_device *pdev = data->pdev; | |
390 | struct device *dev = &pdev->dev; | |
a160a465 KW |
391 | int ret; |
392 | ||
a160a465 KW |
393 | data->clk = devm_clk_get(dev, "thermal_clk"); |
394 | if (IS_ERR(data->clk)) { | |
395 | ret = PTR_ERR(data->clk); | |
396 | if (ret != -EPROBE_DEFER) | |
397 | dev_err(dev, "failed to get thermal clk: %d\n", ret); | |
398 | return ret; | |
399 | } | |
400 | ||
8c0ffc8f DL |
401 | data->sensor = devm_kzalloc(dev, sizeof(*data->sensor), GFP_KERNEL); |
402 | if (!data->sensor) | |
403 | return -ENOMEM; | |
404 | ||
a849eece | 405 | data->sensor[0].id = HI6220_CLUSTER0_SENSOR; |
2cffaeff | 406 | data->sensor[0].irq_name = "tsensor_intr"; |
8c0ffc8f DL |
407 | data->sensor[0].data = data; |
408 | data->nr_sensors = 1; | |
a160a465 KW |
409 | |
410 | return 0; | |
411 | } | |
412 | ||
2bb60a8e KW |
413 | static int hi3660_thermal_probe(struct hisi_thermal_data *data) |
414 | { | |
8c0ffc8f DL |
415 | struct platform_device *pdev = data->pdev; |
416 | struct device *dev = &pdev->dev; | |
417 | ||
7d3a2a2b | 418 | data->nr_sensors = 1; |
8c6c3684 DL |
419 | |
420 | data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) * | |
421 | data->nr_sensors, GFP_KERNEL); | |
8c0ffc8f DL |
422 | if (!data->sensor) |
423 | return -ENOMEM; | |
424 | ||
a849eece | 425 | data->sensor[0].id = HI3660_BIG_SENSOR; |
2cffaeff | 426 | data->sensor[0].irq_name = "tsensor_a73"; |
8c0ffc8f | 427 | data->sensor[0].data = data; |
8c6c3684 | 428 | |
2bb60a8e KW |
429 | return 0; |
430 | } | |
431 | ||
5ee7811e | 432 | static int hisi_thermal_get_temp(struct thermal_zone_device *tz, int *temp) |
9a5238a9 | 433 | { |
5f68d078 | 434 | struct hisi_thermal_sensor *sensor = thermal_zone_device_priv(tz); |
49e778d1 | 435 | struct hisi_thermal_data *data = sensor->data; |
9a5238a9 | 436 | |
9c9ae8da | 437 | *temp = data->ops->get_temp(sensor); |
9a5238a9 | 438 | |
9a5238a9 | 439 | return 0; |
440 | } | |
441 | ||
5ee7811e | 442 | static const struct thermal_zone_device_ops hisi_of_thermal_ops = { |
9a5238a9 | 443 | .get_temp = hisi_thermal_get_temp, |
444 | }; | |
445 | ||
10d7e9a9 | 446 | static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev) |
9a5238a9 | 447 | { |
7edc5e40 DL |
448 | struct hisi_thermal_sensor *sensor = dev; |
449 | struct hisi_thermal_data *data = sensor->data; | |
5ed82b79 | 450 | int temp = 0; |
9a5238a9 | 451 | |
9c9ae8da | 452 | data->ops->irq_handler(sensor); |
9a5238a9 | 453 | |
5ee7811e | 454 | temp = data->ops->get_temp(sensor); |
9a5238a9 | 455 | |
10d7e9a9 | 456 | if (temp >= sensor->thres_temp) { |
7edc5e40 DL |
457 | dev_crit(&data->pdev->dev, |
458 | "sensor <%d> THERMAL ALARM: %d > %d\n", | |
459 | sensor->id, temp, sensor->thres_temp); | |
9a5238a9 | 460 | |
7edc5e40 | 461 | thermal_zone_device_update(sensor->tzd, |
10d7e9a9 | 462 | THERMAL_EVENT_UNSPECIFIED); |
9a5238a9 | 463 | |
5ed82b79 | 464 | } else { |
7edc5e40 DL |
465 | dev_crit(&data->pdev->dev, |
466 | "sensor <%d> THERMAL ALARM stopped: %d < %d\n", | |
467 | sensor->id, temp, sensor->thres_temp); | |
10d7e9a9 | 468 | } |
9a5238a9 | 469 | |
470 | return IRQ_HANDLED; | |
471 | } | |
472 | ||
473 | static int hisi_thermal_register_sensor(struct platform_device *pdev, | |
a160a465 | 474 | struct hisi_thermal_sensor *sensor) |
9a5238a9 | 475 | { |
476 | int ret, i; | |
68a306cc | 477 | struct thermal_trip trip; |
9a5238a9 | 478 | |
5ee7811e DL |
479 | sensor->tzd = devm_thermal_of_zone_register(&pdev->dev, |
480 | sensor->id, sensor, | |
481 | &hisi_of_thermal_ops); | |
9a5238a9 | 482 | if (IS_ERR(sensor->tzd)) { |
483 | ret = PTR_ERR(sensor->tzd); | |
439dc968 | 484 | sensor->tzd = NULL; |
9a5238a9 | 485 | dev_err(&pdev->dev, "failed to register sensor id %d: %d\n", |
486 | sensor->id, ret); | |
487 | return ret; | |
488 | } | |
489 | ||
68a306cc | 490 | for (i = 0; i < thermal_zone_get_num_trips(sensor->tzd); i++) { |
9a5238a9 | 491 | |
68a306cc DL |
492 | thermal_zone_get_trip(sensor->tzd, i, &trip); |
493 | ||
494 | if (trip.type == THERMAL_TRIP_PASSIVE) { | |
495 | sensor->thres_temp = trip.temperature; | |
9a5238a9 | 496 | break; |
497 | } | |
498 | } | |
499 | ||
500 | return 0; | |
501 | } | |
502 | ||
c90aaecc DL |
503 | static const struct hisi_thermal_ops hi6220_ops = { |
504 | .get_temp = hi6220_thermal_get_temp, | |
505 | .enable_sensor = hi6220_thermal_enable_sensor, | |
506 | .disable_sensor = hi6220_thermal_disable_sensor, | |
507 | .irq_handler = hi6220_thermal_irq_handler, | |
508 | .probe = hi6220_thermal_probe, | |
509 | }; | |
510 | ||
511 | static const struct hisi_thermal_ops hi3660_ops = { | |
512 | .get_temp = hi3660_thermal_get_temp, | |
513 | .enable_sensor = hi3660_thermal_enable_sensor, | |
514 | .disable_sensor = hi3660_thermal_disable_sensor, | |
515 | .irq_handler = hi3660_thermal_irq_handler, | |
516 | .probe = hi3660_thermal_probe, | |
517 | }; | |
518 | ||
9a5238a9 | 519 | static const struct of_device_id of_hisi_thermal_match[] = { |
2bb60a8e KW |
520 | { |
521 | .compatible = "hisilicon,tsensor", | |
c90aaecc | 522 | .data = &hi6220_ops, |
2bb60a8e KW |
523 | }, |
524 | { | |
525 | .compatible = "hisilicon,hi3660-tsensor", | |
c90aaecc | 526 | .data = &hi3660_ops, |
2bb60a8e | 527 | }, |
9a5238a9 | 528 | { /* end */ } |
529 | }; | |
530 | MODULE_DEVICE_TABLE(of, of_hisi_thermal_match); | |
531 | ||
532 | static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor, | |
533 | bool on) | |
534 | { | |
535 | struct thermal_zone_device *tzd = sensor->tzd; | |
536 | ||
7f4957be AP |
537 | if (on) |
538 | thermal_zone_device_enable(tzd); | |
539 | else | |
540 | thermal_zone_device_disable(tzd); | |
9a5238a9 | 541 | } |
542 | ||
543 | static int hisi_thermal_probe(struct platform_device *pdev) | |
544 | { | |
545 | struct hisi_thermal_data *data; | |
a160a465 | 546 | struct device *dev = &pdev->dev; |
7edc5e40 | 547 | int i, ret; |
9a5238a9 | 548 | |
a160a465 | 549 | data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
9a5238a9 | 550 | if (!data) |
551 | return -ENOMEM; | |
552 | ||
9a5238a9 | 553 | data->pdev = pdev; |
a160a465 | 554 | platform_set_drvdata(pdev, data); |
c90aaecc | 555 | data->ops = of_device_get_match_data(dev); |
9a5238a9 | 556 | |
e45c9a2f | 557 | data->regs = devm_platform_ioremap_resource(pdev, 0); |
beaa4102 | 558 | if (IS_ERR(data->regs)) |
9bb4ec8d | 559 | return PTR_ERR(data->regs); |
9bb4ec8d | 560 | |
c90aaecc | 561 | ret = data->ops->probe(data); |
a160a465 | 562 | if (ret) |
9a5238a9 | 563 | return ret; |
9a5238a9 | 564 | |
7edc5e40 DL |
565 | for (i = 0; i < data->nr_sensors; i++) { |
566 | struct hisi_thermal_sensor *sensor = &data->sensor[i]; | |
9a5238a9 | 567 | |
7edc5e40 DL |
568 | ret = hisi_thermal_register_sensor(pdev, sensor); |
569 | if (ret) { | |
570 | dev_err(dev, "failed to register thermal sensor: %d\n", | |
571 | ret); | |
572 | return ret; | |
573 | } | |
574 | ||
5d7ab8f0 | 575 | ret = platform_get_irq(pdev, 0); |
a18e83e7 DL |
576 | if (ret < 0) |
577 | return ret; | |
ff4ec299 | 578 | |
a18e83e7 | 579 | ret = devm_request_threaded_irq(dev, ret, NULL, |
7edc5e40 | 580 | hisi_thermal_alarm_irq_thread, |
2cffaeff | 581 | IRQF_ONESHOT, sensor->irq_name, |
7edc5e40 | 582 | sensor); |
a160a465 | 583 | if (ret < 0) { |
a18e83e7 | 584 | dev_err(dev, "Failed to request alarm irq: %d\n", ret); |
a160a465 KW |
585 | return ret; |
586 | } | |
2cb4de78 | 587 | |
7edc5e40 DL |
588 | ret = data->ops->enable_sensor(sensor); |
589 | if (ret) { | |
590 | dev_err(dev, "Failed to setup the sensor: %d\n", ret); | |
591 | return ret; | |
592 | } | |
593 | ||
594 | hisi_thermal_toggle_sensor(sensor, true); | |
595 | } | |
c176b10b | 596 | |
9a5238a9 | 597 | return 0; |
9a5238a9 | 598 | } |
599 | ||
6abe2f00 | 600 | static void hisi_thermal_remove(struct platform_device *pdev) |
9a5238a9 | 601 | { |
602 | struct hisi_thermal_data *data = platform_get_drvdata(pdev); | |
7edc5e40 | 603 | int i; |
9a5238a9 | 604 | |
7edc5e40 DL |
605 | for (i = 0; i < data->nr_sensors; i++) { |
606 | struct hisi_thermal_sensor *sensor = &data->sensor[i]; | |
a160a465 | 607 | |
7edc5e40 DL |
608 | hisi_thermal_toggle_sensor(sensor, false); |
609 | data->ops->disable_sensor(sensor); | |
610 | } | |
9a5238a9 | 611 | } |
612 | ||
9a5238a9 | 613 | static int hisi_thermal_suspend(struct device *dev) |
614 | { | |
615 | struct hisi_thermal_data *data = dev_get_drvdata(dev); | |
7edc5e40 | 616 | int i; |
9a5238a9 | 617 | |
7edc5e40 DL |
618 | for (i = 0; i < data->nr_sensors; i++) |
619 | data->ops->disable_sensor(&data->sensor[i]); | |
9a5238a9 | 620 | |
9a5238a9 | 621 | return 0; |
622 | } | |
623 | ||
624 | static int hisi_thermal_resume(struct device *dev) | |
625 | { | |
626 | struct hisi_thermal_data *data = dev_get_drvdata(dev); | |
7edc5e40 DL |
627 | int i, ret = 0; |
628 | ||
629 | for (i = 0; i < data->nr_sensors; i++) | |
630 | ret |= data->ops->enable_sensor(&data->sensor[i]); | |
9a5238a9 | 631 | |
7edc5e40 | 632 | return ret; |
9a5238a9 | 633 | } |
9a5238a9 | 634 | |
7bb732fe | 635 | static DEFINE_SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops, |
9a5238a9 | 636 | hisi_thermal_suspend, hisi_thermal_resume); |
637 | ||
638 | static struct platform_driver hisi_thermal_driver = { | |
639 | .driver = { | |
640 | .name = "hisi_thermal", | |
7bb732fe | 641 | .pm = pm_sleep_ptr(&hisi_thermal_pm_ops), |
9a5238a9 | 642 | .of_match_table = of_hisi_thermal_match, |
643 | }, | |
644 | .probe = hisi_thermal_probe, | |
6abe2f00 | 645 | .remove_new = hisi_thermal_remove, |
9a5238a9 | 646 | }; |
647 | ||
648 | module_platform_driver(hisi_thermal_driver); | |
649 | ||
650 | MODULE_AUTHOR("Xinwei Kong <[email protected]>"); | |
651 | MODULE_AUTHOR("Leo Yan <[email protected]>"); | |
4481b39f | 652 | MODULE_DESCRIPTION("HiSilicon thermal driver"); |
9a5238a9 | 653 | MODULE_LICENSE("GPL v2"); |