bcm2835_thermal.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. #include "../thermal_hwmon.h"
  29. #define BCM2835_TS_TSENSCTL 0x00
  30. #define BCM2835_TS_TSENSSTAT 0x04
  31. #define BCM2835_TS_TSENSCTL_PRWDW BIT(0)
  32. #define BCM2835_TS_TSENSCTL_RSTB BIT(1)
  33. /*
  34. * bandgap reference voltage in 6 mV increments
  35. * 000b = 1178 mV, 001b = 1184 mV, ... 111b = 1220 mV
  36. */
  37. #define BCM2835_TS_TSENSCTL_CTRL_BITS 3
  38. #define BCM2835_TS_TSENSCTL_CTRL_SHIFT 2
  39. #define BCM2835_TS_TSENSCTL_CTRL_MASK \
  40. GENMASK(BCM2835_TS_TSENSCTL_CTRL_BITS + \
  41. BCM2835_TS_TSENSCTL_CTRL_SHIFT - 1, \
  42. BCM2835_TS_TSENSCTL_CTRL_SHIFT)
  43. #define BCM2835_TS_TSENSCTL_CTRL_DEFAULT 1
  44. #define BCM2835_TS_TSENSCTL_EN_INT BIT(5)
  45. #define BCM2835_TS_TSENSCTL_DIRECT BIT(6)
  46. #define BCM2835_TS_TSENSCTL_CLR_INT BIT(7)
  47. #define BCM2835_TS_TSENSCTL_THOLD_SHIFT 8
  48. #define BCM2835_TS_TSENSCTL_THOLD_BITS 10
  49. #define BCM2835_TS_TSENSCTL_THOLD_MASK \
  50. GENMASK(BCM2835_TS_TSENSCTL_THOLD_BITS + \
  51. BCM2835_TS_TSENSCTL_THOLD_SHIFT - 1, \
  52. BCM2835_TS_TSENSCTL_THOLD_SHIFT)
  53. /*
  54. * time how long the block to be asserted in reset
  55. * which based on a clock counter (TSENS clock assumed)
  56. */
  57. #define BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT 18
  58. #define BCM2835_TS_TSENSCTL_RSTDELAY_BITS 8
  59. #define BCM2835_TS_TSENSCTL_REGULEN BIT(26)
  60. #define BCM2835_TS_TSENSSTAT_DATA_BITS 10
  61. #define BCM2835_TS_TSENSSTAT_DATA_SHIFT 0
  62. #define BCM2835_TS_TSENSSTAT_DATA_MASK \
  63. GENMASK(BCM2835_TS_TSENSSTAT_DATA_BITS + \
  64. BCM2835_TS_TSENSSTAT_DATA_SHIFT - 1, \
  65. BCM2835_TS_TSENSSTAT_DATA_SHIFT)
  66. #define BCM2835_TS_TSENSSTAT_VALID BIT(10)
  67. #define BCM2835_TS_TSENSSTAT_INTERRUPT BIT(11)
  68. struct bcm2835_thermal_data {
  69. struct thermal_zone_device *tz;
  70. void __iomem *regs;
  71. struct clk *clk;
  72. struct dentry *debugfsdir;
  73. };
  74. static int bcm2835_thermal_adc2temp(u32 adc, int offset, int slope)
  75. {
  76. return offset + slope * adc;
  77. }
  78. static int bcm2835_thermal_temp2adc(int temp, int offset, int slope)
  79. {
  80. temp -= offset;
  81. temp /= slope;
  82. if (temp < 0)
  83. temp = 0;
  84. if (temp >= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS))
  85. temp = BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1;
  86. return temp;
  87. }
  88. static int bcm2835_thermal_get_temp(void *d, int *temp)
  89. {
  90. struct bcm2835_thermal_data *data = d;
  91. u32 val = readl(data->regs + BCM2835_TS_TSENSSTAT);
  92. if (!(val & BCM2835_TS_TSENSSTAT_VALID))
  93. return -EIO;
  94. val &= BCM2835_TS_TSENSSTAT_DATA_MASK;
  95. *temp = bcm2835_thermal_adc2temp(
  96. val,
  97. thermal_zone_get_offset(data->tz),
  98. thermal_zone_get_slope(data->tz));
  99. return 0;
  100. }
  101. static const struct debugfs_reg32 bcm2835_thermal_regs[] = {
  102. {
  103. .name = "ctl",
  104. .offset = 0
  105. },
  106. {
  107. .name = "stat",
  108. .offset = 4
  109. }
  110. };
  111. static void bcm2835_thermal_debugfs(struct platform_device *pdev)
  112. {
  113. struct bcm2835_thermal_data *data = platform_get_drvdata(pdev);
  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 const 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 %lu Hz is outside of the recommended range: 1.92 to 5MHz\n",
  186. data->clk, rate);
  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. dev_err(&pdev->dev,
  215. "Not able to read trip_temp: %d\n",
  216. err);
  217. goto err_tz;
  218. }
  219. /* set bandgap reference voltage and enable voltage regulator */
  220. val = (BCM2835_TS_TSENSCTL_CTRL_DEFAULT <<
  221. BCM2835_TS_TSENSCTL_CTRL_SHIFT) |
  222. BCM2835_TS_TSENSCTL_REGULEN;
  223. /* use the recommended reset duration */
  224. val |= (0xFE << BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT);
  225. /* trip_adc value from info */
  226. val |= bcm2835_thermal_temp2adc(trip_temp,
  227. offset,
  228. slope)
  229. << BCM2835_TS_TSENSCTL_THOLD_SHIFT;
  230. /* write the value back to the register as 2 steps */
  231. writel(val, data->regs + BCM2835_TS_TSENSCTL);
  232. val |= BCM2835_TS_TSENSCTL_RSTB;
  233. writel(val, data->regs + BCM2835_TS_TSENSCTL);
  234. }
  235. data->tz = tz;
  236. platform_set_drvdata(pdev, data);
  237. /*
  238. * Thermal_zone doesn't enable hwmon as default,
  239. * enable it here
  240. */
  241. tz->tzp->no_hwmon = false;
  242. err = thermal_add_hwmon_sysfs(tz);
  243. if (err)
  244. goto err_tz;
  245. bcm2835_thermal_debugfs(pdev);
  246. return 0;
  247. err_tz:
  248. thermal_zone_of_sensor_unregister(&pdev->dev, tz);
  249. err_clk:
  250. clk_disable_unprepare(data->clk);
  251. return err;
  252. }
  253. static int bcm2835_thermal_remove(struct platform_device *pdev)
  254. {
  255. struct bcm2835_thermal_data *data = platform_get_drvdata(pdev);
  256. struct thermal_zone_device *tz = data->tz;
  257. debugfs_remove_recursive(data->debugfsdir);
  258. thermal_zone_of_sensor_unregister(&pdev->dev, tz);
  259. clk_disable_unprepare(data->clk);
  260. return 0;
  261. }
  262. static struct platform_driver bcm2835_thermal_driver = {
  263. .probe = bcm2835_thermal_probe,
  264. .remove = bcm2835_thermal_remove,
  265. .driver = {
  266. .name = "bcm2835_thermal",
  267. .of_match_table = bcm2835_thermal_of_match_table,
  268. },
  269. };
  270. module_platform_driver(bcm2835_thermal_driver);
  271. MODULE_AUTHOR("Martin Sperl");
  272. MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
  273. MODULE_LICENSE("GPL");