bcm2835_thermal.c 8.2 KB

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