Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Renesas RZ/G2L TSU Thermal Sensor Driver
0004  *
0005  * Copyright (C) 2021 Renesas Electronics Corporation
0006  */
0007 #include <linux/delay.h>
0008 #include <linux/err.h>
0009 #include <linux/io.h>
0010 #include <linux/iopoll.h>
0011 #include <linux/math.h>
0012 #include <linux/module.h>
0013 #include <linux/of_device.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/pm_runtime.h>
0016 #include <linux/reset.h>
0017 #include <linux/thermal.h>
0018 #include <linux/units.h>
0019 
0020 #include "thermal_hwmon.h"
0021 
0022 #define CTEMP_MASK  0xFFF
0023 
0024 /* default calibration values, if FUSE values are missing */
0025 #define SW_CALIB0_VAL   3148
0026 #define SW_CALIB1_VAL   503
0027 
0028 /* Register offsets */
0029 #define TSU_SM      0x00
0030 #define TSU_ST      0x04
0031 #define TSU_SAD     0x0C
0032 #define TSU_SS      0x10
0033 
0034 #define OTPTSUTRIM_REG(n)   (0x18 + ((n) * 0x4))
0035 #define OTPTSUTRIM_EN_MASK  BIT(31)
0036 #define OTPTSUTRIM_MASK     GENMASK(11, 0)
0037 
0038 /* Sensor Mode Register(TSU_SM) */
0039 #define TSU_SM_EN_TS        BIT(0)
0040 #define TSU_SM_ADC_EN_TS    BIT(1)
0041 #define TSU_SM_NORMAL_MODE  (TSU_SM_EN_TS | TSU_SM_ADC_EN_TS)
0042 
0043 /* TSU_ST bits */
0044 #define TSU_ST_START        BIT(0)
0045 
0046 #define TSU_SS_CONV_RUNNING BIT(0)
0047 
0048 #define TS_CODE_AVE_SCALE(x)    ((x) * 1000000)
0049 #define MCELSIUS(temp)      ((temp) * MILLIDEGREE_PER_DEGREE)
0050 #define TS_CODE_CAP_TIMES   8   /* Total number of ADC data samples */
0051 
0052 #define RZG2L_THERMAL_GRAN  500 /* milli Celsius */
0053 #define RZG2L_TSU_SS_TIMEOUT_US 1000
0054 
0055 #define CURVATURE_CORRECTION_CONST  13
0056 
0057 struct rzg2l_thermal_priv {
0058     struct device *dev;
0059     void __iomem *base;
0060     struct thermal_zone_device *zone;
0061     struct reset_control *rstc;
0062     u32 calib0, calib1;
0063 };
0064 
0065 static inline u32 rzg2l_thermal_read(struct rzg2l_thermal_priv *priv, u32 reg)
0066 {
0067     return ioread32(priv->base + reg);
0068 }
0069 
0070 static inline void rzg2l_thermal_write(struct rzg2l_thermal_priv *priv, u32 reg,
0071                        u32 data)
0072 {
0073     iowrite32(data, priv->base + reg);
0074 }
0075 
0076 static int rzg2l_thermal_get_temp(void *devdata, int *temp)
0077 {
0078     struct rzg2l_thermal_priv *priv = devdata;
0079     u32 result = 0, dsensor, ts_code_ave;
0080     int val, i;
0081 
0082     for (i = 0; i < TS_CODE_CAP_TIMES ; i++) {
0083         /*
0084          * TSU repeats measurement at 20 microseconds intervals and
0085          * automatically updates the results of measurement. As per
0086          * the HW manual for measuring temperature we need to read 8
0087          * values consecutively and then take the average.
0088          * ts_code_ave = (ts_code[0] + ⋯ + ts_code[7]) / 8
0089          */
0090         result += rzg2l_thermal_read(priv, TSU_SAD) & CTEMP_MASK;
0091         usleep_range(20, 30);
0092     }
0093 
0094     ts_code_ave = result / TS_CODE_CAP_TIMES;
0095 
0096     /*
0097      * Calculate actual sensor value by applying curvature correction formula
0098      * dsensor = ts_code_ave / (1 + ts_code_ave * 0.000013). Here we are doing
0099      * integer calculation by scaling all the values by 1000000.
0100      */
0101     dsensor = TS_CODE_AVE_SCALE(ts_code_ave) /
0102         (TS_CODE_AVE_SCALE(1) + (ts_code_ave * CURVATURE_CORRECTION_CONST));
0103 
0104     /*
0105      * The temperature Tj is calculated by the formula
0106      * Tj = (dsensor − calib1) * 165/ (calib0 − calib1) − 40
0107      * where calib0 and calib1 are the calibration values.
0108      */
0109     val = ((dsensor - priv->calib1) * (MCELSIUS(165) /
0110         (priv->calib0 - priv->calib1))) - MCELSIUS(40);
0111 
0112     *temp = roundup(val, RZG2L_THERMAL_GRAN);
0113 
0114     return 0;
0115 }
0116 
0117 static const struct thermal_zone_of_device_ops rzg2l_tz_of_ops = {
0118     .get_temp = rzg2l_thermal_get_temp,
0119 };
0120 
0121 static int rzg2l_thermal_init(struct rzg2l_thermal_priv *priv)
0122 {
0123     u32 reg_val;
0124 
0125     rzg2l_thermal_write(priv, TSU_SM, TSU_SM_NORMAL_MODE);
0126     rzg2l_thermal_write(priv, TSU_ST, 0);
0127 
0128     /*
0129      * Before setting the START bit, TSU should be in normal operating
0130      * mode. As per the HW manual, it will take 60 µs to place the TSU
0131      * into normal operating mode.
0132      */
0133     usleep_range(60, 80);
0134 
0135     reg_val = rzg2l_thermal_read(priv, TSU_ST);
0136     reg_val |= TSU_ST_START;
0137     rzg2l_thermal_write(priv, TSU_ST, reg_val);
0138 
0139     return readl_poll_timeout(priv->base + TSU_SS, reg_val,
0140                   reg_val == TSU_SS_CONV_RUNNING, 50,
0141                   RZG2L_TSU_SS_TIMEOUT_US);
0142 }
0143 
0144 static void rzg2l_thermal_reset_assert_pm_disable_put(struct platform_device *pdev)
0145 {
0146     struct rzg2l_thermal_priv *priv = dev_get_drvdata(&pdev->dev);
0147 
0148     pm_runtime_put(&pdev->dev);
0149     pm_runtime_disable(&pdev->dev);
0150     reset_control_assert(priv->rstc);
0151 }
0152 
0153 static int rzg2l_thermal_remove(struct platform_device *pdev)
0154 {
0155     struct rzg2l_thermal_priv *priv = dev_get_drvdata(&pdev->dev);
0156 
0157     thermal_remove_hwmon_sysfs(priv->zone);
0158     rzg2l_thermal_reset_assert_pm_disable_put(pdev);
0159 
0160     return 0;
0161 }
0162 
0163 static int rzg2l_thermal_probe(struct platform_device *pdev)
0164 {
0165     struct thermal_zone_device *zone;
0166     struct rzg2l_thermal_priv *priv;
0167     struct device *dev = &pdev->dev;
0168     int ret;
0169 
0170     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0171     if (!priv)
0172         return -ENOMEM;
0173 
0174     priv->base = devm_platform_ioremap_resource(pdev, 0);
0175     if (IS_ERR(priv->base))
0176         return PTR_ERR(priv->base);
0177 
0178     priv->dev = dev;
0179     priv->rstc = devm_reset_control_get_exclusive(dev, NULL);
0180     if (IS_ERR(priv->rstc))
0181         return dev_err_probe(dev, PTR_ERR(priv->rstc),
0182                      "failed to get cpg reset");
0183 
0184     ret = reset_control_deassert(priv->rstc);
0185     if (ret)
0186         return dev_err_probe(dev, ret, "failed to deassert");
0187 
0188     pm_runtime_enable(dev);
0189     pm_runtime_get_sync(dev);
0190 
0191     priv->calib0 = rzg2l_thermal_read(priv, OTPTSUTRIM_REG(0));
0192     if (priv->calib0 & OTPTSUTRIM_EN_MASK)
0193         priv->calib0 &= OTPTSUTRIM_MASK;
0194     else
0195         priv->calib0 = SW_CALIB0_VAL;
0196 
0197     priv->calib1 = rzg2l_thermal_read(priv, OTPTSUTRIM_REG(1));
0198     if (priv->calib1 & OTPTSUTRIM_EN_MASK)
0199         priv->calib1 &= OTPTSUTRIM_MASK;
0200     else
0201         priv->calib1 = SW_CALIB1_VAL;
0202 
0203     platform_set_drvdata(pdev, priv);
0204     ret = rzg2l_thermal_init(priv);
0205     if (ret) {
0206         dev_err(dev, "Failed to start TSU");
0207         goto err;
0208     }
0209 
0210     zone = devm_thermal_zone_of_sensor_register(dev, 0, priv,
0211                             &rzg2l_tz_of_ops);
0212     if (IS_ERR(zone)) {
0213         dev_err(dev, "Can't register thermal zone");
0214         ret = PTR_ERR(zone);
0215         goto err;
0216     }
0217 
0218     priv->zone = zone;
0219     priv->zone->tzp->no_hwmon = false;
0220     ret = thermal_add_hwmon_sysfs(priv->zone);
0221     if (ret)
0222         goto err;
0223 
0224     dev_dbg(dev, "TSU probed with %s calibration values",
0225         rzg2l_thermal_read(priv, OTPTSUTRIM_REG(0)) ?  "hw" : "sw");
0226 
0227     return 0;
0228 
0229 err:
0230     rzg2l_thermal_reset_assert_pm_disable_put(pdev);
0231     return ret;
0232 }
0233 
0234 static const struct of_device_id rzg2l_thermal_dt_ids[] = {
0235     { .compatible = "renesas,rzg2l-tsu", },
0236     { /* sentinel */ }
0237 };
0238 MODULE_DEVICE_TABLE(of, rzg2l_thermal_dt_ids);
0239 
0240 static struct platform_driver rzg2l_thermal_driver = {
0241     .driver = {
0242         .name = "rzg2l_thermal",
0243         .of_match_table = rzg2l_thermal_dt_ids,
0244     },
0245     .probe = rzg2l_thermal_probe,
0246     .remove = rzg2l_thermal_remove,
0247 };
0248 module_platform_driver(rzg2l_thermal_driver);
0249 
0250 MODULE_DESCRIPTION("Renesas RZ/G2L TSU Thermal Sensor Driver");
0251 MODULE_AUTHOR("Biju Das <biju.das.jz@bp.renesas.com>");
0252 MODULE_LICENSE("GPL v2");