1 // SPDX-License-Identifier: GPL-2.0
3 * Thermal sensor driver for Allwinner SOC
4 * Copyright (C) 2019 Yangtao Li
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>
11 #include <linux/bitmap.h>
12 #include <linux/clk.h>
13 #include <linux/device.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/reset.h>
21 #include <linux/slab.h>
22 #include <linux/thermal.h>
24 #include "thermal_hwmon.h"
26 #define MAX_SENSOR_NUM 4
28 #define FT_TEMP_MASK GENMASK(11, 0)
29 #define TEMP_CALIB_MASK GENMASK(11, 0)
30 #define CALIBRATE_DEFAULT 0x800
32 #define SUN8I_THS_CTRL0 0x00
33 #define SUN8I_THS_CTRL2 0x40
34 #define SUN8I_THS_IC 0x44
35 #define SUN8I_THS_IS 0x48
36 #define SUN8I_THS_MFC 0x70
37 #define SUN8I_THS_TEMP_CALIB 0x74
38 #define SUN8I_THS_TEMP_DATA 0x80
40 #define SUN50I_THS_CTRL0 0x00
41 #define SUN50I_H6_THS_ENABLE 0x04
42 #define SUN50I_H6_THS_PC 0x08
43 #define SUN50I_H6_THS_DIC 0x10
44 #define SUN50I_H6_THS_DIS 0x20
45 #define SUN50I_H6_THS_MFC 0x30
46 #define SUN50I_H6_THS_TEMP_CALIB 0xa0
47 #define SUN50I_H6_THS_TEMP_DATA 0xc0
49 #define SUN8I_THS_CTRL0_T_ACQ0(x) (GENMASK(15, 0) & (x))
50 #define SUN8I_THS_CTRL2_T_ACQ1(x) ((GENMASK(15, 0) & (x)) << 16)
51 #define SUN8I_THS_DATA_IRQ_STS(x) BIT(x + 8)
53 #define SUN50I_THS_CTRL0_T_ACQ(x) ((GENMASK(15, 0) & (x)) << 16)
54 #define SUN50I_THS_FILTER_EN BIT(2)
55 #define SUN50I_THS_FILTER_TYPE(x) (GENMASK(1, 0) & (x))
56 #define SUN50I_H6_THS_PC_TEMP_PERIOD(x) ((GENMASK(19, 0) & (x)) << 12)
57 #define SUN50I_H6_THS_DATA_IRQ_STS(x) BIT(x)
59 /* millidegree celsius */
62 struct ths_device
*tmdev
;
63 struct thermal_zone_device
*tzd
;
67 struct ths_thermal_chip
{
69 bool has_bus_clk_reset
;
75 int (*calibrate
)(struct ths_device
*tmdev
,
76 u16
*caldata
, int callen
);
77 int (*init
)(struct ths_device
*tmdev
);
78 unsigned long (*irq_ack
)(struct ths_device
*tmdev
);
79 int (*calc_temp
)(struct ths_device
*tmdev
,
84 const struct ths_thermal_chip
*chip
;
86 struct regmap
*regmap
;
87 struct reset_control
*reset
;
90 struct tsensor sensor
[MAX_SENSOR_NUM
];
93 /* Temp Unit: millidegree Celsius */
94 static int sun8i_ths_calc_temp(struct ths_device
*tmdev
,
97 return tmdev
->chip
->offset
- (reg
* tmdev
->chip
->scale
/ 10);
100 static int sun50i_h5_calc_temp(struct ths_device
*tmdev
,
104 return -1191 * reg
/ 10 + 223000;
106 return -1452 * reg
/ 10 + 259000;
108 return -1590 * reg
/ 10 + 276000;
111 static int sun8i_ths_get_temp(void *data
, int *temp
)
113 struct tsensor
*s
= data
;
114 struct ths_device
*tmdev
= s
->tmdev
;
117 regmap_read(tmdev
->regmap
, tmdev
->chip
->temp_data_base
+
120 /* ths have no data yet */
124 *temp
= tmdev
->chip
->calc_temp(tmdev
, s
->id
, val
);
126 * According to the original sdk, there are some platforms(rarely)
127 * that add a fixed offset value after calculating the temperature
128 * value. We can't simply put it on the formula for calculating the
129 * temperature above, because the formula for calculating the
130 * temperature above is also used when the sensor is calibrated. If
131 * do this, the correct calibration formula is hard to know.
133 *temp
+= tmdev
->chip
->ft_deviation
;
138 static const struct thermal_zone_of_device_ops ths_ops
= {
139 .get_temp
= sun8i_ths_get_temp
,
142 static const struct regmap_config config
= {
147 .max_register
= 0xfc,
150 static unsigned long sun8i_h3_irq_ack(struct ths_device
*tmdev
)
152 unsigned long irq_bitmap
= 0;
155 regmap_read(tmdev
->regmap
, SUN8I_THS_IS
, &state
);
157 for (i
= 0; i
< tmdev
->chip
->sensor_num
; i
++) {
158 if (state
& SUN8I_THS_DATA_IRQ_STS(i
)) {
159 regmap_write(tmdev
->regmap
, SUN8I_THS_IS
,
160 SUN8I_THS_DATA_IRQ_STS(i
));
161 bitmap_set(&irq_bitmap
, i
, 1);
168 static unsigned long sun50i_h6_irq_ack(struct ths_device
*tmdev
)
170 unsigned long irq_bitmap
= 0;
173 regmap_read(tmdev
->regmap
, SUN50I_H6_THS_DIS
, &state
);
175 for (i
= 0; i
< tmdev
->chip
->sensor_num
; i
++) {
176 if (state
& SUN50I_H6_THS_DATA_IRQ_STS(i
)) {
177 regmap_write(tmdev
->regmap
, SUN50I_H6_THS_DIS
,
178 SUN50I_H6_THS_DATA_IRQ_STS(i
));
179 bitmap_set(&irq_bitmap
, i
, 1);
186 static irqreturn_t
sun8i_irq_thread(int irq
, void *data
)
188 struct ths_device
*tmdev
= data
;
189 unsigned long irq_bitmap
= tmdev
->chip
->irq_ack(tmdev
);
192 for_each_set_bit(i
, &irq_bitmap
, tmdev
->chip
->sensor_num
) {
193 thermal_zone_device_update(tmdev
->sensor
[i
].tzd
,
194 THERMAL_EVENT_UNSPECIFIED
);
200 static int sun8i_h3_ths_calibrate(struct ths_device
*tmdev
,
201 u16
*caldata
, int callen
)
205 if (!caldata
[0] || callen
< 2 * tmdev
->chip
->sensor_num
)
208 for (i
= 0; i
< tmdev
->chip
->sensor_num
; i
++) {
209 int offset
= (i
% 2) << 4;
211 regmap_update_bits(tmdev
->regmap
,
212 SUN8I_THS_TEMP_CALIB
+ (4 * (i
>> 1)),
214 caldata
[i
] << offset
);
220 static int sun50i_h6_ths_calibrate(struct ths_device
*tmdev
,
221 u16
*caldata
, int callen
)
223 struct device
*dev
= tmdev
->dev
;
226 if (!caldata
[0] || callen
< 2 + 2 * tmdev
->chip
->sensor_num
)
233 * +-------+-------+-------+
234 * |temp| |sensor0|sensor1|
235 * +-------+-------+-------+
237 * The calibration data on the H6 is the ambient temperature and
238 * sensor values that are filled during the factory test stage.
240 * The unit of stored FT temperature is 0.1 degreee celusis.
242 * We need to calculate a delta between measured and caluclated
243 * register values and this will become a calibration offset.
245 ft_temp
= (caldata
[0] & FT_TEMP_MASK
) * 100;
247 for (i
= 0; i
< tmdev
->chip
->sensor_num
; i
++) {
248 int sensor_reg
= caldata
[i
+ 1] & TEMP_CALIB_MASK
;
250 int sensor_temp
= tmdev
->chip
->calc_temp(tmdev
, i
, sensor_reg
);
253 * Calibration data is CALIBRATE_DEFAULT - (calculated
254 * temperature from sensor reading at factory temperature
255 * minus actual factory temperature) * 14.88 (scale from
256 * temperature to register values)
258 cdata
= CALIBRATE_DEFAULT
-
259 ((sensor_temp
- ft_temp
) * 10 / tmdev
->chip
->scale
);
260 if (cdata
& ~TEMP_CALIB_MASK
) {
262 * Calibration value more than 12-bit, but calibration
263 * register is 12-bit. In this case, ths hardware can
264 * still work without calibration, although the data
265 * won't be so accurate.
267 dev_warn(dev
, "sensor%d is not calibrated.\n", i
);
271 offset
= (i
% 2) * 16;
272 regmap_update_bits(tmdev
->regmap
,
273 SUN50I_H6_THS_TEMP_CALIB
+ (i
/ 2 * 4),
281 static int sun8i_ths_calibrate(struct ths_device
*tmdev
)
283 struct nvmem_cell
*calcell
;
284 struct device
*dev
= tmdev
->dev
;
289 calcell
= devm_nvmem_cell_get(dev
, "calibration");
290 if (IS_ERR(calcell
)) {
291 if (PTR_ERR(calcell
) == -EPROBE_DEFER
)
292 return -EPROBE_DEFER
;
294 * Even if the external calibration data stored in sid is
295 * not accessible, the THS hardware can still work, although
296 * the data won't be so accurate.
298 * The default value of calibration register is 0x800 for
299 * every sensor, and the calibration value is usually 0x7xx
300 * or 0x8xx, so they won't be away from the default value
303 * So here we do not return error if the calibartion data is
304 * not available, except the probe needs deferring.
309 caldata
= nvmem_cell_read(calcell
, &callen
);
310 if (IS_ERR(caldata
)) {
311 ret
= PTR_ERR(caldata
);
315 tmdev
->chip
->calibrate(tmdev
, caldata
, callen
);
322 static int sun8i_ths_resource_init(struct ths_device
*tmdev
)
324 struct device
*dev
= tmdev
->dev
;
325 struct platform_device
*pdev
= to_platform_device(dev
);
329 base
= devm_platform_ioremap_resource(pdev
, 0);
331 return PTR_ERR(base
);
333 tmdev
->regmap
= devm_regmap_init_mmio(dev
, base
, &config
);
334 if (IS_ERR(tmdev
->regmap
))
335 return PTR_ERR(tmdev
->regmap
);
337 if (tmdev
->chip
->has_bus_clk_reset
) {
338 tmdev
->reset
= devm_reset_control_get(dev
, NULL
);
339 if (IS_ERR(tmdev
->reset
))
340 return PTR_ERR(tmdev
->reset
);
342 tmdev
->bus_clk
= devm_clk_get(&pdev
->dev
, "bus");
343 if (IS_ERR(tmdev
->bus_clk
))
344 return PTR_ERR(tmdev
->bus_clk
);
347 if (tmdev
->chip
->has_mod_clk
) {
348 tmdev
->mod_clk
= devm_clk_get(&pdev
->dev
, "mod");
349 if (IS_ERR(tmdev
->mod_clk
))
350 return PTR_ERR(tmdev
->mod_clk
);
353 ret
= reset_control_deassert(tmdev
->reset
);
357 ret
= clk_prepare_enable(tmdev
->bus_clk
);
361 ret
= clk_set_rate(tmdev
->mod_clk
, 24000000);
365 ret
= clk_prepare_enable(tmdev
->mod_clk
);
369 ret
= sun8i_ths_calibrate(tmdev
);
376 clk_disable_unprepare(tmdev
->mod_clk
);
378 clk_disable_unprepare(tmdev
->bus_clk
);
380 reset_control_assert(tmdev
->reset
);
385 static int sun8i_h3_thermal_init(struct ths_device
*tmdev
)
389 /* average over 4 samples */
390 regmap_write(tmdev
->regmap
, SUN8I_THS_MFC
,
391 SUN50I_THS_FILTER_EN
|
392 SUN50I_THS_FILTER_TYPE(1));
398 * x = period * clkin / 4096 / filter_samples - 1
401 val
= GENMASK(7 + tmdev
->chip
->sensor_num
, 8);
402 regmap_write(tmdev
->regmap
, SUN8I_THS_IC
,
403 SUN50I_H6_THS_PC_TEMP_PERIOD(365) | val
);
408 * x = T_acq * clkin - 1
411 regmap_write(tmdev
->regmap
, SUN8I_THS_CTRL0
,
412 SUN8I_THS_CTRL0_T_ACQ0(479));
413 val
= GENMASK(tmdev
->chip
->sensor_num
- 1, 0);
414 regmap_write(tmdev
->regmap
, SUN8I_THS_CTRL2
,
415 SUN8I_THS_CTRL2_T_ACQ1(479) | val
);
421 * Without this undocummented value, the returned temperatures would
422 * be higher than real ones by about 20C.
424 #define SUN50I_H6_CTRL0_UNK 0x0000002f
426 static int sun50i_h6_thermal_init(struct ths_device
*tmdev
)
434 * x = T_acq * clkin - 1
437 regmap_write(tmdev
->regmap
, SUN50I_THS_CTRL0
,
438 SUN50I_H6_CTRL0_UNK
| SUN50I_THS_CTRL0_T_ACQ(479));
439 /* average over 4 samples */
440 regmap_write(tmdev
->regmap
, SUN50I_H6_THS_MFC
,
441 SUN50I_THS_FILTER_EN
|
442 SUN50I_THS_FILTER_TYPE(1));
448 * x = period * clkin / 4096 / filter_samples - 1
451 regmap_write(tmdev
->regmap
, SUN50I_H6_THS_PC
,
452 SUN50I_H6_THS_PC_TEMP_PERIOD(365));
454 val
= GENMASK(tmdev
->chip
->sensor_num
- 1, 0);
455 regmap_write(tmdev
->regmap
, SUN50I_H6_THS_ENABLE
, val
);
456 /* thermal data interrupt enable */
457 val
= GENMASK(tmdev
->chip
->sensor_num
- 1, 0);
458 regmap_write(tmdev
->regmap
, SUN50I_H6_THS_DIC
, val
);
463 static int sun8i_ths_register(struct ths_device
*tmdev
)
467 for (i
= 0; i
< tmdev
->chip
->sensor_num
; i
++) {
468 tmdev
->sensor
[i
].tmdev
= tmdev
;
469 tmdev
->sensor
[i
].id
= i
;
470 tmdev
->sensor
[i
].tzd
=
471 devm_thermal_zone_of_sensor_register(tmdev
->dev
,
475 if (IS_ERR(tmdev
->sensor
[i
].tzd
))
476 return PTR_ERR(tmdev
->sensor
[i
].tzd
);
478 if (devm_thermal_add_hwmon_sysfs(tmdev
->sensor
[i
].tzd
))
480 "Failed to add hwmon sysfs attributes\n");
486 static int sun8i_ths_probe(struct platform_device
*pdev
)
488 struct ths_device
*tmdev
;
489 struct device
*dev
= &pdev
->dev
;
492 tmdev
= devm_kzalloc(dev
, sizeof(*tmdev
), GFP_KERNEL
);
497 tmdev
->chip
= of_device_get_match_data(&pdev
->dev
);
501 platform_set_drvdata(pdev
, tmdev
);
503 ret
= sun8i_ths_resource_init(tmdev
);
507 irq
= platform_get_irq(pdev
, 0);
511 ret
= tmdev
->chip
->init(tmdev
);
515 ret
= sun8i_ths_register(tmdev
);
520 * Avoid entering the interrupt handler, the thermal device is not
521 * registered yet, we deffer the registration of the interrupt to
524 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
526 IRQF_ONESHOT
, "ths", tmdev
);
533 static int sun8i_ths_remove(struct platform_device
*pdev
)
535 struct ths_device
*tmdev
= platform_get_drvdata(pdev
);
537 clk_disable_unprepare(tmdev
->mod_clk
);
538 clk_disable_unprepare(tmdev
->bus_clk
);
539 reset_control_assert(tmdev
->reset
);
544 static const struct ths_thermal_chip sun8i_a83t_ths
= {
548 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
549 .calibrate
= sun8i_h3_ths_calibrate
,
550 .init
= sun8i_h3_thermal_init
,
551 .irq_ack
= sun8i_h3_irq_ack
,
552 .calc_temp
= sun8i_ths_calc_temp
,
555 static const struct ths_thermal_chip sun8i_h3_ths
= {
560 .has_bus_clk_reset
= true,
561 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
562 .calibrate
= sun8i_h3_ths_calibrate
,
563 .init
= sun8i_h3_thermal_init
,
564 .irq_ack
= sun8i_h3_irq_ack
,
565 .calc_temp
= sun8i_ths_calc_temp
,
568 static const struct ths_thermal_chip sun8i_r40_ths
= {
573 .has_bus_clk_reset
= true,
574 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
575 .calibrate
= sun8i_h3_ths_calibrate
,
576 .init
= sun8i_h3_thermal_init
,
577 .irq_ack
= sun8i_h3_irq_ack
,
578 .calc_temp
= sun8i_ths_calc_temp
,
581 static const struct ths_thermal_chip sun50i_a64_ths
= {
586 .has_bus_clk_reset
= true,
587 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
588 .calibrate
= sun8i_h3_ths_calibrate
,
589 .init
= sun8i_h3_thermal_init
,
590 .irq_ack
= sun8i_h3_irq_ack
,
591 .calc_temp
= sun8i_ths_calc_temp
,
594 static const struct ths_thermal_chip sun50i_a100_ths
= {
596 .has_bus_clk_reset
= true,
597 .ft_deviation
= 8000,
600 .temp_data_base
= SUN50I_H6_THS_TEMP_DATA
,
601 .calibrate
= sun50i_h6_ths_calibrate
,
602 .init
= sun50i_h6_thermal_init
,
603 .irq_ack
= sun50i_h6_irq_ack
,
604 .calc_temp
= sun8i_ths_calc_temp
,
607 static const struct ths_thermal_chip sun50i_h5_ths
= {
610 .has_bus_clk_reset
= true,
611 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
612 .calibrate
= sun8i_h3_ths_calibrate
,
613 .init
= sun8i_h3_thermal_init
,
614 .irq_ack
= sun8i_h3_irq_ack
,
615 .calc_temp
= sun50i_h5_calc_temp
,
618 static const struct ths_thermal_chip sun50i_h6_ths
= {
620 .has_bus_clk_reset
= true,
621 .ft_deviation
= 7000,
624 .temp_data_base
= SUN50I_H6_THS_TEMP_DATA
,
625 .calibrate
= sun50i_h6_ths_calibrate
,
626 .init
= sun50i_h6_thermal_init
,
627 .irq_ack
= sun50i_h6_irq_ack
,
628 .calc_temp
= sun8i_ths_calc_temp
,
631 static const struct of_device_id of_ths_match
[] = {
632 { .compatible
= "allwinner,sun8i-a83t-ths", .data
= &sun8i_a83t_ths
},
633 { .compatible
= "allwinner,sun8i-h3-ths", .data
= &sun8i_h3_ths
},
634 { .compatible
= "allwinner,sun8i-r40-ths", .data
= &sun8i_r40_ths
},
635 { .compatible
= "allwinner,sun50i-a64-ths", .data
= &sun50i_a64_ths
},
636 { .compatible
= "allwinner,sun50i-a100-ths", .data
= &sun50i_a100_ths
},
637 { .compatible
= "allwinner,sun50i-h5-ths", .data
= &sun50i_h5_ths
},
638 { .compatible
= "allwinner,sun50i-h6-ths", .data
= &sun50i_h6_ths
},
641 MODULE_DEVICE_TABLE(of
, of_ths_match
);
643 static struct platform_driver ths_driver
= {
644 .probe
= sun8i_ths_probe
,
645 .remove
= sun8i_ths_remove
,
647 .name
= "sun8i-thermal",
648 .of_match_table
= of_ths_match
,
651 module_platform_driver(ths_driver
);
653 MODULE_DESCRIPTION("Thermal sensor driver for Allwinner SOC");
654 MODULE_LICENSE("GPL v2");