1 // SPDX-License-Identifier: GPL-2.0
3 * R-Car Gen3 THS thermal sensor driver
4 * Based on rcar_thermal.c and work from Hien Dang and Khiem Nguyen.
6 * Copyright (C) 2016 Renesas Electronics Corporation.
7 * Copyright (C) 2016 Sang Engineering
9 #include <linux/delay.h>
10 #include <linux/err.h>
11 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/sys_soc.h>
18 #include <linux/thermal.h>
20 #include "thermal_core.h"
21 #include "thermal_hwmon.h"
23 /* Register offsets */
24 #define REG_GEN3_IRQSTR 0x04
25 #define REG_GEN3_IRQMSK 0x08
26 #define REG_GEN3_IRQCTL 0x0C
27 #define REG_GEN3_IRQEN 0x10
28 #define REG_GEN3_IRQTEMP1 0x14
29 #define REG_GEN3_IRQTEMP2 0x18
30 #define REG_GEN3_IRQTEMP3 0x1C
31 #define REG_GEN3_CTSR 0x20
32 #define REG_GEN3_THCTR 0x20
33 #define REG_GEN3_TEMP 0x28
34 #define REG_GEN3_THCODE1 0x50
35 #define REG_GEN3_THCODE2 0x54
36 #define REG_GEN3_THCODE3 0x58
37 #define REG_GEN3_PTAT1 0x5c
38 #define REG_GEN3_PTAT2 0x60
39 #define REG_GEN3_PTAT3 0x64
40 #define REG_GEN3_THSCP 0x68
42 /* IRQ{STR,MSK,EN} bits */
43 #define IRQ_TEMP1 BIT(0)
44 #define IRQ_TEMP2 BIT(1)
45 #define IRQ_TEMP3 BIT(2)
46 #define IRQ_TEMPD1 BIT(3)
47 #define IRQ_TEMPD2 BIT(4)
48 #define IRQ_TEMPD3 BIT(5)
51 #define CTSR_PONM BIT(8)
52 #define CTSR_AOUT BIT(7)
53 #define CTSR_THBGR BIT(5)
54 #define CTSR_VMEN BIT(4)
55 #define CTSR_VMST BIT(1)
56 #define CTSR_THSST BIT(0)
59 #define THCTR_PONM BIT(6)
60 #define THCTR_THSST BIT(0)
63 #define THSCP_COR_PARA_VLD (BIT(15) | BIT(14))
65 #define CTEMP_MASK 0xFFF
67 #define MCELSIUS(temp) ((temp) * 1000)
68 #define GEN3_FUSE_MASK 0xFFF
72 /* Structure for thermal temperature calculation */
73 struct equation_coefs {
80 struct rcar_gen3_thermal_tsc {
82 struct thermal_zone_device *zone;
83 struct equation_coefs coef;
88 struct rcar_gen3_thermal_priv {
89 struct rcar_gen3_thermal_tsc *tscs[TSC_MAX_NUM];
90 unsigned int num_tscs;
91 void (*thermal_init)(struct rcar_gen3_thermal_tsc *tsc);
95 static inline u32 rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc *tsc,
98 return ioread32(tsc->base + reg);
101 static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc *tsc,
104 iowrite32(data, tsc->base + reg);
108 * Linear approximation for temperature
110 * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
112 * The constants a and b are calculated using two triplets of int values PTAT
113 * and THCODE. PTAT and THCODE can either be read from hardware or use hard
114 * coded values from driver. The formula to calculate a and b are taken from
115 * BSP and sparsely documented and understood.
117 * Examining the linear formula and the formula used to calculate constants a
118 * and b while knowing that the span for PTAT and THCODE values are between
119 * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
120 * Integer also needs to be signed so that leaves 7 bits for binary
121 * fixed point scaling.
124 #define FIXPT_SHIFT 7
125 #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
126 #define INT_FIXPT(_x) ((_x) >> FIXPT_SHIFT)
127 #define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
128 #define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
130 #define RCAR3_THERMAL_GRAN 500 /* mili Celsius */
132 /* no idea where these constants come from */
135 static void rcar_gen3_thermal_calc_coefs(struct rcar_gen3_thermal_priv *priv,
136 struct rcar_gen3_thermal_tsc *tsc,
139 /* TODO: Find documentation and document constant calculation formula */
142 * Division is not scaled in BSP and if scaled it might overflow
143 * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled
145 tsc->tj_t = (FIXPT_INT((priv->ptat[1] - priv->ptat[2]) * (ths_tj_1 - TJ_3))
146 / (priv->ptat[0] - priv->ptat[2])) + FIXPT_INT(TJ_3);
148 tsc->coef.a1 = FIXPT_DIV(FIXPT_INT(tsc->thcode[1] - tsc->thcode[2]),
149 tsc->tj_t - FIXPT_INT(TJ_3));
150 tsc->coef.b1 = FIXPT_INT(tsc->thcode[2]) - tsc->coef.a1 * TJ_3;
152 tsc->coef.a2 = FIXPT_DIV(FIXPT_INT(tsc->thcode[1] - tsc->thcode[0]),
153 tsc->tj_t - FIXPT_INT(ths_tj_1));
154 tsc->coef.b2 = FIXPT_INT(tsc->thcode[0]) - tsc->coef.a2 * ths_tj_1;
157 static int rcar_gen3_thermal_round(int temp)
159 int result, round_offs;
161 round_offs = temp >= 0 ? RCAR3_THERMAL_GRAN / 2 :
162 -RCAR3_THERMAL_GRAN / 2;
163 result = (temp + round_offs) / RCAR3_THERMAL_GRAN;
164 return result * RCAR3_THERMAL_GRAN;
167 static int rcar_gen3_thermal_get_temp(void *devdata, int *temp)
169 struct rcar_gen3_thermal_tsc *tsc = devdata;
173 /* Read register and convert to mili Celsius */
174 reg = rcar_gen3_thermal_read(tsc, REG_GEN3_TEMP) & CTEMP_MASK;
176 if (reg <= tsc->thcode[1])
177 val = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b1,
180 val = FIXPT_DIV(FIXPT_INT(reg) - tsc->coef.b2,
182 mcelsius = FIXPT_TO_MCELSIUS(val);
184 /* Guaranteed operating range is -40C to 125C. */
186 /* Round value to device granularity setting */
187 *temp = rcar_gen3_thermal_round(mcelsius);
192 static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc *tsc,
197 celsius = DIV_ROUND_CLOSEST(mcelsius, 1000);
198 if (celsius <= INT_FIXPT(tsc->tj_t))
199 val = celsius * tsc->coef.a1 + tsc->coef.b1;
201 val = celsius * tsc->coef.a2 + tsc->coef.b2;
203 return INT_FIXPT(val);
206 static int rcar_gen3_thermal_set_trips(void *devdata, int low, int high)
208 struct rcar_gen3_thermal_tsc *tsc = devdata;
211 if (low != -INT_MAX) {
212 irqmsk |= IRQ_TEMPD1;
213 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP1,
214 rcar_gen3_thermal_mcelsius_to_temp(tsc, low));
217 if (high != INT_MAX) {
219 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQTEMP2,
220 rcar_gen3_thermal_mcelsius_to_temp(tsc, high));
223 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, irqmsk);
228 static struct thermal_zone_of_device_ops rcar_gen3_tz_of_ops = {
229 .get_temp = rcar_gen3_thermal_get_temp,
230 .set_trips = rcar_gen3_thermal_set_trips,
233 static irqreturn_t rcar_gen3_thermal_irq(int irq, void *data)
235 struct rcar_gen3_thermal_priv *priv = data;
239 for (i = 0; i < priv->num_tscs; i++) {
240 status = rcar_gen3_thermal_read(priv->tscs[i], REG_GEN3_IRQSTR);
241 rcar_gen3_thermal_write(priv->tscs[i], REG_GEN3_IRQSTR, 0);
243 thermal_zone_device_update(priv->tscs[i]->zone,
244 THERMAL_EVENT_UNSPECIFIED);
250 static const struct soc_device_attribute r8a7795es1[] = {
251 { .soc_id = "r8a7795", .revision = "ES1.*" },
255 static bool rcar_gen3_thermal_read_fuses(struct rcar_gen3_thermal_priv *priv)
260 /* If fuses are not set, fallback to pseudo values. */
261 thscp = rcar_gen3_thermal_read(priv->tscs[0], REG_GEN3_THSCP);
262 if ((thscp & THSCP_COR_PARA_VLD) != THSCP_COR_PARA_VLD) {
263 /* Default THCODE values in case FUSEs are not set. */
264 static const int thcodes[TSC_MAX_NUM][3] = {
265 { 3397, 2800, 2221 },
266 { 3393, 2795, 2216 },
267 { 3389, 2805, 2237 },
268 { 3415, 2694, 2195 },
269 { 3356, 2724, 2244 },
272 priv->ptat[0] = 2631;
273 priv->ptat[1] = 1509;
276 for (i = 0; i < priv->num_tscs; i++) {
277 struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
279 tsc->thcode[0] = thcodes[i][0];
280 tsc->thcode[1] = thcodes[i][1];
281 tsc->thcode[2] = thcodes[i][2];
288 * Set the pseudo calibration points with fused values.
289 * PTAT is shared between all TSCs but only fused for the first
290 * TSC while THCODEs are fused for each TSC.
292 priv->ptat[0] = rcar_gen3_thermal_read(priv->tscs[0], REG_GEN3_PTAT1) &
294 priv->ptat[1] = rcar_gen3_thermal_read(priv->tscs[0], REG_GEN3_PTAT2) &
296 priv->ptat[2] = rcar_gen3_thermal_read(priv->tscs[0], REG_GEN3_PTAT3) &
299 for (i = 0; i < priv->num_tscs; i++) {
300 struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
302 tsc->thcode[0] = rcar_gen3_thermal_read(tsc, REG_GEN3_THCODE1) &
304 tsc->thcode[1] = rcar_gen3_thermal_read(tsc, REG_GEN3_THCODE2) &
306 tsc->thcode[2] = rcar_gen3_thermal_read(tsc, REG_GEN3_THCODE3) &
313 static void rcar_gen3_thermal_init_r8a7795es1(struct rcar_gen3_thermal_tsc *tsc)
315 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_THBGR);
316 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, 0x0);
318 usleep_range(1000, 2000);
320 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR, CTSR_PONM);
322 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0x3F);
323 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
324 if (tsc->zone->ops->set_trips)
325 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN,
326 IRQ_TEMPD1 | IRQ_TEMP2);
328 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
329 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN);
331 usleep_range(100, 200);
333 rcar_gen3_thermal_write(tsc, REG_GEN3_CTSR,
334 CTSR_PONM | CTSR_AOUT | CTSR_THBGR | CTSR_VMEN |
335 CTSR_VMST | CTSR_THSST);
337 usleep_range(1000, 2000);
340 static void rcar_gen3_thermal_init(struct rcar_gen3_thermal_tsc *tsc)
344 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
345 reg_val &= ~THCTR_PONM;
346 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
348 usleep_range(1000, 2000);
350 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQCTL, 0);
351 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQMSK, 0);
352 if (tsc->zone->ops->set_trips)
353 rcar_gen3_thermal_write(tsc, REG_GEN3_IRQEN,
354 IRQ_TEMPD1 | IRQ_TEMP2);
356 reg_val = rcar_gen3_thermal_read(tsc, REG_GEN3_THCTR);
357 reg_val |= THCTR_THSST;
358 rcar_gen3_thermal_write(tsc, REG_GEN3_THCTR, reg_val);
360 usleep_range(1000, 2000);
363 static const int rcar_gen3_ths_tj_1 = 126;
364 static const int rcar_gen3_ths_tj_1_m3_w = 116;
365 static const struct of_device_id rcar_gen3_thermal_dt_ids[] = {
367 .compatible = "renesas,r8a774a1-thermal",
368 .data = &rcar_gen3_ths_tj_1_m3_w,
371 .compatible = "renesas,r8a774b1-thermal",
372 .data = &rcar_gen3_ths_tj_1,
375 .compatible = "renesas,r8a774e1-thermal",
376 .data = &rcar_gen3_ths_tj_1,
379 .compatible = "renesas,r8a7795-thermal",
380 .data = &rcar_gen3_ths_tj_1,
383 .compatible = "renesas,r8a7796-thermal",
384 .data = &rcar_gen3_ths_tj_1_m3_w,
387 .compatible = "renesas,r8a77961-thermal",
388 .data = &rcar_gen3_ths_tj_1_m3_w,
391 .compatible = "renesas,r8a77965-thermal",
392 .data = &rcar_gen3_ths_tj_1,
395 .compatible = "renesas,r8a77980-thermal",
396 .data = &rcar_gen3_ths_tj_1,
399 .compatible = "renesas,r8a779a0-thermal",
400 .data = &rcar_gen3_ths_tj_1,
404 MODULE_DEVICE_TABLE(of, rcar_gen3_thermal_dt_ids);
406 static int rcar_gen3_thermal_remove(struct platform_device *pdev)
408 struct device *dev = &pdev->dev;
411 pm_runtime_disable(dev);
416 static void rcar_gen3_hwmon_action(void *data)
418 struct thermal_zone_device *zone = data;
420 thermal_remove_hwmon_sysfs(zone);
423 static int rcar_gen3_thermal_request_irqs(struct rcar_gen3_thermal_priv *priv,
424 struct platform_device *pdev)
426 struct device *dev = &pdev->dev;
431 for (i = 0; i < 2; i++) {
432 irq = platform_get_irq_optional(pdev, i);
436 irqname = devm_kasprintf(dev, GFP_KERNEL, "%s:ch%d",
441 ret = devm_request_threaded_irq(dev, irq, NULL,
442 rcar_gen3_thermal_irq,
443 IRQF_ONESHOT, irqname, priv);
451 static int rcar_gen3_thermal_probe(struct platform_device *pdev)
453 struct rcar_gen3_thermal_priv *priv;
454 struct device *dev = &pdev->dev;
455 const int *ths_tj_1 = of_device_get_match_data(dev);
456 struct resource *res;
457 struct thermal_zone_device *zone;
461 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
465 priv->thermal_init = rcar_gen3_thermal_init;
466 if (soc_device_match(r8a7795es1))
467 priv->thermal_init = rcar_gen3_thermal_init_r8a7795es1;
469 platform_set_drvdata(pdev, priv);
471 if (rcar_gen3_thermal_request_irqs(priv, pdev))
472 rcar_gen3_tz_of_ops.set_trips = NULL;
474 pm_runtime_enable(dev);
475 pm_runtime_get_sync(dev);
477 for (i = 0; i < TSC_MAX_NUM; i++) {
478 struct rcar_gen3_thermal_tsc *tsc;
480 res = platform_get_resource(pdev, IORESOURCE_MEM, i);
484 tsc = devm_kzalloc(dev, sizeof(*tsc), GFP_KERNEL);
487 goto error_unregister;
490 tsc->base = devm_ioremap_resource(dev, res);
491 if (IS_ERR(tsc->base)) {
492 ret = PTR_ERR(tsc->base);
493 goto error_unregister;
501 if (!rcar_gen3_thermal_read_fuses(priv))
502 dev_info(dev, "No calibration values fused, fallback to driver values\n");
504 for (i = 0; i < priv->num_tscs; i++) {
505 struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
507 zone = devm_thermal_zone_of_sensor_register(dev, i, tsc,
508 &rcar_gen3_tz_of_ops);
510 dev_err(dev, "Can't register thermal zone\n");
512 goto error_unregister;
516 priv->thermal_init(tsc);
517 rcar_gen3_thermal_calc_coefs(priv, tsc, *ths_tj_1);
519 tsc->zone->tzp->no_hwmon = false;
520 ret = thermal_add_hwmon_sysfs(tsc->zone);
522 goto error_unregister;
524 ret = devm_add_action_or_reset(dev, rcar_gen3_hwmon_action, zone);
526 goto error_unregister;
528 ret = of_thermal_get_ntrips(tsc->zone);
530 goto error_unregister;
532 dev_info(dev, "TSC%u: Loaded %d trip points\n", i, ret);
535 if (!priv->num_tscs) {
537 goto error_unregister;
543 rcar_gen3_thermal_remove(pdev);
548 static int __maybe_unused rcar_gen3_thermal_resume(struct device *dev)
550 struct rcar_gen3_thermal_priv *priv = dev_get_drvdata(dev);
553 for (i = 0; i < priv->num_tscs; i++) {
554 struct rcar_gen3_thermal_tsc *tsc = priv->tscs[i];
555 struct thermal_zone_device *zone = tsc->zone;
557 priv->thermal_init(tsc);
558 if (zone->ops->set_trips)
559 rcar_gen3_thermal_set_trips(tsc, zone->prev_low_trip,
560 zone->prev_high_trip);
566 static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops, NULL,
567 rcar_gen3_thermal_resume);
569 static struct platform_driver rcar_gen3_thermal_driver = {
571 .name = "rcar_gen3_thermal",
572 .pm = &rcar_gen3_thermal_pm_ops,
573 .of_match_table = rcar_gen3_thermal_dt_ids,
575 .probe = rcar_gen3_thermal_probe,
576 .remove = rcar_gen3_thermal_remove,
578 module_platform_driver(rcar_gen3_thermal_driver);
580 MODULE_LICENSE("GPL v2");
581 MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
582 MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");