OSDN Git Service

MIPS: VDSO: Prevent use of smp_processor_id()
[android-x86/kernel.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_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                  (-60000)
39 #define HISI_TEMP_RESET                 (100000)
40 #define HISI_TEMP_STEP                  (784)
41
42 #define HISI_MAX_SENSORS                4
43
44 struct 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
53 struct 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
65 /*
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  */
83 static inline int hisi_thermal_step_to_temp(int step)
84 {
85         return HISI_TEMP_BASE + (step * HISI_TEMP_STEP);
86 }
87
88 static inline long hisi_thermal_temp_to_step(long temp)
89 {
90         return (temp - HISI_TEMP_BASE) / HISI_TEMP_STEP;
91 }
92
93 static 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
99 static 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);
122         val = hisi_thermal_step_to_temp(val);
123
124         mutex_unlock(&data->thermal_lock);
125
126         return val;
127 }
128
129 static 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 */
149         writel(hisi_thermal_temp_to_step(sensor->thres_temp) | 0x0FFFFFF00,
150                data->regs + TEMP0_TH);
151
152         writel(hisi_thermal_temp_to_step(HISI_TEMP_RESET),
153                data->regs + TEMP0_RST_TH);
154
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
167 static 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
179 static int hisi_thermal_get_temp(void *_sensor, int *temp)
180 {
181         struct hisi_thermal_sensor *sensor = _sensor;
182         struct hisi_thermal_data *data = sensor->thermal;
183
184         int sensor_id = -1, i;
185         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++) {
192                 if (!data->sensors[i].tzd)
193                         continue;
194
195                 if (data->sensors[i].sensor_temp >= max_temp) {
196                         max_temp = data->sensors[i].sensor_temp;
197                         sensor_id = i;
198                 }
199         }
200
201         /* If no sensor has been enabled, then skip to enable irq */
202         if (sensor_id == -1)
203                 return 0;
204
205         mutex_lock(&data->thermal_lock);
206         data->irq_bind_sensor = sensor_id;
207         mutex_unlock(&data->thermal_lock);
208
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);
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
230 static struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
231         .get_temp = hisi_thermal_get_temp,
232 };
233
234 static 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
244 static 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",
254                  sensor->thres_temp);
255         mutex_unlock(&data->thermal_lock);
256
257         for (i = 0; i < HISI_MAX_SENSORS; i++) {
258                 if (!data->sensors[i].tzd)
259                         continue;
260
261                 thermal_zone_device_update(data->sensors[i].tzd,
262                                            THERMAL_EVENT_UNSPECIFIED);
263         }
264
265         return IRQ_HANDLED;
266 }
267
268 static 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
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);
283                 sensor->tzd = NULL;
284                 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) {
293                         sensor->thres_temp = hisi_thermal_round_temp(trip[i].temperature);
294                         break;
295                 }
296         }
297
298         return 0;
299 }
300
301 static const struct of_device_id of_hisi_thermal_match[] = {
302         { .compatible = "hisilicon,tsensor" },
303         { /* end */ }
304 };
305 MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
306
307 static 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
316 static 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
341         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
359         hisi_thermal_enable_bind_irq_sensor(data);
360         data->irq_enabled = true;
361
362         for (i = 0; i < HISI_MAX_SENSORS; ++i) {
363                 ret = hisi_thermal_register_sensor(pdev, data,
364                                                    &data->sensors[i], i);
365                 if (ret)
366                         dev_err(&pdev->dev,
367                                 "failed to register thermal sensor: %d\n", ret);
368                 else
369                         hisi_thermal_toggle_sensor(&data->sensors[i], true);
370         }
371
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);
376         if (ret < 0) {
377                 dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
378                 return ret;
379         }
380
381         enable_irq(data->irq);
382
383         return 0;
384 }
385
386 static 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
394                 if (!sensor->tzd)
395                         continue;
396
397                 hisi_thermal_toggle_sensor(sensor, false);
398         }
399
400         hisi_thermal_disable_sensor(data);
401         clk_disable_unprepare(data->clk);
402
403         return 0;
404 }
405
406 #ifdef CONFIG_PM_SLEEP
407 static 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
419 static int hisi_thermal_resume(struct device *dev)
420 {
421         struct hisi_thermal_data *data = dev_get_drvdata(dev);
422         int ret;
423
424         ret = clk_prepare_enable(data->clk);
425         if (ret)
426                 return ret;
427
428         data->irq_enabled = true;
429         hisi_thermal_enable_bind_irq_sensor(data);
430
431         return 0;
432 }
433 #endif
434
435 static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
436                          hisi_thermal_suspend, hisi_thermal_resume);
437
438 static struct platform_driver hisi_thermal_driver = {
439         .driver = {
440                 .name           = "hisi_thermal",
441                 .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
448 module_platform_driver(hisi_thermal_driver);
449
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");