]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/thermal/hisi_thermal.c
thermal/drivers/hisi: Encapsulate register writes into helpers
[linux.git] / drivers / thermal / hisi_thermal.c
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_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)
38
39 #define HISI_TEMP_BASE                  (-60000)
40 #define HISI_TEMP_RESET                 (100000)
41 #define HISI_TEMP_STEP                  (784)
42
43 #define HISI_MAX_SENSORS                4
44 #define HISI_DEFAULT_SENSOR             2
45
46 struct hisi_thermal_sensor {
47         struct hisi_thermal_data *thermal;
48         struct thermal_zone_device *tzd;
49
50         long sensor_temp;
51         uint32_t id;
52         uint32_t thres_temp;
53 };
54
55 struct hisi_thermal_data {
56         struct mutex thermal_lock;    /* protects register data */
57         struct platform_device *pdev;
58         struct clk *clk;
59         struct hisi_thermal_sensor sensors;
60         int irq;
61         bool irq_enabled;
62
63         void __iomem *regs;
64 };
65
66 /*
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
71  *
72  * The register is programmed in temperature steps, every step is 784
73  * millidegree and begins at -60 000 m°C
74  *
75  * The temperature from the steps:
76  *
77  *      Temp = TempBase + (steps x 784)
78  *
79  * and the steps from the temperature:
80  *
81  *      steps = (Temp - TempBase) / 784
82  *
83  */
84 static inline int hisi_thermal_step_to_temp(int step)
85 {
86         return HISI_TEMP_BASE + (step * HISI_TEMP_STEP);
87 }
88
89 static inline long hisi_thermal_temp_to_step(long temp)
90 {
91         return (temp - HISI_TEMP_BASE) / HISI_TEMP_STEP;
92 }
93
94 static inline long hisi_thermal_round_temp(int temp)
95 {
96         return hisi_thermal_step_to_temp(
97                 hisi_thermal_temp_to_step(temp));
98 }
99
100 static inline void hisi_thermal_set_lag(void __iomem *addr, int value)
101 {
102         writel(value, addr + TEMP0_LAG);
103 }
104
105 static inline void hisi_thermal_alarm_clear(void __iomem *addr, int value)
106 {
107         writel(value, addr + TEMP0_INT_CLR);
108 }
109
110 static inline void hisi_thermal_alarm_enable(void __iomem *addr, int value)
111 {
112         writel(value, addr + TEMP0_INT_EN);
113 }
114
115 static inline void hisi_thermal_alarm_set(void __iomem *addr, int temp)
116 {
117         writel(hisi_thermal_temp_to_step(temp) | 0x0FFFFFF00, addr + TEMP0_TH);
118 }
119
120 static inline void hisi_thermal_reset_set(void __iomem *addr, int temp)
121 {
122         writel(hisi_thermal_temp_to_step(temp), addr + TEMP0_RST_TH);
123 }
124
125 static inline void hisi_thermal_reset_enable(void __iomem *addr, int value)
126 {
127         writel(value, addr + TEMP0_RST_MSK);
128 }
129
130 static inline void hisi_thermal_enable(void __iomem *addr, int value)
131 {
132         writel(value, addr + TEMP0_EN);
133 }
134
135 static inline void hisi_thermal_sensor_select(void __iomem *addr, int sensor)
136 {
137         writel((sensor << 12), addr + TEMP0_CFG);
138 }
139
140 static inline int hisi_thermal_get_temperature(void __iomem *addr)
141 {
142         return hisi_thermal_step_to_temp(readl(addr + TEMP0_VALUE));
143 }
144
145 static inline void hisi_thermal_hdak_set(void __iomem *addr, int value)
146 {
147         writel(value, addr + TEMP0_CFG);
148 }
149
150 static long hisi_thermal_get_sensor_temp(struct hisi_thermal_data *data,
151                                          struct hisi_thermal_sensor *sensor)
152 {
153         long val;
154
155         mutex_lock(&data->thermal_lock);
156
157         /* disable interrupt */
158         hisi_thermal_alarm_enable(data->regs, 0);
159         hisi_thermal_alarm_clear(data->regs, 1);
160
161         /* disable module firstly */
162         hisi_thermal_enable(data->regs, 0);
163
164         /* select sensor id */
165         hisi_thermal_sensor_select(data->regs, sensor->id);
166
167         /* enable module */
168         hisi_thermal_enable(data->regs, 1);
169
170         usleep_range(3000, 5000);
171
172         val = hisi_thermal_get_temperature(data->regs);
173
174         mutex_unlock(&data->thermal_lock);
175
176         return val;
177 }
178
179 static void hisi_thermal_enable_bind_irq_sensor
180                         (struct hisi_thermal_data *data)
181 {
182         struct hisi_thermal_sensor *sensor;
183
184         mutex_lock(&data->thermal_lock);
185
186         sensor = &data->sensors;
187
188         /* setting the hdak time */
189         hisi_thermal_hdak_set(data->regs, 0);
190
191         /* disable module firstly */
192         hisi_thermal_reset_enable(data->regs, 0);
193         hisi_thermal_enable(data->regs, 0);
194
195         /* select sensor id */
196         hisi_thermal_sensor_select(data->regs, sensor->id);
197
198         /* enable for interrupt */
199         hisi_thermal_alarm_set(data->regs, sensor->thres_temp);
200
201         hisi_thermal_reset_set(data->regs, HISI_TEMP_RESET);
202
203         /* enable module */
204         hisi_thermal_reset_enable(data->regs, 1);
205         hisi_thermal_enable(data->regs, 1);
206
207         hisi_thermal_alarm_clear(data->regs, 0);
208         hisi_thermal_alarm_enable(data->regs, 1);
209
210         usleep_range(3000, 5000);
211
212         mutex_unlock(&data->thermal_lock);
213 }
214
215 static void hisi_thermal_disable_sensor(struct hisi_thermal_data *data)
216 {
217         mutex_lock(&data->thermal_lock);
218
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);
223
224         mutex_unlock(&data->thermal_lock);
225 }
226
227 static int hisi_thermal_get_temp(void *_sensor, int *temp)
228 {
229         struct hisi_thermal_sensor *sensor = _sensor;
230         struct hisi_thermal_data *data = sensor->thermal;
231
232         *temp = hisi_thermal_get_sensor_temp(data, sensor);
233
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);
236         /*
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;
240          */
241         if (data->irq_enabled) {
242                 hisi_thermal_enable_bind_irq_sensor(data);
243                 return 0;
244         }
245
246         if (*temp < sensor->thres_temp) {
247                 data->irq_enabled = true;
248                 hisi_thermal_enable_bind_irq_sensor(data);
249                 enable_irq(data->irq);
250         }
251
252         return 0;
253 }
254
255 static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
256         .get_temp = hisi_thermal_get_temp,
257 };
258
259 static irqreturn_t hisi_thermal_alarm_irq(int irq, void *dev)
260 {
261         struct hisi_thermal_data *data = dev;
262
263         disable_irq_nosync(irq);
264         data->irq_enabled = false;
265
266         return IRQ_WAKE_THREAD;
267 }
268
269 static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
270 {
271         struct hisi_thermal_data *data = dev;
272         struct hisi_thermal_sensor *sensor = &data->sensors;
273
274         dev_crit(&data->pdev->dev, "THERMAL ALARM: T > %d\n",
275                  sensor->thres_temp);
276
277         thermal_zone_device_update(data->sensors.tzd,
278                                    THERMAL_EVENT_UNSPECIFIED);
279
280         return IRQ_HANDLED;
281 }
282
283 static int hisi_thermal_register_sensor(struct platform_device *pdev,
284                                         struct hisi_thermal_data *data,
285                                         struct hisi_thermal_sensor *sensor,
286                                         int index)
287 {
288         int ret, i;
289         const struct thermal_trip *trip;
290
291         sensor->id = index;
292         sensor->thermal = data;
293
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);
298                 sensor->tzd = NULL;
299                 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
300                         sensor->id, ret);
301                 return ret;
302         }
303
304         trip = of_thermal_get_trip_points(sensor->tzd);
305
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);
309                         break;
310                 }
311         }
312
313         return 0;
314 }
315
316 static const struct of_device_id of_hisi_thermal_match[] = {
317         { .compatible = "hisilicon,tsensor" },
318         { /* end */ }
319 };
320 MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
321
322 static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
323                                        bool on)
324 {
325         struct thermal_zone_device *tzd = sensor->tzd;
326
327         tzd->ops->set_mode(tzd,
328                 on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
329 }
330
331 static int hisi_thermal_probe(struct platform_device *pdev)
332 {
333         struct hisi_thermal_data *data;
334         struct resource *res;
335         int ret;
336
337         data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
338         if (!data)
339                 return -ENOMEM;
340
341         mutex_init(&data->thermal_lock);
342         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
355         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
366         /* enable clock for thermal */
367         ret = clk_prepare_enable(data->clk);
368         if (ret) {
369                 dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
370                 return ret;
371         }
372
373         hisi_thermal_enable_bind_irq_sensor(data);
374         data->irq_enabled = true;
375
376         ret = hisi_thermal_register_sensor(pdev, data,
377                                            &data->sensors,
378                                            HISI_DEFAULT_SENSOR);
379         if (ret) {
380                 dev_err(&pdev->dev, "failed to register thermal sensor: %d\n",
381                         ret);
382                 return ret;
383         }
384
385         hisi_thermal_toggle_sensor(&data->sensors, true);
386
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);
391         if (ret < 0) {
392                 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
393                 return ret;
394         }
395
396         enable_irq(data->irq);
397
398         return 0;
399 }
400
401 static int hisi_thermal_remove(struct platform_device *pdev)
402 {
403         struct hisi_thermal_data *data = platform_get_drvdata(pdev);
404         struct hisi_thermal_sensor *sensor = &data->sensors;
405
406         hisi_thermal_toggle_sensor(sensor, false);
407         hisi_thermal_disable_sensor(data);
408         clk_disable_unprepare(data->clk);
409
410         return 0;
411 }
412
413 #ifdef CONFIG_PM_SLEEP
414 static int hisi_thermal_suspend(struct device *dev)
415 {
416         struct hisi_thermal_data *data = dev_get_drvdata(dev);
417
418         hisi_thermal_disable_sensor(data);
419         data->irq_enabled = false;
420
421         clk_disable_unprepare(data->clk);
422
423         return 0;
424 }
425
426 static int hisi_thermal_resume(struct device *dev)
427 {
428         struct hisi_thermal_data *data = dev_get_drvdata(dev);
429         int ret;
430
431         ret = clk_prepare_enable(data->clk);
432         if (ret)
433                 return ret;
434
435         data->irq_enabled = true;
436         hisi_thermal_enable_bind_irq_sensor(data);
437
438         return 0;
439 }
440 #endif
441
442 static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
443                          hisi_thermal_suspend, hisi_thermal_resume);
444
445 static struct platform_driver hisi_thermal_driver = {
446         .driver = {
447                 .name           = "hisi_thermal",
448                 .pm             = &hisi_thermal_pm_ops,
449                 .of_match_table = of_hisi_thermal_match,
450         },
451         .probe  = hisi_thermal_probe,
452         .remove = hisi_thermal_remove,
453 };
454
455 module_platform_driver(hisi_thermal_driver);
456
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");