2 * Driver for Broadcom BCM2835 SoC temperature sensor
4 * Copyright (C) 2016 Martin Sperl
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/clk.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/err.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/platform_device.h>
28 #include <linux/thermal.h>
30 #include "../thermal_hwmon.h"
32 #define BCM2835_TS_TSENSCTL 0x00
33 #define BCM2835_TS_TSENSSTAT 0x04
35 #define BCM2835_TS_TSENSCTL_PRWDW BIT(0)
36 #define BCM2835_TS_TSENSCTL_RSTB BIT(1)
39 * bandgap reference voltage in 6 mV increments
40 * 000b = 1178 mV, 001b = 1184 mV, ... 111b = 1220 mV
42 #define BCM2835_TS_TSENSCTL_CTRL_BITS 3
43 #define BCM2835_TS_TSENSCTL_CTRL_SHIFT 2
44 #define BCM2835_TS_TSENSCTL_CTRL_MASK \
45 GENMASK(BCM2835_TS_TSENSCTL_CTRL_BITS + \
46 BCM2835_TS_TSENSCTL_CTRL_SHIFT - 1, \
47 BCM2835_TS_TSENSCTL_CTRL_SHIFT)
48 #define BCM2835_TS_TSENSCTL_CTRL_DEFAULT 1
49 #define BCM2835_TS_TSENSCTL_EN_INT BIT(5)
50 #define BCM2835_TS_TSENSCTL_DIRECT BIT(6)
51 #define BCM2835_TS_TSENSCTL_CLR_INT BIT(7)
52 #define BCM2835_TS_TSENSCTL_THOLD_SHIFT 8
53 #define BCM2835_TS_TSENSCTL_THOLD_BITS 10
54 #define BCM2835_TS_TSENSCTL_THOLD_MASK \
55 GENMASK(BCM2835_TS_TSENSCTL_THOLD_BITS + \
56 BCM2835_TS_TSENSCTL_THOLD_SHIFT - 1, \
57 BCM2835_TS_TSENSCTL_THOLD_SHIFT)
59 * time how long the block to be asserted in reset
60 * which based on a clock counter (TSENS clock assumed)
62 #define BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT 18
63 #define BCM2835_TS_TSENSCTL_RSTDELAY_BITS 8
64 #define BCM2835_TS_TSENSCTL_REGULEN BIT(26)
66 #define BCM2835_TS_TSENSSTAT_DATA_BITS 10
67 #define BCM2835_TS_TSENSSTAT_DATA_SHIFT 0
68 #define BCM2835_TS_TSENSSTAT_DATA_MASK \
69 GENMASK(BCM2835_TS_TSENSSTAT_DATA_BITS + \
70 BCM2835_TS_TSENSSTAT_DATA_SHIFT - 1, \
71 BCM2835_TS_TSENSSTAT_DATA_SHIFT)
72 #define BCM2835_TS_TSENSSTAT_VALID BIT(10)
73 #define BCM2835_TS_TSENSSTAT_INTERRUPT BIT(11)
75 struct bcm2835_thermal_data {
76 struct thermal_zone_device *tz;
79 struct dentry *debugfsdir;
82 static int bcm2835_thermal_adc2temp(u32 adc, int offset, int slope)
84 return offset + slope * adc;
87 static int bcm2835_thermal_temp2adc(int temp, int offset, int slope)
94 if (temp >= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS))
95 temp = BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1;
100 static int bcm2835_thermal_get_temp(void *d, int *temp)
102 struct bcm2835_thermal_data *data = d;
103 u32 val = readl(data->regs + BCM2835_TS_TSENSSTAT);
105 if (!(val & BCM2835_TS_TSENSSTAT_VALID))
108 val &= BCM2835_TS_TSENSSTAT_DATA_MASK;
110 *temp = bcm2835_thermal_adc2temp(
112 thermal_zone_get_offset(data->tz),
113 thermal_zone_get_slope(data->tz));
118 static const struct debugfs_reg32 bcm2835_thermal_regs[] = {
129 static void bcm2835_thermal_debugfs(struct platform_device *pdev)
131 struct bcm2835_thermal_data *data = platform_get_drvdata(pdev);
132 struct debugfs_regset32 *regset;
134 data->debugfsdir = debugfs_create_dir("bcm2835_thermal", NULL);
135 if (!data->debugfsdir)
138 regset = devm_kzalloc(&pdev->dev, sizeof(*regset), GFP_KERNEL);
142 regset->regs = bcm2835_thermal_regs;
143 regset->nregs = ARRAY_SIZE(bcm2835_thermal_regs);
144 regset->base = data->regs;
146 debugfs_create_regset32("regset", 0444, data->debugfsdir, regset);
149 static const struct thermal_zone_of_device_ops bcm2835_thermal_ops = {
150 .get_temp = bcm2835_thermal_get_temp,
154 * Note: as per Raspberry Foundation FAQ
155 * (https://www.raspberrypi.org/help/faqs/#performanceOperatingTemperature)
156 * the recommended temperature range for the SoC -40C to +85C
157 * so the trip limit is set to 80C.
158 * this applies to all the BCM283X SoC
161 static const struct of_device_id bcm2835_thermal_of_match_table[] = {
163 .compatible = "brcm,bcm2835-thermal",
166 .compatible = "brcm,bcm2836-thermal",
169 .compatible = "brcm,bcm2837-thermal",
173 MODULE_DEVICE_TABLE(of, bcm2835_thermal_of_match_table);
175 static int bcm2835_thermal_probe(struct platform_device *pdev)
177 const struct of_device_id *match;
178 struct thermal_zone_device *tz;
179 struct bcm2835_thermal_data *data;
180 struct resource *res;
185 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
189 match = of_match_device(bcm2835_thermal_of_match_table,
194 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
195 data->regs = devm_ioremap_resource(&pdev->dev, res);
196 if (IS_ERR(data->regs)) {
197 err = PTR_ERR(data->regs);
198 dev_err(&pdev->dev, "Could not get registers: %d\n", err);
202 data->clk = devm_clk_get(&pdev->dev, NULL);
203 if (IS_ERR(data->clk)) {
204 err = PTR_ERR(data->clk);
205 if (err != -EPROBE_DEFER)
206 dev_err(&pdev->dev, "Could not get clk: %d\n", err);
210 err = clk_prepare_enable(data->clk);
214 rate = clk_get_rate(data->clk);
215 if ((rate < 1920000) || (rate > 5000000))
217 "Clock %pCn running at %lu Hz is outside of the recommended range: 1.92 to 5MHz\n",
220 /* register of thermal sensor and get info from DT */
221 tz = thermal_zone_of_sensor_register(&pdev->dev, 0, data,
222 &bcm2835_thermal_ops);
226 "Failed to register the thermal device: %d\n",
232 * right now the FW does set up the HW-block, so we are not
233 * touching the configuration registers.
234 * But if the HW is not enabled, then set it up
235 * using "sane" values used by the firmware right now.
237 val = readl(data->regs + BCM2835_TS_TSENSCTL);
238 if (!(val & BCM2835_TS_TSENSCTL_RSTB)) {
239 int trip_temp, offset, slope;
241 slope = thermal_zone_get_slope(tz);
242 offset = thermal_zone_get_offset(tz);
244 * For now we deal only with critical, otherwise
245 * would need to iterate
247 err = tz->ops->get_trip_temp(tz, 0, &trip_temp);
250 "Not able to read trip_temp: %d\n",
255 /* set bandgap reference voltage and enable voltage regulator */
256 val = (BCM2835_TS_TSENSCTL_CTRL_DEFAULT <<
257 BCM2835_TS_TSENSCTL_CTRL_SHIFT) |
258 BCM2835_TS_TSENSCTL_REGULEN;
260 /* use the recommended reset duration */
261 val |= (0xFE << BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT);
263 /* trip_adc value from info */
264 val |= bcm2835_thermal_temp2adc(trip_temp,
267 << BCM2835_TS_TSENSCTL_THOLD_SHIFT;
269 /* write the value back to the register as 2 steps */
270 writel(val, data->regs + BCM2835_TS_TSENSCTL);
271 val |= BCM2835_TS_TSENSCTL_RSTB;
272 writel(val, data->regs + BCM2835_TS_TSENSCTL);
277 platform_set_drvdata(pdev, data);
280 * Thermal_zone doesn't enable hwmon as default,
283 tz->tzp->no_hwmon = false;
284 err = thermal_add_hwmon_sysfs(tz);
288 bcm2835_thermal_debugfs(pdev);
292 thermal_zone_of_sensor_unregister(&pdev->dev, tz);
294 clk_disable_unprepare(data->clk);
299 static int bcm2835_thermal_remove(struct platform_device *pdev)
301 struct bcm2835_thermal_data *data = platform_get_drvdata(pdev);
302 struct thermal_zone_device *tz = data->tz;
304 debugfs_remove_recursive(data->debugfsdir);
305 thermal_zone_of_sensor_unregister(&pdev->dev, tz);
306 clk_disable_unprepare(data->clk);
311 static struct platform_driver bcm2835_thermal_driver = {
312 .probe = bcm2835_thermal_probe,
313 .remove = bcm2835_thermal_remove,
315 .name = "bcm2835_thermal",
316 .of_match_table = bcm2835_thermal_of_match_table,
319 module_platform_driver(bcm2835_thermal_driver);
321 MODULE_AUTHOR("Martin Sperl");
322 MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
323 MODULE_LICENSE("GPL");