OSDN Git Service

powerpc/configs/6s: Drop obsolete crypto ALGs
[tomoyo/tomoyo-test1.git] / drivers / thermal / hisi_thermal.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HiSilicon thermal sensor driver
4  *
5  * Copyright (c) 2014-2015 HiSilicon Limited.
6  * Copyright (c) 2014-2015 Linaro Limited.
7  *
8  * Xinwei Kong <kong.kongxinwei@hisilicon.com>
9  * Leo Yan <leo.yan@linaro.org>
10  */
11
12 #include <linux/cpufreq.h>
13 #include <linux/delay.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/io.h>
18 #include <linux/of_device.h>
19 #include <linux/thermal.h>
20
21 #define HI6220_TEMP0_LAG                        (0x0)
22 #define HI6220_TEMP0_TH                         (0x4)
23 #define HI6220_TEMP0_RST_TH                     (0x8)
24 #define HI6220_TEMP0_CFG                        (0xC)
25 #define HI6220_TEMP0_CFG_SS_MSK                 (0xF000)
26 #define HI6220_TEMP0_CFG_HDAK_MSK               (0x30)
27 #define HI6220_TEMP0_EN                         (0x10)
28 #define HI6220_TEMP0_INT_EN                     (0x14)
29 #define HI6220_TEMP0_INT_CLR                    (0x18)
30 #define HI6220_TEMP0_RST_MSK                    (0x1C)
31 #define HI6220_TEMP0_VALUE                      (0x28)
32
33 #define HI3660_OFFSET(chan)             ((chan) * 0x40)
34 #define HI3660_TEMP(chan)               (HI3660_OFFSET(chan) + 0x1C)
35 #define HI3660_TH(chan)                 (HI3660_OFFSET(chan) + 0x20)
36 #define HI3660_LAG(chan)                (HI3660_OFFSET(chan) + 0x28)
37 #define HI3660_INT_EN(chan)             (HI3660_OFFSET(chan) + 0x2C)
38 #define HI3660_INT_CLR(chan)            (HI3660_OFFSET(chan) + 0x30)
39
40 #define HI6220_TEMP_BASE                        (-60000)
41 #define HI6220_TEMP_RESET                       (100000)
42 #define HI6220_TEMP_STEP                        (785)
43 #define HI6220_TEMP_LAG                         (3500)
44
45 #define HI3660_TEMP_BASE                (-63780)
46 #define HI3660_TEMP_STEP                (205)
47 #define HI3660_TEMP_LAG                 (4000)
48
49 #define HI6220_CLUSTER0_SENSOR          2
50 #define HI6220_CLUSTER1_SENSOR          1
51
52 #define HI3660_LITTLE_SENSOR            0
53 #define HI3660_BIG_SENSOR               1
54 #define HI3660_G3D_SENSOR               2
55 #define HI3660_MODEM_SENSOR             3
56
57 struct hisi_thermal_data;
58
59 struct hisi_thermal_sensor {
60         struct hisi_thermal_data *data;
61         struct thermal_zone_device *tzd;
62         const char *irq_name;
63         uint32_t id;
64         uint32_t thres_temp;
65 };
66
67 struct hisi_thermal_ops {
68         int (*get_temp)(struct hisi_thermal_sensor *sensor);
69         int (*enable_sensor)(struct hisi_thermal_sensor *sensor);
70         int (*disable_sensor)(struct hisi_thermal_sensor *sensor);
71         int (*irq_handler)(struct hisi_thermal_sensor *sensor);
72         int (*probe)(struct hisi_thermal_data *data);
73 };
74
75 struct hisi_thermal_data {
76         const struct hisi_thermal_ops *ops;
77         struct hisi_thermal_sensor *sensor;
78         struct platform_device *pdev;
79         struct clk *clk;
80         void __iomem *regs;
81         int nr_sensors;
82 };
83
84 /*
85  * The temperature computation on the tsensor is as follow:
86  *      Unit: millidegree Celsius
87  *      Step: 200/255 (0.7843)
88  *      Temperature base: -60°C
89  *
90  * The register is programmed in temperature steps, every step is 785
91  * millidegree and begins at -60 000 m°C
92  *
93  * The temperature from the steps:
94  *
95  *      Temp = TempBase + (steps x 785)
96  *
97  * and the steps from the temperature:
98  *
99  *      steps = (Temp - TempBase) / 785
100  *
101  */
102 static inline int hi6220_thermal_step_to_temp(int step)
103 {
104         return HI6220_TEMP_BASE + (step * HI6220_TEMP_STEP);
105 }
106
107 static inline int hi6220_thermal_temp_to_step(int temp)
108 {
109         return DIV_ROUND_UP(temp - HI6220_TEMP_BASE, HI6220_TEMP_STEP);
110 }
111
112 /*
113  * for Hi3660,
114  *      Step: 189/922 (0.205)
115  *      Temperature base: -63.780°C
116  *
117  * The register is programmed in temperature steps, every step is 205
118  * millidegree and begins at -63 780 m°C
119  */
120 static inline int hi3660_thermal_step_to_temp(int step)
121 {
122         return HI3660_TEMP_BASE + step * HI3660_TEMP_STEP;
123 }
124
125 static inline int hi3660_thermal_temp_to_step(int temp)
126 {
127         return DIV_ROUND_UP(temp - HI3660_TEMP_BASE, HI3660_TEMP_STEP);
128 }
129
130 /*
131  * The lag register contains 5 bits encoding the temperature in steps.
132  *
133  * Each time the temperature crosses the threshold boundary, an
134  * interrupt is raised. It could be when the temperature is going
135  * above the threshold or below. However, if the temperature is
136  * fluctuating around this value due to the load, we can receive
137  * several interrupts which may not desired.
138  *
139  * We can setup a temperature representing the delta between the
140  * threshold and the current temperature when the temperature is
141  * decreasing.
142  *
143  * For instance: the lag register is 5°C, the threshold is 65°C, when
144  * the temperature reaches 65°C an interrupt is raised and when the
145  * temperature decrease to 65°C - 5°C another interrupt is raised.
146  *
147  * A very short lag can lead to an interrupt storm, a long lag
148  * increase the latency to react to the temperature changes.  In our
149  * case, that is not really a problem as we are polling the
150  * temperature.
151  *
152  * [0:4] : lag register
153  *
154  * The temperature is coded in steps, cf. HI6220_TEMP_STEP.
155  *
156  * Min : 0x00 :  0.0 °C
157  * Max : 0x1F : 24.3 °C
158  *
159  * The 'value' parameter is in milliCelsius.
160  */
161 static inline void hi6220_thermal_set_lag(void __iomem *addr, int value)
162 {
163         writel(DIV_ROUND_UP(value, HI6220_TEMP_STEP) & 0x1F,
164                         addr + HI6220_TEMP0_LAG);
165 }
166
167 static inline void hi6220_thermal_alarm_clear(void __iomem *addr, int value)
168 {
169         writel(value, addr + HI6220_TEMP0_INT_CLR);
170 }
171
172 static inline void hi6220_thermal_alarm_enable(void __iomem *addr, int value)
173 {
174         writel(value, addr + HI6220_TEMP0_INT_EN);
175 }
176
177 static inline void hi6220_thermal_alarm_set(void __iomem *addr, int temp)
178 {
179         writel(hi6220_thermal_temp_to_step(temp) | 0x0FFFFFF00,
180                addr + HI6220_TEMP0_TH);
181 }
182
183 static inline void hi6220_thermal_reset_set(void __iomem *addr, int temp)
184 {
185         writel(hi6220_thermal_temp_to_step(temp), addr + HI6220_TEMP0_RST_TH);
186 }
187
188 static inline void hi6220_thermal_reset_enable(void __iomem *addr, int value)
189 {
190         writel(value, addr + HI6220_TEMP0_RST_MSK);
191 }
192
193 static inline void hi6220_thermal_enable(void __iomem *addr, int value)
194 {
195         writel(value, addr + HI6220_TEMP0_EN);
196 }
197
198 static inline int hi6220_thermal_get_temperature(void __iomem *addr)
199 {
200         return hi6220_thermal_step_to_temp(readl(addr + HI6220_TEMP0_VALUE));
201 }
202
203 /*
204  * [0:6] lag register
205  *
206  * The temperature is coded in steps, cf. HI3660_TEMP_STEP.
207  *
208  * Min : 0x00 :  0.0 °C
209  * Max : 0x7F : 26.0 °C
210  *
211  */
212 static inline void hi3660_thermal_set_lag(void __iomem *addr,
213                                           int id, int value)
214 {
215         writel(DIV_ROUND_UP(value, HI3660_TEMP_STEP) & 0x7F,
216                         addr + HI3660_LAG(id));
217 }
218
219 static inline void hi3660_thermal_alarm_clear(void __iomem *addr,
220                                               int id, int value)
221 {
222         writel(value, addr + HI3660_INT_CLR(id));
223 }
224
225 static inline void hi3660_thermal_alarm_enable(void __iomem *addr,
226                                                int id, int value)
227 {
228         writel(value, addr + HI3660_INT_EN(id));
229 }
230
231 static inline void hi3660_thermal_alarm_set(void __iomem *addr,
232                                             int id, int value)
233 {
234         writel(value, addr + HI3660_TH(id));
235 }
236
237 static inline int hi3660_thermal_get_temperature(void __iomem *addr, int id)
238 {
239         return hi3660_thermal_step_to_temp(readl(addr + HI3660_TEMP(id)));
240 }
241
242 /*
243  * Temperature configuration register - Sensor selection
244  *
245  * Bits [19:12]
246  *
247  * 0x0: local sensor (default)
248  * 0x1: remote sensor 1 (ACPU cluster 1)
249  * 0x2: remote sensor 2 (ACPU cluster 0)
250  * 0x3: remote sensor 3 (G3D)
251  */
252 static inline void hi6220_thermal_sensor_select(void __iomem *addr, int sensor)
253 {
254         writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_SS_MSK) |
255                (sensor << 12), addr + HI6220_TEMP0_CFG);
256 }
257
258 /*
259  * Temperature configuration register - Hdak conversion polling interval
260  *
261  * Bits [5:4]
262  *
263  * 0x0 :   0.768 ms
264  * 0x1 :   6.144 ms
265  * 0x2 :  49.152 ms
266  * 0x3 : 393.216 ms
267  */
268 static inline void hi6220_thermal_hdak_set(void __iomem *addr, int value)
269 {
270         writel((readl(addr + HI6220_TEMP0_CFG) & ~HI6220_TEMP0_CFG_HDAK_MSK) |
271                (value << 4), addr + HI6220_TEMP0_CFG);
272 }
273
274 static int hi6220_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
275 {
276         struct hisi_thermal_data *data = sensor->data;
277
278         hi6220_thermal_alarm_clear(data->regs, 1);
279         return 0;
280 }
281
282 static int hi3660_thermal_irq_handler(struct hisi_thermal_sensor *sensor)
283 {
284         struct hisi_thermal_data *data = sensor->data;
285
286         hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
287         return 0;
288 }
289
290 static int hi6220_thermal_get_temp(struct hisi_thermal_sensor *sensor)
291 {
292         struct hisi_thermal_data *data = sensor->data;
293
294         return hi6220_thermal_get_temperature(data->regs);
295 }
296
297 static int hi3660_thermal_get_temp(struct hisi_thermal_sensor *sensor)
298 {
299         struct hisi_thermal_data *data = sensor->data;
300
301         return hi3660_thermal_get_temperature(data->regs, sensor->id);
302 }
303
304 static int hi6220_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
305 {
306         struct hisi_thermal_data *data = sensor->data;
307
308         /* disable sensor module */
309         hi6220_thermal_enable(data->regs, 0);
310         hi6220_thermal_alarm_enable(data->regs, 0);
311         hi6220_thermal_reset_enable(data->regs, 0);
312
313         clk_disable_unprepare(data->clk);
314
315         return 0;
316 }
317
318 static int hi3660_thermal_disable_sensor(struct hisi_thermal_sensor *sensor)
319 {
320         struct hisi_thermal_data *data = sensor->data;
321
322         /* disable sensor module */
323         hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
324         return 0;
325 }
326
327 static int hi6220_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
328 {
329         struct hisi_thermal_data *data = sensor->data;
330         int ret;
331
332         /* enable clock for tsensor */
333         ret = clk_prepare_enable(data->clk);
334         if (ret)
335                 return ret;
336
337         /* disable module firstly */
338         hi6220_thermal_reset_enable(data->regs, 0);
339         hi6220_thermal_enable(data->regs, 0);
340
341         /* select sensor id */
342         hi6220_thermal_sensor_select(data->regs, sensor->id);
343
344         /* setting the hdak time */
345         hi6220_thermal_hdak_set(data->regs, 0);
346
347         /* setting lag value between current temp and the threshold */
348         hi6220_thermal_set_lag(data->regs, HI6220_TEMP_LAG);
349
350         /* enable for interrupt */
351         hi6220_thermal_alarm_set(data->regs, sensor->thres_temp);
352
353         hi6220_thermal_reset_set(data->regs, HI6220_TEMP_RESET);
354
355         /* enable module */
356         hi6220_thermal_reset_enable(data->regs, 1);
357         hi6220_thermal_enable(data->regs, 1);
358
359         hi6220_thermal_alarm_clear(data->regs, 0);
360         hi6220_thermal_alarm_enable(data->regs, 1);
361
362         return 0;
363 }
364
365 static int hi3660_thermal_enable_sensor(struct hisi_thermal_sensor *sensor)
366 {
367         unsigned int value;
368         struct hisi_thermal_data *data = sensor->data;
369
370         /* disable interrupt */
371         hi3660_thermal_alarm_enable(data->regs, sensor->id, 0);
372
373         /* setting lag value between current temp and the threshold */
374         hi3660_thermal_set_lag(data->regs, sensor->id, HI3660_TEMP_LAG);
375
376         /* set interrupt threshold */
377         value = hi3660_thermal_temp_to_step(sensor->thres_temp);
378         hi3660_thermal_alarm_set(data->regs, sensor->id, value);
379
380         /* enable interrupt */
381         hi3660_thermal_alarm_clear(data->regs, sensor->id, 1);
382         hi3660_thermal_alarm_enable(data->regs, sensor->id, 1);
383
384         return 0;
385 }
386
387 static int hi6220_thermal_probe(struct hisi_thermal_data *data)
388 {
389         struct platform_device *pdev = data->pdev;
390         struct device *dev = &pdev->dev;
391         int ret;
392
393         data->clk = devm_clk_get(dev, "thermal_clk");
394         if (IS_ERR(data->clk)) {
395                 ret = PTR_ERR(data->clk);
396                 if (ret != -EPROBE_DEFER)
397                         dev_err(dev, "failed to get thermal clk: %d\n", ret);
398                 return ret;
399         }
400
401         data->sensor = devm_kzalloc(dev, sizeof(*data->sensor), GFP_KERNEL);
402         if (!data->sensor)
403                 return -ENOMEM;
404
405         data->sensor[0].id = HI6220_CLUSTER0_SENSOR;
406         data->sensor[0].irq_name = "tsensor_intr";
407         data->sensor[0].data = data;
408         data->nr_sensors = 1;
409
410         return 0;
411 }
412
413 static int hi3660_thermal_probe(struct hisi_thermal_data *data)
414 {
415         struct platform_device *pdev = data->pdev;
416         struct device *dev = &pdev->dev;
417
418         data->nr_sensors = 1;
419
420         data->sensor = devm_kzalloc(dev, sizeof(*data->sensor) *
421                                     data->nr_sensors, GFP_KERNEL);
422         if (!data->sensor)
423                 return -ENOMEM;
424
425         data->sensor[0].id = HI3660_BIG_SENSOR;
426         data->sensor[0].irq_name = "tsensor_a73";
427         data->sensor[0].data = data;
428
429         return 0;
430 }
431
432 static int hisi_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
433 {
434         struct hisi_thermal_sensor *sensor = tz->devdata;
435         struct hisi_thermal_data *data = sensor->data;
436
437         *temp = data->ops->get_temp(sensor);
438
439         dev_dbg(&data->pdev->dev, "tzd=%p, id=%d, temp=%d, thres=%d\n",
440                 sensor->tzd, sensor->id, *temp, sensor->thres_temp);
441
442         return 0;
443 }
444
445 static const struct thermal_zone_device_ops hisi_of_thermal_ops = {
446         .get_temp = hisi_thermal_get_temp,
447 };
448
449 static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
450 {
451         struct hisi_thermal_sensor *sensor = dev;
452         struct hisi_thermal_data *data = sensor->data;
453         int temp = 0;
454
455         data->ops->irq_handler(sensor);
456
457         temp = data->ops->get_temp(sensor);
458
459         if (temp >= sensor->thres_temp) {
460                 dev_crit(&data->pdev->dev,
461                          "sensor <%d> THERMAL ALARM: %d > %d\n",
462                          sensor->id, temp, sensor->thres_temp);
463
464                 thermal_zone_device_update(sensor->tzd,
465                                            THERMAL_EVENT_UNSPECIFIED);
466
467         } else {
468                 dev_crit(&data->pdev->dev,
469                          "sensor <%d> THERMAL ALARM stopped: %d < %d\n",
470                          sensor->id, temp, sensor->thres_temp);
471         }
472
473         return IRQ_HANDLED;
474 }
475
476 static int hisi_thermal_register_sensor(struct platform_device *pdev,
477                                         struct hisi_thermal_sensor *sensor)
478 {
479         int ret, i;
480         struct thermal_trip trip;
481
482         sensor->tzd = devm_thermal_of_zone_register(&pdev->dev,
483                                                     sensor->id, sensor,
484                                                     &hisi_of_thermal_ops);
485         if (IS_ERR(sensor->tzd)) {
486                 ret = PTR_ERR(sensor->tzd);
487                 sensor->tzd = NULL;
488                 dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
489                         sensor->id, ret);
490                 return ret;
491         }
492
493         for (i = 0; i < thermal_zone_get_num_trips(sensor->tzd); i++) {
494
495                 thermal_zone_get_trip(sensor->tzd, i, &trip);
496
497                 if (trip.type == THERMAL_TRIP_PASSIVE) {
498                         sensor->thres_temp = trip.temperature;
499                         break;
500                 }
501         }
502
503         return 0;
504 }
505
506 static const struct hisi_thermal_ops hi6220_ops = {
507         .get_temp       = hi6220_thermal_get_temp,
508         .enable_sensor  = hi6220_thermal_enable_sensor,
509         .disable_sensor = hi6220_thermal_disable_sensor,
510         .irq_handler    = hi6220_thermal_irq_handler,
511         .probe          = hi6220_thermal_probe,
512 };
513
514 static const struct hisi_thermal_ops hi3660_ops = {
515         .get_temp       = hi3660_thermal_get_temp,
516         .enable_sensor  = hi3660_thermal_enable_sensor,
517         .disable_sensor = hi3660_thermal_disable_sensor,
518         .irq_handler    = hi3660_thermal_irq_handler,
519         .probe          = hi3660_thermal_probe,
520 };
521
522 static const struct of_device_id of_hisi_thermal_match[] = {
523         {
524                 .compatible = "hisilicon,tsensor",
525                 .data = &hi6220_ops,
526         },
527         {
528                 .compatible = "hisilicon,hi3660-tsensor",
529                 .data = &hi3660_ops,
530         },
531         { /* end */ }
532 };
533 MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
534
535 static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
536                                        bool on)
537 {
538         struct thermal_zone_device *tzd = sensor->tzd;
539
540         if (on)
541                 thermal_zone_device_enable(tzd);
542         else
543                 thermal_zone_device_disable(tzd);
544 }
545
546 static int hisi_thermal_probe(struct platform_device *pdev)
547 {
548         struct hisi_thermal_data *data;
549         struct device *dev = &pdev->dev;
550         struct resource *res;
551         int i, ret;
552
553         data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
554         if (!data)
555                 return -ENOMEM;
556
557         data->pdev = pdev;
558         platform_set_drvdata(pdev, data);
559         data->ops = of_device_get_match_data(dev);
560
561         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
562         data->regs = devm_ioremap_resource(dev, res);
563         if (IS_ERR(data->regs))
564                 return PTR_ERR(data->regs);
565
566         ret = data->ops->probe(data);
567         if (ret)
568                 return ret;
569
570         for (i = 0; i < data->nr_sensors; i++) {
571                 struct hisi_thermal_sensor *sensor = &data->sensor[i];
572
573                 ret = hisi_thermal_register_sensor(pdev, sensor);
574                 if (ret) {
575                         dev_err(dev, "failed to register thermal sensor: %d\n",
576                                 ret);
577                         return ret;
578                 }
579
580                 ret = platform_get_irq(pdev, 0);
581                 if (ret < 0)
582                         return ret;
583
584                 ret = devm_request_threaded_irq(dev, ret, NULL,
585                                                 hisi_thermal_alarm_irq_thread,
586                                                 IRQF_ONESHOT, sensor->irq_name,
587                                                 sensor);
588                 if (ret < 0) {
589                         dev_err(dev, "Failed to request alarm irq: %d\n", ret);
590                         return ret;
591                 }
592
593                 ret = data->ops->enable_sensor(sensor);
594                 if (ret) {
595                         dev_err(dev, "Failed to setup the sensor: %d\n", ret);
596                         return ret;
597                 }
598
599                 hisi_thermal_toggle_sensor(sensor, true);
600         }
601
602         return 0;
603 }
604
605 static int hisi_thermal_remove(struct platform_device *pdev)
606 {
607         struct hisi_thermal_data *data = platform_get_drvdata(pdev);
608         int i;
609
610         for (i = 0; i < data->nr_sensors; i++) {
611                 struct hisi_thermal_sensor *sensor = &data->sensor[i];
612
613                 hisi_thermal_toggle_sensor(sensor, false);
614                 data->ops->disable_sensor(sensor);
615         }
616
617         return 0;
618 }
619
620 static int hisi_thermal_suspend(struct device *dev)
621 {
622         struct hisi_thermal_data *data = dev_get_drvdata(dev);
623         int i;
624
625         for (i = 0; i < data->nr_sensors; i++)
626                 data->ops->disable_sensor(&data->sensor[i]);
627
628         return 0;
629 }
630
631 static int hisi_thermal_resume(struct device *dev)
632 {
633         struct hisi_thermal_data *data = dev_get_drvdata(dev);
634         int i, ret = 0;
635
636         for (i = 0; i < data->nr_sensors; i++)
637                 ret |= data->ops->enable_sensor(&data->sensor[i]);
638
639         return ret;
640 }
641
642 static DEFINE_SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
643                          hisi_thermal_suspend, hisi_thermal_resume);
644
645 static struct platform_driver hisi_thermal_driver = {
646         .driver = {
647                 .name           = "hisi_thermal",
648                 .pm             = pm_sleep_ptr(&hisi_thermal_pm_ops),
649                 .of_match_table = of_hisi_thermal_match,
650         },
651         .probe  = hisi_thermal_probe,
652         .remove = hisi_thermal_remove,
653 };
654
655 module_platform_driver(hisi_thermal_driver);
656
657 MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
658 MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
659 MODULE_DESCRIPTION("HiSilicon thermal driver");
660 MODULE_LICENSE("GPL v2");