bcm2835_thermal.c 7.3 KB

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