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