1 // SPDX-License-Identifier: GPL-2.0
3 * Renesas RZ/G2L TSU Thermal Sensor Driver
5 * Copyright (C) 2021 Renesas Electronics Corporation
7 #include <linux/delay.h>
10 #include <linux/iopoll.h>
11 #include <linux/math.h>
12 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/reset.h>
17 #include <linux/thermal.h>
18 #include <linux/units.h>
20 #include "thermal_hwmon.h"
22 #define CTEMP_MASK 0xFFF
24 /* default calibration values, if FUSE values are missing */
25 #define SW_CALIB0_VAL 3148
26 #define SW_CALIB1_VAL 503
28 /* Register offsets */
34 #define OTPTSUTRIM_REG(n) (0x18 + ((n) * 0x4))
35 #define OTPTSUTRIM_EN_MASK BIT(31)
36 #define OTPTSUTRIM_MASK GENMASK(11, 0)
38 /* Sensor Mode Register(TSU_SM) */
39 #define TSU_SM_EN_TS BIT(0)
40 #define TSU_SM_ADC_EN_TS BIT(1)
41 #define TSU_SM_NORMAL_MODE (TSU_SM_EN_TS | TSU_SM_ADC_EN_TS)
44 #define TSU_ST_START BIT(0)
46 #define TSU_SS_CONV_RUNNING BIT(0)
48 #define TS_CODE_AVE_SCALE(x) ((x) * 1000000)
49 #define MCELSIUS(temp) ((temp) * MILLIDEGREE_PER_DEGREE)
50 #define TS_CODE_CAP_TIMES 8 /* Capture times */
52 #define RZG2L_THERMAL_GRAN 500 /* milli Celsius */
53 #define RZG2L_TSU_SS_TIMEOUT_US 1000
55 #define CURVATURE_CORRECTION_CONST 13
57 struct rzg2l_thermal_priv {
60 struct thermal_zone_device *zone;
61 struct reset_control *rstc;
65 static inline u32 rzg2l_thermal_read(struct rzg2l_thermal_priv *priv, u32 reg)
67 return ioread32(priv->base + reg);
70 static inline void rzg2l_thermal_write(struct rzg2l_thermal_priv *priv, u32 reg,
73 iowrite32(data, priv->base + reg);
76 static int rzg2l_thermal_get_temp(void *devdata, int *temp)
78 struct rzg2l_thermal_priv *priv = devdata;
79 u32 result = 0, dsensor, ts_code_ave;
82 for (i = 0; i < TS_CODE_CAP_TIMES ; i++) {
83 /* TSU repeats measurement at 20 microseconds intervals and
84 * automatically updates the results of measurement. As per
85 * the HW manual for measuring temperature we need to read 8
86 * values consecutively and then take the average.
87 * ts_code_ave = (ts_code[0] + ⋯ + ts_code[7]) / 8
89 result += rzg2l_thermal_read(priv, TSU_SAD) & CTEMP_MASK;
93 ts_code_ave = result / TS_CODE_CAP_TIMES;
95 /* Calculate actual sensor value by applying curvature correction formula
96 * dsensor = ts_code_ave / (1 + ts_code_ave * 0.000013). Here we are doing
97 * integer calculation by scaling all the values by 1000000.
99 dsensor = TS_CODE_AVE_SCALE(ts_code_ave) /
100 (TS_CODE_AVE_SCALE(1) + (ts_code_ave * CURVATURE_CORRECTION_CONST));
102 /* The temperature Tj is calculated by the formula
103 * Tj = (dsensor − calib1) * 165/ (calib0 − calib1) − 40
104 * where calib0 and calib1 are the caliberation values.
106 val = ((dsensor - priv->calib1) * (MCELSIUS(165) /
107 (priv->calib0 - priv->calib1))) - MCELSIUS(40);
109 *temp = roundup(val, RZG2L_THERMAL_GRAN);
114 static const struct thermal_zone_of_device_ops rzg2l_tz_of_ops = {
115 .get_temp = rzg2l_thermal_get_temp,
118 static int rzg2l_thermal_init(struct rzg2l_thermal_priv *priv)
122 rzg2l_thermal_write(priv, TSU_SM, TSU_SM_NORMAL_MODE);
123 rzg2l_thermal_write(priv, TSU_ST, 0);
125 /* Before setting the START bit, TSU should be in normal operating
126 * mode. As per the HW manual, it will take 60 µs to place the TSU
127 * into normal operating mode.
129 usleep_range(60, 80);
131 reg_val = rzg2l_thermal_read(priv, TSU_ST);
132 reg_val |= TSU_ST_START;
133 rzg2l_thermal_write(priv, TSU_ST, reg_val);
135 return readl_poll_timeout(priv->base + TSU_SS, reg_val,
136 reg_val == TSU_SS_CONV_RUNNING, 50,
137 RZG2L_TSU_SS_TIMEOUT_US);
140 static void rzg2l_thermal_reset_assert_pm_disable_put(struct platform_device *pdev)
142 struct rzg2l_thermal_priv *priv = dev_get_drvdata(&pdev->dev);
144 pm_runtime_put(&pdev->dev);
145 pm_runtime_disable(&pdev->dev);
146 reset_control_assert(priv->rstc);
149 static int rzg2l_thermal_remove(struct platform_device *pdev)
151 struct rzg2l_thermal_priv *priv = dev_get_drvdata(&pdev->dev);
153 thermal_remove_hwmon_sysfs(priv->zone);
154 rzg2l_thermal_reset_assert_pm_disable_put(pdev);
159 static int rzg2l_thermal_probe(struct platform_device *pdev)
161 struct thermal_zone_device *zone;
162 struct rzg2l_thermal_priv *priv;
163 struct device *dev = &pdev->dev;
166 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
170 priv->base = devm_platform_ioremap_resource(pdev, 0);
171 if (IS_ERR(priv->base))
172 return PTR_ERR(priv->base);
175 priv->rstc = devm_reset_control_get_exclusive(dev, NULL);
176 if (IS_ERR(priv->rstc))
177 return dev_err_probe(dev, PTR_ERR(priv->rstc),
178 "failed to get cpg reset");
180 ret = reset_control_deassert(priv->rstc);
182 return dev_err_probe(dev, ret, "failed to deassert");
184 pm_runtime_enable(dev);
185 pm_runtime_get_sync(dev);
187 priv->calib0 = rzg2l_thermal_read(priv, OTPTSUTRIM_REG(0));
188 if (priv->calib0 & OTPTSUTRIM_EN_MASK)
189 priv->calib0 &= OTPTSUTRIM_MASK;
191 priv->calib0 = SW_CALIB0_VAL;
193 priv->calib1 = rzg2l_thermal_read(priv, OTPTSUTRIM_REG(1));
194 if (priv->calib1 & OTPTSUTRIM_EN_MASK)
195 priv->calib1 &= OTPTSUTRIM_MASK;
197 priv->calib1 = SW_CALIB1_VAL;
199 platform_set_drvdata(pdev, priv);
200 ret = rzg2l_thermal_init(priv);
202 dev_err(dev, "Failed to start TSU");
206 zone = devm_thermal_zone_of_sensor_register(dev, 0, priv,
209 dev_err(dev, "Can't register thermal zone");
215 priv->zone->tzp->no_hwmon = false;
216 ret = thermal_add_hwmon_sysfs(priv->zone);
220 dev_dbg(dev, "TSU probed with %s caliberation values",
221 rzg2l_thermal_read(priv, OTPTSUTRIM_REG(0)) ? "hw" : "sw");
226 rzg2l_thermal_reset_assert_pm_disable_put(pdev);
230 static const struct of_device_id rzg2l_thermal_dt_ids[] = {
231 { .compatible = "renesas,rzg2l-tsu", },
234 MODULE_DEVICE_TABLE(of, rzg2l_thermal_dt_ids);
236 static struct platform_driver rzg2l_thermal_driver = {
238 .name = "rzg2l_thermal",
239 .of_match_table = rzg2l_thermal_dt_ids,
241 .probe = rzg2l_thermal_probe,
242 .remove = rzg2l_thermal_remove,
244 module_platform_driver(rzg2l_thermal_driver);
246 MODULE_DESCRIPTION("Renesas RZ/G2L TSU Thermal Sensor Driver");
247 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
248 MODULE_LICENSE("GPL v2");