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_EN (0x10)
34 #define TEMP0_INT_EN (0x14)
35 #define TEMP0_INT_CLR (0x18)
36 #define TEMP0_RST_MSK (0x1C)
37 #define TEMP0_VALUE (0x28)
39 #define HISI_TEMP_BASE (-60000)
40 #define HISI_TEMP_RESET (100000)
41 #define HISI_TEMP_STEP (784)
43 #define HISI_MAX_SENSORS 4
44 #define HISI_DEFAULT_SENSOR 2
46 struct hisi_thermal_sensor {
47 struct hisi_thermal_data *thermal;
48 struct thermal_zone_device *tzd;
55 struct hisi_thermal_data {
56 struct mutex thermal_lock; /* protects register data */
57 struct platform_device *pdev;
59 struct hisi_thermal_sensor sensors;
67 * The temperature computation on the tsensor is as follow:
68 * Unit: millidegree Celsius
69 * Step: 255/200 (0.7843)
70 * Temperature base: -60°C
72 * The register is programmed in temperature steps, every step is 784
73 * millidegree and begins at -60 000 m°C
75 * The temperature from the steps:
77 * Temp = TempBase + (steps x 784)
79 * and the steps from the temperature:
81 * steps = (Temp - TempBase) / 784
84 static inline int hisi_thermal_step_to_temp(int step)
86 return HISI_TEMP_BASE + (step * HISI_TEMP_STEP);
89 static inline long hisi_thermal_temp_to_step(long temp)
91 return (temp - HISI_TEMP_BASE) / HISI_TEMP_STEP;
94 static inline long hisi_thermal_round_temp(int temp)
96 return hisi_thermal_step_to_temp(
97 hisi_thermal_temp_to_step(temp));
100 static inline void hisi_thermal_set_lag(void __iomem *addr, int value)
102 writel(value, addr + TEMP0_LAG);
105 static inline void hisi_thermal_alarm_clear(void __iomem *addr, int value)
107 writel(value, addr + TEMP0_INT_CLR);
110 static inline void hisi_thermal_alarm_enable(void __iomem *addr, int value)
112 writel(value, addr + TEMP0_INT_EN);
115 static inline void hisi_thermal_alarm_set(void __iomem *addr, int temp)
117 writel(hisi_thermal_temp_to_step(temp) | 0x0FFFFFF00, addr + TEMP0_TH);
120 static inline void hisi_thermal_reset_set(void __iomem *addr, int temp)
122 writel(hisi_thermal_temp_to_step(temp), addr + TEMP0_RST_TH);
125 static inline void hisi_thermal_reset_enable(void __iomem *addr, int value)
127 writel(value, addr + TEMP0_RST_MSK);
130 static inline void hisi_thermal_enable(void __iomem *addr, int value)
132 writel(value, addr + TEMP0_EN);
135 static inline void hisi_thermal_sensor_select(void __iomem *addr, int sensor)
137 writel((sensor << 12), addr + TEMP0_CFG);
140 static inline int hisi_thermal_get_temperature(void __iomem *addr)
142 return hisi_thermal_step_to_temp(readl(addr + TEMP0_VALUE));
145 static inline void hisi_thermal_hdak_set(void __iomem *addr, int value)
147 writel(value, addr + TEMP0_CFG);
150 static long hisi_thermal_get_sensor_temp(struct hisi_thermal_data *data,
151 struct hisi_thermal_sensor *sensor)
155 mutex_lock(&data->thermal_lock);
157 /* disable interrupt */
158 hisi_thermal_alarm_enable(data->regs, 0);
159 hisi_thermal_alarm_clear(data->regs, 1);
161 /* disable module firstly */
162 hisi_thermal_enable(data->regs, 0);
164 /* select sensor id */
165 hisi_thermal_sensor_select(data->regs, sensor->id);
168 hisi_thermal_enable(data->regs, 1);
170 usleep_range(3000, 5000);
172 val = hisi_thermal_get_temperature(data->regs);
174 mutex_unlock(&data->thermal_lock);
179 static void hisi_thermal_enable_bind_irq_sensor
180 (struct hisi_thermal_data *data)
182 struct hisi_thermal_sensor *sensor;
184 mutex_lock(&data->thermal_lock);
186 sensor = &data->sensors;
188 /* setting the hdak time */
189 hisi_thermal_hdak_set(data->regs, 0);
191 /* disable module firstly */
192 hisi_thermal_reset_enable(data->regs, 0);
193 hisi_thermal_enable(data->regs, 0);
195 /* select sensor id */
196 hisi_thermal_sensor_select(data->regs, sensor->id);
198 /* enable for interrupt */
199 hisi_thermal_alarm_set(data->regs, sensor->thres_temp);
201 hisi_thermal_reset_set(data->regs, HISI_TEMP_RESET);
204 hisi_thermal_reset_enable(data->regs, 1);
205 hisi_thermal_enable(data->regs, 1);
207 hisi_thermal_alarm_clear(data->regs, 0);
208 hisi_thermal_alarm_enable(data->regs, 1);
210 usleep_range(3000, 5000);
212 mutex_unlock(&data->thermal_lock);
215 static void hisi_thermal_disable_sensor(struct hisi_thermal_data *data)
217 mutex_lock(&data->thermal_lock);
219 /* disable sensor module */
220 hisi_thermal_enable(data->regs, 0);
221 hisi_thermal_alarm_enable(data->regs, 0);
222 hisi_thermal_reset_enable(data->regs, 0);
224 mutex_unlock(&data->thermal_lock);
227 static int hisi_thermal_get_temp(void *_sensor, int *temp)
229 struct hisi_thermal_sensor *sensor = _sensor;
230 struct hisi_thermal_data *data = sensor->thermal;
232 *temp = hisi_thermal_get_sensor_temp(data, sensor);
234 dev_dbg(&data->pdev->dev, "id=%d, irq=%d, temp=%d, thres=%d\n",
235 sensor->id, data->irq_enabled, *temp, sensor->thres_temp);
237 * Bind irq to sensor for two cases:
238 * Reenable alarm IRQ if temperature below threshold;
239 * if irq has been enabled, always set it;
241 if (data->irq_enabled) {
242 hisi_thermal_enable_bind_irq_sensor(data);
246 if (*temp < sensor->thres_temp) {
247 data->irq_enabled = true;
248 hisi_thermal_enable_bind_irq_sensor(data);
249 enable_irq(data->irq);
255 static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
256 .get_temp = hisi_thermal_get_temp,
259 static irqreturn_t hisi_thermal_alarm_irq(int irq, void *dev)
261 struct hisi_thermal_data *data = dev;
263 disable_irq_nosync(irq);
264 data->irq_enabled = false;
266 return IRQ_WAKE_THREAD;
269 static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
271 struct hisi_thermal_data *data = dev;
272 struct hisi_thermal_sensor *sensor = &data->sensors;
274 dev_crit(&data->pdev->dev, "THERMAL ALARM: T > %d\n",
277 thermal_zone_device_update(data->sensors.tzd,
278 THERMAL_EVENT_UNSPECIFIED);
283 static int hisi_thermal_register_sensor(struct platform_device *pdev,
284 struct hisi_thermal_data *data,
285 struct hisi_thermal_sensor *sensor,
289 const struct thermal_trip *trip;
292 sensor->thermal = data;
294 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
295 sensor->id, sensor, &hisi_of_thermal_ops);
296 if (IS_ERR(sensor->tzd)) {
297 ret = PTR_ERR(sensor->tzd);
299 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
304 trip = of_thermal_get_trip_points(sensor->tzd);
306 for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
307 if (trip[i].type == THERMAL_TRIP_PASSIVE) {
308 sensor->thres_temp = hisi_thermal_round_temp(trip[i].temperature);
316 static const struct of_device_id of_hisi_thermal_match[] = {
317 { .compatible = "hisilicon,tsensor" },
320 MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
322 static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
325 struct thermal_zone_device *tzd = sensor->tzd;
327 tzd->ops->set_mode(tzd,
328 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
331 static int hisi_thermal_probe(struct platform_device *pdev)
333 struct hisi_thermal_data *data;
334 struct resource *res;
337 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
341 mutex_init(&data->thermal_lock);
344 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
345 data->regs = devm_ioremap_resource(&pdev->dev, res);
346 if (IS_ERR(data->regs)) {
347 dev_err(&pdev->dev, "failed to get io address\n");
348 return PTR_ERR(data->regs);
351 data->irq = platform_get_irq(pdev, 0);
355 platform_set_drvdata(pdev, data);
357 data->clk = devm_clk_get(&pdev->dev, "thermal_clk");
358 if (IS_ERR(data->clk)) {
359 ret = PTR_ERR(data->clk);
360 if (ret != -EPROBE_DEFER)
362 "failed to get thermal clk: %d\n", ret);
366 /* enable clock for thermal */
367 ret = clk_prepare_enable(data->clk);
369 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
373 hisi_thermal_enable_bind_irq_sensor(data);
374 data->irq_enabled = true;
376 ret = hisi_thermal_register_sensor(pdev, data,
378 HISI_DEFAULT_SENSOR);
380 dev_err(&pdev->dev, "failed to register thermal sensor: %d\n",
385 hisi_thermal_toggle_sensor(&data->sensors, true);
387 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
388 hisi_thermal_alarm_irq,
389 hisi_thermal_alarm_irq_thread,
390 0, "hisi_thermal", data);
392 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
396 enable_irq(data->irq);
401 static int hisi_thermal_remove(struct platform_device *pdev)
403 struct hisi_thermal_data *data = platform_get_drvdata(pdev);
404 struct hisi_thermal_sensor *sensor = &data->sensors;
406 hisi_thermal_toggle_sensor(sensor, false);
407 hisi_thermal_disable_sensor(data);
408 clk_disable_unprepare(data->clk);
413 #ifdef CONFIG_PM_SLEEP
414 static int hisi_thermal_suspend(struct device *dev)
416 struct hisi_thermal_data *data = dev_get_drvdata(dev);
418 hisi_thermal_disable_sensor(data);
419 data->irq_enabled = false;
421 clk_disable_unprepare(data->clk);
426 static int hisi_thermal_resume(struct device *dev)
428 struct hisi_thermal_data *data = dev_get_drvdata(dev);
431 ret = clk_prepare_enable(data->clk);
435 data->irq_enabled = true;
436 hisi_thermal_enable_bind_irq_sensor(data);
442 static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
443 hisi_thermal_suspend, hisi_thermal_resume);
445 static struct platform_driver hisi_thermal_driver = {
447 .name = "hisi_thermal",
448 .pm = &hisi_thermal_pm_ops,
449 .of_match_table = of_hisi_thermal_match,
451 .probe = hisi_thermal_probe,
452 .remove = hisi_thermal_remove,
455 module_platform_driver(hisi_thermal_driver);
457 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
458 MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
459 MODULE_DESCRIPTION("Hisilicon thermal driver");
460 MODULE_LICENSE("GPL v2");