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