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/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>
23 #include "thermal_hwmon.h"
25 #define MAX_SENSOR_NUM 4
27 #define FT_TEMP_MASK GENMASK(11, 0)
28 #define TEMP_CALIB_MASK GENMASK(11, 0)
29 #define CALIBRATE_DEFAULT 0x800
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
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
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)
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)
58 /* millidegree celsius */
61 struct ths_device
*tmdev
;
62 struct thermal_zone_device
*tzd
;
66 struct ths_thermal_chip
{
68 bool has_bus_clk_reset
;
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
,
83 const struct ths_thermal_chip
*chip
;
85 struct regmap
*regmap
;
86 struct reset_control
*reset
;
89 struct tsensor sensor
[MAX_SENSOR_NUM
];
92 /* Temp Unit: millidegree Celsius */
93 static int sun8i_ths_calc_temp(struct ths_device
*tmdev
,
96 return tmdev
->chip
->offset
- (reg
* tmdev
->chip
->scale
/ 10);
99 static int sun50i_h5_calc_temp(struct ths_device
*tmdev
,
103 return -1191 * reg
/ 10 + 223000;
105 return -1452 * reg
/ 10 + 259000;
107 return -1590 * reg
/ 10 + 276000;
110 static int sun8i_ths_get_temp(void *data
, int *temp
)
112 struct tsensor
*s
= data
;
113 struct ths_device
*tmdev
= s
->tmdev
;
116 regmap_read(tmdev
->regmap
, tmdev
->chip
->temp_data_base
+
119 /* ths have no data yet */
123 *temp
= tmdev
->chip
->calc_temp(tmdev
, s
->id
, val
);
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.
132 *temp
+= tmdev
->chip
->ft_deviation
;
137 static const struct thermal_zone_of_device_ops ths_ops
= {
138 .get_temp
= sun8i_ths_get_temp
,
141 static const struct regmap_config config
= {
146 .max_register
= 0xfc,
149 static int sun8i_h3_irq_ack(struct ths_device
*tmdev
)
151 int i
, state
, ret
= 0;
153 regmap_read(tmdev
->regmap
, SUN8I_THS_IS
, &state
);
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
));
166 static int sun50i_h6_irq_ack(struct ths_device
*tmdev
)
168 int i
, state
, ret
= 0;
170 regmap_read(tmdev
->regmap
, SUN50I_H6_THS_DIS
, &state
);
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
));
183 static irqreturn_t
sun8i_irq_thread(int irq
, void *data
)
185 struct ths_device
*tmdev
= data
;
188 state
= tmdev
->chip
->irq_ack(tmdev
);
190 for (i
= 0; i
< tmdev
->chip
->sensor_num
; i
++) {
192 thermal_zone_device_update(tmdev
->sensor
[i
].tzd
,
193 THERMAL_EVENT_UNSPECIFIED
);
199 static int sun8i_h3_ths_calibrate(struct ths_device
*tmdev
,
200 u16
*caldata
, int callen
)
204 if (!caldata
[0] || callen
< 2 * tmdev
->chip
->sensor_num
)
207 for (i
= 0; i
< tmdev
->chip
->sensor_num
; i
++) {
208 int offset
= (i
% 2) << 4;
210 regmap_update_bits(tmdev
->regmap
,
211 SUN8I_THS_TEMP_CALIB
+ (4 * (i
>> 1)),
213 caldata
[i
] << offset
);
219 static int sun50i_h6_ths_calibrate(struct ths_device
*tmdev
,
220 u16
*caldata
, int callen
)
222 struct device
*dev
= tmdev
->dev
;
225 if (!caldata
[0] || callen
< 2 + 2 * tmdev
->chip
->sensor_num
)
232 * +-------+-------+-------+
233 * |temp| |sensor0|sensor1|
234 * +-------+-------+-------+
236 * The calibration data on the H6 is the ambient temperature and
237 * sensor values that are filled during the factory test stage.
239 * The unit of stored FT temperature is 0.1 degreee celusis.
241 * We need to calculate a delta between measured and caluclated
242 * register values and this will become a calibration offset.
244 ft_temp
= (caldata
[0] & FT_TEMP_MASK
) * 100;
246 for (i
= 0; i
< tmdev
->chip
->sensor_num
; i
++) {
247 int sensor_reg
= caldata
[i
+ 1];
249 int sensor_temp
= tmdev
->chip
->calc_temp(tmdev
, i
, sensor_reg
);
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)
257 cdata
= CALIBRATE_DEFAULT
-
258 ((sensor_temp
- ft_temp
) * 10 / tmdev
->chip
->scale
);
259 if (cdata
& ~TEMP_CALIB_MASK
) {
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.
266 dev_warn(dev
, "sensor%d is not calibrated.\n", i
);
270 offset
= (i
% 2) * 16;
271 regmap_update_bits(tmdev
->regmap
,
272 SUN50I_H6_THS_TEMP_CALIB
+ (i
/ 2 * 4),
280 static int sun8i_ths_calibrate(struct ths_device
*tmdev
)
282 struct nvmem_cell
*calcell
;
283 struct device
*dev
= tmdev
->dev
;
288 calcell
= devm_nvmem_cell_get(dev
, "calibration");
289 if (IS_ERR(calcell
)) {
290 if (PTR_ERR(calcell
) == -EPROBE_DEFER
)
291 return -EPROBE_DEFER
;
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.
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
302 * So here we do not return error if the calibartion data is
303 * not available, except the probe needs deferring.
308 caldata
= nvmem_cell_read(calcell
, &callen
);
309 if (IS_ERR(caldata
)) {
310 ret
= PTR_ERR(caldata
);
314 tmdev
->chip
->calibrate(tmdev
, caldata
, callen
);
321 static int sun8i_ths_resource_init(struct ths_device
*tmdev
)
323 struct device
*dev
= tmdev
->dev
;
324 struct platform_device
*pdev
= to_platform_device(dev
);
328 base
= devm_platform_ioremap_resource(pdev
, 0);
330 return PTR_ERR(base
);
332 tmdev
->regmap
= devm_regmap_init_mmio(dev
, base
, &config
);
333 if (IS_ERR(tmdev
->regmap
))
334 return PTR_ERR(tmdev
->regmap
);
336 if (tmdev
->chip
->has_bus_clk_reset
) {
337 tmdev
->reset
= devm_reset_control_get(dev
, NULL
);
338 if (IS_ERR(tmdev
->reset
))
339 return PTR_ERR(tmdev
->reset
);
341 tmdev
->bus_clk
= devm_clk_get(&pdev
->dev
, "bus");
342 if (IS_ERR(tmdev
->bus_clk
))
343 return PTR_ERR(tmdev
->bus_clk
);
346 if (tmdev
->chip
->has_mod_clk
) {
347 tmdev
->mod_clk
= devm_clk_get(&pdev
->dev
, "mod");
348 if (IS_ERR(tmdev
->mod_clk
))
349 return PTR_ERR(tmdev
->mod_clk
);
352 ret
= reset_control_deassert(tmdev
->reset
);
356 ret
= clk_prepare_enable(tmdev
->bus_clk
);
360 ret
= clk_set_rate(tmdev
->mod_clk
, 24000000);
364 ret
= clk_prepare_enable(tmdev
->mod_clk
);
368 ret
= sun8i_ths_calibrate(tmdev
);
375 clk_disable_unprepare(tmdev
->mod_clk
);
377 clk_disable_unprepare(tmdev
->bus_clk
);
379 reset_control_assert(tmdev
->reset
);
384 static int sun8i_h3_thermal_init(struct ths_device
*tmdev
)
388 /* average over 4 samples */
389 regmap_write(tmdev
->regmap
, SUN8I_THS_MFC
,
390 SUN50I_THS_FILTER_EN
|
391 SUN50I_THS_FILTER_TYPE(1));
397 * x = period * clkin / 4096 / filter_samples - 1
400 val
= GENMASK(7 + tmdev
->chip
->sensor_num
, 8);
401 regmap_write(tmdev
->regmap
, SUN8I_THS_IC
,
402 SUN50I_H6_THS_PC_TEMP_PERIOD(365) | val
);
407 * x = T_acq * clkin - 1
410 regmap_write(tmdev
->regmap
, SUN8I_THS_CTRL0
,
411 SUN8I_THS_CTRL0_T_ACQ0(479));
412 val
= GENMASK(tmdev
->chip
->sensor_num
- 1, 0);
413 regmap_write(tmdev
->regmap
, SUN8I_THS_CTRL2
,
414 SUN8I_THS_CTRL2_T_ACQ1(479) | val
);
420 * Without this undocummented value, the returned temperatures would
421 * be higher than real ones by about 20C.
423 #define SUN50I_H6_CTRL0_UNK 0x0000002f
425 static int sun50i_h6_thermal_init(struct ths_device
*tmdev
)
433 * x = T_acq * clkin - 1
436 regmap_write(tmdev
->regmap
, SUN50I_THS_CTRL0
,
437 SUN50I_H6_CTRL0_UNK
| SUN50I_THS_CTRL0_T_ACQ(479));
438 /* average over 4 samples */
439 regmap_write(tmdev
->regmap
, SUN50I_H6_THS_MFC
,
440 SUN50I_THS_FILTER_EN
|
441 SUN50I_THS_FILTER_TYPE(1));
447 * x = period * clkin / 4096 / filter_samples - 1
450 regmap_write(tmdev
->regmap
, SUN50I_H6_THS_PC
,
451 SUN50I_H6_THS_PC_TEMP_PERIOD(365));
453 val
= GENMASK(tmdev
->chip
->sensor_num
- 1, 0);
454 regmap_write(tmdev
->regmap
, SUN50I_H6_THS_ENABLE
, val
);
455 /* thermal data interrupt enable */
456 val
= GENMASK(tmdev
->chip
->sensor_num
- 1, 0);
457 regmap_write(tmdev
->regmap
, SUN50I_H6_THS_DIC
, val
);
462 static int sun8i_ths_register(struct ths_device
*tmdev
)
466 for (i
= 0; i
< tmdev
->chip
->sensor_num
; i
++) {
467 tmdev
->sensor
[i
].tmdev
= tmdev
;
468 tmdev
->sensor
[i
].id
= i
;
469 tmdev
->sensor
[i
].tzd
=
470 devm_thermal_zone_of_sensor_register(tmdev
->dev
,
474 if (IS_ERR(tmdev
->sensor
[i
].tzd
))
475 return PTR_ERR(tmdev
->sensor
[i
].tzd
);
477 if (devm_thermal_add_hwmon_sysfs(tmdev
->sensor
[i
].tzd
))
479 "Failed to add hwmon sysfs attributes\n");
485 static int sun8i_ths_probe(struct platform_device
*pdev
)
487 struct ths_device
*tmdev
;
488 struct device
*dev
= &pdev
->dev
;
491 tmdev
= devm_kzalloc(dev
, sizeof(*tmdev
), GFP_KERNEL
);
496 tmdev
->chip
= of_device_get_match_data(&pdev
->dev
);
500 platform_set_drvdata(pdev
, tmdev
);
502 ret
= sun8i_ths_resource_init(tmdev
);
506 irq
= platform_get_irq(pdev
, 0);
510 ret
= tmdev
->chip
->init(tmdev
);
514 ret
= sun8i_ths_register(tmdev
);
519 * Avoid entering the interrupt handler, the thermal device is not
520 * registered yet, we deffer the registration of the interrupt to
523 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
525 IRQF_ONESHOT
, "ths", tmdev
);
532 static int sun8i_ths_remove(struct platform_device
*pdev
)
534 struct ths_device
*tmdev
= platform_get_drvdata(pdev
);
536 clk_disable_unprepare(tmdev
->mod_clk
);
537 clk_disable_unprepare(tmdev
->bus_clk
);
538 reset_control_assert(tmdev
->reset
);
543 static const struct ths_thermal_chip sun8i_a83t_ths
= {
547 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
548 .calibrate
= sun8i_h3_ths_calibrate
,
549 .init
= sun8i_h3_thermal_init
,
550 .irq_ack
= sun8i_h3_irq_ack
,
551 .calc_temp
= sun8i_ths_calc_temp
,
554 static const struct ths_thermal_chip sun8i_h3_ths
= {
559 .has_bus_clk_reset
= true,
560 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
561 .calibrate
= sun8i_h3_ths_calibrate
,
562 .init
= sun8i_h3_thermal_init
,
563 .irq_ack
= sun8i_h3_irq_ack
,
564 .calc_temp
= sun8i_ths_calc_temp
,
567 static const struct ths_thermal_chip sun8i_r40_ths
= {
572 .has_bus_clk_reset
= true,
573 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
574 .calibrate
= sun8i_h3_ths_calibrate
,
575 .init
= sun8i_h3_thermal_init
,
576 .irq_ack
= sun8i_h3_irq_ack
,
577 .calc_temp
= sun8i_ths_calc_temp
,
580 static const struct ths_thermal_chip sun50i_a64_ths
= {
585 .has_bus_clk_reset
= true,
586 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
587 .calibrate
= sun8i_h3_ths_calibrate
,
588 .init
= sun8i_h3_thermal_init
,
589 .irq_ack
= sun8i_h3_irq_ack
,
590 .calc_temp
= sun8i_ths_calc_temp
,
593 static const struct ths_thermal_chip sun50i_h5_ths
= {
596 .has_bus_clk_reset
= true,
597 .temp_data_base
= SUN8I_THS_TEMP_DATA
,
598 .calibrate
= sun8i_h3_ths_calibrate
,
599 .init
= sun8i_h3_thermal_init
,
600 .irq_ack
= sun8i_h3_irq_ack
,
601 .calc_temp
= sun50i_h5_calc_temp
,
604 static const struct ths_thermal_chip sun50i_h6_ths
= {
606 .has_bus_clk_reset
= true,
607 .ft_deviation
= 7000,
610 .temp_data_base
= SUN50I_H6_THS_TEMP_DATA
,
611 .calibrate
= sun50i_h6_ths_calibrate
,
612 .init
= sun50i_h6_thermal_init
,
613 .irq_ack
= sun50i_h6_irq_ack
,
614 .calc_temp
= sun8i_ths_calc_temp
,
617 static const struct of_device_id of_ths_match
[] = {
618 { .compatible
= "allwinner,sun8i-a83t-ths", .data
= &sun8i_a83t_ths
},
619 { .compatible
= "allwinner,sun8i-h3-ths", .data
= &sun8i_h3_ths
},
620 { .compatible
= "allwinner,sun8i-r40-ths", .data
= &sun8i_r40_ths
},
621 { .compatible
= "allwinner,sun50i-a64-ths", .data
= &sun50i_a64_ths
},
622 { .compatible
= "allwinner,sun50i-h5-ths", .data
= &sun50i_h5_ths
},
623 { .compatible
= "allwinner,sun50i-h6-ths", .data
= &sun50i_h6_ths
},
626 MODULE_DEVICE_TABLE(of
, of_ths_match
);
628 static struct platform_driver ths_driver
= {
629 .probe
= sun8i_ths_probe
,
630 .remove
= sun8i_ths_remove
,
632 .name
= "sun8i-thermal",
633 .of_match_table
= of_ths_match
,
636 module_platform_driver(ths_driver
);
638 MODULE_DESCRIPTION("Thermal sensor driver for Allwinner SOC");
639 MODULE_LICENSE("GPL v2");