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