Debian Codegraph @

drivers/thermal/renesas/rzg3e_thermal.c

raw
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Renesas RZ/G3E TSU Temperature Sensor Unit
4 *
5 * Copyright (C) 2025 Renesas Electronics Corporation
6 */
7 #include <linux/arm-smccc.h>
8 #include <linux/clk.h>
9 #include <linux/cleanup.h>
10 #include <linux/delay.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
13 #include <linux/io.h>
14 #include <linux/iopoll.h>
15 #include <linux/kernel.h>
16 #include <linux/mfd/syscon.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <linux/reset.h>
23 #include <linux/thermal.h>
24 #include <linux/units.h>
25
26 #include "../thermal_hwmon.h"
27
28 /* TSU Register offsets and bits */
29 #define TSU_SSUSR 0x00
30 #define TSU_SSUSR_EN_TS BIT(0)
31 #define TSU_SSUSR_ADC_PD_TS BIT(1)
32 #define TSU_SSUSR_SOC_TS_EN BIT(2)
33
34 #define TSU_STRGR 0x04
35 #define TSU_STRGR_ADST BIT(0)
36
37 #define TSU_SOSR1 0x08
38 #define TSU_SOSR1_ADCT_8 0x03
39 #define TSU_SOSR1_ADCS BIT(4)
40 #define TSU_SOSR1_OUTSEL BIT(9)
41
42 #define TSU_SCRR 0x10
43 #define TSU_SCRR_OUT12BIT_TS GENMASK(11, 0)
44
45 #define TSU_SSR 0x14
46 #define TSU_SSR_CONV BIT(0)
47
48 #define TSU_CMSR 0x18
49 #define TSU_CMSR_CMPEN BIT(0)
50
51 #define TSU_LLSR 0x1C
52 #define TSU_ULSR 0x20
53
54 #define TSU_SISR 0x30
55 #define TSU_SISR_ADF BIT(0)
56 #define TSU_SISR_CMPF BIT(1)
57
58 #define TSU_SIER 0x34
59 #define TSU_SIER_CMPIE BIT(1)
60
61 #define TSU_SICR 0x38
62 #define TSU_SICR_ADCLR BIT(0)
63 #define TSU_SICR_CMPCLR BIT(1)
64
65 /* Temperature calculation constants from datasheet */
66 #define TSU_CODE_MAX 0xFFF
67
68 /* Timing specifications from datasheet */
69 #define TSU_POWERUP_TIME_US 120 /* 120T at 1MHz sensor clock per datasheet */
70 #define TSU_CONV_TIME_US 50 /* Per sample conversion time */
71 #define TSU_POLL_DELAY_US 10 /* Polling interval */
72 #define TSU_MIN_CLOCK_RATE 24000000 /* TSU_PCLK minimum 24MHz */
73
74 #define RZ_SIP_SVC_GET_SYSTSU 0x82000022
75 #define OTP_TSU_REG_ADR_TEMPHI 0x01DC
76 #define OTP_TSU_REG_ADR_TEMPLO 0x01DD
77
78 struct rzg3e_thermal_priv;
79
80 struct rzg3e_thermal_info {
81 int (*get_trim)(struct rzg3e_thermal_priv *priv);
82 int temp_d_mc;
83 int temp_e_mc;
84 };
85
86 /**
87 * struct rzg3e_thermal_priv - RZ/G3E TSU private data
88 * @base: TSU register base
89 * @dev: device pointer
90 * @syscon: regmap for calibration values
91 * @zone: thermal zone device
92 * @rstc: reset control
93 * @info: chip type specific information
94 * @trmval0: calibration value 0 (b)
95 * @trmval1: calibration value 1 (c)
96 * @trim_offset: offset for trim registers in syscon
97 * @lock: protects hardware access during conversions
98 */
99 struct rzg3e_thermal_priv {
100 void __iomem *base;
101 struct device *dev;
102 struct thermal_zone_device *zone;
103 struct reset_control *rstc;
104 const struct rzg3e_thermal_info *info;
105 u16 trmval0;
106 u16 trmval1;
107 struct mutex lock;
108 };
109
110 static int rzg3e_thermal_power_on(struct rzg3e_thermal_priv *priv)
111 {
112 u32 val;
113 int ret;
114
115 /* Clear any pending interrupts */
116 writel(TSU_SICR_ADCLR | TSU_SICR_CMPCLR, priv->base + TSU_SICR);
117
118 /* Disable all interrupts during setup */
119 writel(0, priv->base + TSU_SIER);
120
121 /*
122 * Power-on sequence per datasheet 7.11.9.1:
123 * SOC_TS_EN must be set at same time or before EN_TS and ADC_PD_TS
124 */
125 val = TSU_SSUSR_SOC_TS_EN | TSU_SSUSR_EN_TS;
126 writel(val, priv->base + TSU_SSUSR);
127
128 /* Wait for sensor stabilization per datasheet 7.11.7.1 */
129 usleep_range(TSU_POWERUP_TIME_US, TSU_POWERUP_TIME_US + 10);
130
131 /* Configure for average mode with 8 samples */
132 val = TSU_SOSR1_OUTSEL | TSU_SOSR1_ADCT_8;
133 writel(val, priv->base + TSU_SOSR1);
134
135 /* Ensure we're in single scan mode (default) */
136 val = readl(priv->base + TSU_SOSR1);
137 if (val & TSU_SOSR1_ADCS) {
138 dev_err(priv->dev, "Invalid scan mode setting\n");
139 return -EINVAL;
140 }
141
142 /* Wait for any ongoing conversion to complete */
143 ret = readl_poll_timeout(priv->base + TSU_SSR, val,
144 !(val & TSU_SSR_CONV),
145 TSU_POLL_DELAY_US,
146 USEC_PER_MSEC);
147 if (ret) {
148 dev_err(priv->dev, "Timeout waiting for conversion\n");
149 return ret;
150 }
151
152 return 0;
153 }
154
155 static void rzg3e_thermal_power_off(struct rzg3e_thermal_priv *priv)
156 {
157 /* Disable all interrupts */
158 writel(0, priv->base + TSU_SIER);
159
160 /* Clear pending interrupts */
161 writel(TSU_SICR_ADCLR | TSU_SICR_CMPCLR, priv->base + TSU_SICR);
162
163 /* Power down sequence per datasheet */
164 writel(TSU_SSUSR_ADC_PD_TS, priv->base + TSU_SSUSR);
165 }
166
167 /*
168 * Convert 12-bit sensor code to temperature in millicelsius
169 * Formula from datasheet 7.11.7.8:
170 * T(°C) = ((e - d) / (c - b)) * (a - b) + d
171 * where: a = sensor code, b = trmval0, c = trmval1, d = -41, e = 126
172 */
173 static int rzg3e_thermal_code_to_temp(struct rzg3e_thermal_priv *priv, u16 code)
174 {
175 const struct rzg3e_thermal_info *info = priv->info;
176 s64 numerator, denominator;
177 int temp_mc;
178
179 numerator = (info->temp_e_mc - info->temp_d_mc) *
180 (s64)(code - priv->trmval0);
181 denominator = priv->trmval1 - priv->trmval0;
182
183 temp_mc = div64_s64(numerator, denominator) + info->temp_d_mc;
184
185 return clamp(temp_mc, info->temp_d_mc, info->temp_e_mc);
186 }
187
188 /*
189 * Convert temperature in millicelsius to 12-bit sensor code
190 * Formula from datasheet 7.11.7.9 (inverse of above)
191 */
192 static u16 rzg3e_thermal_temp_to_code(struct rzg3e_thermal_priv *priv, int temp_mc)
193 {
194 const struct rzg3e_thermal_info *info = priv->info;
195 s64 numerator, denominator;
196 s64 code;
197
198 numerator = (temp_mc - info->temp_d_mc) * (priv->trmval1 - priv->trmval0);
199 denominator = info->temp_e_mc - info->temp_d_mc;
200
201 code = div64_s64(numerator, denominator) + priv->trmval0;
202
203 return clamp_val(code, 0, TSU_CODE_MAX);
204 }
205
206 static int rzg3e_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
207 {
208 struct rzg3e_thermal_priv *priv = thermal_zone_device_priv(tz);
209 u32 status, code;
210 int ret, timeout;
211
212 ret = pm_runtime_resume_and_get(priv->dev);
213 if (ret < 0)
214 return ret;
215
216 guard(mutex)(&priv->lock);
217
218 /* Clear any previous conversion status */
219 writel(TSU_SICR_ADCLR, priv->base + TSU_SICR);
220
221 /* Start single conversion */
222 writel(TSU_STRGR_ADST, priv->base + TSU_STRGR);
223
224 /* Wait for conversion completion - 8 samples at ~50us each */
225 timeout = TSU_CONV_TIME_US * 8 * 2; /* Double for margin */
226 ret = readl_poll_timeout(priv->base + TSU_SISR, status,
227 status & TSU_SISR_ADF,
228 TSU_POLL_DELAY_US, timeout);
229 if (ret) {
230 dev_err(priv->dev, "Conversion timeout (status=0x%08x)\n", status);
231 goto out;
232 }
233
234 /* Read the averaged result and clear the complete flag */
235 code = readl(priv->base + TSU_SCRR) & TSU_SCRR_OUT12BIT_TS;
236 writel(TSU_SICR_ADCLR, priv->base + TSU_SICR);
237
238 /* Convert to temperature */
239 *temp = rzg3e_thermal_code_to_temp(priv, code);
240
241 dev_dbg(priv->dev, "temp=%d mC (%d.%03d°C), code=0x%03x\n",
242 *temp, *temp / 1000, abs(*temp) % 1000, code);
243
244 out:
245 pm_runtime_mark_last_busy(priv->dev);
246 pm_runtime_put_autosuspend(priv->dev);
247 return ret;
248 }
249
250 static int rzg3e_thermal_set_trips(struct thermal_zone_device *tz,
251 int low, int high)
252 {
253 struct rzg3e_thermal_priv *priv = thermal_zone_device_priv(tz);
254 u16 low_code, high_code;
255 u32 val;
256 int ret;
257
258 /* Hardware requires low < high */
259 if (low >= high)
260 return -EINVAL;
261
262 ret = pm_runtime_resume_and_get(priv->dev);
263 if (ret < 0)
264 return ret;
265
266 guard(mutex)(&priv->lock);
267
268 /* Convert temperatures to codes */
269 low_code = rzg3e_thermal_temp_to_code(priv, low);
270 high_code = rzg3e_thermal_temp_to_code(priv, high);
271
272 dev_dbg(priv->dev, "set_trips: low=%d high=%d (codes: 0x%03x/0x%03x)\n",
273 low, high, low_code, high_code);
274
275 /* Disable comparison during reconfiguration */
276 writel(0, priv->base + TSU_SIER);
277 writel(0, priv->base + TSU_CMSR);
278
279 /* Clear any pending comparison interrupts */
280 writel(TSU_SICR_CMPCLR, priv->base + TSU_SICR);
281
282 /* Set trip points */
283 writel(low_code, priv->base + TSU_LLSR);
284 writel(high_code, priv->base + TSU_ULSR);
285
286 /*
287 * Ensure OUTSEL is set for comparison per datasheet 7.11.7.4
288 * Comparison uses averaged data
289 */
290 val = readl(priv->base + TSU_SOSR1);
291 val |= TSU_SOSR1_OUTSEL;
292 writel(val, priv->base + TSU_SOSR1);
293
294 /* Enable comparison with "out of range" mode (CMPCOND=0) */
295 writel(TSU_CMSR_CMPEN, priv->base + TSU_CMSR);
296
297 /* Unmask compare IRQ and start a conversion to evaluate window */
298 writel(TSU_SIER_CMPIE, priv->base + TSU_SIER);
299 writel(TSU_STRGR_ADST, priv->base + TSU_STRGR);
300
301 pm_runtime_mark_last_busy(priv->dev);
302 pm_runtime_put_autosuspend(priv->dev);
303
304 return 0;
305 }
306
307 static irqreturn_t rzg3e_thermal_irq_thread(int irq, void *data)
308 {
309 struct rzg3e_thermal_priv *priv = data;
310
311 dev_dbg(priv->dev, "Temperature threshold crossed\n");
312
313 /* Notify thermal framework to re-evaluate trip points */
314 thermal_zone_device_update(priv->zone, THERMAL_TRIP_VIOLATED);
315
316 return IRQ_HANDLED;
317 }
318
319 static irqreturn_t rzg3e_thermal_irq(int irq, void *data)
320 {
321 struct rzg3e_thermal_priv *priv = data;
322 u32 status;
323
324 status = readl(priv->base + TSU_SISR);
325
326 /* Check if comparison interrupt occurred */
327 if (status & TSU_SISR_CMPF) {
328 /* Clear irq flag and disable interrupt until reconfigured */
329 writel(TSU_SICR_CMPCLR, priv->base + TSU_SICR);
330 writel(0, priv->base + TSU_SIER);
331
332 return IRQ_WAKE_THREAD;
333 }
334
335 return IRQ_NONE;
336 }
337
338 static const struct thermal_zone_device_ops rzg3e_tz_ops = {
339 .get_temp = rzg3e_thermal_get_temp,
340 .set_trips = rzg3e_thermal_set_trips,
341 };
342
343 static int rzg3e_thermal_get_syscon_trim(struct rzg3e_thermal_priv *priv)
344 {
345 struct device_node *np = priv->dev->of_node;
346 struct regmap *syscon;
347 u32 offset;
348 int ret;
349 u32 val;
350
351 syscon = syscon_regmap_lookup_by_phandle_args(np, "renesas,tsu-trim", 1, &offset);
352 if (IS_ERR(syscon))
353 return dev_err_probe(priv->dev, PTR_ERR(syscon),
354 "Failed to parse renesas,tsu-trim\n");
355
356 /* Read calibration values from syscon */
357 ret = regmap_read(syscon, offset, &val);
358 if (ret)
359 return ret;
360 priv->trmval0 = val & TSU_CODE_MAX;
361
362 ret = regmap_read(syscon, offset + 4, &val);
363 if (ret)
364 return ret;
365 priv->trmval1 = val & TSU_CODE_MAX;
366
367 return 0;
368 }
369
370 static int rzg3e_thermal_get_smc_trim(struct rzg3e_thermal_priv *priv)
371 {
372 struct arm_smccc_res local_res;
373
374 arm_smccc_smc(RZ_SIP_SVC_GET_SYSTSU, OTP_TSU_REG_ADR_TEMPLO,
375 0, 0, 0, 0, 0, 0, &local_res);
376 priv->trmval0 = local_res.a0 & TSU_CODE_MAX;
377
378 arm_smccc_smc(RZ_SIP_SVC_GET_SYSTSU, OTP_TSU_REG_ADR_TEMPHI,
379 0, 0, 0, 0, 0, 0, &local_res);
380 priv->trmval1 = local_res.a0 & TSU_CODE_MAX;
381
382 return 0;
383 }
384
385 static int rzg3e_thermal_probe(struct platform_device *pdev)
386 {
387 struct device *dev = &pdev->dev;
388 struct rzg3e_thermal_priv *priv;
389 struct clk *clk;
390 int irq, ret;
391
392 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
393 if (!priv)
394 return -ENOMEM;
395
396 priv->dev = dev;
397 ret = devm_mutex_init(dev, &priv->lock);
398 if (ret)
399 return ret;
400 platform_set_drvdata(pdev, priv);
401
402 priv->info = device_get_match_data(dev);
403
404 priv->base = devm_platform_ioremap_resource(pdev, 0);
405 if (IS_ERR(priv->base))
406 return PTR_ERR(priv->base);
407
408 ret = priv->info->get_trim(priv);
409 if (ret)
410 return ret;
411
412 if (!priv->trmval0 || !priv->trmval1 ||
413 priv->trmval0 == priv->trmval1 ||
414 priv->trmval0 == TSU_CODE_MAX || priv->trmval1 == TSU_CODE_MAX)
415 return dev_err_probe(priv->dev, -EINVAL,
416 "Invalid calibration: b=0x%03x, c=0x%03x\n",
417 priv->trmval0, priv->trmval1);
418
419 dev_dbg(priv->dev, "Calibration: b=0x%03x (%u), c=0x%03x (%u)\n",
420 priv->trmval0, priv->trmval0, priv->trmval1, priv->trmval1);
421
422 /* Get clock to verify frequency - clock is managed by power domain */
423 clk = devm_clk_get(dev, NULL);
424 if (IS_ERR(clk))
425 return dev_err_probe(dev, PTR_ERR(clk),
426 "Failed to get clock\n");
427
428 if (clk_get_rate(clk) < TSU_MIN_CLOCK_RATE)
429 return dev_err_probe(dev, -EINVAL,
430 "Clock rate %lu Hz too low (min %u Hz)\n",
431 clk_get_rate(clk), TSU_MIN_CLOCK_RATE);
432
433 priv->rstc = devm_reset_control_get_optional_exclusive_deasserted(dev, NULL);
434 if (IS_ERR(priv->rstc))
435 return dev_err_probe(dev, PTR_ERR(priv->rstc),
436 "Failed to get/deassert reset control\n");
437
438 /* Get comparison interrupt */
439 irq = platform_get_irq_byname(pdev, "adcmpi");
440 if (irq < 0)
441 return irq;
442
443 /* Enable runtime PM */
444 pm_runtime_set_autosuspend_delay(dev, 1000);
445 pm_runtime_use_autosuspend(dev);
446 devm_pm_runtime_enable(dev);
447
448 /* Initial hardware setup */
449 ret = pm_runtime_resume_and_get(dev);
450 if (ret < 0)
451 return dev_err_probe(dev, ret, "Runtime resume failed\n");
452
453 /* Register thermal zone - this will trigger DT parsing */
454 priv->zone = devm_thermal_of_zone_register(dev, 0, priv, &rzg3e_tz_ops);
455 if (IS_ERR(priv->zone)) {
456 ret = PTR_ERR(priv->zone);
457 dev_err(dev, "Failed to register thermal zone: %d\n", ret);
458 goto err_pm_put;
459 }
460
461 /* Request threaded IRQ for comparison interrupt */
462 ret = devm_request_threaded_irq(dev, irq, rzg3e_thermal_irq,
463 rzg3e_thermal_irq_thread,
464 IRQF_ONESHOT, "rzg3e_thermal", priv);
465 if (ret) {
466 dev_err(dev, "Failed to request IRQ: %d\n", ret);
467 goto err_pm_put;
468 }
469
470 /* Add hwmon sysfs interface */
471 ret = devm_thermal_add_hwmon_sysfs(dev, priv->zone);
472 if (ret)
473 dev_warn(dev, "Failed to add hwmon sysfs attributes\n");
474
475 pm_runtime_mark_last_busy(dev);
476 pm_runtime_put_autosuspend(dev);
477
478 dev_info(dev, "RZ/G3E thermal sensor registered\n");
479
480 return 0;
481
482 err_pm_put:
483 pm_runtime_put_sync(dev);
484 return ret;
485 }
486
487 static int rzg3e_thermal_runtime_suspend(struct device *dev)
488 {
489 struct rzg3e_thermal_priv *priv = dev_get_drvdata(dev);
490
491 rzg3e_thermal_power_off(priv);
492 return 0;
493 }
494
495 static int rzg3e_thermal_runtime_resume(struct device *dev)
496 {
497 struct rzg3e_thermal_priv *priv = dev_get_drvdata(dev);
498
499 return rzg3e_thermal_power_on(priv);
500 }
501
502 static int rzg3e_thermal_suspend(struct device *dev)
503 {
504 struct rzg3e_thermal_priv *priv = dev_get_drvdata(dev);
505
506 /* If device is active, power it off */
507 if (pm_runtime_active(dev))
508 rzg3e_thermal_power_off(priv);
509
510 /* Assert reset to ensure clean state after resume */
511 reset_control_assert(priv->rstc);
512
513 return 0;
514 }
515
516 static int rzg3e_thermal_resume(struct device *dev)
517 {
518 struct rzg3e_thermal_priv *priv = dev_get_drvdata(dev);
519 int ret;
520
521 /* Deassert reset */
522 ret = reset_control_deassert(priv->rstc);
523 if (ret) {
524 dev_err(dev, "Failed to deassert reset: %d\n", ret);
525 return ret;
526 }
527
528 /* If device was active before suspend, power it back on */
529 if (pm_runtime_active(dev))
530 return rzg3e_thermal_power_on(priv);
531
532 return 0;
533 }
534
535 static const struct dev_pm_ops rzg3e_thermal_pm_ops = {
536 RUNTIME_PM_OPS(rzg3e_thermal_runtime_suspend,
537 rzg3e_thermal_runtime_resume, NULL)
538 SYSTEM_SLEEP_PM_OPS(rzg3e_thermal_suspend, rzg3e_thermal_resume)
539 };
540
541 static const struct rzg3e_thermal_info rzg3e_thermal_info = {
542 .get_trim = rzg3e_thermal_get_syscon_trim,
543 .temp_d_mc = -41000,
544 .temp_e_mc = 126000,
545 };
546
547 static const struct rzg3e_thermal_info rzt2h_thermal_info = {
548 .get_trim = rzg3e_thermal_get_smc_trim,
549 .temp_d_mc = -40000,
550 .temp_e_mc = 125000,
551 };
552
553 static const struct of_device_id rzg3e_thermal_dt_ids[] = {
554 { .compatible = "renesas,r9a09g047-tsu", .data = &rzg3e_thermal_info },
555 { .compatible = "renesas,r9a09g077-tsu", .data = &rzt2h_thermal_info },
556 { /* sentinel */ }
557 };
558 MODULE_DEVICE_TABLE(of, rzg3e_thermal_dt_ids);
559
560 static struct platform_driver rzg3e_thermal_driver = {
561 .driver = {
562 .name = "rzg3e_thermal",
563 .of_match_table = rzg3e_thermal_dt_ids,
564 .pm = pm_ptr(&rzg3e_thermal_pm_ops),
565 },
566 .probe = rzg3e_thermal_probe,
567 };
568 module_platform_driver(rzg3e_thermal_driver);
569
570 MODULE_DESCRIPTION("Renesas RZ/G3E TSU Thermal Sensor Driver");
571 MODULE_AUTHOR("John Madieu <[email protected]>");
572 MODULE_LICENSE("GPL");