2 * Hisilicon thermal sensor driver
4 * Copyright (c) 2014-2015 Hisilicon Limited.
5 * Copyright (c) 2014-2015 Linaro Limited.
7 * Xinwei Kong <kong.kongxinwei@hisilicon.com>
8 * Leo Yan <leo.yan@linaro.org>
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.
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.
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>
26 #include <linux/of_device.h>
28 #include "thermal_core.h"
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)
34 #define HI6220_TEMP0_CFG_SS_MSK (0xF000)
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)
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)
49 #define HI6220_TEMP_BASE (-60000)
50 #define HI6220_TEMP_RESET (100000)
51 #define HI6220_TEMP_STEP (785)
52 #define HI6220_TEMP_LAG (3500)
54 #define HI3660_TEMP_BASE (-63780)
55 #define HI3660_TEMP_STEP (205)
56 #define HI3660_TEMP_LAG (4000)
58 #define HI6220_CLUSTER0_SENSOR 2
59 #define HI6220_CLUSTER1_SENSOR 1
61 #define HI3660_LITTLE_SENSOR 0
62 #define HI3660_BIG_SENSOR 1
63 #define HI3660_G3D_SENSOR 2
64 #define HI3660_MODEM_SENSOR 3
66 struct hisi_thermal_data
;
68 struct hisi_thermal_sensor
{
69 struct hisi_thermal_data
*data
;
70 struct thermal_zone_device
*tzd
;
76 struct hisi_thermal_ops
{
77 int (*get_temp
)(struct hisi_thermal_sensor
*sensor
);
78 int (*enable_sensor
)(struct hisi_thermal_sensor
*sensor
);
79 int (*disable_sensor
)(struct hisi_thermal_sensor
*sensor
);
80 int (*irq_handler
)(struct hisi_thermal_sensor
*sensor
);
81 int (*probe
)(struct hisi_thermal_data
*data
);
84 struct hisi_thermal_data
{
85 const struct hisi_thermal_ops
*ops
;
86 struct hisi_thermal_sensor
*sensor
;
87 struct platform_device
*pdev
;
94 * The temperature computation on the tsensor is as follow:
95 * Unit: millidegree Celsius
96 * Step: 200/255 (0.7843)
97 * Temperature base: -60°C
99 * The register is programmed in temperature steps, every step is 785
100 * millidegree and begins at -60 000 m°C
102 * The temperature from the steps:
104 * Temp = TempBase + (steps x 785)
106 * and the steps from the temperature:
108 * steps = (Temp - TempBase) / 785
111 static inline int hi6220_thermal_step_to_temp(int step
)
113 return HI6220_TEMP_BASE
+ (step
* HI6220_TEMP_STEP
);
116 static inline int hi6220_thermal_temp_to_step(int temp
)
118 return DIV_ROUND_UP(temp
- HI6220_TEMP_BASE
, HI6220_TEMP_STEP
);
123 * Step: 189/922 (0.205)
124 * Temperature base: -63.780°C
126 * The register is programmed in temperature steps, every step is 205
127 * millidegree and begins at -63 780 m°C
129 static inline int hi3660_thermal_step_to_temp(int step
)
131 return HI3660_TEMP_BASE
+ step
* HI3660_TEMP_STEP
;
134 static inline int hi3660_thermal_temp_to_step(int temp
)
136 return DIV_ROUND_UP(temp
- HI3660_TEMP_BASE
, HI3660_TEMP_STEP
);
140 * The lag register contains 5 bits encoding the temperature in steps.
142 * Each time the temperature crosses the threshold boundary, an
143 * interrupt is raised. It could be when the temperature is going
144 * above the threshold or below. However, if the temperature is
145 * fluctuating around this value due to the load, we can receive
146 * several interrupts which may not desired.
148 * We can setup a temperature representing the delta between the
149 * threshold and the current temperature when the temperature is
152 * For instance: the lag register is 5°C, the threshold is 65°C, when
153 * the temperature reaches 65°C an interrupt is raised and when the
154 * temperature decrease to 65°C - 5°C another interrupt is raised.
156 * A very short lag can lead to an interrupt storm, a long lag
157 * increase the latency to react to the temperature changes. In our
158 * case, that is not really a problem as we are polling the
161 * [0:4] : lag register
163 * The temperature is coded in steps, cf. HI6220_TEMP_STEP.
165 * Min : 0x00 : 0.0 °C
166 * Max : 0x1F : 24.3 °C
168 * The 'value' parameter is in milliCelsius.
170 static inline void hi6220_thermal_set_lag(void __iomem
*addr
, int value
)
172 writel(DIV_ROUND_UP(value
, HI6220_TEMP_STEP
) & 0x1F,
173 addr
+ HI6220_TEMP0_LAG
);
176 static inline void hi6220_thermal_alarm_clear(void __iomem
*addr
, int value
)
178 writel(value
, addr
+ HI6220_TEMP0_INT_CLR
);
181 static inline void hi6220_thermal_alarm_enable(void __iomem
*addr
, int value
)
183 writel(value
, addr
+ HI6220_TEMP0_INT_EN
);
186 static inline void hi6220_thermal_alarm_set(void __iomem
*addr
, int temp
)
188 writel(hi6220_thermal_temp_to_step(temp
) | 0x0FFFFFF00,
189 addr
+ HI6220_TEMP0_TH
);
192 static inline void hi6220_thermal_reset_set(void __iomem
*addr
, int temp
)
194 writel(hi6220_thermal_temp_to_step(temp
), addr
+ HI6220_TEMP0_RST_TH
);
197 static inline void hi6220_thermal_reset_enable(void __iomem
*addr
, int value
)
199 writel(value
, addr
+ HI6220_TEMP0_RST_MSK
);
202 static inline void hi6220_thermal_enable(void __iomem
*addr
, int value
)
204 writel(value
, addr
+ HI6220_TEMP0_EN
);
207 static inline int hi6220_thermal_get_temperature(void __iomem
*addr
)
209 return hi6220_thermal_step_to_temp(readl(addr
+ HI6220_TEMP0_VALUE
));
215 * The temperature is coded in steps, cf. HI3660_TEMP_STEP.
217 * Min : 0x00 : 0.0 °C
218 * Max : 0x7F : 26.0 °C
221 static inline void hi3660_thermal_set_lag(void __iomem
*addr
,
224 writel(DIV_ROUND_UP(value
, HI3660_TEMP_STEP
) & 0x7F,
225 addr
+ HI3660_LAG(id
));
228 static inline void hi3660_thermal_alarm_clear(void __iomem
*addr
,
231 writel(value
, addr
+ HI3660_INT_CLR(id
));
234 static inline void hi3660_thermal_alarm_enable(void __iomem
*addr
,
237 writel(value
, addr
+ HI3660_INT_EN(id
));
240 static inline void hi3660_thermal_alarm_set(void __iomem
*addr
,
243 writel(value
, addr
+ HI3660_TH(id
));
246 static inline int hi3660_thermal_get_temperature(void __iomem
*addr
, int id
)
248 return hi3660_thermal_step_to_temp(readl(addr
+ HI3660_TEMP(id
)));
252 * Temperature configuration register - Sensor selection
256 * 0x0: local sensor (default)
257 * 0x1: remote sensor 1 (ACPU cluster 1)
258 * 0x2: remote sensor 2 (ACPU cluster 0)
259 * 0x3: remote sensor 3 (G3D)
261 static inline void hi6220_thermal_sensor_select(void __iomem
*addr
, int sensor
)
263 writel((readl(addr
+ HI6220_TEMP0_CFG
) & ~HI6220_TEMP0_CFG_SS_MSK
) |
264 (sensor
<< 12), addr
+ HI6220_TEMP0_CFG
);
268 * Temperature configuration register - Hdak conversion polling interval
277 static inline void hi6220_thermal_hdak_set(void __iomem
*addr
, int value
)
279 writel((readl(addr
+ HI6220_TEMP0_CFG
) & ~HI6220_TEMP0_CFG_HDAK_MSK
) |
280 (value
<< 4), addr
+ HI6220_TEMP0_CFG
);
283 static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor
*sensor
)
285 struct hisi_thermal_data
*data
= sensor
->data
;
287 hi6220_thermal_alarm_clear(data
->regs
, 1);
291 static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor
*sensor
)
293 struct hisi_thermal_data
*data
= sensor
->data
;
295 hi3660_thermal_alarm_clear(data
->regs
, sensor
->id
, 1);
299 static int hi6220_thermal_get_temp(struct hisi_thermal_sensor
*sensor
)
301 struct hisi_thermal_data
*data
= sensor
->data
;
303 return hi6220_thermal_get_temperature(data
->regs
);
306 static int hi3660_thermal_get_temp(struct hisi_thermal_sensor
*sensor
)
308 struct hisi_thermal_data
*data
= sensor
->data
;
310 return hi3660_thermal_get_temperature(data
->regs
, sensor
->id
);
313 static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor
*sensor
)
315 struct hisi_thermal_data
*data
= sensor
->data
;
317 /* disable sensor module */
318 hi6220_thermal_enable(data
->regs
, 0);
319 hi6220_thermal_alarm_enable(data
->regs
, 0);
320 hi6220_thermal_reset_enable(data
->regs
, 0);
322 clk_disable_unprepare(data
->clk
);
327 static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor
*sensor
)
329 struct hisi_thermal_data
*data
= sensor
->data
;
331 /* disable sensor module */
332 hi3660_thermal_alarm_enable(data
->regs
, sensor
->id
, 0);
336 static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor
*sensor
)
338 struct hisi_thermal_data
*data
= sensor
->data
;
341 /* enable clock for tsensor */
342 ret
= clk_prepare_enable(data
->clk
);
346 /* disable module firstly */
347 hi6220_thermal_reset_enable(data
->regs
, 0);
348 hi6220_thermal_enable(data
->regs
, 0);
350 /* select sensor id */
351 hi6220_thermal_sensor_select(data
->regs
, sensor
->id
);
353 /* setting the hdak time */
354 hi6220_thermal_hdak_set(data
->regs
, 0);
356 /* setting lag value between current temp and the threshold */
357 hi6220_thermal_set_lag(data
->regs
, HI6220_TEMP_LAG
);
359 /* enable for interrupt */
360 hi6220_thermal_alarm_set(data
->regs
, sensor
->thres_temp
);
362 hi6220_thermal_reset_set(data
->regs
, HI6220_TEMP_RESET
);
365 hi6220_thermal_reset_enable(data
->regs
, 1);
366 hi6220_thermal_enable(data
->regs
, 1);
368 hi6220_thermal_alarm_clear(data
->regs
, 0);
369 hi6220_thermal_alarm_enable(data
->regs
, 1);
374 static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor
*sensor
)
377 struct hisi_thermal_data
*data
= sensor
->data
;
379 /* disable interrupt */
380 hi3660_thermal_alarm_enable(data
->regs
, sensor
->id
, 0);
382 /* setting lag value between current temp and the threshold */
383 hi3660_thermal_set_lag(data
->regs
, sensor
->id
, HI3660_TEMP_LAG
);
385 /* set interrupt threshold */
386 value
= hi3660_thermal_temp_to_step(sensor
->thres_temp
);
387 hi3660_thermal_alarm_set(data
->regs
, sensor
->id
, value
);
389 /* enable interrupt */
390 hi3660_thermal_alarm_clear(data
->regs
, sensor
->id
, 1);
391 hi3660_thermal_alarm_enable(data
->regs
, sensor
->id
, 1);
396 static int hi6220_thermal_probe(struct hisi_thermal_data
*data
)
398 struct platform_device
*pdev
= data
->pdev
;
399 struct device
*dev
= &pdev
->dev
;
402 data
->clk
= devm_clk_get(dev
, "thermal_clk");
403 if (IS_ERR(data
->clk
)) {
404 ret
= PTR_ERR(data
->clk
);
405 if (ret
!= -EPROBE_DEFER
)
406 dev_err(dev
, "failed to get thermal clk: %d\n", ret
);
410 data
->sensor
= devm_kzalloc(dev
, sizeof(*data
->sensor
), GFP_KERNEL
);
414 data
->sensor
[0].id
= HI6220_CLUSTER0_SENSOR
;
415 data
->sensor
[0].irq_name
= "tsensor_intr";
416 data
->sensor
[0].data
= data
;
417 data
->nr_sensors
= 1;
422 static int hi3660_thermal_probe(struct hisi_thermal_data
*data
)
424 struct platform_device
*pdev
= data
->pdev
;
425 struct device
*dev
= &pdev
->dev
;
427 data
->nr_sensors
= 1;
429 data
->sensor
= devm_kzalloc(dev
, sizeof(*data
->sensor
) *
430 data
->nr_sensors
, GFP_KERNEL
);
434 data
->sensor
[0].id
= HI3660_BIG_SENSOR
;
435 data
->sensor
[0].irq_name
= "tsensor_a73";
436 data
->sensor
[0].data
= data
;
438 data
->sensor
[1].id
= HI3660_LITTLE_SENSOR
;
439 data
->sensor
[1].irq_name
= "tsensor_a53";
440 data
->sensor
[1].data
= data
;
445 static int hisi_thermal_get_temp(void *__data
, int *temp
)
447 struct hisi_thermal_sensor
*sensor
= __data
;
448 struct hisi_thermal_data
*data
= sensor
->data
;
450 *temp
= data
->ops
->get_temp(sensor
);
452 dev_dbg(&data
->pdev
->dev
, "tzd=%p, id=%d, temp=%d, thres=%d\n",
453 sensor
->tzd
, sensor
->id
, *temp
, sensor
->thres_temp
);
458 static const struct thermal_zone_of_device_ops hisi_of_thermal_ops
= {
459 .get_temp
= hisi_thermal_get_temp
,
462 static irqreturn_t
hisi_thermal_alarm_irq_thread(int irq
, void *dev
)
464 struct hisi_thermal_sensor
*sensor
= dev
;
465 struct hisi_thermal_data
*data
= sensor
->data
;
468 data
->ops
->irq_handler(sensor
);
470 hisi_thermal_get_temp(sensor
, &temp
);
472 if (temp
>= sensor
->thres_temp
) {
473 dev_crit(&data
->pdev
->dev
,
474 "sensor <%d> THERMAL ALARM: %d > %d\n",
475 sensor
->id
, temp
, sensor
->thres_temp
);
477 thermal_zone_device_update(sensor
->tzd
,
478 THERMAL_EVENT_UNSPECIFIED
);
481 dev_crit(&data
->pdev
->dev
,
482 "sensor <%d> THERMAL ALARM stopped: %d < %d\n",
483 sensor
->id
, temp
, sensor
->thres_temp
);
489 static int hisi_thermal_register_sensor(struct platform_device
*pdev
,
490 struct hisi_thermal_sensor
*sensor
)
493 const struct thermal_trip
*trip
;
495 sensor
->tzd
= devm_thermal_zone_of_sensor_register(&pdev
->dev
,
497 &hisi_of_thermal_ops
);
498 if (IS_ERR(sensor
->tzd
)) {
499 ret
= PTR_ERR(sensor
->tzd
);
501 dev_err(&pdev
->dev
, "failed to register sensor id %d: %d\n",
506 trip
= of_thermal_get_trip_points(sensor
->tzd
);
508 for (i
= 0; i
< of_thermal_get_ntrips(sensor
->tzd
); i
++) {
509 if (trip
[i
].type
== THERMAL_TRIP_PASSIVE
) {
510 sensor
->thres_temp
= trip
[i
].temperature
;
518 static const struct hisi_thermal_ops hi6220_ops
= {
519 .get_temp
= hi6220_thermal_get_temp
,
520 .enable_sensor
= hi6220_thermal_enable_sensor
,
521 .disable_sensor
= hi6220_thermal_disable_sensor
,
522 .irq_handler
= hi6220_thermal_irq_handler
,
523 .probe
= hi6220_thermal_probe
,
526 static const struct hisi_thermal_ops hi3660_ops
= {
527 .get_temp
= hi3660_thermal_get_temp
,
528 .enable_sensor
= hi3660_thermal_enable_sensor
,
529 .disable_sensor
= hi3660_thermal_disable_sensor
,
530 .irq_handler
= hi3660_thermal_irq_handler
,
531 .probe
= hi3660_thermal_probe
,
534 static const struct of_device_id of_hisi_thermal_match
[] = {
536 .compatible
= "hisilicon,tsensor",
540 .compatible
= "hisilicon,hi3660-tsensor",
545 MODULE_DEVICE_TABLE(of
, of_hisi_thermal_match
);
547 static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor
*sensor
,
550 struct thermal_zone_device
*tzd
= sensor
->tzd
;
552 tzd
->ops
->set_mode(tzd
,
553 on
? THERMAL_DEVICE_ENABLED
: THERMAL_DEVICE_DISABLED
);
556 static int hisi_thermal_probe(struct platform_device
*pdev
)
558 struct hisi_thermal_data
*data
;
559 struct device
*dev
= &pdev
->dev
;
560 struct resource
*res
;
563 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
568 platform_set_drvdata(pdev
, data
);
569 data
->ops
= of_device_get_match_data(dev
);
571 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
572 data
->regs
= devm_ioremap_resource(dev
, res
);
573 if (IS_ERR(data
->regs
)) {
574 dev_err(dev
, "failed to get io address\n");
575 return PTR_ERR(data
->regs
);
578 ret
= data
->ops
->probe(data
);
582 for (i
= 0; i
< data
->nr_sensors
; i
++) {
583 struct hisi_thermal_sensor
*sensor
= &data
->sensor
[i
];
585 ret
= hisi_thermal_register_sensor(pdev
, sensor
);
587 dev_err(dev
, "failed to register thermal sensor: %d\n",
592 ret
= platform_get_irq(pdev
, 0);
596 ret
= devm_request_threaded_irq(dev
, ret
, NULL
,
597 hisi_thermal_alarm_irq_thread
,
598 IRQF_ONESHOT
, sensor
->irq_name
,
601 dev_err(dev
, "Failed to request alarm irq: %d\n", ret
);
605 ret
= data
->ops
->enable_sensor(sensor
);
607 dev_err(dev
, "Failed to setup the sensor: %d\n", ret
);
611 hisi_thermal_toggle_sensor(sensor
, true);
617 static int hisi_thermal_remove(struct platform_device
*pdev
)
619 struct hisi_thermal_data
*data
= platform_get_drvdata(pdev
);
622 for (i
= 0; i
< data
->nr_sensors
; i
++) {
623 struct hisi_thermal_sensor
*sensor
= &data
->sensor
[i
];
625 hisi_thermal_toggle_sensor(sensor
, false);
626 data
->ops
->disable_sensor(sensor
);
632 #ifdef CONFIG_PM_SLEEP
633 static int hisi_thermal_suspend(struct device
*dev
)
635 struct hisi_thermal_data
*data
= dev_get_drvdata(dev
);
638 for (i
= 0; i
< data
->nr_sensors
; i
++)
639 data
->ops
->disable_sensor(&data
->sensor
[i
]);
644 static int hisi_thermal_resume(struct device
*dev
)
646 struct hisi_thermal_data
*data
= dev_get_drvdata(dev
);
649 for (i
= 0; i
< data
->nr_sensors
; i
++)
650 ret
|= data
->ops
->enable_sensor(&data
->sensor
[i
]);
656 static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops
,
657 hisi_thermal_suspend
, hisi_thermal_resume
);
659 static struct platform_driver hisi_thermal_driver
= {
661 .name
= "hisi_thermal",
662 .pm
= &hisi_thermal_pm_ops
,
663 .of_match_table
= of_hisi_thermal_match
,
665 .probe
= hisi_thermal_probe
,
666 .remove
= hisi_thermal_remove
,
669 module_platform_driver(hisi_thermal_driver
);
671 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
672 MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
673 MODULE_DESCRIPTION("Hisilicon thermal driver");
674 MODULE_LICENSE("GPL v2");