hisi_thermal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Hisilicon thermal sensor driver
  3. *
  4. * Copyright (c) 2014-2015 Hisilicon Limited.
  5. * Copyright (c) 2014-2015 Linaro Limited.
  6. *
  7. * Xinwei Kong <kong.kongxinwei@hisilicon.com>
  8. * Leo Yan <leo.yan@linaro.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  15. * kind, whether express or implied; without even the implied warranty
  16. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/cpufreq.h>
  20. #include <linux/delay.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include "thermal_core.h"
  26. #define TEMP0_LAG (0x0)
  27. #define TEMP0_TH (0x4)
  28. #define TEMP0_RST_TH (0x8)
  29. #define TEMP0_CFG (0xC)
  30. #define TEMP0_CFG_SS_MSK (0xF000)
  31. #define TEMP0_CFG_HDAK_MSK (0x30)
  32. #define TEMP0_EN (0x10)
  33. #define TEMP0_INT_EN (0x14)
  34. #define TEMP0_INT_CLR (0x18)
  35. #define TEMP0_RST_MSK (0x1C)
  36. #define TEMP0_VALUE (0x28)
  37. #define HISI_TEMP_BASE (-60000)
  38. #define HISI_TEMP_RESET (100000)
  39. #define HISI_TEMP_STEP (784)
  40. #define HISI_TEMP_LAG (3500)
  41. #define HISI_MAX_SENSORS 4
  42. #define HISI_DEFAULT_SENSOR 2
  43. struct hisi_thermal_sensor {
  44. struct hisi_thermal_data *thermal;
  45. struct thermal_zone_device *tzd;
  46. long sensor_temp;
  47. uint32_t id;
  48. uint32_t thres_temp;
  49. };
  50. struct hisi_thermal_data {
  51. struct mutex thermal_lock; /* protects register data */
  52. struct platform_device *pdev;
  53. struct clk *clk;
  54. struct hisi_thermal_sensor sensors;
  55. int irq;
  56. void __iomem *regs;
  57. };
  58. /*
  59. * The temperature computation on the tsensor is as follow:
  60. * Unit: millidegree Celsius
  61. * Step: 255/200 (0.7843)
  62. * Temperature base: -60°C
  63. *
  64. * The register is programmed in temperature steps, every step is 784
  65. * millidegree and begins at -60 000 m°C
  66. *
  67. * The temperature from the steps:
  68. *
  69. * Temp = TempBase + (steps x 784)
  70. *
  71. * and the steps from the temperature:
  72. *
  73. * steps = (Temp - TempBase) / 784
  74. *
  75. */
  76. static inline int hisi_thermal_step_to_temp(int step)
  77. {
  78. return HISI_TEMP_BASE + (step * HISI_TEMP_STEP);
  79. }
  80. static inline long hisi_thermal_temp_to_step(long temp)
  81. {
  82. return (temp - HISI_TEMP_BASE) / HISI_TEMP_STEP;
  83. }
  84. static inline long hisi_thermal_round_temp(int temp)
  85. {
  86. return hisi_thermal_step_to_temp(
  87. hisi_thermal_temp_to_step(temp));
  88. }
  89. /*
  90. * The lag register contains 5 bits encoding the temperature in steps.
  91. *
  92. * Each time the temperature crosses the threshold boundary, an
  93. * interrupt is raised. It could be when the temperature is going
  94. * above the threshold or below. However, if the temperature is
  95. * fluctuating around this value due to the load, we can receive
  96. * several interrupts which may not desired.
  97. *
  98. * We can setup a temperature representing the delta between the
  99. * threshold and the current temperature when the temperature is
  100. * decreasing.
  101. *
  102. * For instance: the lag register is 5°C, the threshold is 65°C, when
  103. * the temperature reaches 65°C an interrupt is raised and when the
  104. * temperature decrease to 65°C - 5°C another interrupt is raised.
  105. *
  106. * A very short lag can lead to an interrupt storm, a long lag
  107. * increase the latency to react to the temperature changes. In our
  108. * case, that is not really a problem as we are polling the
  109. * temperature.
  110. *
  111. * [0:4] : lag register
  112. *
  113. * The temperature is coded in steps, cf. HISI_TEMP_STEP.
  114. *
  115. * Min : 0x00 : 0.0 °C
  116. * Max : 0x1F : 24.3 °C
  117. *
  118. * The 'value' parameter is in milliCelsius.
  119. */
  120. static inline void hisi_thermal_set_lag(void __iomem *addr, int value)
  121. {
  122. writel((value / HISI_TEMP_STEP) & 0x1F, addr + TEMP0_LAG);
  123. }
  124. static inline void hisi_thermal_alarm_clear(void __iomem *addr, int value)
  125. {
  126. writel(value, addr + TEMP0_INT_CLR);
  127. }
  128. static inline void hisi_thermal_alarm_enable(void __iomem *addr, int value)
  129. {
  130. writel(value, addr + TEMP0_INT_EN);
  131. }
  132. static inline void hisi_thermal_alarm_set(void __iomem *addr, int temp)
  133. {
  134. writel(hisi_thermal_temp_to_step(temp) | 0x0FFFFFF00, addr + TEMP0_TH);
  135. }
  136. static inline void hisi_thermal_reset_set(void __iomem *addr, int temp)
  137. {
  138. writel(hisi_thermal_temp_to_step(temp), addr + TEMP0_RST_TH);
  139. }
  140. static inline void hisi_thermal_reset_enable(void __iomem *addr, int value)
  141. {
  142. writel(value, addr + TEMP0_RST_MSK);
  143. }
  144. static inline void hisi_thermal_enable(void __iomem *addr, int value)
  145. {
  146. writel(value, addr + TEMP0_EN);
  147. }
  148. static inline int hisi_thermal_get_temperature(void __iomem *addr)
  149. {
  150. return hisi_thermal_step_to_temp(readl(addr + TEMP0_VALUE));
  151. }
  152. /*
  153. * Temperature configuration register - Sensor selection
  154. *
  155. * Bits [19:12]
  156. *
  157. * 0x0: local sensor (default)
  158. * 0x1: remote sensor 1 (ACPU cluster 1)
  159. * 0x2: remote sensor 2 (ACPU cluster 0)
  160. * 0x3: remote sensor 3 (G3D)
  161. */
  162. static inline void hisi_thermal_sensor_select(void __iomem *addr, int sensor)
  163. {
  164. writel((readl(addr + TEMP0_CFG) & ~TEMP0_CFG_SS_MSK) |
  165. (sensor << 12), addr + TEMP0_CFG);
  166. }
  167. /*
  168. * Temperature configuration register - Hdak conversion polling interval
  169. *
  170. * Bits [5:4]
  171. *
  172. * 0x0 : 0.768 ms
  173. * 0x1 : 6.144 ms
  174. * 0x2 : 49.152 ms
  175. * 0x3 : 393.216 ms
  176. */
  177. static inline void hisi_thermal_hdak_set(void __iomem *addr, int value)
  178. {
  179. writel((readl(addr + TEMP0_CFG) & ~TEMP0_CFG_HDAK_MSK) |
  180. (value << 4), addr + TEMP0_CFG);
  181. }
  182. static void hisi_thermal_disable_sensor(struct hisi_thermal_data *data)
  183. {
  184. mutex_lock(&data->thermal_lock);
  185. /* disable sensor module */
  186. hisi_thermal_enable(data->regs, 0);
  187. hisi_thermal_alarm_enable(data->regs, 0);
  188. hisi_thermal_reset_enable(data->regs, 0);
  189. mutex_unlock(&data->thermal_lock);
  190. }
  191. static int hisi_thermal_get_temp(void *_sensor, int *temp)
  192. {
  193. struct hisi_thermal_sensor *sensor = _sensor;
  194. struct hisi_thermal_data *data = sensor->thermal;
  195. *temp = hisi_thermal_get_temperature(data->regs);
  196. dev_dbg(&data->pdev->dev, "id=%d, temp=%d, thres=%d\n",
  197. sensor->id, *temp, sensor->thres_temp);
  198. return 0;
  199. }
  200. static const struct thermal_zone_of_device_ops hisi_of_thermal_ops = {
  201. .get_temp = hisi_thermal_get_temp,
  202. };
  203. static irqreturn_t hisi_thermal_alarm_irq_thread(int irq, void *dev)
  204. {
  205. struct hisi_thermal_data *data = dev;
  206. struct hisi_thermal_sensor *sensor = &data->sensors;
  207. int temp;
  208. hisi_thermal_alarm_clear(data->regs, 1);
  209. temp = hisi_thermal_get_temperature(data->regs);
  210. if (temp >= sensor->thres_temp) {
  211. dev_crit(&data->pdev->dev, "THERMAL ALARM: %d > %d\n",
  212. temp, sensor->thres_temp);
  213. thermal_zone_device_update(data->sensors.tzd,
  214. THERMAL_EVENT_UNSPECIFIED);
  215. } else if (temp < sensor->thres_temp) {
  216. dev_crit(&data->pdev->dev, "THERMAL ALARM stopped: %d < %d\n",
  217. temp, sensor->thres_temp);
  218. }
  219. return IRQ_HANDLED;
  220. }
  221. static int hisi_thermal_register_sensor(struct platform_device *pdev,
  222. struct hisi_thermal_data *data,
  223. struct hisi_thermal_sensor *sensor,
  224. int index)
  225. {
  226. int ret, i;
  227. const struct thermal_trip *trip;
  228. sensor->id = index;
  229. sensor->thermal = data;
  230. sensor->tzd = devm_thermal_zone_of_sensor_register(&pdev->dev,
  231. sensor->id, sensor, &hisi_of_thermal_ops);
  232. if (IS_ERR(sensor->tzd)) {
  233. ret = PTR_ERR(sensor->tzd);
  234. sensor->tzd = NULL;
  235. dev_err(&pdev->dev, "failed to register sensor id %d: %d\n",
  236. sensor->id, ret);
  237. return ret;
  238. }
  239. trip = of_thermal_get_trip_points(sensor->tzd);
  240. for (i = 0; i < of_thermal_get_ntrips(sensor->tzd); i++) {
  241. if (trip[i].type == THERMAL_TRIP_PASSIVE) {
  242. sensor->thres_temp = hisi_thermal_round_temp(trip[i].temperature);
  243. break;
  244. }
  245. }
  246. return 0;
  247. }
  248. static const struct of_device_id of_hisi_thermal_match[] = {
  249. { .compatible = "hisilicon,tsensor" },
  250. { /* end */ }
  251. };
  252. MODULE_DEVICE_TABLE(of, of_hisi_thermal_match);
  253. static void hisi_thermal_toggle_sensor(struct hisi_thermal_sensor *sensor,
  254. bool on)
  255. {
  256. struct thermal_zone_device *tzd = sensor->tzd;
  257. tzd->ops->set_mode(tzd,
  258. on ? THERMAL_DEVICE_ENABLED : THERMAL_DEVICE_DISABLED);
  259. }
  260. static int hisi_thermal_setup(struct hisi_thermal_data *data)
  261. {
  262. struct hisi_thermal_sensor *sensor;
  263. sensor = &data->sensors;
  264. /* disable module firstly */
  265. hisi_thermal_reset_enable(data->regs, 0);
  266. hisi_thermal_enable(data->regs, 0);
  267. /* select sensor id */
  268. hisi_thermal_sensor_select(data->regs, sensor->id);
  269. /* setting the hdak time */
  270. hisi_thermal_hdak_set(data->regs, 0);
  271. /* setting lag value between current temp and the threshold */
  272. hisi_thermal_set_lag(data->regs, HISI_TEMP_LAG);
  273. /* enable for interrupt */
  274. hisi_thermal_alarm_set(data->regs, sensor->thres_temp);
  275. hisi_thermal_reset_set(data->regs, HISI_TEMP_RESET);
  276. /* enable module */
  277. hisi_thermal_reset_enable(data->regs, 1);
  278. hisi_thermal_enable(data->regs, 1);
  279. hisi_thermal_alarm_clear(data->regs, 0);
  280. hisi_thermal_alarm_enable(data->regs, 1);
  281. return 0;
  282. }
  283. static int hisi_thermal_probe(struct platform_device *pdev)
  284. {
  285. struct hisi_thermal_data *data;
  286. struct resource *res;
  287. int ret;
  288. data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
  289. if (!data)
  290. return -ENOMEM;
  291. mutex_init(&data->thermal_lock);
  292. data->pdev = pdev;
  293. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  294. data->regs = devm_ioremap_resource(&pdev->dev, res);
  295. if (IS_ERR(data->regs)) {
  296. dev_err(&pdev->dev, "failed to get io address\n");
  297. return PTR_ERR(data->regs);
  298. }
  299. data->irq = platform_get_irq(pdev, 0);
  300. if (data->irq < 0)
  301. return data->irq;
  302. platform_set_drvdata(pdev, data);
  303. data->clk = devm_clk_get(&pdev->dev, "thermal_clk");
  304. if (IS_ERR(data->clk)) {
  305. ret = PTR_ERR(data->clk);
  306. if (ret != -EPROBE_DEFER)
  307. dev_err(&pdev->dev,
  308. "failed to get thermal clk: %d\n", ret);
  309. return ret;
  310. }
  311. /* enable clock for thermal */
  312. ret = clk_prepare_enable(data->clk);
  313. if (ret) {
  314. dev_err(&pdev->dev, "failed to enable thermal clk: %d\n", ret);
  315. return ret;
  316. }
  317. ret = hisi_thermal_register_sensor(pdev, data,
  318. &data->sensors,
  319. HISI_DEFAULT_SENSOR);
  320. if (ret) {
  321. dev_err(&pdev->dev, "failed to register thermal sensor: %d\n",
  322. ret);
  323. return ret;
  324. }
  325. ret = hisi_thermal_setup(data);
  326. if (ret) {
  327. dev_err(&pdev->dev, "Failed to setup the sensor: %d\n", ret);
  328. return ret;
  329. }
  330. ret = devm_request_threaded_irq(&pdev->dev, data->irq, NULL,
  331. hisi_thermal_alarm_irq_thread,
  332. IRQF_ONESHOT, "hisi_thermal", data);
  333. if (ret < 0) {
  334. dev_err(&pdev->dev, "failed to request alarm irq: %d\n", ret);
  335. return ret;
  336. }
  337. hisi_thermal_toggle_sensor(&data->sensors, true);
  338. return 0;
  339. }
  340. static int hisi_thermal_remove(struct platform_device *pdev)
  341. {
  342. struct hisi_thermal_data *data = platform_get_drvdata(pdev);
  343. struct hisi_thermal_sensor *sensor = &data->sensors;
  344. hisi_thermal_toggle_sensor(sensor, false);
  345. hisi_thermal_disable_sensor(data);
  346. clk_disable_unprepare(data->clk);
  347. return 0;
  348. }
  349. #ifdef CONFIG_PM_SLEEP
  350. static int hisi_thermal_suspend(struct device *dev)
  351. {
  352. struct hisi_thermal_data *data = dev_get_drvdata(dev);
  353. hisi_thermal_disable_sensor(data);
  354. clk_disable_unprepare(data->clk);
  355. return 0;
  356. }
  357. static int hisi_thermal_resume(struct device *dev)
  358. {
  359. struct hisi_thermal_data *data = dev_get_drvdata(dev);
  360. int ret;
  361. ret = clk_prepare_enable(data->clk);
  362. if (ret)
  363. return ret;
  364. hisi_thermal_setup(data);
  365. return 0;
  366. }
  367. #endif
  368. static SIMPLE_DEV_PM_OPS(hisi_thermal_pm_ops,
  369. hisi_thermal_suspend, hisi_thermal_resume);
  370. static struct platform_driver hisi_thermal_driver = {
  371. .driver = {
  372. .name = "hisi_thermal",
  373. .pm = &hisi_thermal_pm_ops,
  374. .of_match_table = of_hisi_thermal_match,
  375. },
  376. .probe = hisi_thermal_probe,
  377. .remove = hisi_thermal_remove,
  378. };
  379. module_platform_driver(hisi_thermal_driver);
  380. MODULE_AUTHOR("Xinwei Kong <kong.kongxinwei@hisilicon.com>");
  381. MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");
  382. MODULE_DESCRIPTION("Hisilicon thermal driver");
  383. MODULE_LICENSE("GPL v2");