1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Broadcom BCM2835 SoC temperature sensor
5 * Copyright (C) 2016 Martin Sperl
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/thermal.h>
21 #include "../thermal_hwmon.h"
23 #define BCM2835_TS_TSENSCTL 0x00
24 #define BCM2835_TS_TSENSSTAT 0x04
26 #define BCM2835_TS_TSENSCTL_PRWDW BIT(0)
27 #define BCM2835_TS_TSENSCTL_RSTB BIT(1)
30 * bandgap reference voltage in 6 mV increments
31 * 000b = 1178 mV, 001b = 1184 mV, ... 111b = 1220 mV
33 #define BCM2835_TS_TSENSCTL_CTRL_BITS 3
34 #define BCM2835_TS_TSENSCTL_CTRL_SHIFT 2
35 #define BCM2835_TS_TSENSCTL_CTRL_MASK \
36 GENMASK(BCM2835_TS_TSENSCTL_CTRL_BITS + \
37 BCM2835_TS_TSENSCTL_CTRL_SHIFT - 1, \
38 BCM2835_TS_TSENSCTL_CTRL_SHIFT)
39 #define BCM2835_TS_TSENSCTL_CTRL_DEFAULT 1
40 #define BCM2835_TS_TSENSCTL_EN_INT BIT(5)
41 #define BCM2835_TS_TSENSCTL_DIRECT BIT(6)
42 #define BCM2835_TS_TSENSCTL_CLR_INT BIT(7)
43 #define BCM2835_TS_TSENSCTL_THOLD_SHIFT 8
44 #define BCM2835_TS_TSENSCTL_THOLD_BITS 10
45 #define BCM2835_TS_TSENSCTL_THOLD_MASK \
46 GENMASK(BCM2835_TS_TSENSCTL_THOLD_BITS + \
47 BCM2835_TS_TSENSCTL_THOLD_SHIFT - 1, \
48 BCM2835_TS_TSENSCTL_THOLD_SHIFT)
50 * time how long the block to be asserted in reset
51 * which based on a clock counter (TSENS clock assumed)
53 #define BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT 18
54 #define BCM2835_TS_TSENSCTL_RSTDELAY_BITS 8
55 #define BCM2835_TS_TSENSCTL_REGULEN BIT(26)
57 #define BCM2835_TS_TSENSSTAT_DATA_BITS 10
58 #define BCM2835_TS_TSENSSTAT_DATA_SHIFT 0
59 #define BCM2835_TS_TSENSSTAT_DATA_MASK \
60 GENMASK(BCM2835_TS_TSENSSTAT_DATA_BITS + \
61 BCM2835_TS_TSENSSTAT_DATA_SHIFT - 1, \
62 BCM2835_TS_TSENSSTAT_DATA_SHIFT)
63 #define BCM2835_TS_TSENSSTAT_VALID BIT(10)
64 #define BCM2835_TS_TSENSSTAT_INTERRUPT BIT(11)
66 struct bcm2835_thermal_data
{
67 struct thermal_zone_device
*tz
;
70 struct dentry
*debugfsdir
;
73 static int bcm2835_thermal_adc2temp(u32 adc
, int offset
, int slope
)
75 return offset
+ slope
* adc
;
78 static int bcm2835_thermal_temp2adc(int temp
, int offset
, int slope
)
85 if (temp
>= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS
))
86 temp
= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS
) - 1;
91 static int bcm2835_thermal_get_temp(struct thermal_zone_device
*tz
, int *temp
)
93 struct bcm2835_thermal_data
*data
= thermal_zone_device_priv(tz
);
94 u32 val
= readl(data
->regs
+ BCM2835_TS_TSENSSTAT
);
96 if (!(val
& BCM2835_TS_TSENSSTAT_VALID
))
99 val
&= BCM2835_TS_TSENSSTAT_DATA_MASK
;
101 *temp
= bcm2835_thermal_adc2temp(
103 thermal_zone_get_offset(data
->tz
),
104 thermal_zone_get_slope(data
->tz
));
109 static const struct debugfs_reg32 bcm2835_thermal_regs
[] = {
120 static void bcm2835_thermal_debugfs(struct platform_device
*pdev
)
122 struct bcm2835_thermal_data
*data
= platform_get_drvdata(pdev
);
123 struct debugfs_regset32
*regset
;
125 data
->debugfsdir
= debugfs_create_dir("bcm2835_thermal", NULL
);
127 regset
= devm_kzalloc(&pdev
->dev
, sizeof(*regset
), GFP_KERNEL
);
131 regset
->regs
= bcm2835_thermal_regs
;
132 regset
->nregs
= ARRAY_SIZE(bcm2835_thermal_regs
);
133 regset
->base
= data
->regs
;
135 debugfs_create_regset32("regset", 0444, data
->debugfsdir
, regset
);
138 static const struct thermal_zone_device_ops bcm2835_thermal_ops
= {
139 .get_temp
= bcm2835_thermal_get_temp
,
143 * Note: as per Raspberry Foundation FAQ
144 * (https://www.raspberrypi.org/help/faqs/#performanceOperatingTemperature)
145 * the recommended temperature range for the SoC -40C to +85C
146 * so the trip limit is set to 80C.
147 * this applies to all the BCM283X SoC
150 static const struct of_device_id bcm2835_thermal_of_match_table
[] = {
152 .compatible
= "brcm,bcm2835-thermal",
155 .compatible
= "brcm,bcm2836-thermal",
158 .compatible
= "brcm,bcm2837-thermal",
162 MODULE_DEVICE_TABLE(of
, bcm2835_thermal_of_match_table
);
164 static int bcm2835_thermal_probe(struct platform_device
*pdev
)
166 struct device
*dev
= &pdev
->dev
;
167 const struct of_device_id
*match
;
168 struct thermal_zone_device
*tz
;
169 struct bcm2835_thermal_data
*data
;
174 data
= devm_kzalloc(dev
, sizeof(*data
), GFP_KERNEL
);
178 match
= of_match_device(bcm2835_thermal_of_match_table
, dev
);
182 data
->regs
= devm_platform_get_and_ioremap_resource(pdev
, 0, NULL
);
183 if (IS_ERR(data
->regs
)) {
184 err
= PTR_ERR(data
->regs
);
188 data
->clk
= devm_clk_get_enabled(dev
, NULL
);
189 if (IS_ERR(data
->clk
))
190 return dev_err_probe(dev
, PTR_ERR(data
->clk
), "Could not get clk\n");
192 rate
= clk_get_rate(data
->clk
);
193 if ((rate
< 1920000) || (rate
> 5000000))
195 "Clock %pCn running at %lu Hz is outside of the recommended range: 1.92 to 5MHz\n",
198 /* register of thermal sensor and get info from DT */
199 tz
= devm_thermal_of_zone_register(dev
, 0, data
, &bcm2835_thermal_ops
);
201 return dev_err_probe(dev
, PTR_ERR(tz
), "Failed to register the thermal device\n");
204 * right now the FW does set up the HW-block, so we are not
205 * touching the configuration registers.
206 * But if the HW is not enabled, then set it up
207 * using "sane" values used by the firmware right now.
209 val
= readl(data
->regs
+ BCM2835_TS_TSENSCTL
);
210 if (!(val
& BCM2835_TS_TSENSCTL_RSTB
)) {
211 int offset
, slope
, crit_temp
;
213 slope
= thermal_zone_get_slope(tz
);
214 offset
= thermal_zone_get_offset(tz
);
216 * For now we deal only with critical, otherwise
217 * would need to iterate
219 err
= thermal_zone_get_crit_temp(tz
, &crit_temp
);
221 dev_err(dev
, "Not able to read trip_temp: %d\n", err
);
225 /* set bandgap reference voltage and enable voltage regulator */
226 val
= (BCM2835_TS_TSENSCTL_CTRL_DEFAULT
<<
227 BCM2835_TS_TSENSCTL_CTRL_SHIFT
) |
228 BCM2835_TS_TSENSCTL_REGULEN
;
230 /* use the recommended reset duration */
231 val
|= (0xFE << BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT
);
233 /* trip_adc value from info */
234 val
|= bcm2835_thermal_temp2adc(crit_temp
,
237 << BCM2835_TS_TSENSCTL_THOLD_SHIFT
;
239 /* write the value back to the register as 2 steps */
240 writel(val
, data
->regs
+ BCM2835_TS_TSENSCTL
);
241 val
|= BCM2835_TS_TSENSCTL_RSTB
;
242 writel(val
, data
->regs
+ BCM2835_TS_TSENSCTL
);
247 platform_set_drvdata(pdev
, data
);
250 * Thermal_zone doesn't enable hwmon as default,
253 err
= thermal_add_hwmon_sysfs(tz
);
257 bcm2835_thermal_debugfs(pdev
);
262 static void bcm2835_thermal_remove(struct platform_device
*pdev
)
264 struct bcm2835_thermal_data
*data
= platform_get_drvdata(pdev
);
266 debugfs_remove_recursive(data
->debugfsdir
);
269 static struct platform_driver bcm2835_thermal_driver
= {
270 .probe
= bcm2835_thermal_probe
,
271 .remove
= bcm2835_thermal_remove
,
273 .name
= "bcm2835_thermal",
274 .of_match_table
= bcm2835_thermal_of_match_table
,
277 module_platform_driver(bcm2835_thermal_driver
);
279 MODULE_AUTHOR("Martin Sperl");
280 MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
281 MODULE_LICENSE("GPL");