blob: a0b7e26e4796f0e2d79310795ab7093963fd5122 [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
Daniel Lezcano1e11b012017-10-19 19:05:49 +020029#define TEMP0_LAG (0x0)
kongxinwei9a5238a2015-05-20 19:16:37 +080030#define TEMP0_TH (0x4)
31#define TEMP0_RST_TH (0x8)
32#define TEMP0_CFG (0xC)
Daniel Lezcanob4243152017-10-19 19:05:50 +020033#define TEMP0_CFG_SS_MSK (0xF000)
34#define TEMP0_CFG_HDAK_MSK (0x30)
kongxinwei9a5238a2015-05-20 19:16:37 +080035#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)
40
Daniel Lezcano48880b92017-10-19 19:05:46 +020041#define HISI_TEMP_BASE (-60000)
kongxinwei9a5238a2015-05-20 19:16:37 +080042#define HISI_TEMP_RESET (100000)
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +020043#define HISI_TEMP_STEP (785)
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +020044#define HISI_TEMP_LAG (3500)
kongxinwei9a5238a2015-05-20 19:16:37 +080045
46#define HISI_MAX_SENSORS 4
Daniel Lezcanoff4ec292017-10-19 19:05:44 +020047#define HISI_DEFAULT_SENSOR 2
kongxinwei9a5238a2015-05-20 19:16:37 +080048
49struct hisi_thermal_sensor {
kongxinwei9a5238a2015-05-20 19:16:37 +080050 struct thermal_zone_device *tzd;
kongxinwei9a5238a2015-05-20 19:16:37 +080051 uint32_t id;
52 uint32_t thres_temp;
53};
54
55struct hisi_thermal_data {
kongxinwei9a5238a2015-05-20 19:16:37 +080056 struct platform_device *pdev;
57 struct clk *clk;
Daniel Lezcano609f26d2017-10-19 19:05:52 +020058 struct hisi_thermal_sensor sensor;
kongxinwei9a5238a2015-05-20 19:16:37 +080059 void __iomem *regs;
Daniel Lezcano609f26d2017-10-19 19:05:52 +020060 int irq;
kongxinwei9a5238a2015-05-20 19:16:37 +080061};
62
Daniel Lezcano48880b92017-10-19 19:05:46 +020063/*
64 * The temperature computation on the tsensor is as follow:
65 * Unit: millidegree Celsius
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +020066 * Step: 200/255 (0.7843)
Daniel Lezcano48880b92017-10-19 19:05:46 +020067 * Temperature base: -60°C
68 *
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +020069 * The register is programmed in temperature steps, every step is 785
Daniel Lezcano48880b92017-10-19 19:05:46 +020070 * millidegree and begins at -60 000 m°C
71 *
72 * The temperature from the steps:
73 *
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +020074 * Temp = TempBase + (steps x 785)
Daniel Lezcano48880b92017-10-19 19:05:46 +020075 *
76 * and the steps from the temperature:
77 *
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +020078 * steps = (Temp - TempBase) / 785
Daniel Lezcano48880b92017-10-19 19:05:46 +020079 *
80 */
81static inline int hisi_thermal_step_to_temp(int step)
kongxinwei9a5238a2015-05-20 19:16:37 +080082{
Daniel Lezcano48880b92017-10-19 19:05:46 +020083 return HISI_TEMP_BASE + (step * HISI_TEMP_STEP);
kongxinwei9a5238a2015-05-20 19:16:37 +080084}
85
Daniel Lezcanobc02ef62017-10-19 19:05:53 +020086static inline int hisi_thermal_temp_to_step(int temp)
kongxinwei9a5238a2015-05-20 19:16:37 +080087{
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +020088 return DIV_ROUND_UP(temp - HISI_TEMP_BASE, HISI_TEMP_STEP);
Daniel Lezcanodb2b0332017-10-19 19:05:47 +020089}
90
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +020091/*
92 * The lag register contains 5 bits encoding the temperature in steps.
93 *
94 * Each time the temperature crosses the threshold boundary, an
95 * interrupt is raised. It could be when the temperature is going
96 * above the threshold or below. However, if the temperature is
97 * fluctuating around this value due to the load, we can receive
98 * several interrupts which may not desired.
99 *
100 * We can setup a temperature representing the delta between the
101 * threshold and the current temperature when the temperature is
102 * decreasing.
103 *
104 * For instance: the lag register is 5°C, the threshold is 65°C, when
105 * the temperature reaches 65°C an interrupt is raised and when the
106 * temperature decrease to 65°C - 5°C another interrupt is raised.
107 *
108 * A very short lag can lead to an interrupt storm, a long lag
109 * increase the latency to react to the temperature changes. In our
110 * case, that is not really a problem as we are polling the
111 * temperature.
112 *
113 * [0:4] : lag register
114 *
115 * The temperature is coded in steps, cf. HISI_TEMP_STEP.
116 *
117 * Min : 0x00 : 0.0 °C
118 * Max : 0x1F : 24.3 °C
119 *
120 * The 'value' parameter is in milliCelsius.
121 */
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200122static inline void hisi_thermal_set_lag(void __iomem *addr, int value)
123{
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +0200124 writel(DIV_ROUND_UP(value, HISI_TEMP_STEP) & 0x1F, addr + TEMP0_LAG);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200125}
126
127static inline void hisi_thermal_alarm_clear(void __iomem *addr, int value)
128{
129 writel(value, addr + TEMP0_INT_CLR);
130}
131
132static inline void hisi_thermal_alarm_enable(void __iomem *addr, int value)
133{
134 writel(value, addr + TEMP0_INT_EN);
135}
136
137static inline void hisi_thermal_alarm_set(void __iomem *addr, int temp)
138{
139 writel(hisi_thermal_temp_to_step(temp) | 0x0FFFFFF00, addr + TEMP0_TH);
140}
141
142static inline void hisi_thermal_reset_set(void __iomem *addr, int temp)
143{
144 writel(hisi_thermal_temp_to_step(temp), addr + TEMP0_RST_TH);
145}
146
147static inline void hisi_thermal_reset_enable(void __iomem *addr, int value)
148{
149 writel(value, addr + TEMP0_RST_MSK);
150}
151
152static inline void hisi_thermal_enable(void __iomem *addr, int value)
153{
154 writel(value, addr + TEMP0_EN);
155}
156
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200157static inline int hisi_thermal_get_temperature(void __iomem *addr)
158{
159 return hisi_thermal_step_to_temp(readl(addr + TEMP0_VALUE));
160}
161
Daniel Lezcanob4243152017-10-19 19:05:50 +0200162/*
163 * Temperature configuration register - Sensor selection
164 *
165 * Bits [19:12]
166 *
167 * 0x0: local sensor (default)
168 * 0x1: remote sensor 1 (ACPU cluster 1)
169 * 0x2: remote sensor 2 (ACPU cluster 0)
170 * 0x3: remote sensor 3 (G3D)
171 */
172static inline void hisi_thermal_sensor_select(void __iomem *addr, int sensor)
173{
174 writel((readl(addr + TEMP0_CFG) & ~TEMP0_CFG_SS_MSK) |
175 (sensor << 12), addr + TEMP0_CFG);
176}
177
178/*
179 * Temperature configuration register - Hdak conversion polling interval
180 *
181 * Bits [5:4]
182 *
183 * 0x0 : 0.768 ms
184 * 0x1 : 6.144 ms
185 * 0x2 : 49.152 ms
186 * 0x3 : 393.216 ms
187 */
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200188static inline void hisi_thermal_hdak_set(void __iomem *addr, int value)
189{
Daniel Lezcanob4243152017-10-19 19:05:50 +0200190 writel((readl(addr + TEMP0_CFG) & ~TEMP0_CFG_HDAK_MSK) |
191 (value << 4), addr + TEMP0_CFG);
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200192}
193
kongxinwei9a5238a2015-05-20 19:16:37 +0800194static void hisi_thermal_disable_sensor(struct hisi_thermal_data *data)
195{
kongxinwei9a5238a2015-05-20 19:16:37 +0800196 /* disable sensor module */
Daniel Lezcano1e11b012017-10-19 19:05:49 +0200197 hisi_thermal_enable(data->regs, 0);
198 hisi_thermal_alarm_enable(data->regs, 0);
199 hisi_thermal_reset_enable(data->regs, 0);
Kevin Wangtao943c0f62017-10-19 19:05:56 +0200200
201 clk_disable_unprepare(data->clk);
kongxinwei9a5238a2015-05-20 19:16:37 +0800202}
203
Kevin Wangtaoa0678da82017-10-22 10:54:32 +0200204static int hisi_thermal_setup(struct hisi_thermal_data *data)
205{
206 struct hisi_thermal_sensor *sensor = &data->sensor;
207 int ret;
208
209 /* enable clock for tsensor */
210 ret = clk_prepare_enable(data->clk);
211 if (ret)
212 return ret;
213
214 /* disable module firstly */
215 hisi_thermal_reset_enable(data->regs, 0);
216 hisi_thermal_enable(data->regs, 0);
217
218 /* select sensor id */
219 hisi_thermal_sensor_select(data->regs, sensor->id);
220
221 /* setting the hdak time */
222 hisi_thermal_hdak_set(data->regs, 0);
223
224 /* setting lag value between current temp and the threshold */
225 hisi_thermal_set_lag(data->regs, HISI_TEMP_LAG);
226
227 /* enable for interrupt */
228 hisi_thermal_alarm_set(data->regs, sensor->thres_temp);
229
230 hisi_thermal_reset_set(data->regs, HISI_TEMP_RESET);
231
232 /* enable module */
233 hisi_thermal_reset_enable(data->regs, 1);
234 hisi_thermal_enable(data->regs, 1);
235
236 hisi_thermal_alarm_clear(data->regs, 0);
237 hisi_thermal_alarm_enable(data->regs, 1);
238
239 return 0;
240}
241
Daniel Lezcano81d7cb72017-10-19 19:05:54 +0200242static int hisi_thermal_get_temp(void *__data, int *temp)
kongxinwei9a5238a2015-05-20 19:16:37 +0800243{
Daniel Lezcano81d7cb72017-10-19 19:05:54 +0200244 struct hisi_thermal_data *data = __data;
245 struct hisi_thermal_sensor *sensor = &data->sensor;
kongxinwei9a5238a2015-05-20 19:16:37 +0800246
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200247 *temp = hisi_thermal_get_temperature(data->regs);
kongxinwei9a5238a2015-05-20 19:16:37 +0800248
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200249 dev_dbg(&data->pdev->dev, "id=%d, temp=%d, thres=%d\n",
250 sensor->id, *temp, sensor->thres_temp);
kongxinwei9a5238a2015-05-20 19:16:37 +0800251
252 return 0;
253}
254
Julia Lawall3fe156f2017-08-08 17:08:55 +0200255static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
kongxinwei9a5238a2015-05-20 19:16:37 +0800256 .get_temp = hisi_thermal_get_temp,
257};
258
kongxinwei9a5238a2015-05-20 19:16:37 +0800259static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
260{
261 struct hisi_thermal_data *data = dev;
Daniel Lezcano609f26d2017-10-19 19:05:52 +0200262 struct hisi_thermal_sensor *sensor = &data->sensor;
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200263 int temp;
kongxinwei9a5238a2015-05-20 19:16:37 +0800264
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200265 hisi_thermal_alarm_clear(data->regs, 1);
kongxinwei9a5238a2015-05-20 19:16:37 +0800266
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200267 temp = hisi_thermal_get_temperature(data->regs);
268
269 if (temp >= sensor->thres_temp) {
270 dev_crit(&data->pdev->dev, "THERMAL ALARM: %d > %d\n",
271 temp, sensor->thres_temp);
272
Daniel Lezcano609f26d2017-10-19 19:05:52 +0200273 thermal_zone_device_update(data->sensor.tzd,
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200274 THERMAL_EVENT_UNSPECIFIED);
275
276 } else if (temp < sensor->thres_temp) {
277 dev_crit(&data->pdev->dev, "THERMAL ALARM stopped: %d < %d\n",
278 temp, sensor->thres_temp);
279 }
kongxinwei9a5238a2015-05-20 19:16:37 +0800280
281 return IRQ_HANDLED;
282}
283
284static int hisi_thermal_register_sensor(struct platform_device *pdev,
285 struct hisi_thermal_data *data,
286 struct hisi_thermal_sensor *sensor,
287 int index)
288{
289 int ret, i;
290 const struct thermal_trip *trip;
291
292 sensor->id = index;
kongxinwei9a5238a2015-05-20 19:16:37 +0800293
Eduardo Valentin44a520d2016-03-09 13:07:13 -0800294 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
Daniel Lezcano81d7cb72017-10-19 19:05:54 +0200295 sensor->id, data,
296 &hisi_of_thermal_ops);
kongxinwei9a5238a2015-05-20 19:16:37 +0800297 if (IS_ERR(sensor->tzd)) {
298 ret = PTR_ERR(sensor->tzd);
Leo Yan439dc962016-03-29 19:27:12 +0800299 sensor->tzd = NULL;
kongxinwei9a5238a2015-05-20 19:16:37 +0800300 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
301 sensor->id, ret);
302 return ret;
303 }
304
305 trip = of_thermal_get_trip_points(sensor->tzd);
306
307 for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
308 if (trip[i].type == THERMAL_TRIP_PASSIVE) {
Kevin Wangtaoe42bbe12017-10-19 19:05:57 +0200309 sensor->thres_temp = trip[i].temperature;
kongxinwei9a5238a2015-05-20 19:16:37 +0800310 break;
311 }
312 }
313
314 return 0;
315}
316
317static const struct of_device_id of_hisi_thermal_match[] = {
318 { .compatible = "hisilicon,tsensor" },
319 { /* end */ }
320};
321MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
322
323static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
324 bool on)
325{
326 struct thermal_zone_device *tzd = sensor->tzd;
327
328 tzd->ops->set_mode(tzd,
329 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
330}
331
332static int hisi_thermal_probe(struct platform_device *pdev)
333{
334 struct hisi_thermal_data *data;
335 struct resource *res;
kongxinwei9a5238a2015-05-20 19:16:37 +0800336 int ret;
337
338 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
339 if (!data)
340 return -ENOMEM;
341
kongxinwei9a5238a2015-05-20 19:16:37 +0800342 data->pdev = pdev;
343
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);
349 }
350
351 data->irq = platform_get_irq(pdev, 0);
352 if (data->irq < 0)
353 return data->irq;
354
kongxinwei9a5238a2015-05-20 19:16:37 +0800355 platform_set_drvdata(pdev, data);
356
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)
361 dev_err(&pdev->dev,
362 "failed to get thermal clk: %d\n", ret);
363 return ret;
364 }
365
Daniel Lezcanoff4ec292017-10-19 19:05:44 +0200366 ret = hisi_thermal_register_sensor(pdev, data,
Daniel Lezcano609f26d2017-10-19 19:05:52 +0200367 &data->sensor,
Daniel Lezcanoff4ec292017-10-19 19:05:44 +0200368 HISI_DEFAULT_SENSOR);
369 if (ret) {
370 dev_err(&pdev->dev, "failed to register thermal sensor: %d\n",
371 ret);
372 return ret;
kongxinwei9a5238a2015-05-20 19:16:37 +0800373 }
374
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200375 ret = hisi_thermal_setup(data);
376 if (ret) {
377 dev_err(&pdev->dev, "Failed to setup the sensor: %d\n", ret);
378 return ret;
379 }
Daniel Lezcanoff4ec292017-10-19 19:05:44 +0200380
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200381 ret = devm_request_threaded_irq(&pdev->dev, data->irq, NULL,
Daniel Lezcano2cb4de72017-10-19 19:05:45 +0200382 hisi_thermal_alarm_irq_thread,
Daniel Lezcano10d7e9a2017-10-19 19:05:51 +0200383 IRQF_ONESHOT, "hisi_thermal", data);
Daniel Lezcano2cb4de72017-10-19 19:05:45 +0200384 if (ret < 0) {
385 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
386 return ret;
387 }
388
Daniel Lezcano609f26d2017-10-19 19:05:52 +0200389 hisi_thermal_toggle_sensor(&data->sensor, true);
Daniel Lezcanoc176b102017-10-19 19:05:43 +0200390
kongxinwei9a5238a2015-05-20 19:16:37 +0800391 return 0;
kongxinwei9a5238a2015-05-20 19:16:37 +0800392}
393
394static int hisi_thermal_remove(struct platform_device *pdev)
395{
396 struct hisi_thermal_data *data = platform_get_drvdata(pdev);
Daniel Lezcano609f26d2017-10-19 19:05:52 +0200397 struct hisi_thermal_sensor *sensor = &data->sensor;
kongxinwei9a5238a2015-05-20 19:16:37 +0800398
Daniel Lezcanoff4ec292017-10-19 19:05:44 +0200399 hisi_thermal_toggle_sensor(sensor, false);
kongxinwei9a5238a2015-05-20 19:16:37 +0800400 hisi_thermal_disable_sensor(data);
kongxinwei9a5238a2015-05-20 19:16:37 +0800401
402 return 0;
403}
404
405#ifdef CONFIG_PM_SLEEP
406static int hisi_thermal_suspend(struct device *dev)
407{
408 struct hisi_thermal_data *data = dev_get_drvdata(dev);
409
410 hisi_thermal_disable_sensor(data);
kongxinwei9a5238a2015-05-20 19:16:37 +0800411
kongxinwei9a5238a2015-05-20 19:16:37 +0800412 return 0;
413}
414
415static int hisi_thermal_resume(struct device *dev)
416{
417 struct hisi_thermal_data *data = dev_get_drvdata(dev);
418
Kevin Wangtao943c0f62017-10-19 19:05:56 +0200419 return hisi_thermal_setup(data);
kongxinwei9a5238a2015-05-20 19:16:37 +0800420}
421#endif
422
423static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
424 hisi_thermal_suspend, hisi_thermal_resume);
425
426static struct platform_driver hisi_thermal_driver = {
427 .driver = {
428 .name = "hisi_thermal",
kongxinwei9a5238a2015-05-20 19:16:37 +0800429 .pm = &hisi_thermal_pm_ops,
430 .of_match_table = of_hisi_thermal_match,
431 },
432 .probe = hisi_thermal_probe,
433 .remove = hisi_thermal_remove,
434};
435
436module_platform_driver(hisi_thermal_driver);
437
438MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
439MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
440MODULE_DESCRIPTION("Hisilicon thermal driver");
441MODULE_LICENSE("GPL v2");