1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright 2016 Freescale Semiconductor, Inc.
4 
5 #include <linux/module.h>
6 #include <linux/platform_device.h>
7 #include <linux/err.h>
8 #include <linux/io.h>
9 #include <linux/of.h>
10 #include <linux/of_address.h>
11 #include <linux/thermal.h>
12 
13 #include "thermal_core.h"
14 
15 #define SITES_MAX	16
16 
17 /*
18  * QorIQ TMU Registers
19  */
20 struct qoriq_tmu_site_regs {
21 	u32 tritsr;		/* Immediate Temperature Site Register */
22 	u32 tratsr;		/* Average Temperature Site Register */
23 	u8 res0[0x8];
24 };
25 
26 struct qoriq_tmu_regs {
27 	u32 tmr;		/* Mode Register */
28 #define TMR_DISABLE	0x0
29 #define TMR_ME		0x80000000
30 #define TMR_ALPF	0x0c000000
31 	u32 tsr;		/* Status Register */
32 	u32 tmtmir;		/* Temperature measurement interval Register */
33 #define TMTMIR_DEFAULT	0x0000000f
34 	u8 res0[0x14];
35 	u32 tier;		/* Interrupt Enable Register */
36 #define TIER_DISABLE	0x0
37 	u32 tidr;		/* Interrupt Detect Register */
38 	u32 tiscr;		/* Interrupt Site Capture Register */
39 	u32 ticscr;		/* Interrupt Critical Site Capture Register */
40 	u8 res1[0x10];
41 	u32 tmhtcrh;		/* High Temperature Capture Register */
42 	u32 tmhtcrl;		/* Low Temperature Capture Register */
43 	u8 res2[0x8];
44 	u32 tmhtitr;		/* High Temperature Immediate Threshold */
45 	u32 tmhtatr;		/* High Temperature Average Threshold */
46 	u32 tmhtactr;	/* High Temperature Average Crit Threshold */
47 	u8 res3[0x24];
48 	u32 ttcfgr;		/* Temperature Configuration Register */
49 	u32 tscfgr;		/* Sensor Configuration Register */
50 	u8 res4[0x78];
51 	struct qoriq_tmu_site_regs site[SITES_MAX];
52 	u8 res5[0x9f8];
53 	u32 ipbrr0;		/* IP Block Revision Register 0 */
54 	u32 ipbrr1;		/* IP Block Revision Register 1 */
55 	u8 res6[0x310];
56 	u32 ttr0cr;		/* Temperature Range 0 Control Register */
57 	u32 ttr1cr;		/* Temperature Range 1 Control Register */
58 	u32 ttr2cr;		/* Temperature Range 2 Control Register */
59 	u32 ttr3cr;		/* Temperature Range 3 Control Register */
60 };
61 
62 /*
63  * Thermal zone data
64  */
65 struct qoriq_tmu_data {
66 	struct thermal_zone_device *tz;
67 	struct qoriq_tmu_regs __iomem *regs;
68 	int sensor_id;
69 	bool little_endian;
70 };
71 
72 static void tmu_write(struct qoriq_tmu_data *p, u32 val, void __iomem *addr)
73 {
74 	if (p->little_endian)
75 		iowrite32(val, addr);
76 	else
77 		iowrite32be(val, addr);
78 }
79 
80 static u32 tmu_read(struct qoriq_tmu_data *p, void __iomem *addr)
81 {
82 	if (p->little_endian)
83 		return ioread32(addr);
84 	else
85 		return ioread32be(addr);
86 }
87 
88 static int tmu_get_temp(void *p, int *temp)
89 {
90 	u32 val;
91 	struct qoriq_tmu_data *data = p;
92 
93 	val = tmu_read(data, &data->regs->site[data->sensor_id].tritsr);
94 	*temp = (val & 0xff) * 1000;
95 
96 	return 0;
97 }
98 
99 static int qoriq_tmu_get_sensor_id(void)
100 {
101 	int ret, id;
102 	struct of_phandle_args sensor_specs;
103 	struct device_node *np, *sensor_np;
104 
105 	np = of_find_node_by_name(NULL, "thermal-zones");
106 	if (!np)
107 		return -ENODEV;
108 
109 	sensor_np = of_get_next_child(np, NULL);
110 	ret = of_parse_phandle_with_args(sensor_np, "thermal-sensors",
111 			"#thermal-sensor-cells",
112 			0, &sensor_specs);
113 	if (ret) {
114 		of_node_put(np);
115 		of_node_put(sensor_np);
116 		return ret;
117 	}
118 
119 	if (sensor_specs.args_count >= 1) {
120 		id = sensor_specs.args[0];
121 		WARN(sensor_specs.args_count > 1,
122 				"%pOFn: too many cells in sensor specifier %d\n",
123 				sensor_specs.np, sensor_specs.args_count);
124 	} else {
125 		id = 0;
126 	}
127 
128 	of_node_put(np);
129 	of_node_put(sensor_np);
130 
131 	return id;
132 }
133 
134 static int qoriq_tmu_calibration(struct platform_device *pdev)
135 {
136 	int i, val, len;
137 	u32 range[4];
138 	const u32 *calibration;
139 	struct device_node *np = pdev->dev.of_node;
140 	struct qoriq_tmu_data *data = platform_get_drvdata(pdev);
141 
142 	if (of_property_read_u32_array(np, "fsl,tmu-range", range, 4)) {
143 		dev_err(&pdev->dev, "missing calibration range.\n");
144 		return -ENODEV;
145 	}
146 
147 	/* Init temperature range registers */
148 	tmu_write(data, range[0], &data->regs->ttr0cr);
149 	tmu_write(data, range[1], &data->regs->ttr1cr);
150 	tmu_write(data, range[2], &data->regs->ttr2cr);
151 	tmu_write(data, range[3], &data->regs->ttr3cr);
152 
153 	calibration = of_get_property(np, "fsl,tmu-calibration", &len);
154 	if (calibration == NULL || len % 8) {
155 		dev_err(&pdev->dev, "invalid calibration data.\n");
156 		return -ENODEV;
157 	}
158 
159 	for (i = 0; i < len; i += 8, calibration += 2) {
160 		val = of_read_number(calibration, 1);
161 		tmu_write(data, val, &data->regs->ttcfgr);
162 		val = of_read_number(calibration + 1, 1);
163 		tmu_write(data, val, &data->regs->tscfgr);
164 	}
165 
166 	return 0;
167 }
168 
169 static void qoriq_tmu_init_device(struct qoriq_tmu_data *data)
170 {
171 	/* Disable interrupt, using polling instead */
172 	tmu_write(data, TIER_DISABLE, &data->regs->tier);
173 
174 	/* Set update_interval */
175 	tmu_write(data, TMTMIR_DEFAULT, &data->regs->tmtmir);
176 
177 	/* Disable monitoring */
178 	tmu_write(data, TMR_DISABLE, &data->regs->tmr);
179 }
180 
181 static const struct thermal_zone_of_device_ops tmu_tz_ops = {
182 	.get_temp = tmu_get_temp,
183 };
184 
185 static int qoriq_tmu_probe(struct platform_device *pdev)
186 {
187 	int ret;
188 	struct qoriq_tmu_data *data;
189 	struct device_node *np = pdev->dev.of_node;
190 	u32 site;
191 
192 	if (!np) {
193 		dev_err(&pdev->dev, "Device OF-Node is NULL");
194 		return -ENODEV;
195 	}
196 
197 	data = devm_kzalloc(&pdev->dev, sizeof(struct qoriq_tmu_data),
198 			    GFP_KERNEL);
199 	if (!data)
200 		return -ENOMEM;
201 
202 	platform_set_drvdata(pdev, data);
203 
204 	data->little_endian = of_property_read_bool(np, "little-endian");
205 
206 	data->sensor_id = qoriq_tmu_get_sensor_id();
207 	if (data->sensor_id < 0) {
208 		dev_err(&pdev->dev, "Failed to get sensor id\n");
209 		ret = -ENODEV;
210 		goto err_iomap;
211 	}
212 
213 	data->regs = of_iomap(np, 0);
214 	if (!data->regs) {
215 		dev_err(&pdev->dev, "Failed to get memory region\n");
216 		ret = -ENODEV;
217 		goto err_iomap;
218 	}
219 
220 	qoriq_tmu_init_device(data);	/* TMU initialization */
221 
222 	ret = qoriq_tmu_calibration(pdev);	/* TMU calibration */
223 	if (ret < 0)
224 		goto err_tmu;
225 
226 	data->tz = devm_thermal_zone_of_sensor_register(&pdev->dev,
227 							data->sensor_id,
228 							data, &tmu_tz_ops);
229 	if (IS_ERR(data->tz)) {
230 		ret = PTR_ERR(data->tz);
231 		dev_err(&pdev->dev,
232 			"Failed to register thermal zone device %d\n", ret);
233 		goto err_tmu;
234 	}
235 
236 	/* Enable monitoring */
237 	site = 0x1 << (15 - data->sensor_id);
238 	tmu_write(data, site | TMR_ME | TMR_ALPF, &data->regs->tmr);
239 
240 	return 0;
241 
242 err_tmu:
243 	iounmap(data->regs);
244 
245 err_iomap:
246 	platform_set_drvdata(pdev, NULL);
247 
248 	return ret;
249 }
250 
251 static int qoriq_tmu_remove(struct platform_device *pdev)
252 {
253 	struct qoriq_tmu_data *data = platform_get_drvdata(pdev);
254 
255 	/* Disable monitoring */
256 	tmu_write(data, TMR_DISABLE, &data->regs->tmr);
257 
258 	iounmap(data->regs);
259 	platform_set_drvdata(pdev, NULL);
260 
261 	return 0;
262 }
263 
264 #ifdef CONFIG_PM_SLEEP
265 static int qoriq_tmu_suspend(struct device *dev)
266 {
267 	u32 tmr;
268 	struct qoriq_tmu_data *data = dev_get_drvdata(dev);
269 
270 	/* Disable monitoring */
271 	tmr = tmu_read(data, &data->regs->tmr);
272 	tmr &= ~TMR_ME;
273 	tmu_write(data, tmr, &data->regs->tmr);
274 
275 	return 0;
276 }
277 
278 static int qoriq_tmu_resume(struct device *dev)
279 {
280 	u32 tmr;
281 	struct qoriq_tmu_data *data = dev_get_drvdata(dev);
282 
283 	/* Enable monitoring */
284 	tmr = tmu_read(data, &data->regs->tmr);
285 	tmr |= TMR_ME;
286 	tmu_write(data, tmr, &data->regs->tmr);
287 
288 	return 0;
289 }
290 #endif
291 
292 static SIMPLE_DEV_PM_OPS(qoriq_tmu_pm_ops,
293 			 qoriq_tmu_suspend, qoriq_tmu_resume);
294 
295 static const struct of_device_id qoriq_tmu_match[] = {
296 	{ .compatible = "fsl,qoriq-tmu", },
297 	{ .compatible = "fsl,imx8mq-tmu", },
298 	{},
299 };
300 MODULE_DEVICE_TABLE(of, qoriq_tmu_match);
301 
302 static struct platform_driver qoriq_tmu = {
303 	.driver	= {
304 		.name		= "qoriq_thermal",
305 		.pm		= &qoriq_tmu_pm_ops,
306 		.of_match_table	= qoriq_tmu_match,
307 	},
308 	.probe	= qoriq_tmu_probe,
309 	.remove	= qoriq_tmu_remove,
310 };
311 module_platform_driver(qoriq_tmu);
312 
313 MODULE_AUTHOR("Jia Hongtao <hongtao.jia@nxp.com>");
314 MODULE_DESCRIPTION("QorIQ Thermal Monitoring Unit driver");
315 MODULE_LICENSE("GPL v2");
316