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_TH (0x4)
30 #define TEMP0_RST_TH (0x8)
31 #define TEMP0_CFG (0xC)
32 #define TEMP0_EN (0x10)
33 #define TEMP0_INT_EN (0x14)
34 #define TEMP0_INT_CLR (0x18)
35 #define TEMP0_RST_MSK (0x1C)
36 #define TEMP0_VALUE (0x28)
38 #define HISI_TEMP_BASE (-60000)
39 #define HISI_TEMP_RESET (100000)
40 #define HISI_TEMP_STEP (784)
42 #define HISI_MAX_SENSORS 4
44 struct hisi_thermal_sensor {
45 struct hisi_thermal_data *thermal;
46 struct thermal_zone_device *tzd;
53 struct hisi_thermal_data {
54 struct mutex thermal_lock; /* protects register data */
55 struct platform_device *pdev;
57 struct hisi_thermal_sensor sensors[HISI_MAX_SENSORS];
59 int irq, irq_bind_sensor;
66 * The temperature computation on the tsensor is as follow:
67 * Unit: millidegree Celsius
68 * Step: 255/200 (0.7843)
69 * Temperature base: -60°C
71 * The register is programmed in temperature steps, every step is 784
72 * millidegree and begins at -60 000 m°C
74 * The temperature from the steps:
76 * Temp = TempBase + (steps x 784)
78 * and the steps from the temperature:
80 * steps = (Temp - TempBase) / 784
83 static inline int hisi_thermal_step_to_temp(int step)
85 return HISI_TEMP_BASE + (step * HISI_TEMP_STEP);
88 static inline long hisi_thermal_temp_to_step(long temp)
90 return (temp - HISI_TEMP_BASE) / HISI_TEMP_STEP;
93 static inline long hisi_thermal_round_temp(int temp)
95 return hisi_thermal_step_to_temp(
96 hisi_thermal_temp_to_step(temp));
99 static long hisi_thermal_get_sensor_temp(struct hisi_thermal_data *data,
100 struct hisi_thermal_sensor *sensor)
104 mutex_lock(&data->thermal_lock);
106 /* disable interrupt */
107 writel(0x0, data->regs + TEMP0_INT_EN);
108 writel(0x1, data->regs + TEMP0_INT_CLR);
110 /* disable module firstly */
111 writel(0x0, data->regs + TEMP0_EN);
113 /* select sensor id */
114 writel((sensor->id << 12), data->regs + TEMP0_CFG);
117 writel(0x1, data->regs + TEMP0_EN);
119 usleep_range(3000, 5000);
121 val = readl(data->regs + TEMP0_VALUE);
122 val = hisi_thermal_step_to_temp(val);
124 mutex_unlock(&data->thermal_lock);
129 static void hisi_thermal_enable_bind_irq_sensor
130 (struct hisi_thermal_data *data)
132 struct hisi_thermal_sensor *sensor;
134 mutex_lock(&data->thermal_lock);
136 sensor = &data->sensors[data->irq_bind_sensor];
138 /* setting the hdak time */
139 writel(0x0, data->regs + TEMP0_CFG);
141 /* disable module firstly */
142 writel(0x0, data->regs + TEMP0_RST_MSK);
143 writel(0x0, data->regs + TEMP0_EN);
145 /* select sensor id */
146 writel((sensor->id << 12), data->regs + TEMP0_CFG);
148 /* enable for interrupt */
149 writel(hisi_thermal_temp_to_step(sensor->thres_temp) | 0x0FFFFFF00,
150 data->regs + TEMP0_TH);
152 writel(hisi_thermal_temp_to_step(HISI_TEMP_RESET),
153 data->regs + TEMP0_RST_TH);
156 writel(0x1, data->regs + TEMP0_RST_MSK);
157 writel(0x1, data->regs + TEMP0_EN);
159 writel(0x0, data->regs + TEMP0_INT_CLR);
160 writel(0x1, data->regs + TEMP0_INT_EN);
162 usleep_range(3000, 5000);
164 mutex_unlock(&data->thermal_lock);
167 static void hisi_thermal_disable_sensor(struct hisi_thermal_data *data)
169 mutex_lock(&data->thermal_lock);
171 /* disable sensor module */
172 writel(0x0, data->regs + TEMP0_INT_EN);
173 writel(0x0, data->regs + TEMP0_RST_MSK);
174 writel(0x0, data->regs + TEMP0_EN);
176 mutex_unlock(&data->thermal_lock);
179 static int hisi_thermal_get_temp(void *_sensor, int *temp)
181 struct hisi_thermal_sensor *sensor = _sensor;
182 struct hisi_thermal_data *data = sensor->thermal;
184 int sensor_id = -1, i;
187 *temp = hisi_thermal_get_sensor_temp(data, sensor);
189 sensor->sensor_temp = *temp;
191 for (i = 0; i < HISI_MAX_SENSORS; i++) {
192 if (!data->sensors[i].tzd)
195 if (data->sensors[i].sensor_temp >= max_temp) {
196 max_temp = data->sensors[i].sensor_temp;
201 /* If no sensor has been enabled, then skip to enable irq */
205 mutex_lock(&data->thermal_lock);
206 data->irq_bind_sensor = sensor_id;
207 mutex_unlock(&data->thermal_lock);
209 dev_dbg(&data->pdev->dev, "id=%d, irq=%d, temp=%d, thres=%d\n",
210 sensor->id, data->irq_enabled, *temp, sensor->thres_temp);
212 * Bind irq to sensor for two cases:
213 * Reenable alarm IRQ if temperature below threshold;
214 * if irq has been enabled, always set it;
216 if (data->irq_enabled) {
217 hisi_thermal_enable_bind_irq_sensor(data);
221 if (max_temp < sensor->thres_temp) {
222 data->irq_enabled = true;
223 hisi_thermal_enable_bind_irq_sensor(data);
224 enable_irq(data->irq);
230 static struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
231 .get_temp = hisi_thermal_get_temp,
234 static irqreturn_t hisi_thermal_alarm_irq(int irq, void *dev)
236 struct hisi_thermal_data *data = dev;
238 disable_irq_nosync(irq);
239 data->irq_enabled = false;
241 return IRQ_WAKE_THREAD;
244 static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
246 struct hisi_thermal_data *data = dev;
247 struct hisi_thermal_sensor *sensor;
250 mutex_lock(&data->thermal_lock);
251 sensor = &data->sensors[data->irq_bind_sensor];
253 dev_crit(&data->pdev->dev, "THERMAL ALARM: T > %d\n",
255 mutex_unlock(&data->thermal_lock);
257 for (i = 0; i < HISI_MAX_SENSORS; i++) {
258 if (!data->sensors[i].tzd)
261 thermal_zone_device_update(data->sensors[i].tzd,
262 THERMAL_EVENT_UNSPECIFIED);
268 static int hisi_thermal_register_sensor(struct platform_device *pdev,
269 struct hisi_thermal_data *data,
270 struct hisi_thermal_sensor *sensor,
274 const struct thermal_trip *trip;
277 sensor->thermal = data;
279 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
280 sensor->id, sensor, &hisi_of_thermal_ops);
281 if (IS_ERR(sensor->tzd)) {
282 ret = PTR_ERR(sensor->tzd);
284 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
289 trip = of_thermal_get_trip_points(sensor->tzd);
291 for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
292 if (trip[i].type == THERMAL_TRIP_PASSIVE) {
293 sensor->thres_temp = hisi_thermal_round_temp(trip[i].temperature);
301 static const struct of_device_id of_hisi_thermal_match[] = {
302 { .compatible = "hisilicon,tsensor" },
305 MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
307 static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
310 struct thermal_zone_device *tzd = sensor->tzd;
312 tzd->ops->set_mode(tzd,
313 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
316 static int hisi_thermal_probe(struct platform_device *pdev)
318 struct hisi_thermal_data *data;
319 struct resource *res;
323 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
327 mutex_init(&data->thermal_lock);
330 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
331 data->regs = devm_ioremap_resource(&pdev->dev, res);
332 if (IS_ERR(data->regs)) {
333 dev_err(&pdev->dev, "failed to get io address\n");
334 return PTR_ERR(data->regs);
337 data->irq = platform_get_irq(pdev, 0);
341 platform_set_drvdata(pdev, data);
343 data->clk = devm_clk_get(&pdev->dev, "thermal_clk");
344 if (IS_ERR(data->clk)) {
345 ret = PTR_ERR(data->clk);
346 if (ret != -EPROBE_DEFER)
348 "failed to get thermal clk: %d\n", ret);
352 /* enable clock for thermal */
353 ret = clk_prepare_enable(data->clk);
355 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
359 hisi_thermal_enable_bind_irq_sensor(data);
360 data->irq_enabled = true;
362 for (i = 0; i < HISI_MAX_SENSORS; ++i) {
363 ret = hisi_thermal_register_sensor(pdev, data,
364 &data->sensors[i], i);
367 "failed to register thermal sensor: %d\n", ret);
369 hisi_thermal_toggle_sensor(&data->sensors[i], true);
372 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
373 hisi_thermal_alarm_irq,
374 hisi_thermal_alarm_irq_thread,
375 0, "hisi_thermal", data);
377 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
381 enable_irq(data->irq);
386 static int hisi_thermal_remove(struct platform_device *pdev)
388 struct hisi_thermal_data *data = platform_get_drvdata(pdev);
391 for (i = 0; i < HISI_MAX_SENSORS; i++) {
392 struct hisi_thermal_sensor *sensor = &data->sensors[i];
397 hisi_thermal_toggle_sensor(sensor, false);
400 hisi_thermal_disable_sensor(data);
401 clk_disable_unprepare(data->clk);
406 #ifdef CONFIG_PM_SLEEP
407 static int hisi_thermal_suspend(struct device *dev)
409 struct hisi_thermal_data *data = dev_get_drvdata(dev);
411 hisi_thermal_disable_sensor(data);
412 data->irq_enabled = false;
414 clk_disable_unprepare(data->clk);
419 static int hisi_thermal_resume(struct device *dev)
421 struct hisi_thermal_data *data = dev_get_drvdata(dev);
424 ret = clk_prepare_enable(data->clk);
428 data->irq_enabled = true;
429 hisi_thermal_enable_bind_irq_sensor(data);
435 static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
436 hisi_thermal_suspend, hisi_thermal_resume);
438 static struct platform_driver hisi_thermal_driver = {
440 .name = "hisi_thermal",
441 .pm = &hisi_thermal_pm_ops,
442 .of_match_table = of_hisi_thermal_match,
444 .probe = hisi_thermal_probe,
445 .remove = hisi_thermal_remove,
448 module_platform_driver(hisi_thermal_driver);
450 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
451 MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
452 MODULE_DESCRIPTION("Hisilicon thermal driver");
453 MODULE_LICENSE("GPL v2");