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