xref: /linux/drivers/thermal/uniphier_thermal.c (revision 79b2027097dd3046ff95a2701af441405f1ff298)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * uniphier_thermal.c - Socionext UniPhier thermal driver
4  * Copyright 2014      Panasonic Corporation
5  * Copyright 2016-2017 Socionext Inc.
6  * Author:
7  *	Kunihiko Hayashi <hayashi.kunihiko@socionext.com>
8  */
9 
10 #include <linux/bitops.h>
11 #include <linux/interrupt.h>
12 #include <linux/mfd/syscon.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/thermal.h>
19 
20 #include "thermal_core.h"
21 
22 /*
23  * block registers
24  * addresses are the offset from .block_base
25  */
26 #define PVTCTLEN			0x0000
27 #define PVTCTLEN_EN			BIT(0)
28 
29 #define PVTCTLMODE			0x0004
30 #define PVTCTLMODE_MASK			0xf
31 #define PVTCTLMODE_TEMPMON		0x5
32 
33 #define EMONREPEAT			0x0040
34 #define EMONREPEAT_ENDLESS		BIT(24)
35 #define EMONREPEAT_PERIOD		GENMASK(3, 0)
36 #define EMONREPEAT_PERIOD_1000000	0x9
37 
38 /*
39  * common registers
40  * addresses are the offset from .map_base
41  */
42 #define PVTCTLSEL			0x0900
43 #define PVTCTLSEL_MASK			GENMASK(2, 0)
44 #define PVTCTLSEL_MONITOR		0
45 
46 #define SETALERT0			0x0910
47 #define SETALERT1			0x0914
48 #define SETALERT2			0x0918
49 #define SETALERT_TEMP_OVF		(GENMASK(7, 0) << 16)
50 #define SETALERT_TEMP_OVF_VALUE(val)	(((val) & GENMASK(7, 0)) << 16)
51 #define SETALERT_EN			BIT(0)
52 
53 #define PMALERTINTCTL			0x0920
54 #define PMALERTINTCTL_CLR(ch)		BIT(4 * (ch) + 2)
55 #define PMALERTINTCTL_SET(ch)		BIT(4 * (ch) + 1)
56 #define PMALERTINTCTL_EN(ch)		BIT(4 * (ch) + 0)
57 #define PMALERTINTCTL_MASK		(GENMASK(10, 8) | GENMASK(6, 4) | \
58 					 GENMASK(2, 0))
59 
60 #define TMOD				0x0928
61 #define TMOD_WIDTH			9
62 
63 #define TMODCOEF			0x0e5c
64 
65 #define TMODSETUP0_EN			BIT(30)
66 #define TMODSETUP0_VAL(val)		(((val) & GENMASK(13, 0)) << 16)
67 #define TMODSETUP1_EN			BIT(15)
68 #define TMODSETUP1_VAL(val)		((val) & GENMASK(14, 0))
69 
70 /* SoC critical temperature */
71 #define CRITICAL_TEMP_LIMIT		(120 * 1000)
72 
73 /* Max # of alert channels */
74 #define ALERT_CH_NUM			3
75 
76 /* SoC specific thermal sensor data */
77 struct uniphier_tm_soc_data {
78 	u32 map_base;
79 	u32 block_base;
80 	u32 tmod_setup_addr;
81 };
82 
83 struct uniphier_tm_dev {
84 	struct regmap *regmap;
85 	struct device *dev;
86 	bool alert_en[ALERT_CH_NUM];
87 	struct thermal_zone_device *tz_dev;
88 	const struct uniphier_tm_soc_data *data;
89 };
90 
91 static int uniphier_tm_initialize_sensor(struct uniphier_tm_dev *tdev)
92 {
93 	struct regmap *map = tdev->regmap;
94 	u32 val;
95 	u32 tmod_calib[2];
96 	int ret;
97 
98 	/* stop PVT */
99 	regmap_write_bits(map, tdev->data->block_base + PVTCTLEN,
100 			  PVTCTLEN_EN, 0);
101 
102 	/*
103 	 * Since SoC has a calibrated value that was set in advance,
104 	 * TMODCOEF shows non-zero and PVT refers the value internally.
105 	 *
106 	 * If TMODCOEF shows zero, the boards don't have the calibrated
107 	 * value, and the driver has to set default value from DT.
108 	 */
109 	ret = regmap_read(map, tdev->data->map_base + TMODCOEF, &val);
110 	if (ret)
111 		return ret;
112 	if (!val) {
113 		/* look for the default values in DT */
114 		ret = of_property_read_u32_array(tdev->dev->of_node,
115 						 "socionext,tmod-calibration",
116 						 tmod_calib,
117 						 ARRAY_SIZE(tmod_calib));
118 		if (ret)
119 			return ret;
120 
121 		regmap_write(map, tdev->data->tmod_setup_addr,
122 			TMODSETUP0_EN | TMODSETUP0_VAL(tmod_calib[0]) |
123 			TMODSETUP1_EN | TMODSETUP1_VAL(tmod_calib[1]));
124 	}
125 
126 	/* select temperature mode */
127 	regmap_write_bits(map, tdev->data->block_base + PVTCTLMODE,
128 			  PVTCTLMODE_MASK, PVTCTLMODE_TEMPMON);
129 
130 	/* set monitoring period */
131 	regmap_write_bits(map, tdev->data->block_base + EMONREPEAT,
132 			  EMONREPEAT_ENDLESS | EMONREPEAT_PERIOD,
133 			  EMONREPEAT_ENDLESS | EMONREPEAT_PERIOD_1000000);
134 
135 	/* set monitor mode */
136 	regmap_write_bits(map, tdev->data->map_base + PVTCTLSEL,
137 			  PVTCTLSEL_MASK, PVTCTLSEL_MONITOR);
138 
139 	return 0;
140 }
141 
142 static void uniphier_tm_set_alert(struct uniphier_tm_dev *tdev, u32 ch,
143 				  u32 temp)
144 {
145 	struct regmap *map = tdev->regmap;
146 
147 	/* set alert temperature */
148 	regmap_write_bits(map, tdev->data->map_base + SETALERT0 + (ch << 2),
149 			  SETALERT_EN | SETALERT_TEMP_OVF,
150 			  SETALERT_EN |
151 			  SETALERT_TEMP_OVF_VALUE(temp / 1000));
152 }
153 
154 static void uniphier_tm_enable_sensor(struct uniphier_tm_dev *tdev)
155 {
156 	struct regmap *map = tdev->regmap;
157 	int i;
158 	u32 bits = 0;
159 
160 	for (i = 0; i < ALERT_CH_NUM; i++)
161 		if (tdev->alert_en[i])
162 			bits |= PMALERTINTCTL_EN(i);
163 
164 	/* enable alert interrupt */
165 	regmap_write_bits(map, tdev->data->map_base + PMALERTINTCTL,
166 			  PMALERTINTCTL_MASK, bits);
167 
168 	/* start PVT */
169 	regmap_write_bits(map, tdev->data->block_base + PVTCTLEN,
170 			  PVTCTLEN_EN, PVTCTLEN_EN);
171 
172 	usleep_range(700, 1500);	/* The spec note says at least 700us */
173 }
174 
175 static void uniphier_tm_disable_sensor(struct uniphier_tm_dev *tdev)
176 {
177 	struct regmap *map = tdev->regmap;
178 
179 	/* disable alert interrupt */
180 	regmap_write_bits(map, tdev->data->map_base + PMALERTINTCTL,
181 			  PMALERTINTCTL_MASK, 0);
182 
183 	/* stop PVT */
184 	regmap_write_bits(map, tdev->data->block_base + PVTCTLEN,
185 			  PVTCTLEN_EN, 0);
186 
187 	usleep_range(1000, 2000);	/* The spec note says at least 1ms */
188 }
189 
190 static int uniphier_tm_get_temp(struct thermal_zone_device *tz, int *out_temp)
191 {
192 	struct uniphier_tm_dev *tdev = tz->devdata;
193 	struct regmap *map = tdev->regmap;
194 	int ret;
195 	u32 temp;
196 
197 	ret = regmap_read(map, tdev->data->map_base + TMOD, &temp);
198 	if (ret)
199 		return ret;
200 
201 	/* MSB of the TMOD field is a sign bit */
202 	*out_temp = sign_extend32(temp, TMOD_WIDTH - 1) * 1000;
203 
204 	return 0;
205 }
206 
207 static const struct thermal_zone_device_ops uniphier_of_thermal_ops = {
208 	.get_temp = uniphier_tm_get_temp,
209 };
210 
211 static void uniphier_tm_irq_clear(struct uniphier_tm_dev *tdev)
212 {
213 	u32 mask = 0, bits = 0;
214 	int i;
215 
216 	for (i = 0; i < ALERT_CH_NUM; i++) {
217 		mask |= (PMALERTINTCTL_CLR(i) | PMALERTINTCTL_SET(i));
218 		bits |= PMALERTINTCTL_CLR(i);
219 	}
220 
221 	/* clear alert interrupt */
222 	regmap_write_bits(tdev->regmap,
223 			  tdev->data->map_base + PMALERTINTCTL, mask, bits);
224 }
225 
226 static irqreturn_t uniphier_tm_alarm_irq(int irq, void *_tdev)
227 {
228 	struct uniphier_tm_dev *tdev = _tdev;
229 
230 	disable_irq_nosync(irq);
231 	uniphier_tm_irq_clear(tdev);
232 
233 	return IRQ_WAKE_THREAD;
234 }
235 
236 static irqreturn_t uniphier_tm_alarm_irq_thread(int irq, void *_tdev)
237 {
238 	struct uniphier_tm_dev *tdev = _tdev;
239 
240 	thermal_zone_device_update(tdev->tz_dev, THERMAL_EVENT_UNSPECIFIED);
241 
242 	return IRQ_HANDLED;
243 }
244 
245 static int uniphier_tm_probe(struct platform_device *pdev)
246 {
247 	struct device *dev = &pdev->dev;
248 	struct regmap *regmap;
249 	struct device_node *parent;
250 	struct uniphier_tm_dev *tdev;
251 	int i, ret, irq, crit_temp = INT_MAX;
252 
253 	tdev = devm_kzalloc(dev, sizeof(*tdev), GFP_KERNEL);
254 	if (!tdev)
255 		return -ENOMEM;
256 	tdev->dev = dev;
257 
258 	tdev->data = of_device_get_match_data(dev);
259 	if (WARN_ON(!tdev->data))
260 		return -EINVAL;
261 
262 	irq = platform_get_irq(pdev, 0);
263 	if (irq < 0)
264 		return irq;
265 
266 	/* get regmap from syscon node */
267 	parent = of_get_parent(dev->of_node); /* parent should be syscon node */
268 	regmap = syscon_node_to_regmap(parent);
269 	of_node_put(parent);
270 	if (IS_ERR(regmap)) {
271 		dev_err(dev, "failed to get regmap (error %ld)\n",
272 			PTR_ERR(regmap));
273 		return PTR_ERR(regmap);
274 	}
275 	tdev->regmap = regmap;
276 
277 	ret = uniphier_tm_initialize_sensor(tdev);
278 	if (ret) {
279 		dev_err(dev, "failed to initialize sensor\n");
280 		return ret;
281 	}
282 
283 	ret = devm_request_threaded_irq(dev, irq, uniphier_tm_alarm_irq,
284 					uniphier_tm_alarm_irq_thread,
285 					0, "thermal", tdev);
286 	if (ret)
287 		return ret;
288 
289 	platform_set_drvdata(pdev, tdev);
290 
291 	tdev->tz_dev = devm_thermal_of_zone_register(dev, 0, tdev,
292 						     &uniphier_of_thermal_ops);
293 	if (IS_ERR(tdev->tz_dev)) {
294 		dev_err(dev, "failed to register sensor device\n");
295 		return PTR_ERR(tdev->tz_dev);
296 	}
297 
298 	/* set alert temperatures */
299 	for (i = 0; i < thermal_zone_get_num_trips(tdev->tz_dev); i++) {
300 		struct thermal_trip trip;
301 
302 		ret = thermal_zone_get_trip(tdev->tz_dev, i, &trip);
303 		if (ret)
304 			return ret;
305 
306 		if (trip.type == THERMAL_TRIP_CRITICAL &&
307 		    trip.temperature < crit_temp)
308 			crit_temp = trip.temperature;
309 		uniphier_tm_set_alert(tdev, i, trip.temperature);
310 		tdev->alert_en[i] = true;
311 	}
312 	if (crit_temp > CRITICAL_TEMP_LIMIT) {
313 		dev_err(dev, "critical trip is over limit(>%d), or not set\n",
314 			CRITICAL_TEMP_LIMIT);
315 		return -EINVAL;
316 	}
317 
318 	uniphier_tm_enable_sensor(tdev);
319 
320 	return 0;
321 }
322 
323 static int uniphier_tm_remove(struct platform_device *pdev)
324 {
325 	struct uniphier_tm_dev *tdev = platform_get_drvdata(pdev);
326 
327 	/* disable sensor */
328 	uniphier_tm_disable_sensor(tdev);
329 
330 	return 0;
331 }
332 
333 static const struct uniphier_tm_soc_data uniphier_pxs2_tm_data = {
334 	.map_base        = 0xe000,
335 	.block_base      = 0xe000,
336 	.tmod_setup_addr = 0xe904,
337 };
338 
339 static const struct uniphier_tm_soc_data uniphier_ld20_tm_data = {
340 	.map_base        = 0xe000,
341 	.block_base      = 0xe800,
342 	.tmod_setup_addr = 0xe938,
343 };
344 
345 static const struct of_device_id uniphier_tm_dt_ids[] = {
346 	{
347 		.compatible = "socionext,uniphier-pxs2-thermal",
348 		.data       = &uniphier_pxs2_tm_data,
349 	},
350 	{
351 		.compatible = "socionext,uniphier-ld20-thermal",
352 		.data       = &uniphier_ld20_tm_data,
353 	},
354 	{
355 		.compatible = "socionext,uniphier-pxs3-thermal",
356 		.data       = &uniphier_ld20_tm_data,
357 	},
358 	{
359 		.compatible = "socionext,uniphier-nx1-thermal",
360 		.data       = &uniphier_ld20_tm_data,
361 	},
362 	{ /* sentinel */ }
363 };
364 MODULE_DEVICE_TABLE(of, uniphier_tm_dt_ids);
365 
366 static struct platform_driver uniphier_tm_driver = {
367 	.probe = uniphier_tm_probe,
368 	.remove = uniphier_tm_remove,
369 	.driver = {
370 		.name = "uniphier-thermal",
371 		.of_match_table = uniphier_tm_dt_ids,
372 	},
373 };
374 module_platform_driver(uniphier_tm_driver);
375 
376 MODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>");
377 MODULE_DESCRIPTION("UniPhier thermal driver");
378 MODULE_LICENSE("GPL v2");
379