086-0003-thermal-bcm2835-add-thermal-driver-for-bcm2835-SoC.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. From bcb7dd9ef206f7d646ed8dac6fe7772083714253 Mon Sep 17 00:00:00 2001
  2. From: Stefan Wahren <stefan.wahren@i2se.com>
  3. Date: Fri, 31 Mar 2017 20:03:06 +0000
  4. Subject: [PATCH] thermal: bcm2835: add thermal driver for bcm2835 SoC
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Add basic thermal driver for bcm2835 SoC.
  9. This driver currently make sure that tsense HW block is set up
  10. correctly.
  11. Tested-by: Rafał Miłecki <rafal@milecki.pl>
  12. Signed-off-by: Martin Sperl <kernel@martin.sperl.org>
  13. Signed-off-by: Stefan Wahren <stefan.wahren@i2se.com>
  14. Acked-by: Eric Anholt <eric@anholt.net>
  15. Acked-by: Eduardo Valentin <edubezval@gmail.com>
  16. Signed-off-by: Eduardo Valentin <edubezval@gmail.com>
  17. ---
  18. drivers/thermal/Kconfig | 8 +
  19. drivers/thermal/Makefile | 1 +
  20. drivers/thermal/bcm2835_thermal.c | 314 ++++++++++++++++++++++++++++++++++++++
  21. 3 files changed, 323 insertions(+)
  22. create mode 100644 drivers/thermal/bcm2835_thermal.c
  23. --- a/drivers/thermal/Kconfig
  24. +++ b/drivers/thermal/Kconfig
  25. @@ -391,4 +391,12 @@ config QCOM_SPMI_TEMP_ALARM
  26. real time die temperature if an ADC is present or an estimate of the
  27. temperature based upon the over temperature stage value.
  28. +config BCM2835_THERMAL
  29. + tristate "Thermal sensors on bcm2835 SoC"
  30. + depends on ARCH_BCM2835 || COMPILE_TEST
  31. + depends on HAS_IOMEM
  32. + depends on THERMAL_OF
  33. + help
  34. + Support for thermal sensors on Broadcom bcm2835 SoCs.
  35. +
  36. endif
  37. --- a/drivers/thermal/Makefile
  38. +++ b/drivers/thermal/Makefile
  39. @@ -48,3 +48,4 @@ obj-$(CONFIG_INTEL_PCH_THERMAL) += intel
  40. obj-$(CONFIG_ST_THERMAL) += st/
  41. obj-$(CONFIG_TEGRA_SOCTHERM) += tegra_soctherm.o
  42. obj-$(CONFIG_HISI_THERMAL) += hisi_thermal.o
  43. +obj-$(CONFIG_BCM2835_THERMAL) += bcm2835_thermal.o
  44. --- /dev/null
  45. +++ b/drivers/thermal/bcm2835_thermal.c
  46. @@ -0,0 +1,314 @@
  47. +/*
  48. + * Driver for Broadcom BCM2835 SoC temperature sensor
  49. + *
  50. + * Copyright (C) 2016 Martin Sperl
  51. + *
  52. + * This program is free software; you can redistribute it and/or modify
  53. + * it under the terms of the GNU General Public License as published by
  54. + * the Free Software Foundation; either version 2 of the License, or
  55. + * (at your option) any later version.
  56. + *
  57. + * This program is distributed in the hope that it will be useful,
  58. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  59. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  60. + * GNU General Public License for more details.
  61. + */
  62. +
  63. +#include <linux/clk.h>
  64. +#include <linux/debugfs.h>
  65. +#include <linux/device.h>
  66. +#include <linux/err.h>
  67. +#include <linux/io.h>
  68. +#include <linux/kernel.h>
  69. +#include <linux/module.h>
  70. +#include <linux/of.h>
  71. +#include <linux/of_address.h>
  72. +#include <linux/of_device.h>
  73. +#include <linux/platform_device.h>
  74. +#include <linux/thermal.h>
  75. +
  76. +#define BCM2835_TS_TSENSCTL 0x00
  77. +#define BCM2835_TS_TSENSSTAT 0x04
  78. +
  79. +#define BCM2835_TS_TSENSCTL_PRWDW BIT(0)
  80. +#define BCM2835_TS_TSENSCTL_RSTB BIT(1)
  81. +
  82. +/*
  83. + * bandgap reference voltage in 6 mV increments
  84. + * 000b = 1178 mV, 001b = 1184 mV, ... 111b = 1220 mV
  85. + */
  86. +#define BCM2835_TS_TSENSCTL_CTRL_BITS 3
  87. +#define BCM2835_TS_TSENSCTL_CTRL_SHIFT 2
  88. +#define BCM2835_TS_TSENSCTL_CTRL_MASK \
  89. + GENMASK(BCM2835_TS_TSENSCTL_CTRL_BITS + \
  90. + BCM2835_TS_TSENSCTL_CTRL_SHIFT - 1, \
  91. + BCM2835_TS_TSENSCTL_CTRL_SHIFT)
  92. +#define BCM2835_TS_TSENSCTL_CTRL_DEFAULT 1
  93. +#define BCM2835_TS_TSENSCTL_EN_INT BIT(5)
  94. +#define BCM2835_TS_TSENSCTL_DIRECT BIT(6)
  95. +#define BCM2835_TS_TSENSCTL_CLR_INT BIT(7)
  96. +#define BCM2835_TS_TSENSCTL_THOLD_SHIFT 8
  97. +#define BCM2835_TS_TSENSCTL_THOLD_BITS 10
  98. +#define BCM2835_TS_TSENSCTL_THOLD_MASK \
  99. + GENMASK(BCM2835_TS_TSENSCTL_THOLD_BITS + \
  100. + BCM2835_TS_TSENSCTL_THOLD_SHIFT - 1, \
  101. + BCM2835_TS_TSENSCTL_THOLD_SHIFT)
  102. +/*
  103. + * time how long the block to be asserted in reset
  104. + * which based on a clock counter (TSENS clock assumed)
  105. + */
  106. +#define BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT 18
  107. +#define BCM2835_TS_TSENSCTL_RSTDELAY_BITS 8
  108. +#define BCM2835_TS_TSENSCTL_REGULEN BIT(26)
  109. +
  110. +#define BCM2835_TS_TSENSSTAT_DATA_BITS 10
  111. +#define BCM2835_TS_TSENSSTAT_DATA_SHIFT 0
  112. +#define BCM2835_TS_TSENSSTAT_DATA_MASK \
  113. + GENMASK(BCM2835_TS_TSENSSTAT_DATA_BITS + \
  114. + BCM2835_TS_TSENSSTAT_DATA_SHIFT - 1, \
  115. + BCM2835_TS_TSENSSTAT_DATA_SHIFT)
  116. +#define BCM2835_TS_TSENSSTAT_VALID BIT(10)
  117. +#define BCM2835_TS_TSENSSTAT_INTERRUPT BIT(11)
  118. +
  119. +struct bcm2835_thermal_data {
  120. + struct thermal_zone_device *tz;
  121. + void __iomem *regs;
  122. + struct clk *clk;
  123. + struct dentry *debugfsdir;
  124. +};
  125. +
  126. +static int bcm2835_thermal_adc2temp(u32 adc, int offset, int slope)
  127. +{
  128. + return offset + slope * adc;
  129. +}
  130. +
  131. +static int bcm2835_thermal_temp2adc(int temp, int offset, int slope)
  132. +{
  133. + temp -= offset;
  134. + temp /= slope;
  135. +
  136. + if (temp < 0)
  137. + temp = 0;
  138. + if (temp >= BIT(BCM2835_TS_TSENSSTAT_DATA_BITS))
  139. + temp = BIT(BCM2835_TS_TSENSSTAT_DATA_BITS) - 1;
  140. +
  141. + return temp;
  142. +}
  143. +
  144. +static int bcm2835_thermal_get_temp(void *d, int *temp)
  145. +{
  146. + struct bcm2835_thermal_data *data = d;
  147. + u32 val = readl(data->regs + BCM2835_TS_TSENSSTAT);
  148. +
  149. + if (!(val & BCM2835_TS_TSENSSTAT_VALID))
  150. + return -EIO;
  151. +
  152. + val &= BCM2835_TS_TSENSSTAT_DATA_MASK;
  153. +
  154. + *temp = bcm2835_thermal_adc2temp(
  155. + val,
  156. + thermal_zone_get_offset(data->tz),
  157. + thermal_zone_get_slope(data->tz));
  158. +
  159. + return 0;
  160. +}
  161. +
  162. +static const struct debugfs_reg32 bcm2835_thermal_regs[] = {
  163. + {
  164. + .name = "ctl",
  165. + .offset = 0
  166. + },
  167. + {
  168. + .name = "stat",
  169. + .offset = 4
  170. + }
  171. +};
  172. +
  173. +static void bcm2835_thermal_debugfs(struct platform_device *pdev)
  174. +{
  175. + struct thermal_zone_device *tz = platform_get_drvdata(pdev);
  176. + struct bcm2835_thermal_data *data = tz->devdata;
  177. + struct debugfs_regset32 *regset;
  178. +
  179. + data->debugfsdir = debugfs_create_dir("bcm2835_thermal", NULL);
  180. + if (!data->debugfsdir)
  181. + return;
  182. +
  183. + regset = devm_kzalloc(&pdev->dev, sizeof(*regset), GFP_KERNEL);
  184. + if (!regset)
  185. + return;
  186. +
  187. + regset->regs = bcm2835_thermal_regs;
  188. + regset->nregs = ARRAY_SIZE(bcm2835_thermal_regs);
  189. + regset->base = data->regs;
  190. +
  191. + debugfs_create_regset32("regset", 0444, data->debugfsdir, regset);
  192. +}
  193. +
  194. +static struct thermal_zone_of_device_ops bcm2835_thermal_ops = {
  195. + .get_temp = bcm2835_thermal_get_temp,
  196. +};
  197. +
  198. +/*
  199. + * Note: as per Raspberry Foundation FAQ
  200. + * (https://www.raspberrypi.org/help/faqs/#performanceOperatingTemperature)
  201. + * the recommended temperature range for the SoC -40C to +85C
  202. + * so the trip limit is set to 80C.
  203. + * this applies to all the BCM283X SoC
  204. + */
  205. +
  206. +static const struct of_device_id bcm2835_thermal_of_match_table[] = {
  207. + {
  208. + .compatible = "brcm,bcm2835-thermal",
  209. + },
  210. + {
  211. + .compatible = "brcm,bcm2836-thermal",
  212. + },
  213. + {
  214. + .compatible = "brcm,bcm2837-thermal",
  215. + },
  216. + {},
  217. +};
  218. +MODULE_DEVICE_TABLE(of, bcm2835_thermal_of_match_table);
  219. +
  220. +static int bcm2835_thermal_probe(struct platform_device *pdev)
  221. +{
  222. + const struct of_device_id *match;
  223. + struct thermal_zone_device *tz;
  224. + struct bcm2835_thermal_data *data;
  225. + struct resource *res;
  226. + int err = 0;
  227. + u32 val;
  228. + unsigned long rate;
  229. +
  230. + data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  231. + if (!data)
  232. + return -ENOMEM;
  233. +
  234. + match = of_match_device(bcm2835_thermal_of_match_table,
  235. + &pdev->dev);
  236. + if (!match)
  237. + return -EINVAL;
  238. +
  239. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  240. + data->regs = devm_ioremap_resource(&pdev->dev, res);
  241. + if (IS_ERR(data->regs)) {
  242. + err = PTR_ERR(data->regs);
  243. + dev_err(&pdev->dev, "Could not get registers: %d\n", err);
  244. + return err;
  245. + }
  246. +
  247. + data->clk = devm_clk_get(&pdev->dev, NULL);
  248. + if (IS_ERR(data->clk)) {
  249. + err = PTR_ERR(data->clk);
  250. + if (err != -EPROBE_DEFER)
  251. + dev_err(&pdev->dev, "Could not get clk: %d\n", err);
  252. + return err;
  253. + }
  254. +
  255. + err = clk_prepare_enable(data->clk);
  256. + if (err)
  257. + return err;
  258. +
  259. + rate = clk_get_rate(data->clk);
  260. + if ((rate < 1920000) || (rate > 5000000))
  261. + dev_warn(&pdev->dev,
  262. + "Clock %pCn running at %pCr Hz is outside of the recommended range: 1.92 to 5MHz\n",
  263. + data->clk, data->clk);
  264. +
  265. + /* register of thermal sensor and get info from DT */
  266. + tz = thermal_zone_of_sensor_register(&pdev->dev, 0, data,
  267. + &bcm2835_thermal_ops);
  268. + if (IS_ERR(tz)) {
  269. + err = PTR_ERR(tz);
  270. + dev_err(&pdev->dev,
  271. + "Failed to register the thermal device: %d\n",
  272. + err);
  273. + goto err_clk;
  274. + }
  275. +
  276. + /*
  277. + * right now the FW does set up the HW-block, so we are not
  278. + * touching the configuration registers.
  279. + * But if the HW is not enabled, then set it up
  280. + * using "sane" values used by the firmware right now.
  281. + */
  282. + val = readl(data->regs + BCM2835_TS_TSENSCTL);
  283. + if (!(val & BCM2835_TS_TSENSCTL_RSTB)) {
  284. + int trip_temp, offset, slope;
  285. +
  286. + slope = thermal_zone_get_slope(tz);
  287. + offset = thermal_zone_get_offset(tz);
  288. + /*
  289. + * For now we deal only with critical, otherwise
  290. + * would need to iterate
  291. + */
  292. + err = tz->ops->get_trip_temp(tz, 0, &trip_temp);
  293. + if (err < 0) {
  294. + err = PTR_ERR(tz);
  295. + dev_err(&pdev->dev,
  296. + "Not able to read trip_temp: %d\n",
  297. + err);
  298. + goto err_tz;
  299. + }
  300. +
  301. + /* set bandgap reference voltage and enable voltage regulator */
  302. + val = (BCM2835_TS_TSENSCTL_CTRL_DEFAULT <<
  303. + BCM2835_TS_TSENSCTL_CTRL_SHIFT) |
  304. + BCM2835_TS_TSENSCTL_REGULEN;
  305. +
  306. + /* use the recommended reset duration */
  307. + val |= (0xFE << BCM2835_TS_TSENSCTL_RSTDELAY_SHIFT);
  308. +
  309. + /* trip_adc value from info */
  310. + val |= bcm2835_thermal_temp2adc(trip_temp,
  311. + offset,
  312. + slope)
  313. + << BCM2835_TS_TSENSCTL_THOLD_SHIFT;
  314. +
  315. + /* write the value back to the register as 2 steps */
  316. + writel(val, data->regs + BCM2835_TS_TSENSCTL);
  317. + val |= BCM2835_TS_TSENSCTL_RSTB;
  318. + writel(val, data->regs + BCM2835_TS_TSENSCTL);
  319. + }
  320. +
  321. + data->tz = tz;
  322. +
  323. + platform_set_drvdata(pdev, tz);
  324. +
  325. + bcm2835_thermal_debugfs(pdev);
  326. +
  327. + return 0;
  328. +err_tz:
  329. + thermal_zone_of_sensor_unregister(&pdev->dev, tz);
  330. +err_clk:
  331. + clk_disable_unprepare(data->clk);
  332. +
  333. + return err;
  334. +}
  335. +
  336. +static int bcm2835_thermal_remove(struct platform_device *pdev)
  337. +{
  338. + struct thermal_zone_device *tz = platform_get_drvdata(pdev);
  339. + struct bcm2835_thermal_data *data = tz->devdata;
  340. +
  341. + debugfs_remove_recursive(data->debugfsdir);
  342. + thermal_zone_of_sensor_unregister(&pdev->dev, tz);
  343. + clk_disable_unprepare(data->clk);
  344. +
  345. + return 0;
  346. +}
  347. +
  348. +static struct platform_driver bcm2835_thermal_driver = {
  349. + .probe = bcm2835_thermal_probe,
  350. + .remove = bcm2835_thermal_remove,
  351. + .driver = {
  352. + .name = "bcm2835_thermal",
  353. + .of_match_table = bcm2835_thermal_of_match_table,
  354. + },
  355. +};
  356. +module_platform_driver(bcm2835_thermal_driver);
  357. +
  358. +MODULE_AUTHOR("Martin Sperl");
  359. +MODULE_DESCRIPTION("Thermal driver for bcm2835 chip");
  360. +MODULE_LICENSE("GPL");