blob: c5285ed34fdda7e13fddc7411d394643cbe9afaf [file] [log] [blame]
kongxinwei9a5238a2015-05-20 19:16:37 +08001/*
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>
26
27#include "thermal_core.h"
28
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)
37
Daniel Lezcano1b2c46a2017-10-19 19:05:46 +020038#define HISI_TEMP_BASE (-60000)
kongxinwei9a5238a2015-05-20 19:16:37 +080039#define HISI_TEMP_RESET (100000)
Daniel Lezcano1b2c46a2017-10-19 19:05:46 +020040#define HISI_TEMP_STEP (784)
kongxinwei9a5238a2015-05-20 19:16:37 +080041
42#define HISI_MAX_SENSORS 4
43
44struct hisi_thermal_sensor {
45 struct hisi_thermal_data *thermal;
46 struct thermal_zone_device *tzd;
47
48 long sensor_temp;
49 uint32_t id;
50 uint32_t thres_temp;
51};
52
53struct hisi_thermal_data {
54 struct mutex thermal_lock; /* protects register data */
55 struct platform_device *pdev;
56 struct clk *clk;
57 struct hisi_thermal_sensor sensors[HISI_MAX_SENSORS];
58
59 int irq, irq_bind_sensor;
60 bool irq_enabled;
61
62 void __iomem *regs;
63};
64
Daniel Lezcano1b2c46a2017-10-19 19:05:46 +020065/*
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
70 *
71 * The register is programmed in temperature steps, every step is 784
72 * millidegree and begins at -60 000 m°C
73 *
74 * The temperature from the steps:
75 *
76 * Temp = TempBase + (steps x 784)
77 *
78 * and the steps from the temperature:
79 *
80 * steps = (Temp - TempBase) / 784
81 *
82 */
83static inline int hisi_thermal_step_to_temp(int step)
kongxinwei9a5238a2015-05-20 19:16:37 +080084{
Daniel Lezcano1b2c46a2017-10-19 19:05:46 +020085 return HISI_TEMP_BASE + (step * HISI_TEMP_STEP);
kongxinwei9a5238a2015-05-20 19:16:37 +080086}
87
Daniel Lezcano1b2c46a2017-10-19 19:05:46 +020088static inline long hisi_thermal_temp_to_step(long temp)
kongxinwei9a5238a2015-05-20 19:16:37 +080089{
Daniel Lezcano1b2c46a2017-10-19 19:05:46 +020090 return (temp - HISI_TEMP_BASE) / HISI_TEMP_STEP;
kongxinwei9a5238a2015-05-20 19:16:37 +080091}
92
Daniel Lezcano3cff9072017-10-19 19:05:47 +020093static inline long hisi_thermal_round_temp(int temp)
94{
95 return hisi_thermal_step_to_temp(
96 hisi_thermal_temp_to_step(temp));
97}
98
kongxinwei9a5238a2015-05-20 19:16:37 +080099static long hisi_thermal_get_sensor_temp(struct hisi_thermal_data *data,
100 struct hisi_thermal_sensor *sensor)
101{
102 long val;
103
104 mutex_lock(&data->thermal_lock);
105
106 /* disable interrupt */
107 writel(0x0, data->regs + TEMP0_INT_EN);
108 writel(0x1, data->regs + TEMP0_INT_CLR);
109
110 /* disable module firstly */
111 writel(0x0, data->regs + TEMP0_EN);
112
113 /* select sensor id */
114 writel((sensor->id << 12), data->regs + TEMP0_CFG);
115
116 /* enable module */
117 writel(0x1, data->regs + TEMP0_EN);
118
119 usleep_range(3000, 5000);
120
121 val = readl(data->regs + TEMP0_VALUE);
Daniel Lezcano1b2c46a2017-10-19 19:05:46 +0200122 val = hisi_thermal_step_to_temp(val);
kongxinwei9a5238a2015-05-20 19:16:37 +0800123
124 mutex_unlock(&data->thermal_lock);
125
126 return val;
127}
128
129static void hisi_thermal_enable_bind_irq_sensor
130 (struct hisi_thermal_data *data)
131{
132 struct hisi_thermal_sensor *sensor;
133
134 mutex_lock(&data->thermal_lock);
135
136 sensor = &data->sensors[data->irq_bind_sensor];
137
138 /* setting the hdak time */
139 writel(0x0, data->regs + TEMP0_CFG);
140
141 /* disable module firstly */
142 writel(0x0, data->regs + TEMP0_RST_MSK);
143 writel(0x0, data->regs + TEMP0_EN);
144
145 /* select sensor id */
146 writel((sensor->id << 12), data->regs + TEMP0_CFG);
147
148 /* enable for interrupt */
Daniel Lezcano1b2c46a2017-10-19 19:05:46 +0200149 writel(hisi_thermal_temp_to_step(sensor->thres_temp) | 0x0FFFFFF00,
kongxinwei9a5238a2015-05-20 19:16:37 +0800150 data->regs + TEMP0_TH);
151
Daniel Lezcano1b2c46a2017-10-19 19:05:46 +0200152 writel(hisi_thermal_temp_to_step(HISI_TEMP_RESET),
153 data->regs + TEMP0_RST_TH);
kongxinwei9a5238a2015-05-20 19:16:37 +0800154
155 /* enable module */
156 writel(0x1, data->regs + TEMP0_RST_MSK);
157 writel(0x1, data->regs + TEMP0_EN);
158
159 writel(0x0, data->regs + TEMP0_INT_CLR);
160 writel(0x1, data->regs + TEMP0_INT_EN);
161
162 usleep_range(3000, 5000);
163
164 mutex_unlock(&data->thermal_lock);
165}
166
167static void hisi_thermal_disable_sensor(struct hisi_thermal_data *data)
168{
169 mutex_lock(&data->thermal_lock);
170
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);
175
176 mutex_unlock(&data->thermal_lock);
177}
178
Sascha Hauer17e83512015-07-24 08:12:54 +0200179static int hisi_thermal_get_temp(void *_sensor, int *temp)
kongxinwei9a5238a2015-05-20 19:16:37 +0800180{
181 struct hisi_thermal_sensor *sensor = _sensor;
182 struct hisi_thermal_data *data = sensor->thermal;
183
Leo Yan439dc962016-03-29 19:27:12 +0800184 int sensor_id = -1, i;
kongxinwei9a5238a2015-05-20 19:16:37 +0800185 long max_temp = 0;
186
187 *temp = hisi_thermal_get_sensor_temp(data, sensor);
188
189 sensor->sensor_temp = *temp;
190
191 for (i = 0; i < HISI_MAX_SENSORS; i++) {
Leo Yan439dc962016-03-29 19:27:12 +0800192 if (!data->sensors[i].tzd)
193 continue;
194
kongxinwei9a5238a2015-05-20 19:16:37 +0800195 if (data->sensors[i].sensor_temp >= max_temp) {
196 max_temp = data->sensors[i].sensor_temp;
197 sensor_id = i;
198 }
199 }
200
Leo Yan439dc962016-03-29 19:27:12 +0800201 /* If no sensor has been enabled, then skip to enable irq */
202 if (sensor_id == -1)
203 return 0;
204
kongxinwei9a5238a2015-05-20 19:16:37 +0800205 mutex_lock(&data->thermal_lock);
206 data->irq_bind_sensor = sensor_id;
207 mutex_unlock(&data->thermal_lock);
208
Sascha Hauer17e83512015-07-24 08:12:54 +0200209 dev_dbg(&data->pdev->dev, "id=%d, irq=%d, temp=%d, thres=%d\n",
kongxinwei9a5238a2015-05-20 19:16:37 +0800210 sensor->id, data->irq_enabled, *temp, sensor->thres_temp);
211 /*
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;
215 */
216 if (data->irq_enabled) {
217 hisi_thermal_enable_bind_irq_sensor(data);
218 return 0;
219 }
220
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);
225 }
226
227 return 0;
228}
229
230static struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
231 .get_temp = hisi_thermal_get_temp,
232};
233
234static irqreturn_t hisi_thermal_alarm_irq(int irq, void *dev)
235{
236 struct hisi_thermal_data *data = dev;
237
238 disable_irq_nosync(irq);
239 data->irq_enabled = false;
240
241 return IRQ_WAKE_THREAD;
242}
243
244static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
245{
246 struct hisi_thermal_data *data = dev;
247 struct hisi_thermal_sensor *sensor;
248 int i;
249
250 mutex_lock(&data->thermal_lock);
251 sensor = &data->sensors[data->irq_bind_sensor];
252
253 dev_crit(&data->pdev->dev, "THERMAL ALARM: T > %d\n",
Daniel Lezcano3cff9072017-10-19 19:05:47 +0200254 sensor->thres_temp);
kongxinwei9a5238a2015-05-20 19:16:37 +0800255 mutex_unlock(&data->thermal_lock);
256
Leo Yan439dc962016-03-29 19:27:12 +0800257 for (i = 0; i < HISI_MAX_SENSORS; i++) {
258 if (!data->sensors[i].tzd)
259 continue;
260
Srinivas Pandruvada0e70f462016-08-26 16:21:16 -0700261 thermal_zone_device_update(data->sensors[i].tzd,
262 THERMAL_EVENT_UNSPECIFIED);
Leo Yan439dc962016-03-29 19:27:12 +0800263 }
kongxinwei9a5238a2015-05-20 19:16:37 +0800264
265 return IRQ_HANDLED;
266}
267
268static int hisi_thermal_register_sensor(struct platform_device *pdev,
269 struct hisi_thermal_data *data,
270 struct hisi_thermal_sensor *sensor,
271 int index)
272{
273 int ret, i;
274 const struct thermal_trip *trip;
275
276 sensor->id = index;
277 sensor->thermal = data;
278
Eduardo Valentin44a520d2016-03-09 13:07:13 -0800279 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
280 sensor->id, sensor, &hisi_of_thermal_ops);
kongxinwei9a5238a2015-05-20 19:16:37 +0800281 if (IS_ERR(sensor->tzd)) {
282 ret = PTR_ERR(sensor->tzd);
Leo Yan439dc962016-03-29 19:27:12 +0800283 sensor->tzd = NULL;
kongxinwei9a5238a2015-05-20 19:16:37 +0800284 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
285 sensor->id, ret);
286 return ret;
287 }
288
289 trip = of_thermal_get_trip_points(sensor->tzd);
290
291 for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
292 if (trip[i].type == THERMAL_TRIP_PASSIVE) {
Daniel Lezcano3cff9072017-10-19 19:05:47 +0200293 sensor->thres_temp = hisi_thermal_round_temp(trip[i].temperature);
kongxinwei9a5238a2015-05-20 19:16:37 +0800294 break;
295 }
296 }
297
298 return 0;
299}
300
301static const struct of_device_id of_hisi_thermal_match[] = {
302 { .compatible = "hisilicon,tsensor" },
303 { /* end */ }
304};
305MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
306
307static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
308 bool on)
309{
310 struct thermal_zone_device *tzd = sensor->tzd;
311
312 tzd->ops->set_mode(tzd,
313 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
314}
315
316static int hisi_thermal_probe(struct platform_device *pdev)
317{
318 struct hisi_thermal_data *data;
319 struct resource *res;
320 int i;
321 int ret;
322
323 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
324 if (!data)
325 return -ENOMEM;
326
327 mutex_init(&data->thermal_lock);
328 data->pdev = pdev;
329
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);
335 }
336
337 data->irq = platform_get_irq(pdev, 0);
338 if (data->irq < 0)
339 return data->irq;
340
kongxinwei9a5238a2015-05-20 19:16:37 +0800341 platform_set_drvdata(pdev, data);
342
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)
347 dev_err(&pdev->dev,
348 "failed to get thermal clk: %d\n", ret);
349 return ret;
350 }
351
352 /* enable clock for thermal */
353 ret = clk_prepare_enable(data->clk);
354 if (ret) {
355 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
356 return ret;
357 }
358
Leo Yan469ace02016-03-29 19:27:13 +0800359 hisi_thermal_enable_bind_irq_sensor(data);
Daniel Lezcanob679b8d2017-10-19 19:05:43 +0200360 data->irq_enabled = true;
Leo Yan469ace02016-03-29 19:27:13 +0800361
kongxinwei9a5238a2015-05-20 19:16:37 +0800362 for (i = 0; i < HISI_MAX_SENSORS; ++i) {
363 ret = hisi_thermal_register_sensor(pdev, data,
364 &data->sensors[i], i);
Leo Yan439dc962016-03-29 19:27:12 +0800365 if (ret)
kongxinwei9a5238a2015-05-20 19:16:37 +0800366 dev_err(&pdev->dev,
367 "failed to register thermal sensor: %d\n", ret);
Leo Yan439dc962016-03-29 19:27:12 +0800368 else
369 hisi_thermal_toggle_sensor(&data->sensors[i], true);
kongxinwei9a5238a2015-05-20 19:16:37 +0800370 }
371
Daniel Lezcano2dac5592017-10-19 19:05:45 +0200372 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);
376 if (ret < 0) {
377 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
378 return ret;
379 }
380
Daniel Lezcanob679b8d2017-10-19 19:05:43 +0200381 enable_irq(data->irq);
382
kongxinwei9a5238a2015-05-20 19:16:37 +0800383 return 0;
kongxinwei9a5238a2015-05-20 19:16:37 +0800384}
385
386static int hisi_thermal_remove(struct platform_device *pdev)
387{
388 struct hisi_thermal_data *data = platform_get_drvdata(pdev);
389 int i;
390
391 for (i = 0; i < HISI_MAX_SENSORS; i++) {
392 struct hisi_thermal_sensor *sensor = &data->sensors[i];
393
Leo Yan439dc962016-03-29 19:27:12 +0800394 if (!sensor->tzd)
395 continue;
396
kongxinwei9a5238a2015-05-20 19:16:37 +0800397 hisi_thermal_toggle_sensor(sensor, false);
kongxinwei9a5238a2015-05-20 19:16:37 +0800398 }
399
400 hisi_thermal_disable_sensor(data);
401 clk_disable_unprepare(data->clk);
402
403 return 0;
404}
405
406#ifdef CONFIG_PM_SLEEP
407static int hisi_thermal_suspend(struct device *dev)
408{
409 struct hisi_thermal_data *data = dev_get_drvdata(dev);
410
411 hisi_thermal_disable_sensor(data);
412 data->irq_enabled = false;
413
414 clk_disable_unprepare(data->clk);
415
416 return 0;
417}
418
419static int hisi_thermal_resume(struct device *dev)
420{
421 struct hisi_thermal_data *data = dev_get_drvdata(dev);
Arvind Yadav82bf76a2017-06-06 15:04:46 +0530422 int ret;
kongxinwei9a5238a2015-05-20 19:16:37 +0800423
Arvind Yadav82bf76a2017-06-06 15:04:46 +0530424 ret = clk_prepare_enable(data->clk);
425 if (ret)
426 return ret;
kongxinwei9a5238a2015-05-20 19:16:37 +0800427
428 data->irq_enabled = true;
429 hisi_thermal_enable_bind_irq_sensor(data);
430
431 return 0;
432}
433#endif
434
435static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
436 hisi_thermal_suspend, hisi_thermal_resume);
437
438static struct platform_driver hisi_thermal_driver = {
439 .driver = {
440 .name = "hisi_thermal",
kongxinwei9a5238a2015-05-20 19:16:37 +0800441 .pm = &hisi_thermal_pm_ops,
442 .of_match_table = of_hisi_thermal_match,
443 },
444 .probe = hisi_thermal_probe,
445 .remove = hisi_thermal_remove,
446};
447
448module_platform_driver(hisi_thermal_driver);
449
450MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
451MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
452MODULE_DESCRIPTION("Hisilicon thermal driver");
453MODULE_LICENSE("GPL v2");