]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/thermal/hisi_thermal.c
thermal/drivers/hisi: Fix kernel panic on alarm interrupt
[mirror_ubuntu-jammy-kernel.git] / drivers / thermal / hisi_thermal.c
CommitLineData
9a5238a9 1/*
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
38#define HISI_TEMP_BASE (-60)
39#define HISI_TEMP_RESET (100000)
40
41#define HISI_MAX_SENSORS 4
ff4ec299 42#define HISI_DEFAULT_SENSOR 2
9a5238a9 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;
ff4ec299
DL
57 struct hisi_thermal_sensor sensors;
58 int irq;
9a5238a9 59 bool irq_enabled;
60
61 void __iomem *regs;
62};
63
64/* in millicelsius */
65static inline int _step_to_temp(int step)
66{
67 /*
68 * Every step equals (1 * 200) / 255 celsius, and finally
69 * need convert to millicelsius.
70 */
5fdfc48b 71 return (HISI_TEMP_BASE * 1000 + (step * 200000 / 255));
9a5238a9 72}
73
74static inline long _temp_to_step(long temp)
75{
5fdfc48b 76 return ((temp - HISI_TEMP_BASE * 1000) * 255) / 200000;
9a5238a9 77}
78
79static long hisi_thermal_get_sensor_temp(struct hisi_thermal_data *data,
80 struct hisi_thermal_sensor *sensor)
81{
82 long val;
83
84 mutex_lock(&data->thermal_lock);
85
86 /* disable interrupt */
87 writel(0x0, data->regs + TEMP0_INT_EN);
88 writel(0x1, data->regs + TEMP0_INT_CLR);
89
90 /* disable module firstly */
91 writel(0x0, data->regs + TEMP0_EN);
92
93 /* select sensor id */
94 writel((sensor->id << 12), data->regs + TEMP0_CFG);
95
96 /* enable module */
97 writel(0x1, data->regs + TEMP0_EN);
98
99 usleep_range(3000, 5000);
100
101 val = readl(data->regs + TEMP0_VALUE);
102 val = _step_to_temp(val);
103
104 mutex_unlock(&data->thermal_lock);
105
106 return val;
107}
108
109static void hisi_thermal_enable_bind_irq_sensor
110 (struct hisi_thermal_data *data)
111{
112 struct hisi_thermal_sensor *sensor;
113
114 mutex_lock(&data->thermal_lock);
115
ff4ec299 116 sensor = &data->sensors;
9a5238a9 117
118 /* setting the hdak time */
119 writel(0x0, data->regs + TEMP0_CFG);
120
121 /* disable module firstly */
122 writel(0x0, data->regs + TEMP0_RST_MSK);
123 writel(0x0, data->regs + TEMP0_EN);
124
125 /* select sensor id */
126 writel((sensor->id << 12), data->regs + TEMP0_CFG);
127
128 /* enable for interrupt */
129 writel(_temp_to_step(sensor->thres_temp) | 0x0FFFFFF00,
130 data->regs + TEMP0_TH);
131
132 writel(_temp_to_step(HISI_TEMP_RESET), data->regs + TEMP0_RST_TH);
133
134 /* enable module */
135 writel(0x1, data->regs + TEMP0_RST_MSK);
136 writel(0x1, data->regs + TEMP0_EN);
137
138 writel(0x0, data->regs + TEMP0_INT_CLR);
139 writel(0x1, data->regs + TEMP0_INT_EN);
140
141 usleep_range(3000, 5000);
142
143 mutex_unlock(&data->thermal_lock);
144}
145
146static void hisi_thermal_disable_sensor(struct hisi_thermal_data *data)
147{
148 mutex_lock(&data->thermal_lock);
149
150 /* disable sensor module */
151 writel(0x0, data->regs + TEMP0_INT_EN);
152 writel(0x0, data->regs + TEMP0_RST_MSK);
153 writel(0x0, data->regs + TEMP0_EN);
154
155 mutex_unlock(&data->thermal_lock);
156}
157
17e8351a 158static int hisi_thermal_get_temp(void *_sensor, int *temp)
9a5238a9 159{
160 struct hisi_thermal_sensor *sensor = _sensor;
161 struct hisi_thermal_data *data = sensor->thermal;
162
9a5238a9 163 *temp = hisi_thermal_get_sensor_temp(data, sensor);
164
17e8351a 165 dev_dbg(&data->pdev->dev, "id=%d, irq=%d, temp=%d, thres=%d\n",
9a5238a9 166 sensor->id, data->irq_enabled, *temp, sensor->thres_temp);
167 /*
168 * Bind irq to sensor for two cases:
169 * Reenable alarm IRQ if temperature below threshold;
170 * if irq has been enabled, always set it;
171 */
172 if (data->irq_enabled) {
173 hisi_thermal_enable_bind_irq_sensor(data);
174 return 0;
175 }
176
ff4ec299 177 if (*temp < sensor->thres_temp) {
9a5238a9 178 data->irq_enabled = true;
179 hisi_thermal_enable_bind_irq_sensor(data);
180 enable_irq(data->irq);
181 }
182
183 return 0;
184}
185
3fe156f1 186static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
9a5238a9 187 .get_temp = hisi_thermal_get_temp,
188};
189
190static irqreturn_t hisi_thermal_alarm_irq(int irq, void *dev)
191{
192 struct hisi_thermal_data *data = dev;
193
194 disable_irq_nosync(irq);
195 data->irq_enabled = false;
196
197 return IRQ_WAKE_THREAD;
198}
199
200static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
201{
202 struct hisi_thermal_data *data = dev;
203 struct hisi_thermal_sensor *sensor;
9a5238a9 204
205 mutex_lock(&data->thermal_lock);
ff4ec299 206 sensor = &data->sensors;
9a5238a9 207
208 dev_crit(&data->pdev->dev, "THERMAL ALARM: T > %d\n",
209 sensor->thres_temp / 1000);
210 mutex_unlock(&data->thermal_lock);
211
ff4ec299
DL
212 thermal_zone_device_update(data->sensors.tzd,
213 THERMAL_EVENT_UNSPECIFIED);
9a5238a9 214
215 return IRQ_HANDLED;
216}
217
218static int hisi_thermal_register_sensor(struct platform_device *pdev,
219 struct hisi_thermal_data *data,
220 struct hisi_thermal_sensor *sensor,
221 int index)
222{
223 int ret, i;
224 const struct thermal_trip *trip;
225
226 sensor->id = index;
227 sensor->thermal = data;
228
44a520d8
EV
229 sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
230 sensor->id, sensor, &hisi_of_thermal_ops);
9a5238a9 231 if (IS_ERR(sensor->tzd)) {
232 ret = PTR_ERR(sensor->tzd);
439dc968 233 sensor->tzd = NULL;
9a5238a9 234 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
235 sensor->id, ret);
236 return ret;
237 }
238
239 trip = of_thermal_get_trip_points(sensor->tzd);
240
241 for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
242 if (trip[i].type == THERMAL_TRIP_PASSIVE) {
243 sensor->thres_temp = trip[i].temperature;
244 break;
245 }
246 }
247
248 return 0;
249}
250
251static const struct of_device_id of_hisi_thermal_match[] = {
252 { .compatible = "hisilicon,tsensor" },
253 { /* end */ }
254};
255MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
256
257static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
258 bool on)
259{
260 struct thermal_zone_device *tzd = sensor->tzd;
261
262 tzd->ops->set_mode(tzd,
263 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
264}
265
266static int hisi_thermal_probe(struct platform_device *pdev)
267{
268 struct hisi_thermal_data *data;
269 struct resource *res;
9a5238a9 270 int ret;
271
272 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
273 if (!data)
274 return -ENOMEM;
275
276 mutex_init(&data->thermal_lock);
277 data->pdev = pdev;
278
279 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
280 data->regs = devm_ioremap_resource(&pdev->dev, res);
281 if (IS_ERR(data->regs)) {
282 dev_err(&pdev->dev, "failed to get io address\n");
283 return PTR_ERR(data->regs);
284 }
285
286 data->irq = platform_get_irq(pdev, 0);
287 if (data->irq < 0)
288 return data->irq;
289
9a5238a9 290 platform_set_drvdata(pdev, data);
291
292 data->clk = devm_clk_get(&pdev->dev, "thermal_clk");
293 if (IS_ERR(data->clk)) {
294 ret = PTR_ERR(data->clk);
295 if (ret != -EPROBE_DEFER)
296 dev_err(&pdev->dev,
297 "failed to get thermal clk: %d\n", ret);
298 return ret;
299 }
300
301 /* enable clock for thermal */
302 ret = clk_prepare_enable(data->clk);
303 if (ret) {
304 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
305 return ret;
306 }
307
469ace07 308 hisi_thermal_enable_bind_irq_sensor(data);
c176b10b 309 data->irq_enabled = true;
469ace07 310
ff4ec299
DL
311 ret = hisi_thermal_register_sensor(pdev, data,
312 &data->sensors,
313 HISI_DEFAULT_SENSOR);
314 if (ret) {
315 dev_err(&pdev->dev, "failed to register thermal sensor: %d\n",
316 ret);
317 return ret;
9a5238a9 318 }
319
ff4ec299
DL
320 hisi_thermal_toggle_sensor(&data->sensors, true);
321
2cb4de78
DL
322 ret = devm_request_threaded_irq(&pdev->dev, data->irq,
323 hisi_thermal_alarm_irq,
324 hisi_thermal_alarm_irq_thread,
325 0, "hisi_thermal", data);
326 if (ret < 0) {
327 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
328 return ret;
329 }
330
c176b10b
DL
331 enable_irq(data->irq);
332
9a5238a9 333 return 0;
9a5238a9 334}
335
336static int hisi_thermal_remove(struct platform_device *pdev)
337{
338 struct hisi_thermal_data *data = platform_get_drvdata(pdev);
ff4ec299 339 struct hisi_thermal_sensor *sensor = &data->sensors;
9a5238a9 340
ff4ec299 341 hisi_thermal_toggle_sensor(sensor, false);
9a5238a9 342 hisi_thermal_disable_sensor(data);
343 clk_disable_unprepare(data->clk);
344
345 return 0;
346}
347
348#ifdef CONFIG_PM_SLEEP
349static int hisi_thermal_suspend(struct device *dev)
350{
351 struct hisi_thermal_data *data = dev_get_drvdata(dev);
352
353 hisi_thermal_disable_sensor(data);
354 data->irq_enabled = false;
355
356 clk_disable_unprepare(data->clk);
357
358 return 0;
359}
360
361static int hisi_thermal_resume(struct device *dev)
362{
363 struct hisi_thermal_data *data = dev_get_drvdata(dev);
919054fd 364 int ret;
9a5238a9 365
919054fd
AY
366 ret = clk_prepare_enable(data->clk);
367 if (ret)
368 return ret;
9a5238a9 369
370 data->irq_enabled = true;
371 hisi_thermal_enable_bind_irq_sensor(data);
372
373 return 0;
374}
375#endif
376
377static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
378 hisi_thermal_suspend, hisi_thermal_resume);
379
380static struct platform_driver hisi_thermal_driver = {
381 .driver = {
382 .name = "hisi_thermal",
9a5238a9 383 .pm = &hisi_thermal_pm_ops,
384 .of_match_table = of_hisi_thermal_match,
385 },
386 .probe = hisi_thermal_probe,
387 .remove = hisi_thermal_remove,
388};
389
390module_platform_driver(hisi_thermal_driver);
391
392MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
393MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
394MODULE_DESCRIPTION("Hisilicon thermal driver");
395MODULE_LICENSE("GPL v2");