1 // SPDX-License-Identifier: GPL-2.0
5 * Author: Anson Huang <Anson.Huang@nxp.com>
8 #include <linux/bitfield.h>
10 #include <linux/err.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
16 #include <linux/thermal.h>
18 #include "thermal_core.h"
20 #define TER 0x0 /* TMU enable */
22 #define TRITSR 0x20 /* TMU immediate temp */
24 #define TER_ADC_PD BIT(30)
25 #define TER_EN BIT(31)
26 #define TRITSR_TEMP0_VAL_MASK 0xff
27 #define TRITSR_TEMP1_VAL_MASK 0xff0000
29 #define PROBE_SEL_ALL GENMASK(31, 30)
31 #define probe_status_offset(x) (30 + x)
32 #define SIGN_BIT BIT(7)
33 #define TEMP_VAL_MASK GENMASK(6, 0)
35 #define VER1_TEMP_LOW_LIMIT 10000
36 #define VER2_TEMP_LOW_LIMIT -40000
37 #define VER2_TEMP_HIGH_LIMIT 125000
42 struct thermal_soc_data {
45 int (*get_temp)(void *, int *);
49 struct imx8mm_tmu *priv;
51 struct thermal_zone_device *tzd;
57 const struct thermal_soc_data *socdata;
58 struct tmu_sensor sensors[];
61 static int imx8mm_tmu_get_temp(void *data, int *temp)
63 struct tmu_sensor *sensor = data;
64 struct imx8mm_tmu *tmu = sensor->priv;
67 val = readl_relaxed(tmu->base + TRITSR) & TRITSR_TEMP0_VAL_MASK;
70 * Do not validate against the V bit (bit 31) due to errata
71 * ERR051272: TMU: Bit 31 of registers TMU_TSCR/TMU_TRITSR/TMU_TRATSR invalid
75 if (*temp < VER1_TEMP_LOW_LIMIT || *temp > VER2_TEMP_HIGH_LIMIT)
81 static int imx8mp_tmu_get_temp(void *data, int *temp)
83 struct tmu_sensor *sensor = data;
84 struct imx8mm_tmu *tmu = sensor->priv;
88 val = readl_relaxed(tmu->base + TRITSR);
89 ready = test_bit(probe_status_offset(sensor->hw_id), &val);
93 val = sensor->hw_id ? FIELD_GET(TRITSR_TEMP1_VAL_MASK, val) :
94 FIELD_GET(TRITSR_TEMP0_VAL_MASK, val);
95 if (val & SIGN_BIT) /* negative */
96 val = (~(val & TEMP_VAL_MASK) + 1);
99 if (*temp < VER2_TEMP_LOW_LIMIT || *temp > VER2_TEMP_HIGH_LIMIT)
105 static int tmu_get_temp(struct thermal_zone_device *tz, int *temp)
107 struct tmu_sensor *sensor = tz->devdata;
108 struct imx8mm_tmu *tmu = sensor->priv;
110 return tmu->socdata->get_temp(sensor, temp);
113 static const struct thermal_zone_device_ops tmu_tz_ops = {
114 .get_temp = tmu_get_temp,
117 static void imx8mm_tmu_enable(struct imx8mm_tmu *tmu, bool enable)
121 val = readl_relaxed(tmu->base + TER);
122 val = enable ? (val | TER_EN) : (val & ~TER_EN);
123 if (tmu->socdata->version == TMU_VER2)
124 val = enable ? (val & ~TER_ADC_PD) : (val | TER_ADC_PD);
125 writel_relaxed(val, tmu->base + TER);
128 static void imx8mm_tmu_probe_sel_all(struct imx8mm_tmu *tmu)
132 val = readl_relaxed(tmu->base + TPS);
133 val |= PROBE_SEL_ALL;
134 writel_relaxed(val, tmu->base + TPS);
137 static int imx8mm_tmu_probe(struct platform_device *pdev)
139 const struct thermal_soc_data *data;
140 struct imx8mm_tmu *tmu;
144 data = of_device_get_match_data(&pdev->dev);
146 tmu = devm_kzalloc(&pdev->dev, struct_size(tmu, sensors,
147 data->num_sensors), GFP_KERNEL);
153 tmu->base = devm_platform_ioremap_resource(pdev, 0);
154 if (IS_ERR(tmu->base))
155 return PTR_ERR(tmu->base);
157 tmu->clk = devm_clk_get(&pdev->dev, NULL);
158 if (IS_ERR(tmu->clk))
159 return dev_err_probe(&pdev->dev, PTR_ERR(tmu->clk),
160 "failed to get tmu clock\n");
162 ret = clk_prepare_enable(tmu->clk);
164 dev_err(&pdev->dev, "failed to enable tmu clock: %d\n", ret);
168 /* disable the monitor during initialization */
169 imx8mm_tmu_enable(tmu, false);
171 for (i = 0; i < data->num_sensors; i++) {
172 tmu->sensors[i].priv = tmu;
173 tmu->sensors[i].tzd =
174 devm_thermal_of_zone_register(&pdev->dev, i,
177 if (IS_ERR(tmu->sensors[i].tzd)) {
178 ret = PTR_ERR(tmu->sensors[i].tzd);
180 "failed to register thermal zone sensor[%d]: %d\n",
184 tmu->sensors[i].hw_id = i;
187 platform_set_drvdata(pdev, tmu);
189 /* enable all the probes for V2 TMU */
190 if (tmu->socdata->version == TMU_VER2)
191 imx8mm_tmu_probe_sel_all(tmu);
193 /* enable the monitor */
194 imx8mm_tmu_enable(tmu, true);
199 clk_disable_unprepare(tmu->clk);
203 static int imx8mm_tmu_remove(struct platform_device *pdev)
205 struct imx8mm_tmu *tmu = platform_get_drvdata(pdev);
208 imx8mm_tmu_enable(tmu, false);
210 clk_disable_unprepare(tmu->clk);
211 platform_set_drvdata(pdev, NULL);
216 static struct thermal_soc_data imx8mm_tmu_data = {
219 .get_temp = imx8mm_tmu_get_temp,
222 static struct thermal_soc_data imx8mp_tmu_data = {
225 .get_temp = imx8mp_tmu_get_temp,
228 static const struct of_device_id imx8mm_tmu_table[] = {
229 { .compatible = "fsl,imx8mm-tmu", .data = &imx8mm_tmu_data, },
230 { .compatible = "fsl,imx8mp-tmu", .data = &imx8mp_tmu_data, },
233 MODULE_DEVICE_TABLE(of, imx8mm_tmu_table);
235 static struct platform_driver imx8mm_tmu = {
237 .name = "i.mx8mm_thermal",
238 .of_match_table = imx8mm_tmu_table,
240 .probe = imx8mm_tmu_probe,
241 .remove = imx8mm_tmu_remove,
243 module_platform_driver(imx8mm_tmu);
245 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
246 MODULE_DESCRIPTION("i.MX8MM Thermal Monitor Unit driver");
247 MODULE_LICENSE("GPL v2");