xref: /linux/drivers/thermal/broadcom/brcmstb_thermal.c (revision c4dde411bc366f568dbe33366253bbfea049e8ea)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Broadcom STB AVS TMON thermal sensor driver
4  *
5  * Copyright (c) 2015-2017 Broadcom
6  */
7 
8 #define DRV_NAME	"brcmstb_thermal"
9 
10 #define pr_fmt(fmt)	DRV_NAME ": " fmt
11 
12 #include <linux/bitops.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/io.h>
16 #include <linux/irqreturn.h>
17 #include <linux/interrupt.h>
18 #include <linux/kernel.h>
19 #include <linux/minmax.h>
20 #include <linux/module.h>
21 #include <linux/of.h>
22 #include <linux/platform_device.h>
23 #include <linux/thermal.h>
24 
25 #define AVS_TMON_STATUS			0x00
26  #define AVS_TMON_STATUS_valid_msk	BIT(11)
27  #define AVS_TMON_STATUS_data_msk	GENMASK(10, 1)
28  #define AVS_TMON_STATUS_data_shift	1
29 
30 #define AVS_TMON_EN_OVERTEMP_RESET	0x04
31  #define AVS_TMON_EN_OVERTEMP_RESET_msk	BIT(0)
32 
33 #define AVS_TMON_RESET_THRESH		0x08
34  #define AVS_TMON_RESET_THRESH_msk	GENMASK(10, 1)
35  #define AVS_TMON_RESET_THRESH_shift	1
36 
37 #define AVS_TMON_INT_IDLE_TIME		0x10
38 
39 #define AVS_TMON_EN_TEMP_INT_SRCS	0x14
40  #define AVS_TMON_EN_TEMP_INT_SRCS_high	BIT(1)
41  #define AVS_TMON_EN_TEMP_INT_SRCS_low	BIT(0)
42 
43 #define AVS_TMON_INT_THRESH		0x18
44  #define AVS_TMON_INT_THRESH_high_msk	GENMASK(26, 17)
45  #define AVS_TMON_INT_THRESH_high_shift	17
46  #define AVS_TMON_INT_THRESH_low_msk	GENMASK(10, 1)
47  #define AVS_TMON_INT_THRESH_low_shift	1
48 
49 #define AVS_TMON_TEMP_INT_CODE		0x1c
50 #define AVS_TMON_TP_TEST_ENABLE		0x20
51 
52 /* Default coefficients */
53 #define AVS_TMON_TEMP_SLOPE		487
54 #define AVS_TMON_TEMP_OFFSET		410040
55 
56 /* HW related temperature constants */
57 #define AVS_TMON_TEMP_MAX		0x3ff
58 #define AVS_TMON_TEMP_MIN		-88161
59 #define AVS_TMON_TEMP_MASK		AVS_TMON_TEMP_MAX
60 
61 enum avs_tmon_trip_type {
62 	TMON_TRIP_TYPE_LOW = 0,
63 	TMON_TRIP_TYPE_HIGH,
64 	TMON_TRIP_TYPE_RESET,
65 	TMON_TRIP_TYPE_MAX,
66 };
67 
68 struct avs_tmon_trip {
69 	/* HW bit to enable the trip */
70 	u32 enable_offs;
71 	u32 enable_mask;
72 
73 	/* HW field to read the trip temperature */
74 	u32 reg_offs;
75 	u32 reg_msk;
76 	int reg_shift;
77 };
78 
79 static struct avs_tmon_trip avs_tmon_trips[] = {
80 	/* Trips when temperature is below threshold */
81 	[TMON_TRIP_TYPE_LOW] = {
82 		.enable_offs	= AVS_TMON_EN_TEMP_INT_SRCS,
83 		.enable_mask	= AVS_TMON_EN_TEMP_INT_SRCS_low,
84 		.reg_offs	= AVS_TMON_INT_THRESH,
85 		.reg_msk	= AVS_TMON_INT_THRESH_low_msk,
86 		.reg_shift	= AVS_TMON_INT_THRESH_low_shift,
87 	},
88 	/* Trips when temperature is above threshold */
89 	[TMON_TRIP_TYPE_HIGH] = {
90 		.enable_offs	= AVS_TMON_EN_TEMP_INT_SRCS,
91 		.enable_mask	= AVS_TMON_EN_TEMP_INT_SRCS_high,
92 		.reg_offs	= AVS_TMON_INT_THRESH,
93 		.reg_msk	= AVS_TMON_INT_THRESH_high_msk,
94 		.reg_shift	= AVS_TMON_INT_THRESH_high_shift,
95 	},
96 	/* Automatically resets chip when above threshold */
97 	[TMON_TRIP_TYPE_RESET] = {
98 		.enable_offs	= AVS_TMON_EN_OVERTEMP_RESET,
99 		.enable_mask	= AVS_TMON_EN_OVERTEMP_RESET_msk,
100 		.reg_offs	= AVS_TMON_RESET_THRESH,
101 		.reg_msk	= AVS_TMON_RESET_THRESH_msk,
102 		.reg_shift	= AVS_TMON_RESET_THRESH_shift,
103 	},
104 };
105 
106 struct brcmstb_thermal_params {
107 	unsigned int offset;
108 	unsigned int mult;
109 	const struct thermal_zone_device_ops *of_ops;
110 };
111 
112 struct brcmstb_thermal_priv {
113 	void __iomem *tmon_base;
114 	struct device *dev;
115 	struct thermal_zone_device *thermal;
116 	/* Process specific thermal parameters used for calculations */
117 	const struct brcmstb_thermal_params *temp_params;
118 };
119 
120 /* Convert a HW code to a temperature reading (millidegree celsius) */
121 static inline int avs_tmon_code_to_temp(struct brcmstb_thermal_priv *priv,
122 					u32 code)
123 {
124 	int offset = priv->temp_params->offset;
125 	int mult = priv->temp_params->mult;
126 
127 	return (offset - (int)((code & AVS_TMON_TEMP_MASK) * mult));
128 }
129 
130 /*
131  * Convert a temperature value (millidegree celsius) to a HW code
132  *
133  * @temp: temperature to convert
134  * @low: if true, round toward the low side
135  */
136 static inline u32 avs_tmon_temp_to_code(struct brcmstb_thermal_priv *priv,
137 					int temp, bool low)
138 {
139 	int offset = priv->temp_params->offset;
140 	int mult = priv->temp_params->mult;
141 
142 	if (temp < AVS_TMON_TEMP_MIN)
143 		return AVS_TMON_TEMP_MAX;	/* Maximum code value */
144 
145 	if (temp >= offset)
146 		return 0;	/* Minimum code value */
147 
148 	if (low)
149 		return (u32)(DIV_ROUND_UP(offset - temp, mult));
150 	else
151 		return (u32)((offset - temp) / mult);
152 }
153 
154 static int brcmstb_get_temp(struct thermal_zone_device *tz, int *temp)
155 {
156 	struct brcmstb_thermal_priv *priv = thermal_zone_device_priv(tz);
157 	u32 val;
158 	int t;
159 
160 	val = __raw_readl(priv->tmon_base + AVS_TMON_STATUS);
161 
162 	if (!(val & AVS_TMON_STATUS_valid_msk))
163 		return -EIO;
164 
165 	val = (val & AVS_TMON_STATUS_data_msk) >> AVS_TMON_STATUS_data_shift;
166 
167 	t = avs_tmon_code_to_temp(priv, val);
168 	*temp = max(0, t);
169 
170 	return 0;
171 }
172 
173 static void avs_tmon_trip_enable(struct brcmstb_thermal_priv *priv,
174 				 enum avs_tmon_trip_type type, int en)
175 {
176 	struct avs_tmon_trip *trip = &avs_tmon_trips[type];
177 	u32 val = __raw_readl(priv->tmon_base + trip->enable_offs);
178 
179 	dev_dbg(priv->dev, "%sable trip, type %d\n", en ? "en" : "dis", type);
180 
181 	if (en)
182 		val |= trip->enable_mask;
183 	else
184 		val &= ~trip->enable_mask;
185 
186 	__raw_writel(val, priv->tmon_base + trip->enable_offs);
187 }
188 
189 static int avs_tmon_get_trip_temp(struct brcmstb_thermal_priv *priv,
190 				  enum avs_tmon_trip_type type)
191 {
192 	struct avs_tmon_trip *trip = &avs_tmon_trips[type];
193 	u32 val = __raw_readl(priv->tmon_base + trip->reg_offs);
194 
195 	val &= trip->reg_msk;
196 	val >>= trip->reg_shift;
197 
198 	return avs_tmon_code_to_temp(priv, val);
199 }
200 
201 static void avs_tmon_set_trip_temp(struct brcmstb_thermal_priv *priv,
202 				   enum avs_tmon_trip_type type,
203 				   int temp)
204 {
205 	struct avs_tmon_trip *trip = &avs_tmon_trips[type];
206 	u32 val, orig;
207 
208 	dev_dbg(priv->dev, "set temp %d to %d\n", type, temp);
209 
210 	/* round toward low temp for the low interrupt */
211 	val = avs_tmon_temp_to_code(priv, temp,
212 				    type == TMON_TRIP_TYPE_LOW);
213 
214 	val <<= trip->reg_shift;
215 	val &= trip->reg_msk;
216 
217 	orig = __raw_readl(priv->tmon_base + trip->reg_offs);
218 	orig &= ~trip->reg_msk;
219 	orig |= val;
220 	__raw_writel(orig, priv->tmon_base + trip->reg_offs);
221 }
222 
223 static int avs_tmon_get_intr_temp(struct brcmstb_thermal_priv *priv)
224 {
225 	u32 val;
226 
227 	val = __raw_readl(priv->tmon_base + AVS_TMON_TEMP_INT_CODE);
228 	return avs_tmon_code_to_temp(priv, val);
229 }
230 
231 static irqreturn_t brcmstb_tmon_irq_thread(int irq, void *data)
232 {
233 	struct brcmstb_thermal_priv *priv = data;
234 	int low, high, intr;
235 
236 	low = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_LOW);
237 	high = avs_tmon_get_trip_temp(priv, TMON_TRIP_TYPE_HIGH);
238 	intr = avs_tmon_get_intr_temp(priv);
239 
240 	dev_dbg(priv->dev, "low/intr/high: %d/%d/%d\n",
241 			low, intr, high);
242 
243 	/* Disable high-temp until next threshold shift */
244 	if (intr >= high)
245 		avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
246 	/* Disable low-temp until next threshold shift */
247 	if (intr <= low)
248 		avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
249 
250 	/*
251 	 * Notify using the interrupt temperature, in case the temperature
252 	 * changes before it can next be read out
253 	 */
254 	thermal_zone_device_update(priv->thermal, intr);
255 
256 	return IRQ_HANDLED;
257 }
258 
259 static int brcmstb_set_trips(struct thermal_zone_device *tz, int low, int high)
260 {
261 	struct brcmstb_thermal_priv *priv = thermal_zone_device_priv(tz);
262 
263 	dev_dbg(priv->dev, "set trips %d <--> %d\n", low, high);
264 
265 	/*
266 	 * Disable low-temp if "low" is too small. As per thermal framework
267 	 * API, we use -INT_MAX rather than INT_MIN.
268 	 */
269 	if (low <= -INT_MAX) {
270 		avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 0);
271 	} else {
272 		avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_LOW, low);
273 		avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_LOW, 1);
274 	}
275 
276 	/* Disable high-temp if "high" is too big. */
277 	if (high == INT_MAX) {
278 		avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 0);
279 	} else {
280 		avs_tmon_set_trip_temp(priv, TMON_TRIP_TYPE_HIGH, high);
281 		avs_tmon_trip_enable(priv, TMON_TRIP_TYPE_HIGH, 1);
282 	}
283 
284 	return 0;
285 }
286 
287 static const struct thermal_zone_device_ops brcmstb_of_ops = {
288 	.get_temp	= brcmstb_get_temp,
289 };
290 
291 static const struct brcmstb_thermal_params brcmstb_8nm_params = {
292 	.offset	= 418670,
293 	.mult	= 509,
294 	.of_ops	= &brcmstb_of_ops,
295 };
296 
297 static const struct brcmstb_thermal_params brcmstb_16nm_params = {
298 	.offset	= 457829,
299 	.mult	= 557,
300 	.of_ops	= &brcmstb_of_ops,
301 };
302 
303 static const struct thermal_zone_device_ops brcmstb_28nm_of_ops = {
304 	.get_temp	= brcmstb_get_temp,
305 	.set_trips	= brcmstb_set_trips,
306 };
307 
308 static const struct brcmstb_thermal_params brcmstb_28nm_params = {
309 	.offset	= 410040,
310 	.mult	= 487,
311 	.of_ops	= &brcmstb_28nm_of_ops,
312 };
313 
314 static const struct of_device_id brcmstb_thermal_id_table[] = {
315 	{ .compatible = "brcm,avs-tmon-bcm74110", .data = &brcmstb_8nm_params },
316 	{ .compatible = "brcm,avs-tmon-bcm7216", .data = &brcmstb_16nm_params },
317 	{ .compatible = "brcm,avs-tmon", .data = &brcmstb_28nm_params },
318 	{},
319 };
320 MODULE_DEVICE_TABLE(of, brcmstb_thermal_id_table);
321 
322 static int brcmstb_thermal_probe(struct platform_device *pdev)
323 {
324 	const struct thermal_zone_device_ops *of_ops;
325 	struct thermal_zone_device *thermal;
326 	struct brcmstb_thermal_priv *priv;
327 	int irq, ret;
328 
329 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
330 	if (!priv)
331 		return -ENOMEM;
332 
333 	priv->temp_params = of_device_get_match_data(&pdev->dev);
334 	if (!priv->temp_params)
335 		return -EINVAL;
336 
337 	priv->tmon_base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
338 	if (IS_ERR(priv->tmon_base))
339 		return PTR_ERR(priv->tmon_base);
340 
341 	priv->dev = &pdev->dev;
342 	of_ops = priv->temp_params->of_ops;
343 
344 	thermal = devm_thermal_of_zone_register(&pdev->dev, 0, priv,
345 						of_ops);
346 	if (IS_ERR(thermal))
347 		return dev_err_probe(&pdev->dev, PTR_ERR(thermal),
348 					"could not register sensor\n");
349 
350 	priv->thermal = thermal;
351 
352 	irq = platform_get_irq_optional(pdev, 0);
353 	if (irq >= 0) {
354 		ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
355 						brcmstb_tmon_irq_thread,
356 						IRQF_ONESHOT,
357 						DRV_NAME, priv);
358 		if (ret < 0)
359 			return dev_err_probe(&pdev->dev, ret,
360 						"could not request IRQ\n");
361 	}
362 
363 	dev_info(&pdev->dev, "registered AVS TMON of-sensor driver\n");
364 
365 	return 0;
366 }
367 
368 static struct platform_driver brcmstb_thermal_driver = {
369 	.probe = brcmstb_thermal_probe,
370 	.driver = {
371 		.name = DRV_NAME,
372 		.of_match_table = brcmstb_thermal_id_table,
373 	},
374 };
375 module_platform_driver(brcmstb_thermal_driver);
376 
377 MODULE_LICENSE("GPL v2");
378 MODULE_AUTHOR("Brian Norris");
379 MODULE_DESCRIPTION("Broadcom STB AVS TMON thermal driver");
380