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;
69 if (*temp < VER1_TEMP_LOW_LIMIT)
75 static int imx8mp_tmu_get_temp(void *data, int *temp)
77 struct tmu_sensor *sensor = data;
78 struct imx8mm_tmu *tmu = sensor->priv;
82 val = readl_relaxed(tmu->base + TRITSR);
83 ready = test_bit(probe_status_offset(sensor->hw_id), &val);
87 val = sensor->hw_id ? FIELD_GET(TRITSR_TEMP1_VAL_MASK, val) :
88 FIELD_GET(TRITSR_TEMP0_VAL_MASK, val);
89 if (val & SIGN_BIT) /* negative */
90 val = (~(val & TEMP_VAL_MASK) + 1);
93 if (*temp < VER2_TEMP_LOW_LIMIT || *temp > VER2_TEMP_HIGH_LIMIT)
99 static int tmu_get_temp(struct thermal_zone_device *tz, int *temp)
101 struct tmu_sensor *sensor = tz->devdata;
102 struct imx8mm_tmu *tmu = sensor->priv;
104 return tmu->socdata->get_temp(sensor, temp);
107 static const struct thermal_zone_device_ops tmu_tz_ops = {
108 .get_temp = tmu_get_temp,
111 static void imx8mm_tmu_enable(struct imx8mm_tmu *tmu, bool enable)
115 val = readl_relaxed(tmu->base + TER);
116 val = enable ? (val | TER_EN) : (val & ~TER_EN);
117 if (tmu->socdata->version == TMU_VER2)
118 val = enable ? (val & ~TER_ADC_PD) : (val | TER_ADC_PD);
119 writel_relaxed(val, tmu->base + TER);
122 static void imx8mm_tmu_probe_sel_all(struct imx8mm_tmu *tmu)
126 val = readl_relaxed(tmu->base + TPS);
127 val |= PROBE_SEL_ALL;
128 writel_relaxed(val, tmu->base + TPS);
131 static int imx8mm_tmu_probe(struct platform_device *pdev)
133 const struct thermal_soc_data *data;
134 struct imx8mm_tmu *tmu;
138 data = of_device_get_match_data(&pdev->dev);
140 tmu = devm_kzalloc(&pdev->dev, struct_size(tmu, sensors,
141 data->num_sensors), GFP_KERNEL);
147 tmu->base = devm_platform_ioremap_resource(pdev, 0);
148 if (IS_ERR(tmu->base))
149 return PTR_ERR(tmu->base);
151 tmu->clk = devm_clk_get(&pdev->dev, NULL);
152 if (IS_ERR(tmu->clk))
153 return dev_err_probe(&pdev->dev, PTR_ERR(tmu->clk),
154 "failed to get tmu clock\n");
156 ret = clk_prepare_enable(tmu->clk);
158 dev_err(&pdev->dev, "failed to enable tmu clock: %d\n", ret);
162 /* disable the monitor during initialization */
163 imx8mm_tmu_enable(tmu, false);
165 for (i = 0; i < data->num_sensors; i++) {
166 tmu->sensors[i].priv = tmu;
167 tmu->sensors[i].tzd =
168 devm_thermal_of_zone_register(&pdev->dev, i,
171 if (IS_ERR(tmu->sensors[i].tzd)) {
172 ret = PTR_ERR(tmu->sensors[i].tzd);
174 "failed to register thermal zone sensor[%d]: %d\n",
178 tmu->sensors[i].hw_id = i;
181 platform_set_drvdata(pdev, tmu);
183 /* enable all the probes for V2 TMU */
184 if (tmu->socdata->version == TMU_VER2)
185 imx8mm_tmu_probe_sel_all(tmu);
187 /* enable the monitor */
188 imx8mm_tmu_enable(tmu, true);
193 clk_disable_unprepare(tmu->clk);
197 static int imx8mm_tmu_remove(struct platform_device *pdev)
199 struct imx8mm_tmu *tmu = platform_get_drvdata(pdev);
202 imx8mm_tmu_enable(tmu, false);
204 clk_disable_unprepare(tmu->clk);
205 platform_set_drvdata(pdev, NULL);
210 static struct thermal_soc_data imx8mm_tmu_data = {
213 .get_temp = imx8mm_tmu_get_temp,
216 static struct thermal_soc_data imx8mp_tmu_data = {
219 .get_temp = imx8mp_tmu_get_temp,
222 static const struct of_device_id imx8mm_tmu_table[] = {
223 { .compatible = "fsl,imx8mm-tmu", .data = &imx8mm_tmu_data, },
224 { .compatible = "fsl,imx8mp-tmu", .data = &imx8mp_tmu_data, },
227 MODULE_DEVICE_TABLE(of, imx8mm_tmu_table);
229 static struct platform_driver imx8mm_tmu = {
231 .name = "i.mx8mm_thermal",
232 .of_match_table = imx8mm_tmu_table,
234 .probe = imx8mm_tmu_probe,
235 .remove = imx8mm_tmu_remove,
237 module_platform_driver(imx8mm_tmu);
239 MODULE_AUTHOR("Anson Huang <Anson.Huang@nxp.com>");
240 MODULE_DESCRIPTION("i.MX8MM Thermal Monitor Unit driver");
241 MODULE_LICENSE("GPL v2");