2 * R-Car Gen3 THS thermal sensor driver
3 * Based on rcar_thermal.c and work from Hien Dang and Khiem Nguyen.
5 * Copyright (C) 2016 Renesas Electronics Corporation.
6 * Copyright (C) 2016 Sang Engineering
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
18 #include <linux/delay.h>
19 #include <linux/err.h>
20 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/spinlock.h>
27 #include <linux/thermal.h>
29 #include "thermal_core.h"
31 /* Register offsets */
32 #define REG_GEN3_IRQSTR 0x04
33 #define REG_GEN3_IRQMSK 0x08
34 #define REG_GEN3_IRQCTL 0x0C
35 #define REG_GEN3_IRQEN 0x10
36 #define REG_GEN3_IRQTEMP1 0x14
37 #define REG_GEN3_IRQTEMP2 0x18
38 #define REG_GEN3_IRQTEMP3 0x1C
39 #define REG_GEN3_CTSR 0x20
40 #define REG_GEN3_THCTR 0x20
41 #define REG_GEN3_TEMP 0x28
42 #define REG_GEN3_THCODE1 0x50
43 #define REG_GEN3_THCODE2 0x54
44 #define REG_GEN3_THCODE3 0x58
46 /* IRQ{STR,MSK,EN} bits */
47 #define IRQ_TEMP1 BIT(0)
48 #define IRQ_TEMP2 BIT(1)
49 #define IRQ_TEMP3 BIT(2)
50 #define IRQ_TEMPD1 BIT(3)
51 #define IRQ_TEMPD2 BIT(4)
52 #define IRQ_TEMPD3 BIT(5)
55 #define CTSR_PONM BIT(8)
56 #define CTSR_AOUT BIT(7)
57 #define CTSR_THBGR BIT(5)
58 #define CTSR_VMEN BIT(4)
59 #define CTSR_VMST BIT(1)
60 #define CTSR_THSST BIT(0)
63 #define THCTR_PONM BIT(6)
64 #define THCTR_THSST BIT(0)
66 #define CTEMP_MASK 0xFFF
68 #define MCELSIUS(temp) ((temp) * 1000)
69 #define GEN3_FUSE_MASK 0xFFF
73 /* Structure for thermal temperature calculation */
74 struct equation_coefs
{
81 struct rcar_gen3_thermal_tsc
{
83 struct thermal_zone_device
*zone
;
84 struct equation_coefs coef
;
89 struct rcar_gen3_thermal_priv
{
90 struct rcar_gen3_thermal_tsc
*tscs
[TSC_MAX_NUM
];
91 unsigned int num_tscs
;
92 spinlock_t lock
; /* Protect interrupts on and off */
93 const struct rcar_gen3_thermal_data
*data
;
96 struct rcar_gen3_thermal_data
{
97 void (*thermal_init
)(struct rcar_gen3_thermal_tsc
*tsc
);
100 static inline u32
rcar_gen3_thermal_read(struct rcar_gen3_thermal_tsc
*tsc
,
103 return ioread32(tsc
->base
+ reg
);
106 static inline void rcar_gen3_thermal_write(struct rcar_gen3_thermal_tsc
*tsc
,
109 iowrite32(data
, tsc
->base
+ reg
);
113 * Linear approximation for temperature
115 * [reg] = [temp] * a + b => [temp] = ([reg] - b) / a
117 * The constants a and b are calculated using two triplets of int values PTAT
118 * and THCODE. PTAT and THCODE can either be read from hardware or use hard
119 * coded values from driver. The formula to calculate a and b are taken from
120 * BSP and sparsely documented and understood.
122 * Examining the linear formula and the formula used to calculate constants a
123 * and b while knowing that the span for PTAT and THCODE values are between
124 * 0x000 and 0xfff the largest integer possible is 0xfff * 0xfff == 0xffe001.
125 * Integer also needs to be signed so that leaves 7 bits for binary
126 * fixed point scaling.
129 #define FIXPT_SHIFT 7
130 #define FIXPT_INT(_x) ((_x) << FIXPT_SHIFT)
131 #define INT_FIXPT(_x) ((_x) >> FIXPT_SHIFT)
132 #define FIXPT_DIV(_a, _b) DIV_ROUND_CLOSEST(((_a) << FIXPT_SHIFT), (_b))
133 #define FIXPT_TO_MCELSIUS(_x) ((_x) * 1000 >> FIXPT_SHIFT)
135 #define RCAR3_THERMAL_GRAN 500 /* mili Celsius */
137 /* no idea where these constants come from */
141 static void rcar_gen3_thermal_calc_coefs(struct equation_coefs
*coef
,
142 int *ptat
, int *thcode
)
146 /* TODO: Find documentation and document constant calculation formula */
149 * Division is not scaled in BSP and if scaled it might overflow
150 * the dividend (4095 * 4095 << 14 > INT_MAX) so keep it unscaled
152 tj_2
= (FIXPT_INT((ptat
[1] - ptat
[2]) * 137)
153 / (ptat
[0] - ptat
[2])) - FIXPT_INT(41);
155 coef
->a1
= FIXPT_DIV(FIXPT_INT(thcode
[1] - thcode
[2]),
156 tj_2
- FIXPT_INT(TJ_3
));
157 coef
->b1
= FIXPT_INT(thcode
[2]) - coef
->a1
* TJ_3
;
159 coef
->a2
= FIXPT_DIV(FIXPT_INT(thcode
[1] - thcode
[0]),
160 tj_2
- FIXPT_INT(TJ_1
));
161 coef
->b2
= FIXPT_INT(thcode
[0]) - coef
->a2
* TJ_1
;
164 static int rcar_gen3_thermal_round(int temp
)
166 int result
, round_offs
;
168 round_offs
= temp
>= 0 ? RCAR3_THERMAL_GRAN
/ 2 :
169 -RCAR3_THERMAL_GRAN
/ 2;
170 result
= (temp
+ round_offs
) / RCAR3_THERMAL_GRAN
;
171 return result
* RCAR3_THERMAL_GRAN
;
174 static int rcar_gen3_thermal_get_temp(void *devdata
, int *temp
)
176 struct rcar_gen3_thermal_tsc
*tsc
= devdata
;
177 int mcelsius
, val1
, val2
;
180 /* Read register and convert to mili Celsius */
181 reg
= rcar_gen3_thermal_read(tsc
, REG_GEN3_TEMP
) & CTEMP_MASK
;
183 val1
= FIXPT_DIV(FIXPT_INT(reg
) - tsc
->coef
.b1
, tsc
->coef
.a1
);
184 val2
= FIXPT_DIV(FIXPT_INT(reg
) - tsc
->coef
.b2
, tsc
->coef
.a2
);
185 mcelsius
= FIXPT_TO_MCELSIUS((val1
+ val2
) / 2);
187 /* Make sure we are inside specifications */
188 if ((mcelsius
< MCELSIUS(-40)) || (mcelsius
> MCELSIUS(125)))
191 /* Round value to device granularity setting */
192 *temp
= rcar_gen3_thermal_round(mcelsius
);
197 static int rcar_gen3_thermal_mcelsius_to_temp(struct rcar_gen3_thermal_tsc
*tsc
,
200 int celsius
, val1
, val2
;
202 celsius
= DIV_ROUND_CLOSEST(mcelsius
, 1000);
203 val1
= celsius
* tsc
->coef
.a1
+ tsc
->coef
.b1
;
204 val2
= celsius
* tsc
->coef
.a2
+ tsc
->coef
.b2
;
206 return INT_FIXPT((val1
+ val2
) / 2);
209 static int rcar_gen3_thermal_set_trips(void *devdata
, int low
, int high
)
211 struct rcar_gen3_thermal_tsc
*tsc
= devdata
;
213 low
= clamp_val(low
, -40000, 125000);
214 high
= clamp_val(high
, -40000, 125000);
216 rcar_gen3_thermal_write(tsc
, REG_GEN3_IRQTEMP1
,
217 rcar_gen3_thermal_mcelsius_to_temp(tsc
, low
));
219 rcar_gen3_thermal_write(tsc
, REG_GEN3_IRQTEMP2
,
220 rcar_gen3_thermal_mcelsius_to_temp(tsc
, high
));
228 static const 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 void rcar_thermal_irq_set(struct rcar_gen3_thermal_priv
*priv
, bool on
)
236 u32 val
= on
? IRQ_TEMPD1
| IRQ_TEMP2
: 0;
238 for (i
= 0; i
< priv
->num_tscs
; i
++)
239 rcar_gen3_thermal_write(priv
->tscs
[i
], REG_GEN3_IRQMSK
, val
);
242 static irqreturn_t
rcar_gen3_thermal_irq(int irq
, void *data
)
244 struct rcar_gen3_thermal_priv
*priv
= data
;
246 int i
, ret
= IRQ_HANDLED
;
248 spin_lock(&priv
->lock
);
249 for (i
= 0; i
< priv
->num_tscs
; i
++) {
250 status
= rcar_gen3_thermal_read(priv
->tscs
[i
], REG_GEN3_IRQSTR
);
251 rcar_gen3_thermal_write(priv
->tscs
[i
], REG_GEN3_IRQSTR
, 0);
253 ret
= IRQ_WAKE_THREAD
;
256 if (ret
== IRQ_WAKE_THREAD
)
257 rcar_thermal_irq_set(priv
, false);
259 spin_unlock(&priv
->lock
);
264 static irqreturn_t
rcar_gen3_thermal_irq_thread(int irq
, void *data
)
266 struct rcar_gen3_thermal_priv
*priv
= data
;
270 for (i
= 0; i
< priv
->num_tscs
; i
++)
271 thermal_zone_device_update(priv
->tscs
[i
]->zone
,
272 THERMAL_EVENT_UNSPECIFIED
);
274 spin_lock_irqsave(&priv
->lock
, flags
);
275 rcar_thermal_irq_set(priv
, true);
276 spin_unlock_irqrestore(&priv
->lock
, flags
);
281 static void r8a7795_thermal_init(struct rcar_gen3_thermal_tsc
*tsc
)
283 rcar_gen3_thermal_write(tsc
, REG_GEN3_CTSR
, CTSR_THBGR
);
284 rcar_gen3_thermal_write(tsc
, REG_GEN3_CTSR
, 0x0);
286 usleep_range(1000, 2000);
288 rcar_gen3_thermal_write(tsc
, REG_GEN3_CTSR
, CTSR_PONM
);
290 rcar_gen3_thermal_write(tsc
, REG_GEN3_IRQCTL
, 0x3F);
291 rcar_gen3_thermal_write(tsc
, REG_GEN3_IRQMSK
, 0);
292 rcar_gen3_thermal_write(tsc
, REG_GEN3_IRQEN
, IRQ_TEMPD1
| IRQ_TEMP2
);
294 rcar_gen3_thermal_write(tsc
, REG_GEN3_CTSR
,
295 CTSR_PONM
| CTSR_AOUT
| CTSR_THBGR
| CTSR_VMEN
);
297 usleep_range(100, 200);
299 rcar_gen3_thermal_write(tsc
, REG_GEN3_CTSR
,
300 CTSR_PONM
| CTSR_AOUT
| CTSR_THBGR
| CTSR_VMEN
|
301 CTSR_VMST
| CTSR_THSST
);
303 usleep_range(1000, 2000);
306 static void r8a7796_thermal_init(struct rcar_gen3_thermal_tsc
*tsc
)
310 reg_val
= rcar_gen3_thermal_read(tsc
, REG_GEN3_THCTR
);
311 reg_val
&= ~THCTR_PONM
;
312 rcar_gen3_thermal_write(tsc
, REG_GEN3_THCTR
, reg_val
);
314 usleep_range(1000, 2000);
316 rcar_gen3_thermal_write(tsc
, REG_GEN3_IRQCTL
, 0x3F);
317 rcar_gen3_thermal_write(tsc
, REG_GEN3_IRQMSK
, 0);
318 rcar_gen3_thermal_write(tsc
, REG_GEN3_IRQEN
, IRQ_TEMPD1
| IRQ_TEMP2
);
320 reg_val
= rcar_gen3_thermal_read(tsc
, REG_GEN3_THCTR
);
321 reg_val
|= THCTR_THSST
;
322 rcar_gen3_thermal_write(tsc
, REG_GEN3_THCTR
, reg_val
);
324 usleep_range(1000, 2000);
327 static const struct rcar_gen3_thermal_data r8a7795_data
= {
328 .thermal_init
= r8a7795_thermal_init
,
331 static const struct rcar_gen3_thermal_data r8a7796_data
= {
332 .thermal_init
= r8a7796_thermal_init
,
335 static const struct of_device_id rcar_gen3_thermal_dt_ids
[] = {
336 { .compatible
= "renesas,r8a7795-thermal", .data
= &r8a7795_data
},
337 { .compatible
= "renesas,r8a7796-thermal", .data
= &r8a7796_data
},
340 MODULE_DEVICE_TABLE(of
, rcar_gen3_thermal_dt_ids
);
342 static int rcar_gen3_thermal_remove(struct platform_device
*pdev
)
344 struct device
*dev
= &pdev
->dev
;
347 pm_runtime_disable(dev
);
352 static int rcar_gen3_thermal_probe(struct platform_device
*pdev
)
354 struct rcar_gen3_thermal_priv
*priv
;
355 struct device
*dev
= &pdev
->dev
;
356 struct resource
*res
;
357 struct thermal_zone_device
*zone
;
361 /* default values if FUSEs are missing */
362 /* TODO: Read values from hardware on supported platforms */
363 int ptat
[3] = { 2351, 1509, 435 };
364 int thcode
[TSC_MAX_NUM
][3] = {
365 { 3248, 2800, 2221 },
366 { 3245, 2795, 2216 },
367 { 3250, 2805, 2237 },
370 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
374 priv
->data
= of_device_get_match_data(dev
);
376 spin_lock_init(&priv
->lock
);
378 platform_set_drvdata(pdev
, priv
);
381 * Request 2 (of the 3 possible) IRQs, the driver only needs to
382 * to trigger on the low and high trip points of the current
383 * temp window at this point.
385 for (i
= 0; i
< 2; i
++) {
386 irq
= platform_get_irq(pdev
, i
);
390 irqname
= devm_kasprintf(dev
, GFP_KERNEL
, "%s:ch%d",
395 ret
= devm_request_threaded_irq(dev
, irq
, rcar_gen3_thermal_irq
,
396 rcar_gen3_thermal_irq_thread
,
397 IRQF_SHARED
, irqname
, priv
);
402 pm_runtime_enable(dev
);
403 pm_runtime_get_sync(dev
);
405 for (i
= 0; i
< TSC_MAX_NUM
; i
++) {
406 struct rcar_gen3_thermal_tsc
*tsc
;
408 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
412 tsc
= devm_kzalloc(dev
, sizeof(*tsc
), GFP_KERNEL
);
415 goto error_unregister
;
418 tsc
->base
= devm_ioremap_resource(dev
, res
);
419 if (IS_ERR(tsc
->base
)) {
420 ret
= PTR_ERR(tsc
->base
);
421 goto error_unregister
;
426 priv
->data
->thermal_init(tsc
);
427 rcar_gen3_thermal_calc_coefs(&tsc
->coef
, ptat
, thcode
[i
]);
429 zone
= devm_thermal_zone_of_sensor_register(dev
, i
, tsc
,
430 &rcar_gen3_tz_of_ops
);
432 dev_err(dev
, "Can't register thermal zone\n");
434 goto error_unregister
;
438 ret
= of_thermal_get_ntrips(tsc
->zone
);
440 goto error_unregister
;
442 dev_info(dev
, "TSC%d: Loaded %d trip points\n", i
, ret
);
447 if (!priv
->num_tscs
) {
449 goto error_unregister
;
452 rcar_thermal_irq_set(priv
, true);
457 rcar_gen3_thermal_remove(pdev
);
462 static int __maybe_unused
rcar_gen3_thermal_suspend(struct device
*dev
)
464 struct rcar_gen3_thermal_priv
*priv
= dev_get_drvdata(dev
);
466 rcar_thermal_irq_set(priv
, false);
471 static int __maybe_unused
rcar_gen3_thermal_resume(struct device
*dev
)
473 struct rcar_gen3_thermal_priv
*priv
= dev_get_drvdata(dev
);
476 for (i
= 0; i
< priv
->num_tscs
; i
++) {
477 struct rcar_gen3_thermal_tsc
*tsc
= priv
->tscs
[i
];
479 priv
->data
->thermal_init(tsc
);
480 rcar_gen3_thermal_set_trips(tsc
, tsc
->low
, tsc
->high
);
483 rcar_thermal_irq_set(priv
, true);
488 static SIMPLE_DEV_PM_OPS(rcar_gen3_thermal_pm_ops
, rcar_gen3_thermal_suspend
,
489 rcar_gen3_thermal_resume
);
491 static struct platform_driver rcar_gen3_thermal_driver
= {
493 .name
= "rcar_gen3_thermal",
494 .pm
= &rcar_gen3_thermal_pm_ops
,
495 .of_match_table
= rcar_gen3_thermal_dt_ids
,
497 .probe
= rcar_gen3_thermal_probe
,
498 .remove
= rcar_gen3_thermal_remove
,
500 module_platform_driver(rcar_gen3_thermal_driver
);
502 MODULE_LICENSE("GPL v2");
503 MODULE_DESCRIPTION("R-Car Gen3 THS thermal sensor driver");
504 MODULE_AUTHOR("Wolfram Sang <wsa+renesas@sang-engineering.com>");