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>
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)
60 struct ths_device *tmdev;
61 struct thermal_zone_device *tzd;
65 struct ths_thermal_chip {
67 bool has_bus_clk_reset;
73 int (*calibrate)(struct ths_device *tmdev,
74 u16 *caldata, int callen);
75 int (*init)(struct ths_device *tmdev);
76 unsigned long (*irq_ack)(struct ths_device *tmdev);
77 int (*calc_temp)(struct ths_device *tmdev,
82 const struct ths_thermal_chip *chip;
84 struct regmap *regmap;
85 struct reset_control *reset;
88 struct tsensor sensor[MAX_SENSOR_NUM];
91 /* Temp Unit: millidegree Celsius */
92 static int sun8i_ths_calc_temp(struct ths_device *tmdev,
95 return tmdev->chip->offset - (reg * tmdev->chip->scale / 10);
98 static int sun50i_h5_calc_temp(struct ths_device *tmdev,
102 return -1191 * reg / 10 + 223000;
104 return -1452 * reg / 10 + 259000;
106 return -1590 * reg / 10 + 276000;
109 static int sun8i_ths_get_temp(struct thermal_zone_device *tz, int *temp)
111 struct tsensor *s = thermal_zone_device_priv(tz);
112 struct ths_device *tmdev = s->tmdev;
115 regmap_read(tmdev->regmap, tmdev->chip->temp_data_base +
118 /* ths have no data yet */
122 *temp = tmdev->chip->calc_temp(tmdev, s->id, val);
124 * According to the original sdk, there are some platforms(rarely)
125 * that add a fixed offset value after calculating the temperature
126 * value. We can't simply put it on the formula for calculating the
127 * temperature above, because the formula for calculating the
128 * temperature above is also used when the sensor is calibrated. If
129 * do this, the correct calibration formula is hard to know.
131 *temp += tmdev->chip->ft_deviation;
136 static const struct thermal_zone_device_ops ths_ops = {
137 .get_temp = sun8i_ths_get_temp,
140 static const struct regmap_config config = {
145 .max_register = 0xfc,
148 static unsigned long sun8i_h3_irq_ack(struct ths_device *tmdev)
150 unsigned long irq_bitmap = 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));
159 bitmap_set(&irq_bitmap, i, 1);
166 static unsigned long sun50i_h6_irq_ack(struct ths_device *tmdev)
168 unsigned long irq_bitmap = 0;
171 regmap_read(tmdev->regmap, SUN50I_H6_THS_DIS, &state);
173 for (i = 0; i < tmdev->chip->sensor_num; i++) {
174 if (state & SUN50I_H6_THS_DATA_IRQ_STS(i)) {
175 regmap_write(tmdev->regmap, SUN50I_H6_THS_DIS,
176 SUN50I_H6_THS_DATA_IRQ_STS(i));
177 bitmap_set(&irq_bitmap, i, 1);
184 static irqreturn_t sun8i_irq_thread(int irq, void *data)
186 struct ths_device *tmdev = data;
187 unsigned long irq_bitmap = tmdev->chip->irq_ack(tmdev);
190 for_each_set_bit(i, &irq_bitmap, tmdev->chip->sensor_num) {
191 thermal_zone_device_update(tmdev->sensor[i].tzd,
192 THERMAL_EVENT_UNSPECIFIED);
198 static int sun8i_h3_ths_calibrate(struct ths_device *tmdev,
199 u16 *caldata, int callen)
203 if (!caldata[0] || callen < 2 * tmdev->chip->sensor_num)
206 for (i = 0; i < tmdev->chip->sensor_num; i++) {
207 int offset = (i % 2) << 4;
209 regmap_update_bits(tmdev->regmap,
210 SUN8I_THS_TEMP_CALIB + (4 * (i >> 1)),
211 TEMP_CALIB_MASK << offset,
212 caldata[i] << offset);
218 static int sun50i_h6_ths_calibrate(struct ths_device *tmdev,
219 u16 *caldata, int callen)
221 struct device *dev = tmdev->dev;
224 if (!caldata[0] || callen < 2 + 2 * tmdev->chip->sensor_num)
231 * +-------+-------+-------+
232 * |temp| |sensor0|sensor1|
233 * +-------+-------+-------+
235 * The calibration data on the H6 is the ambient temperature and
236 * sensor values that are filled during the factory test stage.
238 * The unit of stored FT temperature is 0.1 degree celsius.
240 * We need to calculate a delta between measured and caluclated
241 * register values and this will become a calibration offset.
243 ft_temp = (caldata[0] & FT_TEMP_MASK) * 100;
245 for (i = 0; i < tmdev->chip->sensor_num; i++) {
246 int sensor_reg = caldata[i + 1] & TEMP_CALIB_MASK;
248 int sensor_temp = tmdev->chip->calc_temp(tmdev, i, sensor_reg);
251 * Calibration data is CALIBRATE_DEFAULT - (calculated
252 * temperature from sensor reading at factory temperature
253 * minus actual factory temperature) * 14.88 (scale from
254 * temperature to register values)
256 cdata = CALIBRATE_DEFAULT -
257 ((sensor_temp - ft_temp) * 10 / tmdev->chip->scale);
258 if (cdata & ~TEMP_CALIB_MASK) {
260 * Calibration value more than 12-bit, but calibration
261 * register is 12-bit. In this case, ths hardware can
262 * still work without calibration, although the data
263 * won't be so accurate.
265 dev_warn(dev, "sensor%d is not calibrated.\n", i);
269 offset = (i % 2) * 16;
270 regmap_update_bits(tmdev->regmap,
271 SUN50I_H6_THS_TEMP_CALIB + (i / 2 * 4),
272 TEMP_CALIB_MASK << offset,
279 static int sun8i_ths_calibrate(struct ths_device *tmdev)
281 struct nvmem_cell *calcell;
282 struct device *dev = tmdev->dev;
287 calcell = nvmem_cell_get(dev, "calibration");
288 if (IS_ERR(calcell)) {
289 if (PTR_ERR(calcell) == -EPROBE_DEFER)
290 return -EPROBE_DEFER;
292 * Even if the external calibration data stored in sid is
293 * not accessible, the THS hardware can still work, although
294 * the data won't be so accurate.
296 * The default value of calibration register is 0x800 for
297 * every sensor, and the calibration value is usually 0x7xx
298 * or 0x8xx, so they won't be away from the default value
301 * So here we do not return error if the calibration data is
302 * not available, except the probe needs deferring.
307 caldata = nvmem_cell_read(calcell, &callen);
308 if (IS_ERR(caldata)) {
309 ret = PTR_ERR(caldata);
313 tmdev->chip->calibrate(tmdev, caldata, callen);
317 if (!IS_ERR(calcell))
318 nvmem_cell_put(calcell);
322 static void sun8i_ths_reset_control_assert(void *data)
324 reset_control_assert(data);
327 static int sun8i_ths_resource_init(struct ths_device *tmdev)
329 struct device *dev = tmdev->dev;
330 struct platform_device *pdev = to_platform_device(dev);
334 base = devm_platform_ioremap_resource(pdev, 0);
336 return PTR_ERR(base);
338 tmdev->regmap = devm_regmap_init_mmio(dev, base, &config);
339 if (IS_ERR(tmdev->regmap))
340 return PTR_ERR(tmdev->regmap);
342 if (tmdev->chip->has_bus_clk_reset) {
343 tmdev->reset = devm_reset_control_get(dev, NULL);
344 if (IS_ERR(tmdev->reset))
345 return PTR_ERR(tmdev->reset);
347 ret = reset_control_deassert(tmdev->reset);
351 ret = devm_add_action_or_reset(dev, sun8i_ths_reset_control_assert,
356 tmdev->bus_clk = devm_clk_get_enabled(&pdev->dev, "bus");
357 if (IS_ERR(tmdev->bus_clk))
358 return PTR_ERR(tmdev->bus_clk);
361 if (tmdev->chip->has_mod_clk) {
362 tmdev->mod_clk = devm_clk_get_enabled(&pdev->dev, "mod");
363 if (IS_ERR(tmdev->mod_clk))
364 return PTR_ERR(tmdev->mod_clk);
367 ret = clk_set_rate(tmdev->mod_clk, 24000000);
371 ret = sun8i_ths_calibrate(tmdev);
378 static int sun8i_h3_thermal_init(struct ths_device *tmdev)
382 /* average over 4 samples */
383 regmap_write(tmdev->regmap, SUN8I_THS_MFC,
384 SUN50I_THS_FILTER_EN |
385 SUN50I_THS_FILTER_TYPE(1));
391 * x = period * clkin / 4096 / filter_samples - 1
394 val = GENMASK(7 + tmdev->chip->sensor_num, 8);
395 regmap_write(tmdev->regmap, SUN8I_THS_IC,
396 SUN50I_H6_THS_PC_TEMP_PERIOD(365) | val);
401 * x = T_acq * clkin - 1
404 regmap_write(tmdev->regmap, SUN8I_THS_CTRL0,
405 SUN8I_THS_CTRL0_T_ACQ0(479));
406 val = GENMASK(tmdev->chip->sensor_num - 1, 0);
407 regmap_write(tmdev->regmap, SUN8I_THS_CTRL2,
408 SUN8I_THS_CTRL2_T_ACQ1(479) | val);
414 * Without this undocumented value, the returned temperatures would
415 * be higher than real ones by about 20C.
417 #define SUN50I_H6_CTRL0_UNK 0x0000002f
419 static int sun50i_h6_thermal_init(struct ths_device *tmdev)
427 * x = T_acq * clkin - 1
430 regmap_write(tmdev->regmap, SUN50I_THS_CTRL0,
431 SUN50I_H6_CTRL0_UNK | SUN50I_THS_CTRL0_T_ACQ(479));
432 /* average over 4 samples */
433 regmap_write(tmdev->regmap, SUN50I_H6_THS_MFC,
434 SUN50I_THS_FILTER_EN |
435 SUN50I_THS_FILTER_TYPE(1));
441 * x = period * clkin / 4096 / filter_samples - 1
444 regmap_write(tmdev->regmap, SUN50I_H6_THS_PC,
445 SUN50I_H6_THS_PC_TEMP_PERIOD(365));
447 val = GENMASK(tmdev->chip->sensor_num - 1, 0);
448 regmap_write(tmdev->regmap, SUN50I_H6_THS_ENABLE, val);
449 /* thermal data interrupt enable */
450 val = GENMASK(tmdev->chip->sensor_num - 1, 0);
451 regmap_write(tmdev->regmap, SUN50I_H6_THS_DIC, val);
456 static int sun8i_ths_register(struct ths_device *tmdev)
460 for (i = 0; i < tmdev->chip->sensor_num; i++) {
461 tmdev->sensor[i].tmdev = tmdev;
462 tmdev->sensor[i].id = i;
463 tmdev->sensor[i].tzd =
464 devm_thermal_of_zone_register(tmdev->dev,
468 if (IS_ERR(tmdev->sensor[i].tzd))
469 return PTR_ERR(tmdev->sensor[i].tzd);
471 devm_thermal_add_hwmon_sysfs(tmdev->dev, tmdev->sensor[i].tzd);
477 static int sun8i_ths_probe(struct platform_device *pdev)
479 struct ths_device *tmdev;
480 struct device *dev = &pdev->dev;
483 tmdev = devm_kzalloc(dev, sizeof(*tmdev), GFP_KERNEL);
488 tmdev->chip = of_device_get_match_data(&pdev->dev);
492 ret = sun8i_ths_resource_init(tmdev);
496 irq = platform_get_irq(pdev, 0);
500 ret = tmdev->chip->init(tmdev);
504 ret = sun8i_ths_register(tmdev);
509 * Avoid entering the interrupt handler, the thermal device is not
510 * registered yet, we deffer the registration of the interrupt to
513 ret = devm_request_threaded_irq(dev, irq, NULL,
515 IRQF_ONESHOT, "ths", tmdev);
522 static const struct ths_thermal_chip sun8i_a83t_ths = {
526 .temp_data_base = SUN8I_THS_TEMP_DATA,
527 .calibrate = sun8i_h3_ths_calibrate,
528 .init = sun8i_h3_thermal_init,
529 .irq_ack = sun8i_h3_irq_ack,
530 .calc_temp = sun8i_ths_calc_temp,
533 static const struct ths_thermal_chip sun8i_h3_ths = {
538 .has_bus_clk_reset = true,
539 .temp_data_base = SUN8I_THS_TEMP_DATA,
540 .calibrate = sun8i_h3_ths_calibrate,
541 .init = sun8i_h3_thermal_init,
542 .irq_ack = sun8i_h3_irq_ack,
543 .calc_temp = sun8i_ths_calc_temp,
546 static const struct ths_thermal_chip sun8i_r40_ths = {
551 .has_bus_clk_reset = true,
552 .temp_data_base = SUN8I_THS_TEMP_DATA,
553 .calibrate = sun8i_h3_ths_calibrate,
554 .init = sun8i_h3_thermal_init,
555 .irq_ack = sun8i_h3_irq_ack,
556 .calc_temp = sun8i_ths_calc_temp,
559 static const struct ths_thermal_chip sun50i_a64_ths = {
564 .has_bus_clk_reset = true,
565 .temp_data_base = SUN8I_THS_TEMP_DATA,
566 .calibrate = sun8i_h3_ths_calibrate,
567 .init = sun8i_h3_thermal_init,
568 .irq_ack = sun8i_h3_irq_ack,
569 .calc_temp = sun8i_ths_calc_temp,
572 static const struct ths_thermal_chip sun50i_a100_ths = {
574 .has_bus_clk_reset = true,
575 .ft_deviation = 8000,
578 .temp_data_base = SUN50I_H6_THS_TEMP_DATA,
579 .calibrate = sun50i_h6_ths_calibrate,
580 .init = sun50i_h6_thermal_init,
581 .irq_ack = sun50i_h6_irq_ack,
582 .calc_temp = sun8i_ths_calc_temp,
585 static const struct ths_thermal_chip sun50i_h5_ths = {
588 .has_bus_clk_reset = true,
589 .temp_data_base = SUN8I_THS_TEMP_DATA,
590 .calibrate = sun8i_h3_ths_calibrate,
591 .init = sun8i_h3_thermal_init,
592 .irq_ack = sun8i_h3_irq_ack,
593 .calc_temp = sun50i_h5_calc_temp,
596 static const struct ths_thermal_chip sun50i_h6_ths = {
598 .has_bus_clk_reset = true,
599 .ft_deviation = 7000,
602 .temp_data_base = SUN50I_H6_THS_TEMP_DATA,
603 .calibrate = sun50i_h6_ths_calibrate,
604 .init = sun50i_h6_thermal_init,
605 .irq_ack = sun50i_h6_irq_ack,
606 .calc_temp = sun8i_ths_calc_temp,
609 static const struct of_device_id of_ths_match[] = {
610 { .compatible = "allwinner,sun8i-a83t-ths", .data = &sun8i_a83t_ths },
611 { .compatible = "allwinner,sun8i-h3-ths", .data = &sun8i_h3_ths },
612 { .compatible = "allwinner,sun8i-r40-ths", .data = &sun8i_r40_ths },
613 { .compatible = "allwinner,sun50i-a64-ths", .data = &sun50i_a64_ths },
614 { .compatible = "allwinner,sun50i-a100-ths", .data = &sun50i_a100_ths },
615 { .compatible = "allwinner,sun50i-h5-ths", .data = &sun50i_h5_ths },
616 { .compatible = "allwinner,sun50i-h6-ths", .data = &sun50i_h6_ths },
619 MODULE_DEVICE_TABLE(of, of_ths_match);
621 static struct platform_driver ths_driver = {
622 .probe = sun8i_ths_probe,
624 .name = "sun8i-thermal",
625 .of_match_table = of_ths_match,
628 module_platform_driver(ths_driver);
630 MODULE_DESCRIPTION("Thermal sensor driver for Allwinner SOC");
631 MODULE_LICENSE("GPL v2");