bcm2835_thermal.c 7.8 KB

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