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