GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / thermal / sun8i_thermal.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Thermal sensor driver for Allwinner SOC
4  * Copyright (C) 2019 Yangtao Li
5  *
6  * Based on the work of Icenowy Zheng <icenowy@aosc.io>
7  * Based on the work of Ondrej Jirman <megous@megous.com>
8  * Based on the work of Josef Gajdusek <atx@atx.name>
9  */
10
11 #include <linux/clk.h>
12 #include <linux/device.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/nvmem-consumer.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 #include <linux/reset.h>
20 #include <linux/slab.h>
21 #include <linux/thermal.h>
22
23 #include "thermal_hwmon.h"
24
25 #define MAX_SENSOR_NUM  4
26
27 #define FT_TEMP_MASK                            GENMASK(11, 0)
28 #define TEMP_CALIB_MASK                         GENMASK(11, 0)
29 #define CALIBRATE_DEFAULT                       0x800
30
31 #define SUN8I_THS_CTRL0                         0x00
32 #define SUN8I_THS_CTRL2                         0x40
33 #define SUN8I_THS_IC                            0x44
34 #define SUN8I_THS_IS                            0x48
35 #define SUN8I_THS_MFC                           0x70
36 #define SUN8I_THS_TEMP_CALIB                    0x74
37 #define SUN8I_THS_TEMP_DATA                     0x80
38
39 #define SUN50I_THS_CTRL0                        0x00
40 #define SUN50I_H6_THS_ENABLE                    0x04
41 #define SUN50I_H6_THS_PC                        0x08
42 #define SUN50I_H6_THS_DIC                       0x10
43 #define SUN50I_H6_THS_DIS                       0x20
44 #define SUN50I_H6_THS_MFC                       0x30
45 #define SUN50I_H6_THS_TEMP_CALIB                0xa0
46 #define SUN50I_H6_THS_TEMP_DATA                 0xc0
47
48 #define SUN8I_THS_CTRL0_T_ACQ0(x)               (GENMASK(15, 0) & (x))
49 #define SUN8I_THS_CTRL2_T_ACQ1(x)               ((GENMASK(15, 0) & (x)) << 16)
50 #define SUN8I_THS_DATA_IRQ_STS(x)               BIT(x + 8)
51
52 #define SUN50I_THS_CTRL0_T_ACQ(x)               ((GENMASK(15, 0) & (x)) << 16)
53 #define SUN50I_THS_FILTER_EN                    BIT(2)
54 #define SUN50I_THS_FILTER_TYPE(x)               (GENMASK(1, 0) & (x))
55 #define SUN50I_H6_THS_PC_TEMP_PERIOD(x)         ((GENMASK(19, 0) & (x)) << 12)
56 #define SUN50I_H6_THS_DATA_IRQ_STS(x)           BIT(x)
57
58 /* millidegree celsius */
59
60 struct tsensor {
61         struct ths_device               *tmdev;
62         struct thermal_zone_device      *tzd;
63         int                             id;
64 };
65
66 struct ths_thermal_chip {
67         bool            has_mod_clk;
68         bool            has_bus_clk_reset;
69         int             sensor_num;
70         int             offset;
71         int             scale;
72         int             ft_deviation;
73         int             temp_data_base;
74         int             (*calibrate)(struct ths_device *tmdev,
75                                      u16 *caldata, int callen);
76         int             (*init)(struct ths_device *tmdev);
77         int             (*irq_ack)(struct ths_device *tmdev);
78         int             (*calc_temp)(struct ths_device *tmdev,
79                                      int id, int reg);
80 };
81
82 struct ths_device {
83         const struct ths_thermal_chip           *chip;
84         struct device                           *dev;
85         struct regmap                           *regmap;
86         struct reset_control                    *reset;
87         struct clk                              *bus_clk;
88         struct clk                              *mod_clk;
89         struct tsensor                          sensor[MAX_SENSOR_NUM];
90 };
91
92 /* Temp Unit: millidegree Celsius */
93 static int sun8i_ths_calc_temp(struct ths_device *tmdev,
94                                int id, int reg)
95 {
96         return tmdev->chip->offset - (reg * tmdev->chip->scale / 10);
97 }
98
99 static int sun50i_h5_calc_temp(struct ths_device *tmdev,
100                                int id, int reg)
101 {
102         if (reg >= 0x500)
103                 return -1191 * reg / 10 + 223000;
104         else if (!id)
105                 return -1452 * reg / 10 + 259000;
106         else
107                 return -1590 * reg / 10 + 276000;
108 }
109
110 static int sun8i_ths_get_temp(void *data, int *temp)
111 {
112         struct tsensor *s = data;
113         struct ths_device *tmdev = s->tmdev;
114         int val = 0;
115
116         regmap_read(tmdev->regmap, tmdev->chip->temp_data_base +
117                     0x4 * s->id, &val);
118
119         /* ths have no data yet */
120         if (!val)
121                 return -EAGAIN;
122
123         *temp = tmdev->chip->calc_temp(tmdev, s->id, val);
124         /*
125          * According to the original sdk, there are some platforms(rarely)
126          * that add a fixed offset value after calculating the temperature
127          * value. We can't simply put it on the formula for calculating the
128          * temperature above, because the formula for calculating the
129          * temperature above is also used when the sensor is calibrated. If
130          * do this, the correct calibration formula is hard to know.
131          */
132         *temp += tmdev->chip->ft_deviation;
133
134         return 0;
135 }
136
137 static const struct thermal_zone_of_device_ops ths_ops = {
138         .get_temp = sun8i_ths_get_temp,
139 };
140
141 static const struct regmap_config config = {
142         .reg_bits = 32,
143         .val_bits = 32,
144         .reg_stride = 4,
145         .fast_io = true,
146         .max_register = 0xfc,
147 };
148
149 static int sun8i_h3_irq_ack(struct ths_device *tmdev)
150 {
151         int i, state, ret = 0;
152
153         regmap_read(tmdev->regmap, SUN8I_THS_IS, &state);
154
155         for (i = 0; i < tmdev->chip->sensor_num; i++) {
156                 if (state & SUN8I_THS_DATA_IRQ_STS(i)) {
157                         regmap_write(tmdev->regmap, SUN8I_THS_IS,
158                                      SUN8I_THS_DATA_IRQ_STS(i));
159                         ret |= BIT(i);
160                 }
161         }
162
163         return ret;
164 }
165
166 static int sun50i_h6_irq_ack(struct ths_device *tmdev)
167 {
168         int i, state, ret = 0;
169
170         regmap_read(tmdev->regmap, SUN50I_H6_THS_DIS, &state);
171
172         for (i = 0; i < tmdev->chip->sensor_num; i++) {
173                 if (state & SUN50I_H6_THS_DATA_IRQ_STS(i)) {
174                         regmap_write(tmdev->regmap, SUN50I_H6_THS_DIS,
175                                      SUN50I_H6_THS_DATA_IRQ_STS(i));
176                         ret |= BIT(i);
177                 }
178         }
179
180         return ret;
181 }
182
183 static irqreturn_t sun8i_irq_thread(int irq, void *data)
184 {
185         struct ths_device *tmdev = data;
186         int i, state;
187
188         state = tmdev->chip->irq_ack(tmdev);
189
190         for (i = 0; i < tmdev->chip->sensor_num; i++) {
191                 if (state & BIT(i))
192                         thermal_zone_device_update(tmdev->sensor[i].tzd,
193                                                    THERMAL_EVENT_UNSPECIFIED);
194         }
195
196         return IRQ_HANDLED;
197 }
198
199 static int sun8i_h3_ths_calibrate(struct ths_device *tmdev,
200                                   u16 *caldata, int callen)
201 {
202         int i;
203
204         if (!caldata[0] || callen < 2 * tmdev->chip->sensor_num)
205                 return -EINVAL;
206
207         for (i = 0; i < tmdev->chip->sensor_num; i++) {
208                 int offset = (i % 2) << 4;
209
210                 regmap_update_bits(tmdev->regmap,
211                                    SUN8I_THS_TEMP_CALIB + (4 * (i >> 1)),
212                                    0xfff << offset,
213                                    caldata[i] << offset);
214         }
215
216         return 0;
217 }
218
219 static int sun50i_h6_ths_calibrate(struct ths_device *tmdev,
220                                    u16 *caldata, int callen)
221 {
222         struct device *dev = tmdev->dev;
223         int i, ft_temp;
224
225         if (!caldata[0] || callen < 2 + 2 * tmdev->chip->sensor_num)
226                 return -EINVAL;
227
228         /*
229          * efuse layout:
230          *
231          *      0   11  16       32
232          *      +-------+-------+-------+
233          *      |temp|  |sensor0|sensor1|
234          *      +-------+-------+-------+
235          *
236          * The calibration data on the H6 is the ambient temperature and
237          * sensor values that are filled during the factory test stage.
238          *
239          * The unit of stored FT temperature is 0.1 degreee celusis.
240          *
241          * We need to calculate a delta between measured and caluclated
242          * register values and this will become a calibration offset.
243          */
244         ft_temp = (caldata[0] & FT_TEMP_MASK) * 100;
245
246         for (i = 0; i < tmdev->chip->sensor_num; i++) {
247                 int sensor_reg = caldata[i + 1] & TEMP_CALIB_MASK;
248                 int cdata, offset;
249                 int sensor_temp = tmdev->chip->calc_temp(tmdev, i, sensor_reg);
250
251                 /*
252                  * Calibration data is CALIBRATE_DEFAULT - (calculated
253                  * temperature from sensor reading at factory temperature
254                  * minus actual factory temperature) * 14.88 (scale from
255                  * temperature to register values)
256                  */
257                 cdata = CALIBRATE_DEFAULT -
258                         ((sensor_temp - ft_temp) * 10 / tmdev->chip->scale);
259                 if (cdata & ~TEMP_CALIB_MASK) {
260                         /*
261                          * Calibration value more than 12-bit, but calibration
262                          * register is 12-bit. In this case, ths hardware can
263                          * still work without calibration, although the data
264                          * won't be so accurate.
265                          */
266                         dev_warn(dev, "sensor%d is not calibrated.\n", i);
267                         continue;
268                 }
269
270                 offset = (i % 2) * 16;
271                 regmap_update_bits(tmdev->regmap,
272                                    SUN50I_H6_THS_TEMP_CALIB + (i / 2 * 4),
273                                    0xfff << offset,
274                                    cdata << offset);
275         }
276
277         return 0;
278 }
279
280 static int sun8i_ths_calibrate(struct ths_device *tmdev)
281 {
282         struct nvmem_cell *calcell;
283         struct device *dev = tmdev->dev;
284         u16 *caldata;
285         size_t callen;
286         int ret = 0;
287
288         calcell = devm_nvmem_cell_get(dev, "calibration");
289         if (IS_ERR(calcell)) {
290                 if (PTR_ERR(calcell) == -EPROBE_DEFER)
291                         return -EPROBE_DEFER;
292                 /*
293                  * Even if the external calibration data stored in sid is
294                  * not accessible, the THS hardware can still work, although
295                  * the data won't be so accurate.
296                  *
297                  * The default value of calibration register is 0x800 for
298                  * every sensor, and the calibration value is usually 0x7xx
299                  * or 0x8xx, so they won't be away from the default value
300                  * for a lot.
301                  *
302                  * So here we do not return error if the calibartion data is
303                  * not available, except the probe needs deferring.
304                  */
305                 goto out;
306         }
307
308         caldata = nvmem_cell_read(calcell, &callen);
309         if (IS_ERR(caldata)) {
310                 ret = PTR_ERR(caldata);
311                 goto out;
312         }
313
314         tmdev->chip->calibrate(tmdev, caldata, callen);
315
316         kfree(caldata);
317 out:
318         return ret;
319 }
320
321 static void sun8i_ths_reset_control_assert(void *data)
322 {
323         reset_control_assert(data);
324 }
325
326 static int sun8i_ths_resource_init(struct ths_device *tmdev)
327 {
328         struct device *dev = tmdev->dev;
329         struct platform_device *pdev = to_platform_device(dev);
330         void __iomem *base;
331         int ret;
332
333         base = devm_platform_ioremap_resource(pdev, 0);
334         if (IS_ERR(base))
335                 return PTR_ERR(base);
336
337         tmdev->regmap = devm_regmap_init_mmio(dev, base, &config);
338         if (IS_ERR(tmdev->regmap))
339                 return PTR_ERR(tmdev->regmap);
340
341         if (tmdev->chip->has_bus_clk_reset) {
342                 tmdev->reset = devm_reset_control_get(dev, NULL);
343                 if (IS_ERR(tmdev->reset))
344                         return PTR_ERR(tmdev->reset);
345
346                 ret = reset_control_deassert(tmdev->reset);
347                 if (ret)
348                         return ret;
349
350                 ret = devm_add_action_or_reset(dev, sun8i_ths_reset_control_assert,
351                                                tmdev->reset);
352                 if (ret)
353                         return ret;
354
355                 tmdev->bus_clk = devm_clk_get_enabled(&pdev->dev, "bus");
356                 if (IS_ERR(tmdev->bus_clk))
357                         return PTR_ERR(tmdev->bus_clk);
358         }
359
360         if (tmdev->chip->has_mod_clk) {
361                 tmdev->mod_clk = devm_clk_get_enabled(&pdev->dev, "mod");
362                 if (IS_ERR(tmdev->mod_clk))
363                         return PTR_ERR(tmdev->mod_clk);
364         }
365
366         ret = clk_set_rate(tmdev->mod_clk, 24000000);
367         if (ret)
368                 return ret;
369
370         ret = sun8i_ths_calibrate(tmdev);
371         if (ret)
372                 return ret;
373
374         return 0;
375 }
376
377 static int sun8i_h3_thermal_init(struct ths_device *tmdev)
378 {
379         int val;
380
381         /* average over 4 samples */
382         regmap_write(tmdev->regmap, SUN8I_THS_MFC,
383                      SUN50I_THS_FILTER_EN |
384                      SUN50I_THS_FILTER_TYPE(1));
385         /*
386          * clkin = 24MHz
387          * filter_samples = 4
388          * period = 0.25s
389          *
390          * x = period * clkin / 4096 / filter_samples - 1
391          *   = 365
392          */
393         val = GENMASK(7 + tmdev->chip->sensor_num, 8);
394         regmap_write(tmdev->regmap, SUN8I_THS_IC,
395                      SUN50I_H6_THS_PC_TEMP_PERIOD(365) | val);
396         /*
397          * T_acq = 20us
398          * clkin = 24MHz
399          *
400          * x = T_acq * clkin - 1
401          *   = 479
402          */
403         regmap_write(tmdev->regmap, SUN8I_THS_CTRL0,
404                      SUN8I_THS_CTRL0_T_ACQ0(479));
405         val = GENMASK(tmdev->chip->sensor_num - 1, 0);
406         regmap_write(tmdev->regmap, SUN8I_THS_CTRL2,
407                      SUN8I_THS_CTRL2_T_ACQ1(479) | val);
408
409         return 0;
410 }
411
412 /*
413  * Without this undocummented value, the returned temperatures would
414  * be higher than real ones by about 20C.
415  */
416 #define SUN50I_H6_CTRL0_UNK 0x0000002f
417
418 static int sun50i_h6_thermal_init(struct ths_device *tmdev)
419 {
420         int val;
421
422         /*
423          * T_acq = 20us
424          * clkin = 24MHz
425          *
426          * x = T_acq * clkin - 1
427          *   = 479
428          */
429         regmap_write(tmdev->regmap, SUN50I_THS_CTRL0,
430                      SUN50I_H6_CTRL0_UNK | SUN50I_THS_CTRL0_T_ACQ(479));
431         /* average over 4 samples */
432         regmap_write(tmdev->regmap, SUN50I_H6_THS_MFC,
433                      SUN50I_THS_FILTER_EN |
434                      SUN50I_THS_FILTER_TYPE(1));
435         /*
436          * clkin = 24MHz
437          * filter_samples = 4
438          * period = 0.25s
439          *
440          * x = period * clkin / 4096 / filter_samples - 1
441          *   = 365
442          */
443         regmap_write(tmdev->regmap, SUN50I_H6_THS_PC,
444                      SUN50I_H6_THS_PC_TEMP_PERIOD(365));
445         /* enable sensor */
446         val = GENMASK(tmdev->chip->sensor_num - 1, 0);
447         regmap_write(tmdev->regmap, SUN50I_H6_THS_ENABLE, val);
448         /* thermal data interrupt enable */
449         val = GENMASK(tmdev->chip->sensor_num - 1, 0);
450         regmap_write(tmdev->regmap, SUN50I_H6_THS_DIC, val);
451
452         return 0;
453 }
454
455 static int sun8i_ths_register(struct ths_device *tmdev)
456 {
457         int i;
458
459         for (i = 0; i < tmdev->chip->sensor_num; i++) {
460                 tmdev->sensor[i].tmdev = tmdev;
461                 tmdev->sensor[i].id = i;
462                 tmdev->sensor[i].tzd =
463                         devm_thermal_zone_of_sensor_register(tmdev->dev,
464                                                              i,
465                                                              &tmdev->sensor[i],
466                                                              &ths_ops);
467                 if (IS_ERR(tmdev->sensor[i].tzd))
468                         return PTR_ERR(tmdev->sensor[i].tzd);
469
470                 if (devm_thermal_add_hwmon_sysfs(tmdev->sensor[i].tzd))
471                         dev_warn(tmdev->dev,
472                                  "Failed to add hwmon sysfs attributes\n");
473         }
474
475         return 0;
476 }
477
478 static int sun8i_ths_probe(struct platform_device *pdev)
479 {
480         struct ths_device *tmdev;
481         struct device *dev = &pdev->dev;
482         int ret, irq;
483
484         tmdev = devm_kzalloc(dev, sizeof(*tmdev), GFP_KERNEL);
485         if (!tmdev)
486                 return -ENOMEM;
487
488         tmdev->dev = dev;
489         tmdev->chip = of_device_get_match_data(&pdev->dev);
490         if (!tmdev->chip)
491                 return -EINVAL;
492
493         platform_set_drvdata(pdev, tmdev);
494
495         ret = sun8i_ths_resource_init(tmdev);
496         if (ret)
497                 return ret;
498
499         irq = platform_get_irq(pdev, 0);
500         if (irq < 0)
501                 return irq;
502
503         ret = tmdev->chip->init(tmdev);
504         if (ret)
505                 return ret;
506
507         ret = sun8i_ths_register(tmdev);
508         if (ret)
509                 return ret;
510
511         /*
512          * Avoid entering the interrupt handler, the thermal device is not
513          * registered yet, we deffer the registration of the interrupt to
514          * the end.
515          */
516         ret = devm_request_threaded_irq(dev, irq, NULL,
517                                         sun8i_irq_thread,
518                                         IRQF_ONESHOT, "ths", tmdev);
519         if (ret)
520                 return ret;
521
522         return 0;
523 }
524
525 static const struct ths_thermal_chip sun8i_a83t_ths = {
526         .sensor_num = 3,
527         .scale = 705,
528         .offset = 191668,
529         .temp_data_base = SUN8I_THS_TEMP_DATA,
530         .calibrate = sun8i_h3_ths_calibrate,
531         .init = sun8i_h3_thermal_init,
532         .irq_ack = sun8i_h3_irq_ack,
533         .calc_temp = sun8i_ths_calc_temp,
534 };
535
536 static const struct ths_thermal_chip sun8i_h3_ths = {
537         .sensor_num = 1,
538         .scale = 1211,
539         .offset = 217000,
540         .has_mod_clk = true,
541         .has_bus_clk_reset = true,
542         .temp_data_base = SUN8I_THS_TEMP_DATA,
543         .calibrate = sun8i_h3_ths_calibrate,
544         .init = sun8i_h3_thermal_init,
545         .irq_ack = sun8i_h3_irq_ack,
546         .calc_temp = sun8i_ths_calc_temp,
547 };
548
549 static const struct ths_thermal_chip sun8i_r40_ths = {
550         .sensor_num = 2,
551         .offset = 251086,
552         .scale = 1130,
553         .has_mod_clk = true,
554         .has_bus_clk_reset = true,
555         .temp_data_base = SUN8I_THS_TEMP_DATA,
556         .calibrate = sun8i_h3_ths_calibrate,
557         .init = sun8i_h3_thermal_init,
558         .irq_ack = sun8i_h3_irq_ack,
559         .calc_temp = sun8i_ths_calc_temp,
560 };
561
562 static const struct ths_thermal_chip sun50i_a64_ths = {
563         .sensor_num = 3,
564         .offset = 260890,
565         .scale = 1170,
566         .has_mod_clk = true,
567         .has_bus_clk_reset = true,
568         .temp_data_base = SUN8I_THS_TEMP_DATA,
569         .calibrate = sun8i_h3_ths_calibrate,
570         .init = sun8i_h3_thermal_init,
571         .irq_ack = sun8i_h3_irq_ack,
572         .calc_temp = sun8i_ths_calc_temp,
573 };
574
575 static const struct ths_thermal_chip sun50i_a100_ths = {
576         .sensor_num = 3,
577         .has_bus_clk_reset = true,
578         .ft_deviation = 8000,
579         .offset = 187744,
580         .scale = 672,
581         .temp_data_base = SUN50I_H6_THS_TEMP_DATA,
582         .calibrate = sun50i_h6_ths_calibrate,
583         .init = sun50i_h6_thermal_init,
584         .irq_ack = sun50i_h6_irq_ack,
585         .calc_temp = sun8i_ths_calc_temp,
586 };
587
588 static const struct ths_thermal_chip sun50i_h5_ths = {
589         .sensor_num = 2,
590         .has_mod_clk = true,
591         .has_bus_clk_reset = true,
592         .temp_data_base = SUN8I_THS_TEMP_DATA,
593         .calibrate = sun8i_h3_ths_calibrate,
594         .init = sun8i_h3_thermal_init,
595         .irq_ack = sun8i_h3_irq_ack,
596         .calc_temp = sun50i_h5_calc_temp,
597 };
598
599 static const struct ths_thermal_chip sun50i_h6_ths = {
600         .sensor_num = 2,
601         .has_bus_clk_reset = true,
602         .ft_deviation = 7000,
603         .offset = 187744,
604         .scale = 672,
605         .temp_data_base = SUN50I_H6_THS_TEMP_DATA,
606         .calibrate = sun50i_h6_ths_calibrate,
607         .init = sun50i_h6_thermal_init,
608         .irq_ack = sun50i_h6_irq_ack,
609         .calc_temp = sun8i_ths_calc_temp,
610 };
611
612 static const struct of_device_id of_ths_match[] = {
613         { .compatible = "allwinner,sun8i-a83t-ths", .data = &sun8i_a83t_ths },
614         { .compatible = "allwinner,sun8i-h3-ths", .data = &sun8i_h3_ths },
615         { .compatible = "allwinner,sun8i-r40-ths", .data = &sun8i_r40_ths },
616         { .compatible = "allwinner,sun50i-a64-ths", .data = &sun50i_a64_ths },
617         { .compatible = "allwinner,sun50i-a100-ths", .data = &sun50i_a100_ths },
618         { .compatible = "allwinner,sun50i-h5-ths", .data = &sun50i_h5_ths },
619         { .compatible = "allwinner,sun50i-h6-ths", .data = &sun50i_h6_ths },
620         { /* sentinel */ },
621 };
622 MODULE_DEVICE_TABLE(of, of_ths_match);
623
624 static struct platform_driver ths_driver = {
625         .probe = sun8i_ths_probe,
626         .driver = {
627                 .name = "sun8i-thermal",
628                 .of_match_table = of_ths_match,
629         },
630 };
631 module_platform_driver(ths_driver);
632
633 MODULE_DESCRIPTION("Thermal sensor driver for Allwinner SOC");
634 MODULE_LICENSE("GPL v2");