bcm2835_thermal.c 7.7 KB

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