xref: /linux/drivers/thermal/broadcom/bcm2835_thermal.c (revision 6f7e6393d1ce636bb7ec77a7fe7b77458fddf701)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Broadcom BCM2835 SoC temperature sensor
4  *
5  * Copyright (C) 2016 Martin Sperl
6  */
7 
8 #include <linux/clk.h>
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/io.h>
13 #include <linux/kernel.h>
14 #include <linux/minmax.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.h>
20 #include <linux/thermal.h>
21 
22 #include "../thermal_hwmon.h"
23 
24 #define BCM2835_TS_TSENSCTL			0x00
25 #define BCM2835_TS_TSENSSTAT			0x04
26 
27 #define BCM2835_TS_TSENSCTL_PRWDW		BIT(0)
28 #define BCM2835_TS_TSENSCTL_RSTB		BIT(1)
29 
30 /*
31  * bandgap reference voltage in 6 mV increments
32  * 000b = 1178 mV, 001b = 1184 mV, ... 111b = 1220 mV
33  */
34 #define BCM2835_TS_TSENSCTL_CTRL_BITS		3
35 #define BCM2835_TS_TSENSCTL_CTRL_SHIFT		2
36 #define BCM2835_TS_TSENSCTL_CTRL_MASK		    \
37 	GENMASK(BCM2835_TS_TSENSCTL_CTRL_BITS +     \
38 		BCM2835_TS_TSENSCTL_CTRL_SHIFT - 1, \
39 		BCM2835_TS_TSENSCTL_CTRL_SHIFT)
40 #define BCM2835_TS_TSENSCTL_CTRL_DEFAULT	1
41 #define BCM2835_TS_TSENSCTL_EN_INT		BIT(5)
42 #define BCM2835_TS_TSENSCTL_DIRECT		BIT(6)
43 #define BCM2835_TS_TSENSCTL_CLR_INT		BIT(7)
44 #define BCM2835_TS_TSENSCTL_THOLD_SHIFT		8
45 #define BCM2835_TS_TSENSCTL_THOLD_BITS		10
46 #define BCM2835_TS_TSENSCTL_THOLD_MASK		     \
47 	GENMASK(BCM2835_TS_TSENSCTL_THOLD_BITS +     \
48 		BCM2835_TS_TSENSCTL_THOLD_SHIFT - 1, \
49 		BCM2835_TS_TSENSCTL_THOLD_SHIFT)
50 /*
51  * time how long the block to be asserted in reset
52  * which based on a clock counter (TSENS clock assumed)
53  */
54 #define BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT	18
55 #define BCM2835_TS_TSENSCTL_RSTDELAY_BITS	8
56 #define BCM2835_TS_TSENSCTL_REGULEN		BIT(26)
57 
58 #define BCM2835_TS_TSENSSTAT_DATA_BITS		10
59 #define BCM2835_TS_TSENSSTAT_DATA_SHIFT		0
60 #define BCM2835_TS_TSENSSTAT_DATA_MASK		     \
61 	GENMASK(BCM2835_TS_TSENSSTAT_DATA_BITS +     \
62 		BCM2835_TS_TSENSSTAT_DATA_SHIFT - 1, \
63 		BCM2835_TS_TSENSSTAT_DATA_SHIFT)
64 #define BCM2835_TS_TSENSSTAT_VALID		BIT(10)
65 #define BCM2835_TS_TSENSSTAT_INTERRUPT		BIT(11)
66 
67 struct bcm2835_thermal_data {
68 	struct thermal_zone_device *tz;
69 	void __iomem *regs;
70 	struct clk *clk;
71 	struct dentry *debugfsdir;
72 };
73 
74 static int bcm2835_thermal_adc2temp(u32 adc, int offset, int slope)
75 {
76 	return offset + slope * adc;
77 }
78 
79 static int bcm2835_thermal_temp2adc(int temp, int offset, int slope)
80 {
81 	temp -= offset;
82 	temp /= slope;
83 
84 	return clamp(temp, 0, (int)BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1);
85 }
86 
87 static int bcm2835_thermal_get_temp(struct thermal_zone_device *tz, int *temp)
88 {
89 	struct bcm2835_thermal_data *data = thermal_zone_device_priv(tz);
90 	u32 val = readl(data->regs + BCM2835_TS_TSENSSTAT);
91 
92 	if (!(val & BCM2835_TS_TSENSSTAT_VALID))
93 		return -EIO;
94 
95 	val &= BCM2835_TS_TSENSSTAT_DATA_MASK;
96 
97 	*temp = bcm2835_thermal_adc2temp(
98 		val,
99 		thermal_zone_get_offset(data->tz),
100 		thermal_zone_get_slope(data->tz));
101 
102 	return 0;
103 }
104 
105 static const struct debugfs_reg32 bcm2835_thermal_regs[] = {
106 	{
107 		.name = "ctl",
108 		.offset = 0
109 	},
110 	{
111 		.name = "stat",
112 		.offset = 4
113 	}
114 };
115 
116 static void bcm2835_thermal_debugfs(struct platform_device *pdev)
117 {
118 	struct bcm2835_thermal_data *data = platform_get_drvdata(pdev);
119 	struct debugfs_regset32 *regset;
120 
121 	data->debugfsdir = debugfs_create_dir("bcm2835_thermal", NULL);
122 
123 	regset = devm_kzalloc(&pdev->dev, sizeof(*regset), GFP_KERNEL);
124 	if (!regset)
125 		return;
126 
127 	regset->regs = bcm2835_thermal_regs;
128 	regset->nregs = ARRAY_SIZE(bcm2835_thermal_regs);
129 	regset->base = data->regs;
130 
131 	debugfs_create_regset32("regset", 0444, data->debugfsdir, regset);
132 }
133 
134 static const struct thermal_zone_device_ops bcm2835_thermal_ops = {
135 	.get_temp = bcm2835_thermal_get_temp,
136 };
137 
138 /*
139  * Note: as per Raspberry Foundation FAQ
140  * (https://www.raspberrypi.org/help/faqs/#performanceOperatingTemperature)
141  * the recommended temperature range for the SoC -40C to +85C
142  * so the trip limit is set to 80C.
143  * this applies to all the BCM283X SoC
144  */
145 
146 static const struct of_device_id bcm2835_thermal_of_match_table[] = {
147 	{
148 		.compatible = "brcm,bcm2835-thermal",
149 	},
150 	{
151 		.compatible = "brcm,bcm2836-thermal",
152 	},
153 	{
154 		.compatible = "brcm,bcm2837-thermal",
155 	},
156 	{},
157 };
158 MODULE_DEVICE_TABLE(of, bcm2835_thermal_of_match_table);
159 
160 static int bcm2835_thermal_probe(struct platform_device *pdev)
161 {
162 	struct device *dev = &pdev->dev;
163 	const struct of_device_id *match;
164 	struct thermal_zone_device *tz;
165 	struct bcm2835_thermal_data *data;
166 	int err = 0;
167 	u32 val;
168 	unsigned long rate;
169 
170 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
171 	if (!data)
172 		return -ENOMEM;
173 
174 	match = of_match_device(bcm2835_thermal_of_match_table, dev);
175 	if (!match)
176 		return -EINVAL;
177 
178 	data->regs = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
179 	if (IS_ERR(data->regs)) {
180 		err = PTR_ERR(data->regs);
181 		return err;
182 	}
183 
184 	data->clk = devm_clk_get_enabled(dev, NULL);
185 	if (IS_ERR(data->clk))
186 		return dev_err_probe(dev, PTR_ERR(data->clk), "Could not get clk\n");
187 
188 	rate = clk_get_rate(data->clk);
189 	if ((rate < 1920000) || (rate > 5000000))
190 		dev_warn(dev,
191 			 "Clock %pC running at %lu Hz is outside of the recommended range: 1.92 to 5MHz\n",
192 			 data->clk, rate);
193 
194 	/* register of thermal sensor and get info from DT */
195 	tz = devm_thermal_of_zone_register(dev, 0, data, &bcm2835_thermal_ops);
196 	if (IS_ERR(tz))
197 		return dev_err_probe(dev, PTR_ERR(tz), "Failed to register the thermal device\n");
198 
199 	/*
200 	 * right now the FW does set up the HW-block, so we are not
201 	 * touching the configuration registers.
202 	 * But if the HW is not enabled, then set it up
203 	 * using "sane" values used by the firmware right now.
204 	 */
205 	val = readl(data->regs + BCM2835_TS_TSENSCTL);
206 	if (!(val & BCM2835_TS_TSENSCTL_RSTB)) {
207 		int offset, slope, crit_temp;
208 
209 		slope = thermal_zone_get_slope(tz);
210 		offset = thermal_zone_get_offset(tz);
211 		/*
212 		 * For now we deal only with critical, otherwise
213 		 * would need to iterate
214 		 */
215 		err = thermal_zone_get_crit_temp(tz, &crit_temp);
216 		if (err < 0) {
217 			dev_err(dev, "Not able to read trip_temp: %d\n", err);
218 			return err;
219 		}
220 
221 		/* set bandgap reference voltage and enable voltage regulator */
222 		val = (BCM2835_TS_TSENSCTL_CTRL_DEFAULT <<
223 		       BCM2835_TS_TSENSCTL_CTRL_SHIFT) |
224 		      BCM2835_TS_TSENSCTL_REGULEN;
225 
226 		/* use the recommended reset duration */
227 		val |= (0xFE << BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT);
228 
229 		/*  trip_adc value from info */
230 		val |= bcm2835_thermal_temp2adc(crit_temp,
231 						offset,
232 						slope)
233 			<< BCM2835_TS_TSENSCTL_THOLD_SHIFT;
234 
235 		/* write the value back to the register as 2 steps */
236 		writel(val, data->regs + BCM2835_TS_TSENSCTL);
237 		val |= BCM2835_TS_TSENSCTL_RSTB;
238 		writel(val, data->regs + BCM2835_TS_TSENSCTL);
239 	}
240 
241 	data->tz = tz;
242 
243 	platform_set_drvdata(pdev, data);
244 
245 	/*
246 	 * Thermal_zone doesn't enable hwmon as default,
247 	 * enable it here
248 	 */
249 	err = thermal_add_hwmon_sysfs(tz);
250 	if (err)
251 		return err;
252 
253 	bcm2835_thermal_debugfs(pdev);
254 
255 	return 0;
256 }
257 
258 static void bcm2835_thermal_remove(struct platform_device *pdev)
259 {
260 	struct bcm2835_thermal_data *data = platform_get_drvdata(pdev);
261 
262 	debugfs_remove_recursive(data->debugfsdir);
263 }
264 
265 static struct platform_driver bcm2835_thermal_driver = {
266 	.probe = bcm2835_thermal_probe,
267 	.remove = bcm2835_thermal_remove,
268 	.driver = {
269 		.name = "bcm2835_thermal",
270 		.of_match_table = bcm2835_thermal_of_match_table,
271 	},
272 };
273 module_platform_driver(bcm2835_thermal_driver);
274 
275 MODULE_AUTHOR("Martin Sperl");
276 MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
277 MODULE_LICENSE("GPL");
278