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>
27 #include "thermal_core.h"
29 #define TEMP0_LAG (0x0)
30 #define TEMP0_TH (0x4)
31 #define TEMP0_RST_TH (0x8)
32 #define TEMP0_CFG (0xC)
33 #define TEMP0_CFG_SS_MSK (0xF000)
34 #define TEMP0_CFG_HDAK_MSK (0x30)
35 #define TEMP0_EN (0x10)
36 #define TEMP0_INT_EN (0x14)
37 #define TEMP0_INT_CLR (0x18)
38 #define TEMP0_RST_MSK (0x1C)
39 #define TEMP0_VALUE (0x28)
41 #define HISI_TEMP_BASE (-60000)
42 #define HISI_TEMP_RESET (100000)
43 #define HISI_TEMP_STEP (784)
44 #define HISI_TEMP_LAG (3500)
46 #define HISI_MAX_SENSORS 4
47 #define HISI_DEFAULT_SENSOR 2
49 struct hisi_thermal_sensor {
50 struct hisi_thermal_data *thermal;
51 struct thermal_zone_device *tzd;
58 struct hisi_thermal_data {
59 struct mutex thermal_lock; /* protects register data */
60 struct platform_device *pdev;
62 struct hisi_thermal_sensor sensors;
68 * The temperature computation on the tsensor is as follow:
69 * Unit: millidegree Celsius
70 * Step: 255/200 (0.7843)
71 * Temperature base: -60°C
73 * The register is programmed in temperature steps, every step is 784
74 * millidegree and begins at -60 000 m°C
76 * The temperature from the steps:
78 * Temp = TempBase + (steps x 784)
80 * and the steps from the temperature:
82 * steps = (Temp - TempBase) / 784
85 static inline int hisi_thermal_step_to_temp(int step)
87 return HISI_TEMP_BASE + (step * HISI_TEMP_STEP);
90 static inline long hisi_thermal_temp_to_step(long temp)
92 return (temp - HISI_TEMP_BASE) / HISI_TEMP_STEP;
95 static inline long hisi_thermal_round_temp(int temp)
97 return hisi_thermal_step_to_temp(
98 hisi_thermal_temp_to_step(temp));
102 * The lag register contains 5 bits encoding the temperature in steps.
104 * Each time the temperature crosses the threshold boundary, an
105 * interrupt is raised. It could be when the temperature is going
106 * above the threshold or below. However, if the temperature is
107 * fluctuating around this value due to the load, we can receive
108 * several interrupts which may not desired.
110 * We can setup a temperature representing the delta between the
111 * threshold and the current temperature when the temperature is
114 * For instance: the lag register is 5°C, the threshold is 65°C, when
115 * the temperature reaches 65°C an interrupt is raised and when the
116 * temperature decrease to 65°C - 5°C another interrupt is raised.
118 * A very short lag can lead to an interrupt storm, a long lag
119 * increase the latency to react to the temperature changes. In our
120 * case, that is not really a problem as we are polling the
123 * [0:4] : lag register
125 * The temperature is coded in steps, cf. HISI_TEMP_STEP.
127 * Min : 0x00 : 0.0 °C
128 * Max : 0x1F : 24.3 °C
130 * The 'value' parameter is in milliCelsius.
132 static inline void hisi_thermal_set_lag(void __iomem *addr, int value)
134 writel((value / HISI_TEMP_STEP) & 0x1F, addr + TEMP0_LAG);
137 static inline void hisi_thermal_alarm_clear(void __iomem *addr, int value)
139 writel(value, addr + TEMP0_INT_CLR);
142 static inline void hisi_thermal_alarm_enable(void __iomem *addr, int value)
144 writel(value, addr + TEMP0_INT_EN);
147 static inline void hisi_thermal_alarm_set(void __iomem *addr, int temp)
149 writel(hisi_thermal_temp_to_step(temp) | 0x0FFFFFF00, addr + TEMP0_TH);
152 static inline void hisi_thermal_reset_set(void __iomem *addr, int temp)
154 writel(hisi_thermal_temp_to_step(temp), addr + TEMP0_RST_TH);
157 static inline void hisi_thermal_reset_enable(void __iomem *addr, int value)
159 writel(value, addr + TEMP0_RST_MSK);
162 static inline void hisi_thermal_enable(void __iomem *addr, int value)
164 writel(value, addr + TEMP0_EN);
167 static inline int hisi_thermal_get_temperature(void __iomem *addr)
169 return hisi_thermal_step_to_temp(readl(addr + TEMP0_VALUE));
173 * Temperature configuration register - Sensor selection
177 * 0x0: local sensor (default)
178 * 0x1: remote sensor 1 (ACPU cluster 1)
179 * 0x2: remote sensor 2 (ACPU cluster 0)
180 * 0x3: remote sensor 3 (G3D)
182 static inline void hisi_thermal_sensor_select(void __iomem *addr, int sensor)
184 writel((readl(addr + TEMP0_CFG) & ~TEMP0_CFG_SS_MSK) |
185 (sensor << 12), addr + TEMP0_CFG);
189 * Temperature configuration register - Hdak conversion polling interval
198 static inline void hisi_thermal_hdak_set(void __iomem *addr, int value)
200 writel((readl(addr + TEMP0_CFG) & ~TEMP0_CFG_HDAK_MSK) |
201 (value << 4), addr + TEMP0_CFG);
204 static void hisi_thermal_disable_sensor(struct hisi_thermal_data *data)
206 mutex_lock(&data->thermal_lock);
208 /* disable sensor module */
209 hisi_thermal_enable(data->regs, 0);
210 hisi_thermal_alarm_enable(data->regs, 0);
211 hisi_thermal_reset_enable(data->regs, 0);
213 mutex_unlock(&data->thermal_lock);
216 static int hisi_thermal_get_temp(void *_sensor, int *temp)
218 struct hisi_thermal_sensor *sensor = _sensor;
219 struct hisi_thermal_data *data = sensor->thermal;
221 *temp = hisi_thermal_get_temperature(data->regs);
223 dev_dbg(&data->pdev->dev, "id=%d, temp=%d, thres=%d\n",
224 sensor->id, *temp, sensor->thres_temp);
229 static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
230 .get_temp = hisi_thermal_get_temp,
233 static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
235 struct hisi_thermal_data *data = dev;
236 struct hisi_thermal_sensor *sensor = &data->sensors;
239 hisi_thermal_alarm_clear(data->regs, 1);
241 temp = hisi_thermal_get_temperature(data->regs);
243 if (temp >= sensor->thres_temp) {
244 dev_crit(&data->pdev->dev, "THERMAL ALARM: %d > %d\n",
245 temp, sensor->thres_temp);
247 thermal_zone_device_update(data->sensors.tzd,
248 THERMAL_EVENT_UNSPECIFIED);
250 } else if (temp < sensor->thres_temp) {
251 dev_crit(&data->pdev->dev, "THERMAL ALARM stopped: %d < %d\n",
252 temp, sensor->thres_temp);
258 static int hisi_thermal_register_sensor(struct platform_device *pdev,
259 struct hisi_thermal_data *data,
260 struct hisi_thermal_sensor *sensor,
264 const struct thermal_trip *trip;
267 sensor->thermal = data;
269 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
270 sensor->id, sensor, &hisi_of_thermal_ops);
271 if (IS_ERR(sensor->tzd)) {
272 ret = PTR_ERR(sensor->tzd);
274 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
279 trip = of_thermal_get_trip_points(sensor->tzd);
281 for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
282 if (trip[i].type == THERMAL_TRIP_PASSIVE) {
283 sensor->thres_temp = hisi_thermal_round_temp(trip[i].temperature);
291 static const struct of_device_id of_hisi_thermal_match[] = {
292 { .compatible = "hisilicon,tsensor" },
295 MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
297 static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
300 struct thermal_zone_device *tzd = sensor->tzd;
302 tzd->ops->set_mode(tzd,
303 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
306 static int hisi_thermal_setup(struct hisi_thermal_data *data)
308 struct hisi_thermal_sensor *sensor;
310 sensor = &data->sensors;
312 /* disable module firstly */
313 hisi_thermal_reset_enable(data->regs, 0);
314 hisi_thermal_enable(data->regs, 0);
316 /* select sensor id */
317 hisi_thermal_sensor_select(data->regs, sensor->id);
319 /* setting the hdak time */
320 hisi_thermal_hdak_set(data->regs, 0);
322 /* setting lag value between current temp and the threshold */
323 hisi_thermal_set_lag(data->regs, HISI_TEMP_LAG);
325 /* enable for interrupt */
326 hisi_thermal_alarm_set(data->regs, sensor->thres_temp);
328 hisi_thermal_reset_set(data->regs, HISI_TEMP_RESET);
331 hisi_thermal_reset_enable(data->regs, 1);
332 hisi_thermal_enable(data->regs, 1);
334 hisi_thermal_alarm_clear(data->regs, 0);
335 hisi_thermal_alarm_enable(data->regs, 1);
340 static int hisi_thermal_probe(struct platform_device *pdev)
342 struct hisi_thermal_data *data;
343 struct resource *res;
346 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
350 mutex_init(&data->thermal_lock);
353 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
354 data->regs = devm_ioremap_resource(&pdev->dev, res);
355 if (IS_ERR(data->regs)) {
356 dev_err(&pdev->dev, "failed to get io address\n");
357 return PTR_ERR(data->regs);
360 data->irq = platform_get_irq(pdev, 0);
364 platform_set_drvdata(pdev, data);
366 data->clk = devm_clk_get(&pdev->dev, "thermal_clk");
367 if (IS_ERR(data->clk)) {
368 ret = PTR_ERR(data->clk);
369 if (ret != -EPROBE_DEFER)
371 "failed to get thermal clk: %d\n", ret);
375 /* enable clock for thermal */
376 ret = clk_prepare_enable(data->clk);
378 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
382 ret = hisi_thermal_register_sensor(pdev, data,
384 HISI_DEFAULT_SENSOR);
386 dev_err(&pdev->dev, "failed to register thermal sensor: %d\n",
391 ret = hisi_thermal_setup(data);
393 dev_err(&pdev->dev, "Failed to setup the sensor: %d\n", ret);
397 ret = devm_request_threaded_irq(&pdev->dev, data->irq, NULL,
398 hisi_thermal_alarm_irq_thread,
399 IRQF_ONESHOT, "hisi_thermal", data);
401 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
405 hisi_thermal_toggle_sensor(&data->sensors, true);
410 static int hisi_thermal_remove(struct platform_device *pdev)
412 struct hisi_thermal_data *data = platform_get_drvdata(pdev);
413 struct hisi_thermal_sensor *sensor = &data->sensors;
415 hisi_thermal_toggle_sensor(sensor, false);
416 hisi_thermal_disable_sensor(data);
417 clk_disable_unprepare(data->clk);
422 #ifdef CONFIG_PM_SLEEP
423 static int hisi_thermal_suspend(struct device *dev)
425 struct hisi_thermal_data *data = dev_get_drvdata(dev);
427 hisi_thermal_disable_sensor(data);
429 clk_disable_unprepare(data->clk);
434 static int hisi_thermal_resume(struct device *dev)
436 struct hisi_thermal_data *data = dev_get_drvdata(dev);
439 ret = clk_prepare_enable(data->clk);
443 hisi_thermal_setup(data);
449 static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
450 hisi_thermal_suspend, hisi_thermal_resume);
452 static struct platform_driver hisi_thermal_driver = {
454 .name = "hisi_thermal",
455 .pm = &hisi_thermal_pm_ops,
456 .of_match_table = of_hisi_thermal_match,
458 .probe = hisi_thermal_probe,
459 .remove = hisi_thermal_remove,
462 module_platform_driver(hisi_thermal_driver);
464 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
465 MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
466 MODULE_DESCRIPTION("Hisilicon thermal driver");
467 MODULE_LICENSE("GPL v2");